libnl  3.2.28
socket.c
1 /*
2  * lib/socket.c Netlink Socket
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup core_types
14  * @defgroup socket Socket
15  *
16  * Representation of a netlink socket
17  *
18  * Related sections in the development guide:
19  * - @core_doc{core_sockets, Netlink Sockets}
20  *
21  * @{
22  *
23  * Header
24  * ------
25  * ~~~~{.c}
26  * #include <netlink/socket.h>
27  * ~~~~
28  */
29 
30 #include "defs.h"
31 
32 #include "sys/socket.h"
33 
34 #include <netlink-private/netlink.h>
35 #include <netlink-private/socket.h>
36 #include <netlink/netlink.h>
37 #include <netlink/utils.h>
38 #include <netlink/handlers.h>
39 #include <netlink/msg.h>
40 #include <netlink/attr.h>
41 
42 static int default_cb = NL_CB_DEFAULT;
43 
44 static void __init init_default_cb(void)
45 {
46  char *nlcb;
47 
48  if ((nlcb = getenv("NLCB"))) {
49  if (!strcasecmp(nlcb, "default"))
50  default_cb = NL_CB_DEFAULT;
51  else if (!strcasecmp(nlcb, "verbose"))
52  default_cb = NL_CB_VERBOSE;
53  else if (!strcasecmp(nlcb, "debug"))
54  default_cb = NL_CB_DEBUG;
55  else {
56  fprintf(stderr, "Unknown value for NLCB, valid values: "
57  "{default | verbose | debug}\n");
58  }
59  }
60 }
61 
62 static uint32_t used_ports_map[32];
63 static NL_RW_LOCK(port_map_lock);
64 
65 static uint32_t generate_local_port(void)
66 {
67  int i, j, n, m;
68  static uint16_t idx_state = 0;
69  uint32_t pid = getpid() & 0x3FFFFF;
70 
71  nl_write_lock(&port_map_lock);
72 
73  if (idx_state == 0) {
74  uint32_t t = time(NULL);
75 
76  /* from time to time (on average each 2^15 calls), the idx_state will
77  * be zero again. No problem, just "seed" anew with time(). */
78  idx_state = t ^ (t >> 16) ^ 0x3047;
79  } else
80  idx_state = idx_state + 20011; /* add prime number */
81 
82  i = idx_state >> 5;
83  n = idx_state;
84  for (j = 0; j < 32; j++) {
85  /* walk the index somewhat randomized, with always leaving the block
86  * #0 as last. The reason is that libnl-1 will start at block #0,
87  * so just leave the first 32 ports preferably for libnl-1 owned sockets
88  * (this is relevant only if the applications ends up using both versions
89  * of the library and doesn't hurt otherwise). */
90  if (j == 31)
91  i = 0;
92  else
93  i = (((i-1) + 7) % 31) + 1;
94 
95  if (used_ports_map[i] == 0xFFFFFFFF)
96  continue;
97 
98  for (m = 0; m < 32; m++) {
99  n = (n + 13) % 32;
100  if (1UL & (used_ports_map[i] >> n))
101  continue;
102 
103  used_ports_map[i] |= (1UL << n);
104  n += (i * 32);
105 
106  /* PID_MAX_LIMIT is currently at 2^22, leaving 10 bit
107  * to, i.e. 1024 unique ports per application. */
108 
109  nl_write_unlock(&port_map_lock);
110 
111  /* ensure we don't return zero. */
112  pid = pid + (((uint32_t)n) << 22);
113  return pid ? pid : 1024;
114  }
115  }
116 
117  nl_write_unlock(&port_map_lock);
118  return 0;
119 }
120 
121 static void release_local_port(uint32_t port)
122 {
123  int nr;
124  uint32_t mask;
125 
126  BUG_ON(port == 0);
127 
128  nr = port >> 22;
129  mask = 1UL << (nr % 32);
130  nr /= 32;
131 
132  nl_write_lock(&port_map_lock);
133  BUG_ON((used_ports_map[nr] & mask) != mask);
134  used_ports_map[nr] &= ~mask;
135  nl_write_unlock(&port_map_lock);
136 }
137 
138 /** \cond skip */
139 void _nl_socket_used_ports_release_all(const uint32_t *used_ports)
140 {
141  int i;
142 
143  for (i = 0; i < 32; i++) {
144  if (used_ports[i] != 0) {
145  nl_write_lock(&port_map_lock);
146  for (; i < 32; i++) {
147  BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
148  used_ports_map[i] &= ~(used_ports[i]);
149  }
150  nl_write_unlock(&port_map_lock);
151  return;
152  }
153  }
154 }
155 
156 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
157 {
158  int nr;
159  int32_t mask;
160 
161  nr = port >> 22;
162  mask = 1UL << (nr % 32);
163  nr /= 32;
164 
165  /*
166  BUG_ON(port == 0 || (getpid() & 0x3FFFFF) != (port & 0x3FFFFF));
167  BUG_ON(used_ports[nr] & mask);
168  */
169 
170  used_ports[nr] |= mask;
171 }
172 /** \endcond */
173 
174 /**
175  * @name Allocation
176  * @{
177  */
178 
179 static struct nl_sock *__alloc_socket(struct nl_cb *cb)
180 {
181  struct nl_sock *sk;
182 
183  sk = calloc(1, sizeof(*sk));
184  if (!sk)
185  return NULL;
186 
187  sk->s_fd = -1;
188  sk->s_cb = nl_cb_get(cb);
189  sk->s_local.nl_family = AF_NETLINK;
190  sk->s_peer.nl_family = AF_NETLINK;
191  sk->s_seq_expect = sk->s_seq_next = time(NULL);
192 
193  /* the port is 0 (unspecified), meaning NL_OWN_PORT */
194  sk->s_flags = NL_OWN_PORT;
195 
196  return sk;
197 }
198 
199 /**
200  * Allocate new netlink socket
201  *
202  * @return Newly allocated netlink socket or NULL.
203  */
204 struct nl_sock *nl_socket_alloc(void)
205 {
206  struct nl_cb *cb;
207  struct nl_sock *sk;
208 
209  cb = nl_cb_alloc(default_cb);
210  if (!cb)
211  return NULL;
212 
213  /* will increment cb reference count on success */
214  sk = __alloc_socket(cb);
215 
216  nl_cb_put(cb);
217 
218  return sk;
219 }
220 
221 /**
222  * Allocate new socket with custom callbacks
223  * @arg cb Callback handler
224  *
225  * The reference to the callback handler is taken into account
226  * automatically, it is released again upon calling nl_socket_free().
227  *
228  *@return Newly allocted socket handle or NULL.
229  */
230 struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
231 {
232  if (cb == NULL)
233  BUG();
234 
235  return __alloc_socket(cb);
236 }
237 
238 /**
239  * Free a netlink socket.
240  * @arg sk Netlink socket.
241  */
242 void nl_socket_free(struct nl_sock *sk)
243 {
244  if (!sk)
245  return;
246 
247  if (sk->s_fd >= 0)
248  close(sk->s_fd);
249 
250  if (!(sk->s_flags & NL_OWN_PORT))
251  release_local_port(sk->s_local.nl_pid);
252 
253  nl_cb_put(sk->s_cb);
254  free(sk);
255 }
256 
257 /** @} */
258 
259 /**
260  * @name Sequence Numbers
261  * @{
262  */
263 
264 static int noop_seq_check(struct nl_msg *msg, void *arg)
265 {
266  return NL_OK;
267 }
268 
269 
270 /**
271  * Disable sequence number checking.
272  * @arg sk Netlink socket.
273  *
274  * Disables checking of sequence numbers on the netlink socket This is
275  * required to allow messages to be processed which were not requested by
276  * a preceding request message, e.g. netlink events.
277  *
278  * @note This function modifies the NL_CB_SEQ_CHECK configuration in
279  * the callback handle associated with the socket.
280  */
281 void nl_socket_disable_seq_check(struct nl_sock *sk)
282 {
283  nl_cb_set(sk->s_cb, NL_CB_SEQ_CHECK,
284  NL_CB_CUSTOM, noop_seq_check, NULL);
285 }
286 
287 /**
288  * Use next sequence number
289  * @arg sk Netlink socket.
290  *
291  * Uses the next available sequence number and increases the counter
292  * by one for subsequent calls.
293  *
294  * @return Unique serial sequence number
295  */
296 unsigned int nl_socket_use_seq(struct nl_sock *sk)
297 {
298  return sk->s_seq_next++;
299 }
300 
301 /**
302  * Disable automatic request for ACK
303  * @arg sk Netlink socket.
304  *
305  * The default behaviour of a socket is to request an ACK for
306  * each message sent to allow for the caller to synchronize to
307  * the completion of the netlink operation. This function
308  * disables this behaviour and will result in requests being
309  * sent which will not have the NLM_F_ACK flag set automatically.
310  * However, it is still possible for the caller to set the
311  * NLM_F_ACK flag explicitely.
312  */
313 void nl_socket_disable_auto_ack(struct nl_sock *sk)
314 {
315  sk->s_flags |= NL_NO_AUTO_ACK;
316 }
317 
318 /**
319  * Enable automatic request for ACK (default)
320  * @arg sk Netlink socket.
321  * @see nl_socket_disable_auto_ack
322  */
323 void nl_socket_enable_auto_ack(struct nl_sock *sk)
324 {
325  sk->s_flags &= ~NL_NO_AUTO_ACK;
326 }
327 
328 /** @} */
329 
330 /** \cond skip */
331 int _nl_socket_is_local_port_unspecified(struct nl_sock *sk)
332 {
333  return (sk->s_local.nl_pid == 0);
334 }
335 
336 uint32_t _nl_socket_set_local_port_no_release(struct nl_sock *sk, int generate_other)
337 {
338  uint32_t port;
339 
340  /* reset the port to generate_local_port(), but do not release
341  * the previously generated port. */
342 
343  if (generate_other)
344  port = generate_local_port();
345  else
346  port = 0;
347  sk->s_local.nl_pid = port;
348  if (port == 0) {
349  /* failed to find an unsed port. Restore the socket to have an
350  * unspecified port. */
351  sk->s_flags |= NL_OWN_PORT;
352  } else
353  sk->s_flags &= ~NL_OWN_PORT;
354  return port;
355 }
356 /** \endcond */
357 
358 /**
359  * @name Source Idenficiation
360  * @{
361  */
362 
363 uint32_t nl_socket_get_local_port(const struct nl_sock *sk)
364 {
365  if (sk->s_local.nl_pid == 0) {
366  struct nl_sock *sk_mutable = (struct nl_sock *) sk;
367 
368  /* modify the const argument sk. This is justified, because
369  * nobody ever saw the local_port from externally. So, we
370  * initilize it on first use.
371  *
372  * Note that this also means that you cannot call this function
373  * from multiple threads without synchronization. But nl_sock
374  * is not automatically threadsafe anyway, so the user is not
375  * allowed to do that.
376  */
377  sk_mutable->s_local.nl_pid = generate_local_port();
378  if (sk_mutable->s_local.nl_pid == 0) {
379  /* could not generate a local port. Assign UINT32_MAX to preserve
380  * backward compatibility. A user who cares can clear that anyway
381  * with nl_socket_set_local_port(). */
382  sk_mutable->s_local.nl_pid = UINT32_MAX;
383  sk_mutable->s_flags |= NL_OWN_PORT;
384  } else
385  sk_mutable->s_flags &= ~NL_OWN_PORT;
386  }
387  return sk->s_local.nl_pid;
388 }
389 
390 /**
391  * Set local port of socket
392  * @arg sk Netlink socket.
393  * @arg port Local port identifier
394  *
395  * Assigns a local port identifier to the socket.
396  *
397  * If port is 0, the port is reset to 'unspecified' as it is after newly
398  * calling nl_socket_alloc().
399  * Unspecified means, that the port will be generated automatically later
400  * on first use (either on nl_socket_get_local_port() or nl_connect()).
401  */
402 void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
403 {
404  if (!(sk->s_flags & NL_OWN_PORT))
405  release_local_port(sk->s_local.nl_pid);
406  sk->s_flags |= NL_OWN_PORT;
407  sk->s_local.nl_pid = port;
408 }
409 
410 /** @} */
411 
412 /**
413  * @name Group Subscriptions
414  * @{
415  */
416 
417 /**
418  * Join groups
419  * @arg sk Netlink socket
420  * @arg group Group identifier
421  *
422  * Joins the specified groups using the modern socket option which
423  * is available since kernel version 2.6.14. It allows joining an
424  * almost arbitary number of groups without limitation. The list
425  * of groups has to be terminated by 0 (%NFNLGRP_NONE).
426  *
427  * Make sure to use the correct group definitions as the older
428  * bitmask definitions for nl_join_groups() are likely to still
429  * be present for backward compatibility reasons.
430  *
431  * @return 0 on sucess or a negative error code.
432  */
433 int nl_socket_add_memberships(struct nl_sock *sk, int group, ...)
434 {
435  int err;
436  va_list ap;
437 
438  if (sk->s_fd == -1)
439  return -NLE_BAD_SOCK;
440 
441  va_start(ap, group);
442 
443  while (group != 0) {
444  if (group < 0) {
445  va_end(ap);
446  return -NLE_INVAL;
447  }
448 
449  err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
450  &group, sizeof(group));
451  if (err < 0) {
452  char buf[64];
453 
454  va_end(ap);
455  NL_DBG(4, "nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
456  sk, errno, strerror_r(errno, buf, sizeof(buf)));
457  return -nl_syserr2nlerr(errno);
458  }
459 
460  group = va_arg(ap, int);
461  }
462 
463  va_end(ap);
464 
465  return 0;
466 }
467 
468 int nl_socket_add_membership(struct nl_sock *sk, int group)
469 {
470  return nl_socket_add_memberships(sk, group, 0);
471 }
472 
473 /**
474  * Leave groups
475  * @arg sk Netlink socket
476  * @arg group Group identifier
477  *
478  * Leaves the specified groups using the modern socket option
479  * which is available since kernel version 2.6.14. The list of groups
480  * has to terminated by 0 (%NFNLGRP_NONE).
481  *
482  * @see nl_socket_add_membership
483  * @return 0 on success or a negative error code.
484  */
485 int nl_socket_drop_memberships(struct nl_sock *sk, int group, ...)
486 {
487  int err;
488  va_list ap;
489 
490  if (sk->s_fd == -1)
491  return -NLE_BAD_SOCK;
492 
493  va_start(ap, group);
494 
495  while (group != 0) {
496  if (group < 0) {
497  va_end(ap);
498  return -NLE_INVAL;
499  }
500 
501  err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
502  &group, sizeof(group));
503  if (err < 0) {
504  char buf[64];
505 
506  va_end(ap);
507  NL_DBG(4, "nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
508  sk, errno, strerror_r(errno, buf, sizeof(buf)));
509  return -nl_syserr2nlerr(errno);
510  }
511 
512  group = va_arg(ap, int);
513  }
514 
515  va_end(ap);
516 
517  return 0;
518 }
519 
520 int nl_socket_drop_membership(struct nl_sock *sk, int group)
521 {
522  return nl_socket_drop_memberships(sk, group, 0);
523 }
524 
525 
526 /**
527  * Join multicast groups (deprecated)
528  * @arg sk Netlink socket.
529  * @arg groups Bitmask of groups to join.
530  *
531  * This function defines the old way of joining multicast group which
532  * has to be done prior to calling nl_connect(). It works on any kernel
533  * version but is very limited as only 32 groups can be joined.
534  */
535 void nl_join_groups(struct nl_sock *sk, int groups)
536 {
537  sk->s_local.nl_groups |= groups;
538 }
539 
540 
541 /** @} */
542 
543 /**
544  * @name Peer Identfication
545  * @{
546  */
547 
548 uint32_t nl_socket_get_peer_port(const struct nl_sock *sk)
549 {
550  return sk->s_peer.nl_pid;
551 }
552 
553 void nl_socket_set_peer_port(struct nl_sock *sk, uint32_t port)
554 {
555  sk->s_peer.nl_pid = port;
556 }
557 
558 uint32_t nl_socket_get_peer_groups(const struct nl_sock *sk)
559 {
560  return sk->s_peer.nl_groups;
561 }
562 
563 void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups)
564 {
565  sk->s_peer.nl_groups = groups;
566 }
567 
568 
569 
570 /** @} */
571 
572 /**
573  * @name File Descriptor
574  * @{
575  */
576 
577 /**
578  * Return the file descriptor of the backing socket
579  * @arg sk Netlink socket
580  *
581  * Only valid after calling nl_connect() to create and bind the respective
582  * socket.
583  *
584  * @return File descriptor or -1 if not available.
585  */
586 int nl_socket_get_fd(const struct nl_sock *sk)
587 {
588  return sk->s_fd;
589 }
590 
591 /**
592  * Set the socket file descriptor externally which initializes the
593  * socket similar to nl_connect().
594  *
595  * @arg sk Netlink socket (required)
596  * @arg protocol The socket protocol (optional). Linux 2.6.32 supports
597  * the socket option SO_PROTOCOL. In this case, you can set
598  * protocol to a negative value and let it autodetect.
599  * If you set it to a non-negative value, the detected protocol
600  * must match the one provided.
601  * To support older kernels, you must specify the protocol.
602  * @arg fd Socket file descriptor to use (required)
603  *
604  * Set the socket file descriptor. @fd must be valid and bind'ed.
605  *
606  * This is an alternative to nl_connect(). nl_connect() creates, binds and
607  * sets the socket. With this function you can set the socket to an externally
608  * created file descriptor.
609  *
610  * @see nl_connect()
611  *
612  * @return 0 on success or a negative error code. On error, @fd is not closed but
613  * possibly unusable.
614  *
615  * @retval -NLE_BAD_SOCK Netlink socket is already connected
616  * @retval -NLE_INVAL Socket is of unexpected type
617  */
618 int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
619 {
620  int err = 0;
621  socklen_t addrlen;
622  char buf[64];
623  struct sockaddr_nl local = { 0 };
624  int so_type = -1, so_protocol = -1;
625 
626  if (sk->s_fd != -1)
627  return -NLE_BAD_SOCK;
628  if (fd < 0)
629  return -NLE_INVAL;
630 
631  addrlen = sizeof(local);
632  err = getsockname(fd, (struct sockaddr *) &local,
633  &addrlen);
634  if (err < 0) {
635  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
636  sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
637  return -nl_syserr2nlerr(errno);
638  }
639  if (addrlen != sizeof(local))
640  return -NLE_INVAL;
641  if (local.nl_family != AF_NETLINK) {
642  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
643  sk, fd, local.nl_family, AF_NETLINK);
644  return -NLE_INVAL;
645  }
646 
647  addrlen = sizeof(so_type);
648  err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
649  if (err < 0) {
650  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
651  sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
652  return -nl_syserr2nlerr(errno);
653  }
654  if (addrlen != sizeof(so_type))
655  return -NLE_INVAL;
656  if (so_type != SOCK_RAW) {
657  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
658  sk, fd, so_type, SOCK_RAW);
659  return -NLE_INVAL;
660  }
661 
662 #if SO_PROTOCOL
663  addrlen = sizeof(so_protocol);
664  err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
665  if (err < 0) {
666  if (errno == ENOPROTOOPT)
667  goto no_so_protocol;
668  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
669  sk, fd, errno, strerror_r(errno, buf, sizeof(buf)));
670  return -nl_syserr2nlerr(errno);
671  }
672  if (addrlen != sizeof(so_protocol))
673  return -NLE_INVAL;
674  if (protocol >= 0 && protocol != so_protocol) {
675  NL_DBG(4, "nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
676  sk, fd, so_protocol, protocol);
677  return -NLE_INVAL;
678  }
679 
680  if (0)
681 #endif
682  {
683 no_so_protocol:
684  if (protocol < 0) {
685  NL_DBG(4, "nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
686  sk, fd);
687  return -NLE_INVAL;
688  }
689  so_protocol = protocol;
690  }
691 
692  nl_socket_set_local_port (sk, local.nl_pid);
693  sk->s_local = local;
694  sk->s_fd = fd;
695  sk->s_proto = so_protocol;
696 
697  return 0;
698 }
699 
700 /**
701  * Set file descriptor of socket to non-blocking state
702  * @arg sk Netlink socket.
703  *
704  * @return 0 on success or a negative error code.
705  */
706 int nl_socket_set_nonblocking(const struct nl_sock *sk)
707 {
708  if (sk->s_fd == -1)
709  return -NLE_BAD_SOCK;
710 
711  if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
712  char buf[64];
713 
714  NL_DBG(4, "nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
715  sk, errno, strerror_r(errno, buf, sizeof(buf)));
716  return -nl_syserr2nlerr(errno);
717  }
718 
719  return 0;
720 }
721 
722 /**
723  * Enable use of MSG_PEEK when reading from socket
724  * @arg sk Netlink socket.
725  */
726 void nl_socket_enable_msg_peek(struct nl_sock *sk)
727 {
728  sk->s_flags |= NL_MSG_PEEK;
729 }
730 
731 /**
732  * Disable use of MSG_PEEK when reading from socket
733  * @arg sk Netlink socket.
734  */
735 void nl_socket_disable_msg_peek(struct nl_sock *sk)
736 {
737  sk->s_flags &= ~NL_MSG_PEEK;
738 }
739 
740 /** @} */
741 
742 /**
743  * @name Callback Handler
744  * @{
745  */
746 
747 struct nl_cb *nl_socket_get_cb(const struct nl_sock *sk)
748 {
749  return nl_cb_get(sk->s_cb);
750 }
751 
752 void nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb)
753 {
754  if (cb == NULL)
755  BUG();
756 
757  nl_cb_put(sk->s_cb);
758  sk->s_cb = nl_cb_get(cb);
759 }
760 
761 /**
762  * Modify the callback handler associated with the socket
763  * @arg sk Netlink socket.
764  * @arg type which type callback to set
765  * @arg kind kind of callback
766  * @arg func callback function
767  * @arg arg argument to be passed to callback function
768  *
769  * @see nl_cb_set
770  */
771 int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type,
772  enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
773  void *arg)
774 {
775  return nl_cb_set(sk->s_cb, type, kind, func, arg);
776 }
777 
778 /**
779  * Modify the error callback handler associated with the socket
780  * @arg sk Netlink socket.
781  * @arg kind kind of callback
782  * @arg func callback function
783  * @arg arg argument to be passed to callback function
784  *
785  * @see nl_cb_err
786  */
787 int nl_socket_modify_err_cb(struct nl_sock *sk, enum nl_cb_kind kind,
788  nl_recvmsg_err_cb_t func, void *arg)
789 {
790  return nl_cb_err(sk->s_cb, kind, func, arg);
791 }
792 
793 /** @} */
794 
795 /**
796  * @name Utilities
797  * @{
798  */
799 
800 /**
801  * Set socket buffer size of netlink socket.
802  * @arg sk Netlink socket.
803  * @arg rxbuf New receive socket buffer size in bytes.
804  * @arg txbuf New transmit socket buffer size in bytes.
805  *
806  * Sets the socket buffer size of a netlink socket to the specified
807  * values \c rxbuf and \c txbuf. Providing a value of \c 0 assumes a
808  * good default value.
809  *
810  * @note It is not required to call this function prior to nl_connect().
811  * @return 0 on sucess or a negative error code.
812  */
813 int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
814 {
815  int err;
816  char buf[64];
817 
818  if (rxbuf <= 0)
819  rxbuf = 32768;
820 
821  if (txbuf <= 0)
822  txbuf = 32768;
823 
824  if (sk->s_fd == -1)
825  return -NLE_BAD_SOCK;
826 
827  err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
828  &txbuf, sizeof(txbuf));
829  if (err < 0) {
830  NL_DBG(4, "nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
831  sk, errno, strerror_r(errno, buf, sizeof(buf)));
832  return -nl_syserr2nlerr(errno);
833  }
834 
835  err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
836  &rxbuf, sizeof(rxbuf));
837  if (err < 0) {
838  NL_DBG(4, "nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
839  sk, errno, strerror_r(errno, buf, sizeof(buf)));
840  return -nl_syserr2nlerr(errno);
841  }
842 
843  return 0;
844 }
845 
846 /**
847  * Set default message buffer size of netlink socket.
848  * @arg sk Netlink socket.
849  * @arg bufsize Default message buffer size in bytes.
850  *
851  * Sets the default message buffer size to the specified length in bytes.
852  * The default message buffer size limits the maximum message size the
853  * socket will be able to receive. It is generally recommneded to specify
854  * a buffer size no less than the size of a memory page.
855  *
856  * @return 0 on success or a negative error code.
857  */
858 int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
859 {
860  sk->s_bufsize = bufsize;
861 
862  return 0;
863 }
864 
865 /**
866  * Get default message buffer size of netlink socket.
867  * @arg sk Netlink socket.
868  *
869  * @return Size of default message buffer.
870  */
871 size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
872 {
873  return sk->s_bufsize;
874 }
875 
876 /**
877  * Enable/disable credential passing on netlink socket.
878  * @arg sk Netlink socket.
879  * @arg state New state (0 - disabled, 1 - enabled)
880  *
881  * @return 0 on success or a negative error code
882  */
883 int nl_socket_set_passcred(struct nl_sock *sk, int state)
884 {
885  int err;
886 
887  if (sk->s_fd == -1)
888  return -NLE_BAD_SOCK;
889 
890  err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
891  &state, sizeof(state));
892  if (err < 0) {
893  char buf[64];
894 
895  NL_DBG(4, "nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
896  sk, errno, strerror_r(errno, buf, sizeof(buf)));
897  return -nl_syserr2nlerr(errno);
898  }
899 
900  if (state)
901  sk->s_flags |= NL_SOCK_PASSCRED;
902  else
903  sk->s_flags &= ~NL_SOCK_PASSCRED;
904 
905  return 0;
906 }
907 
908 /**
909  * Enable/disable receival of additional packet information
910  * @arg sk Netlink socket.
911  * @arg state New state (0 - disabled, 1 - enabled)
912  *
913  * @return 0 on success or a negative error code
914  */
915 int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
916 {
917  int err;
918 
919  if (sk->s_fd == -1)
920  return -NLE_BAD_SOCK;
921 
922  err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
923  &state, sizeof(state));
924  if (err < 0) {
925  char buf[64];
926 
927  NL_DBG(4, "nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
928  sk, errno, strerror_r(errno, buf, sizeof(buf)));
929  return -nl_syserr2nlerr(errno);
930  }
931 
932  return 0;
933 }
934 
935 /** @} */
936 
937 /** @} */
void nl_socket_enable_auto_ack(struct nl_sock *sk)
Enable automatic request for ACK (default)
Definition: socket.c:323
int nl_socket_set_passcred(struct nl_sock *sk, int state)
Enable/disable credential passing on netlink socket.
Definition: socket.c:883
int nl_socket_drop_memberships(struct nl_sock *sk, int group,...)
Leave groups.
Definition: socket.c:485
Customized handler specified by the user.
Definition: handlers.h:80
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:586
void nl_socket_disable_auto_ack(struct nl_sock *sk)
Disable automatic request for ACK.
Definition: socket.c:313
void nl_socket_enable_msg_peek(struct nl_sock *sk)
Enable use of MSG_PEEK when reading from socket.
Definition: socket.c:726
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
Set local port of socket.
Definition: socket.c:402
int nl_socket_modify_err_cb(struct nl_sock *sk, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Modify the error callback handler associated with the socket.
Definition: socket.c:787
nl_cb_kind
Callback kinds.
Definition: handlers.h:72
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:293
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.
Definition: socket.c:204
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated with the socket.
Definition: socket.c:771
struct nl_sock * nl_socket_alloc_cb(struct nl_cb *cb)
Allocate new socket with custom callbacks.
Definition: socket.c:230
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:281
int nl_socket_set_nonblocking(const struct nl_sock *sk)
Set file descriptor of socket to non-blocking state.
Definition: socket.c:706
int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
Definition: handlers.h:50
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
Definition: socket.c:242
unsigned int nl_socket_use_seq(struct nl_sock *sk)
Use next sequence number.
Definition: socket.c:296
Debug handlers for debugging.
Definition: handlers.h:78
void nl_socket_disable_msg_peek(struct nl_sock *sk)
Disable use of MSG_PEEK when reading from socket.
Definition: socket.c:735
int(* nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
Definition: handlers.h:41
Called instead of internal sequence number checking.
Definition: handlers.h:108
int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
Set the socket file descriptor externally which initializes the socket similar to nl_connect()...
Definition: socket.c:618
Proceed with wathever would come next.
Definition: handlers.h:61
int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
Set default message buffer size of netlink socket.
Definition: socket.c:858
nl_cb_type
Callback types.
Definition: handlers.h:90
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
Definition: handlers.c:201
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
Definition: socket.c:813
void nl_join_groups(struct nl_sock *sk, int groups)
Join multicast groups (deprecated)
Definition: socket.c:535
int nl_socket_add_memberships(struct nl_sock *sk, int group,...)
Join groups.
Definition: socket.c:433
Default handlers (quiet)
Definition: handlers.h:74
int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
Enable/disable receival of additional packet information.
Definition: socket.c:915
size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
Get default message buffer size of netlink socket.
Definition: socket.c:871
int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Set up an error callback.
Definition: handlers.c:343
Verbose default handlers (error messages printed)
Definition: handlers.h:76