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

# mnl\_attr\_put(3)

Netlink attribute helpers

Part of [mnl\_attr\_get\_len(3)](/mnl_attr_get_len/3.md).

## NAME

attr - Netlink attribute helpers

## SYNOPSIS

### Functions

uint16\_t **mnl\_attr\_get\_type** (const struct nlattr \*attr)\
 uint16\_t **mnl\_attr\_get\_len** (const struct nlattr \*attr)\
 uint16\_t **mnl\_attr\_get\_payload\_len** (const struct nlattr \*attr)\
 void \* **mnl\_attr\_get\_payload** (const struct nlattr \*attr)\
 bool **mnl\_attr\_ok** (const struct nlattr \*attr, int len)\
 struct nlattr \* **mnl\_attr\_next** (const struct nlattr \*attr)\
 int **mnl\_attr\_type\_valid** (const struct nlattr \*attr, uint16\_t max)\
 int **mnl\_attr\_validate** (const struct nlattr \*attr, enum mnl\_attr\_data\_type type)\
 int **mnl\_attr\_validate2** (const struct nlattr \*attr, enum mnl\_attr\_data\_type type, size\_t exp\_len)\
 int **mnl\_attr\_parse** (const struct nlmsghdr \*nlh, unsigned int offset, mnl\_attr\_cb\_t cb, void \*data)\
 int **mnl\_attr\_parse\_nested** (const struct nlattr \*nested, mnl\_attr\_cb\_t cb, void \*data)\
 int **mnl\_attr\_parse\_payload** (const void \*payload, size\_t payload\_len, mnl\_attr\_cb\_t cb, void \*data)\
 uint8\_t **mnl\_attr\_get\_u8** (const struct nlattr \*attr)\
 uint16\_t **mnl\_attr\_get\_u16** (const struct nlattr \*attr)\
 uint32\_t **mnl\_attr\_get\_u32** (const struct nlattr \*attr)\
 uint64\_t **mnl\_attr\_get\_u64** (const struct nlattr \*attr)\
 const char \* **mnl\_attr\_get\_str** (const struct nlattr \*attr)\
 void **mnl\_attr\_put** (struct nlmsghdr \*nlh, uint16\_t type, size\_t len, const void \*data)\
 void **mnl\_attr\_put\_u8** (struct nlmsghdr \*nlh, uint16\_t type, uint8\_t data)\
 void **mnl\_attr\_put\_u16** (struct nlmsghdr \*nlh, uint16\_t type, uint16\_t data)\
 void **mnl\_attr\_put\_u32** (struct nlmsghdr \*nlh, uint16\_t type, uint32\_t data)\
 void **mnl\_attr\_put\_u64** (struct nlmsghdr \*nlh, uint16\_t type, uint64\_t data)\
 void **mnl\_attr\_put\_str** (struct nlmsghdr \*nlh, uint16\_t type, const char \*data)\
 void **mnl\_attr\_put\_strz** (struct nlmsghdr \*nlh, uint16\_t type, const char \*data)\
 struct nlattr \* **mnl\_attr\_nest\_start** (struct nlmsghdr \*nlh, uint16\_t type)\
 bool **mnl\_attr\_put\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, size\_t len, const void \*data)\
 bool **mnl\_attr\_put\_u8\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, uint8\_t data)\
 bool **mnl\_attr\_put\_u16\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, uint16\_t data)\
 bool **mnl\_attr\_put\_u32\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, uint32\_t data)\
 bool **mnl\_attr\_put\_u64\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, uint64\_t data)\
 bool **mnl\_attr\_put\_str\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, const char \*data)\
 bool **mnl\_attr\_put\_strz\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type, const char \*data)\
 struct nlattr \* **mnl\_attr\_nest\_start\_check** (struct nlmsghdr \*nlh, size\_t buflen, uint16\_t type)\
 void **mnl\_attr\_nest\_end** (struct nlmsghdr \*nlh, struct nlattr \*start)\
 void **mnl\_attr\_nest\_cancel** (struct nlmsghdr \*nlh, struct nlattr \*start)\

## Detailed Description

Netlink Type-Length-Value (TLV) attribute:

```
        |<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
        -------------------------------------------------
        |     length    |      type     |      value     |
        -------------------------------------------------
        |<--------- header ------------>|<-- payload --->|
```

 The payload of the Netlink message contains sequences of attributes that are expressed in TLV format.

## Function Documentation

### uint16\_t mnl\_attr\_get\_len (const struct nlattr \* attr)

mnl\_attr\_get\_len - get length of netlink attribute

**Parameters**

