Mass
Mass

Reputation: 123

How to send SOCK_DGRAM packet using packet_mmap without being concerned about MAC address?

I want to send packets using packet_mmap for getting high packet transmission rate. I managed to send packets using packet socket in raw mode, which for that purpose I created L2, L3, and etc in buffer and send it using

sendto(fd_socket, NULL, 0, 0, NULL, sizeof(struct sockaddr_ll));

However, I don't want to bother with destination mac address. So I turned into using Dgram instead. If I want to employ sendto there is an argument for destination MAC Address. Even though this is not what I wished for in terms of not being concerned with arp cache and specifying destination's MAC address:

sendto(fd_socket, NULL, 0, 0, (struct sockaddr *) ps_sockaddr, sizeof(struct sockaddr_ll));

However I found that send is also allowed to be used with packet socket. http://man7.org/linux/man-pages/man7/packet.7.html Therefore, I filled the buffer with ip header and so on. In this case send returns zero as nothing is found to be sent, which I expect to be something other than zero, if there is an error with transmission of packet.

Is there a way to use packet_mmap without being concerned about L2 address?

Upvotes: 2

Views: 633

Answers (1)

Gil Hamilton
Gil Hamilton

Reputation: 12357

No. When you use a packet socket in SOCK_DGRAM mode, you needn't construct the L2 header, but you must still provide the L2 address and then the system will construct the header for you. (So that would have the benefit of actually building the L2 portion of the packet for you and you wouldn't need to specify the source MAC address but you still need to specify the interface from which you want the packet sent and the target MAC address.)

How would the system know where to send the packet otherwise? I believe you were hoping that the system would look at the IP header you've already constructed in the packet buffer, and then do an interface selection and ARP lookup on your behalf, but by using a packet socket, you're bypassing that part of the network stack. (Also, if the IP address is not on the local LAN, there would be a routing step needed -- usually just finding the default gateway and ARPing for its MAC address.)

Now you can get most of what (I think) you want with a raw socket (http://man7.org/linux/man-pages/man7/raw.7.html). In this case, you're telling the kernel that you're using IP but not relying on it for any of the higher layers (> L3). And you can also construct your own IP header in this case and have the routing decision based on it using the IP_HDRINCL option.

Upvotes: 0

Related Questions