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(&nexthop
->id
, sizeof(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)
262 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
267 req
= ordered_set_get(
268 manager
->request_queue
,
270 .type
= REQUEST_TYPE_NEXTHOP
,
271 .userdata
= (void*) &(const NextHop
) { .id
= id
},
272 .hash_func
= (hash_func_t
) nexthop_hash_func
,
273 .compare_func
= (compare_func_t
) nexthop_compare_func
,
283 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
288 assert(link
->manager
);
292 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
294 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
295 * nexthop_section_verify(). */
296 assert(link
->manager
->manage_foreign_nexthops
);
298 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
300 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
301 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
304 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
305 if (nexthop
->ifindex
!= ifindex
)
307 if (nexthop_compare_full(nexthop
, in
) != 0)
318 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
319 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
325 r
= nexthop_new(&nexthop
);
331 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
337 nexthop
->manager
= manager
;
346 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
353 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
354 * nexthop_section_verify(). */
355 assert(manager
->manage_foreign_nexthops
);
357 /* Find the lowest unused ID. */
359 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
360 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
362 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
364 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
374 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
375 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
376 struct nexthop_grp
*nhg
;
386 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
387 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
388 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
390 HASHMAP_FOREACH(nhg
, nexthop
->group
)
391 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
393 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
394 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
396 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
397 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
400 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
405 /* link may be NULL. */
407 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
410 r
= sd_netlink_message_get_errno(m
);
411 if (r
< 0 && r
!= -ENOENT
)
412 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
417 static int nexthop_remove(NextHop
*nexthop
) {
418 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
425 assert(nexthop
->id
> 0);
427 manager
= ASSERT_PTR(nexthop
->manager
);
429 /* link may be NULL. */
430 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
432 log_nexthop_debug(nexthop
, "Removing", manager
);
434 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
436 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
438 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
440 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
442 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
443 link
? link_netlink_destroy_callback
: NULL
, link
);
445 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
447 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
449 nexthop_enter_removing(nexthop
);
450 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
451 nexthop_enter_removing(req
->userdata
);
456 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
457 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
461 assert(nexthop
->id
> 0);
462 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
464 assert(link
->manager
);
465 assert(link
->manager
->rtnl
);
466 assert(link
->ifindex
> 0);
469 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
471 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
475 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
479 if (!hashmap_isempty(nexthop
->group
)) {
480 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
481 struct nexthop_grp
*p
, *nhg
;
483 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
488 HASHMAP_FOREACH(nhg
, nexthop
->group
)
491 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
495 } else if (nexthop
->blackhole
) {
496 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
500 assert(nexthop
->ifindex
== link
->ifindex
);
502 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
506 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
507 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
511 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
517 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
520 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
526 r
= sd_netlink_message_get_errno(m
);
527 if (r
< 0 && r
!= -EEXIST
) {
528 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
529 link_enter_failed(link
);
533 if (link
->static_nexthop_messages
== 0) {
534 log_link_debug(link
, "Nexthops set");
535 link
->static_nexthops_configured
= true;
536 link_check_ready(link
);
542 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
543 struct nexthop_grp
*nhg
;
547 assert(nexthop
->id
> 0);
549 if (!link_is_ready_to_configure(link
, false))
552 if (nexthop_bound_to_link(nexthop
)) {
553 assert(nexthop
->ifindex
== link
->ifindex
);
555 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
556 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
558 if (link
->set_flags_messages
> 0)
560 if (!FLAGS_SET(link
->flags
, IFF_UP
))
564 /* All group members must be configured first. */
565 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
568 if (nexthop_get_by_id(link
->manager
, nhg
->id
, &g
) < 0)
571 if (!nexthop_exists(g
))
575 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
578 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
584 assert(link
->manager
);
587 if (!nexthop_is_ready_to_configure(link
, nexthop
))
590 r
= nexthop_configure(nexthop
, link
, req
);
592 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
594 nexthop_enter_configuring(nexthop
);
595 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
596 nexthop_enter_configuring(existing
);
601 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
602 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
603 NextHop
*existing
= NULL
;
607 assert(link
->manager
);
609 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
611 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
612 return 0; /* already requested, skipping. */
614 r
= nexthop_dup(nexthop
, &tmp
);
618 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
619 r
= nexthop_acquire_id(link
->manager
, tmp
);
624 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
625 tmp
->id
= existing
->id
;
627 /* Copy state for logging below. */
628 tmp
->state
= existing
->state
;
631 if (nexthop_bound_to_link(tmp
))
632 tmp
->ifindex
= link
->ifindex
;
634 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
635 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
639 nexthop_compare_func
,
640 nexthop_process_request
,
641 &link
->static_nexthop_messages
,
642 static_nexthop_handler
,
647 nexthop_enter_requesting(tmp
);
649 nexthop_enter_requesting(existing
);
655 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
660 assert(link
->network
);
662 link
->static_nexthops_configured
= false;
664 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
665 if (only_ipv4
&& nh
->family
!= AF_INET
)
668 r
= link_request_nexthop(link
, nh
);
670 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
673 if (link
->static_nexthop_messages
== 0) {
674 link
->static_nexthops_configured
= true;
675 link_check_ready(link
);
677 log_link_debug(link
, "Requesting nexthops");
678 link_set_state(link
, LINK_STATE_CONFIGURING
);
684 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
685 assert(assigned_nexthop
);
686 assert(assigned_nexthop
->manager
);
687 assert(requested_nexthop
);
688 assert(requested_nexthop
->network
);
690 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
691 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
692 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
695 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
696 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
697 * future when we support to configure them.*/
699 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
700 * including this nexthop, then the kernel refuses to replace the existing nexthop.
701 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
702 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
703 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
709 static void link_mark_nexthops(Link
*link
, bool foreign
) {
714 assert(link
->manager
);
716 /* First, mark all nexthops. */
717 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
718 /* do not touch nexthop created by the kernel */
719 if (nexthop
->protocol
== RTPROT_KERNEL
)
722 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
723 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
726 /* Ignore nexthops not assigned yet or already removed. */
727 if (!nexthop_exists(nexthop
))
730 /* Ignore nexthops bound to other links. */
731 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
734 nexthop_mark(nexthop
);
737 /* Then, unmark all nexthops requested by active links. */
738 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
739 if (!foreign
&& other
== link
)
742 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
745 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
748 if (nexthop_get(other
, nexthop
, &existing
) < 0)
751 if (!nexthop_can_update(existing
, nexthop
))
754 /* Found matching static configuration. Keep the existing nexthop. */
755 nexthop_unmark(existing
);
760 int link_drop_nexthops(Link
*link
, bool foreign
) {
765 assert(link
->manager
);
767 link_mark_nexthops(link
, foreign
);
769 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
770 if (!nexthop_is_marked(nexthop
))
773 RET_GATHER(r
, nexthop_remove(nexthop
));
779 void link_foreignize_nexthops(Link
*link
) {
783 assert(link
->manager
);
785 link_mark_nexthops(link
, /* foreign = */ false);
787 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
788 if (!nexthop_is_marked(nexthop
))
791 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
795 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
796 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
801 assert(group
|| size
== 0);
803 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
804 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
805 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
807 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
808 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
809 "rtnl: received nexthop message with invalid alignment, ignoring.");
811 n_group
= size
/ sizeof(struct nexthop_grp
);
812 for (size_t i
= 0; i
< n_group
; i
++) {
813 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
815 if (group
[i
].id
== 0) {
816 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
820 if (group
[i
].weight
> 254) {
821 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
825 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
829 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
833 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
840 hashmap_free_free(nexthop
->group
);
841 nexthop
->group
= TAKE_PTR(h
);
845 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
846 _cleanup_free_
void *raw_group
= NULL
;
847 size_t raw_group_size
;
849 uint32_t id
, ifindex
;
850 NextHop
*nexthop
= NULL
;
859 if (sd_netlink_message_is_error(message
)) {
860 r
= sd_netlink_message_get_errno(message
);
862 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
867 r
= sd_netlink_message_get_type(message
, &type
);
869 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
871 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
872 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
876 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
878 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
881 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
883 } else if (id
== 0) {
884 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
888 (void) nexthop_get_by_id(m
, id
, &nexthop
);
889 (void) nexthop_get_request_by_id(m
, id
, &req
);
891 if (type
== RTM_DELNEXTHOP
) {
893 nexthop_enter_removed(nexthop
);
894 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
895 nexthop_free(nexthop
);
897 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
900 nexthop_enter_removed(req
->userdata
);
905 /* If we did not know the nexthop, then save it. */
907 r
= nexthop_add_new(m
, id
, &nexthop
);
909 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
916 /* Also update information that cannot be obtained through netlink notification. */
917 if (req
&& req
->waiting_reply
) {
918 NextHop
*n
= ASSERT_PTR(req
->userdata
);
920 nexthop
->source
= n
->source
;
923 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
925 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
927 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
929 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
931 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
933 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
935 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
937 nexthop
->group
= hashmap_free_free(nexthop
->group
);
939 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
941 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
943 if (nexthop
->family
!= AF_UNSPEC
) {
944 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
946 nexthop
->gw
= IN_ADDR_NULL
;
948 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
951 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
953 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
955 nexthop
->blackhole
= r
;
957 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
959 nexthop
->ifindex
= 0;
961 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
962 else if (ifindex
> INT32_MAX
)
963 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
965 nexthop
->ifindex
= (int) ifindex
;
967 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
968 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
969 if (!nexthop_bound_to_link(nexthop
))
970 nexthop
->ifindex
= 0;
972 nexthop_enter_configured(nexthop
);
974 nexthop_enter_configured(req
->userdata
);
976 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
980 static int nexthop_section_verify(NextHop
*nh
) {
981 if (section_is_invalid(nh
->section
))
984 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
985 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
986 "%s: [NextHop] section without specifying Id= is not supported "
987 "if ManageForeignNextHops=no is set in networkd.conf. "
988 "Ignoring [NextHop] section from line %u.",
989 nh
->section
->filename
, nh
->section
->line
);
991 if (!hashmap_isempty(nh
->group
)) {
992 if (in_addr_is_set(nh
->family
, &nh
->gw
))
993 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
994 "%s: nexthop group cannot have gateway address. "
995 "Ignoring [NextHop] section from line %u.",
996 nh
->section
->filename
, nh
->section
->line
);
998 if (nh
->family
!= AF_UNSPEC
)
999 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1000 "%s: nexthop group cannot have Family= setting. "
1001 "Ignoring [NextHop] section from line %u.",
1002 nh
->section
->filename
, nh
->section
->line
);
1005 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1006 "%s: nexthop group cannot be a blackhole. "
1007 "Ignoring [NextHop] section from line %u.",
1008 nh
->section
->filename
, nh
->section
->line
);
1011 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1012 "%s: nexthop group cannot have on-link flag. "
1013 "Ignoring [NextHop] section from line %u.",
1014 nh
->section
->filename
, nh
->section
->line
);
1015 } else if (nh
->family
== AF_UNSPEC
)
1016 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1017 nh
->family
= AF_INET
;
1019 if (nh
->blackhole
) {
1020 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1021 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1022 "%s: blackhole nexthop cannot have gateway address. "
1023 "Ignoring [NextHop] section from line %u.",
1024 nh
->section
->filename
, nh
->section
->line
);
1027 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1028 "%s: blackhole nexthop cannot have on-link flag. "
1029 "Ignoring [NextHop] section from line %u.",
1030 nh
->section
->filename
, nh
->section
->line
);
1033 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1034 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1035 /* If no address is configured, in most cases the gateway cannot be reachable.
1036 * TODO: we may need to improve the condition above. */
1037 log_warning("%s: Gateway= without static address configured. "
1038 "Enabling OnLink= option.",
1039 nh
->section
->filename
);
1043 if (nh
->onlink
>= 0)
1044 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1049 int network_drop_invalid_nexthops(Network
*network
) {
1050 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1056 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1057 if (nexthop_section_verify(nh
) < 0) {
1065 /* Always use the setting specified later. So, remove the previously assigned setting. */
1066 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1068 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1069 "dropping the nexthop setting specified at line %u.",
1070 dup
->section
->filename
,
1071 nh
->id
, nh
->section
->line
,
1072 dup
->section
->line
, dup
->section
->line
);
1073 /* nexthop_free() will drop the nexthop from nexthops_by_section. */
1077 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1086 int manager_build_nexthop_ids(Manager
*manager
) {
1092 if (!manager
->manage_foreign_nexthops
)
1095 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1097 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1100 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1104 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1113 int config_parse_nexthop_id(
1115 const char *filename
,
1117 const char *section
,
1118 unsigned section_line
,
1125 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1126 Network
*network
= userdata
;
1136 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1140 if (isempty(rvalue
)) {
1146 r
= safe_atou32(rvalue
, &id
);
1148 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1149 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1153 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1154 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1163 int config_parse_nexthop_gateway(
1165 const char *filename
,
1167 const char *section
,
1168 unsigned section_line
,
1175 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1176 Network
*network
= userdata
;
1185 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1189 if (isempty(rvalue
)) {
1190 n
->family
= AF_UNSPEC
;
1191 n
->gw
= IN_ADDR_NULL
;
1197 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1199 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1200 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1208 int config_parse_nexthop_family(
1210 const char *filename
,
1212 const char *section
,
1213 unsigned section_line
,
1220 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1221 Network
*network
= userdata
;
1231 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1235 if (isempty(rvalue
) &&
1236 !in_addr_is_set(n
->family
, &n
->gw
)) {
1237 /* Accept an empty string only when Gateway= is null or not specified. */
1238 n
->family
= AF_UNSPEC
;
1243 a
= nexthop_address_family_from_string(rvalue
);
1245 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1246 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1250 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1251 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1252 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1253 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1254 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1255 "ignoring assignment.", rvalue
);
1260 case ADDRESS_FAMILY_IPV4
:
1261 n
->family
= AF_INET
;
1263 case ADDRESS_FAMILY_IPV6
:
1264 n
->family
= AF_INET6
;
1267 assert_not_reached();
1274 int config_parse_nexthop_onlink(
1276 const char *filename
,
1278 const char *section
,
1279 unsigned section_line
,
1286 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1287 Network
*network
= userdata
;
1296 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1300 r
= parse_tristate(rvalue
, &n
->onlink
);
1302 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1303 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1311 int config_parse_nexthop_blackhole(
1313 const char *filename
,
1315 const char *section
,
1316 unsigned section_line
,
1323 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1324 Network
*network
= userdata
;
1333 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1337 r
= parse_boolean(rvalue
);
1339 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1340 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1350 int config_parse_nexthop_group(
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 if (isempty(rvalue
)) {
1377 n
->group
= hashmap_free_free(n
->group
);
1382 for (const char *p
= rvalue
;;) {
1383 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1384 _cleanup_free_
char *word
= NULL
;
1388 r
= extract_first_word(&p
, &word
, NULL
, 0);
1392 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1393 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1399 nhg
= new0(struct nexthop_grp
, 1);
1403 sep
= strchr(word
, ':');
1406 r
= safe_atou32(sep
, &w
);
1408 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1409 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1413 if (w
== 0 || w
> 256) {
1414 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1415 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1419 /* See comments in config_parse_multipath_route(). */
1420 nhg
->weight
= w
- 1;
1423 r
= safe_atou32(word
, &nhg
->id
);
1425 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1426 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1427 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1431 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1432 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1433 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1437 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1441 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1442 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1443 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));