man-pages/man7/raw.7.html
2021-03-31 01:06:50 +01:00

441 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of RAW</TITLE>
</HEAD><BODY>
<H1>RAW</H1>
Section: Linux Programmer's Manual (7)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
raw - Linux IPv4 raw sockets
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/netinet/in.h">netinet/in.h</A>&gt;</B>
<BR>
<B>raw_socket = socket(AF_INET, SOCK_RAW, int </B><I>protocol</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
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>
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>
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>
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
and
<B><A HREF="/cgi-bin/man/man2html?3+getprotobyname">getprotobyname</A></B>(3).
<P>
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.
<DL COMPACT><DT id="1"><DD>
<TABLE BORDER>
<TR VALIGN=top><TD ALIGN=center COLSPAN=2>IP Header fields modified on sending by <B>IP_HDRINCL</B><BR></TD></TR>
<TR VALIGN=top><TD>IP Checksum</TD><TD>Always filled in<BR></TD></TR>
<TR VALIGN=top><TD>Source Address</TD><TD>Filled in when zero<BR></TD></TR>
<TR VALIGN=top><TD>Packet ID</TD><TD>Filled in when zero<BR></TD></TR>
<TR VALIGN=top><TD>Total Length</TD><TD>Always filled in<BR></TD></TR>
</TABLE>
</DL>
<P>
<P>
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>
If
<B>IP_HDRINCL</B>
isn't set, then IP header options can be set on raw sockets with
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2);
see
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
for more information.
<P>
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>
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).
<A NAME="lbAE">&nbsp;</A>
<H3>Address format</H3>
For sending and receiving datagrams
(<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+recvfrom">recvfrom</A></B>(2),
and similar),
raw sockets use the standard
<I>sockaddr_in</I>
address structure defined in
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></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.
<A NAME="lbAF">&nbsp;</A>
<H3>Socket options</H3>
Raw socket options can be set with
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
and read with
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
by passing the
<B>IPPROTO_RAW</B>
family flag.
<DL COMPACT>
<DT id="2"><B>ICMP_FILTER</B>
<DD>
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.
</DL>
<P>
In addition, all
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
<B>IPPROTO_IP</B>
socket options valid for datagram sockets are supported.
<A NAME="lbAG">&nbsp;</A>
<H3>Error handling</H3>
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.
<A NAME="lbAH">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="3"><B>EACCES</B>
<DD>
User tried to send to a broadcast address without having the
broadcast flag set on the socket.
<DT id="4"><B>EFAULT</B>
<DD>
An invalid memory address was supplied.
<DT id="5"><B>EINVAL</B>
<DD>
Invalid argument.
<DT id="6"><B>EMSGSIZE</B>
<DD>
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&nbsp;kB.
<DT id="7"><B>EOPNOTSUPP</B>
<DD>
Invalid flag has been passed to a socket call (like
<B>MSG_OOB</B>).
<DT id="8"><B>EPERM</B>
<DD>
The user doesn't 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.
<DT id="9"><B>EPROTO</B>
<DD>
An ICMP error has arrived reporting a parameter problem.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>VERSIONS</H2>
<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>
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.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
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><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></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>
A raw socket can be bound to a specific local address using the
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(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
<B>SO_BINDTODEVICE</B>;
see
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7).
<P>
An
<B>IPPROTO_RAW</B>
socket is send only.
If you really want to receive all IP packets, use a
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7)
socket with the
<B>ETH_P_IP</B>
protocol.
Note that packet sockets don't reassemble IP fragments,
unlike raw sockets.
<P>
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><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
<P>
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>
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>
Raw sockets are generally rather unportable and should be avoided in
programs intended to be portable.
<P>
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>.
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
Transparent proxy extensions are not described.
<P>
When the
<B>IP_HDRINCL</B>
option is set, datagrams will not be fragmented and are limited to
the interface MTU.
<P>
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><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
call is always used.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
<P>
<B>RFC&nbsp;1191</B>
for path MTU discovery.
<B>RFC&nbsp;791</B>
and the
<I>&lt;<A HREF="file:///usr/include/linux/ip.h">linux/ip.h</A>&gt;</I>
header file for the IP protocol.
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 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
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="13"><A HREF="#lbAE">Address format</A><DD>
<DT id="14"><A HREF="#lbAF">Socket options</A><DD>
<DT id="15"><A HREF="#lbAG">Error handling</A><DD>
</DL>
<DT id="16"><A HREF="#lbAH">ERRORS</A><DD>
<DT id="17"><A HREF="#lbAI">VERSIONS</A><DD>
<DT id="18"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="19"><A HREF="#lbAK">BUGS</A><DD>
<DT id="20"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="21"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:06:09 GMT, March 31, 2021
</BODY>
</HTML>