]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/rtnl-message.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
4 #include <linux/if_addrlabel.h>
8 #include "sd-netlink.h"
10 #include "format-util.h"
12 #include "netlink-internal.h"
13 #include "netlink-types.h"
14 #include "netlink-util.h"
16 #include "socket-util.h"
19 int sd_rtnl_message_route_set_dst_prefixlen(sd_netlink_message
*m
, unsigned char prefixlen
) {
22 assert_return(m
, -EINVAL
);
23 assert_return(m
->hdr
, -EINVAL
);
24 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
26 rtm
= NLMSG_DATA(m
->hdr
);
28 if ((rtm
->rtm_family
== AF_INET
&& prefixlen
> 32) ||
29 (rtm
->rtm_family
== AF_INET6
&& prefixlen
> 128))
32 rtm
->rtm_dst_len
= prefixlen
;
37 int sd_rtnl_message_route_set_src_prefixlen(sd_netlink_message
*m
, unsigned char prefixlen
) {
40 assert_return(m
, -EINVAL
);
41 assert_return(m
->hdr
, -EINVAL
);
42 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
44 rtm
= NLMSG_DATA(m
->hdr
);
46 if ((rtm
->rtm_family
== AF_INET
&& prefixlen
> 32) ||
47 (rtm
->rtm_family
== AF_INET6
&& prefixlen
> 128))
50 rtm
->rtm_src_len
= prefixlen
;
55 int sd_rtnl_message_route_set_scope(sd_netlink_message
*m
, unsigned char scope
) {
58 assert_return(m
, -EINVAL
);
59 assert_return(m
->hdr
, -EINVAL
);
60 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
62 rtm
= NLMSG_DATA(m
->hdr
);
64 rtm
->rtm_scope
= scope
;
69 int sd_rtnl_message_route_set_flags(sd_netlink_message
*m
, unsigned flags
) {
72 assert_return(m
, -EINVAL
);
73 assert_return(m
->hdr
, -EINVAL
);
74 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
76 rtm
= NLMSG_DATA(m
->hdr
);
78 rtm
->rtm_flags
= flags
;
83 int sd_rtnl_message_route_get_flags(sd_netlink_message
*m
, unsigned *flags
) {
86 assert_return(m
, -EINVAL
);
87 assert_return(m
->hdr
, -EINVAL
);
88 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
89 assert_return(flags
, -EINVAL
);
91 rtm
= NLMSG_DATA(m
->hdr
);
93 *flags
= rtm
->rtm_flags
;
98 int sd_rtnl_message_route_set_table(sd_netlink_message
*m
, unsigned char table
) {
101 assert_return(m
, -EINVAL
);
102 assert_return(m
->hdr
, -EINVAL
);
103 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
105 rtm
= NLMSG_DATA(m
->hdr
);
107 rtm
->rtm_table
= table
;
112 int sd_rtnl_message_route_get_family(sd_netlink_message
*m
, int *family
) {
115 assert_return(m
, -EINVAL
);
116 assert_return(m
->hdr
, -EINVAL
);
117 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
118 assert_return(family
, -EINVAL
);
120 rtm
= NLMSG_DATA(m
->hdr
);
122 *family
= rtm
->rtm_family
;
127 int sd_rtnl_message_route_set_family(sd_netlink_message
*m
, int family
) {
130 assert_return(m
, -EINVAL
);
131 assert_return(m
->hdr
, -EINVAL
);
132 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
134 rtm
= NLMSG_DATA(m
->hdr
);
136 rtm
->rtm_family
= family
;
141 int sd_rtnl_message_route_get_type(sd_netlink_message
*m
, unsigned char *type
) {
144 assert_return(m
, -EINVAL
);
145 assert_return(m
->hdr
, -EINVAL
);
146 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
147 assert_return(type
, -EINVAL
);
149 rtm
= NLMSG_DATA(m
->hdr
);
151 *type
= rtm
->rtm_type
;
156 int sd_rtnl_message_route_set_type(sd_netlink_message
*m
, unsigned char type
) {
159 assert_return(m
, -EINVAL
);
160 assert_return(m
->hdr
, -EINVAL
);
161 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
163 rtm
= NLMSG_DATA(m
->hdr
);
165 rtm
->rtm_type
= type
;
170 int sd_rtnl_message_route_get_protocol(sd_netlink_message
*m
, unsigned char *protocol
) {
173 assert_return(m
, -EINVAL
);
174 assert_return(m
->hdr
, -EINVAL
);
175 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
176 assert_return(protocol
, -EINVAL
);
178 rtm
= NLMSG_DATA(m
->hdr
);
180 *protocol
= rtm
->rtm_protocol
;
185 int sd_rtnl_message_route_get_scope(sd_netlink_message
*m
, unsigned char *scope
) {
188 assert_return(m
, -EINVAL
);
189 assert_return(m
->hdr
, -EINVAL
);
190 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
191 assert_return(scope
, -EINVAL
);
193 rtm
= NLMSG_DATA(m
->hdr
);
195 *scope
= rtm
->rtm_scope
;
200 int sd_rtnl_message_route_get_tos(sd_netlink_message
*m
, unsigned char *tos
) {
203 assert_return(m
, -EINVAL
);
204 assert_return(m
->hdr
, -EINVAL
);
205 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
206 assert_return(tos
, -EINVAL
);
208 rtm
= NLMSG_DATA(m
->hdr
);
215 int sd_rtnl_message_route_get_table(sd_netlink_message
*m
, unsigned char *table
) {
218 assert_return(m
, -EINVAL
);
219 assert_return(m
->hdr
, -EINVAL
);
220 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
221 assert_return(table
, -EINVAL
);
223 rtm
= NLMSG_DATA(m
->hdr
);
225 *table
= rtm
->rtm_table
;
230 int sd_rtnl_message_route_get_dst_prefixlen(sd_netlink_message
*m
, unsigned char *dst_len
) {
233 assert_return(m
, -EINVAL
);
234 assert_return(m
->hdr
, -EINVAL
);
235 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
236 assert_return(dst_len
, -EINVAL
);
238 rtm
= NLMSG_DATA(m
->hdr
);
240 *dst_len
= rtm
->rtm_dst_len
;
245 int sd_rtnl_message_route_get_src_prefixlen(sd_netlink_message
*m
, unsigned char *src_len
) {
248 assert_return(m
, -EINVAL
);
249 assert_return(m
->hdr
, -EINVAL
);
250 assert_return(rtnl_message_type_is_route(m
->hdr
->nlmsg_type
), -EINVAL
);
251 assert_return(src_len
, -EINVAL
);
253 rtm
= NLMSG_DATA(m
->hdr
);
255 *src_len
= rtm
->rtm_src_len
;
260 int sd_rtnl_message_new_route(sd_netlink
*rtnl
, sd_netlink_message
**ret
,
261 uint16_t nlmsg_type
, int rtm_family
,
262 unsigned char rtm_protocol
) {
266 assert_return(rtnl_message_type_is_route(nlmsg_type
), -EINVAL
);
267 assert_return((nlmsg_type
== RTM_GETROUTE
&& rtm_family
== AF_UNSPEC
) ||
268 IN_SET(rtm_family
, AF_INET
, AF_INET6
), -EINVAL
);
269 assert_return(ret
, -EINVAL
);
271 r
= message_new(rtnl
, ret
, nlmsg_type
);
275 if (nlmsg_type
== RTM_NEWROUTE
)
276 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_APPEND
;
278 rtm
= NLMSG_DATA((*ret
)->hdr
);
280 rtm
->rtm_family
= rtm_family
;
281 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
282 rtm
->rtm_type
= RTN_UNICAST
;
283 rtm
->rtm_table
= RT_TABLE_MAIN
;
284 rtm
->rtm_protocol
= rtm_protocol
;
289 int sd_rtnl_message_neigh_set_flags(sd_netlink_message
*m
, uint8_t flags
) {
292 assert_return(m
, -EINVAL
);
293 assert_return(m
->hdr
, -EINVAL
);
294 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
296 ndm
= NLMSG_DATA(m
->hdr
);
297 ndm
->ndm_flags
|= flags
;
302 int sd_rtnl_message_neigh_set_state(sd_netlink_message
*m
, uint16_t state
) {
305 assert_return(m
, -EINVAL
);
306 assert_return(m
->hdr
, -EINVAL
);
307 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
309 ndm
= NLMSG_DATA(m
->hdr
);
310 ndm
->ndm_state
|= state
;
315 int sd_rtnl_message_neigh_get_flags(sd_netlink_message
*m
, uint8_t *flags
) {
318 assert_return(m
, -EINVAL
);
319 assert_return(m
->hdr
, -EINVAL
);
320 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
322 ndm
= NLMSG_DATA(m
->hdr
);
323 *flags
= ndm
->ndm_flags
;
328 int sd_rtnl_message_neigh_get_state(sd_netlink_message
*m
, uint16_t *state
) {
331 assert_return(m
, -EINVAL
);
332 assert_return(m
->hdr
, -EINVAL
);
333 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
335 ndm
= NLMSG_DATA(m
->hdr
);
336 *state
= ndm
->ndm_state
;
341 int sd_rtnl_message_neigh_get_family(sd_netlink_message
*m
, int *family
) {
344 assert_return(m
, -EINVAL
);
345 assert_return(m
->hdr
, -EINVAL
);
346 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
347 assert_return(family
, -EINVAL
);
349 ndm
= NLMSG_DATA(m
->hdr
);
351 *family
= ndm
->ndm_family
;
356 int sd_rtnl_message_neigh_get_ifindex(sd_netlink_message
*m
, int *index
) {
359 assert_return(m
, -EINVAL
);
360 assert_return(m
->hdr
, -EINVAL
);
361 assert_return(rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
), -EINVAL
);
362 assert_return(index
, -EINVAL
);
364 ndm
= NLMSG_DATA(m
->hdr
);
366 *index
= ndm
->ndm_ifindex
;
371 int sd_rtnl_message_new_neigh(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t nlmsg_type
, int index
, int ndm_family
) {
375 assert_return(rtnl_message_type_is_neigh(nlmsg_type
), -EINVAL
);
376 assert_return(IN_SET(ndm_family
, AF_INET
, AF_INET6
, PF_BRIDGE
), -EINVAL
);
377 assert_return(ret
, -EINVAL
);
379 r
= message_new(rtnl
, ret
, nlmsg_type
);
383 if (nlmsg_type
== RTM_NEWNEIGH
)
384 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_APPEND
;
386 ndm
= NLMSG_DATA((*ret
)->hdr
);
388 ndm
->ndm_family
= ndm_family
;
389 ndm
->ndm_ifindex
= index
;
394 int sd_rtnl_message_link_set_flags(sd_netlink_message
*m
, unsigned flags
, unsigned change
) {
395 struct ifinfomsg
*ifi
;
397 assert_return(m
, -EINVAL
);
398 assert_return(m
->hdr
, -EINVAL
);
399 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
400 assert_return(change
, -EINVAL
);
402 ifi
= NLMSG_DATA(m
->hdr
);
404 ifi
->ifi_flags
= flags
;
405 ifi
->ifi_change
= change
;
410 int sd_rtnl_message_link_set_type(sd_netlink_message
*m
, unsigned type
) {
411 struct ifinfomsg
*ifi
;
413 assert_return(m
, -EINVAL
);
414 assert_return(m
->hdr
, -EINVAL
);
415 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
417 ifi
= NLMSG_DATA(m
->hdr
);
419 ifi
->ifi_type
= type
;
424 int sd_rtnl_message_link_set_family(sd_netlink_message
*m
, unsigned family
) {
425 struct ifinfomsg
*ifi
;
427 assert_return(m
, -EINVAL
);
428 assert_return(m
->hdr
, -EINVAL
);
429 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
431 ifi
= NLMSG_DATA(m
->hdr
);
433 ifi
->ifi_family
= family
;
438 int sd_rtnl_message_new_link(sd_netlink
*rtnl
, sd_netlink_message
**ret
,
439 uint16_t nlmsg_type
, int index
) {
440 struct ifinfomsg
*ifi
;
443 assert_return(rtnl_message_type_is_link(nlmsg_type
), -EINVAL
);
444 assert_return(ret
, -EINVAL
);
446 r
= message_new(rtnl
, ret
, nlmsg_type
);
450 if (nlmsg_type
== RTM_NEWLINK
)
451 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
453 ifi
= NLMSG_DATA((*ret
)->hdr
);
455 ifi
->ifi_family
= AF_UNSPEC
;
456 ifi
->ifi_index
= index
;
461 int sd_rtnl_message_addr_set_prefixlen(sd_netlink_message
*m
, unsigned char prefixlen
) {
462 struct ifaddrmsg
*ifa
;
464 assert_return(m
, -EINVAL
);
465 assert_return(m
->hdr
, -EINVAL
);
466 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
468 ifa
= NLMSG_DATA(m
->hdr
);
470 if ((ifa
->ifa_family
== AF_INET
&& prefixlen
> 32) ||
471 (ifa
->ifa_family
== AF_INET6
&& prefixlen
> 128))
474 ifa
->ifa_prefixlen
= prefixlen
;
479 int sd_rtnl_message_addr_set_flags(sd_netlink_message
*m
, unsigned char flags
) {
480 struct ifaddrmsg
*ifa
;
482 assert_return(m
, -EINVAL
);
483 assert_return(m
->hdr
, -EINVAL
);
484 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
486 ifa
= NLMSG_DATA(m
->hdr
);
488 ifa
->ifa_flags
= flags
;
493 int sd_rtnl_message_addr_set_scope(sd_netlink_message
*m
, unsigned char scope
) {
494 struct ifaddrmsg
*ifa
;
496 assert_return(m
, -EINVAL
);
497 assert_return(m
->hdr
, -EINVAL
);
498 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
500 ifa
= NLMSG_DATA(m
->hdr
);
502 ifa
->ifa_scope
= scope
;
507 int sd_rtnl_message_addr_get_family(sd_netlink_message
*m
, int *family
) {
508 struct ifaddrmsg
*ifa
;
510 assert_return(m
, -EINVAL
);
511 assert_return(m
->hdr
, -EINVAL
);
512 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
513 assert_return(family
, -EINVAL
);
515 ifa
= NLMSG_DATA(m
->hdr
);
517 *family
= ifa
->ifa_family
;
522 int sd_rtnl_message_addr_get_prefixlen(sd_netlink_message
*m
, unsigned char *prefixlen
) {
523 struct ifaddrmsg
*ifa
;
525 assert_return(m
, -EINVAL
);
526 assert_return(m
->hdr
, -EINVAL
);
527 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
528 assert_return(prefixlen
, -EINVAL
);
530 ifa
= NLMSG_DATA(m
->hdr
);
532 *prefixlen
= ifa
->ifa_prefixlen
;
537 int sd_rtnl_message_addr_get_scope(sd_netlink_message
*m
, unsigned char *scope
) {
538 struct ifaddrmsg
*ifa
;
540 assert_return(m
, -EINVAL
);
541 assert_return(m
->hdr
, -EINVAL
);
542 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
543 assert_return(scope
, -EINVAL
);
545 ifa
= NLMSG_DATA(m
->hdr
);
547 *scope
= ifa
->ifa_scope
;
552 int sd_rtnl_message_addr_get_flags(sd_netlink_message
*m
, unsigned char *flags
) {
553 struct ifaddrmsg
*ifa
;
555 assert_return(m
, -EINVAL
);
556 assert_return(m
->hdr
, -EINVAL
);
557 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
558 assert_return(flags
, -EINVAL
);
560 ifa
= NLMSG_DATA(m
->hdr
);
562 *flags
= ifa
->ifa_flags
;
567 int sd_rtnl_message_addr_get_ifindex(sd_netlink_message
*m
, int *ifindex
) {
568 struct ifaddrmsg
*ifa
;
570 assert_return(m
, -EINVAL
);
571 assert_return(m
->hdr
, -EINVAL
);
572 assert_return(rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
), -EINVAL
);
573 assert_return(ifindex
, -EINVAL
);
575 ifa
= NLMSG_DATA(m
->hdr
);
577 *ifindex
= ifa
->ifa_index
;
582 int sd_rtnl_message_new_addr(sd_netlink
*rtnl
, sd_netlink_message
**ret
,
583 uint16_t nlmsg_type
, int index
,
585 struct ifaddrmsg
*ifa
;
588 assert_return(rtnl_message_type_is_addr(nlmsg_type
), -EINVAL
);
589 assert_return((nlmsg_type
== RTM_GETADDR
&& index
== 0) ||
591 assert_return((nlmsg_type
== RTM_GETADDR
&& family
== AF_UNSPEC
) ||
592 IN_SET(family
, AF_INET
, AF_INET6
), -EINVAL
);
593 assert_return(ret
, -EINVAL
);
595 r
= message_new(rtnl
, ret
, nlmsg_type
);
599 if (nlmsg_type
== RTM_GETADDR
)
600 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_DUMP
;
602 ifa
= NLMSG_DATA((*ret
)->hdr
);
604 ifa
->ifa_index
= index
;
605 ifa
->ifa_family
= family
;
606 if (family
== AF_INET
)
607 ifa
->ifa_prefixlen
= 32;
608 else if (family
== AF_INET6
)
609 ifa
->ifa_prefixlen
= 128;
614 int sd_rtnl_message_new_addr_update(sd_netlink
*rtnl
, sd_netlink_message
**ret
,
615 int index
, int family
) {
618 r
= sd_rtnl_message_new_addr(rtnl
, ret
, RTM_NEWADDR
, index
, family
);
622 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_REPLACE
;
627 int sd_rtnl_message_link_get_ifindex(sd_netlink_message
*m
, int *ifindex
) {
628 struct ifinfomsg
*ifi
;
630 assert_return(m
, -EINVAL
);
631 assert_return(m
->hdr
, -EINVAL
);
632 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
633 assert_return(ifindex
, -EINVAL
);
635 ifi
= NLMSG_DATA(m
->hdr
);
637 *ifindex
= ifi
->ifi_index
;
642 int sd_rtnl_message_link_get_flags(sd_netlink_message
*m
, unsigned *flags
) {
643 struct ifinfomsg
*ifi
;
645 assert_return(m
, -EINVAL
);
646 assert_return(m
->hdr
, -EINVAL
);
647 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
648 assert_return(flags
, -EINVAL
);
650 ifi
= NLMSG_DATA(m
->hdr
);
652 *flags
= ifi
->ifi_flags
;
657 int sd_rtnl_message_link_get_type(sd_netlink_message
*m
, unsigned short *type
) {
658 struct ifinfomsg
*ifi
;
660 assert_return(m
, -EINVAL
);
661 assert_return(m
->hdr
, -EINVAL
);
662 assert_return(rtnl_message_type_is_link(m
->hdr
->nlmsg_type
), -EINVAL
);
663 assert_return(type
, -EINVAL
);
665 ifi
= NLMSG_DATA(m
->hdr
);
667 *type
= ifi
->ifi_type
;
672 int sd_rtnl_message_get_family(sd_netlink_message
*m
, int *family
) {
673 assert_return(m
, -EINVAL
);
674 assert_return(family
, -EINVAL
);
678 if (rtnl_message_type_is_link(m
->hdr
->nlmsg_type
)) {
679 struct ifinfomsg
*ifi
;
681 ifi
= NLMSG_DATA(m
->hdr
);
683 *family
= ifi
->ifi_family
;
686 } else if (rtnl_message_type_is_route(m
->hdr
->nlmsg_type
)) {
689 rtm
= NLMSG_DATA(m
->hdr
);
691 *family
= rtm
->rtm_family
;
694 } else if (rtnl_message_type_is_neigh(m
->hdr
->nlmsg_type
)) {
697 ndm
= NLMSG_DATA(m
->hdr
);
699 *family
= ndm
->ndm_family
;
702 } else if (rtnl_message_type_is_addr(m
->hdr
->nlmsg_type
)) {
703 struct ifaddrmsg
*ifa
;
705 ifa
= NLMSG_DATA(m
->hdr
);
707 *family
= ifa
->ifa_family
;
710 } else if (rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
)) {
713 rtm
= NLMSG_DATA(m
->hdr
);
715 *family
= rtm
->rtm_family
;
723 int sd_rtnl_message_new_addrlabel(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t nlmsg_type
, int ifindex
, int ifal_family
) {
724 struct ifaddrlblmsg
*addrlabel
;
727 assert_return(rtnl_message_type_is_addrlabel(nlmsg_type
), -EINVAL
);
728 assert_return(ret
, -EINVAL
);
730 r
= message_new(rtnl
, ret
, nlmsg_type
);
734 if (nlmsg_type
== RTM_NEWADDRLABEL
)
735 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
737 addrlabel
= NLMSG_DATA((*ret
)->hdr
);
739 addrlabel
->ifal_family
= ifal_family
;
740 addrlabel
->ifal_index
= ifindex
;
745 int sd_rtnl_message_addrlabel_set_prefixlen(sd_netlink_message
*m
, unsigned char prefixlen
) {
746 struct ifaddrlblmsg
*addrlabel
;
748 assert_return(m
, -EINVAL
);
749 assert_return(m
->hdr
, -EINVAL
);
750 assert_return(rtnl_message_type_is_addrlabel(m
->hdr
->nlmsg_type
), -EINVAL
);
752 addrlabel
= NLMSG_DATA(m
->hdr
);
757 addrlabel
->ifal_prefixlen
= prefixlen
;
762 int sd_rtnl_message_addrlabel_get_prefixlen(sd_netlink_message
*m
, unsigned char *prefixlen
) {
763 struct ifaddrlblmsg
*addrlabel
;
765 assert_return(m
, -EINVAL
);
766 assert_return(m
->hdr
, -EINVAL
);
767 assert_return(rtnl_message_type_is_addrlabel(m
->hdr
->nlmsg_type
), -EINVAL
);
769 addrlabel
= NLMSG_DATA(m
->hdr
);
771 *prefixlen
= addrlabel
->ifal_prefixlen
;
776 int sd_rtnl_message_new_routing_policy_rule(sd_netlink
*rtnl
, sd_netlink_message
**ret
, uint16_t nlmsg_type
, int ifal_family
) {
780 assert_return(rtnl_message_type_is_routing_policy_rule(nlmsg_type
), -EINVAL
);
781 assert_return(ret
, -EINVAL
);
783 r
= message_new(rtnl
, ret
, nlmsg_type
);
787 if (nlmsg_type
== RTM_NEWRULE
)
788 (*ret
)->hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
790 rtm
= NLMSG_DATA((*ret
)->hdr
);
791 rtm
->rtm_family
= ifal_family
;
792 rtm
->rtm_protocol
= RTPROT_BOOT
;
793 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
794 rtm
->rtm_type
= RTN_UNICAST
;
799 int sd_rtnl_message_routing_policy_rule_set_tos(sd_netlink_message
*m
, unsigned char tos
) {
800 struct rtmsg
*routing_policy_rule
;
802 assert_return(m
, -EINVAL
);
803 assert_return(m
->hdr
, -EINVAL
);
804 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
806 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
808 routing_policy_rule
->rtm_tos
= tos
;
813 int sd_rtnl_message_routing_policy_rule_get_tos(sd_netlink_message
*m
, unsigned char *tos
) {
814 struct rtmsg
*routing_policy_rule
;
816 assert_return(m
, -EINVAL
);
817 assert_return(m
->hdr
, -EINVAL
);
818 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
820 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
822 *tos
= routing_policy_rule
->rtm_tos
;
827 int sd_rtnl_message_routing_policy_rule_set_table(sd_netlink_message
*m
, unsigned char table
) {
828 struct rtmsg
*routing_policy_rule
;
830 assert_return(m
, -EINVAL
);
831 assert_return(m
->hdr
, -EINVAL
);
832 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
834 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
836 routing_policy_rule
->rtm_table
= table
;
841 int sd_rtnl_message_routing_policy_rule_get_table(sd_netlink_message
*m
, unsigned char *table
) {
842 struct rtmsg
*routing_policy_rule
;
844 assert_return(m
, -EINVAL
);
845 assert_return(m
->hdr
, -EINVAL
);
846 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
848 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
850 *table
= routing_policy_rule
->rtm_table
;
855 int sd_rtnl_message_routing_policy_rule_set_rtm_type(sd_netlink_message
*m
, unsigned char type
) {
856 struct rtmsg
*routing_policy_rule
;
858 assert_return(m
, -EINVAL
);
859 assert_return(m
->hdr
, -EINVAL
);
860 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
862 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
864 routing_policy_rule
->rtm_type
= type
;
869 int sd_rtnl_message_routing_policy_rule_get_rtm_type(sd_netlink_message
*m
, unsigned char *type
) {
870 struct rtmsg
*routing_policy_rule
;
872 assert_return(m
, -EINVAL
);
873 assert_return(m
->hdr
, -EINVAL
);
874 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
876 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
878 *type
= routing_policy_rule
->rtm_type
;
883 int sd_rtnl_message_routing_policy_rule_set_rtm_dst_prefixlen(sd_netlink_message
*m
, unsigned char len
) {
884 struct rtmsg
*routing_policy_rule
;
886 assert_return(m
, -EINVAL
);
887 assert_return(m
->hdr
, -EINVAL
);
888 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
890 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
892 routing_policy_rule
->rtm_dst_len
= len
;
897 int sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(sd_netlink_message
*m
, unsigned char *len
) {
898 struct rtmsg
*routing_policy_rule
;
900 assert_return(m
, -EINVAL
);
901 assert_return(m
->hdr
, -EINVAL
);
902 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
904 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
906 *len
= routing_policy_rule
->rtm_dst_len
;
911 int sd_rtnl_message_routing_policy_rule_set_rtm_src_prefixlen(sd_netlink_message
*m
, unsigned char len
) {
912 struct rtmsg
*routing_policy_rule
;
914 assert_return(m
, -EINVAL
);
915 assert_return(m
->hdr
, -EINVAL
);
916 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
918 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
920 routing_policy_rule
->rtm_src_len
= len
;
925 int sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(sd_netlink_message
*m
, unsigned char *len
) {
926 struct rtmsg
*routing_policy_rule
;
928 assert_return(m
, -EINVAL
);
929 assert_return(m
->hdr
, -EINVAL
);
930 assert_return(rtnl_message_type_is_routing_policy_rule(m
->hdr
->nlmsg_type
), -EINVAL
);
932 routing_policy_rule
= NLMSG_DATA(m
->hdr
);
934 *len
= routing_policy_rule
->rtm_src_len
;