Blame man7/raw.7

Packit 7cfc04
'\" t
Packit 7cfc04
.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM_ONE_PARA)
Packit 7cfc04
.\" Permission is granted to distribute possibly modified copies
Packit 7cfc04
.\" of this page provided the header is included verbatim,
Packit 7cfc04
.\" and in case of nontrivial modification author and date
Packit 7cfc04
.\" of the modification is added to the header.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" $Id: raw.7,v 1.6 1999/06/05 10:32:08 freitag Exp $
Packit 7cfc04
.\"
Packit 7cfc04
.TH RAW  7 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
raw \- Linux IPv4 raw sockets
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.br
Packit 7cfc04
.B #include <netinet/in.h>
Packit 7cfc04
.br
Packit 7cfc04
.BI "raw_socket = socket(AF_INET, SOCK_RAW, int " protocol );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
Raw sockets allow new IPv4 protocols to be implemented in user space.
Packit 7cfc04
A raw socket receives or sends the raw datagram not
Packit 7cfc04
including link level headers.
Packit 7cfc04
.PP
Packit 7cfc04
The IPv4 layer generates an IP header when sending a packet unless the
Packit 7cfc04
.B IP_HDRINCL
Packit 7cfc04
socket option is enabled on the socket.
Packit 7cfc04
When it is enabled, the packet must contain an IP header.
Packit 7cfc04
For receiving, the IP header is always included in the packet.
Packit 7cfc04
.PP
Packit 7cfc04
In order to create a raw socket, a process must have the
Packit 7cfc04
.B CAP_NET_RAW
Packit 7cfc04
capability in the user namespace that governs its network namespace.
Packit 7cfc04
.PP
Packit 7cfc04
All packets or errors matching the
Packit 7cfc04
.I protocol
Packit 7cfc04
number specified
Packit 7cfc04
for the raw socket are passed to this socket.
Packit 7cfc04
For a list of the allowed protocols,
Packit 7cfc04
see the IANA list of assigned protocol numbers at
Packit 7cfc04
.UR http://www.iana.org/assignments/protocol\-numbers/
Packit 7cfc04
.UE
Packit 7cfc04
and
Packit 7cfc04
.BR getprotobyname (3).
Packit 7cfc04
.PP
Packit 7cfc04
A protocol of
Packit 7cfc04
.B IPPROTO_RAW
Packit 7cfc04
implies enabled
Packit 7cfc04
.B IP_HDRINCL
Packit 7cfc04
and is able to send any IP protocol that is specified in the passed
Packit 7cfc04
header.
Packit 7cfc04
Receiving of all IP protocols via
Packit 7cfc04
.B IPPROTO_RAW
Packit 7cfc04
is not possible using raw sockets.
Packit 7cfc04
.RS
Packit 7cfc04
.TS
Packit 7cfc04
tab(:) allbox;
Packit 7cfc04
c s
Packit 7cfc04
l l.
Packit 7cfc04
IP Header fields modified on sending by \fBIP_HDRINCL\fP
Packit 7cfc04
IP Checksum:Always filled in
Packit 7cfc04
Source Address:Filled in when zero
Packit 7cfc04
Packet ID:Filled in when zero
Packit 7cfc04
Total Length:Always filled in
Packit 7cfc04
.TE
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.B IP_HDRINCL
Packit 7cfc04
is specified and the IP header has a nonzero destination address, then
Packit 7cfc04
the destination address of the socket is used to route the packet.
Packit 7cfc04
When
Packit 7cfc04
.B MSG_DONTROUTE
Packit 7cfc04
is specified, the destination address should refer to a local interface,
Packit 7cfc04
otherwise a routing table lookup is done anyway but gatewayed routes
Packit 7cfc04
are ignored.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.B IP_HDRINCL
Packit 7cfc04
isn't set, then IP header options can be set on raw sockets with
Packit 7cfc04
.BR setsockopt (2);
Packit 7cfc04
see
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
for more information.
Packit 7cfc04
.PP
Packit 7cfc04
Starting with Linux 2.2, all IP header fields and options can be set using
Packit 7cfc04
IP socket options.
Packit 7cfc04
This means raw sockets are usually needed only for new
Packit 7cfc04
protocols or protocols with no user interface (like ICMP).
Packit 7cfc04
.PP
Packit 7cfc04
When a packet is received, it is passed to any raw sockets which have
Packit 7cfc04
been bound to its protocol before it is passed to other protocol handlers
Packit 7cfc04
(e.g., kernel protocol modules).
Packit 7cfc04
.SS Address format
Packit 7cfc04
For sending and receiving datagrams
Packit 7cfc04
.RB ( sendto (2),
Packit 7cfc04
.BR recvfrom (2),
Packit 7cfc04
and similar),
Packit 7cfc04
raw sockets use the standard
Packit 7cfc04
.I sockaddr_in
Packit 7cfc04
address structure defined in
Packit 7cfc04
.BR ip (7).
Packit 7cfc04
The
Packit 7cfc04
.I sin_port
Packit 7cfc04
field could be used to specify the IP protocol number,
Packit 7cfc04
but it is ignored for sending in Linux 2.2 and later, and should be always
Packit 7cfc04
set to 0 (see BUGS).
Packit 7cfc04
For incoming packets,
Packit 7cfc04
.I sin_port
Packit 7cfc04
.\" commit f59fc7f30b710d45aadf715460b3e60dbe9d3418
Packit 7cfc04
is set to zero.
Packit 7cfc04
.SS Socket options
Packit 7cfc04
Raw socket options can be set with
Packit 7cfc04
.BR setsockopt (2)
Packit 7cfc04
and read with
Packit 7cfc04
.BR getsockopt (2)
Packit 7cfc04
by passing the
Packit 7cfc04
.B IPPROTO_RAW
Packit 7cfc04
.\" Or SOL_RAW on Linux
Packit 7cfc04
family flag.
Packit 7cfc04
.TP
Packit 7cfc04
.B ICMP_FILTER
Packit 7cfc04
Enable a special filter for raw sockets bound to the
Packit 7cfc04
.B IPPROTO_ICMP
Packit 7cfc04
protocol.
Packit 7cfc04
The value has a bit set for each ICMP message type which
Packit 7cfc04
should be filtered out.
Packit 7cfc04
The default is to filter no ICMP messages.
Packit 7cfc04
.PP
Packit 7cfc04
In addition, all
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
.B IPPROTO_IP
Packit 7cfc04
socket options valid for datagram sockets are supported.
Packit 7cfc04
.SS Error handling
Packit 7cfc04
Errors originating from the network are passed to the user only when the
Packit 7cfc04
socket is connected or the
Packit 7cfc04
.B IP_RECVERR
Packit 7cfc04
flag is enabled.
Packit 7cfc04
For connected sockets, only
Packit 7cfc04
.B EMSGSIZE
Packit 7cfc04
and
Packit 7cfc04
.B EPROTO
Packit 7cfc04
are passed for compatibility.
Packit 7cfc04
With
Packit 7cfc04
.BR IP_RECVERR ,
Packit 7cfc04
all network errors are saved in the error queue.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
User tried to send to a broadcast address without having the
Packit 7cfc04
broadcast flag set on the socket.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
An invalid memory address was supplied.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Invalid argument.
Packit 7cfc04
.TP
Packit 7cfc04
.B EMSGSIZE
Packit 7cfc04
Packet too big.
Packit 7cfc04
Either Path MTU Discovery is enabled (the
Packit 7cfc04
.B IP_MTU_DISCOVER
Packit 7cfc04
socket flag) or the packet size exceeds the maximum allowed IPv4
Packit 7cfc04
packet size of 64\ kB.
Packit 7cfc04
.TP
Packit 7cfc04
.B EOPNOTSUPP
Packit 7cfc04
Invalid flag has been passed to a socket call (like
Packit 7cfc04
.BR MSG_OOB ).
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The user doesn't have permission to open raw sockets.
Packit 7cfc04
Only processes with an effective user ID of 0 or the
Packit 7cfc04
.B CAP_NET_RAW
Packit 7cfc04
attribute may do that.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPROTO
Packit 7cfc04
An ICMP error has arrived reporting a parameter problem.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.B IP_RECVERR
Packit 7cfc04
and
Packit 7cfc04
.B ICMP_FILTER
Packit 7cfc04
are new in Linux 2.2.
Packit 7cfc04
They are Linux extensions and should not be used in portable programs.
Packit 7cfc04
.PP
Packit 7cfc04
Linux 2.0 enabled some bug-to-bug compatibility with BSD in the
Packit 7cfc04
raw socket code when the
Packit 7cfc04
.B SO_BSDCOMPAT
Packit 7cfc04
socket option was set; since Linux 2.2,
Packit 7cfc04
this option no longer has that effect.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
By default, raw sockets do path MTU (Maximum Transmission Unit) discovery.
Packit 7cfc04
This means the kernel
Packit 7cfc04
will keep track of the MTU to a specific target IP address and return
Packit 7cfc04
.B EMSGSIZE
Packit 7cfc04
when a raw packet write exceeds it.
Packit 7cfc04
When this happens, the application should decrease the packet size.
Packit 7cfc04
Path MTU discovery can be also turned off using the
Packit 7cfc04
.B IP_MTU_DISCOVER
Packit 7cfc04
socket option or the
Packit 7cfc04
.I /proc/sys/net/ipv4/ip_no_pmtu_disc
Packit 7cfc04
file, see
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
for details.
Packit 7cfc04
When turned off, raw sockets will fragment outgoing packets
Packit 7cfc04
that exceed the interface MTU.
Packit 7cfc04
However, disabling it is not recommended
Packit 7cfc04
for performance and reliability reasons.
Packit 7cfc04
.PP
Packit 7cfc04
A raw socket can be bound to a specific local address using the
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
call.
Packit 7cfc04
If it isn't bound, all packets with the specified IP protocol are received.
Packit 7cfc04
In addition, a raw socket can be bound to a specific network device using
Packit 7cfc04
.BR SO_BINDTODEVICE ;
Packit 7cfc04
see
Packit 7cfc04
.BR socket (7).
Packit 7cfc04
.PP
Packit 7cfc04
An
Packit 7cfc04
.B IPPROTO_RAW
Packit 7cfc04
socket is send only.
Packit 7cfc04
If you really want to receive all IP packets, use a
Packit 7cfc04
.BR packet (7)
Packit 7cfc04
socket with the
Packit 7cfc04
.B ETH_P_IP
Packit 7cfc04
protocol.
Packit 7cfc04
Note that packet sockets don't reassemble IP fragments,
Packit 7cfc04
unlike raw sockets.
Packit 7cfc04
.PP
Packit 7cfc04
If you want to receive all ICMP packets for a datagram socket,
Packit 7cfc04
it is often better to use
Packit 7cfc04
.B IP_RECVERR
Packit 7cfc04
on that particular socket; see
Packit 7cfc04
.BR ip (7).
Packit 7cfc04
.PP
Packit 7cfc04
Raw sockets may tap all IP protocols in Linux, even
Packit 7cfc04
protocols like ICMP or TCP which have a protocol module in the kernel.
Packit 7cfc04
In this case, the packets are passed to both the kernel module and the raw
Packit 7cfc04
socket(s).
Packit 7cfc04
This should not be relied upon in portable programs, many other BSD
Packit 7cfc04
socket implementation have limitations here.
Packit 7cfc04
.PP
Packit 7cfc04
Linux never changes headers passed from the user (except for filling
Packit 7cfc04
in some zeroed fields as described for
Packit 7cfc04
.BR IP_HDRINCL ).
Packit 7cfc04
This differs from many other implementations of raw sockets.
Packit 7cfc04
.PP
Packit 7cfc04
Raw sockets are generally rather unportable and should be avoided in
Packit 7cfc04
programs intended to be portable.
Packit 7cfc04
.PP
Packit 7cfc04
Sending on raw sockets should take the IP protocol from
Packit 7cfc04
.IR sin_port ;
Packit 7cfc04
this ability was lost in Linux 2.2.
Packit 7cfc04
The workaround is to use
Packit 7cfc04
.BR IP_HDRINCL .
Packit 7cfc04
.SH BUGS
Packit 7cfc04
Transparent proxy extensions are not described.
Packit 7cfc04
.PP
Packit 7cfc04
When the
Packit 7cfc04
.B IP_HDRINCL
Packit 7cfc04
option is set, datagrams will not be fragmented and are limited to
Packit 7cfc04
the interface MTU.
Packit 7cfc04
.PP
Packit 7cfc04
Setting the IP protocol for sending in
Packit 7cfc04
.I sin_port
Packit 7cfc04
got lost in Linux 2.2.
Packit 7cfc04
The protocol that the socket was bound to or that
Packit 7cfc04
was specified in the initial
Packit 7cfc04
.BR socket (2)
Packit 7cfc04
call is always used.
Packit 7cfc04
.\" .SH AUTHORS
Packit 7cfc04
.\" This man page was written by Andi Kleen.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR recvmsg (2),
Packit 7cfc04
.BR sendmsg (2),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR socket (7)
Packit 7cfc04
.PP
Packit 7cfc04
.B RFC\ 1191
Packit 7cfc04
for path MTU discovery.
Packit 7cfc04
.B RFC\ 791
Packit 7cfc04
and the
Packit 7cfc04
.I <linux/ip.h>
Packit 7cfc04
header file for the IP protocol.
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.