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.h"
16 #include "networkd-route-util.h"
17 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
22 static void nexthop_detach_from_group_members(NextHop
*nexthop
) {
24 assert(nexthop
->manager
);
25 assert(nexthop
->id
> 0);
27 struct nexthop_grp
*nhg
;
28 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
31 if (nexthop_get_by_id(nexthop
->manager
, nhg
->id
, &nh
) < 0)
34 set_remove(nh
->nexthops
, UINT32_TO_PTR(nexthop
->id
));
38 static void nexthop_attach_to_group_members(NextHop
*nexthop
) {
42 assert(nexthop
->manager
);
43 assert(nexthop
->id
> 0);
45 struct nexthop_grp
*nhg
;
46 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
49 r
= nexthop_get_by_id(nexthop
->manager
, nhg
->id
, &nh
);
51 if (nexthop
->manager
->manage_foreign_nexthops
)
52 log_debug_errno(r
, "Nexthop (id=%"PRIu32
") has unknown group member (%"PRIu32
"), ignoring.",
53 nexthop
->id
, nhg
->id
);
57 r
= set_ensure_put(&nh
->nexthops
, NULL
, UINT32_TO_PTR(nexthop
->id
));
59 log_debug_errno(r
, "Failed to save nexthop ID (%"PRIu32
") to group member (%"PRIu32
"), ignoring: %m",
60 nexthop
->id
, nhg
->id
);
64 static NextHop
* nexthop_detach_impl(NextHop
*nexthop
) {
66 assert(!nexthop
->manager
|| !nexthop
->network
);
68 if (nexthop
->network
) {
69 assert(nexthop
->section
);
70 ordered_hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
71 nexthop
->network
= NULL
;
75 if (nexthop
->manager
) {
76 assert(nexthop
->id
> 0);
78 nexthop_detach_from_group_members(nexthop
);
80 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
81 nexthop
->manager
= NULL
;
88 static void nexthop_detach(NextHop
*nexthop
) {
89 nexthop_unref(nexthop_detach_impl(nexthop
));
92 static NextHop
* nexthop_free(NextHop
*nexthop
) {
96 nexthop_detach_impl(nexthop
);
98 config_section_free(nexthop
->section
);
99 hashmap_free_free(nexthop
->group
);
100 set_free(nexthop
->nexthops
);
101 set_free(nexthop
->routes
);
103 return mfree(nexthop
);
106 DEFINE_TRIVIAL_REF_UNREF_FUNC(NextHop
, nexthop
, nexthop_free
);
107 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_unref
);
109 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
113 trivial_compare_func
,
117 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
118 nexthop_section_hash_ops
,
120 config_section_hash_func
,
121 config_section_compare_func
,
125 static int nexthop_new(NextHop
**ret
) {
126 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
128 nexthop
= new(NextHop
, 1);
132 *nexthop
= (NextHop
) {
137 *ret
= TAKE_PTR(nexthop
);
142 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
143 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
144 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
150 assert(section_line
> 0);
152 r
= config_section_new(filename
, section_line
, &n
);
156 nexthop
= ordered_hashmap_get(network
->nexthops_by_section
, n
);
158 *ret
= TAKE_PTR(nexthop
);
162 r
= nexthop_new(&nexthop
);
166 nexthop
->protocol
= RTPROT_STATIC
;
167 nexthop
->network
= network
;
168 nexthop
->section
= TAKE_PTR(n
);
169 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
171 r
= ordered_hashmap_ensure_put(&network
->nexthops_by_section
, &nexthop_section_hash_ops
, nexthop
->section
, nexthop
);
175 *ret
= TAKE_PTR(nexthop
);
179 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
183 siphash24_compress_typesafe(nexthop
->id
, state
);
186 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
190 return CMP(a
->id
, b
->id
);
193 static int nexthop_compare_full(const NextHop
*a
, const NextHop
*b
) {
199 /* This compares detailed configs, except for ID and ifindex. */
201 r
= CMP(a
->protocol
, b
->protocol
);
205 r
= CMP(a
->flags
, b
->flags
);
209 r
= CMP(hashmap_size(a
->group
), hashmap_size(b
->group
));
213 if (!hashmap_isempty(a
->group
)) {
214 struct nexthop_grp
*ga
;
216 HASHMAP_FOREACH(ga
, a
->group
) {
217 struct nexthop_grp
*gb
;
219 gb
= hashmap_get(b
->group
, UINT32_TO_PTR(ga
->id
));
223 r
= CMP(ga
->weight
, gb
->weight
);
229 r
= CMP(a
->blackhole
, b
->blackhole
);
233 r
= CMP(a
->family
, b
->family
);
237 if (IN_SET(a
->family
, AF_INET
, AF_INET6
)) {
238 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
246 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
247 _cleanup_(nexthop_unrefp
) NextHop
*dest
= NULL
;
248 struct nexthop_grp
*nhg
;
254 dest
= newdup(NextHop
, src
, 1);
258 /* clear the reference counter and all pointers */
260 dest
->manager
= NULL
;
261 dest
->network
= NULL
;
262 dest
->section
= NULL
;
265 HASHMAP_FOREACH(nhg
, src
->group
) {
266 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
268 g
= newdup(struct nexthop_grp
, nhg
, 1);
272 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
279 *ret
= TAKE_PTR(dest
);
283 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
285 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
288 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
296 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
305 static int nexthop_get(Link
*link
, const NextHop
*in
, NextHop
**ret
) {
310 assert(link
->manager
);
314 return nexthop_get_by_id(link
->manager
, in
->id
, ret
);
316 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
317 * nexthop_section_verify(). */
318 assert(link
->manager
->manage_foreign_nexthops
);
320 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
322 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
323 if (nexthop
->ifindex
!= ifindex
)
325 if (nexthop_compare_full(nexthop
, in
) != 0)
328 /* Even if the configuration matches, it may be configured with another [NextHop] section
329 * that has an explicit ID. If so, the assigned nexthop is not the one we are looking for. */
330 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
341 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
349 req
= ordered_set_get(
350 manager
->request_queue
,
352 .type
= REQUEST_TYPE_NEXTHOP
,
353 .userdata
= (void*) &(const NextHop
) { .id
= id
},
354 .hash_func
= (hash_func_t
) nexthop_hash_func
,
355 .compare_func
= (compare_func_t
) nexthop_compare_func
,
365 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
370 assert(link
->manager
);
374 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
376 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
377 * nexthop_section_verify(). */
378 assert(link
->manager
->manage_foreign_nexthops
);
380 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
382 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
383 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
386 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
387 if (nexthop
->ifindex
!= ifindex
)
389 if (nexthop_compare_full(nexthop
, in
) != 0)
392 /* Even if the configuration matches, it may be requested by another [NextHop] section
393 * that has an explicit ID. If so, the request is not the one we are looking for. */
394 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
405 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
406 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
412 r
= nexthop_new(&nexthop
);
418 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
424 nexthop
->manager
= manager
;
433 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
440 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
441 * nexthop_section_verify(). */
442 assert(manager
->manage_foreign_nexthops
);
444 /* Find the lowest unused ID. */
446 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
447 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
449 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
451 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
461 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
462 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
463 struct nexthop_grp
*nhg
;
473 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
474 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
475 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
477 HASHMAP_FOREACH(nhg
, nexthop
->group
)
478 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
480 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
481 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
483 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
484 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
487 static int nexthop_remove_dependents(NextHop
*nexthop
, Manager
*manager
) {
493 /* If a nexthop is removed, the kernel silently removes nexthops and routes that depend on the
494 * removed nexthop. Let's remove them for safety (though, they are already removed in the kernel,
495 * hence that should fail), and forget them. */
498 SET_FOREACH(id
, nexthop
->nexthops
) {
501 if (nexthop_get_by_id(manager
, PTR_TO_UINT32(id
), &nh
) < 0)
504 RET_GATHER(r
, nexthop_remove(nh
, manager
));
508 SET_FOREACH(route
, nexthop
->routes
)
509 RET_GATHER(r
, route_remove(route
, manager
));
514 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, RemoveRequest
*rreq
) {
520 Manager
*manager
= ASSERT_PTR(rreq
->manager
);
521 NextHop
*nexthop
= ASSERT_PTR(rreq
->userdata
);
523 r
= sd_netlink_message_get_errno(m
);
525 log_message_full_errno(m
,
526 (r
== -ENOENT
|| !nexthop
->manager
) ? LOG_DEBUG
: LOG_WARNING
,
527 r
, "Could not drop nexthop, ignoring");
529 (void) nexthop_remove_dependents(nexthop
, manager
);
531 if (nexthop
->manager
) {
532 /* If the nexthop cannot be removed, then assume the nexthop is already removed. */
533 log_nexthop_debug(nexthop
, "Forgetting", manager
);
536 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
537 nexthop_enter_removed(req
->userdata
);
539 nexthop_detach(nexthop
);
546 int nexthop_remove(NextHop
*nexthop
, Manager
*manager
) {
547 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
552 assert(nexthop
->id
> 0);
555 /* If the nexthop is remembered, then use the remembered object. */
556 (void) nexthop_get_by_id(manager
, PTR_TO_UINT32(nexthop
->id
), &nexthop
);
558 /* link may be NULL. */
559 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
561 log_nexthop_debug(nexthop
, "Removing", manager
);
563 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
565 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
567 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
569 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
571 r
= manager_remove_request_add(manager
, nexthop
, nexthop
, manager
->rtnl
, m
, nexthop_remove_handler
);
573 return log_link_error_errno(link
, r
, "Could not queue rtnetlink message: %m");
575 nexthop_enter_removing(nexthop
);
579 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
580 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
584 assert(nexthop
->id
> 0);
585 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
587 assert(link
->manager
);
588 assert(link
->manager
->rtnl
);
589 assert(link
->ifindex
> 0);
592 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
594 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
598 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
602 if (!hashmap_isempty(nexthop
->group
)) {
603 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
604 struct nexthop_grp
*p
, *nhg
;
606 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
611 HASHMAP_FOREACH(nhg
, nexthop
->group
)
614 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
618 } else if (nexthop
->blackhole
) {
619 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
623 assert(nexthop
->ifindex
== link
->ifindex
);
625 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
629 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
630 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
634 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
640 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
643 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
649 r
= sd_netlink_message_get_errno(m
);
650 if (r
< 0 && r
!= -EEXIST
) {
651 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
652 link_enter_failed(link
);
656 if (link
->static_nexthop_messages
== 0) {
657 log_link_debug(link
, "Nexthops set");
658 link
->static_nexthops_configured
= true;
659 link_check_ready(link
);
665 int nexthop_is_ready(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
673 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
676 if (nexthop_get_by_id(manager
, id
, &nexthop
) < 0)
679 if (!nexthop_exists(nexthop
))
694 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
695 struct nexthop_grp
*nhg
;
700 assert(nexthop
->id
> 0);
702 if (!link_is_ready_to_configure(link
, false))
705 if (nexthop_bound_to_link(nexthop
)) {
706 assert(nexthop
->ifindex
== link
->ifindex
);
708 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
709 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
711 if (link
->set_flags_messages
> 0)
713 if (!FLAGS_SET(link
->flags
, IFF_UP
))
717 /* All group members must be configured first. */
718 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
719 r
= nexthop_is_ready(link
->manager
, nhg
->id
, NULL
);
724 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
727 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
733 assert(link
->manager
);
736 if (!nexthop_is_ready_to_configure(link
, nexthop
))
739 r
= nexthop_configure(nexthop
, link
, req
);
741 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
743 nexthop_enter_configuring(nexthop
);
744 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
745 nexthop_enter_configuring(existing
);
750 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
751 _cleanup_(nexthop_unrefp
) NextHop
*tmp
= NULL
;
752 NextHop
*existing
= NULL
;
756 assert(link
->manager
);
758 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
760 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
761 return 0; /* already requested, skipping. */
763 r
= nexthop_dup(nexthop
, &tmp
);
767 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
768 r
= nexthop_acquire_id(link
->manager
, tmp
);
773 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
774 tmp
->id
= existing
->id
;
776 /* Copy state for logging below. */
777 tmp
->state
= existing
->state
;
780 if (nexthop_bound_to_link(tmp
))
781 tmp
->ifindex
= link
->ifindex
;
783 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
784 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
788 nexthop_compare_func
,
789 nexthop_process_request
,
790 &link
->static_nexthop_messages
,
791 static_nexthop_handler
,
796 nexthop_enter_requesting(tmp
);
798 nexthop_enter_requesting(existing
);
804 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
809 assert(link
->network
);
811 link
->static_nexthops_configured
= false;
813 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
814 if (only_ipv4
&& nh
->family
!= AF_INET
)
817 r
= link_request_nexthop(link
, nh
);
819 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
822 if (link
->static_nexthop_messages
== 0) {
823 link
->static_nexthops_configured
= true;
824 link_check_ready(link
);
826 log_link_debug(link
, "Requesting nexthops");
827 link_set_state(link
, LINK_STATE_CONFIGURING
);
833 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
834 assert(assigned_nexthop
);
835 assert(assigned_nexthop
->manager
);
836 assert(requested_nexthop
);
837 assert(requested_nexthop
->network
);
839 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
840 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
841 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
844 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
845 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
846 * future when we support to configure them.*/
848 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
849 * including this nexthop, then the kernel refuses to replace the existing nexthop.
850 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
851 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
852 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
858 static void link_mark_nexthops(Link
*link
, bool foreign
) {
863 assert(link
->manager
);
865 /* First, mark all nexthops. */
866 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
867 /* do not touch nexthop created by the kernel */
868 if (nexthop
->protocol
== RTPROT_KERNEL
)
871 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
872 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
875 /* Ignore nexthops not assigned yet or already removed. */
876 if (!nexthop_exists(nexthop
))
879 /* Ignore nexthops bound to other links. */
880 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
883 nexthop_mark(nexthop
);
886 /* Then, unmark all nexthops requested by active links. */
887 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
888 if (!foreign
&& other
== link
)
891 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
894 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
897 if (nexthop_get(other
, nexthop
, &existing
) < 0)
900 if (!nexthop_can_update(existing
, nexthop
))
903 /* Found matching static configuration. Keep the existing nexthop. */
904 nexthop_unmark(existing
);
909 int link_drop_nexthops(Link
*link
, bool foreign
) {
914 assert(link
->manager
);
916 link_mark_nexthops(link
, foreign
);
918 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
919 if (!nexthop_is_marked(nexthop
))
922 RET_GATHER(r
, nexthop_remove(nexthop
, link
->manager
));
928 void link_foreignize_nexthops(Link
*link
) {
932 assert(link
->manager
);
934 link_mark_nexthops(link
, /* foreign = */ false);
936 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
937 if (!nexthop_is_marked(nexthop
))
940 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
944 static int nexthop_update_group(NextHop
*nexthop
, sd_netlink_message
*message
) {
945 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
946 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
947 size_t size
= 0, n_group
;
953 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &size
, (void**) &group
);
954 if (r
< 0 && r
!= -ENODATA
)
955 return log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
957 nexthop_detach_from_group_members(nexthop
);
959 if (size
% sizeof(struct nexthop_grp
) != 0)
960 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
961 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
963 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
964 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
965 "rtnl: received nexthop message with invalid alignment, ignoring.");
967 n_group
= size
/ sizeof(struct nexthop_grp
);
968 for (size_t i
= 0; i
< n_group
; i
++) {
969 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
971 if (group
[i
].id
== 0) {
972 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
976 if (group
[i
].weight
> 254) {
977 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
981 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
985 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
989 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
996 hashmap_free_free(nexthop
->group
);
997 nexthop
->group
= TAKE_PTR(h
);
999 nexthop_attach_to_group_members(nexthop
);
1003 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
1005 uint32_t id
, ifindex
;
1006 NextHop
*nexthop
= NULL
;
1007 Request
*req
= NULL
;
1008 bool is_new
= false;
1015 if (sd_netlink_message_is_error(message
)) {
1016 r
= sd_netlink_message_get_errno(message
);
1018 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
1023 r
= sd_netlink_message_get_type(message
, &type
);
1025 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1027 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
1028 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
1032 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
1033 if (r
== -ENODATA
) {
1034 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
1037 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1039 } else if (id
== 0) {
1040 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
1044 (void) nexthop_get_by_id(m
, id
, &nexthop
);
1045 (void) nexthop_get_request_by_id(m
, id
, &req
);
1047 if (type
== RTM_DELNEXTHOP
) {
1049 nexthop_enter_removed(nexthop
);
1050 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
1051 (void) nexthop_remove_dependents(nexthop
, m
);
1052 nexthop_detach(nexthop
);
1054 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
1057 nexthop_enter_removed(req
->userdata
);
1062 /* If we did not know the nexthop, then save it. */
1064 r
= nexthop_add_new(m
, id
, &nexthop
);
1066 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
1073 /* Also update information that cannot be obtained through netlink notification. */
1074 if (req
&& req
->waiting_reply
) {
1075 NextHop
*n
= ASSERT_PTR(req
->userdata
);
1077 nexthop
->source
= n
->source
;
1080 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
1082 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
1084 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
1086 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
1088 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
1090 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
1092 (void) nexthop_update_group(nexthop
, message
);
1094 if (nexthop
->family
!= AF_UNSPEC
) {
1095 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
1097 nexthop
->gw
= IN_ADDR_NULL
;
1099 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1102 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
1104 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
1106 nexthop
->blackhole
= r
;
1108 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
1110 nexthop
->ifindex
= 0;
1112 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1113 else if (ifindex
> INT32_MAX
)
1114 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
1116 nexthop
->ifindex
= (int) ifindex
;
1118 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1119 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1120 if (!nexthop_bound_to_link(nexthop
))
1121 nexthop
->ifindex
= 0;
1123 nexthop_enter_configured(nexthop
);
1125 nexthop_enter_configured(req
->userdata
);
1127 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1131 static int nexthop_section_verify(NextHop
*nh
) {
1132 if (section_is_invalid(nh
->section
))
1135 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1136 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1137 "%s: [NextHop] section without specifying Id= is not supported "
1138 "if ManageForeignNextHops=no is set in networkd.conf. "
1139 "Ignoring [NextHop] section from line %u.",
1140 nh
->section
->filename
, nh
->section
->line
);
1142 if (!hashmap_isempty(nh
->group
)) {
1143 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1144 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1145 "%s: nexthop group cannot have gateway address. "
1146 "Ignoring [NextHop] section from line %u.",
1147 nh
->section
->filename
, nh
->section
->line
);
1149 if (nh
->family
!= AF_UNSPEC
)
1150 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1151 "%s: nexthop group cannot have Family= setting. "
1152 "Ignoring [NextHop] section from line %u.",
1153 nh
->section
->filename
, nh
->section
->line
);
1156 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1157 "%s: nexthop group cannot be a blackhole. "
1158 "Ignoring [NextHop] section from line %u.",
1159 nh
->section
->filename
, nh
->section
->line
);
1162 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1163 "%s: nexthop group cannot have on-link flag. "
1164 "Ignoring [NextHop] section from line %u.",
1165 nh
->section
->filename
, nh
->section
->line
);
1166 } else if (nh
->family
== AF_UNSPEC
)
1167 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1168 nh
->family
= AF_INET
;
1170 if (nh
->blackhole
) {
1171 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1172 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1173 "%s: blackhole nexthop cannot have gateway address. "
1174 "Ignoring [NextHop] section from line %u.",
1175 nh
->section
->filename
, nh
->section
->line
);
1178 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1179 "%s: blackhole nexthop cannot have on-link flag. "
1180 "Ignoring [NextHop] section from line %u.",
1181 nh
->section
->filename
, nh
->section
->line
);
1184 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1185 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1186 /* If no address is configured, in most cases the gateway cannot be reachable.
1187 * TODO: we may need to improve the condition above. */
1188 log_warning("%s: Gateway= without static address configured. "
1189 "Enabling OnLink= option.",
1190 nh
->section
->filename
);
1194 if (nh
->onlink
>= 0)
1195 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1200 int network_drop_invalid_nexthops(Network
*network
) {
1201 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1207 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1208 if (nexthop_section_verify(nh
) < 0) {
1216 /* Always use the setting specified later. So, remove the previously assigned setting. */
1217 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1219 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1220 "dropping the nexthop setting specified at line %u.",
1221 dup
->section
->filename
,
1222 nh
->id
, nh
->section
->line
,
1223 dup
->section
->line
, dup
->section
->line
);
1224 /* nexthop_detach() will drop the nexthop from nexthops_by_section. */
1225 nexthop_detach(dup
);
1228 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1237 int manager_build_nexthop_ids(Manager
*manager
) {
1243 if (!manager
->manage_foreign_nexthops
)
1246 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1248 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1251 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1255 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1264 int config_parse_nexthop_id(
1266 const char *filename
,
1268 const char *section
,
1269 unsigned section_line
,
1276 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1277 Network
*network
= userdata
;
1287 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1291 if (isempty(rvalue
)) {
1297 r
= safe_atou32(rvalue
, &id
);
1299 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1300 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1304 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1305 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1314 int config_parse_nexthop_gateway(
1316 const char *filename
,
1318 const char *section
,
1319 unsigned section_line
,
1326 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1327 Network
*network
= userdata
;
1336 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1340 if (isempty(rvalue
)) {
1341 n
->family
= AF_UNSPEC
;
1342 n
->gw
= IN_ADDR_NULL
;
1348 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1350 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1351 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1359 int config_parse_nexthop_family(
1361 const char *filename
,
1363 const char *section
,
1364 unsigned section_line
,
1371 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1372 Network
*network
= userdata
;
1382 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1386 if (isempty(rvalue
) &&
1387 !in_addr_is_set(n
->family
, &n
->gw
)) {
1388 /* Accept an empty string only when Gateway= is null or not specified. */
1389 n
->family
= AF_UNSPEC
;
1394 a
= nexthop_address_family_from_string(rvalue
);
1396 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1397 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1401 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1402 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1403 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1404 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1405 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1406 "ignoring assignment.", rvalue
);
1411 case ADDRESS_FAMILY_IPV4
:
1412 n
->family
= AF_INET
;
1414 case ADDRESS_FAMILY_IPV6
:
1415 n
->family
= AF_INET6
;
1418 assert_not_reached();
1425 int config_parse_nexthop_onlink(
1427 const char *filename
,
1429 const char *section
,
1430 unsigned section_line
,
1437 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1438 Network
*network
= userdata
;
1447 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1451 r
= parse_tristate(rvalue
, &n
->onlink
);
1453 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1454 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1462 int config_parse_nexthop_blackhole(
1464 const char *filename
,
1466 const char *section
,
1467 unsigned section_line
,
1474 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1475 Network
*network
= userdata
;
1484 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1488 r
= parse_boolean(rvalue
);
1490 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1491 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1501 int config_parse_nexthop_group(
1503 const char *filename
,
1505 const char *section
,
1506 unsigned section_line
,
1513 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1514 Network
*network
= userdata
;
1523 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1527 if (isempty(rvalue
)) {
1528 n
->group
= hashmap_free_free(n
->group
);
1533 for (const char *p
= rvalue
;;) {
1534 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1535 _cleanup_free_
char *word
= NULL
;
1539 r
= extract_first_word(&p
, &word
, NULL
, 0);
1543 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1544 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1550 nhg
= new0(struct nexthop_grp
, 1);
1554 sep
= strchr(word
, ':');
1557 r
= safe_atou32(sep
, &w
);
1559 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1560 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1564 if (w
== 0 || w
> 256) {
1565 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1566 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1570 /* See comments in config_parse_multipath_route(). */
1571 nhg
->weight
= w
- 1;
1574 r
= safe_atou32(word
, &nhg
->id
);
1576 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1577 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1578 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1582 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1583 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1584 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1588 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1592 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1593 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1594 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));