raw(7)

๐Ÿ’– raw(7) โ€” the vibe check ๐Ÿ’–

okay bestie, sit down, hydrate, because today we're talking about raw(7): Linux IPv4 raw sockets. This lives in the miscellaneous corner of the manual (yes, section 7, we love her) and she is powerful, she is unportable, and she does NOT come to play. ๐Ÿซถ


โœจ NAME โœจ

raw โ€” Linux IPv4 raw sockets. that's it, that's the girl. ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/socket.h>
#include <netinet/in.h>
raw_socket = socket(AF_INET, SOCK_RAW, int protocol);

look at that. one syscall. so much power. be careful with her. ๐ŸŽ€


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so raw sockets let you implement brand new IPv4 protocols in user space. iconic. a raw socket either receives or sends the raw datagram, and ngl it does not include link level headers.

The IPv4 layer generates an IP header when sending a packet โ€” unless the IP_HDRINCL socket option is enabled on the socket. when it IS enabled, the packet must contain an IP header. and for receiving? the IP header is always included in the packet. no exceptions, periodt.

In order to create a raw socket, a process must have the CAP_NET_RAW capability in the user namespace that governs its network namespace. so like. permissions first, girlies. ๐Ÿ‘‘

All packets or errors matching the protocol number specified for the raw socket are passed to this socket. for the list of allowed protocols, go see the IANA list of assigned protocol numbers at http://www.iana.org/assignments/protocol-numbers/ and also getprotobyname(3).

A protocol of IPPROTO_RAW implies enabled IP_HDRINCL and is able to send any IP protocol that is specified in the passed header. BUT โ€” and this is important, bestie โ€” receiving of all IP protocols via IPPROTO_RAW is not possible using raw sockets. ๐Ÿ˜ญ

๐Ÿ’œ IP Header fields modified on sending by IP_HDRINCL ๐Ÿ’œ

