Debian Codegraph @

upstream/debian-trixie/man7/raw.7

raw
1 '\" t
2 .\" SPDX-License-Identifier: Linux-man-pages-1-para
3 .\"
4 .\" This man page is Copyright (C) 1999 Andi Kleen <[email protected]>.
5 .\"
6 .\" $Id: raw.7,v 1.6 1999/06/05 10:32:08 freitag Exp $
7 .\"
8 .TH raw 7 2024-05-02 "Linux man-pages 6.9.1"
9 .SH NAME
10 raw \- Linux IPv4 raw sockets
11 .SH SYNOPSIS
12 .nf
13 .B #include <sys/socket.h>
14 .B #include <netinet/in.h>
15 .BI "raw_socket = socket(AF_INET, SOCK_RAW, int " protocol );
16 .fi
17 .SH DESCRIPTION
18 Raw sockets allow new IPv4 protocols to be implemented in user space.
19 A raw socket receives or sends the raw datagram not
20 including link level headers.
21 .P
22 The IPv4 layer generates an IP header when sending a packet unless the
23 .B IP_HDRINCL
24 socket option is enabled on the socket.
25 When it is enabled, the packet must contain an IP header.
26 For receiving, the IP header is always included in the packet.
27 .P
28 In order to create a raw socket, a process must have the
29 .B CAP_NET_RAW
30 capability in the user namespace that governs its network namespace.
31 .P
32 All packets or errors matching the
33 .I protocol
34 number specified
35 for the raw socket are passed to this socket.
36 For a list of the allowed protocols,
37 see the IANA list of assigned protocol numbers at
38 .UR http://www.iana.org/assignments/protocol\-numbers/
39 .UE
40 and
41 .BR getprotobyname (3).
42 .P
43 A protocol of
44 .B IPPROTO_RAW
45 implies enabled
46 .B IP_HDRINCL
47 and is able to send any IP protocol that is specified in the passed
48 header.
49 Receiving of all IP protocols via
50 .B IPPROTO_RAW
51 is not possible using raw sockets.
52 .RS
53 .TS
54 tab(:) allbox;
55 c s
56 l l.
57 IP Header fields modified on sending by \fBIP_HDRINCL\fP
58 IP Checksum:Always filled in
59 Source Address:Filled in when zero
60 Packet ID:Filled in when zero
61 Total Length:Always filled in
62 .TE
63 .RE
64 .P
65 If
66 .B IP_HDRINCL
67 is specified and the IP header has a nonzero destination address, then
68 the destination address of the socket is used to route the packet.
69 When
70 .B MSG_DONTROUTE
71 is specified, the destination address should refer to a local interface,
72 otherwise a routing table lookup is done anyway but gatewayed routes
73 are ignored.
74 .P
75 If
76 .B IP_HDRINCL
77 isn't set, then IP header options can be set on raw sockets with
78 .BR setsockopt (2);
79 see
80 .BR ip (7)
81 for more information.
82 .P
83 Starting with Linux 2.2, all IP header fields and options can be set using
84 IP socket options.
85 This means raw sockets are usually needed only for new
86 protocols or protocols with no user interface (like ICMP).
87 .P
88 When a packet is received, it is passed to any raw sockets which have
89 been bound to its protocol before it is passed to other protocol handlers
90 (e.g., kernel protocol modules).
91 .SS Address format
92 For sending and receiving datagrams
93 .RB ( sendto (2),
94 .BR recvfrom (2),
95 and similar),
96 raw sockets use the standard
97 .I sockaddr_in
98 address structure defined in
99 .BR ip (7).
100 The
101 .I sin_port
102 field could be used to specify the IP protocol number,
103 but it is ignored for sending in Linux 2.2 and later, and should be always
104 set to 0 (see BUGS).
105 For incoming packets,
106 .I sin_port
107 .\" commit f59fc7f30b710d45aadf715460b3e60dbe9d3418
108 is set to zero.
109 .SS Socket options
110 Raw socket options can be set with
111 .BR setsockopt (2)
112 and read with
113 .BR getsockopt (2)
114 by passing the
115 .B IPPROTO_RAW
116 .\" Or SOL_RAW on Linux
117 family flag.
118 .TP
119 .B ICMP_FILTER
120 Enable a special filter for raw sockets bound to the
121 .B IPPROTO_ICMP
122 protocol.
123 The value has a bit set for each ICMP message type which
124 should be filtered out.
125 The default is to filter no ICMP messages.
126 .P
127 In addition, all
128 .BR ip (7)
129 .B IPPROTO_IP
130 socket options valid for datagram sockets are supported.
131 .SS Error handling
132 Errors originating from the network are passed to the user only when the
133 socket is connected or the
134 .B IP_RECVERR
135 flag is enabled.
136 For connected sockets, only
137 .B EMSGSIZE
138 and
139 .B EPROTO
140 are passed for compatibility.
141 With
142 .BR IP_RECVERR ,
143 all network errors are saved in the error queue.
144 .SH ERRORS
145 .TP
146 .B EACCES
147 User tried to send to a broadcast address without having the
148 broadcast flag set on the socket.
149 .TP
150 .B EFAULT
151 An invalid memory address was supplied.
152 .TP
153 .B EINVAL
154 Invalid argument.
155 .TP
156 .B EMSGSIZE
157 Packet too big.
158 Either Path MTU Discovery is enabled (the
159 .B IP_MTU_DISCOVER
160 socket flag) or the packet size exceeds the maximum allowed IPv4
161 packet size of 64\ kB.
162 .TP
163 .B EOPNOTSUPP
164 Invalid flag has been passed to a socket call (like
165 .BR MSG_OOB ).
166 .TP
167 .B EPERM
168 The user doesn't have permission to open raw sockets.
169 Only processes with an effective user ID of 0 or the
170 .B CAP_NET_RAW
171 attribute may do that.
172 .TP
173 .B EPROTO
174 An ICMP error has arrived reporting a parameter problem.
175 .SH VERSIONS
176 .B IP_RECVERR
177 and
178 .B ICMP_FILTER
179 are new in Linux 2.2.
180 They are Linux extensions and should not be used in portable programs.
181 .P
182 Linux 2.0 enabled some bug-to-bug compatibility with BSD in the
183 raw socket code when the
184 .B SO_BSDCOMPAT
185 socket option was set; since Linux 2.2,
186 this option no longer has that effect.
187 .SH NOTES
188 By default, raw sockets do path MTU (Maximum Transmission Unit) discovery.
189 This means the kernel
190 will keep track of the MTU to a specific target IP address and return
191 .B EMSGSIZE
192 when a raw packet write exceeds it.
193 When this happens, the application should decrease the packet size.
194 Path MTU discovery can be also turned off using the
195 .B IP_MTU_DISCOVER
196 socket option or the
197 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
198 file, see
199 .BR ip (7)
200 for details.
201 When turned off, raw sockets will fragment outgoing packets
202 that exceed the interface MTU.
203 However, disabling it is not recommended
204 for performance and reliability reasons.
205 .P
206 A raw socket can be bound to a specific local address using the
207 .BR bind (2)
208 call.
209 If it isn't bound, all packets with the specified IP protocol are received.
210 In addition, a raw socket can be bound to a specific network device using
211 .BR SO_BINDTODEVICE ;
212 see
213 .BR socket (7).
214 .P
215 An
216 .B IPPROTO_RAW
217 socket is send only.
218 If you really want to receive all IP packets, use a
219 .BR packet (7)
220 socket with the
221 .B ETH_P_IP
222 protocol.
223 Note that packet sockets don't reassemble IP fragments,
224 unlike raw sockets.
225 .P
226 If you want to receive all ICMP packets for a datagram socket,
227 it is often better to use
228 .B IP_RECVERR
229 on that particular socket; see
230 .BR ip (7).
231 .P
232 Raw sockets may tap all IP protocols in Linux, even
233 protocols like ICMP or TCP which have a protocol module in the kernel.
234 In this case, the packets are passed to both the kernel module and the raw
235 socket(s).
236 This should not be relied upon in portable programs, many other BSD
237 socket implementation have limitations here.
238 .P
239 Linux never changes headers passed from the user (except for filling
240 in some zeroed fields as described for
241 .BR IP_HDRINCL ).
242 This differs from many other implementations of raw sockets.
243 .P
244 Raw sockets are generally rather unportable and should be avoided in
245 programs intended to be portable.
246 .P
247 Sending on raw sockets should take the IP protocol from
248 .IR sin_port ;
249 this ability was lost in Linux 2.2.
250 The workaround is to use
251 .BR IP_HDRINCL .
252 .SH BUGS
253 Transparent proxy extensions are not described.
254 .P
255 When the
256 .B IP_HDRINCL
257 option is set, datagrams will not be fragmented and are limited to
258 the interface MTU.
259 .P
260 Setting the IP protocol for sending in
261 .I sin_port
262 got lost in Linux 2.2.
263 The protocol that the socket was bound to or that
264 was specified in the initial
265 .BR socket (2)
266 call is always used.
267 .\" .SH AUTHORS
268 .\" This man page was written by Andi Kleen.
269 .SH SEE ALSO
270 .BR recvmsg (2),
271 .BR sendmsg (2),
272 .BR capabilities (7),
273 .BR ip (7),
274 .BR socket (7)
275 .P
276 .B RFC\ 1191
277 for path MTU discovery.
278 .B RFC\ 791
279 and the
280 .I <linux/ip.h>
281 header file for the IP protocol.