Loading...
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * include/net/l3mdev.h - L3 master device API
4 * Copyright (c) 2015 Cumulus Networks
5 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
6 */
7#ifndef _NET_L3MDEV_H_
8#define _NET_L3MDEV_H_
9
10#include <net/dst.h>
11#include <net/fib_rules.h>
12
13enum l3mdev_type {
14 L3MDEV_TYPE_UNSPEC,
15 L3MDEV_TYPE_VRF,
16 __L3MDEV_TYPE_MAX
17};
18
19#define L3MDEV_TYPE_MAX (__L3MDEV_TYPE_MAX - 1)
20
21typedef int (*lookup_by_table_id_t)(struct net *net, u32 table_d);
22
23/**
24 * struct l3mdev_ops - l3mdev operations
25 *
26 * @l3mdev_fib_table: Get FIB table id to use for lookups
27 *
28 * @l3mdev_l3_rcv: Hook in L3 receive path
29 *
30 * @l3mdev_l3_out: Hook in L3 output path
31 *
32 * @l3mdev_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
33 */
34
35struct l3mdev_ops {
36 u32 (*l3mdev_fib_table)(const struct net_device *dev);
37 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
38 struct sk_buff *skb, u16 proto);
39 struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
40 struct sock *sk, struct sk_buff *skb,
41 u16 proto);
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
50int l3mdev_table_lookup_register(enum l3mdev_type l3type,
51 lookup_by_table_id_t fn);
52
53void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
54 lookup_by_table_id_t fn);
55
56int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
57 u32 table_id);
58
59int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
60 struct fib_lookup_arg *arg);
61
62void l3mdev_update_flow(struct net *net, struct flowi *fl);
63
64int l3mdev_master_ifindex_rcu(const struct net_device *dev);
65static inline int l3mdev_master_ifindex(struct net_device *dev)
66{
67 int ifindex;
68
69 rcu_read_lock();
70 ifindex = l3mdev_master_ifindex_rcu(dev);
71 rcu_read_unlock();
72
73 return ifindex;
74}
75
76static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
77{
78 struct net_device *dev;
79 int rc = 0;
80
81 if (ifindex) {
82 rcu_read_lock();
83
84 dev = dev_get_by_index_rcu(net, ifindex);
85 if (dev)
86 rc = l3mdev_master_ifindex_rcu(dev);
87
88 rcu_read_unlock();
89 }
90
91 return rc;
92}
93
94static inline
95struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
96{
97 /* netdev_master_upper_dev_get_rcu calls
98 * list_first_or_null_rcu to walk the upper dev list.
99 * list_first_or_null_rcu does not handle a const arg. We aren't
100 * making changes, just want the master device from that list so
101 * typecast to remove the const
102 */
103 struct net_device *dev = (struct net_device *)_dev;
104 struct net_device *master;
105
106 if (!dev)
107 return NULL;
108
109 if (netif_is_l3_master(dev))
110 master = dev;
111 else if (netif_is_l3_slave(dev))
112 master = netdev_master_upper_dev_get_rcu(dev);
113 else
114 master = NULL;
115
116 return master;
117}
118
119int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex);
120static inline
121int l3mdev_master_upper_ifindex_by_index(struct net *net, int ifindex)
122{
123 rcu_read_lock();
124 ifindex = l3mdev_master_upper_ifindex_by_index_rcu(net, ifindex);
125 rcu_read_unlock();
126
127 return ifindex;
128}
129
130u32 l3mdev_fib_table_rcu(const struct net_device *dev);
131u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
132static inline u32 l3mdev_fib_table(const struct net_device *dev)
133{
134 u32 tb_id;
135
136 rcu_read_lock();
137 tb_id = l3mdev_fib_table_rcu(dev);
138 rcu_read_unlock();
139
140 return tb_id;
141}
142
143static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
144{
145 struct net_device *dev;
146 bool rc = false;
147
148 if (ifindex == 0)
149 return false;
150
151 rcu_read_lock();
152
153 dev = dev_get_by_index_rcu(net, ifindex);
154 if (dev)
155 rc = netif_is_l3_master(dev);
156
157 rcu_read_unlock();
158
159 return rc;
160}
161
162struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
163
164static inline
165struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
166{
167 struct net_device *master = NULL;
168
169 if (netif_is_l3_slave(skb->dev))
170 master = netdev_master_upper_dev_get_rcu(skb->dev);
171 else if (netif_is_l3_master(skb->dev) ||
172 netif_has_l3_rx_handler(skb->dev))
173 master = skb->dev;
174
175 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
176 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
177
178 return skb;
179}
180
181static inline
182struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
183{
184 return l3mdev_l3_rcv(skb, AF_INET);
185}
186
187static inline
188struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
189{
190 return l3mdev_l3_rcv(skb, AF_INET6);
191}
192
193static inline
194struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
195{
196 struct net_device *dev = skb_dst(skb)->dev;
197
198 if (netif_is_l3_slave(dev)) {
199 struct net_device *master;
200
201 rcu_read_lock();
202 master = netdev_master_upper_dev_get_rcu(dev);
203 if (master && master->l3mdev_ops->l3mdev_l3_out)
204 skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
205 skb, proto);
206 rcu_read_unlock();
207 }
208
209 return skb;
210}
211
212static inline
213struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
214{
215 return l3mdev_l3_out(sk, skb, AF_INET);
216}
217
218static inline
219struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
220{
221 return l3mdev_l3_out(sk, skb, AF_INET6);
222}
223#else
224
225static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
226{
227 return 0;
228}
229static inline int l3mdev_master_ifindex(struct net_device *dev)
230{
231 return 0;
232}
233
234static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
235{
236 return 0;
237}
238
239static inline
240int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex)
241{
242 return 0;
243}
244static inline
245int l3mdev_master_upper_ifindex_by_index(struct net *net, int ifindex)
246{
247 return 0;
248}
249
250static inline
251struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
252{
253 return NULL;
254}
255
256static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
257{
258 return 0;
259}
260static inline u32 l3mdev_fib_table(const struct net_device *dev)
261{
262 return 0;
263}
264static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
265{
266 return 0;
267}
268
269static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
270{
271 return false;
272}
273
274static inline
275struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
276{
277 return NULL;
278}
279
280static inline
281struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
282{
283 return skb;
284}
285
286static inline
287struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
288{
289 return skb;
290}
291
292static inline
293struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
294{
295 return skb;
296}
297
298static inline
299struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
300{
301 return skb;
302}
303
304static inline
305int l3mdev_table_lookup_register(enum l3mdev_type l3type,
306 lookup_by_table_id_t fn)
307{
308 return -EOPNOTSUPP;
309}
310
311static inline
312void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
313 lookup_by_table_id_t fn)
314{
315}
316
317static inline
318int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
319 u32 table_id)
320{
321 return -ENODEV;
322}
323
324static inline
325int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
326 struct fib_lookup_arg *arg)
327{
328 return 1;
329}
330static inline
331void l3mdev_update_flow(struct net *net, struct flowi *fl)
332{
333}
334#endif
335
336#endif /* _NET_L3MDEV_H_ */
1/*
2 * include/net/l3mdev.h - L3 master device API
3 * Copyright (c) 2015 Cumulus Networks
4 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
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_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
27 */
28
29struct l3mdev_ops {
30 u32 (*l3mdev_fib_table)(const struct net_device *dev);
31 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
32 struct sk_buff *skb, u16 proto);
33 struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
34 struct sock *sk, struct sk_buff *skb,
35 u16 proto);
36
37 /* IPv6 ops */
38 struct dst_entry * (*l3mdev_link_scope_lookup)(const struct net_device *dev,
39 struct flowi6 *fl6);
40};
41
42#ifdef CONFIG_NET_L3_MASTER_DEV
43
44int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
45 struct fib_lookup_arg *arg);
46
47void l3mdev_update_flow(struct net *net, struct flowi *fl);
48
49int l3mdev_master_ifindex_rcu(const struct net_device *dev);
50static inline int l3mdev_master_ifindex(struct net_device *dev)
51{
52 int ifindex;
53
54 rcu_read_lock();
55 ifindex = l3mdev_master_ifindex_rcu(dev);
56 rcu_read_unlock();
57
58 return ifindex;
59}
60
61static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
62{
63 struct net_device *dev;
64 int rc = 0;
65
66 if (likely(ifindex)) {
67 rcu_read_lock();
68
69 dev = dev_get_by_index_rcu(net, ifindex);
70 if (dev)
71 rc = l3mdev_master_ifindex_rcu(dev);
72
73 rcu_read_unlock();
74 }
75
76 return rc;
77}
78
79static inline
80struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
81{
82 /* netdev_master_upper_dev_get_rcu calls
83 * list_first_or_null_rcu to walk the upper dev list.
84 * list_first_or_null_rcu does not handle a const arg. We aren't
85 * making changes, just want the master device from that list so
86 * typecast to remove the const
87 */
88 struct net_device *dev = (struct net_device *)_dev;
89 struct net_device *master;
90
91 if (!dev)
92 return NULL;
93
94 if (netif_is_l3_master(dev))
95 master = dev;
96 else if (netif_is_l3_slave(dev))
97 master = netdev_master_upper_dev_get_rcu(dev);
98 else
99 master = NULL;
100
101 return master;
102}
103
104u32 l3mdev_fib_table_rcu(const struct net_device *dev);
105u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
106static inline u32 l3mdev_fib_table(const struct net_device *dev)
107{
108 u32 tb_id;
109
110 rcu_read_lock();
111 tb_id = l3mdev_fib_table_rcu(dev);
112 rcu_read_unlock();
113
114 return tb_id;
115}
116
117static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
118{
119 struct net_device *dev;
120 bool rc = false;
121
122 if (ifindex == 0)
123 return false;
124
125 rcu_read_lock();
126
127 dev = dev_get_by_index_rcu(net, ifindex);
128 if (dev)
129 rc = netif_is_l3_master(dev);
130
131 rcu_read_unlock();
132
133 return rc;
134}
135
136struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
137
138static inline
139struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
140{
141 struct net_device *master = NULL;
142
143 if (netif_is_l3_slave(skb->dev))
144 master = netdev_master_upper_dev_get_rcu(skb->dev);
145 else if (netif_is_l3_master(skb->dev))
146 master = skb->dev;
147
148 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
149 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
150
151 return skb;
152}
153
154static inline
155struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
156{
157 return l3mdev_l3_rcv(skb, AF_INET);
158}
159
160static inline
161struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
162{
163 return l3mdev_l3_rcv(skb, AF_INET6);
164}
165
166static inline
167struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
168{
169 struct net_device *dev = skb_dst(skb)->dev;
170
171 if (netif_is_l3_slave(dev)) {
172 struct net_device *master;
173
174 master = netdev_master_upper_dev_get_rcu(dev);
175 if (master && master->l3mdev_ops->l3mdev_l3_out)
176 skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
177 skb, proto);
178 }
179
180 return skb;
181}
182
183static inline
184struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
185{
186 return l3mdev_l3_out(sk, skb, AF_INET);
187}
188
189static inline
190struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
191{
192 return l3mdev_l3_out(sk, skb, AF_INET6);
193}
194#else
195
196static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
197{
198 return 0;
199}
200static inline int l3mdev_master_ifindex(struct net_device *dev)
201{
202 return 0;
203}
204
205static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
206{
207 return 0;
208}
209
210static inline
211struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
212{
213 return NULL;
214}
215
216static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
217{
218 return 0;
219}
220static inline u32 l3mdev_fib_table(const struct net_device *dev)
221{
222 return 0;
223}
224static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
225{
226 return 0;
227}
228
229static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
230{
231 return false;
232}
233
234static inline
235struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
236{
237 return NULL;
238}
239
240static inline
241struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
242{
243 return skb;
244}
245
246static inline
247struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
248{
249 return skb;
250}
251
252static inline
253struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
254{
255 return skb;
256}
257
258static inline
259struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
260{
261 return skb;
262}
263
264static inline
265int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
266 struct fib_lookup_arg *arg)
267{
268 return 1;
269}
270static inline
271void l3mdev_update_flow(struct net *net, struct flowi *fl)
272{
273}
274#endif
275
276#endif /* _NET_L3MDEV_H_ */