Many pages: Document fixed-width types with ISO C naming
[man-pages.git] / man7 / packet.7
blobd3c1d8a7b1978c2961483a27fb7f91183e3295d3
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\"
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.
8 .\" %%%LICENSE_END
9 .\"
10 .\" $Id: packet.7,v 1.13 2000/08/14 08:03:45 ak Exp $
11 .\"
12 .TH PACKET  7 2021-03-22 "Linux man-pages (unreleased)"
13 .SH NAME
14 packet \- packet interface on device level
15 .SH SYNOPSIS
16 .nf
17 .B #include <sys/socket.h>
18 .B #include <linux/if_packet.h>
19 .B #include <net/ethernet.h>     /* the L2 protocols */
20 .PP
21 .BI "packet_socket = socket(AF_PACKET, int " socket_type ", int "protocol );
22 .fi
23 .SH DESCRIPTION
24 Packet sockets are used to receive or send raw packets at the device driver
25 (OSI Layer 2) level.
26 They allow the user to implement protocol modules in user space
27 on top of the physical layer.
28 .PP
29 The
30 .I socket_type
31 is either
32 .B SOCK_RAW
33 for raw packets including the link-level header or
34 .B SOCK_DGRAM
35 for cooked packets with the link-level header removed.
36 The link-level header information is available in a common format in a
37 .I sockaddr_ll
38 structure.
39 .I protocol
40 is the IEEE 802.3 protocol number in network byte order.
41 See the
42 .I <linux/if_ether.h>
43 include file for a list of allowed protocols.
44 When protocol
45 is set to
46 .BR htons(ETH_P_ALL) ,
47 then all protocols are received.
48 All incoming packets of that protocol type will be passed to the packet
49 socket before they are passed to the protocols implemented in the kernel.
51 .I protocol
52 is set to zero,
53 no packets are received.
54 .BR bind (2)
55 can optionally be called with a nonzero
56 .I sll_protocol
57 to start receiving packets for the protocols specified.
58 .PP
59 In order to create a packet socket, a process must have the
60 .B CAP_NET_RAW
61 capability in the user namespace that governs its network namespace.
62 .PP
63 .B SOCK_RAW
64 packets are passed to and from the device driver without any changes in
65 the packet data.
66 When receiving a packet, the address is still parsed and
67 passed in a standard
68 .I sockaddr_ll
69 address structure.
70 When transmitting a packet, the user-supplied buffer
71 should contain the physical-layer header.
72 That packet is then
73 queued unmodified to the network driver of the interface defined by the
74 destination address.
75 Some device drivers always add other headers.
76 .B SOCK_RAW
77 is similar to but not compatible with the obsolete
78 .B AF_INET/SOCK_PACKET
79 of Linux 2.0.
80 .PP
81 .B SOCK_DGRAM
82 operates on a slightly higher level.
83 The physical header is removed before the packet is passed to the user.
84 Packets sent through a
85 .B SOCK_DGRAM
86 packet socket get a suitable physical-layer header based on the
87 information in the
88 .I sockaddr_ll
89 destination address before they are queued.
90 .PP
91 By default, all packets of the specified protocol type
92 are passed to a packet socket.
93 To get packets only from a specific interface use
94 .BR bind (2)
95 specifying an address in a
96 .I struct sockaddr_ll
97 to bind the packet socket to an interface.
98 Fields used for binding are
99 .I sll_family
100 (should be
101 .BR AF_PACKET ),
102 .IR sll_protocol ,
104 .IR sll_ifindex .
107 .BR connect (2)
108 operation is not supported on packet sockets.
110 When the
111 .B MSG_TRUNC
112 flag is passed to
113 .BR recvmsg (2),
114 .BR recv (2),
116 .BR recvfrom (2),
117 the real length of the packet on the wire is always returned,
118 even when it is longer than the buffer.
119 .SS Address types
121 .I sockaddr_ll
122 structure is a device-independent physical-layer address.
124 .in +4n
126 struct sockaddr_ll {
127     unsigned short sll_family;   /* Always AF_PACKET */
128     unsigned short sll_protocol; /* Physical\-layer protocol */
129     int            sll_ifindex;  /* Interface number */
130     unsigned short sll_hatype;   /* ARP hardware type */
131     unsigned char  sll_pkttype;  /* Packet type */
132     unsigned char  sll_halen;    /* Length of address */
133     unsigned char  sll_addr[8];  /* Physical\-layer address */
138 The fields of this structure are as follows:
139 .IP * 3
140 .I sll_protocol
141 is the standard ethernet protocol type in network byte order as defined
142 in the
143 .I <linux/if_ether.h>
144 include file.
145 It defaults to the socket's protocol.
146 .IP *
147 .I sll_ifindex
148 is the interface index of the interface
149 (see
150 .BR netdevice (7));
151 0 matches any interface (only permitted for binding).
152 .I sll_hatype
153 is an ARP type as defined in the
154 .I <linux/if_arp.h>
155 include file.
156 .IP *
157 .I sll_pkttype
158 contains the packet type.
159 Valid types are
160 .B PACKET_HOST
161 for a packet addressed to the local host,
162 .B PACKET_BROADCAST
163 for a physical-layer broadcast packet,
164 .B PACKET_MULTICAST
165 for a packet sent to a physical-layer multicast address,
166 .B PACKET_OTHERHOST
167 for a packet to some other host that has been caught by a device driver
168 in promiscuous mode, and
169 .B PACKET_OUTGOING
170 for a packet originating from the local host that is looped back to a packet
171 socket.
172 These types make sense only for receiving.
173 .IP *
174 .I sll_addr
176 .I sll_halen
177 contain the physical-layer (e.g., IEEE 802.3) address and its length.
178 The exact interpretation depends on the device.
180 When you send packets, it is enough to specify
181 .IR sll_family ,
182 .IR sll_addr ,
183 .IR sll_halen ,
184 .IR sll_ifindex ,
186 .IR sll_protocol .
187 The other fields should be 0.
188 .I sll_hatype
190 .I sll_pkttype
191 are set on received packets for your information.
192 .SS Socket options
193 Packet socket options are configured by calling
194 .BR setsockopt (2)
195 with level
196 .BR SOL_PACKET .
198 .B PACKET_ADD_MEMBERSHIP
199 .PD 0
201 .B PACKET_DROP_MEMBERSHIP
203 Packet sockets can be used to configure physical-layer multicasting
204 and promiscuous mode.
205 .B PACKET_ADD_MEMBERSHIP
206 adds a binding and
207 .B PACKET_DROP_MEMBERSHIP
208 drops it.
209 They both expect a
210 .I packet_mreq
211 structure as argument:
213 .in +4n
215 struct packet_mreq {
216     int            mr_ifindex;    /* interface index */
217     unsigned short mr_type;       /* action */
218     unsigned short mr_alen;       /* address length */
219     unsigned char  mr_address[8]; /* physical\-layer address */
224 .I mr_ifindex
225 contains the interface index for the interface whose status
226 should be changed.
228 .I mr_type
229 field specifies which action to perform.
230 .B PACKET_MR_PROMISC
231 enables receiving all packets on a shared medium (often known as
232 "promiscuous mode"),
233 .B PACKET_MR_MULTICAST
234 binds the socket to the physical-layer multicast group specified in
235 .I mr_address
237 .IR mr_alen ,
239 .B PACKET_MR_ALLMULTI
240 sets the socket up to receive all multicast packets arriving at
241 the interface.
243 In addition, the traditional ioctls
244 .BR SIOCSIFFLAGS ,
245 .BR SIOCADDMULTI ,
246 .B SIOCDELMULTI
247 can be used for the same purpose.
249 .BR PACKET_AUXDATA " (since Linux 2.6.21)"
250 .\" commit 8dc4194474159660d7f37c495e3fc3f10d0db8cc
251 If this binary option is enabled, the packet socket passes a metadata
252 structure along with each packet in the
253 .BR recvmsg (2)
254 control field.
255 The structure can be read with
256 .BR cmsg (3).
257 It is defined as
259 .in +4n
261 struct tpacket_auxdata {
262     uint32_t tp_status;
263     uint32_t tp_len;       /* packet length */
264     uint32_t tp_snaplen;   /* captured length */
265     uint16_t tp_mac;
266     uint16_t tp_net;
267     uint16_t tp_vlan_tci;
268     uint16_t tp_vlan_tpid; /* Since Linux 3.14; earlier, these
269                               were unused padding bytes */
270 .\" commit a0cdfcf39362410d5ea983f4daf67b38de129408 added tp_vlan_tpid
275 .BR PACKET_FANOUT " (since Linux 3.1)"
276 .\" commit dc99f600698dcac69b8f56dda9a8a00d645c5ffc
277 To scale processing across threads, packet sockets can form a fanout
278 group.
279 In this mode, each matching packet is enqueued onto only one
280 socket in the group.
281 A socket joins a fanout group by calling
282 .BR setsockopt (2)
283 with level
284 .B SOL_PACKET
285 and option
286 .BR PACKET_FANOUT .
287 Each network namespace can have up to 65536 independent groups.
288 A socket selects a group by encoding the ID in the first 16 bits of
289 the integer option value.
290 The first packet socket to join a group implicitly creates it.
291 To successfully join an existing group, subsequent packet sockets
292 must have the same protocol, device settings, fanout mode, and
293 flags (see below).
294 Packet sockets can leave a fanout group only by closing the socket.
295 The group is deleted when the last socket is closed.
297 Fanout supports multiple algorithms to spread traffic between sockets,
298 as follows:
300 .IP * 3
301 The default mode,
302 .BR PACKET_FANOUT_HASH ,
303 sends packets from the same flow to the same socket to maintain
304 per-flow ordering.
305 For each packet, it chooses a socket by taking the packet flow hash
306 modulo the number of sockets in the group, where a flow hash is a hash
307 over network-layer address and optional transport-layer port fields.
308 .IP *
309 The load-balance mode
310 .B PACKET_FANOUT_LB
311 implements a round-robin algorithm.
312 .IP *
313 .B PACKET_FANOUT_CPU
314 selects the socket based on the CPU that the packet arrived on.
315 .IP *
316 .B PACKET_FANOUT_ROLLOVER
317 processes all data on a single socket, moving to the next when one
318 becomes backlogged.
319 .IP *
320 .B PACKET_FANOUT_RND
321 selects the socket using a pseudo-random number generator.
322 .IP *
323 .B PACKET_FANOUT_QM
324 .\" commit 2d36097d26b5991d71a2cf4a20c1a158f0f1bfcd
325 (available since Linux 3.14)
326 selects the socket using the recorded queue_mapping of the received skb.
329 Fanout modes can take additional options.
330 IP fragmentation causes packets from the same flow to have different
331 flow hashes.
332 The flag
333 .BR PACKET_FANOUT_FLAG_DEFRAG ,
334 if set, causes packets to be defragmented before fanout is applied, to
335 preserve order even in this case.
336 Fanout mode and options are communicated in the second 16 bits of the
337 integer option value.
338 The flag
339 .B PACKET_FANOUT_FLAG_ROLLOVER
340 enables the roll over mechanism as a backup strategy: if the
341 original fanout algorithm selects a backlogged socket, the packet
342 rolls over to the next available one.
344 .BR PACKET_LOSS " (with " PACKET_TX_RING )
345 When a malformed packet is encountered on a transmit ring,
346 the default is to reset its
347 .I tp_status
349 .B TP_STATUS_WRONG_FORMAT
350 and abort the transmission immediately.
351 The malformed packet blocks itself and subsequently enqueued packets from
352 being sent.
353 The format error must be fixed, the associated
354 .I tp_status
355 reset to
356 .BR TP_STATUS_SEND_REQUEST ,
357 and the transmission process restarted via
358 .BR send (2).
359 However, if
360 .B PACKET_LOSS
361 is set, any malformed packet will be skipped, its
362 .I tp_status
363 reset to
364 .BR TP_STATUS_AVAILABLE ,
365 and the transmission process continued.
367 .BR PACKET_RESERVE " (with " PACKET_RX_RING )
368 By default, a packet receive ring writes packets immediately following the
369 metadata structure and alignment padding.
370 This integer option reserves additional headroom.
372 .B PACKET_RX_RING
373 Create a memory-mapped ring buffer for asynchronous packet reception.
374 The packet socket reserves a contiguous region of application address
375 space, lays it out into an array of packet slots and copies packets
376 (up to
377 .IR tp_snaplen )
378 into subsequent slots.
379 Each packet is preceded by a metadata structure similar to
380 .IR tpacket_auxdata .
381 The protocol fields encode the offset to the data
382 from the start of the metadata header.
383 .I tp_net
384 stores the offset to the network layer.
385 If the packet socket is of type
386 .BR SOCK_DGRAM ,
387 then
388 .I tp_mac
389 is the same.
390 If it is of type
391 .BR SOCK_RAW ,
392 then that field stores the offset to the link-layer frame.
393 Packet socket and application communicate the head and tail of the ring
394 through the
395 .I tp_status
396 field.
397 The packet socket owns all slots with
398 .I tp_status
399 equal to
400 .BR TP_STATUS_KERNEL .
401 After filling a slot, it changes the status of the slot to transfer
402 ownership to the application.
403 During normal operation, the new
404 .I tp_status
405 value has at least the
406 .B TP_STATUS_USER
407 bit set to signal that a received packet has been stored.
408 When the application has finished processing a packet, it transfers
409 ownership of the slot back to the socket by setting
410 .I tp_status
411 equal to
412 .BR TP_STATUS_KERNEL .
414 Packet sockets implement multiple variants of the packet ring.
415 The implementation details are described in
416 .I Documentation/networking/packet_mmap.rst
417 in the Linux kernel source tree.
419 .B PACKET_STATISTICS
420 Retrieve packet socket statistics in the form of a structure
422 .in +4n
424 struct tpacket_stats {
425     unsigned int tp_packets;  /* Total packet count */
426     unsigned int tp_drops;    /* Dropped packet count */
431 Receiving statistics resets the internal counters.
432 The statistics structure differs when using a ring of variant
433 .BR TPACKET_V3 .
435 .BR PACKET_TIMESTAMP " (with " PACKET_RX_RING "; since Linux 2.6.36)"
436 .\" commit 614f60fa9d73a9e8fdff3df83381907fea7c5649
437 The packet receive ring always stores a timestamp in the metadata header.
438 By default, this is a software generated timestamp generated when the
439 packet is copied into the ring.
440 This integer option selects the type of timestamp.
441 Besides the default, it support the two hardware formats described in
442 .I Documentation/networking/timestamping.rst
443 in the Linux kernel source tree.
445 .BR PACKET_TX_RING " (since Linux 2.6.31)"
446 .\" commit 69e3c75f4d541a6eb151b3ef91f34033cb3ad6e1
447 Create a memory-mapped ring buffer for packet transmission.
448 This option is similar to
449 .B PACKET_RX_RING
450 and takes the same arguments.
451 The application writes packets into slots with
452 .I tp_status
453 equal to
454 .B TP_STATUS_AVAILABLE
455 and schedules them for transmission by changing
456 .I tp_status
458 .BR TP_STATUS_SEND_REQUEST .
459 When packets are ready to be transmitted, the application calls
460 .BR send (2)
461 or a variant thereof.
463 .I buf
465 .I len
466 fields of this call are ignored.
467 If an address is passed using
468 .BR sendto (2)
470 .BR sendmsg (2),
471 then that overrides the socket default.
472 On successful transmission, the socket resets
473 .I tp_status
475 .BR TP_STATUS_AVAILABLE .
476 It immediately aborts the transmission on error unless
477 .B PACKET_LOSS
478 is set.
480 .BR PACKET_VERSION " (with " PACKET_RX_RING "; since Linux 2.6.27)"
481 .\" commit bbd6ef87c544d88c30e4b762b1b61ef267a7d279
482 By default,
483 .B PACKET_RX_RING
484 creates a packet receive ring of variant
485 .BR TPACKET_V1 .
486 To create another variant, configure the desired variant by setting this
487 integer option before creating the ring.
489 .BR PACKET_QDISC_BYPASS " (since Linux 3.14)"
490 .\" commit d346a3fae3ff1d99f5d0c819bf86edf9094a26a1
491 By default, packets sent through packet sockets pass through the kernel's
492 qdisc (traffic control) layer, which is fine for the vast majority of use
493 cases.
494 For traffic generator appliances using packet sockets
495 that intend to brute-force flood the network\(emfor example,
496 to test devices under load in a similar
497 fashion to pktgen\(emthis layer can be bypassed by setting
498 this integer option to 1.
499 A side effect is that packet buffering in the qdisc layer is avoided,
500 which will lead to increased drops when network
501 device transmit queues are busy;
502 therefore, use at your own risk.
503 .SS Ioctls
504 .B SIOCGSTAMP
505 can be used to receive the timestamp of the last received packet.
506 Argument is a
507 .I struct timeval
508 variable.
509 .\" FIXME Document SIOCGSTAMPNS
511 In addition, all standard ioctls defined in
512 .BR netdevice (7)
514 .BR socket (7)
515 are valid on packet sockets.
516 .SS Error handling
517 Packet sockets do no error handling other than errors occurred
518 while passing the packet to the device driver.
519 They don't have the concept of a pending error.
520 .SH ERRORS
522 .B EADDRNOTAVAIL
523 Unknown multicast group address passed.
525 .B EFAULT
526 User passed invalid memory address.
528 .B EINVAL
529 Invalid argument.
531 .B EMSGSIZE
532 Packet is bigger than interface MTU.
534 .B ENETDOWN
535 Interface is not up.
537 .B ENOBUFS
538 Not enough memory to allocate the packet.
540 .B ENODEV
541 Unknown device name or interface index specified in interface address.
543 .B ENOENT
544 No packet received.
546 .B ENOTCONN
547 No interface address passed.
549 .B ENXIO
550 Interface address contained an invalid interface index.
552 .B EPERM
553 User has insufficient privileges to carry out this operation.
555 In addition, other errors may be generated by the low-level driver.
556 .SH VERSIONS
557 .B AF_PACKET
558 is a new feature in Linux 2.2.
559 Earlier Linux versions supported only
560 .BR SOCK_PACKET .
561 .SH NOTES
562 For portable programs it is suggested to use
563 .B AF_PACKET
565 .BR pcap (3);
566 although this covers only a subset of the
567 .B AF_PACKET
568 features.
571 .B SOCK_DGRAM
572 packet sockets make no attempt to create or parse the IEEE 802.2 LLC
573 header for a IEEE 802.3 frame.
574 When
575 .B ETH_P_802_3
576 is specified as protocol for sending the kernel creates the
577 802.3 frame and fills out the length field; the user has to supply the LLC
578 header to get a fully conforming packet.
579 Incoming 802.3 packets are not multiplexed on the DSAP/SSAP protocol
580 fields; instead they are supplied to the user as protocol
581 .B ETH_P_802_2
582 with the LLC header prefixed.
583 It is thus not possible to bind to
584 .BR ETH_P_802_3 ;
585 bind to
586 .B ETH_P_802_2
587 instead and do the protocol multiplex yourself.
588 The default for sending is the standard Ethernet DIX
589 encapsulation with the protocol filled in.
591 Packet sockets are not subject to the input or output firewall chains.
592 .SS Compatibility
593 In Linux 2.0, the only way to get a packet socket was with the call:
595 .in +4n
597 socket(AF_INET, SOCK_PACKET, protocol)
601 This is still supported, but deprecated and strongly discouraged.
602 The main difference between the two methods is that
603 .B SOCK_PACKET
604 uses the old
605 .I struct sockaddr_pkt
606 to specify an interface, which doesn't provide physical-layer
607 independence.
609 .in +4n
611 struct sockaddr_pkt {
612     unsigned short spkt_family;
613     unsigned char  spkt_device[14];
614     unsigned short spkt_protocol;
619 .I spkt_family
620 contains
621 the device type,
622 .I spkt_protocol
623 is the IEEE 802.3 protocol type as defined in
624 .I <sys/if_ether.h>
626 .I spkt_device
627 is the device name as a null-terminated string, for example, eth0.
629 This structure is obsolete and should not be used in new code.
630 .SH BUGS
631 .SS LLC header handling
632 The IEEE 802.2/803.3 LLC handling could be considered as a bug.
633 .SS MSG_TRUNC issues
635 .B MSG_TRUNC
636 .BR recvmsg (2)
637 extension is an ugly hack and should be replaced by a control message.
638 There is currently no way to get the original destination address of
639 packets via
640 .BR SOCK_DGRAM .
641 .SS spkt_device device name truncation
643 .I spkt_device
644 field of
645 .I sockaddr_pkt
646 has a size of 14 bytes,
647 which is less than the constant
648 .B IFNAMSIZ
649 defined in
650 .I <net/if.h>
651 which is 16 bytes and describes the system limit for a network interface name.
652 This means the names of network devices longer than 14 bytes
653 will be truncated to fit into
654 .IR spkt_device .
655 All these lengths include the terminating null byte (\(aq\e0\(aq)).
657 Issues from this with old code typically show up with
658 very long interface names used by the
659 .B Predictable Network Interface Names
660 feature enabled by default in many modern Linux distributions.
662 The preferred solution is to rewrite code to avoid
663 .BR SOCK_PACKET .
664 Possible user solutions are to disable
665 .B Predictable Network Interface Names
666 or to rename the interface to a name of at most 13 bytes,
667 for example using the
668 .BR ip (8)
669 tool.
670 .SS Documentation issues
671 Socket filters are not documented.
672 .\" .SH CREDITS
673 .\" This man page was written by Andi Kleen with help from Matthew Wilcox.
674 .\" AF_PACKET in Linux 2.2 was implemented
675 .\" by Alexey Kuznetsov, based on code by Alan Cox and others.
676 .SH SEE ALSO
677 .BR socket (2),
678 .BR pcap (3),
679 .BR capabilities (7),
680 .BR ip (7),
681 .BR raw (7),
682 .BR socket (7),
683 .BR ip (8),
685 RFC\ 894 for the standard IP Ethernet encapsulation.
686 RFC\ 1700 for the IEEE 802.3 IP encapsulation.
689 .I <linux/if_ether.h>
690 include file for physical-layer protocols.
692 The Linux kernel source tree.
693 .I Documentation/networking/filter.rst
694 describes how to apply Berkeley Packet Filters to packet sockets.
695 .I tools/testing/selftests/net/psock_tpacket.c
696 contains example source code for all available versions of
697 .B PACKET_RX_RING
699 .BR PACKET_TX_RING .