]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv6/seg6.c
netfilter: nft_ct: prepare for key-dependent error unwind
[mirror_ubuntu-artful-kernel.git] / net / ipv6 / seg6.c
CommitLineData
915d7e5e
DL
1/*
2 * SR-IPv6 implementation
3 *
4 * Author:
5 * David Lebrun <david.lebrun@uclouvain.be>
6 *
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/socket.h>
17#include <linux/net.h>
18#include <linux/in6.h>
19#include <linux/slab.h>
20
21#include <net/ipv6.h>
22#include <net/protocol.h>
23
24#include <net/seg6.h>
25#include <net/genetlink.h>
26#include <linux/seg6.h>
27#include <linux/seg6_genl.h>
4f4853dc
DL
28#ifdef CONFIG_IPV6_SEG6_HMAC
29#include <net/seg6_hmac.h>
30#endif
915d7e5e 31
6c8702c6
DL
32bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len)
33{
34 int trailing;
35 unsigned int tlv_offset;
36
37 if (srh->type != IPV6_SRCRT_TYPE_4)
38 return false;
39
40 if (((srh->hdrlen + 1) << 3) != len)
41 return false;
42
43 if (srh->segments_left != srh->first_segment)
44 return false;
45
46 tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4);
47
48 trailing = len - tlv_offset;
49 if (trailing < 0)
50 return false;
51
52 while (trailing) {
53 struct sr6_tlv *tlv;
54 unsigned int tlv_len;
55
56 tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset);
57 tlv_len = sizeof(*tlv) + tlv->len;
58
59 trailing -= tlv_len;
60 if (trailing < 0)
61 return false;
62
63 tlv_offset += tlv_len;
64 }
65
66 return true;
67}
68
915d7e5e
DL
69static struct genl_family seg6_genl_family;
70
71static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
72 [SEG6_ATTR_DST] = { .type = NLA_BINARY,
73 .len = sizeof(struct in6_addr) },
74 [SEG6_ATTR_DSTLEN] = { .type = NLA_S32, },
75 [SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, },
76 [SEG6_ATTR_SECRET] = { .type = NLA_BINARY, },
77 [SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, },
78 [SEG6_ATTR_ALGID] = { .type = NLA_U8, },
79 [SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, },
80};
81
4f4853dc
DL
82#ifdef CONFIG_IPV6_SEG6_HMAC
83
84static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
85{
86 struct net *net = genl_info_net(info);
87 struct seg6_pernet_data *sdata;
88 struct seg6_hmac_info *hinfo;
89 u32 hmackeyid;
90 char *secret;
91 int err = 0;
92 u8 algid;
93 u8 slen;
94
95 sdata = seg6_pernet(net);
96
97 if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
98 !info->attrs[SEG6_ATTR_SECRETLEN] ||
99 !info->attrs[SEG6_ATTR_ALGID])
100 return -EINVAL;
101
102 hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
103 slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
104 algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
105
106 if (hmackeyid == 0)
107 return -EINVAL;
108
109 if (slen > SEG6_HMAC_SECRET_LEN)
110 return -EINVAL;
111
112 mutex_lock(&sdata->lock);
113 hinfo = seg6_hmac_info_lookup(net, hmackeyid);
114
115 if (!slen) {
116 if (!hinfo)
117 err = -ENOENT;
118
119 err = seg6_hmac_info_del(net, hmackeyid);
120
121 goto out_unlock;
122 }
123
124 if (!info->attrs[SEG6_ATTR_SECRET]) {
125 err = -EINVAL;
126 goto out_unlock;
127 }
128
129 if (hinfo) {
130 err = seg6_hmac_info_del(net, hmackeyid);
131 if (err)
132 goto out_unlock;
133 }
134
135 secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
136
137 hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
138 if (!hinfo) {
139 err = -ENOMEM;
140 goto out_unlock;
141 }
142
143 memcpy(hinfo->secret, secret, slen);
144 hinfo->slen = slen;
145 hinfo->alg_id = algid;
146 hinfo->hmackeyid = hmackeyid;
147
148 err = seg6_hmac_info_add(net, hmackeyid, hinfo);
149 if (err)
150 kfree(hinfo);
151
152out_unlock:
153 mutex_unlock(&sdata->lock);
154 return err;
155}
156
157#else
158
915d7e5e
DL
159static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
160{
161 return -ENOTSUPP;
162}
163
4f4853dc
DL
164#endif
165
915d7e5e
DL
166static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
167{
168 struct net *net = genl_info_net(info);
169 struct in6_addr *val, *t_old, *t_new;
170 struct seg6_pernet_data *sdata;
171
172 sdata = seg6_pernet(net);
173
174 if (!info->attrs[SEG6_ATTR_DST])
175 return -EINVAL;
176
177 val = nla_data(info->attrs[SEG6_ATTR_DST]);
178 t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
e363116b
ED
179 if (!t_new)
180 return -ENOMEM;
915d7e5e
DL
181
182 mutex_lock(&sdata->lock);
183
184 t_old = sdata->tun_src;
185 rcu_assign_pointer(sdata->tun_src, t_new);
186
187 mutex_unlock(&sdata->lock);
188
189 synchronize_net();
190 kfree(t_old);
191
192 return 0;
193}
194
195static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
196{
197 struct net *net = genl_info_net(info);
198 struct in6_addr *tun_src;
199 struct sk_buff *msg;
200 void *hdr;
201
202 msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
203 if (!msg)
204 return -ENOMEM;
205
206 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
207 &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
208 if (!hdr)
209 goto free_msg;
210
211 rcu_read_lock();
212 tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
213
214 if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
215 goto nla_put_failure;
216
217 rcu_read_unlock();
218
219 genlmsg_end(msg, hdr);
220 genlmsg_reply(msg, info);
221
222 return 0;
223
224nla_put_failure:
225 rcu_read_unlock();
226 genlmsg_cancel(msg, hdr);
227free_msg:
228 nlmsg_free(msg);
229 return -ENOMEM;
230}
231
4f4853dc
DL
232#ifdef CONFIG_IPV6_SEG6_HMAC
233
234static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
235 struct sk_buff *msg)
236{
237 if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
238 nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
239 nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
240 nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
241 return -1;
242
243 return 0;
244}
245
246static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
247 u32 portid, u32 seq, u32 flags,
248 struct sk_buff *skb, u8 cmd)
249{
250 void *hdr;
251
252 hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
253 if (!hdr)
254 return -ENOMEM;
255
256 if (__seg6_hmac_fill_info(hinfo, skb) < 0)
257 goto nla_put_failure;
258
259 genlmsg_end(skb, hdr);
260 return 0;
261
262nla_put_failure:
263 genlmsg_cancel(skb, hdr);
264 return -EMSGSIZE;
265}
266
267static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
268{
269 struct net *net = sock_net(cb->skb->sk);
270 struct seg6_pernet_data *sdata;
271 struct rhashtable_iter *iter;
272
273 sdata = seg6_pernet(net);
274 iter = (struct rhashtable_iter *)cb->args[0];
275
276 if (!iter) {
277 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
278 if (!iter)
279 return -ENOMEM;
280
281 cb->args[0] = (long)iter;
282 }
283
284 rhashtable_walk_enter(&sdata->hmac_infos, iter);
285
286 return 0;
287}
288
289static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
290{
291 struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
292
293 rhashtable_walk_exit(iter);
294
295 kfree(iter);
296
297 return 0;
298}
299
300static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
301{
302 struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
303 struct net *net = sock_net(skb->sk);
304 struct seg6_pernet_data *sdata;
305 struct seg6_hmac_info *hinfo;
306 int ret;
307
308 sdata = seg6_pernet(net);
309
310 ret = rhashtable_walk_start(iter);
311 if (ret && ret != -EAGAIN)
312 goto done;
313
314 for (;;) {
315 hinfo = rhashtable_walk_next(iter);
316
317 if (IS_ERR(hinfo)) {
318 if (PTR_ERR(hinfo) == -EAGAIN)
319 continue;
320 ret = PTR_ERR(hinfo);
321 goto done;
322 } else if (!hinfo) {
323 break;
324 }
325
326 ret = __seg6_genl_dumphmac_element(hinfo,
327 NETLINK_CB(cb->skb).portid,
328 cb->nlh->nlmsg_seq,
329 NLM_F_MULTI,
330 skb, SEG6_CMD_DUMPHMAC);
331 if (ret)
332 goto done;
333 }
334
335 ret = skb->len;
336
337done:
338 rhashtable_walk_stop(iter);
339 return ret;
340}
341
342#else
343
344static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
345{
346 return 0;
347}
348
349static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
350{
351 return 0;
352}
353
915d7e5e
DL
354static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
355{
356 return -ENOTSUPP;
357}
358
4f4853dc
DL
359#endif
360
915d7e5e
DL
361static int __net_init seg6_net_init(struct net *net)
362{
363 struct seg6_pernet_data *sdata;
364
365 sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
366 if (!sdata)
367 return -ENOMEM;
368
369 mutex_init(&sdata->lock);
370
371 sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
372 if (!sdata->tun_src) {
373 kfree(sdata);
374 return -ENOMEM;
375 }
376
377 net->ipv6.seg6_data = sdata;
378
4f4853dc
DL
379#ifdef CONFIG_IPV6_SEG6_HMAC
380 seg6_hmac_net_init(net);
381#endif
382
915d7e5e
DL
383 return 0;
384}
385
386static void __net_exit seg6_net_exit(struct net *net)
387{
388 struct seg6_pernet_data *sdata = seg6_pernet(net);
389
4f4853dc
DL
390#ifdef CONFIG_IPV6_SEG6_HMAC
391 seg6_hmac_net_exit(net);
392#endif
393
915d7e5e
DL
394 kfree(sdata->tun_src);
395 kfree(sdata);
396}
397
398static struct pernet_operations ip6_segments_ops = {
399 .init = seg6_net_init,
400 .exit = seg6_net_exit,
401};
402
403static const struct genl_ops seg6_genl_ops[] = {
404 {
405 .cmd = SEG6_CMD_SETHMAC,
406 .doit = seg6_genl_sethmac,
407 .policy = seg6_genl_policy,
408 .flags = GENL_ADMIN_PERM,
409 },
410 {
411 .cmd = SEG6_CMD_DUMPHMAC,
4f4853dc 412 .start = seg6_genl_dumphmac_start,
915d7e5e 413 .dumpit = seg6_genl_dumphmac,
4f4853dc 414 .done = seg6_genl_dumphmac_done,
915d7e5e
DL
415 .policy = seg6_genl_policy,
416 .flags = GENL_ADMIN_PERM,
417 },
418 {
419 .cmd = SEG6_CMD_SET_TUNSRC,
420 .doit = seg6_genl_set_tunsrc,
421 .policy = seg6_genl_policy,
422 .flags = GENL_ADMIN_PERM,
423 },
424 {
425 .cmd = SEG6_CMD_GET_TUNSRC,
426 .doit = seg6_genl_get_tunsrc,
427 .policy = seg6_genl_policy,
428 .flags = GENL_ADMIN_PERM,
429 },
430};
431
432static struct genl_family seg6_genl_family __ro_after_init = {
433 .hdrsize = 0,
434 .name = SEG6_GENL_NAME,
435 .version = SEG6_GENL_VERSION,
436 .maxattr = SEG6_ATTR_MAX,
437 .netnsok = true,
438 .parallel_ops = true,
439 .ops = seg6_genl_ops,
440 .n_ops = ARRAY_SIZE(seg6_genl_ops),
441 .module = THIS_MODULE,
442};
443
444int __init seg6_init(void)
445{
446 int err = -ENOMEM;
447
448 err = genl_register_family(&seg6_genl_family);
449 if (err)
450 goto out;
451
452 err = register_pernet_subsys(&ip6_segments_ops);
453 if (err)
454 goto out_unregister_genl;
455
46738b13 456#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
6c8702c6
DL
457 err = seg6_iptunnel_init();
458 if (err)
459 goto out_unregister_pernet;
46738b13 460#endif
6c8702c6 461
4f4853dc
DL
462#ifdef CONFIG_IPV6_SEG6_HMAC
463 err = seg6_hmac_init();
464 if (err)
465 goto out_unregister_iptun;
466#endif
467
915d7e5e
DL
468 pr_info("Segment Routing with IPv6\n");
469
470out:
471 return err;
4f4853dc
DL
472#ifdef CONFIG_IPV6_SEG6_HMAC
473out_unregister_iptun:
46738b13 474#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
4f4853dc
DL
475 seg6_iptunnel_exit();
476#endif
46738b13
DL
477#endif
478#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
6c8702c6
DL
479out_unregister_pernet:
480 unregister_pernet_subsys(&ip6_segments_ops);
46738b13 481#endif
915d7e5e
DL
482out_unregister_genl:
483 genl_unregister_family(&seg6_genl_family);
484 goto out;
485}
486
487void seg6_exit(void)
488{
4f4853dc
DL
489#ifdef CONFIG_IPV6_SEG6_HMAC
490 seg6_hmac_exit();
491#endif
46738b13 492#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
6c8702c6 493 seg6_iptunnel_exit();
46738b13 494#endif
915d7e5e
DL
495 unregister_pernet_subsys(&ip6_segments_ops);
496 genl_unregister_family(&seg6_genl_family);
497}