Commit | Line | Data |
---|---|---|
499a2425 RP |
1 | /* |
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> | |
ffce4196 | 28 | #include <net/ip6_fib.h> |
9ed59592 | 29 | #include <net/nexthop.h> |
499a2425 | 30 | |
745041e2 RS |
31 | #ifdef CONFIG_MODULES |
32 | ||
33 | static 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"; | |
6c8702c6 DL |
43 | case LWTUNNEL_ENCAP_SEG6: |
44 | return "SEG6"; | |
3a0af8fd TG |
45 | case LWTUNNEL_ENCAP_BPF: |
46 | return "BPF"; | |
745041e2 RS |
47 | 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 | ||
499a2425 RP |
60 | struct 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 | } | |
68 | EXPORT_SYMBOL(lwtunnel_state_alloc); | |
69 | ||
92a99bf3 | 70 | static const struct lwtunnel_encap_ops __rcu * |
499a2425 RP |
71 | lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly; |
72 | ||
73 | int 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 | } | |
83 | EXPORT_SYMBOL(lwtunnel_encap_add_ops); | |
84 | ||
85 | int 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 | } | |
102 | EXPORT_SYMBOL(lwtunnel_encap_del_ops); | |
103 | ||
104 | int lwtunnel_build_state(struct net_device *dev, u16 encap_type, | |
127eb7cd TH |
105 | struct nlattr *encap, unsigned int family, |
106 | const void *cfg, struct lwtunnel_state **lws) | |
499a2425 RP |
107 | { |
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]); | |
9ed59592 DA |
118 | if (likely(ops && ops->build_state)) |
119 | ret = ops->build_state(dev, encap, family, cfg, lws); | |
120 | rcu_read_unlock(); | |
121 | ||
122 | return ret; | |
123 | } | |
124 | EXPORT_SYMBOL(lwtunnel_build_state); | |
125 | ||
126 | int lwtunnel_valid_encap_type(u16 encap_type) | |
127 | { | |
128 | const struct lwtunnel_encap_ops *ops; | |
129 | int ret = -EINVAL; | |
130 | ||
131 | if (encap_type == LWTUNNEL_ENCAP_NONE || | |
132 | encap_type > LWTUNNEL_ENCAP_MAX) | |
133 | return ret; | |
134 | ||
135 | rcu_read_lock(); | |
136 | ops = rcu_dereference(lwtun_encaps[encap_type]); | |
137 | rcu_read_unlock(); | |
745041e2 RS |
138 | #ifdef CONFIG_MODULES |
139 | if (!ops) { | |
140 | const char *encap_type_str = lwtunnel_encap_str(encap_type); | |
141 | ||
142 | if (encap_type_str) { | |
9ed59592 | 143 | __rtnl_unlock(); |
745041e2 | 144 | request_module("rtnl-lwt-%s", encap_type_str); |
9ed59592 DA |
145 | rtnl_lock(); |
146 | ||
745041e2 RS |
147 | rcu_read_lock(); |
148 | ops = rcu_dereference(lwtun_encaps[encap_type]); | |
9ed59592 | 149 | rcu_read_unlock(); |
745041e2 RS |
150 | } |
151 | } | |
152 | #endif | |
9ed59592 DA |
153 | return ops ? 0 : -EOPNOTSUPP; |
154 | } | |
155 | EXPORT_SYMBOL(lwtunnel_valid_encap_type); | |
499a2425 | 156 | |
9ed59592 DA |
157 | int lwtunnel_valid_encap_type_attr(struct nlattr *attr, int remaining) |
158 | { | |
159 | struct rtnexthop *rtnh = (struct rtnexthop *)attr; | |
160 | struct nlattr *nla_entype; | |
161 | struct nlattr *attrs; | |
162 | struct nlattr *nla; | |
163 | u16 encap_type; | |
164 | int attrlen; | |
165 | ||
166 | while (rtnh_ok(rtnh, remaining)) { | |
167 | attrlen = rtnh_attrlen(rtnh); | |
168 | if (attrlen > 0) { | |
169 | attrs = rtnh_attrs(rtnh); | |
170 | nla = nla_find(attrs, attrlen, RTA_ENCAP); | |
171 | nla_entype = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); | |
172 | ||
173 | if (nla_entype) { | |
174 | encap_type = nla_get_u16(nla_entype); | |
175 | ||
176 | if (lwtunnel_valid_encap_type(encap_type) != 0) | |
177 | return -EOPNOTSUPP; | |
178 | } | |
179 | } | |
180 | rtnh = rtnh_next(rtnh, &remaining); | |
181 | } | |
182 | ||
183 | return 0; | |
499a2425 | 184 | } |
9ed59592 | 185 | EXPORT_SYMBOL(lwtunnel_valid_encap_type_attr); |
499a2425 | 186 | |
1104d9ba TH |
187 | void lwtstate_free(struct lwtunnel_state *lws) |
188 | { | |
189 | const struct lwtunnel_encap_ops *ops = lwtun_encaps[lws->type]; | |
190 | ||
191 | if (ops->destroy_state) { | |
192 | ops->destroy_state(lws); | |
193 | kfree_rcu(lws, rcu); | |
194 | } else { | |
195 | kfree(lws); | |
196 | } | |
197 | } | |
198 | EXPORT_SYMBOL(lwtstate_free); | |
199 | ||
499a2425 RP |
200 | int lwtunnel_fill_encap(struct sk_buff *skb, struct lwtunnel_state *lwtstate) |
201 | { | |
202 | const struct lwtunnel_encap_ops *ops; | |
203 | struct nlattr *nest; | |
204 | int ret = -EINVAL; | |
205 | ||
206 | if (!lwtstate) | |
207 | return 0; | |
208 | ||
209 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || | |
210 | lwtstate->type > LWTUNNEL_ENCAP_MAX) | |
211 | return 0; | |
212 | ||
213 | ret = -EOPNOTSUPP; | |
214 | nest = nla_nest_start(skb, RTA_ENCAP); | |
215 | rcu_read_lock(); | |
216 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); | |
217 | if (likely(ops && ops->fill_encap)) | |
218 | ret = ops->fill_encap(skb, lwtstate); | |
219 | rcu_read_unlock(); | |
220 | ||
221 | if (ret) | |
222 | goto nla_put_failure; | |
223 | nla_nest_end(skb, nest); | |
224 | ret = nla_put_u16(skb, RTA_ENCAP_TYPE, lwtstate->type); | |
225 | if (ret) | |
226 | goto nla_put_failure; | |
227 | ||
228 | return 0; | |
229 | ||
230 | nla_put_failure: | |
231 | nla_nest_cancel(skb, nest); | |
232 | ||
233 | return (ret == -EOPNOTSUPP ? 0 : ret); | |
234 | } | |
235 | EXPORT_SYMBOL(lwtunnel_fill_encap); | |
236 | ||
237 | int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate) | |
238 | { | |
239 | const struct lwtunnel_encap_ops *ops; | |
240 | int ret = 0; | |
241 | ||
242 | if (!lwtstate) | |
243 | return 0; | |
244 | ||
245 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || | |
246 | lwtstate->type > LWTUNNEL_ENCAP_MAX) | |
247 | return 0; | |
248 | ||
249 | rcu_read_lock(); | |
250 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); | |
251 | if (likely(ops && ops->get_encap_size)) | |
252 | ret = nla_total_size(ops->get_encap_size(lwtstate)); | |
253 | rcu_read_unlock(); | |
254 | ||
255 | return ret; | |
256 | } | |
257 | EXPORT_SYMBOL(lwtunnel_get_encap_size); | |
258 | ||
259 | int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) | |
260 | { | |
261 | const struct lwtunnel_encap_ops *ops; | |
262 | int ret = 0; | |
263 | ||
264 | if (!a && !b) | |
265 | return 0; | |
266 | ||
267 | if (!a || !b) | |
268 | return 1; | |
269 | ||
270 | if (a->type != b->type) | |
271 | return 1; | |
272 | ||
273 | if (a->type == LWTUNNEL_ENCAP_NONE || | |
274 | a->type > LWTUNNEL_ENCAP_MAX) | |
275 | return 0; | |
276 | ||
277 | rcu_read_lock(); | |
278 | ops = rcu_dereference(lwtun_encaps[a->type]); | |
279 | if (likely(ops && ops->cmp_encap)) | |
280 | ret = ops->cmp_encap(a, b); | |
281 | rcu_read_unlock(); | |
282 | ||
283 | return ret; | |
284 | } | |
285 | EXPORT_SYMBOL(lwtunnel_cmp_encap); | |
ffce4196 | 286 | |
ede2059d | 287 | int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
ffce4196 | 288 | { |
61adedf3 | 289 | struct dst_entry *dst = skb_dst(skb); |
ffce4196 | 290 | const struct lwtunnel_encap_ops *ops; |
61adedf3 | 291 | struct lwtunnel_state *lwtstate; |
ffce4196 RP |
292 | int ret = -EINVAL; |
293 | ||
61adedf3 | 294 | if (!dst) |
ffce4196 | 295 | goto drop; |
61adedf3 | 296 | lwtstate = dst->lwtstate; |
ffce4196 RP |
297 | |
298 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || | |
299 | lwtstate->type > LWTUNNEL_ENCAP_MAX) | |
300 | return 0; | |
301 | ||
302 | ret = -EOPNOTSUPP; | |
303 | rcu_read_lock(); | |
304 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); | |
305 | if (likely(ops && ops->output)) | |
ede2059d | 306 | ret = ops->output(net, sk, skb); |
ffce4196 RP |
307 | rcu_read_unlock(); |
308 | ||
309 | if (ret == -EOPNOTSUPP) | |
310 | goto drop; | |
311 | ||
312 | return ret; | |
313 | ||
314 | drop: | |
e11f40b9 | 315 | kfree_skb(skb); |
ffce4196 RP |
316 | |
317 | return ret; | |
318 | } | |
ffce4196 | 319 | EXPORT_SYMBOL(lwtunnel_output); |
25368623 | 320 | |
14972cbd RP |
321 | int lwtunnel_xmit(struct sk_buff *skb) |
322 | { | |
323 | struct dst_entry *dst = skb_dst(skb); | |
324 | const struct lwtunnel_encap_ops *ops; | |
325 | struct lwtunnel_state *lwtstate; | |
326 | int ret = -EINVAL; | |
327 | ||
328 | if (!dst) | |
329 | goto drop; | |
330 | ||
331 | lwtstate = dst->lwtstate; | |
332 | ||
333 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || | |
334 | lwtstate->type > LWTUNNEL_ENCAP_MAX) | |
335 | return 0; | |
336 | ||
337 | ret = -EOPNOTSUPP; | |
338 | rcu_read_lock(); | |
339 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); | |
340 | if (likely(ops && ops->xmit)) | |
341 | ret = ops->xmit(skb); | |
342 | rcu_read_unlock(); | |
343 | ||
344 | if (ret == -EOPNOTSUPP) | |
345 | goto drop; | |
346 | ||
347 | return ret; | |
348 | ||
349 | drop: | |
350 | kfree_skb(skb); | |
351 | ||
352 | return ret; | |
353 | } | |
354 | EXPORT_SYMBOL(lwtunnel_xmit); | |
355 | ||
61adedf3 | 356 | int lwtunnel_input(struct sk_buff *skb) |
25368623 | 357 | { |
61adedf3 | 358 | struct dst_entry *dst = skb_dst(skb); |
25368623 | 359 | const struct lwtunnel_encap_ops *ops; |
61adedf3 | 360 | struct lwtunnel_state *lwtstate; |
25368623 TH |
361 | int ret = -EINVAL; |
362 | ||
61adedf3 | 363 | if (!dst) |
25368623 | 364 | goto drop; |
61adedf3 | 365 | lwtstate = dst->lwtstate; |
25368623 TH |
366 | |
367 | if (lwtstate->type == LWTUNNEL_ENCAP_NONE || | |
368 | lwtstate->type > LWTUNNEL_ENCAP_MAX) | |
369 | return 0; | |
370 | ||
371 | ret = -EOPNOTSUPP; | |
372 | rcu_read_lock(); | |
373 | ops = rcu_dereference(lwtun_encaps[lwtstate->type]); | |
374 | if (likely(ops && ops->input)) | |
375 | ret = ops->input(skb); | |
376 | rcu_read_unlock(); | |
377 | ||
378 | if (ret == -EOPNOTSUPP) | |
379 | goto drop; | |
380 | ||
381 | return ret; | |
382 | ||
383 | drop: | |
384 | kfree_skb(skb); | |
385 | ||
386 | return ret; | |
387 | } | |
25368623 | 388 | EXPORT_SYMBOL(lwtunnel_input); |