Penguin
Annotated edit history of packet(7) version 2, including all changes. View license author blame.
Rev Author # Line
1 perry 1 !!NAME
2 packet, PF_PACKET - packet interface on device level.
2 PerryLorier 3
1 perry 4 !!SYNOPSIS
2 PerryLorier 5 #include <sys/socket.h>
6 #include <features.h> /* for the glibc version number */
7
8 #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
9 # include <netpacket/packet.h>
10 # include <net/ethernet.h> /* the L2 protocols */
11 #else
12 # include <asm/types.h>
13 # include <linux/if_packet.h>
14 # include <linux/if_ether.h> /* The L2 protocols */
15 #endif
1 perry 16
2 PerryLorier 17 packet_socket = socket(PF_PACKET, int socket_type, int protocol);
1 perry 18
19 !!DESCRIPTION
2 PerryLorier 20 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.
1 perry 21
2 PerryLorier 22 The ''socket_type'' is either __SOCK_RAW__ for raw packets including the link level header or __SOCK_DGRAM__ for cooked packets with the link level header removed. The link level header information is available in a common format in a __sockaddr_ll__. ''protocol'' is the IEEE 802.3 protocol number in network order. See the __<linux/if_ether.h>__ include file for a list of allowed protocols. When protocol is set to __htons(ETH_P_ALL)__ 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.
1 perry 23
2 PerryLorier 24 Only processes with effective uid 0 or the __CAP_NET_RAW__ capability may open packet sockets.
1 perry 25
2 PerryLorier 26 __SOCK_RAW__ 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 __sockaddr_ll__ 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. __SOCK_RAW__ is similar to but not compatible with the obsolete __SOCK_PACKET__ of Linux 2.0.
1 perry 27
2 PerryLorier 28 __SOCK_DGRAM__ operates on a slightly higher level. The physical header is removed before the packet is passed to the user. Packets sent through a __SOCK_DGRAM__ packet socket get a suitable physical layer header based on the information in the __sockaddr_ll__ destination address before they are queued.
1 perry 29
2 PerryLorier 30 By default all packets of the specified protocol type are passed to a packet socket. To only get packets from a specific interface use bind(2) specifying an address in a __struct sockaddr_ll__ to bind the packet socket to an interface. Only the __sll_protocol__ and the __sll_ifindex__ address fields are used for purposes of binding.
1 perry 31
2 PerryLorier 32 The connect(2) operation is not supported on packet sockets.
1 perry 33
2 PerryLorier 34 When the __MSG_TRUNC__ 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.
1 perry 35
36 !!ADDRESS TYPES
2 PerryLorier 37 The sockaddr_ll is a device independent physical layer address.
1 perry 38
2 PerryLorier 39 struct sockaddr_ll {
40 unsigned short sll_family; /* Always AF_PACKET */
41 unsigned short sll_protocol; /* Physical layer protocol */
42 int sll_ifindex; /* Interface number */
43 unsigned short sll_hatype; /* Header type */
44 unsigned char sll_pkttype; /* Packet type */
45 unsigned char sll_halen; /* Length of address */
46 unsigned char sll_addr[[8]; /* Physical layer address */
47 };
1 perry 48
2 PerryLorier 49 __sll_protocol__ is the standard ethernet protocol type in network order as defined in the __linux/if_ether.h__ include file. It defaults to the socket's protocol.
50 __sll_ifindex__ is the interface index of the interface (see netdevice(2)); 0 matches any interface (only legal for binding). __sll_hatype__ is a ARP type as defined in the __linux/if_arp.h__ include file. __sll_pkttype__ contains the packet type. Valid types are __PACKET_HOST__ for a packet addressed to the local host, __PACKET_BROADCAST__ for a physical layer broadcast packet, __PACKET_MULTICAST__ for a packet sent to a physical layer multicast address, __PACKET_OTHERHOST__ for a packet to some other host that has been caught by a device driver in promiscuous mode, and __PACKET_OUTGOING__ for a packet originated from the local host that is looped back to a packet socket. These types make only sense for receiving. __sll_addr__ and __sll_halen__ contain the physical layer (e.g. IEEE 802.3) address and its length. The exact interpretation depends on the device.
1 perry 51
2 PerryLorier 52 When you send packets it is enough to specify __sll_family__, __sll_addr__, __sll_halen__, __sll_ifindex__. The other fields should be 0. __sll_hatype__ and __sll_pkttype__ are set on received packets for your information. For bind only __sll_protocol__ and __sll_ifindex__ are used.
1 perry 53
54 !!SOCKET OPTIONS
2 PerryLorier 55 Packet sockets can be used to configure physical layer multicasting and promiscuous mode. It works by calling setsockopt(2) on a packet socket for SOL_PACKET and one of the options __PACKET_ADD_MEMBERSHIP__ to add a binding or __PACKET_DROP_MEMBERSHIP__ to drop it. They both expect a __packet_mreq__ structure as argument:
1 perry 56
2 PerryLorier 57 struct packet_mreq
58 {
59 int mr_ifindex; /* interface index */
60 unsigned short mr_type; /* action */
61 unsigned short mr_alen; /* address length */
62 unsigned char mr_address[[8]; /* physical layer address */
63 };
1 perry 64
2 PerryLorier 65 __mr_ifindex__ contains the interface index for the interface whose status should be changed. The __mr_type__ parameter specifies which action to perform.
1 perry 66
2 PerryLorier 67 __PACKET_MR_PROMISC__ enables receiving all packets on a shared medium - often known as ``promiscuous mode'', __PACKET_MR_MULTICAST__ binds the socket to the physical
68 layer multicast group specified in __mr_address__ and __mr_alen__, and __PACKET_MR_ALLMULTI__ sets the socket up to receive all multicast packets arriving at the interface.
1 perry 69
2 PerryLorier 70 In addition the traditional ioctls __SIOCSIFFLAGS, SIOCADDMULTI, SIOCDELMULTI__ can be used for the same purpose.
1 perry 71
72 !!IOCTLS
2 PerryLorier 73 [SIOCGSTAMP] can be used to receive the time stamp of the last received packet. Argument is a __struct timeval.__
1 perry 74
2 PerryLorier 75 In addition all standard ioctls defined in netdevice(7) and socket(7) are valid on packet sockets.
1 perry 76
77 !!ERROR HANDLING
2 PerryLorier 78 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.
1 perry 79
80 !!COMPATIBILITY
2 PerryLorier 81 In Linux 2.0, the only way to get a packet socket was by calling __socket(PF_INET, SOCK_PACKET,__ ''protocol''__)__. This is still supported but strongly deprecated. The main difference between the two methods is that __SOCK_PACKET__ uses the old __struct sockaddr_pkt__ to specify an interface, which doesn't provide physical layer independence.
1 perry 82
2 PerryLorier 83 struct sockaddr_pkt
84 {
85 unsigned short spkt_family;
86 unsigned char spkt_device[[14];
87 unsigned short spkt_protocol;
88 };
1 perry 89
90
2 PerryLorier 91 __spkt_family__ contains the device type, __spkt_protocol__ is the IEEE 802.3 protocol type as defined in __<sys/if_ether.h>__ and __spkt_device__ is the device name as a null terminated string, e.g. eth0.
1 perry 92
2 PerryLorier 93 This structure is obsolete and should not be used in new code.
1 perry 94
95 !!NOTES
2 PerryLorier 96 For portable programs it is suggested to use __PF_PACKET__ via pcap(3); although this only covers a subset of the __PF_PACKET__ features.
1 perry 97
2 PerryLorier 98 The __SOCK_DGRAM__ packet sockets make no attempt to create or parse the IEEE 802.2 LLC header for a IEEE 802.3 frame. When __ETH_P_802_3__ 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 __ETH_P_802_2__ with the LLC header prepended. It is thus not possible to bind to __ETH_P_802_3;__ bind to __ETH_P_802_2__ instead and do the protocol multiplex yourself. The default for sending is the standard Ethernet DIX encapsulation with the protocol filled in.
1 perry 99
2 PerryLorier 100 Packet sockets are not subject to the input or output firewall chains.
1 perry 101
102 !!ERRORS
2 PerryLorier 103 ;[ENETDOWN]: Interface is not up.
104 ;[ENOTCONN]: No interface address passed.
105 ;[ENODEV]: Unknown device name or interface index specified in interface address.
106 ;[EMSGSIZE]: Packet is bigger than interface MTU.
107 ;[ENOBUFS]: Not enough memory to allocate the packet.
108 ;[EFAULT]: User passed invalid memory address.
109 ;[EINVAL]: Invalid argument.
110 ;[ENXIO]: Interface address contained illegal interface index.
111 ;[EPERM]: User has insufficient privileges to carry out this operation.
112 ;[EADDRNOTAVAIL]: Unknown multicast group address passed.
113 ;[ENOENT]: No packet received.
1 perry 114
2 PerryLorier 115 In addition other errors may be generated by the low-level driver.
1 perry 116
117 !!VERSIONS
2 PerryLorier 118 __PF_PACKET__ is a new feature in Linux 2.2. Earlier Linux versions supported only __SOCK_PACKET.__
1 perry 119
120 !!BUGS
2 PerryLorier 121 glibc 2.1 does not have a define for __SOL_PACKET.__ The suggested workaround is to use
1 perry 122
2 PerryLorier 123 #ifndef SOL_PACKET
124 #define SOL_PACKET 263
125 #endif
1 perry 126
2 PerryLorier 127 This is fixed in later glibc versions and also does not occur on libc5 systems.
1 perry 128
2 PerryLorier 129 The IEEE 802.2/803.3 LLC handling could be considered as a bug.
1 perry 130
131 Socket filters are not documented.
132
2 PerryLorier 133 The ''MSG_TRUNC'' recvmsg 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 SOCK_DGRAM.
1 perry 134
135 !!CREDITS
2 PerryLorier 136 This man page was written by Andi Kleen with help from Matthew Wilcox. PF_PACKET in Linux 2.2 was implemented by Alexey Kuznetsov, based on code by Alan Cox and
137 others.
1 perry 138
139 !!SEE ALSO
2 PerryLorier 140 ip(7), socket(7), socket(2), raw(7), pcap(3)
1 perry 141
2 PerryLorier 142 RFC:894 for the standard IP Ethernet encapsulation. RFC:1700 for the IEEE 802.3 IP encapsulation.
1 perry 143
2 PerryLorier 144 The ''<linux/if_ether.h>'' include file for physical layer protocols.
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 15 times)