---
name: "packet"
section: "7"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/packet"
---

# packet(7)

packet interface on device level

## NAME

packet - packet interface on device level

## SYNOPSIS

```
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h> /* the L2 protocols */
```

**packet\_socket = socket(AF\_PACKET, int** *socket\_type***, int** *protocol***);**

## DESCRIPTION

Packet sockets are used to receive or send raw packets at the device driver (OSI Layer 2) level. They allow the user to implement protocol modules in user space on top of the physical layer.

The *socket\_type* is either **SOCK\_RAW** for raw packets including the link-level header or **SOCK\_DGRAM** for cooked packets with the link-level header removed. The link-level header information is available in a common format in a *sockaddr\_ll* structure. *protocol* is the IEEE 802.3 protocol number in network byte order. See the *\<linux/if\_ether.h\>* include file for a list of allowed protocols. When protocol is set to **htons(ETH\_P\_ALL)**, then all protocols are received. All incoming packets of that protocol type will be passed to the packet socket before they are passed to the protocols implemented in the kernel. If *protocol* is set to zero, no packets are received. [**bind**(2)](/bind/2.md) can optionally be called with a nonzero *sll\_protocol* to start receiving packets for the protocols specified.

In order to create a packet socket, a process must have the **CAP\_NET\_RAW** capability in the user namespace that governs its network namespace.

**SOCK\_RAW** packets are passed to and from the device driver without any changes in the packet data. When receiving a packet, the address is still parsed and passed in a standard *sockaddr\_ll* address structure. When transmitting a packet, the user-supplied buffer should contain the physical-layer header. That packet is then queued unmodified to the network driver of the interface defined by the destination address. Some device drivers always add other headers. **SOCK\_RAW** is similar to but not compatible with the obsolete **AF\_INET/SOCK\_PACKET** of Linux 2.0.

**SOCK\_DGRAM** operates on a slightly higher level. The physical header is removed before the packet is passed to the user. Packets sent through a **SOCK\_DGRAM** packet socket get a suitable physical-layer header based on the information in the *sockaddr\_ll* destination address before they are queued.

By default, all packets of the specified protocol type are passed to a packet socket. To get packets only from a specific interface use [**bind**(2)](/bind/2.md) specifying an address in a *struct sockaddr\_ll* to bind the packet socket to an interface. Fields used for binding are *sll\_family* (should be **AF\_PACKET**), *sll\_protocol*, and *sll\_ifindex*.

The [**connect**(2)](/connect/2.md) operation is not supported on packet sockets.

When the **MSG\_TRUNC** flag is passed to [**recvmsg**(2)](/recvmsg/2.md), [**recv**(2)](/recv/2.md), or [**recvfrom**(2)](/recvfrom/2.md), the real size of the packet on the wire is always returned, even when it is longer than the buffer.

### Address types

The *sockaddr\_ll* structure is a device-independent physical-layer address.

```
struct sockaddr_ll {
    unsigned short sll_family;   /* Always AF_PACKET */
    unsigned short sll_protocol; /* Physical-layer protocol */
    int            sll_ifindex;  /* Interface number */
    unsigned short sll_hatype;   /* ARP hardware type */
    unsigned char  sll_pkttype;  /* Packet type */
    unsigned char  sll_halen;    /* Size of address */
    unsigned char  sll_addr[8];  /* Physical-layer address */
};
```

The fields of this structure are as follows:

- *sll\_protocol*

  is the standard ethernet protocol type in network byte order as defined in the *\<linux/if\_ether.h\>* include file. It defaults to the socket's protocol.

- *sll\_ifindex*

  is the interface index of the interface (see [**netdevice**(7)](/netdevice/7.md)); 0 matches any interface (only permitted for binding). *sll\_hatype* is an ARP type as defined in the *\<linux/if\_arp.h\>* include file.

- *sll\_pkttype*

  contains the packet type. Valid types are **PACKET\_HOST** for a packet addressed to the local host, **PACKET\_BROADCAST** for a physical-layer broadcast packet, **PACKET\_MULTICAST** for a packet sent to a physical-layer multicast address, **PACKET\_OTHERHOST** for a packet to some other host that has been caught by a device driver in promiscuous mode, and **PACKET\_OUTGOING** for a packet originating from the local host that is looped back to a packet socket. These types make sense only for receiving.

- *sll\_addr* *sll\_halen*

  contain the physical-layer (e.g., IEEE 802.3) address and its size. The exact interpretation depends on the device.

When you send packets, it is enough to specify *sll\_family*, *sll\_addr*, *sll\_halen*, *sll\_ifindex*, and *sll\_protocol*. The other fields should be 0. *sll\_hatype* and *sll\_pkttype* are set on received packets for your information.

