1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
7 #include "sd-netlink.h"
9 #include "alloc-util.h"
11 #include "format-util.h"
14 #include "netlink-internal.h"
15 #include "netlink-types.h"
16 #include "netlink-util.h"
18 #include "socket-util.h"
21 int socket_open(int family
) {
24 fd
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, family
);
28 return fd_move_above_stdio(fd
);
31 static int broadcast_groups_get(sd_netlink
*nl
) {
32 _cleanup_free_
uint32_t *groups
= NULL
;
33 socklen_t len
= 0, old_len
;
40 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, NULL
, &len
);
42 if (errno
== ENOPROTOOPT
) {
43 nl
->broadcast_group_dont_leave
= true;
52 groups
= new0(uint32_t, len
);
58 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, groups
, &len
);
65 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
69 for (i
= 0; i
< len
; i
++) {
70 for (j
= 0; j
< sizeof(uint32_t) * 8; j
++) {
76 if (!(groups
[i
] & offset
))
79 group
= i
* sizeof(uint32_t) * 8 + j
+ 1;
81 r
= hashmap_put(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(1));
90 int socket_bind(sd_netlink
*nl
) {
94 r
= setsockopt_int(nl
->fd
, SOL_NETLINK
, NETLINK_PKTINFO
, true);
98 addrlen
= sizeof(nl
->sockaddr
);
100 r
= bind(nl
->fd
, &nl
->sockaddr
.sa
, addrlen
);
101 /* ignore EINVAL to allow opening an already bound socket */
102 if (r
< 0 && errno
!= EINVAL
)
105 r
= getsockname(nl
->fd
, &nl
->sockaddr
.sa
, &addrlen
);
109 r
= broadcast_groups_get(nl
);
116 static unsigned broadcast_group_get_ref(sd_netlink
*nl
, unsigned group
) {
119 return PTR_TO_UINT(hashmap_get(nl
->broadcast_group_refs
, UINT_TO_PTR(group
)));
122 static int broadcast_group_set_ref(sd_netlink
*nl
, unsigned group
, unsigned n_ref
) {
127 r
= hashmap_replace(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(n_ref
));
134 static int broadcast_group_join(sd_netlink
*nl
, unsigned group
) {
141 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
, &group
, sizeof(group
));
148 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
) {
154 n_ref
= broadcast_group_get_ref(nl
, group
);
158 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
162 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
167 /* not yet in the group */
170 r
= broadcast_group_join(nl
, group
);
177 static int broadcast_group_leave(sd_netlink
*nl
, unsigned group
) {
184 if (nl
->broadcast_group_dont_leave
)
187 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_DROP_MEMBERSHIP
, &group
, sizeof(group
));
194 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
) {
200 n_ref
= broadcast_group_get_ref(nl
, group
);
206 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
211 /* still refs left */
214 r
= broadcast_group_leave(nl
, group
);
221 /* returns the number of bytes sent, or a negative error code */
222 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
) {
225 struct sockaddr_nl nl
;
227 .nl
.nl_family
= AF_NETLINK
,
235 k
= sendto(nl
->fd
, m
->hdr
, m
->hdr
->nlmsg_len
,
236 0, &addr
.sa
, sizeof(addr
));
243 static int socket_recv_message(int fd
, struct iovec
*iov
, uint32_t *_group
, bool peek
) {
244 union sockaddr_union sender
;
245 uint8_t cmsg_buffer
[CMSG_SPACE(sizeof(struct nl_pktinfo
))];
246 struct msghdr msg
= {
250 .msg_namelen
= sizeof(sender
),
251 .msg_control
= cmsg_buffer
,
252 .msg_controllen
= sizeof(cmsg_buffer
),
254 struct cmsghdr
*cmsg
;
261 n
= recvmsg(fd
, &msg
, MSG_TRUNC
| (peek
? MSG_PEEK
: 0));
264 if (errno
== ENOBUFS
)
265 log_debug("rtnl: kernel receive buffer overrun");
266 else if (errno
== EAGAIN
)
267 log_debug("rtnl: no data in socket");
269 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
272 if (sender
.nl
.nl_pid
!= 0) {
273 /* not from the kernel, ignore */
274 log_debug("rtnl: ignoring message from portid %"PRIu32
, sender
.nl
.nl_pid
);
277 /* drop the message */
278 n
= recvmsg(fd
, &msg
, 0);
280 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
286 CMSG_FOREACH(cmsg
, &msg
) {
287 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
288 cmsg
->cmsg_type
== NETLINK_PKTINFO
&&
289 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct nl_pktinfo
))) {
290 struct nl_pktinfo
*pktinfo
= (void *)CMSG_DATA(cmsg
);
292 /* multi-cast group */
293 group
= pktinfo
->group
;
303 /* On success, the number of bytes received is returned and *ret points to the received message
304 * which has a valid header and the correct size.
305 * If nothing useful was received 0 is returned.
306 * On failure, a negative error code is returned.
308 int socket_read_message(sd_netlink
*rtnl
) {
309 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*first
= NULL
;
310 struct iovec iov
= {};
312 bool multi_part
= false, done
= false;
313 struct nlmsghdr
*new_msg
;
317 const NLTypeSystem
*type_system_root
;
320 assert(rtnl
->rbuffer
);
321 assert(rtnl
->rbuffer_allocated
>= sizeof(struct nlmsghdr
));
323 type_system_root
= type_system_get_root(rtnl
->protocol
);
325 /* read nothing, just get the pending message size */
326 r
= socket_recv_message(rtnl
->fd
, &iov
, NULL
, true);
332 /* make room for the pending message */
333 if (!greedy_realloc((void **)&rtnl
->rbuffer
,
334 &rtnl
->rbuffer_allocated
,
335 len
, sizeof(uint8_t)))
338 iov
= IOVEC_MAKE(rtnl
->rbuffer
, rtnl
->rbuffer_allocated
);
340 /* read the pending message */
341 r
= socket_recv_message(rtnl
->fd
, &iov
, &group
, false);
347 if (len
> rtnl
->rbuffer_allocated
)
348 /* message did not fit in read buffer */
351 if (NLMSG_OK(rtnl
->rbuffer
, len
) && rtnl
->rbuffer
->nlmsg_flags
& NLM_F_MULTI
) {
354 for (i
= 0; i
< rtnl
->rqueue_partial_size
; i
++) {
355 if (rtnl_message_get_serial(rtnl
->rqueue_partial
[i
]) ==
356 rtnl
->rbuffer
->nlmsg_seq
) {
357 first
= rtnl
->rqueue_partial
[i
];
363 for (new_msg
= rtnl
->rbuffer
; NLMSG_OK(new_msg
, len
) && !done
; new_msg
= NLMSG_NEXT(new_msg
, len
)) {
364 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
365 const NLType
*nl_type
;
367 if (!group
&& new_msg
->nlmsg_pid
!= rtnl
->sockaddr
.nl
.nl_pid
)
368 /* not broadcast and not for us */
371 if (new_msg
->nlmsg_type
== NLMSG_NOOP
)
372 /* silently drop noop messages */
375 if (new_msg
->nlmsg_type
== NLMSG_DONE
) {
376 /* finished reading multi-part message */
379 /* if first is not defined, put NLMSG_DONE into the receive queue. */
384 /* check that we support this message type */
385 r
= type_system_get_type(type_system_root
, &nl_type
, new_msg
->nlmsg_type
);
387 if (r
== -EOPNOTSUPP
)
388 log_debug("sd-netlink: ignored message with unknown type: %i",
389 new_msg
->nlmsg_type
);
394 /* check that the size matches the message type */
395 if (new_msg
->nlmsg_len
< NLMSG_LENGTH(type_get_size(nl_type
))) {
396 log_debug("sd-netlink: message larger than expected, dropping");
400 r
= message_new_empty(rtnl
, &m
);
404 m
->broadcast
= !!group
;
406 m
->hdr
= memdup(new_msg
, new_msg
->nlmsg_len
);
410 /* seal and parse the top-level message */
411 r
= sd_netlink_message_rewind(m
);
415 /* push the message onto the multi-part message stack */
422 log_debug("sd-netlink: discarding %zu bytes of incoming message", len
);
427 if (!multi_part
|| done
) {
428 /* we got a complete message, push it on the read queue */
429 r
= rtnl_rqueue_make_room(rtnl
);
433 rtnl
->rqueue
[rtnl
->rqueue_size
++] = TAKE_PTR(first
);
435 if (multi_part
&& (i
< rtnl
->rqueue_partial_size
)) {
436 /* remove the message form the partial read queue */
437 memmove(rtnl
->rqueue_partial
+ i
,rtnl
->rqueue_partial
+ i
+ 1,
438 sizeof(sd_netlink_message
*) * (rtnl
->rqueue_partial_size
- i
- 1));
439 rtnl
->rqueue_partial_size
--;
444 /* we only got a partial multi-part message, push it on the
445 partial read queue */
446 if (i
< rtnl
->rqueue_partial_size
)
447 rtnl
->rqueue_partial
[i
] = TAKE_PTR(first
);
449 r
= rtnl_rqueue_partial_make_room(rtnl
);
453 rtnl
->rqueue_partial
[rtnl
->rqueue_partial_size
++] = TAKE_PTR(first
);