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"
13 #include "netlink-internal.h"
14 #include "netlink-types.h"
15 #include "netlink-util.h"
17 #include "socket-util.h"
20 int socket_open(int family
) {
23 fd
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, family
);
27 return fd_move_above_stdio(fd
);
30 static int broadcast_groups_get(sd_netlink
*nl
) {
31 _cleanup_free_
uint32_t *groups
= NULL
;
32 socklen_t len
= 0, old_len
;
39 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, NULL
, &len
);
41 if (errno
== ENOPROTOOPT
) {
42 nl
->broadcast_group_dont_leave
= true;
51 groups
= new0(uint32_t, len
);
57 r
= getsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_LIST_MEMBERSHIPS
, groups
, &len
);
64 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
68 for (i
= 0; i
< len
; i
++) {
69 for (j
= 0; j
< sizeof(uint32_t) * 8; j
++) {
75 if (!(groups
[i
] & offset
))
78 group
= i
* sizeof(uint32_t) * 8 + j
+ 1;
80 r
= hashmap_put(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(1));
89 int socket_bind(sd_netlink
*nl
) {
93 r
= setsockopt_int(nl
->fd
, SOL_NETLINK
, NETLINK_PKTINFO
, true);
97 addrlen
= sizeof(nl
->sockaddr
);
99 r
= bind(nl
->fd
, &nl
->sockaddr
.sa
, addrlen
);
100 /* ignore EINVAL to allow opening an already bound socket */
101 if (r
< 0 && errno
!= EINVAL
)
104 r
= getsockname(nl
->fd
, &nl
->sockaddr
.sa
, &addrlen
);
108 r
= broadcast_groups_get(nl
);
115 static unsigned broadcast_group_get_ref(sd_netlink
*nl
, unsigned group
) {
118 return PTR_TO_UINT(hashmap_get(nl
->broadcast_group_refs
, UINT_TO_PTR(group
)));
121 static int broadcast_group_set_ref(sd_netlink
*nl
, unsigned group
, unsigned n_ref
) {
126 r
= hashmap_replace(nl
->broadcast_group_refs
, UINT_TO_PTR(group
), UINT_TO_PTR(n_ref
));
133 static int broadcast_group_join(sd_netlink
*nl
, unsigned group
) {
140 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_ADD_MEMBERSHIP
, &group
, sizeof(group
));
147 int socket_broadcast_group_ref(sd_netlink
*nl
, unsigned group
) {
153 n_ref
= broadcast_group_get_ref(nl
, group
);
157 r
= hashmap_ensure_allocated(&nl
->broadcast_group_refs
, NULL
);
161 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
166 /* not yet in the group */
169 r
= broadcast_group_join(nl
, group
);
176 static int broadcast_group_leave(sd_netlink
*nl
, unsigned group
) {
183 if (nl
->broadcast_group_dont_leave
)
186 r
= setsockopt(nl
->fd
, SOL_NETLINK
, NETLINK_DROP_MEMBERSHIP
, &group
, sizeof(group
));
193 int socket_broadcast_group_unref(sd_netlink
*nl
, unsigned group
) {
199 n_ref
= broadcast_group_get_ref(nl
, group
);
205 r
= broadcast_group_set_ref(nl
, group
, n_ref
);
210 /* still refs left */
213 r
= broadcast_group_leave(nl
, group
);
220 /* returns the number of bytes sent, or a negative error code */
221 int socket_write_message(sd_netlink
*nl
, sd_netlink_message
*m
) {
224 struct sockaddr_nl nl
;
226 .nl
.nl_family
= AF_NETLINK
,
234 k
= sendto(nl
->fd
, m
->hdr
, m
->hdr
->nlmsg_len
,
235 0, &addr
.sa
, sizeof(addr
));
242 static int socket_recv_message(int fd
, struct iovec
*iov
, uint32_t *_group
, bool peek
) {
243 union sockaddr_union sender
;
244 uint8_t cmsg_buffer
[CMSG_SPACE(sizeof(struct nl_pktinfo
))];
245 struct msghdr msg
= {
249 .msg_namelen
= sizeof(sender
),
250 .msg_control
= cmsg_buffer
,
251 .msg_controllen
= sizeof(cmsg_buffer
),
253 struct cmsghdr
*cmsg
;
260 n
= recvmsg(fd
, &msg
, MSG_TRUNC
| (peek
? MSG_PEEK
: 0));
263 if (errno
== ENOBUFS
)
264 log_debug("rtnl: kernel receive buffer overrun");
265 else if (errno
== EAGAIN
)
266 log_debug("rtnl: no data in socket");
268 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
271 if (sender
.nl
.nl_pid
!= 0) {
272 /* not from the kernel, ignore */
273 log_debug("rtnl: ignoring message from portid %"PRIu32
, sender
.nl
.nl_pid
);
276 /* drop the message */
277 n
= recvmsg(fd
, &msg
, 0);
279 return IN_SET(errno
, EAGAIN
, EINTR
) ? 0 : -errno
;
285 CMSG_FOREACH(cmsg
, &msg
) {
286 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
287 cmsg
->cmsg_type
== NETLINK_PKTINFO
&&
288 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct nl_pktinfo
))) {
289 struct nl_pktinfo
*pktinfo
= (void *)CMSG_DATA(cmsg
);
291 /* multi-cast group */
292 group
= pktinfo
->group
;
302 /* On success, the number of bytes received is returned and *ret points to the received message
303 * which has a valid header and the correct size.
304 * If nothing useful was received 0 is returned.
305 * On failure, a negative error code is returned.
307 int socket_read_message(sd_netlink
*rtnl
) {
308 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*first
= NULL
;
309 struct iovec iov
= {};
311 bool multi_part
= false, done
= false;
312 struct nlmsghdr
*new_msg
;
316 const NLTypeSystem
*type_system_root
;
319 assert(rtnl
->rbuffer
);
320 assert(rtnl
->rbuffer_allocated
>= sizeof(struct nlmsghdr
));
322 type_system_root
= type_system_get_root(rtnl
->protocol
);
324 /* read nothing, just get the pending message size */
325 r
= socket_recv_message(rtnl
->fd
, &iov
, NULL
, true);
331 /* make room for the pending message */
332 if (!greedy_realloc((void **)&rtnl
->rbuffer
,
333 &rtnl
->rbuffer_allocated
,
334 len
, sizeof(uint8_t)))
337 iov
.iov_base
= rtnl
->rbuffer
;
338 iov
.iov_len
= 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
);