### Socket options

Packet socket options are configured by calling [**setsockopt**(2)](/setsockopt/2.md) with level **SOL\_PACKET**.

- **PACKET\_ADD\_MEMBERSHIP**

  - **PACKET\_DROP\_MEMBERSHIP**

  Packet sockets can be used to configure physical-layer multicasting and promiscuous mode. **PACKET\_ADD\_MEMBERSHIP** adds a binding and **PACKET\_DROP\_MEMBERSHIP** drops it. They both expect a *packet\_mreq* structure as argument:

-

  ```
  struct packet_mreq {
      int            mr_ifindex;    /* interface index */
      unsigned short mr_type;       /* action */
      unsigned short mr_alen;       /* address size */
      unsigned char  mr_address[8]; /* physical-layer address */
  };
  ```

-

  *mr\_ifindex* contains the interface index for the interface whose status should be changed. The *mr\_type* field specifies which action to perform. **PACKET\_MR\_PROMISC** enables receiving all packets on a shared medium (often known as "promiscuous mode"), **PACKET\_MR\_MULTICAST** binds the socket to the physical-layer multicast group specified in *mr\_address* and *mr\_alen*, and **PACKET\_MR\_ALLMULTI** sets the socket up to receive all multicast packets arriving at the interface.

-

  In addition, the traditional ioctls **SIOCSIFFLAGS**, **SIOCADDMULTI**, **SIOCDELMULTI** can be used for the same purpose.

- **PACKET\_AUXDATA** (since Linux 2.6.21)

  If this binary option is enabled, the packet socket passes a metadata structure along with each packet in the [**recvmsg**(2)](/recvmsg/2.md) control field. The structure can be read with [**cmsg**(3)](/cmsg/3.md). It is defined as

-

  ```
  struct tpacket_auxdata {
      __u32 tp_status;
      __u32 tp_len;      /* packet size */
      __u32 tp_snaplen;  /* captured size */
      __u16 tp_mac;
      __u16 tp_net;
      __u16 tp_vlan_tci;
      __u16 tp_vlan_tpid; /* Since Linux 3.14; earlier, these
                             were unused padding bytes */
  };
  ```

- **PACKET\_FANOUT** (since Linux 3.1)

  To scale processing across threads, packet sockets can form a fanout group. In this mode, each matching packet is enqueued onto only one socket in the group. A socket joins a fanout group by calling [**setsockopt**(2)](/setsockopt/2.md) with level **SOL\_PACKET** and option **PACKET\_FANOUT**. Each network namespace can have up to 65536 independent groups. A socket selects a group by encoding the ID in the first 16 bits of the integer option value. The first packet socket to join a group implicitly creates it. To successfully join an existing group, subsequent packet sockets must have the same protocol, device settings, fanout mode, and flags (see below). Packet sockets can leave a fanout group only by closing the socket. The group is deleted when the last socket is closed.

-

  Fanout supports multiple algorithms to spread traffic between sockets, as follows:

  - •

    The default mode, **PACKET\_FANOUT\_HASH**, sends packets from the same flow to the same socket to maintain per-flow ordering. For each packet, it chooses a socket by taking the packet flow hash modulo the number of sockets in the group, where a flow hash is a hash over network-layer address and optional transport-layer port fields.

  - •

    The load-balance mode **PACKET\_FANOUT\_LB** implements a round-robin algorithm.

  - •

    **PACKET\_FANOUT\_CPU** selects the socket based on the CPU that the packet arrived on.

  - •

    **PACKET\_FANOUT\_ROLLOVER** processes all data on a single socket, moving to the next when one becomes backlogged.

  - •

    **PACKET\_FANOUT\_RND** selects the socket using a pseudo-random number generator.

  - •

    **PACKET\_FANOUT\_QM** (available since Linux 3.14) selects the socket using the recorded queue\_mapping of the received skb.

-

  Fanout modes can take additional options. IP fragmentation causes packets from the same flow to have different flow hashes. The flag **PACKET\_FANOUT\_FLAG\_DEFRAG**, if set, causes packets to be defragmented before fanout is applied, to preserve order even in this case. Fanout mode and options are communicated in the second 16 bits of the integer option value. The flag **PACKET\_FANOUT\_FLAG\_ROLLOVER** enables the roll over mechanism as a backup strategy: if the original fanout algorithm selects a backlogged socket, the packet rolls over to the next available one.

