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 NextHop
* nexthop_detach_impl(NextHop
*nexthop
) {
23 assert(!nexthop
->manager
|| !nexthop
->network
);
25 if (nexthop
->network
) {
26 assert(nexthop
->section
);
27 ordered_hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
28 nexthop
->network
= NULL
;
32 if (nexthop
->manager
) {
33 assert(nexthop
->id
> 0);
34 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
35 nexthop
->manager
= NULL
;
42 static void nexthop_detach(NextHop
*nexthop
) {
43 nexthop_unref(nexthop_detach_impl(nexthop
));
46 static NextHop
* nexthop_free(NextHop
*nexthop
) {
50 nexthop_detach_impl(nexthop
);
52 config_section_free(nexthop
->section
);
53 hashmap_free_free(nexthop
->group
);
55 return mfree(nexthop
);
58 DEFINE_TRIVIAL_REF_UNREF_FUNC(NextHop
, nexthop
, nexthop_free
);
59 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_unref
);
61 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
69 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
70 nexthop_section_hash_ops
,
72 config_section_hash_func
,
73 config_section_compare_func
,
77 static int nexthop_new(NextHop
**ret
) {
78 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
80 nexthop
= new(NextHop
, 1);
84 *nexthop
= (NextHop
) {
89 *ret
= TAKE_PTR(nexthop
);
94 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
95 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
96 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
102 assert(section_line
> 0);
104 r
= config_section_new(filename
, section_line
, &n
);
108 nexthop
= ordered_hashmap_get(network
->nexthops_by_section
, n
);
110 *ret
= TAKE_PTR(nexthop
);
114 r
= nexthop_new(&nexthop
);
118 nexthop
->protocol
= RTPROT_STATIC
;
119 nexthop
->network
= network
;
120 nexthop
->section
= TAKE_PTR(n
);
121 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
123 r
= ordered_hashmap_ensure_put(&network
->nexthops_by_section
, &nexthop_section_hash_ops
, nexthop
->section
, nexthop
);
127 *ret
= TAKE_PTR(nexthop
);
131 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
135 siphash24_compress_typesafe(nexthop
->id
, state
);
138 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
142 return CMP(a
->id
, b
->id
);
145 static int nexthop_compare_full(const NextHop
*a
, const NextHop
*b
) {
151 /* This compares detailed configs, except for ID and ifindex. */
153 r
= CMP(a
->protocol
, b
->protocol
);
157 r
= CMP(a
->flags
, b
->flags
);
161 r
= CMP(hashmap_size(a
->group
), hashmap_size(b
->group
));
165 if (!hashmap_isempty(a
->group
)) {
166 struct nexthop_grp
*ga
;
168 HASHMAP_FOREACH(ga
, a
->group
) {
169 struct nexthop_grp
*gb
;
171 gb
= hashmap_get(b
->group
, UINT32_TO_PTR(ga
->id
));
175 r
= CMP(ga
->weight
, gb
->weight
);
181 r
= CMP(a
->blackhole
, b
->blackhole
);
185 r
= CMP(a
->family
, b
->family
);
189 if (IN_SET(a
->family
, AF_INET
, AF_INET6
)) {
190 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
198 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
199 _cleanup_(nexthop_unrefp
) NextHop
*dest
= NULL
;
200 struct nexthop_grp
*nhg
;
206 dest
= newdup(NextHop
, src
, 1);
210 /* clear the reference counter and all pointers */
212 dest
->manager
= NULL
;
213 dest
->network
= NULL
;
214 dest
->section
= NULL
;
217 HASHMAP_FOREACH(nhg
, src
->group
) {
218 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
220 g
= newdup(struct nexthop_grp
, nhg
, 1);
224 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
231 *ret
= TAKE_PTR(dest
);
235 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
237 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
240 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
248 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
257 static int nexthop_get(Link
*link
, const NextHop
*in
, NextHop
**ret
) {
262 assert(link
->manager
);
266 return nexthop_get_by_id(link
->manager
, in
->id
, ret
);
268 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
269 * nexthop_section_verify(). */
270 assert(link
->manager
->manage_foreign_nexthops
);
272 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
274 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
275 if (nexthop
->ifindex
!= ifindex
)
277 if (nexthop_compare_full(nexthop
, in
) != 0)
280 /* Even if the configuration matches, it may be configured with another [NextHop] section
281 * that has an explicit ID. If so, the assigned nexthop is not the one we are looking for. */
282 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
293 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
298 req
= ordered_set_get(
299 manager
->request_queue
,
301 .type
= REQUEST_TYPE_NEXTHOP
,
302 .userdata
= (void*) &(const NextHop
) { .id
= id
},
303 .hash_func
= (hash_func_t
) nexthop_hash_func
,
304 .compare_func
= (compare_func_t
) nexthop_compare_func
,
314 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
319 assert(link
->manager
);
323 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
325 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
326 * nexthop_section_verify(). */
327 assert(link
->manager
->manage_foreign_nexthops
);
329 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
331 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
332 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
335 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
336 if (nexthop
->ifindex
!= ifindex
)
338 if (nexthop_compare_full(nexthop
, in
) != 0)
341 /* Even if the configuration matches, it may be requested by another [NextHop] section
342 * that has an explicit ID. If so, the request is not the one we are looking for. */
343 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
354 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
355 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
361 r
= nexthop_new(&nexthop
);
367 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
373 nexthop
->manager
= manager
;
382 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
389 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
390 * nexthop_section_verify(). */
391 assert(manager
->manage_foreign_nexthops
);
393 /* Find the lowest unused ID. */
395 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
396 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
398 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
400 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
410 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
411 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
412 struct nexthop_grp
*nhg
;
422 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
423 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
424 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
426 HASHMAP_FOREACH(nhg
, nexthop
->group
)
427 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
429 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
430 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
432 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
433 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
436 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
441 /* link may be NULL. */
443 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
446 r
= sd_netlink_message_get_errno(m
);
447 if (r
< 0 && r
!= -ENOENT
)
448 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
453 static int nexthop_remove(NextHop
*nexthop
) {
454 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
461 assert(nexthop
->id
> 0);
463 manager
= ASSERT_PTR(nexthop
->manager
);
465 /* link may be NULL. */
466 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
468 log_nexthop_debug(nexthop
, "Removing", manager
);
470 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
472 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
474 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
476 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
478 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
479 link
? link_netlink_destroy_callback
: NULL
, link
);
481 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
483 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
485 nexthop_enter_removing(nexthop
);
486 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
487 nexthop_enter_removing(req
->userdata
);
492 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
493 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
497 assert(nexthop
->id
> 0);
498 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
500 assert(link
->manager
);
501 assert(link
->manager
->rtnl
);
502 assert(link
->ifindex
> 0);
505 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
507 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
511 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
515 if (!hashmap_isempty(nexthop
->group
)) {
516 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
517 struct nexthop_grp
*p
, *nhg
;
519 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
524 HASHMAP_FOREACH(nhg
, nexthop
->group
)
527 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
531 } else if (nexthop
->blackhole
) {
532 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
536 assert(nexthop
->ifindex
== link
->ifindex
);
538 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
542 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
543 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
547 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
553 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
556 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
562 r
= sd_netlink_message_get_errno(m
);
563 if (r
< 0 && r
!= -EEXIST
) {
564 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
565 link_enter_failed(link
);
569 if (link
->static_nexthop_messages
== 0) {
570 log_link_debug(link
, "Nexthops set");
571 link
->static_nexthops_configured
= true;
572 link_check_ready(link
);
578 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
579 struct nexthop_grp
*nhg
;
583 assert(nexthop
->id
> 0);
585 if (!link_is_ready_to_configure(link
, false))
588 if (nexthop_bound_to_link(nexthop
)) {
589 assert(nexthop
->ifindex
== link
->ifindex
);
591 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
592 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
594 if (link
->set_flags_messages
> 0)
596 if (!FLAGS_SET(link
->flags
, IFF_UP
))
600 /* All group members must be configured first. */
601 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
604 if (nexthop_get_by_id(link
->manager
, nhg
->id
, &g
) < 0)
607 if (!nexthop_exists(g
))
611 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
614 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
620 assert(link
->manager
);
623 if (!nexthop_is_ready_to_configure(link
, nexthop
))
626 r
= nexthop_configure(nexthop
, link
, req
);
628 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
630 nexthop_enter_configuring(nexthop
);
631 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
632 nexthop_enter_configuring(existing
);
637 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
638 _cleanup_(nexthop_unrefp
) NextHop
*tmp
= NULL
;
639 NextHop
*existing
= NULL
;
643 assert(link
->manager
);
645 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
647 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
648 return 0; /* already requested, skipping. */
650 r
= nexthop_dup(nexthop
, &tmp
);
654 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
655 r
= nexthop_acquire_id(link
->manager
, tmp
);
660 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
661 tmp
->id
= existing
->id
;
663 /* Copy state for logging below. */
664 tmp
->state
= existing
->state
;
667 if (nexthop_bound_to_link(tmp
))
668 tmp
->ifindex
= link
->ifindex
;
670 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
671 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
675 nexthop_compare_func
,
676 nexthop_process_request
,
677 &link
->static_nexthop_messages
,
678 static_nexthop_handler
,
683 nexthop_enter_requesting(tmp
);
685 nexthop_enter_requesting(existing
);
691 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
696 assert(link
->network
);
698 link
->static_nexthops_configured
= false;
700 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
701 if (only_ipv4
&& nh
->family
!= AF_INET
)
704 r
= link_request_nexthop(link
, nh
);
706 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
709 if (link
->static_nexthop_messages
== 0) {
710 link
->static_nexthops_configured
= true;
711 link_check_ready(link
);
713 log_link_debug(link
, "Requesting nexthops");
714 link_set_state(link
, LINK_STATE_CONFIGURING
);
720 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
721 assert(assigned_nexthop
);
722 assert(assigned_nexthop
->manager
);
723 assert(requested_nexthop
);
724 assert(requested_nexthop
->network
);
726 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
727 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
728 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
731 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
732 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
733 * future when we support to configure them.*/
735 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
736 * including this nexthop, then the kernel refuses to replace the existing nexthop.
737 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
738 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
739 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
745 static void link_mark_nexthops(Link
*link
, bool foreign
) {
750 assert(link
->manager
);
752 /* First, mark all nexthops. */
753 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
754 /* do not touch nexthop created by the kernel */
755 if (nexthop
->protocol
== RTPROT_KERNEL
)
758 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
759 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
762 /* Ignore nexthops not assigned yet or already removed. */
763 if (!nexthop_exists(nexthop
))
766 /* Ignore nexthops bound to other links. */
767 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
770 nexthop_mark(nexthop
);
773 /* Then, unmark all nexthops requested by active links. */
774 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
775 if (!foreign
&& other
== link
)
778 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
781 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
784 if (nexthop_get(other
, nexthop
, &existing
) < 0)
787 if (!nexthop_can_update(existing
, nexthop
))
790 /* Found matching static configuration. Keep the existing nexthop. */
791 nexthop_unmark(existing
);
796 int link_drop_nexthops(Link
*link
, bool foreign
) {
801 assert(link
->manager
);
803 link_mark_nexthops(link
, foreign
);
805 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
806 if (!nexthop_is_marked(nexthop
))
809 RET_GATHER(r
, nexthop_remove(nexthop
));
815 void link_foreignize_nexthops(Link
*link
) {
819 assert(link
->manager
);
821 link_mark_nexthops(link
, /* foreign = */ false);
823 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
824 if (!nexthop_is_marked(nexthop
))
827 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
831 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
832 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
837 assert(group
|| size
== 0);
839 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
840 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
841 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
843 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
844 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
845 "rtnl: received nexthop message with invalid alignment, ignoring.");
847 n_group
= size
/ sizeof(struct nexthop_grp
);
848 for (size_t i
= 0; i
< n_group
; i
++) {
849 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
851 if (group
[i
].id
== 0) {
852 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
856 if (group
[i
].weight
> 254) {
857 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
861 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
865 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
869 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
876 hashmap_free_free(nexthop
->group
);
877 nexthop
->group
= TAKE_PTR(h
);
881 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
882 _cleanup_free_
void *raw_group
= NULL
;
883 size_t raw_group_size
;
885 uint32_t id
, ifindex
;
886 NextHop
*nexthop
= NULL
;
895 if (sd_netlink_message_is_error(message
)) {
896 r
= sd_netlink_message_get_errno(message
);
898 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
903 r
= sd_netlink_message_get_type(message
, &type
);
905 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
907 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
908 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
912 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
914 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
917 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
919 } else if (id
== 0) {
920 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
924 (void) nexthop_get_by_id(m
, id
, &nexthop
);
925 (void) nexthop_get_request_by_id(m
, id
, &req
);
927 if (type
== RTM_DELNEXTHOP
) {
929 nexthop_enter_removed(nexthop
);
930 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
931 nexthop_detach(nexthop
);
933 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
936 nexthop_enter_removed(req
->userdata
);
941 /* If we did not know the nexthop, then save it. */
943 r
= nexthop_add_new(m
, id
, &nexthop
);
945 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
952 /* Also update information that cannot be obtained through netlink notification. */
953 if (req
&& req
->waiting_reply
) {
954 NextHop
*n
= ASSERT_PTR(req
->userdata
);
956 nexthop
->source
= n
->source
;
959 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
961 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
963 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
965 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
967 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
969 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
971 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
973 nexthop
->group
= hashmap_free_free(nexthop
->group
);
975 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
977 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
979 if (nexthop
->family
!= AF_UNSPEC
) {
980 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
982 nexthop
->gw
= IN_ADDR_NULL
;
984 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
987 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
989 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
991 nexthop
->blackhole
= r
;
993 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
995 nexthop
->ifindex
= 0;
997 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
998 else if (ifindex
> INT32_MAX
)
999 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
1001 nexthop
->ifindex
= (int) ifindex
;
1003 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1004 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1005 if (!nexthop_bound_to_link(nexthop
))
1006 nexthop
->ifindex
= 0;
1008 nexthop_enter_configured(nexthop
);
1010 nexthop_enter_configured(req
->userdata
);
1012 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1016 static int nexthop_section_verify(NextHop
*nh
) {
1017 if (section_is_invalid(nh
->section
))
1020 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1021 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1022 "%s: [NextHop] section without specifying Id= is not supported "
1023 "if ManageForeignNextHops=no is set in networkd.conf. "
1024 "Ignoring [NextHop] section from line %u.",
1025 nh
->section
->filename
, nh
->section
->line
);
1027 if (!hashmap_isempty(nh
->group
)) {
1028 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1029 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1030 "%s: nexthop group cannot have gateway address. "
1031 "Ignoring [NextHop] section from line %u.",
1032 nh
->section
->filename
, nh
->section
->line
);
1034 if (nh
->family
!= AF_UNSPEC
)
1035 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1036 "%s: nexthop group cannot have Family= setting. "
1037 "Ignoring [NextHop] section from line %u.",
1038 nh
->section
->filename
, nh
->section
->line
);
1041 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1042 "%s: nexthop group cannot be a blackhole. "
1043 "Ignoring [NextHop] section from line %u.",
1044 nh
->section
->filename
, nh
->section
->line
);
1047 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1048 "%s: nexthop group cannot have on-link flag. "
1049 "Ignoring [NextHop] section from line %u.",
1050 nh
->section
->filename
, nh
->section
->line
);
1051 } else if (nh
->family
== AF_UNSPEC
)
1052 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1053 nh
->family
= AF_INET
;
1055 if (nh
->blackhole
) {
1056 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1057 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1058 "%s: blackhole nexthop cannot have gateway address. "
1059 "Ignoring [NextHop] section from line %u.",
1060 nh
->section
->filename
, nh
->section
->line
);
1063 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1064 "%s: blackhole nexthop cannot have on-link flag. "
1065 "Ignoring [NextHop] section from line %u.",
1066 nh
->section
->filename
, nh
->section
->line
);
1069 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1070 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1071 /* If no address is configured, in most cases the gateway cannot be reachable.
1072 * TODO: we may need to improve the condition above. */
1073 log_warning("%s: Gateway= without static address configured. "
1074 "Enabling OnLink= option.",
1075 nh
->section
->filename
);
1079 if (nh
->onlink
>= 0)
1080 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1085 int network_drop_invalid_nexthops(Network
*network
) {
1086 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1092 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1093 if (nexthop_section_verify(nh
) < 0) {
1101 /* Always use the setting specified later. So, remove the previously assigned setting. */
1102 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1104 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1105 "dropping the nexthop setting specified at line %u.",
1106 dup
->section
->filename
,
1107 nh
->id
, nh
->section
->line
,
1108 dup
->section
->line
, dup
->section
->line
);
1109 /* nexthop_detach() will drop the nexthop from nexthops_by_section. */
1110 nexthop_detach(dup
);
1113 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1122 int manager_build_nexthop_ids(Manager
*manager
) {
1128 if (!manager
->manage_foreign_nexthops
)
1131 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1133 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1136 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1140 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1149 int config_parse_nexthop_id(
1151 const char *filename
,
1153 const char *section
,
1154 unsigned section_line
,
1161 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1162 Network
*network
= userdata
;
1172 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1176 if (isempty(rvalue
)) {
1182 r
= safe_atou32(rvalue
, &id
);
1184 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1185 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1189 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1190 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1199 int config_parse_nexthop_gateway(
1201 const char *filename
,
1203 const char *section
,
1204 unsigned section_line
,
1211 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1212 Network
*network
= userdata
;
1221 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1225 if (isempty(rvalue
)) {
1226 n
->family
= AF_UNSPEC
;
1227 n
->gw
= IN_ADDR_NULL
;
1233 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1235 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1236 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1244 int config_parse_nexthop_family(
1246 const char *filename
,
1248 const char *section
,
1249 unsigned section_line
,
1256 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1257 Network
*network
= userdata
;
1267 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1271 if (isempty(rvalue
) &&
1272 !in_addr_is_set(n
->family
, &n
->gw
)) {
1273 /* Accept an empty string only when Gateway= is null or not specified. */
1274 n
->family
= AF_UNSPEC
;
1279 a
= nexthop_address_family_from_string(rvalue
);
1281 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1282 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1286 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1287 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1288 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1289 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1290 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1291 "ignoring assignment.", rvalue
);
1296 case ADDRESS_FAMILY_IPV4
:
1297 n
->family
= AF_INET
;
1299 case ADDRESS_FAMILY_IPV6
:
1300 n
->family
= AF_INET6
;
1303 assert_not_reached();
1310 int config_parse_nexthop_onlink(
1312 const char *filename
,
1314 const char *section
,
1315 unsigned section_line
,
1322 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1323 Network
*network
= userdata
;
1332 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1336 r
= parse_tristate(rvalue
, &n
->onlink
);
1338 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1339 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1347 int config_parse_nexthop_blackhole(
1349 const char *filename
,
1351 const char *section
,
1352 unsigned section_line
,
1359 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1360 Network
*network
= userdata
;
1369 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1373 r
= parse_boolean(rvalue
);
1375 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1376 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1386 int config_parse_nexthop_group(
1388 const char *filename
,
1390 const char *section
,
1391 unsigned section_line
,
1398 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1399 Network
*network
= userdata
;
1408 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1412 if (isempty(rvalue
)) {
1413 n
->group
= hashmap_free_free(n
->group
);
1418 for (const char *p
= rvalue
;;) {
1419 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1420 _cleanup_free_
char *word
= NULL
;
1424 r
= extract_first_word(&p
, &word
, NULL
, 0);
1428 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1429 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1435 nhg
= new0(struct nexthop_grp
, 1);
1439 sep
= strchr(word
, ':');
1442 r
= safe_atou32(sep
, &w
);
1444 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1445 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1449 if (w
== 0 || w
> 256) {
1450 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1451 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1455 /* See comments in config_parse_multipath_route(). */
1456 nhg
->weight
= w
- 1;
1459 r
= safe_atou32(word
, &nhg
->id
);
1461 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1462 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1463 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1467 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1468 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1469 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1473 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1477 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1478 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1479 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));