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
{
112 int call_nexthop_notifier(struct notifier_block
*nb
, struct net
*net
,
113 enum nexthop_event_type event_type
,
115 int register_nexthop_notifier(struct net
*net
, struct notifier_block
*nb
);
116 int unregister_nexthop_notifier(struct net
*net
, struct notifier_block
*nb
);
118 /* caller is holding rcu or rtnl; no reference taken to nexthop */
119 struct nexthop
*nexthop_find_by_id(struct net
*net
, u32 id
);
120 void nexthop_free_rcu(struct rcu_head
*head
);
122 static inline bool nexthop_get(struct nexthop
*nh
)
124 return refcount_inc_not_zero(&nh
->refcnt
);
127 static inline void nexthop_put(struct nexthop
*nh
)
129 if (refcount_dec_and_test(&nh
->refcnt
))
130 call_rcu(&nh
->rcu
, nexthop_free_rcu
);
133 static inline bool nexthop_cmp(const struct nexthop
*nh1
,
134 const struct nexthop
*nh2
)
139 static inline bool nexthop_is_fdb(const struct nexthop
*nh
)
142 const struct nh_group
*nh_grp
;
144 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
145 return nh_grp
->fdb_nh
;
147 const struct nh_info
*nhi
;
149 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
154 static inline bool nexthop_has_v4(const struct nexthop
*nh
)
157 struct nh_group
*nh_grp
;
159 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
160 return nh_grp
->has_v4
;
165 static inline bool nexthop_is_multipath(const struct nexthop
*nh
)
168 struct nh_group
*nh_grp
;
170 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
171 return nh_grp
->mpath
;
176 struct nexthop
*nexthop_select_path(struct nexthop
*nh
, int hash
);
178 static inline unsigned int nexthop_num_path(const struct nexthop
*nh
)
183 struct nh_group
*nh_grp
;
185 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
194 struct nexthop
*nexthop_mpath_select(const struct nh_group
*nhg
, int nhsel
)
196 /* for_nexthops macros in fib_semantics.c grabs a pointer to
197 * the nexthop before checking nhsel
199 if (nhsel
>= nhg
->num_nh
)
202 return nhg
->nh_entries
[nhsel
].nh
;
206 int nexthop_mpath_fill_node(struct sk_buff
*skb
, struct nexthop
*nh
,
209 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
212 for (i
= 0; i
< nhg
->num_nh
; i
++) {
213 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
214 struct nh_info
*nhi
= rcu_dereference_rtnl(nhe
->nh_info
);
215 struct fib_nh_common
*nhc
= &nhi
->fib_nhc
;
216 int weight
= nhg
->nh_entries
[i
].weight
;
218 if (fib_add_nexthop(skb
, nhc
, weight
, rt_family
) < 0)
225 /* called with rcu lock */
226 static inline bool nexthop_is_blackhole(const struct nexthop
*nh
)
228 const struct nh_info
*nhi
;
231 struct nh_group
*nh_grp
;
233 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
234 if (nh_grp
->num_nh
> 1)
237 nh
= nh_grp
->nh_entries
[0].nh
;
240 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
241 return nhi
->reject_nh
;
244 static inline void nexthop_path_fib_result(struct fib_result
*res
, int hash
)
249 nh
= nexthop_select_path(res
->fi
->nh
, hash
);
250 nhi
= rcu_dereference(nh
->nh_info
);
251 res
->nhc
= &nhi
->fib_nhc
;
254 /* called with rcu read lock or rtnl held */
256 struct fib_nh_common
*nexthop_fib_nhc(struct nexthop
*nh
, int nhsel
)
260 BUILD_BUG_ON(offsetof(struct fib_nh
, nh_common
) != 0);
261 BUILD_BUG_ON(offsetof(struct fib6_nh
, nh_common
) != 0);
264 struct nh_group
*nh_grp
;
266 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
268 nh
= nexthop_mpath_select(nh_grp
, nhsel
);
274 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
275 return &nhi
->fib_nhc
;
278 /* called from fib_table_lookup with rcu_lock */
280 struct fib_nh_common
*nexthop_get_nhc_lookup(const struct nexthop
*nh
,
282 const struct flowi4
*flp
,
288 struct nh_group
*nhg
= rcu_dereference(nh
->nh_grp
);
291 for (i
= 0; i
< nhg
->num_nh
; i
++) {
292 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
294 nhi
= rcu_dereference(nhe
->nh_info
);
295 if (fib_lookup_good_nhc(&nhi
->fib_nhc
, fib_flags
, flp
)) {
297 return &nhi
->fib_nhc
;
301 nhi
= rcu_dereference(nh
->nh_info
);
302 if (fib_lookup_good_nhc(&nhi
->fib_nhc
, fib_flags
, flp
)) {
304 return &nhi
->fib_nhc
;
311 static inline bool nexthop_uses_dev(const struct nexthop
*nh
,
312 const struct net_device
*dev
)
317 struct nh_group
*nhg
= rcu_dereference(nh
->nh_grp
);
320 for (i
= 0; i
< nhg
->num_nh
; i
++) {
321 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
323 nhi
= rcu_dereference(nhe
->nh_info
);
324 if (nhc_l3mdev_matches_dev(&nhi
->fib_nhc
, dev
))
328 nhi
= rcu_dereference(nh
->nh_info
);
329 if (nhc_l3mdev_matches_dev(&nhi
->fib_nhc
, dev
))
336 static inline unsigned int fib_info_num_path(const struct fib_info
*fi
)
338 if (unlikely(fi
->nh
))
339 return nexthop_num_path(fi
->nh
);
344 int fib_check_nexthop(struct nexthop
*nh
, u8 scope
,
345 struct netlink_ext_ack
*extack
);
347 static inline struct fib_nh_common
*fib_info_nhc(struct fib_info
*fi
, int nhsel
)
349 if (unlikely(fi
->nh
))
350 return nexthop_fib_nhc(fi
->nh
, nhsel
);
352 return &fi
->fib_nh
[nhsel
].nh_common
;
355 /* only used when fib_nh is built into fib_info */
356 static inline struct fib_nh
*fib_info_nh(struct fib_info
*fi
, int nhsel
)
360 return &fi
->fib_nh
[nhsel
];
366 int fib6_check_nexthop(struct nexthop
*nh
, struct fib6_config
*cfg
,
367 struct netlink_ext_ack
*extack
);
369 static inline struct fib6_nh
*nexthop_fib6_nh(struct nexthop
*nh
)
374 struct nh_group
*nh_grp
;
376 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
377 nh
= nexthop_mpath_select(nh_grp
, 0);
382 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
383 if (nhi
->family
== AF_INET6
)
384 return &nhi
->fib6_nh
;
389 static inline struct net_device
*fib6_info_nh_dev(struct fib6_info
*f6i
)
391 struct fib6_nh
*fib6_nh
;
393 fib6_nh
= f6i
->nh
? nexthop_fib6_nh(f6i
->nh
) : f6i
->fib6_nh
;
394 return fib6_nh
->fib_nh_dev
;
397 static inline void nexthop_path_fib6_result(struct fib6_result
*res
, int hash
)
399 struct nexthop
*nh
= res
->f6i
->nh
;
402 nh
= nexthop_select_path(nh
, hash
);
404 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
405 if (nhi
->reject_nh
) {
406 res
->fib6_type
= RTN_BLACKHOLE
;
407 res
->fib6_flags
|= RTF_REJECT
;
408 res
->nh
= nexthop_fib6_nh(nh
);
410 res
->nh
= &nhi
->fib6_nh
;
414 int nexthop_for_each_fib6_nh(struct nexthop
*nh
,
415 int (*cb
)(struct fib6_nh
*nh
, void *arg
),
418 static inline int nexthop_get_family(struct nexthop
*nh
)
420 struct nh_info
*nhi
= rcu_dereference_rtnl(nh
->nh_info
);
426 struct fib_nh_common
*nexthop_fdb_nhc(struct nexthop
*nh
)
428 struct nh_info
*nhi
= rcu_dereference_rtnl(nh
->nh_info
);
430 return &nhi
->fib_nhc
;
433 static inline struct fib_nh_common
*nexthop_path_fdb_result(struct nexthop
*nh
,
439 nhp
= nexthop_select_path(nh
, hash
);
442 nhi
= rcu_dereference(nhp
->nh_info
);
443 return &nhi
->fib_nhc
;