- **PACKET\_LOSS** (with **PACKET\_TX\_RING**)

  When a malformed packet is encountered on a transmit ring, the default is to reset its *tp\_status* to **TP\_STATUS\_WRONG\_FORMAT** and abort the transmission immediately. The malformed packet blocks itself and subsequently enqueued packets from being sent. The format error must be fixed, the associated *tp\_status* reset to **TP\_STATUS\_SEND\_REQUEST**, and the transmission process restarted via [**send**(2)](/send/2.md). However, if **PACKET\_LOSS** is set, any malformed packet will be skipped, its *tp\_status* reset to **TP\_STATUS\_AVAILABLE**, and the transmission process continued.

- **PACKET\_RESERVE** (with **PACKET\_RX\_RING**)

  By default, a packet receive ring writes packets immediately following the metadata structure and alignment padding. This integer option reserves additional headroom.

- **PACKET\_RX\_RING**

  Create a memory-mapped ring buffer for asynchronous packet reception. The packet socket reserves a contiguous region of application address space, lays it out into an array of packet slots and copies packets (up to *tp\_snaplen*) into subsequent slots. Each packet is preceded by a metadata structure similar to *tpacket\_auxdata*. The protocol fields encode the offset to the data from the start of the metadata header. *tp\_net* stores the offset to the network layer. If the packet socket is of type **SOCK\_DGRAM**, then *tp\_mac* is the same. If it is of type **SOCK\_RAW**, then that field stores the offset to the link-layer frame. Packet socket and application communicate the head and tail of the ring through the *tp\_status* field. The packet socket owns all slots with *tp\_status* equal to **TP\_STATUS\_KERNEL**. After filling a slot, it changes the status of the slot to transfer ownership to the application. During normal operation, the new *tp\_status* value has at least the **TP\_STATUS\_USER** bit set to signal that a received packet has been stored. When the application has finished processing a packet, it transfers ownership of the slot back to the socket by setting *tp\_status* equal to **TP\_STATUS\_KERNEL**.

-

  Packet sockets implement multiple variants of the packet ring. The implementation details are described in *Documentation/networking/packet\_mmap.rst* in the Linux kernel source tree.

- **PACKET\_STATISTICS**

  Retrieve packet socket statistics in the form of a structure

-

  ```
  struct tpacket_stats {
      unsigned int tp_packets;  /* Total packet count */
      unsigned int tp_drops;    /* Dropped packet count */
  };
  ```

-

  Receiving statistics resets the internal counters. The statistics structure differs when using a ring of variant **TPACKET\_V3**.

- **PACKET\_TIMESTAMP** (with **PACKET\_RX\_RING**; since Linux 2.6.36)

  The packet receive ring always stores a timestamp in the metadata header. By default, this is a software generated timestamp generated when the packet is copied into the ring. This integer option selects the type of timestamp. Besides the default, it support the two hardware formats described in *Documentation/networking/timestamping.rst* in the Linux kernel source tree.

- **PACKET\_TX\_RING** (since Linux 2.6.31)

  Create a memory-mapped ring buffer for packet transmission. This option is similar to **PACKET\_RX\_RING** and takes the same arguments. The application writes packets into slots with *tp\_status* equal to **TP\_STATUS\_AVAILABLE** and schedules them for transmission by changing *tp\_status* to **TP\_STATUS\_SEND\_REQUEST**. When packets are ready to be transmitted, the application calls [**send**(2)](/send/2.md) or a variant thereof. The *buf* and *len* fields of this call are ignored. If an address is passed using [**sendto**(2)](/sendto/2.md) or [**sendmsg**(2)](/sendmsg/2.md), then that overrides the socket default. On successful transmission, the socket resets *tp\_status* to **TP\_STATUS\_AVAILABLE**. It immediately aborts the transmission on error unless **PACKET\_LOSS** is set.

- **PACKET\_VERSION** (with **PACKET\_RX\_RING**; since Linux 2.6.27)

  By default, **PACKET\_RX\_RING** creates a packet receive ring of variant **TPACKET\_V1**. To create another variant, configure the desired variant by setting this integer option before creating the ring.

- **PACKET\_QDISC\_BYPASS** (since Linux 3.14)

  By default, packets sent through packet sockets pass through the kernel's qdisc (traffic control) layer, which is fine for the vast majority of use cases. For traffic generator appliances using packet sockets that intend to brute-force flood the network—for example, to test devices under load in a similar fashion to pktgen—this layer can be bypassed by setting this integer option to 1. A side effect is that packet buffering in the qdisc layer is avoided, which will lead to increased drops when network device transmit queues are busy; therefore, use at your own risk.

### Ioctls

**SIOCGSTAMP** can be used to receive the timestamp of the last received packet. Argument is a *struct timeval* variable.

In addition, all standard ioctls defined in [**netdevice**(7)](/netdevice/7.md) and [**socket**(7)](/socket/7.md) are valid on packet sockets.

### Error handling

Packet sockets do no error handling other than errors occurred while passing the packet to the device driver. They don't have the concept of a pending error.

