1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" $Id: packet.7,v 1.13 2000/08/14 08:03:45 ak Exp $
12 .TH PACKET 7 2014-08-19 "Linux" "Linux Programmer's Manual"
14 packet \- packet interface on device level
17 .B #include <sys/socket.h>
19 .B #include <linux/if_packet.h>
21 .B #include <net/ethernet.h> /* the L2 protocols */
23 .BI "packet_socket = socket(AF_PACKET, int " socket_type ", int "protocol );
26 Packet sockets are used to receive or send raw packets at the device driver
28 They allow the user to implement protocol modules in user space
29 on top of the physical layer.
35 for raw packets including the link-level header or
37 for cooked packets with the link-level header removed.
38 The link-level header information is available in a common format in a
41 is the IEEE 802.3 protocol number in network byte order.
44 include file for a list of allowed protocols.
48 then all protocols are received.
49 All incoming packets of that protocol type will be passed to the packet
50 socket before they are passed to the protocols implemented in the kernel.
52 Only processes with effective UID 0 or the
54 capability may open packet sockets.
57 packets are passed to and from the device driver without any changes in
59 When receiving a packet, the address is still parsed and
63 When transmitting a packet, the user supplied buffer
64 should contain the physical layer header.
66 queued unmodified to the network driver of the interface defined by the
68 Some device drivers always add other headers.
70 is similar to but not compatible with the obsolete
71 .B AF_INET/SOCK_PACKET
75 operates on a slightly higher level.
76 The physical header is removed before the packet is passed to the user.
77 Packets sent through a
79 packet socket get a suitable physical layer header based on the
82 destination address before they are queued.
84 By default all packets of the specified protocol type
85 are passed to a packet socket.
86 To get packets only from a specific interface use
88 specifying an address in a
90 to bind the packet socket to an interface.
95 address fields are used for purposes of binding.
99 operation is not supported on packet sockets.
107 the real length of the packet on the wire is always returned,
108 even when it is longer than the buffer.
112 is a device independent physical layer address.
117 unsigned short sll_family; /* Always AF_PACKET */
118 unsigned short sll_protocol; /* Physical layer protocol */
119 int sll_ifindex; /* Interface number */
120 unsigned short sll_hatype; /* ARP hardware type */
121 unsigned char sll_pkttype; /* Packet type */
122 unsigned char sll_halen; /* Length of address */
123 unsigned char sll_addr[8]; /* Physical layer address */
129 is the standard ethernet protocol type in network byte order as defined
131 .I <linux/if_ether.h>
133 It defaults to the socket's protocol.
135 is the interface index of the interface
138 0 matches any interface (only permitted for binding).
140 is an ARP type as defined in the
144 contains the packet type.
147 for a packet addressed to the local host,
149 for a physical layer broadcast packet,
151 for a packet sent to a physical layer multicast address,
153 for a packet to some other host that has been caught by a device driver
154 in promiscuous mode, and
156 for a packet originated from the local host that is looped back to a packet
158 These types make sense only for receiving.
162 contain the physical layer (e.g., IEEE 802.3) address and its length.
163 The exact interpretation depends on the device.
165 When you send packets it is enough to specify
170 The other fields should be 0.
174 are set on received packets for your information.
181 Packet socket options are configured by calling
186 .BR PACKET_ADD_MEMBERSHIP
189 .BR PACKET_DROP_MEMBERSHIP
191 Packet sockets can be used to configure physical layer multicasting
192 and promiscuous mode.
193 .B PACKET_ADD_MEMBERSHIP
195 .B PACKET_DROP_MEMBERSHIP
199 structure as argument:
204 int mr_ifindex; /* interface index */
205 unsigned short mr_type; /* action */
206 unsigned short mr_alen; /* address length */
207 unsigned char mr_address[8]; /* physical layer address */
213 contains the interface index for the interface whose status
217 parameter specifies which action to perform.
219 enables receiving all packets on a shared medium (often known as
221 .B PACKET_MR_MULTICAST
222 binds the socket to the physical layer multicast group specified in
227 .B PACKET_MR_ALLMULTI
228 sets the socket up to receive all multicast packets arriving at
231 In addition, the traditional ioctls
235 can be used for the same purpose.
237 .BR PACKET_AUXDATA " (since Linux 2.6.21)"
238 .\" commit 8dc4194474159660d7f37c495e3fc3f10d0db8cc
239 If this binary option is enabled, the packet socket passes a metadata
240 structure along with each packet in the
243 The structure can be read with
249 struct tpacket_auxdata {
251 __u32 tp_len; /* packet length */
252 __u32 tp_snaplen; /* captured length */
261 .BR PACKET_FANOUT " (since Linux 3.1)"
262 .\" commit dc99f600698dcac69b8f56dda9a8a00d645c5ffc
263 To scale processing across threads, packet sockets can form a fanout
265 In this mode, each matching packet is enqueued onto only one
267 A socket joins a fanout group by calling
273 Each network namespace can have up to 65536 independent groups.
274 A socket selects a group by encoding the ID in the first 16 bits of
275 the integer option value.
276 The first packet socket to join a group implicitly creates it.
277 To successfully join an existing group, subsequent packet sockets
278 must have the same protocol, device settings, fanout mode and
280 Packet sockets can leave a fanout group only by closing the socket.
281 The group is deleted when the last socket is closed.
283 Fanout supports multiple algorithms to spread traffic between sockets.
285 .BR PACKET_FANOUT_HASH ,
286 sends packets from the same flow to the same socket to maintain
288 For each packet, it chooses a socket by taking the packet flow hash
289 modulo the number of sockets in the group, where a flow hash is a hash
290 over network-layer address and optional transport-layer port fields.
291 The load-balance mode
293 implements a round-robin algorithm.
294 .BR PACKET_FANOUT_CPU
295 selects the socket based on the CPU that the packet arrived on.
296 .BR PACKET_FANOUT_ROLLOVER
297 processes all data on a single socket, moves to the next when one
299 .BR PACKET_FANOUT_RND
300 selects the socket using a pseudo-random number generator.
302 .\" commit 2d36097d26b5991d71a2cf4a20c1a158f0f1bfcd
303 (available since Linux 3.14)
304 selects the socket using the recorded queue_mapping of the received skb.
306 Fanout modes can take additional options.
307 IP fragmentation causes packets from the same flow to have different
310 .BR PACKET_FANOUT_FLAG_DEFRAG ,
311 if set, causes packet to be defragmented before fanout is applied, to
312 preserve order even in this case.
313 Fanout mode and options are communicated in the second 16 bits of the
314 integer option value.
316 .BR PACKET_FANOUT_FLAG_ROLLOVER
317 enables the roll over mechanism as a backup strategy: if the
318 original fanout algorithm selects a backlogged socket, the packet
319 rolls over to the next available one.
321 .BR PACKET_LOSS " (with " PACKET_TX_RING )
322 When a malformed packet is encountered on a transmit ring,
323 the default is to reset its
326 .BR TP_STATUS_WRONG_FORMAT
327 and abort the transmission immediately.
328 The malformed packet blocks itself and subsequently enqueued packets from
330 The format error must be fixed, the associated
333 .BR TP_STATUS_SEND_REQUEST ,
334 and the transmission process restarted via
338 is set, any malformed packet will be skipped, its
341 .BR TP_STATUS_AVAILABLE ,
342 and the transmission process continued.
344 .BR PACKET_RESERVE " (with " PACKET_RX_RING )
345 By default, a packet receive ring writes packets immediately following the
346 metadata structure and alignment padding.
347 This integer option reserves additional headroom.
350 Create a memory-mapped ring buffer for asynchronous packet reception.
351 The packet socket reserves a contiguous region of application address
352 space, lays it out into an array of packet slots and copies packets
355 into subsequent slots.
356 Each packet is preceded by a metadata structure similar to
357 .IR tpacket_auxdata .
358 The protocol fields encode the offset to the data
359 from the start of the metadata header.
361 stores the offset to the network layer.
362 If the packet socket is of type
369 then that field stores the offset to the link-layer frame.
370 Packet socket and application communicate the head and tail of the ring
374 The packet socket owns all slots with
377 .BR TP_STATUS_KERNEL .
378 After filling a slot, it changes the status of the slot to transfer
379 ownership to the application.
380 During normal operation, the new
382 value has at least the
384 bit set to signal that a received packet has been stored.
385 When the application has finished processing a packet, it transfers
386 ownership of the slot back to the socket by setting
389 .BR TP_STATUS_KERNEL .
390 Packet sockets implement multiple variants of the packet ring.
391 The implementation details are described in
392 .IR Documentation/networking/packet_mmap.txt
393 in the Linux kernel source tree.
395 .BR PACKET_STATISTICS
396 Retrieve packet socket statistics in the form of a structure
400 struct tpacket_stats {
401 unsigned int tp_packets; /* Total packet count */
402 unsigned int tp_drops; /* Dropped packet count */
407 Receiving statistics resets the internal counters.
408 The statistics structure differs when using a ring of variant
411 .BR PACKET_TIMESTAMP " (with " PACKET_RX_RING "; since Linux 2.6.36)"
412 .\" commit 614f60fa9d73a9e8fdff3df83381907fea7c5649
413 The packet receive ring always stores a timestamp in the metadata header.
414 By default, this is a software generated timestamp generated when the
415 packet is copied into the ring.
416 This integer option selects the type of timestamp.
417 Besides the default, it support the two hardware formats described in
418 .IR Documentation/networking/timestamping.txt
419 in the Linux kernel source tree.
421 .BR PACKET_TX_RING " (since Linux 2.6.31)"
422 .\" commit 69e3c75f4d541a6eb151b3ef91f34033cb3ad6e1
423 Create a memory-mapped ring buffer for packet transmission.
424 This option is similar to
426 and takes the same arguments.
427 The application writes packets into slots with
430 .BR TP_STATUS_AVAILABLE
431 and schedules them for transmission by changing
434 .BR TP_STATUS_SEND_REQUEST .
435 When packets are ready to be transmitted, the application calls
437 or a variant thereof.
442 fields of this call are ignored.
443 If an address is passed using
447 then that overrides the socket default.
448 On successful transmission, the socket resets
451 .BR TP_STATUS_AVAILABLE .
452 It immediately aborts the transmission on error unless
456 .BR PACKET_VERSION " (with " PACKET_RX_RING "; since Linux 2.6.27)"
457 .\" commit bbd6ef87c544d88c30e4b762b1b61ef267a7d279
460 creates a packet receive ring of variant
462 To create another variant, configure the desired variant by setting this
463 integer option before creating the ring.
465 .BR PACKET_QDISC_BYPASS " (since Linux 3.14)"
466 .\" commit d346a3fae3ff1d99f5d0c819bf86edf9094a26a1
467 By default, packets sent through packet sockets pass through the kernel's
468 qdisc (traffic control) layer, which is fine for the vast majority of use
470 For traffic generator appliances using packet sockets
471 that intend to brute-force flood the network\(emfor example,
472 to test devices under load in a similar
473 fashion to pktgen\(emthis layer can be bypassed by setting
474 this integer option to 1.
475 A side effect is that packet buffering in the qdisc layer is avoided,
476 which will lead to increased drops when network
477 device transmit queues are busy;
478 therefore, use at your own risk.
481 can be used to receive the timestamp of the last received packet.
485 .\" FIXME Document SIOCGSTAMPNS
487 In addition, all standard ioctls defined in
491 are valid on packet sockets.
493 Packet sockets do no error handling other than errors occurred
494 while passing the packet to the device driver.
495 They don't have the concept of a pending error.
499 Unknown multicast group address passed.
502 User passed invalid memory address.
508 Packet is bigger than interface MTU.
514 Not enough memory to allocate the packet.
517 Unknown device name or interface index specified in interface address.
523 No interface address passed.
526 Interface address contained an invalid interface index.
529 User has insufficient privileges to carry out this operation.
531 In addition, other errors may be generated by the low-level driver.
534 is a new feature in Linux 2.2.
535 Earlier Linux versions supported only
539 For portable programs it is suggested to use
543 although this covers only a subset of the
549 packet sockets make no attempt to create or parse the IEEE 802.2 LLC
550 header for a IEEE 802.3 frame.
553 is specified as protocol for sending the kernel creates the
554 802.3 frame and fills out the length field; the user has to supply the LLC
555 header to get a fully conforming packet.
556 Incoming 802.3 packets are not multiplexed on the DSAP/SSAP protocol
557 fields; instead they are supplied to the user as protocol
559 with the LLC header prefixed.
560 It is thus not possible to bind to
564 instead and do the protocol multiplex yourself.
565 The default for sending is the standard Ethernet DIX
566 encapsulation with the protocol filled in.
568 Packet sockets are not subject to the input or output firewall chains.
570 In Linux 2.0, the only way to get a packet socket was by calling
571 .BI "socket(AF_INET, SOCK_PACKET, " protocol )\fR.
572 This is still supported but strongly deprecated.
573 The main difference between the two methods is that
576 .I struct sockaddr_pkt
577 to specify an interface, which doesn't provide physical layer
582 struct sockaddr_pkt {
583 unsigned short spkt_family;
584 unsigned char spkt_device[14];
585 unsigned short spkt_protocol;
594 is the IEEE 802.3 protocol type as defined in
598 is the device name as a null-terminated string, for example, eth0.
600 This structure is obsolete and should not be used in new code.
602 glibc 2.1 does not have a define for
604 The suggested workaround is to use:
609 #define SOL_PACKET 263
614 This is fixed in later glibc versions.
616 The IEEE 802.2/803.3 LLC handling could be considered as a bug.
618 Socket filters are not documented.
623 extension is an ugly hack and should be replaced by a control message.
624 There is currently no way to get the original destination address of
628 .\" This man page was written by Andi Kleen with help from Matthew Wilcox.
629 .\" AF_PACKET in Linux 2.2 was implemented
630 .\" by Alexey Kuznetsov, based on code by Alan Cox and others.
634 .BR capabilities (7),
639 RFC\ 894 for the standard IP Ethernet encapsulation.
640 RFC\ 1700 for the IEEE 802.3 IP encapsulation.
643 .I <linux/if_ether.h>
644 include file for physical layer protocols.
646 The Linux kernel source tree.
647 .IR /Documentation/networking/filter.txt
648 describes how to apply Berkeley Packet Filters to packet sockets.
649 .IR /tools/testing/selftests/net/psock_tpacket.c
650 contains example source code for all available versions of