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