blob: 9414ae46fc1c0f41bc803ecf4a9650cc78f55880 [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>
David Ahernf88d8ea2019-06-03 20:19:52 -070013#include <linux/route.h>
David Ahernab84be72019-05-24 14:43:04 -070014#include <linux/types.h>
15#include <net/ip_fib.h>
David Ahern53010f92019-05-24 14:43:06 -070016#include <net/ip6_fib.h>
David Ahernab84be72019-05-24 14:43:04 -070017#include <net/netlink.h>
18
19#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
20
21struct nexthop;
22
23struct nh_config {
24 u32 nh_id;
25
26 u8 nh_family;
27 u8 nh_protocol;
28 u8 nh_blackhole;
29 u32 nh_flags;
30
31 int nh_ifindex;
32 struct net_device *dev;
33
David Ahern597cfe4f2019-05-24 14:43:05 -070034 union {
35 __be32 ipv4;
David Ahern53010f92019-05-24 14:43:06 -070036 struct in6_addr ipv6;
David Ahern597cfe4f2019-05-24 14:43:05 -070037 } gw;
38
David Ahern430a0492019-05-24 14:43:08 -070039 struct nlattr *nh_grp;
40 u16 nh_grp_type;
41
David Ahernb513bd02019-05-24 14:43:07 -070042 struct nlattr *nh_encap;
43 u16 nh_encap_type;
44
David Ahernab84be72019-05-24 14:43:04 -070045 u32 nlflags;
46 struct nl_info nlinfo;
47};
48
49struct nh_info {
50 struct hlist_node dev_hash; /* entry on netns devhash */
51 struct nexthop *nh_parent;
52
53 u8 family;
54 bool reject_nh;
55
56 union {
57 struct fib_nh_common fib_nhc;
David Ahern597cfe4f2019-05-24 14:43:05 -070058 struct fib_nh fib_nh;
David Ahern53010f92019-05-24 14:43:06 -070059 struct fib6_nh fib6_nh;
David Ahernab84be72019-05-24 14:43:04 -070060 };
61};
62
David Ahern430a0492019-05-24 14:43:08 -070063struct nh_grp_entry {
64 struct nexthop *nh;
65 u8 weight;
66 atomic_t upper_bound;
67
68 struct list_head nh_list;
69 struct nexthop *nh_parent; /* nexthop of group with this entry */
70};
71
72struct nh_group {
Nikolay Aleksandrov90f33bf2020-05-26 12:56:15 -060073 struct nh_group *spare; /* spare group for removals */
David Ahern430a0492019-05-24 14:43:08 -070074 u16 num_nh;
75 bool mpath;
76 bool has_v4;
Gustavo A. R. Silva97a888c2020-02-28 18:14:11 -060077 struct nh_grp_entry nh_entries[];
David Ahern430a0492019-05-24 14:43:08 -070078};
79
David Ahernab84be72019-05-24 14:43:04 -070080struct nexthop {
81 struct rb_node rb_node; /* entry on netns rbtree */
David Ahern4c7e8082019-06-03 20:19:51 -070082 struct list_head fi_list; /* v4 entries using nh */
David Ahernf88d8ea2019-06-03 20:19:52 -070083 struct list_head f6i_list; /* v6 entries using nh */
David Ahern430a0492019-05-24 14:43:08 -070084 struct list_head grp_list; /* nh group entries using this nh */
David Ahernab84be72019-05-24 14:43:04 -070085 struct net *net;
86
87 u32 id;
88
89 u8 protocol; /* app managing this nh */
90 u8 nh_flags;
David Ahern430a0492019-05-24 14:43:08 -070091 bool is_group;
David Ahernab84be72019-05-24 14:43:04 -070092
93 refcount_t refcnt;
94 struct rcu_head rcu;
95
96 union {
97 struct nh_info __rcu *nh_info;
David Ahern430a0492019-05-24 14:43:08 -070098 struct nh_group __rcu *nh_grp;
David Ahernab84be72019-05-24 14:43:04 -070099 };
100};
101
102/* caller is holding rcu or rtnl; no reference taken to nexthop */
103struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
104void nexthop_free_rcu(struct rcu_head *head);
105
106static inline bool nexthop_get(struct nexthop *nh)
107{
108 return refcount_inc_not_zero(&nh->refcnt);
109}
110
111static inline void nexthop_put(struct nexthop *nh)
112{
113 if (refcount_dec_and_test(&nh->refcnt))
114 call_rcu(&nh->rcu, nexthop_free_rcu);
115}
116
David Ahern4c7e8082019-06-03 20:19:51 -0700117static inline bool nexthop_cmp(const struct nexthop *nh1,
118 const struct nexthop *nh2)
119{
120 return nh1 == nh2;
121}
122
David Ahern430a0492019-05-24 14:43:08 -0700123static inline bool nexthop_is_multipath(const struct nexthop *nh)
124{
125 if (nh->is_group) {
126 struct nh_group *nh_grp;
127
128 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
129 return nh_grp->mpath;
130 }
131 return false;
132}
133
134struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
135
136static inline unsigned int nexthop_num_path(const struct nexthop *nh)
137{
138 unsigned int rc = 1;
139
David Ahern0b5e2e32020-05-26 12:56:16 -0600140 if (nh->is_group) {
David Ahern430a0492019-05-24 14:43:08 -0700141 struct nh_group *nh_grp;
142
143 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
David Ahern0b5e2e32020-05-26 12:56:16 -0600144 if (nh_grp->mpath)
145 rc = nh_grp->num_nh;
David Ahern430a0492019-05-24 14:43:08 -0700146 }
147
148 return rc;
149}
150
151static inline
David Ahern0b5e2e32020-05-26 12:56:16 -0600152struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
David Ahern430a0492019-05-24 14:43:08 -0700153{
David Ahern430a0492019-05-24 14:43:08 -0700154 /* for_nexthops macros in fib_semantics.c grabs a pointer to
155 * the nexthop before checking nhsel
156 */
Dan Carpenter52700412019-06-07 18:31:07 +0300157 if (nhsel >= nhg->num_nh)
David Ahern430a0492019-05-24 14:43:08 -0700158 return NULL;
159
160 return nhg->nh_entries[nhsel].nh;
161}
162
163static inline
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400164int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
165 u8 rt_family)
David Ahern430a0492019-05-24 14:43:08 -0700166{
167 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
168 int i;
169
170 for (i = 0; i < nhg->num_nh; i++) {
171 struct nexthop *nhe = nhg->nh_entries[i].nh;
172 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
173 struct fib_nh_common *nhc = &nhi->fib_nhc;
174 int weight = nhg->nh_entries[i].weight;
175
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400176 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
David Ahern430a0492019-05-24 14:43:08 -0700177 return -EMSGSIZE;
178 }
179
180 return 0;
181}
182
David Ahernab84be72019-05-24 14:43:04 -0700183/* called with rcu lock */
184static inline bool nexthop_is_blackhole(const struct nexthop *nh)
185{
186 const struct nh_info *nhi;
187
David Ahern0b5e2e32020-05-26 12:56:16 -0600188 if (nh->is_group) {
189 struct nh_group *nh_grp;
190
191 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
192 if (nh_grp->num_nh > 1)
David Ahern430a0492019-05-24 14:43:08 -0700193 return false;
David Ahern0b5e2e32020-05-26 12:56:16 -0600194
195 nh = nh_grp->nh_entries[0].nh;
David Ahern430a0492019-05-24 14:43:08 -0700196 }
197
198 nhi = rcu_dereference_rtnl(nh->nh_info);
David Ahernab84be72019-05-24 14:43:04 -0700199 return nhi->reject_nh;
200}
David Ahern5481d732019-06-03 20:19:49 -0700201
David Ahern4c7e8082019-06-03 20:19:51 -0700202static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
203{
204 struct nh_info *nhi;
205 struct nexthop *nh;
206
207 nh = nexthop_select_path(res->fi->nh, hash);
208 nhi = rcu_dereference(nh->nh_info);
209 res->nhc = &nhi->fib_nhc;
210}
211
212/* called with rcu read lock or rtnl held */
213static inline
214struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
215{
216 struct nh_info *nhi;
217
218 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
219 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
220
David Ahern0b5e2e32020-05-26 12:56:16 -0600221 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 nh = nexthop_mpath_select(nh_grp, nhsel);
227 if (!nh)
228 return NULL;
229 }
David Ahern4c7e8082019-06-03 20:19:51 -0700230 }
231
232 nhi = rcu_dereference_rtnl(nh->nh_info);
233 return &nhi->fib_nhc;
234}
235
David Ahernaf7888a2020-05-26 12:56:17 -0600236/* called from fib_table_lookup with rcu_lock */
237static inline
238struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
239 int fib_flags,
240 const struct flowi4 *flp,
241 int *nhsel)
242{
243 struct nh_info *nhi;
244
245 if (nh->is_group) {
246 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
247 int i;
248
249 for (i = 0; i < nhg->num_nh; i++) {
250 struct nexthop *nhe = nhg->nh_entries[i].nh;
251
252 nhi = rcu_dereference(nhe->nh_info);
253 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
254 *nhsel = i;
255 return &nhi->fib_nhc;
256 }
257 }
258 } else {
259 nhi = rcu_dereference(nh->nh_info);
260 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
261 *nhsel = 0;
262 return &nhi->fib_nhc;
263 }
264 }
265
266 return NULL;
267}
268
David Ahern5481d732019-06-03 20:19:49 -0700269static inline unsigned int fib_info_num_path(const struct fib_info *fi)
270{
David Ahern4c7e8082019-06-03 20:19:51 -0700271 if (unlikely(fi->nh))
272 return nexthop_num_path(fi->nh);
273
David Ahern5481d732019-06-03 20:19:49 -0700274 return fi->fib_nhs;
275}
276
David Ahern4c7e8082019-06-03 20:19:51 -0700277int fib_check_nexthop(struct nexthop *nh, u8 scope,
278 struct netlink_ext_ack *extack);
279
David Ahern5481d732019-06-03 20:19:49 -0700280static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
281{
David Ahern4c7e8082019-06-03 20:19:51 -0700282 if (unlikely(fi->nh))
283 return nexthop_fib_nhc(fi->nh, nhsel);
284
David Ahern5481d732019-06-03 20:19:49 -0700285 return &fi->fib_nh[nhsel].nh_common;
286}
287
David Ahern4c7e8082019-06-03 20:19:51 -0700288/* only used when fib_nh is built into fib_info */
David Ahern5481d732019-06-03 20:19:49 -0700289static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
290{
David Ahern4c7e8082019-06-03 20:19:51 -0700291 WARN_ON(fi->nh);
292
David Ahern5481d732019-06-03 20:19:49 -0700293 return &fi->fib_nh[nhsel];
294}
David Ahernf88d8ea2019-06-03 20:19:52 -0700295
296/*
297 * IPv6 variants
298 */
299int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
300 struct netlink_ext_ack *extack);
301
302static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
303{
304 struct nh_info *nhi;
305
David Ahern0b5e2e32020-05-26 12:56:16 -0600306 if (nh->is_group) {
307 struct nh_group *nh_grp;
308
309 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
310 nh = nexthop_mpath_select(nh_grp, 0);
David Ahernf88d8ea2019-06-03 20:19:52 -0700311 if (!nh)
312 return NULL;
313 }
314
315 nhi = rcu_dereference_rtnl(nh->nh_info);
316 if (nhi->family == AF_INET6)
317 return &nhi->fib6_nh;
318
319 return NULL;
320}
321
322static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
323{
324 struct fib6_nh *fib6_nh;
325
326 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
327 return fib6_nh->fib_nh_dev;
328}
329
330static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
331{
332 struct nexthop *nh = res->f6i->nh;
333 struct nh_info *nhi;
334
335 nh = nexthop_select_path(nh, hash);
336
337 nhi = rcu_dereference_rtnl(nh->nh_info);
338 if (nhi->reject_nh) {
339 res->fib6_type = RTN_BLACKHOLE;
340 res->fib6_flags |= RTF_REJECT;
341 res->nh = nexthop_fib6_nh(nh);
342 } else {
343 res->nh = &nhi->fib6_nh;
344 }
345}
David Ahernf88c9aa2019-06-08 14:53:22 -0700346
347int nexthop_for_each_fib6_nh(struct nexthop *nh,
348 int (*cb)(struct fib6_nh *nh, void *arg),
349 void *arg);
David Ahernab84be72019-05-24 14:43:04 -0700350#endif