Raw Ethernet PF_PACKET issue on localhost

1.1k Views Asked by At

I am working on raw ethernet programming in c. I have two files client and server which are running on localhost. I am using my own protocol number for communication in socket().

On the client side i have follwing code

    s = socket(PF_PACKET, SOCK_RAW, 61187);  
    unsigned char dest_mac[6]= {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};  
    struct sockaddr_ll socket_address;

        socket_address.sll_family   = PF_PACKET;
        socket_address.sll_protocol = 61187;
        socket_address.sll_ifindex  = ifindex;
        socket_address.sll_hatype   = ARPHRD_ETHER;
        socket_address.sll_pkttype  = PACKET_BROADCAST;
        socket_address.sll_halen    = ETH_ALEN;
        socket_address.sll_addr[0]  = dest_mac[0];
        socket_address.sll_addr[1]  = dest_mac[1];
        socket_address.sll_addr[2]  = dest_mac[2];
        socket_address.sll_addr[3]  = dest_mac[3];
        socket_address.sll_addr[4]  = dest_mac[4];
        socket_address.sll_addr[5]  = dest_mac[5];
        socket_address.sll_addr[6]  = 0x00;
        socket_address.sll_addr[7]  = 0x00;

and then i send some data as follows

sent = sendto(s, buffer, ETH_HEADER_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));

On the server side i do socket creation as in client and i am doing recvfrom as follows

length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);

But i dont receive any packet on server side. Could anyone let me know what the problem is?

1

There are 1 best solutions below

0
On

It's hard to tell from your incomplete example, but I would suspect that you're using a SOCK_RAW where you want to use a SOCK_DGRAM. In your SOCK_RAW, the Ethernet header is assumed to be part of the buffer you specify, i.e. your setting of a destination address is irrelevant to the contents of the packet. With SOCK_DGRAM, the contents of buffer form the payload of the generated Ethernet frame and the header comes from your address field.

This kind of thing is fairly easy to debug when you let tcpdump -neX (or something equivalent with Wireshark or tshark) run while testing - you'll see exactly what packet you are generating.