Netlink Message Construction/Parsing Interface. More...
Modules | |
Netlink attribute helpers | |
Netlink Type-Length-Value (TLV) attribute: | |
Data Structures | |
struct | nlmsghdr |
Netlink message header. More... | |
struct | nlmsgerr |
Netlink error message. More... | |
Macros | |
#define | NL_AUTO_PID 0 |
Will cause the netlink pid to be set to the pid assigned to the netlink handle (socket) just before sending the message off. More... | |
#define | NL_AUTO_SEQ 0 |
May be used to refer to a sequence number which should be automatically set just before sending the message off. More... | |
Iterators | |
#define | nlmsg_for_each_attr(pos, nlh, hdrlen, rem) |
Iterate over a stream of attributes in a message. More... | |
#define | nlmsg_for_each_msg(pos, head, len, rem) |
Iterate over a stream of messages. More... | |
Standard message flags | |
#define | NLM_F_REQUEST 1 |
Must be set on all request messages (typically from user space to kernel space). More... | |
#define | NLM_F_MULTI 2 |
Indicates the message is part of a multipart message terminated by NLMSG_DONE. More... | |
#define | NLM_F_ACK 4 |
Request for an acknowledgment on success. More... | |
#define | NLM_F_ECHO 8 |
Echo this request. More... | |
Additional message flags for GET requests | |
#define | NLM_F_ROOT 0x100 |
Return the complete table instead of a single entry. More... | |
#define | NLM_F_MATCH 0x200 |
Return all entries matching criteria passed in message content. More... | |
#define | NLM_F_ATOMIC 0x400 |
Return an atomic snapshot of the table being referenced. More... | |
#define | NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH) |
Dump all entries. More... | |
Additional messsage flags for NEW requests | |
#define | NLM_F_REPLACE 0x100 |
Replace existing matching config object with this request. More... | |
#define | NLM_F_EXCL 0x200 |
Don't replace the config object if it already exists. More... | |
#define | NLM_F_CREATE 0x400 |
Create config object if it doesn't already exist. More... | |
#define | NLM_F_APPEND 0x800 |
Add to the end of the object list. More... | |
Standard Message types | |
#define | NLMSG_NOOP 0x1 |
No operation, message must be ignored. More... | |
#define | NLMSG_ERROR 0x2 |
The message signals an error and the payload contains a nlmsgerr structure. More... | |
#define | NLMSG_DONE 0x3 |
Message terminates a multipart message. More... | |
#define | NLMSG_OVERRUN 0x4 |
The message signals that data got lost. More... | |
#define | NLMSG_MIN_TYPE 0x10 |
Lower limit of reserved message types. More... | |
Size Calculations | |
int | nlmsg_msg_size (int payload) |
length of netlink message not including padding More... | |
int | nlmsg_total_size (int payload) |
length of netlink message including padding More... | |
int | nlmsg_padlen (int payload) |
length of padding at the message's tail More... | |
Payload Access | |
void * | nlmsg_data (const struct nlmsghdr *nlh) |
head of message payload More... | |
void * | nlmsg_tail (const struct nlmsghdr *nlh) |
int | nlmsg_len (const struct nlmsghdr *nlh) |
length of message payload More... | |
Attribute Access | |
struct nlattr * | nlmsg_attrdata (const struct nlmsghdr *nlh, int hdrlen) |
head of attributes data More... | |
int | nlmsg_attrlen (const struct nlmsghdr *nlh, int hdrlen) |
length of attributes data More... | |
Message Parsing | |
int | nlmsg_valid_hdr (const struct nlmsghdr *nlh, int hdrlen) |
int | nlmsg_ok (const struct nlmsghdr *nlh, int remaining) |
check if the netlink message fits into the remaining bytes More... | |
struct nlmsghdr * | nlmsg_next (struct nlmsghdr *nlh, int *remaining) |
next netlink message in message stream More... | |
int | nlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy) |
parse attributes of a netlink message More... | |
struct nlattr * | nlmsg_find_attr (struct nlmsghdr *nlh, int hdrlen, int attrtype) |
nlmsg_find_attr - find a specific attribute in a netlink message More... | |
int | nlmsg_validate (struct nlmsghdr *nlh, int hdrlen, int maxtype, struct nla_policy *policy) |
nlmsg_validate - validate a netlink message including attributes More... | |
Message Building/Access | |
struct nl_msg * | nlmsg_alloc (void) |
Allocate a new netlink message with the default maximum payload size. More... | |
struct nl_msg * | nlmsg_alloc_size (size_t max) |
Allocate a new netlink message with maximum payload size specified. More... | |
struct nl_msg * | nlmsg_inherit (struct nlmsghdr *hdr) |
Allocate a new netlink message and inherit netlink message header. More... | |
struct nl_msg * | nlmsg_alloc_simple (int nlmsgtype, int flags) |
Allocate a new netlink message. More... | |
void | nlmsg_set_default_size (size_t max) |
Set the default maximum message payload size for allocated messages. More... | |
struct nl_msg * | nlmsg_convert (struct nlmsghdr *hdr) |
Convert a netlink message received from a netlink socket to a nl_msg. More... | |
void * | nlmsg_reserve (struct nl_msg *n, size_t len, int pad) |
Reserve room for additional data in a netlink message. More... | |
int | nlmsg_append (struct nl_msg *n, void *data, size_t len, int pad) |
Append data to tail of a netlink message. More... | |
int | nlmsg_expand (struct nl_msg *n, size_t newlen) |
Expand maximum payload size of a netlink message. More... | |
struct nlmsghdr * | nlmsg_put (struct nl_msg *n, uint32_t pid, uint32_t seq, int type, int payload, int flags) |
Add a netlink message header to a netlink message. More... | |
struct nlmsghdr * | nlmsg_hdr (struct nl_msg *n) |
Return actual netlink message. More... | |
void | nlmsg_get (struct nl_msg *msg) |
Acquire a reference on a netlink message. More... | |
void | nlmsg_free (struct nl_msg *msg) |
Release a reference from an netlink message. More... | |
Attributes | |
void | nlmsg_set_proto (struct nl_msg *msg, int protocol) |
int | nlmsg_get_proto (struct nl_msg *msg) |
size_t | nlmsg_get_max_size (struct nl_msg *msg) |
void | nlmsg_set_src (struct nl_msg *msg, struct sockaddr_nl *addr) |
struct sockaddr_nl * | nlmsg_get_src (struct nl_msg *msg) |
void | nlmsg_set_dst (struct nl_msg *msg, struct sockaddr_nl *addr) |
struct sockaddr_nl * | nlmsg_get_dst (struct nl_msg *msg) |
void | nlmsg_set_creds (struct nl_msg *msg, struct ucred *creds) |
struct ucred * | nlmsg_get_creds (struct nl_msg *msg) |
Netlink Message Type Translations | |
char * | nl_nlmsgtype2str (int type, char *buf, size_t size) |
int | nl_str2nlmsgtype (const char *name) |
Netlink Message Flags Translations | |
char * | nl_nlmsg_flags2str (int flags, char *buf, size_t len) |
Direct Parsing | |
int | nl_msg_parse (struct nl_msg *msg, void(*cb)(struct nl_object *, void *), void *arg) |
Dumping | |
void | nl_msg_dump (struct nl_msg *msg, FILE *ofd) |
Dump message in human readable format to file descriptor. More... | |
Netlink Message Construction/Parsing Interface.
The following information is partly extracted from RFC3549 (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)
NLM_F_MULTI
Netlink header flag set, except for the last header which has the Netlink header type NLMSG_DONE
.#define NL_AUTO_PID 0 |
Will cause the netlink pid to be set to the pid assigned to the netlink handle (socket) just before sending the message off.
#define NL_AUTO_SEQ 0 |
May be used to refer to a sequence number which should be automatically set just before sending the message off.
#define NLM_F_ACK 4 |
Request for an acknowledgment on success.
#define NLM_F_APPEND 0x800 |
Add to the end of the object list.
#define NLM_F_ATOMIC 0x400 |
Return an atomic snapshot of the table being referenced.
This may require special privileges because it has the potential to interrupt service in the FE for a longer time.
#define NLM_F_CREATE 0x400 |
Create config object if it doesn't already exist.
#define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH) |
Dump all entries.
#define NLM_F_ECHO 8 |
Echo this request.
#define NLM_F_EXCL 0x200 |
Don't replace the config object if it already exists.
#define NLM_F_MATCH 0x200 |
Return all entries matching criteria passed in message content.
#define NLM_F_MULTI 2 |
Indicates the message is part of a multipart message terminated by NLMSG_DONE.
#define NLM_F_REPLACE 0x100 |
Replace existing matching config object with this request.
#define NLM_F_REQUEST 1 |
Must be set on all request messages (typically from user space to kernel space).
#define NLM_F_ROOT 0x100 |
Return the complete table instead of a single entry.
#define NLMSG_DONE 0x3 |
Message terminates a multipart message.
#define NLMSG_ERROR 0x2 |
The message signals an error and the payload contains a nlmsgerr structure.
This can be looked at as a NACK and typically it is from FEC to CPC.
Iterate over a stream of attributes in a message.
Iterate over a stream of messages.
#define NLMSG_MIN_TYPE 0x10 |
Lower limit of reserved message types.
#define NLMSG_NOOP 0x1 |
No operation, message must be ignored.
#define NLMSG_OVERRUN 0x4 |
The message signals that data got lost.
Dump message in human readable format to file descriptor.
References nl_cache_ops::co_hdrsize, nlmsgerr::error, nlmsgerr::msg, nl_cache_ops_associate(), nlmsg_attrdata(), nlmsg_attrlen(), nlmsg_data(), NLMSG_ERROR, nlmsg_free(), nlmsg_get_proto(), nlmsg_hdr(), nlmsg_inherit(), nlmsghdr::nlmsg_len, nlmsg_len(), nlmsg_msg_size(), and nlmsghdr::nlmsg_type.
References nl_cache_ops_associate(), nl_cache_parse(), NLE_MSGTYPE_NOSUPPORT, nlmsg_get_proto(), nlmsg_hdr(), NULL, nl_parser_param::pp_arg, and nl_parser_param::pp_cb.
char* nl_nlmsg_flags2str | ( | int | flags, |
char * | buf, | ||
size_t | len | ||
) |
References buf, PRINT_FLAG, REQUEST, and s.
char* nl_nlmsgtype2str | ( | int | type, |
char * | buf, | ||
size_t | size | ||
) |
int nl_str2nlmsgtype | ( | const char * | name | ) |
Allocate a new netlink message with the default maximum payload size.
Allocates a new netlink message without any further payload. The maximum payload size defaults to PAGESIZE or as otherwise specified with nlmsg_set_default_size().
Referenced by nfnl_nft_set_dump_request(), nfnl_nft_setelem_dump_request(), nlmsg_inherit(), and rtnl_neightbl_build_change_request().
Allocate a new netlink message.
References flags, NL_DBG, nlmsg_inherit(), and nlmsghdr::nlmsg_type.
Referenced by flnl_lookup_build_request(), main(), nfnlmsg_alloc_simple(), nl_send_simple(), rtnl_class_build_delete_request(), rtnl_link_build_change_request(), rtnl_neightbl_build_change_request(), and rtnl_qdisc_build_delete_request().
Allocate a new netlink message with maximum payload size specified.
Append data to tail of a netlink message.
Extends the netlink message as needed and appends the data of given length to the message.
References NL_DBG, NLE_NOMEM, nlmsg_reserve(), and NULL.
Referenced by flnl_lookup_build_request(), main(), nl_send_simple(), rtnl_class_build_delete_request(), rtnl_link_build_change_request(), rtnl_neightbl_build_change_request(), rtnl_qdisc_build_delete_request(), and rtnl_route_build_msg().
head of attributes data
References data, NLMSG_ALIGN, and nlmsg_data().
Referenced by nl_msg_dump(), nlmsg_find_attr(), nlmsg_parse(), and nlmsg_validate().
length of attributes data
References NLMSG_ALIGN, and nlmsg_len().
Referenced by nl_msg_dump(), nlmsg_find_attr(), nlmsg_parse(), and nlmsg_validate().
Convert a netlink message received from a netlink socket to a nl_msg.
Allocates a new netlink message and copies all of the data pointed to by hdr into the new message object.
References NLMSG_ALIGN, nlmsg_free(), nlmsghdr::nlmsg_len, nl_msg::nm_nlh, and NULL.
head of message payload
References NLMSG_HDRLEN.
Referenced by genlmsg_parse(), genlmsg_put(), genlmsg_valid_hdr(), genlmsg_validate(), nfnl_dump_packet(), nfnlmsg_family(), nfnlmsg_res_id(), nl_msg_dump(), nla_put(), nla_put_nested(), nla_reserve(), nlmsg_attrdata(), and rtnl_route_parse().
Expand maximum payload size of a netlink message.
Reallocates the payload section of a netlink message and increases the maximum payload size of the message.
References NLE_INVAL, NLE_NOMEM, nl_msg::nm_nlh, nl_msg::nm_size, and NULL.
nlmsg_find_attr - find a specific attribute in a netlink message
Returns the first attribute which matches the specified type.
References nla_find(), nlmsg_attrdata(), and nlmsg_attrlen().
Release a reference from an netlink message.
Frees memory after the last reference has been released.
References BUG, NL_DBG, nl_msg::nm_nlh, and nl_msg::nm_refcnt.
Referenced by flnl_lookup(), flnl_lookup_build_request(), main(), nfnl_ct_add(), nfnl_ct_del(), nfnl_ct_query(), nfnl_log_build_create_request(), nfnl_nft_chain_add(), nfnl_nft_chain_delete(), nfnl_nft_chain_query(), nfnl_nft_rule_add(), nfnl_nft_rule_delete(), nfnl_nft_rule_query(), nfnl_nft_set_add(), nfnl_nft_set_delete(), nfnl_nft_set_dump_request(), nfnl_nft_set_query(), nfnl_nft_setelem_dump_request(), nfnl_nft_setelem_query(), nfnl_nft_table_add(), nfnl_nft_table_delete(), nfnl_nft_table_query(), nfnl_queue_build_create_request(), nfnl_queue_msg_build_verdict(), nfnl_queue_msg_send_verdict(), nfnl_queue_msg_send_verdict_payload(), nfnlmsg_alloc_simple(), nl_msg_dump(), nl_send_simple(), nlmsg_convert(), rtnl_addr_add(), rtnl_addr_delete(), rtnl_class_add(), rtnl_class_build_delete_request(), rtnl_class_delete(), rtnl_cls_add(), rtnl_cls_change(), rtnl_cls_delete(), rtnl_link_build_change_request(), rtnl_link_change(), rtnl_neigh_add(), rtnl_neigh_delete(), rtnl_neightbl_build_change_request(), rtnl_neightbl_change(), rtnl_qdisc_add(), rtnl_qdisc_build_delete_request(), rtnl_qdisc_change(), rtnl_qdisc_delete(), rtnl_route_add(), rtnl_route_delete(), rtnl_rule_add(), and rtnl_rule_delete().
Acquire a reference on a netlink message.
References NL_DBG, and nl_msg::nm_refcnt.
References NL_MSG_CRED_PRESENT, nl_msg::nm_creds, nl_msg::nm_flags, and NULL.
Referenced by nl_send_iovec().
struct sockaddr_nl* nlmsg_get_dst | ( | struct nl_msg * | msg | ) |
References nl_msg::nm_dst.
Referenced by nl_send_iovec().
References nl_msg::nm_size.
References nl_msg::nm_protocol.
Referenced by nl_msg_dump(), and nl_msg_parse().
struct sockaddr_nl* nlmsg_get_src | ( | struct nl_msg * | msg | ) |
References nl_msg::nm_src.
Return actual netlink message.
Returns the actual netlink message casted to the type of the netlink message header.
References nl_msg::nm_nlh.
Referenced by nfnl_queue_msg_send_verdict_payload(), nl_auto_complete(), nl_cache_parse_and_add(), nl_msg_dump(), nl_msg_parse(), and nl_send().
Allocate a new netlink message and inherit netlink message header.
Allocates a new netlink message and inherits the original message header. If hdr is not NULL it will be used as a template for the netlink message header, otherwise the header is left blank.
References nlmsg_alloc(), nlmsghdr::nlmsg_flags, nlmsghdr::nlmsg_pid, nlmsghdr::nlmsg_seq, nlmsghdr::nlmsg_type, and nl_msg::nm_nlh.
Referenced by nl_msg_dump(), and nlmsg_alloc_simple().
length of message payload
References NLMSG_HDRLEN, and nlmsghdr::nlmsg_len.
Referenced by nl_msg_dump(), nla_put_nested(), nlmsg_attrlen(), and nlmsg_reserve().
int nlmsg_msg_size | ( | int | payload | ) |
length of netlink message not including padding
References NLMSG_HDRLEN, and payload.
Referenced by nl_msg_dump(), nlmsg_padlen(), nlmsg_total_size(), and nlmsg_valid_hdr().
next netlink message in message stream
References NLMSG_ALIGN, and nlmsghdr::nlmsg_len.
check if the netlink message fits into the remaining bytes
References nlmsghdr::nlmsg_len.
int nlmsg_padlen | ( | int | payload | ) |
length of padding at the message's tail
References nlmsg_msg_size(), and nlmsg_total_size().
int nlmsg_parse | ( | struct nlmsghdr * | nlh, |
int | hdrlen, | ||
struct nlattr * | tb[], | ||
int | maxtype, | ||
struct nla_policy * | policy | ||
) |
parse attributes of a netlink message
See nla_parse()
References nla_parse(), NLE_MSG_TOOSHORT, nlmsg_attrdata(), nlmsg_attrlen(), and nlmsg_valid_hdr().
struct nlmsghdr* nlmsg_put | ( | struct nl_msg * | n, |
uint32_t | pid, | ||
uint32_t | seq, | ||
int | type, | ||
int | payload, | ||
int | flags | ||
) |
Add a netlink message header to a netlink message.
Adds or overwrites the netlink message header in an existing message object. If payload is greater-than zero additional room will be reserved, f.e. for family specific headers. It can be accesed via nlmsg_data().
References BUG, NL_DBG, nlh, NLMSG_ALIGNTO, nlmsghdr::nlmsg_flags, NLMSG_HDRLEN, nlmsghdr::nlmsg_len, nlmsghdr::nlmsg_pid, nlmsg_reserve(), nlmsghdr::nlmsg_seq, nlmsghdr::nlmsg_type, nl_msg::nm_nlh, and NULL.
Referenced by genlmsg_put(), and nfnlmsg_put().
Reserve room for additional data in a netlink message.
Reserves room for additional data at the tail of the an existing netlink message. Eventual padding required will be zeroed out.
References buf, len, NL_DBG, nlmsghdr::nlmsg_len, nlmsg_len(), nl_msg::nm_nlh, nl_msg::nm_size, and NULL.
Referenced by nlmsg_append(), nlmsg_put(), and rtnl_route_build_msg().
References NL_MSG_CRED_PRESENT, nl_msg::nm_creds, and nl_msg::nm_flags.
void nlmsg_set_default_size | ( | size_t | max | ) |
Set the default maximum message payload size for allocated messages.
References max, and nlmsg_total_size().
Referenced by main().
void nlmsg_set_dst | ( | struct nl_msg * | msg, |
struct sockaddr_nl * | addr | ||
) |
References nl_msg::nm_dst.
References nl_msg::nm_protocol.
void nlmsg_set_src | ( | struct nl_msg * | msg, |
struct sockaddr_nl * | addr | ||
) |
References NLMSG_ALIGN, and nlmsghdr::nlmsg_len.
Referenced by nla_nest_end(), nla_nest_start(), nla_reserve(), and rtnl_route_build_msg().
int nlmsg_total_size | ( | int | payload | ) |
length of netlink message including padding
References NLMSG_ALIGN, and nlmsg_msg_size().
Referenced by nlmsg_padlen(), and nlmsg_set_default_size().
References nlmsghdr::nlmsg_len, and nlmsg_msg_size().
Referenced by genlmsg_valid_hdr(), nlmsg_parse(), and nlmsg_validate().
int nlmsg_validate | ( | struct nlmsghdr * | nlh, |
int | hdrlen, | ||
int | maxtype, | ||
struct nla_policy * | policy | ||
) |
nlmsg_validate - validate a netlink message including attributes
References nla_validate(), NLE_MSG_TOOSHORT, nlmsg_attrdata(), nlmsg_attrlen(), and nlmsg_valid_hdr().