1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * lwtunnel Infrastructure for light weight tunnels like mpls
4 *
5 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
6 */
7
8 #include <linux/capability.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/lwtunnel.h>
17 #include <linux/in.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20
21 #include <net/lwtunnel.h>
22 #include <net/rtnetlink.h>
23 #include <net/ip6_fib.h>
24 #include <net/rtnh.h>
25
26 #ifdef CONFIG_MODULES
27
lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)28 static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
29 {
30 /* Only lwt encaps implemented without using an interface for
31 * the encap need to return a string here.
32 */
33 switch (encap_type) {
34 case LWTUNNEL_ENCAP_MPLS:
35 return "MPLS";
36 case LWTUNNEL_ENCAP_ILA:
37 return "ILA";
38 case LWTUNNEL_ENCAP_SEG6:
39 return "SEG6";
40 case LWTUNNEL_ENCAP_BPF:
41 return "BPF";
42 case LWTUNNEL_ENCAP_SEG6_LOCAL:
43 return "SEG6LOCAL";
44 case LWTUNNEL_ENCAP_IP6:
45 case LWTUNNEL_ENCAP_IP:
46 case LWTUNNEL_ENCAP_NONE:
47 case __LWTUNNEL_ENCAP_MAX:
48 /* should not have got here */
49 WARN_ON(1);
50 break;
51 }
52 return NULL;
53 }
54
55 #endif /* CONFIG_MODULES */
56
lwtunnel_state_alloc(int encap_len)57 struct lwtunnel_state *lwtunnel_state_alloc(int encap_len)
58 {
59 struct lwtunnel_state *lws;
60
61 lws = kzalloc(sizeof(*lws) + encap_len, GFP_ATOMIC);
62
63 return lws;
64 }
65 EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
66
67 static const struct lwtunnel_encap_ops __rcu *
68 lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
69
lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops * ops,unsigned int num)70 int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
71 unsigned int num)
72 {
73 if (num > LWTUNNEL_ENCAP_MAX)
74 return -ERANGE;
75
76 return !cmpxchg((const struct lwtunnel_encap_ops **)
77 &lwtun_encaps[num],
78 NULL, ops) ? 0 : -1;
79 }
80 EXPORT_SYMBOL_GPL(lwtunnel_encap_add_ops);
81
lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops * ops,unsigned int encap_type)82 int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *ops,
83 unsigned int encap_type)
84 {
85 int ret;
86
87 if (encap_type == LWTUNNEL_ENCAP_NONE ||
88 encap_type > LWTUNNEL_ENCAP_MAX)
89 return -ERANGE;
90
91 ret = (cmpxchg((const struct lwtunnel_encap_ops **)
92 &lwtun_encaps[encap_type],
93 ops, NULL) == ops) ? 0 : -1;
94
95 synchronize_net();
96
97 return ret;
98 }
99 EXPORT_SYMBOL_GPL(lwtunnel_encap_del_ops);
100
lwtunnel_build_state(u16 encap_type,struct nlattr * encap,unsigned int family,const void * cfg,struct lwtunnel_state ** lws,struct netlink_ext_ack * extack)101 int lwtunnel_build_state(u16 encap_type,
102 struct nlattr *encap, unsigned int family,
103 const void *cfg, struct lwtunnel_state **lws,
104 struct netlink_ext_ack *extack)
105 {
106 const struct lwtunnel_encap_ops *ops;
107 bool found = false;
108 int ret = -EINVAL;
109
110 if (encap_type == LWTUNNEL_ENCAP_NONE ||
111 encap_type > LWTUNNEL_ENCAP_MAX) {
112 NL_SET_ERR_MSG_ATTR(extack, encap,
113 "Unknown LWT encapsulation type");
114 return ret;
115 }
116
117 ret = -EOPNOTSUPP;
118 rcu_read_lock();
119 ops = rcu_dereference(lwtun_encaps[encap_type]);
120 if (likely(ops && ops->build_state && try_module_get(ops->owner)))
121 found = true;
122 rcu_read_unlock();
123
124 if (found) {
125 ret = ops->build_state(encap, family, cfg, lws, extack);
126 if (ret)
127 module_put(ops->owner);
128 } else {
129 /* don't rely on -EOPNOTSUPP to detect match as build_state
130 * handlers could return it
131 */
132 NL_SET_ERR_MSG_ATTR(extack, encap,
133 "LWT encapsulation type not supported");
134 }
135
136 return ret;
137 }
138 EXPORT_SYMBOL_GPL(lwtunnel_build_state);
139
lwtunnel_valid_encap_type(u16 encap_type,struct netlink_ext_ack * extack)140 int lwtunnel_valid_encap_type(u16 encap_type, struct netlink_ext_ack *extack)
141 {
142 const struct lwtunnel_encap_ops *ops;
143 int ret = -EINVAL;
144
145 if (encap_type == LWTUNNEL_ENCAP_NONE ||
146 encap_type > LWTUNNEL_ENCAP_MAX) {
147 NL_SET_ERR_MSG(extack, "Unknown lwt encapsulation type");
148 return ret;
149 }
150
151 rcu_read_lock();
152 ops = rcu_dereference(lwtun_encaps[encap_type]);
153 rcu_read_unlock();
154 #ifdef CONFIG_MODULES
155 if (!ops) {
156 const char *encap_type_str = lwtunnel_encap_str(encap_type);
157
158 if (encap_type_str) {
159 __rtnl_unlock();
160 request_module("rtnl-lwt-%s", encap_type_str);
161 rtnl_lock();
162
163 rcu_read_lock();
164 ops = rcu_dereference(lwtun_encaps[encap_type]);
165 rcu_read_unlock();
166 }
167 }
168 #endif
169 ret = ops ? 0 : -EOPNOTSUPP;
170 if (ret < 0)
171 NL_SET_ERR_MSG(extack, "lwt encapsulation type not supported");
172
173 return ret;
174 }
175 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type);
176
lwtunnel_valid_encap_type_attr(struct nlattr * attr,int remaining,struct netlink_ext_ack * extack)177 int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining,
178 struct netlink_ext_ack *extack)
179 {
180 struct rtnexthop *rtnh = (struct rtnexthop *)attr;
181 struct nlattr *nla_entype;
182 struct nlattr *attrs;
183 u16 encap_type;
184 int attrlen;
185
186 while (rtnh_ok(rtnh, remaining)) {
187 attrlen = rtnh_attrlen(rtnh);
188 if (attrlen > 0) {
189 attrs = rtnh_attrs(rtnh);
190 nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
191
192 if (nla_entype) {
193 if (nla_len(nla_entype) < sizeof(u16)) {
194 NL_SET_ERR_MSG(extack, "Invalid RTA_ENCAP_TYPE");
195 return -EINVAL;
196 }
197 encap_type = nla_get_u16(nla_entype);
198
199 if (lwtunnel_valid_encap_type(encap_type,
200 extack) != 0)
201 return -EOPNOTSUPP;
202 }
203 }
204 rtnh = rtnh_next(rtnh, &remaining);
205 }
206
207 return 0;
208 }
209 EXPORT_SYMBOL_GPL(lwtunnel_valid_encap_type_attr);
210
lwtstate_free(struct lwtunnel_state * lws)211 void lwtstate_free(struct lwtunnel_state *lws)
212 {
213 const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type];
214
215 if (ops->destroy_state) {
216 ops->destroy_state(lws);
217 kfree_rcu(lws, rcu);
218 } else {
219 kfree(lws);
220 }
221 module_put(ops->owner);
222 }
223 EXPORT_SYMBOL_GPL(lwtstate_free);
224
lwtunnel_fill_encap(struct sk_buff * skb,struct lwtunnel_state * lwtstate,int encap_attr,int encap_type_attr)225 int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate,
226 int encap_attr, int encap_type_attr)
227 {
228 const struct lwtunnel_encap_ops *ops;
229 struct nlattr *nest;
230 int ret;
231
232 if (!lwtstate)
233 return 0;
234
235 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
236 lwtstate->type > LWTUNNEL_ENCAP_MAX)
237 return 0;
238
239 nest = nla_nest_start_noflag(skb, encap_attr);
240 if (!nest)
241 return -EMSGSIZE;
242
243 ret = -EOPNOTSUPP;
244 rcu_read_lock();
245 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
246 if (likely(ops && ops->fill_encap))
247 ret = ops->fill_encap(skb, lwtstate);
248 rcu_read_unlock();
249
250 if (ret)
251 goto nla_put_failure;
252 nla_nest_end(skb, nest);
253 ret = nla_put_u16(skb, encap_type_attr, lwtstate->type);
254 if (ret)
255 goto nla_put_failure;
256
257 return 0;
258
259 nla_put_failure:
260 nla_nest_cancel(skb, nest);
261
262 return (ret == -EOPNOTSUPP ? 0 : ret);
263 }
264 EXPORT_SYMBOL_GPL(lwtunnel_fill_encap);
265
lwtunnel_get_encap_size(struct lwtunnel_state * lwtstate)266 int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
267 {
268 const struct lwtunnel_encap_ops *ops;
269 int ret = 0;
270
271 if (!lwtstate)
272 return 0;
273
274 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
275 lwtstate->type > LWTUNNEL_ENCAP_MAX)
276 return 0;
277
278 rcu_read_lock();
279 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
280 if (likely(ops && ops->get_encap_size))
281 ret = nla_total_size(ops->get_encap_size(lwtstate));
282 rcu_read_unlock();
283
284 return ret;
285 }
286 EXPORT_SYMBOL_GPL(lwtunnel_get_encap_size);
287
lwtunnel_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)288 int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
289 {
290 const struct lwtunnel_encap_ops *ops;
291 int ret = 0;
292
293 if (!a && !b)
294 return 0;
295
296 if (!a || !b)
297 return 1;
298
299 if (a->type != b->type)
300 return 1;
301
302 if (a->type == LWTUNNEL_ENCAP_NONE ||
303 a->type > LWTUNNEL_ENCAP_MAX)
304 return 0;
305
306 rcu_read_lock();
307 ops = rcu_dereference(lwtun_encaps[a->type]);
308 if (likely(ops && ops->cmp_encap))
309 ret = ops->cmp_encap(a, b);
310 rcu_read_unlock();
311
312 return ret;
313 }
314 EXPORT_SYMBOL_GPL(lwtunnel_cmp_encap);
315
lwtunnel_output(struct net * net,struct sock * sk,struct sk_buff * skb)316 int lwtunnel_output(struct net *net, struct sock *sk, 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 lwtstate = dst->lwtstate;
326
327 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
328 lwtstate->type > LWTUNNEL_ENCAP_MAX)
329 return 0;
330
331 ret = -EOPNOTSUPP;
332 rcu_read_lock();
333 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
334 if (likely(ops && ops->output))
335 ret = ops->output(net, sk, skb);
336 rcu_read_unlock();
337
338 if (ret == -EOPNOTSUPP)
339 goto drop;
340
341 return ret;
342
343 drop:
344 kfree_skb(skb);
345
346 return ret;
347 }
348 EXPORT_SYMBOL_GPL(lwtunnel_output);
349
lwtunnel_xmit(struct sk_buff * skb)350 int lwtunnel_xmit(struct sk_buff *skb)
351 {
352 struct dst_entry *dst = skb_dst(skb);
353 const struct lwtunnel_encap_ops *ops;
354 struct lwtunnel_state *lwtstate;
355 int ret = -EINVAL;
356
357 if (!dst)
358 goto drop;
359
360 lwtstate = dst->lwtstate;
361
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->xmit))
370 ret = ops->xmit(skb);
371 rcu_read_unlock();
372
373 if (ret == -EOPNOTSUPP)
374 goto drop;
375
376 return ret;
377
378 drop:
379 kfree_skb(skb);
380
381 return ret;
382 }
383 EXPORT_SYMBOL_GPL(lwtunnel_xmit);
384
lwtunnel_input(struct sk_buff * skb)385 int lwtunnel_input(struct sk_buff *skb)
386 {
387 struct dst_entry *dst = skb_dst(skb);
388 const struct lwtunnel_encap_ops *ops;
389 struct lwtunnel_state *lwtstate;
390 int ret = -EINVAL;
391
392 if (!dst)
393 goto drop;
394 lwtstate = dst->lwtstate;
395
396 if (lwtstate->type == LWTUNNEL_ENCAP_NONE ||
397 lwtstate->type > LWTUNNEL_ENCAP_MAX)
398 return 0;
399
400 ret = -EOPNOTSUPP;
401 rcu_read_lock();
402 ops = rcu_dereference(lwtun_encaps[lwtstate->type]);
403 if (likely(ops && ops->input))
404 ret = ops->input(skb);
405 rcu_read_unlock();
406
407 if (ret == -EOPNOTSUPP)
408 goto drop;
409
410 return ret;
411
412 drop:
413 kfree_skb(skb);
414
415 return ret;
416 }
417 EXPORT_SYMBOL_GPL(lwtunnel_input);
418