Sendto does not use the MAC address specified in struct sockaddr_ll when sending raw packets

I am trying to send an Ethernet OAM frame using a raw socket. I was successful at that.

The submit function I wrote:

int send_frame(sock_info *info,char *buf,int length) { struct sockaddr_ll dest_addr; memset(&dest_addr,0,sizeof(struct sockaddr_ll)); dest_addr.sll_family = PF_PACKET; dest_addr.sll_protocol = htons(8902); dest_addr.sll_ifindex = info->if_index; dest_addr.sll_halen = ETH_MAC_ADDR_LEN; dest_addr.sll_pkttype = PACKET_OTHERHOST; dest_addr.sll_hatype = ARPHRD_ETHER; memset(dest_addr.sll_addr,0,8); dest_addr.sll_addr[0] = 0x00; dest_addr.sll_addr[1] = 0xE0; dest_addr.sll_addr[2] = 0x0C; dest_addr.sll_addr[3] = 0x00; dest_addr.sll_addr[4] = 0x95; dest_addr.sll_addr[5] = 0x02; return sendto(info->sock_fd, buf, length, 0, (struct sockaddr*) &dest_addr, sizeof(struct sockaddr_ll)); } 

I was unable to capture the package using wirehark. After too much, I found that the buffer used for sending should have all the fields of the ethernet frame (starting from the destination address). When I added the destination address and the source address and other ethernet fields to the buffer, I was able to capture the packet using wirehark. Thus, the send function does not use the MAC address stored in dest_addr.sll_addr .

My question is: then why do I need the sll_addr field in struct sockaddr_ll ? The manuals say that this is the destination MAC address.

+6
source share
1 answer

It seems to me that it works as described on the man page (man 7 packet):

SOCK_RAW packets are also transferred from the device driver without any change in the packet data. Upon receipt of the packet, the address is still parsed and passed in the standard structure of the address bar sockaddr_ll. When transmitting a packet, the user buffer must contain a physical layer header . Then, this package is not changed for the network driver interface specified by the destination address. Some device drivers always add other headers. SOCK_RAW is similar to but not compatible with the legacy PF_INET / SOCK_PACKET Linux 2.0.

Here, the buffer refers to the second sendto() parameter. Thus, stuct sockaddr_ll is used only to return data to the caller, and not to format the RAW packet. Maybe you want to use SOCK_DGRAM or libpcap?

+3
source

Source: https://habr.com/ru/post/943706/


All Articles