blob: 69e249fbc02f8363141f07a723fe52d77653ea96 [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 Ahern3c618c12019-04-20 09:28:20 -070029#include <net/rtnh.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";
David Lebrund1df6fd2017-08-05 12:38:26 +020047 case LWTUNNEL_ENCAP_SEG6_LOCAL:
48 return "SEG6LOCAL";
Robert Shearman745041e2016-02-19 09:43:16 +000049 case LWTUNNEL_ENCAP_IP6:
50 case LWTUNNEL_ENCAP_IP:
51 case LWTUNNEL_ENCAP_NONE:
52 case __LWTUNNEL_ENCAP_MAX:
53 /* should not have got here */
54 WARN_ON(1);
55 break;
56 }
57 return NULL;
58}
59
60#endif /* CONFIG_MODULES */
61
Roopa Prabhu499a2422015-07-21 10:43:46 +020062struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
63{
64 struct lwtunnel_state *lws;
65
66 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
67
68 return lws;
69}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -070070EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
Roopa Prabhu499a2422015-07-21 10:43:46 +020071
Thomas Graf92a99bf2015-07-29 09:45:40 +020072static const struct lwtunnel_encap_ops __rcu *
Roopa Prabhu499a2422015-07-21 10:43:46 +020073 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
74
75int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
76 unsigned int num)
77{
78 if (num > LWTUNNEL_ENCAP_MAX)
79 return -ERANGE;
80
81 return !cmpxchg((const struct lwtunnel_encap_ops **)
82 &lwtun_encaps[num],
83 NULL, ops) ? 0 : -1;
84}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -070085EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
Roopa Prabhu499a2422015-07-21 10:43:46 +020086
87int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
88 unsigned int encap_type)
89{
90 int ret;
91
92 if (encap_type == LWTUNNEL_ENCAP_NONE ||
93 encap_type > LWTUNNEL_ENCAP_MAX)
94 return -ERANGE;
95
96 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
97 &lwtun_encaps[encap_type],
98 ops, NULL) == ops) ? 0 : -1;
99
100 synchronize_net();
101
102 return ret;
103}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700104EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200105
David Ahern30357d72017-01-30 12:07:37 -0800106int lwtunnel_build_state(u16 encap_type,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700107 struct nlattr *encap, unsigned int family,
David Ahern9ae28722017-05-27 16:19:28 -0600108 const void *cfg, struct lwtunnel_state **lws,
109 struct netlink_ext_ack *extack)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200110{
111 const struct lwtunnel_encap_ops *ops;
David Ahern9ae28722017-05-27 16:19:28 -0600112 bool found = false;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200113 int ret = -EINVAL;
114
115 if (encap_type == LWTUNNEL_ENCAP_NONE ||
David Ahern9ae28722017-05-27 16:19:28 -0600116 encap_type > LWTUNNEL_ENCAP_MAX) {
117 NL_SET_ERR_MSG_ATTR(extack, encap,
118 "Unknown LWT encapsulation type");
Roopa Prabhu499a2422015-07-21 10:43:46 +0200119 return ret;
David Ahern9ae28722017-05-27 16:19:28 -0600120 }
Roopa Prabhu499a2422015-07-21 10:43:46 +0200121
122 ret = -EOPNOTSUPP;
123 rcu_read_lock();
124 ops = rcu_dereference(lwtun_encaps[encap_type]);
wenxu3d25eab2019-02-23 21:32:54 +0800125 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
David Ahern9ae28722017-05-27 16:19:28 -0600126 found = true;
wenxu3d25eab2019-02-23 21:32:54 +0800127 rcu_read_unlock();
128
129 if (found) {
David Ahern9ae28722017-05-27 16:19:28 -0600130 ret = ops->build_state(encap, family, cfg, lws, extack);
Robert Shearman85c81402017-01-24 16:26:48 +0000131 if (ret)
132 module_put(ops->owner);
wenxu3d25eab2019-02-23 21:32:54 +0800133 } else {
134 /* don't rely on -EOPNOTSUPP to detect match as build_state
135 * handlers could return it
136 */
David Ahern9ae28722017-05-27 16:19:28 -0600137 NL_SET_ERR_MSG_ATTR(extack, encap,
138 "LWT encapsulation type not supported");
139 }
140
Roopa Prabhu499a2422015-07-21 10:43:46 +0200141 return ret;
142}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700143EXPORT_SYMBOL_GPL(lwtunnel_build_state);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200144
David Ahernc255bd62017-05-27 16:19:27 -0600145int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
David Ahern9ed59592017-01-17 14:57:36 -0800146{
147 const struct lwtunnel_encap_ops *ops;
148 int ret = -EINVAL;
149
150 if (encap_type == LWTUNNEL_ENCAP_NONE ||
David Ahernc255bd62017-05-27 16:19:27 -0600151 encap_type > LWTUNNEL_ENCAP_MAX) {
152 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
David Ahern9ed59592017-01-17 14:57:36 -0800153 return ret;
David Ahernc255bd62017-05-27 16:19:27 -0600154 }
David Ahern9ed59592017-01-17 14:57:36 -0800155
156 rcu_read_lock();
157 ops = rcu_dereference(lwtun_encaps[encap_type]);
158 rcu_read_unlock();
159#ifdef CONFIG_MODULES
160 if (!ops) {
161 const char *encap_type_str = lwtunnel_encap_str(encap_type);
162
163 if (encap_type_str) {
164 __rtnl_unlock();
165 request_module("rtnl-lwt-%s", encap_type_str);
166 rtnl_lock();
167
168 rcu_read_lock();
169 ops = rcu_dereference(lwtun_encaps[encap_type]);
170 rcu_read_unlock();
171 }
172 }
173#endif
David Ahernc255bd62017-05-27 16:19:27 -0600174 ret = ops ? 0 : -EOPNOTSUPP;
175 if (ret < 0)
176 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
177
178 return ret;
David Ahern9ed59592017-01-17 14:57:36 -0800179}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700180EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
David Ahern9ed59592017-01-17 14:57:36 -0800181
David Ahernc255bd62017-05-27 16:19:27 -0600182int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
183 struct netlink_ext_ack *extack)
David Ahern9ed59592017-01-17 14:57:36 -0800184{
185 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
186 struct nlattr *nla_entype;
187 struct nlattr *attrs;
David Ahern9ed59592017-01-17 14:57:36 -0800188 u16 encap_type;
189 int attrlen;
190
191 while (rtnh_ok(rtnh, remaining)) {
192 attrlen = rtnh_attrlen(rtnh);
193 if (attrlen > 0) {
194 attrs = rtnh_attrs(rtnh);
David Ahern9ed59592017-01-17 14:57:36 -0800195 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
196
197 if (nla_entype) {
198 encap_type = nla_get_u16(nla_entype);
199
David Ahernc255bd62017-05-27 16:19:27 -0600200 if (lwtunnel_valid_encap_type(encap_type,
201 extack) != 0)
David Ahern9ed59592017-01-17 14:57:36 -0800202 return -EOPNOTSUPP;
203 }
204 }
205 rtnh = rtnh_next(rtnh, &remaining);
206 }
207
208 return 0;
209}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700210EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
David Ahern9ed59592017-01-17 14:57:36 -0800211
Tom Herbert1104d9b2016-10-14 11:25:36 -0700212void lwtstate_free(struct lwtunnel_state *lws)
213{
214 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
215
216 if (ops->destroy_state) {
217 ops->destroy_state(lws);
218 kfree_rcu(lws, rcu);
219 } else {
220 kfree(lws);
221 }
Robert Shearman85c81402017-01-24 16:26:48 +0000222 module_put(ops->owner);
Tom Herbert1104d9b2016-10-14 11:25:36 -0700223}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700224EXPORT_SYMBOL_GPL(lwtstate_free);
Tom Herbert1104d9b2016-10-14 11:25:36 -0700225
David Ahernffa8ce52019-04-23 08:23:41 -0700226int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
227 int encap_attr, int encap_type_attr)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200228{
229 const struct lwtunnel_encap_ops *ops;
230 struct nlattr *nest;
Dan Carpenter39f37092017-04-28 16:03:48 +0300231 int ret;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200232
233 if (!lwtstate)
234 return 0;
235
236 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
237 lwtstate->type > LWTUNNEL_ENCAP_MAX)
238 return 0;
239
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200240 nest = nla_nest_start_noflag(skb, encap_attr);
Pan Biana50fe0f2017-04-23 14:28:37 +0800241 if (!nest)
Dan Carpenter39f37092017-04-28 16:03:48 +0300242 return -EMSGSIZE;
243
244 ret = -EOPNOTSUPP;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200245 rcu_read_lock();
246 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
247 if (likely(ops && ops->fill_encap))
248 ret = ops->fill_encap(skb, lwtstate);
249 rcu_read_unlock();
250
251 if (ret)
252 goto nla_put_failure;
253 nla_nest_end(skb, nest);
David Ahernffa8ce52019-04-23 08:23:41 -0700254 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200255 if (ret)
256 goto nla_put_failure;
257
258 return 0;
259
260nla_put_failure:
261 nla_nest_cancel(skb, nest);
262
263 return (ret == -EOPNOTSUPP ? 0 : ret);
264}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700265EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200266
267int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
268{
269 const struct lwtunnel_encap_ops *ops;
270 int ret = 0;
271
272 if (!lwtstate)
273 return 0;
274
275 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
276 lwtstate->type > LWTUNNEL_ENCAP_MAX)
277 return 0;
278
279 rcu_read_lock();
280 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
281 if (likely(ops && ops->get_encap_size))
282 ret = nla_total_size(ops->get_encap_size(lwtstate));
283 rcu_read_unlock();
284
285 return ret;
286}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700287EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
Roopa Prabhu499a2422015-07-21 10:43:46 +0200288
289int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
290{
291 const struct lwtunnel_encap_ops *ops;
292 int ret = 0;
293
294 if (!a && !b)
295 return 0;
296
297 if (!a || !b)
298 return 1;
299
300 if (a->type != b->type)
301 return 1;
302
303 if (a->type == LWTUNNEL_ENCAP_NONE ||
304 a->type > LWTUNNEL_ENCAP_MAX)
305 return 0;
306
307 rcu_read_lock();
308 ops = rcu_dereference(lwtun_encaps[a->type]);
309 if (likely(ops && ops->cmp_encap))
310 ret = ops->cmp_encap(a, b);
311 rcu_read_unlock();
312
313 return ret;
314}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700315EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200316
Eric W. Biedermanede20592015-10-07 16:48:47 -0500317int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200318{
Jiri Benc61adedf2015-08-20 13:56:25 +0200319 struct dst_entry *dst = skb_dst(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200320 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200321 struct lwtunnel_state *lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200322 int ret = -EINVAL;
323
Jiri Benc61adedf2015-08-20 13:56:25 +0200324 if (!dst)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200325 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200326 lwtstate = dst->lwtstate;
Roopa Prabhuffce4192015-07-21 10:43:49 +0200327
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->output))
Eric W. Biedermanede20592015-10-07 16:48:47 -0500336 ret = ops->output(net, sk, skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200337 rcu_read_unlock();
338
339 if (ret == -EOPNOTSUPP)
340 goto drop;
341
342 return ret;
343
344drop:
Dan Carpentere11f40b2015-07-27 11:07:47 +0300345 kfree_skb(skb);
Roopa Prabhuffce4192015-07-21 10:43:49 +0200346
347 return ret;
348}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700349EXPORT_SYMBOL_GPL(lwtunnel_output);
Tom Herbert25368622015-08-17 13:42:24 -0700350
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700351int lwtunnel_xmit(struct sk_buff *skb)
352{
353 struct dst_entry *dst = skb_dst(skb);
354 const struct lwtunnel_encap_ops *ops;
355 struct lwtunnel_state *lwtstate;
356 int ret = -EINVAL;
357
358 if (!dst)
359 goto drop;
360
361 lwtstate = dst->lwtstate;
362
363 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
364 lwtstate->type > LWTUNNEL_ENCAP_MAX)
365 return 0;
366
367 ret = -EOPNOTSUPP;
368 rcu_read_lock();
369 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
370 if (likely(ops && ops->xmit))
371 ret = ops->xmit(skb);
372 rcu_read_unlock();
373
374 if (ret == -EOPNOTSUPP)
375 goto drop;
376
377 return ret;
378
379drop:
380 kfree_skb(skb);
381
382 return ret;
383}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700384EXPORT_SYMBOL_GPL(lwtunnel_xmit);
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700385
Jiri Benc61adedf2015-08-20 13:56:25 +0200386int lwtunnel_input(struct sk_buff *skb)
Tom Herbert25368622015-08-17 13:42:24 -0700387{
Jiri Benc61adedf2015-08-20 13:56:25 +0200388 struct dst_entry *dst = skb_dst(skb);
Tom Herbert25368622015-08-17 13:42:24 -0700389 const struct lwtunnel_encap_ops *ops;
Jiri Benc61adedf2015-08-20 13:56:25 +0200390 struct lwtunnel_state *lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700391 int ret = -EINVAL;
392
Jiri Benc61adedf2015-08-20 13:56:25 +0200393 if (!dst)
Tom Herbert25368622015-08-17 13:42:24 -0700394 goto drop;
Jiri Benc61adedf2015-08-20 13:56:25 +0200395 lwtstate = dst->lwtstate;
Tom Herbert25368622015-08-17 13:42:24 -0700396
397 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
398 lwtstate->type > LWTUNNEL_ENCAP_MAX)
399 return 0;
400
401 ret = -EOPNOTSUPP;
402 rcu_read_lock();
403 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
404 if (likely(ops && ops->input))
405 ret = ops->input(skb);
406 rcu_read_unlock();
407
408 if (ret == -EOPNOTSUPP)
409 goto drop;
410
411 return ret;
412
413drop:
414 kfree_skb(skb);
415
416 return ret;
417}
Roopa Prabhu08bd10f2017-08-04 18:19:18 -0700418EXPORT_SYMBOL_GPL(lwtunnel_input);