blob: d929c98931ade02acfa3ce3aca598064b1db599c [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;
Roopa Prabhu38428d62020-05-21 22:26:13 -070029 u8 nh_fdb;
David Ahernab84be72019-05-24 14:43:04 -070030 u32 nh_flags;
31
32 int nh_ifindex;
33 struct net_device *dev;
34
David Ahern597cfe4f2019-05-24 14:43:05 -070035 union {
36 __be32 ipv4;
David Ahern53010f92019-05-24 14:43:06 -070037 struct in6_addr ipv6;
David Ahern597cfe4f2019-05-24 14:43:05 -070038 } gw;
39
David Ahern430a0492019-05-24 14:43:08 -070040 struct nlattr *nh_grp;
41 u16 nh_grp_type;
42
David Ahernb513bd02019-05-24 14:43:07 -070043 struct nlattr *nh_encap;
44 u16 nh_encap_type;
45
David Ahernab84be72019-05-24 14:43:04 -070046 u32 nlflags;
47 struct nl_info nlinfo;
48};
49
50struct nh_info {
51 struct hlist_node dev_hash; /* entry on netns devhash */
52 struct nexthop *nh_parent;
53
54 u8 family;
55 bool reject_nh;
Roopa Prabhu38428d62020-05-21 22:26:13 -070056 bool fdb_nh;
David Ahernab84be72019-05-24 14:43:04 -070057
58 union {
59 struct fib_nh_common fib_nhc;
David Ahern597cfe4f2019-05-24 14:43:05 -070060 struct fib_nh fib_nh;
David Ahern53010f92019-05-24 14:43:06 -070061 struct fib6_nh fib6_nh;
David Ahernab84be72019-05-24 14:43:04 -070062 };
63};
64
David Ahern430a0492019-05-24 14:43:08 -070065struct nh_grp_entry {
66 struct nexthop *nh;
67 u8 weight;
68 atomic_t upper_bound;
69
70 struct list_head nh_list;
71 struct nexthop *nh_parent; /* nexthop of group with this entry */
72};
73
74struct nh_group {
75 u16 num_nh;
76 bool mpath;
77 bool has_v4;
Gustavo A. R. Silva97a888c2020-02-28 18:14:11 -060078 struct nh_grp_entry nh_entries[];
David Ahern430a0492019-05-24 14:43:08 -070079};
80
David Ahernab84be72019-05-24 14:43:04 -070081struct nexthop {
82 struct rb_node rb_node; /* entry on netns rbtree */
David Ahern4c7e8082019-06-03 20:19:51 -070083 struct list_head fi_list; /* v4 entries using nh */
David Ahernf88d8ea2019-06-03 20:19:52 -070084 struct list_head f6i_list; /* v6 entries using nh */
Roopa Prabhu38428d62020-05-21 22:26:13 -070085 struct list_head fdb_list; /* fdb entries using this nh */
David Ahern430a0492019-05-24 14:43:08 -070086 struct list_head grp_list; /* nh group entries using this nh */
David Ahernab84be72019-05-24 14:43:04 -070087 struct net *net;
88
89 u32 id;
90
91 u8 protocol; /* app managing this nh */
92 u8 nh_flags;
David Ahern430a0492019-05-24 14:43:08 -070093 bool is_group;
Roopa Prabhu38428d62020-05-21 22:26:13 -070094 bool is_fdb_nh;
David Ahernab84be72019-05-24 14:43:04 -070095
96 refcount_t refcnt;
97 struct rcu_head rcu;
98
99 union {
100 struct nh_info __rcu *nh_info;
David Ahern430a0492019-05-24 14:43:08 -0700101 struct nh_group __rcu *nh_grp;
David Ahernab84be72019-05-24 14:43:04 -0700102 };
103};
104
105/* caller is holding rcu or rtnl; no reference taken to nexthop */
106struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
107void nexthop_free_rcu(struct rcu_head *head);
108
109static inline bool nexthop_get(struct nexthop *nh)
110{
111 return refcount_inc_not_zero(&nh->refcnt);
112}
113
114static inline void nexthop_put(struct nexthop *nh)
115{
116 if (refcount_dec_and_test(&nh->refcnt))
117 call_rcu(&nh->rcu, nexthop_free_rcu);
118}
119
David Ahern4c7e8082019-06-03 20:19:51 -0700120static inline bool nexthop_cmp(const struct nexthop *nh1,
121 const struct nexthop *nh2)
122{
123 return nh1 == nh2;
124}
125
David Ahern430a0492019-05-24 14:43:08 -0700126static inline bool nexthop_is_multipath(const struct nexthop *nh)
127{
128 if (nh->is_group) {
129 struct nh_group *nh_grp;
130
131 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
132 return nh_grp->mpath;
133 }
134 return false;
135}
136
137struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
138
139static inline unsigned int nexthop_num_path(const struct nexthop *nh)
140{
141 unsigned int rc = 1;
142
143 if (nexthop_is_multipath(nh)) {
144 struct nh_group *nh_grp;
145
146 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
147 rc = nh_grp->num_nh;
David Ahern430a0492019-05-24 14:43:08 -0700148 }
149
150 return rc;
151}
152
153static inline
154struct nexthop *nexthop_mpath_select(const struct nexthop *nh, int nhsel)
155{
156 const struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
157
158 /* for_nexthops macros in fib_semantics.c grabs a pointer to
159 * the nexthop before checking nhsel
160 */
Dan Carpenter52700412019-06-07 18:31:07 +0300161 if (nhsel >= nhg->num_nh)
David Ahern430a0492019-05-24 14:43:08 -0700162 return NULL;
163
164 return nhg->nh_entries[nhsel].nh;
165}
166
167static inline
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400168int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
169 u8 rt_family)
David Ahern430a0492019-05-24 14:43:08 -0700170{
171 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
172 int i;
173
174 for (i = 0; i < nhg->num_nh; i++) {
175 struct nexthop *nhe = nhg->nh_entries[i].nh;
176 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
177 struct fib_nh_common *nhc = &nhi->fib_nhc;
178 int weight = nhg->nh_entries[i].weight;
179
Donald Sharp7bdf4de2019-09-04 10:11:58 -0400180 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
David Ahern430a0492019-05-24 14:43:08 -0700181 return -EMSGSIZE;
182 }
183
184 return 0;
185}
186
David Ahernab84be72019-05-24 14:43:04 -0700187/* called with rcu lock */
188static inline bool nexthop_is_blackhole(const struct nexthop *nh)
189{
190 const struct nh_info *nhi;
191
David Ahern430a0492019-05-24 14:43:08 -0700192 if (nexthop_is_multipath(nh)) {
193 if (nexthop_num_path(nh) > 1)
194 return false;
195 nh = nexthop_mpath_select(nh, 0);
196 if (!nh)
197 return false;
198 }
199
200 nhi = rcu_dereference_rtnl(nh->nh_info);
David Ahernab84be72019-05-24 14:43:04 -0700201 return nhi->reject_nh;
202}
David Ahern5481d732019-06-03 20:19:49 -0700203
David Ahern4c7e8082019-06-03 20:19:51 -0700204static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
205{
206 struct nh_info *nhi;
207 struct nexthop *nh;
208
209 nh = nexthop_select_path(res->fi->nh, hash);
210 nhi = rcu_dereference(nh->nh_info);
211 res->nhc = &nhi->fib_nhc;
212}
213
214/* called with rcu read lock or rtnl held */
215static inline
216struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
217{
218 struct nh_info *nhi;
219
220 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
221 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
222
223 if (nexthop_is_multipath(nh)) {
224 nh = nexthop_mpath_select(nh, nhsel);
225 if (!nh)
226 return NULL;
227 }
228
229 nhi = rcu_dereference_rtnl(nh->nh_info);
230 return &nhi->fib_nhc;
231}
232
David Ahern5481d732019-06-03 20:19:49 -0700233static inline unsigned int fib_info_num_path(const struct fib_info *fi)
234{
David Ahern4c7e8082019-06-03 20:19:51 -0700235 if (unlikely(fi->nh))
236 return nexthop_num_path(fi->nh);
237
David Ahern5481d732019-06-03 20:19:49 -0700238 return fi->fib_nhs;
239}
240
David Ahern4c7e8082019-06-03 20:19:51 -0700241int fib_check_nexthop(struct nexthop *nh, u8 scope,
242 struct netlink_ext_ack *extack);
243
David Ahern5481d732019-06-03 20:19:49 -0700244static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
245{
David Ahern4c7e8082019-06-03 20:19:51 -0700246 if (unlikely(fi->nh))
247 return nexthop_fib_nhc(fi->nh, nhsel);
248
David Ahern5481d732019-06-03 20:19:49 -0700249 return &fi->fib_nh[nhsel].nh_common;
250}
251
David Ahern4c7e8082019-06-03 20:19:51 -0700252/* only used when fib_nh is built into fib_info */
David Ahern5481d732019-06-03 20:19:49 -0700253static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
254{
David Ahern4c7e8082019-06-03 20:19:51 -0700255 WARN_ON(fi->nh);
256
David Ahern5481d732019-06-03 20:19:49 -0700257 return &fi->fib_nh[nhsel];
258}
David Ahernf88d8ea2019-06-03 20:19:52 -0700259
260/*
261 * IPv6 variants
262 */
263int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
264 struct netlink_ext_ack *extack);
265
266static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
267{
268 struct nh_info *nhi;
269
270 if (nexthop_is_multipath(nh)) {
271 nh = nexthop_mpath_select(nh, 0);
272 if (!nh)
273 return NULL;
274 }
275
276 nhi = rcu_dereference_rtnl(nh->nh_info);
277 if (nhi->family == AF_INET6)
278 return &nhi->fib6_nh;
279
280 return NULL;
281}
282
283static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
284{
285 struct fib6_nh *fib6_nh;
286
287 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
288 return fib6_nh->fib_nh_dev;
289}
290
291static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
292{
293 struct nexthop *nh = res->f6i->nh;
294 struct nh_info *nhi;
295
296 nh = nexthop_select_path(nh, hash);
297
298 nhi = rcu_dereference_rtnl(nh->nh_info);
299 if (nhi->reject_nh) {
300 res->fib6_type = RTN_BLACKHOLE;
301 res->fib6_flags |= RTF_REJECT;
302 res->nh = nexthop_fib6_nh(nh);
303 } else {
304 res->nh = &nhi->fib6_nh;
305 }
306}
David Ahernf88c9aa2019-06-08 14:53:22 -0700307
308int nexthop_for_each_fib6_nh(struct nexthop *nh,
309 int (*cb)(struct fib6_nh *nh, void *arg),
310 void *arg);
Roopa Prabhu38428d62020-05-21 22:26:13 -0700311
312static inline int nexthop_get_family(struct nexthop *nh)
313{
314 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
315
316 return nhi->family;
317}
318
319static inline
320struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
321{
322 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
323
324 return &nhi->fib_nhc;
325}
326
327static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
328 int hash)
329{
330 struct nh_info *nhi;
331 struct nexthop *nhp;
332
333 nhp = nexthop_select_path(nh, hash);
334 if (unlikely(!nhp))
335 return NULL;
336 nhi = rcu_dereference(nhp->nh_info);
337 return &nhi->fib_nhc;
338}
David Ahernab84be72019-05-24 14:43:04 -0700339#endif