1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2019 VMware, Inc.
6 #include <linux/nexthop.h>
8 #include "alloc-util.h"
9 #include "netlink-util.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "networkd-nexthop.h"
14 #include "networkd-queue.h"
15 #include "networkd-route-util.h"
16 #include "parse-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
21 NextHop
*nexthop_free(NextHop
*nexthop
) {
25 if (nexthop
->network
) {
26 assert(nexthop
->section
);
27 hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
30 config_section_free(nexthop
->section
);
33 set_remove(nexthop
->link
->nexthops
, nexthop
);
35 if (nexthop
->link
->manager
&& nexthop
->id
> 0)
36 hashmap_remove(nexthop
->link
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
39 if (nexthop
->manager
) {
40 set_remove(nexthop
->manager
->nexthops
, nexthop
);
43 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
46 hashmap_free_free(nexthop
->group
);
48 return mfree(nexthop
);
51 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_free
);
53 static int nexthop_new(NextHop
**ret
) {
54 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
56 nexthop
= new(NextHop
, 1);
60 *nexthop
= (NextHop
) {
65 *ret
= TAKE_PTR(nexthop
);
70 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
71 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
72 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
78 assert(section_line
> 0);
80 r
= config_section_new(filename
, section_line
, &n
);
84 nexthop
= hashmap_get(network
->nexthops_by_section
, n
);
86 *ret
= TAKE_PTR(nexthop
);
90 r
= nexthop_new(&nexthop
);
94 nexthop
->protocol
= RTPROT_STATIC
;
95 nexthop
->network
= network
;
96 nexthop
->section
= TAKE_PTR(n
);
97 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
99 r
= hashmap_ensure_put(&network
->nexthops_by_section
, &config_section_hash_ops
, nexthop
->section
, nexthop
);
103 *ret
= TAKE_PTR(nexthop
);
107 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
110 siphash24_compress(&nexthop
->protocol
, sizeof(nexthop
->protocol
), state
);
111 siphash24_compress(&nexthop
->id
, sizeof(nexthop
->id
), state
);
112 siphash24_compress(&nexthop
->blackhole
, sizeof(nexthop
->blackhole
), state
);
113 siphash24_compress(&nexthop
->family
, sizeof(nexthop
->family
), state
);
115 switch (nexthop
->family
) {
118 siphash24_compress(&nexthop
->gw
, FAMILY_ADDRESS_SIZE(nexthop
->family
), state
);
122 /* treat any other address family as AF_UNSPEC */
127 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
130 r
= CMP(a
->protocol
, b
->protocol
);
134 r
= CMP(a
->id
, b
->id
);
138 r
= CMP(a
->blackhole
, b
->blackhole
);
142 r
= CMP(a
->family
, b
->family
);
146 if (IN_SET(a
->family
, AF_INET
, AF_INET6
))
147 return memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
152 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
156 nexthop_compare_func
,
159 static bool nexthop_equal(const NextHop
*a
, const NextHop
*b
) {
166 return nexthop_compare_func(a
, b
) == 0;
169 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
170 _cleanup_(nexthop_freep
) NextHop
*dest
= NULL
;
171 struct nexthop_grp
*nhg
;
177 dest
= newdup(NextHop
, src
, 1);
181 /* unset all pointers */
182 dest
->manager
= NULL
;
184 dest
->network
= NULL
;
185 dest
->section
= NULL
;
188 HASHMAP_FOREACH(nhg
, src
->group
) {
189 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
191 g
= newdup(struct nexthop_grp
, nhg
, 1);
195 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
202 *ret
= TAKE_PTR(dest
);
206 int manager_get_nexthop_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
214 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
223 static bool nexthop_owned_by_link(const NextHop
*nexthop
) {
224 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
227 static int nexthop_get(Manager
*manager
, Link
*link
, NextHop
*in
, NextHop
**ret
) {
233 if (nexthop_owned_by_link(in
)) {
237 nexthops
= link
->nexthops
;
242 nexthops
= manager
->nexthops
;
245 nexthop
= set_get(nexthops
, in
);
255 /* Also find nexthop configured without ID. */
256 SET_FOREACH(nexthop
, nexthops
) {
262 found
= nexthop_equal(nexthop
, in
);
276 static int nexthop_add(Manager
*manager
, Link
*link
, NextHop
*nexthop
) {
280 assert(nexthop
->id
> 0);
282 if (nexthop_owned_by_link(nexthop
)) {
285 r
= set_ensure_put(&link
->nexthops
, &nexthop_hash_ops
, nexthop
);
291 nexthop
->link
= link
;
293 manager
= link
->manager
;
297 r
= set_ensure_put(&manager
->nexthops
, &nexthop_hash_ops
, nexthop
);
303 nexthop
->manager
= manager
;
306 return hashmap_ensure_put(&manager
->nexthops_by_id
, NULL
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
309 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
310 _cleanup_set_free_ Set
*ids
= NULL
;
321 /* Find the lowest unused ID. */
323 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
326 HASHMAP_FOREACH(tmp
, network
->nexthops_by_section
) {
330 r
= set_ensure_put(&ids
, NULL
, UINT32_TO_PTR(tmp
->id
));
336 for (id
= 1; id
< UINT32_MAX
; id
++) {
337 if (manager_get_nexthop_by_id(manager
, id
, NULL
) >= 0)
339 if (set_contains(ids
, UINT32_TO_PTR(id
)))
348 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, const Link
*link
) {
349 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
350 struct nexthop_grp
*nhg
;
355 /* link may be NULL. */
360 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
361 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
363 HASHMAP_FOREACH(nhg
, nexthop
->group
)
364 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
366 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
367 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
369 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
370 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
373 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
378 /* link may be NULL. */
380 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
383 r
= sd_netlink_message_get_errno(m
);
384 if (r
< 0 && r
!= -ENOENT
)
385 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
390 static int nexthop_remove(NextHop
*nexthop
) {
391 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
397 assert(nexthop
->manager
|| (nexthop
->link
&& nexthop
->link
->manager
));
399 /* link may be NULL. */
400 link
= nexthop
->link
;
401 manager
= nexthop
->manager
?: nexthop
->link
->manager
;
403 if (nexthop
->id
== 0) {
404 log_link_debug(link
, "Cannot remove nexthop without valid ID, ignoring.");
408 log_nexthop_debug(nexthop
, "Removing", link
);
410 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
412 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
414 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
416 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
418 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
419 link
? link_netlink_destroy_callback
: NULL
, link
);
421 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
423 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
425 nexthop_enter_removing(nexthop
);
429 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
430 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
434 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
436 assert(link
->manager
);
437 assert(link
->manager
->rtnl
);
438 assert(link
->ifindex
> 0);
441 log_nexthop_debug(nexthop
, "Configuring", link
);
443 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
447 if (nexthop
->id
> 0) {
448 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
453 if (!hashmap_isempty(nexthop
->group
)) {
454 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
455 struct nexthop_grp
*p
, *nhg
;
457 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
462 HASHMAP_FOREACH(nhg
, nexthop
->group
)
465 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
469 } else if (nexthop
->blackhole
) {
470 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
474 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, link
->ifindex
);
478 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
479 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
483 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
489 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
492 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
498 r
= sd_netlink_message_get_errno(m
);
499 if (r
< 0 && r
!= -EEXIST
) {
500 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
501 link_enter_failed(link
);
505 if (link
->static_nexthop_messages
== 0) {
506 log_link_debug(link
, "Nexthops set");
507 link
->static_nexthops_configured
= true;
508 link_check_ready(link
);
514 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
515 struct nexthop_grp
*nhg
;
520 if (!link_is_ready_to_configure(link
, false))
523 if (nexthop_owned_by_link(nexthop
)) {
524 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
525 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
527 if (link
->set_flags_messages
> 0)
529 if (!FLAGS_SET(link
->flags
, IFF_UP
))
533 /* All group members must be configured first. */
534 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
537 if (manager_get_nexthop_by_id(link
->manager
, nhg
->id
, &g
) < 0)
540 if (!nexthop_exists(g
))
544 if (nexthop
->id
== 0) {
547 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
548 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
550 if (((NextHop
*) req
->userdata
)->id
!= 0)
551 return false; /* first configure nexthop with id. */
555 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
558 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
565 if (!nexthop_is_ready_to_configure(link
, nexthop
))
568 r
= nexthop_configure(nexthop
, link
, req
);
570 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
572 nexthop_enter_configuring(nexthop
);
576 static int link_request_nexthop(Link
*link
, NextHop
*nexthop
) {
582 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
584 if (nexthop_get(link
->manager
, link
, nexthop
, &existing
) < 0) {
585 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
587 r
= nexthop_dup(nexthop
, &tmp
);
591 r
= nexthop_acquire_id(link
->manager
, tmp
);
595 r
= nexthop_add(link
->manager
, link
, tmp
);
599 existing
= TAKE_PTR(tmp
);
601 existing
->source
= nexthop
->source
;
603 log_nexthop_debug(existing
, "Requesting", link
);
604 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
607 nexthop_compare_func
,
608 nexthop_process_request
,
609 &link
->static_nexthop_messages
,
610 static_nexthop_handler
,
615 nexthop_enter_requesting(existing
);
619 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
624 assert(link
->network
);
626 link
->static_nexthops_configured
= false;
628 HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
629 if (only_ipv4
&& nh
->family
!= AF_INET
)
632 r
= link_request_nexthop(link
, nh
);
634 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
637 if (link
->static_nexthop_messages
== 0) {
638 link
->static_nexthops_configured
= true;
639 link_check_ready(link
);
641 log_link_debug(link
, "Requesting nexthops");
642 link_set_state(link
, LINK_STATE_CONFIGURING
);
648 static void manager_mark_nexthops(Manager
*manager
, bool foreign
, const Link
*except
) {
654 /* First, mark all nexthops. */
655 SET_FOREACH(nexthop
, manager
->nexthops
) {
656 /* do not touch nexthop created by the kernel */
657 if (nexthop
->protocol
== RTPROT_KERNEL
)
660 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
661 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
664 /* Ignore nexthops not assigned yet or already removed. */
665 if (!nexthop_exists(nexthop
))
668 nexthop_mark(nexthop
);
671 /* Then, unmark all nexthops requested by active links. */
672 HASHMAP_FOREACH(link
, manager
->links_by_index
) {
676 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
679 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
682 if (nexthop_get(manager
, NULL
, nexthop
, &existing
) >= 0)
683 nexthop_unmark(existing
);
688 static int manager_drop_marked_nexthops(Manager
*manager
) {
694 SET_FOREACH(nexthop
, manager
->nexthops
) {
695 if (!nexthop_is_marked(nexthop
))
698 RET_GATHER(r
, nexthop_remove(nexthop
));
704 int link_drop_foreign_nexthops(Link
*link
) {
709 assert(link
->manager
);
710 assert(link
->network
);
712 /* First, mark all nexthops. */
713 SET_FOREACH(nexthop
, link
->nexthops
) {
714 /* do not touch nexthop created by the kernel */
715 if (nexthop
->protocol
== RTPROT_KERNEL
)
718 /* Do not remove nexthops we configured. */
719 if (nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
)
722 /* Ignore nexthops not assigned yet or already removed. */
723 if (!nexthop_exists(nexthop
))
726 nexthop_mark(nexthop
);
729 /* Then, unmark all nexthops requested by active links. */
730 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
733 if (nexthop_get(NULL
, link
, nexthop
, &existing
) >= 0)
734 nexthop_unmark(existing
);
737 /* Finally, remove all marked rules. */
738 SET_FOREACH(nexthop
, link
->nexthops
) {
739 if (!nexthop_is_marked(nexthop
))
742 RET_GATHER(r
, nexthop_remove(nexthop
));
745 manager_mark_nexthops(link
->manager
, /* foreign = */ true, NULL
);
747 return RET_GATHER(r
, manager_drop_marked_nexthops(link
->manager
));
750 int link_drop_managed_nexthops(Link
*link
) {
755 assert(link
->manager
);
757 SET_FOREACH(nexthop
, link
->nexthops
) {
758 /* do not touch nexthop created by the kernel */
759 if (nexthop
->protocol
== RTPROT_KERNEL
)
762 /* Do not touch addresses managed by kernel or other tools. */
763 if (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
)
766 /* Ignore nexthops not assigned yet or already removing. */
767 if (!nexthop_exists(nexthop
))
770 RET_GATHER(r
, nexthop_remove(nexthop
));
773 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
775 return RET_GATHER(r
, manager_drop_marked_nexthops(link
->manager
));
778 void link_foreignize_nexthops(Link
*link
) {
783 SET_FOREACH(nexthop
, link
->nexthops
)
784 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
786 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
788 SET_FOREACH(nexthop
, link
->manager
->nexthops
) {
789 if (!nexthop_is_marked(nexthop
))
792 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
796 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
797 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
798 _cleanup_free_
void *raw_group
= NULL
;
799 NextHop
*nexthop
= NULL
;
800 size_t raw_group_size
;
810 if (sd_netlink_message_is_error(message
)) {
811 r
= sd_netlink_message_get_errno(message
);
813 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
818 r
= sd_netlink_message_get_type(message
, &type
);
820 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
822 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
823 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
827 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
828 if (r
< 0 && r
!= -ENODATA
) {
829 log_warning_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
833 log_warning("rtnl: received nexthop message with invalid ifindex %"PRIu32
", ignoring.", ifindex
);
837 r
= link_get_by_index(m
, ifindex
, &link
);
840 log_warning("rtnl: received nexthop message for link (%"PRIu32
") we do not know about, ignoring", ifindex
);
845 r
= nexthop_new(&tmp
);
849 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
851 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop family, ignoring: %m");
853 } else if (!IN_SET(tmp
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
)) {
854 log_link_debug(link
, "rtnl: received nexthop message with invalid family %d, ignoring.", tmp
->family
);
858 r
= sd_rtnl_message_nexthop_get_protocol(message
, &tmp
->protocol
);
860 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop protocol, ignoring: %m");
864 r
= sd_rtnl_message_nexthop_get_flags(message
, &tmp
->flags
);
866 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop flags, ignoring: %m");
870 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
871 if (r
< 0 && r
!= -ENODATA
) {
872 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
875 struct nexthop_grp
*group
= raw_group
;
878 if (raw_group_size
== 0 || raw_group_size
% sizeof(struct nexthop_grp
) != 0) {
879 log_link_warning(link
, "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
883 assert((uintptr_t) group
% alignof(struct nexthop_grp
) == 0);
885 n_group
= raw_group_size
/ sizeof(struct nexthop_grp
);
886 for (size_t i
= 0; i
< n_group
; i
++) {
887 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
889 if (group
[i
].id
== 0) {
890 log_link_warning(link
, "rtnl: received nexthop message with invalid ID in group, ignoring.");
893 if (group
[i
].weight
> 254) {
894 log_link_warning(link
, "rtnl: received nexthop message with invalid weight in group, ignoring.");
898 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
902 r
= hashmap_ensure_put(&tmp
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
906 log_link_warning_errno(link
, r
, "Failed to store nexthop group, ignoring: %m");
914 if (tmp
->family
!= AF_UNSPEC
) {
915 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, tmp
->family
, &tmp
->gw
);
916 if (r
< 0 && r
!= -ENODATA
) {
917 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
922 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
924 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
929 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &tmp
->id
);
931 log_link_warning_errno(link
, r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
934 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
936 } else if (tmp
->id
== 0) {
937 log_link_warning(link
, "rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
941 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
942 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
943 if (!nexthop_owned_by_link(tmp
))
946 (void) nexthop_get(m
, link
, tmp
, &nexthop
);
951 nexthop
->flags
= tmp
->flags
;
952 nexthop_enter_configured(nexthop
);
953 log_nexthop_debug(tmp
, "Received remembered", link
);
955 nexthop_enter_configured(tmp
);
956 log_nexthop_debug(tmp
, "Remembering", link
);
958 r
= nexthop_add(m
, link
, tmp
);
960 log_link_warning_errno(link
, r
, "Could not remember foreign nexthop, ignoring: %m");
970 nexthop_enter_removed(nexthop
);
971 if (nexthop
->state
== 0) {
972 log_nexthop_debug(nexthop
, "Forgetting", link
);
973 nexthop_free(nexthop
);
975 log_nexthop_debug(nexthop
, "Removed", link
);
977 log_nexthop_debug(tmp
, "Kernel removed unknown", link
);
981 assert_not_reached();
987 static int nexthop_section_verify(NextHop
*nh
) {
988 if (section_is_invalid(nh
->section
))
991 if (!hashmap_isempty(nh
->group
)) {
992 if (in_addr_is_set(nh
->family
, &nh
->gw
))
993 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
994 "%s: nexthop group cannot have gateway address. "
995 "Ignoring [NextHop] section from line %u.",
996 nh
->section
->filename
, nh
->section
->line
);
998 if (nh
->family
!= AF_UNSPEC
)
999 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1000 "%s: nexthop group cannot have Family= setting. "
1001 "Ignoring [NextHop] section from line %u.",
1002 nh
->section
->filename
, nh
->section
->line
);
1004 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1005 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1006 "%s: nexthop group cannot be a blackhole. "
1007 "Ignoring [NextHop] section from line %u.",
1008 nh
->section
->filename
, nh
->section
->line
);
1009 } else if (nh
->family
== AF_UNSPEC
)
1010 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1011 nh
->family
= AF_INET
;
1013 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1014 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1015 "%s: blackhole nexthop cannot have gateway address. "
1016 "Ignoring [NextHop] section from line %u.",
1017 nh
->section
->filename
, nh
->section
->line
);
1019 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1020 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1021 /* If no address is configured, in most cases the gateway cannot be reachable.
1022 * TODO: we may need to improve the condition above. */
1023 log_warning("%s: Gateway= without static address configured. "
1024 "Enabling OnLink= option.",
1025 nh
->section
->filename
);
1029 if (nh
->onlink
>= 0)
1030 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1035 void network_drop_invalid_nexthops(Network
*network
) {
1040 HASHMAP_FOREACH(nh
, network
->nexthops_by_section
)
1041 if (nexthop_section_verify(nh
) < 0)
1045 int config_parse_nexthop_id(
1047 const char *filename
,
1049 const char *section
,
1050 unsigned section_line
,
1057 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1058 Network
*network
= userdata
;
1068 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1072 if (isempty(rvalue
)) {
1078 r
= safe_atou32(rvalue
, &id
);
1080 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1081 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1085 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1086 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1095 int config_parse_nexthop_gateway(
1097 const char *filename
,
1099 const char *section
,
1100 unsigned section_line
,
1107 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1108 Network
*network
= userdata
;
1117 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1121 if (isempty(rvalue
)) {
1122 n
->family
= AF_UNSPEC
;
1123 n
->gw
= IN_ADDR_NULL
;
1129 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1131 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1132 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1140 int config_parse_nexthop_family(
1142 const char *filename
,
1144 const char *section
,
1145 unsigned section_line
,
1152 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1153 Network
*network
= userdata
;
1163 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1167 if (isempty(rvalue
) &&
1168 !in_addr_is_set(n
->family
, &n
->gw
)) {
1169 /* Accept an empty string only when Gateway= is null or not specified. */
1170 n
->family
= AF_UNSPEC
;
1175 a
= nexthop_address_family_from_string(rvalue
);
1177 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1178 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1182 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1183 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1184 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1185 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1186 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1187 "ignoring assignment.", rvalue
);
1192 case ADDRESS_FAMILY_IPV4
:
1193 n
->family
= AF_INET
;
1195 case ADDRESS_FAMILY_IPV6
:
1196 n
->family
= AF_INET6
;
1199 assert_not_reached();
1206 int config_parse_nexthop_onlink(
1208 const char *filename
,
1210 const char *section
,
1211 unsigned section_line
,
1218 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1219 Network
*network
= userdata
;
1228 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1232 if (isempty(rvalue
)) {
1238 r
= parse_boolean(rvalue
);
1240 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1241 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1251 int config_parse_nexthop_blackhole(
1253 const char *filename
,
1255 const char *section
,
1256 unsigned section_line
,
1263 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1264 Network
*network
= userdata
;
1273 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1277 r
= parse_boolean(rvalue
);
1279 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1280 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1290 int config_parse_nexthop_group(
1292 const char *filename
,
1294 const char *section
,
1295 unsigned section_line
,
1302 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1303 Network
*network
= userdata
;
1312 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1316 if (isempty(rvalue
)) {
1317 n
->group
= hashmap_free_free(n
->group
);
1322 for (const char *p
= rvalue
;;) {
1323 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1324 _cleanup_free_
char *word
= NULL
;
1328 r
= extract_first_word(&p
, &word
, NULL
, 0);
1332 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1333 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1339 nhg
= new0(struct nexthop_grp
, 1);
1343 sep
= strchr(word
, ':');
1346 r
= safe_atou32(sep
, &w
);
1348 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1349 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1353 if (w
== 0 || w
> 256) {
1354 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1355 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1359 /* See comments in config_parse_multipath_route(). */
1360 nhg
->weight
= w
- 1;
1363 r
= safe_atou32(word
, &nhg
->id
);
1365 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1366 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1367 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1371 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1372 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1373 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1377 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1381 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1382 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1383 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));