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