## ERRORS

- **EADDRNOTAVAIL**

  Unknown multicast group address passed.

- **EFAULT**

  User passed invalid memory address.

- **EINVAL**

  Invalid argument.

- **EMSGSIZE**

  Packet is bigger than interface MTU.

- **ENETDOWN**

  Interface is not up.

- **ENOBUFS**

  Not enough memory to allocate the packet.

- **ENODEV**

  Unknown device name or interface index specified in interface address.

- **ENOENT**

  No packet received.

- **ENOTCONN**

  No interface address passed.

- **ENXIO**

  Interface address contained an invalid interface index.

- **EPERM**

  User has insufficient privileges to carry out this operation.

In addition, other errors may be generated by the low-level driver.

## VERSIONS

**AF\_PACKET** is a new feature in Linux 2.2. Earlier Linux versions supported only **SOCK\_PACKET**.

## NOTES

For portable programs it is suggested to use **AF\_PACKET** via [**pcap**(3)](/pcap/3.md); although this covers only a subset of the **AF\_PACKET** features.

The **SOCK\_DGRAM** packet sockets make no attempt to create or parse the IEEE 802.2 LLC header for a IEEE 802.3 frame. When **ETH\_P\_802\_3** is specified as protocol for sending the kernel creates the 802.3 frame and fills out the size field; the user has to supply the LLC header to get a fully conforming packet. Incoming 802.3 packets are not multiplexed on the DSAP/SSAP protocol fields; instead they are supplied to the user as protocol **ETH\_P\_802\_2** with the LLC header prefixed. It is thus not possible to bind to **ETH\_P\_802\_3**; bind to **ETH\_P\_802\_2** instead and do the protocol multiplex yourself. The default for sending is the standard Ethernet DIX encapsulation with the protocol filled in.

Packet sockets are not subject to the input or output firewall chains.

### Compatibility

In Linux 2.0, the only way to get a packet socket was with the call:

```
socket(AF_INET, SOCK_PACKET, protocol)
```

This is still supported, but deprecated and strongly discouraged. The main difference between the two methods is that **SOCK\_PACKET** uses the old *struct sockaddr\_pkt* to specify an interface, which doesn't provide physical-layer independence.

```
struct sockaddr_pkt {
    unsigned short spkt_family;
    unsigned char  spkt_device[14];
    unsigned short spkt_protocol;
};
```

*spkt\_family* contains the device type, *spkt\_protocol* is the IEEE 802.3 protocol type as defined in *\<sys/if\_ether.h\>* and *spkt\_device* is the device name as a null-terminated string, for example, eth0.

This structure is obsolete and should not be used in new code.

## BUGS

### LLC header handling

The IEEE 802.2/803.3 LLC handling could be considered as a bug.

### MSG\_TRUNC issues

The **MSG\_TRUNC** [**recvmsg**(2)](/recvmsg/2.md) extension is an ugly hack and should be replaced by a control message. There is currently no way to get the original destination address of packets via **SOCK\_DGRAM**.

### spkt\_device device name truncation

The *spkt\_device* field of *sockaddr\_pkt* has a size of 14 bytes, which is less than the constant **IFNAMSIZ** defined in *\<net/if.h\>* which is 16 bytes and describes the system limit for a network interface name. This means the names of network devices longer than 14 bytes will be truncated to fit into *spkt\_device*. All these sizes include the terminating null byte ('\\0')).

Issues from this with old code typically show up with very long interface names used by the **Predictable Network Interface Names** feature enabled by default in many modern Linux distributions.

The preferred solution is to rewrite code to avoid **SOCK\_PACKET**. Possible user solutions are to disable **Predictable Network Interface Names** or to rename the interface to a name of at most 13 bytes, for example using the [**ip**(8)](/ip/8.md) tool.

### Documentation issues

Socket filters are not documented.

## SEE ALSO

[**socket**(2)](/socket/2.md), [**pcap**(3)](/pcap/3.md), [**capabilities**(7)](/capabilities/7.md), [**ip**(7)](/ip/7.md), [**raw**(7)](/raw/7.md), [**socket**(7)](/socket/7.md), [**ip**(8)](/ip/8.md),

RFC 894 for the standard IP Ethernet encapsulation. RFC 1700 for the IEEE 802.3 IP encapsulation.

The *\<linux/if\_ether.h\>* include file for physical-layer protocols.

The Linux kernel source tree. *Documentation/networking/filter.rst* describes how to apply Berkeley Packet Filters to packet sockets. *tools/testing/selftests/net/psock\_tpacket.c* contains example source code for all available versions of **PACKET\_RX\_RING** and **PACKET\_TX\_RING**.
