socket.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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-2008 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup core
  13. * @defgroup socket Socket
  14. * @{
  15. */
  16. #include <netlink-local.h>
  17. #include <netlink/netlink.h>
  18. #include <netlink/utils.h>
  19. #include <netlink/handlers.h>
  20. #include <netlink/msg.h>
  21. #include <netlink/attr.h>
  22. static uint32_t used_ports_map[32];
  23. static uint32_t generate_local_port(void)
  24. {
  25. int i, n;
  26. uint32_t pid = getpid() & 0x3FFFFF;
  27. for (i = 0; i < 32; i++) {
  28. if (used_ports_map[i] == 0xFFFFFFFF)
  29. continue;
  30. for (n = 0; n < 32; n++) {
  31. if (1UL & (used_ports_map[i] >> n))
  32. continue;
  33. used_ports_map[i] |= (1UL << n);
  34. n += (i * 32);
  35. /* PID_MAX_LIMIT is currently at 2^22, leaving 10 bit
  36. * to, i.e. 1024 unique ports per application. */
  37. return pid + (n << 22);
  38. }
  39. }
  40. /* Out of sockets in our own PID namespace, what to do? FIXME */
  41. return UINT_MAX;
  42. }
  43. static void release_local_port(uint32_t port)
  44. {
  45. int nr;
  46. if (port == UINT_MAX)
  47. return;
  48. nr = port >> 22;
  49. used_ports_map[nr / 32] &= ~(1 << nr % 32);
  50. }
  51. /**
  52. * @name Allocation
  53. * @{
  54. */
  55. static struct nl_sock *__alloc_socket(struct nl_cb *cb)
  56. {
  57. struct nl_sock *sk;
  58. sk = calloc(1, sizeof(*sk));
  59. if (!sk)
  60. return NULL;
  61. sk->s_fd = -1;
  62. sk->s_cb = cb;
  63. sk->s_local.nl_family = AF_NETLINK;
  64. sk->s_peer.nl_family = AF_NETLINK;
  65. sk->s_seq_expect = sk->s_seq_next = time(0);
  66. sk->s_local.nl_pid = generate_local_port();
  67. if (sk->s_local.nl_pid == UINT_MAX) {
  68. nl_socket_free(sk);
  69. return NULL;
  70. }
  71. return sk;
  72. }
  73. /**
  74. * Allocate new netlink socket
  75. *
  76. * @return Newly allocated netlink socket or NULL.
  77. */
  78. struct nl_sock *nl_socket_alloc(void)
  79. {
  80. struct nl_cb *cb;
  81. cb = nl_cb_alloc(NL_CB_DEFAULT);
  82. if (!cb)
  83. return NULL;
  84. return __alloc_socket(cb);
  85. }
  86. /**
  87. * Allocate new socket with custom callbacks
  88. * @arg cb Callback handler
  89. *
  90. * The reference to the callback handler is taken into account
  91. * automatically, it is released again upon calling nl_socket_free().
  92. *
  93. *@return Newly allocted socket handle or NULL.
  94. */
  95. struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
  96. {
  97. if (cb == NULL)
  98. BUG();
  99. return __alloc_socket(nl_cb_get(cb));
  100. }
  101. /**
  102. * Free a netlink socket.
  103. * @arg sk Netlink socket.
  104. */
  105. void nl_socket_free(struct nl_sock *sk)
  106. {
  107. if (!sk)
  108. return;
  109. if (sk->s_fd >= 0)
  110. close(sk->s_fd);
  111. if (!(sk->s_flags & NL_OWN_PORT))
  112. release_local_port(sk->s_local.nl_pid);
  113. nl_cb_put(sk->s_cb);
  114. free(sk);
  115. }
  116. /** @} */
  117. /**
  118. * @name Sequence Numbers
  119. * @{
  120. */
  121. static int noop_seq_check(struct nl_msg *msg, void *arg)
  122. {
  123. return NL_OK;
  124. }
  125. /**
  126. * Disable sequence number checking.
  127. * @arg sk Netlink socket.
  128. *
  129. * Disables checking of sequence numbers on the netlink socket This is
  130. * required to allow messages to be processed which were not requested by
  131. * a preceding request message, e.g. netlink events.
  132. *
  133. * @note This function modifies the NL_CB_SEQ_CHECK configuration in
  134. * the callback handle associated with the socket.
  135. */
  136. void nl_socket_disable_seq_check(struct nl_sock *sk)
  137. {
  138. nl_cb_set(sk->s_cb, NL_CB_SEQ_CHECK,
  139. NL_CB_CUSTOM, noop_seq_check, NULL);
  140. }
  141. /** @} */
  142. /**
  143. * Set local port of socket
  144. * @arg sk Netlink socket.
  145. * @arg port Local port identifier
  146. *
  147. * Assigns a local port identifier to the socket. If port is 0
  148. * a unique port identifier will be generated automatically.
  149. */
  150. void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
  151. {
  152. if (port == 0) {
  153. port = generate_local_port();
  154. sk->s_flags &= ~NL_OWN_PORT;
  155. } else {
  156. if (!(sk->s_flags & NL_OWN_PORT))
  157. release_local_port(sk->s_local.nl_pid);
  158. sk->s_flags |= NL_OWN_PORT;
  159. }
  160. sk->s_local.nl_pid = port;
  161. }
  162. /** @} */
  163. /**
  164. * @name Group Subscriptions
  165. * @{
  166. */
  167. /**
  168. * Join groups
  169. * @arg sk Netlink socket
  170. * @arg group Group identifier
  171. *
  172. * Joins the specified groups using the modern socket option which
  173. * is available since kernel version 2.6.14. It allows joining an
  174. * almost arbitary number of groups without limitation. The list
  175. * of groups has to be terminated by 0 (%NFNLGRP_NONE).
  176. *
  177. * Make sure to use the correct group definitions as the older
  178. * bitmask definitions for nl_join_groups() are likely to still
  179. * be present for backward compatibility reasons.
  180. *
  181. * @return 0 on sucess or a negative error code.
  182. */
  183. int nl_socket_add_memberships(struct nl_sock *sk, int group, ...)
  184. {
  185. int err;
  186. va_list ap;
  187. if (sk->s_fd == -1)
  188. return -NLE_BAD_SOCK;
  189. va_start(ap, group);
  190. while (group != 0) {
  191. if (group < 0)
  192. return -NLE_INVAL;
  193. err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
  194. &group, sizeof(group));
  195. if (err < 0)
  196. return -nl_syserr2nlerr(errno);
  197. group = va_arg(ap, int);
  198. }
  199. va_end(ap);
  200. return 0;
  201. }
  202. /**
  203. * Leave groups
  204. * @arg sk Netlink socket
  205. * @arg group Group identifier
  206. *
  207. * Leaves the specified groups using the modern socket option
  208. * which is available since kernel version 2.6.14. The list of groups
  209. * has to terminated by 0 (%NFNLGRP_NONE).
  210. *
  211. * @see nl_socket_add_membership
  212. * @return 0 on success or a negative error code.
  213. */
  214. int nl_socket_drop_memberships(struct nl_sock *sk, int group, ...)
  215. {
  216. int err;
  217. va_list ap;
  218. if (sk->s_fd == -1)
  219. return -NLE_BAD_SOCK;
  220. va_start(ap, group);
  221. while (group != 0) {
  222. if (group < 0)
  223. return -NLE_INVAL;
  224. err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
  225. &group, sizeof(group));
  226. if (err < 0)
  227. return -nl_syserr2nlerr(errno);
  228. group = va_arg(ap, int);
  229. }
  230. va_end(ap);
  231. return 0;
  232. }
  233. /** @} */
  234. /**
  235. * Set file descriptor of socket to non-blocking state
  236. * @arg sk Netlink socket.
  237. *
  238. * @return 0 on success or a negative error code.
  239. */
  240. int nl_socket_set_nonblocking(struct nl_sock *sk)
  241. {
  242. if (sk->s_fd == -1)
  243. return -NLE_BAD_SOCK;
  244. if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0)
  245. return -nl_syserr2nlerr(errno);
  246. return 0;
  247. }
  248. /** @} */
  249. /**
  250. * @name Utilities
  251. * @{
  252. */
  253. /**
  254. * Set socket buffer size of netlink socket.
  255. * @arg sk Netlink socket.
  256. * @arg rxbuf New receive socket buffer size in bytes.
  257. * @arg txbuf New transmit socket buffer size in bytes.
  258. *
  259. * Sets the socket buffer size of a netlink socket to the specified
  260. * values \c rxbuf and \c txbuf. Providing a value of \c 0 assumes a
  261. * good default value.
  262. *
  263. * @note It is not required to call this function prior to nl_connect().
  264. * @return 0 on sucess or a negative error code.
  265. */
  266. int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
  267. {
  268. int err;
  269. if (rxbuf <= 0)
  270. rxbuf = 32768;
  271. if (txbuf <= 0)
  272. txbuf = 32768;
  273. if (sk->s_fd == -1)
  274. return -NLE_BAD_SOCK;
  275. err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
  276. &txbuf, sizeof(txbuf));
  277. if (err < 0)
  278. return -nl_syserr2nlerr(errno);
  279. err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
  280. &rxbuf, sizeof(rxbuf));
  281. if (err < 0)
  282. return -nl_syserr2nlerr(errno);
  283. sk->s_flags |= NL_SOCK_BUFSIZE_SET;
  284. return 0;
  285. }
  286. /**
  287. * Enable/disable credential passing on netlink socket.
  288. * @arg sk Netlink socket.
  289. * @arg state New state (0 - disabled, 1 - enabled)
  290. *
  291. * @return 0 on success or a negative error code
  292. */
  293. int nl_socket_set_passcred(struct nl_sock *sk, int state)
  294. {
  295. int err;
  296. if (sk->s_fd == -1)
  297. return -NLE_BAD_SOCK;
  298. err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
  299. &state, sizeof(state));
  300. if (err < 0)
  301. return -nl_syserr2nlerr(errno);
  302. if (state)
  303. sk->s_flags |= NL_SOCK_PASSCRED;
  304. else
  305. sk->s_flags &= ~NL_SOCK_PASSCRED;
  306. return 0;
  307. }
  308. /**
  309. * Enable/disable receival of additional packet information
  310. * @arg sk Netlink socket.
  311. * @arg state New state (0 - disabled, 1 - enabled)
  312. *
  313. * @return 0 on success or a negative error code
  314. */
  315. int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
  316. {
  317. int err;
  318. if (sk->s_fd == -1)
  319. return -NLE_BAD_SOCK;
  320. err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
  321. &state, sizeof(state));
  322. if (err < 0)
  323. return -nl_syserr2nlerr(errno);
  324. return 0;
  325. }
  326. /** @} */
  327. /** @} */