]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/packet.7
Import of man-pages 1.70
[thirdparty/man-pages.git] / man7 / packet.7
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
6 .\" $Id: packet.7,v 1.13 2000/08/14 08:03:45 ak Exp $
7 .TH PACKET 7 1999-04-29 "Linux Man Page" "Linux Programmer's Manual"
8 .SH NAME
9 packet, PF_PACKET \- packet interface on device level.
10 .SH SYNOPSIS
11 .nf
12 .B #include <sys/socket.h>
13 .br
14 .B #include <netpacket/packet.h>
15 .br
16 .B #include <net/ethernet.h> /* the L2 protocols */
17 .sp
18 .BI "packet_socket = socket(PF_PACKET, int " socket_type ", int "protocol );
19 .fi
20 .SH DESCRIPTION
21 Packet sockets are used to receive or send raw packets at the device driver
22 (OSI Layer 2)
23 level. They allow the user to implement protocol modules in user space
24 on top of the physical layer.
25
26 The
27 .I socket_type
28 is either
29 .B SOCK_RAW
30 for raw packets including the link level header or
31 .B SOCK_DGRAM
32 for cooked packets with the link level header removed. The link level
33 header information is available in a common format in a
34 .BR sockaddr_ll .
35 .I protocol
36 is the IEEE 802.3 protocol number in network order. See the
37 .B <linux/if_ether.h>
38 include file for a list of allowed protocols. When protocol
39 is set to
40 .B htons(ETH_P_ALL)
41 then all protocols are received.
42 All incoming packets of that protocol type will be passed to the packet
43 socket before they are passed to the protocols implemented in the kernel.
44
45 Only processes with effective uid 0 or the
46 .B CAP_NET_RAW
47 capability may open packet sockets.
48
49 .B SOCK_RAW
50 packets are passed to and from the device driver without any changes in
51 the packet data. When receiving a packet, the address is still parsed and
52 passed in a standard
53 .B sockaddr_ll
54 address structure. When transmitting a packet, the user supplied buffer
55 should contain the physical layer header. That packet is then
56 queued unmodified to the network driver of the interface defined by the
57 destination address. Some device drivers always add other headers.
58 .B SOCK_RAW
59 is similar to but not compatible with the obsolete
60 .B SOCK_PACKET
61 of Linux 2.0.
62
63 .B SOCK_DGRAM
64 operates on a slightly higher level. The physical header is removed before
65 the packet is passed to the user. Packets sent through a
66 .B SOCK_DGRAM
67 packet socket get a suitable physical layer header based on the information
68 in the
69 .B sockaddr_ll
70 destination address before they are queued.
71
72 By default all packets of the specified protocol type
73 are passed to a packet socket. To only get packets from a specific interface
74 use
75 .BR bind (2)
76 specifying an address in a
77 .B struct sockaddr_ll
78 to bind the packet socket to an interface. Only the
79 .B sll_protocol
80 and the
81 .B sll_ifindex
82 address fields are used for purposes of binding.
83
84 The
85 .BR connect (2)
86 operation is not supported on packet sockets.
87
88 When the
89 .B MSG_TRUNC
90 flag is passed to
91 .BR recvmsg (2),
92 .BR recv (2),
93 .BR recvfrom (2)
94 the real length of the packet on the wire is always returned, even when it
95 is longer than the buffer.
96
97 .SH "ADDRESS TYPES"
98 The sockaddr_ll is a device independent physical layer address.
99
100 .RS
101 .nf
102 .ta 4n 20n 35n
103 struct sockaddr_ll {
104 unsigned short sll_family; /* Always AF_PACKET */
105 unsigned short sll_protocol; /* Physical layer protocol */
106 int sll_ifindex; /* Interface number */
107 unsigned short sll_hatype; /* Header type */
108 unsigned char sll_pkttype; /* Packet type */
109 unsigned char sll_halen; /* Length of address */
110 unsigned char sll_addr[8]; /* Physical layer address */
111 };
112 .ta
113 .fi
114 .RE
115
116 .B sll_protocol
117 is the standard ethernet protocol type in network order as defined
118 in the
119 .B linux/if_ether.h
120 include file. It defaults to the socket's protocol.
121 .B sll_ifindex
122 is the interface index of the interface
123 (see
124 .BR netdevice (7));
125 0 matches any interface (only legal for binding).
126 .B sll_hatype
127 is a ARP type as defined in the
128 .B linux/if_arp.h
129 include file.
130 .B sll_pkttype
131 contains the packet type. Valid types are
132 .B PACKET_HOST
133 for a packet addressed to the local host,
134 .B PACKET_BROADCAST
135 for a physical layer broadcast packet,
136 .B PACKET_MULTICAST
137 for a packet sent to a physical layer multicast address,
138 .B PACKET_OTHERHOST
139 for a packet to some other host that has been caught by a device driver
140 in promiscuous mode, and
141 .B PACKET_OUTGOING
142 for a packet originated from the local host that is looped back to a packet
143 socket. These types make only sense for receiving.
144 .B sll_addr
145 and
146 .B sll_halen
147 contain the physical layer (e.g. IEEE 802.3) address and its length. The
148 exact interpretation depends on the device.
149
150 When you send packets it is enough to specify
151 .BR sll_family ,
152 .BR sll_addr ,
153 .BR sll_halen ,
154 .BR sll_ifindex .
155 The other fields should be 0.
156 .B sll_hatype
157 and
158 .B sll_pkttype
159 are set on received packets for your information.
160 For bind only
161 .B sll_protocol
162 and
163 .B sll_ifindex
164 are used.
165
166 .SH "SOCKET OPTIONS"
167 Packet sockets can be used to configure physical layer multicasting
168 and promiscuous mode. It works by calling
169 .BR setsockopt (2)
170 on a packet socket for SOL_PACKET and one of the options
171 .B PACKET_ADD_MEMBERSHIP
172 to add a binding or
173 .B PACKET_DROP_MEMBERSHIP
174 to drop it.
175 They both expect a
176 .B packet_mreq
177 structure as argument:
178
179 .RS
180 .nf
181 .ta 4n 20n 35n
182 struct packet_mreq
183 {
184 int mr_ifindex; /* interface index */
185 unsigned short mr_type; /* action */
186 unsigned short mr_alen; /* address length */
187 unsigned char mr_address[8]; /* physical layer address */
188 };
189 .ta
190 .fi
191 .RE
192
193 .B mr_ifindex
194 contains the interface index for the interface whose status
195 should be changed.
196 The
197 .B mr_type
198 parameter specifies which action to perform.
199 .B PACKET_MR_PROMISC
200 enables receiving all packets on a shared medium - often known as
201 ``promiscuous mode'',
202 .B PACKET_MR_MULTICAST
203 binds the socket to the physical layer multicast group specified in
204 .B mr_address
205 and
206 .BR mr_alen ,
207 and
208 .B PACKET_MR_ALLMULTI
209 sets the socket up to receive all multicast packets arriving at the interface.
210
211 In addition the traditional ioctls
212 .B SIOCSIFFLAGS,
213 .B SIOCADDMULTI,
214 .B SIOCDELMULTI
215 can be used for the same purpose.
216
217
218 .SH IOCTLS
219 .B SIOCGSTAMP
220 can be used to receive the time stamp of the last received packet. Argument
221 is a
222 .B struct timeval.
223
224 In addition all standard ioctls defined in
225 .BR netdevice (7)
226 and
227 .BR socket (7)
228 are valid on packet sockets.
229
230 .SH "ERROR HANDLING"
231 Packet sockets do no error handling other than errors occurred while passing
232 the packet to the device driver. They don't have the concept of a pending
233 error.
234
235 .SH COMPATIBILITY
236 In Linux 2.0, the only way to get a packet socket was by calling
237 .BI "socket(PF_INET, SOCK_PACKET, " protocol )\fR.
238 This is still supported but strongly deprecated.
239 The main difference between the two methods is that
240 .B SOCK_PACKET
241 uses the old
242 .B struct sockaddr_pkt
243 to specify an interface, which doesn't provide physical layer independence.
244
245 .RS
246 .nf
247 .ta 4n 20n 35n
248 struct sockaddr_pkt
249 {
250 unsigned short spkt_family;
251 unsigned char spkt_device[14];
252 unsigned short spkt_protocol;
253 };
254 .ta
255 .fi
256 .RE
257
258 .B spkt_family
259 contains
260 the device type,
261 .B spkt_protocol
262 is the IEEE 802.3 protocol type as defined in
263 .B <sys/if_ether.h>
264 and
265 .B spkt_device
266 is the device name as a null terminated string, e.g. eth0.
267
268 This structure is obsolete and should not be used in new code.
269
270 .SH NOTES
271 For portable programs it is suggested to use
272 .B PF_PACKET
273 via
274 .BR pcap (3);
275 although this only covers a subset of the
276 .B PF_PACKET
277 features.
278
279 The
280 .B SOCK_DGRAM
281 packet sockets make no attempt to create or parse the IEEE 802.2 LLC header
282 for a IEEE 802.3 frame.
283 When
284 .B ETH_P_802_3
285 is specified as protocol for sending the kernel creates the
286 802.3 frame and fills out the length field; the user has to supply the LLC
287 header to get a fully conforming packet. Incoming 802.3 packets are not
288 multiplexed on the DSAP/SSAP protocol fields; instead they are supplied to the
289 user as protocol
290 .B ETH_P_802_2
291 with the LLC header prepended. It is thus not possible to bind to
292 .B ETH_P_802_3;
293 bind to
294 .B ETH_P_802_2
295 instead and do the protocol multiplex yourself.
296 The default for sending is the standard Ethernet DIX
297 encapsulation with the protocol filled in.
298
299 Packet sockets are not subject to the input or output firewall chains.
300
301 .SH ERRORS
302 .TP
303 .B ENETDOWN
304 Interface is not up.
305
306 .TP
307 .B ENOTCONN
308 No interface address passed.
309
310 .TP
311 .B ENODEV
312 Unknown device name or interface index specified in interface address.
313
314 .TP
315 .B EMSGSIZE
316 Packet is bigger than interface MTU.
317
318 .TP
319 .B ENOBUFS
320 Not enough memory to allocate the packet.
321
322 .TP
323 .B EFAULT
324 User passed invalid memory address.
325
326 .TP
327 .B EINVAL
328 Invalid argument.
329
330 .TP
331 .B ENXIO
332 Interface address contained illegal interface index.
333
334 .TP
335 .B EPERM
336 User has insufficient privileges to carry out this operation.
337
338 .TP
339 .B EADDRNOTAVAIL
340 Unknown multicast group address passed.
341
342 .TP
343 .B ENOENT
344 No packet received.
345
346 In addition other errors may be generated by the low-level driver.
347 .SH VERSIONS
348 .B PF_PACKET
349 is a new feature in Linux 2.2. Earlier Linux versions supported only
350 .B SOCK_PACKET.
351
352 .SH BUGS
353 glibc 2.1 does not have a define for
354 .B SOL_PACKET.
355 The suggested workaround is to use
356 .RS
357 .nf
358 #ifndef SOL_PACKET
359 #define SOL_PACKET 263
360 #endif
361 .fi
362 .RE
363 This is fixed in later glibc versions and also does not occur on libc5 systems.
364
365 The IEEE 802.2/803.3 LLC handling could be considered as a bug.
366
367 Socket filters are not documented.
368
369 The
370 .I MSG_TRUNC
371 recvmsg extension is an ugly hack and should be replaced by a control message.
372 There is currently no way to get the original destination address of
373 packets via SOCK_DGRAM.
374
375 .SH HISTORICAL NOTE
376 The include file
377 .I <netpacket/packet.h>
378 is present since glibc2.1. Older systems need
379 .sp
380 .nf
381 .B #include <asm/types.h>
382 .br
383 .B #include <linux/if_packet.h>
384 .br
385 .B #include <linux/if_ether.h> /* The L2 protocols */
386 .br
387 .fi
388 .\" .SH CREDITS
389 .\" This man page was written by Andi Kleen with help from Matthew Wilcox.
390 .\" PF_PACKET in Linux 2.2 was implemented
391 .\" by Alexey Kuznetsov, based on code by Alan Cox and others.
392 .SH "SEE ALSO"
393 .BR socket (2),
394 .BR pcap (3),
395 .BR capabilities (7),
396 .BR ip (7),
397 .BR raw (7),
398 .BR socket (7)
399
400 RFC 894 for the standard IP Ethernet encapsulation.
401
402 RFC 1700 for the IEEE 802.3 IP encapsulation.
403
404 The
405 .I <linux/if_ether.h>
406 include file for physical layer protocols.