Linux socket operation Link Layer

Source: Internet
Author: User

Linux provides the most common network communication application development interface-berkerley socket (socket ). it is applicable to inter-process communication (IPC) on the same host and inter-process communication on different hosts. Socket settings are completed through SOCKET call:

Int socket (INT family, int type, int Protocol );

Family indicates the communication domain or protocol family. Linux supports dozens of network protocol families, including pf_unix, pf_ipx, and pf_packet. type indicates the socket type, currently, sock_stream, sock_dgram, sock_raw, and sock_packet are supported. Protocol is the specific protocol type number used by the socket.

The interfaces provided by Linux system for developing applications based on the data link layer are integrated into sockets. They are used to create a packet-type socket. enables applications to receive or send raw data packets (such as ARP packets) that are not processed by the system at the data link layer ), you can also define your own special network protocols on the physical layer using a packet-type socket. Only user (superuser) processes with registration number 0 can establish or open sockets used to access the Low-layer network. in Linux, The PACKET socket created in the following three methods can be directly used to access the data link layer:
(1) socket of the sock_packei type in the pf_inet protocol family
(2) sockets of the sock_raw type in the pf_packet protocol family
(3) socket of the sock_dgram type in the pf_packet protocol family

In Linux 2.0, operations on the data link layer mainly use the PACKET socket defined by sock_packet. The initialization definition is as follows:
Sockfd = socket (af_inet, sock_packet, Protocol );
Protocol is used to determine the physical layer protocol used by the socket (defined in 802.3 ). here I select the commonly used physical layer protocol eth_p_ip (Internet Protocol ). sock_packet uses an old sockaddr_pkt data structure to set network interfaces.

In Linux 2 2, pf_packet is used instead of sock_packet to define a PACKET socket. The initialization definition of this socket is as follows:
Sockfd = socket (pf_packet, socket_type, Protocol );
Socket_type can only be sock_raw or sock_dgram, and protocol is the physical layer communication protocol (same as above ). Sockets of the sock_raw and sock_dgram types use a standard physical layer address structure (sockaddr_ll) that is not related to the device, but the specific operation Message format is different. The sock_raw socket directly sends (or receives from the network hardware driver) complete data packets without any processing (including the frame header of the Physical Frame) to the network hardware driver ), this requires the programmer to understand the physical frame header structure of the corresponding device to correctly load and analyze packets. The physical frame header of the data packets received by the sock_dgram socket is automatically removed by the system.
The destination address information in the sockaddr_ll structure adds an appropriate physical frame header to the data packet.

By default, all datagram files received from any interface that comply with the specified protocol are transmitted to the PACKET socket. The BIND System Call uses a sochddr_l1 address structure to bind the paccket socket to a network interface, so that the socket can only receive
The socaddr_ll address structure is defined as follows:
Struct sockaddr_ll
{
Unsigned short sll_family;/* always af_packet */
Unsigned short sll_protocol;/* Physical Layer Protocol */
Int sll_ifindex;/* interface number */
Unsigned short sll_hatype;/* Header type */
Unsigned char sll_pkttype;/* group type */
Unsigned char sll_halen;/* address length */
Unsigned char sll_addr [8];/* physical layer address */
};

1. Implement ARP using the _packet socket in the pf_lnet protocol family
(1) create a socket
The socket system call is used to create a socket. The format is as follows:
Sockfd = socket (pf_inet, sock_packet, htons (eth_p_arp ));
(2) Load packets
For sockets of the sock_packet type, the Ethernet physical frame header should be set by the programmer as part of the sent packet. The format of the Physical Frame header is defined as follows: (in/usr/include/Linux/if_ether.h)
92 struct ethhdr
93 {
94 unsigned char h_dest [eth_alen];/* destination eth addr */
95 unsigned char h_source [eth_alen];/* Source ether ADDR */
96 unsigned short h_proto;/* packet type Id field */
97 };
The actually sent Address Resolution Packet frame is composed of the Ethernet physical frame header and the frame data (ARP packet). It is used as the structure arppacket table.
As shown below:
Typedef struct {
Struct ethhdr eth_header; // struct defined in Linux/if_ether.h
Arphdr arp_header;
} Arppacket;
The preceding message structure is relatively simple to load. For ARP, The arp_header settings are as follows:
PTK. arp_header.ar_hrd = htons (arphrd_ether); // arphrd_ether is defined in Linux/if_arp.h
PTK. arp_header.ar_pro = htons (ethertype_ip );
PTK. arp_header.ar_hln = 6;
PTK. arp_header.ar_pln = 4;
PTK. arp_header.ar_op = htons (arpop_request );

