libnl-tiny: set SOCK_CLOEXEC if available
[project/libnl-tiny.git] / nl.c
1 /*
2 * lib/nl.c Core Netlink Interface
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 /**
13 * @defgroup core Core
14 *
15 * @details
16 * @par 1) Connecting the socket
17 * @code
18 * // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
19 * nl_connect(sk, NETLINK_ROUTE);
20 * @endcode
21 *
22 * @par 2) Sending data
23 * @code
24 * // The most rudimentary method is to use nl_sendto() simply pushing
25 * // a piece of data to the other netlink peer. This method is not
26 * // recommended.
27 * const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
28 * nl_sendto(sk, buf, sizeof(buf));
29 *
30 * // A more comfortable interface is nl_send() taking a pointer to
31 * // a netlink message.
32 * struct nl_msg *msg = my_msg_builder();
33 * nl_send(sk, nlmsg_hdr(msg));
34 *
35 * // nl_sendmsg() provides additional control over the sendmsg() message
36 * // header in order to allow more specific addressing of multiple peers etc.
37 * struct msghdr hdr = { ... };
38 * nl_sendmsg(sk, nlmsg_hdr(msg), &hdr);
39 *
40 * // You're probably too lazy to fill out the netlink pid, sequence number
41 * // and message flags all the time. nl_send_auto_complete() automatically
42 * // extends your message header as needed with an appropriate sequence
43 * // number, the netlink pid stored in the netlink socket and the message
44 * // flags NLM_F_REQUEST and NLM_F_ACK (if not disabled in the socket)
45 * nl_send_auto_complete(sk, nlmsg_hdr(msg));
46 *
47 * // Simple protocols don't require the complex message construction interface
48 * // and may favour nl_send_simple() to easly send a bunch of payload
49 * // encapsulated in a netlink message header.
50 * nl_send_simple(sk, MY_MSG_TYPE, 0, buf, sizeof(buf));
51 * @endcode
52 *
53 * @par 3) Receiving data
54 * @code
55 * // nl_recv() receives a single message allocating a buffer for the message
56 * // content and gives back the pointer to you.
57 * struct sockaddr_nl peer;
58 * unsigned char *msg;
59 * nl_recv(sk, &peer, &msg);
60 *
61 * // nl_recvmsgs() receives a bunch of messages until the callback system
62 * // orders it to state, usually after receving a compolete multi part
63 * // message series.
64 * nl_recvmsgs(sk, my_callback_configuration);
65 *
66 * // nl_recvmsgs_default() acts just like nl_recvmsg() but uses the callback
67 * // configuration stored in the socket.
68 * nl_recvmsgs_default(sk);
69 *
70 * // In case you want to wait for the ACK to be recieved that you requested
71 * // with your latest message, you can call nl_wait_for_ack()
72 * nl_wait_for_ack(sk);
73 * @endcode
74 *
75 * @par 4) Closing
76 * @code
77 * // Close the socket first to release kernel memory
78 * nl_close(sk);
79 * @endcode
80 *
81 * @{
82 */
83
84 #include <netlink-local.h>
85 #include <netlink/netlink.h>
86 #include <netlink/utils.h>
87 #include <netlink/handlers.h>
88 #include <netlink/msg.h>
89 #include <netlink/attr.h>
90
91 /**
92 * @name Connection Management
93 * @{
94 */
95
96 /**
97 * Create and connect netlink socket.
98 * @arg sk Netlink socket.
99 * @arg protocol Netlink protocol to use.
100 *
101 * Creates a netlink socket using the specified protocol, binds the socket
102 * and issues a connection attempt.
103 *
104 * @return 0 on success or a negative error code.
105 */
106 int nl_connect(struct nl_sock *sk, int protocol)
107 {
108 int err;
109 int flags = 0;
110 socklen_t addrlen;
111
112 #ifdef SOCK_CLOEXEC
113 flags = SOCK_CLOEXEC;
114 #endif
115
116 sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
117 if (sk->s_fd < 0) {
118 err = -nl_syserr2nlerr(errno);
119 goto errout;
120 }
121
122 if (!(sk->s_flags & NL_SOCK_BUFSIZE_SET)) {
123 err = nl_socket_set_buffer_size(sk, 0, 0);
124 if (err < 0)
125 goto errout;
126 }
127
128 err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
129 sizeof(sk->s_local));
130 if (err < 0) {
131 err = -nl_syserr2nlerr(errno);
132 goto errout;
133 }
134
135 addrlen = sizeof(sk->s_local);
136 err = getsockname(sk->s_fd, (struct sockaddr *) &sk->s_local,
137 &addrlen);
138 if (err < 0) {
139 err = -nl_syserr2nlerr(errno);
140 goto errout;
141 }
142
143 if (addrlen != sizeof(sk->s_local)) {
144 err = -NLE_NOADDR;
145 goto errout;
146 }
147
148 if (sk->s_local.nl_family != AF_NETLINK) {
149 err = -NLE_AF_NOSUPPORT;
150 goto errout;
151 }
152
153 sk->s_proto = protocol;
154
155 return 0;
156 errout:
157 close(sk->s_fd);
158 sk->s_fd = -1;
159
160 return err;
161 }
162
163 /**
164 * Close/Disconnect netlink socket.
165 * @arg sk Netlink socket.
166 */
167 void nl_close(struct nl_sock *sk)
168 {
169 if (sk->s_fd >= 0) {
170 close(sk->s_fd);
171 sk->s_fd = -1;
172 }
173
174 sk->s_proto = 0;
175 }
176
177 /** @} */
178
179 /**
180 * @name Send
181 * @{
182 */
183
184 /**
185 * Send raw data over netlink socket.
186 * @arg sk Netlink socket.
187 * @arg buf Data buffer.
188 * @arg size Size of data buffer.
189 * @return Number of characters written on success or a negative error code.
190 */
191 int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
192 {
193 int ret;
194
195 ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
196 &sk->s_peer, sizeof(sk->s_peer));
197 if (ret < 0)
198 return -nl_syserr2nlerr(errno);
199
200 return ret;
201 }
202
203 /**
204 * Send netlink message with control over sendmsg() message header.
205 * @arg sk Netlink socket.
206 * @arg msg Netlink message to be sent.
207 * @arg hdr Sendmsg() message header.
208 * @return Number of characters sent on sucess or a negative error code.
209 */
210 int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
211 {
212 struct nl_cb *cb;
213 int ret;
214
215 struct iovec iov = {
216 .iov_base = (void *) nlmsg_hdr(msg),
217 .iov_len = nlmsg_hdr(msg)->nlmsg_len,
218 };
219
220 hdr->msg_iov = &iov;
221 hdr->msg_iovlen = 1;
222
223 nlmsg_set_src(msg, &sk->s_local);
224
225 cb = sk->s_cb;
226 if (cb->cb_set[NL_CB_MSG_OUT])
227 if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_OK)
228 return 0;
229
230 ret = sendmsg(sk->s_fd, hdr, 0);
231 if (ret < 0)
232 return -nl_syserr2nlerr(errno);
233
234 return ret;
235 }
236
237
238 /**
239 * Send netlink message.
240 * @arg sk Netlink socket.
241 * @arg msg Netlink message to be sent.
242 * @see nl_sendmsg()
243 * @return Number of characters sent on success or a negative error code.
244 */
245 int nl_send(struct nl_sock *sk, struct nl_msg *msg)
246 {
247 struct sockaddr_nl *dst;
248 struct ucred *creds;
249
250 struct msghdr hdr = {
251 .msg_name = (void *) &sk->s_peer,
252 .msg_namelen = sizeof(struct sockaddr_nl),
253 };
254
255 /* Overwrite destination if specified in the message itself, defaults
256 * to the peer address of the socket.
257 */
258 dst = nlmsg_get_dst(msg);
259 if (dst->nl_family == AF_NETLINK)
260 hdr.msg_name = dst;
261
262 /* Add credentials if present. */
263 creds = nlmsg_get_creds(msg);
264 if (creds != NULL) {
265 char buf[CMSG_SPACE(sizeof(struct ucred))];
266 struct cmsghdr *cmsg;
267
268 hdr.msg_control = buf;
269 hdr.msg_controllen = sizeof(buf);
270
271 cmsg = CMSG_FIRSTHDR(&hdr);
272 cmsg->cmsg_level = SOL_SOCKET;
273 cmsg->cmsg_type = SCM_CREDENTIALS;
274 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
275 memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
276 }
277
278 return nl_sendmsg(sk, msg, &hdr);
279 }
280
281 /**
282 * Send netlink message and check & extend header values as needed.
283 * @arg sk Netlink socket.
284 * @arg msg Netlink message to be sent.
285 *
286 * Checks the netlink message \c nlh for completness and extends it
287 * as required before sending it out. Checked fields include pid,
288 * sequence nr, and flags.
289 *
290 * @see nl_send()
291 * @return Number of characters sent or a negative error code.
292 */
293 int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
294 {
295 struct nlmsghdr *nlh;
296 struct nl_cb *cb = sk->s_cb;
297
298 nlh = nlmsg_hdr(msg);
299 if (nlh->nlmsg_pid == 0)
300 nlh->nlmsg_pid = sk->s_local.nl_pid;
301
302 if (nlh->nlmsg_seq == 0)
303 nlh->nlmsg_seq = sk->s_seq_next++;
304
305 if (msg->nm_protocol == -1)
306 msg->nm_protocol = sk->s_proto;
307
308 nlh->nlmsg_flags |= NLM_F_REQUEST;
309
310 if (!(sk->s_flags & NL_NO_AUTO_ACK))
311 nlh->nlmsg_flags |= NLM_F_ACK;
312
313 if (cb->cb_send_ow)
314 return cb->cb_send_ow(sk, msg);
315 else
316 return nl_send(sk, msg);
317 }
318
319 /**
320 * Send simple netlink message using nl_send_auto_complete()
321 * @arg sk Netlink socket.
322 * @arg type Netlink message type.
323 * @arg flags Netlink message flags.
324 * @arg buf Data buffer.
325 * @arg size Size of data buffer.
326 *
327 * Builds a netlink message with the specified type and flags and
328 * appends the specified data as payload to the message.
329 *
330 * @see nl_send_auto_complete()
331 * @return Number of characters sent on success or a negative error code.
332 */
333 int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
334 size_t size)
335 {
336 int err;
337 struct nl_msg *msg;
338
339 msg = nlmsg_alloc_simple(type, flags);
340 if (!msg)
341 return -NLE_NOMEM;
342
343 if (buf && size) {
344 err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
345 if (err < 0)
346 goto errout;
347 }
348
349
350 err = nl_send_auto_complete(sk, msg);
351 errout:
352 nlmsg_free(msg);
353
354 return err;
355 }
356
357 /** @} */
358
359 /**
360 * @name Receive
361 * @{
362 */
363
364 /**
365 * Receive data from netlink socket
366 * @arg sk Netlink socket.
367 * @arg nla Destination pointer for peer's netlink address.
368 * @arg buf Destination pointer for message content.
369 * @arg creds Destination pointer for credentials.
370 *
371 * Receives a netlink message, allocates a buffer in \c *buf and
372 * stores the message content. The peer's netlink address is stored
373 * in \c *nla. The caller is responsible for freeing the buffer allocated
374 * in \c *buf if a positive value is returned. Interrupted system calls
375 * are handled by repeating the read. The input buffer size is determined
376 * by peeking before the actual read is done.
377 *
378 * A non-blocking sockets causes the function to return immediately with
379 * a return value of 0 if no data is available.
380 *
381 * @return Number of octets read, 0 on EOF or a negative error code.
382 */
383 int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
384 unsigned char **buf, struct ucred **creds)
385 {
386 int n;
387 int flags = 0;
388 static int page_size = 0;
389 struct iovec iov;
390 struct msghdr msg = {
391 .msg_name = (void *) nla,
392 .msg_namelen = sizeof(struct sockaddr_nl),
393 .msg_iov = &iov,
394 .msg_iovlen = 1,
395 .msg_control = NULL,
396 .msg_controllen = 0,
397 .msg_flags = 0,
398 };
399 struct cmsghdr *cmsg;
400
401 if (sk->s_flags & NL_MSG_PEEK)
402 flags |= MSG_PEEK;
403
404 if (page_size == 0)
405 page_size = getpagesize() * 4;
406
407 iov.iov_len = page_size;
408 iov.iov_base = *buf = calloc(1, iov.iov_len);
409 if (!*buf)
410 return -nl_syserr2nlerr(errno);
411
412 if (sk->s_flags & NL_SOCK_PASSCRED) {
413 msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
414 msg.msg_control = calloc(1, msg.msg_controllen);
415 }
416 retry:
417
418 n = recvmsg(sk->s_fd, &msg, flags);
419 if (!n)
420 goto abort;
421 else if (n < 0) {
422 if (errno == EINTR) {
423 NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
424 goto retry;
425 } else if (errno == EAGAIN) {
426 NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
427 goto abort;
428 } else {
429 free(msg.msg_control);
430 free(*buf);
431 *buf = NULL;
432 return -nl_syserr2nlerr(errno);
433 }
434 }
435
436 if (iov.iov_len < (size_t) n ||
437 msg.msg_flags & MSG_TRUNC) {
438 /* Provided buffer is not long enough, enlarge it
439 * and try again. */
440 iov.iov_len *= 2;
441 iov.iov_base = *buf = realloc(*buf, iov.iov_len);
442 goto retry;
443 } else if (msg.msg_flags & MSG_CTRUNC) {
444 msg.msg_controllen *= 2;
445 msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
446 goto retry;
447 } else if (flags != 0) {
448 /* Buffer is big enough, do the actual reading */
449 flags = 0;
450 goto retry;
451 }
452
453 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
454 free(msg.msg_control);
455 free(*buf);
456 *buf = NULL;
457 return -NLE_NOADDR;
458 }
459
460 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
461 if (cmsg->cmsg_level == SOL_SOCKET &&
462 cmsg->cmsg_type == SCM_CREDENTIALS) {
463 *creds = calloc(1, sizeof(struct ucred));
464 memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
465 break;
466 }
467 }
468
469 free(msg.msg_control);
470 return n;
471
472 abort:
473 free(msg.msg_control);
474 free(*buf);
475 *buf = NULL;
476 return 0;
477 }
478
479 #define NL_CB_CALL(cb, type, msg) \
480 do { \
481 err = nl_cb_call(cb, type, msg); \
482 switch (err) { \
483 case NL_OK: \
484 err = 0; \
485 break; \
486 case NL_SKIP: \
487 goto skip; \
488 case NL_STOP: \
489 goto stop; \
490 default: \
491 goto out; \
492 } \
493 } while (0)
494
495 static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
496 {
497 int n, err = 0, multipart = 0;
498 unsigned char *buf = NULL;
499 struct nlmsghdr *hdr;
500 struct sockaddr_nl nla = {0};
501 struct nl_msg *msg = NULL;
502 struct ucred *creds = NULL;
503
504 continue_reading:
505 NL_DBG(3, "Attempting to read from %p\n", sk);
506 if (cb->cb_recv_ow)
507 n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
508 else
509 n = nl_recv(sk, &nla, &buf, &creds);
510
511 if (n <= 0)
512 return n;
513
514 /* make clang analyzer happy */
515 assert(n > 0 && buf);
516
517 NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
518
519 hdr = (struct nlmsghdr *) buf;
520 while (nlmsg_ok(hdr, n)) {
521 NL_DBG(3, "recgmsgs(%p): Processing valid message...\n", sk);
522
523 nlmsg_free(msg);
524 msg = nlmsg_convert(hdr);
525 if (!msg) {
526 err = -NLE_NOMEM;
527 goto out;
528 }
529
530 nlmsg_set_proto(msg, sk->s_proto);
531 nlmsg_set_src(msg, &nla);
532 if (creds)
533 nlmsg_set_creds(msg, creds);
534
535 /* Raw callback is the first, it gives the most control
536 * to the user and he can do his very own parsing. */
537 if (cb->cb_set[NL_CB_MSG_IN])
538 NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
539
540 /* Sequence number checking. The check may be done by
541 * the user, otherwise a very simple check is applied
542 * enforcing strict ordering */
543 if (cb->cb_set[NL_CB_SEQ_CHECK])
544 NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
545 else if (hdr->nlmsg_seq != sk->s_seq_expect) {
546 if (cb->cb_set[NL_CB_INVALID])
547 NL_CB_CALL(cb, NL_CB_INVALID, msg);
548 else {
549 err = -NLE_SEQ_MISMATCH;
550 goto out;
551 }
552 }
553
554 if (hdr->nlmsg_type == NLMSG_DONE ||
555 hdr->nlmsg_type == NLMSG_ERROR ||
556 hdr->nlmsg_type == NLMSG_NOOP ||
557 hdr->nlmsg_type == NLMSG_OVERRUN) {
558 /* We can't check for !NLM_F_MULTI since some netlink
559 * users in the kernel are broken. */
560 sk->s_seq_expect++;
561 NL_DBG(3, "recvmsgs(%p): Increased expected " \
562 "sequence number to %d\n",
563 sk, sk->s_seq_expect);
564 }
565
566 if (hdr->nlmsg_flags & NLM_F_MULTI)
567 multipart = 1;
568
569 /* Other side wishes to see an ack for this message */
570 if (hdr->nlmsg_flags & NLM_F_ACK) {
571 if (cb->cb_set[NL_CB_SEND_ACK])
572 NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
573 else {
574 /* FIXME: implement */
575 }
576 }
577
578 /* messages terminates a multpart message, this is
579 * usually the end of a message and therefore we slip
580 * out of the loop by default. the user may overrule
581 * this action by skipping this packet. */
582 if (hdr->nlmsg_type == NLMSG_DONE) {
583 multipart = 0;
584 if (cb->cb_set[NL_CB_FINISH])
585 NL_CB_CALL(cb, NL_CB_FINISH, msg);
586 }
587
588 /* Message to be ignored, the default action is to
589 * skip this message if no callback is specified. The
590 * user may overrule this action by returning
591 * NL_PROCEED. */
592 else if (hdr->nlmsg_type == NLMSG_NOOP) {
593 if (cb->cb_set[NL_CB_SKIPPED])
594 NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
595 else
596 goto skip;
597 }
598
599 /* Data got lost, report back to user. The default action is to
600 * quit parsing. The user may overrule this action by retuning
601 * NL_SKIP or NL_PROCEED (dangerous) */
602 else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
603 if (cb->cb_set[NL_CB_OVERRUN])
604 NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
605 else {
606 err = -NLE_MSG_OVERFLOW;
607 goto out;
608 }
609 }
610
611 /* Message carries a nlmsgerr */
612 else if (hdr->nlmsg_type == NLMSG_ERROR) {
613 struct nlmsgerr *e = nlmsg_data(hdr);
614
615 if (hdr->nlmsg_len < (unsigned) nlmsg_msg_size(sizeof(*e))) {
616 /* Truncated error message, the default action
617 * is to stop parsing. The user may overrule
618 * this action by returning NL_SKIP or
619 * NL_PROCEED (dangerous) */
620 if (cb->cb_set[NL_CB_INVALID])
621 NL_CB_CALL(cb, NL_CB_INVALID, msg);
622 else {
623 err = -NLE_MSG_TRUNC;
624 goto out;
625 }
626 } else if (e->error) {
627 /* Error message reported back from kernel. */
628 if (cb->cb_err) {
629 err = cb->cb_err(&nla, e,
630 cb->cb_err_arg);
631 if (err < 0)
632 goto out;
633 else if (err == NL_SKIP)
634 goto skip;
635 else if (err == NL_STOP) {
636 err = -nl_syserr2nlerr(e->error);
637 goto out;
638 }
639 } else {
640 err = -nl_syserr2nlerr(e->error);
641 goto out;
642 }
643 } else if (cb->cb_set[NL_CB_ACK])
644 NL_CB_CALL(cb, NL_CB_ACK, msg);
645 } else {
646 /* Valid message (not checking for MULTIPART bit to
647 * get along with broken kernels. NL_SKIP has no
648 * effect on this. */
649 if (cb->cb_set[NL_CB_VALID])
650 NL_CB_CALL(cb, NL_CB_VALID, msg);
651 }
652 skip:
653 hdr = nlmsg_next(hdr, &n);
654 }
655
656 nlmsg_free(msg);
657 free(buf);
658 free(creds);
659 buf = NULL;
660 msg = NULL;
661 creds = NULL;
662
663 if (multipart) {
664 /* Multipart message not yet complete, continue reading */
665 goto continue_reading;
666 }
667 stop:
668 err = 0;
669 out:
670 nlmsg_free(msg);
671 free(buf);
672 free(creds);
673
674 return err;
675 }
676
677 /**
678 * Receive a set of messages from a netlink socket.
679 * @arg sk Netlink socket.
680 * @arg cb set of callbacks to control behaviour.
681 *
682 * Repeatedly calls nl_recv() or the respective replacement if provided
683 * by the application (see nl_cb_overwrite_recv()) and parses the
684 * received data as netlink messages. Stops reading if one of the
685 * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
686 *
687 * A non-blocking sockets causes the function to return immediately if
688 * no data is available.
689 *
690 * @return 0 on success or a negative error code from nl_recv().
691 */
692 int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
693 {
694 if (cb->cb_recvmsgs_ow)
695 return cb->cb_recvmsgs_ow(sk, cb);
696 else
697 return recvmsgs(sk, cb);
698 }
699
700
701 static int ack_wait_handler(struct nl_msg *msg, void *arg)
702 {
703 return NL_STOP;
704 }
705
706 /**
707 * Wait for ACK.
708 * @arg sk Netlink socket.
709 * @pre The netlink socket must be in blocking state.
710 *
711 * Waits until an ACK is received for the latest not yet acknowledged
712 * netlink message.
713 */
714 int nl_wait_for_ack(struct nl_sock *sk)
715 {
716 int err;
717 struct nl_cb *cb;
718
719 cb = nl_cb_clone(sk->s_cb);
720 if (cb == NULL)
721 return -NLE_NOMEM;
722
723 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
724 err = nl_recvmsgs(sk, cb);
725 nl_cb_put(cb);
726
727 return err;
728 }
729
730 /** @} */
731
732 /** @} */