libnl 1.1.4

Handle representing a netlink socket. More...

Allocation

struct nl_handle * nl_handle_alloc (void)
 Allocate new netlink socket handle.
 
struct nl_handle * nl_handle_alloc_cb (struct nl_cb *cb)
 Allocate new socket handle with custom callbacks.
 
void nl_handle_destroy (struct nl_handle *handle)
 Destroy netlink handle.
 

Sequence Numbers

void nl_disable_sequence_check (struct nl_handle *handle)
 Disable sequence number checking.
 
unsigned int nl_socket_use_seq (struct nl_handle *handle)
 Use next sequence number.
 

Source Idenficiation

uint32_t nl_socket_get_local_port (struct nl_handle *handle)
 
void nl_socket_set_local_port (struct nl_handle *handle, uint32_t port)
 Set local port of socket.
 

Group Subscriptions

int nl_socket_add_membership (struct nl_handle *handle, int group)
 Join a group.
 
int nl_socket_drop_membership (struct nl_handle *handle, int group)
 Leave a group.
 
void nl_join_groups (struct nl_handle *handle, int groups)
 Join multicast groups (deprecated)
 

Peer Identfication

uint32_t nl_socket_get_peer_port (struct nl_handle *handle)
 
void nl_socket_set_peer_port (struct nl_handle *handle, uint32_t port)
 

File Descriptor

int nl_socket_get_fd (struct nl_handle *handle)
 
int nl_socket_set_nonblocking (struct nl_handle *handle)
 Set file descriptor of socket handle to non-blocking state.
 
void nl_socket_enable_msg_peek (struct nl_handle *handle)
 Enable use of MSG_PEEK when reading from socket.
 
void nl_socket_disable_msg_peek (struct nl_handle *handle)
 Disable use of MSG_PEEK when reading from socket.
 

Callback Handler

struct nl_cb * nl_socket_get_cb (struct nl_handle *handle)
 
void nl_socket_set_cb (struct nl_handle *handle, struct nl_cb *cb)
 
int nl_socket_modify_cb (struct nl_handle *handle, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
 Modify the callback handler associated to the socket.
 

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.
 
int nl_socket_recv_pktinfo (struct nl_handle *handle, int state)
 Enable/disable receival of additional packet information.
 

Detailed Description

The socket is represented in a structure called the netlink handle, besides the socket, it stores various settings and values related to the socket. Every socket handle has a mandatory association with a set of callbacks which can be used to modify the behaviour when sending/receiving data from the socket.

Socket Attributes
  • Local Port: The local port is a netlink port identifying the local endpoint. It is used as source address for outgoing messages and will be addressed in replies. It must therefore be unique among all userspace applications. When the socket handle is allocated, a unique port number is generated automatically in the form of 22 bits Process Identifier + 10 bits Arbitary Number. Therefore the library is capable of generating 1024 unique local port numbers for every process. If more sockets are required, the application has to manage port numbers itself using nl_socket_set_local_port().
  • Group Subscriptions: A socket can subscribe to any number of multicast groups. It will then receive a copy of all messages sent to one of the groups. This method is mainly used for event notification. Prior to kernel 2.6.14, the group subscription was done via bitmask which limited to a total number of groups of 32. With 2.6.14 a new method was added based on continous identifiers which supports an arbitary number of groups. Both methods are supported, see nl_join_groups() respectively nl_socket_add_membership() and nl_socket_drop_membership().
  • Peer Port: The peer port is a netlink port identifying the peer's endpoint. If no peer port is specified, the kernel will try to autobind to a socket of the specified netlink family automatically. This is very common as typically only one listening socket exists on the kernel side. The peer port can be modified using nl_socket_set_peer_port().
  • Peer Groups:
  • File Descriptor: The file descriptor of the socket, it can be accessed via nl_socket_get_fd() to change socket options or monitor activity using poll()/select().
  • Protocol: Once connected, the socket is bound to stick to one netlink family. This field is invisible, it is maintained automatically. (See nl_connect())
  • Next Sequence Number: Next available sequence number to be used for the next message being sent out. (Initial value: UNIX time when the socket was allocated.) Sequence numbers can be used via nl_socket_use_seq().
  • Expected Sequence Number: Expected sequence number in the next message received from the socket. (Initial value: Equal to next sequence number.)
  • Callbacks Configuration:
1) Creating the netlink handle
struct nl_handle *handle;
// Allocate and initialize a new netlink handle
handle = nl_handle_alloc();
// Use nl_socket_get_fd() to fetch the file description, for example to
// put a socket into non-blocking i/o mode.
fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
struct nl_handle * nl_handle_alloc(void)
Allocate new netlink socket handle.
Definition: socket.c:206
2) Group Subscriptions
// Event notifications are typically sent to multicast addresses which
// represented by groups. Join a group to f.e. receive link notifications.
nl_socket_add_membership(handle, RTNLGRP_LINK);
int nl_socket_add_membership(struct nl_handle *handle, int group)
Join a group.
Definition: socket.c:361
6) Cleaning up
// Finally destroy the netlink handle
void nl_handle_destroy(struct nl_handle *handle)
Destroy netlink handle.
Definition: socket.c:246

