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
);
33 set_remove(nexthop
->link
->nexthops
, nexthop
);
35 if (nexthop
->link
->manager
&& nexthop
->id
> 0)
36 hashmap_remove(nexthop
->link
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
39 if (nexthop
->manager
) {
40 set_remove(nexthop
->manager
->nexthops
, nexthop
);
43 hashmap_remove(nexthop
->manager
->nexthops_by_id
, UINT32_TO_PTR(nexthop
->id
));
46 hashmap_free_free(nexthop
->group
);
48 return mfree(nexthop
);
51 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop
, nexthop_free
);
53 static int nexthop_new(NextHop
**ret
) {
54 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
56 nexthop
= new(NextHop
, 1);
60 *nexthop
= (NextHop
) {
65 *ret
= TAKE_PTR(nexthop
);
70 static int nexthop_new_static(Network
*network
, const char *filename
, unsigned section_line
, NextHop
**ret
) {
71 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
72 _cleanup_(nexthop_freep
) NextHop
*nexthop
= NULL
;
78 assert(section_line
> 0);
80 r
= config_section_new(filename
, section_line
, &n
);
84 nexthop
= hashmap_get(network
->nexthops_by_section
, n
);
86 *ret
= TAKE_PTR(nexthop
);
90 r
= nexthop_new(&nexthop
);
94 nexthop
->protocol
= RTPROT_STATIC
;
95 nexthop
->network
= network
;
96 nexthop
->section
= TAKE_PTR(n
);
97 nexthop
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
99 r
= hashmap_ensure_put(&network
->nexthops_by_section
, &config_section_hash_ops
, nexthop
->section
, nexthop
);
103 *ret
= TAKE_PTR(nexthop
);
107 static void nexthop_hash_func(const NextHop
*nexthop
, struct siphash
*state
) {
110 siphash24_compress(&nexthop
->protocol
, sizeof(nexthop
->protocol
), state
);
111 siphash24_compress(&nexthop
->id
, sizeof(nexthop
->id
), state
);
112 siphash24_compress(&nexthop
->blackhole
, sizeof(nexthop
->blackhole
), state
);
113 siphash24_compress(&nexthop
->family
, sizeof(nexthop
->family
), state
);
115 switch (nexthop
->family
) {
118 siphash24_compress(&nexthop
->gw
, FAMILY_ADDRESS_SIZE(nexthop
->family
), state
);
122 /* treat any other address family as AF_UNSPEC */
127 static int nexthop_compare_func(const NextHop
*a
, const NextHop
*b
) {
130 r
= CMP(a
->protocol
, b
->protocol
);
134 r
= CMP(a
->id
, b
->id
);
138 r
= CMP(a
->blackhole
, b
->blackhole
);
142 r
= CMP(a
->family
, b
->family
);
146 if (IN_SET(a
->family
, AF_INET
, AF_INET6
))
147 return memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
152 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
156 nexthop_compare_func
,
159 static bool nexthop_equal(const NextHop
*a
, const NextHop
*b
) {
166 return nexthop_compare_func(a
, b
) == 0;
169 static int nexthop_dup(const NextHop
*src
, NextHop
**ret
) {
170 _cleanup_(nexthop_freep
) NextHop
*dest
= NULL
;
171 struct nexthop_grp
*nhg
;
177 dest
= newdup(NextHop
, src
, 1);
181 /* unset all pointers */
182 dest
->manager
= NULL
;
184 dest
->network
= NULL
;
185 dest
->section
= NULL
;
188 HASHMAP_FOREACH(nhg
, src
->group
) {
189 _cleanup_free_
struct nexthop_grp
*g
= NULL
;
191 g
= newdup(struct nexthop_grp
, nhg
, 1);
195 r
= hashmap_ensure_put(&dest
->group
, NULL
, UINT32_TO_PTR(g
->id
), g
);
202 *ret
= TAKE_PTR(dest
);
206 static bool nexthop_bound_to_link(const NextHop
*nexthop
) {
208 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
211 int nexthop_get_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
219 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
228 static int nexthop_get(Manager
*manager
, Link
*link
, NextHop
*in
, NextHop
**ret
) {
234 if (nexthop_bound_to_link(in
)) {
238 nexthops
= link
->nexthops
;
243 nexthops
= manager
->nexthops
;
246 nexthop
= set_get(nexthops
, in
);
256 /* Also find nexthop configured without ID. */
257 SET_FOREACH(nexthop
, nexthops
) {
263 found
= nexthop_equal(nexthop
, in
);
277 static int nexthop_add(Manager
*manager
, Link
*link
, NextHop
*nexthop
) {
281 assert(nexthop
->id
> 0);
283 if (nexthop_bound_to_link(nexthop
)) {
286 r
= set_ensure_put(&link
->nexthops
, &nexthop_hash_ops
, nexthop
);
292 nexthop
->link
= link
;
294 manager
= link
->manager
;
298 r
= set_ensure_put(&manager
->nexthops
, &nexthop_hash_ops
, nexthop
);
304 nexthop
->manager
= manager
;
307 return hashmap_ensure_put(&manager
->nexthops_by_id
, NULL
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
310 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
311 _cleanup_set_free_ Set
*ids
= NULL
;
321 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
322 * nexthop_section_verify(). */
323 assert(manager
->manage_foreign_nexthops
);
325 /* Find the lowest unused ID. */
327 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
330 HASHMAP_FOREACH(tmp
, network
->nexthops_by_section
) {
334 r
= set_ensure_put(&ids
, NULL
, UINT32_TO_PTR(tmp
->id
));
340 for (uint32_t id
= 1; id
< UINT32_MAX
; id
++) {
341 if (nexthop_get_by_id(manager
, id
, NULL
) >= 0)
343 if (set_contains(ids
, UINT32_TO_PTR(id
)))
353 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, const Link
*link
) {
354 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
355 struct nexthop_grp
*nhg
;
360 /* link may be NULL. */
365 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
366 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
368 HASHMAP_FOREACH(nhg
, nexthop
->group
)
369 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
371 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
372 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
374 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
375 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
378 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
383 /* link may be NULL. */
385 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
388 r
= sd_netlink_message_get_errno(m
);
389 if (r
< 0 && r
!= -ENOENT
)
390 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
395 static int nexthop_remove(NextHop
*nexthop
) {
396 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
402 assert(nexthop
->manager
|| (nexthop
->link
&& nexthop
->link
->manager
));
404 /* link may be NULL. */
405 link
= nexthop
->link
;
406 manager
= nexthop
->manager
?: nexthop
->link
->manager
;
408 if (nexthop
->id
== 0) {
409 log_link_debug(link
, "Cannot remove nexthop without valid ID, ignoring.");
413 log_nexthop_debug(nexthop
, "Removing", link
);
415 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &m
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
417 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
419 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
421 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
423 r
= netlink_call_async(manager
->rtnl
, NULL
, m
, nexthop_remove_handler
,
424 link
? link_netlink_destroy_callback
: NULL
, link
);
426 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
428 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
430 nexthop_enter_removing(nexthop
);
434 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
435 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
439 assert(nexthop
->id
> 0);
440 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
442 assert(link
->manager
);
443 assert(link
->manager
->rtnl
);
444 assert(link
->ifindex
> 0);
447 log_nexthop_debug(nexthop
, "Configuring", link
);
449 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
453 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
457 if (!hashmap_isempty(nexthop
->group
)) {
458 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
459 struct nexthop_grp
*p
, *nhg
;
461 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
466 HASHMAP_FOREACH(nhg
, nexthop
->group
)
469 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
473 } else if (nexthop
->blackhole
) {
474 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
478 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, link
->ifindex
);
482 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
483 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
487 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
493 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
496 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
502 r
= sd_netlink_message_get_errno(m
);
503 if (r
< 0 && r
!= -EEXIST
) {
504 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
505 link_enter_failed(link
);
509 if (link
->static_nexthop_messages
== 0) {
510 log_link_debug(link
, "Nexthops set");
511 link
->static_nexthops_configured
= true;
512 link_check_ready(link
);
518 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
519 struct nexthop_grp
*nhg
;
524 if (!link_is_ready_to_configure(link
, false))
527 if (nexthop_bound_to_link(nexthop
)) {
528 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
529 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
531 if (link
->set_flags_messages
> 0)
533 if (!FLAGS_SET(link
->flags
, IFF_UP
))
537 /* All group members must be configured first. */
538 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
541 if (nexthop_get_by_id(link
->manager
, nhg
->id
, &g
) < 0)
544 if (!nexthop_exists(g
))
548 if (nexthop
->id
== 0) {
551 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
552 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
554 if (((NextHop
*) req
->userdata
)->id
!= 0)
555 return false; /* first configure nexthop with id. */
559 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
562 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
569 if (!nexthop_is_ready_to_configure(link
, nexthop
))
572 r
= nexthop_configure(nexthop
, link
, req
);
574 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
576 nexthop_enter_configuring(nexthop
);
580 static int link_request_nexthop(Link
*link
, NextHop
*nexthop
) {
586 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
588 if (nexthop_get(link
->manager
, link
, nexthop
, &existing
) < 0) {
589 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
591 r
= nexthop_dup(nexthop
, &tmp
);
595 r
= nexthop_acquire_id(link
->manager
, tmp
);
599 r
= nexthop_add(link
->manager
, link
, tmp
);
603 existing
= TAKE_PTR(tmp
);
605 existing
->source
= nexthop
->source
;
607 log_nexthop_debug(existing
, "Requesting", link
);
608 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
611 nexthop_compare_func
,
612 nexthop_process_request
,
613 &link
->static_nexthop_messages
,
614 static_nexthop_handler
,
619 nexthop_enter_requesting(existing
);
623 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
628 assert(link
->network
);
630 link
->static_nexthops_configured
= false;
632 HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
633 if (only_ipv4
&& nh
->family
!= AF_INET
)
636 r
= link_request_nexthop(link
, nh
);
638 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
641 if (link
->static_nexthop_messages
== 0) {
642 link
->static_nexthops_configured
= true;
643 link_check_ready(link
);
645 log_link_debug(link
, "Requesting nexthops");
646 link_set_state(link
, LINK_STATE_CONFIGURING
);
652 static void manager_mark_nexthops(Manager
*manager
, bool foreign
, const Link
*except
) {
658 /* First, mark all nexthops. */
659 SET_FOREACH(nexthop
, manager
->nexthops
) {
660 /* do not touch nexthop created by the kernel */
661 if (nexthop
->protocol
== RTPROT_KERNEL
)
664 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
665 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
668 /* Ignore nexthops not assigned yet or already removed. */
669 if (!nexthop_exists(nexthop
))
672 nexthop_mark(nexthop
);
675 /* Then, unmark all nexthops requested by active links. */
676 HASHMAP_FOREACH(link
, manager
->links_by_index
) {
680 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
683 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
686 if (nexthop_get(manager
, NULL
, nexthop
, &existing
) >= 0)
687 nexthop_unmark(existing
);
692 static int manager_drop_marked_nexthops(Manager
*manager
) {
698 SET_FOREACH(nexthop
, manager
->nexthops
) {
699 if (!nexthop_is_marked(nexthop
))
702 RET_GATHER(r
, nexthop_remove(nexthop
));
708 int link_drop_foreign_nexthops(Link
*link
) {
713 assert(link
->manager
);
714 assert(link
->network
);
716 /* First, mark all nexthops. */
717 SET_FOREACH(nexthop
, link
->nexthops
) {
718 /* do not touch nexthop created by the kernel */
719 if (nexthop
->protocol
== RTPROT_KERNEL
)
722 /* Do not remove nexthops we configured. */
723 if (nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
)
726 /* Ignore nexthops not assigned yet or already removed. */
727 if (!nexthop_exists(nexthop
))
730 nexthop_mark(nexthop
);
733 /* Then, unmark all nexthops requested by active links. */
734 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
737 if (nexthop_get(NULL
, link
, nexthop
, &existing
) >= 0)
738 nexthop_unmark(existing
);
741 /* Finally, remove all marked rules. */
742 SET_FOREACH(nexthop
, link
->nexthops
) {
743 if (!nexthop_is_marked(nexthop
))
746 RET_GATHER(r
, nexthop_remove(nexthop
));
749 manager_mark_nexthops(link
->manager
, /* foreign = */ true, NULL
);
751 return RET_GATHER(r
, manager_drop_marked_nexthops(link
->manager
));
754 int link_drop_managed_nexthops(Link
*link
) {
759 assert(link
->manager
);
761 SET_FOREACH(nexthop
, link
->nexthops
) {
762 /* do not touch nexthop created by the kernel */
763 if (nexthop
->protocol
== RTPROT_KERNEL
)
766 /* Do not touch addresses managed by kernel or other tools. */
767 if (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
)
770 /* Ignore nexthops not assigned yet or already removing. */
771 if (!nexthop_exists(nexthop
))
774 RET_GATHER(r
, nexthop_remove(nexthop
));
777 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
779 return RET_GATHER(r
, manager_drop_marked_nexthops(link
->manager
));
782 void link_foreignize_nexthops(Link
*link
) {
787 SET_FOREACH(nexthop
, link
->nexthops
)
788 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
790 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
792 SET_FOREACH(nexthop
, link
->manager
->nexthops
) {
793 if (!nexthop_is_marked(nexthop
))
796 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
800 static int nexthop_update_group(NextHop
*nexthop
, const struct nexthop_grp
*group
, size_t size
) {
801 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
806 assert(group
|| size
== 0);
808 if (size
== 0 || size
% sizeof(struct nexthop_grp
) != 0)
809 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
810 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
812 if ((uintptr_t) group
% alignof(struct nexthop_grp
) != 0)
813 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
814 "rtnl: received nexthop message with invalid alignment, ignoring.");
816 n_group
= size
/ sizeof(struct nexthop_grp
);
817 for (size_t i
= 0; i
< n_group
; i
++) {
818 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
820 if (group
[i
].id
== 0) {
821 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
825 if (group
[i
].weight
> 254) {
826 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
830 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
834 r
= hashmap_ensure_put(&h
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
838 log_debug_errno(r
, "Failed to store nexthop group, ignoring: %m");
845 hashmap_free_free(nexthop
->group
);
846 nexthop
->group
= TAKE_PTR(h
);
850 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
851 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
852 _cleanup_free_
void *raw_group
= NULL
;
853 NextHop
*nexthop
= NULL
;
854 size_t raw_group_size
;
864 if (sd_netlink_message_is_error(message
)) {
865 r
= sd_netlink_message_get_errno(message
);
867 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
872 r
= sd_netlink_message_get_type(message
, &type
);
874 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
876 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
877 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
881 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
882 if (r
< 0 && r
!= -ENODATA
) {
883 log_warning_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
887 log_warning("rtnl: received nexthop message with invalid ifindex %"PRIu32
", ignoring.", ifindex
);
891 r
= link_get_by_index(m
, ifindex
, &link
);
894 log_warning("rtnl: received nexthop message for link (%"PRIu32
") we do not know about, ignoring", ifindex
);
899 r
= nexthop_new(&tmp
);
903 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
905 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop family, ignoring: %m");
907 } else if (!IN_SET(tmp
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
)) {
908 log_link_debug(link
, "rtnl: received nexthop message with invalid family %d, ignoring.", tmp
->family
);
912 r
= sd_rtnl_message_nexthop_get_protocol(message
, &tmp
->protocol
);
914 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop protocol, ignoring: %m");
918 r
= sd_rtnl_message_nexthop_get_flags(message
, &tmp
->flags
);
920 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop flags, ignoring: %m");
924 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
925 if (r
< 0 && r
!= -ENODATA
) {
926 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
929 r
= nexthop_update_group(tmp
, raw_group
, raw_group_size
);
934 if (tmp
->family
!= AF_UNSPEC
) {
935 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, tmp
->family
, &tmp
->gw
);
936 if (r
< 0 && r
!= -ENODATA
) {
937 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
942 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
944 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
949 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &tmp
->id
);
951 log_link_warning_errno(link
, r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
954 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
956 } else if (tmp
->id
== 0) {
957 log_link_warning(link
, "rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
961 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
962 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
963 if (!nexthop_bound_to_link(tmp
))
966 (void) nexthop_get(m
, link
, tmp
, &nexthop
);
971 nexthop
->flags
= tmp
->flags
;
972 nexthop_enter_configured(nexthop
);
973 log_nexthop_debug(tmp
, "Received remembered", link
);
975 nexthop_enter_configured(tmp
);
976 log_nexthop_debug(tmp
, "Remembering", link
);
978 r
= nexthop_add(m
, link
, tmp
);
980 log_link_warning_errno(link
, r
, "Could not remember foreign nexthop, ignoring: %m");
990 nexthop_enter_removed(nexthop
);
991 if (nexthop
->state
== 0) {
992 log_nexthop_debug(nexthop
, "Forgetting", link
);
993 nexthop_free(nexthop
);
995 log_nexthop_debug(nexthop
, "Removed", link
);
997 log_nexthop_debug(tmp
, "Kernel removed unknown", link
);
1001 assert_not_reached();
1007 static int nexthop_section_verify(NextHop
*nh
) {
1008 if (section_is_invalid(nh
->section
))
1011 if (!nh
->network
->manager
->manage_foreign_nexthops
&& nh
->id
== 0)
1012 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1013 "%s: [NextHop] section without specifying Id= is not supported "
1014 "if ManageForeignNextHops=no is set in networkd.conf. "
1015 "Ignoring [NextHop] section from line %u.",
1016 nh
->section
->filename
, nh
->section
->line
);
1018 if (!hashmap_isempty(nh
->group
)) {
1019 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1020 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1021 "%s: nexthop group cannot have gateway address. "
1022 "Ignoring [NextHop] section from line %u.",
1023 nh
->section
->filename
, nh
->section
->line
);
1025 if (nh
->family
!= AF_UNSPEC
)
1026 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1027 "%s: nexthop group cannot have Family= setting. "
1028 "Ignoring [NextHop] section from line %u.",
1029 nh
->section
->filename
, nh
->section
->line
);
1031 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1032 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1033 "%s: nexthop group cannot be a blackhole. "
1034 "Ignoring [NextHop] section from line %u.",
1035 nh
->section
->filename
, nh
->section
->line
);
1036 } else if (nh
->family
== AF_UNSPEC
)
1037 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1038 nh
->family
= AF_INET
;
1040 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1041 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1042 "%s: blackhole nexthop cannot have gateway address. "
1043 "Ignoring [NextHop] section from line %u.",
1044 nh
->section
->filename
, nh
->section
->line
);
1046 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1047 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1048 /* If no address is configured, in most cases the gateway cannot be reachable.
1049 * TODO: we may need to improve the condition above. */
1050 log_warning("%s: Gateway= without static address configured. "
1051 "Enabling OnLink= option.",
1052 nh
->section
->filename
);
1056 if (nh
->onlink
>= 0)
1057 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1062 void network_drop_invalid_nexthops(Network
*network
) {
1067 HASHMAP_FOREACH(nh
, network
->nexthops_by_section
)
1068 if (nexthop_section_verify(nh
) < 0)
1072 int config_parse_nexthop_id(
1074 const char *filename
,
1076 const char *section
,
1077 unsigned section_line
,
1084 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1085 Network
*network
= userdata
;
1095 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1099 if (isempty(rvalue
)) {
1105 r
= safe_atou32(rvalue
, &id
);
1107 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1108 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1112 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1113 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1122 int config_parse_nexthop_gateway(
1124 const char *filename
,
1126 const char *section
,
1127 unsigned section_line
,
1134 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1135 Network
*network
= userdata
;
1144 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1148 if (isempty(rvalue
)) {
1149 n
->family
= AF_UNSPEC
;
1150 n
->gw
= IN_ADDR_NULL
;
1156 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1158 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1159 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1167 int config_parse_nexthop_family(
1169 const char *filename
,
1171 const char *section
,
1172 unsigned section_line
,
1179 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1180 Network
*network
= userdata
;
1190 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1194 if (isempty(rvalue
) &&
1195 !in_addr_is_set(n
->family
, &n
->gw
)) {
1196 /* Accept an empty string only when Gateway= is null or not specified. */
1197 n
->family
= AF_UNSPEC
;
1202 a
= nexthop_address_family_from_string(rvalue
);
1204 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1205 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1209 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1210 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1211 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1212 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1213 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1214 "ignoring assignment.", rvalue
);
1219 case ADDRESS_FAMILY_IPV4
:
1220 n
->family
= AF_INET
;
1222 case ADDRESS_FAMILY_IPV6
:
1223 n
->family
= AF_INET6
;
1226 assert_not_reached();
1233 int config_parse_nexthop_onlink(
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_tristate(rvalue
, &n
->onlink
);
1261 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1262 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1270 int config_parse_nexthop_blackhole(
1272 const char *filename
,
1274 const char *section
,
1275 unsigned section_line
,
1282 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1283 Network
*network
= userdata
;
1292 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1296 r
= parse_boolean(rvalue
);
1298 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1299 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1309 int config_parse_nexthop_group(
1311 const char *filename
,
1313 const char *section
,
1314 unsigned section_line
,
1321 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1322 Network
*network
= userdata
;
1331 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1335 if (isempty(rvalue
)) {
1336 n
->group
= hashmap_free_free(n
->group
);
1341 for (const char *p
= rvalue
;;) {
1342 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1343 _cleanup_free_
char *word
= NULL
;
1347 r
= extract_first_word(&p
, &word
, NULL
, 0);
1351 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1352 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1358 nhg
= new0(struct nexthop_grp
, 1);
1362 sep
= strchr(word
, ':');
1365 r
= safe_atou32(sep
, &w
);
1367 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1368 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1372 if (w
== 0 || w
> 256) {
1373 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1374 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1378 /* See comments in config_parse_multipath_route(). */
1379 nhg
->weight
= w
- 1;
1382 r
= safe_atou32(word
, &nhg
->id
);
1384 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1385 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1386 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1390 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1391 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1392 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1396 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1400 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1401 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1402 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));