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
) {
275 req
= ordered_set_get(
276 manager
->request_queue
,
278 .type
= REQUEST_TYPE_NEXTHOP
,
279 .userdata
= (void*) &(const NextHop
) { .id
= id
},
280 .hash_func
= (hash_func_t
) nexthop_hash_func
,
281 .compare_func
= (compare_func_t
) nexthop_compare_func
,
291 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
296 assert(link
->manager
);
300 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
302 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
303 * nexthop_section_verify(). */
304 assert(link
->manager
->manage_foreign_nexthops
);
306 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
308 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
309 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
312 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
313 if (nexthop
->ifindex
!= ifindex
)
315 if (nexthop_compare_full(nexthop
, in
) != 0)
318 /* Even if the configuration matches, it may be requested by another [NextHop] section
319 * that has an explicit ID. If so, the request is not the one we are looking for. */
320 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
331 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
332 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
338 r
= nexthop_new(&nexthop
);
344 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
350 nexthop
->manager
= manager
;
359 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
366 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
367 * nexthop_section_verify(). */
368 assert(manager
->manage_foreign_nexthops
);
370 /* Find the lowest unused ID. */
372 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
373 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
375 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
377 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
387 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
388 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
389 struct nexthop_grp
*nhg
;
399 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
400 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
401 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
403 HASHMAP_FOREACH(nhg
, nexthop
->group
)
404 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
406 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
407 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
409 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
410 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
413 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
418 /* link may be NULL. */
420 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
423 r
= sd_netlink_message_get_errno(m
);
424 if (r
< 0 && r
!= -ENOENT
)
425 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
430 int nexthop_remove(NextHop
*nexthop
, Manager
*manager
) {
431 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
436 assert(nexthop
->id
> 0);
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
);
463 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
464 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
468 assert(nexthop
->id
> 0);
469 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
471 assert(link
->manager
);
472 assert(link
->manager
->rtnl
);
473 assert(link
->ifindex
> 0);
476 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
478 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
482 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
486 if (!hashmap_isempty(nexthop
->group
)) {
487 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
488 struct nexthop_grp
*p
, *nhg
;
490 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
495 HASHMAP_FOREACH(nhg
, nexthop
->group
)
498 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
502 } else if (nexthop
->blackhole
) {
503 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
507 assert(nexthop
->ifindex
== link
->ifindex
);
509 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
513 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
514 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
518 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
524 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
527 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
533 r
= sd_netlink_message_get_errno(m
);
534 if (r
< 0 && r
!= -EEXIST
) {
535 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
536 link_enter_failed(link
);
540 if (link
->static_nexthop_messages
== 0) {
541 log_link_debug(link
, "Nexthops set");
542 link
->static_nexthops_configured
= true;
543 link_check_ready(link
);
549 int nexthop_is_ready(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
557 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
560 if (nexthop_get_by_id(manager
, id
, &nexthop
) < 0)
563 if (!nexthop_exists(nexthop
))
578 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
579 struct nexthop_grp
*nhg
;
584 assert(nexthop
->id
> 0);
586 if (!link_is_ready_to_configure(link
, false))
589 if (nexthop_bound_to_link(nexthop
)) {
590 assert(nexthop
->ifindex
== link
->ifindex
);
592 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
593 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
595 if (link
->set_flags_messages
> 0)
597 if (!FLAGS_SET(link
->flags
, IFF_UP
))
601 /* All group members must be configured first. */
602 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
603 r
= nexthop_is_ready(link
->manager
, nhg
->id
, NULL
);
608 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
611 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
617 assert(link
->manager
);
620 if (!nexthop_is_ready_to_configure(link
, nexthop
))
623 r
= nexthop_configure(nexthop
, link
, req
);
625 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
627 nexthop_enter_configuring(nexthop
);
628 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
629 nexthop_enter_configuring(existing
);
634 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
635 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
636 NextHop
*existing
= NULL
;
640 assert(link
->manager
);
642 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
644 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
645 return 0; /* already requested, skipping. */
647 r
= nexthop_dup(nexthop
, &tmp
);
651 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
652 r
= nexthop_acquire_id(link
->manager
, tmp
);
657 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
658 tmp
->id
= existing
->id
;
660 /* Copy state for logging below. */
661 tmp
->state
= existing
->state
;
664 if (nexthop_bound_to_link(tmp
))
665 tmp
->ifindex
= link
->ifindex
;
667 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
668 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
672 nexthop_compare_func
,
673 nexthop_process_request
,
674 &link
->static_nexthop_messages
,
675 static_nexthop_handler
,
680 nexthop_enter_requesting(tmp
);
682 nexthop_enter_requesting(existing
);
688 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
693 assert(link
->network
);
695 link
->static_nexthops_configured
= false;
697 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
698 if (only_ipv4
&& nh
->family
!= AF_INET
)
701 r
= link_request_nexthop(link
, nh
);
703 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
706 if (link
->static_nexthop_messages
== 0) {
707 link
->static_nexthops_configured
= true;
708 link_check_ready(link
);
710 log_link_debug(link
, "Requesting nexthops");
711 link_set_state(link
, LINK_STATE_CONFIGURING
);
717 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
718 assert(assigned_nexthop
);
719 assert(assigned_nexthop
->manager
);
720 assert(requested_nexthop
);
721 assert(requested_nexthop
->network
);
723 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
724 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
725 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
728 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
729 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
730 * future when we support to configure them.*/
732 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
733 * including this nexthop, then the kernel refuses to replace the existing nexthop.
734 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
735 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
736 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
742 static void link_mark_nexthops(Link
*link
, bool foreign
) {
747 assert(link
->manager
);
749 /* First, mark all nexthops. */
750 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
751 /* do not touch nexthop created by the kernel */
752 if (nexthop
->protocol
== RTPROT_KERNEL
)
755 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
756 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
759 /* Ignore nexthops not assigned yet or already removed. */
760 if (!nexthop_exists(nexthop
))
763 /* Ignore nexthops bound to other links. */
764 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
767 nexthop_mark(nexthop
);
770 /* Then, unmark all nexthops requested by active links. */
771 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
772 if (!foreign
&& other
== link
)
775 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
778 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
781 if (nexthop_get(other
, nexthop
, &existing
) < 0)
784 if (!nexthop_can_update(existing
, nexthop
))
787 /* Found matching static configuration. Keep the existing nexthop. */
788 nexthop_unmark(existing
);
793 int link_drop_nexthops(Link
*link
, bool foreign
) {
798 assert(link
->manager
);
800 link_mark_nexthops(link
, foreign
);
802 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
803 if (!nexthop_is_marked(nexthop
))
806 RET_GATHER(r
, nexthop_remove(nexthop
, link
->manager
));
812 void link_foreignize_nexthops(Link
*link
) {
816 assert(link
->manager
);
818 link_mark_nexthops(link
, /* foreign = */ false);
820 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
821 if (!nexthop_is_marked(nexthop
))
824 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
828 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
829 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
834 assert(group
|| size
== 0);
836 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
837 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
838 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
840 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
841 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
842 "rtnl: received nexthop message with invalid alignment, ignoring.");
844 n_group
= size
/ sizeof(struct nexthop_grp
);
845 for (size_t i
= 0; i
< n_group
; i
++) {
846 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
848 if (group
[i
].id
== 0) {
849 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
853 if (group
[i
].weight
> 254) {
854 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
858 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
862 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
866 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
873 hashmap_free_free(nexthop
->group
);
874 nexthop
->group
= TAKE_PTR(h
);
878 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
879 _cleanup_free_
void *raw_group
= NULL
;
880 size_t raw_group_size
;
882 uint32_t id
, ifindex
;
883 NextHop
*nexthop
= NULL
;
892 if (sd_netlink_message_is_error(message
)) {
893 r
= sd_netlink_message_get_errno(message
);
895 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
900 r
= sd_netlink_message_get_type(message
, &type
);
902 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
904 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
905 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
909 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
911 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
914 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
916 } else if (id
== 0) {
917 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
921 (void) nexthop_get_by_id(m
, id
, &nexthop
);
922 (void) nexthop_get_request_by_id(m
, id
, &req
);
924 if (type
== RTM_DELNEXTHOP
) {
926 nexthop_enter_removed(nexthop
);
927 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
928 nexthop_free(nexthop
);
930 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
933 nexthop_enter_removed(req
->userdata
);
938 /* If we did not know the nexthop, then save it. */
940 r
= nexthop_add_new(m
, id
, &nexthop
);
942 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
949 /* Also update information that cannot be obtained through netlink notification. */
950 if (req
&& req
->waiting_reply
) {
951 NextHop
*n
= ASSERT_PTR(req
->userdata
);
953 nexthop
->source
= n
->source
;
956 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
958 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
960 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
962 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
964 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
966 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
968 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
970 nexthop
->group
= hashmap_free_free(nexthop
->group
);
972 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
974 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
976 if (nexthop
->family
!= AF_UNSPEC
) {
977 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
979 nexthop
->gw
= IN_ADDR_NULL
;
981 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
984 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
986 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
988 nexthop
->blackhole
= r
;
990 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
992 nexthop
->ifindex
= 0;
994 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
995 else if (ifindex
> INT32_MAX
)
996 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
998 nexthop
->ifindex
= (int) ifindex
;
1000 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1001 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1002 if (!nexthop_bound_to_link(nexthop
))
1003 nexthop
->ifindex
= 0;
1005 nexthop_enter_configured(nexthop
);
1007 nexthop_enter_configured(req
->userdata
);
1009 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1013 static int nexthop_section_verify(NextHop
*nh
) {
1014 if (section_is_invalid(nh
->section
))
1017 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1018 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1019 "%s: [NextHop] section without specifying Id= is not supported "
1020 "if ManageForeignNextHops=no is set in networkd.conf. "
1021 "Ignoring [NextHop] section from line %u.",
1022 nh
->section
->filename
, nh
->section
->line
);
1024 if (!hashmap_isempty(nh
->group
)) {
1025 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1026 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1027 "%s: nexthop group cannot have gateway address. "
1028 "Ignoring [NextHop] section from line %u.",
1029 nh
->section
->filename
, nh
->section
->line
);
1031 if (nh
->family
!= AF_UNSPEC
)
1032 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1033 "%s: nexthop group cannot have Family= setting. "
1034 "Ignoring [NextHop] section from line %u.",
1035 nh
->section
->filename
, nh
->section
->line
);
1038 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1039 "%s: nexthop group cannot be a blackhole. "
1040 "Ignoring [NextHop] section from line %u.",
1041 nh
->section
->filename
, nh
->section
->line
);
1044 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1045 "%s: nexthop group cannot have on-link flag. "
1046 "Ignoring [NextHop] section from line %u.",
1047 nh
->section
->filename
, nh
->section
->line
);
1048 } else if (nh
->family
== AF_UNSPEC
)
1049 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1050 nh
->family
= AF_INET
;
1052 if (nh
->blackhole
) {
1053 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1054 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1055 "%s: blackhole nexthop cannot have gateway address. "
1056 "Ignoring [NextHop] section from line %u.",
1057 nh
->section
->filename
, nh
->section
->line
);
1060 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1061 "%s: blackhole nexthop cannot have on-link flag. "
1062 "Ignoring [NextHop] section from line %u.",
1063 nh
->section
->filename
, nh
->section
->line
);
1066 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1067 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1068 /* If no address is configured, in most cases the gateway cannot be reachable.
1069 * TODO: we may need to improve the condition above. */
1070 log_warning("%s: Gateway= without static address configured. "
1071 "Enabling OnLink= option.",
1072 nh
->section
->filename
);
1076 if (nh
->onlink
>= 0)
1077 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1082 int network_drop_invalid_nexthops(Network
*network
) {
1083 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1089 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1090 if (nexthop_section_verify(nh
) < 0) {
1098 /* Always use the setting specified later. So, remove the previously assigned setting. */
1099 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1101 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1102 "dropping the nexthop setting specified at line %u.",
1103 dup
->section
->filename
,
1104 nh
->id
, nh
->section
->line
,
1105 dup
->section
->line
, dup
->section
->line
);
1106 /* nexthop_free() will drop the nexthop from nexthops_by_section. */
1110 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1119 int manager_build_nexthop_ids(Manager
*manager
) {
1125 if (!manager
->manage_foreign_nexthops
)
1128 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1130 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1133 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1137 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1146 int config_parse_nexthop_id(
1148 const char *filename
,
1150 const char *section
,
1151 unsigned section_line
,
1158 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1159 Network
*network
= userdata
;
1169 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1173 if (isempty(rvalue
)) {
1179 r
= safe_atou32(rvalue
, &id
);
1181 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1182 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1186 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1187 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1196 int config_parse_nexthop_gateway(
1198 const char *filename
,
1200 const char *section
,
1201 unsigned section_line
,
1208 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1209 Network
*network
= userdata
;
1218 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1222 if (isempty(rvalue
)) {
1223 n
->family
= AF_UNSPEC
;
1224 n
->gw
= IN_ADDR_NULL
;
1230 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1232 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1233 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1241 int config_parse_nexthop_family(
1243 const char *filename
,
1245 const char *section
,
1246 unsigned section_line
,
1253 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1254 Network
*network
= userdata
;
1264 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1268 if (isempty(rvalue
) &&
1269 !in_addr_is_set(n
->family
, &n
->gw
)) {
1270 /* Accept an empty string only when Gateway= is null or not specified. */
1271 n
->family
= AF_UNSPEC
;
1276 a
= nexthop_address_family_from_string(rvalue
);
1278 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1279 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1283 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1284 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1285 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1286 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1287 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1288 "ignoring assignment.", rvalue
);
1293 case ADDRESS_FAMILY_IPV4
:
1294 n
->family
= AF_INET
;
1296 case ADDRESS_FAMILY_IPV6
:
1297 n
->family
= AF_INET6
;
1300 assert_not_reached();
1307 int config_parse_nexthop_onlink(
1309 const char *filename
,
1311 const char *section
,
1312 unsigned section_line
,
1319 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1320 Network
*network
= userdata
;
1329 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1333 r
= parse_tristate(rvalue
, &n
->onlink
);
1335 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1336 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1344 int config_parse_nexthop_blackhole(
1346 const char *filename
,
1348 const char *section
,
1349 unsigned section_line
,
1356 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1357 Network
*network
= userdata
;
1366 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1370 r
= parse_boolean(rvalue
);
1372 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1373 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1383 int config_parse_nexthop_group(
1385 const char *filename
,
1387 const char *section
,
1388 unsigned section_line
,
1395 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1396 Network
*network
= userdata
;
1405 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1409 if (isempty(rvalue
)) {
1410 n
->group
= hashmap_free_free(n
->group
);
1415 for (const char *p
= rvalue
;;) {
1416 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1417 _cleanup_free_
char *word
= NULL
;
1421 r
= extract_first_word(&p
, &word
, NULL
, 0);
1425 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1426 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1432 nhg
= new0(struct nexthop_grp
, 1);
1436 sep
= strchr(word
, ':');
1439 r
= safe_atou32(sep
, &w
);
1441 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1442 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1446 if (w
== 0 || w
> 256) {
1447 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1448 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1452 /* See comments in config_parse_multipath_route(). */
1453 nhg
->weight
= w
- 1;
1456 r
= safe_atou32(word
, &nhg
->id
);
1458 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1459 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1460 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1464 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1465 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1466 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1470 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1474 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1475 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1476 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));