Function Documentation

◆ nl_handle_alloc()

struct nl_handle * nl_handle_alloc ( void  )
Returns
Newly allocated netlink socket handle or NULL.

Definition at line 206 of file socket.c.

207{
208 struct nl_cb *cb;
209 struct nl_handle *sk;
210
211 cb = nl_cb_alloc(default_cb);
212 if (!cb) {
213 nl_errno(ENOMEM);
214 return NULL;
215 }
216
217 /* will increment cb reference count on success */
218 sk = __alloc_handle(cb);
219
220 nl_cb_put(cb);
221
222 return sk;
223}
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
Definition: handlers.c:256

References nl_cb_alloc().

◆ nl_handle_alloc_cb()

struct nl_handle * nl_handle_alloc_cb ( struct nl_cb *  cb)
Parameters
cbCallback handler

The reference to the callback handler is taken into account automatically, it is released again upon calling nl_handle_destroy().

Returns
Newly allocted socket handle or NULL.

Definition at line 234 of file socket.c.

235{
236 if (cb == NULL)
237 BUG();
238
239 return __alloc_handle(cb);
240}

◆ nl_handle_destroy()

void nl_handle_destroy ( struct nl_handle *  handle)
Parameters
handleNetlink handle.

Definition at line 246 of file socket.c.

247{
248 if (!handle)
249 return;
250
251 if (handle->h_fd >= 0)
252 close(handle->h_fd);
253
254 if (!(handle->h_flags & NL_OWN_PORT))
255 release_local_port(handle->h_local.nl_pid);
256
257 nl_cb_put(handle->h_cb);
258 free(handle);
259}

Referenced by nl_cache_mngr_free().

◆ nl_disable_sequence_check()

void nl_disable_sequence_check ( struct nl_handle *  handle)
Parameters
handleNetlink handle.

Disables checking of sequence numbers on the netlink handle. This is required to allow messages to be processed which were not requested by a preceding request message, e.g. netlink events.

Note
This function modifies the NL_CB_SEQ_CHECK configuration in the callback handle associated with the socket.

Definition at line 285 of file socket.c.

286{
287 nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK,
288 NL_CB_CUSTOM, noop_seq_check, NULL);
289}
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition: handlers.c:338
@ NL_CB_SEQ_CHECK
Called instead of internal sequence number checking.
Definition: handlers.h:112
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition: handlers.h:84

References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, and nl_cb_set().

Referenced by nl_cache_mngr_alloc().

◆ nl_socket_use_seq()

unsigned int nl_socket_use_seq ( struct nl_handle *  handle)
Parameters
handleNetlink handle

Uses the next available sequence number and increases the counter by one for subsequent calls.

Returns
Unique serial sequence number

Definition at line 300 of file socket.c.

301{
302 return handle->h_seq_next++;
303}

◆ nl_socket_get_local_port()

uint32_t nl_socket_get_local_port ( struct nl_handle *  handle)

Definition at line 312 of file socket.c.

313{
314 return handle->h_local.nl_pid;
315}

