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 static void nexthop_detach_from_group_members(NextHop
*nexthop
) {
23 assert(nexthop
->manager
);
24 assert(nexthop
->id
> 0);
26 struct nexthop_grp
*nhg
;
27 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
30 if (nexthop_get_by_id(nexthop
->manager
, nhg
->id
, &nh
) < 0)
33 set_remove(nh
->nexthops
, UINT32_TO_PTR(nexthop
->id
));
37 static void nexthop_attach_to_group_members(NextHop
*nexthop
) {
41 assert(nexthop
->manager
);
42 assert(nexthop
->id
> 0);
44 struct nexthop_grp
*nhg
;
45 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
48 r
= nexthop_get_by_id(nexthop
->manager
, nhg
->id
, &nh
);
50 if (nexthop
->manager
->manage_foreign_nexthops
)
51 log_debug_errno(r
, "Nexthop (id=%"PRIu32
") has unknown group member (%"PRIu32
"), ignoring.",
52 nexthop
->id
, nhg
->id
);
56 r
= set_ensure_put(&nh
->nexthops
, NULL
, UINT32_TO_PTR(nexthop
->id
));
58 log_debug_errno(r
, "Failed to save nexthop ID (%"PRIu32
") to group member (%"PRIu32
"), ignoring: %m",
59 nexthop
->id
, nhg
->id
);
63 static NextHop
* nexthop_detach_impl(NextHop
*nexthop
) {
65 assert(!nexthop
->manager
|| !nexthop
->network
);
67 if (nexthop
->network
) {
68 assert(nexthop
->section
);
69 ordered_hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
70 nexthop
->network
= NULL
;
74 if (nexthop
->manager
) {
75 assert(nexthop
->id
> 0);
77 nexthop_detach_from_group_members(nexthop
);
79 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
80 nexthop
->manager
= NULL
;
87 static void nexthop_detach(NextHop
*nexthop
) {
88 nexthop_unref(nexthop_detach_impl(nexthop
));
91 static NextHop
* nexthop_free(NextHop
*nexthop
) {
95 nexthop_detach_impl(nexthop
);
97 config_section_free(nexthop
->section
);
98 hashmap_free_free(nexthop
->group
);
99 set_free(nexthop
->nexthops
);
101 return mfree(nexthop
);
104 DEFINE_TRIVIAL_REF_UNREF_FUNC(NextHop
, nexthop
, nexthop_free
);
105 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_unref
);
107 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
111 trivial_compare_func
,
115 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
116 nexthop_section_hash_ops
,
118 config_section_hash_func
,
119 config_section_compare_func
,
123 static int nexthop_new(NextHop
**ret
) {
124 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
126 nexthop
= new(NextHop
, 1);
130 *nexthop
= (NextHop
) {
135 *ret
= TAKE_PTR(nexthop
);
140 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
141 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
142 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
148 assert(section_line
> 0);
150 r
= config_section_new(filename
, section_line
, &n
);
154 nexthop
= ordered_hashmap_get(network
->nexthops_by_section
, n
);
156 *ret
= TAKE_PTR(nexthop
);
160 r
= nexthop_new(&nexthop
);
164 nexthop
->protocol
= RTPROT_STATIC
;
165 nexthop
->network
= network
;
166 nexthop
->section
= TAKE_PTR(n
);
167 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
169 r
= ordered_hashmap_ensure_put(&network
->nexthops_by_section
, &nexthop_section_hash_ops
, nexthop
->section
, nexthop
);
173 *ret
= TAKE_PTR(nexthop
);
177 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
181 siphash24_compress_typesafe(nexthop
->id
, state
);
184 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
188 return CMP(a
->id
, b
->id
);
191 static int nexthop_compare_full(const NextHop
*a
, const NextHop
*b
) {
197 /* This compares detailed configs, except for ID and ifindex. */
199 r
= CMP(a
->protocol
, b
->protocol
);
203 r
= CMP(a
->flags
, b
->flags
);
207 r
= CMP(hashmap_size(a
->group
), hashmap_size(b
->group
));
211 if (!hashmap_isempty(a
->group
)) {
212 struct nexthop_grp
*ga
;
214 HASHMAP_FOREACH(ga
, a
->group
) {
215 struct nexthop_grp
*gb
;
217 gb
= hashmap_get(b
->group
, UINT32_TO_PTR(ga
->id
));
221 r
= CMP(ga
->weight
, gb
->weight
);
227 r
= CMP(a
->blackhole
, b
->blackhole
);
231 r
= CMP(a
->family
, b
->family
);
235 if (IN_SET(a
->family
, AF_INET
, AF_INET6
)) {
236 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
244 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
245 _cleanup_(nexthop_unrefp
) NextHop
*dest
= NULL
;
246 struct nexthop_grp
*nhg
;
252 dest
= newdup(NextHop
, src
, 1);
256 /* clear the reference counter and all pointers */
258 dest
->manager
= NULL
;
259 dest
->network
= NULL
;
260 dest
->section
= NULL
;
263 HASHMAP_FOREACH(nhg
, src
->group
) {
264 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
266 g
= newdup(struct nexthop_grp
, nhg
, 1);
270 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
277 *ret
= TAKE_PTR(dest
);
281 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
283 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
286 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
294 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
303 static int nexthop_get(Link
*link
, const NextHop
*in
, NextHop
**ret
) {
308 assert(link
->manager
);
312 return nexthop_get_by_id(link
->manager
, in
->id
, ret
);
314 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
315 * nexthop_section_verify(). */
316 assert(link
->manager
->manage_foreign_nexthops
);
318 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
320 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
321 if (nexthop
->ifindex
!= ifindex
)
323 if (nexthop_compare_full(nexthop
, in
) != 0)
326 /* Even if the configuration matches, it may be configured with another [NextHop] section
327 * that has an explicit ID. If so, the assigned nexthop is not the one we are looking for. */
328 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
339 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
347 req
= ordered_set_get(
348 manager
->request_queue
,
350 .type
= REQUEST_TYPE_NEXTHOP
,
351 .userdata
= (void*) &(const NextHop
) { .id
= id
},
352 .hash_func
= (hash_func_t
) nexthop_hash_func
,
353 .compare_func
= (compare_func_t
) nexthop_compare_func
,
363 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
368 assert(link
->manager
);
372 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
374 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
375 * nexthop_section_verify(). */
376 assert(link
->manager
->manage_foreign_nexthops
);
378 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
380 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
381 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
384 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
385 if (nexthop
->ifindex
!= ifindex
)
387 if (nexthop_compare_full(nexthop
, in
) != 0)
390 /* Even if the configuration matches, it may be requested by another [NextHop] section
391 * that has an explicit ID. If so, the request is not the one we are looking for. */
392 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
403 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
404 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
410 r
= nexthop_new(&nexthop
);
416 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
422 nexthop
->manager
= manager
;
431 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
438 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
439 * nexthop_section_verify(). */
440 assert(manager
->manage_foreign_nexthops
);
442 /* Find the lowest unused ID. */
444 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
445 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
447 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
449 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
459 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
460 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
461 struct nexthop_grp
*nhg
;
471 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
472 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
473 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
475 HASHMAP_FOREACH(nhg
, nexthop
->group
)
476 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
478 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
479 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
481 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
482 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
485 static int nexthop_remove_dependents(NextHop
*nexthop
, Manager
*manager
) {
491 /* If a nexthop is removed, the kernel silently removes nexthops that depend on the
492 * removed nexthop. Let's remove them for safety (though, they are already removed in the kernel,
493 * hence that should fail), and forget them. */
496 SET_FOREACH(id
, nexthop
->nexthops
) {
499 if (nexthop_get_by_id(manager
, PTR_TO_UINT32(id
), &nh
) < 0)
502 RET_GATHER(r
, nexthop_remove(nh
, manager
));
508 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, RemoveRequest
*rreq
) {
514 Manager
*manager
= ASSERT_PTR(rreq
->manager
);
515 NextHop
*nexthop
= ASSERT_PTR(rreq
->userdata
);
517 r
= sd_netlink_message_get_errno(m
);
519 log_message_full_errno(m
,
520 (r
== -ENOENT
|| !nexthop
->manager
) ? LOG_DEBUG
: LOG_WARNING
,
521 r
, "Could not drop nexthop, ignoring");
523 (void) nexthop_remove_dependents(nexthop
, manager
);
525 if (nexthop
->manager
) {
526 /* If the nexthop cannot be removed, then assume the nexthop is already removed. */
527 log_nexthop_debug(nexthop
, "Forgetting", manager
);
530 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
531 nexthop_enter_removed(req
->userdata
);
533 nexthop_detach(nexthop
);
540 int nexthop_remove(NextHop
*nexthop
, Manager
*manager
) {
541 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
546 assert(nexthop
->id
> 0);
549 /* link may be NULL. */
550 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
552 log_nexthop_debug(nexthop
, "Removing", manager
);
554 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
556 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
558 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
560 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
562 r
= manager_remove_request_add(manager
, nexthop
, nexthop
, manager
->rtnl
, m
, nexthop_remove_handler
);
564 return log_link_error_errno(link
, r
, "Could not queue rtnetlink message: %m");
566 nexthop_enter_removing(nexthop
);
570 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
571 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
575 assert(nexthop
->id
> 0);
576 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
578 assert(link
->manager
);
579 assert(link
->manager
->rtnl
);
580 assert(link
->ifindex
> 0);
583 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
585 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
589 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
593 if (!hashmap_isempty(nexthop
->group
)) {
594 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
595 struct nexthop_grp
*p
, *nhg
;
597 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
602 HASHMAP_FOREACH(nhg
, nexthop
->group
)
605 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
609 } else if (nexthop
->blackhole
) {
610 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
614 assert(nexthop
->ifindex
== link
->ifindex
);
616 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
620 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
621 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
625 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
631 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
634 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
640 r
= sd_netlink_message_get_errno(m
);
641 if (r
< 0 && r
!= -EEXIST
) {
642 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
643 link_enter_failed(link
);
647 if (link
->static_nexthop_messages
== 0) {
648 log_link_debug(link
, "Nexthops set");
649 link
->static_nexthops_configured
= true;
650 link_check_ready(link
);
656 int nexthop_is_ready(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
664 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
667 if (nexthop_get_by_id(manager
, id
, &nexthop
) < 0)
670 if (!nexthop_exists(nexthop
))
685 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
686 struct nexthop_grp
*nhg
;
691 assert(nexthop
->id
> 0);
693 if (!link_is_ready_to_configure(link
, false))
696 if (nexthop_bound_to_link(nexthop
)) {
697 assert(nexthop
->ifindex
== link
->ifindex
);
699 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
700 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
702 if (link
->set_flags_messages
> 0)
704 if (!FLAGS_SET(link
->flags
, IFF_UP
))
708 /* All group members must be configured first. */
709 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
710 r
= nexthop_is_ready(link
->manager
, nhg
->id
, NULL
);
715 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
718 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
724 assert(link
->manager
);
727 if (!nexthop_is_ready_to_configure(link
, nexthop
))
730 r
= nexthop_configure(nexthop
, link
, req
);
732 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
734 nexthop_enter_configuring(nexthop
);
735 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
736 nexthop_enter_configuring(existing
);
741 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
742 _cleanup_(nexthop_unrefp
) NextHop
*tmp
= NULL
;
743 NextHop
*existing
= NULL
;
747 assert(link
->manager
);
749 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
751 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
752 return 0; /* already requested, skipping. */
754 r
= nexthop_dup(nexthop
, &tmp
);
758 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
759 r
= nexthop_acquire_id(link
->manager
, tmp
);
764 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
765 tmp
->id
= existing
->id
;
767 /* Copy state for logging below. */
768 tmp
->state
= existing
->state
;
771 if (nexthop_bound_to_link(tmp
))
772 tmp
->ifindex
= link
->ifindex
;
774 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
775 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
779 nexthop_compare_func
,
780 nexthop_process_request
,
781 &link
->static_nexthop_messages
,
782 static_nexthop_handler
,
787 nexthop_enter_requesting(tmp
);
789 nexthop_enter_requesting(existing
);
795 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
800 assert(link
->network
);
802 link
->static_nexthops_configured
= false;
804 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
805 if (only_ipv4
&& nh
->family
!= AF_INET
)
808 r
= link_request_nexthop(link
, nh
);
810 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
813 if (link
->static_nexthop_messages
== 0) {
814 link
->static_nexthops_configured
= true;
815 link_check_ready(link
);
817 log_link_debug(link
, "Requesting nexthops");
818 link_set_state(link
, LINK_STATE_CONFIGURING
);
824 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
825 assert(assigned_nexthop
);
826 assert(assigned_nexthop
->manager
);
827 assert(requested_nexthop
);
828 assert(requested_nexthop
->network
);
830 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
831 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
832 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
835 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
836 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
837 * future when we support to configure them.*/
839 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
840 * including this nexthop, then the kernel refuses to replace the existing nexthop.
841 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
842 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
843 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
849 static void link_mark_nexthops(Link
*link
, bool foreign
) {
854 assert(link
->manager
);
856 /* First, mark all nexthops. */
857 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
858 /* do not touch nexthop created by the kernel */
859 if (nexthop
->protocol
== RTPROT_KERNEL
)
862 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
863 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
866 /* Ignore nexthops not assigned yet or already removed. */
867 if (!nexthop_exists(nexthop
))
870 /* Ignore nexthops bound to other links. */
871 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
874 nexthop_mark(nexthop
);
877 /* Then, unmark all nexthops requested by active links. */
878 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
879 if (!foreign
&& other
== link
)
882 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
885 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
888 if (nexthop_get(other
, nexthop
, &existing
) < 0)
891 if (!nexthop_can_update(existing
, nexthop
))
894 /* Found matching static configuration. Keep the existing nexthop. */
895 nexthop_unmark(existing
);
900 int link_drop_nexthops(Link
*link
, bool foreign
) {
905 assert(link
->manager
);
907 link_mark_nexthops(link
, foreign
);
909 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
910 if (!nexthop_is_marked(nexthop
))
913 RET_GATHER(r
, nexthop_remove(nexthop
, link
->manager
));
919 void link_foreignize_nexthops(Link
*link
) {
923 assert(link
->manager
);
925 link_mark_nexthops(link
, /* foreign = */ false);
927 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
928 if (!nexthop_is_marked(nexthop
))
931 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
935 static int nexthop_update_group(NextHop
*nexthop
, sd_netlink_message
*message
) {
936 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
937 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
938 size_t size
= 0, n_group
;
944 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &size
, (void**) &group
);
945 if (r
< 0 && r
!= -ENODATA
)
946 return log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
948 nexthop_detach_from_group_members(nexthop
);
950 if (size
% sizeof(struct nexthop_grp
) != 0)
951 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
952 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
954 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
955 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
956 "rtnl: received nexthop message with invalid alignment, ignoring.");
958 n_group
= size
/ sizeof(struct nexthop_grp
);
959 for (size_t i
= 0; i
< n_group
; i
++) {
960 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
962 if (group
[i
].id
== 0) {
963 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
967 if (group
[i
].weight
> 254) {
968 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
972 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
976 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
980 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
987 hashmap_free_free(nexthop
->group
);
988 nexthop
->group
= TAKE_PTR(h
);
990 nexthop_attach_to_group_members(nexthop
);
994 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
996 uint32_t id
, ifindex
;
997 NextHop
*nexthop
= NULL
;
1006 if (sd_netlink_message_is_error(message
)) {
1007 r
= sd_netlink_message_get_errno(message
);
1009 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
1014 r
= sd_netlink_message_get_type(message
, &type
);
1016 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1018 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
1019 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
1023 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
1024 if (r
== -ENODATA
) {
1025 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
1028 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1030 } else if (id
== 0) {
1031 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
1035 (void) nexthop_get_by_id(m
, id
, &nexthop
);
1036 (void) nexthop_get_request_by_id(m
, id
, &req
);
1038 if (type
== RTM_DELNEXTHOP
) {
1040 nexthop_enter_removed(nexthop
);
1041 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
1042 (void) nexthop_remove_dependents(nexthop
, m
);
1043 nexthop_detach(nexthop
);
1045 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
1048 nexthop_enter_removed(req
->userdata
);
1053 /* If we did not know the nexthop, then save it. */
1055 r
= nexthop_add_new(m
, id
, &nexthop
);
1057 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
1064 /* Also update information that cannot be obtained through netlink notification. */
1065 if (req
&& req
->waiting_reply
) {
1066 NextHop
*n
= ASSERT_PTR(req
->userdata
);
1068 nexthop
->source
= n
->source
;
1071 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
1073 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
1075 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
1077 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
1079 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
1081 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
1083 (void) nexthop_update_group(nexthop
, message
);
1085 if (nexthop
->family
!= AF_UNSPEC
) {
1086 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
1088 nexthop
->gw
= IN_ADDR_NULL
;
1090 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1093 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
1095 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
1097 nexthop
->blackhole
= r
;
1099 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
1101 nexthop
->ifindex
= 0;
1103 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1104 else if (ifindex
> INT32_MAX
)
1105 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
1107 nexthop
->ifindex
= (int) ifindex
;
1109 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1110 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1111 if (!nexthop_bound_to_link(nexthop
))
1112 nexthop
->ifindex
= 0;
1114 nexthop_enter_configured(nexthop
);
1116 nexthop_enter_configured(req
->userdata
);
1118 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1122 static int nexthop_section_verify(NextHop
*nh
) {
1123 if (section_is_invalid(nh
->section
))
1126 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1127 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1128 "%s: [NextHop] section without specifying Id= is not supported "
1129 "if ManageForeignNextHops=no is set in networkd.conf. "
1130 "Ignoring [NextHop] section from line %u.",
1131 nh
->section
->filename
, nh
->section
->line
);
1133 if (!hashmap_isempty(nh
->group
)) {
1134 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1135 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1136 "%s: nexthop group cannot have gateway address. "
1137 "Ignoring [NextHop] section from line %u.",
1138 nh
->section
->filename
, nh
->section
->line
);
1140 if (nh
->family
!= AF_UNSPEC
)
1141 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1142 "%s: nexthop group cannot have Family= setting. "
1143 "Ignoring [NextHop] section from line %u.",
1144 nh
->section
->filename
, nh
->section
->line
);
1147 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1148 "%s: nexthop group cannot be a blackhole. "
1149 "Ignoring [NextHop] section from line %u.",
1150 nh
->section
->filename
, nh
->section
->line
);
1153 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1154 "%s: nexthop group cannot have on-link flag. "
1155 "Ignoring [NextHop] section from line %u.",
1156 nh
->section
->filename
, nh
->section
->line
);
1157 } else if (nh
->family
== AF_UNSPEC
)
1158 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1159 nh
->family
= AF_INET
;
1161 if (nh
->blackhole
) {
1162 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1163 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1164 "%s: blackhole nexthop cannot have gateway address. "
1165 "Ignoring [NextHop] section from line %u.",
1166 nh
->section
->filename
, nh
->section
->line
);
1169 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1170 "%s: blackhole nexthop cannot have on-link flag. "
1171 "Ignoring [NextHop] section from line %u.",
1172 nh
->section
->filename
, nh
->section
->line
);
1175 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1176 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1177 /* If no address is configured, in most cases the gateway cannot be reachable.
1178 * TODO: we may need to improve the condition above. */
1179 log_warning("%s: Gateway= without static address configured. "
1180 "Enabling OnLink= option.",
1181 nh
->section
->filename
);
1185 if (nh
->onlink
>= 0)
1186 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1191 int network_drop_invalid_nexthops(Network
*network
) {
1192 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1198 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1199 if (nexthop_section_verify(nh
) < 0) {
1207 /* Always use the setting specified later. So, remove the previously assigned setting. */
1208 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1210 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1211 "dropping the nexthop setting specified at line %u.",
1212 dup
->section
->filename
,
1213 nh
->id
, nh
->section
->line
,
1214 dup
->section
->line
, dup
->section
->line
);
1215 /* nexthop_detach() will drop the nexthop from nexthops_by_section. */
1216 nexthop_detach(dup
);
1219 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1228 int manager_build_nexthop_ids(Manager
*manager
) {
1234 if (!manager
->manage_foreign_nexthops
)
1237 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1239 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1242 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1246 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1255 int config_parse_nexthop_id(
1257 const char *filename
,
1259 const char *section
,
1260 unsigned section_line
,
1267 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1268 Network
*network
= userdata
;
1278 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1282 if (isempty(rvalue
)) {
1288 r
= safe_atou32(rvalue
, &id
);
1290 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1291 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1295 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1296 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1305 int config_parse_nexthop_gateway(
1307 const char *filename
,
1309 const char *section
,
1310 unsigned section_line
,
1317 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1318 Network
*network
= userdata
;
1327 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1331 if (isempty(rvalue
)) {
1332 n
->family
= AF_UNSPEC
;
1333 n
->gw
= IN_ADDR_NULL
;
1339 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1341 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1342 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1350 int config_parse_nexthop_family(
1352 const char *filename
,
1354 const char *section
,
1355 unsigned section_line
,
1362 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1363 Network
*network
= userdata
;
1373 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1377 if (isempty(rvalue
) &&
1378 !in_addr_is_set(n
->family
, &n
->gw
)) {
1379 /* Accept an empty string only when Gateway= is null or not specified. */
1380 n
->family
= AF_UNSPEC
;
1385 a
= nexthop_address_family_from_string(rvalue
);
1387 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1388 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1392 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1393 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1394 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1395 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1396 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1397 "ignoring assignment.", rvalue
);
1402 case ADDRESS_FAMILY_IPV4
:
1403 n
->family
= AF_INET
;
1405 case ADDRESS_FAMILY_IPV6
:
1406 n
->family
= AF_INET6
;
1409 assert_not_reached();
1416 int config_parse_nexthop_onlink(
1418 const char *filename
,
1420 const char *section
,
1421 unsigned section_line
,
1428 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1429 Network
*network
= userdata
;
1438 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1442 r
= parse_tristate(rvalue
, &n
->onlink
);
1444 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1445 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1453 int config_parse_nexthop_blackhole(
1455 const char *filename
,
1457 const char *section
,
1458 unsigned section_line
,
1465 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1466 Network
*network
= userdata
;
1475 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1479 r
= parse_boolean(rvalue
);
1481 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1482 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1492 int config_parse_nexthop_group(
1494 const char *filename
,
1496 const char *section
,
1497 unsigned section_line
,
1504 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1505 Network
*network
= userdata
;
1514 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1518 if (isempty(rvalue
)) {
1519 n
->group
= hashmap_free_free(n
->group
);
1524 for (const char *p
= rvalue
;;) {
1525 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1526 _cleanup_free_
char *word
= NULL
;
1530 r
= extract_first_word(&p
, &word
, NULL
, 0);
1534 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1535 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1541 nhg
= new0(struct nexthop_grp
, 1);
1545 sep
= strchr(word
, ':');
1548 r
= safe_atou32(sep
, &w
);
1550 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1551 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1555 if (w
== 0 || w
> 256) {
1556 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1557 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1561 /* See comments in config_parse_multipath_route(). */
1562 nhg
->weight
= w
- 1;
1565 r
= safe_atou32(word
, &nhg
->id
);
1567 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1568 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1569 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1573 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1574 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1575 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1579 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1583 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1584 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1585 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));