d0e245b0635d92f43efa7e8a8db248fb9a3cfcef
[linux-2.6-microblaze.git] / include / net / nexthop.h
1 /* 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>
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>
19
20 #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
21
22 struct nexthop;
23
24 struct nh_config {
25         u32             nh_id;
26
27         u8              nh_family;
28         u8              nh_protocol;
29         u8              nh_blackhole;
30         u8              nh_fdb;
31         u32             nh_flags;
32
33         int             nh_ifindex;
34         struct net_device *dev;
35
36         union {
37                 __be32          ipv4;
38                 struct in6_addr ipv6;
39         } gw;
40
41         struct nlattr   *nh_grp;
42         u16             nh_grp_type;
43
44         struct nlattr   *nh_encap;
45         u16             nh_encap_type;
46
47         u32             nlflags;
48         struct nl_info  nlinfo;
49 };
50
51 struct 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;
57         bool                    fdb_nh;
58
59         union {
60                 struct fib_nh_common    fib_nhc;
61                 struct fib_nh           fib_nh;
62                 struct fib6_nh          fib6_nh;
63         };
64 };
65
66 struct nh_grp_entry {
67         struct nexthop  *nh;
68         u8              weight;
69
70         union {
71                 struct {
72                         atomic_t        upper_bound;
73                 } mpath;
74         };
75
76         struct list_head nh_list;
77         struct nexthop  *nh_parent;  /* nexthop of group with this entry */
78 };
79
80 struct nh_group {
81         struct nh_group         *spare; /* spare group for removals */
82         u16                     num_nh;
83         bool                    mpath;
84         bool                    fdb_nh;
85         bool                    has_v4;
86         struct nh_grp_entry     nh_entries[];
87 };
88
89 struct nexthop {
90         struct rb_node          rb_node;    /* entry on netns rbtree */
91         struct list_head        fi_list;    /* v4 entries using nh */
92         struct list_head        f6i_list;   /* v6 entries using nh */
93         struct list_head        fdb_list;   /* fdb entries using this nh */
94         struct list_head        grp_list;   /* nh group entries using this nh */
95         struct net              *net;
96
97         u32                     id;
98
99         u8                      protocol;   /* app managing this nh */
100         u8                      nh_flags;
101         bool                    is_group;
102
103         refcount_t              refcnt;
104         struct rcu_head         rcu;
105
106         union {
107                 struct nh_info  __rcu *nh_info;
108                 struct nh_group __rcu *nh_grp;
109         };
110 };
111
112 enum nexthop_event_type {
113         NEXTHOP_EVENT_DEL,
114         NEXTHOP_EVENT_REPLACE,
115 };
116
117 struct nh_notifier_single_info {
118         struct net_device *dev;
119         u8 gw_family;
120         union {
121                 __be32 ipv4;
122                 struct in6_addr ipv6;
123         };
124         u8 is_reject:1,
125            is_fdb:1,
126            has_encap:1;
127 };
128
129 struct nh_notifier_grp_entry_info {
130         u8 weight;
131         u32 id;
132         struct nh_notifier_single_info nh;
133 };
134
135 struct nh_notifier_grp_info {
136         u16 num_nh;
137         bool is_fdb;
138         struct nh_notifier_grp_entry_info nh_entries[];
139 };
140
141 struct nh_notifier_info {
142         struct net *net;
143         struct netlink_ext_ack *extack;
144         u32 id;
145         bool is_grp;
146         union {
147                 struct nh_notifier_single_info *nh;
148                 struct nh_notifier_grp_info *nh_grp;
149         };
150 };
151
152 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
153                               struct netlink_ext_ack *extack);
154 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
155 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
156
157 /* caller is holding rcu or rtnl; no reference taken to nexthop */
158 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
159 void nexthop_free_rcu(struct rcu_head *head);
160
161 static inline bool nexthop_get(struct nexthop *nh)
162 {
163         return refcount_inc_not_zero(&nh->refcnt);
164 }
165
166 static inline void nexthop_put(struct nexthop *nh)
167 {
168         if (refcount_dec_and_test(&nh->refcnt))
169                 call_rcu(&nh->rcu, nexthop_free_rcu);
170 }
171
172 static inline bool nexthop_cmp(const struct nexthop *nh1,
173                                const struct nexthop *nh2)
174 {
175         return nh1 == nh2;
176 }
177
178 static inline bool nexthop_is_fdb(const struct nexthop *nh)
179 {
180         if (nh->is_group) {
181                 const struct nh_group *nh_grp;
182
183                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
184                 return nh_grp->fdb_nh;
185         } else {
186                 const struct nh_info *nhi;
187
188                 nhi = rcu_dereference_rtnl(nh->nh_info);
189                 return nhi->fdb_nh;
190         }
191 }
192
193 static inline bool nexthop_has_v4(const struct nexthop *nh)
194 {
195         if (nh->is_group) {
196                 struct nh_group *nh_grp;
197
198                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
199                 return nh_grp->has_v4;
200         }
201         return false;
202 }
203
204 static inline bool nexthop_is_multipath(const struct nexthop *nh)
205 {
206         if (nh->is_group) {
207                 struct nh_group *nh_grp;
208
209                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
210                 return nh_grp->mpath;
211         }
212         return false;
213 }
214
215 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
216
217 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
218 {
219         unsigned int rc = 1;
220
221         if (nh->is_group) {
222                 struct nh_group *nh_grp;
223
224                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
225                 if (nh_grp->mpath)
226                         rc = nh_grp->num_nh;
227         }
228
229         return rc;
230 }
231
232 static inline
233 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
234 {
235         /* for_nexthops macros in fib_semantics.c grabs a pointer to
236          * the nexthop before checking nhsel
237          */
238         if (nhsel >= nhg->num_nh)
239                 return NULL;
240
241         return nhg->nh_entries[nhsel].nh;
242 }
243
244 static inline
245 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
246                             u8 rt_family)
247 {
248         struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
249         int i;
250
251         for (i = 0; i < nhg->num_nh; i++) {
252                 struct nexthop *nhe = nhg->nh_entries[i].nh;
253                 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
254                 struct fib_nh_common *nhc = &nhi->fib_nhc;
255                 int weight = nhg->nh_entries[i].weight;
256
257                 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
258                         return -EMSGSIZE;
259         }
260
261         return 0;
262 }
263
264 /* called with rcu lock */
265 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
266 {
267         const struct nh_info *nhi;
268
269         if (nh->is_group) {
270                 struct nh_group *nh_grp;
271
272                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
273                 if (nh_grp->num_nh > 1)
274                         return false;
275
276                 nh = nh_grp->nh_entries[0].nh;
277         }
278
279         nhi = rcu_dereference_rtnl(nh->nh_info);
280         return nhi->reject_nh;
281 }
282
283 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
284 {
285         struct nh_info *nhi;
286         struct nexthop *nh;
287
288         nh = nexthop_select_path(res->fi->nh, hash);
289         nhi = rcu_dereference(nh->nh_info);
290         res->nhc = &nhi->fib_nhc;
291 }
292
293 /* called with rcu read lock or rtnl held */
294 static inline
295 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
296 {
297         struct nh_info *nhi;
298
299         BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
300         BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
301
302         if (nh->is_group) {
303                 struct nh_group *nh_grp;
304
305                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
306                 if (nh_grp->mpath) {
307                         nh = nexthop_mpath_select(nh_grp, nhsel);
308                         if (!nh)
309                                 return NULL;
310                 }
311         }
312
313         nhi = rcu_dereference_rtnl(nh->nh_info);
314         return &nhi->fib_nhc;
315 }
316
317 /* called from fib_table_lookup with rcu_lock */
318 static inline
319 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
320                                              int fib_flags,
321                                              const struct flowi4 *flp,
322                                              int *nhsel)
323 {
324         struct nh_info *nhi;
325
326         if (nh->is_group) {
327                 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
328                 int i;
329
330                 for (i = 0; i < nhg->num_nh; i++) {
331                         struct nexthop *nhe = nhg->nh_entries[i].nh;
332
333                         nhi = rcu_dereference(nhe->nh_info);
334                         if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
335                                 *nhsel = i;
336                                 return &nhi->fib_nhc;
337                         }
338                 }
339         } else {
340                 nhi = rcu_dereference(nh->nh_info);
341                 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
342                         *nhsel = 0;
343                         return &nhi->fib_nhc;
344                 }
345         }
346
347         return NULL;
348 }
349
350 static inline bool nexthop_uses_dev(const struct nexthop *nh,
351                                     const struct net_device *dev)
352 {
353         struct nh_info *nhi;
354
355         if (nh->is_group) {
356                 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
357                 int i;
358
359                 for (i = 0; i < nhg->num_nh; i++) {
360                         struct nexthop *nhe = nhg->nh_entries[i].nh;
361
362                         nhi = rcu_dereference(nhe->nh_info);
363                         if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
364                                 return true;
365                 }
366         } else {
367                 nhi = rcu_dereference(nh->nh_info);
368                 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
369                         return true;
370         }
371
372         return false;
373 }
374
375 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
376 {
377         if (unlikely(fi->nh))
378                 return nexthop_num_path(fi->nh);
379
380         return fi->fib_nhs;
381 }
382
383 int fib_check_nexthop(struct nexthop *nh, u8 scope,
384                       struct netlink_ext_ack *extack);
385
386 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
387 {
388         if (unlikely(fi->nh))
389                 return nexthop_fib_nhc(fi->nh, nhsel);
390
391         return &fi->fib_nh[nhsel].nh_common;
392 }
393
394 /* only used when fib_nh is built into fib_info */
395 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
396 {
397         WARN_ON(fi->nh);
398
399         return &fi->fib_nh[nhsel];
400 }
401
402 /*
403  * IPv6 variants
404  */
405 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
406                        struct netlink_ext_ack *extack);
407
408 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
409 {
410         struct nh_info *nhi;
411
412         if (nh->is_group) {
413                 struct nh_group *nh_grp;
414
415                 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
416                 nh = nexthop_mpath_select(nh_grp, 0);
417                 if (!nh)
418                         return NULL;
419         }
420
421         nhi = rcu_dereference_rtnl(nh->nh_info);
422         if (nhi->family == AF_INET6)
423                 return &nhi->fib6_nh;
424
425         return NULL;
426 }
427
428 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
429 {
430         struct fib6_nh *fib6_nh;
431
432         fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
433         return fib6_nh->fib_nh_dev;
434 }
435
436 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
437 {
438         struct nexthop *nh = res->f6i->nh;
439         struct nh_info *nhi;
440
441         nh = nexthop_select_path(nh, hash);
442
443         nhi = rcu_dereference_rtnl(nh->nh_info);
444         if (nhi->reject_nh) {
445                 res->fib6_type = RTN_BLACKHOLE;
446                 res->fib6_flags |= RTF_REJECT;
447                 res->nh = nexthop_fib6_nh(nh);
448         } else {
449                 res->nh = &nhi->fib6_nh;
450         }
451 }
452
453 int nexthop_for_each_fib6_nh(struct nexthop *nh,
454                              int (*cb)(struct fib6_nh *nh, void *arg),
455                              void *arg);
456
457 static inline int nexthop_get_family(struct nexthop *nh)
458 {
459         struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
460
461         return nhi->family;
462 }
463
464 static inline
465 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
466 {
467         struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
468
469         return &nhi->fib_nhc;
470 }
471
472 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
473                                                             int hash)
474 {
475         struct nh_info *nhi;
476         struct nexthop *nhp;
477
478         nhp = nexthop_select_path(nh, hash);
479         if (unlikely(!nhp))
480                 return NULL;
481         nhi = rcu_dereference(nhp->nh_info);
482         return &nhi->fib_nhc;
483 }
484 #endif