blob: 3331585174d9dab0470a8119885c3d6c9b1537eb [file] [log] [blame]
Roopa Prabhu499a2422015-07-21 10:43:46 +02001/*
2 * lwtunnel Infrastructure for light weight tunnels like mpls
3 *
4 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <linux/capability.h>
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/skbuff.h>
20#include <linux/netdevice.h>
21#include <linux/lwtunnel.h>
22#include <linux/in.h>
23#include <linux/init.h>
24#include <linux/err.h>
25
26#include <net/lwtunnel.h>
27#include <net/rtnetlink.h>
Roopa Prabhuffce4192015-07-21 10:43:49 +020028#include <net/ip6_fib.h>
Roopa Prabhu499a2422015-07-21 10:43:46 +020029
30struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
31{
32 struct lwtunnel_state *lws;
33
34 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
35
36 return lws;
37}
38EXPORT_SYMBOL(lwtunnel_state_alloc);
39
Thomas Graf92a99bf2015-07-29 09:45:40 +020040static const struct lwtunnel_encap_ops __rcu *
Roopa Prabhu499a2422015-07-21 10:43:46 +020041 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
42
43int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
44 unsigned int num)
45{
46 if (num > LWTUNNEL_ENCAP_MAX)
47 return -ERANGE;
48
49 return !cmpxchg((const struct lwtunnel_encap_ops **)
50 &lwtun_encaps[num],
51 NULL, ops) ? 0 : -1;
52}
53EXPORT_SYMBOL(lwtunnel_encap_add_ops);
54
55int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
56 unsigned int encap_type)
57{
58 int ret;
59
60 if (encap_type == LWTUNNEL_ENCAP_NONE ||
61 encap_type > LWTUNNEL_ENCAP_MAX)
62 return -ERANGE;
63
64 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
65 &lwtun_encaps[encap_type],
66 ops, NULL) == ops) ? 0 : -1;
67
68 synchronize_net();
69
70 return ret;
71}
72EXPORT_SYMBOL(lwtunnel_encap_del_ops);
73
74int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
75 struct nlattr *encap, struct lwtunnel_state **lws)
76{
77 const struct lwtunnel_encap_ops *ops;
78 int ret = -EINVAL;
79
80 if (encap_type == LWTUNNEL_ENCAP_NONE ||
81 encap_type > LWTUNNEL_ENCAP_MAX)
82 return ret;
83
84 ret = -EOPNOTSUPP;
85 rcu_read_lock();
86 ops = rcu_dereference(lwtun_encaps[encap_type]);
87 if (likely(ops && ops->build_state))
88 ret = ops->build_state(dev, encap, lws);
89 rcu_read_unlock();
90
91 return ret;
92}
93EXPORT_SYMBOL(lwtunnel_build_state);
94
95int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
96{
97 const struct lwtunnel_encap_ops *ops;
98 struct nlattr *nest;
99 int ret = -EINVAL;
100
101 if (!lwtstate)
102 return 0;
103
104 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
105 lwtstate->type > LWTUNNEL_ENCAP_MAX)
106 return 0;
107
108 ret = -EOPNOTSUPP;
109 nest = nla_nest_start(skb, RTA_ENCAP);
110 rcu_read_lock();
111 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
112 if (likely(ops && ops->fill_encap))
113 ret = ops->fill_encap(skb, lwtstate);
114 rcu_read_unlock();
115
116 if (ret)
117 goto nla_put_failure;
118 nla_nest_end(skb, nest);
119 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
120 if (ret)
121 goto nla_put_failure;
122
123 return 0;
124
125nla_put_failure:
126 nla_nest_cancel(skb, nest);
127
128 return (ret == -EOPNOTSUPP ? 0 : ret);
129}
130EXPORT_SYMBOL(lwtunnel_fill_encap);
131
132int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
133{
134 const struct lwtunnel_encap_ops *ops;
135 int ret = 0;
136
137 if (!lwtstate)
138 return 0;
139
140 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
141 lwtstate->type > LWTUNNEL_ENCAP_MAX)
142 return 0;
143
144 rcu_read_lock();
145 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
146 if (likely(ops && ops->get_encap_size))
147 ret = nla_total_size(ops->get_encap_size(lwtstate));
148 rcu_read_unlock();
149
150 return ret;
151}
152EXPORT_SYMBOL(lwtunnel_get_encap_size);
153
154int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
155{
156 const struct lwtunnel_encap_ops *ops;
157 int ret = 0;
158
159 if (!a && !b)
160 return 0;
161
162 if (!a || !b)
163 return 1;
164
165 if (a->type != b->type)
166 return 1;
167
168 if (a->type == LWTUNNEL_ENCAP_NONE ||
169 a->type > LWTUNNEL_ENCAP_MAX)
170 return 0;
171
172 rcu_read_lock();
173 ops = rcu_dereference(lwtun_encaps[a->type]);
174 if (likely(ops && ops->cmp_encap))
175 ret = ops->cmp_encap(a, b);
176 rcu_read_unlock();
177
178 return ret;
179}
180EXPORT_SYMBOL(lwtunnel_cmp_encap);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200181
182int __lwtunnel_output(struct sock *sk, struct sk_buff *skb,
183 struct lwtunnel_state *lwtstate)
184{
185 const struct lwtunnel_encap_ops *ops;
186 int ret = -EINVAL;
187
188 if (!lwtstate)
189 goto drop;
190
191 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
192 lwtstate->type > LWTUNNEL_ENCAP_MAX)
193 return 0;
194
195 ret = -EOPNOTSUPP;
196 rcu_read_lock();
197 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
198 if (likely(ops && ops->output))
199 ret = ops->output(sk, skb);
200 rcu_read_unlock();
201
202 if (ret == -EOPNOTSUPP)
203 goto drop;
204
205 return ret;
206
207drop:
Dan Carpentere11f40b2015-07-27 11:07:47 +0300208 kfree_skb(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200209
210 return ret;
211}
212
213int lwtunnel_output6(struct sock *sk, struct sk_buff *skb)
214{
215 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
216 struct lwtunnel_state *lwtstate = NULL;
217
Robert Shearmanabf7c1c2015-08-03 17:39:20 +0100218 if (rt) {
Roopa Prabhuffce4192015-07-21 10:43:49 +0200219 lwtstate = rt->rt6i_lwtstate;
Robert Shearmanabf7c1c2015-08-03 17:39:20 +0100220 skb->dev = rt->dst.dev;
221 }
222
223 skb->protocol = htons(ETH_P_IPV6);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200224
225 return __lwtunnel_output(sk, skb, lwtstate);
226}
227EXPORT_SYMBOL(lwtunnel_output6);
228
229int lwtunnel_output(struct sock *sk, struct sk_buff *skb)
230{
231 struct rtable *rt = (struct rtable *)skb_dst(skb);
232 struct lwtunnel_state *lwtstate = NULL;
233
Robert Shearmanabf7c1c2015-08-03 17:39:20 +0100234 if (rt) {
Roopa Prabhuffce4192015-07-21 10:43:49 +0200235 lwtstate = rt->rt_lwtstate;
Robert Shearmanabf7c1c2015-08-03 17:39:20 +0100236 skb->dev = rt->dst.dev;
237 }
238
239 skb->protocol = htons(ETH_P_IP);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200240
241 return __lwtunnel_output(sk, skb, lwtstate);
242}
243EXPORT_SYMBOL(lwtunnel_output);
Tom Herbert25368622015-08-17 13:42:24 -0700244
245int __lwtunnel_input(struct sk_buff *skb,
246 struct lwtunnel_state *lwtstate)
247{
248 const struct lwtunnel_encap_ops *ops;
249 int ret = -EINVAL;
250
251 if (!lwtstate)
252 goto drop;
253
254 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
255 lwtstate->type > LWTUNNEL_ENCAP_MAX)
256 return 0;
257
258 ret = -EOPNOTSUPP;
259 rcu_read_lock();
260 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
261 if (likely(ops && ops->input))
262 ret = ops->input(skb);
263 rcu_read_unlock();
264
265 if (ret == -EOPNOTSUPP)
266 goto drop;
267
268 return ret;
269
270drop:
271 kfree_skb(skb);
272
273 return ret;
274}
275
276int lwtunnel_input6(struct sk_buff *skb)
277{
278 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
279 struct lwtunnel_state *lwtstate = NULL;
280
281 if (rt)
282 lwtstate = rt->rt6i_lwtstate;
283
284 return __lwtunnel_input(skb, lwtstate);
285}
286EXPORT_SYMBOL(lwtunnel_input6);
287
288int lwtunnel_input(struct sk_buff *skb)
289{
290 struct rtable *rt = (struct rtable *)skb_dst(skb);
291 struct lwtunnel_state *lwtstate = NULL;
292
293 if (rt)
294 lwtstate = rt->rt_lwtstate;
295
296 return __lwtunnel_input(skb, lwtstate);
297}
298EXPORT_SYMBOL(lwtunnel_input);