xref: /openbmc/linux/include/net/nexthop.h (revision 547840bd)
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 	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 {
76 	struct nh_group		*spare; /* spare group for removals */
77 	u16			num_nh;
78 	bool			mpath;
79 	bool			has_v4;
80 	struct nh_grp_entry	nh_entries[];
81 };
82 
83 struct nexthop {
84 	struct rb_node		rb_node;    /* entry on netns rbtree */
85 	struct list_head	fi_list;    /* v4 entries using nh */
86 	struct list_head	f6i_list;   /* v6 entries using nh */
87 	struct list_head        fdb_list;   /* fdb entries using this nh */
88 	struct list_head	grp_list;   /* nh group entries using this nh */
89 	struct net		*net;
90 
91 	u32			id;
92 
93 	u8			protocol;   /* app managing this nh */
94 	u8			nh_flags;
95 	bool			is_group;
96 	bool			is_fdb_nh;
97 
98 	refcount_t		refcnt;
99 	struct rcu_head		rcu;
100 
101 	union {
102 		struct nh_info	__rcu *nh_info;
103 		struct nh_group __rcu *nh_grp;
104 	};
105 };
106 
107 enum nexthop_event_type {
108 	NEXTHOP_EVENT_ADD,
109 	NEXTHOP_EVENT_DEL
110 };
111 
112 int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
113 			  enum nexthop_event_type event_type,
114 			  struct nexthop *nh);
115 int register_nexthop_notifier(struct net *net, struct notifier_block *nb);
116 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
117 
118 /* caller is holding rcu or rtnl; no reference taken to nexthop */
119 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
120 void nexthop_free_rcu(struct rcu_head *head);
121 
122 static inline bool nexthop_get(struct nexthop *nh)
123 {
124 	return refcount_inc_not_zero(&nh->refcnt);
125 }
126 
127 static inline void nexthop_put(struct nexthop *nh)
128 {
129 	if (refcount_dec_and_test(&nh->refcnt))
130 		call_rcu(&nh->rcu, nexthop_free_rcu);
131 }
132 
133 static inline bool nexthop_cmp(const struct nexthop *nh1,
134 			       const struct nexthop *nh2)
135 {
136 	return nh1 == nh2;
137 }
138 
139 static inline bool nexthop_is_multipath(const struct nexthop *nh)
140 {
141 	if (nh->is_group) {
142 		struct nh_group *nh_grp;
143 
144 		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
145 		return nh_grp->mpath;
146 	}
147 	return false;
148 }
149 
150 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
151 
152 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
153 {
154 	unsigned int rc = 1;
155 
156 	if (nh->is_group) {
157 		struct nh_group *nh_grp;
158 
159 		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
160 		if (nh_grp->mpath)
161 			rc = nh_grp->num_nh;
162 	}
163 
164 	return rc;
165 }
166 
167 static inline
168 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
169 {
170 	/* for_nexthops macros in fib_semantics.c grabs a pointer to
171 	 * the nexthop before checking nhsel
172 	 */
173 	if (nhsel >= nhg->num_nh)
174 		return NULL;
175 
176 	return nhg->nh_entries[nhsel].nh;
177 }
178 
179 static inline
180 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
181 			    u8 rt_family)
182 {
183 	struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
184 	int i;
185 
186 	for (i = 0; i < nhg->num_nh; i++) {
187 		struct nexthop *nhe = nhg->nh_entries[i].nh;
188 		struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
189 		struct fib_nh_common *nhc = &nhi->fib_nhc;
190 		int weight = nhg->nh_entries[i].weight;
191 
192 		if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
193 			return -EMSGSIZE;
194 	}
195 
196 	return 0;
197 }
198 
199 /* called with rcu lock */
200 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
201 {
202 	const struct nh_info *nhi;
203 
204 	if (nh->is_group) {
205 		struct nh_group *nh_grp;
206 
207 		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
208 		if (nh_grp->num_nh > 1)
209 			return false;
210 
211 		nh = nh_grp->nh_entries[0].nh;
212 	}
213 
214 	nhi = rcu_dereference_rtnl(nh->nh_info);
215 	return nhi->reject_nh;
216 }
217 
218 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
219 {
220 	struct nh_info *nhi;
221 	struct nexthop *nh;
222 
223 	nh = nexthop_select_path(res->fi->nh, hash);
224 	nhi = rcu_dereference(nh->nh_info);
225 	res->nhc = &nhi->fib_nhc;
226 }
227 
228 /* called with rcu read lock or rtnl held */
229 static inline
230 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
231 {
232 	struct nh_info *nhi;
233 
234 	BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
235 	BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
236 
237 	if (nh->is_group) {
238 		struct nh_group *nh_grp;
239 
240 		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
241 		if (nh_grp->mpath) {
242 			nh = nexthop_mpath_select(nh_grp, nhsel);
243 			if (!nh)
244 				return NULL;
245 		}
246 	}
247 
248 	nhi = rcu_dereference_rtnl(nh->nh_info);
249 	return &nhi->fib_nhc;
250 }
251 
252 /* called from fib_table_lookup with rcu_lock */
253 static inline
254 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
255 					     int fib_flags,
256 					     const struct flowi4 *flp,
257 					     int *nhsel)
258 {
259 	struct nh_info *nhi;
260 
261 	if (nh->is_group) {
262 		struct nh_group *nhg = rcu_dereference(nh->nh_grp);
263 		int i;
264 
265 		for (i = 0; i < nhg->num_nh; i++) {
266 			struct nexthop *nhe = nhg->nh_entries[i].nh;
267 
268 			nhi = rcu_dereference(nhe->nh_info);
269 			if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
270 				*nhsel = i;
271 				return &nhi->fib_nhc;
272 			}
273 		}
274 	} else {
275 		nhi = rcu_dereference(nh->nh_info);
276 		if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
277 			*nhsel = 0;
278 			return &nhi->fib_nhc;
279 		}
280 	}
281 
282 	return NULL;
283 }
284 
285 static inline bool nexthop_uses_dev(const struct nexthop *nh,
286 				    const struct net_device *dev)
287 {
288 	struct nh_info *nhi;
289 
290 	if (nh->is_group) {
291 		struct nh_group *nhg = rcu_dereference(nh->nh_grp);
292 		int i;
293 
294 		for (i = 0; i < nhg->num_nh; i++) {
295 			struct nexthop *nhe = nhg->nh_entries[i].nh;
296 
297 			nhi = rcu_dereference(nhe->nh_info);
298 			if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
299 				return true;
300 		}
301 	} else {
302 		nhi = rcu_dereference(nh->nh_info);
303 		if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
304 			return true;
305 	}
306 
307 	return false;
308 }
309 
310 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
311 {
312 	if (unlikely(fi->nh))
313 		return nexthop_num_path(fi->nh);
314 
315 	return fi->fib_nhs;
316 }
317 
318 int fib_check_nexthop(struct nexthop *nh, u8 scope,
319 		      struct netlink_ext_ack *extack);
320 
321 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
322 {
323 	if (unlikely(fi->nh))
324 		return nexthop_fib_nhc(fi->nh, nhsel);
325 
326 	return &fi->fib_nh[nhsel].nh_common;
327 }
328 
329 /* only used when fib_nh is built into fib_info */
330 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
331 {
332 	WARN_ON(fi->nh);
333 
334 	return &fi->fib_nh[nhsel];
335 }
336 
337 /*
338  * IPv6 variants
339  */
340 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
341 		       struct netlink_ext_ack *extack);
342 
343 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
344 {
345 	struct nh_info *nhi;
346 
347 	if (nh->is_group) {
348 		struct nh_group *nh_grp;
349 
350 		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
351 		nh = nexthop_mpath_select(nh_grp, 0);
352 		if (!nh)
353 			return NULL;
354 	}
355 
356 	nhi = rcu_dereference_rtnl(nh->nh_info);
357 	if (nhi->family == AF_INET6)
358 		return &nhi->fib6_nh;
359 
360 	return NULL;
361 }
362 
363 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
364 {
365 	struct fib6_nh *fib6_nh;
366 
367 	fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
368 	return fib6_nh->fib_nh_dev;
369 }
370 
371 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
372 {
373 	struct nexthop *nh = res->f6i->nh;
374 	struct nh_info *nhi;
375 
376 	nh = nexthop_select_path(nh, hash);
377 
378 	nhi = rcu_dereference_rtnl(nh->nh_info);
379 	if (nhi->reject_nh) {
380 		res->fib6_type = RTN_BLACKHOLE;
381 		res->fib6_flags |= RTF_REJECT;
382 		res->nh = nexthop_fib6_nh(nh);
383 	} else {
384 		res->nh = &nhi->fib6_nh;
385 	}
386 }
387 
388 int nexthop_for_each_fib6_nh(struct nexthop *nh,
389 			     int (*cb)(struct fib6_nh *nh, void *arg),
390 			     void *arg);
391 
392 static inline int nexthop_get_family(struct nexthop *nh)
393 {
394 	struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
395 
396 	return nhi->family;
397 }
398 
399 static inline
400 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
401 {
402 	struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
403 
404 	return &nhi->fib_nhc;
405 }
406 
407 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
408 							    int hash)
409 {
410 	struct nh_info *nhi;
411 	struct nexthop *nhp;
412 
413 	nhp = nexthop_select_path(nh, hash);
414 	if (unlikely(!nhp))
415 		return NULL;
416 	nhi = rcu_dereference(nhp->nh_info);
417 	return &nhi->fib_nhc;
418 }
419 #endif
420