blob: afa64f086d8720ac13e7302fed043f9c6bf49d47 [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>
David Aherne9db0422017-01-17 14:57:36 -080029#include <net/nexthop.h>
Roopa Prabhu499a2422015-07-21 10:43:46 +020030
Robert Shearman745041e2016-02-19 09:43:16 +000031#ifdef CONFIG_MODULES
32
33static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
34{
35 /* Only lwt encaps implemented without using an interface for
36 * the encap need to return a string here.
37 */
38 switch (encap_type) {
39 case LWTUNNEL_ENCAP_MPLS:
40 return "MPLS";
41 case LWTUNNEL_ENCAP_ILA:
42 return "ILA";
43 case LWTUNNEL_ENCAP_IP6:
44 case LWTUNNEL_ENCAP_IP:
45 case LWTUNNEL_ENCAP_NONE:
46 case __LWTUNNEL_ENCAP_MAX:
47 /* should not have got here */
48 WARN_ON(1);
49 break;
50 }
51 return NULL;
52}
53
54#endif /* CONFIG_MODULES */
55
Roopa Prabhu499a2422015-07-21 10:43:46 +020056struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
57{
58 struct lwtunnel_state *lws;
59
60 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
61
62 return lws;
63}
64EXPORT_SYMBOL(lwtunnel_state_alloc);
65
Thomas Graf92a99bf2015-07-29 09:45:40 +020066static const struct lwtunnel_encap_ops __rcu *
Roopa Prabhu499a2422015-07-21 10:43:46 +020067 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
68
Robert Shearmane972cce2017-01-24 16:26:48 +000069void lwtstate_free(struct lwtunnel_state *lws)
70{
71 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
72
73 kfree(lws);
74 module_put(ops->owner);
75}
76EXPORT_SYMBOL(lwtstate_free);
77
Roopa Prabhu499a2422015-07-21 10:43:46 +020078int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
79 unsigned int num)
80{
81 if (num > LWTUNNEL_ENCAP_MAX)
82 return -ERANGE;
83
84 return !cmpxchg((const struct lwtunnel_encap_ops **)
85 &lwtun_encaps[num],
86 NULL, ops) ? 0 : -1;
87}
88EXPORT_SYMBOL(lwtunnel_encap_add_ops);
89
90int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
91 unsigned int encap_type)
92{
93 int ret;
94
95 if (encap_type == LWTUNNEL_ENCAP_NONE ||
96 encap_type > LWTUNNEL_ENCAP_MAX)
97 return -ERANGE;
98
99 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
100 &lwtun_encaps[encap_type],
101 ops, NULL) == ops) ? 0 : -1;
102
103 synchronize_net();
104
105 return ret;
106}
107EXPORT_SYMBOL(lwtunnel_encap_del_ops);
108
109int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700110 struct nlattr *encap, unsigned int family,
111 const void *cfg, struct lwtunnel_state **lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200112{
113 const struct lwtunnel_encap_ops *ops;
114 int ret = -EINVAL;
115
116 if (encap_type == LWTUNNEL_ENCAP_NONE ||
117 encap_type > LWTUNNEL_ENCAP_MAX)
118 return ret;
119
120 ret = -EOPNOTSUPP;
121 rcu_read_lock();
122 ops = rcu_dereference(lwtun_encaps[encap_type]);
Robert Shearmane972cce2017-01-24 16:26:48 +0000123 if (likely(ops && ops->build_state && try_module_get(ops->owner))) {
Tom Herbert127eb7c2015-08-24 09:45:41 -0700124 ret = ops->build_state(dev, encap, family, cfg, lws);
Robert Shearmane972cce2017-01-24 16:26:48 +0000125 if (ret)
126 module_put(ops->owner);
127 }
Roopa Prabhu499a2422015-07-21 10:43:46 +0200128 rcu_read_unlock();
129
130 return ret;
131}
132EXPORT_SYMBOL(lwtunnel_build_state);
133
David Aherne9db0422017-01-17 14:57:36 -0800134int lwtunnel_valid_encap_type(u16 encap_type)
135{
136 const struct lwtunnel_encap_ops *ops;
137 int ret = -EINVAL;
138
139 if (encap_type == LWTUNNEL_ENCAP_NONE ||
140 encap_type > LWTUNNEL_ENCAP_MAX)
141 return ret;
142
143 rcu_read_lock();
144 ops = rcu_dereference(lwtun_encaps[encap_type]);
145 rcu_read_unlock();
146#ifdef CONFIG_MODULES
147 if (!ops) {
148 const char *encap_type_str = lwtunnel_encap_str(encap_type);
149
150 if (encap_type_str) {
151 __rtnl_unlock();
152 request_module("rtnl-lwt-%s", encap_type_str);
153 rtnl_lock();
154
155 rcu_read_lock();
156 ops = rcu_dereference(lwtun_encaps[encap_type]);
157 rcu_read_unlock();
158 }
159 }
160#endif
161 return ops ? 0 : -EOPNOTSUPP;
162}
163EXPORT_SYMBOL(lwtunnel_valid_encap_type);
164
165int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining)
166{
167 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
168 struct nlattr *nla_entype;
169 struct nlattr *attrs;
170 struct nlattr *nla;
171 u16 encap_type;
172 int attrlen;
173
174 while (rtnh_ok(rtnh, remaining)) {
175 attrlen = rtnh_attrlen(rtnh);
176 if (attrlen > 0) {
177 attrs = rtnh_attrs(rtnh);
178 nla = nla_find(attrs, attrlen, RTA_ENCAP);
179 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
180
181 if (nla_entype) {
182 encap_type = nla_get_u16(nla_entype);
183
184 if (lwtunnel_valid_encap_type(encap_type) != 0)
185 return -EOPNOTSUPP;
186 }
187 }
188 rtnh = rtnh_next(rtnh, &remaining);
189 }
190
191 return 0;
192}
193EXPORT_SYMBOL(lwtunnel_valid_encap_type_attr);
194
Roopa Prabhu499a2422015-07-21 10:43:46 +0200195int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
196{
197 const struct lwtunnel_encap_ops *ops;
198 struct nlattr *nest;
199 int ret = -EINVAL;
200
201 if (!lwtstate)
202 return 0;
203
204 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
205 lwtstate->type > LWTUNNEL_ENCAP_MAX)
206 return 0;
207
208 ret = -EOPNOTSUPP;
209 nest = nla_nest_start(skb, RTA_ENCAP);
210 rcu_read_lock();
211 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
212 if (likely(ops && ops->fill_encap))
213 ret = ops->fill_encap(skb, lwtstate);
214 rcu_read_unlock();
215
216 if (ret)
217 goto nla_put_failure;
218 nla_nest_end(skb, nest);
219 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
220 if (ret)
221 goto nla_put_failure;
222
223 return 0;
224
225nla_put_failure:
226 nla_nest_cancel(skb, nest);
227
228 return (ret == -EOPNOTSUPP ? 0 : ret);
229}
230EXPORT_SYMBOL(lwtunnel_fill_encap);
231
232int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
233{
234 const struct lwtunnel_encap_ops *ops;
235 int ret = 0;
236
237 if (!lwtstate)
238 return 0;
239
240 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
241 lwtstate->type > LWTUNNEL_ENCAP_MAX)
242 return 0;
243
244 rcu_read_lock();
245 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
246 if (likely(ops && ops->get_encap_size))
247 ret = nla_total_size(ops->get_encap_size(lwtstate));
248 rcu_read_unlock();
249
250 return ret;
251}
252EXPORT_SYMBOL(lwtunnel_get_encap_size);
253
254int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
255{
256 const struct lwtunnel_encap_ops *ops;
257 int ret = 0;
258
259 if (!a && !b)
260 return 0;
261
262 if (!a || !b)
263 return 1;
264
265 if (a->type != b->type)
266 return 1;
267
268 if (a->type == LWTUNNEL_ENCAP_NONE ||
269 a->type > LWTUNNEL_ENCAP_MAX)
270 return 0;
271
272 rcu_read_lock();
273 ops = rcu_dereference(lwtun_encaps[a->type]);
274 if (likely(ops && ops->cmp_encap))
275 ret = ops->cmp_encap(a, b);
276 rcu_read_unlock();
277
278 return ret;
279}
280EXPORT_SYMBOL(lwtunnel_cmp_encap);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200281
Eric W. Biedermanede20592015-10-07 16:48:47 -0500282int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200283{
Jiri Benc61adedf2015-08-20 13:56:25 +0200284 struct dst_entry *dst = skb_dst(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200285 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200286 struct lwtunnel_state *lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200287 int ret = -EINVAL;
288
Jiri Benc61adedf2015-08-20 13:56:25 +0200289 if (!dst)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200290 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200291 lwtstate = dst->lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200292
293 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
294 lwtstate->type > LWTUNNEL_ENCAP_MAX)
295 return 0;
296
297 ret = -EOPNOTSUPP;
298 rcu_read_lock();
299 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
300 if (likely(ops && ops->output))
Eric W. Biedermanede20592015-10-07 16:48:47 -0500301 ret = ops->output(net, sk, skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200302 rcu_read_unlock();
303
304 if (ret == -EOPNOTSUPP)
305 goto drop;
306
307 return ret;
308
309drop:
Dan Carpentere11f40b2015-07-27 11:07:47 +0300310 kfree_skb(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200311
312 return ret;
313}
Roopa Prabhuffce4192015-07-21 10:43:49 +0200314EXPORT_SYMBOL(lwtunnel_output);
Tom Herbert25368622015-08-17 13:42:24 -0700315
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700316int lwtunnel_xmit(struct sk_buff *skb)
317{
318 struct dst_entry *dst = skb_dst(skb);
319 const struct lwtunnel_encap_ops *ops;
320 struct lwtunnel_state *lwtstate;
321 int ret = -EINVAL;
322
323 if (!dst)
324 goto drop;
325
326 lwtstate = dst->lwtstate;
327
328 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
329 lwtstate->type > LWTUNNEL_ENCAP_MAX)
330 return 0;
331
332 ret = -EOPNOTSUPP;
333 rcu_read_lock();
334 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
335 if (likely(ops && ops->xmit))
336 ret = ops->xmit(skb);
337 rcu_read_unlock();
338
339 if (ret == -EOPNOTSUPP)
340 goto drop;
341
342 return ret;
343
344drop:
345 kfree_skb(skb);
346
347 return ret;
348}
349EXPORT_SYMBOL(lwtunnel_xmit);
350
Jiri Benc61adedf2015-08-20 13:56:25 +0200351int lwtunnel_input(struct sk_buff *skb)
Tom Herbert25368622015-08-17 13:42:24 -0700352{
Jiri Benc61adedf2015-08-20 13:56:25 +0200353 struct dst_entry *dst = skb_dst(skb);
Tom Herbert25368622015-08-17 13:42:24 -0700354 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200355 struct lwtunnel_state *lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700356 int ret = -EINVAL;
357
Jiri Benc61adedf2015-08-20 13:56:25 +0200358 if (!dst)
Tom Herbert25368622015-08-17 13:42:24 -0700359 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200360 lwtstate = dst->lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700361
362 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
363 lwtstate->type > LWTUNNEL_ENCAP_MAX)
364 return 0;
365
366 ret = -EOPNOTSUPP;
367 rcu_read_lock();
368 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
369 if (likely(ops && ops->input))
370 ret = ops->input(skb);
371 rcu_read_unlock();
372
373 if (ret == -EOPNOTSUPP)
374 goto drop;
375
376 return ret;
377
378drop:
379 kfree_skb(skb);
380
381 return ret;
382}
Tom Herbert25368622015-08-17 13:42:24 -0700383EXPORT_SYMBOL(lwtunnel_input);