{"id":4080,"date":"2022-12-20T17:39:20","date_gmt":"2022-12-20T20:39:20","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/raw-man7\/"},"modified":"2022-12-20T17:39:20","modified_gmt":"2022-12-20T20:39:20","slug":"raw-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/raw-man7\/","title":{"rendered":"RAW (man7)"},"content":{"rendered":"<h1 align=\"center\">RAW<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">raw \u2212 Linux IPv4 raw sockets<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/socket.h> <br \/> #include <netinet\/in.h> <br \/> raw_socket = socket(AF_INET, SOCK_RAW, int<\/b> <i>protocol<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Raw sockets allow new IPv4 protocols to be implemented in user space. A raw socket receives or sends the raw datagram not including link level headers.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The IPv4 layer generates an IP header when sending a packet unless the <b>IP_HDRINCL<\/b> socket option is enabled on the socket. When it is enabled, the packet must contain an IP header. For receiving, the IP header is always included in the packet.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In order to create a raw socket, a process must have the <b>CAP_NET_RAW<\/b> capability in the user namespace that governs its network namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">All packets or errors matching the <i>protocol<\/i> number specified for the raw socket are passed to this socket. For a list of the allowed protocols, see the IANA list of assigned protocol numbers at <a href=\"http:\/\/www.iana.org\/assignments\/protocol-numbers\/\">http:\/\/www.iana.org\/assignments\/protocol-numbers\/<\/a> and <b>getprotobyname<\/b>(3).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A protocol of <b>IPPROTO_RAW<\/b> implies enabled <b>IP_HDRINCL<\/b> and is able to send any IP protocol that is specified in the passed header. Receiving of all IP protocols via <b>IPPROTO_RAW<\/b> is not possible using raw sockets.<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-931991.png\" alt=\"Image grohtml-931991.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <b>IP_HDRINCL<\/b> 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 <b>MSG_DONTROUTE<\/b> is specified, the destination address should refer to a local interface, otherwise a routing table lookup is done anyway but gatewayed routes are ignored.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <b>IP_HDRINCL<\/b> isn\u2019t set, then IP header options can be set on raw sockets with <b>setsockopt<\/b>(2); see <b>ip<\/b>(7) for more information.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Starting with Linux 2.2, all IP header fields and options can be set using IP socket options. This means raw sockets are usually needed only for new protocols or protocols with no user interface (like ICMP).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Address format<\/b> <br \/> For sending and receiving datagrams (<b>sendto<\/b>(2), <b>recvfrom<\/b>(2), and similar), raw sockets use the standard <i>sockaddr_in<\/i> address structure defined in <b>ip<\/b>(7). The <i>sin_port<\/i> 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, <i>sin_port<\/i> is set to zero.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Socket options<\/b> <br \/> Raw socket options can be set with <b>setsockopt<\/b>(2) and read with <b>getsockopt<\/b>(2) by passing the <b>IPPROTO_RAW<\/b> family flag. <b><br \/> ICMP_FILTER<\/b><\/p>\n<p style=\"margin-left:22%;\">Enable a special filter for raw sockets bound to the <b>IPPROTO_ICMP<\/b> 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition, all <b>ip<\/b>(7) <b>IPPROTO_IP<\/b> socket options valid for datagram sockets are supported.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Error handling<\/b> <br \/> Errors originating from the network are passed to the user only when the socket is connected or the <b>IP_RECVERR<\/b> flag is enabled. For connected sockets, only <b>EMSGSIZE<\/b> and <b>EPROTO<\/b> are passed for compatibility. With <b>IP_RECVERR<\/b>, all network errors are saved in the error queue.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">User tried to send to a broadcast address without having the broadcast flag set on the socket.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EFAULT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>An invalid memory address was supplied.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Invalid argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EMSGSIZE<\/b><\/p>\n<p style=\"margin-left:22%;\">Packet too big. Either Path MTU Discovery is enabled (the <b>IP_MTU_DISCOVER<\/b> socket flag) or the packet size exceeds the maximum allowed IPv4 packet size of 64\u00a0kB.<\/p>\n<p style=\"margin-left:11%;\"><b>EOPNOTSUPP<\/b><\/p>\n<p style=\"margin-left:22%;\">Invalid flag has been passed to a socket call (like <b>MSG_OOB<\/b>).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EPERM<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The user doesn\u2019t have permission to open raw sockets. Only processes with an effective user ID of 0 or the <b>CAP_NET_RAW<\/b> attribute may do that.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EPROTO<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>An ICMP error has arrived reporting a parameter problem.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>IP_RECVERR<\/b> and <b>ICMP_FILTER<\/b> are new in Linux 2.2. They are Linux extensions and should not be used in portable programs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Linux 2.0 enabled some bug-to-bug compatibility with BSD in the raw socket code when the <b>SO_BSDCOMPAT<\/b> socket option was set; since Linux 2.2, this option no longer has that effect.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <b>EMSGSIZE<\/b> when a raw packet write exceeds it. When this happens, the application should decrease the packet size. Path MTU discovery can be also turned off using the <b>IP_MTU_DISCOVER<\/b> socket option or the <i>\/proc\/sys\/net\/ipv4\/ip_no_pmtu_disc<\/i> file, see <b>ip<\/b>(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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A raw socket can be bound to a specific local address using the <b>bind<\/b>(2) call. If it isn\u2019t bound, all packets with the specified IP protocol are received. In addition, a raw socket can be bound to a specific network device using <b>SO_BINDTODEVICE<\/b>; see <b>socket<\/b>(7).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An <b>IPPROTO_RAW<\/b> socket is send only. If you really want to receive all IP packets, use a <b>packet<\/b>(7) socket with the <b>ETH_P_IP<\/b> protocol. Note that packet sockets don\u2019t reassemble IP fragments, unlike raw sockets.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you want to receive all ICMP packets for a datagram socket, it is often better to use <b>IP_RECVERR<\/b> on that particular socket; see <b>ip<\/b>(7).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 implementation have limitations here.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Linux never changes headers passed from the user (except for filling in some zeroed fields as described for <b>IP_HDRINCL<\/b>). This differs from many other implementations of raw sockets.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Raw sockets are generally rather unportable and should be avoided in programs intended to be portable.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Sending on raw sockets should take the IP protocol from <i>sin_port<\/i>; this ability was lost in Linux 2.2. The workaround is to use <b>IP_HDRINCL<\/b>.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Transparent proxy extensions are not described.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the <b>IP_HDRINCL<\/b> option is set, datagrams will not be fragmented and are limited to the interface MTU.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Setting the IP protocol for sending in <i>sin_port<\/i> got lost in Linux 2.2. The protocol that the socket was bound to or that was specified in the initial <b>socket<\/b>(2) call is always used.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>recvmsg<\/b>(2), <b>sendmsg<\/b>(2), <b>capabilities<\/b>(7), <b>ip<\/b>(7), <b>socket<\/b>(7)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>RFC\u00a01191<\/b> for path MTU discovery. <b>RFC\u00a0791<\/b> and the <i><linux\/ip.h><\/i> header file for the IP protocol.<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  raw \u2212 Linux IPv4 raw sockets <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,1160],"class_list":["post-4080","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-raw"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4080"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4080\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}