◆ nl_socket_set_local_port()

void nl_socket_set_local_port ( struct nl_handle *  handle,
uint32_t  port 
)
Parameters
handleNetlink handle
portLocal port identifier

Assigns a local port identifier to the socket. If port is 0 a unique port identifier will be generated automatically.

Definition at line 325 of file socket.c.

326{
327 if (port == 0) {
328 port = generate_local_port();
329 handle->h_flags &= ~NL_OWN_PORT;
330 } else {
331 if (!(handle->h_flags & NL_OWN_PORT))
332 release_local_port(handle->h_local.nl_pid);
333 handle->h_flags |= NL_OWN_PORT;
334 }
335
336 handle->h_local.nl_pid = port;
337}

◆ nl_socket_add_membership()

int nl_socket_add_membership ( struct nl_handle *  handle,
int  group 
)
Parameters
handleNetlink handle
groupGroup identifier

Joins the specified group using the modern socket option which is available since kernel version 2.6.14. It allows joining an almost arbitary number of groups without limitation.

Make sure to use the correct group definitions as the older bitmask definitions for nl_join_groups() are likely to still be present for backward compatibility reasons.

Returns
0 on sucess or a negative error code.

Definition at line 361 of file socket.c.

362{
363 int err;
364
365 if (handle->h_fd == -1)
366 return nl_error(EBADFD, "Socket not connected");
367
368 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
369 &group, sizeof(group));
370 if (err < 0)
371 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) "
372 "failed");
373
374 return 0;
375}

Referenced by nl_cache_mngr_add().

◆ nl_socket_drop_membership()

int nl_socket_drop_membership ( struct nl_handle *  handle,
int  group 
)
Parameters
handleNetlink handle
groupGroup identifier

Leaves the specified group using the modern socket option which is available since kernel version 2.6.14.

See also
nl_socket_add_membership
Returns
0 on success or a negative error code.

Definition at line 388 of file socket.c.

389{
390 int err;
391
392 if (handle->h_fd == -1)
393 return nl_error(EBADFD, "Socket not connected");
394
395 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
396 &group, sizeof(group));
397 if (err < 0)
398 return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) "
399 "failed");
400
401 return 0;
402}

Referenced by nl_cache_mngr_add().

◆ nl_join_groups()

void nl_join_groups ( struct nl_handle *  handle,
int  groups 
)
Parameters
handleNetlink handle.
groupsBitmask of groups to join.

This function defines the old way of joining multicast group which has to be done prior to calling nl_connect(). It works on any kernel version but is very limited as only 32 groups can be joined.

Definition at line 413 of file socket.c.

414{
415 handle->h_local.nl_groups |= groups;
416}

◆ nl_socket_get_peer_port()

uint32_t nl_socket_get_peer_port ( struct nl_handle *  handle)

Definition at line 426 of file socket.c.

427{
428 return handle->h_peer.nl_pid;
429}

◆ nl_socket_set_peer_port()

void nl_socket_set_peer_port ( struct nl_handle *  handle,
uint32_t  port 
)

Definition at line 431 of file socket.c.

432{
433 handle->h_peer.nl_pid = port;
434}

◆ nl_socket_get_fd()

int nl_socket_get_fd ( struct nl_handle *  handle)

Definition at line 443 of file socket.c.

444{
445 return handle->h_fd;
446}

◆ nl_socket_set_nonblocking()

int nl_socket_set_nonblocking ( struct nl_handle *  handle)
Parameters
handleNetlink socket
Returns
0 on success or a negative error code.

Definition at line 454 of file socket.c.

455{
456 if (handle->h_fd == -1)
457 return nl_error(EBADFD, "Socket not connected");
458
459 if (fcntl(handle->h_fd, F_SETFL, O_NONBLOCK) < 0)
460 return nl_error(errno, "fcntl(F_SETFL, O_NONBLOCK) failed");
461
462 return 0;
463}

Referenced by nl_cache_mngr_alloc().

◆ nl_socket_enable_msg_peek()

void nl_socket_enable_msg_peek ( struct nl_handle *  handle)
Parameters
handleNetlink socket

