1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2019 VMware, Inc.
6 #include <linux/nexthop.h>
8 #include "alloc-util.h"
9 #include "netlink-util.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "networkd-nexthop.h"
14 #include "networkd-queue.h"
15 #include "networkd-route-util.h"
16 #include "parse-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
21 NextHop
*nexthop_free(NextHop
*nexthop
) {
25 if (nexthop
->network
) {
26 assert(nexthop
->section
);
27 hashmap_remove(nexthop
->network
->nexthops_by_section
, nexthop
->section
);
30 config_section_free(nexthop
->section
);
32 if (nexthop
->manager
) {
33 assert(nexthop
->id
> 0);
34 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
37 hashmap_free_free(nexthop
->group
);
39 return mfree(nexthop
);
42 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_free
);
44 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
52 static int nexthop_new(NextHop
**ret
) {
53 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
55 nexthop
= new(NextHop
, 1);
59 *nexthop
= (NextHop
) {
64 *ret
= TAKE_PTR(nexthop
);
69 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
70 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
71 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
77 assert(section_line
> 0);
79 r
= config_section_new(filename
, section_line
, &n
);
83 nexthop
= hashmap_get(network
->nexthops_by_section
, n
);
85 *ret
= TAKE_PTR(nexthop
);
89 r
= nexthop_new(&nexthop
);
93 nexthop
->protocol
= RTPROT_STATIC
;
94 nexthop
->network
= network
;
95 nexthop
->section
= TAKE_PTR(n
);
96 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
98 r
= hashmap_ensure_put(&network
->nexthops_by_section
, &config_section_hash_ops
, nexthop
->section
, nexthop
);
102 *ret
= TAKE_PTR(nexthop
);
106 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
110 siphash24_compress(&nexthop
->id
, sizeof(nexthop
->id
), state
);
113 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
117 return CMP(a
->id
, b
->id
);
120 static int nexthop_compare_full(const NextHop
*a
, const NextHop
*b
) {
126 /* This compares detailed configs, except for ID and ifindex. */
128 r
= CMP(a
->protocol
, b
->protocol
);
132 r
= CMP(a
->flags
, b
->flags
);
136 r
= CMP(hashmap_size(a
->group
), hashmap_size(b
->group
));
140 if (!hashmap_isempty(a
->group
)) {
141 struct nexthop_grp
*ga
;
143 HASHMAP_FOREACH(ga
, a
->group
) {
144 struct nexthop_grp
*gb
;
146 gb
= hashmap_get(b
->group
, UINT32_TO_PTR(ga
->id
));
150 r
= CMP(ga
->weight
, gb
->weight
);
156 r
= CMP(a
->blackhole
, b
->blackhole
);
160 r
= CMP(a
->family
, b
->family
);
164 if (IN_SET(a
->family
, AF_INET
, AF_INET6
)) {
165 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
173 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
174 _cleanup_(nexthop_freep
) NextHop
*dest
= NULL
;
175 struct nexthop_grp
*nhg
;
181 dest
= newdup(NextHop
, src
, 1);
185 /* unset all pointers */
186 dest
->manager
= NULL
;
187 dest
->network
= NULL
;
188 dest
->section
= NULL
;
191 HASHMAP_FOREACH(nhg
, src
->group
) {
192 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
194 g
= newdup(struct nexthop_grp
, nhg
, 1);
198 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
205 *ret
= TAKE_PTR(dest
);
209 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
211 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
214 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
222 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
231 static int nexthop_get(Link
*link
, const NextHop
*in
, NextHop
**ret
) {
236 assert(link
->manager
);
240 return nexthop_get_by_id(link
->manager
, in
->id
, ret
);
242 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
244 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
245 if (nexthop
->ifindex
!= ifindex
)
247 if (nexthop_compare_full(nexthop
, in
) != 0)
258 static int nexthop_get_request_by_id(Manager
*manager
, uint32_t id
, Request
**ret
) {
263 req
= ordered_set_get(
264 manager
->request_queue
,
266 .type
= REQUEST_TYPE_NEXTHOP
,
267 .userdata
= (void*) &(const NextHop
) { .id
= id
},
268 .hash_func
= (hash_func_t
) nexthop_hash_func
,
269 .compare_func
= (compare_func_t
) nexthop_compare_func
,
279 static int nexthop_get_request(Link
*link
, const NextHop
*in
, Request
**ret
) {
284 assert(link
->manager
);
288 return nexthop_get_request_by_id(link
->manager
, in
->id
, ret
);
290 ifindex
= nexthop_bound_to_link(in
) ? link
->ifindex
: 0;
292 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
293 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
296 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
297 if (nexthop
->ifindex
!= ifindex
)
299 if (nexthop_compare_full(nexthop
, in
) != 0)
310 static int nexthop_add_new(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
311 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
317 r
= nexthop_new(&nexthop
);
323 r
= hashmap_ensure_put(&manager
->nexthops_by_id
, &nexthop_hash_ops
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
329 nexthop
->manager
= manager
;
338 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
339 _cleanup_set_free_ Set
*ids
= NULL
;
349 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
350 * nexthop_section_verify(). */
351 assert(manager
->manage_foreign_nexthops
);
353 /* Find the lowest unused ID. */
355 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
358 HASHMAP_FOREACH(tmp
, network
->nexthops_by_section
) {
362 r
= set_ensure_put(&ids
, NULL
, UINT32_TO_PTR(tmp
->id
));
368 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
369 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
371 if (nexthop_get_request_by_id(manager
, id
, NULL
) >= 0)
373 if (set_contains(ids
, UINT32_TO_PTR(id
)))
383 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, Manager
*manager
) {
384 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
385 struct nexthop_grp
*nhg
;
395 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
396 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
397 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
399 HASHMAP_FOREACH(nhg
, nexthop
->group
)
400 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
402 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
403 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
405 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
406 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
409 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
414 /* link may be NULL. */
416 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
419 r
= sd_netlink_message_get_errno(m
);
420 if (r
< 0 && r
!= -ENOENT
)
421 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
426 static int nexthop_remove(NextHop
*nexthop
) {
427 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
433 manager
= ASSERT_PTR(ASSERT_PTR(nexthop
)->manager
);
435 /* link may be NULL. */
436 (void) link_get_by_index(manager
, nexthop
->ifindex
, &link
);
438 if (nexthop
->id
== 0) {
439 log_link_debug(link
, "Cannot remove nexthop without valid ID, ignoring.");
443 log_nexthop_debug(nexthop
, "Removing", manager
);
445 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
447 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
449 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
451 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
453 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
454 link
? link_netlink_destroy_callback
: NULL
, link
);
456 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
458 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
460 nexthop_enter_removing(nexthop
);
461 if (nexthop_get_request_by_id(manager
, nexthop
->id
, &req
) >= 0)
462 nexthop_enter_removing(req
->userdata
);
467 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
468 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
472 assert(nexthop
->id
> 0);
473 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
475 assert(link
->manager
);
476 assert(link
->manager
->rtnl
);
477 assert(link
->ifindex
> 0);
480 log_nexthop_debug(nexthop
, "Configuring", link
->manager
);
482 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
486 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
490 if (!hashmap_isempty(nexthop
->group
)) {
491 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
492 struct nexthop_grp
*p
, *nhg
;
494 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
499 HASHMAP_FOREACH(nhg
, nexthop
->group
)
502 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
506 } else if (nexthop
->blackhole
) {
507 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
511 assert(nexthop
->ifindex
== link
->ifindex
);
513 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, nexthop
->ifindex
);
517 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
518 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
522 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
528 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
531 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
537 r
= sd_netlink_message_get_errno(m
);
538 if (r
< 0 && r
!= -EEXIST
) {
539 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
540 link_enter_failed(link
);
544 if (link
->static_nexthop_messages
== 0) {
545 log_link_debug(link
, "Nexthops set");
546 link
->static_nexthops_configured
= true;
547 link_check_ready(link
);
553 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
554 struct nexthop_grp
*nhg
;
559 if (!link_is_ready_to_configure(link
, false))
562 if (nexthop_bound_to_link(nexthop
)) {
563 assert(nexthop
->ifindex
== link
->ifindex
);
565 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
566 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
568 if (link
->set_flags_messages
> 0)
570 if (!FLAGS_SET(link
->flags
, IFF_UP
))
574 /* All group members must be configured first. */
575 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
578 if (nexthop_get_by_id(link
->manager
, nhg
->id
, &g
) < 0)
581 if (!nexthop_exists(g
))
585 if (nexthop
->id
== 0) {
588 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
589 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
591 if (((NextHop
*) req
->userdata
)->id
!= 0)
592 return false; /* first configure nexthop with id. */
596 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
599 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
605 assert(link
->manager
);
608 if (!nexthop_is_ready_to_configure(link
, nexthop
))
611 r
= nexthop_configure(nexthop
, link
, req
);
613 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
615 nexthop_enter_configuring(nexthop
);
616 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, &existing
) >= 0)
617 nexthop_enter_configuring(existing
);
622 static int link_request_nexthop(Link
*link
, const NextHop
*nexthop
) {
623 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
624 NextHop
*existing
= NULL
;
628 assert(link
->manager
);
630 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
632 if (nexthop_get_request(link
, nexthop
, NULL
) >= 0)
633 return 0; /* already requested, skipping. */
635 r
= nexthop_dup(nexthop
, &tmp
);
639 if (nexthop_get(link
, nexthop
, &existing
) < 0) {
640 r
= nexthop_acquire_id(link
->manager
, tmp
);
645 assert(tmp
->id
== 0 || tmp
->id
== existing
->id
);
646 tmp
->id
= existing
->id
;
648 /* Copy state for logging below. */
649 tmp
->state
= existing
->state
;
652 if (nexthop_bound_to_link(tmp
))
653 tmp
->ifindex
= link
->ifindex
;
655 log_nexthop_debug(tmp
, "Requesting", link
->manager
);
656 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
660 nexthop_compare_func
,
661 nexthop_process_request
,
662 &link
->static_nexthop_messages
,
663 static_nexthop_handler
,
668 nexthop_enter_requesting(tmp
);
670 nexthop_enter_requesting(existing
);
676 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
681 assert(link
->network
);
683 link
->static_nexthops_configured
= false;
685 HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
686 if (only_ipv4
&& nh
->family
!= AF_INET
)
689 r
= link_request_nexthop(link
, nh
);
691 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
694 if (link
->static_nexthop_messages
== 0) {
695 link
->static_nexthops_configured
= true;
696 link_check_ready(link
);
698 log_link_debug(link
, "Requesting nexthops");
699 link_set_state(link
, LINK_STATE_CONFIGURING
);
705 static void link_mark_nexthops(Link
*link
, bool foreign
) {
710 assert(link
->manager
);
712 /* First, mark all nexthops. */
713 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
714 /* do not touch nexthop created by the kernel */
715 if (nexthop
->protocol
== RTPROT_KERNEL
)
718 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
719 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
722 /* Ignore nexthops not assigned yet or already removed. */
723 if (!nexthop_exists(nexthop
))
726 /* Ignore nexthops bound to other links. */
727 if (nexthop
->ifindex
> 0 && nexthop
->ifindex
!= link
->ifindex
)
730 nexthop_mark(nexthop
);
733 /* Then, unmark all nexthops requested by active links. */
734 HASHMAP_FOREACH(other
, link
->manager
->links_by_index
) {
735 if (!foreign
&& other
== link
)
738 if (!IN_SET(other
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
741 HASHMAP_FOREACH(nexthop
, other
->network
->nexthops_by_section
) {
744 if (nexthop_get(other
, nexthop
, &existing
) >= 0)
745 nexthop_unmark(existing
);
750 int link_drop_nexthops(Link
*link
, bool foreign
) {
755 assert(link
->manager
);
757 link_mark_nexthops(link
, foreign
);
759 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
760 if (!nexthop_is_marked(nexthop
))
763 RET_GATHER(r
, nexthop_remove(nexthop
));
769 void link_foreignize_nexthops(Link
*link
) {
773 assert(link
->manager
);
775 link_mark_nexthops(link
, /* foreign = */ false);
777 HASHMAP_FOREACH(nexthop
, link
->manager
->nexthops_by_id
) {
778 if (!nexthop_is_marked(nexthop
))
781 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
785 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
786 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
791 assert(group
|| size
== 0);
793 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
794 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
795 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
797 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
798 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
799 "rtnl: received nexthop message with invalid alignment, ignoring.");
801 n_group
= size
/ sizeof(struct nexthop_grp
);
802 for (size_t i
= 0; i
< n_group
; i
++) {
803 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
805 if (group
[i
].id
== 0) {
806 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
810 if (group
[i
].weight
> 254) {
811 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
815 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
819 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
823 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
830 hashmap_free_free(nexthop
->group
);
831 nexthop
->group
= TAKE_PTR(h
);
835 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
836 _cleanup_free_
void *raw_group
= NULL
;
837 size_t raw_group_size
;
839 uint32_t id
, ifindex
;
840 NextHop
*nexthop
= NULL
;
849 if (sd_netlink_message_is_error(message
)) {
850 r
= sd_netlink_message_get_errno(message
);
852 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
857 r
= sd_netlink_message_get_type(message
, &type
);
859 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
861 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
862 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
866 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &id
);
868 log_warning_errno(r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
871 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
873 } else if (id
== 0) {
874 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
878 (void) nexthop_get_by_id(m
, id
, &nexthop
);
879 (void) nexthop_get_request_by_id(m
, id
, &req
);
881 if (type
== RTM_DELNEXTHOP
) {
883 nexthop_enter_removed(nexthop
);
884 log_nexthop_debug(nexthop
, "Forgetting removed", m
);
885 nexthop_free(nexthop
);
887 log_nexthop_debug(&(const NextHop
) { .id
= id
}, "Kernel removed unknown", m
);
890 nexthop_enter_removed(req
->userdata
);
895 /* If we did not know the nexthop, then save it. */
897 r
= nexthop_add_new(m
, id
, &nexthop
);
899 log_warning_errno(r
, "Failed to add received nexthop, ignoring: %m");
906 /* Also update information that cannot be obtained through netlink notification. */
907 if (req
&& req
->waiting_reply
) {
908 NextHop
*n
= ASSERT_PTR(req
->userdata
);
910 nexthop
->source
= n
->source
;
913 r
= sd_rtnl_message_get_family(message
, &nexthop
->family
);
915 log_debug_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
917 r
= sd_rtnl_message_nexthop_get_protocol(message
, &nexthop
->protocol
);
919 log_debug_errno(r
, "rtnl: could not get nexthop protocol, ignoring: %m");
921 r
= sd_rtnl_message_nexthop_get_flags(message
, &nexthop
->flags
);
923 log_debug_errno(r
, "rtnl: could not get nexthop flags, ignoring: %m");
925 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
927 nexthop
->group
= hashmap_free_free(nexthop
->group
);
929 log_debug_errno(r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
931 (void) nexthop_update_group(nexthop
, raw_group
, raw_group_size
);
933 if (nexthop
->family
!= AF_UNSPEC
) {
934 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
936 nexthop
->gw
= IN_ADDR_NULL
;
938 log_debug_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
941 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
943 log_debug_errno(r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
945 nexthop
->blackhole
= r
;
947 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
949 nexthop
->ifindex
= 0;
951 log_debug_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
952 else if (ifindex
> INT32_MAX
)
953 log_debug_errno(r
, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
955 nexthop
->ifindex
= (int) ifindex
;
957 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
958 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
959 if (!nexthop_bound_to_link(nexthop
))
960 nexthop
->ifindex
= 0;
962 nexthop_enter_configured(nexthop
);
964 nexthop_enter_configured(req
->userdata
);
966 log_nexthop_debug(nexthop
, is_new
? "Remembering" : "Received remembered", m
);
970 static int nexthop_section_verify(NextHop
*nh
) {
971 if (section_is_invalid(nh
->section
))
974 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
975 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
976 "%s: [NextHop] section without specifying Id= is not supported "
977 "if ManageForeignNextHops=no is set in networkd.conf. "
978 "Ignoring [NextHop] section from line %u.",
979 nh
->section
->filename
, nh
->section
->line
);
981 if (!hashmap_isempty(nh
->group
)) {
982 if (in_addr_is_set(nh
->family
, &nh
->gw
))
983 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
984 "%s: nexthop group cannot have gateway address. "
985 "Ignoring [NextHop] section from line %u.",
986 nh
->section
->filename
, nh
->section
->line
);
988 if (nh
->family
!= AF_UNSPEC
)
989 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
990 "%s: nexthop group cannot have Family= setting. "
991 "Ignoring [NextHop] section from line %u.",
992 nh
->section
->filename
, nh
->section
->line
);
994 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
995 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
996 "%s: nexthop group cannot be a blackhole. "
997 "Ignoring [NextHop] section from line %u.",
998 nh
->section
->filename
, nh
->section
->line
);
999 } else if (nh
->family
== AF_UNSPEC
)
1000 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1001 nh
->family
= AF_INET
;
1003 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1004 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1005 "%s: blackhole nexthop cannot have gateway address. "
1006 "Ignoring [NextHop] section from line %u.",
1007 nh
->section
->filename
, nh
->section
->line
);
1009 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1010 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1011 /* If no address is configured, in most cases the gateway cannot be reachable.
1012 * TODO: we may need to improve the condition above. */
1013 log_warning("%s: Gateway= without static address configured. "
1014 "Enabling OnLink= option.",
1015 nh
->section
->filename
);
1019 if (nh
->onlink
>= 0)
1020 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1025 void network_drop_invalid_nexthops(Network
*network
) {
1030 HASHMAP_FOREACH(nh
, network
->nexthops_by_section
)
1031 if (nexthop_section_verify(nh
) < 0)
1035 int config_parse_nexthop_id(
1037 const char *filename
,
1039 const char *section
,
1040 unsigned section_line
,
1047 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1048 Network
*network
= userdata
;
1058 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1062 if (isempty(rvalue
)) {
1068 r
= safe_atou32(rvalue
, &id
);
1070 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1071 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1075 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1076 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1085 int config_parse_nexthop_gateway(
1087 const char *filename
,
1089 const char *section
,
1090 unsigned section_line
,
1097 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1098 Network
*network
= userdata
;
1107 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1111 if (isempty(rvalue
)) {
1112 n
->family
= AF_UNSPEC
;
1113 n
->gw
= IN_ADDR_NULL
;
1119 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1121 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1122 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1130 int config_parse_nexthop_family(
1132 const char *filename
,
1134 const char *section
,
1135 unsigned section_line
,
1142 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1143 Network
*network
= userdata
;
1153 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1157 if (isempty(rvalue
) &&
1158 !in_addr_is_set(n
->family
, &n
->gw
)) {
1159 /* Accept an empty string only when Gateway= is null or not specified. */
1160 n
->family
= AF_UNSPEC
;
1165 a
= nexthop_address_family_from_string(rvalue
);
1167 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1168 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1172 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1173 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1174 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1175 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1176 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1177 "ignoring assignment.", rvalue
);
1182 case ADDRESS_FAMILY_IPV4
:
1183 n
->family
= AF_INET
;
1185 case ADDRESS_FAMILY_IPV6
:
1186 n
->family
= AF_INET6
;
1189 assert_not_reached();
1196 int config_parse_nexthop_onlink(
1198 const char *filename
,
1200 const char *section
,
1201 unsigned section_line
,
1208 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1209 Network
*network
= userdata
;
1218 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1222 r
= parse_tristate(rvalue
, &n
->onlink
);
1224 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1225 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1233 int config_parse_nexthop_blackhole(
1235 const char *filename
,
1237 const char *section
,
1238 unsigned section_line
,
1245 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1246 Network
*network
= userdata
;
1255 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1259 r
= parse_boolean(rvalue
);
1261 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1262 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1272 int config_parse_nexthop_group(
1274 const char *filename
,
1276 const char *section
,
1277 unsigned section_line
,
1284 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1285 Network
*network
= userdata
;
1294 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1298 if (isempty(rvalue
)) {
1299 n
->group
= hashmap_free_free(n
->group
);
1304 for (const char *p
= rvalue
;;) {
1305 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1306 _cleanup_free_
char *word
= NULL
;
1310 r
= extract_first_word(&p
, &word
, NULL
, 0);
1314 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1315 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1321 nhg
= new0(struct nexthop_grp
, 1);
1325 sep
= strchr(word
, ':');
1328 r
= safe_atou32(sep
, &w
);
1330 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1331 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1335 if (w
== 0 || w
> 256) {
1336 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1337 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1341 /* See comments in config_parse_multipath_route(). */
1342 nhg
->weight
= w
- 1;
1345 r
= safe_atou32(word
, &nhg
->id
);
1347 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1348 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1349 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1353 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1354 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1355 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1359 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1363 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1364 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1365 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));