Packet sockets: Difference between revisions
No edit summary |
|||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Packet sockets allow a program to more directly interface with the networking stack than standard Layer 4 Berkeley sockets (e.g. AF_INET + SOCK_STREAM | Packet sockets allow a program to more directly interface with the networking stack than standard Layer 4 Berkeley sockets (e.g. AF_INET/AF_INET6 + SOCK_STREAM/SOCK_DGRAM). | ||
==[[Linux APIs|Linux]] | ==[[Linux APIs|Linux]] packet sockets== | ||
The SOCK_PACKET socket type is strongly deprecated (see packet(7)), and thus not discussed here. | The SOCK_PACKET socket type is strongly deprecated (see packet(7)), and thus not discussed here. | ||
{| class="wikitable" border="1" | {| class="wikitable" border="1" | ||
| Line 35: | Line 35: | ||
|- | |- | ||
| IP_HDRINCL-enabled PF_INET, SOCK_RAW | | IP_HDRINCL-enabled PF_INET, SOCK_RAW | ||
(only one IP protocol per socket when protocol is other than IPPROTO_RAW) | (only one IP protocol per socket when protocol is other than IPPROTO_RAW, | ||
not valid for PF_INET6 sockets) | |||
| Y | | Y | ||
| N | | N | ||
| Line 64: | Line 65: | ||
Cooked packets (common, protocol-independent link-layer header in a <tt>sockaddr_ll</tt>). | Cooked packets (common, protocol-independent link-layer header in a <tt>sockaddr_ll</tt>). | ||
==AF_INET/AF_INET6== | ==AF_INET/AF_INET6== | ||
The protocol field restricts the Layer 3 protocols which will be passed to the socket; IPPROTO_RAW can set arbitrary IP protocol values, and thus implies the IP_HDRINCL socket option. | The protocol field restricts the Layer 3 protocols which will be passed to the socket; IPPROTO_RAW can set arbitrary IP protocol values (for transmission, but not reception), and thus implies the IP_HDRINCL socket option. Even if IP_HDRINCL is set, raw sockets might rewrite the following fields: | ||
* Total length | |||
* Checksum | |||
* Packet ID (given a random value if set to 0) | |||
* Source address (selected via routing lookup if set to 0) | |||
The IP_HDRINCL (IPV6_HDRINCL) option is not supported for IPv6 raw sockets. | |||
===SOCK_RAW=== | ===SOCK_RAW=== | ||
Path MTU is performed on outgoing packets unless disabled via IP_MTU_DISCOVER sockopt, in which case they might be fragmented if they exceed the outgoing device's MTU (unless IP_HDRINCL is used). Fragment reassembly is performed before delivering packets to receiving sockets. sendto() must be used with raw sockets, as opposed to a mere send(). When using an IPv6 raw socket, sin6_port must be set to 0 to avoid an EINVAL ("Invalid Argument") error. | |||
===SOCK_PACKET=== | ===SOCK_PACKET=== | ||
Obsolete since Linux 2.0. | Obsolete since Linux 2.0. | ||
| Line 71: | Line 79: | ||
==See also== | ==See also== | ||
* [[Linux APIs]] and [[FreeBSD APIs]] | * [[Linux APIs]] and [[FreeBSD APIs]] | ||
* "[https://fly.io/blog/bpf-xdp-packet-filters-and-udp/ BPF, XDP, Packet Filters and UDP]" at fly.io | |||