1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 #include <netinet/in.h>
12 #include "sd-netlink.h"
14 #include "alloc-util.h"
16 #include "format-util.h"
18 #include "netlink-internal.h"
19 #include "netlink-types.h"
20 #include "netlink-util.h"
22 #include "socket-util.h"
25 int socket_open(int family
) {
28 fd
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, family
);
32 return fd_move_above_stdio(fd
);
35 static int broadcast_groups_get(sd_netlink
*nl
) {
36 _cleanup_free_
uint32_t *groups
= NULL
;
37 socklen_t len
= 0, old_len
;
44 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, NULL
, &len
);
46 if (errno
== ENOPROTOOPT
) {
47 nl
->broadcast_group_dont_leave
= true;
56 groups
= new0(uint32_t, len
);
62 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, groups
, &len
);
69 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
73 for (i
= 0; i
< len
; i
++) {
74 for (j
= 0; j
< sizeof(uint32_t) * 8; j
++) {
80 if (!(groups
[i
] & offset
))
83 group
= i
* sizeof(uint32_t) * 8 + j
+ 1;
85 r
= hashmap_put(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(1));
94 int socket_bind(sd_netlink
*nl
) {
98 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_PKTINFO
, &one
, sizeof(one
));
102 addrlen
= sizeof(nl
->sockaddr
);
104 r
= bind(nl
->fd
, &nl
->sockaddr
.sa
, addrlen
);
105 /* ignore EINVAL to allow opening an already bound socket */
106 if (r
< 0 && errno
!= EINVAL
)
109 r
= getsockname(nl
->fd
, &nl
->sockaddr
.sa
, &addrlen
);
113 r
= broadcast_groups_get(nl
);
120 static unsigned broadcast_group_get_ref(sd_netlink
*nl
, unsigned group
) {
123 return PTR_TO_UINT(hashmap_get(nl
->broadcast_group_refs
, UINT_TO_PTR(group
)));
126 static int broadcast_group_set_ref(sd_netlink
*nl
, unsigned group
, unsigned n_ref
) {
131 r
= hashmap_replace(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(n_ref
));
138 static int broadcast_group_join(sd_netlink
*nl
, unsigned group
) {
145 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
, &group
, sizeof(group
));
152 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
) {
158 n_ref
= broadcast_group_get_ref(nl
, group
);
162 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
166 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
171 /* not yet in the group */
174 r
= broadcast_group_join(nl
, group
);
181 static int broadcast_group_leave(sd_netlink
*nl
, unsigned group
) {
188 if (nl
->broadcast_group_dont_leave
)
191 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_DROP_MEMBERSHIP
, &group
, sizeof(group
));
198 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
) {
204 n_ref
= broadcast_group_get_ref(nl
, group
);
210 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
215 /* still refs left */
218 r
= broadcast_group_leave(nl
, group
);
225 /* returns the number of bytes sent, or a negative error code */
226 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
) {
229 struct sockaddr_nl nl
;
231 .nl
.nl_family
= AF_NETLINK
,
239 k
= sendto(nl
->fd
, m
->hdr
, m
->hdr
->nlmsg_len
,
240 0, &addr
.sa
, sizeof(addr
));
247 static int socket_recv_message(int fd
, struct iovec
*iov
, uint32_t *_group
, bool peek
) {
248 union sockaddr_union sender
;
249 uint8_t cmsg_buffer
[CMSG_SPACE(sizeof(struct nl_pktinfo
))];
250 struct msghdr msg
= {
254 .msg_namelen
= sizeof(sender
),
255 .msg_control
= cmsg_buffer
,
256 .msg_controllen
= sizeof(cmsg_buffer
),
258 struct cmsghdr
*cmsg
;
265 n
= recvmsg(fd
, &msg
, MSG_TRUNC
| (peek
? MSG_PEEK
: 0));
268 if (errno
== ENOBUFS
)
269 log_debug("rtnl: kernel receive buffer overrun");
270 else if (errno
== EAGAIN
)
271 log_debug("rtnl: no data in socket");
273 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
276 if (sender
.nl
.nl_pid
!= 0) {
277 /* not from the kernel, ignore */
278 log_debug("rtnl: ignoring message from portid %"PRIu32
, sender
.nl
.nl_pid
);
281 /* drop the message */
282 n
= recvmsg(fd
, &msg
, 0);
284 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
290 CMSG_FOREACH(cmsg
, &msg
) {
291 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
292 cmsg
->cmsg_type
== NETLINK_PKTINFO
&&
293 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct nl_pktinfo
))) {
294 struct nl_pktinfo
*pktinfo
= (void *)CMSG_DATA(cmsg
);
296 /* multi-cast group */
297 group
= pktinfo
->group
;
307 /* On success, the number of bytes received is returned and *ret points to the received message
308 * which has a valid header and the correct size.
309 * If nothing useful was received 0 is returned.
310 * On failure, a negative error code is returned.
312 int socket_read_message(sd_netlink
*rtnl
) {
313 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*first
= NULL
;
314 struct iovec iov
= {};
316 bool multi_part
= false, done
= false;
317 struct nlmsghdr
*new_msg
;
321 const NLTypeSystem
*type_system_root
;
324 assert(rtnl
->rbuffer
);
325 assert(rtnl
->rbuffer_allocated
>= sizeof(struct nlmsghdr
));
327 type_system_root
= type_system_get_root(rtnl
->protocol
);
329 /* read nothing, just get the pending message size */
330 r
= socket_recv_message(rtnl
->fd
, &iov
, NULL
, true);
336 /* make room for the pending message */
337 if (!greedy_realloc((void **)&rtnl
->rbuffer
,
338 &rtnl
->rbuffer_allocated
,
339 len
, sizeof(uint8_t)))
342 iov
.iov_base
= rtnl
->rbuffer
;
343 iov
.iov_len
= rtnl
->rbuffer_allocated
;
345 /* read the pending message */
346 r
= socket_recv_message(rtnl
->fd
, &iov
, &group
, false);
352 if (len
> rtnl
->rbuffer_allocated
)
353 /* message did not fit in read buffer */
356 if (NLMSG_OK(rtnl
->rbuffer
, len
) && rtnl
->rbuffer
->nlmsg_flags
& NLM_F_MULTI
) {
359 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++) {
360 if (rtnl_message_get_serial(rtnl
->rqueue_partial
[i
]) ==
361 rtnl
->rbuffer
->nlmsg_seq
) {
362 first
= rtnl
->rqueue_partial
[i
];
368 for (new_msg
= rtnl
->rbuffer
; NLMSG_OK(new_msg
, len
) && !done
; new_msg
= NLMSG_NEXT(new_msg
, len
)) {
369 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
370 const NLType
*nl_type
;
372 if (!group
&& new_msg
->nlmsg_pid
!= rtnl
->sockaddr
.nl
.nl_pid
)
373 /* not broadcast and not for us */
376 if (new_msg
->nlmsg_type
== NLMSG_NOOP
)
377 /* silently drop noop messages */
380 if (new_msg
->nlmsg_type
== NLMSG_DONE
) {
381 /* finished reading multi-part message */
384 /* if first is not defined, put NLMSG_DONE into the receive queue. */
389 /* check that we support this message type */
390 r
= type_system_get_type(type_system_root
, &nl_type
, new_msg
->nlmsg_type
);
392 if (r
== -EOPNOTSUPP
)
393 log_debug("sd-netlink: ignored message with unknown type: %i",
394 new_msg
->nlmsg_type
);
399 /* check that the size matches the message type */
400 if (new_msg
->nlmsg_len
< NLMSG_LENGTH(type_get_size(nl_type
))) {
401 log_debug("sd-netlink: message larger than expected, dropping");
405 r
= message_new_empty(rtnl
, &m
);
409 m
->broadcast
= !!group
;
411 m
->hdr
= memdup(new_msg
, new_msg
->nlmsg_len
);
415 /* seal and parse the top-level message */
416 r
= sd_netlink_message_rewind(m
);
420 /* push the message onto the multi-part message stack */
427 log_debug("sd-netlink: discarding %zu bytes of incoming message", len
);
432 if (!multi_part
|| done
) {
433 /* we got a complete message, push it on the read queue */
434 r
= rtnl_rqueue_make_room(rtnl
);
438 rtnl
->rqueue
[rtnl
->rqueue_size
++] = TAKE_PTR(first
);
440 if (multi_part
&& (i
< rtnl
->rqueue_partial_size
)) {
441 /* remove the message form the partial read queue */
442 memmove(rtnl
->rqueue_partial
+ i
,rtnl
->rqueue_partial
+ i
+ 1,
443 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_partial_size
- i
- 1));
444 rtnl
->rqueue_partial_size
--;
449 /* we only got a partial multi-part message, push it on the
450 partial read queue */
451 if (i
< rtnl
->rqueue_partial_size
)
452 rtnl
->rqueue_partial
[i
] = TAKE_PTR(first
);
454 r
= rtnl_rqueue_partial_make_room(rtnl
);
458 rtnl
->rqueue_partial
[rtnl
->rqueue_partial_size
++] = TAKE_PTR(first
);