*attr* pointer to netlink attribute

This function returns the attribute length that is the attribute header plus the attribute payload.

Definition at line **50** of file **attr.c**.

### void \* mnl\_attr\_get\_payload (const struct nlattr \* attr)

mnl\_attr\_get\_payload - get pointer to the attribute payload

**Parameters**

*attr* pointer to netlink attribute

This function return a pointer to the attribute payload.

Definition at line **72** of file **attr.c**.

### uint16\_t mnl\_attr\_get\_payload\_len (const struct nlattr \* attr)

mnl\_attr\_get\_payload\_len - get the attribute payload-value length

**Parameters**

*attr* pointer to netlink attribute

This function returns the attribute payload-value length.

Definition at line **61** of file **attr.c**.

### const char \* mnl\_attr\_get\_str (const struct nlattr \* attr)

mnl\_attr\_get\_str - returns pointer to string attribute.

**Parameters**

*attr* pointer to netlink attribute

This function returns the payload of string attribute value.

Definition at line **380** of file **attr.c**.

### uint16\_t mnl\_attr\_get\_type (const struct nlattr \* attr)

mnl\_attr\_get\_type - get type of netlink attribute

**Parameters**

*attr* pointer to netlink attribute

This function returns the attribute type.

Definition at line **38** of file **attr.c**.

### uint16\_t mnl\_attr\_get\_u16 (const struct nlattr \* attr)

mnl\_attr\_get\_u16 - returns 16-bit unsigned integer attribute payload

**Parameters**

*attr* pointer to netlink attribute

This function returns the 16-bit value of the attribute payload.

Definition at line **343** of file **attr.c**.

### uint32\_t mnl\_attr\_get\_u32 (const struct nlattr \* attr)

mnl\_attr\_get\_u32 - returns 32-bit unsigned integer attribute payload

**Parameters**

*attr* pointer to netlink attribute

This function returns the 32-bit value of the attribute payload.

Definition at line **354** of file **attr.c**.

### uint64\_t mnl\_attr\_get\_u64 (const struct nlattr \* attr)

mnl\_attr\_get\_u64 - returns 64-bit unsigned integer attribute.

**Parameters**

*attr* pointer to netlink attribute

This function returns the 64-bit value of the attribute payload. This function is align-safe, since accessing 64-bit Netlink attributes is a common source of alignment issues.

Definition at line **367** of file **attr.c**.

### uint8\_t mnl\_attr\_get\_u8 (const struct nlattr \* attr)

mnl\_attr\_get\_u8 - returns 8-bit unsigned integer attribute payload

**Parameters**

*attr* pointer to netlink attribute

This function returns the 8-bit value of the attribute payload.

Definition at line **332** of file **attr.c**.

### void mnl\_attr\_nest\_cancel (struct nlmsghdr \* nlh, struct nlattr \* start)

mnl\_attr\_nest\_cancel - cancel an attribute nest

**Parameters**

*nlh* pointer to the netlink message \
 *start* pointer to the attribute nest returned by **mnl\_attr\_nest\_start()**

This function updates the attribute header that identifies the nest.

Definition at line **714** of file **attr.c**.

### void mnl\_attr\_nest\_end (struct nlmsghdr \* nlh, struct nlattr \* start)

mnl\_attr\_nest\_end - end an attribute nest

**Parameters**

*nlh* pointer to the netlink message \
 *start* pointer to the attribute nest returned by **mnl\_attr\_nest\_start()**

This function updates the attribute header that identifies the nest.

Definition at line **701** of file **attr.c**.

### struct nlattr \* mnl\_attr\_nest\_start (struct nlmsghdr \* nlh, uint16\_t type)

mnl\_attr\_nest\_start - start an attribute nest

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest. This function always returns a valid pointer to the beginning of the nest.

Definition at line **514** of file **attr.c**.

### struct nlattr \* mnl\_attr\_nest\_start\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type)

mnl\_attr\_nest\_start\_check - start an attribute nest

**Parameters**

*buflen* size of buffer which stores the message \
 *nlh* pointer to the netlink message \
 *type* netlink attribute type

This function adds the attribute header that identifies the beginning of an attribute nest. If the nested attribute cannot be added then NULL, otherwise valid pointer to the beginning of the nest is returned.

Definition at line **685** of file **attr.c**.

### struct nlattr \* mnl\_attr\_next (const struct nlattr \* attr)

mnl\_attr\_next - get the next attribute in the payload of a netlink message

**Parameters**

*attr* pointer to the current attribute

This function returns a pointer to the next attribute after the one passed as parameter. You have to use **mnl\_attr\_ok()** to ensure that the next attribute is valid.

