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 int manager_get_nexthop_by_id(Manager
*manager
, uint32_t id
, NextHop
**ret
) {
214 nh
= hashmap_get(manager
->nexthops_by_id
, UINT32_TO_PTR(id
));
223 static bool nexthop_owned_by_link(const NextHop
*nexthop
) {
224 return !nexthop
->blackhole
&& hashmap_isempty(nexthop
->group
);
227 static int nexthop_get(Manager
*manager
, Link
*link
, NextHop
*in
, NextHop
**ret
) {
233 if (nexthop_owned_by_link(in
)) {
237 nexthops
= link
->nexthops
;
242 nexthops
= manager
->nexthops
;
245 nexthop
= set_get(nexthops
, in
);
255 /* Also find nexthop configured without ID. */
256 SET_FOREACH(nexthop
, nexthops
) {
262 found
= nexthop_equal(nexthop
, in
);
276 static int nexthop_add(Manager
*manager
, Link
*link
, NextHop
*nexthop
) {
280 assert(nexthop
->id
> 0);
282 if (nexthop_owned_by_link(nexthop
)) {
285 r
= set_ensure_put(&link
->nexthops
, &nexthop_hash_ops
, nexthop
);
291 nexthop
->link
= link
;
293 manager
= link
->manager
;
297 r
= set_ensure_put(&manager
->nexthops
, &nexthop_hash_ops
, nexthop
);
303 nexthop
->manager
= manager
;
306 return hashmap_ensure_put(&manager
->nexthops_by_id
, NULL
, UINT32_TO_PTR(nexthop
->id
), nexthop
);
309 static int nexthop_acquire_id(Manager
*manager
, NextHop
*nexthop
) {
310 _cleanup_set_free_ Set
*ids
= NULL
;
321 /* Find the lowest unused ID. */
323 ORDERED_HASHMAP_FOREACH(network
, manager
->networks
) {
326 HASHMAP_FOREACH(tmp
, network
->nexthops_by_section
) {
330 r
= set_ensure_put(&ids
, NULL
, UINT32_TO_PTR(tmp
->id
));
336 for (id
= 1; id
< UINT32_MAX
; id
++) {
337 if (manager_get_nexthop_by_id(manager
, id
, NULL
) >= 0)
339 if (set_contains(ids
, UINT32_TO_PTR(id
)))
348 static void log_nexthop_debug(const NextHop
*nexthop
, const char *str
, const Link
*link
) {
349 _cleanup_free_
char *state
= NULL
, *group
= NULL
, *flags
= NULL
;
350 struct nexthop_grp
*nhg
;
355 /* link may be NULL. */
360 (void) network_config_state_to_string_alloc(nexthop
->state
, &state
);
361 (void) route_flags_to_string_alloc(nexthop
->flags
, &flags
);
363 HASHMAP_FOREACH(nhg
, nexthop
->group
)
364 (void) strextendf_with_separator(&group
, ",", "%"PRIu32
":%"PRIu32
, nhg
->id
, nhg
->weight
+1u);
366 log_link_debug(link
, "%s %s nexthop (%s): id: %"PRIu32
", gw: %s, blackhole: %s, group: %s, flags: %s",
367 str
, strna(network_config_source_to_string(nexthop
->source
)), strna(state
),
369 IN_ADDR_TO_STRING(nexthop
->family
, &nexthop
->gw
),
370 yes_no(nexthop
->blackhole
), strna(group
), strna(flags
));
373 static int nexthop_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
378 /* link may be NULL. */
380 if (link
&& IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
383 r
= sd_netlink_message_get_errno(m
);
384 if (r
< 0 && r
!= -ENOENT
)
385 log_link_message_warning_errno(link
, m
, r
, "Could not drop nexthop, ignoring");
390 static int nexthop_remove(NextHop
*nexthop
) {
391 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
397 assert(nexthop
->manager
|| (nexthop
->link
&& nexthop
->link
->manager
));
399 /* link may be NULL. */
400 link
= nexthop
->link
;
401 manager
= nexthop
->manager
?: nexthop
->link
->manager
;
403 if (nexthop
->id
== 0) {
404 log_link_debug(link
, "Cannot remove nexthop without valid ID, ignoring.");
408 log_nexthop_debug(nexthop
, "Removing", link
);
410 r
= sd_rtnl_message_new_nexthop(manager
->rtnl
, &req
, RTM_DELNEXTHOP
, AF_UNSPEC
, RTPROT_UNSPEC
);
412 return log_link_error_errno(link
, r
, "Could not create RTM_DELNEXTHOP message: %m");
414 r
= sd_netlink_message_append_u32(req
, NHA_ID
, nexthop
->id
);
416 return log_link_error_errno(link
, r
, "Could not append NHA_ID attribute: %m");
418 r
= netlink_call_async(manager
->rtnl
, NULL
, req
, nexthop_remove_handler
,
419 link
? link_netlink_destroy_callback
: NULL
, link
);
421 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
423 link_ref(link
); /* link may be NULL, link_ref() is OK with that */
425 nexthop_enter_removing(nexthop
);
429 static int nexthop_configure(NextHop
*nexthop
, Link
*link
, Request
*req
) {
430 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
434 assert(IN_SET(nexthop
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
));
436 assert(link
->manager
);
437 assert(link
->manager
->rtnl
);
438 assert(link
->ifindex
> 0);
441 log_nexthop_debug(nexthop
, "Configuring", link
);
443 r
= sd_rtnl_message_new_nexthop(link
->manager
->rtnl
, &m
, RTM_NEWNEXTHOP
, nexthop
->family
, nexthop
->protocol
);
447 if (nexthop
->id
> 0) {
448 r
= sd_netlink_message_append_u32(m
, NHA_ID
, nexthop
->id
);
453 if (!hashmap_isempty(nexthop
->group
)) {
454 _cleanup_free_
struct nexthop_grp
*group
= NULL
;
455 struct nexthop_grp
*p
, *nhg
;
457 group
= new(struct nexthop_grp
, hashmap_size(nexthop
->group
));
462 HASHMAP_FOREACH(nhg
, nexthop
->group
)
465 r
= sd_netlink_message_append_data(m
, NHA_GROUP
, group
, sizeof(struct nexthop_grp
) * hashmap_size(nexthop
->group
));
469 } else if (nexthop
->blackhole
) {
470 r
= sd_netlink_message_append_flag(m
, NHA_BLACKHOLE
);
474 r
= sd_netlink_message_append_u32(m
, NHA_OIF
, link
->ifindex
);
478 if (in_addr_is_set(nexthop
->family
, &nexthop
->gw
)) {
479 r
= netlink_message_append_in_addr_union(m
, NHA_GATEWAY
, nexthop
->family
, &nexthop
->gw
);
483 r
= sd_rtnl_message_nexthop_set_flags(m
, nexthop
->flags
& RTNH_F_ONLINK
);
489 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
492 static int static_nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, NextHop
*nexthop
) {
498 r
= sd_netlink_message_get_errno(m
);
499 if (r
< 0 && r
!= -EEXIST
) {
500 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
501 link_enter_failed(link
);
505 if (link
->static_nexthop_messages
== 0) {
506 log_link_debug(link
, "Nexthops set");
507 link
->static_nexthops_configured
= true;
508 link_check_ready(link
);
514 static bool nexthop_is_ready_to_configure(Link
*link
, const NextHop
*nexthop
) {
515 struct nexthop_grp
*nhg
;
520 if (!link_is_ready_to_configure(link
, false))
523 if (nexthop_owned_by_link(nexthop
)) {
524 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
525 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
527 if (link
->set_flags_messages
> 0)
529 if (!FLAGS_SET(link
->flags
, IFF_UP
))
533 /* All group members must be configured first. */
534 HASHMAP_FOREACH(nhg
, nexthop
->group
) {
537 if (manager_get_nexthop_by_id(link
->manager
, nhg
->id
, &g
) < 0)
540 if (!nexthop_exists(g
))
544 if (nexthop
->id
== 0) {
547 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
548 if (req
->type
!= REQUEST_TYPE_NEXTHOP
)
550 if (((NextHop
*) req
->userdata
)->id
!= 0)
551 return false; /* first configure nexthop with id. */
555 return gateway_is_ready(link
, FLAGS_SET(nexthop
->flags
, RTNH_F_ONLINK
), nexthop
->family
, &nexthop
->gw
);
558 static int nexthop_process_request(Request
*req
, Link
*link
, NextHop
*nexthop
) {
565 if (!nexthop_is_ready_to_configure(link
, nexthop
))
568 r
= nexthop_configure(nexthop
, link
, req
);
570 return log_link_warning_errno(link
, r
, "Failed to configure nexthop");
572 nexthop_enter_configuring(nexthop
);
576 static int link_request_nexthop(Link
*link
, NextHop
*nexthop
) {
582 assert(nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
584 if (nexthop_get(link
->manager
, link
, nexthop
, &existing
) < 0) {
585 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
587 r
= nexthop_dup(nexthop
, &tmp
);
591 r
= nexthop_acquire_id(link
->manager
, tmp
);
595 r
= nexthop_add(link
->manager
, link
, tmp
);
599 existing
= TAKE_PTR(tmp
);
601 existing
->source
= nexthop
->source
;
603 log_nexthop_debug(existing
, "Requesting", link
);
604 r
= link_queue_request_safe(link
, REQUEST_TYPE_NEXTHOP
,
607 nexthop_compare_func
,
608 nexthop_process_request
,
609 &link
->static_nexthop_messages
,
610 static_nexthop_handler
,
615 nexthop_enter_requesting(existing
);
619 int link_request_static_nexthops(Link
*link
, bool only_ipv4
) {
624 assert(link
->network
);
626 link
->static_nexthops_configured
= false;
628 HASHMAP_FOREACH(nh
, link
->network
->nexthops_by_section
) {
629 if (only_ipv4
&& nh
->family
!= AF_INET
)
632 r
= link_request_nexthop(link
, nh
);
634 return log_link_warning_errno(link
, r
, "Could not request nexthop: %m");
637 if (link
->static_nexthop_messages
== 0) {
638 link
->static_nexthops_configured
= true;
639 link_check_ready(link
);
641 log_link_debug(link
, "Requesting nexthops");
642 link_set_state(link
, LINK_STATE_CONFIGURING
);
648 static void manager_mark_nexthops(Manager
*manager
, bool foreign
, const Link
*except
) {
654 /* First, mark all nexthops. */
655 SET_FOREACH(nexthop
, manager
->nexthops
) {
656 /* do not touch nexthop created by the kernel */
657 if (nexthop
->protocol
== RTPROT_KERNEL
)
660 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
661 if (foreign
!= (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
))
664 /* Ignore nexthops not assigned yet or already removed. */
665 if (!nexthop_exists(nexthop
))
668 nexthop_mark(nexthop
);
671 /* Then, unmark all nexthops requested by active links. */
672 HASHMAP_FOREACH(link
, manager
->links_by_index
) {
676 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
679 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
682 if (nexthop_get(manager
, NULL
, nexthop
, &existing
) >= 0)
683 nexthop_unmark(existing
);
688 static int manager_drop_marked_nexthops(Manager
*manager
) {
694 SET_FOREACH(nexthop
, manager
->nexthops
) {
695 if (!nexthop_is_marked(nexthop
))
698 k
= nexthop_remove(nexthop
);
706 int link_drop_foreign_nexthops(Link
*link
) {
711 assert(link
->manager
);
712 assert(link
->network
);
714 /* First, mark all nexthops. */
715 SET_FOREACH(nexthop
, link
->nexthops
) {
716 /* do not touch nexthop created by the kernel */
717 if (nexthop
->protocol
== RTPROT_KERNEL
)
720 /* Do not remove nexthops we configured. */
721 if (nexthop
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
)
724 /* Ignore nexthops not assigned yet or already removed. */
725 if (!nexthop_exists(nexthop
))
728 nexthop_mark(nexthop
);
731 /* Then, unmark all nexthops requested by active links. */
732 HASHMAP_FOREACH(nexthop
, link
->network
->nexthops_by_section
) {
735 if (nexthop_get(NULL
, link
, nexthop
, &existing
) >= 0)
736 nexthop_unmark(existing
);
739 /* Finally, remove all marked rules. */
740 SET_FOREACH(nexthop
, link
->nexthops
) {
741 if (!nexthop_is_marked(nexthop
))
744 k
= nexthop_remove(nexthop
);
749 manager_mark_nexthops(link
->manager
, /* foreign = */ true, NULL
);
751 k
= manager_drop_marked_nexthops(link
->manager
);
758 int link_drop_managed_nexthops(Link
*link
) {
763 assert(link
->manager
);
765 SET_FOREACH(nexthop
, link
->nexthops
) {
766 /* do not touch nexthop created by the kernel */
767 if (nexthop
->protocol
== RTPROT_KERNEL
)
770 /* Do not touch addresses managed by kernel or other tools. */
771 if (nexthop
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
)
774 /* Ignore nexthops not assigned yet or already removing. */
775 if (!nexthop_exists(nexthop
))
778 k
= nexthop_remove(nexthop
);
783 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
785 k
= manager_drop_marked_nexthops(link
->manager
);
792 void link_foreignize_nexthops(Link
*link
) {
797 SET_FOREACH(nexthop
, link
->nexthops
)
798 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
800 manager_mark_nexthops(link
->manager
, /* foreign = */ false, link
);
802 SET_FOREACH(nexthop
, link
->manager
->nexthops
) {
803 if (!nexthop_is_marked(nexthop
))
806 nexthop
->source
= NETWORK_CONFIG_SOURCE_FOREIGN
;
810 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
811 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
812 _cleanup_free_
void *raw_group
= NULL
;
813 NextHop
*nexthop
= NULL
;
814 size_t raw_group_size
;
824 if (sd_netlink_message_is_error(message
)) {
825 r
= sd_netlink_message_get_errno(message
);
827 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
832 r
= sd_netlink_message_get_type(message
, &type
);
834 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
836 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
837 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
841 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &ifindex
);
842 if (r
< 0 && r
!= -ENODATA
) {
843 log_warning_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
847 log_warning("rtnl: received nexthop message with invalid ifindex %"PRIu32
", ignoring.", ifindex
);
851 r
= link_get_by_index(m
, ifindex
, &link
);
852 if (r
< 0 || !link
) {
854 log_warning("rtnl: received nexthop message for link (%"PRIu32
") we do not know about, ignoring", ifindex
);
859 r
= nexthop_new(&tmp
);
863 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
865 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop family, ignoring: %m");
867 } else if (!IN_SET(tmp
->family
, AF_UNSPEC
, AF_INET
, AF_INET6
)) {
868 log_link_debug(link
, "rtnl: received nexthop message with invalid family %d, ignoring.", tmp
->family
);
872 r
= sd_rtnl_message_nexthop_get_protocol(message
, &tmp
->protocol
);
874 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop protocol, ignoring: %m");
878 r
= sd_rtnl_message_nexthop_get_flags(message
, &tmp
->flags
);
880 log_link_warning_errno(link
, r
, "rtnl: could not get nexthop flags, ignoring: %m");
884 r
= sd_netlink_message_read_data(message
, NHA_GROUP
, &raw_group_size
, &raw_group
);
885 if (r
< 0 && r
!= -ENODATA
) {
886 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
889 struct nexthop_grp
*group
= raw_group
;
892 if (raw_group_size
== 0 || raw_group_size
% sizeof(struct nexthop_grp
) != 0) {
893 log_link_warning(link
, "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
897 assert((uintptr_t) group
% alignof(struct nexthop_grp
) == 0);
899 n_group
= raw_group_size
/ sizeof(struct nexthop_grp
);
900 for (size_t i
= 0; i
< n_group
; i
++) {
901 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
903 if (group
[i
].id
== 0) {
904 log_link_warning(link
, "rtnl: received nexthop message with invalid ID in group, ignoring.");
907 if (group
[i
].weight
> 254) {
908 log_link_warning(link
, "rtnl: received nexthop message with invalid weight in group, ignoring.");
912 nhg
= newdup(struct nexthop_grp
, group
+ i
, 1);
916 r
= hashmap_ensure_put(&tmp
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
920 log_link_warning_errno(link
, r
, "Failed to store nexthop group, ignoring: %m");
928 if (tmp
->family
!= AF_UNSPEC
) {
929 r
= netlink_message_read_in_addr_union(message
, NHA_GATEWAY
, tmp
->family
, &tmp
->gw
);
930 if (r
< 0 && r
!= -ENODATA
) {
931 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
936 r
= sd_netlink_message_has_flag(message
, NHA_BLACKHOLE
);
938 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
943 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &tmp
->id
);
945 log_link_warning_errno(link
, r
, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
948 log_link_warning_errno(link
, r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
950 } else if (tmp
->id
== 0) {
951 log_link_warning(link
, "rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
955 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
956 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
957 if (!nexthop_owned_by_link(tmp
))
960 (void) nexthop_get(m
, link
, tmp
, &nexthop
);
965 nexthop
->flags
= tmp
->flags
;
966 nexthop_enter_configured(nexthop
);
967 log_nexthop_debug(tmp
, "Received remembered", link
);
969 nexthop_enter_configured(tmp
);
970 log_nexthop_debug(tmp
, "Remembering", link
);
972 r
= nexthop_add(m
, link
, tmp
);
974 log_link_warning_errno(link
, r
, "Could not remember foreign nexthop, ignoring: %m");
984 nexthop_enter_removed(nexthop
);
985 if (nexthop
->state
== 0) {
986 log_nexthop_debug(nexthop
, "Forgetting", link
);
987 nexthop_free(nexthop
);
989 log_nexthop_debug(nexthop
, "Removed", link
);
991 log_nexthop_debug(tmp
, "Kernel removed unknown", link
);
995 assert_not_reached();
1001 static int nexthop_section_verify(NextHop
*nh
) {
1002 if (section_is_invalid(nh
->section
))
1005 if (!hashmap_isempty(nh
->group
)) {
1006 if (in_addr_is_set(nh
->family
, &nh
->gw
))
1007 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1008 "%s: nexthop group cannot have gateway address. "
1009 "Ignoring [NextHop] section from line %u.",
1010 nh
->section
->filename
, nh
->section
->line
);
1012 if (nh
->family
!= AF_UNSPEC
)
1013 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1014 "%s: nexthop group cannot have Family= setting. "
1015 "Ignoring [NextHop] section from line %u.",
1016 nh
->section
->filename
, nh
->section
->line
);
1018 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1019 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1020 "%s: nexthop group cannot be a blackhole. "
1021 "Ignoring [NextHop] section from line %u.",
1022 nh
->section
->filename
, nh
->section
->line
);
1023 } else if (nh
->family
== AF_UNSPEC
)
1024 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1025 nh
->family
= AF_INET
;
1027 if (nh
->blackhole
&& in_addr_is_set(nh
->family
, &nh
->gw
))
1028 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1029 "%s: blackhole nexthop cannot have gateway address. "
1030 "Ignoring [NextHop] section from line %u.",
1031 nh
->section
->filename
, nh
->section
->line
);
1033 if (nh
->onlink
< 0 && in_addr_is_set(nh
->family
, &nh
->gw
) &&
1034 ordered_hashmap_isempty(nh
->network
->addresses_by_section
)) {
1035 /* If no address is configured, in most cases the gateway cannot be reachable.
1036 * TODO: we may need to improve the condition above. */
1037 log_warning("%s: Gateway= without static address configured. "
1038 "Enabling OnLink= option.",
1039 nh
->section
->filename
);
1043 if (nh
->onlink
>= 0)
1044 SET_FLAG(nh
->flags
, RTNH_F_ONLINK
, nh
->onlink
);
1049 void network_drop_invalid_nexthops(Network
*network
) {
1054 HASHMAP_FOREACH(nh
, network
->nexthops_by_section
)
1055 if (nexthop_section_verify(nh
) < 0)
1059 int config_parse_nexthop_id(
1061 const char *filename
,
1063 const char *section
,
1064 unsigned section_line
,
1071 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1072 Network
*network
= userdata
;
1082 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1086 if (isempty(rvalue
)) {
1092 r
= safe_atou32(rvalue
, &id
);
1094 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1095 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1099 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1100 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue
);
1109 int config_parse_nexthop_gateway(
1111 const char *filename
,
1113 const char *section
,
1114 unsigned section_line
,
1121 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1122 Network
*network
= userdata
;
1131 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1135 if (isempty(rvalue
)) {
1136 n
->family
= AF_UNSPEC
;
1137 n
->gw
= IN_ADDR_NULL
;
1143 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1145 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1146 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1154 int config_parse_nexthop_family(
1156 const char *filename
,
1158 const char *section
,
1159 unsigned section_line
,
1166 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1167 Network
*network
= userdata
;
1177 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1181 if (isempty(rvalue
) &&
1182 !in_addr_is_set(n
->family
, &n
->gw
)) {
1183 /* Accept an empty string only when Gateway= is null or not specified. */
1184 n
->family
= AF_UNSPEC
;
1189 a
= nexthop_address_family_from_string(rvalue
);
1191 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1192 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1196 if (in_addr_is_set(n
->family
, &n
->gw
) &&
1197 ((a
== ADDRESS_FAMILY_IPV4
&& n
->family
== AF_INET6
) ||
1198 (a
== ADDRESS_FAMILY_IPV6
&& n
->family
== AF_INET
))) {
1199 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1200 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1201 "ignoring assignment.", rvalue
);
1206 case ADDRESS_FAMILY_IPV4
:
1207 n
->family
= AF_INET
;
1209 case ADDRESS_FAMILY_IPV6
:
1210 n
->family
= AF_INET6
;
1213 assert_not_reached();
1220 int config_parse_nexthop_onlink(
1222 const char *filename
,
1224 const char *section
,
1225 unsigned section_line
,
1232 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1233 Network
*network
= userdata
;
1242 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1246 if (isempty(rvalue
)) {
1252 r
= parse_boolean(rvalue
);
1254 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1255 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1265 int config_parse_nexthop_blackhole(
1267 const char *filename
,
1269 const char *section
,
1270 unsigned section_line
,
1277 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1278 Network
*network
= userdata
;
1287 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1291 r
= parse_boolean(rvalue
);
1293 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1294 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1304 int config_parse_nexthop_group(
1306 const char *filename
,
1308 const char *section
,
1309 unsigned section_line
,
1316 _cleanup_(nexthop_free_or_set_invalidp
) NextHop
*n
= NULL
;
1317 Network
*network
= userdata
;
1326 r
= nexthop_new_static(network
, filename
, section_line
, &n
);
1330 if (isempty(rvalue
)) {
1331 n
->group
= hashmap_free_free(n
->group
);
1336 for (const char *p
= rvalue
;;) {
1337 _cleanup_free_
struct nexthop_grp
*nhg
= NULL
;
1338 _cleanup_free_
char *word
= NULL
;
1342 r
= extract_first_word(&p
, &word
, NULL
, 0);
1346 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1347 "Invalid %s=, ignoring assignment: %s", lvalue
, rvalue
);
1353 nhg
= new0(struct nexthop_grp
, 1);
1357 sep
= strchr(word
, ':');
1360 r
= safe_atou32(sep
, &w
);
1362 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1363 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1367 if (w
== 0 || w
> 256) {
1368 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1369 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1373 /* See comments in config_parse_multipath_route(). */
1374 nhg
->weight
= w
- 1;
1377 r
= safe_atou32(word
, &nhg
->id
);
1379 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1380 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1381 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1385 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1386 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1387 lvalue
, word
, sep
? ":" : "", strempty(sep
));
1391 r
= hashmap_ensure_put(&n
->group
, NULL
, UINT32_TO_PTR(nhg
->id
), nhg
);
1395 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1396 "Nexthop ID %"PRIu32
" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1397 nhg
->id
, lvalue
, word
, sep
? ":" : "", strempty(sep
));