RAW Socket - Ethertype and Receive Algorithm - C

I am working with a raw C socket. I need to send and receive an ethernet packet. The packet must start with an IEEE 802.3 header:

MAC DST [0-5] - MAC SRC [6-11] - ETH TYPE [12-13]

Capturing packets with wireshark I see the following structure:

MAC DST [0-5] - MAC SRC [6-11] - LENGTH [12-13] - TRAILER [14-58] -....

This is my code:

...
sraw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_802_3));
...
retVal = setsockopt(sraw, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
...
val = 3;
retVal = setsockopt(sraw, SOL_SOCKET, SO_PRIORITY, &val, sizeof (val));
...
memcpy(ptr_eth_header->DstMac, dst_mac, 6);
memcpy(ptr_eth_header->SrcMac, src_mac, 6);
ptr_eth_header->Type = htons(ETH_P_802_3);
memcpy(buffer + ETHHDR_SIZE, data, 60);
...
sockaddr.sll_family = htons(PF_PACKET);
sockaddr.sll_protocol = htons(ETH_P_802_3);
sockaddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue;
sockaddr.sll_halen = 6;
memcpy(&(sockaddr.sll_addr), dst_mac, 6);
...
bytes = sendto(sraw, buffer, sizeof(buffer), 0, (struct sockaddr *) &(sockaddr), sizeof (struct sockaddr_ll));

      

Is this just a "problem" problem? Any ideas?

My second problem is getting unprocessed messages. The process gets stuck in recvfrom.

This is my code:

sraw = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_802_3));
...
retVal = setsockopt(sraw, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
...
val = 3;
retVal = setsockopt(sraw, SOL_SOCKET, SO_PRIORITY, &val, sizeof (val));
...
val = CLIENT_PACKET_SIZE;
retVal = setsockopt(sraw, SOL_SOCKET, SO_RCVBUF, &val, sizeof (val));

sockaddr.sll_family    = htons(PF_PACKET);
sockaddr.sll_ifindex   = ifr.ifr_ifindex;
sockaddr.sll_protocol  = htons(ETH_P_802_3);

buffer = malloc(CLIENT_PACKET_SIZE * sizeof(char));
while (count < PACKET_COUNT) {
    bytes = recvfrom(sraw, buffer, CLIENT_PACKET_SIZE, 0, (struct sockaddr *)&sockaddr, (socklen_t*)sizeof(sockaddr));
    ...
}

      

could you help me?

Thanks in advance!

+3


source to share


2 answers


I found an answer about my first question: I am using Ethertype == 0x0001 instead of EtherType> = 0x0600

http://www.cavebear.com/archive/cavebear/Ethernet/type.html



How about your second question? What's wrong with my code?

+1


source


The answer to one question for your question is tricky. But if you ask me why recvfrom might get stuck with your code, I would say that you cannot receive any packet that satisfies your filter. Are you sure you are passing the ifindex value in the expected format? I see that you are passing ifindex sockaddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue; like this in sendto.

Other reasons may be: the size of the buffer you are setting on the socket is not supported by the kernel, or there are not enough buffers in the kernel, and so many other reasons. but the chances of any of them are minimal.



Also, for your scenario, I would suggest using a non-blocking socket instead of blocking. Call recvfrom only when you know there are packets waiting to be read.

0


source







All Articles