blob: cfae3d5fe11f10035394aea48fb5332244058a03 [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 Ahern9ed59592017-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";
David Lebrun6c8702c2016-11-08 14:57:41 +010043 case LWTUNNEL_ENCAP_SEG6:
44 return "SEG6";
Thomas Graf3a0af8f2016-11-30 17:10:10 +010045 case LWTUNNEL_ENCAP_BPF:
46 return "BPF";
Robert Shearman745041e2016-02-19 09:43:16 +000047 case LWTUNNEL_ENCAP_IP6:
48 case LWTUNNEL_ENCAP_IP:
49 case LWTUNNEL_ENCAP_NONE:
50 case __LWTUNNEL_ENCAP_MAX:
51 /* should not have got here */
52 WARN_ON(1);
53 break;
54 }
55 return NULL;
56}
57
58#endif /* CONFIG_MODULES */
59
Roopa Prabhu499a2422015-07-21 10:43:46 +020060struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
61{
62 struct lwtunnel_state *lws;
63
64 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
65
66 return lws;
67}
68EXPORT_SYMBOL(lwtunnel_state_alloc);
69
Thomas Graf92a99bf2015-07-29 09:45:40 +020070static const struct lwtunnel_encap_ops __rcu *
Roopa Prabhu499a2422015-07-21 10:43:46 +020071 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
72
73int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
74 unsigned int num)
75{
76 if (num > LWTUNNEL_ENCAP_MAX)
77 return -ERANGE;
78
79 return !cmpxchg((const struct lwtunnel_encap_ops **)
80 &lwtun_encaps[num],
81 NULL, ops) ? 0 : -1;
82}
83EXPORT_SYMBOL(lwtunnel_encap_add_ops);
84
85int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
86 unsigned int encap_type)
87{
88 int ret;
89
90 if (encap_type == LWTUNNEL_ENCAP_NONE ||
91 encap_type > LWTUNNEL_ENCAP_MAX)
92 return -ERANGE;
93
94 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
95 &lwtun_encaps[encap_type],
96 ops, NULL) == ops) ? 0 : -1;
97
98 synchronize_net();
99
100 return ret;
101}
102EXPORT_SYMBOL(lwtunnel_encap_del_ops);
103
David Ahern30357d72017-01-30 12:07:37 -0800104int lwtunnel_build_state(u16 encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700105 struct nlattr *encap, unsigned int family,
106 const void *cfg, struct lwtunnel_state **lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200107{
108 const struct lwtunnel_encap_ops *ops;
109 int ret = -EINVAL;
110
111 if (encap_type == LWTUNNEL_ENCAP_NONE ||
112 encap_type > LWTUNNEL_ENCAP_MAX)
113 return ret;
114
115 ret = -EOPNOTSUPP;
116 rcu_read_lock();
117 ops = rcu_dereference(lwtun_encaps[encap_type]);
Robert Shearman85c81402017-01-24 16:26:48 +0000118 if (likely(ops && ops->build_state && try_module_get(ops->owner))) {
David Ahern30357d72017-01-30 12:07:37 -0800119 ret = ops->build_state(encap, family, cfg, lws);
Robert Shearman85c81402017-01-24 16:26:48 +0000120 if (ret)
121 module_put(ops->owner);
122 }
Roopa Prabhu499a2422015-07-21 10:43:46 +0200123 rcu_read_unlock();
124
125 return ret;
126}
127EXPORT_SYMBOL(lwtunnel_build_state);
128
David Ahern9ed59592017-01-17 14:57:36 -0800129int lwtunnel_valid_encap_type(u16 encap_type)
130{
131 const struct lwtunnel_encap_ops *ops;
132 int ret = -EINVAL;
133
134 if (encap_type == LWTUNNEL_ENCAP_NONE ||
135 encap_type > LWTUNNEL_ENCAP_MAX)
136 return ret;
137
138 rcu_read_lock();
139 ops = rcu_dereference(lwtun_encaps[encap_type]);
140 rcu_read_unlock();
141#ifdef CONFIG_MODULES
142 if (!ops) {
143 const char *encap_type_str = lwtunnel_encap_str(encap_type);
144
145 if (encap_type_str) {
146 __rtnl_unlock();
147 request_module("rtnl-lwt-%s", encap_type_str);
148 rtnl_lock();
149
150 rcu_read_lock();
151 ops = rcu_dereference(lwtun_encaps[encap_type]);
152 rcu_read_unlock();
153 }
154 }
155#endif
156 return ops ? 0 : -EOPNOTSUPP;
157}
158EXPORT_SYMBOL(lwtunnel_valid_encap_type);
159
160int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining)
161{
162 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
163 struct nlattr *nla_entype;
164 struct nlattr *attrs;
David Ahern9ed59592017-01-17 14:57:36 -0800165 u16 encap_type;
166 int attrlen;
167
168 while (rtnh_ok(rtnh, remaining)) {
169 attrlen = rtnh_attrlen(rtnh);
170 if (attrlen > 0) {
171 attrs = rtnh_attrs(rtnh);
David Ahern9ed59592017-01-17 14:57:36 -0800172 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
173
174 if (nla_entype) {
175 encap_type = nla_get_u16(nla_entype);
176
177 if (lwtunnel_valid_encap_type(encap_type) != 0)
178 return -EOPNOTSUPP;
179 }
180 }
181 rtnh = rtnh_next(rtnh, &remaining);
182 }
183
184 return 0;
185}
186EXPORT_SYMBOL(lwtunnel_valid_encap_type_attr);
187
Tom Herbert1104d9b2016-10-14 11:25:36 -0700188void lwtstate_free(struct lwtunnel_state *lws)
189{
190 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
191
192 if (ops->destroy_state) {
193 ops->destroy_state(lws);
194 kfree_rcu(lws, rcu);
195 } else {
196 kfree(lws);
197 }
Robert Shearman85c81402017-01-24 16:26:48 +0000198 module_put(ops->owner);
Tom Herbert1104d9b2016-10-14 11:25:36 -0700199}
200EXPORT_SYMBOL(lwtstate_free);
201
Roopa Prabhu499a2422015-07-21 10:43:46 +0200202int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate)
203{
204 const struct lwtunnel_encap_ops *ops;
205 struct nlattr *nest;
Dan Carpenter39f37092017-04-28 16:03:48 +0300206 int ret;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200207
208 if (!lwtstate)
209 return 0;
210
211 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
212 lwtstate->type > LWTUNNEL_ENCAP_MAX)
213 return 0;
214
Roopa Prabhu499a2422015-07-21 10:43:46 +0200215 nest = nla_nest_start(skb, RTA_ENCAP);
Pan Biana50fe0f2017-04-23 14:28:37 +0800216 if (!nest)
Dan Carpenter39f37092017-04-28 16:03:48 +0300217 return -EMSGSIZE;
218
219 ret = -EOPNOTSUPP;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200220 rcu_read_lock();
221 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
222 if (likely(ops && ops->fill_encap))
223 ret = ops->fill_encap(skb, lwtstate);
224 rcu_read_unlock();
225
226 if (ret)
227 goto nla_put_failure;
228 nla_nest_end(skb, nest);
229 ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type);
230 if (ret)
231 goto nla_put_failure;
232
233 return 0;
234
235nla_put_failure:
236 nla_nest_cancel(skb, nest);
237
238 return (ret == -EOPNOTSUPP ? 0 : ret);
239}
240EXPORT_SYMBOL(lwtunnel_fill_encap);
241
242int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
243{
244 const struct lwtunnel_encap_ops *ops;
245 int ret = 0;
246
247 if (!lwtstate)
248 return 0;
249
250 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
251 lwtstate->type > LWTUNNEL_ENCAP_MAX)
252 return 0;
253
254 rcu_read_lock();
255 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
256 if (likely(ops && ops->get_encap_size))
257 ret = nla_total_size(ops->get_encap_size(lwtstate));
258 rcu_read_unlock();
259
260 return ret;
261}
262EXPORT_SYMBOL(lwtunnel_get_encap_size);
263
264int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
265{
266 const struct lwtunnel_encap_ops *ops;
267 int ret = 0;
268
269 if (!a && !b)
270 return 0;
271
272 if (!a || !b)
273 return 1;
274
275 if (a->type != b->type)
276 return 1;
277
278 if (a->type == LWTUNNEL_ENCAP_NONE ||
279 a->type > LWTUNNEL_ENCAP_MAX)
280 return 0;
281
282 rcu_read_lock();
283 ops = rcu_dereference(lwtun_encaps[a->type]);
284 if (likely(ops && ops->cmp_encap))
285 ret = ops->cmp_encap(a, b);
286 rcu_read_unlock();
287
288 return ret;
289}
290EXPORT_SYMBOL(lwtunnel_cmp_encap);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200291
Eric W. Biedermanede20592015-10-07 16:48:47 -0500292int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200293{
Jiri Benc61adedf2015-08-20 13:56:25 +0200294 struct dst_entry *dst = skb_dst(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200295 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200296 struct lwtunnel_state *lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200297 int ret = -EINVAL;
298
Jiri Benc61adedf2015-08-20 13:56:25 +0200299 if (!dst)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200300 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200301 lwtstate = dst->lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200302
303 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
304 lwtstate->type > LWTUNNEL_ENCAP_MAX)
305 return 0;
306
307 ret = -EOPNOTSUPP;
308 rcu_read_lock();
309 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
310 if (likely(ops && ops->output))
Eric W. Biedermanede20592015-10-07 16:48:47 -0500311 ret = ops->output(net, sk, skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200312 rcu_read_unlock();
313
314 if (ret == -EOPNOTSUPP)
315 goto drop;
316
317 return ret;
318
319drop:
Dan Carpentere11f40b2015-07-27 11:07:47 +0300320 kfree_skb(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200321
322 return ret;
323}
Roopa Prabhuffce4192015-07-21 10:43:49 +0200324EXPORT_SYMBOL(lwtunnel_output);
Tom Herbert25368622015-08-17 13:42:24 -0700325
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700326int lwtunnel_xmit(struct sk_buff *skb)
327{
328 struct dst_entry *dst = skb_dst(skb);
329 const struct lwtunnel_encap_ops *ops;
330 struct lwtunnel_state *lwtstate;
331 int ret = -EINVAL;
332
333 if (!dst)
334 goto drop;
335
336 lwtstate = dst->lwtstate;
337
338 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
339 lwtstate->type > LWTUNNEL_ENCAP_MAX)
340 return 0;
341
342 ret = -EOPNOTSUPP;
343 rcu_read_lock();
344 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
345 if (likely(ops && ops->xmit))
346 ret = ops->xmit(skb);
347 rcu_read_unlock();
348
349 if (ret == -EOPNOTSUPP)
350 goto drop;
351
352 return ret;
353
354drop:
355 kfree_skb(skb);
356
357 return ret;
358}
359EXPORT_SYMBOL(lwtunnel_xmit);
360
Jiri Benc61adedf2015-08-20 13:56:25 +0200361int lwtunnel_input(struct sk_buff *skb)
Tom Herbert25368622015-08-17 13:42:24 -0700362{
Jiri Benc61adedf2015-08-20 13:56:25 +0200363 struct dst_entry *dst = skb_dst(skb);
Tom Herbert25368622015-08-17 13:42:24 -0700364 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200365 struct lwtunnel_state *lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700366 int ret = -EINVAL;
367
Jiri Benc61adedf2015-08-20 13:56:25 +0200368 if (!dst)
Tom Herbert25368622015-08-17 13:42:24 -0700369 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200370 lwtstate = dst->lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700371
372 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
373 lwtstate->type > LWTUNNEL_ENCAP_MAX)
374 return 0;
375
376 ret = -EOPNOTSUPP;
377 rcu_read_lock();
378 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
379 if (likely(ops && ops->input))
380 ret = ops->input(skb);
381 rcu_read_unlock();
382
383 if (ret == -EOPNOTSUPP)
384 goto drop;
385
386 return ret;
387
388drop:
389 kfree_skb(skb);
390
391 return ret;
392}
Tom Herbert25368622015-08-17 13:42:24 -0700393EXPORT_SYMBOL(lwtunnel_input);