blob: e4b55b43e907659c66692ba6e52c9214f8bf2cd7 [file] [log] [blame]
David Ahernab84be72019-05-24 14:43:04 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Generic nexthop implementation
4 *
5 * Copyright (c) 2017-19 Cumulus Networks
6 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
7 */
8
9#ifndef __LINUX_NEXTHOP_H
10#define __LINUX_NEXTHOP_H
11
12#include <linux/netdevice.h>
Roopa Prabhu8590ceed2020-05-21 22:26:15 -070013#include <linux/notifier.h>
David Ahernf88d8ea2019-06-03 20:19:52 -070014#include <linux/route.h>
David Ahernab84be72019-05-24 14:43:04 -070015#include <linux/types.h>
16#include <net/ip_fib.h>
David Ahern53010f92019-05-24 14:43:06 -070017#include <net/ip6_fib.h>
David Ahernab84be72019-05-24 14:43:04 -070018#include <net/netlink.h>
19
20#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
21
22struct nexthop;
23
24struct nh_config {
25 u32 nh_id;
26
27 u8 nh_family;
28 u8 nh_protocol;
29 u8 nh_blackhole;
Roopa Prabhu38428d62020-05-21 22:26:13 -070030 u8 nh_fdb;
David Ahernab84be72019-05-24 14:43:04 -070031 u32 nh_flags;
32
33 int nh_ifindex;
34 struct net_device *dev;
35
David Ahern597cfe4f2019-05-24 14:43:05 -070036 union {
37 __be32 ipv4;
David Ahern53010f92019-05-24 14:43:06 -070038 struct in6_addr ipv6;
David Ahern597cfe4f2019-05-24 14:43:05 -070039 } gw;
40
David Ahern430a0492019-05-24 14:43:08 -070041 struct nlattr *nh_grp;
42 u16 nh_grp_type;
43
David Ahernb513bd02019-05-24 14:43:07 -070044 struct nlattr *nh_encap;
45 u16 nh_encap_type;
46
David Ahernab84be72019-05-24 14:43:04 -070047 u32 nlflags;
48 struct nl_info nlinfo;
49};
50
51struct nh_info {
52 struct hlist_node dev_hash; /* entry on netns devhash */
53 struct nexthop *nh_parent;
54
55 u8 family;
56 bool reject_nh;
Roopa Prabhu38428d62020-05-21 22:26:13 -070057 bool fdb_nh;
David Ahernab84be72019-05-24 14:43:04 -070058
59 union {
60 struct fib_nh_common fib_nhc;
David Ahern597cfe4f2019-05-24 14:43:05 -070061 struct fib_nh fib_nh;
David Ahern53010f92019-05-24 14:43:06 -070062 struct fib6_nh fib6_nh;
David Ahernab84be72019-05-24 14:43:04 -070063 };
64};
65
David Ahern430a0492019-05-24 14:43:08 -070066struct nh_grp_entry {
67 struct nexthop *nh;
68 u8 weight;
69 atomic_t upper_bound;
70
71 struct list_head nh_list;
72 struct nexthop *nh_parent; /* nexthop of group with this entry */
73};
74
75struct nh_group {
Nikolay Aleksandrov90f33bf2020-05-26 12:56:15 -060076 struct nh_group *spare; /* spare group for removals */
David Ahern430a0492019-05-24 14:43:08 -070077 u16 num_nh;
78 bool mpath;
79 bool has_v4;
Gustavo A. R. Silva97a888c2020-02-28 18:14:11 -060080 struct nh_grp_entry nh_entries[];
David Ahern430a0492019-05-24 14:43:08 -070081};
82
David Ahernab84be72019-05-24 14:43:04 -070083struct nexthop {
84 struct rb_node rb_node; /* entry on netns rbtree */
David Ahern4c7e8082019-06-03 20:19:51 -070085 struct list_head fi_list; /* v4 entries using nh */
David Ahernf88d8ea2019-06-03 20:19:52 -070086 struct list_head f6i_list; /* v6 entries using nh */
Roopa Prabhu38428d62020-05-21 22:26:13 -070087 struct list_head fdb_list; /* fdb entries using this nh */
David Ahern430a0492019-05-24 14:43:08 -070088 struct list_head grp_list; /* nh group entries using this nh */
David Ahernab84be72019-05-24 14:43:04 -070089 struct net *net;
90
91 u32 id;
92
93 u8 protocol; /* app managing this nh */
94 u8 nh_flags;
David Ahern430a0492019-05-24 14:43:08 -070095 bool is_group;
Roopa Prabhu38428d62020-05-21 22:26:13 -070096 bool is_fdb_nh;
David Ahernab84be72019-05-24 14:43:04 -070097
98 refcount_t refcnt;
99 struct rcu_head rcu;
100
101 union {
102 struct nh_info __rcu *nh_info;
David Ahern430a0492019-05-24 14:43:08 -0700103 struct nh_group __rcu *nh_grp;
David Ahernab84be72019-05-24 14:43:04 -0700104 };
105};
106
Roopa Prabhu8590ceed2020-05-21 22:26:15 -0700107enum nexthop_event_type {
108 NEXTHOP_EVENT_ADD,
109 NEXTHOP_EVENT_DEL
110};
111
112int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
113 enum nexthop_event_type event_type,
114 struct nexthop *nh);
115int register_nexthop_notifier(struct net *net, struct notifier_block *nb);
116int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
117
David Ahernab84be72019-05-24 14:43:04 -0700118/* caller is holding rcu or rtnl; no reference taken to nexthop */
119struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
120void nexthop_free_rcu(struct rcu_head *head);
121
122static inline bool nexthop_get(struct nexthop *nh)
123{
124 return refcount_inc_not_zero(&nh->refcnt);
125}
126
127static inline void nexthop_put(struct nexthop *nh)
128{
129 if (refcount_dec_and_test(&nh->refcnt))
130 call_rcu(&nh->rcu, nexthop_free_rcu);
131}
132
David Ahern4c7e8082019-06-03 20:19:51 -0700133static inline bool nexthop_cmp(const struct nexthop *nh1,
134 const struct nexthop *nh2)
135{
136 return nh1 == nh2;
137}
138
David Ahern430a0492019-05-24 14:43:08 -0700139static inline bool nexthop_is_multipath(const struct nexthop *nh)
140{
141 if (nh->is_group) {
142 struct nh_group *nh_grp;
143
144 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
145 return nh_grp->mpath;
146 }
147 return false;
148}
149
150struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
151
152static inline unsigned int nexthop_num_path(const struct nexthop *nh)
153{
154 unsigned int rc = 1;
155
David Ahern0b5e2e32020-05-26 12:56:16 -0600156 if (nh->is_group) {
David Ahern430a0492019-05-24 14:43:08 -0700157 struct nh_group *nh_grp;
158
159 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
David Ahern0b5e2e32020-05-26 12:56:16 -0600160 if (nh_grp->mpath)
161 rc = nh_grp->num_nh;
David Ahern430a0492019-05-24 14:43:08 -0700162 }
163
164 return rc;
165}
166
167static inline
David Ahern0b5e2e32020-05-26 12:56:16 -0600168struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
David Ahern430a0492019-05-24 14:43:08 -0700169{
David Ahern430a0492019-05-24 14:43:08 -0700170 /* for_nexthops macros in fib_semantics.c grabs a pointer to
171 * the nexthop before checking nhsel
172 */
Dan Carpenter52700412019-06-07 18:31:07 +0300173 if (nhsel >= nhg->num_nh)
David Ahern430a0492019-05-24 14:43:08 -0700174 return NULL;
175
176 return nhg->nh_entries[nhsel].nh;
177}
178
179static inline
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400180int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
181 u8 rt_family)
David Ahern430a0492019-05-24 14:43:08 -0700182{
183 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
184 int i;
185
186 for (i = 0; i < nhg->num_nh; i++) {
187 struct nexthop *nhe = nhg->nh_entries[i].nh;
188 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
189 struct fib_nh_common *nhc = &nhi->fib_nhc;
190 int weight = nhg->nh_entries[i].weight;
191
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400192 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
David Ahern430a0492019-05-24 14:43:08 -0700193 return -EMSGSIZE;
194 }
195
196 return 0;
197}
198
David Ahernab84be72019-05-24 14:43:04 -0700199/* called with rcu lock */
200static inline bool nexthop_is_blackhole(const struct nexthop *nh)
201{
202 const struct nh_info *nhi;
203
David Ahern0b5e2e32020-05-26 12:56:16 -0600204 if (nh->is_group) {
205 struct nh_group *nh_grp;
206
207 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
208 if (nh_grp->num_nh > 1)
David Ahern430a0492019-05-24 14:43:08 -0700209 return false;
David Ahern0b5e2e32020-05-26 12:56:16 -0600210
211 nh = nh_grp->nh_entries[0].nh;
David Ahern430a0492019-05-24 14:43:08 -0700212 }
213
214 nhi = rcu_dereference_rtnl(nh->nh_info);
David Ahernab84be72019-05-24 14:43:04 -0700215 return nhi->reject_nh;
216}
David Ahern5481d732019-06-03 20:19:49 -0700217
David Ahern4c7e8082019-06-03 20:19:51 -0700218static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
219{
220 struct nh_info *nhi;
221 struct nexthop *nh;
222
223 nh = nexthop_select_path(res->fi->nh, hash);
224 nhi = rcu_dereference(nh->nh_info);
225 res->nhc = &nhi->fib_nhc;
226}
227
228/* called with rcu read lock or rtnl held */
229static inline
230struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
231{
232 struct nh_info *nhi;
233
234 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
235 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
236
David Ahern0b5e2e32020-05-26 12:56:16 -0600237 if (nh->is_group) {
238 struct nh_group *nh_grp;
239
240 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
241 if (nh_grp->mpath) {
242 nh = nexthop_mpath_select(nh_grp, nhsel);
243 if (!nh)
244 return NULL;
245 }
David Ahern4c7e8082019-06-03 20:19:51 -0700246 }
247
248 nhi = rcu_dereference_rtnl(nh->nh_info);
249 return &nhi->fib_nhc;
250}
251
David Ahernaf7888a2020-05-26 12:56:17 -0600252/* called from fib_table_lookup with rcu_lock */
253static inline
254struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
255 int fib_flags,
256 const struct flowi4 *flp,
257 int *nhsel)
258{
259 struct nh_info *nhi;
260
261 if (nh->is_group) {
262 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
263 int i;
264
265 for (i = 0; i < nhg->num_nh; i++) {
266 struct nexthop *nhe = nhg->nh_entries[i].nh;
267
268 nhi = rcu_dereference(nhe->nh_info);
269 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
270 *nhsel = i;
271 return &nhi->fib_nhc;
272 }
273 }
274 } else {
275 nhi = rcu_dereference(nh->nh_info);
276 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
277 *nhsel = 0;
278 return &nhi->fib_nhc;
279 }
280 }
281
282 return NULL;
283}
284
David Ahern1fd1c762020-05-26 12:56:18 -0600285static inline bool nexthop_uses_dev(const struct nexthop *nh,
286 const struct net_device *dev)
287{
288 struct nh_info *nhi;
289
290 if (nh->is_group) {
291 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
292 int i;
293
294 for (i = 0; i < nhg->num_nh; i++) {
295 struct nexthop *nhe = nhg->nh_entries[i].nh;
296
297 nhi = rcu_dereference(nhe->nh_info);
298 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
299 return true;
300 }
301 } else {
302 nhi = rcu_dereference(nh->nh_info);
303 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
304 return true;
305 }
306
307 return false;
308}
309
David Ahern5481d732019-06-03 20:19:49 -0700310static inline unsigned int fib_info_num_path(const struct fib_info *fi)
311{
David Ahern4c7e8082019-06-03 20:19:51 -0700312 if (unlikely(fi->nh))
313 return nexthop_num_path(fi->nh);
314
David Ahern5481d732019-06-03 20:19:49 -0700315 return fi->fib_nhs;
316}
317
David Ahern4c7e8082019-06-03 20:19:51 -0700318int fib_check_nexthop(struct nexthop *nh, u8 scope,
319 struct netlink_ext_ack *extack);
320
David Ahern5481d732019-06-03 20:19:49 -0700321static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
322{
David Ahern4c7e8082019-06-03 20:19:51 -0700323 if (unlikely(fi->nh))
324 return nexthop_fib_nhc(fi->nh, nhsel);
325
David Ahern5481d732019-06-03 20:19:49 -0700326 return &fi->fib_nh[nhsel].nh_common;
327}
328
David Ahern4c7e8082019-06-03 20:19:51 -0700329/* only used when fib_nh is built into fib_info */
David Ahern5481d732019-06-03 20:19:49 -0700330static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
331{
David Ahern4c7e8082019-06-03 20:19:51 -0700332 WARN_ON(fi->nh);
333
David Ahern5481d732019-06-03 20:19:49 -0700334 return &fi->fib_nh[nhsel];
335}
David Ahernf88d8ea2019-06-03 20:19:52 -0700336
337/*
338 * IPv6 variants
339 */
340int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
341 struct netlink_ext_ack *extack);
342
343static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
344{
345 struct nh_info *nhi;
346
David Ahern0b5e2e32020-05-26 12:56:16 -0600347 if (nh->is_group) {
348 struct nh_group *nh_grp;
349
350 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
351 nh = nexthop_mpath_select(nh_grp, 0);
David Ahernf88d8ea2019-06-03 20:19:52 -0700352 if (!nh)
353 return NULL;
354 }
355
356 nhi = rcu_dereference_rtnl(nh->nh_info);
357 if (nhi->family == AF_INET6)
358 return &nhi->fib6_nh;
359
360 return NULL;
361}
362
363static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
364{
365 struct fib6_nh *fib6_nh;
366
367 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
368 return fib6_nh->fib_nh_dev;
369}
370
371static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
372{
373 struct nexthop *nh = res->f6i->nh;
374 struct nh_info *nhi;
375
376 nh = nexthop_select_path(nh, hash);
377
378 nhi = rcu_dereference_rtnl(nh->nh_info);
379 if (nhi->reject_nh) {
380 res->fib6_type = RTN_BLACKHOLE;
381 res->fib6_flags |= RTF_REJECT;
382 res->nh = nexthop_fib6_nh(nh);
383 } else {
384 res->nh = &nhi->fib6_nh;
385 }
386}
David Ahernf88c9aa2019-06-08 14:53:22 -0700387
388int nexthop_for_each_fib6_nh(struct nexthop *nh,
389 int (*cb)(struct fib6_nh *nh, void *arg),
390 void *arg);
Roopa Prabhu38428d62020-05-21 22:26:13 -0700391
392static inline int nexthop_get_family(struct nexthop *nh)
393{
394 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
395
396 return nhi->family;
397}
398
399static inline
400struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
401{
402 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
403
404 return &nhi->fib_nhc;
405}
406
407static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
408 int hash)
409{
410 struct nh_info *nhi;
411 struct nexthop *nhp;
412
413 nhp = nexthop_select_path(nh, hash);
414 if (unlikely(!nhp))
415 return NULL;
416 nhi = rcu_dereference(nhp->nh_info);
417 return &nhi->fib_nhc;
418}
David Ahernab84be72019-05-24 14:43:04 -0700419#endif