Pkt. arp_header.ar_sha [], Pkt. set arp_header.ar_sip [] and pkt_arp_header.ar_tip [] to the physical address, IP address, and IP address of the host to be resolved. returns the Pkt in the message. the content in arp_header.tha [] is Parsing
The physical address of the target host.

For the Ethernet frame header, Pkt. eth_header.h_dest [] is the destination address, that is, the broadcast physical address 0 xffffff, Pkt. eth_header.source [] is the local physical address (same as Pkt. arp_header.ar_sha []
Pkt. eth_header.h_proto values htons (ethertype_arp) to indicate address resolution packets.
The values of ethertype_arp and eth_p_arp are both 0x0806, but the defined files are different. The former is defined in net/Ethernet. h, and the latter is defined in Linux/if_ether.h.

(3) sending and receiving of packets
Sending/receiving messages at the data link layer are similar to sending/receiving data packets at the IP layer. The system calls sendto () and recvfrom () respectively ()
Complete. You only need to send the configured packets containing the target address to the local network hardware instead of the target host. The corresponding program segment is as follows:
Struct sockaddr to, from;
Int fromlen = 0;
Strcpy (to. sa_data, "eth0 ");
Sendto (sockfd, Pkt, sizeof (struct arppacket), 0, & to, sizeof (struct sockaddr ));
Recvfrom (FIG, Buf, packet_size, 0, & from, & fromlen );
The Buf is a pointer of the character type that contains the arppacket struct.

Check whether the arp_header.ar_op item in the received ARP response message is arpop_reply (ARP response) and whether arp_header.ar_tip is the IP address of the target host.

Ii. Implement ARP using a socket of the sock_raw type in the pf_packet protocol family
(1) create a socket
Sockfd = socket (pf_packet, sock_raw, htons (eth_p_arp ));
(2) Load packets
Similar to sockets of the sock_packet type, when a socket of the sock_raw type operates on the link layer, it also requires that the Ethernet physical frame header be set as part of the sent message by the programmer.
(3) sending and receiving of packets
The sock_raw type socket uses the standard physical layer address structure sockaddr_ll. Therefore, before sending a message, bind the socket to the local physical address structure my_etheraddr configured (called by the BIND () system, you also need to configure the physical address structure broad_etheraddr.
Example:
Struct aockaddr_ll my_etheraddr, broad_etheraddr;
My_etheraddr.sll_family = af_packet;
My_etheraddr.sll_protocol = htons (eth_p_arp );
My_etheraddr.sll_ifindex = 2;/* interface number 2 indicates eth0 */
My_etheraddr.sll_hatype = arphrd_ether;
My_etheraddr.sll_pkttype = packet_host;
My_etheraddr.sll_halen = eth_alen;
The physical address of the host in my_etheraddr.sll_addr [8.

The configuration of broad_etheraddr is the same as that of my_etheraddr except that sll_pkttype uses packet_broadcast and sll_addr to obtain the broadcast physical address (0 xffffff.

The binding format is as follows:
BIND (sockfd, (struct sockaddr *) & my_etheraddr, sizeof (my_etheraddr ));

The sending and receiving call procedure is as follows:
Sendto (sockfd, Buf, sizeof (struct arppacket), 0,
(Struct sockaddr *) & broad_etheraddr, sizeof (broad_etheraddr ));

Recvform (sockfd, Buf, packet_size, & from, & fromlen );

Iii. Implement ARP using sockets of the sock_dgram type in the pf_packet protocol family
The socket of the sock_dgram type does not require the programmer to configure the Ethernet frame header. Therefore, only the data zone (ARP packet) is sent, and other sockets of the sock_raw type are the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.