]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/xfrm/xfrm_user.c
[XFRM] netlink: Move algorithm length calculation to its own function
[mirror_ubuntu-zesty-kernel.git] / net / xfrm / xfrm_user.c
CommitLineData
1da177e4
LT
1/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
df71837d 10 *
1da177e4
LT
11 */
12
9409f38a 13#include <linux/crypto.h>
1da177e4
LT
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
1da177e4
LT
22#include <linux/rtnetlink.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
88fc2c84 29#include <net/netlink.h>
1da177e4 30#include <asm/uaccess.h>
e23c7194
MN
31#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
161a09e7 34#include <linux/audit.h>
1da177e4 35
c26445ac
TG
36static inline int alg_len(struct xfrm_algo *alg)
37{
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39}
40
1da177e4
LT
41static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
42{
43 struct rtattr *rt = xfrma[type - 1];
44 struct xfrm_algo *algp;
31c26852 45 int len;
1da177e4
LT
46
47 if (!rt)
48 return 0;
49
31c26852
HX
50 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
51 if (len < 0)
1da177e4
LT
52 return -EINVAL;
53
54 algp = RTA_DATA(rt);
31c26852 55
a716c119 56 len -= (algp->alg_key_len + 7U) / 8;
31c26852
HX
57 if (len < 0)
58 return -EINVAL;
59
1da177e4
LT
60 switch (type) {
61 case XFRMA_ALG_AUTH:
62 if (!algp->alg_key_len &&
63 strcmp(algp->alg_name, "digest_null") != 0)
64 return -EINVAL;
65 break;
66
67 case XFRMA_ALG_CRYPT:
68 if (!algp->alg_key_len &&
69 strcmp(algp->alg_name, "cipher_null") != 0)
70 return -EINVAL;
71 break;
72
73 case XFRMA_ALG_COMP:
74 /* Zero length keys are legal. */
75 break;
76
77 default:
78 return -EINVAL;
3ff50b79 79 }
1da177e4
LT
80
81 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
82 return 0;
83}
84
85static int verify_encap_tmpl(struct rtattr **xfrma)
86{
87 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
88 struct xfrm_encap_tmpl *encap;
89
90 if (!rt)
91 return 0;
92
93 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
94 return -EINVAL;
95
96 return 0;
97}
98
eb2971b6
MN
99static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
100 xfrm_address_t **addrp)
101{
102 struct rtattr *rt = xfrma[type - 1];
103
104 if (!rt)
105 return 0;
106
107 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
108 return -EINVAL;
109
110 if (addrp)
111 *addrp = RTA_DATA(rt);
112
113 return 0;
114}
df71837d
TJ
115
116static inline int verify_sec_ctx_len(struct rtattr **xfrma)
117{
118 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
119 struct xfrm_user_sec_ctx *uctx;
120 int len = 0;
121
122 if (!rt)
123 return 0;
124
125 if (rt->rta_len < sizeof(*uctx))
126 return -EINVAL;
127
128 uctx = RTA_DATA(rt);
129
df71837d
TJ
130 len += sizeof(struct xfrm_user_sec_ctx);
131 len += uctx->ctx_len;
132
133 if (uctx->len != len)
134 return -EINVAL;
135
136 return 0;
137}
138
139
1da177e4
LT
140static int verify_newsa_info(struct xfrm_usersa_info *p,
141 struct rtattr **xfrma)
142{
143 int err;
144
145 err = -EINVAL;
146 switch (p->family) {
147 case AF_INET:
148 break;
149
150 case AF_INET6:
151#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
152 break;
153#else
154 err = -EAFNOSUPPORT;
155 goto out;
156#endif
157
158 default:
159 goto out;
3ff50b79 160 }
1da177e4
LT
161
162 err = -EINVAL;
163 switch (p->id.proto) {
164 case IPPROTO_AH:
165 if (!xfrma[XFRMA_ALG_AUTH-1] ||
166 xfrma[XFRMA_ALG_CRYPT-1] ||
167 xfrma[XFRMA_ALG_COMP-1])
168 goto out;
169 break;
170
171 case IPPROTO_ESP:
172 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
173 !xfrma[XFRMA_ALG_CRYPT-1]) ||
174 xfrma[XFRMA_ALG_COMP-1])
175 goto out;
176 break;
177
178 case IPPROTO_COMP:
179 if (!xfrma[XFRMA_ALG_COMP-1] ||
180 xfrma[XFRMA_ALG_AUTH-1] ||
181 xfrma[XFRMA_ALG_CRYPT-1])
182 goto out;
183 break;
184
e23c7194
MN
185#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS:
187 case IPPROTO_ROUTING:
188 if (xfrma[XFRMA_ALG_COMP-1] ||
189 xfrma[XFRMA_ALG_AUTH-1] ||
190 xfrma[XFRMA_ALG_CRYPT-1] ||
191 xfrma[XFRMA_ENCAP-1] ||
192 xfrma[XFRMA_SEC_CTX-1] ||
193 !xfrma[XFRMA_COADDR-1])
194 goto out;
195 break;
196#endif
197
1da177e4
LT
198 default:
199 goto out;
3ff50b79 200 }
1da177e4
LT
201
202 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
203 goto out;
204 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
205 goto out;
206 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
207 goto out;
208 if ((err = verify_encap_tmpl(xfrma)))
209 goto out;
df71837d
TJ
210 if ((err = verify_sec_ctx_len(xfrma)))
211 goto out;
060f02a3
NT
212 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
213 goto out;
1da177e4
LT
214
215 err = -EINVAL;
216 switch (p->mode) {
7e49e6de
MN
217 case XFRM_MODE_TRANSPORT:
218 case XFRM_MODE_TUNNEL:
060f02a3 219 case XFRM_MODE_ROUTEOPTIMIZATION:
0a69452c 220 case XFRM_MODE_BEET:
1da177e4
LT
221 break;
222
223 default:
224 goto out;
3ff50b79 225 }
1da177e4
LT
226
227 err = 0;
228
229out:
230 return err;
231}
232
233static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
234 struct xfrm_algo_desc *(*get_byname)(char *, int),
235 struct rtattr *u_arg)
236{
237 struct rtattr *rta = u_arg;
238 struct xfrm_algo *p, *ualg;
239 struct xfrm_algo_desc *algo;
240
241 if (!rta)
242 return 0;
243
244 ualg = RTA_DATA(rta);
245
246 algo = get_byname(ualg->alg_name, 1);
247 if (!algo)
248 return -ENOSYS;
249 *props = algo->desc.sadb_alg_id;
250
c26445ac 251 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
1da177e4
LT
252 if (!p)
253 return -ENOMEM;
254
04ff1260 255 strcpy(p->alg_name, algo->name);
1da177e4
LT
256 *algpp = p;
257 return 0;
258}
259
260static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
261{
262 struct rtattr *rta = u_arg;
263 struct xfrm_encap_tmpl *p, *uencap;
264
265 if (!rta)
266 return 0;
267
268 uencap = RTA_DATA(rta);
cdbc6dae 269 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
1da177e4
LT
270 if (!p)
271 return -ENOMEM;
272
1da177e4
LT
273 *encapp = p;
274 return 0;
275}
276
df71837d 277
661697f7 278static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
df71837d 279{
df71837d
TJ
280 int len = 0;
281
282 if (xfrm_ctx) {
283 len += sizeof(struct xfrm_user_sec_ctx);
284 len += xfrm_ctx->ctx_len;
285 }
286 return len;
287}
288
289static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
290{
291 struct xfrm_user_sec_ctx *uctx;
292
293 if (!u_arg)
294 return 0;
295
296 uctx = RTA_DATA(u_arg);
297 return security_xfrm_state_alloc(x, uctx);
298}
299
060f02a3
NT
300static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
301{
302 struct rtattr *rta = u_arg;
303 xfrm_address_t *p, *uaddrp;
304
305 if (!rta)
306 return 0;
307
308 uaddrp = RTA_DATA(rta);
cdbc6dae 309 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
060f02a3
NT
310 if (!p)
311 return -ENOMEM;
312
060f02a3
NT
313 *addrpp = p;
314 return 0;
315}
316
1da177e4
LT
317static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
318{
319 memcpy(&x->id, &p->id, sizeof(x->id));
320 memcpy(&x->sel, &p->sel, sizeof(x->sel));
321 memcpy(&x->lft, &p->lft, sizeof(x->lft));
322 x->props.mode = p->mode;
323 x->props.replay_window = p->replay_window;
324 x->props.reqid = p->reqid;
325 x->props.family = p->family;
54489c14 326 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
1da177e4 327 x->props.flags = p->flags;
196b0036
HX
328
329 /*
330 * Set inner address family if the KM left it as zero.
331 * See comment in validate_tmpl.
332 */
333 if (!x->sel.family)
334 x->sel.family = p->family;
1da177e4
LT
335}
336
d51d081d
JHS
337/*
338 * someday when pfkey also has support, we could have the code
339 * somehow made shareable and move it to xfrm_state.c - JHS
340 *
341*/
342static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
343{
344 int err = - EINVAL;
345 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
346 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
347 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
348 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
349
350 if (rp) {
351 struct xfrm_replay_state *replay;
352 if (RTA_PAYLOAD(rp) < sizeof(*replay))
353 goto error;
354 replay = RTA_DATA(rp);
355 memcpy(&x->replay, replay, sizeof(*replay));
356 memcpy(&x->preplay, replay, sizeof(*replay));
357 }
358
359 if (lt) {
360 struct xfrm_lifetime_cur *ltime;
361 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
362 goto error;
363 ltime = RTA_DATA(lt);
364 x->curlft.bytes = ltime->bytes;
365 x->curlft.packets = ltime->packets;
366 x->curlft.add_time = ltime->add_time;
367 x->curlft.use_time = ltime->use_time;
368 }
369
370 if (et) {
371 if (RTA_PAYLOAD(et) < sizeof(u32))
372 goto error;
373 x->replay_maxage = *(u32*)RTA_DATA(et);
374 }
375
376 if (rt) {
377 if (RTA_PAYLOAD(rt) < sizeof(u32))
378 goto error;
379 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
380 }
381
382 return 0;
383error:
384 return err;
385}
386
1da177e4
LT
387static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
388 struct rtattr **xfrma,
389 int *errp)
390{
391 struct xfrm_state *x = xfrm_state_alloc();
392 int err = -ENOMEM;
393
394 if (!x)
395 goto error_no_put;
396
397 copy_from_user_state(x, p);
398
399 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
400 xfrm_aalg_get_byname,
401 xfrma[XFRMA_ALG_AUTH-1])))
402 goto error;
403 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
404 xfrm_ealg_get_byname,
405 xfrma[XFRMA_ALG_CRYPT-1])))
406 goto error;
407 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
408 xfrm_calg_get_byname,
409 xfrma[XFRMA_ALG_COMP-1])))
410 goto error;
411 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
412 goto error;
060f02a3
NT
413 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
414 goto error;
72cb6962 415 err = xfrm_init_state(x);
1da177e4
LT
416 if (err)
417 goto error;
418
df71837d
TJ
419 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
420 goto error;
421
1da177e4 422 x->km.seq = p->seq;
d51d081d
JHS
423 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
424 /* sysctl_xfrm_aevent_etime is in 100ms units */
425 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
426 x->preplay.bitmap = 0;
427 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
428 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
429
430 /* override default values from above */
431
432 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
433 if (err < 0)
434 goto error;
1da177e4
LT
435
436 return x;
437
438error:
439 x->km.state = XFRM_STATE_DEAD;
440 xfrm_state_put(x);
441error_no_put:
442 *errp = err;
443 return NULL;
444}
445
22e70050
CH
446static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
447 struct rtattr **xfrma)
1da177e4 448{
7b67c857 449 struct xfrm_usersa_info *p = nlmsg_data(nlh);
1da177e4
LT
450 struct xfrm_state *x;
451 int err;
26b15dad 452 struct km_event c;
1da177e4 453
22e70050 454 err = verify_newsa_info(p, xfrma);
1da177e4
LT
455 if (err)
456 return err;
457
22e70050 458 x = xfrm_state_construct(p, xfrma, &err);
1da177e4
LT
459 if (!x)
460 return err;
461
26b15dad 462 xfrm_state_hold(x);
1da177e4
LT
463 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
464 err = xfrm_state_add(x);
465 else
466 err = xfrm_state_update(x);
467
161a09e7
JL
468 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
469 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
470
1da177e4
LT
471 if (err < 0) {
472 x->km.state = XFRM_STATE_DEAD;
21380b81 473 __xfrm_state_put(x);
7d6dfe1f 474 goto out;
1da177e4
LT
475 }
476
26b15dad
JHS
477 c.seq = nlh->nlmsg_seq;
478 c.pid = nlh->nlmsg_pid;
f60f6b8f 479 c.event = nlh->nlmsg_type;
26b15dad
JHS
480
481 km_state_notify(x, &c);
7d6dfe1f 482out:
26b15dad 483 xfrm_state_put(x);
1da177e4
LT
484 return err;
485}
486
eb2971b6
MN
487static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
488 struct rtattr **xfrma,
489 int *errp)
490{
491 struct xfrm_state *x = NULL;
492 int err;
493
494 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
495 err = -ESRCH;
496 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
497 } else {
498 xfrm_address_t *saddr = NULL;
499
500 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
501 if (err)
502 goto out;
503
504 if (!saddr) {
505 err = -EINVAL;
506 goto out;
507 }
508
9abbffee 509 err = -ESRCH;
eb2971b6
MN
510 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
511 p->family);
512 }
513
514 out:
515 if (!x && errp)
516 *errp = err;
517 return x;
518}
519
22e70050
CH
520static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
521 struct rtattr **xfrma)
1da177e4
LT
522{
523 struct xfrm_state *x;
eb2971b6 524 int err = -ESRCH;
26b15dad 525 struct km_event c;
7b67c857 526 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1da177e4 527
22e70050 528 x = xfrm_user_state_lookup(p, xfrma, &err);
1da177e4 529 if (x == NULL)
eb2971b6 530 return err;
1da177e4 531
6f68dc37 532 if ((err = security_xfrm_state_delete(x)) != 0)
c8c05a8e
CZ
533 goto out;
534
1da177e4 535 if (xfrm_state_kern(x)) {
c8c05a8e
CZ
536 err = -EPERM;
537 goto out;
1da177e4
LT
538 }
539
26b15dad 540 err = xfrm_state_delete(x);
161a09e7 541
c8c05a8e
CZ
542 if (err < 0)
543 goto out;
26b15dad
JHS
544
545 c.seq = nlh->nlmsg_seq;
546 c.pid = nlh->nlmsg_pid;
f60f6b8f 547 c.event = nlh->nlmsg_type;
26b15dad 548 km_state_notify(x, &c);
1da177e4 549
c8c05a8e 550out:
16bec31d
EP
551 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
552 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
c8c05a8e 553 xfrm_state_put(x);
26b15dad 554 return err;
1da177e4
LT
555}
556
557static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
558{
559 memcpy(&p->id, &x->id, sizeof(p->id));
560 memcpy(&p->sel, &x->sel, sizeof(p->sel));
561 memcpy(&p->lft, &x->lft, sizeof(p->lft));
562 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
563 memcpy(&p->stats, &x->stats, sizeof(p->stats));
54489c14 564 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
1da177e4
LT
565 p->mode = x->props.mode;
566 p->replay_window = x->props.replay_window;
567 p->reqid = x->props.reqid;
568 p->family = x->props.family;
569 p->flags = x->props.flags;
570 p->seq = x->km.seq;
571}
572
573struct xfrm_dump_info {
574 struct sk_buff *in_skb;
575 struct sk_buff *out_skb;
576 u32 nlmsg_seq;
577 u16 nlmsg_flags;
578 int start_idx;
579 int this_idx;
580};
581
c0144bea
TG
582static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
583{
584 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
585 struct xfrm_user_sec_ctx *uctx;
586 struct nlattr *attr;
587
588 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
589 if (attr == NULL)
590 return -EMSGSIZE;
591
592 uctx = nla_data(attr);
593 uctx->exttype = XFRMA_SEC_CTX;
594 uctx->len = ctx_size;
595 uctx->ctx_doi = s->ctx_doi;
596 uctx->ctx_alg = s->ctx_alg;
597 uctx->ctx_len = s->ctx_len;
598 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
599
600 return 0;
601}
602
1da177e4
LT
603static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
604{
605 struct xfrm_dump_info *sp = ptr;
606 struct sk_buff *in_skb = sp->in_skb;
607 struct sk_buff *skb = sp->out_skb;
608 struct xfrm_usersa_info *p;
609 struct nlmsghdr *nlh;
1da177e4
LT
610
611 if (sp->this_idx < sp->start_idx)
612 goto out;
613
79b8b7f4
TG
614 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
615 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
616 if (nlh == NULL)
617 return -EMSGSIZE;
1da177e4 618
7b67c857 619 p = nlmsg_data(nlh);
1da177e4
LT
620 copy_to_user_state(x, p);
621
622 if (x->aalg)
c26445ac 623 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
1da177e4 624 if (x->ealg)
c26445ac 625 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
1da177e4 626 if (x->calg)
c0144bea 627 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1da177e4
LT
628
629 if (x->encap)
c0144bea 630 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1da177e4 631
c0144bea
TG
632 if (x->security && copy_sec_ctx(x->security, skb) < 0)
633 goto nla_put_failure;
060f02a3
NT
634
635 if (x->coaddr)
c0144bea 636 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
060f02a3 637
9afaca05 638 if (x->lastused)
c0144bea 639 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
9afaca05 640
9825069d 641 nlmsg_end(skb, nlh);
1da177e4
LT
642out:
643 sp->this_idx++;
644 return 0;
645
c0144bea 646nla_put_failure:
9825069d
TG
647 nlmsg_cancel(skb, nlh);
648 return -EMSGSIZE;
1da177e4
LT
649}
650
651static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
652{
653 struct xfrm_dump_info info;
654
655 info.in_skb = cb->skb;
656 info.out_skb = skb;
657 info.nlmsg_seq = cb->nlh->nlmsg_seq;
658 info.nlmsg_flags = NLM_F_MULTI;
659 info.this_idx = 0;
660 info.start_idx = cb->args[0];
dc00a525 661 (void) xfrm_state_walk(0, dump_one_state, &info);
1da177e4
LT
662 cb->args[0] = info.this_idx;
663
664 return skb->len;
665}
666
667static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
668 struct xfrm_state *x, u32 seq)
669{
670 struct xfrm_dump_info info;
671 struct sk_buff *skb;
672
673 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
674 if (!skb)
675 return ERR_PTR(-ENOMEM);
676
1da177e4
LT
677 info.in_skb = in_skb;
678 info.out_skb = skb;
679 info.nlmsg_seq = seq;
680 info.nlmsg_flags = 0;
681 info.this_idx = info.start_idx = 0;
682
683 if (dump_one_state(x, 0, &info)) {
684 kfree_skb(skb);
685 return NULL;
686 }
687
688 return skb;
689}
690
ecfd6b18
JHS
691static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
692{
5a6d3416
JHS
693 struct xfrmk_spdinfo si;
694 struct xfrmu_spdinfo spc;
695 struct xfrmu_spdhinfo sph;
ecfd6b18
JHS
696 struct nlmsghdr *nlh;
697 u32 *f;
698
699 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
700 if (nlh == NULL) /* shouldnt really happen ... */
701 return -EMSGSIZE;
702
703 f = nlmsg_data(nlh);
704 *f = flags;
705 xfrm_spd_getinfo(&si);
5a6d3416
JHS
706 spc.incnt = si.incnt;
707 spc.outcnt = si.outcnt;
708 spc.fwdcnt = si.fwdcnt;
709 spc.inscnt = si.inscnt;
710 spc.outscnt = si.outscnt;
711 spc.fwdscnt = si.fwdscnt;
712 sph.spdhcnt = si.spdhcnt;
713 sph.spdhmcnt = si.spdhmcnt;
714
715 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
716 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
ecfd6b18
JHS
717
718 return nlmsg_end(skb, nlh);
719
720nla_put_failure:
721 nlmsg_cancel(skb, nlh);
722 return -EMSGSIZE;
723}
724
725static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
726 struct rtattr **xfrma)
727{
728 struct sk_buff *r_skb;
7b67c857 729 u32 *flags = nlmsg_data(nlh);
ecfd6b18
JHS
730 u32 spid = NETLINK_CB(skb).pid;
731 u32 seq = nlh->nlmsg_seq;
732 int len = NLMSG_LENGTH(sizeof(u32));
733
5a6d3416
JHS
734 len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
735 len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
ecfd6b18
JHS
736
737 r_skb = alloc_skb(len, GFP_ATOMIC);
738 if (r_skb == NULL)
739 return -ENOMEM;
740
741 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
742 BUG();
743
744 return nlmsg_unicast(xfrm_nl, r_skb, spid);
745}
746
28d8909b
JHS
747static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
748{
af11e316
JHS
749 struct xfrmk_sadinfo si;
750 struct xfrmu_sadhinfo sh;
28d8909b
JHS
751 struct nlmsghdr *nlh;
752 u32 *f;
753
754 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
755 if (nlh == NULL) /* shouldnt really happen ... */
756 return -EMSGSIZE;
757
758 f = nlmsg_data(nlh);
759 *f = flags;
760 xfrm_sad_getinfo(&si);
761
af11e316
JHS
762 sh.sadhmcnt = si.sadhmcnt;
763 sh.sadhcnt = si.sadhcnt;
764
765 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
766 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
28d8909b
JHS
767
768 return nlmsg_end(skb, nlh);
769
770nla_put_failure:
771 nlmsg_cancel(skb, nlh);
772 return -EMSGSIZE;
773}
774
775static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
776 struct rtattr **xfrma)
777{
778 struct sk_buff *r_skb;
7b67c857 779 u32 *flags = nlmsg_data(nlh);
28d8909b
JHS
780 u32 spid = NETLINK_CB(skb).pid;
781 u32 seq = nlh->nlmsg_seq;
782 int len = NLMSG_LENGTH(sizeof(u32));
783
af11e316
JHS
784 len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
785 len += RTA_SPACE(sizeof(u32));
28d8909b
JHS
786
787 r_skb = alloc_skb(len, GFP_ATOMIC);
788
789 if (r_skb == NULL)
790 return -ENOMEM;
791
792 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
793 BUG();
794
795 return nlmsg_unicast(xfrm_nl, r_skb, spid);
796}
797
22e70050
CH
798static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
799 struct rtattr **xfrma)
1da177e4 800{
7b67c857 801 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1da177e4
LT
802 struct xfrm_state *x;
803 struct sk_buff *resp_skb;
eb2971b6 804 int err = -ESRCH;
1da177e4 805
22e70050 806 x = xfrm_user_state_lookup(p, xfrma, &err);
1da177e4
LT
807 if (x == NULL)
808 goto out_noput;
809
810 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
811 if (IS_ERR(resp_skb)) {
812 err = PTR_ERR(resp_skb);
813 } else {
082a1ad5 814 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
1da177e4
LT
815 }
816 xfrm_state_put(x);
817out_noput:
818 return err;
819}
820
821static int verify_userspi_info(struct xfrm_userspi_info *p)
822{
823 switch (p->info.id.proto) {
824 case IPPROTO_AH:
825 case IPPROTO_ESP:
826 break;
827
828 case IPPROTO_COMP:
829 /* IPCOMP spi is 16-bits. */
830 if (p->max >= 0x10000)
831 return -EINVAL;
832 break;
833
834 default:
835 return -EINVAL;
3ff50b79 836 }
1da177e4
LT
837
838 if (p->min > p->max)
839 return -EINVAL;
840
841 return 0;
842}
843
22e70050
CH
844static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
845 struct rtattr **xfrma)
1da177e4
LT
846{
847 struct xfrm_state *x;
848 struct xfrm_userspi_info *p;
849 struct sk_buff *resp_skb;
850 xfrm_address_t *daddr;
851 int family;
852 int err;
853
7b67c857 854 p = nlmsg_data(nlh);
1da177e4
LT
855 err = verify_userspi_info(p);
856 if (err)
857 goto out_noput;
858
859 family = p->info.family;
860 daddr = &p->info.id.daddr;
861
862 x = NULL;
863 if (p->info.seq) {
864 x = xfrm_find_acq_byseq(p->info.seq);
865 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
866 xfrm_state_put(x);
867 x = NULL;
868 }
869 }
870
871 if (!x)
872 x = xfrm_find_acq(p->info.mode, p->info.reqid,
873 p->info.id.proto, daddr,
874 &p->info.saddr, 1,
875 family);
876 err = -ENOENT;
877 if (x == NULL)
878 goto out_noput;
879
880 resp_skb = ERR_PTR(-ENOENT);
881
882 spin_lock_bh(&x->lock);
883 if (x->km.state != XFRM_STATE_DEAD) {
884 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
885 if (x->id.spi)
886 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
887 }
888 spin_unlock_bh(&x->lock);
889
890 if (IS_ERR(resp_skb)) {
891 err = PTR_ERR(resp_skb);
892 goto out;
893 }
894
082a1ad5 895 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
1da177e4
LT
896
897out:
898 xfrm_state_put(x);
899out_noput:
900 return err;
901}
902
b798a9ed 903static int verify_policy_dir(u8 dir)
1da177e4
LT
904{
905 switch (dir) {
906 case XFRM_POLICY_IN:
907 case XFRM_POLICY_OUT:
908 case XFRM_POLICY_FWD:
909 break;
910
911 default:
912 return -EINVAL;
3ff50b79 913 }
1da177e4
LT
914
915 return 0;
916}
917
b798a9ed 918static int verify_policy_type(u8 type)
f7b6983f
MN
919{
920 switch (type) {
921 case XFRM_POLICY_TYPE_MAIN:
922#ifdef CONFIG_XFRM_SUB_POLICY
923 case XFRM_POLICY_TYPE_SUB:
924#endif
925 break;
926
927 default:
928 return -EINVAL;
3ff50b79 929 }
f7b6983f
MN
930
931 return 0;
932}
933
1da177e4
LT
934static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
935{
936 switch (p->share) {
937 case XFRM_SHARE_ANY:
938 case XFRM_SHARE_SESSION:
939 case XFRM_SHARE_USER:
940 case XFRM_SHARE_UNIQUE:
941 break;
942
943 default:
944 return -EINVAL;
3ff50b79 945 }
1da177e4
LT
946
947 switch (p->action) {
948 case XFRM_POLICY_ALLOW:
949 case XFRM_POLICY_BLOCK:
950 break;
951
952 default:
953 return -EINVAL;
3ff50b79 954 }
1da177e4
LT
955
956 switch (p->sel.family) {
957 case AF_INET:
958 break;
959
960 case AF_INET6:
961#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
962 break;
963#else
964 return -EAFNOSUPPORT;
965#endif
966
967 default:
968 return -EINVAL;
3ff50b79 969 }
1da177e4
LT
970
971 return verify_policy_dir(p->dir);
972}
973
df71837d
TJ
974static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
975{
976 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
977 struct xfrm_user_sec_ctx *uctx;
978
979 if (!rt)
980 return 0;
981
982 uctx = RTA_DATA(rt);
983 return security_xfrm_policy_alloc(pol, uctx);
984}
985
1da177e4
LT
986static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
987 int nr)
988{
989 int i;
990
991 xp->xfrm_nr = nr;
992 for (i = 0; i < nr; i++, ut++) {
993 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
994
995 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
996 memcpy(&t->saddr, &ut->saddr,
997 sizeof(xfrm_address_t));
998 t->reqid = ut->reqid;
999 t->mode = ut->mode;
1000 t->share = ut->share;
1001 t->optional = ut->optional;
1002 t->aalgos = ut->aalgos;
1003 t->ealgos = ut->ealgos;
1004 t->calgos = ut->calgos;
8511d01d 1005 t->encap_family = ut->family;
1da177e4
LT
1006 }
1007}
1008
b4ad86bf
DM
1009static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1010{
1011 int i;
1012
1013 if (nr > XFRM_MAX_DEPTH)
1014 return -EINVAL;
1015
1016 for (i = 0; i < nr; i++) {
1017 /* We never validated the ut->family value, so many
1018 * applications simply leave it at zero. The check was
1019 * never made and ut->family was ignored because all
1020 * templates could be assumed to have the same family as
1021 * the policy itself. Now that we will have ipv4-in-ipv6
1022 * and ipv6-in-ipv4 tunnels, this is no longer true.
1023 */
1024 if (!ut[i].family)
1025 ut[i].family = family;
1026
1027 switch (ut[i].family) {
1028 case AF_INET:
1029 break;
1030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1031 case AF_INET6:
1032 break;
1033#endif
1034 default:
1035 return -EINVAL;
3ff50b79 1036 }
b4ad86bf
DM
1037 }
1038
1039 return 0;
1040}
1041
1da177e4
LT
1042static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1043{
1044 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1da177e4
LT
1045
1046 if (!rt) {
1047 pol->xfrm_nr = 0;
1048 } else {
b4ad86bf
DM
1049 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1050 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1051 int err;
1da177e4 1052
b4ad86bf
DM
1053 err = validate_tmpl(nr, utmpl, pol->family);
1054 if (err)
1055 return err;
1da177e4
LT
1056
1057 copy_templates(pol, RTA_DATA(rt), nr);
1058 }
1059 return 0;
1060}
1061
f7b6983f
MN
1062static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1063{
1064 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1065 struct xfrm_userpolicy_type *upt;
b798a9ed 1066 u8 type = XFRM_POLICY_TYPE_MAIN;
f7b6983f
MN
1067 int err;
1068
1069 if (rt) {
1070 if (rt->rta_len < sizeof(*upt))
1071 return -EINVAL;
1072
1073 upt = RTA_DATA(rt);
1074 type = upt->type;
1075 }
1076
1077 err = verify_policy_type(type);
1078 if (err)
1079 return err;
1080
1081 *tp = type;
1082 return 0;
1083}
1084
1da177e4
LT
1085static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1086{
1087 xp->priority = p->priority;
1088 xp->index = p->index;
1089 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1090 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1091 xp->action = p->action;
1092 xp->flags = p->flags;
1093 xp->family = p->sel.family;
1094 /* XXX xp->share = p->share; */
1095}
1096
1097static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1098{
1099 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1100 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1101 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1102 p->priority = xp->priority;
1103 p->index = xp->index;
1104 p->sel.family = xp->family;
1105 p->dir = dir;
1106 p->action = xp->action;
1107 p->flags = xp->flags;
1108 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1109}
1110
1111static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1112{
1113 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1114 int err;
1115
1116 if (!xp) {
1117 *errp = -ENOMEM;
1118 return NULL;
1119 }
1120
1121 copy_from_user_policy(xp, p);
df71837d 1122
f7b6983f
MN
1123 err = copy_from_user_policy_type(&xp->type, xfrma);
1124 if (err)
1125 goto error;
1126
df71837d
TJ
1127 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1128 err = copy_from_user_sec_ctx(xp, xfrma);
f7b6983f
MN
1129 if (err)
1130 goto error;
1da177e4
LT
1131
1132 return xp;
f7b6983f
MN
1133 error:
1134 *errp = err;
1135 kfree(xp);
1136 return NULL;
1da177e4
LT
1137}
1138
22e70050
CH
1139static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1140 struct rtattr **xfrma)
1da177e4 1141{
7b67c857 1142 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1da177e4 1143 struct xfrm_policy *xp;
26b15dad 1144 struct km_event c;
1da177e4
LT
1145 int err;
1146 int excl;
1147
1148 err = verify_newpolicy_info(p);
df71837d
TJ
1149 if (err)
1150 return err;
22e70050 1151 err = verify_sec_ctx_len(xfrma);
1da177e4
LT
1152 if (err)
1153 return err;
1154
22e70050 1155 xp = xfrm_policy_construct(p, xfrma, &err);
1da177e4
LT
1156 if (!xp)
1157 return err;
1158
26b15dad
JHS
1159 /* shouldnt excl be based on nlh flags??
1160 * Aha! this is anti-netlink really i.e more pfkey derived
1161 * in netlink excl is a flag and you wouldnt need
1162 * a type XFRM_MSG_UPDPOLICY - JHS */
1da177e4
LT
1163 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1164 err = xfrm_policy_insert(p->dir, xp, excl);
161a09e7
JL
1165 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1166 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1167
1da177e4 1168 if (err) {
5f8ac64b 1169 security_xfrm_policy_free(xp);
1da177e4
LT
1170 kfree(xp);
1171 return err;
1172 }
1173
f60f6b8f 1174 c.event = nlh->nlmsg_type;
26b15dad
JHS
1175 c.seq = nlh->nlmsg_seq;
1176 c.pid = nlh->nlmsg_pid;
1177 km_policy_notify(xp, p->dir, &c);
1178
1da177e4
LT
1179 xfrm_pol_put(xp);
1180
1181 return 0;
1182}
1183
1184static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1185{
1186 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1187 int i;
1188
1189 if (xp->xfrm_nr == 0)
1190 return 0;
1191
1192 for (i = 0; i < xp->xfrm_nr; i++) {
1193 struct xfrm_user_tmpl *up = &vec[i];
1194 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1195
1196 memcpy(&up->id, &kp->id, sizeof(up->id));
8511d01d 1197 up->family = kp->encap_family;
1da177e4
LT
1198 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1199 up->reqid = kp->reqid;
1200 up->mode = kp->mode;
1201 up->share = kp->share;
1202 up->optional = kp->optional;
1203 up->aalgos = kp->aalgos;
1204 up->ealgos = kp->ealgos;
1205 up->calgos = kp->calgos;
1206 }
df71837d 1207
c0144bea
TG
1208 return nla_put(skb, XFRMA_TMPL,
1209 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
0d681623
SH
1210}
1211
1212static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1213{
1214 if (x->security) {
1215 return copy_sec_ctx(x->security, skb);
df71837d
TJ
1216 }
1217 return 0;
0d681623 1218}
df71837d 1219
0d681623
SH
1220static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1221{
1222 if (xp->security) {
1223 return copy_sec_ctx(xp->security, skb);
1224 }
1225 return 0;
df71837d
TJ
1226}
1227
f7b6983f 1228#ifdef CONFIG_XFRM_SUB_POLICY
b798a9ed 1229static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
f7b6983f 1230{
c0144bea
TG
1231 struct xfrm_userpolicy_type upt = {
1232 .type = type,
1233 };
f7b6983f 1234
c0144bea 1235 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
f7b6983f
MN
1236}
1237
1238#else
b798a9ed 1239static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
f7b6983f
MN
1240{
1241 return 0;
1242}
1243#endif
1244
1da177e4
LT
1245static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1246{
1247 struct xfrm_dump_info *sp = ptr;
1248 struct xfrm_userpolicy_info *p;
1249 struct sk_buff *in_skb = sp->in_skb;
1250 struct sk_buff *skb = sp->out_skb;
1251 struct nlmsghdr *nlh;
1da177e4
LT
1252
1253 if (sp->this_idx < sp->start_idx)
1254 goto out;
1255
79b8b7f4
TG
1256 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1257 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1258 if (nlh == NULL)
1259 return -EMSGSIZE;
1da177e4 1260
7b67c857 1261 p = nlmsg_data(nlh);
1da177e4
LT
1262 copy_to_user_policy(xp, p, dir);
1263 if (copy_to_user_tmpl(xp, skb) < 0)
1264 goto nlmsg_failure;
df71837d
TJ
1265 if (copy_to_user_sec_ctx(xp, skb))
1266 goto nlmsg_failure;
1459bb36 1267 if (copy_to_user_policy_type(xp->type, skb) < 0)
f7b6983f 1268 goto nlmsg_failure;
1da177e4 1269
9825069d 1270 nlmsg_end(skb, nlh);
1da177e4
LT
1271out:
1272 sp->this_idx++;
1273 return 0;
1274
1275nlmsg_failure:
9825069d
TG
1276 nlmsg_cancel(skb, nlh);
1277 return -EMSGSIZE;
1da177e4
LT
1278}
1279
1280static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1281{
1282 struct xfrm_dump_info info;
1283
1284 info.in_skb = cb->skb;
1285 info.out_skb = skb;
1286 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1287 info.nlmsg_flags = NLM_F_MULTI;
1288 info.this_idx = 0;
1289 info.start_idx = cb->args[0];
f7b6983f
MN
1290 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1291#ifdef CONFIG_XFRM_SUB_POLICY
1292 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1293#endif
1da177e4
LT
1294 cb->args[0] = info.this_idx;
1295
1296 return skb->len;
1297}
1298
1299static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1300 struct xfrm_policy *xp,
1301 int dir, u32 seq)
1302{
1303 struct xfrm_dump_info info;
1304 struct sk_buff *skb;
1305
1306 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1307 if (!skb)
1308 return ERR_PTR(-ENOMEM);
1309
1da177e4
LT
1310 info.in_skb = in_skb;
1311 info.out_skb = skb;
1312 info.nlmsg_seq = seq;
1313 info.nlmsg_flags = 0;
1314 info.this_idx = info.start_idx = 0;
1315
1316 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1317 kfree_skb(skb);
1318 return NULL;
1319 }
1320
1321 return skb;
1322}
1323
22e70050
CH
1324static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1325 struct rtattr **xfrma)
1da177e4
LT
1326{
1327 struct xfrm_policy *xp;
1328 struct xfrm_userpolicy_id *p;
b798a9ed 1329 u8 type = XFRM_POLICY_TYPE_MAIN;
1da177e4 1330 int err;
26b15dad 1331 struct km_event c;
1da177e4
LT
1332 int delete;
1333
7b67c857 1334 p = nlmsg_data(nlh);
1da177e4
LT
1335 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1336
22e70050 1337 err = copy_from_user_policy_type(&type, xfrma);
f7b6983f
MN
1338 if (err)
1339 return err;
1340
1da177e4
LT
1341 err = verify_policy_dir(p->dir);
1342 if (err)
1343 return err;
1344
1345 if (p->index)
ef41aaa0 1346 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
df71837d 1347 else {
22e70050 1348 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
df71837d
TJ
1349 struct xfrm_policy tmp;
1350
22e70050 1351 err = verify_sec_ctx_len(xfrma);
df71837d
TJ
1352 if (err)
1353 return err;
1354
1355 memset(&tmp, 0, sizeof(struct xfrm_policy));
1356 if (rt) {
1357 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1358
1359 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1360 return err;
1361 }
ef41aaa0
EP
1362 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1363 delete, &err);
df71837d
TJ
1364 security_xfrm_policy_free(&tmp);
1365 }
1da177e4
LT
1366 if (xp == NULL)
1367 return -ENOENT;
1368
1369 if (!delete) {
1370 struct sk_buff *resp_skb;
1371
1372 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1373 if (IS_ERR(resp_skb)) {
1374 err = PTR_ERR(resp_skb);
1375 } else {
082a1ad5
TG
1376 err = nlmsg_unicast(xfrm_nl, resp_skb,
1377 NETLINK_CB(skb).pid);
1da177e4 1378 }
26b15dad 1379 } else {
13fcfbb0
DM
1380 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1381 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1382
1383 if (err != 0)
c8c05a8e 1384 goto out;
13fcfbb0 1385
e7443892 1386 c.data.byid = p->index;
f60f6b8f 1387 c.event = nlh->nlmsg_type;
26b15dad
JHS
1388 c.seq = nlh->nlmsg_seq;
1389 c.pid = nlh->nlmsg_pid;
1390 km_policy_notify(xp, p->dir, &c);
1da177e4
LT
1391 }
1392
c8c05a8e 1393out:
ef41aaa0 1394 xfrm_pol_put(xp);
1da177e4
LT
1395 return err;
1396}
1397
22e70050
CH
1398static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1399 struct rtattr **xfrma)
1da177e4 1400{
26b15dad 1401 struct km_event c;
7b67c857 1402 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
161a09e7 1403 struct xfrm_audit audit_info;
4aa2e62c 1404 int err;
1da177e4 1405
161a09e7
JL
1406 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1407 audit_info.secid = NETLINK_CB(skb).sid;
4aa2e62c
JL
1408 err = xfrm_state_flush(p->proto, &audit_info);
1409 if (err)
1410 return err;
bf08867f 1411 c.data.proto = p->proto;
f60f6b8f 1412 c.event = nlh->nlmsg_type;
26b15dad
JHS
1413 c.seq = nlh->nlmsg_seq;
1414 c.pid = nlh->nlmsg_pid;
1415 km_state_notify(NULL, &c);
1416
1da177e4
LT
1417 return 0;
1418}
1419
d51d081d
JHS
1420
1421static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1422{
1423 struct xfrm_aevent_id *id;
1424 struct nlmsghdr *nlh;
d51d081d 1425
79b8b7f4
TG
1426 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1427 if (nlh == NULL)
1428 return -EMSGSIZE;
d51d081d 1429
7b67c857 1430 id = nlmsg_data(nlh);
2b5f6dcc 1431 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
d51d081d
JHS
1432 id->sa_id.spi = x->id.spi;
1433 id->sa_id.family = x->props.family;
1434 id->sa_id.proto = x->id.proto;
2b5f6dcc
JHS
1435 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1436 id->reqid = x->props.reqid;
d51d081d
JHS
1437 id->flags = c->data.aevent;
1438
c0144bea
TG
1439 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1440 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
d51d081d 1441
c0144bea
TG
1442 if (id->flags & XFRM_AE_RTHR)
1443 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
d51d081d 1444
c0144bea
TG
1445 if (id->flags & XFRM_AE_ETHR)
1446 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1447 x->replay_maxage * 10 / HZ);
d51d081d 1448
9825069d 1449 return nlmsg_end(skb, nlh);
d51d081d 1450
c0144bea 1451nla_put_failure:
9825069d
TG
1452 nlmsg_cancel(skb, nlh);
1453 return -EMSGSIZE;
d51d081d
JHS
1454}
1455
22e70050
CH
1456static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1457 struct rtattr **xfrma)
d51d081d
JHS
1458{
1459 struct xfrm_state *x;
1460 struct sk_buff *r_skb;
1461 int err;
1462 struct km_event c;
7b67c857 1463 struct xfrm_aevent_id *p = nlmsg_data(nlh);
d51d081d
JHS
1464 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1465 struct xfrm_usersa_id *id = &p->sa_id;
1466
1467 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1468 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1469
1470 if (p->flags&XFRM_AE_RTHR)
1471 len+=RTA_SPACE(sizeof(u32));
1472
1473 if (p->flags&XFRM_AE_ETHR)
1474 len+=RTA_SPACE(sizeof(u32));
1475
1476 r_skb = alloc_skb(len, GFP_ATOMIC);
1477 if (r_skb == NULL)
1478 return -ENOMEM;
1479
1480 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1481 if (x == NULL) {
b08d5840 1482 kfree_skb(r_skb);
d51d081d
JHS
1483 return -ESRCH;
1484 }
1485
1486 /*
1487 * XXX: is this lock really needed - none of the other
1488 * gets lock (the concern is things getting updated
1489 * while we are still reading) - jhs
1490 */
1491 spin_lock_bh(&x->lock);
1492 c.data.aevent = p->flags;
1493 c.seq = nlh->nlmsg_seq;
1494 c.pid = nlh->nlmsg_pid;
1495
1496 if (build_aevent(r_skb, x, &c) < 0)
1497 BUG();
082a1ad5 1498 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
d51d081d
JHS
1499 spin_unlock_bh(&x->lock);
1500 xfrm_state_put(x);
1501 return err;
1502}
1503
22e70050
CH
1504static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1505 struct rtattr **xfrma)
d51d081d
JHS
1506{
1507 struct xfrm_state *x;
1508 struct km_event c;
1509 int err = - EINVAL;
7b67c857 1510 struct xfrm_aevent_id *p = nlmsg_data(nlh);
d51d081d
JHS
1511 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1512 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1513
1514 if (!lt && !rp)
1515 return err;
1516
1517 /* pedantic mode - thou shalt sayeth replaceth */
1518 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1519 return err;
1520
1521 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1522 if (x == NULL)
1523 return -ESRCH;
1524
1525 if (x->km.state != XFRM_STATE_VALID)
1526 goto out;
1527
1528 spin_lock_bh(&x->lock);
22e70050 1529 err = xfrm_update_ae_params(x, xfrma);
d51d081d
JHS
1530 spin_unlock_bh(&x->lock);
1531 if (err < 0)
1532 goto out;
1533
1534 c.event = nlh->nlmsg_type;
1535 c.seq = nlh->nlmsg_seq;
1536 c.pid = nlh->nlmsg_pid;
1537 c.data.aevent = XFRM_AE_CU;
1538 km_state_notify(x, &c);
1539 err = 0;
1540out:
1541 xfrm_state_put(x);
1542 return err;
1543}
1544
22e70050
CH
1545static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1546 struct rtattr **xfrma)
1da177e4 1547{
f7b6983f 1548 struct km_event c;
b798a9ed 1549 u8 type = XFRM_POLICY_TYPE_MAIN;
f7b6983f 1550 int err;
161a09e7 1551 struct xfrm_audit audit_info;
f7b6983f 1552
22e70050 1553 err = copy_from_user_policy_type(&type, xfrma);
f7b6983f
MN
1554 if (err)
1555 return err;
26b15dad 1556
161a09e7
JL
1557 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1558 audit_info.secid = NETLINK_CB(skb).sid;
4aa2e62c
JL
1559 err = xfrm_policy_flush(type, &audit_info);
1560 if (err)
1561 return err;
f7b6983f 1562 c.data.type = type;
f60f6b8f 1563 c.event = nlh->nlmsg_type;
26b15dad
JHS
1564 c.seq = nlh->nlmsg_seq;
1565 c.pid = nlh->nlmsg_pid;
1566 km_policy_notify(NULL, 0, &c);
1da177e4
LT
1567 return 0;
1568}
1569
22e70050
CH
1570static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1571 struct rtattr **xfrma)
6c5c8ca7
JHS
1572{
1573 struct xfrm_policy *xp;
7b67c857 1574 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
6c5c8ca7 1575 struct xfrm_userpolicy_info *p = &up->pol;
b798a9ed 1576 u8 type = XFRM_POLICY_TYPE_MAIN;
6c5c8ca7
JHS
1577 int err = -ENOENT;
1578
22e70050 1579 err = copy_from_user_policy_type(&type, xfrma);
f7b6983f
MN
1580 if (err)
1581 return err;
1582
6c5c8ca7 1583 if (p->index)
ef41aaa0 1584 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
6c5c8ca7 1585 else {
22e70050 1586 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
6c5c8ca7
JHS
1587 struct xfrm_policy tmp;
1588
22e70050 1589 err = verify_sec_ctx_len(xfrma);
6c5c8ca7
JHS
1590 if (err)
1591 return err;
1592
1593 memset(&tmp, 0, sizeof(struct xfrm_policy));
1594 if (rt) {
1595 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1596
1597 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1598 return err;
1599 }
ef41aaa0
EP
1600 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1601 0, &err);
6c5c8ca7
JHS
1602 security_xfrm_policy_free(&tmp);
1603 }
1604
1605 if (xp == NULL)
ef41aaa0
EP
1606 return -ENOENT;
1607 read_lock(&xp->lock);
6c5c8ca7
JHS
1608 if (xp->dead) {
1609 read_unlock(&xp->lock);
1610 goto out;
1611 }
1612
1613 read_unlock(&xp->lock);
1614 err = 0;
1615 if (up->hard) {
1616 xfrm_policy_delete(xp, p->dir);
161a09e7
JL
1617 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1618 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1619
6c5c8ca7
JHS
1620 } else {
1621 // reset the timers here?
1622 printk("Dont know what to do with soft policy expire\n");
1623 }
1624 km_policy_expired(xp, p->dir, up->hard, current->pid);
1625
1626out:
1627 xfrm_pol_put(xp);
1628 return err;
1629}
1630
22e70050
CH
1631static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1632 struct rtattr **xfrma)
53bc6b4d
JHS
1633{
1634 struct xfrm_state *x;
1635 int err;
7b67c857 1636 struct xfrm_user_expire *ue = nlmsg_data(nlh);
53bc6b4d
JHS
1637 struct xfrm_usersa_info *p = &ue->state;
1638
1639 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
53bc6b4d 1640
3a765aa5 1641 err = -ENOENT;
53bc6b4d
JHS
1642 if (x == NULL)
1643 return err;
1644
53bc6b4d 1645 spin_lock_bh(&x->lock);
3a765aa5 1646 err = -EINVAL;
53bc6b4d
JHS
1647 if (x->km.state != XFRM_STATE_VALID)
1648 goto out;
1649 km_state_expired(x, ue->hard, current->pid);
1650
161a09e7 1651 if (ue->hard) {
53bc6b4d 1652 __xfrm_state_delete(x);
161a09e7
JL
1653 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1654 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1655 }
3a765aa5 1656 err = 0;
53bc6b4d
JHS
1657out:
1658 spin_unlock_bh(&x->lock);
1659 xfrm_state_put(x);
1660 return err;
1661}
1662
22e70050
CH
1663static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1664 struct rtattr **xfrma)
980ebd25
JHS
1665{
1666 struct xfrm_policy *xp;
1667 struct xfrm_user_tmpl *ut;
1668 int i;
1669 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1670
7b67c857 1671 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
980ebd25
JHS
1672 struct xfrm_state *x = xfrm_state_alloc();
1673 int err = -ENOMEM;
1674
1675 if (!x)
1676 return err;
1677
1678 err = verify_newpolicy_info(&ua->policy);
1679 if (err) {
1680 printk("BAD policy passed\n");
1681 kfree(x);
1682 return err;
1683 }
1684
1685 /* build an XP */
b4ad86bf
DM
1686 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1687 if (!xp) {
980ebd25
JHS
1688 kfree(x);
1689 return err;
1690 }
1691
1692 memcpy(&x->id, &ua->id, sizeof(ua->id));
1693 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1694 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1695
1696 ut = RTA_DATA(rt);
1697 /* extract the templates and for each call km_key */
1698 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1699 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1700 memcpy(&x->id, &t->id, sizeof(x->id));
1701 x->props.mode = t->mode;
1702 x->props.reqid = t->reqid;
1703 x->props.family = ut->family;
1704 t->aalgos = ua->aalgos;
1705 t->ealgos = ua->ealgos;
1706 t->calgos = ua->calgos;
1707 err = km_query(x, t, xp);
1708
1709 }
1710
1711 kfree(x);
1712 kfree(xp);
1713
1714 return 0;
1715}
1716
5c79de6e
SS
1717#ifdef CONFIG_XFRM_MIGRATE
1718static int verify_user_migrate(struct rtattr **xfrma)
1719{
1720 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1721 struct xfrm_user_migrate *um;
1722
1723 if (!rt)
1724 return -EINVAL;
1725
1726 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1727 return -EINVAL;
1728
1729 return 0;
1730}
1731
1732static int copy_from_user_migrate(struct xfrm_migrate *ma,
1733 struct rtattr **xfrma, int *num)
1734{
1735 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1736 struct xfrm_user_migrate *um;
1737 int i, num_migrate;
1738
1739 um = RTA_DATA(rt);
1740 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1741
1742 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1743 return -EINVAL;
1744
1745 for (i = 0; i < num_migrate; i++, um++, ma++) {
1746 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1747 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1748 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1749 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1750
1751 ma->proto = um->proto;
1752 ma->mode = um->mode;
1753 ma->reqid = um->reqid;
1754
1755 ma->old_family = um->old_family;
1756 ma->new_family = um->new_family;
1757 }
1758
1759 *num = i;
1760 return 0;
1761}
1762
1763static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1764 struct rtattr **xfrma)
1765{
7b67c857 1766 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
5c79de6e
SS
1767 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1768 u8 type;
1769 int err;
1770 int n = 0;
1771
1772 err = verify_user_migrate((struct rtattr **)xfrma);
1773 if (err)
1774 return err;
1775
1776 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1777 if (err)
1778 return err;
1779
1780 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1781 (struct rtattr **)xfrma, &n);
1782 if (err)
1783 return err;
1784
1785 if (!n)
1786 return 0;
1787
1788 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1789
1790 return 0;
1791}
1792#else
1793static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1794 struct rtattr **xfrma)
1795{
1796 return -ENOPROTOOPT;
1797}
1798#endif
1799
1800#ifdef CONFIG_XFRM_MIGRATE
1801static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1802{
1803 struct xfrm_user_migrate um;
1804
1805 memset(&um, 0, sizeof(um));
1806 um.proto = m->proto;
1807 um.mode = m->mode;
1808 um.reqid = m->reqid;
1809 um.old_family = m->old_family;
1810 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1811 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1812 um.new_family = m->new_family;
1813 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1814 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1815
c0144bea 1816 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
5c79de6e
SS
1817}
1818
1819static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1820 int num_migrate, struct xfrm_selector *sel,
1821 u8 dir, u8 type)
1822{
1823 struct xfrm_migrate *mp;
1824 struct xfrm_userpolicy_id *pol_id;
1825 struct nlmsghdr *nlh;
5c79de6e
SS
1826 int i;
1827
79b8b7f4
TG
1828 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1829 if (nlh == NULL)
1830 return -EMSGSIZE;
5c79de6e 1831
7b67c857 1832 pol_id = nlmsg_data(nlh);
5c79de6e
SS
1833 /* copy data from selector, dir, and type to the pol_id */
1834 memset(pol_id, 0, sizeof(*pol_id));
1835 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1836 pol_id->dir = dir;
1837
1838 if (copy_to_user_policy_type(type, skb) < 0)
1839 goto nlmsg_failure;
1840
1841 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1842 if (copy_to_user_migrate(mp, skb) < 0)
1843 goto nlmsg_failure;
1844 }
1845
9825069d 1846 return nlmsg_end(skb, nlh);
5c79de6e 1847nlmsg_failure:
9825069d
TG
1848 nlmsg_cancel(skb, nlh);
1849 return -EMSGSIZE;
5c79de6e
SS
1850}
1851
1852static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1853 struct xfrm_migrate *m, int num_migrate)
1854{
1855 struct sk_buff *skb;
1856 size_t len;
1857
1858 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1859 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1860#ifdef CONFIG_XFRM_SUB_POLICY
1861 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1862#endif
1863 skb = alloc_skb(len, GFP_ATOMIC);
1864 if (skb == NULL)
1865 return -ENOMEM;
1866
1867 /* build migrate */
1868 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1869 BUG();
1870
082a1ad5 1871 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
5c79de6e
SS
1872}
1873#else
1874static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1875 struct xfrm_migrate *m, int num_migrate)
1876{
1877 return -ENOPROTOOPT;
1878}
1879#endif
d51d081d 1880
492b558b
TG
1881#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1882
1883static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1884 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1885 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1886 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1887 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1888 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1889 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1890 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
980ebd25 1891 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
53bc6b4d 1892 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
492b558b
TG
1893 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1894 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
6c5c8ca7 1895 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
492b558b
TG
1896 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1897 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
d51d081d
JHS
1898 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1899 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
97a64b45 1900 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
5c79de6e 1901 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
566ec034 1902 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
ecfd6b18 1903 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
1da177e4
LT
1904};
1905
492b558b
TG
1906#undef XMSGSIZE
1907
1da177e4 1908static struct xfrm_link {
22e70050 1909 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
1da177e4 1910 int (*dump)(struct sk_buff *, struct netlink_callback *);
492b558b
TG
1911} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1912 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1913 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1914 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1915 .dump = xfrm_dump_sa },
1916 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1917 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1918 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1919 .dump = xfrm_dump_policy },
1920 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
980ebd25 1921 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
53bc6b4d 1922 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
492b558b
TG
1923 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1924 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
6c5c8ca7 1925 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
492b558b
TG
1926 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1927 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
d51d081d
JHS
1928 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1929 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
5c79de6e 1930 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
566ec034 1931 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
ecfd6b18 1932 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1da177e4
LT
1933};
1934
1d00a4eb 1935static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1da177e4
LT
1936{
1937 struct rtattr *xfrma[XFRMA_MAX];
1938 struct xfrm_link *link;
c702e804 1939 int type, min_len;
1da177e4 1940
1da177e4 1941 type = nlh->nlmsg_type;
1da177e4 1942 if (type > XFRM_MSG_MAX)
1d00a4eb 1943 return -EINVAL;
1da177e4
LT
1944
1945 type -= XFRM_MSG_BASE;
1946 link = &xfrm_dispatch[type];
1947
1948 /* All operations require privileges, even GET */
1d00a4eb
TG
1949 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1950 return -EPERM;
1da177e4 1951
492b558b
TG
1952 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1953 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1954 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1da177e4 1955 if (link->dump == NULL)
1d00a4eb 1956 return -EINVAL;
88fc2c84 1957
c702e804 1958 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1da177e4
LT
1959 }
1960
1961 memset(xfrma, 0, sizeof(xfrma));
1962
1963 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1d00a4eb 1964 return -EINVAL;
1da177e4
LT
1965
1966 if (nlh->nlmsg_len > min_len) {
1967 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1968 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1969
1970 while (RTA_OK(attr, attrlen)) {
1971 unsigned short flavor = attr->rta_type;
1972 if (flavor) {
1973 if (flavor > XFRMA_MAX)
1d00a4eb 1974 return -EINVAL;
1da177e4
LT
1975 xfrma[flavor - 1] = attr;
1976 }
1977 attr = RTA_NEXT(attr, attrlen);
1978 }
1979 }
1980
1981 if (link->doit == NULL)
1d00a4eb 1982 return -EINVAL;
1da177e4 1983
1d00a4eb 1984 return link->doit(skb, nlh, xfrma);
1da177e4
LT
1985}
1986
1da177e4
LT
1987static void xfrm_netlink_rcv(struct sock *sk, int len)
1988{
88fc2c84 1989 unsigned int qlen = 0;
2a0a6ebe 1990
1da177e4 1991 do {
4a3e2f71 1992 mutex_lock(&xfrm_cfg_mutex);
88fc2c84 1993 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
4a3e2f71 1994 mutex_unlock(&xfrm_cfg_mutex);
1da177e4 1995
2a0a6ebe 1996 } while (qlen);
1da177e4
LT
1997}
1998
d51d081d 1999static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1da177e4
LT
2000{
2001 struct xfrm_user_expire *ue;
2002 struct nlmsghdr *nlh;
1da177e4 2003
79b8b7f4
TG
2004 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2005 if (nlh == NULL)
2006 return -EMSGSIZE;
1da177e4 2007
7b67c857 2008 ue = nlmsg_data(nlh);
1da177e4 2009 copy_to_user_state(x, &ue->state);
d51d081d 2010 ue->hard = (c->data.hard != 0) ? 1 : 0;
1da177e4 2011
9825069d 2012 return nlmsg_end(skb, nlh);
1da177e4
LT
2013}
2014
26b15dad 2015static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1da177e4
LT
2016{
2017 struct sk_buff *skb;
ee57eef9 2018 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1da177e4 2019
ee57eef9 2020 skb = alloc_skb(len, GFP_ATOMIC);
1da177e4
LT
2021 if (skb == NULL)
2022 return -ENOMEM;
2023
d51d081d 2024 if (build_expire(skb, x, c) < 0)
1da177e4
LT
2025 BUG();
2026
082a1ad5 2027 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
2028}
2029
d51d081d
JHS
2030static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2031{
2032 struct sk_buff *skb;
2033 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2034
2035 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2036 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2037 skb = alloc_skb(len, GFP_ATOMIC);
2038 if (skb == NULL)
2039 return -ENOMEM;
2040
2041 if (build_aevent(skb, x, c) < 0)
2042 BUG();
2043
082a1ad5 2044 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
d51d081d
JHS
2045}
2046
26b15dad
JHS
2047static int xfrm_notify_sa_flush(struct km_event *c)
2048{
2049 struct xfrm_usersa_flush *p;
2050 struct nlmsghdr *nlh;
2051 struct sk_buff *skb;
26b15dad
JHS
2052 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2053
2054 skb = alloc_skb(len, GFP_ATOMIC);
2055 if (skb == NULL)
2056 return -ENOMEM;
26b15dad 2057
79b8b7f4
TG
2058 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2059 if (nlh == NULL) {
2060 kfree_skb(skb);
2061 return -EMSGSIZE;
2062 }
26b15dad 2063
7b67c857 2064 p = nlmsg_data(nlh);
bf08867f 2065 p->proto = c->data.proto;
26b15dad 2066
9825069d 2067 nlmsg_end(skb, nlh);
26b15dad 2068
082a1ad5 2069 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad
JHS
2070}
2071
b6f99a21 2072static inline int xfrm_sa_len(struct xfrm_state *x)
26b15dad 2073{
0603eac0 2074 int l = 0;
26b15dad 2075 if (x->aalg)
c26445ac 2076 l += RTA_SPACE(alg_len(x->aalg));
26b15dad 2077 if (x->ealg)
c26445ac 2078 l += RTA_SPACE(alg_len(x->ealg));
26b15dad
JHS
2079 if (x->calg)
2080 l += RTA_SPACE(sizeof(*x->calg));
2081 if (x->encap)
2082 l += RTA_SPACE(sizeof(*x->encap));
2083
2084 return l;
2085}
2086
2087static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2088{
2089 struct xfrm_usersa_info *p;
0603eac0 2090 struct xfrm_usersa_id *id;
26b15dad
JHS
2091 struct nlmsghdr *nlh;
2092 struct sk_buff *skb;
26b15dad 2093 int len = xfrm_sa_len(x);
0603eac0
HX
2094 int headlen;
2095
2096 headlen = sizeof(*p);
2097 if (c->event == XFRM_MSG_DELSA) {
2098 len += RTA_SPACE(headlen);
2099 headlen = sizeof(*id);
2100 }
2101 len += NLMSG_SPACE(headlen);
26b15dad
JHS
2102
2103 skb = alloc_skb(len, GFP_ATOMIC);
2104 if (skb == NULL)
2105 return -ENOMEM;
26b15dad 2106
79b8b7f4
TG
2107 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2108 if (nlh == NULL)
c0144bea 2109 goto nla_put_failure;
26b15dad 2110
7b67c857 2111 p = nlmsg_data(nlh);
0603eac0 2112 if (c->event == XFRM_MSG_DELSA) {
c0144bea
TG
2113 struct nlattr *attr;
2114
7b67c857 2115 id = nlmsg_data(nlh);
0603eac0
HX
2116 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2117 id->spi = x->id.spi;
2118 id->family = x->props.family;
2119 id->proto = x->id.proto;
2120
c0144bea
TG
2121 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2122 if (attr == NULL)
2123 goto nla_put_failure;
2124
2125 p = nla_data(attr);
0603eac0
HX
2126 }
2127
26b15dad
JHS
2128 copy_to_user_state(x, p);
2129
2130 if (x->aalg)
c26445ac 2131 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
26b15dad 2132 if (x->ealg)
c26445ac 2133 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
26b15dad 2134 if (x->calg)
c0144bea 2135 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
26b15dad
JHS
2136
2137 if (x->encap)
c0144bea 2138 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
26b15dad 2139
9825069d 2140 nlmsg_end(skb, nlh);
26b15dad 2141
082a1ad5 2142 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad 2143
c0144bea 2144nla_put_failure:
26b15dad
JHS
2145 kfree_skb(skb);
2146 return -1;
2147}
2148
2149static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2150{
2151
2152 switch (c->event) {
f60f6b8f 2153 case XFRM_MSG_EXPIRE:
26b15dad 2154 return xfrm_exp_state_notify(x, c);
d51d081d
JHS
2155 case XFRM_MSG_NEWAE:
2156 return xfrm_aevent_state_notify(x, c);
f60f6b8f
HX
2157 case XFRM_MSG_DELSA:
2158 case XFRM_MSG_UPDSA:
2159 case XFRM_MSG_NEWSA:
26b15dad 2160 return xfrm_notify_sa(x, c);
f60f6b8f 2161 case XFRM_MSG_FLUSHSA:
26b15dad
JHS
2162 return xfrm_notify_sa_flush(c);
2163 default:
2164 printk("xfrm_user: Unknown SA event %d\n", c->event);
2165 break;
2166 }
2167
2168 return 0;
2169
2170}
2171
1da177e4
LT
2172static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2173 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2174 int dir)
2175{
2176 struct xfrm_user_acquire *ua;
2177 struct nlmsghdr *nlh;
1da177e4
LT
2178 __u32 seq = xfrm_get_acqseq();
2179
79b8b7f4
TG
2180 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2181 if (nlh == NULL)
2182 return -EMSGSIZE;
1da177e4 2183
7b67c857 2184 ua = nlmsg_data(nlh);
1da177e4
LT
2185 memcpy(&ua->id, &x->id, sizeof(ua->id));
2186 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2187 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2188 copy_to_user_policy(xp, &ua->policy, dir);
2189 ua->aalgos = xt->aalgos;
2190 ua->ealgos = xt->ealgos;
2191 ua->calgos = xt->calgos;
2192 ua->seq = x->km.seq = seq;
2193
2194 if (copy_to_user_tmpl(xp, skb) < 0)
2195 goto nlmsg_failure;
0d681623 2196 if (copy_to_user_state_sec_ctx(x, skb))
df71837d 2197 goto nlmsg_failure;
1459bb36 2198 if (copy_to_user_policy_type(xp->type, skb) < 0)
f7b6983f 2199 goto nlmsg_failure;
1da177e4 2200
9825069d 2201 return nlmsg_end(skb, nlh);
1da177e4
LT
2202
2203nlmsg_failure:
9825069d
TG
2204 nlmsg_cancel(skb, nlh);
2205 return -EMSGSIZE;
1da177e4
LT
2206}
2207
2208static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2209 struct xfrm_policy *xp, int dir)
2210{
2211 struct sk_buff *skb;
2212 size_t len;
2213
2214 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2215 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
661697f7 2216 len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
785fd8b8
JHS
2217#ifdef CONFIG_XFRM_SUB_POLICY
2218 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2219#endif
1da177e4
LT
2220 skb = alloc_skb(len, GFP_ATOMIC);
2221 if (skb == NULL)
2222 return -ENOMEM;
2223
2224 if (build_acquire(skb, x, xt, xp, dir) < 0)
2225 BUG();
2226
082a1ad5 2227 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1da177e4
LT
2228}
2229
2230/* User gives us xfrm_user_policy_info followed by an array of 0
2231 * or more templates.
2232 */
cb969f07 2233static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
1da177e4
LT
2234 u8 *data, int len, int *dir)
2235{
2236 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2237 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2238 struct xfrm_policy *xp;
2239 int nr;
2240
cb969f07 2241 switch (sk->sk_family) {
1da177e4
LT
2242 case AF_INET:
2243 if (opt != IP_XFRM_POLICY) {
2244 *dir = -EOPNOTSUPP;
2245 return NULL;
2246 }
2247 break;
2248#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2249 case AF_INET6:
2250 if (opt != IPV6_XFRM_POLICY) {
2251 *dir = -EOPNOTSUPP;
2252 return NULL;
2253 }
2254 break;
2255#endif
2256 default:
2257 *dir = -EINVAL;
2258 return NULL;
2259 }
2260
2261 *dir = -EINVAL;
2262
2263 if (len < sizeof(*p) ||
2264 verify_newpolicy_info(p))
2265 return NULL;
2266
2267 nr = ((len - sizeof(*p)) / sizeof(*ut));
b4ad86bf 2268 if (validate_tmpl(nr, ut, p->sel.family))
1da177e4
LT
2269 return NULL;
2270
a4f1bac6
HX
2271 if (p->dir > XFRM_POLICY_OUT)
2272 return NULL;
2273
1da177e4
LT
2274 xp = xfrm_policy_alloc(GFP_KERNEL);
2275 if (xp == NULL) {
2276 *dir = -ENOBUFS;
2277 return NULL;
2278 }
2279
2280 copy_from_user_policy(xp, p);
f7b6983f 2281 xp->type = XFRM_POLICY_TYPE_MAIN;
1da177e4
LT
2282 copy_templates(xp, ut, nr);
2283
2284 *dir = p->dir;
2285
2286 return xp;
2287}
2288
2289static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
d51d081d 2290 int dir, struct km_event *c)
1da177e4
LT
2291{
2292 struct xfrm_user_polexpire *upe;
2293 struct nlmsghdr *nlh;
d51d081d 2294 int hard = c->data.hard;
1da177e4 2295
79b8b7f4
TG
2296 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2297 if (nlh == NULL)
2298 return -EMSGSIZE;
1da177e4 2299
7b67c857 2300 upe = nlmsg_data(nlh);
1da177e4
LT
2301 copy_to_user_policy(xp, &upe->pol, dir);
2302 if (copy_to_user_tmpl(xp, skb) < 0)
2303 goto nlmsg_failure;
df71837d
TJ
2304 if (copy_to_user_sec_ctx(xp, skb))
2305 goto nlmsg_failure;
1459bb36 2306 if (copy_to_user_policy_type(xp->type, skb) < 0)
f7b6983f 2307 goto nlmsg_failure;
1da177e4
LT
2308 upe->hard = !!hard;
2309
9825069d 2310 return nlmsg_end(skb, nlh);
1da177e4
LT
2311
2312nlmsg_failure:
9825069d
TG
2313 nlmsg_cancel(skb, nlh);
2314 return -EMSGSIZE;
1da177e4
LT
2315}
2316
26b15dad 2317static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1da177e4
LT
2318{
2319 struct sk_buff *skb;
2320 size_t len;
2321
2322 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2323 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
661697f7 2324 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
785fd8b8
JHS
2325#ifdef CONFIG_XFRM_SUB_POLICY
2326 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2327#endif
1da177e4
LT
2328 skb = alloc_skb(len, GFP_ATOMIC);
2329 if (skb == NULL)
2330 return -ENOMEM;
2331
d51d081d 2332 if (build_polexpire(skb, xp, dir, c) < 0)
1da177e4
LT
2333 BUG();
2334
082a1ad5 2335 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
2336}
2337
26b15dad
JHS
2338static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2339{
2340 struct xfrm_userpolicy_info *p;
0603eac0 2341 struct xfrm_userpolicy_id *id;
26b15dad
JHS
2342 struct nlmsghdr *nlh;
2343 struct sk_buff *skb;
26b15dad 2344 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
0603eac0
HX
2345 int headlen;
2346
2347 headlen = sizeof(*p);
2348 if (c->event == XFRM_MSG_DELPOLICY) {
2349 len += RTA_SPACE(headlen);
2350 headlen = sizeof(*id);
2351 }
334f3d45
JHS
2352#ifdef CONFIG_XFRM_SUB_POLICY
2353 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2354#endif
0603eac0 2355 len += NLMSG_SPACE(headlen);
26b15dad
JHS
2356
2357 skb = alloc_skb(len, GFP_ATOMIC);
2358 if (skb == NULL)
2359 return -ENOMEM;
26b15dad 2360
79b8b7f4
TG
2361 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2362 if (nlh == NULL)
2363 goto nlmsg_failure;
26b15dad 2364
7b67c857 2365 p = nlmsg_data(nlh);
0603eac0 2366 if (c->event == XFRM_MSG_DELPOLICY) {
c0144bea
TG
2367 struct nlattr *attr;
2368
7b67c857 2369 id = nlmsg_data(nlh);
0603eac0
HX
2370 memset(id, 0, sizeof(*id));
2371 id->dir = dir;
2372 if (c->data.byid)
2373 id->index = xp->index;
2374 else
2375 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2376
c0144bea
TG
2377 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2378 if (attr == NULL)
2379 goto nlmsg_failure;
2380
2381 p = nla_data(attr);
0603eac0 2382 }
26b15dad 2383
26b15dad
JHS
2384 copy_to_user_policy(xp, p, dir);
2385 if (copy_to_user_tmpl(xp, skb) < 0)
2386 goto nlmsg_failure;
1459bb36 2387 if (copy_to_user_policy_type(xp->type, skb) < 0)
f7b6983f 2388 goto nlmsg_failure;
26b15dad 2389
9825069d 2390 nlmsg_end(skb, nlh);
26b15dad 2391
082a1ad5 2392 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
2393
2394nlmsg_failure:
2395 kfree_skb(skb);
2396 return -1;
2397}
2398
2399static int xfrm_notify_policy_flush(struct km_event *c)
2400{
2401 struct nlmsghdr *nlh;
2402 struct sk_buff *skb;
785fd8b8 2403 int len = 0;
f7b6983f 2404#ifdef CONFIG_XFRM_SUB_POLICY
785fd8b8 2405 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
f7b6983f 2406#endif
785fd8b8 2407 len += NLMSG_LENGTH(0);
26b15dad
JHS
2408
2409 skb = alloc_skb(len, GFP_ATOMIC);
2410 if (skb == NULL)
2411 return -ENOMEM;
26b15dad 2412
79b8b7f4
TG
2413 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2414 if (nlh == NULL)
2415 goto nlmsg_failure;
0c51f53c
JHS
2416 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2417 goto nlmsg_failure;
26b15dad 2418
9825069d 2419 nlmsg_end(skb, nlh);
26b15dad 2420
082a1ad5 2421 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
2422
2423nlmsg_failure:
2424 kfree_skb(skb);
2425 return -1;
2426}
2427
2428static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2429{
2430
2431 switch (c->event) {
f60f6b8f
HX
2432 case XFRM_MSG_NEWPOLICY:
2433 case XFRM_MSG_UPDPOLICY:
2434 case XFRM_MSG_DELPOLICY:
26b15dad 2435 return xfrm_notify_policy(xp, dir, c);
f60f6b8f 2436 case XFRM_MSG_FLUSHPOLICY:
26b15dad 2437 return xfrm_notify_policy_flush(c);
f60f6b8f 2438 case XFRM_MSG_POLEXPIRE:
26b15dad
JHS
2439 return xfrm_exp_policy_notify(xp, dir, c);
2440 default:
2441 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2442 }
2443
2444 return 0;
2445
2446}
2447
97a64b45
MN
2448static int build_report(struct sk_buff *skb, u8 proto,
2449 struct xfrm_selector *sel, xfrm_address_t *addr)
2450{
2451 struct xfrm_user_report *ur;
2452 struct nlmsghdr *nlh;
97a64b45 2453
79b8b7f4
TG
2454 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2455 if (nlh == NULL)
2456 return -EMSGSIZE;
97a64b45 2457
7b67c857 2458 ur = nlmsg_data(nlh);
97a64b45
MN
2459 ur->proto = proto;
2460 memcpy(&ur->sel, sel, sizeof(ur->sel));
2461
2462 if (addr)
c0144bea 2463 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
97a64b45 2464
9825069d 2465 return nlmsg_end(skb, nlh);
97a64b45 2466
c0144bea 2467nla_put_failure:
9825069d
TG
2468 nlmsg_cancel(skb, nlh);
2469 return -EMSGSIZE;
97a64b45
MN
2470}
2471
2472static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2473 xfrm_address_t *addr)
2474{
2475 struct sk_buff *skb;
2476 size_t len;
2477
2478 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2479 skb = alloc_skb(len, GFP_ATOMIC);
2480 if (skb == NULL)
2481 return -ENOMEM;
2482
2483 if (build_report(skb, proto, sel, addr) < 0)
2484 BUG();
2485
082a1ad5 2486 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
97a64b45
MN
2487}
2488
1da177e4
LT
2489static struct xfrm_mgr netlink_mgr = {
2490 .id = "netlink",
2491 .notify = xfrm_send_state_notify,
2492 .acquire = xfrm_send_acquire,
2493 .compile_policy = xfrm_compile_policy,
2494 .notify_policy = xfrm_send_policy_notify,
97a64b45 2495 .report = xfrm_send_report,
5c79de6e 2496 .migrate = xfrm_send_migrate,
1da177e4
LT
2497};
2498
2499static int __init xfrm_user_init(void)
2500{
be33690d
PM
2501 struct sock *nlsk;
2502
654b32c6 2503 printk(KERN_INFO "Initializing XFRM netlink socket\n");
1da177e4 2504
be33690d 2505 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
af65bdfc 2506 xfrm_netlink_rcv, NULL, THIS_MODULE);
be33690d 2507 if (nlsk == NULL)
1da177e4 2508 return -ENOMEM;
be33690d 2509 rcu_assign_pointer(xfrm_nl, nlsk);
1da177e4
LT
2510
2511 xfrm_register_km(&netlink_mgr);
2512
2513 return 0;
2514}
2515
2516static void __exit xfrm_user_exit(void)
2517{
be33690d
PM
2518 struct sock *nlsk = xfrm_nl;
2519
1da177e4 2520 xfrm_unregister_km(&netlink_mgr);
be33690d
PM
2521 rcu_assign_pointer(xfrm_nl, NULL);
2522 synchronize_rcu();
2523 sock_release(nlsk->sk_socket);
1da177e4
LT
2524}
2525
2526module_init(xfrm_user_init);
2527module_exit(xfrm_user_exit);
2528MODULE_LICENSE("GPL");
4fdb3bb7 2529MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
f8cd5488 2530