blob: 4a17b040b5020dd699f7c1b107a48e471118f9ca [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;
David Ahernce9ac052020-06-08 20:54:43 -060079 bool fdb_nh;
David Ahern430a0492019-05-24 14:43:08 -070080 bool has_v4;
Gustavo A. R. Silva97a888c2020-02-28 18:14:11 -060081 struct nh_grp_entry nh_entries[];
David Ahern430a0492019-05-24 14:43:08 -070082};
83
David Ahernab84be72019-05-24 14:43:04 -070084struct nexthop {
85 struct rb_node rb_node; /* entry on netns rbtree */
David Ahern4c7e8082019-06-03 20:19:51 -070086 struct list_head fi_list; /* v4 entries using nh */
David Ahernf88d8ea2019-06-03 20:19:52 -070087 struct list_head f6i_list; /* v6 entries using nh */
Roopa Prabhu38428d62020-05-21 22:26:13 -070088 struct list_head fdb_list; /* fdb entries using this nh */
David Ahern430a0492019-05-24 14:43:08 -070089 struct list_head grp_list; /* nh group entries using this nh */
David Ahernab84be72019-05-24 14:43:04 -070090 struct net *net;
91
92 u32 id;
93
94 u8 protocol; /* app managing this nh */
95 u8 nh_flags;
David Ahern430a0492019-05-24 14:43:08 -070096 bool is_group;
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 {
Roopa Prabhu8590ceed2020-05-21 22:26:15 -0700108 NEXTHOP_EVENT_DEL
109};
110
Ido Schimmel1c9cac62020-11-04 15:30:23 +0200111struct nh_notifier_single_info {
112 struct net_device *dev;
113 u8 gw_family;
114 union {
115 __be32 ipv4;
116 struct in6_addr ipv6;
117 };
118 u8 is_reject:1,
119 is_fdb:1,
120 has_encap:1;
121};
122
123struct nh_notifier_grp_entry_info {
124 u8 weight;
125 u32 id;
126 struct nh_notifier_single_info nh;
127};
128
129struct nh_notifier_grp_info {
130 u16 num_nh;
131 bool is_fdb;
132 struct nh_notifier_grp_entry_info nh_entries[];
133};
134
135struct nh_notifier_info {
136 struct net *net;
137 struct netlink_ext_ack *extack;
138 u32 id;
139 bool is_grp;
140 union {
141 struct nh_notifier_single_info *nh;
142 struct nh_notifier_grp_info *nh_grp;
143 };
144};
145
Roopa Prabhu8590ceed2020-05-21 22:26:15 -0700146int register_nexthop_notifier(struct net *net, struct notifier_block *nb);
147int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
148
David Ahernab84be72019-05-24 14:43:04 -0700149/* caller is holding rcu or rtnl; no reference taken to nexthop */
150struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
151void nexthop_free_rcu(struct rcu_head *head);
152
153static inline bool nexthop_get(struct nexthop *nh)
154{
155 return refcount_inc_not_zero(&nh->refcnt);
156}
157
158static inline void nexthop_put(struct nexthop *nh)
159{
160 if (refcount_dec_and_test(&nh->refcnt))
161 call_rcu(&nh->rcu, nexthop_free_rcu);
162}
163
David Ahern4c7e8082019-06-03 20:19:51 -0700164static inline bool nexthop_cmp(const struct nexthop *nh1,
165 const struct nexthop *nh2)
166{
167 return nh1 == nh2;
168}
169
David Ahernce9ac052020-06-08 20:54:43 -0600170static inline bool nexthop_is_fdb(const struct nexthop *nh)
171{
172 if (nh->is_group) {
173 const struct nh_group *nh_grp;
174
175 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
176 return nh_grp->fdb_nh;
177 } else {
178 const struct nh_info *nhi;
179
180 nhi = rcu_dereference_rtnl(nh->nh_info);
181 return nhi->fdb_nh;
182 }
183}
184
David Ahern50cb8762020-06-09 17:27:28 -0600185static inline bool nexthop_has_v4(const struct nexthop *nh)
186{
187 if (nh->is_group) {
188 struct nh_group *nh_grp;
189
190 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
191 return nh_grp->has_v4;
192 }
193 return false;
194}
195
David Ahern430a0492019-05-24 14:43:08 -0700196static inline bool nexthop_is_multipath(const struct nexthop *nh)
197{
198 if (nh->is_group) {
199 struct nh_group *nh_grp;
200
201 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
202 return nh_grp->mpath;
203 }
204 return false;
205}
206
207struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
208
209static inline unsigned int nexthop_num_path(const struct nexthop *nh)
210{
211 unsigned int rc = 1;
212
David Ahern0b5e2e32020-05-26 12:56:16 -0600213 if (nh->is_group) {
David Ahern430a0492019-05-24 14:43:08 -0700214 struct nh_group *nh_grp;
215
216 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
David Ahern0b5e2e32020-05-26 12:56:16 -0600217 if (nh_grp->mpath)
218 rc = nh_grp->num_nh;
David Ahern430a0492019-05-24 14:43:08 -0700219 }
220
221 return rc;
222}
223
224static inline
David Ahern0b5e2e32020-05-26 12:56:16 -0600225struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
David Ahern430a0492019-05-24 14:43:08 -0700226{
David Ahern430a0492019-05-24 14:43:08 -0700227 /* for_nexthops macros in fib_semantics.c grabs a pointer to
228 * the nexthop before checking nhsel
229 */
Dan Carpenter52700412019-06-07 18:31:07 +0300230 if (nhsel >= nhg->num_nh)
David Ahern430a0492019-05-24 14:43:08 -0700231 return NULL;
232
233 return nhg->nh_entries[nhsel].nh;
234}
235
236static inline
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400237int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
238 u8 rt_family)
David Ahern430a0492019-05-24 14:43:08 -0700239{
240 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
241 int i;
242
243 for (i = 0; i < nhg->num_nh; i++) {
244 struct nexthop *nhe = nhg->nh_entries[i].nh;
245 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
246 struct fib_nh_common *nhc = &nhi->fib_nhc;
247 int weight = nhg->nh_entries[i].weight;
248
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400249 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
David Ahern430a0492019-05-24 14:43:08 -0700250 return -EMSGSIZE;
251 }
252
253 return 0;
254}
255
David Ahernab84be72019-05-24 14:43:04 -0700256/* called with rcu lock */
257static inline bool nexthop_is_blackhole(const struct nexthop *nh)
258{
259 const struct nh_info *nhi;
260
David Ahern0b5e2e32020-05-26 12:56:16 -0600261 if (nh->is_group) {
262 struct nh_group *nh_grp;
263
264 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
265 if (nh_grp->num_nh > 1)
David Ahern430a0492019-05-24 14:43:08 -0700266 return false;
David Ahern0b5e2e32020-05-26 12:56:16 -0600267
268 nh = nh_grp->nh_entries[0].nh;
David Ahern430a0492019-05-24 14:43:08 -0700269 }
270
271 nhi = rcu_dereference_rtnl(nh->nh_info);
David Ahernab84be72019-05-24 14:43:04 -0700272 return nhi->reject_nh;
273}
David Ahern5481d732019-06-03 20:19:49 -0700274
David Ahern4c7e8082019-06-03 20:19:51 -0700275static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
276{
277 struct nh_info *nhi;
278 struct nexthop *nh;
279
280 nh = nexthop_select_path(res->fi->nh, hash);
281 nhi = rcu_dereference(nh->nh_info);
282 res->nhc = &nhi->fib_nhc;
283}
284
285/* called with rcu read lock or rtnl held */
286static inline
287struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
288{
289 struct nh_info *nhi;
290
291 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
292 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
293
David Ahern0b5e2e32020-05-26 12:56:16 -0600294 if (nh->is_group) {
295 struct nh_group *nh_grp;
296
297 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
298 if (nh_grp->mpath) {
299 nh = nexthop_mpath_select(nh_grp, nhsel);
300 if (!nh)
301 return NULL;
302 }
David Ahern4c7e8082019-06-03 20:19:51 -0700303 }
304
305 nhi = rcu_dereference_rtnl(nh->nh_info);
306 return &nhi->fib_nhc;
307}
308
David Ahernaf7888a2020-05-26 12:56:17 -0600309/* called from fib_table_lookup with rcu_lock */
310static inline
311struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
312 int fib_flags,
313 const struct flowi4 *flp,
314 int *nhsel)
315{
316 struct nh_info *nhi;
317
318 if (nh->is_group) {
319 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
320 int i;
321
322 for (i = 0; i < nhg->num_nh; i++) {
323 struct nexthop *nhe = nhg->nh_entries[i].nh;
324
325 nhi = rcu_dereference(nhe->nh_info);
326 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
327 *nhsel = i;
328 return &nhi->fib_nhc;
329 }
330 }
331 } else {
332 nhi = rcu_dereference(nh->nh_info);
333 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
334 *nhsel = 0;
335 return &nhi->fib_nhc;
336 }
337 }
338
339 return NULL;
340}
341
David Ahern1fd1c762020-05-26 12:56:18 -0600342static inline bool nexthop_uses_dev(const struct nexthop *nh,
343 const struct net_device *dev)
344{
345 struct nh_info *nhi;
346
347 if (nh->is_group) {
348 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
349 int i;
350
351 for (i = 0; i < nhg->num_nh; i++) {
352 struct nexthop *nhe = nhg->nh_entries[i].nh;
353
354 nhi = rcu_dereference(nhe->nh_info);
355 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
356 return true;
357 }
358 } else {
359 nhi = rcu_dereference(nh->nh_info);
360 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
361 return true;
362 }
363
364 return false;
365}
366
David Ahern5481d732019-06-03 20:19:49 -0700367static inline unsigned int fib_info_num_path(const struct fib_info *fi)
368{
David Ahern4c7e8082019-06-03 20:19:51 -0700369 if (unlikely(fi->nh))
370 return nexthop_num_path(fi->nh);
371
David Ahern5481d732019-06-03 20:19:49 -0700372 return fi->fib_nhs;
373}
374
David Ahern4c7e8082019-06-03 20:19:51 -0700375int fib_check_nexthop(struct nexthop *nh, u8 scope,
376 struct netlink_ext_ack *extack);
377
David Ahern5481d732019-06-03 20:19:49 -0700378static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
379{
David Ahern4c7e8082019-06-03 20:19:51 -0700380 if (unlikely(fi->nh))
381 return nexthop_fib_nhc(fi->nh, nhsel);
382
David Ahern5481d732019-06-03 20:19:49 -0700383 return &fi->fib_nh[nhsel].nh_common;
384}
385
David Ahern4c7e8082019-06-03 20:19:51 -0700386/* only used when fib_nh is built into fib_info */
David Ahern5481d732019-06-03 20:19:49 -0700387static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
388{
David Ahern4c7e8082019-06-03 20:19:51 -0700389 WARN_ON(fi->nh);
390
David Ahern5481d732019-06-03 20:19:49 -0700391 return &fi->fib_nh[nhsel];
392}
David Ahernf88d8ea2019-06-03 20:19:52 -0700393
394/*
395 * IPv6 variants
396 */
397int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
398 struct netlink_ext_ack *extack);
399
400static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
401{
402 struct nh_info *nhi;
403
David Ahern0b5e2e32020-05-26 12:56:16 -0600404 if (nh->is_group) {
405 struct nh_group *nh_grp;
406
407 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
408 nh = nexthop_mpath_select(nh_grp, 0);
David Ahernf88d8ea2019-06-03 20:19:52 -0700409 if (!nh)
410 return NULL;
411 }
412
413 nhi = rcu_dereference_rtnl(nh->nh_info);
414 if (nhi->family == AF_INET6)
415 return &nhi->fib6_nh;
416
417 return NULL;
418}
419
420static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
421{
422 struct fib6_nh *fib6_nh;
423
424 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
425 return fib6_nh->fib_nh_dev;
426}
427
428static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
429{
430 struct nexthop *nh = res->f6i->nh;
431 struct nh_info *nhi;
432
433 nh = nexthop_select_path(nh, hash);
434
435 nhi = rcu_dereference_rtnl(nh->nh_info);
436 if (nhi->reject_nh) {
437 res->fib6_type = RTN_BLACKHOLE;
438 res->fib6_flags |= RTF_REJECT;
439 res->nh = nexthop_fib6_nh(nh);
440 } else {
441 res->nh = &nhi->fib6_nh;
442 }
443}
David Ahernf88c9aa2019-06-08 14:53:22 -0700444
445int nexthop_for_each_fib6_nh(struct nexthop *nh,
446 int (*cb)(struct fib6_nh *nh, void *arg),
447 void *arg);
Roopa Prabhu38428d62020-05-21 22:26:13 -0700448
449static inline int nexthop_get_family(struct nexthop *nh)
450{
451 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
452
453 return nhi->family;
454}
455
456static inline
457struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
458{
459 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
460
461 return &nhi->fib_nhc;
462}
463
464static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
465 int hash)
466{
467 struct nh_info *nhi;
468 struct nexthop *nhp;
469
470 nhp = nexthop_select_path(nh, hash);
471 if (unlikely(!nhp))
472 return NULL;
473 nhi = rcu_dereference(nhp->nh_info);
474 return &nhi->fib_nhc;
475}
David Ahernab84be72019-05-24 14:43:04 -0700476#endif