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 static int nexthop_remove(NextHop
*nexthop
) {
431 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
438 assert(nexthop
->id
> 0);
440 manager
= ASSERT_PTR(nexthop
->manager
);
442 /* link may be NULL. */
443 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
445 log_nexthop_debug(nexthop
, "Removing", manager
);
447 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
449 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
451 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
453 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
455 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
456 link
? link_netlink_destroy_callback
: NULL
, link
);
458 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
460 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
462 nexthop_enter_removing(nexthop
);
463 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
464 nexthop_enter_removing(req
->userdata
);
469 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
470 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
474 assert(nexthop
->id
> 0);
475 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
477 assert(link
->manager
);
478 assert(link
->manager
->rtnl
);
479 assert(link
->ifindex
> 0);
482 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
484 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
488 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
492 if (!hashmap_isempty(nexthop
->group
)) {
493 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
494 struct nexthop_grp
*p
, *nhg
;
496 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
501 HASHMAP_FOREACH(nhg
, nexthop
->group
)
504 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
508 } else if (nexthop
->blackhole
) {
509 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
513 assert(nexthop
->ifindex
== link
->ifindex
);
515 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
519 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
520 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
524 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
530 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
533 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
539 r
= sd_netlink_message_get_errno(m
);
540 if (r
< 0 && r
!= -EEXIST
) {
541 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
542 link_enter_failed(link
);
546 if (link
->static_nexthop_messages
== 0) {
547 log_link_debug(link
, "Nexthops set");
548 link
->static_nexthops_configured
= true;
549 link_check_ready(link
);
555 int nexthop_is_ready(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
563 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
566 if (nexthop_get_by_id(manager
, id
, &nexthop
) < 0)
569 if (!nexthop_exists(nexthop
))
584 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
585 struct nexthop_grp
*nhg
;
590 assert(nexthop
->id
> 0);
592 if (!link_is_ready_to_configure(link
, false))
595 if (nexthop_bound_to_link(nexthop
)) {
596 assert(nexthop
->ifindex
== link
->ifindex
);
598 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
599 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
601 if (link
->set_flags_messages
> 0)
603 if (!FLAGS_SET(link
->flags
, IFF_UP
))
607 /* All group members must be configured first. */
608 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
609 r
= nexthop_is_ready(link
->manager
, nhg
->id
, NULL
);
614 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
617 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
623 assert(link
->manager
);
626 if (!nexthop_is_ready_to_configure(link
, nexthop
))
629 r
= nexthop_configure(nexthop
, link
, req
);
631 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
633 nexthop_enter_configuring(nexthop
);
634 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
635 nexthop_enter_configuring(existing
);
640 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
641 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
642 NextHop
*existing
= NULL
;
646 assert(link
->manager
);
648 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
650 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
651 return 0; /* already requested, skipping. */
653 r
= nexthop_dup(nexthop
, &tmp
);
657 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
658 r
= nexthop_acquire_id(link
->manager
, tmp
);
663 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
664 tmp
->id
= existing
->id
;
666 /* Copy state for logging below. */
667 tmp
->state
= existing
->state
;
670 if (nexthop_bound_to_link(tmp
))
671 tmp
->ifindex
= link
->ifindex
;
673 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
674 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
678 nexthop_compare_func
,
679 nexthop_process_request
,
680 &link
->static_nexthop_messages
,
681 static_nexthop_handler
,
686 nexthop_enter_requesting(tmp
);
688 nexthop_enter_requesting(existing
);
694 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
699 assert(link
->network
);
701 link
->static_nexthops_configured
= false;
703 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
704 if (only_ipv4
&& nh
->family
!= AF_INET
)
707 r
= link_request_nexthop(link
, nh
);
709 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
712 if (link
->static_nexthop_messages
== 0) {
713 link
->static_nexthops_configured
= true;
714 link_check_ready(link
);
716 log_link_debug(link
, "Requesting nexthops");
717 link_set_state(link
, LINK_STATE_CONFIGURING
);
723 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
724 assert(assigned_nexthop
);
725 assert(assigned_nexthop
->manager
);
726 assert(requested_nexthop
);
727 assert(requested_nexthop
->network
);
729 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
730 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
731 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
734 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
735 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
736 * future when we support to configure them.*/
738 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
739 * including this nexthop, then the kernel refuses to replace the existing nexthop.
740 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
741 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
742 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
748 static void link_mark_nexthops(Link
*link
, bool foreign
) {
753 assert(link
->manager
);
755 /* First, mark all nexthops. */
756 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
757 /* do not touch nexthop created by the kernel */
758 if (nexthop
->protocol
== RTPROT_KERNEL
)
761 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
762 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
765 /* Ignore nexthops not assigned yet or already removed. */
766 if (!nexthop_exists(nexthop
))
769 /* Ignore nexthops bound to other links. */
770 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
773 nexthop_mark(nexthop
);
776 /* Then, unmark all nexthops requested by active links. */
777 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
778 if (!foreign
&& other
== link
)
781 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
784 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
787 if (nexthop_get(other
, nexthop
, &existing
) < 0)
790 if (!nexthop_can_update(existing
, nexthop
))
793 /* Found matching static configuration. Keep the existing nexthop. */
794 nexthop_unmark(existing
);
799 int link_drop_nexthops(Link
*link
, bool foreign
) {
804 assert(link
->manager
);
806 link_mark_nexthops(link
, foreign
);
808 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
809 if (!nexthop_is_marked(nexthop
))
812 RET_GATHER(r
, nexthop_remove(nexthop
));
818 void link_foreignize_nexthops(Link
*link
) {
822 assert(link
->manager
);
824 link_mark_nexthops(link
, /* foreign = */ false);
826 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
827 if (!nexthop_is_marked(nexthop
))
830 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
834 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
835 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
840 assert(group
|| size
== 0);
842 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
843 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
844 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
846 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
847 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
848 "rtnl: received nexthop message with invalid alignment, ignoring.");
850 n_group
= size
/ sizeof(struct nexthop_grp
);
851 for (size_t i
= 0; i
< n_group
; i
++) {
852 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
854 if (group
[i
].id
== 0) {
855 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
859 if (group
[i
].weight
> 254) {
860 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
864 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
868 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
872 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
879 hashmap_free_free(nexthop
->group
);
880 nexthop
->group
= TAKE_PTR(h
);
884 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
885 _cleanup_free_
void *raw_group
= NULL
;
886 size_t raw_group_size
;
888 uint32_t id
, ifindex
;
889 NextHop
*nexthop
= NULL
;
898 if (sd_netlink_message_is_error(message
)) {
899 r
= sd_netlink_message_get_errno(message
);
901 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
906 r
= sd_netlink_message_get_type(message
, &type
);
908 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
910 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
911 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
915 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
917 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
920 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
922 } else if (id
== 0) {
923 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
927 (void) nexthop_get_by_id(m
, id
, &nexthop
);
928 (void) nexthop_get_request_by_id(m
, id
, &req
);
930 if (type
== RTM_DELNEXTHOP
) {
932 nexthop_enter_removed(nexthop
);
933 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
934 nexthop_free(nexthop
);
936 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
939 nexthop_enter_removed(req
->userdata
);
944 /* If we did not know the nexthop, then save it. */
946 r
= nexthop_add_new(m
, id
, &nexthop
);
948 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
955 /* Also update information that cannot be obtained through netlink notification. */
956 if (req
&& req
->waiting_reply
) {
957 NextHop
*n
= ASSERT_PTR(req
->userdata
);
959 nexthop
->source
= n
->source
;
962 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
964 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
966 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
968 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
970 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
972 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
974 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
976 nexthop
->group
= hashmap_free_free(nexthop
->group
);
978 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
980 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
982 if (nexthop
->family
!= AF_UNSPEC
) {
983 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
985 nexthop
->gw
= IN_ADDR_NULL
;
987 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
990 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
992 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
994 nexthop
->blackhole
= r
;
996 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
998 nexthop
->ifindex
= 0;
1000 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1001 else if (ifindex
> INT32_MAX
)
1002 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
1004 nexthop
->ifindex
= (int) ifindex
;
1006 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1007 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1008 if (!nexthop_bound_to_link(nexthop
))
1009 nexthop
->ifindex
= 0;
1011 nexthop_enter_configured(nexthop
);
1013 nexthop_enter_configured(req
->userdata
);
1015 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1019 static int nexthop_section_verify(NextHop
*nh
) {
1020 if (section_is_invalid(nh
->section
))
1023 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1024 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1025 "%s: [NextHop] section without specifying Id= is not supported "
1026 "if ManageForeignNextHops=no is set in networkd.conf. "
1027 "Ignoring [NextHop] section from line %u.",
1028 nh
->section
->filename
, nh
->section
->line
);
1030 if (!hashmap_isempty(nh
->group
)) {
1031 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1032 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1033 "%s: nexthop group cannot have gateway address. "
1034 "Ignoring [NextHop] section from line %u.",
1035 nh
->section
->filename
, nh
->section
->line
);
1037 if (nh
->family
!= AF_UNSPEC
)
1038 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1039 "%s: nexthop group cannot have Family= setting. "
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 be a blackhole. "
1046 "Ignoring [NextHop] section from line %u.",
1047 nh
->section
->filename
, nh
->section
->line
);
1050 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1051 "%s: nexthop group cannot have on-link flag. "
1052 "Ignoring [NextHop] section from line %u.",
1053 nh
->section
->filename
, nh
->section
->line
);
1054 } else if (nh
->family
== AF_UNSPEC
)
1055 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1056 nh
->family
= AF_INET
;
1058 if (nh
->blackhole
) {
1059 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1060 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1061 "%s: blackhole nexthop cannot have gateway address. "
1062 "Ignoring [NextHop] section from line %u.",
1063 nh
->section
->filename
, nh
->section
->line
);
1066 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1067 "%s: blackhole nexthop cannot have on-link flag. "
1068 "Ignoring [NextHop] section from line %u.",
1069 nh
->section
->filename
, nh
->section
->line
);
1072 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1073 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1074 /* If no address is configured, in most cases the gateway cannot be reachable.
1075 * TODO: we may need to improve the condition above. */
1076 log_warning("%s: Gateway= without static address configured. "
1077 "Enabling OnLink= option.",
1078 nh
->section
->filename
);
1082 if (nh
->onlink
>= 0)
1083 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1088 int network_drop_invalid_nexthops(Network
*network
) {
1089 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1095 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1096 if (nexthop_section_verify(nh
) < 0) {
1104 /* Always use the setting specified later. So, remove the previously assigned setting. */
1105 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1107 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1108 "dropping the nexthop setting specified at line %u.",
1109 dup
->section
->filename
,
1110 nh
->id
, nh
->section
->line
,
1111 dup
->section
->line
, dup
->section
->line
);
1112 /* nexthop_free() will drop the nexthop from nexthops_by_section. */
1116 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1125 int manager_build_nexthop_ids(Manager
*manager
) {
1131 if (!manager
->manage_foreign_nexthops
)
1134 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1136 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1139 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1143 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1152 int config_parse_nexthop_id(
1154 const char *filename
,
1156 const char *section
,
1157 unsigned section_line
,
1164 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1165 Network
*network
= userdata
;
1175 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1179 if (isempty(rvalue
)) {
1185 r
= safe_atou32(rvalue
, &id
);
1187 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1188 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1192 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1193 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1202 int config_parse_nexthop_gateway(
1204 const char *filename
,
1206 const char *section
,
1207 unsigned section_line
,
1214 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1215 Network
*network
= userdata
;
1224 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1228 if (isempty(rvalue
)) {
1229 n
->family
= AF_UNSPEC
;
1230 n
->gw
= IN_ADDR_NULL
;
1236 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1238 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1239 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1247 int config_parse_nexthop_family(
1249 const char *filename
,
1251 const char *section
,
1252 unsigned section_line
,
1259 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1260 Network
*network
= userdata
;
1270 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1274 if (isempty(rvalue
) &&
1275 !in_addr_is_set(n
->family
, &n
->gw
)) {
1276 /* Accept an empty string only when Gateway= is null or not specified. */
1277 n
->family
= AF_UNSPEC
;
1282 a
= nexthop_address_family_from_string(rvalue
);
1284 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1285 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1289 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1290 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1291 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1292 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1293 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1294 "ignoring assignment.", rvalue
);
1299 case ADDRESS_FAMILY_IPV4
:
1300 n
->family
= AF_INET
;
1302 case ADDRESS_FAMILY_IPV6
:
1303 n
->family
= AF_INET6
;
1306 assert_not_reached();
1313 int config_parse_nexthop_onlink(
1315 const char *filename
,
1317 const char *section
,
1318 unsigned section_line
,
1325 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1326 Network
*network
= userdata
;
1335 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1339 r
= parse_tristate(rvalue
, &n
->onlink
);
1341 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1342 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1350 int config_parse_nexthop_blackhole(
1352 const char *filename
,
1354 const char *section
,
1355 unsigned section_line
,
1362 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1363 Network
*network
= userdata
;
1372 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1376 r
= parse_boolean(rvalue
);
1378 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1379 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1389 int config_parse_nexthop_group(
1391 const char *filename
,
1393 const char *section
,
1394 unsigned section_line
,
1401 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1402 Network
*network
= userdata
;
1411 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1415 if (isempty(rvalue
)) {
1416 n
->group
= hashmap_free_free(n
->group
);
1421 for (const char *p
= rvalue
;;) {
1422 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1423 _cleanup_free_
char *word
= NULL
;
1427 r
= extract_first_word(&p
, &word
, NULL
, 0);
1431 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1432 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1438 nhg
= new0(struct nexthop_grp
, 1);
1442 sep
= strchr(word
, ':');
1445 r
= safe_atou32(sep
, &w
);
1447 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1448 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1452 if (w
== 0 || w
> 256) {
1453 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1454 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1458 /* See comments in config_parse_multipath_route(). */
1459 nhg
->weight
= w
- 1;
1462 r
= safe_atou32(word
, &nhg
->id
);
1464 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1465 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1466 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1470 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1471 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1472 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1476 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1480 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1481 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1482 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));