1RAW(7) Linux Programmer's Manual RAW(7)
2
3
4
6 raw - Linux IPv4 raw sockets
7
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 raw_socket = socket(AF_INET, SOCK_RAW, int protocol);
12
14 Raw sockets allow new IPv4 protocols to be implemented in user space.
15 A raw socket receives or sends the raw datagram not including link
16 level headers.
17
18 The IPv4 layer generates an IP header when sending a packet unless the
19 IP_HDRINCL socket option is enabled on the socket. When it is enabled,
20 the packet must contain an IP header. For receiving, the IP header is
21 always included in the packet.
22
23 In order to create a raw socket, a process must have the CAP_NET_RAW
24 capability in the user namespace that governs its network namespace.
25
26 All packets or errors matching the protocol number specified for the
27 raw socket are passed to this socket. For a list of the allowed proto‐
28 cols, see the IANA list of assigned protocol numbers at
29 ⟨http://www.iana.org/assignments/protocol-numbers/⟩ and getprotoby‐
30 name(3).
31
32 A protocol of IPPROTO_RAW implies enabled IP_HDRINCL and is able to
33 send any IP protocol that is specified in the passed header. Receiving
34 of all IP protocols via IPPROTO_RAW is not possible using raw sockets.
35
36 ┌───────────────────────────────────────────────────┐
37 │IP Header fields modified on sending by IP_HDRINCL │
38 ├──────────────────────┬────────────────────────────┤
39 │IP Checksum │ Always filled in │
40 ├──────────────────────┼────────────────────────────┤
41 │Source Address │ Filled in when zero │
42 ├──────────────────────┼────────────────────────────┤
43 │Packet ID │ Filled in when zero │
44 ├──────────────────────┼────────────────────────────┤
45 │Total Length │ Always filled in │
46 └──────────────────────┴────────────────────────────┘
47 If IP_HDRINCL is specified and the IP header has a nonzero destination
48 address, then the destination address of the socket is used to route
49 the packet. When MSG_DONTROUTE is specified, the destination address
50 should refer to a local interface, otherwise a routing table lookup is
51 done anyway but gatewayed routes are ignored.
52
53 If IP_HDRINCL isn't set, then IP header options can be set on raw sock‐
54 ets with setsockopt(2); see ip(7) for more information.
55
56 Starting with Linux 2.2, all IP header fields and options can be set
57 using IP socket options. This means raw sockets are usually needed
58 only for new protocols or protocols with no user interface (like ICMP).
59
60 When a packet is received, it is passed to any raw sockets which have
61 been bound to its protocol before it is passed to other protocol han‐
62 dlers (e.g., kernel protocol modules).
63
64 Address format
65 For sending and receiving datagrams (sendto(2), recvfrom(2), and simi‐
66 lar), raw sockets use the standard sockaddr_in address stru