libnl 1.1.4
Modules
Netfilter Netlink

Modules

 Conntrack
 
 Log
 

Socket Creating

int nfnl_connect (struct nl_handle *handle)
 Create and connect netfilter netlink socket.
 

Sending

int nfnl_send_simple (struct nl_handle *handle, uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
 Send trivial netfilter netlink message.
 

Message Parsing

uint8_t nfnlmsg_subsys (struct nlmsghdr *nlh)
 Get netfilter subsystem id from message.
 
uint8_t nfnlmsg_subtype (struct nlmsghdr *nlh)
 Get netfilter message type from message.
 
uint8_t nfnlmsg_family (struct nlmsghdr *nlh)
 Get netfilter family from message.
 
uint16_t nfnlmsg_res_id (struct nlmsghdr *nlh)
 Get netfilter resource id from message.
 

Message Building

struct nl_msg * nfnlmsg_alloc_simple (uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
 Allocate a new netfilter netlink message.
 
int nfnlmsg_put (struct nl_msg *msg, uint32_t pid, uint32_t seq, uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
 Add netlink and netfilter netlink headers to netlink message.
 

Detailed Description

Message Format
<------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
+----------------------------+- - -+- - - - - - - - - - -+- - -+
| Header | Pad | Payload | Pad |
| struct nlmsghdr | | | |
+----------------------------+- - -+- - - - - - - - - - -+- - -+
Netlink message header.
<-------- NFNL_HDRLEN --------->
+--------------------------+- - -+------------+
| Netfilter Netlink Header | Pad | Attributes |
| struct nfgenmsg | | |
+--------------------------+- - -+------------+
nfnlmsg_attrdata(nfg, hdrlen)-----^
1) Creating a new netfilter netlink message
struct nl_msg *msg;
// Create a new empty netlink message
msg = nlmsg_alloc();
// Append the netlink and netfilter netlink message header
hdr = nfnlmsg_put(msg, PID, SEQ, SUBSYS, TYPE, NLM_F_ECHO,
FAMILY, RES_ID);
// Append the attributes.
nla_put_u32(msg, 1, 0x10);
// Message is ready to be sent.
nl_send_auto_complete(nl_handle, msg);
// All done? Free the message.
int nla_put_u32(struct nl_msg *n, int attrtype, uint32_t value)
Add a u32 netlink attribute to a netlink message.
Definition: attr.c:569
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:401
#define NLM_F_ECHO
Echo this request.
int nfnlmsg_put(struct nl_msg *msg, uint32_t pid, uint32_t seq, uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
Add netlink and netfilter netlink headers to netlink message.
Definition: nfnl.c:231
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
Definition: nl.c:373
2) Sending of trivial messages
// For trivial messages not requiring any subsys specific header or
// attributes, nfnl_send_simple() may be used to send messages directly.
nfnl_send_simple(nl_handle, SUBSYS, TYPE, 0, FAMILY, RES_ID);
int nfnl_send_simple(struct nl_handle *handle, uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
Send trivial netfilter netlink message.
Definition: nfnl.c:108

Function Documentation

◆ nfnl_connect()

int nfnl_connect ( struct nl_handle *  handle)
Parameters
handleNetlink handle.

Creates a NETLINK_NETFILTER netlink socket, binds the socket and issues a connection attempt.

See also
nl_connect()
Returns
0 on success or a negative error code.

Definition at line 85 of file nfnl.c.

86{
87 return nl_connect(handle, NETLINK_NETFILTER);
88}
int nl_connect(struct nl_handle *handle, int protocol)
Create and connect netlink socket.
Definition: nl.c:191

References nl_connect().

◆ nfnl_send_simple()

int nfnl_send_simple ( struct nl_handle *  handle,
uint8_t  subsys_id,
uint8_t  type,
int  flags,
uint8_t  family,
uint16_t  res_id 
)
Parameters
handleNetlink handle.
subsys_idnfnetlink subsystem
typenfnetlink message type
flagsmessage flags
familynfnetlink address family
res_idnfnetlink resource id
Returns
Newly allocated netlink message or NULL.

Definition at line 108 of file nfnl.c.

110{
111 struct nfgenmsg hdr = {
112 .nfgen_family = family,
113 .version = NFNETLINK_V0,
114 .res_id = htons(res_id),
115 };
116
117 return nl_send_simple(handle, NFNLMSG_TYPE(subsys_id, type), flags,
118 &hdr, sizeof(hdr));
119}
int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf, size_t size)
Send simple netlink message using nl_send_auto_complete()
Definition: nl.c:410

References nl_send_simple().

◆ nfnlmsg_subsys()

uint8_t nfnlmsg_subsys ( struct nlmsghdr nlh)
Parameters
nlhnetlink messsage header

Definition at line 132 of file nfnl.c.

133{
134 return NFNL_SUBSYS_ID(nlh->nlmsg_type);
135}
uint16_t nlmsg_type
Message type (content type)

References nlmsghdr::nlmsg_type.

◆ nfnlmsg_subtype()

uint8_t nfnlmsg_subtype ( struct nlmsghdr nlh)
Parameters
nlhnetlink messsage header

Definition at line 141 of file nfnl.c.

142{
143 return NFNL_MSG_TYPE(nlh->nlmsg_type);
144}

References nlmsghdr::nlmsg_type.

◆ nfnlmsg_family()

uint8_t nfnlmsg_family ( struct nlmsghdr nlh)
Parameters
nlhnetlink messsage header

Definition at line 150 of file nfnl.c.

151{
152 struct nfgenmsg *nfg = nlmsg_data(nlh);
153
154 return nfg->nfgen_family;
155}
void * nlmsg_data(const struct nlmsghdr *nlh)
head of message payload
Definition: msg.c:218

References nlmsg_data().

◆ nfnlmsg_res_id()

uint16_t nfnlmsg_res_id ( struct nlmsghdr nlh)
Parameters
nlhnetlink messsage header

Definition at line 161 of file nfnl.c.

162{
163 struct nfgenmsg *nfg = nlmsg_data(nlh);
164
165 return ntohs(nfg->res_id);
166}

References nlmsg_data().

◆ nfnlmsg_alloc_simple()

struct nl_msg * nfnlmsg_alloc_simple ( uint8_t  subsys_id,
uint8_t  type,
int  flags,
uint8_t  family,
uint16_t  res_id 
)
Parameters
subsys_idnfnetlink subsystem
typenfnetlink message type
flagsmessage flags
familynfnetlink address family
res_idnfnetlink resource id
Returns
Newly allocated netlink message or NULL.

Definition at line 201 of file nfnl.c.

203{
204 struct nl_msg *msg;
205
206 msg = nlmsg_alloc_simple(NFNLMSG_TYPE(subsys_id, type), flags);
207 if (msg == NULL)
208 return NULL;
209
210 if (nfnlmsg_append(msg, family, res_id) < 0)
211 goto nla_put_failure;
212
213 return msg;
214
215nla_put_failure:
216 nlmsg_free(msg);
217 return NULL;
218}
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:448

References nlmsg_alloc_simple(), and nlmsg_free().

◆ nfnlmsg_put()

int nfnlmsg_put ( struct nl_msg *  msg,
uint32_t  pid,
uint32_t  seq,
uint8_t  subsys_id,
uint8_t  type,
int  flags,
uint8_t  family,
uint16_t  res_id 
)
Parameters
msgnetlink message
pidnetlink process id
seqsequence number of message
subsys_idnfnetlink subsystem
typenfnetlink message type
flagsmessage flags
familynfnetlink address family
res_idnfnetlink resource id

Definition at line 231 of file nfnl.c.

234{
235 struct nlmsghdr *nlh;
236
237 nlh = nlmsg_put(msg, pid, seq, NFNLMSG_TYPE(subsys_id, type), 0, flags);
238 if (nlh == NULL)
239 return nl_get_errno();
240
241 return nfnlmsg_append(msg, family, res_id);
242}
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.
Definition: msg.c:610

References nlmsg_put().