Definition at line **108** of file **attr.c**.

### bool mnl\_attr\_ok (const struct nlattr \* attr, int len)

mnl\_attr\_ok - check if there is room for an attribute in a buffer

**Parameters**

*attr* attribute that we want to check if there is room for \
 *len* remaining bytes in a buffer that contains the attribute

This function is used to check that a buffer, which is supposed to contain an attribute, has enough room for the attribute that it stores, i.e. this function can be used to verify that an attribute is neither malformed nor truncated.

This function does not set errno in case of error since it is intended for iterations. Thus, it returns true on success and false on error.

The len parameter may be negative in the case of malformed messages during attribute iteration, that is why we use a signed integer.

Definition at line **93** of file **attr.c**.

### int mnl\_attr\_parse (const struct nlmsghdr \* nlh, unsigned int offset, mnl\_attr\_cb\_t cb, void \* data)

mnl\_attr\_parse - parse attributes

**Parameters**

*nlh* pointer to netlink message \
 *offset* offset to start parsing from (if payload is after any header) \
 *cb* callback function that is called for each attribute \
 *data* pointer to data that is passed to the callback function

This function allows you to iterate over the sequence of attributes that compose the Netlink message. You can then put the attribute in an array as it usually happens at this stage or you can use any other data structure (such as lists or trees).

This function propagates the return value of the callback, which can be MNL\_CB\_ERROR, MNL\_CB\_OK or MNL\_CB\_STOP.

Definition at line **255** of file **attr.c**.

### int mnl\_attr\_parse\_nested (const struct nlattr \* nested, mnl\_attr\_cb\_t cb, void \* data)

mnl\_attr\_parse\_nested - parse attributes inside a nest

**Parameters**

*nested* pointer to netlink attribute that contains a nest \
 *cb* callback function that is called for each attribute in the nest \
 *data* pointer to data passed to the callback function

This function allows you to iterate over the sequence of attributes that compose the Netlink message. You can then put the attribute in an array as it usually happens at this stage or you can use any other data structure (such as lists or trees).

This function propagates the return value of the callback, which can be MNL\_CB\_ERROR, MNL\_CB\_OK or MNL\_CB\_STOP.

Definition at line **282** of file **attr.c**.

### int mnl\_attr\_parse\_payload (const void \* payload, size\_t payload\_len, mnl\_attr\_cb\_t cb, void \* data)

mnl\_attr\_parse\_payload - parse attributes in payload of Netlink message

**Parameters**

*payload* pointer to payload of the Netlink message \
 *payload\_len* payload length that contains the attributes \
 *cb* callback function that is called for each attribute \
 *data* pointer to data that is passed to the callback function

This function takes a pointer to the area that contains the attributes, commonly known as the payload of the Netlink message. Thus, you have to pass a pointer to the Netlink message payload, instead of the entire message.

This function allows you to iterate over the sequence of attributes that are located at some payload offset. You can then put the attributes in one array as usual, or you can use any other data structure (such as lists or trees).

This function propagates the return value of the callback, which can be MNL\_CB\_ERROR, MNL\_CB\_OK or MNL\_CB\_STOP.

Definition at line **313** of file **attr.c**.

### void mnl\_attr\_put (struct nlmsghdr \* nlh, uint16\_t type, size\_t len, const void \* data)

mnl\_attr\_put - add an attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type that you want to add \
 *len* netlink attribute payload length \
 *data* pointer to the data that will be stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **395** of file **attr.c**.

### bool mnl\_attr\_put\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, size\_t len, const void \* data)

mnl\_attr\_put\_check - add an attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type that you want to add \
 *len* netlink attribute payload length \
 *data* pointer to the data that will be stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned.

Definition at line **540** of file **attr.c**.

### void mnl\_attr\_put\_str (struct nlmsghdr \* nlh, uint16\_t type, const char \* data)

mnl\_attr\_put\_str - add string attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* pointer to string data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **481** of file **attr.c**.

### bool mnl\_attr\_put\_str\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, const char \* data)

mnl\_attr\_put\_str\_check - add string attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* pointer to string data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **647** of file **attr.c**.

### void mnl\_attr\_put\_strz (struct nlmsghdr \* nlh, uint16\_t type, const char \* data)

mnl\_attr\_put\_strz - add string attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* pointer to string data that is stored by the new attribute

This function is similar to mnl\_attr\_put\_str, but it includes the NUL/zero ('\\0') terminator at the end of the string.

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **499** of file **attr.c**.

### bool mnl\_attr\_put\_strz\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, const char \* data)