(the manpage has this as a table, we're doing bullets, same facts, same energy โœจ)

  • IP Checksum โ€” Always filled in
  • Source Address โ€” Filled in when zero
  • Packet ID โ€” Filled in when zero
  • Total Length โ€” Always filled in

If IP_HDRINCL is specified and the IP header has a nonzero destination address, then the destination address of the socket is used to route the packet. When MSG_DONTROUTE is specified, the destination address should refer to a local interface, otherwise a routing table lookup is done anyway but gatewayed routes are ignored. okay? okay. ๐Ÿซก

If IP_HDRINCL isn't set, then IP header options can be set on raw sockets with setsockopt(2); see ip(7) for more info.

Starting with Linux 2.2, all IP header fields and options can be set using IP socket options. which means raw sockets are usually needed only for new protocols or protocols with no user interface (like ICMP). so like... lowkey she's niche. and that's fine. ๐Ÿ’…

When a packet is received, it is passed to any raw sockets which have been bound to its protocol before it is passed to other protocol handlers (e.g., kernel protocol modules). front of the line. main character energy fr.

๐ŸŒˆ Address format ๐ŸŒˆ

For sending and receiving datagrams (sendto(2), recvfrom(2), and similar), raw sockets use the standard sockaddr_in address structure defined in ip(7). The sin_port field could be used to specify the IP protocol number, but it is ignored for sending in Linux 2.2 and later, and should be always set to 0 (see BUGS). For incoming packets, sin_port is set to zero.

๐Ÿ“ Socket options ๐Ÿ“

Raw socket options can be set with setsockopt(2) and read with getsockopt(2) by passing the IPPROTO_RAW family flag.

  • ICMP_FILTER โ€” Enable a special filter for raw sockets bound to the IPPROTO_ICMP protocol. The value has a bit set for each ICMP message type which should be filtered out. The default is to filter no ICMP messages. so by default: everything comes through, bestie, all of it. ๐Ÿ™ˆ

In addition, all ip(7) IPPROTO_IP socket options valid for datagram sockets are supported. yes, all of them.

โญ Error handling โญ

Errors originating from the network are passed to the user only when the socket is connected or the IP_RECVERR flag is enabled. For connected sockets, only EMSGSIZE and EPROTO are passed, for compatibility. With IP_RECVERR, all network errors are saved in the error queue.


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • EACCES โ€” User tried to send to a broadcast address without having the broadcast flag set on the socket.
  • EFAULT โ€” An invalid memory address was supplied.
  • EINVAL โ€” Invalid argument.
  • EMSGSIZE โ€” Packet too big. Either Path MTU Discovery is enabled (the IP_MTU_DISCOVER socket flag) or the packet size exceeds the maximum allowed IPv4 packet size of 64 kB.
  • EOPNOTSUPP โ€” Invalid flag has been passed to a socket call (like MSG_OOB).
  • EPERM โ€” The user doesn't have permission to open raw sockets. Only processes with an effective user ID of 0 or the CAP_NET_RAW attribute may do that.
  • EPROTO โ€” An ICMP error has arrived reporting a parameter problem.

๐Ÿงš VERSIONS ๐Ÿงš

IP_RECVERR and ICMP_FILTER are new in Linux 2.2. they are Linux extensions and should not be used in portable programs. noted, bestie, writing that down. ๐Ÿ“

Linux 2.0 enabled some bug-to-bug compatibility with BSD in the raw socket code when the SO_BSDCOMPAT socket option was set; since Linux 2.2, this option no longer has that effect. rip to that era. ๐Ÿ’€


๐Ÿ‘‘ NOTES ๐Ÿ‘‘

By default, raw sockets do path MTU (Maximum Transmission Unit) discovery. This means the kernel will keep track of the MTU to a specific target IP address and return EMSGSIZE when a raw packet write exceeds it. When this happens, the application should decrease the packet size. Path MTU discovery can also be turned off using the IP_MTU_DISCOVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file โ€” see ip(7) for details. When turned off, raw sockets will fragment outgoing packets that exceed the interface MTU. However, disabling it is not recommended for performance and reliability reasons.

ok so real talk: don't turn off path MTU discovery just because it's annoying. she's annoying because she's protecting you. ๐Ÿซถ

A raw socket can be bound to a specific local address using the bind(2) call. If it isn't bound, all packets with the specified IP protocol are received. In addition, a raw socket can be bound to a specific network device using SO_BINDTODEVICE; see socket(7).

An IPPROTO_RAW socket is send only. so if you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. and note that packet sockets don't reassemble IP fragments, unlike raw sockets. different girl, different rules. ๐Ÿ’…

If you want to receive all ICMP packets for a datagram socket, it is often better to use IP_RECVERR on that particular socket; see ip(7).

Raw sockets may tap all IP protocols in Linux, even protocols like ICMP or TCP which have a protocol module in the kernel. In this case, the packets are passed to both the kernel module and the raw socket(s). This should not be relied upon in portable programs โ€” many other BSD socket implementations have limitations here.

Linux never changes headers passed from the user (except for filling in some zeroed fields as described for IP_HDRINCL). This differs from many other implementations of raw sockets. Linux said "your header, your problem" and honestly? respect. โœจ

Raw sockets are generally rather unportable and should be avoided in programs intended to be portable. fr fr. if you want your code to travel, leave her at home. ๐Ÿ˜ญ

Sending on raw sockets should take the IP protocol from sin_port; this ability was lost in Linux 2.2. The workaround is to use IP_HDRINCL.


๐Ÿ™ˆ BUGS ๐Ÿ™ˆ

  • Transparent proxy extensions are not described. (she's mysterious. she's not telling. ๐Ÿ™ˆ)
  • When the IP_HDRINCL option is set, datagrams will not be fragmented and are limited to the interface MTU. important!! ๐Ÿ”ฅ
  • Setting the IP protocol for sending in sin_port got lost in Linux 2.2. The protocol that the socket was bound to or that was specified in the initial socket(2) call is always used.

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ


and that's the whole page, bestie!! go forth and socket responsibly ๐Ÿ’–โœจ

this lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08, and it's the raw(7) page, okay byeee ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-1-para
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms