2 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
4 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
5 .\" Permission is granted to distribute possibly modified copies
6 .\" of this page provided the header is included verbatim,
7 .\" and in case of nontrivial modification author and date
8 .\" of the modification is added to the header.
11 .\" $Id: raw.7,v 1.6 1999/06/05 10:32:08 freitag Exp $
13 .TH RAW 7 2016-10-08 "Linux" "Linux Programmer's Manual"
15 raw \- Linux IPv4 raw sockets
17 .B #include <sys/socket.h>
19 .B #include <netinet/in.h>
21 .BI "raw_socket = socket(AF_INET, SOCK_RAW, int " protocol );
23 Raw sockets allow new IPv4 protocols to be implemented in user space.
24 A raw socket receives or sends the raw datagram not
25 including link level headers.
27 The IPv4 layer generates an IP header when sending a packet unless the
29 socket option is enabled on the socket.
30 When it is enabled, the packet must contain an IP header.
31 For receiving, the IP header is always included in the packet.
33 In order to create a raw socket, a process must have the
35 capability in the user namespace that governs its network namespace.
37 All packets or errors matching the
40 for the raw socket are passed to this socket.
41 For a list of the allowed protocols,
42 see the IANA list of assigned protocol numbers at
43 .UR http://www.iana.org/assignments/protocol\-numbers/
46 .BR getprotobyname (3).
52 and is able to send any IP protocol that is specified in the passed
54 Receiving of all IP protocols via
56 is not possible using raw sockets.
62 IP Header fields modified on sending by \fBIP_HDRINCL\fP
63 IP Checksum:Always filled in
64 Source Address:Filled in when zero
65 Packet ID:Filled in when zero
66 Total Length:Always filled in
73 is specified and the IP header has a nonzero destination address, then
74 the destination address of the socket is used to route the packet.
77 is specified, the destination address should refer to a local interface,
78 otherwise a routing table lookup is done anyway but gatewayed routes
83 isn't set, then IP header options can be set on raw sockets with
89 Starting with Linux 2.2, all IP header fields and options can be set using
91 This means raw sockets are usually needed only for new
92 protocols or protocols with no user interface (like ICMP).
94 When a packet is received, it is passed to any raw sockets which have
95 been bound to its protocol before it is passed to other protocol handlers
96 (e.g., kernel protocol modules).
98 For sending and receiving datagrams
102 raw sockets use the standard
104 address structure defined in
108 field could be used to specify the IP protocol number,
109 but it is ignored for sending in Linux 2.2 and later, and should be always
111 For incoming packets,
113 .\" commit f59fc7f30b710d45aadf715460b3e60dbe9d3418
116 Raw socket options can be set with
122 .\" Or SOL_RAW on Linux
126 Enable a special filter for raw sockets bound to the
129 The value has a bit set for each ICMP message type which
130 should be filtered out.
131 The default is to filter no ICMP messages.
136 socket options valid for datagram sockets are supported.
138 Errors originating from the network are passed to the user only when the
139 socket is connected or the
142 For connected sockets, only
146 are passed for compatibility.
149 all network errors are saved in the error queue.
153 User tried to send to a broadcast address without having the
154 broadcast flag set on the socket.
157 An invalid memory address was supplied.
164 Either Path MTU Discovery is enabled (the
166 socket flag) or the packet size exceeds the maximum allowed IPv4
170 Invalid flag has been passed to a socket call (like
174 The user doesn't have permission to open raw sockets.
175 Only processes with an effective user ID of 0 or the
177 attribute may do that.
180 An ICMP error has arrived reporting a parameter problem.
185 are new in Linux 2.2.
186 They are Linux extensions and should not be used in portable programs.
188 Linux 2.0 enabled some bug-to-bug compatibility with BSD in the
189 raw socket code when the
191 socket option was set; since Linux 2.2,
192 this option no longer has that effect.
194 By default, raw sockets do path MTU (Maximum Transmission Unit) discovery.
195 This means the kernel
196 will keep track of the MTU to a specific target IP address and return
198 when a raw packet write exceeds it.
199 When this happens, the application should decrease the packet size.
200 Path MTU discovery can be also turned off using the
203 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
207 When turned off, raw sockets will fragment outgoing packets
208 that exceed the interface MTU.
209 However, disabling it is not recommended
210 for performance and reliability reasons.
212 A raw socket can be bound to a specific local address using the
215 If it isn't bound, all packets with the specified IP protocol are received.
216 In addition, a raw socket can be bound to a specific network device using
217 .BR SO_BINDTODEVICE ;
224 If you really want to receive all IP packets, use a
229 Note that packet sockets don't reassemble IP fragments,
232 If you want to receive all ICMP packets for a datagram socket,
233 it is often better to use
235 on that particular socket; see
238 Raw sockets may tap all IP protocols in Linux, even
239 protocols like ICMP or TCP which have a protocol module in the kernel.
240 In this case, the packets are passed to both the kernel module and the raw
242 This should not be relied upon in portable programs, many other BSD
243 socket implementation have limitations here.
245 Linux never changes headers passed from the user (except for filling
246 in some zeroed fields as described for
248 This differs from many other implementations of raw sockets.
250 Raw sockets are generally rather unportable and should be avoided in
251 programs intended to be portable.
253 Sending on raw sockets should take the IP protocol from
255 this ability was lost in Linux 2.2.
256 The workaround is to use
259 Transparent proxy extensions are not described.
263 option is set, datagrams will not be fragmented and are limited to
266 Setting the IP protocol for sending in
268 got lost in Linux 2.2.
269 The protocol that the socket was bound to or that
270 was specified in the initial
274 .\" This man page was written by Andi Kleen.
278 .BR capabilities (7),
283 for path MTU discovery.
287 header file for the IP protocol.