blob: c35b9ea3b62b3eda7808d42961f87a9602cefa46 [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;
325}
326
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800327/*
328 * someday when pfkey also has support, we could have the code
329 * somehow made shareable and move it to xfrm_state.c - JHS
330 *
331*/
332static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
333{
334 int err = - EINVAL;
335 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
336 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
337 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
338 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
339
340 if (rp) {
341 struct xfrm_replay_state *replay;
342 if (RTA_PAYLOAD(rp) < sizeof(*replay))
343 goto error;
344 replay = RTA_DATA(rp);
345 memcpy(&x->replay, replay, sizeof(*replay));
346 memcpy(&x->preplay, replay, sizeof(*replay));
347 }
348
349 if (lt) {
350 struct xfrm_lifetime_cur *ltime;
351 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
352 goto error;
353 ltime = RTA_DATA(lt);
354 x->curlft.bytes = ltime->bytes;
355 x->curlft.packets = ltime->packets;
356 x->curlft.add_time = ltime->add_time;
357 x->curlft.use_time = ltime->use_time;
358 }
359
360 if (et) {
361 if (RTA_PAYLOAD(et) < sizeof(u32))
362 goto error;
363 x->replay_maxage = *(u32*)RTA_DATA(et);
364 }
365
366 if (rt) {
367 if (RTA_PAYLOAD(rt) < sizeof(u32))
368 goto error;
369 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
370 }
371
372 return 0;
373error:
374 return err;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
378 struct rtattr **xfrma,
379 int *errp)
380{
381 struct xfrm_state *x = xfrm_state_alloc();
382 int err = -ENOMEM;
383
384 if (!x)
385 goto error_no_put;
386
387 copy_from_user_state(x, p);
388
389 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
390 xfrm_aalg_get_byname,
391 xfrma[XFRMA_ALG_AUTH-1])))
392 goto error;
393 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
394 xfrm_ealg_get_byname,
395 xfrma[XFRMA_ALG_CRYPT-1])))
396 goto error;
397 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
398 xfrm_calg_get_byname,
399 xfrma[XFRMA_ALG_COMP-1])))
400 goto error;
401 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
402 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700403 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
404 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700405 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (err)
407 goto error;
408
Trent Jaegerdf718372005-12-13 23:12:27 -0800409 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
410 goto error;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800413 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
414 /* sysctl_xfrm_aevent_etime is in 100ms units */
415 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
416 x->preplay.bitmap = 0;
417 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
418 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
419
420 /* override default values from above */
421
422 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
423 if (err < 0)
424 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return x;
427
428error:
429 x->km.state = XFRM_STATE_DEAD;
430 xfrm_state_put(x);
431error_no_put:
432 *errp = err;
433 return NULL;
434}
435
Christoph Hellwig22e70052007-01-02 15:22:30 -0800436static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
437 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
440 struct xfrm_state *x;
441 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwig22e70052007-01-02 15:22:30 -0800444 err = verify_newsa_info(p, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (err)
446 return err;
447
Christoph Hellwig22e70052007-01-02 15:22:30 -0800448 x = xfrm_state_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!x)
450 return err;
451
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700452 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
454 err = xfrm_state_add(x);
455 else
456 err = xfrm_state_update(x);
457
Joy Latten161a09e2006-11-27 13:11:54 -0600458 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
459 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (err < 0) {
462 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800463 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700467 c.seq = nlh->nlmsg_seq;
468 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700469 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700470
471 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700472out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700473 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return err;
475}
476
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700477static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
478 struct rtattr **xfrma,
479 int *errp)
480{
481 struct xfrm_state *x = NULL;
482 int err;
483
484 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
485 err = -ESRCH;
486 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
487 } else {
488 xfrm_address_t *saddr = NULL;
489
490 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
491 if (err)
492 goto out;
493
494 if (!saddr) {
495 err = -EINVAL;
496 goto out;
497 }
498
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800499 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700500 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
501 p->family);
502 }
503
504 out:
505 if (!x && errp)
506 *errp = err;
507 return x;
508}
509
Christoph Hellwig22e70052007-01-02 15:22:30 -0800510static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
511 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700514 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700515 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
517
Christoph Hellwig22e70052007-01-02 15:22:30 -0800518 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David S. Miller6f68dc32006-06-08 23:58:52 -0700522 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700523 goto out;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700526 err = -EPERM;
527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700530 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600531
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700532 if (err < 0)
533 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700534
535 c.seq = nlh->nlmsg_seq;
536 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700537 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700538 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700540out:
Eric Paris16bec312007-03-07 16:02:16 -0800541 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
542 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700543 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
548{
549 memcpy(&p->id, &x->id, sizeof(p->id));
550 memcpy(&p->sel, &x->sel, sizeof(p->sel));
551 memcpy(&p->lft, &x->lft, sizeof(p->lft));
552 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
553 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700554 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 p->mode = x->props.mode;
556 p->replay_window = x->props.replay_window;
557 p->reqid = x->props.reqid;
558 p->family = x->props.family;
559 p->flags = x->props.flags;
560 p->seq = x->km.seq;
561}
562
563struct xfrm_dump_info {
564 struct sk_buff *in_skb;
565 struct sk_buff *out_skb;
566 u32 nlmsg_seq;
567 u16 nlmsg_flags;
568 int start_idx;
569 int this_idx;
570};
571
572static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
573{
574 struct xfrm_dump_info *sp = ptr;
575 struct sk_buff *in_skb = sp->in_skb;
576 struct sk_buff *skb = sp->out_skb;
577 struct xfrm_usersa_info *p;
578 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700579 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (sp->this_idx < sp->start_idx)
582 goto out;
583
584 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
585 sp->nlmsg_seq,
586 XFRM_MSG_NEWSA, sizeof(*p));
587 nlh->nlmsg_flags = sp->nlmsg_flags;
588
589 p = NLMSG_DATA(nlh);
590 copy_to_user_state(x, p);
591
592 if (x->aalg)
593 RTA_PUT(skb, XFRMA_ALG_AUTH,
594 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
595 if (x->ealg)
596 RTA_PUT(skb, XFRMA_ALG_CRYPT,
597 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
598 if (x->calg)
599 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
600
601 if (x->encap)
602 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
603
Trent Jaegerdf718372005-12-13 23:12:27 -0800604 if (x->security) {
605 int ctx_size = sizeof(struct xfrm_sec_ctx) +
606 x->security->ctx_len;
607 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
608 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
609
610 uctx->exttype = XFRMA_SEC_CTX;
611 uctx->len = ctx_size;
612 uctx->ctx_doi = x->security->ctx_doi;
613 uctx->ctx_alg = x->security->ctx_alg;
614 uctx->ctx_len = x->security->ctx_len;
615 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
616 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700617
618 if (x->coaddr)
619 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
620
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700621 if (x->lastused)
622 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
623
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700624 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625out:
626 sp->this_idx++;
627 return 0;
628
629nlmsg_failure:
630rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700631 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -1;
633}
634
635static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
636{
637 struct xfrm_dump_info info;
638
639 info.in_skb = cb->skb;
640 info.out_skb = skb;
641 info.nlmsg_seq = cb->nlh->nlmsg_seq;
642 info.nlmsg_flags = NLM_F_MULTI;
643 info.this_idx = 0;
644 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700645 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 cb->args[0] = info.this_idx;
647
648 return skb->len;
649}
650
651static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
652 struct xfrm_state *x, u32 seq)
653{
654 struct xfrm_dump_info info;
655 struct sk_buff *skb;
656
657 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
658 if (!skb)
659 return ERR_PTR(-ENOMEM);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 info.in_skb = in_skb;
662 info.out_skb = skb;
663 info.nlmsg_seq = seq;
664 info.nlmsg_flags = 0;
665 info.this_idx = info.start_idx = 0;
666
667 if (dump_one_state(x, 0, &info)) {
668 kfree_skb(skb);
669 return NULL;
670 }
671
672 return skb;
673}
674
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700675static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
676{
677 struct xfrm_spdinfo si;
678 struct nlmsghdr *nlh;
679 u32 *f;
680
681 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
682 if (nlh == NULL) /* shouldnt really happen ... */
683 return -EMSGSIZE;
684
685 f = nlmsg_data(nlh);
686 *f = flags;
687 xfrm_spd_getinfo(&si);
688
689 if (flags & XFRM_SPD_HMASK)
690 NLA_PUT_U32(skb, XFRMA_SPDHMASK, si.spdhcnt);
691 if (flags & XFRM_SPD_HMAX)
692 NLA_PUT_U32(skb, XFRMA_SPDHMAX, si.spdhmcnt);
693 if (flags & XFRM_SPD_ICNT)
694 NLA_PUT_U32(skb, XFRMA_SPDICNT, si.incnt);
695 if (flags & XFRM_SPD_OCNT)
696 NLA_PUT_U32(skb, XFRMA_SPDOCNT, si.outcnt);
697 if (flags & XFRM_SPD_FCNT)
698 NLA_PUT_U32(skb, XFRMA_SPDFCNT, si.fwdcnt);
699 if (flags & XFRM_SPD_ISCNT)
700 NLA_PUT_U32(skb, XFRMA_SPDISCNT, si.inscnt);
701 if (flags & XFRM_SPD_OSCNT)
702 NLA_PUT_U32(skb, XFRMA_SPDOSCNT, si.inscnt);
703 if (flags & XFRM_SPD_FSCNT)
704 NLA_PUT_U32(skb, XFRMA_SPDFSCNT, si.inscnt);
705
706 return nlmsg_end(skb, nlh);
707
708nla_put_failure:
709 nlmsg_cancel(skb, nlh);
710 return -EMSGSIZE;
711}
712
713static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
714 struct rtattr **xfrma)
715{
716 struct sk_buff *r_skb;
717 u32 *flags = NLMSG_DATA(nlh);
718 u32 spid = NETLINK_CB(skb).pid;
719 u32 seq = nlh->nlmsg_seq;
720 int len = NLMSG_LENGTH(sizeof(u32));
721
722
723 if (*flags & XFRM_SPD_HMASK)
724 len += RTA_SPACE(sizeof(u32));
725 if (*flags & XFRM_SPD_HMAX)
726 len += RTA_SPACE(sizeof(u32));
727 if (*flags & XFRM_SPD_ICNT)
728 len += RTA_SPACE(sizeof(u32));
729 if (*flags & XFRM_SPD_OCNT)
730 len += RTA_SPACE(sizeof(u32));
731 if (*flags & XFRM_SPD_FCNT)
732 len += RTA_SPACE(sizeof(u32));
733 if (*flags & XFRM_SPD_ISCNT)
734 len += RTA_SPACE(sizeof(u32));
735 if (*flags & XFRM_SPD_OSCNT)
736 len += RTA_SPACE(sizeof(u32));
737 if (*flags & XFRM_SPD_FSCNT)
738 len += RTA_SPACE(sizeof(u32));
739
740 r_skb = alloc_skb(len, GFP_ATOMIC);
741 if (r_skb == NULL)
742 return -ENOMEM;
743
744 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
745 BUG();
746
747 return nlmsg_unicast(xfrm_nl, r_skb, spid);
748}
749
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700750static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
751{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700752 struct xfrmk_sadinfo si;
753 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700754 struct nlmsghdr *nlh;
755 u32 *f;
756
757 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
758 if (nlh == NULL) /* shouldnt really happen ... */
759 return -EMSGSIZE;
760
761 f = nlmsg_data(nlh);
762 *f = flags;
763 xfrm_sad_getinfo(&si);
764
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700765 sh.sadhmcnt = si.sadhmcnt;
766 sh.sadhcnt = si.sadhcnt;
767
768 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
769 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700770
771 return nlmsg_end(skb, nlh);
772
773nla_put_failure:
774 nlmsg_cancel(skb, nlh);
775 return -EMSGSIZE;
776}
777
778static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
779 struct rtattr **xfrma)
780{
781 struct sk_buff *r_skb;
782 u32 *flags = NLMSG_DATA(nlh);
783 u32 spid = NETLINK_CB(skb).pid;
784 u32 seq = nlh->nlmsg_seq;
785 int len = NLMSG_LENGTH(sizeof(u32));
786
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700787 len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
788 len += RTA_SPACE(sizeof(u32));
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700789
790 r_skb = alloc_skb(len, GFP_ATOMIC);
791
792 if (r_skb == NULL)
793 return -ENOMEM;
794
795 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
796 BUG();
797
798 return nlmsg_unicast(xfrm_nl, r_skb, spid);
799}
800
Christoph Hellwig22e70052007-01-02 15:22:30 -0800801static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
802 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
805 struct xfrm_state *x;
806 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700807 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig22e70052007-01-02 15:22:30 -0800809 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (x == NULL)
811 goto out_noput;
812
813 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
814 if (IS_ERR(resp_skb)) {
815 err = PTR_ERR(resp_skb);
816 } else {
817 err = netlink_unicast(xfrm_nl, resp_skb,
818 NETLINK_CB(skb).pid, MSG_DONTWAIT);
819 }
820 xfrm_state_put(x);
821out_noput:
822 return err;
823}
824
825static int verify_userspi_info(struct xfrm_userspi_info *p)
826{
827 switch (p->info.id.proto) {
828 case IPPROTO_AH:
829 case IPPROTO_ESP:
830 break;
831
832 case IPPROTO_COMP:
833 /* IPCOMP spi is 16-bits. */
834 if (p->max >= 0x10000)
835 return -EINVAL;
836 break;
837
838 default:
839 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (p->min > p->max)
843 return -EINVAL;
844
845 return 0;
846}
847
Christoph Hellwig22e70052007-01-02 15:22:30 -0800848static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
849 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct xfrm_state *x;
852 struct xfrm_userspi_info *p;
853 struct sk_buff *resp_skb;
854 xfrm_address_t *daddr;
855 int family;
856 int err;
857
858 p = NLMSG_DATA(nlh);
859 err = verify_userspi_info(p);
860 if (err)
861 goto out_noput;
862
863 family = p->info.family;
864 daddr = &p->info.id.daddr;
865
866 x = NULL;
867 if (p->info.seq) {
868 x = xfrm_find_acq_byseq(p->info.seq);
869 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
870 xfrm_state_put(x);
871 x = NULL;
872 }
873 }
874
875 if (!x)
876 x = xfrm_find_acq(p->info.mode, p->info.reqid,
877 p->info.id.proto, daddr,
878 &p->info.saddr, 1,
879 family);
880 err = -ENOENT;
881 if (x == NULL)
882 goto out_noput;
883
884 resp_skb = ERR_PTR(-ENOENT);
885
886 spin_lock_bh(&x->lock);
887 if (x->km.state != XFRM_STATE_DEAD) {
888 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
889 if (x->id.spi)
890 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
891 }
892 spin_unlock_bh(&x->lock);
893
894 if (IS_ERR(resp_skb)) {
895 err = PTR_ERR(resp_skb);
896 goto out;
897 }
898
899 err = netlink_unicast(xfrm_nl, resp_skb,
900 NETLINK_CB(skb).pid, MSG_DONTWAIT);
901
902out:
903 xfrm_state_put(x);
904out_noput:
905 return err;
906}
907
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800908static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 switch (dir) {
911 case XFRM_POLICY_IN:
912 case XFRM_POLICY_OUT:
913 case XFRM_POLICY_FWD:
914 break;
915
916 default:
917 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return 0;
921}
922
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800923static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700924{
925 switch (type) {
926 case XFRM_POLICY_TYPE_MAIN:
927#ifdef CONFIG_XFRM_SUB_POLICY
928 case XFRM_POLICY_TYPE_SUB:
929#endif
930 break;
931
932 default:
933 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700934 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700935
936 return 0;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
940{
941 switch (p->share) {
942 case XFRM_SHARE_ANY:
943 case XFRM_SHARE_SESSION:
944 case XFRM_SHARE_USER:
945 case XFRM_SHARE_UNIQUE:
946 break;
947
948 default:
949 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 switch (p->action) {
953 case XFRM_POLICY_ALLOW:
954 case XFRM_POLICY_BLOCK:
955 break;
956
957 default:
958 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 switch (p->sel.family) {
962 case AF_INET:
963 break;
964
965 case AF_INET6:
966#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
967 break;
968#else
969 return -EAFNOSUPPORT;
970#endif
971
972 default:
973 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 return verify_policy_dir(p->dir);
977}
978
Trent Jaegerdf718372005-12-13 23:12:27 -0800979static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
980{
981 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
982 struct xfrm_user_sec_ctx *uctx;
983
984 if (!rt)
985 return 0;
986
987 uctx = RTA_DATA(rt);
988 return security_xfrm_policy_alloc(pol, uctx);
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
992 int nr)
993{
994 int i;
995
996 xp->xfrm_nr = nr;
997 for (i = 0; i < nr; i++, ut++) {
998 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
999
1000 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1001 memcpy(&t->saddr, &ut->saddr,
1002 sizeof(xfrm_address_t));
1003 t->reqid = ut->reqid;
1004 t->mode = ut->mode;
1005 t->share = ut->share;
1006 t->optional = ut->optional;
1007 t->aalgos = ut->aalgos;
1008 t->ealgos = ut->ealgos;
1009 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -08001010 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012}
1013
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001014static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1015{
1016 int i;
1017
1018 if (nr > XFRM_MAX_DEPTH)
1019 return -EINVAL;
1020
1021 for (i = 0; i < nr; i++) {
1022 /* We never validated the ut->family value, so many
1023 * applications simply leave it at zero. The check was
1024 * never made and ut->family was ignored because all
1025 * templates could be assumed to have the same family as
1026 * the policy itself. Now that we will have ipv4-in-ipv6
1027 * and ipv6-in-ipv4 tunnels, this is no longer true.
1028 */
1029 if (!ut[i].family)
1030 ut[i].family = family;
1031
1032 switch (ut[i].family) {
1033 case AF_INET:
1034 break;
1035#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1036 case AF_INET6:
1037 break;
1038#endif
1039 default:
1040 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001041 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001042 }
1043
1044 return 0;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
1048{
1049 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!rt) {
1052 pol->xfrm_nr = 0;
1053 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001054 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
1055 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
1056 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001058 err = validate_tmpl(nr, utmpl, pol->family);
1059 if (err)
1060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 copy_templates(pol, RTA_DATA(rt), nr);
1063 }
1064 return 0;
1065}
1066
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001067static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
1068{
1069 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
1070 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001071 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001072 int err;
1073
1074 if (rt) {
1075 if (rt->rta_len < sizeof(*upt))
1076 return -EINVAL;
1077
1078 upt = RTA_DATA(rt);
1079 type = upt->type;
1080 }
1081
1082 err = verify_policy_type(type);
1083 if (err)
1084 return err;
1085
1086 *tp = type;
1087 return 0;
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1091{
1092 xp->priority = p->priority;
1093 xp->index = p->index;
1094 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1095 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1096 xp->action = p->action;
1097 xp->flags = p->flags;
1098 xp->family = p->sel.family;
1099 /* XXX xp->share = p->share; */
1100}
1101
1102static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1103{
1104 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1105 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1106 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1107 p->priority = xp->priority;
1108 p->index = xp->index;
1109 p->sel.family = xp->family;
1110 p->dir = dir;
1111 p->action = xp->action;
1112 p->flags = xp->flags;
1113 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1114}
1115
1116static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
1117{
1118 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1119 int err;
1120
1121 if (!xp) {
1122 *errp = -ENOMEM;
1123 return NULL;
1124 }
1125
1126 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001127
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001128 err = copy_from_user_policy_type(&xp->type, xfrma);
1129 if (err)
1130 goto error;
1131
Trent Jaegerdf718372005-12-13 23:12:27 -08001132 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1133 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134 if (err)
1135 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001138 error:
1139 *errp = err;
1140 kfree(xp);
1141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Christoph Hellwig22e70052007-01-02 15:22:30 -08001144static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1145 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1148 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001149 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int err;
1151 int excl;
1152
1153 err = verify_newpolicy_info(p);
1154 if (err)
1155 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001156 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001157 if (err)
1158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Christoph Hellwig22e70052007-01-02 15:22:30 -08001160 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!xp)
1162 return err;
1163
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001164 /* shouldnt excl be based on nlh flags??
1165 * Aha! this is anti-netlink really i.e more pfkey derived
1166 * in netlink excl is a flag and you wouldnt need
1167 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1169 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001170 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1171 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001174 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 kfree(xp);
1176 return err;
1177 }
1178
Herbert Xuf60f6b82005-06-18 22:44:37 -07001179 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001180 c.seq = nlh->nlmsg_seq;
1181 c.pid = nlh->nlmsg_pid;
1182 km_policy_notify(xp, p->dir, &c);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 xfrm_pol_put(xp);
1185
1186 return 0;
1187}
1188
1189static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1190{
1191 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1192 int i;
1193
1194 if (xp->xfrm_nr == 0)
1195 return 0;
1196
1197 for (i = 0; i < xp->xfrm_nr; i++) {
1198 struct xfrm_user_tmpl *up = &vec[i];
1199 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1200
1201 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001202 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1204 up->reqid = kp->reqid;
1205 up->mode = kp->mode;
1206 up->share = kp->share;
1207 up->optional = kp->optional;
1208 up->aalgos = kp->aalgos;
1209 up->ealgos = kp->ealgos;
1210 up->calgos = kp->calgos;
1211 }
1212 RTA_PUT(skb, XFRMA_TMPL,
1213 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1214 vec);
1215
1216 return 0;
1217
1218rtattr_failure:
1219 return -1;
1220}
1221
Serge Hallyn0d681622006-07-24 23:30:44 -07001222static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001223{
Serge Hallyn0d681622006-07-24 23:30:44 -07001224 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1225 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1226 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001227
Serge Hallyn0d681622006-07-24 23:30:44 -07001228 uctx->exttype = XFRMA_SEC_CTX;
1229 uctx->len = ctx_size;
1230 uctx->ctx_doi = s->ctx_doi;
1231 uctx->ctx_alg = s->ctx_alg;
1232 uctx->ctx_len = s->ctx_len;
1233 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001234 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001235
1236 rtattr_failure:
1237 return -1;
1238}
1239
Serge Hallyn0d681622006-07-24 23:30:44 -07001240static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1241{
1242 if (x->security) {
1243 return copy_sec_ctx(x->security, skb);
1244 }
1245 return 0;
1246}
1247
1248static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1249{
1250 if (xp->security) {
1251 return copy_sec_ctx(xp->security, skb);
1252 }
1253 return 0;
1254}
1255
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001256#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001257static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001258{
1259 struct xfrm_userpolicy_type upt;
1260
1261 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001262 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001263
1264 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1265
1266 return 0;
1267
1268rtattr_failure:
1269 return -1;
1270}
1271
1272#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001273static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001274{
1275 return 0;
1276}
1277#endif
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1280{
1281 struct xfrm_dump_info *sp = ptr;
1282 struct xfrm_userpolicy_info *p;
1283 struct sk_buff *in_skb = sp->in_skb;
1284 struct sk_buff *skb = sp->out_skb;
1285 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001286 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (sp->this_idx < sp->start_idx)
1289 goto out;
1290
1291 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1292 sp->nlmsg_seq,
1293 XFRM_MSG_NEWPOLICY, sizeof(*p));
1294 p = NLMSG_DATA(nlh);
1295 nlh->nlmsg_flags = sp->nlmsg_flags;
1296
1297 copy_to_user_policy(xp, p, dir);
1298 if (copy_to_user_tmpl(xp, skb) < 0)
1299 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001300 if (copy_to_user_sec_ctx(xp, skb))
1301 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001302 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001303 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001305 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306out:
1307 sp->this_idx++;
1308 return 0;
1309
1310nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001311 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return -1;
1313}
1314
1315static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1316{
1317 struct xfrm_dump_info info;
1318
1319 info.in_skb = cb->skb;
1320 info.out_skb = skb;
1321 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1322 info.nlmsg_flags = NLM_F_MULTI;
1323 info.this_idx = 0;
1324 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001325 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1326#ifdef CONFIG_XFRM_SUB_POLICY
1327 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 cb->args[0] = info.this_idx;
1330
1331 return skb->len;
1332}
1333
1334static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1335 struct xfrm_policy *xp,
1336 int dir, u32 seq)
1337{
1338 struct xfrm_dump_info info;
1339 struct sk_buff *skb;
1340
1341 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1342 if (!skb)
1343 return ERR_PTR(-ENOMEM);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 info.in_skb = in_skb;
1346 info.out_skb = skb;
1347 info.nlmsg_seq = seq;
1348 info.nlmsg_flags = 0;
1349 info.this_idx = info.start_idx = 0;
1350
1351 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1352 kfree_skb(skb);
1353 return NULL;
1354 }
1355
1356 return skb;
1357}
1358
Christoph Hellwig22e70052007-01-02 15:22:30 -08001359static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1360 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct xfrm_policy *xp;
1363 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001364 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001366 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int delete;
1368
1369 p = NLMSG_DATA(nlh);
1370 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1371
Christoph Hellwig22e70052007-01-02 15:22:30 -08001372 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001373 if (err)
1374 return err;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 err = verify_policy_dir(p->dir);
1377 if (err)
1378 return err;
1379
1380 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001381 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001382 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001383 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001384 struct xfrm_policy tmp;
1385
Christoph Hellwig22e70052007-01-02 15:22:30 -08001386 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001387 if (err)
1388 return err;
1389
1390 memset(&tmp, 0, sizeof(struct xfrm_policy));
1391 if (rt) {
1392 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1393
1394 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1395 return err;
1396 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001397 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1398 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001399 security_xfrm_policy_free(&tmp);
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (xp == NULL)
1402 return -ENOENT;
1403
1404 if (!delete) {
1405 struct sk_buff *resp_skb;
1406
1407 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1408 if (IS_ERR(resp_skb)) {
1409 err = PTR_ERR(resp_skb);
1410 } else {
1411 err = netlink_unicast(xfrm_nl, resp_skb,
1412 NETLINK_CB(skb).pid,
1413 MSG_DONTWAIT);
1414 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001415 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001416 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1417 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1418
1419 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001420 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001421
Herbert Xue7443892005-06-18 22:44:18 -07001422 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001423 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 c.seq = nlh->nlmsg_seq;
1425 c.pid = nlh->nlmsg_pid;
1426 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001429out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001430 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return err;
1432}
1433
Christoph Hellwig22e70052007-01-02 15:22:30 -08001434static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1435 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001439 struct xfrm_audit audit_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Joy Latten161a09e2006-11-27 13:11:54 -06001441 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1442 audit_info.secid = NETLINK_CB(skb).sid;
1443 xfrm_state_flush(p->proto, &audit_info);
Herbert Xubf088672005-06-18 22:44:00 -07001444 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 c.seq = nlh->nlmsg_seq;
1447 c.pid = nlh->nlmsg_pid;
1448 km_state_notify(NULL, &c);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
1452
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001453
1454static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1455{
1456 struct xfrm_aevent_id *id;
1457 struct nlmsghdr *nlh;
1458 struct xfrm_lifetime_cur ltime;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001459 unsigned char *b = skb_tail_pointer(skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001460
1461 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1462 id = NLMSG_DATA(nlh);
1463 nlh->nlmsg_flags = 0;
1464
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001465 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001466 id->sa_id.spi = x->id.spi;
1467 id->sa_id.family = x->props.family;
1468 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001469 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1470 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001471 id->flags = c->data.aevent;
1472
1473 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1474
1475 ltime.bytes = x->curlft.bytes;
1476 ltime.packets = x->curlft.packets;
1477 ltime.add_time = x->curlft.add_time;
1478 ltime.use_time = x->curlft.use_time;
1479
1480 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1481
1482 if (id->flags&XFRM_AE_RTHR) {
1483 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1484 }
1485
1486 if (id->flags&XFRM_AE_ETHR) {
1487 u32 etimer = x->replay_maxage*10/HZ;
1488 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1489 }
1490
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001491 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001492 return skb->len;
1493
1494rtattr_failure:
1495nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001496 nlmsg_trim(skb, b);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001497 return -1;
1498}
1499
Christoph Hellwig22e70052007-01-02 15:22:30 -08001500static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1501 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001502{
1503 struct xfrm_state *x;
1504 struct sk_buff *r_skb;
1505 int err;
1506 struct km_event c;
1507 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1508 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1509 struct xfrm_usersa_id *id = &p->sa_id;
1510
1511 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1512 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1513
1514 if (p->flags&XFRM_AE_RTHR)
1515 len+=RTA_SPACE(sizeof(u32));
1516
1517 if (p->flags&XFRM_AE_ETHR)
1518 len+=RTA_SPACE(sizeof(u32));
1519
1520 r_skb = alloc_skb(len, GFP_ATOMIC);
1521 if (r_skb == NULL)
1522 return -ENOMEM;
1523
1524 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1525 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001526 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001527 return -ESRCH;
1528 }
1529
1530 /*
1531 * XXX: is this lock really needed - none of the other
1532 * gets lock (the concern is things getting updated
1533 * while we are still reading) - jhs
1534 */
1535 spin_lock_bh(&x->lock);
1536 c.data.aevent = p->flags;
1537 c.seq = nlh->nlmsg_seq;
1538 c.pid = nlh->nlmsg_pid;
1539
1540 if (build_aevent(r_skb, x, &c) < 0)
1541 BUG();
1542 err = netlink_unicast(xfrm_nl, r_skb,
1543 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1544 spin_unlock_bh(&x->lock);
1545 xfrm_state_put(x);
1546 return err;
1547}
1548
Christoph Hellwig22e70052007-01-02 15:22:30 -08001549static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1550 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001551{
1552 struct xfrm_state *x;
1553 struct km_event c;
1554 int err = - EINVAL;
1555 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1556 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1557 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1558
1559 if (!lt && !rp)
1560 return err;
1561
1562 /* pedantic mode - thou shalt sayeth replaceth */
1563 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1564 return err;
1565
1566 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1567 if (x == NULL)
1568 return -ESRCH;
1569
1570 if (x->km.state != XFRM_STATE_VALID)
1571 goto out;
1572
1573 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001574 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001575 spin_unlock_bh(&x->lock);
1576 if (err < 0)
1577 goto out;
1578
1579 c.event = nlh->nlmsg_type;
1580 c.seq = nlh->nlmsg_seq;
1581 c.pid = nlh->nlmsg_pid;
1582 c.data.aevent = XFRM_AE_CU;
1583 km_state_notify(x, &c);
1584 err = 0;
1585out:
1586 xfrm_state_put(x);
1587 return err;
1588}
1589
Christoph Hellwig22e70052007-01-02 15:22:30 -08001590static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1591 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001593 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001594 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001595 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001596 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001597
Christoph Hellwig22e70052007-01-02 15:22:30 -08001598 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001599 if (err)
1600 return err;
1601
Joy Latten161a09e2006-11-27 13:11:54 -06001602 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1603 audit_info.secid = NETLINK_CB(skb).sid;
1604 xfrm_policy_flush(type, &audit_info);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001605 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001606 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001607 c.seq = nlh->nlmsg_seq;
1608 c.pid = nlh->nlmsg_pid;
1609 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return 0;
1611}
1612
Christoph Hellwig22e70052007-01-02 15:22:30 -08001613static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1614 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001615{
1616 struct xfrm_policy *xp;
1617 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1618 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001619 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001620 int err = -ENOENT;
1621
Christoph Hellwig22e70052007-01-02 15:22:30 -08001622 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001623 if (err)
1624 return err;
1625
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001626 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001627 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001628 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001629 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001630 struct xfrm_policy tmp;
1631
Christoph Hellwig22e70052007-01-02 15:22:30 -08001632 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001633 if (err)
1634 return err;
1635
1636 memset(&tmp, 0, sizeof(struct xfrm_policy));
1637 if (rt) {
1638 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1639
1640 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1641 return err;
1642 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001643 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1644 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001645 security_xfrm_policy_free(&tmp);
1646 }
1647
1648 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001649 return -ENOENT;
1650 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001651 if (xp->dead) {
1652 read_unlock(&xp->lock);
1653 goto out;
1654 }
1655
1656 read_unlock(&xp->lock);
1657 err = 0;
1658 if (up->hard) {
1659 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001660 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1661 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1662
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001663 } else {
1664 // reset the timers here?
1665 printk("Dont know what to do with soft policy expire\n");
1666 }
1667 km_policy_expired(xp, p->dir, up->hard, current->pid);
1668
1669out:
1670 xfrm_pol_put(xp);
1671 return err;
1672}
1673
Christoph Hellwig22e70052007-01-02 15:22:30 -08001674static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1675 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001676{
1677 struct xfrm_state *x;
1678 int err;
1679 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1680 struct xfrm_usersa_info *p = &ue->state;
1681
1682 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001683
David S. Miller3a765aa2007-02-26 14:52:21 -08001684 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001685 if (x == NULL)
1686 return err;
1687
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001688 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001689 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001690 if (x->km.state != XFRM_STATE_VALID)
1691 goto out;
1692 km_state_expired(x, ue->hard, current->pid);
1693
Joy Latten161a09e2006-11-27 13:11:54 -06001694 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001695 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001696 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1697 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1698 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001699 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001700out:
1701 spin_unlock_bh(&x->lock);
1702 xfrm_state_put(x);
1703 return err;
1704}
1705
Christoph Hellwig22e70052007-01-02 15:22:30 -08001706static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1707 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001708{
1709 struct xfrm_policy *xp;
1710 struct xfrm_user_tmpl *ut;
1711 int i;
1712 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1713
1714 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1715 struct xfrm_state *x = xfrm_state_alloc();
1716 int err = -ENOMEM;
1717
1718 if (!x)
1719 return err;
1720
1721 err = verify_newpolicy_info(&ua->policy);
1722 if (err) {
1723 printk("BAD policy passed\n");
1724 kfree(x);
1725 return err;
1726 }
1727
1728 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001729 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1730 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001731 kfree(x);
1732 return err;
1733 }
1734
1735 memcpy(&x->id, &ua->id, sizeof(ua->id));
1736 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1737 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1738
1739 ut = RTA_DATA(rt);
1740 /* extract the templates and for each call km_key */
1741 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1742 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1743 memcpy(&x->id, &t->id, sizeof(x->id));
1744 x->props.mode = t->mode;
1745 x->props.reqid = t->reqid;
1746 x->props.family = ut->family;
1747 t->aalgos = ua->aalgos;
1748 t->ealgos = ua->ealgos;
1749 t->calgos = ua->calgos;
1750 err = km_query(x, t, xp);
1751
1752 }
1753
1754 kfree(x);
1755 kfree(xp);
1756
1757 return 0;
1758}
1759
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001760#ifdef CONFIG_XFRM_MIGRATE
1761static int verify_user_migrate(struct rtattr **xfrma)
1762{
1763 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1764 struct xfrm_user_migrate *um;
1765
1766 if (!rt)
1767 return -EINVAL;
1768
1769 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1770 return -EINVAL;
1771
1772 return 0;
1773}
1774
1775static int copy_from_user_migrate(struct xfrm_migrate *ma,
1776 struct rtattr **xfrma, int *num)
1777{
1778 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1779 struct xfrm_user_migrate *um;
1780 int i, num_migrate;
1781
1782 um = RTA_DATA(rt);
1783 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1784
1785 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1786 return -EINVAL;
1787
1788 for (i = 0; i < num_migrate; i++, um++, ma++) {
1789 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1790 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1791 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1792 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1793
1794 ma->proto = um->proto;
1795 ma->mode = um->mode;
1796 ma->reqid = um->reqid;
1797
1798 ma->old_family = um->old_family;
1799 ma->new_family = um->new_family;
1800 }
1801
1802 *num = i;
1803 return 0;
1804}
1805
1806static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1807 struct rtattr **xfrma)
1808{
1809 struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
1810 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1811 u8 type;
1812 int err;
1813 int n = 0;
1814
1815 err = verify_user_migrate((struct rtattr **)xfrma);
1816 if (err)
1817 return err;
1818
1819 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1820 if (err)
1821 return err;
1822
1823 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1824 (struct rtattr **)xfrma, &n);
1825 if (err)
1826 return err;
1827
1828 if (!n)
1829 return 0;
1830
1831 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1832
1833 return 0;
1834}
1835#else
1836static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1837 struct rtattr **xfrma)
1838{
1839 return -ENOPROTOOPT;
1840}
1841#endif
1842
1843#ifdef CONFIG_XFRM_MIGRATE
1844static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1845{
1846 struct xfrm_user_migrate um;
1847
1848 memset(&um, 0, sizeof(um));
1849 um.proto = m->proto;
1850 um.mode = m->mode;
1851 um.reqid = m->reqid;
1852 um.old_family = m->old_family;
1853 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1854 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1855 um.new_family = m->new_family;
1856 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1857 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1858
1859 RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1860 return 0;
1861
1862rtattr_failure:
1863 return -1;
1864}
1865
1866static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1867 int num_migrate, struct xfrm_selector *sel,
1868 u8 dir, u8 type)
1869{
1870 struct xfrm_migrate *mp;
1871 struct xfrm_userpolicy_id *pol_id;
1872 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001873 unsigned char *b = skb_tail_pointer(skb);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001874 int i;
1875
1876 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
1877 pol_id = NLMSG_DATA(nlh);
1878 nlh->nlmsg_flags = 0;
1879
1880 /* copy data from selector, dir, and type to the pol_id */
1881 memset(pol_id, 0, sizeof(*pol_id));
1882 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1883 pol_id->dir = dir;
1884
1885 if (copy_to_user_policy_type(type, skb) < 0)
1886 goto nlmsg_failure;
1887
1888 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1889 if (copy_to_user_migrate(mp, skb) < 0)
1890 goto nlmsg_failure;
1891 }
1892
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001893 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001894 return skb->len;
1895nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001896 nlmsg_trim(skb, b);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001897 return -1;
1898}
1899
1900static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1901 struct xfrm_migrate *m, int num_migrate)
1902{
1903 struct sk_buff *skb;
1904 size_t len;
1905
1906 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1907 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1908#ifdef CONFIG_XFRM_SUB_POLICY
1909 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1910#endif
1911 skb = alloc_skb(len, GFP_ATOMIC);
1912 if (skb == NULL)
1913 return -ENOMEM;
1914
1915 /* build migrate */
1916 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1917 BUG();
1918
1919 NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
1920 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
1921 GFP_ATOMIC);
1922}
1923#else
1924static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1925 struct xfrm_migrate *m, int num_migrate)
1926{
1927 return -ENOPROTOOPT;
1928}
1929#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001930
Thomas Graf492b5582005-05-03 14:26:40 -07001931#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1932
1933static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1934 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1935 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1936 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1937 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1938 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1939 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1940 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001941 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001942 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001943 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1944 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001945 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001946 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1947 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001948 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1949 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001950 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001951 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001952 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001953 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954};
1955
Thomas Graf492b5582005-05-03 14:26:40 -07001956#undef XMSGSIZE
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001959 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001961} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1962 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1963 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1964 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1965 .dump = xfrm_dump_sa },
1966 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1967 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1968 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1969 .dump = xfrm_dump_policy },
1970 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001971 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001972 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001973 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1974 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001975 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001976 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1977 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001978 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1979 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001980 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001981 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001982 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983};
1984
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001985static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 struct rtattr *xfrma[XFRMA_MAX];
1988 struct xfrm_link *link;
Thomas Grafc702e802007-03-22 23:30:55 -07001989 int type, min_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001993 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 type -= XFRM_MSG_BASE;
1996 link = &xfrm_dispatch[type];
1997
1998 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001999 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2000 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Thomas Graf492b5582005-05-03 14:26:40 -07002002 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2003 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2004 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Thomas Grafc702e802007-03-22 23:30:55 -07002008 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
2011 memset(xfrma, 0, sizeof(xfrma));
2012
2013 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (nlh->nlmsg_len > min_len) {
2017 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2018 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
2019
2020 while (RTA_OK(attr, attrlen)) {
2021 unsigned short flavor = attr->rta_type;
2022 if (flavor) {
2023 if (flavor > XFRMA_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 xfrma[flavor - 1] = attr;
2026 }
2027 attr = RTA_NEXT(attr, attrlen);
2028 }
2029 }
2030
2031 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002034 return link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037static void xfrm_netlink_rcv(struct sock *sk, int len)
2038{
Thomas Graf88fc2c82005-11-10 02:25:54 +01002039 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002042 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01002043 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08002044 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07002046 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002049static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct xfrm_user_expire *ue;
2052 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002053 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002055 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 sizeof(*ue));
2057 ue = NLMSG_DATA(nlh);
2058 nlh->nlmsg_flags = 0;
2059
2060 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002061 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002063 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return skb->len;
2065
2066nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002067 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -1;
2069}
2070
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002071static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002074 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07002076 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (skb == NULL)
2078 return -ENOMEM;
2079
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002080 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 BUG();
2082
Patrick McHardyac6d4392005-08-14 19:29:52 -07002083 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2084 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002087static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2088{
2089 struct sk_buff *skb;
2090 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
2091
2092 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
2093 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
2094 skb = alloc_skb(len, GFP_ATOMIC);
2095 if (skb == NULL)
2096 return -ENOMEM;
2097
2098 if (build_aevent(skb, x, c) < 0)
2099 BUG();
2100
2101 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
2102 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2103}
2104
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002105static int xfrm_notify_sa_flush(struct km_event *c)
2106{
2107 struct xfrm_usersa_flush *p;
2108 struct nlmsghdr *nlh;
2109 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002110 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002111 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2112
2113 skb = alloc_skb(len, GFP_ATOMIC);
2114 if (skb == NULL)
2115 return -ENOMEM;
2116 b = skb->tail;
2117
2118 nlh = NLMSG_PUT(skb, c->pid, c->seq,
2119 XFRM_MSG_FLUSHSA, sizeof(*p));
2120 nlh->nlmsg_flags = 0;
2121
2122 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002123 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124
2125 nlh->nlmsg_len = skb->tail - b;
2126
Patrick McHardyac6d4392005-08-14 19:29:52 -07002127 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2128 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002129
2130nlmsg_failure:
2131 kfree_skb(skb);
2132 return -1;
2133}
2134
Dave Jonesb6f99a22007-03-22 12:27:49 -07002135static inline int xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002136{
Herbert Xu0603eac2005-06-18 22:54:36 -07002137 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002138 if (x->aalg)
2139 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
2140 if (x->ealg)
2141 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
2142 if (x->calg)
2143 l += RTA_SPACE(sizeof(*x->calg));
2144 if (x->encap)
2145 l += RTA_SPACE(sizeof(*x->encap));
2146
2147 return l;
2148}
2149
2150static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2151{
2152 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002153 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154 struct nlmsghdr *nlh;
2155 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002156 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002157 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002158 int headlen;
2159
2160 headlen = sizeof(*p);
2161 if (c->event == XFRM_MSG_DELSA) {
2162 len += RTA_SPACE(headlen);
2163 headlen = sizeof(*id);
2164 }
2165 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002166
2167 skb = alloc_skb(len, GFP_ATOMIC);
2168 if (skb == NULL)
2169 return -ENOMEM;
2170 b = skb->tail;
2171
Herbert Xu0603eac2005-06-18 22:54:36 -07002172 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002173 nlh->nlmsg_flags = 0;
2174
2175 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002176 if (c->event == XFRM_MSG_DELSA) {
2177 id = NLMSG_DATA(nlh);
2178 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2179 id->spi = x->id.spi;
2180 id->family = x->props.family;
2181 id->proto = x->id.proto;
2182
2183 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2184 }
2185
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002186 copy_to_user_state(x, p);
2187
2188 if (x->aalg)
2189 RTA_PUT(skb, XFRMA_ALG_AUTH,
2190 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
2191 if (x->ealg)
2192 RTA_PUT(skb, XFRMA_ALG_CRYPT,
2193 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
2194 if (x->calg)
2195 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2196
2197 if (x->encap)
2198 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2199
2200 nlh->nlmsg_len = skb->tail - b;
2201
Patrick McHardyac6d4392005-08-14 19:29:52 -07002202 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2203 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002204
2205nlmsg_failure:
2206rtattr_failure:
2207 kfree_skb(skb);
2208 return -1;
2209}
2210
2211static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2212{
2213
2214 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002215 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002216 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002217 case XFRM_MSG_NEWAE:
2218 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002219 case XFRM_MSG_DELSA:
2220 case XFRM_MSG_UPDSA:
2221 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002222 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002223 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002224 return xfrm_notify_sa_flush(c);
2225 default:
2226 printk("xfrm_user: Unknown SA event %d\n", c->event);
2227 break;
2228 }
2229
2230 return 0;
2231
2232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2235 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2236 int dir)
2237{
2238 struct xfrm_user_acquire *ua;
2239 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002240 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 __u32 seq = xfrm_get_acqseq();
2242
2243 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
2244 sizeof(*ua));
2245 ua = NLMSG_DATA(nlh);
2246 nlh->nlmsg_flags = 0;
2247
2248 memcpy(&ua->id, &x->id, sizeof(ua->id));
2249 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2250 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2251 copy_to_user_policy(xp, &ua->policy, dir);
2252 ua->aalgos = xt->aalgos;
2253 ua->ealgos = xt->ealgos;
2254 ua->calgos = xt->calgos;
2255 ua->seq = x->km.seq = seq;
2256
2257 if (copy_to_user_tmpl(xp, skb) < 0)
2258 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002259 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002260 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002261 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002262 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002264 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return skb->len;
2266
2267nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002268 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -1;
2270}
2271
2272static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2273 struct xfrm_policy *xp, int dir)
2274{
2275 struct sk_buff *skb;
2276 size_t len;
2277
2278 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2279 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Joy Latten661697f2007-04-13 16:14:35 -07002280 len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002281#ifdef CONFIG_XFRM_SUB_POLICY
2282 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 skb = alloc_skb(len, GFP_ATOMIC);
2285 if (skb == NULL)
2286 return -ENOMEM;
2287
2288 if (build_acquire(skb, x, xt, xp, dir) < 0)
2289 BUG();
2290
Patrick McHardyac6d4392005-08-14 19:29:52 -07002291 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2292 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
2295/* User gives us xfrm_user_policy_info followed by an array of 0
2296 * or more templates.
2297 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002298static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 u8 *data, int len, int *dir)
2300{
2301 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2302 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2303 struct xfrm_policy *xp;
2304 int nr;
2305
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002306 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 case AF_INET:
2308 if (opt != IP_XFRM_POLICY) {
2309 *dir = -EOPNOTSUPP;
2310 return NULL;
2311 }
2312 break;
2313#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2314 case AF_INET6:
2315 if (opt != IPV6_XFRM_POLICY) {
2316 *dir = -EOPNOTSUPP;
2317 return NULL;
2318 }
2319 break;
2320#endif
2321 default:
2322 *dir = -EINVAL;
2323 return NULL;
2324 }
2325
2326 *dir = -EINVAL;
2327
2328 if (len < sizeof(*p) ||
2329 verify_newpolicy_info(p))
2330 return NULL;
2331
2332 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002333 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return NULL;
2335
Herbert Xua4f1bac2005-07-26 15:43:17 -07002336 if (p->dir > XFRM_POLICY_OUT)
2337 return NULL;
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 xp = xfrm_policy_alloc(GFP_KERNEL);
2340 if (xp == NULL) {
2341 *dir = -ENOBUFS;
2342 return NULL;
2343 }
2344
2345 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002346 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 copy_templates(xp, ut, nr);
2348
2349 *dir = p->dir;
2350
2351 return xp;
2352}
2353
2354static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002355 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct xfrm_user_polexpire *upe;
2358 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002359 int hard = c->data.hard;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002360 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002362 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 upe = NLMSG_DATA(nlh);
2364 nlh->nlmsg_flags = 0;
2365
2366 copy_to_user_policy(xp, &upe->pol, dir);
2367 if (copy_to_user_tmpl(xp, skb) < 0)
2368 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002369 if (copy_to_user_sec_ctx(xp, skb))
2370 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002371 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002372 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 upe->hard = !!hard;
2374
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002375 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return skb->len;
2377
2378nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002379 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return -1;
2381}
2382
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002383static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 struct sk_buff *skb;
2386 size_t len;
2387
2388 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2389 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Joy Latten661697f2007-04-13 16:14:35 -07002390 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002391#ifdef CONFIG_XFRM_SUB_POLICY
2392 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 skb = alloc_skb(len, GFP_ATOMIC);
2395 if (skb == NULL)
2396 return -ENOMEM;
2397
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002398 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 BUG();
2400
Patrick McHardyac6d4392005-08-14 19:29:52 -07002401 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2402 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2406{
2407 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002408 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002409 struct nlmsghdr *nlh;
2410 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002411 sk_buff_data_t b;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002412 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002413 int headlen;
2414
2415 headlen = sizeof(*p);
2416 if (c->event == XFRM_MSG_DELPOLICY) {
2417 len += RTA_SPACE(headlen);
2418 headlen = sizeof(*id);
2419 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002420#ifdef CONFIG_XFRM_SUB_POLICY
2421 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2422#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002423 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002424
2425 skb = alloc_skb(len, GFP_ATOMIC);
2426 if (skb == NULL)
2427 return -ENOMEM;
2428 b = skb->tail;
2429
Herbert Xu0603eac2005-06-18 22:54:36 -07002430 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431
2432 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002433 if (c->event == XFRM_MSG_DELPOLICY) {
2434 id = NLMSG_DATA(nlh);
2435 memset(id, 0, sizeof(*id));
2436 id->dir = dir;
2437 if (c->data.byid)
2438 id->index = xp->index;
2439 else
2440 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2441
2442 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2443 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002444
2445 nlh->nlmsg_flags = 0;
2446
2447 copy_to_user_policy(xp, p, dir);
2448 if (copy_to_user_tmpl(xp, skb) < 0)
2449 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002450 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002451 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452
2453 nlh->nlmsg_len = skb->tail - b;
2454
Patrick McHardyac6d4392005-08-14 19:29:52 -07002455 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2456 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457
2458nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002459rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460 kfree_skb(skb);
2461 return -1;
2462}
2463
2464static int xfrm_notify_policy_flush(struct km_event *c)
2465{
2466 struct nlmsghdr *nlh;
2467 struct sk_buff *skb;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002468 sk_buff_data_t b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002469 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002470#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002471 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002472#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002473 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474
2475 skb = alloc_skb(len, GFP_ATOMIC);
2476 if (skb == NULL)
2477 return -ENOMEM;
2478 b = skb->tail;
2479
2480
2481 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002482 nlh->nlmsg_flags = 0;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002483 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2484 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002485
2486 nlh->nlmsg_len = skb->tail - b;
2487
Patrick McHardyac6d4392005-08-14 19:29:52 -07002488 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2489 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002490
2491nlmsg_failure:
2492 kfree_skb(skb);
2493 return -1;
2494}
2495
2496static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2497{
2498
2499 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002500 case XFRM_MSG_NEWPOLICY:
2501 case XFRM_MSG_UPDPOLICY:
2502 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002503 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002504 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002506 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507 return xfrm_exp_policy_notify(xp, dir, c);
2508 default:
2509 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2510 }
2511
2512 return 0;
2513
2514}
2515
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002516static int build_report(struct sk_buff *skb, u8 proto,
2517 struct xfrm_selector *sel, xfrm_address_t *addr)
2518{
2519 struct xfrm_user_report *ur;
2520 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002521 unsigned char *b = skb_tail_pointer(skb);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002522
2523 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2524 ur = NLMSG_DATA(nlh);
2525 nlh->nlmsg_flags = 0;
2526
2527 ur->proto = proto;
2528 memcpy(&ur->sel, sel, sizeof(ur->sel));
2529
2530 if (addr)
2531 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2532
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002533 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002534 return skb->len;
2535
2536nlmsg_failure:
2537rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002538 nlmsg_trim(skb, b);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002539 return -1;
2540}
2541
2542static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2543 xfrm_address_t *addr)
2544{
2545 struct sk_buff *skb;
2546 size_t len;
2547
2548 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2549 skb = alloc_skb(len, GFP_ATOMIC);
2550 if (skb == NULL)
2551 return -ENOMEM;
2552
2553 if (build_report(skb, proto, sel, addr) < 0)
2554 BUG();
2555
2556 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2557 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2558}
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560static struct xfrm_mgr netlink_mgr = {
2561 .id = "netlink",
2562 .notify = xfrm_send_state_notify,
2563 .acquire = xfrm_send_acquire,
2564 .compile_policy = xfrm_compile_policy,
2565 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002566 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002567 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568};
2569
2570static int __init xfrm_user_init(void)
2571{
Patrick McHardybe336902006-03-20 22:40:54 -08002572 struct sock *nlsk;
2573
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002574 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Patrick McHardybe336902006-03-20 22:40:54 -08002576 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002577 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002578 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002580 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 xfrm_register_km(&netlink_mgr);
2583
2584 return 0;
2585}
2586
2587static void __exit xfrm_user_exit(void)
2588{
Patrick McHardybe336902006-03-20 22:40:54 -08002589 struct sock *nlsk = xfrm_nl;
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002592 rcu_assign_pointer(xfrm_nl, NULL);
2593 synchronize_rcu();
2594 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597module_init(xfrm_user_init);
2598module_exit(xfrm_user_exit);
2599MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002600MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002601