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/route.h>
14 #include <linux/types.h>
15 #include <net/ip_fib.h>
16 #include <net/ip6_fib.h>
17 #include <net/netlink.h>
19 #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
32 struct net_device
*dev
;
39 struct nlattr
*nh_grp
;
42 struct nlattr
*nh_encap
;
46 struct nl_info nlinfo
;
50 struct hlist_node dev_hash
; /* entry on netns devhash */
51 struct nexthop
*nh_parent
;
57 struct fib_nh_common fib_nhc
;
59 struct fib6_nh fib6_nh
;
68 struct list_head nh_list
;
69 struct nexthop
*nh_parent
; /* nexthop of group with this entry */
73 struct nh_group
*spare
; /* spare group for removals */
77 struct nh_grp_entry nh_entries
[];
81 struct rb_node rb_node
; /* entry on netns rbtree */
82 struct list_head fi_list
; /* v4 entries using nh */
83 struct list_head f6i_list
; /* v6 entries using nh */
84 struct list_head grp_list
; /* nh group entries using this nh */
89 u8 protocol
; /* app managing this nh */
97 struct nh_info __rcu
*nh_info
;
98 struct nh_group __rcu
*nh_grp
;
102 /* caller is holding rcu or rtnl; no reference taken to nexthop */
103 struct nexthop
*nexthop_find_by_id(struct net
*net
, u32 id
);
104 void nexthop_free_rcu(struct rcu_head
*head
);
106 static inline bool nexthop_get(struct nexthop
*nh
)
108 return refcount_inc_not_zero(&nh
->refcnt
);
111 static inline void nexthop_put(struct nexthop
*nh
)
113 if (refcount_dec_and_test(&nh
->refcnt
))
114 call_rcu(&nh
->rcu
, nexthop_free_rcu
);
117 static inline bool nexthop_cmp(const struct nexthop
*nh1
,
118 const struct nexthop
*nh2
)
123 static inline bool nexthop_is_multipath(const struct nexthop
*nh
)
126 struct nh_group
*nh_grp
;
128 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
129 return nh_grp
->mpath
;
134 struct nexthop
*nexthop_select_path(struct nexthop
*nh
, int hash
);
136 static inline unsigned int nexthop_num_path(const struct nexthop
*nh
)
141 struct nh_group
*nh_grp
;
143 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
152 struct nexthop
*nexthop_mpath_select(const struct nh_group
*nhg
, int nhsel
)
154 /* for_nexthops macros in fib_semantics.c grabs a pointer to
155 * the nexthop before checking nhsel
157 if (nhsel
>= nhg
->num_nh
)
160 return nhg
->nh_entries
[nhsel
].nh
;
164 int nexthop_mpath_fill_node(struct sk_buff
*skb
, struct nexthop
*nh
,
167 struct nh_group
*nhg
= rtnl_dereference(nh
->nh_grp
);
170 for (i
= 0; i
< nhg
->num_nh
; i
++) {
171 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
172 struct nh_info
*nhi
= rcu_dereference_rtnl(nhe
->nh_info
);
173 struct fib_nh_common
*nhc
= &nhi
->fib_nhc
;
174 int weight
= nhg
->nh_entries
[i
].weight
;
176 if (fib_add_nexthop(skb
, nhc
, weight
, rt_family
) < 0)
183 /* called with rcu lock */
184 static inline bool nexthop_is_blackhole(const struct nexthop
*nh
)
186 const struct nh_info
*nhi
;
189 struct nh_group
*nh_grp
;
191 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
192 if (nh_grp
->num_nh
> 1)
195 nh
= nh_grp
->nh_entries
[0].nh
;
198 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
199 return nhi
->reject_nh
;
202 static inline void nexthop_path_fib_result(struct fib_result
*res
, int hash
)
207 nh
= nexthop_select_path(res
->fi
->nh
, hash
);
208 nhi
= rcu_dereference(nh
->nh_info
);
209 res
->nhc
= &nhi
->fib_nhc
;
212 /* called with rcu read lock or rtnl held */
214 struct fib_nh_common
*nexthop_fib_nhc(struct nexthop
*nh
, int nhsel
)
218 BUILD_BUG_ON(offsetof(struct fib_nh
, nh_common
) != 0);
219 BUILD_BUG_ON(offsetof(struct fib6_nh
, nh_common
) != 0);
222 struct nh_group
*nh_grp
;
224 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
226 nh
= nexthop_mpath_select(nh_grp
, nhsel
);
232 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
233 return &nhi
->fib_nhc
;
236 /* called from fib_table_lookup with rcu_lock */
238 struct fib_nh_common
*nexthop_get_nhc_lookup(const struct nexthop
*nh
,
240 const struct flowi4
*flp
,
246 struct nh_group
*nhg
= rcu_dereference(nh
->nh_grp
);
249 for (i
= 0; i
< nhg
->num_nh
; i
++) {
250 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
252 nhi
= rcu_dereference(nhe
->nh_info
);
253 if (fib_lookup_good_nhc(&nhi
->fib_nhc
, fib_flags
, flp
)) {
255 return &nhi
->fib_nhc
;
259 nhi
= rcu_dereference(nh
->nh_info
);
260 if (fib_lookup_good_nhc(&nhi
->fib_nhc
, fib_flags
, flp
)) {
262 return &nhi
->fib_nhc
;
269 static inline bool nexthop_uses_dev(const struct nexthop
*nh
,
270 const struct net_device
*dev
)
275 struct nh_group
*nhg
= rcu_dereference(nh
->nh_grp
);
278 for (i
= 0; i
< nhg
->num_nh
; i
++) {
279 struct nexthop
*nhe
= nhg
->nh_entries
[i
].nh
;
281 nhi
= rcu_dereference(nhe
->nh_info
);
282 if (nhc_l3mdev_matches_dev(&nhi
->fib_nhc
, dev
))
286 nhi
= rcu_dereference(nh
->nh_info
);
287 if (nhc_l3mdev_matches_dev(&nhi
->fib_nhc
, dev
))
294 static inline unsigned int fib_info_num_path(const struct fib_info
*fi
)
296 if (unlikely(fi
->nh
))
297 return nexthop_num_path(fi
->nh
);
302 int fib_check_nexthop(struct nexthop
*nh
, u8 scope
,
303 struct netlink_ext_ack
*extack
);
305 static inline struct fib_nh_common
*fib_info_nhc(struct fib_info
*fi
, int nhsel
)
307 if (unlikely(fi
->nh
))
308 return nexthop_fib_nhc(fi
->nh
, nhsel
);
310 return &fi
->fib_nh
[nhsel
].nh_common
;
313 /* only used when fib_nh is built into fib_info */
314 static inline struct fib_nh
*fib_info_nh(struct fib_info
*fi
, int nhsel
)
318 return &fi
->fib_nh
[nhsel
];
324 int fib6_check_nexthop(struct nexthop
*nh
, struct fib6_config
*cfg
,
325 struct netlink_ext_ack
*extack
);
327 static inline struct fib6_nh
*nexthop_fib6_nh(struct nexthop
*nh
)
332 struct nh_group
*nh_grp
;
334 nh_grp
= rcu_dereference_rtnl(nh
->nh_grp
);
335 nh
= nexthop_mpath_select(nh_grp
, 0);
340 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
341 if (nhi
->family
== AF_INET6
)
342 return &nhi
->fib6_nh
;
347 static inline struct net_device
*fib6_info_nh_dev(struct fib6_info
*f6i
)
349 struct fib6_nh
*fib6_nh
;
351 fib6_nh
= f6i
->nh
? nexthop_fib6_nh(f6i
->nh
) : f6i
->fib6_nh
;
352 return fib6_nh
->fib_nh_dev
;
355 static inline void nexthop_path_fib6_result(struct fib6_result
*res
, int hash
)
357 struct nexthop
*nh
= res
->f6i
->nh
;
360 nh
= nexthop_select_path(nh
, hash
);
362 nhi
= rcu_dereference_rtnl(nh
->nh_info
);
363 if (nhi
->reject_nh
) {
364 res
->fib6_type
= RTN_BLACKHOLE
;
365 res
->fib6_flags
|= RTF_REJECT
;
366 res
->nh
= nexthop_fib6_nh(nh
);
368 res
->nh
= &nhi
->fib6_nh
;
372 int nexthop_for_each_fib6_nh(struct nexthop
*nh
,
373 int (*cb
)(struct fib6_nh
*nh
, void *arg
),