blob: 5efaf45827002f9da1b25de4d8c1fe3e755ac25e [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
673 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
674 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
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700691static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
692{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700693 struct xfrmk_spdinfo si;
694 struct xfrmu_spdinfo spc;
695 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700696 struct nlmsghdr *nlh;
697 u32 *f;
698
699 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
700 if (nlh == NULL) /* shouldnt really happen ... */
701 return -EMSGSIZE;
702
703 f = nlmsg_data(nlh);
704 *f = flags;
705 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700706 spc.incnt = si.incnt;
707 spc.outcnt = si.outcnt;
708 spc.fwdcnt = si.fwdcnt;
709 spc.inscnt = si.inscnt;
710 spc.outscnt = si.outscnt;
711 spc.fwdscnt = si.fwdscnt;
712 sph.spdhcnt = si.spdhcnt;
713 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700714
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700715 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
716 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700717
718 return nlmsg_end(skb, nlh);
719
720nla_put_failure:
721 nlmsg_cancel(skb, nlh);
722 return -EMSGSIZE;
723}
724
725static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
726 struct rtattr **xfrma)
727{
728 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700729 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700730 u32 spid = NETLINK_CB(skb).pid;
731 u32 seq = nlh->nlmsg_seq;
732 int len = NLMSG_LENGTH(sizeof(u32));
733
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700734 len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
735 len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700736
737 r_skb = alloc_skb(len, GFP_ATOMIC);
738 if (r_skb == NULL)
739 return -ENOMEM;
740
741 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
742 BUG();
743
744 return nlmsg_unicast(xfrm_nl, r_skb, spid);
745}
746
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700747static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
748{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700749 struct xfrmk_sadinfo si;
750 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700751 struct nlmsghdr *nlh;
752 u32 *f;
753
754 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
755 if (nlh == NULL) /* shouldnt really happen ... */
756 return -EMSGSIZE;
757
758 f = nlmsg_data(nlh);
759 *f = flags;
760 xfrm_sad_getinfo(&si);
761
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700762 sh.sadhmcnt = si.sadhmcnt;
763 sh.sadhcnt = si.sadhcnt;
764
765 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
766 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700767
768 return nlmsg_end(skb, nlh);
769
770nla_put_failure:
771 nlmsg_cancel(skb, nlh);
772 return -EMSGSIZE;
773}
774
775static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
776 struct rtattr **xfrma)
777{
778 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700779 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700780 u32 spid = NETLINK_CB(skb).pid;
781 u32 seq = nlh->nlmsg_seq;
782 int len = NLMSG_LENGTH(sizeof(u32));
783
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700784 len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
785 len += RTA_SPACE(sizeof(u32));
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700786
787 r_skb = alloc_skb(len, GFP_ATOMIC);
788
789 if (r_skb == NULL)
790 return -ENOMEM;
791
792 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
793 BUG();
794
795 return nlmsg_unicast(xfrm_nl, r_skb, spid);
796}
797
Christoph Hellwig22e70052007-01-02 15:22:30 -0800798static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
799 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Thomas Graf7b67c852007-08-22 13:53:52 -0700801 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct xfrm_state *x;
803 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700804 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Christoph Hellwig22e70052007-01-02 15:22:30 -0800806 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (x == NULL)
808 goto out_noput;
809
810 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
811 if (IS_ERR(resp_skb)) {
812 err = PTR_ERR(resp_skb);
813 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700814 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 xfrm_state_put(x);
817out_noput:
818 return err;
819}
820
821static int verify_userspi_info(struct xfrm_userspi_info *p)
822{
823 switch (p->info.id.proto) {
824 case IPPROTO_AH:
825 case IPPROTO_ESP:
826 break;
827
828 case IPPROTO_COMP:
829 /* IPCOMP spi is 16-bits. */
830 if (p->max >= 0x10000)
831 return -EINVAL;
832 break;
833
834 default:
835 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (p->min > p->max)
839 return -EINVAL;
840
841 return 0;
842}
843
Christoph Hellwig22e70052007-01-02 15:22:30 -0800844static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
845 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct xfrm_state *x;
848 struct xfrm_userspi_info *p;
849 struct sk_buff *resp_skb;
850 xfrm_address_t *daddr;
851 int family;
852 int err;
853
Thomas Graf7b67c852007-08-22 13:53:52 -0700854 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 err = verify_userspi_info(p);
856 if (err)
857 goto out_noput;
858
859 family = p->info.family;
860 daddr = &p->info.id.daddr;
861
862 x = NULL;
863 if (p->info.seq) {
864 x = xfrm_find_acq_byseq(p->info.seq);
865 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
866 xfrm_state_put(x);
867 x = NULL;
868 }
869 }
870
871 if (!x)
872 x = xfrm_find_acq(p->info.mode, p->info.reqid,
873 p->info.id.proto, daddr,
874 &p->info.saddr, 1,
875 family);
876 err = -ENOENT;
877 if (x == NULL)
878 goto out_noput;
879
880 resp_skb = ERR_PTR(-ENOENT);
881
882 spin_lock_bh(&x->lock);
883 if (x->km.state != XFRM_STATE_DEAD) {
884 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
885 if (x->id.spi)
886 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
887 }
888 spin_unlock_bh(&x->lock);
889
890 if (IS_ERR(resp_skb)) {
891 err = PTR_ERR(resp_skb);
892 goto out;
893 }
894
Thomas Graf082a1ad2007-08-22 13:54:36 -0700895 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897out:
898 xfrm_state_put(x);
899out_noput:
900 return err;
901}
902
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800903static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 switch (dir) {
906 case XFRM_POLICY_IN:
907 case XFRM_POLICY_OUT:
908 case XFRM_POLICY_FWD:
909 break;
910
911 default:
912 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return 0;
916}
917
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800918static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700919{
920 switch (type) {
921 case XFRM_POLICY_TYPE_MAIN:
922#ifdef CONFIG_XFRM_SUB_POLICY
923 case XFRM_POLICY_TYPE_SUB:
924#endif
925 break;
926
927 default:
928 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700929 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700930
931 return 0;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
935{
936 switch (p->share) {
937 case XFRM_SHARE_ANY:
938 case XFRM_SHARE_SESSION:
939 case XFRM_SHARE_USER:
940 case XFRM_SHARE_UNIQUE:
941 break;
942
943 default:
944 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 switch (p->action) {
948 case XFRM_POLICY_ALLOW:
949 case XFRM_POLICY_BLOCK:
950 break;
951
952 default:
953 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 switch (p->sel.family) {
957 case AF_INET:
958 break;
959
960 case AF_INET6:
961#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
962 break;
963#else
964 return -EAFNOSUPPORT;
965#endif
966
967 default:
968 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 return verify_policy_dir(p->dir);
972}
973
Trent Jaegerdf718372005-12-13 23:12:27 -0800974static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
975{
976 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
977 struct xfrm_user_sec_ctx *uctx;
978
979 if (!rt)
980 return 0;
981
982 uctx = RTA_DATA(rt);
983 return security_xfrm_policy_alloc(pol, uctx);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
987 int nr)
988{
989 int i;
990
991 xp->xfrm_nr = nr;
992 for (i = 0; i < nr; i++, ut++) {
993 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
994
995 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
996 memcpy(&t->saddr, &ut->saddr,
997 sizeof(xfrm_address_t));
998 t->reqid = ut->reqid;
999 t->mode = ut->mode;
1000 t->share = ut->share;
1001 t->optional = ut->optional;
1002 t->aalgos = ut->aalgos;
1003 t->ealgos = ut->ealgos;
1004 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -08001005 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007}
1008
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001009static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1010{
1011 int i;
1012
1013 if (nr > XFRM_MAX_DEPTH)
1014 return -EINVAL;
1015
1016 for (i = 0; i < nr; i++) {
1017 /* We never validated the ut->family value, so many
1018 * applications simply leave it at zero. The check was
1019 * never made and ut->family was ignored because all
1020 * templates could be assumed to have the same family as
1021 * the policy itself. Now that we will have ipv4-in-ipv6
1022 * and ipv6-in-ipv4 tunnels, this is no longer true.
1023 */
1024 if (!ut[i].family)
1025 ut[i].family = family;
1026
1027 switch (ut[i].family) {
1028 case AF_INET:
1029 break;
1030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1031 case AF_INET6:
1032 break;
1033#endif
1034 default:
1035 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001036 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001037 }
1038
1039 return 0;
1040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1043{
1044 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (!rt) {
1047 pol->xfrm_nr = 0;
1048 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001049 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1050 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001053 err = validate_tmpl(nr, utmpl, pol->family);
1054 if (err)
1055 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 copy_templates(pol, RTA_DATA(rt), nr);
1058 }
1059 return 0;
1060}
1061
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001062static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1063{
1064 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1065 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001066 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001067 int err;
1068
1069 if (rt) {
1070 if (rt->rta_len < sizeof(*upt))
1071 return -EINVAL;
1072
1073 upt = RTA_DATA(rt);
1074 type = upt->type;
1075 }
1076
1077 err = verify_policy_type(type);
1078 if (err)
1079 return err;
1080
1081 *tp = type;
1082 return 0;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1086{
1087 xp->priority = p->priority;
1088 xp->index = p->index;
1089 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1090 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1091 xp->action = p->action;
1092 xp->flags = p->flags;
1093 xp->family = p->sel.family;
1094 /* XXX xp->share = p->share; */
1095}
1096
1097static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1098{
1099 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1100 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1101 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1102 p->priority = xp->priority;
1103 p->index = xp->index;
1104 p->sel.family = xp->family;
1105 p->dir = dir;
1106 p->action = xp->action;
1107 p->flags = xp->flags;
1108 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1109}
1110
1111static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1112{
1113 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1114 int err;
1115
1116 if (!xp) {
1117 *errp = -ENOMEM;
1118 return NULL;
1119 }
1120
1121 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001122
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001123 err = copy_from_user_policy_type(&xp->type, xfrma);
1124 if (err)
1125 goto error;
1126
Trent Jaegerdf718372005-12-13 23:12:27 -08001127 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1128 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001129 if (err)
1130 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001133 error:
1134 *errp = err;
1135 kfree(xp);
1136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Christoph Hellwig22e70052007-01-02 15:22:30 -08001139static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1140 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Thomas Graf7b67c852007-08-22 13:53:52 -07001142 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001144 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int err;
1146 int excl;
1147
1148 err = verify_newpolicy_info(p);
1149 if (err)
1150 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001151 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001152 if (err)
1153 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Christoph Hellwig22e70052007-01-02 15:22:30 -08001155 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!xp)
1157 return err;
1158
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001159 /* shouldnt excl be based on nlh flags??
1160 * Aha! this is anti-netlink really i.e more pfkey derived
1161 * in netlink excl is a flag and you wouldnt need
1162 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1164 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001165 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1166 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001169 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 kfree(xp);
1171 return err;
1172 }
1173
Herbert Xuf60f6b82005-06-18 22:44:37 -07001174 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001175 c.seq = nlh->nlmsg_seq;
1176 c.pid = nlh->nlmsg_pid;
1177 km_policy_notify(xp, p->dir, &c);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 xfrm_pol_put(xp);
1180
1181 return 0;
1182}
1183
1184static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1185{
1186 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1187 int i;
1188
1189 if (xp->xfrm_nr == 0)
1190 return 0;
1191
1192 for (i = 0; i < xp->xfrm_nr; i++) {
1193 struct xfrm_user_tmpl *up = &vec[i];
1194 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1195
1196 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001197 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1199 up->reqid = kp->reqid;
1200 up->mode = kp->mode;
1201 up->share = kp->share;
1202 up->optional = kp->optional;
1203 up->aalgos = kp->aalgos;
1204 up->ealgos = kp->ealgos;
1205 up->calgos = kp->calgos;
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Thomas Grafc0144be2007-08-22 13:55:43 -07001208 return nla_put(skb, XFRMA_TMPL,
1209 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001210}
1211
Serge Hallyn0d681622006-07-24 23:30:44 -07001212static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1213{
1214 if (x->security) {
1215 return copy_sec_ctx(x->security, skb);
1216 }
1217 return 0;
1218}
1219
1220static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1221{
1222 if (xp->security) {
1223 return copy_sec_ctx(xp->security, skb);
1224 }
1225 return 0;
1226}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001227static inline size_t userpolicy_type_attrsize(void)
1228{
1229#ifdef CONFIG_XFRM_SUB_POLICY
1230 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1231#else
1232 return 0;
1233#endif
1234}
Serge Hallyn0d681622006-07-24 23:30:44 -07001235
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001236#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001237static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001238{
Thomas Grafc0144be2007-08-22 13:55:43 -07001239 struct xfrm_userpolicy_type upt = {
1240 .type = type,
1241 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001242
Thomas Grafc0144be2007-08-22 13:55:43 -07001243 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001244}
1245
1246#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001247static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001248{
1249 return 0;
1250}
1251#endif
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1254{
1255 struct xfrm_dump_info *sp = ptr;
1256 struct xfrm_userpolicy_info *p;
1257 struct sk_buff *in_skb = sp->in_skb;
1258 struct sk_buff *skb = sp->out_skb;
1259 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (sp->this_idx < sp->start_idx)
1262 goto out;
1263
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001264 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1265 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1266 if (nlh == NULL)
1267 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Thomas Graf7b67c852007-08-22 13:53:52 -07001269 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 copy_to_user_policy(xp, p, dir);
1271 if (copy_to_user_tmpl(xp, skb) < 0)
1272 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001273 if (copy_to_user_sec_ctx(xp, skb))
1274 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001275 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001276 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Thomas Graf98250692007-08-22 12:47:26 -07001278 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279out:
1280 sp->this_idx++;
1281 return 0;
1282
1283nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001284 nlmsg_cancel(skb, nlh);
1285 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1289{
1290 struct xfrm_dump_info info;
1291
1292 info.in_skb = cb->skb;
1293 info.out_skb = skb;
1294 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1295 info.nlmsg_flags = NLM_F_MULTI;
1296 info.this_idx = 0;
1297 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001298 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1299#ifdef CONFIG_XFRM_SUB_POLICY
1300 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 cb->args[0] = info.this_idx;
1303
1304 return skb->len;
1305}
1306
1307static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1308 struct xfrm_policy *xp,
1309 int dir, u32 seq)
1310{
1311 struct xfrm_dump_info info;
1312 struct sk_buff *skb;
1313
1314 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1315 if (!skb)
1316 return ERR_PTR(-ENOMEM);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 info.in_skb = in_skb;
1319 info.out_skb = skb;
1320 info.nlmsg_seq = seq;
1321 info.nlmsg_flags = 0;
1322 info.this_idx = info.start_idx = 0;
1323
1324 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1325 kfree_skb(skb);
1326 return NULL;
1327 }
1328
1329 return skb;
1330}
1331
Christoph Hellwig22e70052007-01-02 15:22:30 -08001332static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1333 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct xfrm_policy *xp;
1336 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001337 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001339 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int delete;
1341
Thomas Graf7b67c852007-08-22 13:53:52 -07001342 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1344
Christoph Hellwig22e70052007-01-02 15:22:30 -08001345 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001346 if (err)
1347 return err;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 err = verify_policy_dir(p->dir);
1350 if (err)
1351 return err;
1352
1353 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001354 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001355 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001356 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001357 struct xfrm_policy tmp;
1358
Christoph Hellwig22e70052007-01-02 15:22:30 -08001359 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001360 if (err)
1361 return err;
1362
1363 memset(&tmp, 0, sizeof(struct xfrm_policy));
1364 if (rt) {
1365 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1366
1367 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1368 return err;
1369 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001370 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1371 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001372 security_xfrm_policy_free(&tmp);
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (xp == NULL)
1375 return -ENOENT;
1376
1377 if (!delete) {
1378 struct sk_buff *resp_skb;
1379
1380 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1381 if (IS_ERR(resp_skb)) {
1382 err = PTR_ERR(resp_skb);
1383 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001384 err = nlmsg_unicast(xfrm_nl, resp_skb,
1385 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001387 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001388 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1389 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1390
1391 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001392 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001393
Herbert Xue7443892005-06-18 22:44:18 -07001394 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001395 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001396 c.seq = nlh->nlmsg_seq;
1397 c.pid = nlh->nlmsg_pid;
1398 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001401out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001402 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return err;
1404}
1405
Christoph Hellwig22e70052007-01-02 15:22:30 -08001406static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1407 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001409 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001410 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001411 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001412 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Joy Latten161a09e2006-11-27 13:11:54 -06001414 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1415 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001416 err = xfrm_state_flush(p->proto, &audit_info);
1417 if (err)
1418 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001419 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001420 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421 c.seq = nlh->nlmsg_seq;
1422 c.pid = nlh->nlmsg_pid;
1423 km_state_notify(NULL, &c);
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426}
1427
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001428
1429static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1430{
1431 struct xfrm_aevent_id *id;
1432 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001433
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001434 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1435 if (nlh == NULL)
1436 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001437
Thomas Graf7b67c852007-08-22 13:53:52 -07001438 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001439 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001440 id->sa_id.spi = x->id.spi;
1441 id->sa_id.family = x->props.family;
1442 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001443 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1444 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001445 id->flags = c->data.aevent;
1446
Thomas Grafc0144be2007-08-22 13:55:43 -07001447 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1448 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001449
Thomas Grafc0144be2007-08-22 13:55:43 -07001450 if (id->flags & XFRM_AE_RTHR)
1451 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001452
Thomas Grafc0144be2007-08-22 13:55:43 -07001453 if (id->flags & XFRM_AE_ETHR)
1454 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1455 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456
Thomas Graf98250692007-08-22 12:47:26 -07001457 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001458
Thomas Grafc0144be2007-08-22 13:55:43 -07001459nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001460 nlmsg_cancel(skb, nlh);
1461 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001462}
1463
Christoph Hellwig22e70052007-01-02 15:22:30 -08001464static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1465 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001466{
1467 struct xfrm_state *x;
1468 struct sk_buff *r_skb;
1469 int err;
1470 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001471 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001472 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1473 struct xfrm_usersa_id *id = &p->sa_id;
1474
1475 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1476 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1477
1478 if (p->flags&XFRM_AE_RTHR)
1479 len+=RTA_SPACE(sizeof(u32));
1480
1481 if (p->flags&XFRM_AE_ETHR)
1482 len+=RTA_SPACE(sizeof(u32));
1483
1484 r_skb = alloc_skb(len, GFP_ATOMIC);
1485 if (r_skb == NULL)
1486 return -ENOMEM;
1487
1488 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1489 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001490 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001491 return -ESRCH;
1492 }
1493
1494 /*
1495 * XXX: is this lock really needed - none of the other
1496 * gets lock (the concern is things getting updated
1497 * while we are still reading) - jhs
1498 */
1499 spin_lock_bh(&x->lock);
1500 c.data.aevent = p->flags;
1501 c.seq = nlh->nlmsg_seq;
1502 c.pid = nlh->nlmsg_pid;
1503
1504 if (build_aevent(r_skb, x, &c) < 0)
1505 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001506 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001507 spin_unlock_bh(&x->lock);
1508 xfrm_state_put(x);
1509 return err;
1510}
1511
Christoph Hellwig22e70052007-01-02 15:22:30 -08001512static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1513 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001514{
1515 struct xfrm_state *x;
1516 struct km_event c;
1517 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001518 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001519 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1520 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1521
1522 if (!lt && !rp)
1523 return err;
1524
1525 /* pedantic mode - thou shalt sayeth replaceth */
1526 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1527 return err;
1528
1529 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1530 if (x == NULL)
1531 return -ESRCH;
1532
1533 if (x->km.state != XFRM_STATE_VALID)
1534 goto out;
1535
1536 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001537 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001538 spin_unlock_bh(&x->lock);
1539 if (err < 0)
1540 goto out;
1541
1542 c.event = nlh->nlmsg_type;
1543 c.seq = nlh->nlmsg_seq;
1544 c.pid = nlh->nlmsg_pid;
1545 c.data.aevent = XFRM_AE_CU;
1546 km_state_notify(x, &c);
1547 err = 0;
1548out:
1549 xfrm_state_put(x);
1550 return err;
1551}
1552
Christoph Hellwig22e70052007-01-02 15:22:30 -08001553static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1554 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001556 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001557 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001558 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001559 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001560
Christoph Hellwig22e70052007-01-02 15:22:30 -08001561 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001562 if (err)
1563 return err;
1564
Joy Latten161a09e2006-11-27 13:11:54 -06001565 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1566 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001567 err = xfrm_policy_flush(type, &audit_info);
1568 if (err)
1569 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001570 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001571 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001572 c.seq = nlh->nlmsg_seq;
1573 c.pid = nlh->nlmsg_pid;
1574 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 0;
1576}
1577
Christoph Hellwig22e70052007-01-02 15:22:30 -08001578static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1579 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001580{
1581 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001582 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001583 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001584 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001585 int err = -ENOENT;
1586
Christoph Hellwig22e70052007-01-02 15:22:30 -08001587 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001588 if (err)
1589 return err;
1590
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001591 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001592 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001593 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001594 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001595 struct xfrm_policy tmp;
1596
Christoph Hellwig22e70052007-01-02 15:22:30 -08001597 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001598 if (err)
1599 return err;
1600
1601 memset(&tmp, 0, sizeof(struct xfrm_policy));
1602 if (rt) {
1603 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1604
1605 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1606 return err;
1607 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001608 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1609 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001610 security_xfrm_policy_free(&tmp);
1611 }
1612
1613 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001614 return -ENOENT;
1615 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001616 if (xp->dead) {
1617 read_unlock(&xp->lock);
1618 goto out;
1619 }
1620
1621 read_unlock(&xp->lock);
1622 err = 0;
1623 if (up->hard) {
1624 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001625 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1626 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1627
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001628 } else {
1629 // reset the timers here?
1630 printk("Dont know what to do with soft policy expire\n");
1631 }
1632 km_policy_expired(xp, p->dir, up->hard, current->pid);
1633
1634out:
1635 xfrm_pol_put(xp);
1636 return err;
1637}
1638
Christoph Hellwig22e70052007-01-02 15:22:30 -08001639static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1640 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001641{
1642 struct xfrm_state *x;
1643 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001644 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001645 struct xfrm_usersa_info *p = &ue->state;
1646
1647 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001648
David S. Miller3a765aa2007-02-26 14:52:21 -08001649 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001650 if (x == NULL)
1651 return err;
1652
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001653 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001654 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001655 if (x->km.state != XFRM_STATE_VALID)
1656 goto out;
1657 km_state_expired(x, ue->hard, current->pid);
1658
Joy Latten161a09e2006-11-27 13:11:54 -06001659 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001660 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001661 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1662 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1663 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001664 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001665out:
1666 spin_unlock_bh(&x->lock);
1667 xfrm_state_put(x);
1668 return err;
1669}
1670
Christoph Hellwig22e70052007-01-02 15:22:30 -08001671static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1672 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001673{
1674 struct xfrm_policy *xp;
1675 struct xfrm_user_tmpl *ut;
1676 int i;
1677 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1678
Thomas Graf7b67c852007-08-22 13:53:52 -07001679 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001680 struct xfrm_state *x = xfrm_state_alloc();
1681 int err = -ENOMEM;
1682
1683 if (!x)
1684 return err;
1685
1686 err = verify_newpolicy_info(&ua->policy);
1687 if (err) {
1688 printk("BAD policy passed\n");
1689 kfree(x);
1690 return err;
1691 }
1692
1693 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001694 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1695 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001696 kfree(x);
1697 return err;
1698 }
1699
1700 memcpy(&x->id, &ua->id, sizeof(ua->id));
1701 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1702 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1703
1704 ut = RTA_DATA(rt);
1705 /* extract the templates and for each call km_key */
1706 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1707 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1708 memcpy(&x->id, &t->id, sizeof(x->id));
1709 x->props.mode = t->mode;
1710 x->props.reqid = t->reqid;
1711 x->props.family = ut->family;
1712 t->aalgos = ua->aalgos;
1713 t->ealgos = ua->ealgos;
1714 t->calgos = ua->calgos;
1715 err = km_query(x, t, xp);
1716
1717 }
1718
1719 kfree(x);
1720 kfree(xp);
1721
1722 return 0;
1723}
1724
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001725#ifdef CONFIG_XFRM_MIGRATE
1726static int verify_user_migrate(struct rtattr **xfrma)
1727{
1728 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1729 struct xfrm_user_migrate *um;
1730
1731 if (!rt)
1732 return -EINVAL;
1733
1734 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1735 return -EINVAL;
1736
1737 return 0;
1738}
1739
1740static int copy_from_user_migrate(struct xfrm_migrate *ma,
1741 struct rtattr **xfrma, int *num)
1742{
1743 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1744 struct xfrm_user_migrate *um;
1745 int i, num_migrate;
1746
1747 um = RTA_DATA(rt);
1748 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1749
1750 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1751 return -EINVAL;
1752
1753 for (i = 0; i < num_migrate; i++, um++, ma++) {
1754 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1755 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1756 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1757 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1758
1759 ma->proto = um->proto;
1760 ma->mode = um->mode;
1761 ma->reqid = um->reqid;
1762
1763 ma->old_family = um->old_family;
1764 ma->new_family = um->new_family;
1765 }
1766
1767 *num = i;
1768 return 0;
1769}
1770
1771static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1772 struct rtattr **xfrma)
1773{
Thomas Graf7b67c852007-08-22 13:53:52 -07001774 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001775 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1776 u8 type;
1777 int err;
1778 int n = 0;
1779
1780 err = verify_user_migrate((struct rtattr **)xfrma);
1781 if (err)
1782 return err;
1783
1784 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1785 if (err)
1786 return err;
1787
1788 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1789 (struct rtattr **)xfrma, &n);
1790 if (err)
1791 return err;
1792
1793 if (!n)
1794 return 0;
1795
1796 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1797
1798 return 0;
1799}
1800#else
1801static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1802 struct rtattr **xfrma)
1803{
1804 return -ENOPROTOOPT;
1805}
1806#endif
1807
1808#ifdef CONFIG_XFRM_MIGRATE
1809static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1810{
1811 struct xfrm_user_migrate um;
1812
1813 memset(&um, 0, sizeof(um));
1814 um.proto = m->proto;
1815 um.mode = m->mode;
1816 um.reqid = m->reqid;
1817 um.old_family = m->old_family;
1818 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1819 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1820 um.new_family = m->new_family;
1821 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1822 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1823
Thomas Grafc0144be2007-08-22 13:55:43 -07001824 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001825}
1826
1827static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1828 int num_migrate, struct xfrm_selector *sel,
1829 u8 dir, u8 type)
1830{
1831 struct xfrm_migrate *mp;
1832 struct xfrm_userpolicy_id *pol_id;
1833 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001834 int i;
1835
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001836 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1837 if (nlh == NULL)
1838 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001839
Thomas Graf7b67c852007-08-22 13:53:52 -07001840 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001841 /* copy data from selector, dir, and type to the pol_id */
1842 memset(pol_id, 0, sizeof(*pol_id));
1843 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1844 pol_id->dir = dir;
1845
1846 if (copy_to_user_policy_type(type, skb) < 0)
1847 goto nlmsg_failure;
1848
1849 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1850 if (copy_to_user_migrate(mp, skb) < 0)
1851 goto nlmsg_failure;
1852 }
1853
Thomas Graf98250692007-08-22 12:47:26 -07001854 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001855nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001856 nlmsg_cancel(skb, nlh);
1857 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001858}
1859
1860static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1861 struct xfrm_migrate *m, int num_migrate)
1862{
1863 struct sk_buff *skb;
1864 size_t len;
1865
1866 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1867 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
Thomas Grafcfbfd452007-08-22 13:57:04 -07001868 len += userpolicy_type_attrsize();
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001869 skb = alloc_skb(len, GFP_ATOMIC);
1870 if (skb == NULL)
1871 return -ENOMEM;
1872
1873 /* build migrate */
1874 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1875 BUG();
1876
Thomas Graf082a1ad2007-08-22 13:54:36 -07001877 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001878}
1879#else
1880static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1881 struct xfrm_migrate *m, int num_migrate)
1882{
1883 return -ENOPROTOOPT;
1884}
1885#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001886
Thomas Graf492b5582005-05-03 14:26:40 -07001887#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1888
1889static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1890 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1891 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1892 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1893 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1894 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1895 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1896 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001897 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001898 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001899 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1900 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001901 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001902 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1903 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001904 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1905 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001906 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001907 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001908 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001909 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
Thomas Graf492b5582005-05-03 14:26:40 -07001912#undef XMSGSIZE
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001915 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001917} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1918 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1919 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1920 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1921 .dump = xfrm_dump_sa },
1922 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1923 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1924 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1925 .dump = xfrm_dump_policy },
1926 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001927 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001928 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001929 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1930 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001931 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001932 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1933 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001934 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1935 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001936 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001937 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001938 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939};
1940
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001941static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 struct rtattr *xfrma[XFRMA_MAX];
1944 struct xfrm_link *link;
Thomas Grafc702e802007-03-22 23:30:55 -07001945 int type, min_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 type -= XFRM_MSG_BASE;
1952 link = &xfrm_dispatch[type];
1953
1954 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001955 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1956 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Thomas Graf492b5582005-05-03 14:26:40 -07001958 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1959 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1960 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001962 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Thomas Grafc702e802007-03-22 23:30:55 -07001964 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
1967 memset(xfrma, 0, sizeof(xfrma));
1968
1969 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 if (nlh->nlmsg_len > min_len) {
1973 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1974 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1975
1976 while (RTA_OK(attr, attrlen)) {
1977 unsigned short flavor = attr->rta_type;
1978 if (flavor) {
1979 if (flavor > XFRMA_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 xfrma[flavor - 1] = attr;
1982 }
1983 attr = RTA_NEXT(attr, attrlen);
1984 }
1985 }
1986
1987 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001988 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001990 return link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993static void xfrm_netlink_rcv(struct sock *sk, int len)
1994{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001995 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001998 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001999 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002000 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002002 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002005static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 struct xfrm_user_expire *ue;
2008 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002010 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2011 if (nlh == NULL)
2012 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Thomas Graf7b67c852007-08-22 13:53:52 -07002014 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002016 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Thomas Graf98250692007-08-22 12:47:26 -07002018 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002021static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002024 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002026 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (skb == NULL)
2028 return -ENOMEM;
2029
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002030 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 BUG();
2032
Thomas Graf082a1ad2007-08-22 13:54:36 -07002033 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002036static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2037{
2038 struct sk_buff *skb;
2039 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2040
2041 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2042 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2043 skb = alloc_skb(len, GFP_ATOMIC);
2044 if (skb == NULL)
2045 return -ENOMEM;
2046
2047 if (build_aevent(skb, x, c) < 0)
2048 BUG();
2049
Thomas Graf082a1ad2007-08-22 13:54:36 -07002050 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002051}
2052
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002053static int xfrm_notify_sa_flush(struct km_event *c)
2054{
2055 struct xfrm_usersa_flush *p;
2056 struct nlmsghdr *nlh;
2057 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2059
2060 skb = alloc_skb(len, GFP_ATOMIC);
2061 if (skb == NULL)
2062 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002063
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002064 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2065 if (nlh == NULL) {
2066 kfree_skb(skb);
2067 return -EMSGSIZE;
2068 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002069
Thomas Graf7b67c852007-08-22 13:53:52 -07002070 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002071 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002072
Thomas Graf98250692007-08-22 12:47:26 -07002073 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002074
Thomas Graf082a1ad2007-08-22 13:54:36 -07002075 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002076}
2077
Dave Jonesb6f99a22007-03-22 12:27:49 -07002078static inline int xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002079{
Herbert Xu0603eac2005-06-18 22:54:36 -07002080 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002081 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002082 l += RTA_SPACE(alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002084 l += RTA_SPACE(alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002085 if (x->calg)
2086 l += RTA_SPACE(sizeof(*x->calg));
2087 if (x->encap)
2088 l += RTA_SPACE(sizeof(*x->encap));
2089
2090 return l;
2091}
2092
2093static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2094{
2095 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002096 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097 struct nlmsghdr *nlh;
2098 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002099 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002100 int headlen;
2101
2102 headlen = sizeof(*p);
2103 if (c->event == XFRM_MSG_DELSA) {
2104 len += RTA_SPACE(headlen);
2105 headlen = sizeof(*id);
2106 }
2107 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002108
2109 skb = alloc_skb(len, GFP_ATOMIC);
2110 if (skb == NULL)
2111 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002112
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002113 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2114 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002115 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002116
Thomas Graf7b67c852007-08-22 13:53:52 -07002117 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002118 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002119 struct nlattr *attr;
2120
Thomas Graf7b67c852007-08-22 13:53:52 -07002121 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002122 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2123 id->spi = x->id.spi;
2124 id->family = x->props.family;
2125 id->proto = x->id.proto;
2126
Thomas Grafc0144be2007-08-22 13:55:43 -07002127 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2128 if (attr == NULL)
2129 goto nla_put_failure;
2130
2131 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002132 }
2133
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002134 copy_to_user_state(x, p);
2135
2136 if (x->aalg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002137 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002138 if (x->ealg)
Thomas Grafc26445a2007-08-22 13:56:23 -07002139 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -07002141 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142
2143 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -07002144 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002145
Thomas Graf98250692007-08-22 12:47:26 -07002146 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002147
Thomas Graf082a1ad2007-08-22 13:54:36 -07002148 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002149
Thomas Grafc0144be2007-08-22 13:55:43 -07002150nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002151 kfree_skb(skb);
2152 return -1;
2153}
2154
2155static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2156{
2157
2158 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002159 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002160 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002161 case XFRM_MSG_NEWAE:
2162 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002163 case XFRM_MSG_DELSA:
2164 case XFRM_MSG_UPDSA:
2165 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002166 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002167 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002168 return xfrm_notify_sa_flush(c);
2169 default:
2170 printk("xfrm_user: Unknown SA event %d\n", c->event);
2171 break;
2172 }
2173
2174 return 0;
2175
2176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2179 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2180 int dir)
2181{
2182 struct xfrm_user_acquire *ua;
2183 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 __u32 seq = xfrm_get_acqseq();
2185
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002186 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2187 if (nlh == NULL)
2188 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Graf7b67c852007-08-22 13:53:52 -07002190 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 memcpy(&ua->id, &x->id, sizeof(ua->id));
2192 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2193 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2194 copy_to_user_policy(xp, &ua->policy, dir);
2195 ua->aalgos = xt->aalgos;
2196 ua->ealgos = xt->ealgos;
2197 ua->calgos = xt->calgos;
2198 ua->seq = x->km.seq = seq;
2199
2200 if (copy_to_user_tmpl(xp, skb) < 0)
2201 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002202 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002203 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002204 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002205 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf98250692007-08-22 12:47:26 -07002207 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002210 nlmsg_cancel(skb, nlh);
2211 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2215 struct xfrm_policy *xp, int dir)
2216{
2217 struct sk_buff *skb;
2218 size_t len;
2219
2220 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2221 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Joy Latten661697f2007-04-13 16:14:35 -07002222 len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
Thomas Grafcfbfd452007-08-22 13:57:04 -07002223 len += userpolicy_type_attrsize();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 skb = alloc_skb(len, GFP_ATOMIC);
2225 if (skb == NULL)
2226 return -ENOMEM;
2227
2228 if (build_acquire(skb, x, xt, xp, dir) < 0)
2229 BUG();
2230
Thomas Graf082a1ad2007-08-22 13:54:36 -07002231 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/* User gives us xfrm_user_policy_info followed by an array of 0
2235 * or more templates.
2236 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002237static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 u8 *data, int len, int *dir)
2239{
2240 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2241 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2242 struct xfrm_policy *xp;
2243 int nr;
2244
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002245 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 case AF_INET:
2247 if (opt != IP_XFRM_POLICY) {
2248 *dir = -EOPNOTSUPP;
2249 return NULL;
2250 }
2251 break;
2252#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2253 case AF_INET6:
2254 if (opt != IPV6_XFRM_POLICY) {
2255 *dir = -EOPNOTSUPP;
2256 return NULL;
2257 }
2258 break;
2259#endif
2260 default:
2261 *dir = -EINVAL;
2262 return NULL;
2263 }
2264
2265 *dir = -EINVAL;
2266
2267 if (len < sizeof(*p) ||
2268 verify_newpolicy_info(p))
2269 return NULL;
2270
2271 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002272 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return NULL;
2274
Herbert Xua4f1bac2005-07-26 15:43:17 -07002275 if (p->dir > XFRM_POLICY_OUT)
2276 return NULL;
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 xp = xfrm_policy_alloc(GFP_KERNEL);
2279 if (xp == NULL) {
2280 *dir = -ENOBUFS;
2281 return NULL;
2282 }
2283
2284 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002285 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 copy_templates(xp, ut, nr);
2287
2288 *dir = p->dir;
2289
2290 return xp;
2291}
2292
2293static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002294 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 struct xfrm_user_polexpire *upe;
2297 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002298 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002300 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2301 if (nlh == NULL)
2302 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Thomas Graf7b67c852007-08-22 13:53:52 -07002304 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 copy_to_user_policy(xp, &upe->pol, dir);
2306 if (copy_to_user_tmpl(xp, skb) < 0)
2307 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002308 if (copy_to_user_sec_ctx(xp, skb))
2309 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002310 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002311 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 upe->hard = !!hard;
2313
Thomas Graf98250692007-08-22 12:47:26 -07002314 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002317 nlmsg_cancel(skb, nlh);
2318 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 struct sk_buff *skb;
2324 size_t len;
2325
2326 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2327 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Joy Latten661697f2007-04-13 16:14:35 -07002328 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
Thomas Grafcfbfd452007-08-22 13:57:04 -07002329 len += userpolicy_type_attrsize();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 skb = alloc_skb(len, GFP_ATOMIC);
2331 if (skb == NULL)
2332 return -ENOMEM;
2333
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002334 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 BUG();
2336
Thomas Graf082a1ad2007-08-22 13:54:36 -07002337 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2341{
2342 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002343 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344 struct nlmsghdr *nlh;
2345 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002347 int headlen;
2348
2349 headlen = sizeof(*p);
2350 if (c->event == XFRM_MSG_DELPOLICY) {
2351 len += RTA_SPACE(headlen);
2352 headlen = sizeof(*id);
2353 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002354 len += userpolicy_type_attrsize();
Herbert Xu0603eac2005-06-18 22:54:36 -07002355 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002356
2357 skb = alloc_skb(len, GFP_ATOMIC);
2358 if (skb == NULL)
2359 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002360
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002361 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2362 if (nlh == NULL)
2363 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002364
Thomas Graf7b67c852007-08-22 13:53:52 -07002365 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002366 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002367 struct nlattr *attr;
2368
Thomas Graf7b67c852007-08-22 13:53:52 -07002369 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002370 memset(id, 0, sizeof(*id));
2371 id->dir = dir;
2372 if (c->data.byid)
2373 id->index = xp->index;
2374 else
2375 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2376
Thomas Grafc0144be2007-08-22 13:55:43 -07002377 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2378 if (attr == NULL)
2379 goto nlmsg_failure;
2380
2381 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002382 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002383
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384 copy_to_user_policy(xp, p, dir);
2385 if (copy_to_user_tmpl(xp, skb) < 0)
2386 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002387 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002388 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389
Thomas Graf98250692007-08-22 12:47:26 -07002390 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391
Thomas Graf082a1ad2007-08-22 13:54:36 -07002392 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002393
2394nlmsg_failure:
2395 kfree_skb(skb);
2396 return -1;
2397}
2398
2399static int xfrm_notify_policy_flush(struct km_event *c)
2400{
2401 struct nlmsghdr *nlh;
2402 struct sk_buff *skb;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002403 int len = 0;
Thomas Grafcfbfd452007-08-22 13:57:04 -07002404 len += userpolicy_type_attrsize();
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002405 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406
2407 skb = alloc_skb(len, GFP_ATOMIC);
2408 if (skb == NULL)
2409 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002410
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002411 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2412 if (nlh == NULL)
2413 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002414 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2415 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416
Thomas Graf98250692007-08-22 12:47:26 -07002417 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418
Thomas Graf082a1ad2007-08-22 13:54:36 -07002419 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002420
2421nlmsg_failure:
2422 kfree_skb(skb);
2423 return -1;
2424}
2425
2426static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2427{
2428
2429 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002430 case XFRM_MSG_NEWPOLICY:
2431 case XFRM_MSG_UPDPOLICY:
2432 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002433 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002434 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002436 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002437 return xfrm_exp_policy_notify(xp, dir, c);
2438 default:
2439 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2440 }
2441
2442 return 0;
2443
2444}
2445
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002446static int build_report(struct sk_buff *skb, u8 proto,
2447 struct xfrm_selector *sel, xfrm_address_t *addr)
2448{
2449 struct xfrm_user_report *ur;
2450 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002451
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002452 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2453 if (nlh == NULL)
2454 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002455
Thomas Graf7b67c852007-08-22 13:53:52 -07002456 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002457 ur->proto = proto;
2458 memcpy(&ur->sel, sel, sizeof(ur->sel));
2459
2460 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002461 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002462
Thomas Graf98250692007-08-22 12:47:26 -07002463 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002464
Thomas Grafc0144be2007-08-22 13:55:43 -07002465nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002466 nlmsg_cancel(skb, nlh);
2467 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002468}
2469
2470static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2471 xfrm_address_t *addr)
2472{
2473 struct sk_buff *skb;
2474 size_t len;
2475
2476 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2477 skb = alloc_skb(len, GFP_ATOMIC);
2478 if (skb == NULL)
2479 return -ENOMEM;
2480
2481 if (build_report(skb, proto, sel, addr) < 0)
2482 BUG();
2483
Thomas Graf082a1ad2007-08-22 13:54:36 -07002484 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487static struct xfrm_mgr netlink_mgr = {
2488 .id = "netlink",
2489 .notify = xfrm_send_state_notify,
2490 .acquire = xfrm_send_acquire,
2491 .compile_policy = xfrm_compile_policy,
2492 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002493 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002494 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495};
2496
2497static int __init xfrm_user_init(void)
2498{
Patrick McHardybe336902006-03-20 22:40:54 -08002499 struct sock *nlsk;
2500
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002501 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Patrick McHardybe336902006-03-20 22:40:54 -08002503 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002504 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002505 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002507 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 xfrm_register_km(&netlink_mgr);
2510
2511 return 0;
2512}
2513
2514static void __exit xfrm_user_exit(void)
2515{
Patrick McHardybe336902006-03-20 22:40:54 -08002516 struct sock *nlsk = xfrm_nl;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002519 rcu_assign_pointer(xfrm_nl, NULL);
2520 synchronize_rcu();
2521 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
2524module_init(xfrm_user_init);
2525module_exit(xfrm_user_exit);
2526MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002527MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002528