1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 #include <netinet/in.h>
10 #include "sd-netlink.h"
12 #include "alloc-util.h"
14 #include "format-util.h"
16 #include "netlink-internal.h"
17 #include "netlink-types.h"
18 #include "netlink-util.h"
20 #include "socket-util.h"
23 int socket_open(int family
) {
26 fd
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, family
);
30 return fd_move_above_stdio(fd
);
33 static int broadcast_groups_get(sd_netlink
*nl
) {
34 _cleanup_free_
uint32_t *groups
= NULL
;
35 socklen_t len
= 0, old_len
;
42 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, NULL
, &len
);
44 if (errno
== ENOPROTOOPT
) {
45 nl
->broadcast_group_dont_leave
= true;
54 groups
= new0(uint32_t, len
);
60 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, groups
, &len
);
67 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
71 for (i
= 0; i
< len
; i
++) {
72 for (j
= 0; j
< sizeof(uint32_t) * 8; j
++) {
78 if (!(groups
[i
] & offset
))
81 group
= i
* sizeof(uint32_t) * 8 + j
+ 1;
83 r
= hashmap_put(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(1));
92 int socket_bind(sd_netlink
*nl
) {
96 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_PKTINFO
, &one
, sizeof(one
));
100 addrlen
= sizeof(nl
->sockaddr
);
102 r
= bind(nl
->fd
, &nl
->sockaddr
.sa
, addrlen
);
103 /* ignore EINVAL to allow opening an already bound socket */
104 if (r
< 0 && errno
!= EINVAL
)
107 r
= getsockname(nl
->fd
, &nl
->sockaddr
.sa
, &addrlen
);
111 r
= broadcast_groups_get(nl
);
118 static unsigned broadcast_group_get_ref(sd_netlink
*nl
, unsigned group
) {
121 return PTR_TO_UINT(hashmap_get(nl
->broadcast_group_refs
, UINT_TO_PTR(group
)));
124 static int broadcast_group_set_ref(sd_netlink
*nl
, unsigned group
, unsigned n_ref
) {
129 r
= hashmap_replace(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(n_ref
));
136 static int broadcast_group_join(sd_netlink
*nl
, unsigned group
) {
143 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
, &group
, sizeof(group
));
150 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
) {
156 n_ref
= broadcast_group_get_ref(nl
, group
);
160 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
164 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
169 /* not yet in the group */
172 r
= broadcast_group_join(nl
, group
);
179 static int broadcast_group_leave(sd_netlink
*nl
, unsigned group
) {
186 if (nl
->broadcast_group_dont_leave
)
189 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_DROP_MEMBERSHIP
, &group
, sizeof(group
));
196 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
) {
202 n_ref
= broadcast_group_get_ref(nl
, group
);
208 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
213 /* still refs left */
216 r
= broadcast_group_leave(nl
, group
);
223 /* returns the number of bytes sent, or a negative error code */
224 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
) {
227 struct sockaddr_nl nl
;
229 .nl
.nl_family
= AF_NETLINK
,
237 k
= sendto(nl
->fd
, m
->hdr
, m
->hdr
->nlmsg_len
,
238 0, &addr
.sa
, sizeof(addr
));
245 static int socket_recv_message(int fd
, struct iovec
*iov
, uint32_t *_group
, bool peek
) {
246 union sockaddr_union sender
;
247 uint8_t cmsg_buffer
[CMSG_SPACE(sizeof(struct nl_pktinfo
))];
248 struct msghdr msg
= {
252 .msg_namelen
= sizeof(sender
),
253 .msg_control
= cmsg_buffer
,
254 .msg_controllen
= sizeof(cmsg_buffer
),
256 struct cmsghdr
*cmsg
;
263 n
= recvmsg(fd
, &msg
, MSG_TRUNC
| (peek
? MSG_PEEK
: 0));
266 if (errno
== ENOBUFS
)
267 log_debug("rtnl: kernel receive buffer overrun");
268 else if (errno
== EAGAIN
)
269 log_debug("rtnl: no data in socket");
271 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
274 if (sender
.nl
.nl_pid
!= 0) {
275 /* not from the kernel, ignore */
276 log_debug("rtnl: ignoring message from portid %"PRIu32
, sender
.nl
.nl_pid
);
279 /* drop the message */
280 n
= recvmsg(fd
, &msg
, 0);
282 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
288 CMSG_FOREACH(cmsg
, &msg
) {
289 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
290 cmsg
->cmsg_type
== NETLINK_PKTINFO
&&
291 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct nl_pktinfo
))) {
292 struct nl_pktinfo
*pktinfo
= (void *)CMSG_DATA(cmsg
);
294 /* multi-cast group */
295 group
= pktinfo
->group
;
305 /* On success, the number of bytes received is returned and *ret points to the received message
306 * which has a valid header and the correct size.
307 * If nothing useful was received 0 is returned.
308 * On failure, a negative error code is returned.
310 int socket_read_message(sd_netlink
*rtnl
) {
311 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*first
= NULL
;
312 struct iovec iov
= {};
314 bool multi_part
= false, done
= false;
315 struct nlmsghdr
*new_msg
;
319 const NLTypeSystem
*type_system_root
;
322 assert(rtnl
->rbuffer
);
323 assert(rtnl
->rbuffer_allocated
>= sizeof(struct nlmsghdr
));
325 type_system_root
= type_system_get_root(rtnl
->protocol
);
327 /* read nothing, just get the pending message size */
328 r
= socket_recv_message(rtnl
->fd
, &iov
, NULL
, true);
334 /* make room for the pending message */
335 if (!greedy_realloc((void **)&rtnl
->rbuffer
,
336 &rtnl
->rbuffer_allocated
,
337 len
, sizeof(uint8_t)))
340 iov
.iov_base
= rtnl
->rbuffer
;
341 iov
.iov_len
= rtnl
->rbuffer_allocated
;
343 /* read the pending message */
344 r
= socket_recv_message(rtnl
->fd
, &iov
, &group
, false);
350 if (len
> rtnl
->rbuffer_allocated
)
351 /* message did not fit in read buffer */
354 if (NLMSG_OK(rtnl
->rbuffer
, len
) && rtnl
->rbuffer
->nlmsg_flags
& NLM_F_MULTI
) {
357 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++) {
358 if (rtnl_message_get_serial(rtnl
->rqueue_partial
[i
]) ==
359 rtnl
->rbuffer
->nlmsg_seq
) {
360 first
= rtnl
->rqueue_partial
[i
];
366 for (new_msg
= rtnl
->rbuffer
; NLMSG_OK(new_msg
, len
) && !done
; new_msg
= NLMSG_NEXT(new_msg
, len
)) {
367 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
368 const NLType
*nl_type
;
370 if (!group
&& new_msg
->nlmsg_pid
!= rtnl
->sockaddr
.nl
.nl_pid
)
371 /* not broadcast and not for us */
374 if (new_msg
->nlmsg_type
== NLMSG_NOOP
)
375 /* silently drop noop messages */
378 if (new_msg
->nlmsg_type
== NLMSG_DONE
) {
379 /* finished reading multi-part message */
382 /* if first is not defined, put NLMSG_DONE into the receive queue. */
387 /* check that we support this message type */
388 r
= type_system_get_type(type_system_root
, &nl_type
, new_msg
->nlmsg_type
);
390 if (r
== -EOPNOTSUPP
)
391 log_debug("sd-netlink: ignored message with unknown type: %i",
392 new_msg
->nlmsg_type
);
397 /* check that the size matches the message type */
398 if (new_msg
->nlmsg_len
< NLMSG_LENGTH(type_get_size(nl_type
))) {
399 log_debug("sd-netlink: message larger than expected, dropping");
403 r
= message_new_empty(rtnl
, &m
);
407 m
->broadcast
= !!group
;
409 m
->hdr
= memdup(new_msg
, new_msg
->nlmsg_len
);
413 /* seal and parse the top-level message */
414 r
= sd_netlink_message_rewind(m
);
418 /* push the message onto the multi-part message stack */
425 log_debug("sd-netlink: discarding %zu bytes of incoming message", len
);
430 if (!multi_part
|| done
) {
431 /* we got a complete message, push it on the read queue */
432 r
= rtnl_rqueue_make_room(rtnl
);
436 rtnl
->rqueue
[rtnl
->rqueue_size
++] = TAKE_PTR(first
);
438 if (multi_part
&& (i
< rtnl
->rqueue_partial_size
)) {
439 /* remove the message form the partial read queue */
440 memmove(rtnl
->rqueue_partial
+ i
,rtnl
->rqueue_partial
+ i
+ 1,
441 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_partial_size
- i
- 1));
442 rtnl
->rqueue_partial_size
--;
447 /* we only got a partial multi-part message, push it on the
448 partial read queue */
449 if (i
< rtnl
->rqueue_partial_size
)
450 rtnl
->rqueue_partial
[i
] = TAKE_PTR(first
);
452 r
= rtnl_rqueue_partial_make_room(rtnl
);
456 rtnl
->rqueue_partial
[rtnl
->rqueue_partial_size
++] = TAKE_PTR(first
);