Line data Source code
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 : enum nh_notifier_info_type {
118 : NH_NOTIFIER_INFO_TYPE_SINGLE,
119 : NH_NOTIFIER_INFO_TYPE_GRP,
120 : };
121 :
122 : struct nh_notifier_single_info {
123 : struct net_device *dev;
124 : u8 gw_family;
125 : union {
126 : __be32 ipv4;
127 : struct in6_addr ipv6;
128 : };
129 : u8 is_reject:1,
130 : is_fdb:1,
131 : has_encap:1;
132 : };
133 :
134 : struct nh_notifier_grp_entry_info {
135 : u8 weight;
136 : u32 id;
137 : struct nh_notifier_single_info nh;
138 : };
139 :
140 : struct nh_notifier_grp_info {
141 : u16 num_nh;
142 : bool is_fdb;
143 : struct nh_notifier_grp_entry_info nh_entries[];
144 : };
145 :
146 : struct nh_notifier_info {
147 : struct net *net;
148 : struct netlink_ext_ack *extack;
149 : u32 id;
150 : enum nh_notifier_info_type type;
151 : union {
152 : struct nh_notifier_single_info *nh;
153 : struct nh_notifier_grp_info *nh_grp;
154 : };
155 : };
156 :
157 : int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
158 : struct netlink_ext_ack *extack);
159 : int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
160 : void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
161 :
162 : /* caller is holding rcu or rtnl; no reference taken to nexthop */
163 : struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
164 : void nexthop_free_rcu(struct rcu_head *head);
165 :
166 0 : static inline bool nexthop_get(struct nexthop *nh)
167 : {
168 0 : return refcount_inc_not_zero(&nh->refcnt);
169 : }
170 :
171 0 : static inline void nexthop_put(struct nexthop *nh)
172 : {
173 0 : if (refcount_dec_and_test(&nh->refcnt))
174 0 : call_rcu(&nh->rcu, nexthop_free_rcu);
175 0 : }
176 :
177 0 : static inline bool nexthop_cmp(const struct nexthop *nh1,
178 : const struct nexthop *nh2)
179 : {
180 0 : return nh1 == nh2;
181 : }
182 :
183 : static inline bool nexthop_is_fdb(const struct nexthop *nh)
184 : {
185 : if (nh->is_group) {
186 : const struct nh_group *nh_grp;
187 :
188 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
189 : return nh_grp->fdb_nh;
190 : } else {
191 : const struct nh_info *nhi;
192 :
193 : nhi = rcu_dereference_rtnl(nh->nh_info);
194 : return nhi->fdb_nh;
195 : }
196 : }
197 :
198 : static inline bool nexthop_has_v4(const struct nexthop *nh)
199 : {
200 : if (nh->is_group) {
201 : struct nh_group *nh_grp;
202 :
203 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
204 : return nh_grp->has_v4;
205 : }
206 : return false;
207 : }
208 :
209 : static inline bool nexthop_is_multipath(const struct nexthop *nh)
210 : {
211 : if (nh->is_group) {
212 : struct nh_group *nh_grp;
213 :
214 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
215 : return nh_grp->mpath;
216 : }
217 : return false;
218 : }
219 :
220 : struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
221 :
222 0 : static inline unsigned int nexthop_num_path(const struct nexthop *nh)
223 : {
224 0 : unsigned int rc = 1;
225 :
226 0 : if (nh->is_group) {
227 0 : struct nh_group *nh_grp;
228 :
229 0 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
230 0 : if (nh_grp->mpath)
231 0 : rc = nh_grp->num_nh;
232 : }
233 :
234 0 : return rc;
235 : }
236 :
237 : static inline
238 0 : struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
239 : {
240 : /* for_nexthops macros in fib_semantics.c grabs a pointer to
241 : * the nexthop before checking nhsel
242 : */
243 0 : if (nhsel >= nhg->num_nh)
244 : return NULL;
245 :
246 0 : return nhg->nh_entries[nhsel].nh;
247 : }
248 :
249 : static inline
250 : int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
251 : u8 rt_family)
252 : {
253 : struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
254 : int i;
255 :
256 : for (i = 0; i < nhg->num_nh; i++) {
257 : struct nexthop *nhe = nhg->nh_entries[i].nh;
258 : struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
259 : struct fib_nh_common *nhc = &nhi->fib_nhc;
260 : int weight = nhg->nh_entries[i].weight;
261 :
262 : if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
263 : return -EMSGSIZE;
264 : }
265 :
266 : return 0;
267 : }
268 :
269 : /* called with rcu lock */
270 0 : static inline bool nexthop_is_blackhole(const struct nexthop *nh)
271 : {
272 0 : const struct nh_info *nhi;
273 :
274 0 : if (nh->is_group) {
275 0 : struct nh_group *nh_grp;
276 :
277 0 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
278 0 : if (nh_grp->num_nh > 1)
279 : return false;
280 :
281 0 : nh = nh_grp->nh_entries[0].nh;
282 : }
283 :
284 0 : nhi = rcu_dereference_rtnl(nh->nh_info);
285 0 : return nhi->reject_nh;
286 : }
287 :
288 : static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
289 : {
290 : struct nh_info *nhi;
291 : struct nexthop *nh;
292 :
293 : nh = nexthop_select_path(res->fi->nh, hash);
294 : nhi = rcu_dereference(nh->nh_info);
295 : res->nhc = &nhi->fib_nhc;
296 : }
297 :
298 : /* called with rcu read lock or rtnl held */
299 : static inline
300 0 : struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
301 : {
302 0 : struct nh_info *nhi;
303 :
304 0 : BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
305 0 : BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
306 :
307 0 : if (nh->is_group) {
308 0 : struct nh_group *nh_grp;
309 :
310 0 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
311 0 : if (nh_grp->mpath) {
312 0 : nh = nexthop_mpath_select(nh_grp, nhsel);
313 0 : if (!nh)
314 : return NULL;
315 : }
316 : }
317 :
318 0 : nhi = rcu_dereference_rtnl(nh->nh_info);
319 0 : return &nhi->fib_nhc;
320 : }
321 :
322 : /* called from fib_table_lookup with rcu_lock */
323 : static inline
324 0 : struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
325 : int fib_flags,
326 : const struct flowi4 *flp,
327 : int *nhsel)
328 : {
329 0 : struct nh_info *nhi;
330 :
331 0 : if (nh->is_group) {
332 0 : struct nh_group *nhg = rcu_dereference(nh->nh_grp);
333 : int i;
334 :
335 0 : for (i = 0; i < nhg->num_nh; i++) {
336 0 : struct nexthop *nhe = nhg->nh_entries[i].nh;
337 :
338 0 : nhi = rcu_dereference(nhe->nh_info);
339 0 : if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
340 0 : *nhsel = i;
341 0 : return &nhi->fib_nhc;
342 : }
343 : }
344 : } else {
345 0 : nhi = rcu_dereference(nh->nh_info);
346 0 : if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
347 0 : *nhsel = 0;
348 0 : return &nhi->fib_nhc;
349 : }
350 : }
351 :
352 : return NULL;
353 : }
354 :
355 : static inline bool nexthop_uses_dev(const struct nexthop *nh,
356 : const struct net_device *dev)
357 : {
358 : struct nh_info *nhi;
359 :
360 : if (nh->is_group) {
361 : struct nh_group *nhg = rcu_dereference(nh->nh_grp);
362 : int i;
363 :
364 : for (i = 0; i < nhg->num_nh; i++) {
365 : struct nexthop *nhe = nhg->nh_entries[i].nh;
366 :
367 : nhi = rcu_dereference(nhe->nh_info);
368 : if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
369 : return true;
370 : }
371 : } else {
372 : nhi = rcu_dereference(nh->nh_info);
373 : if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
374 : return true;
375 : }
376 :
377 : return false;
378 : }
379 :
380 134 : static inline unsigned int fib_info_num_path(const struct fib_info *fi)
381 : {
382 134 : if (unlikely(fi->nh))
383 0 : return nexthop_num_path(fi->nh);
384 :
385 134 : return fi->fib_nhs;
386 : }
387 :
388 : int fib_check_nexthop(struct nexthop *nh, u8 scope,
389 : struct netlink_ext_ack *extack);
390 :
391 117 : static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
392 : {
393 117 : if (unlikely(fi->nh))
394 0 : return nexthop_fib_nhc(fi->nh, nhsel);
395 :
396 117 : return &fi->fib_nh[nhsel].nh_common;
397 : }
398 :
399 : /* only used when fib_nh is built into fib_info */
400 5 : static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
401 : {
402 0 : WARN_ON(fi->nh);
403 :
404 5 : return &fi->fib_nh[nhsel];
405 : }
406 :
407 : /*
408 : * IPv6 variants
409 : */
410 : int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
411 : struct netlink_ext_ack *extack);
412 :
413 : static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
414 : {
415 : struct nh_info *nhi;
416 :
417 : if (nh->is_group) {
418 : struct nh_group *nh_grp;
419 :
420 : nh_grp = rcu_dereference_rtnl(nh->nh_grp);
421 : nh = nexthop_mpath_select(nh_grp, 0);
422 : if (!nh)
423 : return NULL;
424 : }
425 :
426 : nhi = rcu_dereference_rtnl(nh->nh_info);
427 : if (nhi->family == AF_INET6)
428 : return &nhi->fib6_nh;
429 :
430 : return NULL;
431 : }
432 :
433 : static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
434 : {
435 : struct fib6_nh *fib6_nh;
436 :
437 : fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
438 : return fib6_nh->fib_nh_dev;
439 : }
440 :
441 : static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
442 : {
443 : struct nexthop *nh = res->f6i->nh;
444 : struct nh_info *nhi;
445 :
446 : nh = nexthop_select_path(nh, hash);
447 :
448 : nhi = rcu_dereference_rtnl(nh->nh_info);
449 : if (nhi->reject_nh) {
450 : res->fib6_type = RTN_BLACKHOLE;
451 : res->fib6_flags |= RTF_REJECT;
452 : res->nh = nexthop_fib6_nh(nh);
453 : } else {
454 : res->nh = &nhi->fib6_nh;
455 : }
456 : }
457 :
458 : int nexthop_for_each_fib6_nh(struct nexthop *nh,
459 : int (*cb)(struct fib6_nh *nh, void *arg),
460 : void *arg);
461 :
462 : static inline int nexthop_get_family(struct nexthop *nh)
463 : {
464 : struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
465 :
466 : return nhi->family;
467 : }
468 :
469 : static inline
470 : struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
471 : {
472 : struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
473 :
474 : return &nhi->fib_nhc;
475 : }
476 :
477 : static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
478 : int hash)
479 : {
480 : struct nh_info *nhi;
481 : struct nexthop *nhp;
482 :
483 : nhp = nexthop_select_path(nh, hash);
484 : if (unlikely(!nhp))
485 : return NULL;
486 : nhi = rcu_dereference(nhp->nh_info);
487 : return &nhi->fib_nhc;
488 : }
489 : #endif
|