Definition at line 469 of file socket.c.

470{
471 handle->h_flags |= NL_MSG_PEEK;
472}

◆ nl_socket_disable_msg_peek()

void nl_socket_disable_msg_peek ( struct nl_handle *  handle)
Parameters
handleNetlink socket

Definition at line 478 of file socket.c.

479{
480 handle->h_flags &= ~NL_MSG_PEEK;
481}

◆ nl_socket_get_cb()

struct nl_cb * nl_socket_get_cb ( struct nl_handle *  handle)

Definition at line 490 of file socket.c.

491{
492 return nl_cb_get(handle->h_cb);
493}

◆ nl_socket_set_cb()

void nl_socket_set_cb ( struct nl_handle *  handle,
struct nl_cb *  cb 
)

Definition at line 495 of file socket.c.

496{
497 if (cb == NULL)
498 BUG();
499
500 nl_cb_put(handle->h_cb);
501 handle->h_cb = nl_cb_get(cb);
502}

◆ nl_socket_modify_cb()

int nl_socket_modify_cb ( struct nl_handle *  handle,
enum nl_cb_type  type,
enum nl_cb_kind  kind,
nl_recvmsg_msg_cb_t  func,
void *  arg 
)
Parameters
handlenetlink handle
typewhich type callback to set
kindkind of callback
funccallback function
argargument to be passwd to callback function
See also
nl_cb_set

Definition at line 514 of file socket.c.

517{
518 return nl_cb_set(handle->h_cb, type, kind, func, arg);
519}

References nl_cb_set().

Referenced by nl_cache_mngr_alloc().

◆ nl_set_buffer_size()

int nl_set_buffer_size ( struct nl_handle *  handle,
int  rxbuf,
int  txbuf 
)
Parameters
handleNetlink handle.
rxbufNew receive socket buffer size in bytes.
txbufNew transmit socket buffer size in bytes.

Sets the socket buffer size of a netlink handle to the specified values rxbuf and txbuf. Providing a value of 0 assumes a good default value.

Note
It is not required to call this function prior to nl_connect().
Returns
0 on sucess or a negative error code.

Definition at line 541 of file socket.c.

542{
543 int err;
544
545 if (rxbuf <= 0)
546 rxbuf = 32768;
547
548 if (txbuf <= 0)
549 txbuf = 32768;
550
551 if (handle->h_fd == -1)
552 return nl_error(EBADFD, "Socket not connected");
553
554 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF,
555 &txbuf, sizeof(txbuf));
556 if (err < 0)
557 return nl_error(errno, "setsockopt(SO_SNDBUF) failed");
558
559 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF,
560 &rxbuf, sizeof(rxbuf));
561 if (err < 0)
562 return nl_error(errno, "setsockopt(SO_RCVBUF) failed");
563
564 handle->h_flags |= NL_SOCK_BUFSIZE_SET;
565
566 return 0;
567}

Referenced by nl_connect().

◆ nl_set_passcred()

int nl_set_passcred ( struct nl_handle *  handle,
int  state 
)
Parameters
handleNetlink handle
stateNew state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

Definition at line 576 of file socket.c.

577{
578 int err;
579
580 if (handle->h_fd == -1)
581 return nl_error(EBADFD, "Socket not connected");
582
583 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED,
584 &state, sizeof(state));
585 if (err < 0)
586 return nl_error(errno, "setsockopt(SO_PASSCRED) failed");
587
588 if (state)
589 handle->h_flags |= NL_SOCK_PASSCRED;
590 else
591 handle->h_flags &= ~NL_SOCK_PASSCRED;
592
593 return 0;
594}

◆ nl_socket_recv_pktinfo()

int nl_socket_recv_pktinfo ( struct nl_handle *  handle,
int  state 
)
Parameters
handleNetlink handle
stateNew state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

Definition at line 603 of file socket.c.

604{
605 int err;
606
607 if (handle->h_fd == -1)
608 return nl_error(EBADFD, "Socket not connected");
609
610 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO,
611 &state, sizeof(state));
612 if (err < 0)
613 return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed");
614
615 return 0;
616}