{"id":3974,"date":"2022-12-20T17:28:34","date_gmt":"2022-12-20T20:28:34","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/packet-man7\/"},"modified":"2022-12-20T17:28:34","modified_gmt":"2022-12-20T20:28:34","slug":"packet-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/packet-man7\/","title":{"rendered":"PACKET (man7)"},"content":{"rendered":"<h1 align=\"center\">PACKET<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">packet \u2212 packet interface on device level<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/socket.h> <br \/> #include <linux\/if_packet.h> #include <net\/ethernet.h> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var the L2 protocols *\/<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>packet_socket = socket(AF_PACKET, int<\/b> <i>socket_type<\/i><b>, int<\/b> <i>protocol<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Packet sockets are used to receive or send raw packets at the device driver (OSI Layer 2) level. They allow the user to implement protocol modules in user space on top of the physical layer.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>socket_type<\/i> is either <b>SOCK_RAW<\/b> for raw packets including the link-level header or <b>SOCK_DGRAM<\/b> for cooked packets with the link-level header removed. The link-level header information is available in a common format in a <i>sockaddr_ll<\/i> structure. <i>protocol<\/i> is the IEEE 802.3 protocol number in network byte order. See the <i><linux\/if_ether.h><\/i> include file for a list of allowed protocols. When protocol is set to <b>htons(ETH_P_ALL)<\/b>, then all protocols are received. All incoming packets of that protocol type will be passed to the packet socket before they are passed to the protocols implemented in the kernel.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In order to create a packet socket, a process must have the <b>CAP_NET_RAW<\/b> capability in the user namespace that governs its network namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>SOCK_RAW<\/b> packets are passed to and from the device driver without any changes in the packet data. When receiving a packet, the address is still parsed and passed in a standard <i>sockaddr_ll<\/i> address structure. When transmitting a packet, the user-supplied buffer should contain the physical-layer header. That packet is then queued unmodified to the network driver of the interface defined by the destination address. Some device drivers always add other headers. <b>SOCK_RAW<\/b> is similar to but not compatible with the obsolete <b>AF_INET\/SOCK_PACKET<\/b> of Linux 2.0.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>SOCK_DGRAM<\/b> operates on a slightly higher level. The physical header is removed before the packet is passed to the user. Packets sent through a <b>SOCK_DGRAM<\/b> packet socket get a suitable physical-layer header based on the information in the <i>sockaddr_ll<\/i> destination address before they are queued.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By default, all packets of the specified protocol type are passed to a packet socket. To get packets only from a specific interface use <b>bind<\/b>(2) specifying an address in a <i>struct sockaddr_ll<\/i> to bind the packet socket to an interface. Fields used for binding are <i>sll_family<\/i> (should be <b>AF_PACKET<\/b>), <i>sll_protocol<\/i>, and <i>sll_ifindex<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>connect<\/b>(2) operation is not supported on packet sockets.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the <b>MSG_TRUNC<\/b> flag is passed to <b>recvmsg<\/b>(2), <b>recv<\/b>(2), or <b>recvfrom<\/b>(2), the real length of the packet on the wire is always returned, even when it is longer than the buffer.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Address types<\/b> <br \/> The <i>sockaddr_ll<\/i> structure is a device-independent physical-layer address.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct sockaddr_ll { <br \/> unsigned short sll_family; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Always AF_PACKET bodies\/ usr\/ <br \/> unsigned short sll_protocol; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Physical-layer protocol bodies\/ usr\/ <br \/> int sll_ifindex; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Interface number bodies\/ usr\/ <br \/> unsigned short sll_hatype; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var ARP hardware type bodies\/ usr\/ <br \/> unsigned char sll_pkttype; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Packet type bodies\/ usr\/ <br \/> unsigned char sll_halen; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Length of address bodies\/ usr\/ <br \/> unsigned char sll_addr[8]; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Physical-layer address bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fields of this structure are as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p><i>sll_protocol<\/i> is the standard ethernet protocol type in network byte order as defined in the <i><linux\/if_ether.h><\/i> include file. It defaults to the socket\u2019s protocol.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p><i>sll_ifindex<\/i> is the interface index of the interface (see <b>netdevice<\/b>(7)); 0 matches any interface (only permitted for binding). <i>sll_hatype<\/i> is an ARP type as defined in the <i><linux\/if_arp.h><\/i> include file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p><i>sll_pkttype<\/i> contains the packet type. Valid types are <b>PACKET_HOST<\/b> for a packet addressed to the local host, <b>PACKET_BROADCAST<\/b> for a physical-layer broadcast packet, <b>PACKET_MULTICAST<\/b> for a packet sent to a physical-layer multicast address, <b>PACKET_OTHERHOST<\/b> for a packet to some other host that has been caught by a device driver in promiscuous mode, and <b>PACKET_OUTGOING<\/b> for a packet originating from the local host that is looped back to a packet socket. These types make sense only for receiving.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p><i>sll_addr<\/i> and <i>sll_halen<\/i> contain the physical-layer (e.g., IEEE 802.3) address and its length. The exact interpretation depends on the device.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">When you send packets, it is enough to specify <i>sll_family<\/i>, <i>sll_addr<\/i>, <i>sll_halen<\/i>, <i>sll_ifindex<\/i>, and <i>sll_protocol<\/i>. The other fields should be 0. <i>sll_hatype<\/i> and <i>sll_pkttype<\/i> are set on received packets for your information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Socket options<\/b> <br \/> Packet socket options are configured by calling <b>setsockopt<\/b>(2) with level <b>SOL_PACKET<\/b>. <b><br \/> PACKET_ADD_MEMBERSHIP <br \/> PACKET_DROP_MEMBERSHIP<\/b><\/p>\n<p style=\"margin-left:22%;\">Packet sockets can be used to configure physical-layer multicasting and promiscuous mode. <b>PACKET_ADD_MEMBERSHIP<\/b> adds a binding and <b>PACKET_DROP_MEMBERSHIP<\/b> drops it. They both expect a <i>packet_mreq<\/i> structure as argument:<\/p>\n<p style=\"margin-left:28%; margin-top: 1em\">struct packet_mreq { <br \/> int mr_ifindex; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var interface index bodies\/ usr\/ <br \/> unsigned short mr_type; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var action bodies\/ usr\/ <br \/> unsigned short mr_alen; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var address length bodies\/ usr\/ <br \/> unsigned char mr_address[8]; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var physical-layer address bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\"><i>mr_ifindex<\/i> contains the interface index for the interface whose status should be changed. The <i>mr_type<\/i> field specifies which action to perform. <b>PACKET_MR_PROMISC<\/b> enables receiving all packets on a shared medium (often known as &#8220;promiscuous mode&#8221;), <b>PACKET_MR_MULTICAST<\/b> binds the socket to the physical-layer multicast group specified in <i>mr_address<\/i> and <i>mr_alen<\/i>, and <b>PACKET_MR_ALLMULTI<\/b> sets the socket up to receive all multicast packets arriving at the interface.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">In addition, the traditional ioctls <b>SIOCSIFFLAGS<\/b>, <b>SIOCADDMULTI<\/b>, <b>SIOCDELMULTI<\/b> can be used for the same purpose.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_AUXDATA<\/b> (since Linux 2.6.21)<\/p>\n<p style=\"margin-left:22%;\">If this binary option is enabled, the packet socket passes a metadata structure along with each packet in the <b>recvmsg<\/b>(2) control field. The structure can be read with <b>cmsg<\/b>(3). It is defined as<\/p>\n<p style=\"margin-left:28%; margin-top: 1em\">struct tpacket_auxdata { <br \/> __u32 tp_status; <br \/> __u32 tp_len; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var packet length bodies\/ usr\/ <br \/> __u32 tp_snaplen; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var captured length bodies\/ usr\/ <br \/> __u16 tp_mac; <br \/> __u16 tp_net; <br \/> __u16 tp_vlan_tci; <br \/> __u16 tp_vlan_tpid; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Since Linux 3.14; earlier, these <br \/> were unused padding bytes bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_FANOUT<\/b> (since Linux 3.1)<\/p>\n<p style=\"margin-left:22%;\">To scale processing across threads, packet sockets can form a fanout group. In this mode, each matching packet is enqueued onto only one socket in the group. A socket joins a fanout group by calling <b>setsockopt<\/b>(2) with level <b>SOL_PACKET<\/b> and option <b>PACKET_FANOUT<\/b>. Each network namespace can have up to 65536 independent groups. A socket selects a group by encoding the ID in the first 16 bits of the integer option value. The first packet socket to join a group implicitly creates it. To successfully join an existing group, subsequent packet sockets must have the same protocol, device settings, fanout mode and flags (see below). Packet sockets can leave a fanout group only by closing the socket. The group is deleted when the last socket is closed.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">Fanout supports multiple algorithms to spread traffic between sockets, as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>The default mode, <b>PACKET_FANOUT_HASH<\/b>, sends packets from the same flow to the same socket to maintain per-flow ordering. For each packet, it chooses a socket by taking the packet flow hash modulo the number of sockets in the group, where a flow hash is a hash over network-layer address and optional transport-layer port fields.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p>The load-balance mode <b>PACKET_FANOUT_LB<\/b> implements a round-robin algorithm.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p><b>PACKET_FANOUT_CPU<\/b> selects the socket based on the CPU that the packet arrived on.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p><b>PACKET_FANOUT_ROLLOVER<\/b> processes all data on a single socket, moving to the next when one becomes backlogged.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p><b>PACKET_FANOUT_RND<\/b> selects the socket using a pseudo-random number generator.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"22%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"74%\">\n<p><b>PACKET_FANOUT_QM<\/b> (available since Linux 3.14) selects the socket using the recorded queue_mapping of the received skb.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:22%; margin-top: 1em\">Fanout modes can take additional options. IP fragmentation causes packets from the same flow to have different flow hashes. The flag <b>PACKET_FANOUT_FLAG_DEFRAG<\/b>, if set, causes packets to be defragmented before fanout is applied, to preserve order even in this case. Fanout mode and options are communicated in the second 16 bits of the integer option value. The flag <b>PACKET_FANOUT_FLAG_ROLLOVER<\/b> enables the roll over mechanism as a backup strategy: if the original fanout algorithm selects a backlogged socket, the packet rolls over to the next available one.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_LOSS<\/b> (with <b>PACKET_TX_RING<\/b>)<\/p>\n<p style=\"margin-left:22%;\">When a malformed packet is encountered on a transmit ring, the default is to reset its <i>tp_status<\/i> to <b>TP_STATUS_WRONG_FORMAT<\/b> and abort the transmission immediately. The malformed packet blocks itself and subsequently enqueued packets from being sent. The format error must be fixed, the associated <i>tp_status<\/i> reset to <b>TP_STATUS_SEND_REQUEST<\/b>, and the transmission process restarted via <b>send<\/b>(2). However, if <b>PACKET_LOSS<\/b> is set, any malformed packet will be skipped, its <i>tp_status<\/i> reset to <b>TP_STATUS_AVAILABLE<\/b>, and the transmission process continued.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_RESERVE<\/b> (with <b>PACKET_RX_RING<\/b>)<\/p>\n<p style=\"margin-left:22%;\">By default, a packet receive ring writes packets immediately following the metadata structure and alignment padding. This integer option reserves additional headroom.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_RX_RING<\/b><\/p>\n<p style=\"margin-left:22%;\">Create a memory-mapped ring buffer for asynchronous packet reception. The packet socket reserves a contiguous region of application address space, lays it out into an array of packet slots and copies packets (up to <i>tp_snaplen<\/i>) into subsequent slots. Each packet is preceded by a metadata structure similar to <i>tpacket_auxdata<\/i>. The protocol fields encode the offset to the data from the start of the metadata header. <i>tp_net<\/i> stores the offset to the network layer. If the packet socket is of type <b>SOCK_DGRAM<\/b>, then <i>tp_mac<\/i> is the same. If it is of type <b>SOCK_RAW<\/b>, then that field stores the offset to the link-layer frame. Packet socket and application communicate the head and tail of the ring through the <i>tp_status<\/i> field. The packet socket owns all slots with <i>tp_status<\/i> equal to <b>TP_STATUS_KERNEL<\/b>. After filling a slot, it changes the status of the slot to transfer ownership to the application. During normal operation, the new <i>tp_status<\/i> value has at least the <b>TP_STATUS_USER<\/b> bit set to signal that a received packet has been stored. When the application has finished processing a packet, it transfers ownership of the slot back to the socket by setting <i>tp_status<\/i> equal to <b>TP_STATUS_KERNEL<\/b>.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">Packet sockets implement multiple variants of the packet ring. The implementation details are described in <i>Documentation\/networking\/packet_mmap.rst<\/i> in the Linux kernel source tree.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_STATISTICS<\/b><\/p>\n<p style=\"margin-left:22%;\">Retrieve packet socket statistics in the form of a structure<\/p>\n<p style=\"margin-left:28%; margin-top: 1em\">struct tpacket_stats { <br \/> unsigned int tp_packets; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Total packet count bodies\/ usr\/ <br \/> unsigned int tp_drops; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Dropped packet count bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">Receiving statistics resets the internal counters. The statistics structure differs when using a ring of variant <b>TPACKET_V3<\/b>.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_TIMESTAMP<\/b> (with <b>PACKET_RX_RING<\/b>; since Linux 2.6.36)<\/p>\n<p style=\"margin-left:22%;\">The packet receive ring always stores a timestamp in the metadata header. By default, this is a software generated timestamp generated when the packet is copied into the ring. This integer option selects the type of timestamp. Besides the default, it support the two hardware formats described in <i>Documentation\/networking\/timestamping.rst<\/i> in the Linux kernel source tree.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_TX_RING<\/b> (since Linux 2.6.31)<\/p>\n<p style=\"margin-left:22%;\">Create a memory-mapped ring buffer for packet transmission. This option is similar to <b>PACKET_RX_RING<\/b> and takes the same arguments. The application writes packets into slots with <i>tp_status<\/i> equal to <b>TP_STATUS_AVAILABLE<\/b> and schedules them for transmission by changing <i>tp_status<\/i> to <b>TP_STATUS_SEND_REQUEST<\/b>. When packets are ready to be transmitted, the application calls <b>send<\/b>(2) or a variant thereof. The <i>buf<\/i> and <i>len<\/i> fields of this call are ignored. If an address is passed using <b>sendto<\/b>(2) or <b>sendmsg<\/b>(2), then that overrides the socket default. On successful transmission, the socket resets <i>tp_status<\/i> to <b>TP_STATUS_AVAILABLE<\/b>. It immediately aborts the transmission on error unless <b>PACKET_LOSS<\/b> is set.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_VERSION<\/b> (with <b>PACKET_RX_RING<\/b>; since Linux 2.6.27)<\/p>\n<p style=\"margin-left:22%;\">By default, <b>PACKET_RX_RING<\/b> creates a packet receive ring of variant <b>TPACKET_V1<\/b>. To create another variant, configure the desired variant by setting this integer option before creating the ring.<\/p>\n<p style=\"margin-left:11%;\"><b>PACKET_QDISC_BYPASS<\/b> (since Linux 3.14)<\/p>\n<p style=\"margin-left:22%;\">By default, packets sent through packet sockets pass through the kernel\u2019s qdisc (traffic control) layer, which is fine for the vast majority of use cases. For traffic generator appliances using packet sockets that intend to brute-force flood the network\u2014for example, to test devices under load in a similar fashion to pktgen\u2014this layer can be bypassed by setting this integer option to 1. A side effect is that packet buffering in the qdisc layer is avoided, which will lead to increased drops when network device transmit queues are busy; therefore, use at your own risk.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Ioctls <br \/> SIOCGSTAMP<\/b> can be used to receive the timestamp of the last received packet. Argument is a <i>struct timeval<\/i> variable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition, all standard ioctls defined in <b>netdevice<\/b>(7) and <b>socket<\/b>(7) are valid on packet sockets.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Error handling<\/b> <br \/> Packet sockets do no error handling other than errors occurred while passing the packet to the device driver. They don\u2019t have the concept of a pending error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>EADDRNOTAVAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">Unknown multicast group address passed.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EFAULT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"53%\">\n<p>User passed invalid memory address.<\/p>\n<\/td>\n<td width=\"25%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"53%\">\n<p>Invalid argument.<\/p>\n<\/td>\n<td width=\"25%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EMSGSIZE<\/b><\/p>\n<p style=\"margin-left:22%;\">Packet is bigger than interface MTU.<\/p>\n<p style=\"margin-left:11%;\"><b>ENETDOWN<\/b><\/p>\n<p style=\"margin-left:22%;\">Interface is not up.<\/p>\n<p style=\"margin-left:11%;\"><b>ENOBUFS<\/b><\/p>\n<p style=\"margin-left:22%;\">Not enough memory to allocate the packet.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENODEV<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Unknown device name or interface index specified in interface address.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>No packet received.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOTCONN<\/b><\/p>\n<p style=\"margin-left:22%;\">No interface address passed.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>ENXIO<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>Interface address contained an invalid interface index.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EPERM<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>User has insufficient privileges to carry out this operation.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition, other errors may be generated by the low-level driver.<\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>AF_PACKET<\/b> is a new feature in Linux 2.2. Earlier Linux versions supported only <b>SOCK_PACKET<\/b>.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For portable programs it is suggested to use <b>AF_PACKET<\/b> via <b>pcap<\/b>(3); although this covers only a subset of the <b>AF_PACKET<\/b> features.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>SOCK_DGRAM<\/b> packet sockets make no attempt to create or parse the IEEE 802.2 LLC header for a IEEE 802.3 frame. When <b>ETH_P_802_3<\/b> is specified as protocol for sending the kernel creates the 802.3 frame and fills out the length field; the user has to supply the LLC header to get a fully conforming packet. Incoming 802.3 packets are not multiplexed on the DSAP\/SSAP protocol fields; instead they are supplied to the user as protocol <b>ETH_P_802_2<\/b> with the LLC header prefixed. It is thus not possible to bind to <b>ETH_P_802_3<\/b>; bind to <b>ETH_P_802_2<\/b> instead and do the protocol multiplex yourself. The default for sending is the standard Ethernet DIX encapsulation with the protocol filled in.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Packet sockets are not subject to the input or output firewall chains.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Compatibility<\/b> <br \/> In Linux 2.0, the only way to get a packet socket was with the call:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">socket(AF_INET, SOCK_PACKET, protocol)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is still supported, but deprecated and strongly discouraged. The main difference between the two methods is that <b>SOCK_PACKET<\/b> uses the old <i>struct sockaddr_pkt<\/i> to specify an interface, which doesn\u2019t provide physical-layer independence.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct sockaddr_pkt { <br \/> unsigned short spkt_family; <br \/> unsigned char spkt_device[14]; <br \/> unsigned short spkt_protocol; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>spkt_family<\/i> contains the device type, <i>spkt_protocol<\/i> is the IEEE 802.3 protocol type as defined in <i><sys\/if_ether.h><\/i> and <i>spkt_device<\/i> is the device name as a null-terminated string, for example, eth0.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This structure is obsolete and should not be used in new code.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The IEEE 802.2\/803.3 LLC handling could be considered as a bug.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Socket filters are not documented.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>MSG_TRUNC recvmsg<\/b>(2) extension is an ugly hack and should be replaced by a control message. There is currently no way to get the original destination address of packets via <b>SOCK_DGRAM<\/b>.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>socket<\/b>(2), <b>pcap<\/b>(3), <b>capabilities<\/b>(7), <b>ip<\/b>(7), <b>raw<\/b>(7), <b>socket<\/b>(7)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">RFC\u00a0894 for the standard IP Ethernet encapsulation. RFC\u00a01700 for the IEEE 802.3 IP encapsulation.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i><linux\/if_ether.h><\/i> include file for physical-layer protocols.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Linux kernel source tree. <i>Documentation\/networking\/filter.rst<\/i> describes how to apply Berkeley Packet Filters to packet sockets. <i>tools\/testing\/selftests\/net\/psock_tpacket.c<\/i> contains example source code for all available versions of <b>PACKET_RX_RING<\/b> and <b>PACKET_TX_RING<\/b>.<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  packet \u2212 packet interface on device level <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,1063],"class_list":["post-3974","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-packet"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3974"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3974\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}