blob: 249940eaced8fa634ffc73d8563a2e74f39cba0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/rtnetlink.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
Joy Latten161a09e2006-11-27 13:11:54 -060034#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
37{
38 struct rtattr *rt = xfrma[type - 1];
39 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070040 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (!rt)
43 return 0;
44
Herbert Xu31c26852005-05-19 12:39:49 -070045 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
46 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return -EINVAL;
48
49 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070050
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +090051 len -= (algp->alg_key_len + 7U) / 8;
Herbert Xu31c26852005-05-19 12:39:49 -070052 if (len < 0)
53 return -EINVAL;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 switch (type) {
56 case XFRMA_ALG_AUTH:
57 if (!algp->alg_key_len &&
58 strcmp(algp->alg_name, "digest_null") != 0)
59 return -EINVAL;
60 break;
61
62 case XFRMA_ALG_CRYPT:
63 if (!algp->alg_key_len &&
64 strcmp(algp->alg_name, "cipher_null") != 0)
65 return -EINVAL;
66 break;
67
68 case XFRMA_ALG_COMP:
69 /* Zero length keys are legal. */
70 break;
71
72 default:
73 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 return 0;
78}
79
80static int verify_encap_tmpl(struct rtattr **xfrma)
81{
82 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
83 struct xfrm_encap_tmpl *encap;
84
85 if (!rt)
86 return 0;
87
88 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
89 return -EINVAL;
90
91 return 0;
92}
93
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
96{
97 struct rtattr *rt = xfrma[type - 1];
98
99 if (!rt)
100 return 0;
101
102 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
103 return -EINVAL;
104
105 if (addrp)
106 *addrp = RTA_DATA(rt);
107
108 return 0;
109}
Trent Jaegerdf718372005-12-13 23:12:27 -0800110
111static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112{
113 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
114 struct xfrm_user_sec_ctx *uctx;
115 int len = 0;
116
117 if (!rt)
118 return 0;
119
120 if (rt->rta_len < sizeof(*uctx))
121 return -EINVAL;
122
123 uctx = RTA_DATA(rt);
124
Trent Jaegerdf718372005-12-13 23:12:27 -0800125 len += sizeof(struct xfrm_user_sec_ctx);
126 len += uctx->ctx_len;
127
128 if (uctx->len != len)
129 return -EINVAL;
130
131 return 0;
132}
133
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int verify_newsa_info(struct xfrm_usersa_info *p,
136 struct rtattr **xfrma)
137{
138 int err;
139
140 err = -EINVAL;
141 switch (p->family) {
142 case AF_INET:
143 break;
144
145 case AF_INET6:
146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 break;
148#else
149 err = -EAFNOSUPPORT;
150 goto out;
151#endif
152
153 default:
154 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 err = -EINVAL;
158 switch (p->id.proto) {
159 case IPPROTO_AH:
160 if (!xfrma[XFRMA_ALG_AUTH-1] ||
161 xfrma[XFRMA_ALG_CRYPT-1] ||
162 xfrma[XFRMA_ALG_COMP-1])
163 goto out;
164 break;
165
166 case IPPROTO_ESP:
167 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
168 !xfrma[XFRMA_ALG_CRYPT-1]) ||
169 xfrma[XFRMA_ALG_COMP-1])
170 goto out;
171 break;
172
173 case IPPROTO_COMP:
174 if (!xfrma[XFRMA_ALG_COMP-1] ||
175 xfrma[XFRMA_ALG_AUTH-1] ||
176 xfrma[XFRMA_ALG_CRYPT-1])
177 goto out;
178 break;
179
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700180#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181 case IPPROTO_DSTOPTS:
182 case IPPROTO_ROUTING:
183 if (xfrma[XFRMA_ALG_COMP-1] ||
184 xfrma[XFRMA_ALG_AUTH-1] ||
185 xfrma[XFRMA_ALG_CRYPT-1] ||
186 xfrma[XFRMA_ENCAP-1] ||
187 xfrma[XFRMA_SEC_CTX-1] ||
188 !xfrma[XFRMA_COADDR-1])
189 goto out;
190 break;
191#endif
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 default:
194 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 goto out;
199 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 goto out;
201 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 goto out;
203 if ((err = verify_encap_tmpl(xfrma)))
204 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800205 if ((err = verify_sec_ctx_len(xfrma)))
206 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700207 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 err = -EINVAL;
211 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700212 case XFRM_MODE_TRANSPORT:
213 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700214 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700215 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217
218 default:
219 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 err = 0;
223
224out:
225 return err;
226}
227
228static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
229 struct xfrm_algo_desc *(*get_byname)(char *, int),
230 struct rtattr *u_arg)
231{
232 struct rtattr *rta = u_arg;
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700235 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!rta)
238 return 0;
239
240 ualg = RTA_DATA(rta);
241
242 algo = get_byname(ualg->alg_name, 1);
243 if (!algo)
244 return -ENOSYS;
245 *props = algo->desc.sadb_alg_id;
246
Herbert Xub9e9dea2005-05-19 12:39:04 -0700247 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200248 p = kmemdup(ualg, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!p)
250 return -ENOMEM;
251
Herbert Xu04ff1262006-08-13 08:50:00 +1000252 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *algpp = p;
254 return 0;
255}
256
257static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
258{
259 struct rtattr *rta = u_arg;
260 struct xfrm_encap_tmpl *p, *uencap;
261
262 if (!rta)
263 return 0;
264
265 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200266 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!p)
268 return -ENOMEM;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *encapp = p;
271 return 0;
272}
273
Trent Jaegerdf718372005-12-13 23:12:27 -0800274
Joy Latten661697f2007-04-13 16:14:35 -0700275static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800276{
Trent Jaegerdf718372005-12-13 23:12:27 -0800277 int len = 0;
278
279 if (xfrm_ctx) {
280 len += sizeof(struct xfrm_user_sec_ctx);
281 len += xfrm_ctx->ctx_len;
282 }
283 return len;
284}
285
286static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
287{
288 struct xfrm_user_sec_ctx *uctx;
289
290 if (!u_arg)
291 return 0;
292
293 uctx = RTA_DATA(u_arg);
294 return security_xfrm_state_alloc(x, uctx);
295}
296
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700297static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
298{
299 struct rtattr *rta = u_arg;
300 xfrm_address_t *p, *uaddrp;
301
302 if (!rta)
303 return 0;
304
305 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200306 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700307 if (!p)
308 return -ENOMEM;
309
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700310 *addrpp = p;
311 return 0;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
315{
316 memcpy(&x->id, &p->id, sizeof(x->id));
317 memcpy(&x->sel, &p->sel, sizeof(x->sel));
318 memcpy(&x->lft, &p->lft, sizeof(x->lft));
319 x->props.mode = p->mode;
320 x->props.replay_window = p->replay_window;
321 x->props.reqid = p->reqid;
322 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700323 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700325
326 /*
327 * Set inner address family if the KM left it as zero.
328 * See comment in validate_tmpl.
329 */
330 if (!x->sel.family)
331 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800334/*
335 * someday when pfkey also has support, we could have the code
336 * somehow made shareable and move it to xfrm_state.c - JHS
337 *
338*/
339static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
340{
341 int err = - EINVAL;
342 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
343 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
344 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
345 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
346
347 if (rp) {
348 struct xfrm_replay_state *replay;
349 if (RTA_PAYLOAD(rp) < sizeof(*replay))
350 goto error;
351 replay = RTA_DATA(rp);
352 memcpy(&x->replay, replay, sizeof(*replay));
353 memcpy(&x->preplay, replay, sizeof(*replay));
354 }
355
356 if (lt) {
357 struct xfrm_lifetime_cur *ltime;
358 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
359 goto error;
360 ltime = RTA_DATA(lt);
361 x->curlft.bytes = ltime->bytes;
362 x->curlft.packets = ltime->packets;
363 x->curlft.add_time = ltime->add_time;
364 x->curlft.use_time = ltime->use_time;
365 }
366
367 if (et) {
368 if (RTA_PAYLOAD(et) < sizeof(u32))
369 goto error;
370 x->replay_maxage = *(u32*)RTA_DATA(et);
371 }
372
373 if (rt) {
374 if (RTA_PAYLOAD(rt) < sizeof(u32))
375 goto error;
376 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
377 }
378
379 return 0;
380error:
381 return err;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
385 struct rtattr **xfrma,
386 int *errp)
387{
388 struct xfrm_state *x = xfrm_state_alloc();
389 int err = -ENOMEM;
390
391 if (!x)
392 goto error_no_put;
393
394 copy_from_user_state(x, p);
395
396 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
397 xfrm_aalg_get_byname,
398 xfrma[XFRMA_ALG_AUTH-1])))
399 goto error;
400 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
401 xfrm_ealg_get_byname,
402 xfrma[XFRMA_ALG_CRYPT-1])))
403 goto error;
404 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
405 xfrm_calg_get_byname,
406 xfrma[XFRMA_ALG_COMP-1])))
407 goto error;
408 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
409 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700410 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
411 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700412 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (err)
414 goto error;
415
Trent Jaegerdf718372005-12-13 23:12:27 -0800416 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
417 goto error;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800420 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
421 /* sysctl_xfrm_aevent_etime is in 100ms units */
422 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
423 x->preplay.bitmap = 0;
424 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
425 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
426
427 /* override default values from above */
428
429 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
430 if (err < 0)
431 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return x;
434
435error:
436 x->km.state = XFRM_STATE_DEAD;
437 xfrm_state_put(x);
438error_no_put:
439 *errp = err;
440 return NULL;
441}
442
Christoph Hellwig22e70052007-01-02 15:22:30 -0800443static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
444 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Thomas Graf7b67c852007-08-22 13:53:52 -0700446 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct xfrm_state *x;
448 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700449 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Christoph Hellwig22e70052007-01-02 15:22:30 -0800451 err = verify_newsa_info(p, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (err)
453 return err;
454
Christoph Hellwig22e70052007-01-02 15:22:30 -0800455 x = xfrm_state_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!x)
457 return err;
458
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700459 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
461 err = xfrm_state_add(x);
462 else
463 err = xfrm_state_update(x);
464
Joy Latten161a09e2006-11-27 13:11:54 -0600465 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
466 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (err < 0) {
469 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800470 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700474 c.seq = nlh->nlmsg_seq;
475 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700476 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700477
478 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700479out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700480 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return err;
482}
483
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700484static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
485 struct rtattr **xfrma,
486 int *errp)
487{
488 struct xfrm_state *x = NULL;
489 int err;
490
491 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
492 err = -ESRCH;
493 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
494 } else {
495 xfrm_address_t *saddr = NULL;
496
497 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
498 if (err)
499 goto out;
500
501 if (!saddr) {
502 err = -EINVAL;
503 goto out;
504 }
505
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800506 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700507 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
508 p->family);
509 }
510
511 out:
512 if (!x && errp)
513 *errp = err;
514 return x;
515}
516
Christoph Hellwig22e70052007-01-02 15:22:30 -0800517static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
518 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700521 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700522 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700523 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Hellwig22e70052007-01-02 15:22:30 -0800525 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David S. Miller6f68dc32006-06-08 23:58:52 -0700529 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700530 goto out;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700533 err = -EPERM;
534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700537 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600538
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700539 if (err < 0)
540 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700541
542 c.seq = nlh->nlmsg_seq;
543 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700544 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700545 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700547out:
Eric Paris16bec312007-03-07 16:02:16 -0800548 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
549 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700550 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
555{
556 memcpy(&p->id, &x->id, sizeof(p->id));
557 memcpy(&p->sel, &x->sel, sizeof(p->sel));
558 memcpy(&p->lft, &x->lft, sizeof(p->lft));
559 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
560 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700561 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 p->mode = x->props.mode;
563 p->replay_window = x->props.replay_window;
564 p->reqid = x->props.reqid;
565 p->family = x->props.family;
566 p->flags = x->props.flags;
567 p->seq = x->km.seq;
568}
569
570struct xfrm_dump_info {
571 struct sk_buff *in_skb;
572 struct sk_buff *out_skb;
573 u32 nlmsg_seq;
574 u16 nlmsg_flags;
575 int start_idx;
576 int this_idx;
577};
578
579static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
580{
581 struct xfrm_dump_info *sp = ptr;
582 struct sk_buff *in_skb = sp->in_skb;
583 struct sk_buff *skb = sp->out_skb;
584 struct xfrm_usersa_info *p;
585 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (sp->this_idx < sp->start_idx)
588 goto out;
589
Thomas Graf79b8b7f2007-08-22 12:46:53 -0700590 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
591 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
592 if (nlh == NULL)
593 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Thomas Graf7b67c852007-08-22 13:53:52 -0700595 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 copy_to_user_state(x, p);
597
598 if (x->aalg)
599 RTA_PUT(skb, XFRMA_ALG_AUTH,
600 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
601 if (x->ealg)
602 RTA_PUT(skb, XFRMA_ALG_CRYPT,
603 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
604 if (x->calg)
605 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
606
607 if (x->encap)
608 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
609
Trent Jaegerdf718372005-12-13 23:12:27 -0800610 if (x->security) {
611 int ctx_size = sizeof(struct xfrm_sec_ctx) +
612 x->security->ctx_len;
613 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
614 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
615
616 uctx->exttype = XFRMA_SEC_CTX;
617 uctx->len = ctx_size;
618 uctx->ctx_doi = x->security->ctx_doi;
619 uctx->ctx_alg = x->security->ctx_alg;
620 uctx->ctx_len = x->security->ctx_len;
621 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
622 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700623
624 if (x->coaddr)
625 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
626
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700627 if (x->lastused)
628 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
629
Thomas Graf98250692007-08-22 12:47:26 -0700630 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631out:
632 sp->this_idx++;
633 return 0;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635rtattr_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700636 nlmsg_cancel(skb, nlh);
637 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
641{
642 struct xfrm_dump_info info;
643
644 info.in_skb = cb->skb;
645 info.out_skb = skb;
646 info.nlmsg_seq = cb->nlh->nlmsg_seq;
647 info.nlmsg_flags = NLM_F_MULTI;
648 info.this_idx = 0;
649 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700650 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 cb->args[0] = info.this_idx;
652
653 return skb->len;
654}
655
656static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
657 struct xfrm_state *x, u32 seq)
658{
659 struct xfrm_dump_info info;
660 struct sk_buff *skb;
661
662 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
663 if (!skb)
664 return ERR_PTR(-ENOMEM);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 info.in_skb = in_skb;
667 info.out_skb = skb;
668 info.nlmsg_seq = seq;
669 info.nlmsg_flags = 0;
670 info.this_idx = info.start_idx = 0;
671
672 if (dump_one_state(x, 0, &info)) {
673 kfree_skb(skb);
674 return NULL;
675 }
676
677 return skb;
678}
679
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700680static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
681{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700682 struct xfrmk_spdinfo si;
683 struct xfrmu_spdinfo spc;
684 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700685 struct nlmsghdr *nlh;
686 u32 *f;
687
688 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
689 if (nlh == NULL) /* shouldnt really happen ... */
690 return -EMSGSIZE;
691
692 f = nlmsg_data(nlh);
693 *f = flags;
694 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700695 spc.incnt = si.incnt;
696 spc.outcnt = si.outcnt;
697 spc.fwdcnt = si.fwdcnt;
698 spc.inscnt = si.inscnt;
699 spc.outscnt = si.outscnt;
700 spc.fwdscnt = si.fwdscnt;
701 sph.spdhcnt = si.spdhcnt;
702 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700703
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700704 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
705 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700706
707 return nlmsg_end(skb, nlh);
708
709nla_put_failure:
710 nlmsg_cancel(skb, nlh);
711 return -EMSGSIZE;
712}
713
714static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
715 struct rtattr **xfrma)
716{
717 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700718 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700719 u32 spid = NETLINK_CB(skb).pid;
720 u32 seq = nlh->nlmsg_seq;
721 int len = NLMSG_LENGTH(sizeof(u32));
722
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700723 len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
724 len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700725
726 r_skb = alloc_skb(len, GFP_ATOMIC);
727 if (r_skb == NULL)
728 return -ENOMEM;
729
730 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
731 BUG();
732
733 return nlmsg_unicast(xfrm_nl, r_skb, spid);
734}
735
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700736static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
737{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700738 struct xfrmk_sadinfo si;
739 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700740 struct nlmsghdr *nlh;
741 u32 *f;
742
743 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
744 if (nlh == NULL) /* shouldnt really happen ... */
745 return -EMSGSIZE;
746
747 f = nlmsg_data(nlh);
748 *f = flags;
749 xfrm_sad_getinfo(&si);
750
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700751 sh.sadhmcnt = si.sadhmcnt;
752 sh.sadhcnt = si.sadhcnt;
753
754 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
755 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700756
757 return nlmsg_end(skb, nlh);
758
759nla_put_failure:
760 nlmsg_cancel(skb, nlh);
761 return -EMSGSIZE;
762}
763
764static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
765 struct rtattr **xfrma)
766{
767 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700768 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700769 u32 spid = NETLINK_CB(skb).pid;
770 u32 seq = nlh->nlmsg_seq;
771 int len = NLMSG_LENGTH(sizeof(u32));
772
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700773 len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
774 len += RTA_SPACE(sizeof(u32));
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700775
776 r_skb = alloc_skb(len, GFP_ATOMIC);
777
778 if (r_skb == NULL)
779 return -ENOMEM;
780
781 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
782 BUG();
783
784 return nlmsg_unicast(xfrm_nl, r_skb, spid);
785}
786
Christoph Hellwig22e70052007-01-02 15:22:30 -0800787static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
788 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Thomas Graf7b67c852007-08-22 13:53:52 -0700790 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct xfrm_state *x;
792 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700793 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Christoph Hellwig22e70052007-01-02 15:22:30 -0800795 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (x == NULL)
797 goto out_noput;
798
799 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
800 if (IS_ERR(resp_skb)) {
801 err = PTR_ERR(resp_skb);
802 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700803 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 xfrm_state_put(x);
806out_noput:
807 return err;
808}
809
810static int verify_userspi_info(struct xfrm_userspi_info *p)
811{
812 switch (p->info.id.proto) {
813 case IPPROTO_AH:
814 case IPPROTO_ESP:
815 break;
816
817 case IPPROTO_COMP:
818 /* IPCOMP spi is 16-bits. */
819 if (p->max >= 0x10000)
820 return -EINVAL;
821 break;
822
823 default:
824 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (p->min > p->max)
828 return -EINVAL;
829
830 return 0;
831}
832
Christoph Hellwig22e70052007-01-02 15:22:30 -0800833static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
834 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct xfrm_state *x;
837 struct xfrm_userspi_info *p;
838 struct sk_buff *resp_skb;
839 xfrm_address_t *daddr;
840 int family;
841 int err;
842
Thomas Graf7b67c852007-08-22 13:53:52 -0700843 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 err = verify_userspi_info(p);
845 if (err)
846 goto out_noput;
847
848 family = p->info.family;
849 daddr = &p->info.id.daddr;
850
851 x = NULL;
852 if (p->info.seq) {
853 x = xfrm_find_acq_byseq(p->info.seq);
854 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
855 xfrm_state_put(x);
856 x = NULL;
857 }
858 }
859
860 if (!x)
861 x = xfrm_find_acq(p->info.mode, p->info.reqid,
862 p->info.id.proto, daddr,
863 &p->info.saddr, 1,
864 family);
865 err = -ENOENT;
866 if (x == NULL)
867 goto out_noput;
868
869 resp_skb = ERR_PTR(-ENOENT);
870
871 spin_lock_bh(&x->lock);
872 if (x->km.state != XFRM_STATE_DEAD) {
873 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
874 if (x->id.spi)
875 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
876 }
877 spin_unlock_bh(&x->lock);
878
879 if (IS_ERR(resp_skb)) {
880 err = PTR_ERR(resp_skb);
881 goto out;
882 }
883
Thomas Graf082a1ad2007-08-22 13:54:36 -0700884 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886out:
887 xfrm_state_put(x);
888out_noput:
889 return err;
890}
891
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800892static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 switch (dir) {
895 case XFRM_POLICY_IN:
896 case XFRM_POLICY_OUT:
897 case XFRM_POLICY_FWD:
898 break;
899
900 default:
901 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return 0;
905}
906
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800907static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700908{
909 switch (type) {
910 case XFRM_POLICY_TYPE_MAIN:
911#ifdef CONFIG_XFRM_SUB_POLICY
912 case XFRM_POLICY_TYPE_SUB:
913#endif
914 break;
915
916 default:
917 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700918 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700919
920 return 0;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
924{
925 switch (p->share) {
926 case XFRM_SHARE_ANY:
927 case XFRM_SHARE_SESSION:
928 case XFRM_SHARE_USER:
929 case XFRM_SHARE_UNIQUE:
930 break;
931
932 default:
933 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 switch (p->action) {
937 case XFRM_POLICY_ALLOW:
938 case XFRM_POLICY_BLOCK:
939 break;
940
941 default:
942 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 switch (p->sel.family) {
946 case AF_INET:
947 break;
948
949 case AF_INET6:
950#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
951 break;
952#else
953 return -EAFNOSUPPORT;
954#endif
955
956 default:
957 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 return verify_policy_dir(p->dir);
961}
962
Trent Jaegerdf718372005-12-13 23:12:27 -0800963static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
964{
965 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
966 struct xfrm_user_sec_ctx *uctx;
967
968 if (!rt)
969 return 0;
970
971 uctx = RTA_DATA(rt);
972 return security_xfrm_policy_alloc(pol, uctx);
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
976 int nr)
977{
978 int i;
979
980 xp->xfrm_nr = nr;
981 for (i = 0; i < nr; i++, ut++) {
982 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
983
984 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
985 memcpy(&t->saddr, &ut->saddr,
986 sizeof(xfrm_address_t));
987 t->reqid = ut->reqid;
988 t->mode = ut->mode;
989 t->share = ut->share;
990 t->optional = ut->optional;
991 t->aalgos = ut->aalgos;
992 t->ealgos = ut->ealgos;
993 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800994 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996}
997
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800998static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
999{
1000 int i;
1001
1002 if (nr > XFRM_MAX_DEPTH)
1003 return -EINVAL;
1004
1005 for (i = 0; i < nr; i++) {
1006 /* We never validated the ut->family value, so many
1007 * applications simply leave it at zero. The check was
1008 * never made and ut->family was ignored because all
1009 * templates could be assumed to have the same family as
1010 * the policy itself. Now that we will have ipv4-in-ipv6
1011 * and ipv6-in-ipv4 tunnels, this is no longer true.
1012 */
1013 if (!ut[i].family)
1014 ut[i].family = family;
1015
1016 switch (ut[i].family) {
1017 case AF_INET:
1018 break;
1019#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1020 case AF_INET6:
1021 break;
1022#endif
1023 default:
1024 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001025 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001026 }
1027
1028 return 0;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1032{
1033 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!rt) {
1036 pol->xfrm_nr = 0;
1037 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001038 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1039 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1040 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001042 err = validate_tmpl(nr, utmpl, pol->family);
1043 if (err)
1044 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 copy_templates(pol, RTA_DATA(rt), nr);
1047 }
1048 return 0;
1049}
1050
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001051static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1052{
1053 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1054 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001055 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001056 int err;
1057
1058 if (rt) {
1059 if (rt->rta_len < sizeof(*upt))
1060 return -EINVAL;
1061
1062 upt = RTA_DATA(rt);
1063 type = upt->type;
1064 }
1065
1066 err = verify_policy_type(type);
1067 if (err)
1068 return err;
1069
1070 *tp = type;
1071 return 0;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1075{
1076 xp->priority = p->priority;
1077 xp->index = p->index;
1078 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1079 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1080 xp->action = p->action;
1081 xp->flags = p->flags;
1082 xp->family = p->sel.family;
1083 /* XXX xp->share = p->share; */
1084}
1085
1086static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1087{
1088 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1089 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1090 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1091 p->priority = xp->priority;
1092 p->index = xp->index;
1093 p->sel.family = xp->family;
1094 p->dir = dir;
1095 p->action = xp->action;
1096 p->flags = xp->flags;
1097 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1098}
1099
1100static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1101{
1102 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1103 int err;
1104
1105 if (!xp) {
1106 *errp = -ENOMEM;
1107 return NULL;
1108 }
1109
1110 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001111
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001112 err = copy_from_user_policy_type(&xp->type, xfrma);
1113 if (err)
1114 goto error;
1115
Trent Jaegerdf718372005-12-13 23:12:27 -08001116 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1117 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001118 if (err)
1119 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001122 error:
1123 *errp = err;
1124 kfree(xp);
1125 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Christoph Hellwig22e70052007-01-02 15:22:30 -08001128static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1129 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Thomas Graf7b67c852007-08-22 13:53:52 -07001131 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001133 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int err;
1135 int excl;
1136
1137 err = verify_newpolicy_info(p);
1138 if (err)
1139 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001140 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001141 if (err)
1142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Christoph Hellwig22e70052007-01-02 15:22:30 -08001144 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!xp)
1146 return err;
1147
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001148 /* shouldnt excl be based on nlh flags??
1149 * Aha! this is anti-netlink really i.e more pfkey derived
1150 * in netlink excl is a flag and you wouldnt need
1151 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1153 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001154 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1155 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001158 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 kfree(xp);
1160 return err;
1161 }
1162
Herbert Xuf60f6b82005-06-18 22:44:37 -07001163 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001164 c.seq = nlh->nlmsg_seq;
1165 c.pid = nlh->nlmsg_pid;
1166 km_policy_notify(xp, p->dir, &c);
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 xfrm_pol_put(xp);
1169
1170 return 0;
1171}
1172
1173static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1174{
1175 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1176 int i;
1177
1178 if (xp->xfrm_nr == 0)
1179 return 0;
1180
1181 for (i = 0; i < xp->xfrm_nr; i++) {
1182 struct xfrm_user_tmpl *up = &vec[i];
1183 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1184
1185 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001186 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1188 up->reqid = kp->reqid;
1189 up->mode = kp->mode;
1190 up->share = kp->share;
1191 up->optional = kp->optional;
1192 up->aalgos = kp->aalgos;
1193 up->ealgos = kp->ealgos;
1194 up->calgos = kp->calgos;
1195 }
1196 RTA_PUT(skb, XFRMA_TMPL,
1197 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1198 vec);
1199
1200 return 0;
1201
1202rtattr_failure:
1203 return -1;
1204}
1205
Serge Hallyn0d681622006-07-24 23:30:44 -07001206static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001207{
Serge Hallyn0d681622006-07-24 23:30:44 -07001208 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1209 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1210 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001211
Serge Hallyn0d681622006-07-24 23:30:44 -07001212 uctx->exttype = XFRMA_SEC_CTX;
1213 uctx->len = ctx_size;
1214 uctx->ctx_doi = s->ctx_doi;
1215 uctx->ctx_alg = s->ctx_alg;
1216 uctx->ctx_len = s->ctx_len;
1217 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001218 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001219
1220 rtattr_failure:
1221 return -1;
1222}
1223
Serge Hallyn0d681622006-07-24 23:30:44 -07001224static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1225{
1226 if (x->security) {
1227 return copy_sec_ctx(x->security, skb);
1228 }
1229 return 0;
1230}
1231
1232static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1233{
1234 if (xp->security) {
1235 return copy_sec_ctx(xp->security, skb);
1236 }
1237 return 0;
1238}
1239
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001240#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001241static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001242{
1243 struct xfrm_userpolicy_type upt;
1244
1245 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001246 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001247
1248 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1249
1250 return 0;
1251
1252rtattr_failure:
1253 return -1;
1254}
1255
1256#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001257static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001258{
1259 return 0;
1260}
1261#endif
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1264{
1265 struct xfrm_dump_info *sp = ptr;
1266 struct xfrm_userpolicy_info *p;
1267 struct sk_buff *in_skb = sp->in_skb;
1268 struct sk_buff *skb = sp->out_skb;
1269 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 if (sp->this_idx < sp->start_idx)
1272 goto out;
1273
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001274 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1275 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1276 if (nlh == NULL)
1277 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Thomas Graf7b67c852007-08-22 13:53:52 -07001279 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 copy_to_user_policy(xp, p, dir);
1281 if (copy_to_user_tmpl(xp, skb) < 0)
1282 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001283 if (copy_to_user_sec_ctx(xp, skb))
1284 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001285 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001286 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Thomas Graf98250692007-08-22 12:47:26 -07001288 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289out:
1290 sp->this_idx++;
1291 return 0;
1292
1293nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001294 nlmsg_cancel(skb, nlh);
1295 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1299{
1300 struct xfrm_dump_info info;
1301
1302 info.in_skb = cb->skb;
1303 info.out_skb = skb;
1304 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1305 info.nlmsg_flags = NLM_F_MULTI;
1306 info.this_idx = 0;
1307 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001308 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1309#ifdef CONFIG_XFRM_SUB_POLICY
1310 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1311#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 cb->args[0] = info.this_idx;
1313
1314 return skb->len;
1315}
1316
1317static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1318 struct xfrm_policy *xp,
1319 int dir, u32 seq)
1320{
1321 struct xfrm_dump_info info;
1322 struct sk_buff *skb;
1323
1324 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1325 if (!skb)
1326 return ERR_PTR(-ENOMEM);
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 info.in_skb = in_skb;
1329 info.out_skb = skb;
1330 info.nlmsg_seq = seq;
1331 info.nlmsg_flags = 0;
1332 info.this_idx = info.start_idx = 0;
1333
1334 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1335 kfree_skb(skb);
1336 return NULL;
1337 }
1338
1339 return skb;
1340}
1341
Christoph Hellwig22e70052007-01-02 15:22:30 -08001342static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1343 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 struct xfrm_policy *xp;
1346 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001347 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001349 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int delete;
1351
Thomas Graf7b67c852007-08-22 13:53:52 -07001352 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1354
Christoph Hellwig22e70052007-01-02 15:22:30 -08001355 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001356 if (err)
1357 return err;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 err = verify_policy_dir(p->dir);
1360 if (err)
1361 return err;
1362
1363 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001364 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001365 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001366 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001367 struct xfrm_policy tmp;
1368
Christoph Hellwig22e70052007-01-02 15:22:30 -08001369 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001370 if (err)
1371 return err;
1372
1373 memset(&tmp, 0, sizeof(struct xfrm_policy));
1374 if (rt) {
1375 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1376
1377 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1378 return err;
1379 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001380 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1381 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001382 security_xfrm_policy_free(&tmp);
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (xp == NULL)
1385 return -ENOENT;
1386
1387 if (!delete) {
1388 struct sk_buff *resp_skb;
1389
1390 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1391 if (IS_ERR(resp_skb)) {
1392 err = PTR_ERR(resp_skb);
1393 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001394 err = nlmsg_unicast(xfrm_nl, resp_skb,
1395 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001397 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001398 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1399 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1400
1401 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001402 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001403
Herbert Xue7443892005-06-18 22:44:18 -07001404 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001405 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1408 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001411out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001412 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return err;
1414}
1415
Christoph Hellwig22e70052007-01-02 15:22:30 -08001416static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1417 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001419 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001420 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001421 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001422 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Joy Latten161a09e2006-11-27 13:11:54 -06001424 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1425 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001426 err = xfrm_state_flush(p->proto, &audit_info);
1427 if (err)
1428 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001429 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001430 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 c.seq = nlh->nlmsg_seq;
1432 c.pid = nlh->nlmsg_pid;
1433 km_state_notify(NULL, &c);
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436}
1437
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438
1439static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1440{
1441 struct xfrm_aevent_id *id;
1442 struct nlmsghdr *nlh;
1443 struct xfrm_lifetime_cur ltime;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001444
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001445 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1446 if (nlh == NULL)
1447 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001448
Thomas Graf7b67c852007-08-22 13:53:52 -07001449 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001450 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001451 id->sa_id.spi = x->id.spi;
1452 id->sa_id.family = x->props.family;
1453 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001454 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1455 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001456 id->flags = c->data.aevent;
1457
1458 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1459
1460 ltime.bytes = x->curlft.bytes;
1461 ltime.packets = x->curlft.packets;
1462 ltime.add_time = x->curlft.add_time;
1463 ltime.use_time = x->curlft.use_time;
1464
1465 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1466
1467 if (id->flags&XFRM_AE_RTHR) {
1468 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1469 }
1470
1471 if (id->flags&XFRM_AE_ETHR) {
1472 u32 etimer = x->replay_maxage*10/HZ;
1473 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1474 }
1475
Thomas Graf98250692007-08-22 12:47:26 -07001476 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001477
1478rtattr_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001479 nlmsg_cancel(skb, nlh);
1480 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001481}
1482
Christoph Hellwig22e70052007-01-02 15:22:30 -08001483static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1484 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001485{
1486 struct xfrm_state *x;
1487 struct sk_buff *r_skb;
1488 int err;
1489 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001490 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001491 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1492 struct xfrm_usersa_id *id = &p->sa_id;
1493
1494 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1495 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1496
1497 if (p->flags&XFRM_AE_RTHR)
1498 len+=RTA_SPACE(sizeof(u32));
1499
1500 if (p->flags&XFRM_AE_ETHR)
1501 len+=RTA_SPACE(sizeof(u32));
1502
1503 r_skb = alloc_skb(len, GFP_ATOMIC);
1504 if (r_skb == NULL)
1505 return -ENOMEM;
1506
1507 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1508 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001509 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001510 return -ESRCH;
1511 }
1512
1513 /*
1514 * XXX: is this lock really needed - none of the other
1515 * gets lock (the concern is things getting updated
1516 * while we are still reading) - jhs
1517 */
1518 spin_lock_bh(&x->lock);
1519 c.data.aevent = p->flags;
1520 c.seq = nlh->nlmsg_seq;
1521 c.pid = nlh->nlmsg_pid;
1522
1523 if (build_aevent(r_skb, x, &c) < 0)
1524 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001525 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001526 spin_unlock_bh(&x->lock);
1527 xfrm_state_put(x);
1528 return err;
1529}
1530
Christoph Hellwig22e70052007-01-02 15:22:30 -08001531static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1532 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001533{
1534 struct xfrm_state *x;
1535 struct km_event c;
1536 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001537 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001538 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1539 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1540
1541 if (!lt && !rp)
1542 return err;
1543
1544 /* pedantic mode - thou shalt sayeth replaceth */
1545 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1546 return err;
1547
1548 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1549 if (x == NULL)
1550 return -ESRCH;
1551
1552 if (x->km.state != XFRM_STATE_VALID)
1553 goto out;
1554
1555 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001556 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001557 spin_unlock_bh(&x->lock);
1558 if (err < 0)
1559 goto out;
1560
1561 c.event = nlh->nlmsg_type;
1562 c.seq = nlh->nlmsg_seq;
1563 c.pid = nlh->nlmsg_pid;
1564 c.data.aevent = XFRM_AE_CU;
1565 km_state_notify(x, &c);
1566 err = 0;
1567out:
1568 xfrm_state_put(x);
1569 return err;
1570}
1571
Christoph Hellwig22e70052007-01-02 15:22:30 -08001572static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1573 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001575 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001576 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001577 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001578 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001579
Christoph Hellwig22e70052007-01-02 15:22:30 -08001580 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001581 if (err)
1582 return err;
1583
Joy Latten161a09e2006-11-27 13:11:54 -06001584 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1585 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001586 err = xfrm_policy_flush(type, &audit_info);
1587 if (err)
1588 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001589 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001590 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001591 c.seq = nlh->nlmsg_seq;
1592 c.pid = nlh->nlmsg_pid;
1593 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return 0;
1595}
1596
Christoph Hellwig22e70052007-01-02 15:22:30 -08001597static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1598 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001599{
1600 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001601 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001602 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001603 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001604 int err = -ENOENT;
1605
Christoph Hellwig22e70052007-01-02 15:22:30 -08001606 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001607 if (err)
1608 return err;
1609
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001610 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001611 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001612 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001613 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001614 struct xfrm_policy tmp;
1615
Christoph Hellwig22e70052007-01-02 15:22:30 -08001616 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001617 if (err)
1618 return err;
1619
1620 memset(&tmp, 0, sizeof(struct xfrm_policy));
1621 if (rt) {
1622 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1623
1624 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1625 return err;
1626 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001627 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1628 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001629 security_xfrm_policy_free(&tmp);
1630 }
1631
1632 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001633 return -ENOENT;
1634 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001635 if (xp->dead) {
1636 read_unlock(&xp->lock);
1637 goto out;
1638 }
1639
1640 read_unlock(&xp->lock);
1641 err = 0;
1642 if (up->hard) {
1643 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001644 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1645 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1646
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001647 } else {
1648 // reset the timers here?
1649 printk("Dont know what to do with soft policy expire\n");
1650 }
1651 km_policy_expired(xp, p->dir, up->hard, current->pid);
1652
1653out:
1654 xfrm_pol_put(xp);
1655 return err;
1656}
1657
Christoph Hellwig22e70052007-01-02 15:22:30 -08001658static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1659 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001660{
1661 struct xfrm_state *x;
1662 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001663 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001664 struct xfrm_usersa_info *p = &ue->state;
1665
1666 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001667
David S. Miller3a765aa2007-02-26 14:52:21 -08001668 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001669 if (x == NULL)
1670 return err;
1671
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001672 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001673 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001674 if (x->km.state != XFRM_STATE_VALID)
1675 goto out;
1676 km_state_expired(x, ue->hard, current->pid);
1677
Joy Latten161a09e2006-11-27 13:11:54 -06001678 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001679 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001680 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1681 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1682 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001683 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001684out:
1685 spin_unlock_bh(&x->lock);
1686 xfrm_state_put(x);
1687 return err;
1688}
1689
Christoph Hellwig22e70052007-01-02 15:22:30 -08001690static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1691 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001692{
1693 struct xfrm_policy *xp;
1694 struct xfrm_user_tmpl *ut;
1695 int i;
1696 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1697
Thomas Graf7b67c852007-08-22 13:53:52 -07001698 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001699 struct xfrm_state *x = xfrm_state_alloc();
1700 int err = -ENOMEM;
1701
1702 if (!x)
1703 return err;
1704
1705 err = verify_newpolicy_info(&ua->policy);
1706 if (err) {
1707 printk("BAD policy passed\n");
1708 kfree(x);
1709 return err;
1710 }
1711
1712 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001713 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1714 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001715 kfree(x);
1716 return err;
1717 }
1718
1719 memcpy(&x->id, &ua->id, sizeof(ua->id));
1720 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1721 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1722
1723 ut = RTA_DATA(rt);
1724 /* extract the templates and for each call km_key */
1725 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1726 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1727 memcpy(&x->id, &t->id, sizeof(x->id));
1728 x->props.mode = t->mode;
1729 x->props.reqid = t->reqid;
1730 x->props.family = ut->family;
1731 t->aalgos = ua->aalgos;
1732 t->ealgos = ua->ealgos;
1733 t->calgos = ua->calgos;
1734 err = km_query(x, t, xp);
1735
1736 }
1737
1738 kfree(x);
1739 kfree(xp);
1740
1741 return 0;
1742}
1743
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001744#ifdef CONFIG_XFRM_MIGRATE
1745static int verify_user_migrate(struct rtattr **xfrma)
1746{
1747 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1748 struct xfrm_user_migrate *um;
1749
1750 if (!rt)
1751 return -EINVAL;
1752
1753 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1754 return -EINVAL;
1755
1756 return 0;
1757}
1758
1759static int copy_from_user_migrate(struct xfrm_migrate *ma,
1760 struct rtattr **xfrma, int *num)
1761{
1762 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1763 struct xfrm_user_migrate *um;
1764 int i, num_migrate;
1765
1766 um = RTA_DATA(rt);
1767 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1768
1769 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1770 return -EINVAL;
1771
1772 for (i = 0; i < num_migrate; i++, um++, ma++) {
1773 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1774 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1775 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1776 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1777
1778 ma->proto = um->proto;
1779 ma->mode = um->mode;
1780 ma->reqid = um->reqid;
1781
1782 ma->old_family = um->old_family;
1783 ma->new_family = um->new_family;
1784 }
1785
1786 *num = i;
1787 return 0;
1788}
1789
1790static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1791 struct rtattr **xfrma)
1792{
Thomas Graf7b67c852007-08-22 13:53:52 -07001793 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001794 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1795 u8 type;
1796 int err;
1797 int n = 0;
1798
1799 err = verify_user_migrate((struct rtattr **)xfrma);
1800 if (err)
1801 return err;
1802
1803 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1804 if (err)
1805 return err;
1806
1807 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1808 (struct rtattr **)xfrma, &n);
1809 if (err)
1810 return err;
1811
1812 if (!n)
1813 return 0;
1814
1815 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1816
1817 return 0;
1818}
1819#else
1820static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1821 struct rtattr **xfrma)
1822{
1823 return -ENOPROTOOPT;
1824}
1825#endif
1826
1827#ifdef CONFIG_XFRM_MIGRATE
1828static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1829{
1830 struct xfrm_user_migrate um;
1831
1832 memset(&um, 0, sizeof(um));
1833 um.proto = m->proto;
1834 um.mode = m->mode;
1835 um.reqid = m->reqid;
1836 um.old_family = m->old_family;
1837 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1838 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1839 um.new_family = m->new_family;
1840 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1841 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1842
1843 RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1844 return 0;
1845
1846rtattr_failure:
1847 return -1;
1848}
1849
1850static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1851 int num_migrate, struct xfrm_selector *sel,
1852 u8 dir, u8 type)
1853{
1854 struct xfrm_migrate *mp;
1855 struct xfrm_userpolicy_id *pol_id;
1856 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001857 int i;
1858
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001859 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1860 if (nlh == NULL)
1861 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001862
Thomas Graf7b67c852007-08-22 13:53:52 -07001863 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001864 /* copy data from selector, dir, and type to the pol_id */
1865 memset(pol_id, 0, sizeof(*pol_id));
1866 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1867 pol_id->dir = dir;
1868
1869 if (copy_to_user_policy_type(type, skb) < 0)
1870 goto nlmsg_failure;
1871
1872 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1873 if (copy_to_user_migrate(mp, skb) < 0)
1874 goto nlmsg_failure;
1875 }
1876
Thomas Graf98250692007-08-22 12:47:26 -07001877 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001878nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001879 nlmsg_cancel(skb, nlh);
1880 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001881}
1882
1883static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1884 struct xfrm_migrate *m, int num_migrate)
1885{
1886 struct sk_buff *skb;
1887 size_t len;
1888
1889 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1890 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1891#ifdef CONFIG_XFRM_SUB_POLICY
1892 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1893#endif
1894 skb = alloc_skb(len, GFP_ATOMIC);
1895 if (skb == NULL)
1896 return -ENOMEM;
1897
1898 /* build migrate */
1899 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1900 BUG();
1901
Thomas Graf082a1ad2007-08-22 13:54:36 -07001902 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001903}
1904#else
1905static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1906 struct xfrm_migrate *m, int num_migrate)
1907{
1908 return -ENOPROTOOPT;
1909}
1910#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001911
Thomas Graf492b5582005-05-03 14:26:40 -07001912#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1913
1914static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1915 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1916 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1917 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1918 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1919 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1920 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1921 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001922 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001923 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001924 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1925 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001926 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001927 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1928 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001929 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1930 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001931 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001932 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001933 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001934 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935};
1936
Thomas Graf492b5582005-05-03 14:26:40 -07001937#undef XMSGSIZE
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001940 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001942} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1943 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1944 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1945 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1946 .dump = xfrm_dump_sa },
1947 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1948 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1949 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1950 .dump = xfrm_dump_policy },
1951 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001952 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001953 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001954 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1955 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001956 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001957 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1958 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001959 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1960 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001961 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001962 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001963 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964};
1965
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001966static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct rtattr *xfrma[XFRMA_MAX];
1969 struct xfrm_link *link;
Thomas Grafc702e802007-03-22 23:30:55 -07001970 int type, min_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 type -= XFRM_MSG_BASE;
1977 link = &xfrm_dispatch[type];
1978
1979 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001980 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Thomas Graf492b5582005-05-03 14:26:40 -07001983 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1984 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1985 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Thomas Grafc702e802007-03-22 23:30:55 -07001989 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
1992 memset(xfrma, 0, sizeof(xfrma));
1993
1994 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 if (nlh->nlmsg_len > min_len) {
1998 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1999 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
2000
2001 while (RTA_OK(attr, attrlen)) {
2002 unsigned short flavor = attr->rta_type;
2003 if (flavor) {
2004 if (flavor > XFRMA_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 xfrma[flavor - 1] = attr;
2007 }
2008 attr = RTA_NEXT(attr, attrlen);
2009 }
2010 }
2011
2012 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002015 return link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018static void xfrm_netlink_rcv(struct sock *sk, int len)
2019{
Thomas Graf88fc2c82005-11-10 02:25:54 +01002020 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002023 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01002024 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002025 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002027 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002030static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct xfrm_user_expire *ue;
2033 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002035 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2036 if (nlh == NULL)
2037 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Thomas Graf7b67c852007-08-22 13:53:52 -07002039 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002041 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Thomas Graf98250692007-08-22 12:47:26 -07002043 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002046static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002049 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002051 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (skb == NULL)
2053 return -ENOMEM;
2054
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002055 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 BUG();
2057
Thomas Graf082a1ad2007-08-22 13:54:36 -07002058 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002061static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2062{
2063 struct sk_buff *skb;
2064 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2065
2066 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2067 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2068 skb = alloc_skb(len, GFP_ATOMIC);
2069 if (skb == NULL)
2070 return -ENOMEM;
2071
2072 if (build_aevent(skb, x, c) < 0)
2073 BUG();
2074
Thomas Graf082a1ad2007-08-22 13:54:36 -07002075 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002076}
2077
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002078static int xfrm_notify_sa_flush(struct km_event *c)
2079{
2080 struct xfrm_usersa_flush *p;
2081 struct nlmsghdr *nlh;
2082 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002083 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2084
2085 skb = alloc_skb(len, GFP_ATOMIC);
2086 if (skb == NULL)
2087 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002088
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002089 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2090 if (nlh == NULL) {
2091 kfree_skb(skb);
2092 return -EMSGSIZE;
2093 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094
Thomas Graf7b67c852007-08-22 13:53:52 -07002095 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002096 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097
Thomas Graf98250692007-08-22 12:47:26 -07002098 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002099
Thomas Graf082a1ad2007-08-22 13:54:36 -07002100 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002101}
2102
Dave Jonesb6f99a22007-03-22 12:27:49 -07002103static inline int xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002104{
Herbert Xu0603eac2005-06-18 22:54:36 -07002105 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002106 if (x->aalg)
2107 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
2108 if (x->ealg)
2109 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
2110 if (x->calg)
2111 l += RTA_SPACE(sizeof(*x->calg));
2112 if (x->encap)
2113 l += RTA_SPACE(sizeof(*x->encap));
2114
2115 return l;
2116}
2117
2118static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2119{
2120 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002121 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002122 struct nlmsghdr *nlh;
2123 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002125 int headlen;
2126
2127 headlen = sizeof(*p);
2128 if (c->event == XFRM_MSG_DELSA) {
2129 len += RTA_SPACE(headlen);
2130 headlen = sizeof(*id);
2131 }
2132 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002133
2134 skb = alloc_skb(len, GFP_ATOMIC);
2135 if (skb == NULL)
2136 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002137
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002138 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2139 if (nlh == NULL)
2140 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141
Thomas Graf7b67c852007-08-22 13:53:52 -07002142 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002143 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7b67c852007-08-22 13:53:52 -07002144 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002145 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2146 id->spi = x->id.spi;
2147 id->family = x->props.family;
2148 id->proto = x->id.proto;
2149
2150 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2151 }
2152
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002153 copy_to_user_state(x, p);
2154
2155 if (x->aalg)
2156 RTA_PUT(skb, XFRMA_ALG_AUTH,
2157 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
2158 if (x->ealg)
2159 RTA_PUT(skb, XFRMA_ALG_CRYPT,
2160 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
2161 if (x->calg)
2162 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2163
2164 if (x->encap)
2165 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2166
Thomas Graf98250692007-08-22 12:47:26 -07002167 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002168
Thomas Graf082a1ad2007-08-22 13:54:36 -07002169 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002170
2171nlmsg_failure:
2172rtattr_failure:
2173 kfree_skb(skb);
2174 return -1;
2175}
2176
2177static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2178{
2179
2180 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002181 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002183 case XFRM_MSG_NEWAE:
2184 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002185 case XFRM_MSG_DELSA:
2186 case XFRM_MSG_UPDSA:
2187 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002189 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002190 return xfrm_notify_sa_flush(c);
2191 default:
2192 printk("xfrm_user: Unknown SA event %d\n", c->event);
2193 break;
2194 }
2195
2196 return 0;
2197
2198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2201 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2202 int dir)
2203{
2204 struct xfrm_user_acquire *ua;
2205 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 __u32 seq = xfrm_get_acqseq();
2207
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002208 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2209 if (nlh == NULL)
2210 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Thomas Graf7b67c852007-08-22 13:53:52 -07002212 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 memcpy(&ua->id, &x->id, sizeof(ua->id));
2214 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2215 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2216 copy_to_user_policy(xp, &ua->policy, dir);
2217 ua->aalgos = xt->aalgos;
2218 ua->ealgos = xt->ealgos;
2219 ua->calgos = xt->calgos;
2220 ua->seq = x->km.seq = seq;
2221
2222 if (copy_to_user_tmpl(xp, skb) < 0)
2223 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002224 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002225 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002226 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002227 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Thomas Graf98250692007-08-22 12:47:26 -07002229 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002232 nlmsg_cancel(skb, nlh);
2233 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
2236static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2237 struct xfrm_policy *xp, int dir)
2238{
2239 struct sk_buff *skb;
2240 size_t len;
2241
2242 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2243 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Joy Latten661697f2007-04-13 16:14:35 -07002244 len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002245#ifdef CONFIG_XFRM_SUB_POLICY
2246 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 skb = alloc_skb(len, GFP_ATOMIC);
2249 if (skb == NULL)
2250 return -ENOMEM;
2251
2252 if (build_acquire(skb, x, xt, xp, dir) < 0)
2253 BUG();
2254
Thomas Graf082a1ad2007-08-22 13:54:36 -07002255 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
2258/* User gives us xfrm_user_policy_info followed by an array of 0
2259 * or more templates.
2260 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002261static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 u8 *data, int len, int *dir)
2263{
2264 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2265 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2266 struct xfrm_policy *xp;
2267 int nr;
2268
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002269 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 case AF_INET:
2271 if (opt != IP_XFRM_POLICY) {
2272 *dir = -EOPNOTSUPP;
2273 return NULL;
2274 }
2275 break;
2276#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2277 case AF_INET6:
2278 if (opt != IPV6_XFRM_POLICY) {
2279 *dir = -EOPNOTSUPP;
2280 return NULL;
2281 }
2282 break;
2283#endif
2284 default:
2285 *dir = -EINVAL;
2286 return NULL;
2287 }
2288
2289 *dir = -EINVAL;
2290
2291 if (len < sizeof(*p) ||
2292 verify_newpolicy_info(p))
2293 return NULL;
2294
2295 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002296 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return NULL;
2298
Herbert Xua4f1bac2005-07-26 15:43:17 -07002299 if (p->dir > XFRM_POLICY_OUT)
2300 return NULL;
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 xp = xfrm_policy_alloc(GFP_KERNEL);
2303 if (xp == NULL) {
2304 *dir = -ENOBUFS;
2305 return NULL;
2306 }
2307
2308 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002309 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 copy_templates(xp, ut, nr);
2311
2312 *dir = p->dir;
2313
2314 return xp;
2315}
2316
2317static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002318 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct xfrm_user_polexpire *upe;
2321 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002322 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002324 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2325 if (nlh == NULL)
2326 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Thomas Graf7b67c852007-08-22 13:53:52 -07002328 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 copy_to_user_policy(xp, &upe->pol, dir);
2330 if (copy_to_user_tmpl(xp, skb) < 0)
2331 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002332 if (copy_to_user_sec_ctx(xp, skb))
2333 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002334 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002335 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 upe->hard = !!hard;
2337
Thomas Graf98250692007-08-22 12:47:26 -07002338 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002341 nlmsg_cancel(skb, nlh);
2342 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002345static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct sk_buff *skb;
2348 size_t len;
2349
2350 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2351 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Joy Latten661697f2007-04-13 16:14:35 -07002352 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002353#ifdef CONFIG_XFRM_SUB_POLICY
2354 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 skb = alloc_skb(len, GFP_ATOMIC);
2357 if (skb == NULL)
2358 return -ENOMEM;
2359
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002360 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 BUG();
2362
Thomas Graf082a1ad2007-08-22 13:54:36 -07002363 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002366static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2367{
2368 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002369 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370 struct nlmsghdr *nlh;
2371 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002373 int headlen;
2374
2375 headlen = sizeof(*p);
2376 if (c->event == XFRM_MSG_DELPOLICY) {
2377 len += RTA_SPACE(headlen);
2378 headlen = sizeof(*id);
2379 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002380#ifdef CONFIG_XFRM_SUB_POLICY
2381 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2382#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002383 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384
2385 skb = alloc_skb(len, GFP_ATOMIC);
2386 if (skb == NULL)
2387 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002389 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2390 if (nlh == NULL)
2391 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392
Thomas Graf7b67c852007-08-22 13:53:52 -07002393 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002394 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7b67c852007-08-22 13:53:52 -07002395 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002396 memset(id, 0, sizeof(*id));
2397 id->dir = dir;
2398 if (c->data.byid)
2399 id->index = xp->index;
2400 else
2401 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2402
2403 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2404 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406 copy_to_user_policy(xp, p, dir);
2407 if (copy_to_user_tmpl(xp, skb) < 0)
2408 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002409 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002410 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411
Thomas Graf98250692007-08-22 12:47:26 -07002412 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413
Thomas Graf082a1ad2007-08-22 13:54:36 -07002414 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415
2416nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002417rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418 kfree_skb(skb);
2419 return -1;
2420}
2421
2422static int xfrm_notify_policy_flush(struct km_event *c)
2423{
2424 struct nlmsghdr *nlh;
2425 struct sk_buff *skb;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002426 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002427#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002428 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002429#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002430 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431
2432 skb = alloc_skb(len, GFP_ATOMIC);
2433 if (skb == NULL)
2434 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002436 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2437 if (nlh == NULL)
2438 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002439 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2440 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002441
Thomas Graf98250692007-08-22 12:47:26 -07002442 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002443
Thomas Graf082a1ad2007-08-22 13:54:36 -07002444 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002445
2446nlmsg_failure:
2447 kfree_skb(skb);
2448 return -1;
2449}
2450
2451static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2452{
2453
2454 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002455 case XFRM_MSG_NEWPOLICY:
2456 case XFRM_MSG_UPDPOLICY:
2457 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002459 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002461 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002462 return xfrm_exp_policy_notify(xp, dir, c);
2463 default:
2464 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2465 }
2466
2467 return 0;
2468
2469}
2470
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002471static int build_report(struct sk_buff *skb, u8 proto,
2472 struct xfrm_selector *sel, xfrm_address_t *addr)
2473{
2474 struct xfrm_user_report *ur;
2475 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002476
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002477 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2478 if (nlh == NULL)
2479 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002480
Thomas Graf7b67c852007-08-22 13:53:52 -07002481 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002482 ur->proto = proto;
2483 memcpy(&ur->sel, sel, sizeof(ur->sel));
2484
2485 if (addr)
2486 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2487
Thomas Graf98250692007-08-22 12:47:26 -07002488 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002489
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002490rtattr_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002491 nlmsg_cancel(skb, nlh);
2492 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002493}
2494
2495static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2496 xfrm_address_t *addr)
2497{
2498 struct sk_buff *skb;
2499 size_t len;
2500
2501 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2502 skb = alloc_skb(len, GFP_ATOMIC);
2503 if (skb == NULL)
2504 return -ENOMEM;
2505
2506 if (build_report(skb, proto, sel, addr) < 0)
2507 BUG();
2508
Thomas Graf082a1ad2007-08-22 13:54:36 -07002509 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512static struct xfrm_mgr netlink_mgr = {
2513 .id = "netlink",
2514 .notify = xfrm_send_state_notify,
2515 .acquire = xfrm_send_acquire,
2516 .compile_policy = xfrm_compile_policy,
2517 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002518 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002519 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520};
2521
2522static int __init xfrm_user_init(void)
2523{
Patrick McHardybe336902006-03-20 22:40:54 -08002524 struct sock *nlsk;
2525
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002526 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Patrick McHardybe336902006-03-20 22:40:54 -08002528 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002529 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002530 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002532 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 xfrm_register_km(&netlink_mgr);
2535
2536 return 0;
2537}
2538
2539static void __exit xfrm_user_exit(void)
2540{
Patrick McHardybe336902006-03-20 22:40:54 -08002541 struct sock *nlsk = xfrm_nl;
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002544 rcu_assign_pointer(xfrm_nl, NULL);
2545 synchronize_rcu();
2546 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
2549module_init(xfrm_user_init);
2550module_exit(xfrm_user_exit);
2551MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002552MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002553