blob: 818fd4f100fc44557e9a42da0700fefbe490ed7e [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 Ahern96c63fa2016-06-08 10:55:39 -070014#include <net/fib_rules.h>
15
David Ahern1b69c6d2015-09-29 20:07:11 -070016/**
17 * struct l3mdev_ops - l3mdev operations
18 *
19 * @l3mdev_fib_table: Get FIB table id to use for lookups
20 *
21 * @l3mdev_get_rtable: Get cached IPv4 rtable (dst_entry) for device
David Ahern8cbb512c2015-10-05 08:51:26 -070022 *
23 * @l3mdev_get_saddr: Get source address for a flow
David Ahernccf3c8c2015-10-12 11:47:07 -070024 *
25 * @l3mdev_get_rt6_dst: Get cached IPv6 rt6_info (dst_entry) for device
David Ahern1b69c6d2015-09-29 20:07:11 -070026 */
27
28struct l3mdev_ops {
29 u32 (*l3mdev_fib_table)(const struct net_device *dev);
David Ahern74b20582016-05-10 11:19:50 -070030 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
31 struct sk_buff *skb, u16 proto);
David Ahernccf3c8c2015-10-12 11:47:07 -070032
33 /* IPv4 ops */
David Ahern1b69c6d2015-09-29 20:07:11 -070034 struct rtable * (*l3mdev_get_rtable)(const struct net_device *dev,
35 const struct flowi4 *fl4);
David Ahernb5bdacf2016-01-04 09:09:27 -080036 int (*l3mdev_get_saddr)(struct net_device *dev,
David Ahern8cbb512c2015-10-05 08:51:26 -070037 struct flowi4 *fl4);
David Ahernccf3c8c2015-10-12 11:47:07 -070038
39 /* IPv6 ops */
40 struct dst_entry * (*l3mdev_get_rt6_dst)(const struct net_device *dev,
David Aherncd2a9e62016-06-13 13:44:17 -070041 struct flowi6 *fl6);
David Ahern0d240e72016-06-16 16:24:25 -070042 int (*l3mdev_get_saddr6)(struct net_device *dev,
43 const struct sock *sk,
44 struct flowi6 *fl6);
David Ahern1b69c6d2015-09-29 20:07:11 -070045};
46
47#ifdef CONFIG_NET_L3_MASTER_DEV
48
David Ahern96c63fa2016-06-08 10:55:39 -070049int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
50 struct fib_lookup_arg *arg);
51
David Ahern3f2fb9a2016-02-24 11:47:02 -080052int l3mdev_master_ifindex_rcu(const struct net_device *dev);
David Ahern1b69c6d2015-09-29 20:07:11 -070053static inline int l3mdev_master_ifindex(struct net_device *dev)
54{
55 int ifindex;
56
57 rcu_read_lock();
58 ifindex = l3mdev_master_ifindex_rcu(dev);
59 rcu_read_unlock();
60
61 return ifindex;
62}
63
David Ahern1a852472015-12-16 13:20:43 -080064static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
65{
66 struct net_device *dev;
67 int rc = 0;
68
69 if (likely(ifindex)) {
70 rcu_read_lock();
71
72 dev = dev_get_by_index_rcu(net, ifindex);
73 if (dev)
74 rc = l3mdev_master_ifindex_rcu(dev);
75
76 rcu_read_unlock();
77 }
78
79 return rc;
80}
81
David Ahern1b69c6d2015-09-29 20:07:11 -070082/* get index of an interface to use for FIB lookups. For devices
83 * enslaved to an L3 master device FIB lookups are based on the
84 * master index
85 */
86static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
87{
88 return l3mdev_master_ifindex_rcu(dev) ? : dev->ifindex;
89}
90
91static inline int l3mdev_fib_oif(struct net_device *dev)
92{
93 int oif;
94
95 rcu_read_lock();
96 oif = l3mdev_fib_oif_rcu(dev);
97 rcu_read_unlock();
98
99 return oif;
100}
101
102u32 l3mdev_fib_table_rcu(const struct net_device *dev);
103u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
104static inline u32 l3mdev_fib_table(const struct net_device *dev)
105{
106 u32 tb_id;
107
108 rcu_read_lock();
109 tb_id = l3mdev_fib_table_rcu(dev);
110 rcu_read_unlock();
111
112 return tb_id;
113}
114
115static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
116 const struct flowi4 *fl4)
117{
118 if (netif_is_l3_master(dev) && dev->l3mdev_ops->l3mdev_get_rtable)
119 return dev->l3mdev_ops->l3mdev_get_rtable(dev, fl4);
120
121 return NULL;
122}
123
David Ahern9478d122015-09-29 20:07:18 -0700124static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
125{
126 struct net_device *dev;
127 bool rc = false;
128
129 if (ifindex == 0)
130 return false;
131
132 rcu_read_lock();
133
134 dev = dev_get_by_index_rcu(net, ifindex);
135 if (dev)
136 rc = netif_is_l3_master(dev);
137
138 rcu_read_unlock();
139
140 return rc;
141}
142
David Ahern4a658962016-05-07 16:48:59 -0700143int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4);
David Ahern8cbb512c2015-10-05 08:51:26 -0700144
David Aherncd2a9e62016-06-13 13:44:17 -0700145struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6);
David Ahern0d240e72016-06-16 16:24:25 -0700146int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
147 struct flowi6 *fl6);
David Ahernccf3c8c2015-10-12 11:47:07 -0700148
David Ahern74b20582016-05-10 11:19:50 -0700149static inline
150struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
151{
152 struct net_device *master = NULL;
153
154 if (netif_is_l3_slave(skb->dev))
155 master = netdev_master_upper_dev_get_rcu(skb->dev);
156 else if (netif_is_l3_master(skb->dev))
157 master = skb->dev;
158
159 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
160 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
161
162 return skb;
163}
164
165static inline
166struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
167{
168 return l3mdev_l3_rcv(skb, AF_INET);
169}
170
171static inline
172struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
173{
174 return l3mdev_l3_rcv(skb, AF_INET6);
175}
176
David Ahern1b69c6d2015-09-29 20:07:11 -0700177#else
178
David Ahern3f2fb9a2016-02-24 11:47:02 -0800179static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
David Ahern1b69c6d2015-09-29 20:07:11 -0700180{
181 return 0;
182}
183static inline int l3mdev_master_ifindex(struct net_device *dev)
184{
185 return 0;
186}
187
David Ahern1a852472015-12-16 13:20:43 -0800188static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
189{
190 return 0;
191}
192
David Ahern1b69c6d2015-09-29 20:07:11 -0700193static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
194{
195 return dev ? dev->ifindex : 0;
196}
197static inline int l3mdev_fib_oif(struct net_device *dev)
198{
199 return dev ? dev->ifindex : 0;
200}
201
202static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
203{
204 return 0;
205}
206static inline u32 l3mdev_fib_table(const struct net_device *dev)
207{
208 return 0;
209}
210static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
211{
212 return 0;
213}
214
215static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
216 const struct flowi4 *fl4)
217{
218 return NULL;
219}
220
David Ahern9478d122015-09-29 20:07:18 -0700221static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
222{
223 return false;
224}
225
David Ahernb5bdacf2016-01-04 09:09:27 -0800226static inline int l3mdev_get_saddr(struct net *net, int ifindex,
227 struct flowi4 *fl4)
David Ahern8cbb512c2015-10-05 08:51:26 -0700228{
David Ahernb5bdacf2016-01-04 09:09:27 -0800229 return 0;
David Ahern8cbb512c2015-10-05 08:51:26 -0700230}
David Ahernccf3c8c2015-10-12 11:47:07 -0700231
232static inline
David Aherncd2a9e62016-06-13 13:44:17 -0700233struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6)
David Ahernccf3c8c2015-10-12 11:47:07 -0700234{
235 return NULL;
236}
David Ahern74b20582016-05-10 11:19:50 -0700237
David Ahern0d240e72016-06-16 16:24:25 -0700238static inline int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
239 struct flowi6 *fl6)
240{
241 return 0;
242}
243
David Ahern74b20582016-05-10 11:19:50 -0700244static inline
245struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
246{
247 return skb;
248}
249
250static inline
251struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
252{
253 return skb;
254}
David Ahern96c63fa2016-06-08 10:55:39 -0700255
256static inline
257int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
258 struct fib_lookup_arg *arg)
259{
260 return 1;
261}
David Ahern1b69c6d2015-09-29 20:07:11 -0700262#endif
263
264#endif /* _NET_L3MDEV_H_ */