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 theIPPROTO_ICMPprotocol. 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 (theIP_MTU_DISCOVERsocket 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 (likeMSG_OOB).EPERMโ The user doesn't have permission to open raw sockets. Only processes with an effective user ID of0or theCAP_NET_RAWattribute 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_HDRINCLoption is set, datagrams will not be fragmented and are limited to the interface MTU. important!! ๐ฅ - Setting the IP protocol for sending in
sin_portgot lost in Linux 2.2. The protocol that the socket was bound to or that was specified in the initialsocket(2)call is always used.
๐ฅ SEE ALSO ๐ฅ
recvmsg(2),sendmsg(2),capabilities(7),ip(7),socket(7)- RFC 1191 for path MTU discovery. RFC 791 and the
<linux/ip.h>header file for the IP protocol.
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
- Copyright 1999, Andi Kleen <andi@firstfloor.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms