1 // SPDX-License-Identifier: GPL-2.0
2 /* Generic nexthop implementation
4 * Copyright (c) 2017-19 Cumulus Networks
5 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
8 #include <linux/nexthop.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/slab.h>
12 #include <net/ipv6_stubs.h>
13 #include <net/lwtunnel.h>
14 #include <net/ndisc.h>
15 #include <net/nexthop.h>
16 #include <net/route.h>
19 static void remove_nexthop(struct net
*net
, struct nexthop
*nh
,
20 struct nl_info
*nlinfo
);
22 #define NH_DEV_HASHBITS 8
23 #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
25 static const struct nla_policy rtm_nh_policy
[NHA_MAX
+ 1] = {
26 [NHA_ID
] = { .type
= NLA_U32
},
27 [NHA_GROUP
] = { .type
= NLA_BINARY
},
28 [NHA_GROUP_TYPE
] = { .type
= NLA_U16
},
29 [NHA_BLACKHOLE
] = { .type
= NLA_FLAG
},
30 [NHA_OIF
] = { .type
= NLA_U32
},
31 [NHA_GATEWAY
] = { .type
= NLA_BINARY
},
32 [NHA_ENCAP_TYPE
] = { .type
= NLA_U16
},
33 [NHA_ENCAP
] = { .type
= NLA_NESTED
},
34 [NHA_GROUPS
] = { .type
= NLA_FLAG
},
35 [NHA_MASTER
] = { .type
= NLA_U32
},
36 [NHA_FDB
] = { .type
= NLA_FLAG
},
39 static bool nexthop_notifiers_is_empty(struct net
*net
)
41 return !net
->nexthop
.notifier_chain
.head
;
45 __nh_notifier_single_info_init(struct nh_notifier_single_info
*nh_info
,
46 const struct nexthop
*nh
)
48 struct nh_info
*nhi
= rtnl_dereference(nh
->nh_info
);
50 nh_info
->dev
= nhi
->fib_nhc
.nhc_dev
;
51 nh_info
->gw_family
= nhi
->fib_nhc
.nhc_gw_family
;
52 if (nh_info
->gw_family
== AF_INET
)
53 nh_info
->ipv4
= nhi
->fib_nhc
.nhc_gw
.ipv4
;
54 else if (nh_info
->gw_family
== AF_INET6
)
55 nh_info
->ipv6
= nhi
->fib_nhc
.nhc_gw
.ipv6
;
57 nh_info
->is_reject
= nhi
->reject_nh
;
58 nh_info
->is_fdb
= nhi
->fdb_nh
;
59 nh_info
->has_encap
= !!nhi
->fib_nhc
.nhc_lwtstate
;
62 static int nh_notifier_single_info_init(struct nh_notifier_info
*info
,
63 const struct nexthop
*nh
)
65 info
->nh
= kzalloc(sizeof(*info
->nh
), GFP_KERNEL
);
69 __nh_notifier_single_info_init(info
->nh
, nh
);
74 static void nh_notifier_single_info_fini(struct nh_notifier_info
*info
)
79 static int nh_notifier_grp_info_init(struct nh_notifier_info
*info
,
80 const struct nexthop
*nh
)
82 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
83 u16 num_nh
= nhg
->num_nh
;
86 info
->nh_grp
= kzalloc(struct_size(info
->nh_grp
, nh_entries
, num_nh
),
91 info
->nh_grp
->num_nh
= num_nh
;
92 info
->nh_grp
->is_fdb
= nhg
->fdb_nh
;
94 for (i
= 0; i
< num_nh
; i
++) {
95 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
97 info
->nh_grp
->nh_entries
[i
].id
= nhge
->nh
->id
;
98 info
->nh_grp
->nh_entries
[i
].weight
= nhge
->weight
;
99 __nh_notifier_single_info_init(&info
->nh_grp
->nh_entries
[i
].nh
,
106 static void nh_notifier_grp_info_fini(struct nh_notifier_info
*info
)
111 static int nh_notifier_info_init(struct nh_notifier_info
*info
,
112 const struct nexthop
*nh
)
115 info
->is_grp
= nh
->is_group
;
118 return nh_notifier_grp_info_init(info
, nh
);
120 return nh_notifier_single_info_init(info
, nh
);
123 static void nh_notifier_info_fini(struct nh_notifier_info
*info
)
126 nh_notifier_grp_info_fini(info
);
128 nh_notifier_single_info_fini(info
);
131 static int call_nexthop_notifiers(struct net
*net
,
132 enum nexthop_event_type event_type
,
134 struct netlink_ext_ack
*extack
)
136 struct nh_notifier_info info
= {
144 if (nexthop_notifiers_is_empty(net
))
147 err
= nh_notifier_info_init(&info
, nh
);
149 NL_SET_ERR_MSG(extack
, "Failed to initialize nexthop notifier info");
153 err
= blocking_notifier_call_chain(&net
->nexthop
.notifier_chain
,
155 nh_notifier_info_fini(&info
);
157 return notifier_to_errno(err
);
160 static unsigned int nh_dev_hashfn(unsigned int val
)
162 unsigned int mask
= NH_DEV_HASHSIZE
- 1;
165 (val
>> NH_DEV_HASHBITS
) ^
166 (val
>> (NH_DEV_HASHBITS
* 2))) & mask
;
169 static void nexthop_devhash_add(struct net
*net
, struct nh_info
*nhi
)
171 struct net_device
*dev
= nhi
->fib_nhc
.nhc_dev
;
172 struct hlist_head
*head
;
177 hash
= nh_dev_hashfn(dev
->ifindex
);
178 head
= &net
->nexthop
.devhash
[hash
];
179 hlist_add_head(&nhi
->dev_hash
, head
);
182 static void nexthop_free_mpath(struct nexthop
*nh
)
184 struct nh_group
*nhg
;
187 nhg
= rcu_dereference_raw(nh
->nh_grp
);
188 for (i
= 0; i
< nhg
->num_nh
; ++i
) {
189 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
191 WARN_ON(!list_empty(&nhge
->nh_list
));
192 nexthop_put(nhge
->nh
);
195 WARN_ON(nhg
->spare
== nhg
);
201 static void nexthop_free_single(struct nexthop
*nh
)
205 nhi
= rcu_dereference_raw(nh
->nh_info
);
206 switch (nhi
->family
) {
208 fib_nh_release(nh
->net
, &nhi
->fib_nh
);
211 ipv6_stub
->fib6_nh_release(&nhi
->fib6_nh
);
217 void nexthop_free_rcu(struct rcu_head
*head
)
219 struct nexthop
*nh
= container_of(head
, struct nexthop
, rcu
);
222 nexthop_free_mpath(nh
);
224 nexthop_free_single(nh
);
228 EXPORT_SYMBOL_GPL(nexthop_free_rcu
);
230 static struct nexthop
*nexthop_alloc(void)
234 nh
= kzalloc(sizeof(struct nexthop
), GFP_KERNEL
);
236 INIT_LIST_HEAD(&nh
->fi_list
);
237 INIT_LIST_HEAD(&nh
->f6i_list
);
238 INIT_LIST_HEAD(&nh
->grp_list
);
239 INIT_LIST_HEAD(&nh
->fdb_list
);
244 static struct nh_group
*nexthop_grp_alloc(u16 num_nh
)
246 struct nh_group
*nhg
;
248 nhg
= kzalloc(struct_size(nhg
, nh_entries
, num_nh
), GFP_KERNEL
);
250 nhg
->num_nh
= num_nh
;
255 static void nh_base_seq_inc(struct net
*net
)
257 while (++net
->nexthop
.seq
== 0)
261 /* no reference taken; rcu lock or rtnl must be held */
262 struct nexthop
*nexthop_find_by_id(struct net
*net
, u32 id
)
264 struct rb_node
**pp
, *parent
= NULL
, *next
;
266 pp
= &net
->nexthop
.rb_root
.rb_node
;
270 next
= rcu_dereference_raw(*pp
);
275 nh
= rb_entry(parent
, struct nexthop
, rb_node
);
278 else if (id
> nh
->id
)
279 pp
= &next
->rb_right
;
285 EXPORT_SYMBOL_GPL(nexthop_find_by_id
);
287 /* used for auto id allocation; called with rtnl held */
288 static u32
nh_find_unused_id(struct net
*net
)
290 u32 id_start
= net
->nexthop
.last_id_allocated
;
293 net
->nexthop
.last_id_allocated
++;
294 if (net
->nexthop
.last_id_allocated
== id_start
)
297 if (!nexthop_find_by_id(net
, net
->nexthop
.last_id_allocated
))
298 return net
->nexthop
.last_id_allocated
;
303 static int nla_put_nh_group(struct sk_buff
*skb
, struct nh_group
*nhg
)
305 struct nexthop_grp
*p
;
306 size_t len
= nhg
->num_nh
* sizeof(*p
);
312 group_type
= NEXTHOP_GRP_TYPE_MPATH
;
314 if (nla_put_u16(skb
, NHA_GROUP_TYPE
, group_type
))
315 goto nla_put_failure
;
317 nla
= nla_reserve(skb
, NHA_GROUP
, len
);
319 goto nla_put_failure
;
322 for (i
= 0; i
< nhg
->num_nh
; ++i
) {
323 p
->id
= nhg
->nh_entries
[i
].nh
->id
;
324 p
->weight
= nhg
->nh_entries
[i
].weight
- 1;
334 static int nh_fill_node(struct sk_buff
*skb
, struct nexthop
*nh
,
335 int event
, u32 portid
, u32 seq
, unsigned int nlflags
)
337 struct fib6_nh
*fib6_nh
;
338 struct fib_nh
*fib_nh
;
339 struct nlmsghdr
*nlh
;
343 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nhm
), nlflags
);
347 nhm
= nlmsg_data(nlh
);
348 nhm
->nh_family
= AF_UNSPEC
;
349 nhm
->nh_flags
= nh
->nh_flags
;
350 nhm
->nh_protocol
= nh
->protocol
;
354 if (nla_put_u32(skb
, NHA_ID
, nh
->id
))
355 goto nla_put_failure
;
358 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
360 if (nhg
->fdb_nh
&& nla_put_flag(skb
, NHA_FDB
))
361 goto nla_put_failure
;
362 if (nla_put_nh_group(skb
, nhg
))
363 goto nla_put_failure
;
367 nhi
= rtnl_dereference(nh
->nh_info
);
368 nhm
->nh_family
= nhi
->family
;
369 if (nhi
->reject_nh
) {
370 if (nla_put_flag(skb
, NHA_BLACKHOLE
))
371 goto nla_put_failure
;
373 } else if (nhi
->fdb_nh
) {
374 if (nla_put_flag(skb
, NHA_FDB
))
375 goto nla_put_failure
;
377 const struct net_device
*dev
;
379 dev
= nhi
->fib_nhc
.nhc_dev
;
380 if (dev
&& nla_put_u32(skb
, NHA_OIF
, dev
->ifindex
))
381 goto nla_put_failure
;
384 nhm
->nh_scope
= nhi
->fib_nhc
.nhc_scope
;
385 switch (nhi
->family
) {
387 fib_nh
= &nhi
->fib_nh
;
388 if (fib_nh
->fib_nh_gw_family
&&
389 nla_put_be32(skb
, NHA_GATEWAY
, fib_nh
->fib_nh_gw4
))
390 goto nla_put_failure
;
394 fib6_nh
= &nhi
->fib6_nh
;
395 if (fib6_nh
->fib_nh_gw_family
&&
396 nla_put_in6_addr(skb
, NHA_GATEWAY
, &fib6_nh
->fib_nh_gw6
))
397 goto nla_put_failure
;
401 if (nhi
->fib_nhc
.nhc_lwtstate
&&
402 lwtunnel_fill_encap(skb
, nhi
->fib_nhc
.nhc_lwtstate
,
403 NHA_ENCAP
, NHA_ENCAP_TYPE
) < 0)
404 goto nla_put_failure
;
411 nlmsg_cancel(skb
, nlh
);
415 static size_t nh_nlmsg_size_grp(struct nexthop
*nh
)
417 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
418 size_t sz
= sizeof(struct nexthop_grp
) * nhg
->num_nh
;
420 return nla_total_size(sz
) +
421 nla_total_size(2); /* NHA_GROUP_TYPE */
424 static size_t nh_nlmsg_size_single(struct nexthop
*nh
)
426 struct nh_info
*nhi
= rtnl_dereference(nh
->nh_info
);
429 /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
430 * are mutually exclusive
432 sz
= nla_total_size(4); /* NHA_OIF */
434 switch (nhi
->family
) {
436 if (nhi
->fib_nh
.fib_nh_gw_family
)
437 sz
+= nla_total_size(4); /* NHA_GATEWAY */
442 if (nhi
->fib6_nh
.fib_nh_gw_family
)
443 sz
+= nla_total_size(sizeof(const struct in6_addr
));
447 if (nhi
->fib_nhc
.nhc_lwtstate
) {
448 sz
+= lwtunnel_get_encap_size(nhi
->fib_nhc
.nhc_lwtstate
);
449 sz
+= nla_total_size(2); /* NHA_ENCAP_TYPE */
455 static size_t nh_nlmsg_size(struct nexthop
*nh
)
457 size_t sz
= NLMSG_ALIGN(sizeof(struct nhmsg
));
459 sz
+= nla_total_size(4); /* NHA_ID */
462 sz
+= nh_nlmsg_size_grp(nh
);
464 sz
+= nh_nlmsg_size_single(nh
);
469 static void nexthop_notify(int event
, struct nexthop
*nh
, struct nl_info
*info
)
471 unsigned int nlflags
= info
->nlh
? info
->nlh
->nlmsg_flags
: 0;
472 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
476 skb
= nlmsg_new(nh_nlmsg_size(nh
), gfp_any());
480 err
= nh_fill_node(skb
, nh
, event
, info
->portid
, seq
, nlflags
);
482 /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
483 WARN_ON(err
== -EMSGSIZE
);
488 rtnl_notify(skb
, info
->nl_net
, info
->portid
, RTNLGRP_NEXTHOP
,
489 info
->nlh
, gfp_any());
493 rtnl_set_sk_err(info
->nl_net
, RTNLGRP_NEXTHOP
, err
);
496 static bool valid_group_nh(struct nexthop
*nh
, unsigned int npaths
,
497 bool *is_fdb
, struct netlink_ext_ack
*extack
)
500 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
502 /* nested multipath (group within a group) is not
506 NL_SET_ERR_MSG(extack
,
507 "Multipath group can not be a nexthop within a group");
510 *is_fdb
= nhg
->fdb_nh
;
512 struct nh_info
*nhi
= rtnl_dereference(nh
->nh_info
);
514 if (nhi
->reject_nh
&& npaths
> 1) {
515 NL_SET_ERR_MSG(extack
,
516 "Blackhole nexthop can not be used in a group with more than 1 path");
519 *is_fdb
= nhi
->fdb_nh
;
525 static int nh_check_attr_fdb_group(struct nexthop
*nh
, u8
*nh_family
,
526 struct netlink_ext_ack
*extack
)
530 nhi
= rtnl_dereference(nh
->nh_info
);
533 NL_SET_ERR_MSG(extack
, "FDB nexthop group can only have fdb nexthops");
537 if (*nh_family
== AF_UNSPEC
) {
538 *nh_family
= nhi
->family
;
539 } else if (*nh_family
!= nhi
->family
) {
540 NL_SET_ERR_MSG(extack
, "FDB nexthop group cannot have mixed family nexthops");
547 static int nh_check_attr_group(struct net
*net
, struct nlattr
*tb
[],
548 struct netlink_ext_ack
*extack
)
550 unsigned int len
= nla_len(tb
[NHA_GROUP
]);
551 u8 nh_family
= AF_UNSPEC
;
552 struct nexthop_grp
*nhg
;
556 if (!len
|| len
& (sizeof(struct nexthop_grp
) - 1)) {
557 NL_SET_ERR_MSG(extack
,
558 "Invalid length for nexthop group attribute");
562 /* convert len to number of nexthop ids */
565 nhg
= nla_data(tb
[NHA_GROUP
]);
566 for (i
= 0; i
< len
; ++i
) {
567 if (nhg
[i
].resvd1
|| nhg
[i
].resvd2
) {
568 NL_SET_ERR_MSG(extack
, "Reserved fields in nexthop_grp must be 0");
571 if (nhg
[i
].weight
> 254) {
572 NL_SET_ERR_MSG(extack
, "Invalid value for weight");
575 for (j
= i
+ 1; j
< len
; ++j
) {
576 if (nhg
[i
].id
== nhg
[j
].id
) {
577 NL_SET_ERR_MSG(extack
, "Nexthop id can not be used twice in a group");
585 nhg
= nla_data(tb
[NHA_GROUP
]);
586 for (i
= 0; i
< len
; ++i
) {
590 nh
= nexthop_find_by_id(net
, nhg
[i
].id
);
592 NL_SET_ERR_MSG(extack
, "Invalid nexthop id");
595 if (!valid_group_nh(nh
, len
, &is_fdb_nh
, extack
))
598 if (nhg_fdb
&& nh_check_attr_fdb_group(nh
, &nh_family
, extack
))
601 if (!nhg_fdb
&& is_fdb_nh
) {
602 NL_SET_ERR_MSG(extack
, "Non FDB nexthop group cannot have fdb nexthops");
606 for (i
= NHA_GROUP_TYPE
+ 1; i
< __NHA_MAX
; ++i
) {
611 NL_SET_ERR_MSG(extack
,
612 "No other attributes can be set in nexthop groups");
619 static bool ipv6_good_nh(const struct fib6_nh
*nh
)
621 int state
= NUD_REACHABLE
;
626 n
= __ipv6_neigh_lookup_noref_stub(nh
->fib_nh_dev
, &nh
->fib_nh_gw6
);
628 state
= n
->nud_state
;
630 rcu_read_unlock_bh();
632 return !!(state
& NUD_VALID
);
635 static bool ipv4_good_nh(const struct fib_nh
*nh
)
637 int state
= NUD_REACHABLE
;
642 n
= __ipv4_neigh_lookup_noref(nh
->fib_nh_dev
,
643 (__force u32
)nh
->fib_nh_gw4
);
645 state
= n
->nud_state
;
647 rcu_read_unlock_bh();
649 return !!(state
& NUD_VALID
);
652 struct nexthop
*nexthop_select_path(struct nexthop
*nh
, int hash
)
654 struct nexthop
*rc
= NULL
;
655 struct nh_group
*nhg
;
661 nhg
= rcu_dereference(nh
->nh_grp
);
662 for (i
= 0; i
< nhg
->num_nh
; ++i
) {
663 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
666 if (hash
> atomic_read(&nhge
->upper_bound
))
669 nhi
= rcu_dereference(nhge
->nh
->nh_info
);
673 /* nexthops always check if it is good and does
674 * not rely on a sysctl for this behavior
676 switch (nhi
->family
) {
678 if (ipv4_good_nh(&nhi
->fib_nh
))
682 if (ipv6_good_nh(&nhi
->fib6_nh
))
693 EXPORT_SYMBOL_GPL(nexthop_select_path
);
695 int nexthop_for_each_fib6_nh(struct nexthop
*nh
,
696 int (*cb
)(struct fib6_nh
*nh
, void *arg
),
703 struct nh_group
*nhg
;
706 nhg
= rcu_dereference_rtnl(nh
->nh_grp
);
707 for (i
= 0; i
< nhg
->num_nh
; i
++) {
708 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
710 nhi
= rcu_dereference_rtnl(nhge
->nh
->nh_info
);
711 err
= cb(&nhi
->fib6_nh
, arg
);
716 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
717 err
= cb(&nhi
->fib6_nh
, arg
);
724 EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh
);
726 static int check_src_addr(const struct in6_addr
*saddr
,
727 struct netlink_ext_ack
*extack
)
729 if (!ipv6_addr_any(saddr
)) {
730 NL_SET_ERR_MSG(extack
, "IPv6 routes using source address can not use nexthop objects");
736 int fib6_check_nexthop(struct nexthop
*nh
, struct fib6_config
*cfg
,
737 struct netlink_ext_ack
*extack
)
742 /* fib6_src is unique to a fib6_info and limits the ability to cache
743 * routes in fib6_nh within a nexthop that is potentially shared
744 * across multiple fib entries. If the config wants to use source
745 * routing it can not use nexthop objects. mlxsw also does not allow
746 * fib6_src on routes.
748 if (cfg
&& check_src_addr(&cfg
->fc_src
, extack
) < 0)
752 struct nh_group
*nhg
;
754 nhg
= rtnl_dereference(nh
->nh_grp
);
757 is_fdb_nh
= nhg
->fdb_nh
;
759 nhi
= rtnl_dereference(nh
->nh_info
);
760 if (nhi
->family
== AF_INET
)
762 is_fdb_nh
= nhi
->fdb_nh
;
766 NL_SET_ERR_MSG(extack
, "Route cannot point to a fdb nexthop");
772 NL_SET_ERR_MSG(extack
, "IPv6 routes can not use an IPv4 nexthop");
775 EXPORT_SYMBOL_GPL(fib6_check_nexthop
);
777 /* if existing nexthop has ipv6 routes linked to it, need
778 * to verify this new spec works with ipv6
780 static int fib6_check_nh_list(struct nexthop
*old
, struct nexthop
*new,
781 struct netlink_ext_ack
*extack
)
783 struct fib6_info
*f6i
;
785 if (list_empty(&old
->f6i_list
))
788 list_for_each_entry(f6i
, &old
->f6i_list
, nh_list
) {
789 if (check_src_addr(&f6i
->fib6_src
.addr
, extack
) < 0)
793 return fib6_check_nexthop(new, NULL
, extack
);
796 static int nexthop_check_scope(struct nh_info
*nhi
, u8 scope
,
797 struct netlink_ext_ack
*extack
)
799 if (scope
== RT_SCOPE_HOST
&& nhi
->fib_nhc
.nhc_gw_family
) {
800 NL_SET_ERR_MSG(extack
,
801 "Route with host scope can not have a gateway");
805 if (nhi
->fib_nhc
.nhc_flags
& RTNH_F_ONLINK
&& scope
>= RT_SCOPE_LINK
) {
806 NL_SET_ERR_MSG(extack
, "Scope mismatch with nexthop");
813 /* Invoked by fib add code to verify nexthop by id is ok with
814 * config for prefix; parts of fib_check_nh not done when nexthop
817 int fib_check_nexthop(struct nexthop
*nh
, u8 scope
,
818 struct netlink_ext_ack
*extack
)
824 struct nh_group
*nhg
;
826 nhg
= rtnl_dereference(nh
->nh_grp
);
828 NL_SET_ERR_MSG(extack
, "Route cannot point to a fdb nexthop");
833 if (scope
== RT_SCOPE_HOST
) {
834 NL_SET_ERR_MSG(extack
, "Route with host scope can not have multiple nexthops");
839 /* all nexthops in a group have the same scope */
840 nhi
= rtnl_dereference(nhg
->nh_entries
[0].nh
->nh_info
);
841 err
= nexthop_check_scope(nhi
, scope
, extack
);
843 nhi
= rtnl_dereference(nh
->nh_info
);
845 NL_SET_ERR_MSG(extack
, "Route cannot point to a fdb nexthop");
849 err
= nexthop_check_scope(nhi
, scope
, extack
);
856 static int fib_check_nh_list(struct nexthop
*old
, struct nexthop
*new,
857 struct netlink_ext_ack
*extack
)
861 list_for_each_entry(fi
, &old
->fi_list
, nh_list
) {
864 err
= fib_check_nexthop(new, fi
->fib_scope
, extack
);
871 static void nh_group_rebalance(struct nh_group
*nhg
)
877 for (i
= 0; i
< nhg
->num_nh
; ++i
)
878 total
+= nhg
->nh_entries
[i
].weight
;
880 for (i
= 0; i
< nhg
->num_nh
; ++i
) {
881 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
885 upper_bound
= DIV_ROUND_CLOSEST_ULL((u64
)w
<< 31, total
) - 1;
886 atomic_set(&nhge
->upper_bound
, upper_bound
);
890 static void remove_nh_grp_entry(struct net
*net
, struct nh_grp_entry
*nhge
,
891 struct nl_info
*nlinfo
)
893 struct nh_grp_entry
*nhges
, *new_nhges
;
894 struct nexthop
*nhp
= nhge
->nh_parent
;
895 struct nexthop
*nh
= nhge
->nh
;
896 struct nh_group
*nhg
, *newg
;
901 nhg
= rtnl_dereference(nhp
->nh_grp
);
904 /* last entry, keep it visible and remove the parent */
905 if (nhg
->num_nh
== 1) {
906 remove_nexthop(net
, nhp
, nlinfo
);
910 newg
->has_v4
= false;
911 newg
->mpath
= nhg
->mpath
;
912 newg
->fdb_nh
= nhg
->fdb_nh
;
913 newg
->num_nh
= nhg
->num_nh
;
915 /* copy old entries to new except the one getting removed */
916 nhges
= nhg
->nh_entries
;
917 new_nhges
= newg
->nh_entries
;
918 for (i
= 0, j
= 0; i
< nhg
->num_nh
; ++i
) {
921 /* current nexthop getting removed */
922 if (nhg
->nh_entries
[i
].nh
== nh
) {
927 nhi
= rtnl_dereference(nhges
[i
].nh
->nh_info
);
928 if (nhi
->family
== AF_INET
)
931 list_del(&nhges
[i
].nh_list
);
932 new_nhges
[j
].nh_parent
= nhges
[i
].nh_parent
;
933 new_nhges
[j
].nh
= nhges
[i
].nh
;
934 new_nhges
[j
].weight
= nhges
[i
].weight
;
935 list_add(&new_nhges
[j
].nh_list
, &new_nhges
[j
].nh
->grp_list
);
939 nh_group_rebalance(newg
);
940 rcu_assign_pointer(nhp
->nh_grp
, newg
);
942 list_del(&nhge
->nh_list
);
943 nexthop_put(nhge
->nh
);
946 nexthop_notify(RTM_NEWNEXTHOP
, nhp
, nlinfo
);
949 static void remove_nexthop_from_groups(struct net
*net
, struct nexthop
*nh
,
950 struct nl_info
*nlinfo
)
952 struct nh_grp_entry
*nhge
, *tmp
;
954 list_for_each_entry_safe(nhge
, tmp
, &nh
->grp_list
, nh_list
)
955 remove_nh_grp_entry(net
, nhge
, nlinfo
);
957 /* make sure all see the newly published array before releasing rtnl */
961 static void remove_nexthop_group(struct nexthop
*nh
, struct nl_info
*nlinfo
)
963 struct nh_group
*nhg
= rcu_dereference_rtnl(nh
->nh_grp
);
964 int i
, num_nh
= nhg
->num_nh
;
966 for (i
= 0; i
< num_nh
; ++i
) {
967 struct nh_grp_entry
*nhge
= &nhg
->nh_entries
[i
];
969 if (WARN_ON(!nhge
->nh
))
972 list_del_init(&nhge
->nh_list
);
976 /* not called for nexthop replace */
977 static void __remove_nexthop_fib(struct net
*net
, struct nexthop
*nh
)
979 struct fib6_info
*f6i
, *tmp
;
980 bool do_flush
= false;
983 list_for_each_entry(fi
, &nh
->fi_list
, nh_list
) {
984 fi
->fib_flags
|= RTNH_F_DEAD
;
990 /* ip6_del_rt removes the entry from this list hence the _safe */
991 list_for_each_entry_safe(f6i
, tmp
, &nh
->f6i_list
, nh_list
) {
992 /* __ip6_del_rt does a release, so do a hold here */
994 ipv6_stub
->ip6_del_rt(net
, f6i
,
995 !net
->ipv4
.sysctl_nexthop_compat_mode
);
999 static void __remove_nexthop(struct net
*net
, struct nexthop
*nh
,
1000 struct nl_info
*nlinfo
)
1002 __remove_nexthop_fib(net
, nh
);
1005 remove_nexthop_group(nh
, nlinfo
);
1007 struct nh_info
*nhi
;
1009 nhi
= rtnl_dereference(nh
->nh_info
);
1010 if (nhi
->fib_nhc
.nhc_dev
)
1011 hlist_del(&nhi
->dev_hash
);
1013 remove_nexthop_from_groups(net
, nh
, nlinfo
);
1017 static void remove_nexthop(struct net
*net
, struct nexthop
*nh
,
1018 struct nl_info
*nlinfo
)
1020 call_nexthop_notifiers(net
, NEXTHOP_EVENT_DEL
, nh
, NULL
);
1022 /* remove from the tree */
1023 rb_erase(&nh
->rb_node
, &net
->nexthop
.rb_root
);
1026 nexthop_notify(RTM_DELNEXTHOP
, nh
, nlinfo
);
1028 __remove_nexthop(net
, nh
, nlinfo
);
1029 nh_base_seq_inc(net
);
1034 /* if any FIB entries reference this nexthop, any dst entries
1035 * need to be regenerated
1037 static void nh_rt_cache_flush(struct net
*net
, struct nexthop
*nh
)
1039 struct fib6_info
*f6i
;
1041 if (!list_empty(&nh
->fi_list
))
1042 rt_cache_flush(net
);
1044 list_for_each_entry(f6i
, &nh
->f6i_list
, nh_list
)
1045 ipv6_stub
->fib6_update_sernum(net
, f6i
);
1048 static int replace_nexthop_grp(struct net
*net
, struct nexthop
*old
,
1049 struct nexthop
*new,
1050 struct netlink_ext_ack
*extack
)
1052 struct nh_group
*oldg
, *newg
;
1055 if (!new->is_group
) {
1056 NL_SET_ERR_MSG(extack
, "Can not replace a nexthop group with a nexthop.");
1060 err
= call_nexthop_notifiers(net
, NEXTHOP_EVENT_REPLACE
, new, extack
);
1064 oldg
= rtnl_dereference(old
->nh_grp
);
1065 newg
= rtnl_dereference(new->nh_grp
);
1067 /* update parents - used by nexthop code for cleanup */
1068 for (i
= 0; i
< newg
->num_nh
; i
++)
1069 newg
->nh_entries
[i
].nh_parent
= old
;
1071 rcu_assign_pointer(old
->nh_grp
, newg
);
1073 for (i
= 0; i
< oldg
->num_nh
; i
++)
1074 oldg
->nh_entries
[i
].nh_parent
= new;
1076 rcu_assign_pointer(new->nh_grp
, oldg
);
1081 static void nh_group_v4_update(struct nh_group
*nhg
)
1083 struct nh_grp_entry
*nhges
;
1084 bool has_v4
= false;
1087 nhges
= nhg
->nh_entries
;
1088 for (i
= 0; i
< nhg
->num_nh
; i
++) {
1089 struct nh_info
*nhi
;
1091 nhi
= rtnl_dereference(nhges
[i
].nh
->nh_info
);
1092 if (nhi
->family
== AF_INET
)
1095 nhg
->has_v4
= has_v4
;
1098 static int replace_nexthop_single(struct net
*net
, struct nexthop
*old
,
1099 struct nexthop
*new,
1100 struct netlink_ext_ack
*extack
)
1102 struct nh_info
*oldi
, *newi
;
1105 if (new->is_group
) {
1106 NL_SET_ERR_MSG(extack
, "Can not replace a nexthop with a nexthop group.");
1110 err
= call_nexthop_notifiers(net
, NEXTHOP_EVENT_REPLACE
, new, extack
);
1114 /* Hardware flags were set on 'old' as 'new' is not in the red-black
1115 * tree. Therefore, inherit the flags from 'old' to 'new'.
1117 new->nh_flags
|= old
->nh_flags
& (RTNH_F_OFFLOAD
| RTNH_F_TRAP
);
1119 oldi
= rtnl_dereference(old
->nh_info
);
1120 newi
= rtnl_dereference(new->nh_info
);
1122 newi
->nh_parent
= old
;
1123 oldi
->nh_parent
= new;
1125 old
->protocol
= new->protocol
;
1126 old
->nh_flags
= new->nh_flags
;
1128 rcu_assign_pointer(old
->nh_info
, newi
);
1129 rcu_assign_pointer(new->nh_info
, oldi
);
1131 /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
1132 * update IPv4 indication in all the groups using the nexthop.
1134 if (oldi
->family
== AF_INET
&& newi
->family
== AF_INET6
) {
1135 struct nh_grp_entry
*nhge
;
1137 list_for_each_entry(nhge
, &old
->grp_list
, nh_list
) {
1138 struct nexthop
*nhp
= nhge
->nh_parent
;
1139 struct nh_group
*nhg
;
1141 nhg
= rtnl_dereference(nhp
->nh_grp
);
1142 nh_group_v4_update(nhg
);
1149 static void __nexthop_replace_notify(struct net
*net
, struct nexthop
*nh
,
1150 struct nl_info
*info
)
1152 struct fib6_info
*f6i
;
1154 if (!list_empty(&nh
->fi_list
)) {
1155 struct fib_info
*fi
;
1157 /* expectation is a few fib_info per nexthop and then
1158 * a lot of routes per fib_info. So mark the fib_info
1159 * and then walk the fib tables once
1161 list_for_each_entry(fi
, &nh
->fi_list
, nh_list
)
1162 fi
->nh_updated
= true;
1164 fib_info_notify_update(net
, info
);
1166 list_for_each_entry(fi
, &nh
->fi_list
, nh_list
)
1167 fi
->nh_updated
= false;
1170 list_for_each_entry(f6i
, &nh
->f6i_list
, nh_list
)
1171 ipv6_stub
->fib6_rt_update(net
, f6i
, info
);
1174 /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
1175 * linked to this nexthop and for all groups that the nexthop
1178 static void nexthop_replace_notify(struct net
*net
, struct nexthop
*nh
,
1179 struct nl_info
*info
)
1181 struct nh_grp_entry
*nhge
;
1183 __nexthop_replace_notify(net
, nh
, info
);
1185 list_for_each_entry(nhge
, &nh
->grp_list
, nh_list
)
1186 __nexthop_replace_notify(net
, nhge
->nh_parent
, info
);
1189 static int replace_nexthop(struct net
*net
, struct nexthop
*old
,
1190 struct nexthop
*new, struct netlink_ext_ack
*extack
)
1192 bool new_is_reject
= false;
1193 struct nh_grp_entry
*nhge
;
1196 /* check that existing FIB entries are ok with the
1197 * new nexthop definition
1199 err
= fib_check_nh_list(old
, new, extack
);
1203 err
= fib6_check_nh_list(old
, new, extack
);
1207 if (!new->is_group
) {
1208 struct nh_info
*nhi
= rtnl_dereference(new->nh_info
);
1210 new_is_reject
= nhi
->reject_nh
;
1213 list_for_each_entry(nhge
, &old
->grp_list
, nh_list
) {
1214 /* if new nexthop is a blackhole, any groups using this
1215 * nexthop cannot have more than 1 path
1217 if (new_is_reject
&&
1218 nexthop_num_path(nhge
->nh_parent
) > 1) {
1219 NL_SET_ERR_MSG(extack
, "Blackhole nexthop can not be a member of a group with more than one path");
1223 err
= fib_check_nh_list(nhge
->nh_parent
, new, extack
);
1227 err
= fib6_check_nh_list(nhge
->nh_parent
, new, extack
);
1233 err
= replace_nexthop_grp(net
, old
, new, extack
);
1235 err
= replace_nexthop_single(net
, old
, new, extack
);
1238 nh_rt_cache_flush(net
, old
);
1240 __remove_nexthop(net
, new, NULL
);
1247 /* called with rtnl_lock held */
1248 static int insert_nexthop(struct net
*net
, struct nexthop
*new_nh
,
1249 struct nh_config
*cfg
, struct netlink_ext_ack
*extack
)
1251 struct rb_node
**pp
, *parent
= NULL
, *next
;
1252 struct rb_root
*root
= &net
->nexthop
.rb_root
;
1253 bool replace
= !!(cfg
->nlflags
& NLM_F_REPLACE
);
1254 bool create
= !!(cfg
->nlflags
& NLM_F_CREATE
);
1255 u32 new_id
= new_nh
->id
;
1256 int replace_notify
= 0;
1259 pp
= &root
->rb_node
;
1269 nh
= rb_entry(parent
, struct nexthop
, rb_node
);
1270 if (new_id
< nh
->id
) {
1271 pp
= &next
->rb_left
;
1272 } else if (new_id
> nh
->id
) {
1273 pp
= &next
->rb_right
;
1274 } else if (replace
) {
1275 rc
= replace_nexthop(net
, nh
, new_nh
, extack
);
1277 new_nh
= nh
; /* send notification with old nh */
1282 /* id already exists and not a replace */
1287 if (replace
&& !create
) {
1288 NL_SET_ERR_MSG(extack
, "Replace specified without create and no entry exists");
1293 rb_link_node_rcu(&new_nh
->rb_node
, parent
, pp
);
1294 rb_insert_color(&new_nh
->rb_node
, root
);
1296 rc
= call_nexthop_notifiers(net
, NEXTHOP_EVENT_REPLACE
, new_nh
, extack
);
1298 rb_erase(&new_nh
->rb_node
, &net
->nexthop
.rb_root
);
1302 nh_base_seq_inc(net
);
1303 nexthop_notify(RTM_NEWNEXTHOP
, new_nh
, &cfg
->nlinfo
);
1304 if (replace_notify
&& net
->ipv4
.sysctl_nexthop_compat_mode
)
1305 nexthop_replace_notify(net
, new_nh
, &cfg
->nlinfo
);
1312 /* remove all nexthops tied to a device being deleted */
1313 static void nexthop_flush_dev(struct net_device
*dev
)
1315 unsigned int hash
= nh_dev_hashfn(dev
->ifindex
);
1316 struct net
*net
= dev_net(dev
);
1317 struct hlist_head
*head
= &net
->nexthop
.devhash
[hash
];
1318 struct hlist_node
*n
;
1319 struct nh_info
*nhi
;
1321 hlist_for_each_entry_safe(nhi
, n
, head
, dev_hash
) {
1322 if (nhi
->fib_nhc
.nhc_dev
!= dev
)
1325 remove_nexthop(net
, nhi
->nh_parent
, NULL
);
1329 /* rtnl; called when net namespace is deleted */
1330 static void flush_all_nexthops(struct net
*net
)
1332 struct rb_root
*root
= &net
->nexthop
.rb_root
;
1333 struct rb_node
*node
;
1336 while ((node
= rb_first(root
))) {
1337 nh
= rb_entry(node
, struct nexthop
, rb_node
);
1338 remove_nexthop(net
, nh
, NULL
);
1343 static struct nexthop
*nexthop_create_group(struct net
*net
,
1344 struct nh_config
*cfg
)
1346 struct nlattr
*grps_attr
= cfg
->nh_grp
;
1347 struct nexthop_grp
*entry
= nla_data(grps_attr
);
1348 u16 num_nh
= nla_len(grps_attr
) / sizeof(*entry
);
1349 struct nh_group
*nhg
;
1353 if (WARN_ON(!num_nh
))
1354 return ERR_PTR(-EINVAL
);
1356 nh
= nexthop_alloc();
1358 return ERR_PTR(-ENOMEM
);
1362 nhg
= nexthop_grp_alloc(num_nh
);
1365 return ERR_PTR(-ENOMEM
);
1368 /* spare group used for removals */
1369 nhg
->spare
= nexthop_grp_alloc(num_nh
);
1373 return ERR_PTR(-ENOMEM
);
1375 nhg
->spare
->spare
= nhg
;
1377 for (i
= 0; i
< nhg
->num_nh
; ++i
) {
1378 struct nexthop
*nhe
;
1379 struct nh_info
*nhi
;
1381 nhe
= nexthop_find_by_id(net
, entry
[i
].id
);
1382 if (!nexthop_get(nhe
))
1385 nhi
= rtnl_dereference(nhe
->nh_info
);
1386 if (nhi
->family
== AF_INET
)
1389 nhg
->nh_entries
[i
].nh
= nhe
;
1390 nhg
->nh_entries
[i
].weight
= entry
[i
].weight
+ 1;
1391 list_add(&nhg
->nh_entries
[i
].nh_list
, &nhe
->grp_list
);
1392 nhg
->nh_entries
[i
].nh_parent
= nh
;
1395 if (cfg
->nh_grp_type
== NEXTHOP_GRP_TYPE_MPATH
) {
1397 nh_group_rebalance(nhg
);
1403 rcu_assign_pointer(nh
->nh_grp
, nhg
);
1409 nexthop_put(nhg
->nh_entries
[i
].nh
);
1415 return ERR_PTR(-ENOENT
);
1418 static int nh_create_ipv4(struct net
*net
, struct nexthop
*nh
,
1419 struct nh_info
*nhi
, struct nh_config
*cfg
,
1420 struct netlink_ext_ack
*extack
)
1422 struct fib_nh
*fib_nh
= &nhi
->fib_nh
;
1423 struct fib_config fib_cfg
= {
1424 .fc_oif
= cfg
->nh_ifindex
,
1425 .fc_gw4
= cfg
->gw
.ipv4
,
1426 .fc_gw_family
= cfg
->gw
.ipv4
? AF_INET
: 0,
1427 .fc_flags
= cfg
->nh_flags
,
1428 .fc_encap
= cfg
->nh_encap
,
1429 .fc_encap_type
= cfg
->nh_encap_type
,
1431 u32 tb_id
= (cfg
->dev
? l3mdev_fib_table(cfg
->dev
) : RT_TABLE_MAIN
);
1434 err
= fib_nh_init(net
, fib_nh
, &fib_cfg
, 1, extack
);
1436 fib_nh_release(net
, fib_nh
);
1443 /* sets nh_dev if successful */
1444 err
= fib_check_nh(net
, fib_nh
, tb_id
, 0, extack
);
1446 nh
->nh_flags
= fib_nh
->fib_nh_flags
;
1447 fib_info_update_nhc_saddr(net
, &fib_nh
->nh_common
,
1448 fib_nh
->fib_nh_scope
);
1450 fib_nh_release(net
, fib_nh
);
1456 static int nh_create_ipv6(struct net
*net
, struct nexthop
*nh
,
1457 struct nh_info
*nhi
, struct nh_config
*cfg
,
1458 struct netlink_ext_ack
*extack
)
1460 struct fib6_nh
*fib6_nh
= &nhi
->fib6_nh
;
1461 struct fib6_config fib6_cfg
= {
1462 .fc_table
= l3mdev_fib_table(cfg
->dev
),
1463 .fc_ifindex
= cfg
->nh_ifindex
,
1464 .fc_gateway
= cfg
->gw
.ipv6
,
1465 .fc_flags
= cfg
->nh_flags
,
1466 .fc_encap
= cfg
->nh_encap
,
1467 .fc_encap_type
= cfg
->nh_encap_type
,
1468 .fc_is_fdb
= cfg
->nh_fdb
,
1472 if (!ipv6_addr_any(&cfg
->gw
.ipv6
))
1473 fib6_cfg
.fc_flags
|= RTF_GATEWAY
;
1475 /* sets nh_dev if successful */
1476 err
= ipv6_stub
->fib6_nh_init(net
, fib6_nh
, &fib6_cfg
, GFP_KERNEL
,
1479 ipv6_stub
->fib6_nh_release(fib6_nh
);
1481 nh
->nh_flags
= fib6_nh
->fib_nh_flags
;
1486 static struct nexthop
*nexthop_create(struct net
*net
, struct nh_config
*cfg
,
1487 struct netlink_ext_ack
*extack
)
1489 struct nh_info
*nhi
;
1493 nh
= nexthop_alloc();
1495 return ERR_PTR(-ENOMEM
);
1497 nhi
= kzalloc(sizeof(*nhi
), GFP_KERNEL
);
1500 return ERR_PTR(-ENOMEM
);
1503 nh
->nh_flags
= cfg
->nh_flags
;
1506 nhi
->nh_parent
= nh
;
1507 nhi
->family
= cfg
->nh_family
;
1508 nhi
->fib_nhc
.nhc_scope
= RT_SCOPE_LINK
;
1513 if (cfg
->nh_blackhole
) {
1515 cfg
->nh_ifindex
= net
->loopback_dev
->ifindex
;
1518 switch (cfg
->nh_family
) {
1520 err
= nh_create_ipv4(net
, nh
, nhi
, cfg
, extack
);
1523 err
= nh_create_ipv6(net
, nh
, nhi
, cfg
, extack
);
1530 return ERR_PTR(err
);
1533 /* add the entry to the device based hash */
1535 nexthop_devhash_add(net
, nhi
);
1537 rcu_assign_pointer(nh
->nh_info
, nhi
);
1542 /* called with rtnl lock held */
1543 static struct nexthop
*nexthop_add(struct net
*net
, struct nh_config
*cfg
,
1544 struct netlink_ext_ack
*extack
)
1549 if (cfg
->nlflags
& NLM_F_REPLACE
&& !cfg
->nh_id
) {
1550 NL_SET_ERR_MSG(extack
, "Replace requires nexthop id");
1551 return ERR_PTR(-EINVAL
);
1555 cfg
->nh_id
= nh_find_unused_id(net
);
1557 NL_SET_ERR_MSG(extack
, "No unused id");
1558 return ERR_PTR(-EINVAL
);
1563 nh
= nexthop_create_group(net
, cfg
);
1565 nh
= nexthop_create(net
, cfg
, extack
);
1570 refcount_set(&nh
->refcnt
, 1);
1571 nh
->id
= cfg
->nh_id
;
1572 nh
->protocol
= cfg
->nh_protocol
;
1575 err
= insert_nexthop(net
, nh
, cfg
, extack
);
1577 __remove_nexthop(net
, nh
, NULL
);
1585 static int rtm_to_nh_config(struct net
*net
, struct sk_buff
*skb
,
1586 struct nlmsghdr
*nlh
, struct nh_config
*cfg
,
1587 struct netlink_ext_ack
*extack
)
1589 struct nhmsg
*nhm
= nlmsg_data(nlh
);
1590 struct nlattr
*tb
[NHA_MAX
+ 1];
1593 err
= nlmsg_parse(nlh
, sizeof(*nhm
), tb
, NHA_MAX
, rtm_nh_policy
,
1599 if (nhm
->resvd
|| nhm
->nh_scope
) {
1600 NL_SET_ERR_MSG(extack
, "Invalid values in ancillary header");
1603 if (nhm
->nh_flags
& ~NEXTHOP_VALID_USER_FLAGS
) {
1604 NL_SET_ERR_MSG(extack
, "Invalid nexthop flags in ancillary header");
1608 switch (nhm
->nh_family
) {
1617 NL_SET_ERR_MSG(extack
, "Invalid address family");
1621 if (tb
[NHA_GROUPS
] || tb
[NHA_MASTER
]) {
1622 NL_SET_ERR_MSG(extack
, "Invalid attributes in request");
1626 memset(cfg
, 0, sizeof(*cfg
));
1627 cfg
->nlflags
= nlh
->nlmsg_flags
;
1628 cfg
->nlinfo
.portid
= NETLINK_CB(skb
).portid
;
1629 cfg
->nlinfo
.nlh
= nlh
;
1630 cfg
->nlinfo
.nl_net
= net
;
1632 cfg
->nh_family
= nhm
->nh_family
;
1633 cfg
->nh_protocol
= nhm
->nh_protocol
;
1634 cfg
->nh_flags
= nhm
->nh_flags
;
1637 cfg
->nh_id
= nla_get_u32(tb
[NHA_ID
]);
1640 if (tb
[NHA_OIF
] || tb
[NHA_BLACKHOLE
] ||
1641 tb
[NHA_ENCAP
] || tb
[NHA_ENCAP_TYPE
]) {
1642 NL_SET_ERR_MSG(extack
, "Fdb attribute can not be used with encap, oif or blackhole");
1645 if (nhm
->nh_flags
) {
1646 NL_SET_ERR_MSG(extack
, "Unsupported nexthop flags in ancillary header");
1649 cfg
->nh_fdb
= nla_get_flag(tb
[NHA_FDB
]);
1652 if (tb
[NHA_GROUP
]) {
1653 if (nhm
->nh_family
!= AF_UNSPEC
) {
1654 NL_SET_ERR_MSG(extack
, "Invalid family for group");
1657 cfg
->nh_grp
= tb
[NHA_GROUP
];
1659 cfg
->nh_grp_type
= NEXTHOP_GRP_TYPE_MPATH
;
1660 if (tb
[NHA_GROUP_TYPE
])
1661 cfg
->nh_grp_type
= nla_get_u16(tb
[NHA_GROUP_TYPE
]);
1663 if (cfg
->nh_grp_type
> NEXTHOP_GRP_TYPE_MAX
) {
1664 NL_SET_ERR_MSG(extack
, "Invalid group type");
1667 err
= nh_check_attr_group(net
, tb
, extack
);
1669 /* no other attributes should be set */
1673 if (tb
[NHA_BLACKHOLE
]) {
1674 if (tb
[NHA_GATEWAY
] || tb
[NHA_OIF
] ||
1675 tb
[NHA_ENCAP
] || tb
[NHA_ENCAP_TYPE
] || tb
[NHA_FDB
]) {
1676 NL_SET_ERR_MSG(extack
, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
1680 cfg
->nh_blackhole
= 1;
1685 if (!cfg
->nh_fdb
&& !tb
[NHA_OIF
]) {
1686 NL_SET_ERR_MSG(extack
, "Device attribute required for non-blackhole and non-fdb nexthops");
1690 if (!cfg
->nh_fdb
&& tb
[NHA_OIF
]) {
1691 cfg
->nh_ifindex
= nla_get_u32(tb
[NHA_OIF
]);
1692 if (cfg
->nh_ifindex
)
1693 cfg
->dev
= __dev_get_by_index(net
, cfg
->nh_ifindex
);
1696 NL_SET_ERR_MSG(extack
, "Invalid device index");
1698 } else if (!(cfg
->dev
->flags
& IFF_UP
)) {
1699 NL_SET_ERR_MSG(extack
, "Nexthop device is not up");
1702 } else if (!netif_carrier_ok(cfg
->dev
)) {
1703 NL_SET_ERR_MSG(extack
, "Carrier for nexthop device is down");
1710 if (tb
[NHA_GATEWAY
]) {
1711 struct nlattr
*gwa
= tb
[NHA_GATEWAY
];
1713 switch (cfg
->nh_family
) {
1715 if (nla_len(gwa
) != sizeof(u32
)) {
1716 NL_SET_ERR_MSG(extack
, "Invalid gateway");
1719 cfg
->gw
.ipv4
= nla_get_be32(gwa
);
1722 if (nla_len(gwa
) != sizeof(struct in6_addr
)) {
1723 NL_SET_ERR_MSG(extack
, "Invalid gateway");
1726 cfg
->gw
.ipv6
= nla_get_in6_addr(gwa
);
1729 NL_SET_ERR_MSG(extack
,
1730 "Unknown address family for gateway");
1734 /* device only nexthop (no gateway) */
1735 if (cfg
->nh_flags
& RTNH_F_ONLINK
) {
1736 NL_SET_ERR_MSG(extack
,
1737 "ONLINK flag can not be set for nexthop without a gateway");
1742 if (tb
[NHA_ENCAP
]) {
1743 cfg
->nh_encap
= tb
[NHA_ENCAP
];
1745 if (!tb
[NHA_ENCAP_TYPE
]) {
1746 NL_SET_ERR_MSG(extack
, "LWT encapsulation type is missing");
1750 cfg
->nh_encap_type
= nla_get_u16(tb
[NHA_ENCAP_TYPE
]);
1751 err
= lwtunnel_valid_encap_type(cfg
->nh_encap_type
, extack
);
1755 } else if (tb
[NHA_ENCAP_TYPE
]) {
1756 NL_SET_ERR_MSG(extack
, "LWT encapsulation attribute is missing");
1767 static int rtm_new_nexthop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1768 struct netlink_ext_ack
*extack
)
1770 struct net
*net
= sock_net(skb
->sk
);
1771 struct nh_config cfg
;
1775 err
= rtm_to_nh_config(net
, skb
, nlh
, &cfg
, extack
);
1777 nh
= nexthop_add(net
, &cfg
, extack
);
1785 static int nh_valid_get_del_req(struct nlmsghdr
*nlh
, u32
*id
,
1786 struct netlink_ext_ack
*extack
)
1788 struct nhmsg
*nhm
= nlmsg_data(nlh
);
1789 struct nlattr
*tb
[NHA_MAX
+ 1];
1792 err
= nlmsg_parse(nlh
, sizeof(*nhm
), tb
, NHA_MAX
, rtm_nh_policy
,
1798 for (i
= 0; i
< __NHA_MAX
; ++i
) {
1806 NL_SET_ERR_MSG_ATTR(extack
, tb
[i
],
1807 "Unexpected attribute in request");
1811 if (nhm
->nh_protocol
|| nhm
->resvd
|| nhm
->nh_scope
|| nhm
->nh_flags
) {
1812 NL_SET_ERR_MSG(extack
, "Invalid values in header");
1817 NL_SET_ERR_MSG(extack
, "Nexthop id is missing");
1821 *id
= nla_get_u32(tb
[NHA_ID
]);
1823 NL_SET_ERR_MSG(extack
, "Invalid nexthop id");
1831 static int rtm_del_nexthop(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1832 struct netlink_ext_ack
*extack
)
1834 struct net
*net
= sock_net(skb
->sk
);
1835 struct nl_info nlinfo
= {
1838 .portid
= NETLINK_CB(skb
).portid
,
1844 err
= nh_valid_get_del_req(nlh
, &id
, extack
);
1848 nh
= nexthop_find_by_id(net
, id
);
1852 remove_nexthop(net
, nh
, &nlinfo
);
1858 static int rtm_get_nexthop(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
1859 struct netlink_ext_ack
*extack
)
1861 struct net
*net
= sock_net(in_skb
->sk
);
1862 struct sk_buff
*skb
= NULL
;
1867 err
= nh_valid_get_del_req(nlh
, &id
, extack
);
1872 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1877 nh
= nexthop_find_by_id(net
, id
);
1881 err
= nh_fill_node(skb
, nh
, RTM_NEWNEXTHOP
, NETLINK_CB(in_skb
).portid
,
1884 WARN_ON(err
== -EMSGSIZE
);
1888 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
1896 static bool nh_dump_filtered(struct nexthop
*nh
, int dev_idx
, int master_idx
,
1897 bool group_filter
, u8 family
)
1899 const struct net_device
*dev
;
1900 const struct nh_info
*nhi
;
1902 if (group_filter
&& !nh
->is_group
)
1905 if (!dev_idx
&& !master_idx
&& !family
)
1911 nhi
= rtnl_dereference(nh
->nh_info
);
1912 if (family
&& nhi
->family
!= family
)
1915 dev
= nhi
->fib_nhc
.nhc_dev
;
1916 if (dev_idx
&& (!dev
|| dev
->ifindex
!= dev_idx
))
1920 struct net_device
*master
;
1925 master
= netdev_master_upper_dev_get((struct net_device
*)dev
);
1926 if (!master
|| master
->ifindex
!= master_idx
)
1933 static int nh_valid_dump_req(const struct nlmsghdr
*nlh
, int *dev_idx
,
1934 int *master_idx
, bool *group_filter
,
1935 bool *fdb_filter
, struct netlink_callback
*cb
)
1937 struct netlink_ext_ack
*extack
= cb
->extack
;
1938 struct nlattr
*tb
[NHA_MAX
+ 1];
1943 err
= nlmsg_parse(nlh
, sizeof(*nhm
), tb
, NHA_MAX
, rtm_nh_policy
,
1948 for (i
= 0; i
<= NHA_MAX
; ++i
) {
1954 idx
= nla_get_u32(tb
[i
]);
1955 if (idx
> INT_MAX
) {
1956 NL_SET_ERR_MSG(extack
, "Invalid device index");
1962 idx
= nla_get_u32(tb
[i
]);
1963 if (idx
> INT_MAX
) {
1964 NL_SET_ERR_MSG(extack
, "Invalid master device index");
1970 *group_filter
= true;
1976 NL_SET_ERR_MSG(extack
, "Unsupported attribute in dump request");
1981 nhm
= nlmsg_data(nlh
);
1982 if (nhm
->nh_protocol
|| nhm
->resvd
|| nhm
->nh_scope
|| nhm
->nh_flags
) {
1983 NL_SET_ERR_MSG(extack
, "Invalid values in header for nexthop dump request");
1991 static int rtm_dump_nexthop(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1993 bool group_filter
= false, fdb_filter
= false;
1994 struct nhmsg
*nhm
= nlmsg_data(cb
->nlh
);
1995 int dev_filter_idx
= 0, master_idx
= 0;
1996 struct net
*net
= sock_net(skb
->sk
);
1997 struct rb_root
*root
= &net
->nexthop
.rb_root
;
1998 struct rb_node
*node
;
2002 err
= nh_valid_dump_req(cb
->nlh
, &dev_filter_idx
, &master_idx
,
2003 &group_filter
, &fdb_filter
, cb
);
2007 s_idx
= cb
->args
[0];
2008 for (node
= rb_first(root
); node
; node
= rb_next(node
)) {
2014 nh
= rb_entry(node
, struct nexthop
, rb_node
);
2015 if (nh_dump_filtered(nh
, dev_filter_idx
, master_idx
,
2016 group_filter
, nhm
->nh_family
))
2019 err
= nh_fill_node(skb
, nh
, RTM_NEWNEXTHOP
,
2020 NETLINK_CB(cb
->skb
).portid
,
2021 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
2023 if (likely(skb
->len
))
2036 cb
->seq
= net
->nexthop
.seq
;
2037 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2042 static void nexthop_sync_mtu(struct net_device
*dev
, u32 orig_mtu
)
2044 unsigned int hash
= nh_dev_hashfn(dev
->ifindex
);
2045 struct net
*net
= dev_net(dev
);
2046 struct hlist_head
*head
= &net
->nexthop
.devhash
[hash
];
2047 struct hlist_node
*n
;
2048 struct nh_info
*nhi
;
2050 hlist_for_each_entry_safe(nhi
, n
, head
, dev_hash
) {
2051 if (nhi
->fib_nhc
.nhc_dev
== dev
) {
2052 if (nhi
->family
== AF_INET
)
2053 fib_nhc_update_mtu(&nhi
->fib_nhc
, dev
->mtu
,
2060 static int nh_netdev_event(struct notifier_block
*this,
2061 unsigned long event
, void *ptr
)
2063 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2064 struct netdev_notifier_info_ext
*info_ext
;
2068 case NETDEV_UNREGISTER
:
2069 nexthop_flush_dev(dev
);
2072 if (!(dev_get_flags(dev
) & (IFF_RUNNING
| IFF_LOWER_UP
)))
2073 nexthop_flush_dev(dev
);
2075 case NETDEV_CHANGEMTU
:
2077 nexthop_sync_mtu(dev
, info_ext
->ext
.mtu
);
2078 rt_cache_flush(dev_net(dev
));
2084 static struct notifier_block nh_netdev_notifier
= {
2085 .notifier_call
= nh_netdev_event
,
2088 int register_nexthop_notifier(struct net
*net
, struct notifier_block
*nb
)
2090 return blocking_notifier_chain_register(&net
->nexthop
.notifier_chain
,
2093 EXPORT_SYMBOL(register_nexthop_notifier
);
2095 int unregister_nexthop_notifier(struct net
*net
, struct notifier_block
*nb
)
2097 return blocking_notifier_chain_unregister(&net
->nexthop
.notifier_chain
,
2100 EXPORT_SYMBOL(unregister_nexthop_notifier
);
2102 void nexthop_set_hw_flags(struct net
*net
, u32 id
, bool offload
, bool trap
)
2104 struct nexthop
*nexthop
;
2108 nexthop
= nexthop_find_by_id(net
, id
);
2112 nexthop
->nh_flags
&= ~(RTNH_F_OFFLOAD
| RTNH_F_TRAP
);
2114 nexthop
->nh_flags
|= RTNH_F_OFFLOAD
;
2116 nexthop
->nh_flags
|= RTNH_F_TRAP
;
2121 EXPORT_SYMBOL(nexthop_set_hw_flags
);
2123 static void __net_exit
nexthop_net_exit(struct net
*net
)
2126 flush_all_nexthops(net
);
2128 kfree(net
->nexthop
.devhash
);
2131 static int __net_init
nexthop_net_init(struct net
*net
)
2133 size_t sz
= sizeof(struct hlist_head
) * NH_DEV_HASHSIZE
;
2135 net
->nexthop
.rb_root
= RB_ROOT
;
2136 net
->nexthop
.devhash
= kzalloc(sz
, GFP_KERNEL
);
2137 if (!net
->nexthop
.devhash
)
2139 BLOCKING_INIT_NOTIFIER_HEAD(&net
->nexthop
.notifier_chain
);
2144 static struct pernet_operations nexthop_net_ops
= {
2145 .init
= nexthop_net_init
,
2146 .exit
= nexthop_net_exit
,
2149 static int __init
nexthop_init(void)
2151 register_pernet_subsys(&nexthop_net_ops
);
2153 register_netdevice_notifier(&nh_netdev_notifier
);
2155 rtnl_register(PF_UNSPEC
, RTM_NEWNEXTHOP
, rtm_new_nexthop
, NULL
, 0);
2156 rtnl_register(PF_UNSPEC
, RTM_DELNEXTHOP
, rtm_del_nexthop
, NULL
, 0);
2157 rtnl_register(PF_UNSPEC
, RTM_GETNEXTHOP
, rtm_get_nexthop
,
2158 rtm_dump_nexthop
, 0);
2160 rtnl_register(PF_INET
, RTM_NEWNEXTHOP
, rtm_new_nexthop
, NULL
, 0);
2161 rtnl_register(PF_INET
, RTM_GETNEXTHOP
, NULL
, rtm_dump_nexthop
, 0);
2163 rtnl_register(PF_INET6
, RTM_NEWNEXTHOP
, rtm_new_nexthop
, NULL
, 0);
2164 rtnl_register(PF_INET6
, RTM_GETNEXTHOP
, NULL
, rtm_dump_nexthop
, 0);
2168 subsys_initcall(nexthop_init
);