---
name: "pktb_alloc"
section: "3"
platform: "linux"
package: "libnetfilter-queue-doc"
version: "1.0.5-4"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/pktb_alloc"
---

# pktb\_alloc(3)

User-space network packet buffer

## NAME

pktbuff - User-space network packet buffer

## SYNOPSIS

### Modules

**Other functions**\

### Functions

struct pkt\_buff \* **pktb\_alloc** (int family, void \*data, size\_t len, size\_t extra)\
 uint8\_t \* **pktb\_data** (struct pkt\_buff \*pktb)\
 uint32\_t **pktb\_len** (struct pkt\_buff \*pktb)\
 void **pktb\_free** (struct pkt\_buff \*pktb)\
 int **pktb\_mangle** (struct pkt\_buff \*pktb, int dataoff, unsigned int match\_offset, unsigned int match\_len, const char \*rep\_buffer, unsigned int rep\_len)\
 bool **pktb\_mangled** (const struct pkt\_buff \*pktb)\

## Detailed Description

This library provides the user-space network packet buffer. This abstraction is strongly inspired by Linux kernel network buffer, the so-called sk\_buff.

## Function Documentation

### struct pkt\_buff \* pktb\_alloc (int family, void \* data, size\_t len, size\_t extra)

pktb\_alloc - allocate a new packet buffer

**Parameters**

*family* Indicate what family. Currently supported families are AF\_BRIDGE, AF\_INET & AF\_INET6. \
 *data* Pointer to packet data \
 *len* Packet length \
 *extra* Extra memory in the tail to be allocated (for mangling)

This function returns a packet buffer that contains the packet data and some extra memory room in the tail (if requested).

**Returns**

Pointer to a new userspace packet buffer or NULL on failure.

**Errors**

**ENOMEM** From **calloc**() \
 **EPROTONOSUPPORT** *family* was **AF\_BRIDGE** and this is not an IP packet (v4 or v6)

**See also**

[**calloc**(3)](/calloc/3.md)

Definition at line **52** of file **pktbuff.c**.

### uint8\_t \* pktb\_data (struct pkt\_buff \* pktb)

pktb\_data - get pointer to network packet

**Parameters**

*pktb* Pointer to userspace packet buffer

**Returns**

Pointer to start of network packet data within **pktb**

It is appropriate to use *pktb\_data* as the second argument of **nfq\_nlmsg\_verdict\_put\_pkt()**

Definition at line **105** of file **pktbuff.c**.

### void pktb\_free (struct pkt\_buff \* pktb)

pktb\_free - release packet buffer

**Parameters**

*pktb* Pointer to userspace packet buffer

Definition at line **129** of file **pktbuff.c**.

### uint32\_t pktb\_len (struct pkt\_buff \* pktb)

pktb\_len - get length of packet buffer

**Parameters**

*pktb* Pointer to userspace packet buffer

**Returns**

Length of packet contained within **pktb**

It is appropriate to use *pktb\_len* as the third argument of **nfq\_nlmsg\_verdict\_put\_pkt()**

Definition at line **119** of file **pktbuff.c**.

### int pktb\_mangle (struct pkt\_buff \* pktb, int dataoff, unsigned int match\_offset, unsigned int match\_len, const char \* rep\_buffer, unsigned int rep\_len)

pktb\_mangle - adjust contents of a packet

**Parameters**

*pktb* Pointer to userspace packet buffer \
 *dataoff* Supplementary offset, usually offset from layer 3 (IP) header to the layer 4 (TCP or UDP) header. Specify zero to access the layer 3 header. If **pktb** was created in family **AF\_BRIDGE**, specify **-ETH\_HLEN** (a negative offset) to access the layer 2 (MAC) header. \
 *match\_offset* Further offset to content that you want to mangle \
 *match\_len* Length of the existing content you want to mangle \
 *rep\_buffer* Pointer to data you want to use to replace current content \
 *rep\_len* Length of data you want to use to replace current content

**Returns**

1 for success and 0 for failure. Failure will occur if the **extra** argument to the **pktb\_alloc()** call that created **pktb** is less than the excess of **rep\_len** over **match\_len**

**Warning**

pktb\_mangle does not update any checksums. Developers should use the appropriate mangler for the protocol level: **nfq\_ip\_mangle()**, **nfq\_tcp\_mangle\_ipv4()** or **nfq\_udp\_mangle\_ipv4()**. IPv6 versions are planned. \
 It is appropriate to use pktb\_mangle to change the MAC header.

Definition at line **314** of file **pktbuff.c**.

### bool pktb\_mangled (const struct pkt\_buff \* pktb)

pktb\_mangled - test whether packet has been mangled

**Parameters**

*pktb* Pointer to userspace packet buffer

**Returns**

**true** if packet has been mangled (modified), else **false**

When assembling a verdict, it is not necessary to return the contents of un-modified packets. Use *pktb\_mangled* to decide whether packet contents need to be returned.

Definition at line **359** of file **pktbuff.c**.

## Author

Generated automatically by Doxygen for libnetfilter\_queue from the source code.
