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 ordered_hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
30 config_section_free(nexthop
->section
);
32 if (nexthop
->manager
) {
33 assert(nexthop
->id
> 0);
34 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
37 hashmap_free_free(nexthop
->group
);
39 return mfree(nexthop
);
42 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_free
);
44 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
52 static int nexthop_new(NextHop
**ret
) {
53 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
55 nexthop
= new(NextHop
, 1);
59 *nexthop
= (NextHop
) {
64 *ret
= TAKE_PTR(nexthop
);
69 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
70 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
71 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
77 assert(section_line
> 0);
79 r
= config_section_new(filename
, section_line
, &n
);
83 nexthop
= ordered_hashmap_get(network
->nexthops_by_section
, n
);
85 *ret
= TAKE_PTR(nexthop
);
89 r
= nexthop_new(&nexthop
);
93 nexthop
->protocol
= RTPROT_STATIC
;
94 nexthop
->network
= network
;
95 nexthop
->section
= TAKE_PTR(n
);
96 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
98 r
= ordered_hashmap_ensure_put(&network
->nexthops_by_section
, &config_section_hash_ops
, nexthop
->section
, nexthop
);
102 *ret
= TAKE_PTR(nexthop
);
106 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
110 siphash24_compress_typesafe(nexthop
->id
, state
);
113 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
117 return CMP(a
->id
, b
->id
);
120 static int nexthop_compare_full(const NextHop
*a
, const NextHop
*b
) {
126 /* This compares detailed configs, except for ID and ifindex. */
128 r
= CMP(a
->protocol
, b
->protocol
);
132 r
= CMP(a
->flags
, b
->flags
);
136 r
= CMP(hashmap_size(a
->group
), hashmap_size(b
->group
));
140 if (!hashmap_isempty(a
->group
)) {
141 struct nexthop_grp
*ga
;
143 HASHMAP_FOREACH(ga
, a
->group
) {
144 struct nexthop_grp
*gb
;
146 gb
= hashmap_get(b
->group
, UINT32_TO_PTR(ga
->id
));
150 r
= CMP(ga
->weight
, gb
->weight
);
156 r
= CMP(a
->blackhole
, b
->blackhole
);
160 r
= CMP(a
->family
, b
->family
);
164 if (IN_SET(a
->family
, AF_INET
, AF_INET6
)) {
165 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
173 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
174 _cleanup_(nexthop_freep
) NextHop
*dest
= NULL
;
175 struct nexthop_grp
*nhg
;
181 dest
= newdup(NextHop
, src
, 1);
185 /* unset all pointers */
186 dest
->manager
= NULL
;
187 dest
->network
= NULL
;
188 dest
->section
= NULL
;
191 HASHMAP_FOREACH(nhg
, src
->group
) {
192 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
194 g
= newdup(struct nexthop_grp
, nhg
, 1);
198 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
205 *ret
= TAKE_PTR(dest
);
209 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
211 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
214 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
222 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
231 static int nexthop_get(Link
*link
, const NextHop
*in
, NextHop
**ret
) {
236 assert(link
->manager
);
240 return nexthop_get_by_id(link
->manager
, in
->id
, ret
);
242 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
243 * nexthop_section_verify(). */
244 assert(link
->manager
->manage_foreign_nexthops
);
246 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
248 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
249 if (nexthop
->ifindex
!= ifindex
)
251 if (nexthop_compare_full(nexthop
, in
) != 0)
254 /* Even if the configuration matches, it may be configured with another [NextHop] section
255 * that has an explicit ID. If so, the assigned nexthop is not the one we are looking for. */
256 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
267 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
272 req
= ordered_set_get(
273 manager
->request_queue
,
275 .type
= REQUEST_TYPE_NEXTHOP
,
276 .userdata
= (void*) &(const NextHop
) { .id
= id
},
277 .hash_func
= (hash_func_t
) nexthop_hash_func
,
278 .compare_func
= (compare_func_t
) nexthop_compare_func
,
288 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
293 assert(link
->manager
);
297 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
299 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
300 * nexthop_section_verify(). */
301 assert(link
->manager
->manage_foreign_nexthops
);
303 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
305 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
306 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
309 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
310 if (nexthop
->ifindex
!= ifindex
)
312 if (nexthop_compare_full(nexthop
, in
) != 0)
315 /* Even if the configuration matches, it may be requested by another [NextHop] section
316 * that has an explicit ID. If so, the request is not the one we are looking for. */
317 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
328 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
329 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
335 r
= nexthop_new(&nexthop
);
341 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
347 nexthop
->manager
= manager
;
356 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
363 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
364 * nexthop_section_verify(). */
365 assert(manager
->manage_foreign_nexthops
);
367 /* Find the lowest unused ID. */
369 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
370 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
372 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
374 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
384 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
385 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
386 struct nexthop_grp
*nhg
;
396 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
397 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
398 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
400 HASHMAP_FOREACH(nhg
, nexthop
->group
)
401 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
403 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
404 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
406 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
407 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
410 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
415 /* link may be NULL. */
417 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
420 r
= sd_netlink_message_get_errno(m
);
421 if (r
< 0 && r
!= -ENOENT
)
422 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
427 static int nexthop_remove(NextHop
*nexthop
) {
428 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
435 assert(nexthop
->id
> 0);
437 manager
= ASSERT_PTR(nexthop
->manager
);
439 /* link may be NULL. */
440 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
442 log_nexthop_debug(nexthop
, "Removing", manager
);
444 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
446 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
448 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
450 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
452 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
453 link
? link_netlink_destroy_callback
: NULL
, link
);
455 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
457 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
459 nexthop_enter_removing(nexthop
);
460 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
461 nexthop_enter_removing(req
->userdata
);
466 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
467 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
471 assert(nexthop
->id
> 0);
472 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
474 assert(link
->manager
);
475 assert(link
->manager
->rtnl
);
476 assert(link
->ifindex
> 0);
479 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
481 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
485 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
489 if (!hashmap_isempty(nexthop
->group
)) {
490 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
491 struct nexthop_grp
*p
, *nhg
;
493 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
498 HASHMAP_FOREACH(nhg
, nexthop
->group
)
501 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
505 } else if (nexthop
->blackhole
) {
506 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
510 assert(nexthop
->ifindex
== link
->ifindex
);
512 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
516 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
517 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
521 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
527 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
530 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
536 r
= sd_netlink_message_get_errno(m
);
537 if (r
< 0 && r
!= -EEXIST
) {
538 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
539 link_enter_failed(link
);
543 if (link
->static_nexthop_messages
== 0) {
544 log_link_debug(link
, "Nexthops set");
545 link
->static_nexthops_configured
= true;
546 link_check_ready(link
);
552 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
553 struct nexthop_grp
*nhg
;
557 assert(nexthop
->id
> 0);
559 if (!link_is_ready_to_configure(link
, false))
562 if (nexthop_bound_to_link(nexthop
)) {
563 assert(nexthop
->ifindex
== link
->ifindex
);
565 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
566 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
568 if (link
->set_flags_messages
> 0)
570 if (!FLAGS_SET(link
->flags
, IFF_UP
))
574 /* All group members must be configured first. */
575 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
578 if (nexthop_get_by_id(link
->manager
, nhg
->id
, &g
) < 0)
581 if (!nexthop_exists(g
))
585 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
588 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
594 assert(link
->manager
);
597 if (!nexthop_is_ready_to_configure(link
, nexthop
))
600 r
= nexthop_configure(nexthop
, link
, req
);
602 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
604 nexthop_enter_configuring(nexthop
);
605 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
606 nexthop_enter_configuring(existing
);
611 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
612 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
613 NextHop
*existing
= NULL
;
617 assert(link
->manager
);
619 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
621 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
622 return 0; /* already requested, skipping. */
624 r
= nexthop_dup(nexthop
, &tmp
);
628 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
629 r
= nexthop_acquire_id(link
->manager
, tmp
);
634 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
635 tmp
->id
= existing
->id
;
637 /* Copy state for logging below. */
638 tmp
->state
= existing
->state
;
641 if (nexthop_bound_to_link(tmp
))
642 tmp
->ifindex
= link
->ifindex
;
644 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
645 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
649 nexthop_compare_func
,
650 nexthop_process_request
,
651 &link
->static_nexthop_messages
,
652 static_nexthop_handler
,
657 nexthop_enter_requesting(tmp
);
659 nexthop_enter_requesting(existing
);
665 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
670 assert(link
->network
);
672 link
->static_nexthops_configured
= false;
674 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
675 if (only_ipv4
&& nh
->family
!= AF_INET
)
678 r
= link_request_nexthop(link
, nh
);
680 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
683 if (link
->static_nexthop_messages
== 0) {
684 link
->static_nexthops_configured
= true;
685 link_check_ready(link
);
687 log_link_debug(link
, "Requesting nexthops");
688 link_set_state(link
, LINK_STATE_CONFIGURING
);
694 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
695 assert(assigned_nexthop
);
696 assert(assigned_nexthop
->manager
);
697 assert(requested_nexthop
);
698 assert(requested_nexthop
->network
);
700 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
701 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
702 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
705 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
706 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
707 * future when we support to configure them.*/
709 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
710 * including this nexthop, then the kernel refuses to replace the existing nexthop.
711 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
712 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
713 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
719 static void link_mark_nexthops(Link
*link
, bool foreign
) {
724 assert(link
->manager
);
726 /* First, mark all nexthops. */
727 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
728 /* do not touch nexthop created by the kernel */
729 if (nexthop
->protocol
== RTPROT_KERNEL
)
732 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
733 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
736 /* Ignore nexthops not assigned yet or already removed. */
737 if (!nexthop_exists(nexthop
))
740 /* Ignore nexthops bound to other links. */
741 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
744 nexthop_mark(nexthop
);
747 /* Then, unmark all nexthops requested by active links. */
748 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
749 if (!foreign
&& other
== link
)
752 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
755 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
758 if (nexthop_get(other
, nexthop
, &existing
) < 0)
761 if (!nexthop_can_update(existing
, nexthop
))
764 /* Found matching static configuration. Keep the existing nexthop. */
765 nexthop_unmark(existing
);
770 int link_drop_nexthops(Link
*link
, bool foreign
) {
775 assert(link
->manager
);
777 link_mark_nexthops(link
, foreign
);
779 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
780 if (!nexthop_is_marked(nexthop
))
783 RET_GATHER(r
, nexthop_remove(nexthop
));
789 void link_foreignize_nexthops(Link
*link
) {
793 assert(link
->manager
);
795 link_mark_nexthops(link
, /* foreign = */ false);
797 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
798 if (!nexthop_is_marked(nexthop
))
801 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
805 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
806 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
811 assert(group
|| size
== 0);
813 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
814 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
815 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
817 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
818 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
819 "rtnl: received nexthop message with invalid alignment, ignoring.");
821 n_group
= size
/ sizeof(struct nexthop_grp
);
822 for (size_t i
= 0; i
< n_group
; i
++) {
823 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
825 if (group
[i
].id
== 0) {
826 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
830 if (group
[i
].weight
> 254) {
831 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
835 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
839 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
843 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
850 hashmap_free_free(nexthop
->group
);
851 nexthop
->group
= TAKE_PTR(h
);
855 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
856 _cleanup_free_
void *raw_group
= NULL
;
857 size_t raw_group_size
;
859 uint32_t id
, ifindex
;
860 NextHop
*nexthop
= NULL
;
869 if (sd_netlink_message_is_error(message
)) {
870 r
= sd_netlink_message_get_errno(message
);
872 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
877 r
= sd_netlink_message_get_type(message
, &type
);
879 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
881 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
882 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
886 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
888 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
891 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
893 } else if (id
== 0) {
894 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
898 (void) nexthop_get_by_id(m
, id
, &nexthop
);
899 (void) nexthop_get_request_by_id(m
, id
, &req
);
901 if (type
== RTM_DELNEXTHOP
) {
903 nexthop_enter_removed(nexthop
);
904 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
905 nexthop_free(nexthop
);
907 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
910 nexthop_enter_removed(req
->userdata
);
915 /* If we did not know the nexthop, then save it. */
917 r
= nexthop_add_new(m
, id
, &nexthop
);
919 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
926 /* Also update information that cannot be obtained through netlink notification. */
927 if (req
&& req
->waiting_reply
) {
928 NextHop
*n
= ASSERT_PTR(req
->userdata
);
930 nexthop
->source
= n
->source
;
933 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
935 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
937 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
939 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
941 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
943 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
945 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
947 nexthop
->group
= hashmap_free_free(nexthop
->group
);
949 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
951 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
953 if (nexthop
->family
!= AF_UNSPEC
) {
954 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
956 nexthop
->gw
= IN_ADDR_NULL
;
958 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
961 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
963 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
965 nexthop
->blackhole
= r
;
967 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
969 nexthop
->ifindex
= 0;
971 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
972 else if (ifindex
> INT32_MAX
)
973 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
975 nexthop
->ifindex
= (int) ifindex
;
977 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
978 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
979 if (!nexthop_bound_to_link(nexthop
))
980 nexthop
->ifindex
= 0;
982 nexthop_enter_configured(nexthop
);
984 nexthop_enter_configured(req
->userdata
);
986 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
990 static int nexthop_section_verify(NextHop
*nh
) {
991 if (section_is_invalid(nh
->section
))
994 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
995 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
996 "%s: [NextHop] section without specifying Id= is not supported "
997 "if ManageForeignNextHops=no is set in networkd.conf. "
998 "Ignoring [NextHop] section from line %u.",
999 nh
->section
->filename
, nh
->section
->line
);
1001 if (!hashmap_isempty(nh
->group
)) {
1002 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1003 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1004 "%s: nexthop group cannot have gateway address. "
1005 "Ignoring [NextHop] section from line %u.",
1006 nh
->section
->filename
, nh
->section
->line
);
1008 if (nh
->family
!= AF_UNSPEC
)
1009 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1010 "%s: nexthop group cannot have Family= setting. "
1011 "Ignoring [NextHop] section from line %u.",
1012 nh
->section
->filename
, nh
->section
->line
);
1015 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1016 "%s: nexthop group cannot be a blackhole. "
1017 "Ignoring [NextHop] section from line %u.",
1018 nh
->section
->filename
, nh
->section
->line
);
1021 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1022 "%s: nexthop group cannot have on-link flag. "
1023 "Ignoring [NextHop] section from line %u.",
1024 nh
->section
->filename
, nh
->section
->line
);
1025 } else if (nh
->family
== AF_UNSPEC
)
1026 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1027 nh
->family
= AF_INET
;
1029 if (nh
->blackhole
) {
1030 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1031 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1032 "%s: blackhole nexthop cannot have gateway address. "
1033 "Ignoring [NextHop] section from line %u.",
1034 nh
->section
->filename
, nh
->section
->line
);
1037 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1038 "%s: blackhole nexthop cannot have on-link flag. "
1039 "Ignoring [NextHop] section from line %u.",
1040 nh
->section
->filename
, nh
->section
->line
);
1043 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1044 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1045 /* If no address is configured, in most cases the gateway cannot be reachable.
1046 * TODO: we may need to improve the condition above. */
1047 log_warning("%s: Gateway= without static address configured. "
1048 "Enabling OnLink= option.",
1049 nh
->section
->filename
);
1053 if (nh
->onlink
>= 0)
1054 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1059 int network_drop_invalid_nexthops(Network
*network
) {
1060 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1066 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1067 if (nexthop_section_verify(nh
) < 0) {
1075 /* Always use the setting specified later. So, remove the previously assigned setting. */
1076 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1078 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1079 "dropping the nexthop setting specified at line %u.",
1080 dup
->section
->filename
,
1081 nh
->id
, nh
->section
->line
,
1082 dup
->section
->line
, dup
->section
->line
);
1083 /* nexthop_free() will drop the nexthop from nexthops_by_section. */
1087 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1096 int manager_build_nexthop_ids(Manager
*manager
) {
1102 if (!manager
->manage_foreign_nexthops
)
1105 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1107 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1110 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1114 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1123 int config_parse_nexthop_id(
1125 const char *filename
,
1127 const char *section
,
1128 unsigned section_line
,
1135 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1136 Network
*network
= userdata
;
1146 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1150 if (isempty(rvalue
)) {
1156 r
= safe_atou32(rvalue
, &id
);
1158 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1159 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1163 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1164 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1173 int config_parse_nexthop_gateway(
1175 const char *filename
,
1177 const char *section
,
1178 unsigned section_line
,
1185 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1186 Network
*network
= userdata
;
1195 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1199 if (isempty(rvalue
)) {
1200 n
->family
= AF_UNSPEC
;
1201 n
->gw
= IN_ADDR_NULL
;
1207 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1209 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1210 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1218 int config_parse_nexthop_family(
1220 const char *filename
,
1222 const char *section
,
1223 unsigned section_line
,
1230 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1231 Network
*network
= userdata
;
1241 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1245 if (isempty(rvalue
) &&
1246 !in_addr_is_set(n
->family
, &n
->gw
)) {
1247 /* Accept an empty string only when Gateway= is null or not specified. */
1248 n
->family
= AF_UNSPEC
;
1253 a
= nexthop_address_family_from_string(rvalue
);
1255 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1256 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1260 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1261 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1262 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1263 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1264 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1265 "ignoring assignment.", rvalue
);
1270 case ADDRESS_FAMILY_IPV4
:
1271 n
->family
= AF_INET
;
1273 case ADDRESS_FAMILY_IPV6
:
1274 n
->family
= AF_INET6
;
1277 assert_not_reached();
1284 int config_parse_nexthop_onlink(
1286 const char *filename
,
1288 const char *section
,
1289 unsigned section_line
,
1296 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1297 Network
*network
= userdata
;
1306 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1310 r
= parse_tristate(rvalue
, &n
->onlink
);
1312 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1313 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1321 int config_parse_nexthop_blackhole(
1323 const char *filename
,
1325 const char *section
,
1326 unsigned section_line
,
1333 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1334 Network
*network
= userdata
;
1343 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1347 r
= parse_boolean(rvalue
);
1349 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1350 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1360 int config_parse_nexthop_group(
1362 const char *filename
,
1364 const char *section
,
1365 unsigned section_line
,
1372 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1373 Network
*network
= userdata
;
1382 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1386 if (isempty(rvalue
)) {
1387 n
->group
= hashmap_free_free(n
->group
);
1392 for (const char *p
= rvalue
;;) {
1393 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1394 _cleanup_free_
char *word
= NULL
;
1398 r
= extract_first_word(&p
, &word
, NULL
, 0);
1402 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1403 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1409 nhg
= new0(struct nexthop_grp
, 1);
1413 sep
= strchr(word
, ':');
1416 r
= safe_atou32(sep
, &w
);
1418 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1419 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1423 if (w
== 0 || w
> 256) {
1424 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1425 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1429 /* See comments in config_parse_multipath_route(). */
1430 nhg
->weight
= w
- 1;
1433 r
= safe_atou32(word
, &nhg
->id
);
1435 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1436 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1437 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1441 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1442 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1443 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1447 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1451 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1452 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1453 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));