1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
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.
10 .\" $Id: raw.7,v 1.6 1999/06/05 10:32:08 freitag Exp $
12 .TH RAW 7 2021-03-22 "Linux" "Linux Programmer's Manual"
14 raw \- Linux IPv4 raw sockets
17 .B #include <sys/socket.h>
18 .B #include <netinet/in.h>
19 .BI "raw_socket = socket(AF_INET, SOCK_RAW, int " protocol );
22 Raw sockets allow new IPv4 protocols to be implemented in user space.
23 A raw socket receives or sends the raw datagram not
24 including link level headers.
26 The IPv4 layer generates an IP header when sending a packet unless the
28 socket option is enabled on the socket.
29 When it is enabled, the packet must contain an IP header.
30 For receiving, the IP header is always included in the packet.
32 In order to create a raw socket, a process must have the
34 capability in the user namespace that governs its network namespace.
36 All packets or errors matching the
39 for the raw socket are passed to this socket.
40 For a list of the allowed protocols,
41 see the IANA list of assigned protocol numbers at
42 .UR http://www.iana.org/assignments/protocol\-numbers/
45 .BR getprotobyname (3).
51 and is able to send any IP protocol that is specified in the passed
53 Receiving of all IP protocols via
55 is not possible using raw sockets.
61 IP Header fields modified on sending by \fBIP_HDRINCL\fP
62 IP Checksum:Always filled in
63 Source Address:Filled in when zero
64 Packet ID:Filled in when zero
65 Total Length:Always filled in
72 is specified and the IP header has a nonzero destination address, then
73 the destination address of the socket is used to route the packet.
76 is specified, the destination address should refer to a local interface,
77 otherwise a routing table lookup is done anyway but gatewayed routes
82 isn't set, then IP header options can be set on raw sockets with
88 Starting with Linux 2.2, all IP header fields and options can be set using
90 This means raw sockets are usually needed only for new
91 protocols or protocols with no user interface (like ICMP).
93 When a packet is received, it is passed to any raw sockets which have
94 been bound to its protocol before it is passed to other protocol handlers
95 (e.g., kernel protocol modules).
97 For sending and receiving datagrams
101 raw sockets use the standard
103 address structure defined in
107 field could be used to specify the IP protocol number,
108 but it is ignored for sending in Linux 2.2 and later, and should be always
110 For incoming packets,
112 .\" commit f59fc7f30b710d45aadf715460b3e60dbe9d3418
115 Raw socket options can be set with
121 .\" Or SOL_RAW on Linux
125 Enable a special filter for raw sockets bound to the
128 The value has a bit set for each ICMP message type which
129 should be filtered out.
130 The default is to filter no ICMP messages.
135 socket options valid for datagram sockets are supported.
137 Errors originating from the network are passed to the user only when the
138 socket is connected or the
141 For connected sockets, only
145 are passed for compatibility.
148 all network errors are saved in the error queue.
152 User tried to send to a broadcast address without having the
153 broadcast flag set on the socket.
156 An invalid memory address was supplied.
163 Either Path MTU Discovery is enabled (the
165 socket flag) or the packet size exceeds the maximum allowed IPv4
166 packet size of 64\ kB.
169 Invalid flag has been passed to a socket call (like
173 The user doesn't have permission to open raw sockets.
174 Only processes with an effective user ID of 0 or the
176 attribute may do that.
179 An ICMP error has arrived reporting a parameter problem.
184 are new in Linux 2.2.
185 They are Linux extensions and should not be used in portable programs.
187 Linux 2.0 enabled some bug-to-bug compatibility with BSD in the
188 raw socket code when the
190 socket option was set; since Linux 2.2,
191 this option no longer has that effect.
193 By default, raw sockets do path MTU (Maximum Transmission Unit) discovery.
194 This means the kernel
195 will keep track of the MTU to a specific target IP address and return
197 when a raw packet write exceeds it.
198 When this happens, the application should decrease the packet size.
199 Path MTU discovery can be also turned off using the
202 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
206 When turned off, raw sockets will fragment outgoing packets
207 that exceed the interface MTU.
208 However, disabling it is not recommended
209 for performance and reliability reasons.
211 A raw socket can be bound to a specific local address using the
214 If it isn't bound, all packets with the specified IP protocol are received.
215 In addition, a raw socket can be bound to a specific network device using
216 .BR SO_BINDTODEVICE ;
223 If you really want to receive all IP packets, use a
228 Note that packet sockets don't reassemble IP fragments,
231 If you want to receive all ICMP packets for a datagram socket,
232 it is often better to use
234 on that particular socket; see
237 Raw sockets may tap all IP protocols in Linux, even
238 protocols like ICMP or TCP which have a protocol module in the kernel.
239 In this case, the packets are passed to both the kernel module and the raw
241 This should not be relied upon in portable programs, many other BSD
242 socket implementation have limitations here.
244 Linux never changes headers passed from the user (except for filling
245 in some zeroed fields as described for
247 This differs from many other implementations of raw sockets.
249 Raw sockets are generally rather unportable and should be avoided in
250 programs intended to be portable.
252 Sending on raw sockets should take the IP protocol from
254 this ability was lost in Linux 2.2.
255 The workaround is to use
258 Transparent proxy extensions are not described.
262 option is set, datagrams will not be fragmented and are limited to
265 Setting the IP protocol for sending in
267 got lost in Linux 2.2.
268 The protocol that the socket was bound to or that
269 was specified in the initial
273 .\" This man page was written by Andi Kleen.
277 .BR capabilities (7),
282 for path MTU discovery.
286 header file for the IP protocol.