NAME
packet - packet interface on device level. SYNOPSIS
#include
#include
#include
/* the L2 protocols */ packetsocket = socket(AFPACKET, int sockettype, int protocol); DESCRIPTION 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. The sockettype is either SOCKRAW for raw packets including the link level header or SOCKDGRAM for cooked packets with the link level header removed. The link level header information is available in a common format in a sockaddrll. protocol is the IEEE 802.3 protocol number in network order. See the include file for a list of allowed protocols. When protocol is set to htons(ETHPALL) 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. Only processes with effective UID 0 or the CAPNETRAW capability may open packet sockets. SOCKRAW 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 sockaddrll 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. SOCKRAW is similar to but not compatible with the obsolete AFINET/SOCKPACKET of Linux 2.0. SOCKDGRAM operates on a slightly higher level. The physical header is removed before the packet is passed to the user. Packets sent through a SOCKDGRAM packet socket get a suitable physical layer header based on the information in the sockaddrll destination address before they are queued. By default all packets of the specified protocol type are passed to a packet socket. To get packets only from a specific interface use bind(2) specifying an address in a struct sockaddrll to bind the packet socket to an interface. Only the sllprotocol and the sllifindex address fields are used for purposes of binding. The connect(2) operation is not supported on packet sockets. When the MSGTRUNC flag is passed to recvmsg(2), recv(2), recvfrom(2) the real length of the packet on the wire is always returned, even when it is longer than the buffer. Address types The sockaddrll is a device independent physical layer address. struct sockaddrll { unsigned short sllfamily; /* Always AFPACKET */ unsigned short sllprotocol; /* Physical layer protocol */ int sllifindex; /* Interface number */ unsigned short sllhatype; /* ARP hardware type */ unsigned char sllpkttype; /* Packet type */ unsigned char sllhalen; /* Length of address */ unsigned char slladdr[8]; /* Physical layer address */ }; sllprotocol is the standard ethernet protocol type in network order as defined in the include file. It defaults to the socket's protocol. sllifindex is the interface index of the interface (see netdevice(7)); 0 matches any interface (only permitted for bind‐ ing). sllhatype is an ARP type as defined in the include file. sllpkttype contains the packet type. Valid types are PACKETHOST for a packet addressed to the local host, PACKETBROADCAST for a physical layer broadcast packet, PACKETMULTICAST for a packet sent to a physical layer multicast address, PACKETOTHERHOST for a packet to some other host that has been caught by a device driver in promiscuous mode, and PACKETOUTGOING for a packet originated from the local host that is looped back to a packet socket. These types make sense only for receiving. slladdr and sllhalen contain the physical layer (e.g., IEEE 802.3) address and its length. The exact interpreta‐ tion depends on the device. When you send packets it is enough to specify sllfamily, slladdr, sllhalen, sllifindex. The other fields should be 0. sllhatype and sllpkttype are set on received packets for your information. For bind only sllprotocol and sllifindex are used. Socket options Packet socket options are configured by calling setsockopt(2) with level SOLPACKET. PACKETADDMEMBERSHIP PACKETDROPMEMBERSHIP Packet sockets can be used to configure physical layer multicas‐ ting and promiscuous mode. PACKETADDMEMBERSHIP adds a binding and PACKETDROPMEMBERSHIP drops it. They both expect a packetmreq structure as argument: struct packetmreq { int mrifindex; /* interface index */ unsigned short mrtype; /* action */ unsigned short mralen; /* address length */ unsigned char mraddress[8]; /* physical layer address */ }; mrifindex contains the interface index for the interface whose status should be changed. The mrtype parameter specifies which action to perform. PACKETMRPROMISC enables receiving all packets on a shared medium (often known as "promiscuous mode"), PACKETMRMULTICAST binds the socket to the physical layer mul‐ ticast group specified in mraddress and mralen, and PACKETMRALLMULTI sets the socket up to receive all multicast packets arriving at the interface. In addition, the traditional ioctls SIOCSIFFLAGS, SIOCADDMULTI, SIOCDELMULTI can be used for the same purpose. PACKETAUXDATA (since Linux 2.6.21) If this binary option is enabled, the packet socket passes a metadata structure along with each packet in the recvmsg(2) con‐ trol field. The structure can be read with cmsg(3). It is defined as struct tpacketauxdata { u32 tpstatus; u32 tplen; /* packet length */ u32 tpsnaplen; /* captured length */ u16 tpmac; u16 tpnet; u16 tpvlantci; u16 tppadding; }; PACKETFANOUT (since Linux 3.1) 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 setsockopt(2) with level SOLPACKET and option PACKETFANOUT. 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 suc‐ cessfully 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. Fanout supports multiple algorithms to spread traffic between sockets. The default mode, PACKETFANOUTHASH, 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. The load-balance mode
PACKETFANOUTLB implements a round-robin algorithm. PACKETFANOUTCPU selects the socket based on the CPU that the packet arrived on. PACKETFANOUTROLLOVER processes all data on a single socket, moves to the next when one becomes backlogged.
PACKETFANOUTRND selects the socket using a pseudo-random num‐ ber generator. Fanout modes can take additional options. IP fragmentation causes packets from the same flow to have different flow hashes. The flag PACKETFANOUTFLAGDEFRAG, if set, causes packet to be defragmented before fanout is applied, to preserve order even in this case. Fanout mode and options are communicated in the sec‐ ond 16 bits of the integer option value. The flag PACKETFANOUTFLAGROLLOVER 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. PACKETLOSS (with PACKETTXRING) If set, do not silently drop a packet on transmission error, but return it with status set to TPSTATUSWRONGFORMAT. PACKETRESERVE (with PACKETRXRING) By default, a packet receive ring writes packets immediately following the metadata structure and alignment padding. This integer option reserves additional headroom. PACKETRXRING
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 tpsnaplen ) into subsequent slots. Each packet is preceded by a metadata structure similar to tpacketauxdata. The protocol fields encode the offset to the data from the start of the metadata header. tpnet stores the offset to the network layer. If the packet socket is of type SOCKDGRAM, then tpmac is the same. If it is of type
SOCKRAW, 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 tpstatus field. The packet socket owns all slots with status TPSTATUSKERNEL. After filling a slot, it changes the status of the slot to transfer ownership to the application. During normal operation, the new status is TPSTATUSUSER, to signal that a correctly 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 the status to TPSTATUSKERNEL. Packet sockets imple‐ ment multiple variants of the packet ring. The implementation details are described in Documentation/network‐ ing/packetmmap.txt in the Linux kernel source tree. PACKETSTATISTICS Retrieve packet socket statistics in the form of a structure struct tpacketstats { unsigned int tppackets; /* Total packet count */ unsigned int tpdrops; /* Dropped packet count */ }; Receiving statistics resets the internal counters. The statis‐ tics structure differs when using a ring of variant TPACKETV3. PACKETTIMESTAMP (with PACKETRXRING; since Linux 2.6.36) The packet receive ring always stores a timestamp in the meta‐ data 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 Documentation/net‐ working/timestamping.txt in the Linux kernel source tree. PACKETTXRING (since Linux 2.6.31)
Create a memory-mapped ring buffer for packet transmission. This option is similar to PACKETRXRING and takes the same arguments. The application writes packets into slots with sta‐ tus TPSTATUSAVAILABLE and schedules them for transmission by changing the status to TPSTATUSSENDREQUEST. When packets are ready to be transmitted, the application calls send(2) or a variant thereof. The buf and len fields of this call are ignored. If an address is passed using sendto(2) or sendmsg(2), then that overrides the socket default. On successful transmis‐ sion, the socket resets the slot to TPSTATUSAVAILABLE. It discards packets silently on error unless PACKETLOSS is set. PACKETVERSION (with PACKETRXRING; since Linux 2.6.27) By default, PACKETRXRING creates a packet receive ring of variant TPACKETV1. To create another variant, configure the desired variant by setting this integer option before creating the ring. Ioctls SIOCGSTAMP can be used to receive the timestamp of the last received packet. Argument is a struct timeval. In addition all standard ioctls defined in netdevice(7) and socket(7) are valid on packet sockets. Error handling Packet sockets do no error handling other than errors occurred while passing the packet to the device driver. They don't have the concept of a pending error. ERRORS EADDRNOTAVAIL Unknown multicast group address passed. EFAULT User passed invalid memory address. EINVAL Invalid argument. EMSGSIZE Packet is bigger than interface MTU. ENETDOWN Interface is not up. ENOBUFS Not enough memory to allocate the packet. ENODEV Unknown device name or interface index specified in interface address. ENOENT No packet received. ENOTCONN No interface address passed. ENXIO Interface address contained an invalid interface index. EPERM User has insufficient privileges to carry out this operation.
In addition other errors may be generated by the low-level driver. VERSIONS AFPACKET is a new feature in Linux 2.2. Earlier Linux versions sup‐ ported only SOCKPACKET. The include file
is present since glibc 2.1. Older systems need: #include
#include
#include
/* The L2 protocols */ NOTES For portable programs it is suggested to use AFPACKET via pcap(3); although this covers only a subset of the AFPACKET features. The SOCKDGRAM packet sockets make no attempt to create or parse the IEEE 802.2 LLC header for a IEEE 802.3 frame. When ETHP8023 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 mul‐ tiplexed on the DSAP/SSAP protocol fields; instead they are supplied to the user as protocol ETHP8022 with the LLC header prepended. It is thus not possible to bind to ETHP8023; bind to ETHP8022 instead and do the protocol multiplex yourself. The default for sending is the standard Ethernet DIX encapsulation with the protocol filled in. Packet sockets are not subject to the input or output firewall chains. Compatibility In Linux 2.0, the only way to get a packet socket was by calling socket(AFINET, SOCKPACKET, protocol). This is still supported but strongly deprecated. The main difference between the two methods is that SOCKPACKET uses the old struct sockaddrpkt to specify an inter‐ face, which doesn't provide physical layer independence. struct sockaddrpkt { unsigned short spktfamily; unsigned char spktdevice[14]; unsigned short spktprotocol; }; spktfamily contains the device type, spktprotocol is the IEEE 802.3 protocol type as defined in and spktdevice is the device name as a null-terminated string, for example, eth0. This structure is obsolete and should not be used in new code. BUGS glibc 2.1 does not have a define for SOLPACKET. The suggested work‐ around is to use:
#ifndef SOLPACKET
#define SOLPACKET 263
#endif This is fixed in later glibc versions and also does not occur on libc5 systems. The IEEE 802.2/803.3 LLC handling could be considered as a bug. Socket filters are not documented. The MSGTRUNC recvmsg(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 SOCKDGRAM. SEE ALSO socket(2), pcap(3), capabilities(7), ip(7), raw(7), socket(7) RFC 894 for the standard IP Ethernet encapsulation. RFC 1700 for the IEEE 802.3 IP encapsulation. The
include file for physical layer protocols. COLOPHON This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2012-05-10 PACKET(7)