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 {
111 int register_nexthop_notifier(struct net *net, struct notifier_block *nb);
112 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
114 /* caller is holding rcu or rtnl; no reference taken to nexthop */
115 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
116 void nexthop_free_rcu(struct rcu_head *head);
118 static inline bool nexthop_get(struct nexthop *nh)
120 return refcount_inc_not_zero(&nh->refcnt);
123 static inline void nexthop_put(struct nexthop *nh)
125 if (refcount_dec_and_test(&nh->refcnt))
126 call_rcu(&nh->rcu, nexthop_free_rcu);
129 static inline bool nexthop_cmp(const struct nexthop *nh1,
130 const struct nexthop *nh2)
135 static inline bool nexthop_is_fdb(const struct nexthop *nh)
138 const struct nh_group *nh_grp;
140 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
141 return nh_grp->fdb_nh;
143 const struct nh_info *nhi;
145 nhi = rcu_dereference_rtnl(nh->nh_info);
150 static inline bool nexthop_has_v4(const struct nexthop *nh)
153 struct nh_group *nh_grp;
155 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
156 return nh_grp->has_v4;
161 static inline bool nexthop_is_multipath(const struct nexthop *nh)
164 struct nh_group *nh_grp;
166 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
167 return nh_grp->mpath;
172 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
174 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
179 struct nh_group *nh_grp;
181 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
190 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
192 /* for_nexthops macros in fib_semantics.c grabs a pointer to
193 * the nexthop before checking nhsel
195 if (nhsel >= nhg->num_nh)
198 return nhg->nh_entries[nhsel].nh;
202 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
205 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
208 for (i = 0; i < nhg->num_nh; i++) {
209 struct nexthop *nhe = nhg->nh_entries[i].nh;
210 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
211 struct fib_nh_common *nhc = &nhi->fib_nhc;
212 int weight = nhg->nh_entries[i].weight;
214 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
221 /* called with rcu lock */
222 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
224 const struct nh_info *nhi;
227 struct nh_group *nh_grp;
229 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
230 if (nh_grp->num_nh > 1)
233 nh = nh_grp->nh_entries[0].nh;
236 nhi = rcu_dereference_rtnl(nh->nh_info);
237 return nhi->reject_nh;
240 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
245 nh = nexthop_select_path(res->fi->nh, hash);
246 nhi = rcu_dereference(nh->nh_info);
247 res->nhc = &nhi->fib_nhc;
250 /* called with rcu read lock or rtnl held */
252 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
256 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
257 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
260 struct nh_group *nh_grp;
262 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
264 nh = nexthop_mpath_select(nh_grp, nhsel);
270 nhi = rcu_dereference_rtnl(nh->nh_info);
271 return &nhi->fib_nhc;
274 /* called from fib_table_lookup with rcu_lock */
276 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
278 const struct flowi4 *flp,
284 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
287 for (i = 0; i < nhg->num_nh; i++) {
288 struct nexthop *nhe = nhg->nh_entries[i].nh;
290 nhi = rcu_dereference(nhe->nh_info);
291 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
293 return &nhi->fib_nhc;
297 nhi = rcu_dereference(nh->nh_info);
298 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
300 return &nhi->fib_nhc;
307 static inline bool nexthop_uses_dev(const struct nexthop *nh,
308 const struct net_device *dev)
313 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
316 for (i = 0; i < nhg->num_nh; i++) {
317 struct nexthop *nhe = nhg->nh_entries[i].nh;
319 nhi = rcu_dereference(nhe->nh_info);
320 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
324 nhi = rcu_dereference(nh->nh_info);
325 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
332 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
334 if (unlikely(fi->nh))
335 return nexthop_num_path(fi->nh);
340 int fib_check_nexthop(struct nexthop *nh, u8 scope,
341 struct netlink_ext_ack *extack);
343 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
345 if (unlikely(fi->nh))
346 return nexthop_fib_nhc(fi->nh, nhsel);
348 return &fi->fib_nh[nhsel].nh_common;
351 /* only used when fib_nh is built into fib_info */
352 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
356 return &fi->fib_nh[nhsel];
362 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
363 struct netlink_ext_ack *extack);
365 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
370 struct nh_group *nh_grp;
372 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
373 nh = nexthop_mpath_select(nh_grp, 0);
378 nhi = rcu_dereference_rtnl(nh->nh_info);
379 if (nhi->family == AF_INET6)
380 return &nhi->fib6_nh;
385 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
387 struct fib6_nh *fib6_nh;
389 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
390 return fib6_nh->fib_nh_dev;
393 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
395 struct nexthop *nh = res->f6i->nh;
398 nh = nexthop_select_path(nh, hash);
400 nhi = rcu_dereference_rtnl(nh->nh_info);
401 if (nhi->reject_nh) {
402 res->fib6_type = RTN_BLACKHOLE;
403 res->fib6_flags |= RTF_REJECT;
404 res->nh = nexthop_fib6_nh(nh);
406 res->nh = &nhi->fib6_nh;
410 int nexthop_for_each_fib6_nh(struct nexthop *nh,
411 int (*cb)(struct fib6_nh *nh, void *arg),
414 static inline int nexthop_get_family(struct nexthop *nh)
416 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
422 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
424 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
426 return &nhi->fib_nhc;
429 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
435 nhp = nexthop_select_path(nh, hash);
438 nhi = rcu_dereference(nhp->nh_info);
439 return &nhi->fib_nhc;