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;
71 struct list_head nh_list;
72 struct nexthop *nh_parent; /* nexthop of group with this entry */
76 struct nh_group *spare; /* spare group for removals */
81 struct nh_grp_entry nh_entries[];
85 struct rb_node rb_node; /* entry on netns rbtree */
86 struct list_head fi_list; /* v4 entries using nh */
87 struct list_head f6i_list; /* v6 entries using nh */
88 struct list_head fdb_list; /* fdb entries using this nh */
89 struct list_head grp_list; /* nh group entries using this nh */
94 u8 protocol; /* app managing this nh */
102 struct nh_info __rcu *nh_info;
103 struct nh_group __rcu *nh_grp;
107 enum nexthop_event_type {
109 NEXTHOP_EVENT_REPLACE,
112 struct nh_notifier_single_info {
113 struct net_device *dev;
117 struct in6_addr ipv6;
124 struct nh_notifier_grp_entry_info {
127 struct nh_notifier_single_info nh;
130 struct nh_notifier_grp_info {
133 struct nh_notifier_grp_entry_info nh_entries[];
136 struct nh_notifier_info {
138 struct netlink_ext_ack *extack;
142 struct nh_notifier_single_info *nh;
143 struct nh_notifier_grp_info *nh_grp;
147 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
148 struct netlink_ext_ack *extack);
149 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
150 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
152 /* caller is holding rcu or rtnl; no reference taken to nexthop */
153 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
154 void nexthop_free_rcu(struct rcu_head *head);
156 static inline bool nexthop_get(struct nexthop *nh)
158 return refcount_inc_not_zero(&nh->refcnt);
161 static inline void nexthop_put(struct nexthop *nh)
163 if (refcount_dec_and_test(&nh->refcnt))
164 call_rcu(&nh->rcu, nexthop_free_rcu);
167 static inline bool nexthop_cmp(const struct nexthop *nh1,
168 const struct nexthop *nh2)
173 static inline bool nexthop_is_fdb(const struct nexthop *nh)
176 const struct nh_group *nh_grp;
178 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
179 return nh_grp->fdb_nh;
181 const struct nh_info *nhi;
183 nhi = rcu_dereference_rtnl(nh->nh_info);
188 static inline bool nexthop_has_v4(const struct nexthop *nh)
191 struct nh_group *nh_grp;
193 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
194 return nh_grp->has_v4;
199 static inline bool nexthop_is_multipath(const struct nexthop *nh)
202 struct nh_group *nh_grp;
204 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
205 return nh_grp->mpath;
210 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
212 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
217 struct nh_group *nh_grp;
219 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
228 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
230 /* for_nexthops macros in fib_semantics.c grabs a pointer to
231 * the nexthop before checking nhsel
233 if (nhsel >= nhg->num_nh)
236 return nhg->nh_entries[nhsel].nh;
240 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
243 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
246 for (i = 0; i < nhg->num_nh; i++) {
247 struct nexthop *nhe = nhg->nh_entries[i].nh;
248 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
249 struct fib_nh_common *nhc = &nhi->fib_nhc;
250 int weight = nhg->nh_entries[i].weight;
252 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
259 /* called with rcu lock */
260 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
262 const struct nh_info *nhi;
265 struct nh_group *nh_grp;
267 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
268 if (nh_grp->num_nh > 1)
271 nh = nh_grp->nh_entries[0].nh;
274 nhi = rcu_dereference_rtnl(nh->nh_info);
275 return nhi->reject_nh;
278 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
283 nh = nexthop_select_path(res->fi->nh, hash);
284 nhi = rcu_dereference(nh->nh_info);
285 res->nhc = &nhi->fib_nhc;
288 /* called with rcu read lock or rtnl held */
290 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
294 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
295 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
298 struct nh_group *nh_grp;
300 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
302 nh = nexthop_mpath_select(nh_grp, nhsel);
308 nhi = rcu_dereference_rtnl(nh->nh_info);
309 return &nhi->fib_nhc;
312 /* called from fib_table_lookup with rcu_lock */
314 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
316 const struct flowi4 *flp,
322 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
325 for (i = 0; i < nhg->num_nh; i++) {
326 struct nexthop *nhe = nhg->nh_entries[i].nh;
328 nhi = rcu_dereference(nhe->nh_info);
329 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
331 return &nhi->fib_nhc;
335 nhi = rcu_dereference(nh->nh_info);
336 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
338 return &nhi->fib_nhc;
345 static inline bool nexthop_uses_dev(const struct nexthop *nh,
346 const struct net_device *dev)
351 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
354 for (i = 0; i < nhg->num_nh; i++) {
355 struct nexthop *nhe = nhg->nh_entries[i].nh;
357 nhi = rcu_dereference(nhe->nh_info);
358 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
362 nhi = rcu_dereference(nh->nh_info);
363 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
370 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
372 if (unlikely(fi->nh))
373 return nexthop_num_path(fi->nh);
378 int fib_check_nexthop(struct nexthop *nh, u8 scope,
379 struct netlink_ext_ack *extack);
381 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
383 if (unlikely(fi->nh))
384 return nexthop_fib_nhc(fi->nh, nhsel);
386 return &fi->fib_nh[nhsel].nh_common;
389 /* only used when fib_nh is built into fib_info */
390 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
394 return &fi->fib_nh[nhsel];
400 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
401 struct netlink_ext_ack *extack);
403 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
408 struct nh_group *nh_grp;
410 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
411 nh = nexthop_mpath_select(nh_grp, 0);
416 nhi = rcu_dereference_rtnl(nh->nh_info);
417 if (nhi->family == AF_INET6)
418 return &nhi->fib6_nh;
423 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
425 struct fib6_nh *fib6_nh;
427 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
428 return fib6_nh->fib_nh_dev;
431 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
433 struct nexthop *nh = res->f6i->nh;
436 nh = nexthop_select_path(nh, hash);
438 nhi = rcu_dereference_rtnl(nh->nh_info);
439 if (nhi->reject_nh) {
440 res->fib6_type = RTN_BLACKHOLE;
441 res->fib6_flags |= RTF_REJECT;
442 res->nh = nexthop_fib6_nh(nh);
444 res->nh = &nhi->fib6_nh;
448 int nexthop_for_each_fib6_nh(struct nexthop *nh,
449 int (*cb)(struct fib6_nh *nh, void *arg),
452 static inline int nexthop_get_family(struct nexthop *nh)
454 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
460 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
462 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
464 return &nhi->fib_nhc;
467 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
473 nhp = nexthop_select_path(nh, hash);
476 nhi = rcu_dereference(nhp->nh_info);
477 return &nhi->fib_nhc;