blob: 783332987491ae730fc766c7a245ac5df6b8025f [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
Thomas Grafc26445a2007-08-22 13:56:23 -070036static inline int alg_len(struct xfrm_algo *alg)
37{
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
42{
43 struct rtattr *rt = xfrma[type - 1];
44 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070045 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 if (!rt)
48 return 0;
49
Herbert Xu31c26852005-05-19 12:39:49 -070050 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
51 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return -EINVAL;
53
54 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070055
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +090056 len -= (algp->alg_key_len + 7U) / 8;
Herbert Xu31c26852005-05-19 12:39:49 -070057 if (len < 0)
58 return -EINVAL;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 switch (type) {
61 case XFRMA_ALG_AUTH:
62 if (!algp->alg_key_len &&
63 strcmp(algp->alg_name, "digest_null") != 0)
64 return -EINVAL;
65 break;
66
67 case XFRMA_ALG_CRYPT:
68 if (!algp->alg_key_len &&
69 strcmp(algp->alg_name, "cipher_null") != 0)
70 return -EINVAL;
71 break;
72
73 case XFRMA_ALG_COMP:
74 /* Zero length keys are legal. */
75 break;
76
77 default:
78 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
82 return 0;
83}
84
85static int verify_encap_tmpl(struct rtattr **xfrma)
86{
87 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
88 struct xfrm_encap_tmpl *encap;
89
90 if (!rt)
91 return 0;
92
93 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
94 return -EINVAL;
95
96 return 0;
97}
98
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
100 xfrm_address_t **addrp)
101{
102 struct rtattr *rt = xfrma[type - 1];
103
104 if (!rt)
105 return 0;
106
107 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
108 return -EINVAL;
109
110 if (addrp)
111 *addrp = RTA_DATA(rt);
112
113 return 0;
114}
Trent Jaegerdf718372005-12-13 23:12:27 -0800115
116static inline int verify_sec_ctx_len(struct rtattr **xfrma)
117{
118 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
119 struct xfrm_user_sec_ctx *uctx;
120 int len = 0;
121
122 if (!rt)
123 return 0;
124
125 if (rt->rta_len < sizeof(*uctx))
126 return -EINVAL;
127
128 uctx = RTA_DATA(rt);
129
Trent Jaegerdf718372005-12-13 23:12:27 -0800130 len += sizeof(struct xfrm_user_sec_ctx);
131 len += uctx->ctx_len;
132
133 if (uctx->len != len)
134 return -EINVAL;
135
136 return 0;
137}
138
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int verify_newsa_info(struct xfrm_usersa_info *p,
141 struct rtattr **xfrma)
142{
143 int err;
144
145 err = -EINVAL;
146 switch (p->family) {
147 case AF_INET:
148 break;
149
150 case AF_INET6:
151#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
152 break;
153#else
154 err = -EAFNOSUPPORT;
155 goto out;
156#endif
157
158 default:
159 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 err = -EINVAL;
163 switch (p->id.proto) {
164 case IPPROTO_AH:
165 if (!xfrma[XFRMA_ALG_AUTH-1] ||
166 xfrma[XFRMA_ALG_CRYPT-1] ||
167 xfrma[XFRMA_ALG_COMP-1])
168 goto out;
169 break;
170
171 case IPPROTO_ESP:
172 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
173 !xfrma[XFRMA_ALG_CRYPT-1]) ||
174 xfrma[XFRMA_ALG_COMP-1])
175 goto out;
176 break;
177
178 case IPPROTO_COMP:
179 if (!xfrma[XFRMA_ALG_COMP-1] ||
180 xfrma[XFRMA_ALG_AUTH-1] ||
181 xfrma[XFRMA_ALG_CRYPT-1])
182 goto out;
183 break;
184
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700185#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS:
187 case IPPROTO_ROUTING:
188 if (xfrma[XFRMA_ALG_COMP-1] ||
189 xfrma[XFRMA_ALG_AUTH-1] ||
190 xfrma[XFRMA_ALG_CRYPT-1] ||
191 xfrma[XFRMA_ENCAP-1] ||
192 xfrma[XFRMA_SEC_CTX-1] ||
193 !xfrma[XFRMA_COADDR-1])
194 goto out;
195 break;
196#endif
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 default:
199 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
203 goto out;
204 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
205 goto out;
206 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
207 goto out;
208 if ((err = verify_encap_tmpl(xfrma)))
209 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800210 if ((err = verify_sec_ctx_len(xfrma)))
211 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700212 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
213 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 err = -EINVAL;
216 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700217 case XFRM_MODE_TRANSPORT:
218 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700219 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700220 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222
223 default:
224 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 err = 0;
228
229out:
230 return err;
231}
232
233static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
234 struct xfrm_algo_desc *(*get_byname)(char *, int),
235 struct rtattr *u_arg)
236{
237 struct rtattr *rta = u_arg;
238 struct xfrm_algo *p, *ualg;
239 struct xfrm_algo_desc *algo;
240
241 if (!rta)
242 return 0;
243
244 ualg = RTA_DATA(rta);
245
246 algo = get_byname(ualg->alg_name, 1);
247 if (!algo)
248 return -ENOSYS;
249 *props = algo->desc.sadb_alg_id;
250
Thomas Grafc26445a2007-08-22 13:56:23 -0700251 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!p)
253 return -ENOMEM;
254
Herbert Xu04ff1262006-08-13 08:50:00 +1000255 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *algpp = p;
257 return 0;
258}
259
260static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
261{
262 struct rtattr *rta = u_arg;
263 struct xfrm_encap_tmpl *p, *uencap;
264
265 if (!rta)
266 return 0;
267
268 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200269 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!p)
271 return -ENOMEM;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *encapp = p;
274 return 0;
275}
276
Trent Jaegerdf718372005-12-13 23:12:27 -0800277
Joy Latten661697f2007-04-13 16:14:35 -0700278static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800279{
Trent Jaegerdf718372005-12-13 23:12:27 -0800280 int len = 0;
281
282 if (xfrm_ctx) {
283 len += sizeof(struct xfrm_user_sec_ctx);
284 len += xfrm_ctx->ctx_len;
285 }
286 return len;
287}
288
289static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
290{
291 struct xfrm_user_sec_ctx *uctx;
292
293 if (!u_arg)
294 return 0;
295
296 uctx = RTA_DATA(u_arg);
297 return security_xfrm_state_alloc(x, uctx);
298}
299
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700300static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
301{
302 struct rtattr *rta = u_arg;
303 xfrm_address_t *p, *uaddrp;
304
305 if (!rta)
306 return 0;
307
308 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200309 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700310 if (!p)
311 return -ENOMEM;
312
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700313 *addrpp = p;
314 return 0;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
318{
319 memcpy(&x->id, &p->id, sizeof(x->id));
320 memcpy(&x->sel, &p->sel, sizeof(x->sel));
321 memcpy(&x->lft, &p->lft, sizeof(x->lft));
322 x->props.mode = p->mode;
323 x->props.replay_window = p->replay_window;
324 x->props.reqid = p->reqid;
325 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700326 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700328
329 /*
330 * Set inner address family if the KM left it as zero.
331 * See comment in validate_tmpl.
332 */
333 if (!x->sel.family)
334 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800337/*
338 * someday when pfkey also has support, we could have the code
339 * somehow made shareable and move it to xfrm_state.c - JHS
340 *
341*/
342static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
343{
344 int err = - EINVAL;
345 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
346 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
347 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
348 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
349
350 if (rp) {
351 struct xfrm_replay_state *replay;
352 if (RTA_PAYLOAD(rp) < sizeof(*replay))
353 goto error;
354 replay = RTA_DATA(rp);
355 memcpy(&x->replay, replay, sizeof(*replay));
356 memcpy(&x->preplay, replay, sizeof(*replay));
357 }
358
359 if (lt) {
360 struct xfrm_lifetime_cur *ltime;
361 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
362 goto error;
363 ltime = RTA_DATA(lt);
364 x->curlft.bytes = ltime->bytes;
365 x->curlft.packets = ltime->packets;
366 x->curlft.add_time = ltime->add_time;
367 x->curlft.use_time = ltime->use_time;
368 }
369
370 if (et) {
371 if (RTA_PAYLOAD(et) < sizeof(u32))
372 goto error;
373 x->replay_maxage = *(u32*)RTA_DATA(et);
374 }
375
376 if (rt) {
377 if (RTA_PAYLOAD(rt) < sizeof(u32))
378 goto error;
379 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
380 }
381
382 return 0;
383error:
384 return err;
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
388 struct rtattr **xfrma,
389 int *errp)
390{
391 struct xfrm_state *x = xfrm_state_alloc();
392 int err = -ENOMEM;
393
394 if (!x)
395 goto error_no_put;
396
397 copy_from_user_state(x, p);
398
399 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
400 xfrm_aalg_get_byname,
401 xfrma[XFRMA_ALG_AUTH-1])))
402 goto error;
403 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
404 xfrm_ealg_get_byname,
405 xfrma[XFRMA_ALG_CRYPT-1])))
406 goto error;
407 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
408 xfrm_calg_get_byname,
409 xfrma[XFRMA_ALG_COMP-1])))
410 goto error;
411 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
412 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700413 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
414 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700415 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (err)
417 goto error;
418
Trent Jaegerdf718372005-12-13 23:12:27 -0800419 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
420 goto error;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800423 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
424 /* sysctl_xfrm_aevent_etime is in 100ms units */
425 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
426 x->preplay.bitmap = 0;
427 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
428 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
429
430 /* override default values from above */
431
432 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
433 if (err < 0)
434 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return x;
437
438error:
439 x->km.state = XFRM_STATE_DEAD;
440 xfrm_state_put(x);
441error_no_put:
442 *errp = err;
443 return NULL;
444}
445
Christoph Hellwig22e70052007-01-02 15:22:30 -0800446static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
447 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Thomas Graf7b67c852007-08-22 13:53:52 -0700449 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct xfrm_state *x;
451 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700452 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwig22e70052007-01-02 15:22:30 -0800454 err = verify_newsa_info(p, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (err)
456 return err;
457
Christoph Hellwig22e70052007-01-02 15:22:30 -0800458 x = xfrm_state_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!x)
460 return err;
461
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700462 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
464 err = xfrm_state_add(x);
465 else
466 err = xfrm_state_update(x);
467
Joy Latten161a09e2006-11-27 13:11:54 -0600468 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
469 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (err < 0) {
472 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800473 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700477 c.seq = nlh->nlmsg_seq;
478 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700479 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700480
481 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700482out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700483 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return err;
485}
486
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700487static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
488 struct rtattr **xfrma,
489 int *errp)
490{
491 struct xfrm_state *x = NULL;
492 int err;
493
494 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
495 err = -ESRCH;
496 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
497 } else {
498 xfrm_address_t *saddr = NULL;
499
500 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
501 if (err)
502 goto out;
503
504 if (!saddr) {
505 err = -EINVAL;
506 goto out;
507 }
508
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800509 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700510 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
511 p->family);
512 }
513
514 out:
515 if (!x && errp)
516 *errp = err;
517 return x;
518}
519
Christoph Hellwig22e70052007-01-02 15:22:30 -0800520static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
521 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700524 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700525 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700526 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Hellwig22e70052007-01-02 15:22:30 -0800528 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David S. Miller6f68dc32006-06-08 23:58:52 -0700532 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700533 goto out;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700536 err = -EPERM;
537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700540 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600541
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700542 if (err < 0)
543 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544
545 c.seq = nlh->nlmsg_seq;
546 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700547 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700548 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700550out:
Eric Paris16bec312007-03-07 16:02:16 -0800551 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
552 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700553 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
558{
559 memcpy(&p->id, &x->id, sizeof(p->id));
560 memcpy(&p->sel, &x->sel, sizeof(p->sel));
561 memcpy(&p->lft, &x->lft, sizeof(p->lft));
562 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
563 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700564 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 p->mode = x->props.mode;
566 p->replay_window = x->props.replay_window;
567 p->reqid = x->props.reqid;
568 p->family = x->props.family;
569 p->flags = x->props.flags;
570 p->seq = x->km.seq;
571}
572
573struct xfrm_dump_info {
574 struct sk_buff *in_skb;
575 struct sk_buff *out_skb;
576 u32 nlmsg_seq;
577 u16 nlmsg_flags;
578 int start_idx;
579 int this_idx;
580};
581
Thomas Grafc0144be2007-08-22 13:55:43 -0700582static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
583{
584 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
585 struct xfrm_user_sec_ctx *uctx;
586 struct nlattr *attr;
587
588 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
589 if (attr == NULL)
590 return -EMSGSIZE;
591
592 uctx = nla_data(attr);
593 uctx->exttype = XFRMA_SEC_CTX;
594 uctx->len = ctx_size;
595 uctx->ctx_doi = s->ctx_doi;
596 uctx->ctx_alg = s->ctx_alg;
597 uctx->ctx_len = s->ctx_len;
598 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
599
600 return 0;
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
604{
605 struct xfrm_dump_info *sp = ptr;
606 struct sk_buff *in_skb = sp->in_skb;
607 struct sk_buff *skb = sp->out_skb;
608 struct xfrm_usersa_info *p;
609 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (sp->this_idx < sp->start_idx)
612 goto out;
613
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700614 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
615 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
616 if (nlh == NULL)
617 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Thomas Graf7b67c852007-08-22 13:53:52 -0700619 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 copy_to_user_state(x, p);
621
622 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700623 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -0700625 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700627 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700630 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Thomas Grafc0144be2007-08-22 13:55:43 -0700632 if (x->security && copy_sec_ctx(x->security, skb) < 0)
633 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700634
635 if (x->coaddr)
Thomas Grafc0144be2007-08-22 13:55:43 -0700636 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700637
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700638 if (x->lastused)
Thomas Grafc0144be2007-08-22 13:55:43 -0700639 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700640
Thomas Graf98250692007-08-22 12:47:26 -0700641 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642out:
643 sp->this_idx++;
644 return 0;
645
Thomas Grafc0144be2007-08-22 13:55:43 -0700646nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700647 nlmsg_cancel(skb, nlh);
648 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
652{
653 struct xfrm_dump_info info;
654
655 info.in_skb = cb->skb;
656 info.out_skb = skb;
657 info.nlmsg_seq = cb->nlh->nlmsg_seq;
658 info.nlmsg_flags = NLM_F_MULTI;
659 info.this_idx = 0;
660 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700661 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 cb->args[0] = info.this_idx;
663
664 return skb->len;
665}
666
667static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
668 struct xfrm_state *x, u32 seq)
669{
670 struct xfrm_dump_info info;
671 struct sk_buff *skb;
672
Thomas Graf7deb2262007-08-22 13:57:39 -0700673 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!skb)
675 return ERR_PTR(-ENOMEM);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 info.in_skb = in_skb;
678 info.out_skb = skb;
679 info.nlmsg_seq = seq;
680 info.nlmsg_flags = 0;
681 info.this_idx = info.start_idx = 0;
682
683 if (dump_one_state(x, 0, &info)) {
684 kfree_skb(skb);
685 return NULL;
686 }
687
688 return skb;
689}
690
Thomas Graf7deb2262007-08-22 13:57:39 -0700691static inline size_t xfrm_spdinfo_msgsize(void)
692{
693 return NLMSG_ALIGN(4)
694 + nla_total_size(sizeof(struct xfrmu_spdinfo))
695 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
696}
697
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700698static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
699{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700700 struct xfrmk_spdinfo si;
701 struct xfrmu_spdinfo spc;
702 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700703 struct nlmsghdr *nlh;
704 u32 *f;
705
706 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
707 if (nlh == NULL) /* shouldnt really happen ... */
708 return -EMSGSIZE;
709
710 f = nlmsg_data(nlh);
711 *f = flags;
712 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700713 spc.incnt = si.incnt;
714 spc.outcnt = si.outcnt;
715 spc.fwdcnt = si.fwdcnt;
716 spc.inscnt = si.inscnt;
717 spc.outscnt = si.outscnt;
718 spc.fwdscnt = si.fwdscnt;
719 sph.spdhcnt = si.spdhcnt;
720 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700721
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700722 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
723 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700724
725 return nlmsg_end(skb, nlh);
726
727nla_put_failure:
728 nlmsg_cancel(skb, nlh);
729 return -EMSGSIZE;
730}
731
732static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
733 struct rtattr **xfrma)
734{
735 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700736 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700737 u32 spid = NETLINK_CB(skb).pid;
738 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700739
Thomas Graf7deb2262007-08-22 13:57:39 -0700740 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700741 if (r_skb == NULL)
742 return -ENOMEM;
743
744 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
745 BUG();
746
747 return nlmsg_unicast(xfrm_nl, r_skb, spid);
748}
749
Thomas Graf7deb2262007-08-22 13:57:39 -0700750static inline size_t xfrm_sadinfo_msgsize(void)
751{
752 return NLMSG_ALIGN(4)
753 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
754 + nla_total_size(4); /* XFRMA_SAD_CNT */
755}
756
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700757static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
758{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700759 struct xfrmk_sadinfo si;
760 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700761 struct nlmsghdr *nlh;
762 u32 *f;
763
764 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
765 if (nlh == NULL) /* shouldnt really happen ... */
766 return -EMSGSIZE;
767
768 f = nlmsg_data(nlh);
769 *f = flags;
770 xfrm_sad_getinfo(&si);
771
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700772 sh.sadhmcnt = si.sadhmcnt;
773 sh.sadhcnt = si.sadhcnt;
774
775 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
776 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700777
778 return nlmsg_end(skb, nlh);
779
780nla_put_failure:
781 nlmsg_cancel(skb, nlh);
782 return -EMSGSIZE;
783}
784
785static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
786 struct rtattr **xfrma)
787{
788 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700789 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700790 u32 spid = NETLINK_CB(skb).pid;
791 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700792
Thomas Graf7deb2262007-08-22 13:57:39 -0700793 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700794 if (r_skb == NULL)
795 return -ENOMEM;
796
797 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
798 BUG();
799
800 return nlmsg_unicast(xfrm_nl, r_skb, spid);
801}
802
Christoph Hellwig22e70052007-01-02 15:22:30 -0800803static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
804 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Thomas Graf7b67c852007-08-22 13:53:52 -0700806 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct xfrm_state *x;
808 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700809 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Christoph Hellwig22e70052007-01-02 15:22:30 -0800811 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (x == NULL)
813 goto out_noput;
814
815 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
816 if (IS_ERR(resp_skb)) {
817 err = PTR_ERR(resp_skb);
818 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700819 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 xfrm_state_put(x);
822out_noput:
823 return err;
824}
825
826static int verify_userspi_info(struct xfrm_userspi_info *p)
827{
828 switch (p->info.id.proto) {
829 case IPPROTO_AH:
830 case IPPROTO_ESP:
831 break;
832
833 case IPPROTO_COMP:
834 /* IPCOMP spi is 16-bits. */
835 if (p->max >= 0x10000)
836 return -EINVAL;
837 break;
838
839 default:
840 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (p->min > p->max)
844 return -EINVAL;
845
846 return 0;
847}
848
Christoph Hellwig22e70052007-01-02 15:22:30 -0800849static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
850 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct xfrm_state *x;
853 struct xfrm_userspi_info *p;
854 struct sk_buff *resp_skb;
855 xfrm_address_t *daddr;
856 int family;
857 int err;
858
Thomas Graf7b67c852007-08-22 13:53:52 -0700859 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 err = verify_userspi_info(p);
861 if (err)
862 goto out_noput;
863
864 family = p->info.family;
865 daddr = &p->info.id.daddr;
866
867 x = NULL;
868 if (p->info.seq) {
869 x = xfrm_find_acq_byseq(p->info.seq);
870 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
871 xfrm_state_put(x);
872 x = NULL;
873 }
874 }
875
876 if (!x)
877 x = xfrm_find_acq(p->info.mode, p->info.reqid,
878 p->info.id.proto, daddr,
879 &p->info.saddr, 1,
880 family);
881 err = -ENOENT;
882 if (x == NULL)
883 goto out_noput;
884
885 resp_skb = ERR_PTR(-ENOENT);
886
887 spin_lock_bh(&x->lock);
888 if (x->km.state != XFRM_STATE_DEAD) {
889 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
890 if (x->id.spi)
891 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
892 }
893 spin_unlock_bh(&x->lock);
894
895 if (IS_ERR(resp_skb)) {
896 err = PTR_ERR(resp_skb);
897 goto out;
898 }
899
Thomas Graf082a1ad2007-08-22 13:54:36 -0700900 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902out:
903 xfrm_state_put(x);
904out_noput:
905 return err;
906}
907
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800908static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 switch (dir) {
911 case XFRM_POLICY_IN:
912 case XFRM_POLICY_OUT:
913 case XFRM_POLICY_FWD:
914 break;
915
916 default:
917 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return 0;
921}
922
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800923static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700924{
925 switch (type) {
926 case XFRM_POLICY_TYPE_MAIN:
927#ifdef CONFIG_XFRM_SUB_POLICY
928 case XFRM_POLICY_TYPE_SUB:
929#endif
930 break;
931
932 default:
933 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700934 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700935
936 return 0;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
940{
941 switch (p->share) {
942 case XFRM_SHARE_ANY:
943 case XFRM_SHARE_SESSION:
944 case XFRM_SHARE_USER:
945 case XFRM_SHARE_UNIQUE:
946 break;
947
948 default:
949 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 switch (p->action) {
953 case XFRM_POLICY_ALLOW:
954 case XFRM_POLICY_BLOCK:
955 break;
956
957 default:
958 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 switch (p->sel.family) {
962 case AF_INET:
963 break;
964
965 case AF_INET6:
966#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
967 break;
968#else
969 return -EAFNOSUPPORT;
970#endif
971
972 default:
973 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 return verify_policy_dir(p->dir);
977}
978
Trent Jaegerdf718372005-12-13 23:12:27 -0800979static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
980{
981 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
982 struct xfrm_user_sec_ctx *uctx;
983
984 if (!rt)
985 return 0;
986
987 uctx = RTA_DATA(rt);
988 return security_xfrm_policy_alloc(pol, uctx);
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
992 int nr)
993{
994 int i;
995
996 xp->xfrm_nr = nr;
997 for (i = 0; i < nr; i++, ut++) {
998 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
999
1000 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1001 memcpy(&t->saddr, &ut->saddr,
1002 sizeof(xfrm_address_t));
1003 t->reqid = ut->reqid;
1004 t->mode = ut->mode;
1005 t->share = ut->share;
1006 t->optional = ut->optional;
1007 t->aalgos = ut->aalgos;
1008 t->ealgos = ut->ealgos;
1009 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -08001010 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012}
1013
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001014static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1015{
1016 int i;
1017
1018 if (nr > XFRM_MAX_DEPTH)
1019 return -EINVAL;
1020
1021 for (i = 0; i < nr; i++) {
1022 /* We never validated the ut->family value, so many
1023 * applications simply leave it at zero. The check was
1024 * never made and ut->family was ignored because all
1025 * templates could be assumed to have the same family as
1026 * the policy itself. Now that we will have ipv4-in-ipv6
1027 * and ipv6-in-ipv4 tunnels, this is no longer true.
1028 */
1029 if (!ut[i].family)
1030 ut[i].family = family;
1031
1032 switch (ut[i].family) {
1033 case AF_INET:
1034 break;
1035#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1036 case AF_INET6:
1037 break;
1038#endif
1039 default:
1040 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001041 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001042 }
1043
1044 return 0;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1048{
1049 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!rt) {
1052 pol->xfrm_nr = 0;
1053 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001054 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1055 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1056 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001058 err = validate_tmpl(nr, utmpl, pol->family);
1059 if (err)
1060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 copy_templates(pol, RTA_DATA(rt), nr);
1063 }
1064 return 0;
1065}
1066
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001067static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1068{
1069 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1070 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001071 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001072 int err;
1073
1074 if (rt) {
1075 if (rt->rta_len < sizeof(*upt))
1076 return -EINVAL;
1077
1078 upt = RTA_DATA(rt);
1079 type = upt->type;
1080 }
1081
1082 err = verify_policy_type(type);
1083 if (err)
1084 return err;
1085
1086 *tp = type;
1087 return 0;
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1091{
1092 xp->priority = p->priority;
1093 xp->index = p->index;
1094 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1095 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1096 xp->action = p->action;
1097 xp->flags = p->flags;
1098 xp->family = p->sel.family;
1099 /* XXX xp->share = p->share; */
1100}
1101
1102static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1103{
1104 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1105 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1106 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1107 p->priority = xp->priority;
1108 p->index = xp->index;
1109 p->sel.family = xp->family;
1110 p->dir = dir;
1111 p->action = xp->action;
1112 p->flags = xp->flags;
1113 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1114}
1115
1116static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1117{
1118 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1119 int err;
1120
1121 if (!xp) {
1122 *errp = -ENOMEM;
1123 return NULL;
1124 }
1125
1126 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001127
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001128 err = copy_from_user_policy_type(&xp->type, xfrma);
1129 if (err)
1130 goto error;
1131
Trent Jaegerdf718372005-12-13 23:12:27 -08001132 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1133 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134 if (err)
1135 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001138 error:
1139 *errp = err;
1140 kfree(xp);
1141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Christoph Hellwig22e70052007-01-02 15:22:30 -08001144static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1145 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Thomas Graf7b67c852007-08-22 13:53:52 -07001147 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001149 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int err;
1151 int excl;
1152
1153 err = verify_newpolicy_info(p);
1154 if (err)
1155 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001156 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001157 if (err)
1158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christoph Hellwig22e70052007-01-02 15:22:30 -08001160 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!xp)
1162 return err;
1163
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001164 /* shouldnt excl be based on nlh flags??
1165 * Aha! this is anti-netlink really i.e more pfkey derived
1166 * in netlink excl is a flag and you wouldnt need
1167 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1169 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001170 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1171 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001174 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 kfree(xp);
1176 return err;
1177 }
1178
Herbert Xuf60f6b82005-06-18 22:44:37 -07001179 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001180 c.seq = nlh->nlmsg_seq;
1181 c.pid = nlh->nlmsg_pid;
1182 km_policy_notify(xp, p->dir, &c);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 xfrm_pol_put(xp);
1185
1186 return 0;
1187}
1188
1189static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1190{
1191 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1192 int i;
1193
1194 if (xp->xfrm_nr == 0)
1195 return 0;
1196
1197 for (i = 0; i < xp->xfrm_nr; i++) {
1198 struct xfrm_user_tmpl *up = &vec[i];
1199 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1200
1201 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001202 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1204 up->reqid = kp->reqid;
1205 up->mode = kp->mode;
1206 up->share = kp->share;
1207 up->optional = kp->optional;
1208 up->aalgos = kp->aalgos;
1209 up->ealgos = kp->ealgos;
1210 up->calgos = kp->calgos;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Thomas Grafc0144be2007-08-22 13:55:43 -07001213 return nla_put(skb, XFRMA_TMPL,
1214 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001215}
1216
Serge Hallyn0d681622006-07-24 23:30:44 -07001217static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1218{
1219 if (x->security) {
1220 return copy_sec_ctx(x->security, skb);
1221 }
1222 return 0;
1223}
1224
1225static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1226{
1227 if (xp->security) {
1228 return copy_sec_ctx(xp->security, skb);
1229 }
1230 return 0;
1231}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001232static inline size_t userpolicy_type_attrsize(void)
1233{
1234#ifdef CONFIG_XFRM_SUB_POLICY
1235 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1236#else
1237 return 0;
1238#endif
1239}
Serge Hallyn0d681622006-07-24 23:30:44 -07001240
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001241#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001242static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001243{
Thomas Grafc0144be2007-08-22 13:55:43 -07001244 struct xfrm_userpolicy_type upt = {
1245 .type = type,
1246 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001247
Thomas Grafc0144be2007-08-22 13:55:43 -07001248 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001249}
1250
1251#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001252static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001253{
1254 return 0;
1255}
1256#endif
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1259{
1260 struct xfrm_dump_info *sp = ptr;
1261 struct xfrm_userpolicy_info *p;
1262 struct sk_buff *in_skb = sp->in_skb;
1263 struct sk_buff *skb = sp->out_skb;
1264 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 if (sp->this_idx < sp->start_idx)
1267 goto out;
1268
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001269 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1270 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1271 if (nlh == NULL)
1272 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Thomas Graf7b67c852007-08-22 13:53:52 -07001274 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 copy_to_user_policy(xp, p, dir);
1276 if (copy_to_user_tmpl(xp, skb) < 0)
1277 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001278 if (copy_to_user_sec_ctx(xp, skb))
1279 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001280 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001281 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Thomas Graf98250692007-08-22 12:47:26 -07001283 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284out:
1285 sp->this_idx++;
1286 return 0;
1287
1288nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001289 nlmsg_cancel(skb, nlh);
1290 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1294{
1295 struct xfrm_dump_info info;
1296
1297 info.in_skb = cb->skb;
1298 info.out_skb = skb;
1299 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1300 info.nlmsg_flags = NLM_F_MULTI;
1301 info.this_idx = 0;
1302 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001303 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1304#ifdef CONFIG_XFRM_SUB_POLICY
1305 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 cb->args[0] = info.this_idx;
1308
1309 return skb->len;
1310}
1311
1312static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1313 struct xfrm_policy *xp,
1314 int dir, u32 seq)
1315{
1316 struct xfrm_dump_info info;
1317 struct sk_buff *skb;
1318
Thomas Graf7deb2262007-08-22 13:57:39 -07001319 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!skb)
1321 return ERR_PTR(-ENOMEM);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 info.in_skb = in_skb;
1324 info.out_skb = skb;
1325 info.nlmsg_seq = seq;
1326 info.nlmsg_flags = 0;
1327 info.this_idx = info.start_idx = 0;
1328
1329 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1330 kfree_skb(skb);
1331 return NULL;
1332 }
1333
1334 return skb;
1335}
1336
Christoph Hellwig22e70052007-01-02 15:22:30 -08001337static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1338 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct xfrm_policy *xp;
1341 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001342 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001344 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int delete;
1346
Thomas Graf7b67c852007-08-22 13:53:52 -07001347 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1349
Christoph Hellwig22e70052007-01-02 15:22:30 -08001350 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001351 if (err)
1352 return err;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 err = verify_policy_dir(p->dir);
1355 if (err)
1356 return err;
1357
1358 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001359 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001360 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001361 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001362 struct xfrm_policy tmp;
1363
Christoph Hellwig22e70052007-01-02 15:22:30 -08001364 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001365 if (err)
1366 return err;
1367
1368 memset(&tmp, 0, sizeof(struct xfrm_policy));
1369 if (rt) {
1370 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1371
1372 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1373 return err;
1374 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001375 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1376 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001377 security_xfrm_policy_free(&tmp);
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (xp == NULL)
1380 return -ENOENT;
1381
1382 if (!delete) {
1383 struct sk_buff *resp_skb;
1384
1385 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1386 if (IS_ERR(resp_skb)) {
1387 err = PTR_ERR(resp_skb);
1388 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001389 err = nlmsg_unicast(xfrm_nl, resp_skb,
1390 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001392 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001393 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1394 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1395
1396 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001397 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001398
Herbert Xue7443892005-06-18 22:44:18 -07001399 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001400 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001401 c.seq = nlh->nlmsg_seq;
1402 c.pid = nlh->nlmsg_pid;
1403 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001406out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001407 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return err;
1409}
1410
Christoph Hellwig22e70052007-01-02 15:22:30 -08001411static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1412 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001414 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001415 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001416 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001417 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Joy Latten161a09e2006-11-27 13:11:54 -06001419 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1420 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001421 err = xfrm_state_flush(p->proto, &audit_info);
1422 if (err)
1423 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001424 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001425 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001426 c.seq = nlh->nlmsg_seq;
1427 c.pid = nlh->nlmsg_pid;
1428 km_state_notify(NULL, &c);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431}
1432
Thomas Graf7deb2262007-08-22 13:57:39 -07001433static inline size_t xfrm_aevent_msgsize(void)
1434{
1435 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1436 + nla_total_size(sizeof(struct xfrm_replay_state))
1437 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1438 + nla_total_size(4) /* XFRM_AE_RTHR */
1439 + nla_total_size(4); /* XFRM_AE_ETHR */
1440}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001441
1442static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1443{
1444 struct xfrm_aevent_id *id;
1445 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001446
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001447 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1448 if (nlh == NULL)
1449 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450
Thomas Graf7b67c852007-08-22 13:53:52 -07001451 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001452 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001453 id->sa_id.spi = x->id.spi;
1454 id->sa_id.family = x->props.family;
1455 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001456 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1457 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001458 id->flags = c->data.aevent;
1459
Thomas Grafc0144be2007-08-22 13:55:43 -07001460 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1461 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001462
Thomas Grafc0144be2007-08-22 13:55:43 -07001463 if (id->flags & XFRM_AE_RTHR)
1464 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001465
Thomas Grafc0144be2007-08-22 13:55:43 -07001466 if (id->flags & XFRM_AE_ETHR)
1467 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1468 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001469
Thomas Graf98250692007-08-22 12:47:26 -07001470 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001471
Thomas Grafc0144be2007-08-22 13:55:43 -07001472nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001473 nlmsg_cancel(skb, nlh);
1474 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001475}
1476
Christoph Hellwig22e70052007-01-02 15:22:30 -08001477static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1478 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001479{
1480 struct xfrm_state *x;
1481 struct sk_buff *r_skb;
1482 int err;
1483 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001484 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001485 struct xfrm_usersa_id *id = &p->sa_id;
1486
Thomas Graf7deb2262007-08-22 13:57:39 -07001487 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001488 if (r_skb == NULL)
1489 return -ENOMEM;
1490
1491 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1492 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001493 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001494 return -ESRCH;
1495 }
1496
1497 /*
1498 * XXX: is this lock really needed - none of the other
1499 * gets lock (the concern is things getting updated
1500 * while we are still reading) - jhs
1501 */
1502 spin_lock_bh(&x->lock);
1503 c.data.aevent = p->flags;
1504 c.seq = nlh->nlmsg_seq;
1505 c.pid = nlh->nlmsg_pid;
1506
1507 if (build_aevent(r_skb, x, &c) < 0)
1508 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001509 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001510 spin_unlock_bh(&x->lock);
1511 xfrm_state_put(x);
1512 return err;
1513}
1514
Christoph Hellwig22e70052007-01-02 15:22:30 -08001515static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1516 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001517{
1518 struct xfrm_state *x;
1519 struct km_event c;
1520 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001521 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001522 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1523 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1524
1525 if (!lt && !rp)
1526 return err;
1527
1528 /* pedantic mode - thou shalt sayeth replaceth */
1529 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1530 return err;
1531
1532 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1533 if (x == NULL)
1534 return -ESRCH;
1535
1536 if (x->km.state != XFRM_STATE_VALID)
1537 goto out;
1538
1539 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001540 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001541 spin_unlock_bh(&x->lock);
1542 if (err < 0)
1543 goto out;
1544
1545 c.event = nlh->nlmsg_type;
1546 c.seq = nlh->nlmsg_seq;
1547 c.pid = nlh->nlmsg_pid;
1548 c.data.aevent = XFRM_AE_CU;
1549 km_state_notify(x, &c);
1550 err = 0;
1551out:
1552 xfrm_state_put(x);
1553 return err;
1554}
1555
Christoph Hellwig22e70052007-01-02 15:22:30 -08001556static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1557 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001559 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001560 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001561 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001562 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001563
Christoph Hellwig22e70052007-01-02 15:22:30 -08001564 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001565 if (err)
1566 return err;
1567
Joy Latten161a09e2006-11-27 13:11:54 -06001568 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1569 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001570 err = xfrm_policy_flush(type, &audit_info);
1571 if (err)
1572 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001573 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001574 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001575 c.seq = nlh->nlmsg_seq;
1576 c.pid = nlh->nlmsg_pid;
1577 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580
Christoph Hellwig22e70052007-01-02 15:22:30 -08001581static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1582 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001583{
1584 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001585 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001586 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001587 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001588 int err = -ENOENT;
1589
Christoph Hellwig22e70052007-01-02 15:22:30 -08001590 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001591 if (err)
1592 return err;
1593
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001594 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001595 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001596 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001597 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001598 struct xfrm_policy tmp;
1599
Christoph Hellwig22e70052007-01-02 15:22:30 -08001600 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001601 if (err)
1602 return err;
1603
1604 memset(&tmp, 0, sizeof(struct xfrm_policy));
1605 if (rt) {
1606 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1607
1608 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1609 return err;
1610 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001611 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1612 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001613 security_xfrm_policy_free(&tmp);
1614 }
1615
1616 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001617 return -ENOENT;
1618 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001619 if (xp->dead) {
1620 read_unlock(&xp->lock);
1621 goto out;
1622 }
1623
1624 read_unlock(&xp->lock);
1625 err = 0;
1626 if (up->hard) {
1627 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001628 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1629 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1630
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001631 } else {
1632 // reset the timers here?
1633 printk("Dont know what to do with soft policy expire\n");
1634 }
1635 km_policy_expired(xp, p->dir, up->hard, current->pid);
1636
1637out:
1638 xfrm_pol_put(xp);
1639 return err;
1640}
1641
Christoph Hellwig22e70052007-01-02 15:22:30 -08001642static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1643 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001644{
1645 struct xfrm_state *x;
1646 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001647 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001648 struct xfrm_usersa_info *p = &ue->state;
1649
1650 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001651
David S. Miller3a765aa2007-02-26 14:52:21 -08001652 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001653 if (x == NULL)
1654 return err;
1655
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001656 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001657 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001658 if (x->km.state != XFRM_STATE_VALID)
1659 goto out;
1660 km_state_expired(x, ue->hard, current->pid);
1661
Joy Latten161a09e2006-11-27 13:11:54 -06001662 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001663 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001664 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1665 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1666 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001667 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001668out:
1669 spin_unlock_bh(&x->lock);
1670 xfrm_state_put(x);
1671 return err;
1672}
1673
Christoph Hellwig22e70052007-01-02 15:22:30 -08001674static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1675 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001676{
1677 struct xfrm_policy *xp;
1678 struct xfrm_user_tmpl *ut;
1679 int i;
1680 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1681
Thomas Graf7b67c852007-08-22 13:53:52 -07001682 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001683 struct xfrm_state *x = xfrm_state_alloc();
1684 int err = -ENOMEM;
1685
1686 if (!x)
1687 return err;
1688
1689 err = verify_newpolicy_info(&ua->policy);
1690 if (err) {
1691 printk("BAD policy passed\n");
1692 kfree(x);
1693 return err;
1694 }
1695
1696 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001697 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1698 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001699 kfree(x);
1700 return err;
1701 }
1702
1703 memcpy(&x->id, &ua->id, sizeof(ua->id));
1704 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1705 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1706
1707 ut = RTA_DATA(rt);
1708 /* extract the templates and for each call km_key */
1709 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1710 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1711 memcpy(&x->id, &t->id, sizeof(x->id));
1712 x->props.mode = t->mode;
1713 x->props.reqid = t->reqid;
1714 x->props.family = ut->family;
1715 t->aalgos = ua->aalgos;
1716 t->ealgos = ua->ealgos;
1717 t->calgos = ua->calgos;
1718 err = km_query(x, t, xp);
1719
1720 }
1721
1722 kfree(x);
1723 kfree(xp);
1724
1725 return 0;
1726}
1727
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001728#ifdef CONFIG_XFRM_MIGRATE
1729static int verify_user_migrate(struct rtattr **xfrma)
1730{
1731 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1732 struct xfrm_user_migrate *um;
1733
1734 if (!rt)
1735 return -EINVAL;
1736
1737 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1738 return -EINVAL;
1739
1740 return 0;
1741}
1742
1743static int copy_from_user_migrate(struct xfrm_migrate *ma,
1744 struct rtattr **xfrma, int *num)
1745{
1746 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1747 struct xfrm_user_migrate *um;
1748 int i, num_migrate;
1749
1750 um = RTA_DATA(rt);
1751 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1752
1753 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1754 return -EINVAL;
1755
1756 for (i = 0; i < num_migrate; i++, um++, ma++) {
1757 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1758 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1759 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1760 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1761
1762 ma->proto = um->proto;
1763 ma->mode = um->mode;
1764 ma->reqid = um->reqid;
1765
1766 ma->old_family = um->old_family;
1767 ma->new_family = um->new_family;
1768 }
1769
1770 *num = i;
1771 return 0;
1772}
1773
1774static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1775 struct rtattr **xfrma)
1776{
Thomas Graf7b67c852007-08-22 13:53:52 -07001777 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001778 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1779 u8 type;
1780 int err;
1781 int n = 0;
1782
1783 err = verify_user_migrate((struct rtattr **)xfrma);
1784 if (err)
1785 return err;
1786
1787 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1788 if (err)
1789 return err;
1790
1791 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1792 (struct rtattr **)xfrma, &n);
1793 if (err)
1794 return err;
1795
1796 if (!n)
1797 return 0;
1798
1799 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1800
1801 return 0;
1802}
1803#else
1804static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1805 struct rtattr **xfrma)
1806{
1807 return -ENOPROTOOPT;
1808}
1809#endif
1810
1811#ifdef CONFIG_XFRM_MIGRATE
1812static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1813{
1814 struct xfrm_user_migrate um;
1815
1816 memset(&um, 0, sizeof(um));
1817 um.proto = m->proto;
1818 um.mode = m->mode;
1819 um.reqid = m->reqid;
1820 um.old_family = m->old_family;
1821 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1822 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1823 um.new_family = m->new_family;
1824 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1825 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1826
Thomas Grafc0144be2007-08-22 13:55:43 -07001827 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001828}
1829
Thomas Graf7deb2262007-08-22 13:57:39 -07001830static inline size_t xfrm_migrate_msgsize(int num_migrate)
1831{
1832 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1833 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1834 + userpolicy_type_attrsize();
1835}
1836
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001837static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1838 int num_migrate, struct xfrm_selector *sel,
1839 u8 dir, u8 type)
1840{
1841 struct xfrm_migrate *mp;
1842 struct xfrm_userpolicy_id *pol_id;
1843 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001844 int i;
1845
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001846 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1847 if (nlh == NULL)
1848 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001849
Thomas Graf7b67c852007-08-22 13:53:52 -07001850 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001851 /* copy data from selector, dir, and type to the pol_id */
1852 memset(pol_id, 0, sizeof(*pol_id));
1853 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1854 pol_id->dir = dir;
1855
1856 if (copy_to_user_policy_type(type, skb) < 0)
1857 goto nlmsg_failure;
1858
1859 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1860 if (copy_to_user_migrate(mp, skb) < 0)
1861 goto nlmsg_failure;
1862 }
1863
Thomas Graf98250692007-08-22 12:47:26 -07001864 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001865nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001866 nlmsg_cancel(skb, nlh);
1867 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001868}
1869
1870static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1871 struct xfrm_migrate *m, int num_migrate)
1872{
1873 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001874
Thomas Graf7deb2262007-08-22 13:57:39 -07001875 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001876 if (skb == NULL)
1877 return -ENOMEM;
1878
1879 /* build migrate */
1880 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1881 BUG();
1882
Thomas Graf082a1ad2007-08-22 13:54:36 -07001883 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001884}
1885#else
1886static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1887 struct xfrm_migrate *m, int num_migrate)
1888{
1889 return -ENOPROTOOPT;
1890}
1891#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001892
Thomas Graf492b5582005-05-03 14:26:40 -07001893#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1894
1895static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1896 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1897 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1898 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1899 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1900 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1901 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1902 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001903 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001904 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001905 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1906 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001907 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001908 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1909 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001910 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1911 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001912 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001913 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001914 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001915 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916};
1917
Thomas Graf492b5582005-05-03 14:26:40 -07001918#undef XMSGSIZE
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001921 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001923} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1924 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1925 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1926 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1927 .dump = xfrm_dump_sa },
1928 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1929 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1930 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1931 .dump = xfrm_dump_policy },
1932 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001933 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001934 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001935 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1936 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001937 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001938 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1939 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001940 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1941 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001942 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001943 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001944 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945};
1946
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001947static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct rtattr *xfrma[XFRMA_MAX];
1950 struct xfrm_link *link;
Thomas Grafc702e802007-03-22 23:30:55 -07001951 int type, min_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 type -= XFRM_MSG_BASE;
1958 link = &xfrm_dispatch[type];
1959
1960 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001961 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1962 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Thomas Graf492b5582005-05-03 14:26:40 -07001964 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1965 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1966 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Thomas Grafc702e802007-03-22 23:30:55 -07001970 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 memset(xfrma, 0, sizeof(xfrma));
1974
1975 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 if (nlh->nlmsg_len > min_len) {
1979 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1980 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1981
1982 while (RTA_OK(attr, attrlen)) {
1983 unsigned short flavor = attr->rta_type;
1984 if (flavor) {
1985 if (flavor > XFRMA_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001986 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 xfrma[flavor - 1] = attr;
1988 }
1989 attr = RTA_NEXT(attr, attrlen);
1990 }
1991 }
1992
1993 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001996 return link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999static void xfrm_netlink_rcv(struct sock *sk, int len)
2000{
Thomas Graf88fc2c82005-11-10 02:25:54 +01002001 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002004 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01002005 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002006 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002008 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Thomas Graf7deb2262007-08-22 13:57:39 -07002011static inline size_t xfrm_expire_msgsize(void)
2012{
2013 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2014}
2015
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002016static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct xfrm_user_expire *ue;
2019 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002021 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2022 if (nlh == NULL)
2023 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Thomas Graf7b67c852007-08-22 13:53:52 -07002025 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002027 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Thomas Graf98250692007-08-22 12:47:26 -07002029 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct sk_buff *skb;
2035
Thomas Graf7deb2262007-08-22 13:57:39 -07002036 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (skb == NULL)
2038 return -ENOMEM;
2039
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002040 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 BUG();
2042
Thomas Graf082a1ad2007-08-22 13:54:36 -07002043 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002046static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2047{
2048 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002049
Thomas Graf7deb2262007-08-22 13:57:39 -07002050 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002051 if (skb == NULL)
2052 return -ENOMEM;
2053
2054 if (build_aevent(skb, x, c) < 0)
2055 BUG();
2056
Thomas Graf082a1ad2007-08-22 13:54:36 -07002057 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002058}
2059
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060static int xfrm_notify_sa_flush(struct km_event *c)
2061{
2062 struct xfrm_usersa_flush *p;
2063 struct nlmsghdr *nlh;
2064 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002065 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066
Thomas Graf7deb2262007-08-22 13:57:39 -07002067 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002068 if (skb == NULL)
2069 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002070
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002071 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2072 if (nlh == NULL) {
2073 kfree_skb(skb);
2074 return -EMSGSIZE;
2075 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002076
Thomas Graf7b67c852007-08-22 13:53:52 -07002077 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002078 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002079
Thomas Graf98250692007-08-22 12:47:26 -07002080 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002081
Thomas Graf082a1ad2007-08-22 13:54:36 -07002082 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083}
2084
Thomas Graf7deb2262007-08-22 13:57:39 -07002085static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002086{
Thomas Graf7deb2262007-08-22 13:57:39 -07002087 size_t l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002088 if (x->aalg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002089 l += nla_total_size(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002090 if (x->ealg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002091 l += nla_total_size(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002092 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002093 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002095 l += nla_total_size(sizeof(*x->encap));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002096
2097 return l;
2098}
2099
2100static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2101{
2102 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002103 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002104 struct nlmsghdr *nlh;
2105 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002106 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002107 int headlen;
2108
2109 headlen = sizeof(*p);
2110 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002111 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002112 headlen = sizeof(*id);
2113 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002114 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002115
Thomas Graf7deb2262007-08-22 13:57:39 -07002116 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002117 if (skb == NULL)
2118 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002119
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002120 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2121 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002122 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002123
Thomas Graf7b67c852007-08-22 13:53:52 -07002124 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002125 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002126 struct nlattr *attr;
2127
Thomas Graf7b67c852007-08-22 13:53:52 -07002128 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002129 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2130 id->spi = x->id.spi;
2131 id->family = x->props.family;
2132 id->proto = x->id.proto;
2133
Thomas Grafc0144be2007-08-22 13:55:43 -07002134 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2135 if (attr == NULL)
2136 goto nla_put_failure;
2137
2138 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002139 }
2140
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141 copy_to_user_state(x, p);
2142
2143 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002144 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002145 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002146 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002147 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002148 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002149
2150 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002151 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002152
Thomas Graf98250692007-08-22 12:47:26 -07002153 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154
Thomas Graf082a1ad2007-08-22 13:54:36 -07002155 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002156
Thomas Grafc0144be2007-08-22 13:55:43 -07002157nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002158 kfree_skb(skb);
2159 return -1;
2160}
2161
2162static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2163{
2164
2165 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002166 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002167 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002168 case XFRM_MSG_NEWAE:
2169 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002170 case XFRM_MSG_DELSA:
2171 case XFRM_MSG_UPDSA:
2172 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002173 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002174 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002175 return xfrm_notify_sa_flush(c);
2176 default:
2177 printk("xfrm_user: Unknown SA event %d\n", c->event);
2178 break;
2179 }
2180
2181 return 0;
2182
2183}
2184
Thomas Graf7deb2262007-08-22 13:57:39 -07002185static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2186 struct xfrm_policy *xp)
2187{
2188 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2189 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2190 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2191 + userpolicy_type_attrsize();
2192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2195 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2196 int dir)
2197{
2198 struct xfrm_user_acquire *ua;
2199 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 __u32 seq = xfrm_get_acqseq();
2201
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002202 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2203 if (nlh == NULL)
2204 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Thomas Graf7b67c852007-08-22 13:53:52 -07002206 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 memcpy(&ua->id, &x->id, sizeof(ua->id));
2208 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2209 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2210 copy_to_user_policy(xp, &ua->policy, dir);
2211 ua->aalgos = xt->aalgos;
2212 ua->ealgos = xt->ealgos;
2213 ua->calgos = xt->calgos;
2214 ua->seq = x->km.seq = seq;
2215
2216 if (copy_to_user_tmpl(xp, skb) < 0)
2217 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002218 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002219 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002220 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002221 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Thomas Graf98250692007-08-22 12:47:26 -07002223 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002226 nlmsg_cancel(skb, nlh);
2227 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2231 struct xfrm_policy *xp, int dir)
2232{
2233 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Thomas Graf7deb2262007-08-22 13:57:39 -07002235 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (skb == NULL)
2237 return -ENOMEM;
2238
2239 if (build_acquire(skb, x, xt, xp, dir) < 0)
2240 BUG();
2241
Thomas Graf082a1ad2007-08-22 13:54:36 -07002242 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245/* User gives us xfrm_user_policy_info followed by an array of 0
2246 * or more templates.
2247 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002248static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 u8 *data, int len, int *dir)
2250{
2251 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2252 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2253 struct xfrm_policy *xp;
2254 int nr;
2255
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002256 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 case AF_INET:
2258 if (opt != IP_XFRM_POLICY) {
2259 *dir = -EOPNOTSUPP;
2260 return NULL;
2261 }
2262 break;
2263#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2264 case AF_INET6:
2265 if (opt != IPV6_XFRM_POLICY) {
2266 *dir = -EOPNOTSUPP;
2267 return NULL;
2268 }
2269 break;
2270#endif
2271 default:
2272 *dir = -EINVAL;
2273 return NULL;
2274 }
2275
2276 *dir = -EINVAL;
2277
2278 if (len < sizeof(*p) ||
2279 verify_newpolicy_info(p))
2280 return NULL;
2281
2282 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002283 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return NULL;
2285
Herbert Xua4f1bac2005-07-26 15:43:17 -07002286 if (p->dir > XFRM_POLICY_OUT)
2287 return NULL;
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 xp = xfrm_policy_alloc(GFP_KERNEL);
2290 if (xp == NULL) {
2291 *dir = -ENOBUFS;
2292 return NULL;
2293 }
2294
2295 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002296 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 copy_templates(xp, ut, nr);
2298
2299 *dir = p->dir;
2300
2301 return xp;
2302}
2303
Thomas Graf7deb2262007-08-22 13:57:39 -07002304static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2305{
2306 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2307 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2308 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2309 + userpolicy_type_attrsize();
2310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002313 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct xfrm_user_polexpire *upe;
2316 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002317 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002319 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2320 if (nlh == NULL)
2321 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Thomas Graf7b67c852007-08-22 13:53:52 -07002323 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 copy_to_user_policy(xp, &upe->pol, dir);
2325 if (copy_to_user_tmpl(xp, skb) < 0)
2326 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002327 if (copy_to_user_sec_ctx(xp, skb))
2328 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002329 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002330 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 upe->hard = !!hard;
2332
Thomas Graf98250692007-08-22 12:47:26 -07002333 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002336 nlmsg_cancel(skb, nlh);
2337 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Thomas Graf7deb2262007-08-22 13:57:39 -07002344 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (skb == NULL)
2346 return -ENOMEM;
2347
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002348 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 BUG();
2350
Thomas Graf082a1ad2007-08-22 13:54:36 -07002351 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002354static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2355{
2356 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002357 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358 struct nlmsghdr *nlh;
2359 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002360 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002361 int headlen;
2362
2363 headlen = sizeof(*p);
2364 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002365 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002366 headlen = sizeof(*id);
2367 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002368 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002369 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370
Thomas Graf7deb2262007-08-22 13:57:39 -07002371 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372 if (skb == NULL)
2373 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002375 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2376 if (nlh == NULL)
2377 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Thomas Graf7b67c852007-08-22 13:53:52 -07002379 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002380 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002381 struct nlattr *attr;
2382
Thomas Graf7b67c852007-08-22 13:53:52 -07002383 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002384 memset(id, 0, sizeof(*id));
2385 id->dir = dir;
2386 if (c->data.byid)
2387 id->index = xp->index;
2388 else
2389 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2390
Thomas Grafc0144be2007-08-22 13:55:43 -07002391 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2392 if (attr == NULL)
2393 goto nlmsg_failure;
2394
2395 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002396 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398 copy_to_user_policy(xp, p, dir);
2399 if (copy_to_user_tmpl(xp, skb) < 0)
2400 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002401 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002402 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403
Thomas Graf98250692007-08-22 12:47:26 -07002404 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405
Thomas Graf082a1ad2007-08-22 13:54:36 -07002406 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002407
2408nlmsg_failure:
2409 kfree_skb(skb);
2410 return -1;
2411}
2412
2413static int xfrm_notify_policy_flush(struct km_event *c)
2414{
2415 struct nlmsghdr *nlh;
2416 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417
Thomas Graf7deb2262007-08-22 13:57:39 -07002418 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419 if (skb == NULL)
2420 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002421
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002422 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2423 if (nlh == NULL)
2424 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002425 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2426 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002427
Thomas Graf98250692007-08-22 12:47:26 -07002428 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002429
Thomas Graf082a1ad2007-08-22 13:54:36 -07002430 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431
2432nlmsg_failure:
2433 kfree_skb(skb);
2434 return -1;
2435}
2436
2437static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2438{
2439
2440 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002441 case XFRM_MSG_NEWPOLICY:
2442 case XFRM_MSG_UPDPOLICY:
2443 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002444 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002445 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002446 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002447 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002448 return xfrm_exp_policy_notify(xp, dir, c);
2449 default:
2450 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2451 }
2452
2453 return 0;
2454
2455}
2456
Thomas Graf7deb2262007-08-22 13:57:39 -07002457static inline size_t xfrm_report_msgsize(void)
2458{
2459 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2460}
2461
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002462static int build_report(struct sk_buff *skb, u8 proto,
2463 struct xfrm_selector *sel, xfrm_address_t *addr)
2464{
2465 struct xfrm_user_report *ur;
2466 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002467
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002468 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2469 if (nlh == NULL)
2470 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002471
Thomas Graf7b67c852007-08-22 13:53:52 -07002472 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002473 ur->proto = proto;
2474 memcpy(&ur->sel, sel, sizeof(ur->sel));
2475
2476 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002477 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002478
Thomas Graf98250692007-08-22 12:47:26 -07002479 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002480
Thomas Grafc0144be2007-08-22 13:55:43 -07002481nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002482 nlmsg_cancel(skb, nlh);
2483 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002484}
2485
2486static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2487 xfrm_address_t *addr)
2488{
2489 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002490
Thomas Graf7deb2262007-08-22 13:57:39 -07002491 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002492 if (skb == NULL)
2493 return -ENOMEM;
2494
2495 if (build_report(skb, proto, sel, addr) < 0)
2496 BUG();
2497
Thomas Graf082a1ad2007-08-22 13:54:36 -07002498 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002499}
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501static struct xfrm_mgr netlink_mgr = {
2502 .id = "netlink",
2503 .notify = xfrm_send_state_notify,
2504 .acquire = xfrm_send_acquire,
2505 .compile_policy = xfrm_compile_policy,
2506 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002507 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002508 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509};
2510
2511static int __init xfrm_user_init(void)
2512{
Patrick McHardybe336902006-03-20 22:40:54 -08002513 struct sock *nlsk;
2514
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002515 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Patrick McHardybe336902006-03-20 22:40:54 -08002517 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002518 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002519 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002521 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 xfrm_register_km(&netlink_mgr);
2524
2525 return 0;
2526}
2527
2528static void __exit xfrm_user_exit(void)
2529{
Patrick McHardybe336902006-03-20 22:40:54 -08002530 struct sock *nlsk = xfrm_nl;
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002533 rcu_assign_pointer(xfrm_nl, NULL);
2534 synchronize_rcu();
2535 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538module_init(xfrm_user_init);
2539module_exit(xfrm_user_exit);
2540MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002541MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002542