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