1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Generic nexthop implementation
5 * Copyright (c) 2017-19 Cumulus Networks
6 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
9 #ifndef __LINUX_NEXTHOP_H
10 #define __LINUX_NEXTHOP_H
12 #include <linux/netdevice.h>
13 #include <linux/notifier.h>
14 #include <linux/route.h>
15 #include <linux/types.h>
16 #include <net/ip_fib.h>
17 #include <net/ip6_fib.h>
18 #include <net/netlink.h>
20 #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
34 struct net_device *dev;
41 struct nlattr *nh_grp;
44 struct nlattr *nh_encap;
48 struct nl_info nlinfo;
52 struct hlist_node dev_hash; /* entry on netns devhash */
53 struct nexthop *nh_parent;
60 struct fib_nh_common fib_nhc;
62 struct fib6_nh fib6_nh;
76 struct list_head nh_list;
77 struct nexthop *nh_parent; /* nexthop of group with this entry */
81 struct nh_group *spare; /* spare group for removals */
86 struct nh_grp_entry nh_entries[];
90 struct rb_node rb_node; /* entry on netns rbtree */
91 struct list_head fi_list; /* v4 entries using nh */
92 struct list_head f6i_list; /* v6 entries using nh */
93 struct list_head fdb_list; /* fdb entries using this nh */
94 struct list_head grp_list; /* nh group entries using this nh */
99 u8 protocol; /* app managing this nh */
107 struct nh_info __rcu *nh_info;
108 struct nh_group __rcu *nh_grp;
112 enum nexthop_event_type {
114 NEXTHOP_EVENT_REPLACE,
117 struct nh_notifier_single_info {
118 struct net_device *dev;
122 struct in6_addr ipv6;
129 struct nh_notifier_grp_entry_info {
132 struct nh_notifier_single_info nh;
135 struct nh_notifier_grp_info {
138 struct nh_notifier_grp_entry_info nh_entries[];
141 struct nh_notifier_info {
143 struct netlink_ext_ack *extack;
147 struct nh_notifier_single_info *nh;
148 struct nh_notifier_grp_info *nh_grp;
152 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
153 struct netlink_ext_ack *extack);
154 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
155 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
157 /* caller is holding rcu or rtnl; no reference taken to nexthop */
158 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
159 void nexthop_free_rcu(struct rcu_head *head);
161 static inline bool nexthop_get(struct nexthop *nh)
163 return refcount_inc_not_zero(&nh->refcnt);
166 static inline void nexthop_put(struct nexthop *nh)
168 if (refcount_dec_and_test(&nh->refcnt))
169 call_rcu(&nh->rcu, nexthop_free_rcu);
172 static inline bool nexthop_cmp(const struct nexthop *nh1,
173 const struct nexthop *nh2)
178 static inline bool nexthop_is_fdb(const struct nexthop *nh)
181 const struct nh_group *nh_grp;
183 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
184 return nh_grp->fdb_nh;
186 const struct nh_info *nhi;
188 nhi = rcu_dereference_rtnl(nh->nh_info);
193 static inline bool nexthop_has_v4(const struct nexthop *nh)
196 struct nh_group *nh_grp;
198 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
199 return nh_grp->has_v4;
204 static inline bool nexthop_is_multipath(const struct nexthop *nh)
207 struct nh_group *nh_grp;
209 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
210 return nh_grp->mpath;
215 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
217 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
222 struct nh_group *nh_grp;
224 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
233 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
235 /* for_nexthops macros in fib_semantics.c grabs a pointer to
236 * the nexthop before checking nhsel
238 if (nhsel >= nhg->num_nh)
241 return nhg->nh_entries[nhsel].nh;
245 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
248 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
251 for (i = 0; i < nhg->num_nh; i++) {
252 struct nexthop *nhe = nhg->nh_entries[i].nh;
253 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
254 struct fib_nh_common *nhc = &nhi->fib_nhc;
255 int weight = nhg->nh_entries[i].weight;
257 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
264 /* called with rcu lock */
265 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
267 const struct nh_info *nhi;
270 struct nh_group *nh_grp;
272 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
273 if (nh_grp->num_nh > 1)
276 nh = nh_grp->nh_entries[0].nh;
279 nhi = rcu_dereference_rtnl(nh->nh_info);
280 return nhi->reject_nh;
283 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
288 nh = nexthop_select_path(res->fi->nh, hash);
289 nhi = rcu_dereference(nh->nh_info);
290 res->nhc = &nhi->fib_nhc;
293 /* called with rcu read lock or rtnl held */
295 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
299 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
300 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
303 struct nh_group *nh_grp;
305 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
307 nh = nexthop_mpath_select(nh_grp, nhsel);
313 nhi = rcu_dereference_rtnl(nh->nh_info);
314 return &nhi->fib_nhc;
317 /* called from fib_table_lookup with rcu_lock */
319 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
321 const struct flowi4 *flp,
327 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
330 for (i = 0; i < nhg->num_nh; i++) {
331 struct nexthop *nhe = nhg->nh_entries[i].nh;
333 nhi = rcu_dereference(nhe->nh_info);
334 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
336 return &nhi->fib_nhc;
340 nhi = rcu_dereference(nh->nh_info);
341 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
343 return &nhi->fib_nhc;
350 static inline bool nexthop_uses_dev(const struct nexthop *nh,
351 const struct net_device *dev)
356 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
359 for (i = 0; i < nhg->num_nh; i++) {
360 struct nexthop *nhe = nhg->nh_entries[i].nh;
362 nhi = rcu_dereference(nhe->nh_info);
363 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
367 nhi = rcu_dereference(nh->nh_info);
368 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
375 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
377 if (unlikely(fi->nh))
378 return nexthop_num_path(fi->nh);
383 int fib_check_nexthop(struct nexthop *nh, u8 scope,
384 struct netlink_ext_ack *extack);
386 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
388 if (unlikely(fi->nh))
389 return nexthop_fib_nhc(fi->nh, nhsel);
391 return &fi->fib_nh[nhsel].nh_common;
394 /* only used when fib_nh is built into fib_info */
395 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
399 return &fi->fib_nh[nhsel];
405 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
406 struct netlink_ext_ack *extack);
408 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
413 struct nh_group *nh_grp;
415 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
416 nh = nexthop_mpath_select(nh_grp, 0);
421 nhi = rcu_dereference_rtnl(nh->nh_info);
422 if (nhi->family == AF_INET6)
423 return &nhi->fib6_nh;
428 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
430 struct fib6_nh *fib6_nh;
432 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
433 return fib6_nh->fib_nh_dev;
436 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
438 struct nexthop *nh = res->f6i->nh;
441 nh = nexthop_select_path(nh, hash);
443 nhi = rcu_dereference_rtnl(nh->nh_info);
444 if (nhi->reject_nh) {
445 res->fib6_type = RTN_BLACKHOLE;
446 res->fib6_flags |= RTF_REJECT;
447 res->nh = nexthop_fib6_nh(nh);
449 res->nh = &nhi->fib6_nh;
453 int nexthop_for_each_fib6_nh(struct nexthop *nh,
454 int (*cb)(struct fib6_nh *nh, void *arg),
457 static inline int nexthop_get_family(struct nexthop *nh)
459 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
465 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
467 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
469 return &nhi->fib_nhc;
472 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
478 nhp = nexthop_select_path(nh, hash);
481 nhi = rcu_dereference(nhp->nh_info);
482 return &nhi->fib_nhc;