struct nl_handle *handle; // Allocate and initialize a new netlink handle handle = nl_handle_new(); // Are multiple handles being allocated? You have to provide a unique // netlink process id and overwrite the default local process id. nl_handle_set_pid(handle, MY_UNIQUE_PID); // Is this socket used for event processing? You need to disable sequence // number checking in order to be able to receive messages not explicitely // requested. nl_disable_sequence_check(handle); // Use nl_handle_get_fd() to fetch the file description, for example to // put a socket into non-blocking i/o mode. fcntl(nl_handle_get_fd(handle), F_SETFL, O_NONBLOCK);
// You may join/subscribe to as many groups as you want, don't forget // to eventually disable sequence number checking. Note: Joining must // be done before connecting/binding the socket. nl_join_groups(handle, GROUP_ID1 | GROUP_ID2);
// Bind and connect the socket to a protocol, NETLINK_ROUTE in this example. nl_connect(handle, NETLINK_ROUTE);
// The most rudimentary method is to use nl_sendto() simply pushing // a piece of data to the other netlink peer. This method is not // recommended. const char buf[] = { 0x01, 0x02, 0x03, 0x04 }; nl_sendto(handle, buf, sizeof(buf)); // A more comfortable interface is nl_send() taking a pointer to // a netlink message. struct nl_msg *msg = my_msg_builder(); nl_send(handle, nlmsg_hdr(msg)); // nl_sendmsg() provides additional control over the sendmsg() message // header in order to allow more specific addressing of multiple peers etc. struct msghdr hdr = { ... }; nl_sendmsg(handle, nlmsg_hdr(msg), &hdr); // You're probably too lazy to fill out the netlink pid, sequence number // and message flags all the time. nl_send_auto_complete() automatically // extends your message header as needed with an appropriate sequence // number, the netlink pid stored in the netlink handle and the message // flags NLM_F_REQUEST and NLM_F_ACK nl_send_auto_complete(handle, nlmsg_hdr(msg)); // Simple protocols don't require the complex message construction interface // and may favour nl_send_simple() to easly send a bunch of payload // encapsulated in a netlink message header. nl_send_simple(handle, MY_MSG_TYPE, 0, buf, sizeof(buf));
// nl_recv() receives a single message allocating a buffer for the message // content and gives back the pointer to you. struct sockaddr_nl peer; unsigned char *msg; nl_recv(handle, &peer, &msg); // nl_recvmsgs() receives a bunch of messages until the callback system // orders it to state, usually after receving a compolete multi part // message series. nl_recvmsgs(handle, my_callback_configuration); // nl_recvmsgs_def() acts just like nl_recvmsg() but uses the callback // configuration stored in the handle. nl_recvmsgs_def(handle); // In case you want to wait for the ACK to be recieved that you requested // with your latest message, you can call nl_wait_for_ack() nl_wait_for_ack(handle);
// Close the socket first to release kernel memory nl_close(handle); // Finally destroy the netlink handle nl_handle_destroy(handle);
Modules | |
Attributes | |
Netlink Attributes Construction/Parsing Interface. | |
Callbacks/Customization | |
Customization via callbacks. | |
Messages | |
Netlink Message Construction/Parsing Interface. | |
Data Structures | |
struct | sockaddr_nl |
Netlink socket address. More... | |
Netlink Family Translations | |
char * | nl_nlfamily2str (int family, char *buf, size_t size) |
Convert netlink family to character string. | |
int | nl_str2nlfamily (const char *name) |
Convert character string to netlink family. | |
Handle Management | |
nl_handle * | nl_handle_alloc_nondefault (enum nl_cb_kind kind) |
Allocate and initialize new non-default netlink handle. | |
nl_handle * | nl_handle_alloc (void) |
Allocate and initialize new netlink handle. | |
void | nl_handle_destroy (struct nl_handle *handle) |
Destroy netlink handle. | |
Utilities | |
int | nl_set_buffer_size (struct nl_handle *handle, int rxbuf, int txbuf) |
Set socket buffer size of netlink handle. | |
int | nl_set_passcred (struct nl_handle *handle, int state) |
Enable/disable credential passing on netlink handle. | |
void | nl_join_groups (struct nl_handle *handle, int groups) |
Join multicast groups. | |
int | nl_join_group (struct nl_handle *handle, int group) |
Set socket buffer size of netlink handle. | |
void | nl_disable_sequence_check (struct nl_handle *handle) |
Disable sequence number checking. | |
#define | SOL_NETLINK 270 |
Set socket buffer size of netlink handle. | |
Acccess Functions | |
pid_t | nl_handle_get_pid (struct nl_handle *handle) |
Get netlink process identifier of netlink handle. | |
void | nl_handle_set_pid (struct nl_handle *handle, pid_t pid) |
Set netlink process identifier of netlink handle. | |
pid_t | nl_handle_get_peer_pid (struct nl_handle *handle) |
Get netlink process identifier of peer from netlink handle. | |
void | nl_handle_set_peer_pid (struct nl_handle *handle, pid_t pid) |
Set netlink process identifier of peer in netlink handle. | |
int | nl_handle_get_fd (struct nl_handle *handle) |
Get file descriptor of netlink handle. | |
sockaddr_nl * | nl_handle_get_local_addr (struct nl_handle *handle) |
Get local netlink address of netlink handle. | |
sockaddr_nl * | nl_handle_get_peer_addr (struct nl_handle *handle) |
Get peer netlink address of netlink handle. | |
nl_cb * | nl_handle_get_cb (struct nl_handle *handle) |
Get callback configuration of netlink handle. | |
Connection Management | |
int | nl_connect (struct nl_handle *handle, int protocol) |
Create and connect netlink socket. | |
void | nl_close (struct nl_handle *handle) |
Close/Disconnect netlink socket. | |
Send | |
int | nl_sendto (struct nl_handle *handle, void *buf, size_t size) |
Send raw data over netlink socket. | |
int | nl_sendmsg (struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr) |
Send netlink message with control over sendmsg() message header. | |
int | nl_send (struct nl_handle *handle, struct nl_msg *msg) |
Send netlink message. | |
int | nl_send_auto_complete (struct nl_handle *handle, struct nl_msg *msg) |
Send netlink message and check & extend header values as needed. | |
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(). | |
Receive | |
int | nl_recv (struct nl_handle *handle, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds) |
Receive netlink message from netlink socket. | |
int | nl_recvmsgs (struct nl_handle *handle, struct nl_cb *cb) |
Receive a set of messages from a netlink socket. | |
int | nl_recvmsgs_def (struct nl_handle *handle) |
Receive a set of message from a netlink socket using handlers in nl_handle. | |
int | nl_wait_for_ack (struct nl_handle *handle) |
Wait for ACK. |
#define SOL_NETLINK 270 |
Set socket buffer size of netlink handle.
handle | Netlink handle. | |
rxbuf | New receive socket buffer size in bytes. | |
txbuf | New transmit socket buffer size in bytes. |
rxbuf
and txbuf
. Providing a value of 0
assumes a good default value.
Definition at line 275 of file nl.c.
Referenced by nl_join_group().
struct nl_handle* nl_handle_alloc_nondefault | ( | enum nl_cb_kind | kind | ) |
Allocate and initialize new non-default netlink handle.
kind | Kind of callback handler to use per default. |
Definition at line 140 of file nl.c.
References nl_cb_new(), and nl_handle_destroy().
Referenced by nl_handle_alloc().
struct nl_handle* nl_handle_alloc | ( | void | ) |
Allocate and initialize new netlink handle.
Allocates and initializes a new netlink handle, the netlink process id is set to the local process id which may conflict if multiple handles are created, therefore you may have to overwrite it using nl_handle_set_pid(). The initial sequence number is initialized to the current UNIX time. The default callback (NL_CB_DEFAULT) handlers are being used.
Definition at line 176 of file nl.c.
References NL_CB_DEFAULT, and nl_handle_alloc_nondefault().
void nl_handle_destroy | ( | struct nl_handle * | handle | ) |
Destroy netlink handle.
handle | Netlink handle. |
Definition at line 185 of file nl.c.
References nl_cb_destroy().
Referenced by nl_handle_alloc_nondefault().
int nl_set_buffer_size | ( | struct nl_handle * | handle, | |
int | rxbuf, | |||
int | txbuf | |||
) |
Set socket buffer size of netlink handle.
handle | Netlink handle. | |
rxbuf | New receive socket buffer size in bytes. | |
txbuf | New transmit socket buffer size in bytes. |
rxbuf
and txbuf
. Providing a value of 0
assumes a good default value.
Definition at line 214 of file nl.c.
Referenced by nl_connect().
int nl_set_passcred | ( | struct nl_handle * | handle, | |
int | state | |||
) |
void nl_join_groups | ( | struct nl_handle * | handle, | |
int | groups | |||
) |
Join multicast groups.
handle | Netlink handle. | |
groups | Bitmask of groups to join. |
int nl_join_group | ( | struct nl_handle * | handle, | |
int | group | |||
) |
Set socket buffer size of netlink handle.
handle | Netlink handle. | |
rxbuf | New receive socket buffer size in bytes. | |
txbuf | New transmit socket buffer size in bytes. |
rxbuf
and txbuf
. Providing a value of 0
assumes a good default value.
Definition at line 278 of file nl.c.
References SOL_NETLINK.
void nl_disable_sequence_check | ( | struct nl_handle * | handle | ) |
Disable sequence number checking.
handle | Netlink handle. |
Definition at line 304 of file nl.c.
References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, nl_cb_set(), and nl_handle_get_cb().
pid_t nl_handle_get_pid | ( | struct nl_handle * | handle | ) |
void nl_handle_set_pid | ( | struct nl_handle * | handle, | |
pid_t | pid | |||
) |
pid_t nl_handle_get_peer_pid | ( | struct nl_handle * | handle | ) |
void nl_handle_set_peer_pid | ( | struct nl_handle * | handle, | |
pid_t | pid | |||
) |
int nl_handle_get_fd | ( | struct nl_handle * | handle | ) |
struct sockaddr_nl* nl_handle_get_local_addr | ( | struct nl_handle * | handle | ) |
struct sockaddr_nl* nl_handle_get_peer_addr | ( | struct nl_handle * | handle | ) |
struct nl_cb* nl_handle_get_cb | ( | struct nl_handle * | handle | ) |
Get callback configuration of netlink handle.
handle | Netlink handle. |
Definition at line 393 of file nl.c.
Referenced by nl_cache_pickup(), nl_disable_sequence_check(), nl_sendmsg(), and nl_wait_for_ack().
int nl_connect | ( | struct nl_handle * | handle, | |
int | protocol | |||
) |
Create and connect netlink socket.
handle | Netlink handle. | |
protocol | Netlink protocol to use. |
Definition at line 415 of file nl.c.
References nl_set_buffer_size().
void nl_close | ( | struct nl_handle * | handle | ) |
int nl_sendto | ( | struct nl_handle * | handle, | |
void * | buf, | |||
size_t | size | |||
) |
int nl_sendmsg | ( | struct nl_handle * | handle, | |
struct nl_msg * | msg, | |||
struct msghdr * | hdr | |||
) |
Send netlink message with control over sendmsg() message header.
handle | Netlink handle. | |
msg | Netlink message to be sent. | |
hdr | Sendmsg() message header. |
Definition at line 499 of file nl.c.
References NL_CB_MSG_OUT, nl_handle_get_cb(), NL_PROCEED, nlmsg_hdr(), nlmsghdr::nlmsg_len, and nlmsg_set_src().
Referenced by nl_send().
int nl_send | ( | struct nl_handle * | handle, | |
struct nl_msg * | msg | |||
) |
Send netlink message.
handle | Netlink handle | |
msg | Netlink message to be sent. |
Definition at line 534 of file nl.c.
References sockaddr_nl::nl_family, nl_sendmsg(), nlmsg_get_creds(), and nlmsg_get_dst().
Referenced by nl_send_auto_complete().
int nl_send_auto_complete | ( | struct nl_handle * | handle, | |
struct nl_msg * | msg | |||
) |
Send netlink message and check & extend header values as needed.
handle | Netlink handle. | |
msg | Netlink message to be sent. |
nlh
for completness and extends it as required before sending it out. Checked fields include pid, sequence nr, and flags.
Definition at line 582 of file nl.c.
References nl_send(), NLM_F_ACK, NLM_F_REQUEST, nlmsghdr::nlmsg_flags, nlmsg_hdr(), nlmsghdr::nlmsg_pid, and nlmsghdr::nlmsg_seq.
Referenced by flnl_lookup(), nl_send_simple(), rtnl_addr_add(), rtnl_addr_delete(), rtnl_class_add(), rtnl_cls_add(), rtnl_cls_change(), rtnl_cls_delete(), rtnl_link_change(), rtnl_neigh_add(), rtnl_neigh_change(), rtnl_neigh_delete(), rtnl_neightbl_change(), rtnl_qdisc_add(), rtnl_qdisc_change(), rtnl_qdisc_delete(), rtnl_rule_add(), and rtnl_rule_delete().
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().
handle | Netlink handle. | |
type | Netlink message type. | |
flags | Netlink message flags. | |
buf | Data buffer. | |
size | Size of data buffer. |
Definition at line 615 of file nl.c.
References nl_send_auto_complete(), nlmsg_append(), nlmsg_build(), nlmsg_free(), nlmsghdr::nlmsg_len, and nlmsg_msg_size().
Referenced by nl_rtgen_request().
int nl_recv | ( | struct nl_handle * | handle, | |
struct sockaddr_nl * | nla, | |||
unsigned char ** | buf, | |||
struct ucred ** | creds | |||
) |
Receive netlink message from netlink socket.
handle | Netlink handle. | |
nla | Destination pointer for peer's netlink address. | |
buf | Destination pointer for message content. | |
creds | Destination pointer for credentials. |
*buf
and stores the message content. The peer's netlink address is stored in *nla
. The caller is responsible for freeing the buffer allocated in *buf
if a positive value is returned. Interruped system calls are handled by repeating the read. The input buffer size is determined by peeking before the actual read is done.A non-blocking sockets causes the function to return immediately if no data is available.
Definition at line 665 of file nl.c.
Referenced by nl_recvmsgs().
int nl_recvmsgs | ( | struct nl_handle * | handle, | |
struct nl_cb * | cb | |||
) |
Receive a set of messages from a netlink socket.
handle | netlink handle | |
cb | set of callbacks to control the behaviour. |
A non-blocking sockets causes the function to return immediately if no data is available.
Definition at line 765 of file nl.c.
References NL_CB_ACK, NL_CB_FINISH, NL_CB_INVALID, NL_CB_MSG_IN, NL_CB_OVERRUN, NL_CB_SEND_ACK, NL_CB_SEQ_CHECK, NL_CB_SKIPPED, NL_CB_VALID, NL_EXIT, nl_recv(), NL_SKIP, NLM_F_ACK, nlmsg_convert(), nlmsg_data(), NLMSG_DONE, NLMSG_ERROR, nlmsg_free(), nlmsg_msg_size(), nlmsg_next(), NLMSG_NOOP, nlmsg_ok(), NLMSG_OVERRUN, nlmsg_set_creds(), nlmsg_set_proto(), and nlmsg_set_src().
Referenced by nl_cache_pickup(), nl_recvmsgs_def(), and nl_wait_for_ack().
int nl_recvmsgs_def | ( | struct nl_handle * | handle | ) |
Receive a set of message from a netlink socket using handlers in nl_handle.
handle | netlink handle |
Definition at line 986 of file nl.c.
References nl_recvmsgs().
int nl_wait_for_ack | ( | struct nl_handle * | handle | ) |
Wait for ACK.
handle | netlink handle |
Definition at line 1007 of file nl.c.
References NL_CB_ACK, nl_cb_clone(), NL_CB_CUSTOM, nl_cb_destroy(), nl_cb_set(), nl_handle_get_cb(), and nl_recvmsgs().
Referenced by rtnl_addr_add(), rtnl_addr_delete(), rtnl_class_add(), rtnl_cls_add(), rtnl_cls_change(), rtnl_cls_delete(), rtnl_link_change(), rtnl_neigh_add(), rtnl_neigh_change(), rtnl_neigh_delete(), rtnl_neightbl_change(), rtnl_qdisc_add(), rtnl_qdisc_change(), rtnl_qdisc_delete(), rtnl_rule_add(), and rtnl_rule_delete().
char* nl_nlfamily2str | ( | int | family, | |
char * | buf, | |||
size_t | size | |||
) |
Convert netlink family to character string.
family | Netlink family. | |
buf | Destination buffer. | |
size | Size of destination buffer. |
int nl_str2nlfamily | ( | const char * | name | ) |