blob: 128487658ff7c2fa147fabd02bc4c9ef943656e3 [file] [log] [blame]
David Ahern1b69c6d2015-09-29 20:07:11 -07001/*
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
David Aherna8e3e1a2016-09-10 12:09:53 -070014#include <net/dst.h>
David Ahern96c63fa2016-06-08 10:55:39 -070015#include <net/fib_rules.h>
16
David Ahern1b69c6d2015-09-29 20:07:11 -070017/**
18 * struct l3mdev_ops - l3mdev operations
19 *
20 * @l3mdev_fib_table: Get FIB table id to use for lookups
21 *
David Aherna8e3e1a2016-09-10 12:09:53 -070022 * @l3mdev_l3_rcv: Hook in L3 receive path
23 *
24 * @l3mdev_l3_out: Hook in L3 output path
25 *
David Ahern4c1feac2016-09-10 12:09:56 -070026 * @l3mdev_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
David Ahern1b69c6d2015-09-29 20:07:11 -070027 */
28
29struct l3mdev_ops {
30 u32 (*l3mdev_fib_table)(const struct net_device *dev);
David Ahern74b20582016-05-10 11:19:50 -070031 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
32 struct sk_buff *skb, u16 proto);
David Aherna8e3e1a2016-09-10 12:09:53 -070033 struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
34 struct sock *sk, struct sk_buff *skb,
35 u16 proto);
David Ahernccf3c8c2015-10-12 11:47:07 -070036
David Ahernccf3c8c2015-10-12 11:47:07 -070037 /* IPv6 ops */
David Ahern4c1feac2016-09-10 12:09:56 -070038 struct dst_entry * (*l3mdev_link_scope_lookup)(const struct net_device *dev,
David Aherncd2a9e62016-06-13 13:44:17 -070039 struct flowi6 *fl6);
David Ahern1b69c6d2015-09-29 20:07:11 -070040};
41
42#ifdef CONFIG_NET_L3_MASTER_DEV
43
David Ahern96c63fa2016-06-08 10:55:39 -070044int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
45 struct fib_lookup_arg *arg);
46
David Ahern9ee00342016-09-10 12:09:52 -070047void l3mdev_update_flow(struct net *net, struct flowi *fl);
48
David Ahern3f2fb9a2016-02-24 11:47:02 -080049int l3mdev_master_ifindex_rcu(const struct net_device *dev);
David Ahern1b69c6d2015-09-29 20:07:11 -070050static 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
David Ahern1a852472015-12-16 13:20:43 -080061static 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
David Ahernafbac6012016-06-16 16:24:26 -070079static inline
David Ahern5f02ce242016-09-10 12:09:54 -070080struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
David Ahernafbac6012016-06-16 16:24:26 -070081{
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;
David Ahern5f02ce242016-09-10 12:09:54 -070089 struct net_device *master;
David Ahernafbac6012016-06-16 16:24:26 -070090
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
David Ahern1b69c6d2015-09-29 20:07:11 -0700104u32 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
David Ahern6104e112016-10-12 13:20:11 -0700117static 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
David Ahern4c1feac2016-09-10 12:09:56 -0700136struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
David Ahernccf3c8c2015-10-12 11:47:07 -0700137
David Ahern74b20582016-05-10 11:19:50 -0700138static 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);
Daniel Borkmannc574feb2019-01-30 12:49:48 +0100145 else if (netif_is_l3_master(skb->dev) ||
146 netif_has_l3_rx_handler(skb->dev))
David Ahern74b20582016-05-10 11:19:50 -0700147 master = skb->dev;
148
149 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
150 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
151
152 return skb;
153}
154
155static inline
156struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
157{
158 return l3mdev_l3_rcv(skb, AF_INET);
159}
160
161static inline
162struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
163{
164 return l3mdev_l3_rcv(skb, AF_INET6);
165}
166
David Aherna8e3e1a2016-09-10 12:09:53 -0700167static inline
168struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
169{
170 struct net_device *dev = skb_dst(skb)->dev;
171
172 if (netif_is_l3_slave(dev)) {
173 struct net_device *master;
174
175 master = netdev_master_upper_dev_get_rcu(dev);
176 if (master && master->l3mdev_ops->l3mdev_l3_out)
177 skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
178 skb, proto);
179 }
180
181 return skb;
182}
183
184static inline
185struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
186{
187 return l3mdev_l3_out(sk, skb, AF_INET);
188}
189
190static inline
191struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
192{
193 return l3mdev_l3_out(sk, skb, AF_INET6);
194}
David Ahern1b69c6d2015-09-29 20:07:11 -0700195#else
196
David Ahern3f2fb9a2016-02-24 11:47:02 -0800197static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
David Ahern1b69c6d2015-09-29 20:07:11 -0700198{
199 return 0;
200}
201static inline int l3mdev_master_ifindex(struct net_device *dev)
202{
203 return 0;
204}
205
David Ahern1a852472015-12-16 13:20:43 -0800206static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
207{
208 return 0;
209}
210
David Ahernafbac6012016-06-16 16:24:26 -0700211static inline
David Ahern5f02ce242016-09-10 12:09:54 -0700212struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
David Ahernafbac6012016-06-16 16:24:26 -0700213{
214 return NULL;
215}
216
David Ahern1b69c6d2015-09-29 20:07:11 -0700217static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
218{
219 return 0;
220}
221static inline u32 l3mdev_fib_table(const struct net_device *dev)
222{
223 return 0;
224}
225static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
226{
227 return 0;
228}
229
David Ahern6104e112016-10-12 13:20:11 -0700230static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
231{
232 return false;
233}
234
David Ahernccf3c8c2015-10-12 11:47:07 -0700235static inline
David Ahern4c1feac2016-09-10 12:09:56 -0700236struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
David Ahernccf3c8c2015-10-12 11:47:07 -0700237{
238 return NULL;
239}
David Ahern74b20582016-05-10 11:19:50 -0700240
241static inline
242struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
243{
244 return skb;
245}
246
247static inline
248struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
249{
250 return skb;
251}
David Ahern96c63fa2016-06-08 10:55:39 -0700252
253static inline
David Aherna8e3e1a2016-09-10 12:09:53 -0700254struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
255{
256 return skb;
257}
258
259static inline
260struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
261{
262 return skb;
263}
264
265static inline
David Ahern96c63fa2016-06-08 10:55:39 -0700266int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
267 struct fib_lookup_arg *arg)
268{
269 return 1;
270}
David Ahern9ee00342016-09-10 12:09:52 -0700271static inline
272void l3mdev_update_flow(struct net *net, struct flowi *fl)
273{
274}
David Ahern1b69c6d2015-09-29 20:07:11 -0700275#endif
276
277#endif /* _NET_L3MDEV_H_ */