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
) {
301 req
= ordered_set_get(
302 manager
->request_queue
,
304 .type
= REQUEST_TYPE_NEXTHOP
,
305 .userdata
= (void*) &(const NextHop
) { .id
= id
},
306 .hash_func
= (hash_func_t
) nexthop_hash_func
,
307 .compare_func
= (compare_func_t
) nexthop_compare_func
,
317 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
322 assert(link
->manager
);
326 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
328 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
329 * nexthop_section_verify(). */
330 assert(link
->manager
->manage_foreign_nexthops
);
332 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
334 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
335 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
338 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
339 if (nexthop
->ifindex
!= ifindex
)
341 if (nexthop_compare_full(nexthop
, in
) != 0)
344 /* Even if the configuration matches, it may be requested by another [NextHop] section
345 * that has an explicit ID. If so, the request is not the one we are looking for. */
346 if (set_contains(link
->manager
->nexthop_ids
, UINT32_TO_PTR(nexthop
->id
)))
357 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
358 _cleanup_(nexthop_unrefp
) NextHop
*nexthop
= NULL
;
364 r
= nexthop_new(&nexthop
);
370 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
376 nexthop
->manager
= manager
;
385 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
392 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
393 * nexthop_section_verify(). */
394 assert(manager
->manage_foreign_nexthops
);
396 /* Find the lowest unused ID. */
398 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
399 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
401 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
403 if (set_contains(manager
->nexthop_ids
, UINT32_TO_PTR(id
)))
413 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
414 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
415 struct nexthop_grp
*nhg
;
425 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
426 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
427 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
429 HASHMAP_FOREACH(nhg
, nexthop
->group
)
430 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
432 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
433 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
435 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
436 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
439 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, RemoveRequest
*rreq
) {
445 Manager
*manager
= ASSERT_PTR(rreq
->manager
);
446 NextHop
*nexthop
= ASSERT_PTR(rreq
->userdata
);
448 r
= sd_netlink_message_get_errno(m
);
450 log_message_full_errno(m
,
451 (r
== -ENOENT
|| !nexthop
->manager
) ? LOG_DEBUG
: LOG_WARNING
,
452 r
, "Could not drop nexthop, ignoring");
454 if (nexthop
->manager
) {
455 /* If the nexthop cannot be removed, then assume the nexthop is already removed. */
456 log_nexthop_debug(nexthop
, "Forgetting", manager
);
459 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
460 nexthop_enter_removed(req
->userdata
);
462 nexthop_detach(nexthop
);
469 int nexthop_remove(NextHop
*nexthop
, Manager
*manager
) {
470 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
475 assert(nexthop
->id
> 0);
478 /* link may be NULL. */
479 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
481 log_nexthop_debug(nexthop
, "Removing", manager
);
483 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
485 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
487 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
489 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
491 r
= manager_remove_request_add(manager
, nexthop
, nexthop
, manager
->rtnl
, m
, nexthop_remove_handler
);
493 return log_link_error_errno(link
, r
, "Could not queue rtnetlink message: %m");
495 nexthop_enter_removing(nexthop
);
499 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
500 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
504 assert(nexthop
->id
> 0);
505 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
507 assert(link
->manager
);
508 assert(link
->manager
->rtnl
);
509 assert(link
->ifindex
> 0);
512 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
514 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
518 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
522 if (!hashmap_isempty(nexthop
->group
)) {
523 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
524 struct nexthop_grp
*p
, *nhg
;
526 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
531 HASHMAP_FOREACH(nhg
, nexthop
->group
)
534 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
538 } else if (nexthop
->blackhole
) {
539 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
543 assert(nexthop
->ifindex
== link
->ifindex
);
545 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
549 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
550 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
554 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
560 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
563 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
569 r
= sd_netlink_message_get_errno(m
);
570 if (r
< 0 && r
!= -EEXIST
) {
571 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
572 link_enter_failed(link
);
576 if (link
->static_nexthop_messages
== 0) {
577 log_link_debug(link
, "Nexthops set");
578 link
->static_nexthops_configured
= true;
579 link_check_ready(link
);
585 int nexthop_is_ready(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
593 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
596 if (nexthop_get_by_id(manager
, id
, &nexthop
) < 0)
599 if (!nexthop_exists(nexthop
))
614 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
615 struct nexthop_grp
*nhg
;
620 assert(nexthop
->id
> 0);
622 if (!link_is_ready_to_configure(link
, false))
625 if (nexthop_bound_to_link(nexthop
)) {
626 assert(nexthop
->ifindex
== link
->ifindex
);
628 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
629 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
631 if (link
->set_flags_messages
> 0)
633 if (!FLAGS_SET(link
->flags
, IFF_UP
))
637 /* All group members must be configured first. */
638 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
639 r
= nexthop_is_ready(link
->manager
, nhg
->id
, NULL
);
644 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
647 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
653 assert(link
->manager
);
656 if (!nexthop_is_ready_to_configure(link
, nexthop
))
659 r
= nexthop_configure(nexthop
, link
, req
);
661 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
663 nexthop_enter_configuring(nexthop
);
664 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
665 nexthop_enter_configuring(existing
);
670 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
671 _cleanup_(nexthop_unrefp
) NextHop
*tmp
= NULL
;
672 NextHop
*existing
= NULL
;
676 assert(link
->manager
);
678 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
680 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
681 return 0; /* already requested, skipping. */
683 r
= nexthop_dup(nexthop
, &tmp
);
687 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
688 r
= nexthop_acquire_id(link
->manager
, tmp
);
693 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
694 tmp
->id
= existing
->id
;
696 /* Copy state for logging below. */
697 tmp
->state
= existing
->state
;
700 if (nexthop_bound_to_link(tmp
))
701 tmp
->ifindex
= link
->ifindex
;
703 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
704 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
708 nexthop_compare_func
,
709 nexthop_process_request
,
710 &link
->static_nexthop_messages
,
711 static_nexthop_handler
,
716 nexthop_enter_requesting(tmp
);
718 nexthop_enter_requesting(existing
);
724 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
729 assert(link
->network
);
731 link
->static_nexthops_configured
= false;
733 ORDERED_HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
734 if (only_ipv4
&& nh
->family
!= AF_INET
)
737 r
= link_request_nexthop(link
, nh
);
739 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
742 if (link
->static_nexthop_messages
== 0) {
743 link
->static_nexthops_configured
= true;
744 link_check_ready(link
);
746 log_link_debug(link
, "Requesting nexthops");
747 link_set_state(link
, LINK_STATE_CONFIGURING
);
753 static bool nexthop_can_update(const NextHop
*assigned_nexthop
, const NextHop
*requested_nexthop
) {
754 assert(assigned_nexthop
);
755 assert(assigned_nexthop
->manager
);
756 assert(requested_nexthop
);
757 assert(requested_nexthop
->network
);
759 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
760 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
761 if (hashmap_isempty(assigned_nexthop
->group
) != hashmap_isempty(requested_nexthop
->group
))
764 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
765 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
766 * future when we support to configure them.*/
768 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
769 * including this nexthop, then the kernel refuses to replace the existing nexthop.
770 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
771 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
772 if (!assigned_nexthop
->blackhole
&& requested_nexthop
->blackhole
)
778 static void link_mark_nexthops(Link
*link
, bool foreign
) {
783 assert(link
->manager
);
785 /* First, mark all nexthops. */
786 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
787 /* do not touch nexthop created by the kernel */
788 if (nexthop
->protocol
== RTPROT_KERNEL
)
791 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
792 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
795 /* Ignore nexthops not assigned yet or already removed. */
796 if (!nexthop_exists(nexthop
))
799 /* Ignore nexthops bound to other links. */
800 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
803 nexthop_mark(nexthop
);
806 /* Then, unmark all nexthops requested by active links. */
807 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
808 if (!foreign
&& other
== link
)
811 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
814 ORDERED_HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
817 if (nexthop_get(other
, nexthop
, &existing
) < 0)
820 if (!nexthop_can_update(existing
, nexthop
))
823 /* Found matching static configuration. Keep the existing nexthop. */
824 nexthop_unmark(existing
);
829 int link_drop_nexthops(Link
*link
, bool foreign
) {
834 assert(link
->manager
);
836 link_mark_nexthops(link
, foreign
);
838 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
839 if (!nexthop_is_marked(nexthop
))
842 RET_GATHER(r
, nexthop_remove(nexthop
, link
->manager
));
848 void link_foreignize_nexthops(Link
*link
) {
852 assert(link
->manager
);
854 link_mark_nexthops(link
, /* foreign = */ false);
856 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
857 if (!nexthop_is_marked(nexthop
))
860 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
864 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
865 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
870 assert(group
|| size
== 0);
872 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
873 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
874 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
876 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
877 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
878 "rtnl: received nexthop message with invalid alignment, ignoring.");
880 n_group
= size
/ sizeof(struct nexthop_grp
);
881 for (size_t i
= 0; i
< n_group
; i
++) {
882 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
884 if (group
[i
].id
== 0) {
885 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
889 if (group
[i
].weight
> 254) {
890 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
894 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
898 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
902 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
909 hashmap_free_free(nexthop
->group
);
910 nexthop
->group
= TAKE_PTR(h
);
914 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
915 _cleanup_free_
void *raw_group
= NULL
;
916 size_t raw_group_size
;
918 uint32_t id
, ifindex
;
919 NextHop
*nexthop
= NULL
;
928 if (sd_netlink_message_is_error(message
)) {
929 r
= sd_netlink_message_get_errno(message
);
931 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
936 r
= sd_netlink_message_get_type(message
, &type
);
938 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
940 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
941 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
945 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
947 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
950 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
952 } else if (id
== 0) {
953 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
957 (void) nexthop_get_by_id(m
, id
, &nexthop
);
958 (void) nexthop_get_request_by_id(m
, id
, &req
);
960 if (type
== RTM_DELNEXTHOP
) {
962 nexthop_enter_removed(nexthop
);
963 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
964 nexthop_detach(nexthop
);
966 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
969 nexthop_enter_removed(req
->userdata
);
974 /* If we did not know the nexthop, then save it. */
976 r
= nexthop_add_new(m
, id
, &nexthop
);
978 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
985 /* Also update information that cannot be obtained through netlink notification. */
986 if (req
&& req
->waiting_reply
) {
987 NextHop
*n
= ASSERT_PTR(req
->userdata
);
989 nexthop
->source
= n
->source
;
992 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
994 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
996 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
998 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
1000 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
1002 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
1004 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
1006 nexthop
->group
= hashmap_free_free(nexthop
->group
);
1008 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
1010 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
1012 if (nexthop
->family
!= AF_UNSPEC
) {
1013 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
1015 nexthop
->gw
= IN_ADDR_NULL
;
1017 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1020 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
1022 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
1024 nexthop
->blackhole
= r
;
1026 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
1028 nexthop
->ifindex
= 0;
1030 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1031 else if (ifindex
> INT32_MAX
)
1032 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
1034 nexthop
->ifindex
= (int) ifindex
;
1036 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
1037 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
1038 if (!nexthop_bound_to_link(nexthop
))
1039 nexthop
->ifindex
= 0;
1041 nexthop_enter_configured(nexthop
);
1043 nexthop_enter_configured(req
->userdata
);
1045 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
1049 static int nexthop_section_verify(NextHop
*nh
) {
1050 if (section_is_invalid(nh
->section
))
1053 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1054 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1055 "%s: [NextHop] section without specifying Id= is not supported "
1056 "if ManageForeignNextHops=no is set in networkd.conf. "
1057 "Ignoring [NextHop] section from line %u.",
1058 nh
->section
->filename
, nh
->section
->line
);
1060 if (!hashmap_isempty(nh
->group
)) {
1061 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1062 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1063 "%s: nexthop group cannot have gateway address. "
1064 "Ignoring [NextHop] section from line %u.",
1065 nh
->section
->filename
, nh
->section
->line
);
1067 if (nh
->family
!= AF_UNSPEC
)
1068 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1069 "%s: nexthop group cannot have Family= setting. "
1070 "Ignoring [NextHop] section from line %u.",
1071 nh
->section
->filename
, nh
->section
->line
);
1074 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1075 "%s: nexthop group cannot be a blackhole. "
1076 "Ignoring [NextHop] section from line %u.",
1077 nh
->section
->filename
, nh
->section
->line
);
1080 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1081 "%s: nexthop group cannot have on-link flag. "
1082 "Ignoring [NextHop] section from line %u.",
1083 nh
->section
->filename
, nh
->section
->line
);
1084 } else if (nh
->family
== AF_UNSPEC
)
1085 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1086 nh
->family
= AF_INET
;
1088 if (nh
->blackhole
) {
1089 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1090 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1091 "%s: blackhole nexthop cannot have gateway address. "
1092 "Ignoring [NextHop] section from line %u.",
1093 nh
->section
->filename
, nh
->section
->line
);
1096 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1097 "%s: blackhole nexthop cannot have on-link flag. "
1098 "Ignoring [NextHop] section from line %u.",
1099 nh
->section
->filename
, nh
->section
->line
);
1102 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1103 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1104 /* If no address is configured, in most cases the gateway cannot be reachable.
1105 * TODO: we may need to improve the condition above. */
1106 log_warning("%s: Gateway= without static address configured. "
1107 "Enabling OnLink= option.",
1108 nh
->section
->filename
);
1112 if (nh
->onlink
>= 0)
1113 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1118 int network_drop_invalid_nexthops(Network
*network
) {
1119 _cleanup_hashmap_free_ Hashmap
*nexthops
= NULL
;
1125 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1126 if (nexthop_section_verify(nh
) < 0) {
1134 /* Always use the setting specified later. So, remove the previously assigned setting. */
1135 NextHop
*dup
= hashmap_remove(nexthops
, UINT32_TO_PTR(nh
->id
));
1137 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32
" is specified at line %u and %u, "
1138 "dropping the nexthop setting specified at line %u.",
1139 dup
->section
->filename
,
1140 nh
->id
, nh
->section
->line
,
1141 dup
->section
->line
, dup
->section
->line
);
1142 /* nexthop_detach() will drop the nexthop from nexthops_by_section. */
1143 nexthop_detach(dup
);
1146 r
= hashmap_ensure_put(&nexthops
, NULL
, UINT32_TO_PTR(nh
->id
), nh
);
1155 int manager_build_nexthop_ids(Manager
*manager
) {
1161 if (!manager
->manage_foreign_nexthops
)
1164 manager
->nexthop_ids
= set_free(manager
->nexthop_ids
);
1166 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
1169 ORDERED_HASHMAP_FOREACH(nh
, network
->nexthops_by_section
) {
1173 r
= set_ensure_put(&manager
->nexthop_ids
, NULL
, UINT32_TO_PTR(nh
->id
));
1182 int config_parse_nexthop_id(
1184 const char *filename
,
1186 const char *section
,
1187 unsigned section_line
,
1194 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1195 Network
*network
= userdata
;
1205 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1209 if (isempty(rvalue
)) {
1215 r
= safe_atou32(rvalue
, &id
);
1217 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1218 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1222 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1223 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1232 int config_parse_nexthop_gateway(
1234 const char *filename
,
1236 const char *section
,
1237 unsigned section_line
,
1244 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1245 Network
*network
= userdata
;
1254 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1258 if (isempty(rvalue
)) {
1259 n
->family
= AF_UNSPEC
;
1260 n
->gw
= IN_ADDR_NULL
;
1266 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1268 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1269 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1277 int config_parse_nexthop_family(
1279 const char *filename
,
1281 const char *section
,
1282 unsigned section_line
,
1289 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1290 Network
*network
= userdata
;
1300 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1304 if (isempty(rvalue
) &&
1305 !in_addr_is_set(n
->family
, &n
->gw
)) {
1306 /* Accept an empty string only when Gateway= is null or not specified. */
1307 n
->family
= AF_UNSPEC
;
1312 a
= nexthop_address_family_from_string(rvalue
);
1314 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1315 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1319 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1320 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1321 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1322 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1323 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1324 "ignoring assignment.", rvalue
);
1329 case ADDRESS_FAMILY_IPV4
:
1330 n
->family
= AF_INET
;
1332 case ADDRESS_FAMILY_IPV6
:
1333 n
->family
= AF_INET6
;
1336 assert_not_reached();
1343 int config_parse_nexthop_onlink(
1345 const char *filename
,
1347 const char *section
,
1348 unsigned section_line
,
1355 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1356 Network
*network
= userdata
;
1365 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1369 r
= parse_tristate(rvalue
, &n
->onlink
);
1371 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1372 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1380 int config_parse_nexthop_blackhole(
1382 const char *filename
,
1384 const char *section
,
1385 unsigned section_line
,
1392 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1393 Network
*network
= userdata
;
1402 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1406 r
= parse_boolean(rvalue
);
1408 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1409 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1419 int config_parse_nexthop_group(
1421 const char *filename
,
1423 const char *section
,
1424 unsigned section_line
,
1431 _cleanup_(nexthop_unref_or_set_invalidp
) NextHop
*n
= NULL
;
1432 Network
*network
= userdata
;
1441 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1445 if (isempty(rvalue
)) {
1446 n
->group
= hashmap_free_free(n
->group
);
1451 for (const char *p
= rvalue
;;) {
1452 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1453 _cleanup_free_
char *word
= NULL
;
1457 r
= extract_first_word(&p
, &word
, NULL
, 0);
1461 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1462 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1468 nhg
= new0(struct nexthop_grp
, 1);
1472 sep
= strchr(word
, ':');
1475 r
= safe_atou32(sep
, &w
);
1477 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1478 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1482 if (w
== 0 || w
> 256) {
1483 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1484 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1488 /* See comments in config_parse_multipath_route(). */
1489 nhg
->weight
= w
- 1;
1492 r
= safe_atou32(word
, &nhg
->id
);
1494 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1495 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1496 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1500 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1501 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1502 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1506 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1510 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1511 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1512 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));