mnl\_attr\_put\_strz\_check - add string attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* pointer to string data that is stored by the new attribute

This function is similar to mnl\_attr\_put\_str, but it includes the NUL/zero ('\\0') terminator at the end of the string.

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned.

Definition at line **669** of file **attr.c**.

### void mnl\_attr\_put\_u16 (struct nlmsghdr \* nlh, uint16\_t type, uint16\_t data)

mnl\_attr\_put\_u16 - add 16-bit unsigned integer attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* 16-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **436** of file **attr.c**.

### bool mnl\_attr\_put\_u16\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, uint16\_t data)

mnl\_attr\_put\_u16\_check - add 16-bit unsigned int attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* 16-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **584** of file **attr.c**.

### void mnl\_attr\_put\_u32 (struct nlmsghdr \* nlh, uint16\_t type, uint32\_t data)

mnl\_attr\_put\_u32 - add 32-bit unsigned integer attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* 32-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **451** of file **attr.c**.

### bool mnl\_attr\_put\_u32\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, uint32\_t data)

mnl\_attr\_put\_u32\_check - add 32-bit unsigned int attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* 32-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **605** of file **attr.c**.

### void mnl\_attr\_put\_u64 (struct nlmsghdr \* nlh, uint16\_t type, uint64\_t data)

mnl\_attr\_put\_u64 - add 64-bit unsigned integer attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* 64-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **466** of file **attr.c**.

### bool mnl\_attr\_put\_u64\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, uint64\_t data)

mnl\_attr\_put\_u64\_check - add 64-bit unsigned int attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* 64-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned. This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **626** of file **attr.c**.

### void mnl\_attr\_put\_u8 (struct nlmsghdr \* nlh, uint16\_t type, uint8\_t data)

mnl\_attr\_put\_u8 - add 8-bit unsigned integer attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *type* netlink attribute type \
 *data* 8-bit unsigned integer data that is stored by the new attribute

This function updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute.

Definition at line **421** of file **attr.c**.

### bool mnl\_attr\_put\_u8\_check (struct nlmsghdr \* nlh, size\_t buflen, uint16\_t type, uint8\_t data)

mnl\_attr\_put\_u8\_check - add 8-bit unsigned int attribute to netlink message

**Parameters**

*nlh* pointer to the netlink message \
 *buflen* size of buffer which stores the message \
 *type* netlink attribute type \
 *data* 8-bit unsigned integer data that is stored by the new attribute

This function first checks that the data can be added to the message (fits into the buffer) and then updates the length field of the Netlink message (nlmsg\_len) by adding the size (header + payload) of the new attribute. The function returns true if the attribute could be added to the message, otherwise false is returned.

Definition at line **563** of file **attr.c**.

### int mnl\_attr\_type\_valid (const struct nlattr \* attr, uint16\_t max)

mnl\_attr\_type\_valid - check if the attribute type is valid

**Parameters**

*attr* pointer to attribute to be checked \
 *max* maximum attribute type

This function allows one to check if the attribute type is higher than the maximum supported type. If the attribute type is invalid, this function returns -1 and errno is explicitly set. On success, this function returns 1.

Strict attribute checking in user-space is not a good idea since you may run an old application with a newer kernel that supports new attributes. This leads to backward compatibility breakages in user-space. Better check if you support an attribute, if not, skip it.

Definition at line **127** of file **attr.c**.

### int mnl\_attr\_validate (const struct nlattr \* attr, enum mnl\_attr\_data\_type type)

mnl\_attr\_validate - validate netlink attribute (simplified version)

**Parameters**

*attr* pointer to netlink attribute that we want to validate \
 *type* data type (see enum mnl\_attr\_data\_type)

The validation is based on the data type. Specifically, it checks that integers (u8, u16, u32 and u64) have enough room for them. This function returns -1 in case of error, and errno is explicitly set.

Definition at line **207** of file **attr.c**.

### int mnl\_attr\_validate2 (const struct nlattr \* attr, enum mnl\_attr\_data\_type type, size\_t exp\_len)

mnl\_attr\_validate2 - validate netlink attribute (extended version)

**Parameters**

*attr* pointer to netlink attribute that we want to validate \
 *type* attribute type (see enum mnl\_attr\_data\_type) \
 *exp\_len* expected attribute data size

This function allows one to perform a more accurate validation for attributes whose size is variable. If the size of the attribute is not what is expected, this functions returns -1 and errno is explicitly set.

Definition at line **229** of file **attr.c**.

## Author

Generated automatically by Doxygen for libmnl from the source code.
