xref: /linux-6.15/include/net/l3mdev.h (revision ca28b8f2)
1 /*
2  * include/net/l3mdev.h - L3 master device API
3  * Copyright (c) 2015 Cumulus Networks
4  * Copyright (c) 2015 David Ahern <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #ifndef _NET_L3MDEV_H_
12 #define _NET_L3MDEV_H_
13 
14 #include <net/dst.h>
15 #include <net/fib_rules.h>
16 
17 /**
18  * struct l3mdev_ops - l3mdev operations
19  *
20  * @l3mdev_fib_table: Get FIB table id to use for lookups
21  *
22  * @l3mdev_l3_rcv:    Hook in L3 receive path
23  *
24  * @l3mdev_l3_out:    Hook in L3 output path
25  *
26  * @l3mdev_get_rtable: Get cached IPv4 rtable (dst_entry) for device
27  *
28  * @l3mdev_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
29  */
30 
31 struct l3mdev_ops {
32 	u32		(*l3mdev_fib_table)(const struct net_device *dev);
33 	struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
34 					  struct sk_buff *skb, u16 proto);
35 	struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
36 					  struct sock *sk, struct sk_buff *skb,
37 					  u16 proto);
38 
39 	/* IPv4 ops */
40 	struct rtable *	(*l3mdev_get_rtable)(const struct net_device *dev,
41 					     const struct flowi4 *fl4);
42 
43 	/* IPv6 ops */
44 	struct dst_entry * (*l3mdev_link_scope_lookup)(const struct net_device *dev,
45 						 struct flowi6 *fl6);
46 };
47 
48 #ifdef CONFIG_NET_L3_MASTER_DEV
49 
50 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
51 			  struct fib_lookup_arg *arg);
52 
53 void l3mdev_update_flow(struct net *net, struct flowi *fl);
54 
55 int l3mdev_master_ifindex_rcu(const struct net_device *dev);
56 static inline int l3mdev_master_ifindex(struct net_device *dev)
57 {
58 	int ifindex;
59 
60 	rcu_read_lock();
61 	ifindex = l3mdev_master_ifindex_rcu(dev);
62 	rcu_read_unlock();
63 
64 	return ifindex;
65 }
66 
67 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
68 {
69 	struct net_device *dev;
70 	int rc = 0;
71 
72 	if (likely(ifindex)) {
73 		rcu_read_lock();
74 
75 		dev = dev_get_by_index_rcu(net, ifindex);
76 		if (dev)
77 			rc = l3mdev_master_ifindex_rcu(dev);
78 
79 		rcu_read_unlock();
80 	}
81 
82 	return rc;
83 }
84 
85 static inline
86 struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
87 {
88 	/* netdev_master_upper_dev_get_rcu calls
89 	 * list_first_or_null_rcu to walk the upper dev list.
90 	 * list_first_or_null_rcu does not handle a const arg. We aren't
91 	 * making changes, just want the master device from that list so
92 	 * typecast to remove the const
93 	 */
94 	struct net_device *dev = (struct net_device *)_dev;
95 	struct net_device *master;
96 
97 	if (!dev)
98 		return NULL;
99 
100 	if (netif_is_l3_master(dev))
101 		master = dev;
102 	else if (netif_is_l3_slave(dev))
103 		master = netdev_master_upper_dev_get_rcu(dev);
104 	else
105 		master = NULL;
106 
107 	return master;
108 }
109 
110 u32 l3mdev_fib_table_rcu(const struct net_device *dev);
111 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
112 static inline u32 l3mdev_fib_table(const struct net_device *dev)
113 {
114 	u32 tb_id;
115 
116 	rcu_read_lock();
117 	tb_id = l3mdev_fib_table_rcu(dev);
118 	rcu_read_unlock();
119 
120 	return tb_id;
121 }
122 
123 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
124 					       const struct flowi4 *fl4)
125 {
126 	if (netif_is_l3_master(dev) && dev->l3mdev_ops->l3mdev_get_rtable)
127 		return dev->l3mdev_ops->l3mdev_get_rtable(dev, fl4);
128 
129 	return NULL;
130 }
131 
132 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
133 {
134 	struct net_device *dev;
135 	bool rc = false;
136 
137 	if (ifindex == 0)
138 		return false;
139 
140 	rcu_read_lock();
141 
142 	dev = dev_get_by_index_rcu(net, ifindex);
143 	if (dev)
144 		rc = netif_is_l3_master(dev);
145 
146 	rcu_read_unlock();
147 
148 	return rc;
149 }
150 
151 struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
152 
153 static inline
154 struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
155 {
156 	struct net_device *master = NULL;
157 
158 	if (netif_is_l3_slave(skb->dev))
159 		master = netdev_master_upper_dev_get_rcu(skb->dev);
160 	else if (netif_is_l3_master(skb->dev))
161 		master = skb->dev;
162 
163 	if (master && master->l3mdev_ops->l3mdev_l3_rcv)
164 		skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
165 
166 	return skb;
167 }
168 
169 static inline
170 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
171 {
172 	return l3mdev_l3_rcv(skb, AF_INET);
173 }
174 
175 static inline
176 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
177 {
178 	return l3mdev_l3_rcv(skb, AF_INET6);
179 }
180 
181 static inline
182 struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
183 {
184 	struct net_device *dev = skb_dst(skb)->dev;
185 
186 	if (netif_is_l3_slave(dev)) {
187 		struct net_device *master;
188 
189 		master = netdev_master_upper_dev_get_rcu(dev);
190 		if (master && master->l3mdev_ops->l3mdev_l3_out)
191 			skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
192 								skb, proto);
193 	}
194 
195 	return skb;
196 }
197 
198 static inline
199 struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
200 {
201 	return l3mdev_l3_out(sk, skb, AF_INET);
202 }
203 
204 static inline
205 struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
206 {
207 	return l3mdev_l3_out(sk, skb, AF_INET6);
208 }
209 #else
210 
211 static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
212 {
213 	return 0;
214 }
215 static inline int l3mdev_master_ifindex(struct net_device *dev)
216 {
217 	return 0;
218 }
219 
220 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
221 {
222 	return 0;
223 }
224 
225 static inline
226 struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
227 {
228 	return NULL;
229 }
230 
231 static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
232 {
233 	return 0;
234 }
235 static inline u32 l3mdev_fib_table(const struct net_device *dev)
236 {
237 	return 0;
238 }
239 static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
240 {
241 	return 0;
242 }
243 
244 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
245 					       const struct flowi4 *fl4)
246 {
247 	return NULL;
248 }
249 
250 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
251 {
252 	return false;
253 }
254 
255 static inline
256 struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
257 {
258 	return NULL;
259 }
260 
261 static inline
262 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
263 {
264 	return skb;
265 }
266 
267 static inline
268 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
269 {
270 	return skb;
271 }
272 
273 static inline
274 struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
275 {
276 	return skb;
277 }
278 
279 static inline
280 struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
281 {
282 	return skb;
283 }
284 
285 static inline
286 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
287 			  struct fib_lookup_arg *arg)
288 {
289 	return 1;
290 }
291 static inline
292 void l3mdev_update_flow(struct net *net, struct flowi *fl)
293 {
294 }
295 #endif
296 
297 #endif /* _NET_L3MDEV_H_ */
298