blob: 30c244bbd8ac65d1251362d375898e2a04b36d41 [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;
74 };
75
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;
155 };
156
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;
195 };
196
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 Beltrami0a69452c2006-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;
220 };
221
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
275static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
276{
277 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
278 int len = 0;
279
280 if (xfrm_ctx) {
281 len += sizeof(struct xfrm_user_sec_ctx);
282 len += xfrm_ctx->ctx_len;
283 }
284 return len;
285}
286
287static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
288{
289 struct xfrm_user_sec_ctx *uctx;
290
291 if (!u_arg)
292 return 0;
293
294 uctx = RTA_DATA(u_arg);
295 return security_xfrm_state_alloc(x, uctx);
296}
297
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700298static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
299{
300 struct rtattr *rta = u_arg;
301 xfrm_address_t *p, *uaddrp;
302
303 if (!rta)
304 return 0;
305
306 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200307 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700308 if (!p)
309 return -ENOMEM;
310
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700311 *addrpp = p;
312 return 0;
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
316{
317 memcpy(&x->id, &p->id, sizeof(x->id));
318 memcpy(&x->sel, &p->sel, sizeof(x->sel));
319 memcpy(&x->lft, &p->lft, sizeof(x->lft));
320 x->props.mode = p->mode;
321 x->props.replay_window = p->replay_window;
322 x->props.reqid = p->reqid;
323 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700324 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 x->props.flags = p->flags;
326}
327
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800328/*
329 * someday when pfkey also has support, we could have the code
330 * somehow made shareable and move it to xfrm_state.c - JHS
331 *
332*/
333static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
334{
335 int err = - EINVAL;
336 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
337 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
338 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
339 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
340
341 if (rp) {
342 struct xfrm_replay_state *replay;
343 if (RTA_PAYLOAD(rp) < sizeof(*replay))
344 goto error;
345 replay = RTA_DATA(rp);
346 memcpy(&x->replay, replay, sizeof(*replay));
347 memcpy(&x->preplay, replay, sizeof(*replay));
348 }
349
350 if (lt) {
351 struct xfrm_lifetime_cur *ltime;
352 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
353 goto error;
354 ltime = RTA_DATA(lt);
355 x->curlft.bytes = ltime->bytes;
356 x->curlft.packets = ltime->packets;
357 x->curlft.add_time = ltime->add_time;
358 x->curlft.use_time = ltime->use_time;
359 }
360
361 if (et) {
362 if (RTA_PAYLOAD(et) < sizeof(u32))
363 goto error;
364 x->replay_maxage = *(u32*)RTA_DATA(et);
365 }
366
367 if (rt) {
368 if (RTA_PAYLOAD(rt) < sizeof(u32))
369 goto error;
370 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
371 }
372
373 return 0;
374error:
375 return err;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
379 struct rtattr **xfrma,
380 int *errp)
381{
382 struct xfrm_state *x = xfrm_state_alloc();
383 int err = -ENOMEM;
384
385 if (!x)
386 goto error_no_put;
387
388 copy_from_user_state(x, p);
389
390 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
391 xfrm_aalg_get_byname,
392 xfrma[XFRMA_ALG_AUTH-1])))
393 goto error;
394 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
395 xfrm_ealg_get_byname,
396 xfrma[XFRMA_ALG_CRYPT-1])))
397 goto error;
398 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
399 xfrm_calg_get_byname,
400 xfrma[XFRMA_ALG_COMP-1])))
401 goto error;
402 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
403 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700404 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
405 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700406 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (err)
408 goto error;
409
Trent Jaegerdf718372005-12-13 23:12:27 -0800410 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
411 goto error;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800414 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
415 /* sysctl_xfrm_aevent_etime is in 100ms units */
416 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
417 x->preplay.bitmap = 0;
418 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
419 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
420
421 /* override default values from above */
422
423 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
424 if (err < 0)
425 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return x;
428
429error:
430 x->km.state = XFRM_STATE_DEAD;
431 xfrm_state_put(x);
432error_no_put:
433 *errp = err;
434 return NULL;
435}
436
Christoph Hellwig22e70052007-01-02 15:22:30 -0800437static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
438 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
441 struct xfrm_state *x;
442 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700443 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwig22e70052007-01-02 15:22:30 -0800445 err = verify_newsa_info(p, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (err)
447 return err;
448
Christoph Hellwig22e70052007-01-02 15:22:30 -0800449 x = xfrm_state_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!x)
451 return err;
452
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700453 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
455 err = xfrm_state_add(x);
456 else
457 err = xfrm_state_update(x);
458
Joy Latten161a09e2006-11-27 13:11:54 -0600459 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
460 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (err < 0) {
463 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800464 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700468 c.seq = nlh->nlmsg_seq;
469 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700470 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700471
472 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700473out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700474 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return err;
476}
477
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700478static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
479 struct rtattr **xfrma,
480 int *errp)
481{
482 struct xfrm_state *x = NULL;
483 int err;
484
485 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
486 err = -ESRCH;
487 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
488 } else {
489 xfrm_address_t *saddr = NULL;
490
491 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
492 if (err)
493 goto out;
494
495 if (!saddr) {
496 err = -EINVAL;
497 goto out;
498 }
499
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800500 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700501 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
502 p->family);
503 }
504
505 out:
506 if (!x && errp)
507 *errp = err;
508 return x;
509}
510
Christoph Hellwig22e70052007-01-02 15:22:30 -0800511static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
512 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700515 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700516 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
518
Christoph Hellwig22e70052007-01-02 15:22:30 -0800519 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David S. Miller6f68dc32006-06-08 23:58:52 -0700523 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700524 goto out;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700527 err = -EPERM;
528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700531 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600532
533 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
534 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
535
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700536 if (err < 0)
537 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700538
539 c.seq = nlh->nlmsg_seq;
540 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700541 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700542 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700544out:
545 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700546 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
550{
551 memcpy(&p->id, &x->id, sizeof(p->id));
552 memcpy(&p->sel, &x->sel, sizeof(p->sel));
553 memcpy(&p->lft, &x->lft, sizeof(p->lft));
554 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
555 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700556 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 p->mode = x->props.mode;
558 p->replay_window = x->props.replay_window;
559 p->reqid = x->props.reqid;
560 p->family = x->props.family;
561 p->flags = x->props.flags;
562 p->seq = x->km.seq;
563}
564
565struct xfrm_dump_info {
566 struct sk_buff *in_skb;
567 struct sk_buff *out_skb;
568 u32 nlmsg_seq;
569 u16 nlmsg_flags;
570 int start_idx;
571 int this_idx;
572};
573
574static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
575{
576 struct xfrm_dump_info *sp = ptr;
577 struct sk_buff *in_skb = sp->in_skb;
578 struct sk_buff *skb = sp->out_skb;
579 struct xfrm_usersa_info *p;
580 struct nlmsghdr *nlh;
581 unsigned char *b = skb->tail;
582
583 if (sp->this_idx < sp->start_idx)
584 goto out;
585
586 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
587 sp->nlmsg_seq,
588 XFRM_MSG_NEWSA, sizeof(*p));
589 nlh->nlmsg_flags = sp->nlmsg_flags;
590
591 p = NLMSG_DATA(nlh);
592 copy_to_user_state(x, p);
593
594 if (x->aalg)
595 RTA_PUT(skb, XFRMA_ALG_AUTH,
596 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
597 if (x->ealg)
598 RTA_PUT(skb, XFRMA_ALG_CRYPT,
599 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
600 if (x->calg)
601 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
602
603 if (x->encap)
604 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
605
Trent Jaegerdf718372005-12-13 23:12:27 -0800606 if (x->security) {
607 int ctx_size = sizeof(struct xfrm_sec_ctx) +
608 x->security->ctx_len;
609 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
610 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
611
612 uctx->exttype = XFRMA_SEC_CTX;
613 uctx->len = ctx_size;
614 uctx->ctx_doi = x->security->ctx_doi;
615 uctx->ctx_alg = x->security->ctx_alg;
616 uctx->ctx_len = x->security->ctx_len;
617 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
618 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700619
620 if (x->coaddr)
621 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
622
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700623 if (x->lastused)
624 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 nlh->nlmsg_len = skb->tail - b;
627out:
628 sp->this_idx++;
629 return 0;
630
631nlmsg_failure:
632rtattr_failure:
633 skb_trim(skb, b - skb->data);
634 return -1;
635}
636
637static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
638{
639 struct xfrm_dump_info info;
640
641 info.in_skb = cb->skb;
642 info.out_skb = skb;
643 info.nlmsg_seq = cb->nlh->nlmsg_seq;
644 info.nlmsg_flags = NLM_F_MULTI;
645 info.this_idx = 0;
646 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700647 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 cb->args[0] = info.this_idx;
649
650 return skb->len;
651}
652
653static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
654 struct xfrm_state *x, u32 seq)
655{
656 struct xfrm_dump_info info;
657 struct sk_buff *skb;
658
659 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
660 if (!skb)
661 return ERR_PTR(-ENOMEM);
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 info.in_skb = in_skb;
664 info.out_skb = skb;
665 info.nlmsg_seq = seq;
666 info.nlmsg_flags = 0;
667 info.this_idx = info.start_idx = 0;
668
669 if (dump_one_state(x, 0, &info)) {
670 kfree_skb(skb);
671 return NULL;
672 }
673
674 return skb;
675}
676
Christoph Hellwig22e70052007-01-02 15:22:30 -0800677static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
678 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
681 struct xfrm_state *x;
682 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700683 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Christoph Hellwig22e70052007-01-02 15:22:30 -0800685 x = xfrm_user_state_lookup(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (x == NULL)
687 goto out_noput;
688
689 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
690 if (IS_ERR(resp_skb)) {
691 err = PTR_ERR(resp_skb);
692 } else {
693 err = netlink_unicast(xfrm_nl, resp_skb,
694 NETLINK_CB(skb).pid, MSG_DONTWAIT);
695 }
696 xfrm_state_put(x);
697out_noput:
698 return err;
699}
700
701static int verify_userspi_info(struct xfrm_userspi_info *p)
702{
703 switch (p->info.id.proto) {
704 case IPPROTO_AH:
705 case IPPROTO_ESP:
706 break;
707
708 case IPPROTO_COMP:
709 /* IPCOMP spi is 16-bits. */
710 if (p->max >= 0x10000)
711 return -EINVAL;
712 break;
713
714 default:
715 return -EINVAL;
716 };
717
718 if (p->min > p->max)
719 return -EINVAL;
720
721 return 0;
722}
723
Christoph Hellwig22e70052007-01-02 15:22:30 -0800724static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
725 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct xfrm_state *x;
728 struct xfrm_userspi_info *p;
729 struct sk_buff *resp_skb;
730 xfrm_address_t *daddr;
731 int family;
732 int err;
733
734 p = NLMSG_DATA(nlh);
735 err = verify_userspi_info(p);
736 if (err)
737 goto out_noput;
738
739 family = p->info.family;
740 daddr = &p->info.id.daddr;
741
742 x = NULL;
743 if (p->info.seq) {
744 x = xfrm_find_acq_byseq(p->info.seq);
745 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
746 xfrm_state_put(x);
747 x = NULL;
748 }
749 }
750
751 if (!x)
752 x = xfrm_find_acq(p->info.mode, p->info.reqid,
753 p->info.id.proto, daddr,
754 &p->info.saddr, 1,
755 family);
756 err = -ENOENT;
757 if (x == NULL)
758 goto out_noput;
759
760 resp_skb = ERR_PTR(-ENOENT);
761
762 spin_lock_bh(&x->lock);
763 if (x->km.state != XFRM_STATE_DEAD) {
764 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
765 if (x->id.spi)
766 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
767 }
768 spin_unlock_bh(&x->lock);
769
770 if (IS_ERR(resp_skb)) {
771 err = PTR_ERR(resp_skb);
772 goto out;
773 }
774
775 err = netlink_unicast(xfrm_nl, resp_skb,
776 NETLINK_CB(skb).pid, MSG_DONTWAIT);
777
778out:
779 xfrm_state_put(x);
780out_noput:
781 return err;
782}
783
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800784static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 switch (dir) {
787 case XFRM_POLICY_IN:
788 case XFRM_POLICY_OUT:
789 case XFRM_POLICY_FWD:
790 break;
791
792 default:
793 return -EINVAL;
794 };
795
796 return 0;
797}
798
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800799static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700800{
801 switch (type) {
802 case XFRM_POLICY_TYPE_MAIN:
803#ifdef CONFIG_XFRM_SUB_POLICY
804 case XFRM_POLICY_TYPE_SUB:
805#endif
806 break;
807
808 default:
809 return -EINVAL;
810 };
811
812 return 0;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
816{
817 switch (p->share) {
818 case XFRM_SHARE_ANY:
819 case XFRM_SHARE_SESSION:
820 case XFRM_SHARE_USER:
821 case XFRM_SHARE_UNIQUE:
822 break;
823
824 default:
825 return -EINVAL;
826 };
827
828 switch (p->action) {
829 case XFRM_POLICY_ALLOW:
830 case XFRM_POLICY_BLOCK:
831 break;
832
833 default:
834 return -EINVAL;
835 };
836
837 switch (p->sel.family) {
838 case AF_INET:
839 break;
840
841 case AF_INET6:
842#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
843 break;
844#else
845 return -EAFNOSUPPORT;
846#endif
847
848 default:
849 return -EINVAL;
850 };
851
852 return verify_policy_dir(p->dir);
853}
854
Trent Jaegerdf718372005-12-13 23:12:27 -0800855static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
856{
857 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
858 struct xfrm_user_sec_ctx *uctx;
859
860 if (!rt)
861 return 0;
862
863 uctx = RTA_DATA(rt);
864 return security_xfrm_policy_alloc(pol, uctx);
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
868 int nr)
869{
870 int i;
871
872 xp->xfrm_nr = nr;
873 for (i = 0; i < nr; i++, ut++) {
874 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
875
876 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
877 memcpy(&t->saddr, &ut->saddr,
878 sizeof(xfrm_address_t));
879 t->reqid = ut->reqid;
880 t->mode = ut->mode;
881 t->share = ut->share;
882 t->optional = ut->optional;
883 t->aalgos = ut->aalgos;
884 t->ealgos = ut->ealgos;
885 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800886 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888}
889
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800890static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
891{
892 int i;
893
894 if (nr > XFRM_MAX_DEPTH)
895 return -EINVAL;
896
897 for (i = 0; i < nr; i++) {
898 /* We never validated the ut->family value, so many
899 * applications simply leave it at zero. The check was
900 * never made and ut->family was ignored because all
901 * templates could be assumed to have the same family as
902 * the policy itself. Now that we will have ipv4-in-ipv6
903 * and ipv6-in-ipv4 tunnels, this is no longer true.
904 */
905 if (!ut[i].family)
906 ut[i].family = family;
907
908 switch (ut[i].family) {
909 case AF_INET:
910 break;
911#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
912 case AF_INET6:
913 break;
914#endif
915 default:
916 return -EINVAL;
917 };
918 }
919
920 return 0;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
924{
925 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!rt) {
928 pol->xfrm_nr = 0;
929 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800930 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
931 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
932 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800934 err = validate_tmpl(nr, utmpl, pol->family);
935 if (err)
936 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 copy_templates(pol, RTA_DATA(rt), nr);
939 }
940 return 0;
941}
942
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700943static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
944{
945 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
946 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800947 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700948 int err;
949
950 if (rt) {
951 if (rt->rta_len < sizeof(*upt))
952 return -EINVAL;
953
954 upt = RTA_DATA(rt);
955 type = upt->type;
956 }
957
958 err = verify_policy_type(type);
959 if (err)
960 return err;
961
962 *tp = type;
963 return 0;
964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
967{
968 xp->priority = p->priority;
969 xp->index = p->index;
970 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
971 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
972 xp->action = p->action;
973 xp->flags = p->flags;
974 xp->family = p->sel.family;
975 /* XXX xp->share = p->share; */
976}
977
978static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
979{
980 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
981 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
982 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
983 p->priority = xp->priority;
984 p->index = xp->index;
985 p->sel.family = xp->family;
986 p->dir = dir;
987 p->action = xp->action;
988 p->flags = xp->flags;
989 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
990}
991
992static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
993{
994 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
995 int err;
996
997 if (!xp) {
998 *errp = -ENOMEM;
999 return NULL;
1000 }
1001
1002 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001003
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001004 err = copy_from_user_policy_type(&xp->type, xfrma);
1005 if (err)
1006 goto error;
1007
Trent Jaegerdf718372005-12-13 23:12:27 -08001008 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1009 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001010 if (err)
1011 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001014 error:
1015 *errp = err;
1016 kfree(xp);
1017 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Christoph Hellwig22e70052007-01-02 15:22:30 -08001020static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1021 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1024 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001025 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int err;
1027 int excl;
1028
1029 err = verify_newpolicy_info(p);
1030 if (err)
1031 return err;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001032 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001033 if (err)
1034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Christoph Hellwig22e70052007-01-02 15:22:30 -08001036 xp = xfrm_policy_construct(p, xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!xp)
1038 return err;
1039
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001040 /* shouldnt excl be based on nlh flags??
1041 * Aha! this is anti-netlink really i.e more pfkey derived
1042 * in netlink excl is a flag and you wouldnt need
1043 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1045 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001046 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1047 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001050 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 kfree(xp);
1052 return err;
1053 }
1054
Herbert Xuf60f6b82005-06-18 22:44:37 -07001055 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001056 c.seq = nlh->nlmsg_seq;
1057 c.pid = nlh->nlmsg_pid;
1058 km_policy_notify(xp, p->dir, &c);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 xfrm_pol_put(xp);
1061
1062 return 0;
1063}
1064
1065static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1066{
1067 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1068 int i;
1069
1070 if (xp->xfrm_nr == 0)
1071 return 0;
1072
1073 for (i = 0; i < xp->xfrm_nr; i++) {
1074 struct xfrm_user_tmpl *up = &vec[i];
1075 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1076
1077 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001078 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1080 up->reqid = kp->reqid;
1081 up->mode = kp->mode;
1082 up->share = kp->share;
1083 up->optional = kp->optional;
1084 up->aalgos = kp->aalgos;
1085 up->ealgos = kp->ealgos;
1086 up->calgos = kp->calgos;
1087 }
1088 RTA_PUT(skb, XFRMA_TMPL,
1089 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1090 vec);
1091
1092 return 0;
1093
1094rtattr_failure:
1095 return -1;
1096}
1097
Serge Hallyn0d681622006-07-24 23:30:44 -07001098static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001099{
Serge Hallyn0d681622006-07-24 23:30:44 -07001100 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1101 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1102 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001103
Serge Hallyn0d681622006-07-24 23:30:44 -07001104 uctx->exttype = XFRMA_SEC_CTX;
1105 uctx->len = ctx_size;
1106 uctx->ctx_doi = s->ctx_doi;
1107 uctx->ctx_alg = s->ctx_alg;
1108 uctx->ctx_len = s->ctx_len;
1109 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001110 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001111
1112 rtattr_failure:
1113 return -1;
1114}
1115
Serge Hallyn0d681622006-07-24 23:30:44 -07001116static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1117{
1118 if (x->security) {
1119 return copy_sec_ctx(x->security, skb);
1120 }
1121 return 0;
1122}
1123
1124static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1125{
1126 if (xp->security) {
1127 return copy_sec_ctx(xp->security, skb);
1128 }
1129 return 0;
1130}
1131
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001132#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001133static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134{
1135 struct xfrm_userpolicy_type upt;
1136
1137 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001138 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001139
1140 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1141
1142 return 0;
1143
1144rtattr_failure:
1145 return -1;
1146}
1147
1148#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001149static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001150{
1151 return 0;
1152}
1153#endif
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1156{
1157 struct xfrm_dump_info *sp = ptr;
1158 struct xfrm_userpolicy_info *p;
1159 struct sk_buff *in_skb = sp->in_skb;
1160 struct sk_buff *skb = sp->out_skb;
1161 struct nlmsghdr *nlh;
1162 unsigned char *b = skb->tail;
1163
1164 if (sp->this_idx < sp->start_idx)
1165 goto out;
1166
1167 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1168 sp->nlmsg_seq,
1169 XFRM_MSG_NEWPOLICY, sizeof(*p));
1170 p = NLMSG_DATA(nlh);
1171 nlh->nlmsg_flags = sp->nlmsg_flags;
1172
1173 copy_to_user_policy(xp, p, dir);
1174 if (copy_to_user_tmpl(xp, skb) < 0)
1175 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001176 if (copy_to_user_sec_ctx(xp, skb))
1177 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001178 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001179 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 nlh->nlmsg_len = skb->tail - b;
1182out:
1183 sp->this_idx++;
1184 return 0;
1185
1186nlmsg_failure:
1187 skb_trim(skb, b - skb->data);
1188 return -1;
1189}
1190
1191static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1192{
1193 struct xfrm_dump_info info;
1194
1195 info.in_skb = cb->skb;
1196 info.out_skb = skb;
1197 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1198 info.nlmsg_flags = NLM_F_MULTI;
1199 info.this_idx = 0;
1200 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001201 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1202#ifdef CONFIG_XFRM_SUB_POLICY
1203 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 cb->args[0] = info.this_idx;
1206
1207 return skb->len;
1208}
1209
1210static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1211 struct xfrm_policy *xp,
1212 int dir, u32 seq)
1213{
1214 struct xfrm_dump_info info;
1215 struct sk_buff *skb;
1216
1217 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1218 if (!skb)
1219 return ERR_PTR(-ENOMEM);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 info.in_skb = in_skb;
1222 info.out_skb = skb;
1223 info.nlmsg_seq = seq;
1224 info.nlmsg_flags = 0;
1225 info.this_idx = info.start_idx = 0;
1226
1227 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1228 kfree_skb(skb);
1229 return NULL;
1230 }
1231
1232 return skb;
1233}
1234
Christoph Hellwig22e70052007-01-02 15:22:30 -08001235static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1236 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct xfrm_policy *xp;
1239 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001240 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001242 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int delete;
1244
1245 p = NLMSG_DATA(nlh);
1246 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1247
Christoph Hellwig22e70052007-01-02 15:22:30 -08001248 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001249 if (err)
1250 return err;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 err = verify_policy_dir(p->dir);
1253 if (err)
1254 return err;
1255
1256 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001257 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001258 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001259 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001260 struct xfrm_policy tmp;
1261
Christoph Hellwig22e70052007-01-02 15:22:30 -08001262 err = verify_sec_ctx_len(xfrma);
Trent Jaegerdf718372005-12-13 23:12:27 -08001263 if (err)
1264 return err;
1265
1266 memset(&tmp, 0, sizeof(struct xfrm_policy));
1267 if (rt) {
1268 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1269
1270 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1271 return err;
1272 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001273 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1274 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001275 security_xfrm_policy_free(&tmp);
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (xp == NULL)
1278 return -ENOENT;
1279
1280 if (!delete) {
1281 struct sk_buff *resp_skb;
1282
1283 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1284 if (IS_ERR(resp_skb)) {
1285 err = PTR_ERR(resp_skb);
1286 } else {
1287 err = netlink_unicast(xfrm_nl, resp_skb,
1288 NETLINK_CB(skb).pid,
1289 MSG_DONTWAIT);
1290 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001291 } else {
David S. Miller13fcfbb02007-02-12 13:53:54 -08001292 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1293 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1294
1295 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001296 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001297
Herbert Xue7443892005-06-18 22:44:18 -07001298 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001299 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001300 c.seq = nlh->nlmsg_seq;
1301 c.pid = nlh->nlmsg_pid;
1302 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001305out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001306 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return err;
1308}
1309
Christoph Hellwig22e70052007-01-02 15:22:30 -08001310static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1311 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001313 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001315 struct xfrm_audit audit_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Joy Latten161a09e2006-11-27 13:11:54 -06001317 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1318 audit_info.secid = NETLINK_CB(skb).sid;
1319 xfrm_state_flush(p->proto, &audit_info);
Herbert Xubf08867f92005-06-18 22:44:00 -07001320 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001321 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001322 c.seq = nlh->nlmsg_seq;
1323 c.pid = nlh->nlmsg_pid;
1324 km_state_notify(NULL, &c);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327}
1328
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001329
1330static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1331{
1332 struct xfrm_aevent_id *id;
1333 struct nlmsghdr *nlh;
1334 struct xfrm_lifetime_cur ltime;
1335 unsigned char *b = skb->tail;
1336
1337 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1338 id = NLMSG_DATA(nlh);
1339 nlh->nlmsg_flags = 0;
1340
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001341 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001342 id->sa_id.spi = x->id.spi;
1343 id->sa_id.family = x->props.family;
1344 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001345 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1346 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001347 id->flags = c->data.aevent;
1348
1349 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1350
1351 ltime.bytes = x->curlft.bytes;
1352 ltime.packets = x->curlft.packets;
1353 ltime.add_time = x->curlft.add_time;
1354 ltime.use_time = x->curlft.use_time;
1355
1356 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1357
1358 if (id->flags&XFRM_AE_RTHR) {
1359 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1360 }
1361
1362 if (id->flags&XFRM_AE_ETHR) {
1363 u32 etimer = x->replay_maxage*10/HZ;
1364 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1365 }
1366
1367 nlh->nlmsg_len = skb->tail - b;
1368 return skb->len;
1369
1370rtattr_failure:
1371nlmsg_failure:
1372 skb_trim(skb, b - skb->data);
1373 return -1;
1374}
1375
Christoph Hellwig22e70052007-01-02 15:22:30 -08001376static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1377 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001378{
1379 struct xfrm_state *x;
1380 struct sk_buff *r_skb;
1381 int err;
1382 struct km_event c;
1383 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1384 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1385 struct xfrm_usersa_id *id = &p->sa_id;
1386
1387 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1388 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1389
1390 if (p->flags&XFRM_AE_RTHR)
1391 len+=RTA_SPACE(sizeof(u32));
1392
1393 if (p->flags&XFRM_AE_ETHR)
1394 len+=RTA_SPACE(sizeof(u32));
1395
1396 r_skb = alloc_skb(len, GFP_ATOMIC);
1397 if (r_skb == NULL)
1398 return -ENOMEM;
1399
1400 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1401 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001402 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001403 return -ESRCH;
1404 }
1405
1406 /*
1407 * XXX: is this lock really needed - none of the other
1408 * gets lock (the concern is things getting updated
1409 * while we are still reading) - jhs
1410 */
1411 spin_lock_bh(&x->lock);
1412 c.data.aevent = p->flags;
1413 c.seq = nlh->nlmsg_seq;
1414 c.pid = nlh->nlmsg_pid;
1415
1416 if (build_aevent(r_skb, x, &c) < 0)
1417 BUG();
1418 err = netlink_unicast(xfrm_nl, r_skb,
1419 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1420 spin_unlock_bh(&x->lock);
1421 xfrm_state_put(x);
1422 return err;
1423}
1424
Christoph Hellwig22e70052007-01-02 15:22:30 -08001425static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1426 struct rtattr **xfrma)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001427{
1428 struct xfrm_state *x;
1429 struct km_event c;
1430 int err = - EINVAL;
1431 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1432 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1433 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1434
1435 if (!lt && !rp)
1436 return err;
1437
1438 /* pedantic mode - thou shalt sayeth replaceth */
1439 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1440 return err;
1441
1442 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1443 if (x == NULL)
1444 return -ESRCH;
1445
1446 if (x->km.state != XFRM_STATE_VALID)
1447 goto out;
1448
1449 spin_lock_bh(&x->lock);
Christoph Hellwig22e70052007-01-02 15:22:30 -08001450 err = xfrm_update_ae_params(x, xfrma);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001451 spin_unlock_bh(&x->lock);
1452 if (err < 0)
1453 goto out;
1454
1455 c.event = nlh->nlmsg_type;
1456 c.seq = nlh->nlmsg_seq;
1457 c.pid = nlh->nlmsg_pid;
1458 c.data.aevent = XFRM_AE_CU;
1459 km_state_notify(x, &c);
1460 err = 0;
1461out:
1462 xfrm_state_put(x);
1463 return err;
1464}
1465
Christoph Hellwig22e70052007-01-02 15:22:30 -08001466static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1467 struct rtattr **xfrma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001469 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001470 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001471 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001472 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001473
Christoph Hellwig22e70052007-01-02 15:22:30 -08001474 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001475 if (err)
1476 return err;
1477
Joy Latten161a09e2006-11-27 13:11:54 -06001478 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1479 audit_info.secid = NETLINK_CB(skb).sid;
1480 xfrm_policy_flush(type, &audit_info);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001481 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001482 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001483 c.seq = nlh->nlmsg_seq;
1484 c.pid = nlh->nlmsg_pid;
1485 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487}
1488
Christoph Hellwig22e70052007-01-02 15:22:30 -08001489static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1490 struct rtattr **xfrma)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001491{
1492 struct xfrm_policy *xp;
1493 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1494 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001495 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001496 int err = -ENOENT;
1497
Christoph Hellwig22e70052007-01-02 15:22:30 -08001498 err = copy_from_user_policy_type(&type, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001499 if (err)
1500 return err;
1501
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001502 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001503 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001504 else {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001505 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001506 struct xfrm_policy tmp;
1507
Christoph Hellwig22e70052007-01-02 15:22:30 -08001508 err = verify_sec_ctx_len(xfrma);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001509 if (err)
1510 return err;
1511
1512 memset(&tmp, 0, sizeof(struct xfrm_policy));
1513 if (rt) {
1514 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1515
1516 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1517 return err;
1518 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001519 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1520 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001521 security_xfrm_policy_free(&tmp);
1522 }
1523
1524 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001525 return -ENOENT;
1526 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001527 if (xp->dead) {
1528 read_unlock(&xp->lock);
1529 goto out;
1530 }
1531
1532 read_unlock(&xp->lock);
1533 err = 0;
1534 if (up->hard) {
1535 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001536 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1537 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1538
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001539 } else {
1540 // reset the timers here?
1541 printk("Dont know what to do with soft policy expire\n");
1542 }
1543 km_policy_expired(xp, p->dir, up->hard, current->pid);
1544
1545out:
1546 xfrm_pol_put(xp);
1547 return err;
1548}
1549
Christoph Hellwig22e70052007-01-02 15:22:30 -08001550static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1551 struct rtattr **xfrma)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001552{
1553 struct xfrm_state *x;
1554 int err;
1555 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1556 struct xfrm_usersa_info *p = &ue->state;
1557
1558 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001559
David S. Miller3a765aa2007-02-26 14:52:21 -08001560 err = -ENOENT;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001561 if (x == NULL)
1562 return err;
1563
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001564 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001565 err = -EINVAL;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001566 if (x->km.state != XFRM_STATE_VALID)
1567 goto out;
1568 km_state_expired(x, ue->hard, current->pid);
1569
Joy Latten161a09e2006-11-27 13:11:54 -06001570 if (ue->hard) {
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001571 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001572 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1573 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1574 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001575 err = 0;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001576out:
1577 spin_unlock_bh(&x->lock);
1578 xfrm_state_put(x);
1579 return err;
1580}
1581
Christoph Hellwig22e70052007-01-02 15:22:30 -08001582static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1583 struct rtattr **xfrma)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001584{
1585 struct xfrm_policy *xp;
1586 struct xfrm_user_tmpl *ut;
1587 int i;
1588 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1589
1590 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1591 struct xfrm_state *x = xfrm_state_alloc();
1592 int err = -ENOMEM;
1593
1594 if (!x)
1595 return err;
1596
1597 err = verify_newpolicy_info(&ua->policy);
1598 if (err) {
1599 printk("BAD policy passed\n");
1600 kfree(x);
1601 return err;
1602 }
1603
1604 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001605 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1606 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001607 kfree(x);
1608 return err;
1609 }
1610
1611 memcpy(&x->id, &ua->id, sizeof(ua->id));
1612 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1613 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1614
1615 ut = RTA_DATA(rt);
1616 /* extract the templates and for each call km_key */
1617 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1618 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1619 memcpy(&x->id, &t->id, sizeof(x->id));
1620 x->props.mode = t->mode;
1621 x->props.reqid = t->reqid;
1622 x->props.family = ut->family;
1623 t->aalgos = ua->aalgos;
1624 t->ealgos = ua->ealgos;
1625 t->calgos = ua->calgos;
1626 err = km_query(x, t, xp);
1627
1628 }
1629
1630 kfree(x);
1631 kfree(xp);
1632
1633 return 0;
1634}
1635
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001636#ifdef CONFIG_XFRM_MIGRATE
1637static int verify_user_migrate(struct rtattr **xfrma)
1638{
1639 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1640 struct xfrm_user_migrate *um;
1641
1642 if (!rt)
1643 return -EINVAL;
1644
1645 if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
1646 return -EINVAL;
1647
1648 return 0;
1649}
1650
1651static int copy_from_user_migrate(struct xfrm_migrate *ma,
1652 struct rtattr **xfrma, int *num)
1653{
1654 struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
1655 struct xfrm_user_migrate *um;
1656 int i, num_migrate;
1657
1658 um = RTA_DATA(rt);
1659 num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
1660
1661 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1662 return -EINVAL;
1663
1664 for (i = 0; i < num_migrate; i++, um++, ma++) {
1665 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1666 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1667 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1668 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1669
1670 ma->proto = um->proto;
1671 ma->mode = um->mode;
1672 ma->reqid = um->reqid;
1673
1674 ma->old_family = um->old_family;
1675 ma->new_family = um->new_family;
1676 }
1677
1678 *num = i;
1679 return 0;
1680}
1681
1682static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1683 struct rtattr **xfrma)
1684{
1685 struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
1686 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1687 u8 type;
1688 int err;
1689 int n = 0;
1690
1691 err = verify_user_migrate((struct rtattr **)xfrma);
1692 if (err)
1693 return err;
1694
1695 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1696 if (err)
1697 return err;
1698
1699 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1700 (struct rtattr **)xfrma, &n);
1701 if (err)
1702 return err;
1703
1704 if (!n)
1705 return 0;
1706
1707 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1708
1709 return 0;
1710}
1711#else
1712static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1713 struct rtattr **xfrma)
1714{
1715 return -ENOPROTOOPT;
1716}
1717#endif
1718
1719#ifdef CONFIG_XFRM_MIGRATE
1720static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1721{
1722 struct xfrm_user_migrate um;
1723
1724 memset(&um, 0, sizeof(um));
1725 um.proto = m->proto;
1726 um.mode = m->mode;
1727 um.reqid = m->reqid;
1728 um.old_family = m->old_family;
1729 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1730 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1731 um.new_family = m->new_family;
1732 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1733 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1734
1735 RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1736 return 0;
1737
1738rtattr_failure:
1739 return -1;
1740}
1741
1742static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1743 int num_migrate, struct xfrm_selector *sel,
1744 u8 dir, u8 type)
1745{
1746 struct xfrm_migrate *mp;
1747 struct xfrm_userpolicy_id *pol_id;
1748 struct nlmsghdr *nlh;
1749 unsigned char *b = skb->tail;
1750 int i;
1751
1752 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
1753 pol_id = NLMSG_DATA(nlh);
1754 nlh->nlmsg_flags = 0;
1755
1756 /* copy data from selector, dir, and type to the pol_id */
1757 memset(pol_id, 0, sizeof(*pol_id));
1758 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1759 pol_id->dir = dir;
1760
1761 if (copy_to_user_policy_type(type, skb) < 0)
1762 goto nlmsg_failure;
1763
1764 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1765 if (copy_to_user_migrate(mp, skb) < 0)
1766 goto nlmsg_failure;
1767 }
1768
1769 nlh->nlmsg_len = skb->tail - b;
1770 return skb->len;
1771nlmsg_failure:
1772 skb_trim(skb, b - skb->data);
1773 return -1;
1774}
1775
1776static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1777 struct xfrm_migrate *m, int num_migrate)
1778{
1779 struct sk_buff *skb;
1780 size_t len;
1781
1782 len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
1783 len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
1784#ifdef CONFIG_XFRM_SUB_POLICY
1785 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1786#endif
1787 skb = alloc_skb(len, GFP_ATOMIC);
1788 if (skb == NULL)
1789 return -ENOMEM;
1790
1791 /* build migrate */
1792 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1793 BUG();
1794
1795 NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
1796 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
1797 GFP_ATOMIC);
1798}
1799#else
1800static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1801 struct xfrm_migrate *m, int num_migrate)
1802{
1803 return -ENOPROTOOPT;
1804}
1805#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806
Thomas Graf492b5582005-05-03 14:26:40 -07001807#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1808
1809static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1810 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1811 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1812 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1813 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1814 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1815 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1816 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001817 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001818 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001819 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1820 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001821 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001822 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1823 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001824 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1825 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001826 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001827 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828};
1829
Thomas Graf492b5582005-05-03 14:26:40 -07001830#undef XMSGSIZE
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static struct xfrm_link {
Christoph Hellwig22e70052007-01-02 15:22:30 -08001833 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001835} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1836 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1837 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1838 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1839 .dump = xfrm_dump_sa },
1840 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1841 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1842 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1843 .dump = xfrm_dump_policy },
1844 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001845 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001846 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001847 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1848 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001849 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001850 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1851 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1853 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001854 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855};
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1858{
1859 struct rtattr *xfrma[XFRMA_MAX];
1860 struct xfrm_link *link;
1861 int type, min_len;
1862
1863 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1864 return 0;
1865
1866 type = nlh->nlmsg_type;
1867
1868 /* A control message: ignore them */
1869 if (type < XFRM_MSG_BASE)
1870 return 0;
1871
1872 /* Unknown message: reply with EINVAL */
1873 if (type > XFRM_MSG_MAX)
1874 goto err_einval;
1875
1876 type -= XFRM_MSG_BASE;
1877 link = &xfrm_dispatch[type];
1878
1879 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001880 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *errp = -EPERM;
1882 return -1;
1883 }
1884
Thomas Graf492b5582005-05-03 14:26:40 -07001885 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1886 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1887 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (link->dump == NULL)
1889 goto err_einval;
1890
1891 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001892 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return -1;
1894 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001895
1896 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return -1;
1898 }
1899
1900 memset(xfrma, 0, sizeof(xfrma));
1901
1902 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1903 goto err_einval;
1904
1905 if (nlh->nlmsg_len > min_len) {
1906 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1907 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1908
1909 while (RTA_OK(attr, attrlen)) {
1910 unsigned short flavor = attr->rta_type;
1911 if (flavor) {
1912 if (flavor > XFRMA_MAX)
1913 goto err_einval;
1914 xfrma[flavor - 1] = attr;
1915 }
1916 attr = RTA_NEXT(attr, attrlen);
1917 }
1918 }
1919
1920 if (link->doit == NULL)
1921 goto err_einval;
Christoph Hellwig22e70052007-01-02 15:22:30 -08001922 *errp = link->doit(skb, nlh, xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 return *errp;
1925
1926err_einval:
1927 *errp = -EINVAL;
1928 return -1;
1929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931static void xfrm_netlink_rcv(struct sock *sk, int len)
1932{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001933 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001936 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001937 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001938 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001940 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001943static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct xfrm_user_expire *ue;
1946 struct nlmsghdr *nlh;
1947 unsigned char *b = skb->tail;
1948
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001949 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 sizeof(*ue));
1951 ue = NLMSG_DATA(nlh);
1952 nlh->nlmsg_flags = 0;
1953
1954 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001955 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 nlh->nlmsg_len = skb->tail - b;
1958 return skb->len;
1959
1960nlmsg_failure:
1961 skb_trim(skb, b - skb->data);
1962 return -1;
1963}
1964
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001965static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001968 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001970 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (skb == NULL)
1972 return -ENOMEM;
1973
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001974 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 BUG();
1976
Patrick McHardyac6d4392005-08-14 19:29:52 -07001977 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1978 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001981static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1982{
1983 struct sk_buff *skb;
1984 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1985
1986 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1987 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1988 skb = alloc_skb(len, GFP_ATOMIC);
1989 if (skb == NULL)
1990 return -ENOMEM;
1991
1992 if (build_aevent(skb, x, c) < 0)
1993 BUG();
1994
1995 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1996 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1997}
1998
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001999static int xfrm_notify_sa_flush(struct km_event *c)
2000{
2001 struct xfrm_usersa_flush *p;
2002 struct nlmsghdr *nlh;
2003 struct sk_buff *skb;
2004 unsigned char *b;
2005 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2006
2007 skb = alloc_skb(len, GFP_ATOMIC);
2008 if (skb == NULL)
2009 return -ENOMEM;
2010 b = skb->tail;
2011
2012 nlh = NLMSG_PUT(skb, c->pid, c->seq,
2013 XFRM_MSG_FLUSHSA, sizeof(*p));
2014 nlh->nlmsg_flags = 0;
2015
2016 p = NLMSG_DATA(nlh);
Herbert Xubf08867f92005-06-18 22:44:00 -07002017 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002018
2019 nlh->nlmsg_len = skb->tail - b;
2020
Patrick McHardyac6d4392005-08-14 19:29:52 -07002021 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2022 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002023
2024nlmsg_failure:
2025 kfree_skb(skb);
2026 return -1;
2027}
2028
2029static int inline xfrm_sa_len(struct xfrm_state *x)
2030{
Herbert Xu0603eac2005-06-18 22:54:36 -07002031 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032 if (x->aalg)
2033 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
2034 if (x->ealg)
2035 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
2036 if (x->calg)
2037 l += RTA_SPACE(sizeof(*x->calg));
2038 if (x->encap)
2039 l += RTA_SPACE(sizeof(*x->encap));
2040
2041 return l;
2042}
2043
2044static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2045{
2046 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002047 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002048 struct nlmsghdr *nlh;
2049 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002050 unsigned char *b;
2051 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002052 int headlen;
2053
2054 headlen = sizeof(*p);
2055 if (c->event == XFRM_MSG_DELSA) {
2056 len += RTA_SPACE(headlen);
2057 headlen = sizeof(*id);
2058 }
2059 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060
2061 skb = alloc_skb(len, GFP_ATOMIC);
2062 if (skb == NULL)
2063 return -ENOMEM;
2064 b = skb->tail;
2065
Herbert Xu0603eac2005-06-18 22:54:36 -07002066 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002067 nlh->nlmsg_flags = 0;
2068
2069 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002070 if (c->event == XFRM_MSG_DELSA) {
2071 id = NLMSG_DATA(nlh);
2072 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2073 id->spi = x->id.spi;
2074 id->family = x->props.family;
2075 id->proto = x->id.proto;
2076
2077 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2078 }
2079
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002080 copy_to_user_state(x, p);
2081
2082 if (x->aalg)
2083 RTA_PUT(skb, XFRMA_ALG_AUTH,
2084 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
2085 if (x->ealg)
2086 RTA_PUT(skb, XFRMA_ALG_CRYPT,
2087 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
2088 if (x->calg)
2089 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2090
2091 if (x->encap)
2092 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2093
2094 nlh->nlmsg_len = skb->tail - b;
2095
Patrick McHardyac6d4392005-08-14 19:29:52 -07002096 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
2097 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002098
2099nlmsg_failure:
2100rtattr_failure:
2101 kfree_skb(skb);
2102 return -1;
2103}
2104
2105static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2106{
2107
2108 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002109 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002110 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002111 case XFRM_MSG_NEWAE:
2112 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002113 case XFRM_MSG_DELSA:
2114 case XFRM_MSG_UPDSA:
2115 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002116 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002117 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002118 return xfrm_notify_sa_flush(c);
2119 default:
2120 printk("xfrm_user: Unknown SA event %d\n", c->event);
2121 break;
2122 }
2123
2124 return 0;
2125
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2129 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2130 int dir)
2131{
2132 struct xfrm_user_acquire *ua;
2133 struct nlmsghdr *nlh;
2134 unsigned char *b = skb->tail;
2135 __u32 seq = xfrm_get_acqseq();
2136
2137 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
2138 sizeof(*ua));
2139 ua = NLMSG_DATA(nlh);
2140 nlh->nlmsg_flags = 0;
2141
2142 memcpy(&ua->id, &x->id, sizeof(ua->id));
2143 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2144 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2145 copy_to_user_policy(xp, &ua->policy, dir);
2146 ua->aalgos = xt->aalgos;
2147 ua->ealgos = xt->ealgos;
2148 ua->calgos = xt->calgos;
2149 ua->seq = x->km.seq = seq;
2150
2151 if (copy_to_user_tmpl(xp, skb) < 0)
2152 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002153 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002154 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002155 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002156 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 nlh->nlmsg_len = skb->tail - b;
2159 return skb->len;
2160
2161nlmsg_failure:
2162 skb_trim(skb, b - skb->data);
2163 return -1;
2164}
2165
2166static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2167 struct xfrm_policy *xp, int dir)
2168{
2169 struct sk_buff *skb;
2170 size_t len;
2171
2172 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2173 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002174 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002175#ifdef CONFIG_XFRM_SUB_POLICY
2176 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 skb = alloc_skb(len, GFP_ATOMIC);
2179 if (skb == NULL)
2180 return -ENOMEM;
2181
2182 if (build_acquire(skb, x, xt, xp, dir) < 0)
2183 BUG();
2184
Patrick McHardyac6d4392005-08-14 19:29:52 -07002185 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2186 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189/* User gives us xfrm_user_policy_info followed by an array of 0
2190 * or more templates.
2191 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002192static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 u8 *data, int len, int *dir)
2194{
2195 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2196 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2197 struct xfrm_policy *xp;
2198 int nr;
2199
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002200 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 case AF_INET:
2202 if (opt != IP_XFRM_POLICY) {
2203 *dir = -EOPNOTSUPP;
2204 return NULL;
2205 }
2206 break;
2207#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2208 case AF_INET6:
2209 if (opt != IPV6_XFRM_POLICY) {
2210 *dir = -EOPNOTSUPP;
2211 return NULL;
2212 }
2213 break;
2214#endif
2215 default:
2216 *dir = -EINVAL;
2217 return NULL;
2218 }
2219
2220 *dir = -EINVAL;
2221
2222 if (len < sizeof(*p) ||
2223 verify_newpolicy_info(p))
2224 return NULL;
2225
2226 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002227 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return NULL;
2229
Herbert Xua4f1bac2005-07-26 15:43:17 -07002230 if (p->dir > XFRM_POLICY_OUT)
2231 return NULL;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 xp = xfrm_policy_alloc(GFP_KERNEL);
2234 if (xp == NULL) {
2235 *dir = -ENOBUFS;
2236 return NULL;
2237 }
2238
2239 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002240 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 copy_templates(xp, ut, nr);
2242
2243 *dir = p->dir;
2244
2245 return xp;
2246}
2247
2248static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002249 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 struct xfrm_user_polexpire *upe;
2252 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002253 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 unsigned char *b = skb->tail;
2255
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002256 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 upe = NLMSG_DATA(nlh);
2258 nlh->nlmsg_flags = 0;
2259
2260 copy_to_user_policy(xp, &upe->pol, dir);
2261 if (copy_to_user_tmpl(xp, skb) < 0)
2262 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002263 if (copy_to_user_sec_ctx(xp, skb))
2264 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002265 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002266 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 upe->hard = !!hard;
2268
2269 nlh->nlmsg_len = skb->tail - b;
2270 return skb->len;
2271
2272nlmsg_failure:
2273 skb_trim(skb, b - skb->data);
2274 return -1;
2275}
2276
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 struct sk_buff *skb;
2280 size_t len;
2281
2282 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2283 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002284 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002285#ifdef CONFIG_XFRM_SUB_POLICY
2286 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 skb = alloc_skb(len, GFP_ATOMIC);
2289 if (skb == NULL)
2290 return -ENOMEM;
2291
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002292 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 BUG();
2294
Patrick McHardyac6d4392005-08-14 19:29:52 -07002295 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2296 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002299static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2300{
2301 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002302 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002303 struct nlmsghdr *nlh;
2304 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305 unsigned char *b;
2306 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002307 int headlen;
2308
2309 headlen = sizeof(*p);
2310 if (c->event == XFRM_MSG_DELPOLICY) {
2311 len += RTA_SPACE(headlen);
2312 headlen = sizeof(*id);
2313 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002314#ifdef CONFIG_XFRM_SUB_POLICY
2315 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2316#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002317 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002318
2319 skb = alloc_skb(len, GFP_ATOMIC);
2320 if (skb == NULL)
2321 return -ENOMEM;
2322 b = skb->tail;
2323
Herbert Xu0603eac2005-06-18 22:54:36 -07002324 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325
2326 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002327 if (c->event == XFRM_MSG_DELPOLICY) {
2328 id = NLMSG_DATA(nlh);
2329 memset(id, 0, sizeof(*id));
2330 id->dir = dir;
2331 if (c->data.byid)
2332 id->index = xp->index;
2333 else
2334 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2335
2336 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2337 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338
2339 nlh->nlmsg_flags = 0;
2340
2341 copy_to_user_policy(xp, p, dir);
2342 if (copy_to_user_tmpl(xp, skb) < 0)
2343 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002344 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002345 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346
2347 nlh->nlmsg_len = skb->tail - b;
2348
Patrick McHardyac6d4392005-08-14 19:29:52 -07002349 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2350 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002351
2352nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002353rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002354 kfree_skb(skb);
2355 return -1;
2356}
2357
2358static int xfrm_notify_policy_flush(struct km_event *c)
2359{
2360 struct nlmsghdr *nlh;
2361 struct sk_buff *skb;
2362 unsigned char *b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002363 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002364#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002365 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002366#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002367 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368
2369 skb = alloc_skb(len, GFP_ATOMIC);
2370 if (skb == NULL)
2371 return -ENOMEM;
2372 b = skb->tail;
2373
2374
2375 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002376 nlh->nlmsg_flags = 0;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002377 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2378 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002379
2380 nlh->nlmsg_len = skb->tail - b;
2381
Patrick McHardyac6d4392005-08-14 19:29:52 -07002382 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2383 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384
2385nlmsg_failure:
2386 kfree_skb(skb);
2387 return -1;
2388}
2389
2390static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2391{
2392
2393 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002394 case XFRM_MSG_NEWPOLICY:
2395 case XFRM_MSG_UPDPOLICY:
2396 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002398 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002400 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 return xfrm_exp_policy_notify(xp, dir, c);
2402 default:
2403 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2404 }
2405
2406 return 0;
2407
2408}
2409
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002410static int build_report(struct sk_buff *skb, u8 proto,
2411 struct xfrm_selector *sel, xfrm_address_t *addr)
2412{
2413 struct xfrm_user_report *ur;
2414 struct nlmsghdr *nlh;
2415 unsigned char *b = skb->tail;
2416
2417 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2418 ur = NLMSG_DATA(nlh);
2419 nlh->nlmsg_flags = 0;
2420
2421 ur->proto = proto;
2422 memcpy(&ur->sel, sel, sizeof(ur->sel));
2423
2424 if (addr)
2425 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2426
2427 nlh->nlmsg_len = skb->tail - b;
2428 return skb->len;
2429
2430nlmsg_failure:
2431rtattr_failure:
2432 skb_trim(skb, b - skb->data);
2433 return -1;
2434}
2435
2436static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2437 xfrm_address_t *addr)
2438{
2439 struct sk_buff *skb;
2440 size_t len;
2441
2442 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2443 skb = alloc_skb(len, GFP_ATOMIC);
2444 if (skb == NULL)
2445 return -ENOMEM;
2446
2447 if (build_report(skb, proto, sel, addr) < 0)
2448 BUG();
2449
2450 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2451 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454static struct xfrm_mgr netlink_mgr = {
2455 .id = "netlink",
2456 .notify = xfrm_send_state_notify,
2457 .acquire = xfrm_send_acquire,
2458 .compile_policy = xfrm_compile_policy,
2459 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002460 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002461 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462};
2463
2464static int __init xfrm_user_init(void)
2465{
Patrick McHardybe336902006-03-20 22:40:54 -08002466 struct sock *nlsk;
2467
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002468 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Patrick McHardybe336902006-03-20 22:40:54 -08002470 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002471 xfrm_netlink_rcv, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002472 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002474 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 xfrm_register_km(&netlink_mgr);
2477
2478 return 0;
2479}
2480
2481static void __exit xfrm_user_exit(void)
2482{
Patrick McHardybe336902006-03-20 22:40:54 -08002483 struct sock *nlsk = xfrm_nl;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002486 rcu_assign_pointer(xfrm_nl, NULL);
2487 synchronize_rcu();
2488 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491module_init(xfrm_user_init);
2492module_exit(xfrm_user_exit);
2493MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002494MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002495