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