blob: ec480b69510b2d419c5afbe54d9282beafaee9cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
Joy Latten161a09e2006-11-27 13:11:54 -060034#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
37{
38 struct rtattr *rt = xfrma[type - 1];
39 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070040 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (!rt)
43 return 0;
44
Herbert Xu31c26852005-05-19 12:39:49 -070045 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
46 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return -EINVAL;
48
49 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070050
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +090051 len -= (algp->alg_key_len + 7U) / 8;
Herbert Xu31c26852005-05-19 12:39:49 -070052 if (len < 0)
53 return -EINVAL;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 switch (type) {
56 case XFRMA_ALG_AUTH:
57 if (!algp->alg_key_len &&
58 strcmp(algp->alg_name, "digest_null") != 0)
59 return -EINVAL;
60 break;
61
62 case XFRMA_ALG_CRYPT:
63 if (!algp->alg_key_len &&
64 strcmp(algp->alg_name, "cipher_null") != 0)
65 return -EINVAL;
66 break;
67
68 case XFRMA_ALG_COMP:
69 /* Zero length keys are legal. */
70 break;
71
72 default:
73 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 return 0;
78}
79
80static int verify_encap_tmpl(struct rtattr **xfrma)
81{
82 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
83 struct xfrm_encap_tmpl *encap;
84
85 if (!rt)
86 return 0;
87
88 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
89 return -EINVAL;
90
91 return 0;
92}
93
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
96{
97 struct rtattr *rt = xfrma[type - 1];
98
99 if (!rt)
100 return 0;
101
102 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
103 return -EINVAL;
104
105 if (addrp)
106 *addrp = RTA_DATA(rt);
107
108 return 0;
109}
Trent Jaegerdf718372005-12-13 23:12:27 -0800110
111static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112{
113 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
114 struct xfrm_user_sec_ctx *uctx;
115 int len = 0;
116
117 if (!rt)
118 return 0;
119
120 if (rt->rta_len < sizeof(*uctx))
121 return -EINVAL;
122
123 uctx = RTA_DATA(rt);
124
Trent Jaegerdf718372005-12-13 23:12:27 -0800125 len += sizeof(struct xfrm_user_sec_ctx);
126 len += uctx->ctx_len;
127
128 if (uctx->len != len)
129 return -EINVAL;
130
131 return 0;
132}
133
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int verify_newsa_info(struct xfrm_usersa_info *p,
136 struct rtattr **xfrma)
137{
138 int err;
139
140 err = -EINVAL;
141 switch (p->family) {
142 case AF_INET:
143 break;
144
145 case AF_INET6:
146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 break;
148#else
149 err = -EAFNOSUPPORT;
150 goto out;
151#endif
152
153 default:
154 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 err = -EINVAL;
158 switch (p->id.proto) {
159 case IPPROTO_AH:
160 if (!xfrma[XFRMA_ALG_AUTH-1] ||
161 xfrma[XFRMA_ALG_CRYPT-1] ||
162 xfrma[XFRMA_ALG_COMP-1])
163 goto out;
164 break;
165
166 case IPPROTO_ESP:
167 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
168 !xfrma[XFRMA_ALG_CRYPT-1]) ||
169 xfrma[XFRMA_ALG_COMP-1])
170 goto out;
171 break;
172
173 case IPPROTO_COMP:
174 if (!xfrma[XFRMA_ALG_COMP-1] ||
175 xfrma[XFRMA_ALG_AUTH-1] ||
176 xfrma[XFRMA_ALG_CRYPT-1])
177 goto out;
178 break;
179
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700180#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181 case IPPROTO_DSTOPTS:
182 case IPPROTO_ROUTING:
183 if (xfrma[XFRMA_ALG_COMP-1] ||
184 xfrma[XFRMA_ALG_AUTH-1] ||
185 xfrma[XFRMA_ALG_CRYPT-1] ||
186 xfrma[XFRMA_ENCAP-1] ||
187 xfrma[XFRMA_SEC_CTX-1] ||
188 !xfrma[XFRMA_COADDR-1])
189 goto out;
190 break;
191#endif
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 default:
194 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 goto out;
199 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 goto out;
201 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 goto out;
203 if ((err = verify_encap_tmpl(xfrma)))
204 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800205 if ((err = verify_sec_ctx_len(xfrma)))
206 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700207 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 err = -EINVAL;
211 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700212 case XFRM_MODE_TRANSPORT:
213 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700214 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700215 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217
218 default:
219 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 err = 0;
223
224out:
225 return err;
226}
227
228static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
229 struct xfrm_algo_desc *(*get_byname)(char *, int),
230 struct rtattr *u_arg)
231{
232 struct rtattr *rta = u_arg;
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700235 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!rta)
238 return 0;
239
240 ualg = RTA_DATA(rta);
241
242 algo = get_byname(ualg->alg_name, 1);
243 if (!algo)
244 return -ENOSYS;
245 *props = algo->desc.sadb_alg_id;
246
Herbert Xub9e9dea2005-05-19 12:39:04 -0700247 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200248 p = kmemdup(ualg, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!p)
250 return -ENOMEM;
251
Herbert Xu04ff1262006-08-13 08:50:00 +1000252 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *algpp = p;
254 return 0;
255}
256
257static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
258{
259 struct rtattr *rta = u_arg;
260 struct xfrm_encap_tmpl *p, *uencap;
261
262 if (!rta)
263 return 0;
264
265 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200266 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!p)
268 return -ENOMEM;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *encapp = p;
271 return 0;
272}
273
Trent Jaegerdf718372005-12-13 23:12:27 -0800274
Joy Latten661697f2007-04-13 16:14:35 -0700275static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800276{
Trent Jaegerdf718372005-12-13 23:12:27 -0800277 int len = 0;
278
279 if (xfrm_ctx) {
280 len += sizeof(struct xfrm_user_sec_ctx);
281 len += xfrm_ctx->ctx_len;
282 }
283 return len;
284}
285
286static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
287{
288 struct xfrm_user_sec_ctx *uctx;
289
290 if (!u_arg)
291 return 0;
292
293 uctx = RTA_DATA(u_arg);
294 return security_xfrm_state_alloc(x, uctx);
295}
296
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700297static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
298{
299 struct rtattr *rta = u_arg;
300 xfrm_address_t *p, *uaddrp;
301
302 if (!rta)
303 return 0;
304
305 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200306 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700307 if (!p)
308 return -ENOMEM;
309
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700310 *addrpp = p;
311 return 0;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
315{
316 memcpy(&x->id, &p->id, sizeof(x->id));
317 memcpy(&x->sel, &p->sel, sizeof(x->sel));
318 memcpy(&x->lft, &p->lft, sizeof(x->lft));
319 x->props.mode = p->mode;
320 x->props.replay_window = p->replay_window;
321 x->props.reqid = p->reqid;
322 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700323 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700325
326 /*
327 * Set inner address family if the KM left it as zero.
328 * See comment in validate_tmpl.
329 */
330 if (!x->sel.family)
331 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800334/*
335 * someday when pfkey also has support, we could have the code
336 * somehow made shareable and move it to xfrm_state.c - JHS
337 *
338*/
339static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
340{
341 int err = - EINVAL;
342 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
343 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
344 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
345 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
346
347 if (rp) {
348 struct xfrm_replay_state *replay;
349 if (RTA_PAYLOAD(rp) < sizeof(*replay))
350 goto error;
351 replay = RTA_DATA(rp);
352 memcpy(&x->replay, replay, sizeof(*replay));
353 memcpy(&x->preplay, replay, sizeof(*replay));
354 }
355
356 if (lt) {
357 struct xfrm_lifetime_cur *ltime;
358 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
359 goto error;
360 ltime = RTA_DATA(lt);
361 x->curlft.bytes = ltime->bytes;
362 x->curlft.packets = ltime->packets;
363 x->curlft.add_time = ltime->add_time;
364 x->curlft.use_time = ltime->use_time;
365 }
366
367 if (et) {
368 if (RTA_PAYLOAD(et) < sizeof(u32))
369 goto error;
370 x->replay_maxage = *(u32*)RTA_DATA(et);
371 }
372
373 if (rt) {
374 if (RTA_PAYLOAD(rt) < sizeof(u32))
375 goto error;
376 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
377 }
378
379 return 0;
380error:
381 return err;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
385 struct rtattr **xfrma,
386 int *errp)
387{
388 struct xfrm_state *x = xfrm_state_alloc();
389 int err = -ENOMEM;
390
391 if (!x)
392 goto error_no_put;
393
394 copy_from_user_state(x, p);
395
396 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
397 xfrm_aalg_get_byname,
398 xfrma[XFRMA_ALG_AUTH-1])))
399 goto error;
400 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
401 xfrm_ealg_get_byname,
402 xfrma[XFRMA_ALG_CRYPT-1])))
403 goto error;
404 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
405 xfrm_calg_get_byname,
406 xfrma[XFRMA_ALG_COMP-1])))
407 goto error;
408 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
409 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700410 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
411 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700412 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (err)
414 goto error;
415
Trent Jaegerdf718372005-12-13 23:12:27 -0800416 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
417 goto error;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800420 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
421 /* sysctl_xfrm_aevent_etime is in 100ms units */
422 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
423 x->preplay.bitmap = 0;
424 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
425 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
426
427 /* override default values from above */
428
429 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
430 if (err < 0)
431 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return x;
434
435error:
436 x->km.state = XFRM_STATE_DEAD;
437 xfrm_state_put(x);
438error_no_put:
439 *errp = err;
440 return NULL;
441}
442
Christoph Hellwig22e70052007-01-02 15:22:30 -0800443static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
444 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
447 struct xfrm_state *x;
448 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700449 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Christoph Hellwig22e70052007-01-02 15:22:30 -0800451 err = verify_newsa_info(p, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (err)
453 return err;
454
Christoph Hellwig22e70052007-01-02 15:22:30 -0800455 x = xfrm_state_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!x)
457 return err;
458
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700459 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
461 err = xfrm_state_add(x);
462 else
463 err = xfrm_state_update(x);
464
Joy Latten161a09e2006-11-27 13:11:54 -0600465 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
466 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (err < 0) {
469 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800470 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700474 c.seq = nlh->nlmsg_seq;
475 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700476 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700477
478 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700479out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700480 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return err;
482}
483
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700484static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
485 struct rtattr **xfrma,
486 int *errp)
487{
488 struct xfrm_state *x = NULL;
489 int err;
490
491 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
492 err = -ESRCH;
493 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
494 } else {
495 xfrm_address_t *saddr = NULL;
496
497 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
498 if (err)
499 goto out;
500
501 if (!saddr) {
502 err = -EINVAL;
503 goto out;
504 }
505
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800506 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700507 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
508 p->family);
509 }
510
511 out:
512 if (!x && errp)
513 *errp = err;
514 return x;
515}
516
Christoph Hellwig22e70052007-01-02 15:22:30 -0800517static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
518 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700521 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700522 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
524
Christoph Hellwig22e70052007-01-02 15:22:30 -0800525 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David S. Miller6f68dc32006-06-08 23:58:52 -0700529 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700530 goto out;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700533 err = -EPERM;
534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700537 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600538
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700539 if (err < 0)
540 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700541
542 c.seq = nlh->nlmsg_seq;
543 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700544 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700545 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700547out:
Eric Paris16bec312007-03-07 16:02:16 -0800548 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
549 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700550 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
555{
556 memcpy(&p->id, &x->id, sizeof(p->id));
557 memcpy(&p->sel, &x->sel, sizeof(p->sel));
558 memcpy(&p->lft, &x->lft, sizeof(p->lft));
559 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
560 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700561 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 p->mode = x->props.mode;
563 p->replay_window = x->props.replay_window;
564 p->reqid = x->props.reqid;
565 p->family = x->props.family;
566 p->flags = x->props.flags;
567 p->seq = x->km.seq;
568}
569
570struct xfrm_dump_info {
571 struct sk_buff *in_skb;
572 struct sk_buff *out_skb;
573 u32 nlmsg_seq;
574 u16 nlmsg_flags;
575 int start_idx;
576 int this_idx;
577};
578
579static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
580{
581 struct xfrm_dump_info *sp = ptr;
582 struct sk_buff *in_skb = sp->in_skb;
583 struct sk_buff *skb = sp->out_skb;
584 struct xfrm_usersa_info *p;
585 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700586 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (sp->this_idx < sp->start_idx)
589 goto out;
590
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700591 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
592 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
593 if (nlh == NULL)
594 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 p = NLMSG_DATA(nlh);
597 copy_to_user_state(x, p);
598
599 if (x->aalg)
600 RTA_PUT(skb, XFRMA_ALG_AUTH,
601 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
602 if (x->ealg)
603 RTA_PUT(skb, XFRMA_ALG_CRYPT,
604 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
605 if (x->calg)
606 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
607
608 if (x->encap)
609 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
610
Trent Jaegerdf718372005-12-13 23:12:27 -0800611 if (x->security) {
612 int ctx_size = sizeof(struct xfrm_sec_ctx) +
613 x->security->ctx_len;
614 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
615 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
616
617 uctx->exttype = XFRMA_SEC_CTX;
618 uctx->len = ctx_size;
619 uctx->ctx_doi = x->security->ctx_doi;
620 uctx->ctx_alg = x->security->ctx_alg;
621 uctx->ctx_len = x->security->ctx_len;
622 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
623 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700624
625 if (x->coaddr)
626 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
627
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700628 if (x->lastused)
629 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
630
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700631 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632out:
633 sp->this_idx++;
634 return 0;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700637 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -1;
639}
640
641static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
642{
643 struct xfrm_dump_info info;
644
645 info.in_skb = cb->skb;
646 info.out_skb = skb;
647 info.nlmsg_seq = cb->nlh->nlmsg_seq;
648 info.nlmsg_flags = NLM_F_MULTI;
649 info.this_idx = 0;
650 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700651 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 cb->args[0] = info.this_idx;
653
654 return skb->len;
655}
656
657static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
658 struct xfrm_state *x, u32 seq)
659{
660 struct xfrm_dump_info info;
661 struct sk_buff *skb;
662
663 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
664 if (!skb)
665 return ERR_PTR(-ENOMEM);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 info.in_skb = in_skb;
668 info.out_skb = skb;
669 info.nlmsg_seq = seq;
670 info.nlmsg_flags = 0;
671 info.this_idx = info.start_idx = 0;
672
673 if (dump_one_state(x, 0, &info)) {
674 kfree_skb(skb);
675 return NULL;
676 }
677
678 return skb;
679}
680
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700681static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
682{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700683 struct xfrmk_spdinfo si;
684 struct xfrmu_spdinfo spc;
685 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700686 struct nlmsghdr *nlh;
687 u32 *f;
688
689 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
690 if (nlh == NULL) /* shouldnt really happen ... */
691 return -EMSGSIZE;
692
693 f = nlmsg_data(nlh);
694 *f = flags;
695 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700696 spc.incnt = si.incnt;
697 spc.outcnt = si.outcnt;
698 spc.fwdcnt = si.fwdcnt;
699 spc.inscnt = si.inscnt;
700 spc.outscnt = si.outscnt;
701 spc.fwdscnt = si.fwdscnt;
702 sph.spdhcnt = si.spdhcnt;
703 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700704
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700705 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
706 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700707
708 return nlmsg_end(skb, nlh);
709
710nla_put_failure:
711 nlmsg_cancel(skb, nlh);
712 return -EMSGSIZE;
713}
714
715static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
716 struct rtattr **xfrma)
717{
718 struct sk_buff *r_skb;
719 u32 *flags = NLMSG_DATA(nlh);
720 u32 spid = NETLINK_CB(skb).pid;
721 u32 seq = nlh->nlmsg_seq;
722 int len = NLMSG_LENGTH(sizeof(u32));
723
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700724 len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
725 len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700726
727 r_skb = alloc_skb(len, GFP_ATOMIC);
728 if (r_skb == NULL)
729 return -ENOMEM;
730
731 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
732 BUG();
733
734 return nlmsg_unicast(xfrm_nl, r_skb, spid);
735}
736
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700737static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
738{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700739 struct xfrmk_sadinfo si;
740 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700741 struct nlmsghdr *nlh;
742 u32 *f;
743
744 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
745 if (nlh == NULL) /* shouldnt really happen ... */
746 return -EMSGSIZE;
747
748 f = nlmsg_data(nlh);
749 *f = flags;
750 xfrm_sad_getinfo(&si);
751
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700752 sh.sadhmcnt = si.sadhmcnt;
753 sh.sadhcnt = si.sadhcnt;
754
755 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
756 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700757
758 return nlmsg_end(skb, nlh);
759
760nla_put_failure:
761 nlmsg_cancel(skb, nlh);
762 return -EMSGSIZE;
763}
764
765static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
766 struct rtattr **xfrma)
767{
768 struct sk_buff *r_skb;
769 u32 *flags = NLMSG_DATA(nlh);
770 u32 spid = NETLINK_CB(skb).pid;
771 u32 seq = nlh->nlmsg_seq;
772 int len = NLMSG_LENGTH(sizeof(u32));
773
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700774 len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
775 len += RTA_SPACE(sizeof(u32));
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700776
777 r_skb = alloc_skb(len, GFP_ATOMIC);
778
779 if (r_skb == NULL)
780 return -ENOMEM;
781
782 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
783 BUG();
784
785 return nlmsg_unicast(xfrm_nl, r_skb, spid);
786}
787
Christoph Hellwig22e70052007-01-02 15:22:30 -0800788static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
789 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
792 struct xfrm_state *x;
793 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700794 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Christoph Hellwig22e70052007-01-02 15:22:30 -0800796 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (x == NULL)
798 goto out_noput;
799
800 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
801 if (IS_ERR(resp_skb)) {
802 err = PTR_ERR(resp_skb);
803 } else {
804 err = netlink_unicast(xfrm_nl, resp_skb,
805 NETLINK_CB(skb).pid, MSG_DONTWAIT);
806 }
807 xfrm_state_put(x);
808out_noput:
809 return err;
810}
811
812static int verify_userspi_info(struct xfrm_userspi_info *p)
813{
814 switch (p->info.id.proto) {
815 case IPPROTO_AH:
816 case IPPROTO_ESP:
817 break;
818
819 case IPPROTO_COMP:
820 /* IPCOMP spi is 16-bits. */
821 if (p->max >= 0x10000)
822 return -EINVAL;
823 break;
824
825 default:
826 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (p->min > p->max)
830 return -EINVAL;
831
832 return 0;
833}
834
Christoph Hellwig22e70052007-01-02 15:22:30 -0800835static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
836 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct xfrm_state *x;
839 struct xfrm_userspi_info *p;
840 struct sk_buff *resp_skb;
841 xfrm_address_t *daddr;
842 int family;
843 int err;
844
845 p = NLMSG_DATA(nlh);
846 err = verify_userspi_info(p);
847 if (err)
848 goto out_noput;
849
850 family = p->info.family;
851 daddr = &p->info.id.daddr;
852
853 x = NULL;
854 if (p->info.seq) {
855 x = xfrm_find_acq_byseq(p->info.seq);
856 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
857 xfrm_state_put(x);
858 x = NULL;
859 }
860 }
861
862 if (!x)
863 x = xfrm_find_acq(p->info.mode, p->info.reqid,
864 p->info.id.proto, daddr,
865 &p->info.saddr, 1,
866 family);
867 err = -ENOENT;
868 if (x == NULL)
869 goto out_noput;
870
871 resp_skb = ERR_PTR(-ENOENT);
872
873 spin_lock_bh(&x->lock);
874 if (x->km.state != XFRM_STATE_DEAD) {
875 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
876 if (x->id.spi)
877 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
878 }
879 spin_unlock_bh(&x->lock);
880
881 if (IS_ERR(resp_skb)) {
882 err = PTR_ERR(resp_skb);
883 goto out;
884 }
885
886 err = netlink_unicast(xfrm_nl, resp_skb,
887 NETLINK_CB(skb).pid, MSG_DONTWAIT);
888
889out:
890 xfrm_state_put(x);
891out_noput:
892 return err;
893}
894
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800895static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 switch (dir) {
898 case XFRM_POLICY_IN:
899 case XFRM_POLICY_OUT:
900 case XFRM_POLICY_FWD:
901 break;
902
903 default:
904 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 return 0;
908}
909
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800910static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700911{
912 switch (type) {
913 case XFRM_POLICY_TYPE_MAIN:
914#ifdef CONFIG_XFRM_SUB_POLICY
915 case XFRM_POLICY_TYPE_SUB:
916#endif
917 break;
918
919 default:
920 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700921 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700922
923 return 0;
924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
927{
928 switch (p->share) {
929 case XFRM_SHARE_ANY:
930 case XFRM_SHARE_SESSION:
931 case XFRM_SHARE_USER:
932 case XFRM_SHARE_UNIQUE:
933 break;
934
935 default:
936 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 switch (p->action) {
940 case XFRM_POLICY_ALLOW:
941 case XFRM_POLICY_BLOCK:
942 break;
943
944 default:
945 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 switch (p->sel.family) {
949 case AF_INET:
950 break;
951
952 case AF_INET6:
953#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
954 break;
955#else
956 return -EAFNOSUPPORT;
957#endif
958
959 default:
960 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 return verify_policy_dir(p->dir);
964}
965
Trent Jaegerdf718372005-12-13 23:12:27 -0800966static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
967{
968 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
969 struct xfrm_user_sec_ctx *uctx;
970
971 if (!rt)
972 return 0;
973
974 uctx = RTA_DATA(rt);
975 return security_xfrm_policy_alloc(pol, uctx);
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
979 int nr)
980{
981 int i;
982
983 xp->xfrm_nr = nr;
984 for (i = 0; i < nr; i++, ut++) {
985 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
986
987 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
988 memcpy(&t->saddr, &ut->saddr,
989 sizeof(xfrm_address_t));
990 t->reqid = ut->reqid;
991 t->mode = ut->mode;
992 t->share = ut->share;
993 t->optional = ut->optional;
994 t->aalgos = ut->aalgos;
995 t->ealgos = ut->ealgos;
996 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800997 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999}
1000
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001001static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1002{
1003 int i;
1004
1005 if (nr > XFRM_MAX_DEPTH)
1006 return -EINVAL;
1007
1008 for (i = 0; i < nr; i++) {
1009 /* We never validated the ut->family value, so many
1010 * applications simply leave it at zero. The check was
1011 * never made and ut->family was ignored because all
1012 * templates could be assumed to have the same family as
1013 * the policy itself. Now that we will have ipv4-in-ipv6
1014 * and ipv6-in-ipv4 tunnels, this is no longer true.
1015 */
1016 if (!ut[i].family)
1017 ut[i].family = family;
1018
1019 switch (ut[i].family) {
1020 case AF_INET:
1021 break;
1022#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1023 case AF_INET6:
1024 break;
1025#endif
1026 default:
1027 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001028 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001029 }
1030
1031 return 0;
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1035{
1036 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (!rt) {
1039 pol->xfrm_nr = 0;
1040 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001041 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1042 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1043 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001045 err = validate_tmpl(nr, utmpl, pol->family);
1046 if (err)
1047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 copy_templates(pol, RTA_DATA(rt), nr);
1050 }
1051 return 0;
1052}
1053
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001054static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1055{
1056 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1057 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001058 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001059 int err;
1060
1061 if (rt) {
1062 if (rt->rta_len < sizeof(*upt))
1063 return -EINVAL;
1064
1065 upt = RTA_DATA(rt);
1066 type = upt->type;
1067 }
1068
1069 err = verify_policy_type(type);
1070 if (err)
1071 return err;
1072
1073 *tp = type;
1074 return 0;
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1078{
1079 xp->priority = p->priority;
1080 xp->index = p->index;
1081 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1082 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1083 xp->action = p->action;
1084 xp->flags = p->flags;
1085 xp->family = p->sel.family;
1086 /* XXX xp->share = p->share; */
1087}
1088
1089static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1090{
1091 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1092 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1093 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1094 p->priority = xp->priority;
1095 p->index = xp->index;
1096 p->sel.family = xp->family;
1097 p->dir = dir;
1098 p->action = xp->action;
1099 p->flags = xp->flags;
1100 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1101}
1102
1103static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1104{
1105 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1106 int err;
1107
1108 if (!xp) {
1109 *errp = -ENOMEM;
1110 return NULL;
1111 }
1112
1113 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001114
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001115 err = copy_from_user_policy_type(&xp->type, xfrma);
1116 if (err)
1117 goto error;
1118
Trent Jaegerdf718372005-12-13 23:12:27 -08001119 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1120 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001121 if (err)
1122 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001125 error:
1126 *errp = err;
1127 kfree(xp);
1128 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Christoph Hellwig22e70052007-01-02 15:22:30 -08001131static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1132 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1135 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001136 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int err;
1138 int excl;
1139
1140 err = verify_newpolicy_info(p);
1141 if (err)
1142 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001143 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001144 if (err)
1145 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Christoph Hellwig22e70052007-01-02 15:22:30 -08001147 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!xp)
1149 return err;
1150
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001151 /* shouldnt excl be based on nlh flags??
1152 * Aha! this is anti-netlink really i.e more pfkey derived
1153 * in netlink excl is a flag and you wouldnt need
1154 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1156 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001157 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1158 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001161 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 kfree(xp);
1163 return err;
1164 }
1165
Herbert Xuf60f6b82005-06-18 22:44:37 -07001166 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001167 c.seq = nlh->nlmsg_seq;
1168 c.pid = nlh->nlmsg_pid;
1169 km_policy_notify(xp, p->dir, &c);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 xfrm_pol_put(xp);
1172
1173 return 0;
1174}
1175
1176static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1177{
1178 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1179 int i;
1180
1181 if (xp->xfrm_nr == 0)
1182 return 0;
1183
1184 for (i = 0; i < xp->xfrm_nr; i++) {
1185 struct xfrm_user_tmpl *up = &vec[i];
1186 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1187
1188 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001189 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1191 up->reqid = kp->reqid;
1192 up->mode = kp->mode;
1193 up->share = kp->share;
1194 up->optional = kp->optional;
1195 up->aalgos = kp->aalgos;
1196 up->ealgos = kp->ealgos;
1197 up->calgos = kp->calgos;
1198 }
1199 RTA_PUT(skb, XFRMA_TMPL,
1200 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1201 vec);
1202
1203 return 0;
1204
1205rtattr_failure:
1206 return -1;
1207}
1208
Serge Hallyn0d681622006-07-24 23:30:44 -07001209static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001210{
Serge Hallyn0d681622006-07-24 23:30:44 -07001211 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1212 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1213 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001214
Serge Hallyn0d681622006-07-24 23:30:44 -07001215 uctx->exttype = XFRMA_SEC_CTX;
1216 uctx->len = ctx_size;
1217 uctx->ctx_doi = s->ctx_doi;
1218 uctx->ctx_alg = s->ctx_alg;
1219 uctx->ctx_len = s->ctx_len;
1220 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001221 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001222
1223 rtattr_failure:
1224 return -1;
1225}
1226
Serge Hallyn0d681622006-07-24 23:30:44 -07001227static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1228{
1229 if (x->security) {
1230 return copy_sec_ctx(x->security, skb);
1231 }
1232 return 0;
1233}
1234
1235static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1236{
1237 if (xp->security) {
1238 return copy_sec_ctx(xp->security, skb);
1239 }
1240 return 0;
1241}
1242
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001243#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001244static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001245{
1246 struct xfrm_userpolicy_type upt;
1247
1248 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001249 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001250
1251 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1252
1253 return 0;
1254
1255rtattr_failure:
1256 return -1;
1257}
1258
1259#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001260static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001261{
1262 return 0;
1263}
1264#endif
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1267{
1268 struct xfrm_dump_info *sp = ptr;
1269 struct xfrm_userpolicy_info *p;
1270 struct sk_buff *in_skb = sp->in_skb;
1271 struct sk_buff *skb = sp->out_skb;
1272 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001273 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (sp->this_idx < sp->start_idx)
1276 goto out;
1277
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001278 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1279 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1280 if (nlh == NULL)
1281 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 p = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 copy_to_user_policy(xp, p, dir);
1285 if (copy_to_user_tmpl(xp, skb) < 0)
1286 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001287 if (copy_to_user_sec_ctx(xp, skb))
1288 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001289 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001290 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001292 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293out:
1294 sp->this_idx++;
1295 return 0;
1296
1297nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001298 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -1;
1300}
1301
1302static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1303{
1304 struct xfrm_dump_info info;
1305
1306 info.in_skb = cb->skb;
1307 info.out_skb = skb;
1308 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1309 info.nlmsg_flags = NLM_F_MULTI;
1310 info.this_idx = 0;
1311 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001312 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1313#ifdef CONFIG_XFRM_SUB_POLICY
1314 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 cb->args[0] = info.this_idx;
1317
1318 return skb->len;
1319}
1320
1321static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1322 struct xfrm_policy *xp,
1323 int dir, u32 seq)
1324{
1325 struct xfrm_dump_info info;
1326 struct sk_buff *skb;
1327
1328 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1329 if (!skb)
1330 return ERR_PTR(-ENOMEM);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 info.in_skb = in_skb;
1333 info.out_skb = skb;
1334 info.nlmsg_seq = seq;
1335 info.nlmsg_flags = 0;
1336 info.this_idx = info.start_idx = 0;
1337
1338 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1339 kfree_skb(skb);
1340 return NULL;
1341 }
1342
1343 return skb;
1344}
1345
Christoph Hellwig22e70052007-01-02 15:22:30 -08001346static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1347 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct xfrm_policy *xp;
1350 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001351 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001353 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int delete;
1355
1356 p = NLMSG_DATA(nlh);
1357 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1358
Christoph Hellwig22e70052007-01-02 15:22:30 -08001359 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001360 if (err)
1361 return err;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 err = verify_policy_dir(p->dir);
1364 if (err)
1365 return err;
1366
1367 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001368 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001369 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001370 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001371 struct xfrm_policy tmp;
1372
Christoph Hellwig22e70052007-01-02 15:22:30 -08001373 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001374 if (err)
1375 return err;
1376
1377 memset(&tmp, 0, sizeof(struct xfrm_policy));
1378 if (rt) {
1379 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1380
1381 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1382 return err;
1383 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001384 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1385 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001386 security_xfrm_policy_free(&tmp);
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (xp == NULL)
1389 return -ENOENT;
1390
1391 if (!delete) {
1392 struct sk_buff *resp_skb;
1393
1394 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1395 if (IS_ERR(resp_skb)) {
1396 err = PTR_ERR(resp_skb);
1397 } else {
1398 err = netlink_unicast(xfrm_nl, resp_skb,
1399 NETLINK_CB(skb).pid,
1400 MSG_DONTWAIT);
1401 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001402 } else {
David S. Miller13fcfbb2007-02-12 13:53:54 -08001403 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1404 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1405
1406 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001407 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001408
Herbert Xue7443892005-06-18 22:44:18 -07001409 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001410 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001411 c.seq = nlh->nlmsg_seq;
1412 c.pid = nlh->nlmsg_pid;
1413 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001416out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001417 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return err;
1419}
1420
Christoph Hellwig22e70052007-01-02 15:22:30 -08001421static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1422 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001426 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001427 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Joy Latten161a09e2006-11-27 13:11:54 -06001429 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1430 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001431 err = xfrm_state_flush(p->proto, &audit_info);
1432 if (err)
1433 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001434 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001435 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001436 c.seq = nlh->nlmsg_seq;
1437 c.pid = nlh->nlmsg_pid;
1438 km_state_notify(NULL, &c);
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
1441}
1442
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001443
1444static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1445{
1446 struct xfrm_aevent_id *id;
1447 struct nlmsghdr *nlh;
1448 struct xfrm_lifetime_cur ltime;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001449 unsigned char *b = skb_tail_pointer(skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001451 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1452 if (nlh == NULL)
1453 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001454 id = NLMSG_DATA(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001456 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001457 id->sa_id.spi = x->id.spi;
1458 id->sa_id.family = x->props.family;
1459 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001460 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1461 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001462 id->flags = c->data.aevent;
1463
1464 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1465
1466 ltime.bytes = x->curlft.bytes;
1467 ltime.packets = x->curlft.packets;
1468 ltime.add_time = x->curlft.add_time;
1469 ltime.use_time = x->curlft.use_time;
1470
1471 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1472
1473 if (id->flags&XFRM_AE_RTHR) {
1474 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1475 }
1476
1477 if (id->flags&XFRM_AE_ETHR) {
1478 u32 etimer = x->replay_maxage*10/HZ;
1479 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1480 }
1481
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001482 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001483 return skb->len;
1484
1485rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001486 nlmsg_trim(skb, b);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001487 return -1;
1488}
1489
Christoph Hellwig22e70052007-01-02 15:22:30 -08001490static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1491 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001492{
1493 struct xfrm_state *x;
1494 struct sk_buff *r_skb;
1495 int err;
1496 struct km_event c;
1497 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1498 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1499 struct xfrm_usersa_id *id = &p->sa_id;
1500
1501 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1502 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1503
1504 if (p->flags&XFRM_AE_RTHR)
1505 len+=RTA_SPACE(sizeof(u32));
1506
1507 if (p->flags&XFRM_AE_ETHR)
1508 len+=RTA_SPACE(sizeof(u32));
1509
1510 r_skb = alloc_skb(len, GFP_ATOMIC);
1511 if (r_skb == NULL)
1512 return -ENOMEM;
1513
1514 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1515 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001516 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001517 return -ESRCH;
1518 }
1519
1520 /*
1521 * XXX: is this lock really needed - none of the other
1522 * gets lock (the concern is things getting updated
1523 * while we are still reading) - jhs
1524 */
1525 spin_lock_bh(&x->lock);
1526 c.data.aevent = p->flags;
1527 c.seq = nlh->nlmsg_seq;
1528 c.pid = nlh->nlmsg_pid;
1529
1530 if (build_aevent(r_skb, x, &c) < 0)
1531 BUG();
1532 err = netlink_unicast(xfrm_nl, r_skb,
1533 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1534 spin_unlock_bh(&x->lock);
1535 xfrm_state_put(x);
1536 return err;
1537}
1538
Christoph Hellwig22e70052007-01-02 15:22:30 -08001539static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1540 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001541{
1542 struct xfrm_state *x;
1543 struct km_event c;
1544 int err = - EINVAL;
1545 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1546 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1547 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1548
1549 if (!lt && !rp)
1550 return err;
1551
1552 /* pedantic mode - thou shalt sayeth replaceth */
1553 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1554 return err;
1555
1556 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1557 if (x == NULL)
1558 return -ESRCH;
1559
1560 if (x->km.state != XFRM_STATE_VALID)
1561 goto out;
1562
1563 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001564 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001565 spin_unlock_bh(&x->lock);
1566 if (err < 0)
1567 goto out;
1568
1569 c.event = nlh->nlmsg_type;
1570 c.seq = nlh->nlmsg_seq;
1571 c.pid = nlh->nlmsg_pid;
1572 c.data.aevent = XFRM_AE_CU;
1573 km_state_notify(x, &c);
1574 err = 0;
1575out:
1576 xfrm_state_put(x);
1577 return err;
1578}
1579
Christoph Hellwig22e70052007-01-02 15:22:30 -08001580static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1581 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001583 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001584 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001585 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001586 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001587
Christoph Hellwig22e70052007-01-02 15:22:30 -08001588 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001589 if (err)
1590 return err;
1591
Joy Latten161a09e2006-11-27 13:11:54 -06001592 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1593 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001594 err = xfrm_policy_flush(type, &audit_info);
1595 if (err)
1596 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001597 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001598 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001599 c.seq = nlh->nlmsg_seq;
1600 c.pid = nlh->nlmsg_pid;
1601 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
1603}
1604
Christoph Hellwig22e70052007-01-02 15:22:30 -08001605static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1606 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001607{
1608 struct xfrm_policy *xp;
1609 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1610 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001611 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001612 int err = -ENOENT;
1613
Christoph Hellwig22e70052007-01-02 15:22:30 -08001614 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001615 if (err)
1616 return err;
1617
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001618 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001619 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001620 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001621 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001622 struct xfrm_policy tmp;
1623
Christoph Hellwig22e70052007-01-02 15:22:30 -08001624 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001625 if (err)
1626 return err;
1627
1628 memset(&tmp, 0, sizeof(struct xfrm_policy));
1629 if (rt) {
1630 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1631
1632 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1633 return err;
1634 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001635 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1636 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001637 security_xfrm_policy_free(&tmp);
1638 }
1639
1640 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001641 return -ENOENT;
1642 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001643 if (xp->dead) {
1644 read_unlock(&xp->lock);
1645 goto out;
1646 }
1647
1648 read_unlock(&xp->lock);
1649 err = 0;
1650 if (up->hard) {
1651 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001652 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1653 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1654
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001655 } else {
1656 // reset the timers here?
1657 printk("Dont know what to do with soft policy expire\n");
1658 }
1659 km_policy_expired(xp, p->dir, up->hard, current->pid);
1660
1661out:
1662 xfrm_pol_put(xp);
1663 return err;
1664}
1665
Christoph Hellwig22e70052007-01-02 15:22:30 -08001666static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1667 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001668{
1669 struct xfrm_state *x;
1670 int err;
1671 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1672 struct xfrm_usersa_info *p = &ue->state;
1673
1674 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001675
David S. Miller3a765aa2007-02-26 14:52:21 -08001676 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001677 if (x == NULL)
1678 return err;
1679
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001680 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001681 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001682 if (x->km.state != XFRM_STATE_VALID)
1683 goto out;
1684 km_state_expired(x, ue->hard, current->pid);
1685
Joy Latten161a09e2006-11-27 13:11:54 -06001686 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001687 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001688 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1689 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1690 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001691 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001692out:
1693 spin_unlock_bh(&x->lock);
1694 xfrm_state_put(x);
1695 return err;
1696}
1697
Christoph Hellwig22e70052007-01-02 15:22:30 -08001698static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1699 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001700{
1701 struct xfrm_policy *xp;
1702 struct xfrm_user_tmpl *ut;
1703 int i;
1704 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1705
1706 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1707 struct xfrm_state *x = xfrm_state_alloc();
1708 int err = -ENOMEM;
1709
1710 if (!x)
1711 return err;
1712
1713 err = verify_newpolicy_info(&ua->policy);
1714 if (err) {
1715 printk("BAD policy passed\n");
1716 kfree(x);
1717 return err;
1718 }
1719
1720 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001721 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1722 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001723 kfree(x);
1724 return err;
1725 }
1726
1727 memcpy(&x->id, &ua->id, sizeof(ua->id));
1728 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1729 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1730
1731 ut = RTA_DATA(rt);
1732 /* extract the templates and for each call km_key */
1733 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1734 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1735 memcpy(&x->id, &t->id, sizeof(x->id));
1736 x->props.mode = t->mode;
1737 x->props.reqid = t->reqid;
1738 x->props.family = ut->family;
1739 t->aalgos = ua->aalgos;
1740 t->ealgos = ua->ealgos;
1741 t->calgos = ua->calgos;
1742 err = km_query(x, t, xp);
1743
1744 }
1745
1746 kfree(x);
1747 kfree(xp);
1748
1749 return 0;
1750}
1751
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001752#ifdef CONFIG_XFRM_MIGRATE
1753static int verify_user_migrate(struct rtattr **xfrma)
1754{
1755 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1756 struct xfrm_user_migrate *um;
1757
1758 if (!rt)
1759 return -EINVAL;
1760
1761 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1762 return -EINVAL;
1763
1764 return 0;
1765}
1766
1767static int copy_from_user_migrate(struct xfrm_migrate *ma,
1768 struct rtattr **xfrma, int *num)
1769{
1770 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1771 struct xfrm_user_migrate *um;
1772 int i, num_migrate;
1773
1774 um = RTA_DATA(rt);
1775 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1776
1777 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1778 return -EINVAL;
1779
1780 for (i = 0; i < num_migrate; i++, um++, ma++) {
1781 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1782 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1783 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1784 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1785
1786 ma->proto = um->proto;
1787 ma->mode = um->mode;
1788 ma->reqid = um->reqid;
1789
1790 ma->old_family = um->old_family;
1791 ma->new_family = um->new_family;
1792 }
1793
1794 *num = i;
1795 return 0;
1796}
1797
1798static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1799 struct rtattr **xfrma)
1800{
1801 struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
1802 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1803 u8 type;
1804 int err;
1805 int n = 0;
1806
1807 err = verify_user_migrate((struct rtattr **)xfrma);
1808 if (err)
1809 return err;
1810
1811 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1812 if (err)
1813 return err;
1814
1815 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1816 (struct rtattr **)xfrma, &n);
1817 if (err)
1818 return err;
1819
1820 if (!n)
1821 return 0;
1822
1823 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1824
1825 return 0;
1826}
1827#else
1828static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1829 struct rtattr **xfrma)
1830{
1831 return -ENOPROTOOPT;
1832}
1833#endif
1834
1835#ifdef CONFIG_XFRM_MIGRATE
1836static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1837{
1838 struct xfrm_user_migrate um;
1839
1840 memset(&um, 0, sizeof(um));
1841 um.proto = m->proto;
1842 um.mode = m->mode;
1843 um.reqid = m->reqid;
1844 um.old_family = m->old_family;
1845 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1846 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1847 um.new_family = m->new_family;
1848 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1849 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1850
1851 RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1852 return 0;
1853
1854rtattr_failure:
1855 return -1;
1856}
1857
1858static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1859 int num_migrate, struct xfrm_selector *sel,
1860 u8 dir, u8 type)
1861{
1862 struct xfrm_migrate *mp;
1863 struct xfrm_userpolicy_id *pol_id;
1864 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001865 unsigned char *b = skb_tail_pointer(skb);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001866 int i;
1867
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001868 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1869 if (nlh == NULL)
1870 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001871 pol_id = NLMSG_DATA(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001872
1873 /* copy data from selector, dir, and type to the pol_id */
1874 memset(pol_id, 0, sizeof(*pol_id));
1875 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1876 pol_id->dir = dir;
1877
1878 if (copy_to_user_policy_type(type, skb) < 0)
1879 goto nlmsg_failure;
1880
1881 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1882 if (copy_to_user_migrate(mp, skb) < 0)
1883 goto nlmsg_failure;
1884 }
1885
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001886 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001887 return skb->len;
1888nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001889 nlmsg_trim(skb, b);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001890 return -1;
1891}
1892
1893static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1894 struct xfrm_migrate *m, int num_migrate)
1895{
1896 struct sk_buff *skb;
1897 size_t len;
1898
1899 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1900 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1901#ifdef CONFIG_XFRM_SUB_POLICY
1902 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1903#endif
1904 skb = alloc_skb(len, GFP_ATOMIC);
1905 if (skb == NULL)
1906 return -ENOMEM;
1907
1908 /* build migrate */
1909 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1910 BUG();
1911
1912 NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
1913 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
1914 GFP_ATOMIC);
1915}
1916#else
1917static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1918 struct xfrm_migrate *m, int num_migrate)
1919{
1920 return -ENOPROTOOPT;
1921}
1922#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001923
Thomas Graf492b5582005-05-03 14:26:40 -07001924#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1925
1926static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1927 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1928 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1929 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1930 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1931 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1932 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1933 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001934 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001935 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001936 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1937 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001939 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1940 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001941 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1942 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001943 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001944 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001945 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001946 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947};
1948
Thomas Graf492b5582005-05-03 14:26:40 -07001949#undef XMSGSIZE
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001952 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001954} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1955 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1956 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1957 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1958 .dump = xfrm_dump_sa },
1959 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1960 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1961 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1962 .dump = xfrm_dump_policy },
1963 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001964 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001965 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001966 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1967 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001968 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001969 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1970 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001971 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1972 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001973 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001974 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001975 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976};
1977
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001978static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct rtattr *xfrma[XFRMA_MAX];
1981 struct xfrm_link *link;
Thomas Grafc702e802007-03-22 23:30:55 -07001982 int type, min_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001986 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 type -= XFRM_MSG_BASE;
1989 link = &xfrm_dispatch[type];
1990
1991 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001992 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1993 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Thomas Graf492b5582005-05-03 14:26:40 -07001995 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1996 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1997 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001999 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Thomas Grafc702e802007-03-22 23:30:55 -07002001 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
2004 memset(xfrma, 0, sizeof(xfrma));
2005
2006 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002007 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 if (nlh->nlmsg_len > min_len) {
2010 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2011 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
2012
2013 while (RTA_OK(attr, attrlen)) {
2014 unsigned short flavor = attr->rta_type;
2015 if (flavor) {
2016 if (flavor > XFRMA_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 xfrma[flavor - 1] = attr;
2019 }
2020 attr = RTA_NEXT(attr, attrlen);
2021 }
2022 }
2023
2024 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002025 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002027 return link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030static void xfrm_netlink_rcv(struct sock *sk, int len)
2031{
Thomas Graf88fc2c82005-11-10 02:25:54 +01002032 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002035 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01002036 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002037 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002039 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002042static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct xfrm_user_expire *ue;
2045 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002046 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002048 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2049 if (nlh == NULL)
2050 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 ue = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002054 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002056 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002063 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002065 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (skb == NULL)
2067 return -ENOMEM;
2068
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002069 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 BUG();
2071
Patrick McHardyac6d4392005-08-14 19:29:52 -07002072 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2073 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002076static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2077{
2078 struct sk_buff *skb;
2079 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2080
2081 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2082 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2083 skb = alloc_skb(len, GFP_ATOMIC);
2084 if (skb == NULL)
2085 return -ENOMEM;
2086
2087 if (build_aevent(skb, x, c) < 0)
2088 BUG();
2089
2090 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
2091 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2092}
2093
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094static int xfrm_notify_sa_flush(struct km_event *c)
2095{
2096 struct xfrm_usersa_flush *p;
2097 struct nlmsghdr *nlh;
2098 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002099 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002100 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2101
2102 skb = alloc_skb(len, GFP_ATOMIC);
2103 if (skb == NULL)
2104 return -ENOMEM;
2105 b = skb->tail;
2106
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002107 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2108 if (nlh == NULL) {
2109 kfree_skb(skb);
2110 return -EMSGSIZE;
2111 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002112
2113 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002114 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002115
2116 nlh->nlmsg_len = skb->tail - b;
2117
Patrick McHardyac6d4392005-08-14 19:29:52 -07002118 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2119 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002120}
2121
Dave Jonesb6f99a22007-03-22 12:27:49 -07002122static inline int xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002123{
Herbert Xu0603eac2005-06-18 22:54:36 -07002124 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002125 if (x->aalg)
2126 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
2127 if (x->ealg)
2128 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
2129 if (x->calg)
2130 l += RTA_SPACE(sizeof(*x->calg));
2131 if (x->encap)
2132 l += RTA_SPACE(sizeof(*x->encap));
2133
2134 return l;
2135}
2136
2137static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2138{
2139 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002140 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141 struct nlmsghdr *nlh;
2142 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002143 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002144 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002145 int headlen;
2146
2147 headlen = sizeof(*p);
2148 if (c->event == XFRM_MSG_DELSA) {
2149 len += RTA_SPACE(headlen);
2150 headlen = sizeof(*id);
2151 }
2152 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002153
2154 skb = alloc_skb(len, GFP_ATOMIC);
2155 if (skb == NULL)
2156 return -ENOMEM;
2157 b = skb->tail;
2158
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002159 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2160 if (nlh == NULL)
2161 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002162
2163 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002164 if (c->event == XFRM_MSG_DELSA) {
2165 id = NLMSG_DATA(nlh);
2166 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2167 id->spi = x->id.spi;
2168 id->family = x->props.family;
2169 id->proto = x->id.proto;
2170
2171 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2172 }
2173
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002174 copy_to_user_state(x, p);
2175
2176 if (x->aalg)
2177 RTA_PUT(skb, XFRMA_ALG_AUTH,
2178 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
2179 if (x->ealg)
2180 RTA_PUT(skb, XFRMA_ALG_CRYPT,
2181 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
2182 if (x->calg)
2183 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2184
2185 if (x->encap)
2186 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2187
2188 nlh->nlmsg_len = skb->tail - b;
2189
Patrick McHardyac6d4392005-08-14 19:29:52 -07002190 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2191 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002192
2193nlmsg_failure:
2194rtattr_failure:
2195 kfree_skb(skb);
2196 return -1;
2197}
2198
2199static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2200{
2201
2202 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002203 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002204 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002205 case XFRM_MSG_NEWAE:
2206 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002207 case XFRM_MSG_DELSA:
2208 case XFRM_MSG_UPDSA:
2209 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002210 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002211 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002212 return xfrm_notify_sa_flush(c);
2213 default:
2214 printk("xfrm_user: Unknown SA event %d\n", c->event);
2215 break;
2216 }
2217
2218 return 0;
2219
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2223 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2224 int dir)
2225{
2226 struct xfrm_user_acquire *ua;
2227 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002228 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 __u32 seq = xfrm_get_acqseq();
2230
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002231 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2232 if (nlh == NULL)
2233 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 ua = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 memcpy(&ua->id, &x->id, sizeof(ua->id));
2237 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2238 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2239 copy_to_user_policy(xp, &ua->policy, dir);
2240 ua->aalgos = xt->aalgos;
2241 ua->ealgos = xt->ealgos;
2242 ua->calgos = xt->calgos;
2243 ua->seq = x->km.seq = seq;
2244
2245 if (copy_to_user_tmpl(xp, skb) < 0)
2246 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002247 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002248 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002249 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002250 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002252 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return skb->len;
2254
2255nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002256 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return -1;
2258}
2259
2260static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2261 struct xfrm_policy *xp, int dir)
2262{
2263 struct sk_buff *skb;
2264 size_t len;
2265
2266 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2267 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Joy Latten661697f2007-04-13 16:14:35 -07002268 len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002269#ifdef CONFIG_XFRM_SUB_POLICY
2270 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 skb = alloc_skb(len, GFP_ATOMIC);
2273 if (skb == NULL)
2274 return -ENOMEM;
2275
2276 if (build_acquire(skb, x, xt, xp, dir) < 0)
2277 BUG();
2278
Patrick McHardyac6d4392005-08-14 19:29:52 -07002279 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2280 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283/* User gives us xfrm_user_policy_info followed by an array of 0
2284 * or more templates.
2285 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002286static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 u8 *data, int len, int *dir)
2288{
2289 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2290 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2291 struct xfrm_policy *xp;
2292 int nr;
2293
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002294 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 case AF_INET:
2296 if (opt != IP_XFRM_POLICY) {
2297 *dir = -EOPNOTSUPP;
2298 return NULL;
2299 }
2300 break;
2301#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2302 case AF_INET6:
2303 if (opt != IPV6_XFRM_POLICY) {
2304 *dir = -EOPNOTSUPP;
2305 return NULL;
2306 }
2307 break;
2308#endif
2309 default:
2310 *dir = -EINVAL;
2311 return NULL;
2312 }
2313
2314 *dir = -EINVAL;
2315
2316 if (len < sizeof(*p) ||
2317 verify_newpolicy_info(p))
2318 return NULL;
2319
2320 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002321 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return NULL;
2323
Herbert Xua4f1bac2005-07-26 15:43:17 -07002324 if (p->dir > XFRM_POLICY_OUT)
2325 return NULL;
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 xp = xfrm_policy_alloc(GFP_KERNEL);
2328 if (xp == NULL) {
2329 *dir = -ENOBUFS;
2330 return NULL;
2331 }
2332
2333 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002334 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 copy_templates(xp, ut, nr);
2336
2337 *dir = p->dir;
2338
2339 return xp;
2340}
2341
2342static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002343 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct xfrm_user_polexpire *upe;
2346 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002347 int hard = c->data.hard;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002348 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002350 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2351 if (nlh == NULL)
2352 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 upe = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 copy_to_user_policy(xp, &upe->pol, dir);
2356 if (copy_to_user_tmpl(xp, skb) < 0)
2357 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002358 if (copy_to_user_sec_ctx(xp, skb))
2359 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002360 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002361 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 upe->hard = !!hard;
2363
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002364 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return skb->len;
2366
2367nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002368 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -1;
2370}
2371
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 struct sk_buff *skb;
2375 size_t len;
2376
2377 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2378 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Joy Latten661697f2007-04-13 16:14:35 -07002379 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002380#ifdef CONFIG_XFRM_SUB_POLICY
2381 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 skb = alloc_skb(len, GFP_ATOMIC);
2384 if (skb == NULL)
2385 return -ENOMEM;
2386
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002387 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 BUG();
2389
Patrick McHardyac6d4392005-08-14 19:29:52 -07002390 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2391 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2395{
2396 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002397 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398 struct nlmsghdr *nlh;
2399 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002400 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002402 int headlen;
2403
2404 headlen = sizeof(*p);
2405 if (c->event == XFRM_MSG_DELPOLICY) {
2406 len += RTA_SPACE(headlen);
2407 headlen = sizeof(*id);
2408 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002409#ifdef CONFIG_XFRM_SUB_POLICY
2410 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2411#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002412 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413
2414 skb = alloc_skb(len, GFP_ATOMIC);
2415 if (skb == NULL)
2416 return -ENOMEM;
2417 b = skb->tail;
2418
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002419 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2420 if (nlh == NULL)
2421 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002422
2423 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002424 if (c->event == XFRM_MSG_DELPOLICY) {
2425 id = NLMSG_DATA(nlh);
2426 memset(id, 0, sizeof(*id));
2427 id->dir = dir;
2428 if (c->data.byid)
2429 id->index = xp->index;
2430 else
2431 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2432
2433 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2434 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436 copy_to_user_policy(xp, p, dir);
2437 if (copy_to_user_tmpl(xp, skb) < 0)
2438 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002439 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002440 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002441
2442 nlh->nlmsg_len = skb->tail - b;
2443
Patrick McHardyac6d4392005-08-14 19:29:52 -07002444 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2445 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002446
2447nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002448rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002449 kfree_skb(skb);
2450 return -1;
2451}
2452
2453static int xfrm_notify_policy_flush(struct km_event *c)
2454{
2455 struct nlmsghdr *nlh;
2456 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002457 sk_buff_data_t b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002458 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002459#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002460 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002461#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002462 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463
2464 skb = alloc_skb(len, GFP_ATOMIC);
2465 if (skb == NULL)
2466 return -ENOMEM;
2467 b = skb->tail;
2468
2469
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002470 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2471 if (nlh == NULL)
2472 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002473 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2474 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002475
2476 nlh->nlmsg_len = skb->tail - b;
2477
Patrick McHardyac6d4392005-08-14 19:29:52 -07002478 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2479 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480
2481nlmsg_failure:
2482 kfree_skb(skb);
2483 return -1;
2484}
2485
2486static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2487{
2488
2489 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002490 case XFRM_MSG_NEWPOLICY:
2491 case XFRM_MSG_UPDPOLICY:
2492 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002494 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002495 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002496 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002497 return xfrm_exp_policy_notify(xp, dir, c);
2498 default:
2499 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2500 }
2501
2502 return 0;
2503
2504}
2505
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002506static int build_report(struct sk_buff *skb, u8 proto,
2507 struct xfrm_selector *sel, xfrm_address_t *addr)
2508{
2509 struct xfrm_user_report *ur;
2510 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002511 unsigned char *b = skb_tail_pointer(skb);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002512
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002513 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2514 if (nlh == NULL)
2515 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002516 ur = NLMSG_DATA(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002517
2518 ur->proto = proto;
2519 memcpy(&ur->sel, sel, sizeof(ur->sel));
2520
2521 if (addr)
2522 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2523
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002524 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002525 return skb->len;
2526
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002527rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002528 nlmsg_trim(skb, b);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002529 return -1;
2530}
2531
2532static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2533 xfrm_address_t *addr)
2534{
2535 struct sk_buff *skb;
2536 size_t len;
2537
2538 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2539 skb = alloc_skb(len, GFP_ATOMIC);
2540 if (skb == NULL)
2541 return -ENOMEM;
2542
2543 if (build_report(skb, proto, sel, addr) < 0)
2544 BUG();
2545
2546 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2547 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2548}
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550static struct xfrm_mgr netlink_mgr = {
2551 .id = "netlink",
2552 .notify = xfrm_send_state_notify,
2553 .acquire = xfrm_send_acquire,
2554 .compile_policy = xfrm_compile_policy,
2555 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002556 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002557 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558};
2559
2560static int __init xfrm_user_init(void)
2561{
Patrick McHardybe336902006-03-20 22:40:54 -08002562 struct sock *nlsk;
2563
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002564 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Patrick McHardybe336902006-03-20 22:40:54 -08002566 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002567 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002568 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002570 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 xfrm_register_km(&netlink_mgr);
2573
2574 return 0;
2575}
2576
2577static void __exit xfrm_user_exit(void)
2578{
Patrick McHardybe336902006-03-20 22:40:54 -08002579 struct sock *nlsk = xfrm_nl;
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002582 rcu_assign_pointer(xfrm_nl, NULL);
2583 synchronize_rcu();
2584 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587module_init(xfrm_user_init);
2588module_exit(xfrm_user_exit);
2589MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002590MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002591