blob: 939808de9e2049027700d44edffeceed673e71e7 [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>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
33{
34 struct rtattr *rt = xfrma[type - 1];
35 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070036 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (!rt)
39 return 0;
40
Herbert Xu31c26852005-05-19 12:39:49 -070041 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
42 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return -EINVAL;
44
45 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070046
47 len -= (algp->alg_key_len + 7U) / 8;
48 if (len < 0)
49 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
70 };
71
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
76static int verify_encap_tmpl(struct rtattr **xfrma)
77{
78 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
79 struct xfrm_encap_tmpl *encap;
80
81 if (!rt)
82 return 0;
83
84 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
85 return -EINVAL;
86
87 return 0;
88}
89
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070090static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
91 xfrm_address_t **addrp)
92{
93 struct rtattr *rt = xfrma[type - 1];
94
95 if (!rt)
96 return 0;
97
98 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
99 return -EINVAL;
100
101 if (addrp)
102 *addrp = RTA_DATA(rt);
103
104 return 0;
105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107static inline int verify_sec_ctx_len(struct rtattr **xfrma)
108{
109 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
110 struct xfrm_user_sec_ctx *uctx;
111 int len = 0;
112
113 if (!rt)
114 return 0;
115
116 if (rt->rta_len < sizeof(*uctx))
117 return -EINVAL;
118
119 uctx = RTA_DATA(rt);
120
Trent Jaegerdf718372005-12-13 23:12:27 -0800121 len += sizeof(struct xfrm_user_sec_ctx);
122 len += uctx->ctx_len;
123
124 if (uctx->len != len)
125 return -EINVAL;
126
127 return 0;
128}
129
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int verify_newsa_info(struct xfrm_usersa_info *p,
132 struct rtattr **xfrma)
133{
134 int err;
135
136 err = -EINVAL;
137 switch (p->family) {
138 case AF_INET:
139 break;
140
141 case AF_INET6:
142#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
143 break;
144#else
145 err = -EAFNOSUPPORT;
146 goto out;
147#endif
148
149 default:
150 goto out;
151 };
152
153 err = -EINVAL;
154 switch (p->id.proto) {
155 case IPPROTO_AH:
156 if (!xfrma[XFRMA_ALG_AUTH-1] ||
157 xfrma[XFRMA_ALG_CRYPT-1] ||
158 xfrma[XFRMA_ALG_COMP-1])
159 goto out;
160 break;
161
162 case IPPROTO_ESP:
163 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
164 !xfrma[XFRMA_ALG_CRYPT-1]) ||
165 xfrma[XFRMA_ALG_COMP-1])
166 goto out;
167 break;
168
169 case IPPROTO_COMP:
170 if (!xfrma[XFRMA_ALG_COMP-1] ||
171 xfrma[XFRMA_ALG_AUTH-1] ||
172 xfrma[XFRMA_ALG_CRYPT-1])
173 goto out;
174 break;
175
176 default:
177 goto out;
178 };
179
180 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
181 goto out;
182 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
183 goto out;
184 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
185 goto out;
186 if ((err = verify_encap_tmpl(xfrma)))
187 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800188 if ((err = verify_sec_ctx_len(xfrma)))
189 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700190 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 err = -EINVAL;
194 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700195 case XFRM_MODE_TRANSPORT:
196 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700197 case XFRM_MODE_ROUTEOPTIMIZATION:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199
200 default:
201 goto out;
202 };
203
204 err = 0;
205
206out:
207 return err;
208}
209
210static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
211 struct xfrm_algo_desc *(*get_byname)(char *, int),
212 struct rtattr *u_arg)
213{
214 struct rtattr *rta = u_arg;
215 struct xfrm_algo *p, *ualg;
216 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700217 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!rta)
220 return 0;
221
222 ualg = RTA_DATA(rta);
223
224 algo = get_byname(ualg->alg_name, 1);
225 if (!algo)
226 return -ENOSYS;
227 *props = algo->desc.sadb_alg_id;
228
Herbert Xub9e9dea2005-05-19 12:39:04 -0700229 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
230 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!p)
232 return -ENOMEM;
233
Herbert Xub9e9dea2005-05-19 12:39:04 -0700234 memcpy(p, ualg, len);
Herbert Xu04ff1262006-08-13 08:50:00 +1000235 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 *algpp = p;
237 return 0;
238}
239
240static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
241{
242 struct rtattr *rta = u_arg;
243 struct xfrm_encap_tmpl *p, *uencap;
244
245 if (!rta)
246 return 0;
247
248 uencap = RTA_DATA(rta);
249 p = kmalloc(sizeof(*p), GFP_KERNEL);
250 if (!p)
251 return -ENOMEM;
252
253 memcpy(p, uencap, sizeof(*p));
254 *encapp = p;
255 return 0;
256}
257
Trent Jaegerdf718372005-12-13 23:12:27 -0800258
259static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
260{
261 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
262 int len = 0;
263
264 if (xfrm_ctx) {
265 len += sizeof(struct xfrm_user_sec_ctx);
266 len += xfrm_ctx->ctx_len;
267 }
268 return len;
269}
270
271static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
272{
273 struct xfrm_user_sec_ctx *uctx;
274
275 if (!u_arg)
276 return 0;
277
278 uctx = RTA_DATA(u_arg);
279 return security_xfrm_state_alloc(x, uctx);
280}
281
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700282static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
283{
284 struct rtattr *rta = u_arg;
285 xfrm_address_t *p, *uaddrp;
286
287 if (!rta)
288 return 0;
289
290 uaddrp = RTA_DATA(rta);
291 p = kmalloc(sizeof(*p), GFP_KERNEL);
292 if (!p)
293 return -ENOMEM;
294
295 memcpy(p, uaddrp, sizeof(*p));
296 *addrpp = p;
297 return 0;
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
301{
302 memcpy(&x->id, &p->id, sizeof(x->id));
303 memcpy(&x->sel, &p->sel, sizeof(x->sel));
304 memcpy(&x->lft, &p->lft, sizeof(x->lft));
305 x->props.mode = p->mode;
306 x->props.replay_window = p->replay_window;
307 x->props.reqid = p->reqid;
308 x->props.family = p->family;
309 x->props.saddr = p->saddr;
310 x->props.flags = p->flags;
311}
312
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800313/*
314 * someday when pfkey also has support, we could have the code
315 * somehow made shareable and move it to xfrm_state.c - JHS
316 *
317*/
318static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
319{
320 int err = - EINVAL;
321 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
322 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
323 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
324 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
325
326 if (rp) {
327 struct xfrm_replay_state *replay;
328 if (RTA_PAYLOAD(rp) < sizeof(*replay))
329 goto error;
330 replay = RTA_DATA(rp);
331 memcpy(&x->replay, replay, sizeof(*replay));
332 memcpy(&x->preplay, replay, sizeof(*replay));
333 }
334
335 if (lt) {
336 struct xfrm_lifetime_cur *ltime;
337 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
338 goto error;
339 ltime = RTA_DATA(lt);
340 x->curlft.bytes = ltime->bytes;
341 x->curlft.packets = ltime->packets;
342 x->curlft.add_time = ltime->add_time;
343 x->curlft.use_time = ltime->use_time;
344 }
345
346 if (et) {
347 if (RTA_PAYLOAD(et) < sizeof(u32))
348 goto error;
349 x->replay_maxage = *(u32*)RTA_DATA(et);
350 }
351
352 if (rt) {
353 if (RTA_PAYLOAD(rt) < sizeof(u32))
354 goto error;
355 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
356 }
357
358 return 0;
359error:
360 return err;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
364 struct rtattr **xfrma,
365 int *errp)
366{
367 struct xfrm_state *x = xfrm_state_alloc();
368 int err = -ENOMEM;
369
370 if (!x)
371 goto error_no_put;
372
373 copy_from_user_state(x, p);
374
375 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
376 xfrm_aalg_get_byname,
377 xfrma[XFRMA_ALG_AUTH-1])))
378 goto error;
379 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
380 xfrm_ealg_get_byname,
381 xfrma[XFRMA_ALG_CRYPT-1])))
382 goto error;
383 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
384 xfrm_calg_get_byname,
385 xfrma[XFRMA_ALG_COMP-1])))
386 goto error;
387 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
388 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700389 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
390 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700391 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (err)
393 goto error;
394
Trent Jaegerdf718372005-12-13 23:12:27 -0800395 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
396 goto error;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800399 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
400 /* sysctl_xfrm_aevent_etime is in 100ms units */
401 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
402 x->preplay.bitmap = 0;
403 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
404 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
405
406 /* override default values from above */
407
408 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
409 if (err < 0)
410 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return x;
413
414error:
415 x->km.state = XFRM_STATE_DEAD;
416 xfrm_state_put(x);
417error_no_put:
418 *errp = err;
419 return NULL;
420}
421
422static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
423{
424 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
425 struct xfrm_state *x;
426 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700427 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Trent Jaegerdf718372005-12-13 23:12:27 -0800429 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (err)
431 return err;
432
Trent Jaegerdf718372005-12-13 23:12:27 -0800433 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!x)
435 return err;
436
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700437 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
439 err = xfrm_state_add(x);
440 else
441 err = xfrm_state_update(x);
442
443 if (err < 0) {
444 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800445 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700449 c.seq = nlh->nlmsg_seq;
450 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700451 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700452
453 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700454out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700455 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return err;
457}
458
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700459static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
460 struct rtattr **xfrma,
461 int *errp)
462{
463 struct xfrm_state *x = NULL;
464 int err;
465
466 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
467 err = -ESRCH;
468 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
469 } else {
470 xfrm_address_t *saddr = NULL;
471
472 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
473 if (err)
474 goto out;
475
476 if (!saddr) {
477 err = -EINVAL;
478 goto out;
479 }
480
481 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
482 p->family);
483 }
484
485 out:
486 if (!x && errp)
487 *errp = err;
488 return x;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
492{
493 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700494 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700495 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
497
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700498 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David S. Miller6f68dc32006-06-08 23:58:52 -0700502 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700503 goto out;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700506 err = -EPERM;
507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700510 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700511 if (err < 0)
512 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700513
514 c.seq = nlh->nlmsg_seq;
515 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700516 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700517 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700519out:
520 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
525{
526 memcpy(&p->id, &x->id, sizeof(p->id));
527 memcpy(&p->sel, &x->sel, sizeof(p->sel));
528 memcpy(&p->lft, &x->lft, sizeof(p->lft));
529 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
530 memcpy(&p->stats, &x->stats, sizeof(p->stats));
531 p->saddr = x->props.saddr;
532 p->mode = x->props.mode;
533 p->replay_window = x->props.replay_window;
534 p->reqid = x->props.reqid;
535 p->family = x->props.family;
536 p->flags = x->props.flags;
537 p->seq = x->km.seq;
538}
539
540struct xfrm_dump_info {
541 struct sk_buff *in_skb;
542 struct sk_buff *out_skb;
543 u32 nlmsg_seq;
544 u16 nlmsg_flags;
545 int start_idx;
546 int this_idx;
547};
548
549static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
550{
551 struct xfrm_dump_info *sp = ptr;
552 struct sk_buff *in_skb = sp->in_skb;
553 struct sk_buff *skb = sp->out_skb;
554 struct xfrm_usersa_info *p;
555 struct nlmsghdr *nlh;
556 unsigned char *b = skb->tail;
557
558 if (sp->this_idx < sp->start_idx)
559 goto out;
560
561 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
562 sp->nlmsg_seq,
563 XFRM_MSG_NEWSA, sizeof(*p));
564 nlh->nlmsg_flags = sp->nlmsg_flags;
565
566 p = NLMSG_DATA(nlh);
567 copy_to_user_state(x, p);
568
569 if (x->aalg)
570 RTA_PUT(skb, XFRMA_ALG_AUTH,
571 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
572 if (x->ealg)
573 RTA_PUT(skb, XFRMA_ALG_CRYPT,
574 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
575 if (x->calg)
576 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
577
578 if (x->encap)
579 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
580
Trent Jaegerdf718372005-12-13 23:12:27 -0800581 if (x->security) {
582 int ctx_size = sizeof(struct xfrm_sec_ctx) +
583 x->security->ctx_len;
584 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
585 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
586
587 uctx->exttype = XFRMA_SEC_CTX;
588 uctx->len = ctx_size;
589 uctx->ctx_doi = x->security->ctx_doi;
590 uctx->ctx_alg = x->security->ctx_alg;
591 uctx->ctx_len = x->security->ctx_len;
592 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
593 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700594
595 if (x->coaddr)
596 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 nlh->nlmsg_len = skb->tail - b;
599out:
600 sp->this_idx++;
601 return 0;
602
603nlmsg_failure:
604rtattr_failure:
605 skb_trim(skb, b - skb->data);
606 return -1;
607}
608
609static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
610{
611 struct xfrm_dump_info info;
612
613 info.in_skb = cb->skb;
614 info.out_skb = skb;
615 info.nlmsg_seq = cb->nlh->nlmsg_seq;
616 info.nlmsg_flags = NLM_F_MULTI;
617 info.this_idx = 0;
618 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700619 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 cb->args[0] = info.this_idx;
621
622 return skb->len;
623}
624
625static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
626 struct xfrm_state *x, u32 seq)
627{
628 struct xfrm_dump_info info;
629 struct sk_buff *skb;
630
631 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
632 if (!skb)
633 return ERR_PTR(-ENOMEM);
634
635 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
636 info.in_skb = in_skb;
637 info.out_skb = skb;
638 info.nlmsg_seq = seq;
639 info.nlmsg_flags = 0;
640 info.this_idx = info.start_idx = 0;
641
642 if (dump_one_state(x, 0, &info)) {
643 kfree_skb(skb);
644 return NULL;
645 }
646
647 return skb;
648}
649
650static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
651{
652 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
653 struct xfrm_state *x;
654 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700655 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700657 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (x == NULL)
659 goto out_noput;
660
661 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
662 if (IS_ERR(resp_skb)) {
663 err = PTR_ERR(resp_skb);
664 } else {
665 err = netlink_unicast(xfrm_nl, resp_skb,
666 NETLINK_CB(skb).pid, MSG_DONTWAIT);
667 }
668 xfrm_state_put(x);
669out_noput:
670 return err;
671}
672
673static int verify_userspi_info(struct xfrm_userspi_info *p)
674{
675 switch (p->info.id.proto) {
676 case IPPROTO_AH:
677 case IPPROTO_ESP:
678 break;
679
680 case IPPROTO_COMP:
681 /* IPCOMP spi is 16-bits. */
682 if (p->max >= 0x10000)
683 return -EINVAL;
684 break;
685
686 default:
687 return -EINVAL;
688 };
689
690 if (p->min > p->max)
691 return -EINVAL;
692
693 return 0;
694}
695
696static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
697{
698 struct xfrm_state *x;
699 struct xfrm_userspi_info *p;
700 struct sk_buff *resp_skb;
701 xfrm_address_t *daddr;
702 int family;
703 int err;
704
705 p = NLMSG_DATA(nlh);
706 err = verify_userspi_info(p);
707 if (err)
708 goto out_noput;
709
710 family = p->info.family;
711 daddr = &p->info.id.daddr;
712
713 x = NULL;
714 if (p->info.seq) {
715 x = xfrm_find_acq_byseq(p->info.seq);
716 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
717 xfrm_state_put(x);
718 x = NULL;
719 }
720 }
721
722 if (!x)
723 x = xfrm_find_acq(p->info.mode, p->info.reqid,
724 p->info.id.proto, daddr,
725 &p->info.saddr, 1,
726 family);
727 err = -ENOENT;
728 if (x == NULL)
729 goto out_noput;
730
731 resp_skb = ERR_PTR(-ENOENT);
732
733 spin_lock_bh(&x->lock);
734 if (x->km.state != XFRM_STATE_DEAD) {
735 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
736 if (x->id.spi)
737 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
738 }
739 spin_unlock_bh(&x->lock);
740
741 if (IS_ERR(resp_skb)) {
742 err = PTR_ERR(resp_skb);
743 goto out;
744 }
745
746 err = netlink_unicast(xfrm_nl, resp_skb,
747 NETLINK_CB(skb).pid, MSG_DONTWAIT);
748
749out:
750 xfrm_state_put(x);
751out_noput:
752 return err;
753}
754
755static int verify_policy_dir(__u8 dir)
756{
757 switch (dir) {
758 case XFRM_POLICY_IN:
759 case XFRM_POLICY_OUT:
760 case XFRM_POLICY_FWD:
761 break;
762
763 default:
764 return -EINVAL;
765 };
766
767 return 0;
768}
769
770static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
771{
772 switch (p->share) {
773 case XFRM_SHARE_ANY:
774 case XFRM_SHARE_SESSION:
775 case XFRM_SHARE_USER:
776 case XFRM_SHARE_UNIQUE:
777 break;
778
779 default:
780 return -EINVAL;
781 };
782
783 switch (p->action) {
784 case XFRM_POLICY_ALLOW:
785 case XFRM_POLICY_BLOCK:
786 break;
787
788 default:
789 return -EINVAL;
790 };
791
792 switch (p->sel.family) {
793 case AF_INET:
794 break;
795
796 case AF_INET6:
797#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
798 break;
799#else
800 return -EAFNOSUPPORT;
801#endif
802
803 default:
804 return -EINVAL;
805 };
806
807 return verify_policy_dir(p->dir);
808}
809
Trent Jaegerdf718372005-12-13 23:12:27 -0800810static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
811{
812 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
813 struct xfrm_user_sec_ctx *uctx;
814
815 if (!rt)
816 return 0;
817
818 uctx = RTA_DATA(rt);
819 return security_xfrm_policy_alloc(pol, uctx);
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
823 int nr)
824{
825 int i;
826
827 xp->xfrm_nr = nr;
828 for (i = 0; i < nr; i++, ut++) {
829 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
830
831 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
832 memcpy(&t->saddr, &ut->saddr,
833 sizeof(xfrm_address_t));
834 t->reqid = ut->reqid;
835 t->mode = ut->mode;
836 t->share = ut->share;
837 t->optional = ut->optional;
838 t->aalgos = ut->aalgos;
839 t->ealgos = ut->ealgos;
840 t->calgos = ut->calgos;
841 }
842}
843
844static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
845{
846 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
847 struct xfrm_user_tmpl *utmpl;
848 int nr;
849
850 if (!rt) {
851 pol->xfrm_nr = 0;
852 } else {
853 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
854
855 if (nr > XFRM_MAX_DEPTH)
856 return -EINVAL;
857
858 copy_templates(pol, RTA_DATA(rt), nr);
859 }
860 return 0;
861}
862
863static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
864{
865 xp->priority = p->priority;
866 xp->index = p->index;
867 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
868 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
869 xp->action = p->action;
870 xp->flags = p->flags;
871 xp->family = p->sel.family;
872 /* XXX xp->share = p->share; */
873}
874
875static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
876{
877 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
878 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
879 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
880 p->priority = xp->priority;
881 p->index = xp->index;
882 p->sel.family = xp->family;
883 p->dir = dir;
884 p->action = xp->action;
885 p->flags = xp->flags;
886 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
887}
888
889static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
890{
891 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
892 int err;
893
894 if (!xp) {
895 *errp = -ENOMEM;
896 return NULL;
897 }
898
899 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800900
901 if (!(err = copy_from_user_tmpl(xp, xfrma)))
902 err = copy_from_user_sec_ctx(xp, xfrma);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (err) {
905 *errp = err;
906 kfree(xp);
907 xp = NULL;
908 }
909
910 return xp;
911}
912
913static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
914{
915 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
916 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700917 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int err;
919 int excl;
920
921 err = verify_newpolicy_info(p);
922 if (err)
923 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800924 err = verify_sec_ctx_len((struct rtattr **)xfrma);
925 if (err)
926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Trent Jaegerdf718372005-12-13 23:12:27 -0800928 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!xp)
930 return err;
931
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700932 /* shouldnt excl be based on nlh flags??
933 * Aha! this is anti-netlink really i.e more pfkey derived
934 * in netlink excl is a flag and you wouldnt need
935 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
937 err = xfrm_policy_insert(p->dir, xp, excl);
938 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -0800939 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 kfree(xp);
941 return err;
942 }
943
Herbert Xuf60f6b82005-06-18 22:44:37 -0700944 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700945 c.seq = nlh->nlmsg_seq;
946 c.pid = nlh->nlmsg_pid;
947 km_policy_notify(xp, p->dir, &c);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 xfrm_pol_put(xp);
950
951 return 0;
952}
953
954static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
955{
956 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
957 int i;
958
959 if (xp->xfrm_nr == 0)
960 return 0;
961
962 for (i = 0; i < xp->xfrm_nr; i++) {
963 struct xfrm_user_tmpl *up = &vec[i];
964 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
965
966 memcpy(&up->id, &kp->id, sizeof(up->id));
967 up->family = xp->family;
968 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
969 up->reqid = kp->reqid;
970 up->mode = kp->mode;
971 up->share = kp->share;
972 up->optional = kp->optional;
973 up->aalgos = kp->aalgos;
974 up->ealgos = kp->ealgos;
975 up->calgos = kp->calgos;
976 }
977 RTA_PUT(skb, XFRMA_TMPL,
978 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
979 vec);
980
981 return 0;
982
983rtattr_failure:
984 return -1;
985}
986
Serge Hallyn0d681622006-07-24 23:30:44 -0700987static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -0800988{
Serge Hallyn0d681622006-07-24 23:30:44 -0700989 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
990 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
991 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800992
Serge Hallyn0d681622006-07-24 23:30:44 -0700993 uctx->exttype = XFRMA_SEC_CTX;
994 uctx->len = ctx_size;
995 uctx->ctx_doi = s->ctx_doi;
996 uctx->ctx_alg = s->ctx_alg;
997 uctx->ctx_len = s->ctx_len;
998 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
999 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001000
1001 rtattr_failure:
1002 return -1;
1003}
1004
Serge Hallyn0d681622006-07-24 23:30:44 -07001005static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1006{
1007 if (x->security) {
1008 return copy_sec_ctx(x->security, skb);
1009 }
1010 return 0;
1011}
1012
1013static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1014{
1015 if (xp->security) {
1016 return copy_sec_ctx(xp->security, skb);
1017 }
1018 return 0;
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1022{
1023 struct xfrm_dump_info *sp = ptr;
1024 struct xfrm_userpolicy_info *p;
1025 struct sk_buff *in_skb = sp->in_skb;
1026 struct sk_buff *skb = sp->out_skb;
1027 struct nlmsghdr *nlh;
1028 unsigned char *b = skb->tail;
1029
1030 if (sp->this_idx < sp->start_idx)
1031 goto out;
1032
1033 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1034 sp->nlmsg_seq,
1035 XFRM_MSG_NEWPOLICY, sizeof(*p));
1036 p = NLMSG_DATA(nlh);
1037 nlh->nlmsg_flags = sp->nlmsg_flags;
1038
1039 copy_to_user_policy(xp, p, dir);
1040 if (copy_to_user_tmpl(xp, skb) < 0)
1041 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001042 if (copy_to_user_sec_ctx(xp, skb))
1043 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 nlh->nlmsg_len = skb->tail - b;
1046out:
1047 sp->this_idx++;
1048 return 0;
1049
1050nlmsg_failure:
1051 skb_trim(skb, b - skb->data);
1052 return -1;
1053}
1054
1055static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1056{
1057 struct xfrm_dump_info info;
1058
1059 info.in_skb = cb->skb;
1060 info.out_skb = skb;
1061 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1062 info.nlmsg_flags = NLM_F_MULTI;
1063 info.this_idx = 0;
1064 info.start_idx = cb->args[0];
1065 (void) xfrm_policy_walk(dump_one_policy, &info);
1066 cb->args[0] = info.this_idx;
1067
1068 return skb->len;
1069}
1070
1071static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1072 struct xfrm_policy *xp,
1073 int dir, u32 seq)
1074{
1075 struct xfrm_dump_info info;
1076 struct sk_buff *skb;
1077
1078 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1079 if (!skb)
1080 return ERR_PTR(-ENOMEM);
1081
1082 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1083 info.in_skb = in_skb;
1084 info.out_skb = skb;
1085 info.nlmsg_seq = seq;
1086 info.nlmsg_flags = 0;
1087 info.this_idx = info.start_idx = 0;
1088
1089 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1090 kfree_skb(skb);
1091 return NULL;
1092 }
1093
1094 return skb;
1095}
1096
1097static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1098{
1099 struct xfrm_policy *xp;
1100 struct xfrm_userpolicy_id *p;
1101 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001102 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int delete;
1104
1105 p = NLMSG_DATA(nlh);
1106 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1107
1108 err = verify_policy_dir(p->dir);
1109 if (err)
1110 return err;
1111
1112 if (p->index)
1113 xp = xfrm_policy_byid(p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001114 else {
1115 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1116 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1117 struct xfrm_policy tmp;
1118
1119 err = verify_sec_ctx_len(rtattrs);
1120 if (err)
1121 return err;
1122
1123 memset(&tmp, 0, sizeof(struct xfrm_policy));
1124 if (rt) {
1125 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1126
1127 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1128 return err;
1129 }
1130 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1131 security_xfrm_policy_free(&tmp);
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (xp == NULL)
1134 return -ENOENT;
1135
1136 if (!delete) {
1137 struct sk_buff *resp_skb;
1138
1139 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1140 if (IS_ERR(resp_skb)) {
1141 err = PTR_ERR(resp_skb);
1142 } else {
1143 err = netlink_unicast(xfrm_nl, resp_skb,
1144 NETLINK_CB(skb).pid,
1145 MSG_DONTWAIT);
1146 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001147 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001148 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001149 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001150 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001151 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001152 c.seq = nlh->nlmsg_seq;
1153 c.pid = nlh->nlmsg_pid;
1154 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 xfrm_pol_put(xp);
1158
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001159out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return err;
1161}
1162
1163static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1164{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001165 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1167
1168 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001169 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001170 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001171 c.seq = nlh->nlmsg_seq;
1172 c.pid = nlh->nlmsg_pid;
1173 km_state_notify(NULL, &c);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return 0;
1176}
1177
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001178
1179static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1180{
1181 struct xfrm_aevent_id *id;
1182 struct nlmsghdr *nlh;
1183 struct xfrm_lifetime_cur ltime;
1184 unsigned char *b = skb->tail;
1185
1186 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1187 id = NLMSG_DATA(nlh);
1188 nlh->nlmsg_flags = 0;
1189
1190 id->sa_id.daddr = x->id.daddr;
1191 id->sa_id.spi = x->id.spi;
1192 id->sa_id.family = x->props.family;
1193 id->sa_id.proto = x->id.proto;
1194 id->flags = c->data.aevent;
1195
1196 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1197
1198 ltime.bytes = x->curlft.bytes;
1199 ltime.packets = x->curlft.packets;
1200 ltime.add_time = x->curlft.add_time;
1201 ltime.use_time = x->curlft.use_time;
1202
1203 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1204
1205 if (id->flags&XFRM_AE_RTHR) {
1206 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1207 }
1208
1209 if (id->flags&XFRM_AE_ETHR) {
1210 u32 etimer = x->replay_maxage*10/HZ;
1211 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1212 }
1213
1214 nlh->nlmsg_len = skb->tail - b;
1215 return skb->len;
1216
1217rtattr_failure:
1218nlmsg_failure:
1219 skb_trim(skb, b - skb->data);
1220 return -1;
1221}
1222
1223static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1224{
1225 struct xfrm_state *x;
1226 struct sk_buff *r_skb;
1227 int err;
1228 struct km_event c;
1229 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1230 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1231 struct xfrm_usersa_id *id = &p->sa_id;
1232
1233 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1234 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1235
1236 if (p->flags&XFRM_AE_RTHR)
1237 len+=RTA_SPACE(sizeof(u32));
1238
1239 if (p->flags&XFRM_AE_ETHR)
1240 len+=RTA_SPACE(sizeof(u32));
1241
1242 r_skb = alloc_skb(len, GFP_ATOMIC);
1243 if (r_skb == NULL)
1244 return -ENOMEM;
1245
1246 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1247 if (x == NULL) {
1248 kfree(r_skb);
1249 return -ESRCH;
1250 }
1251
1252 /*
1253 * XXX: is this lock really needed - none of the other
1254 * gets lock (the concern is things getting updated
1255 * while we are still reading) - jhs
1256 */
1257 spin_lock_bh(&x->lock);
1258 c.data.aevent = p->flags;
1259 c.seq = nlh->nlmsg_seq;
1260 c.pid = nlh->nlmsg_pid;
1261
1262 if (build_aevent(r_skb, x, &c) < 0)
1263 BUG();
1264 err = netlink_unicast(xfrm_nl, r_skb,
1265 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1266 spin_unlock_bh(&x->lock);
1267 xfrm_state_put(x);
1268 return err;
1269}
1270
1271static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1272{
1273 struct xfrm_state *x;
1274 struct km_event c;
1275 int err = - EINVAL;
1276 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1277 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1278 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1279
1280 if (!lt && !rp)
1281 return err;
1282
1283 /* pedantic mode - thou shalt sayeth replaceth */
1284 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1285 return err;
1286
1287 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1288 if (x == NULL)
1289 return -ESRCH;
1290
1291 if (x->km.state != XFRM_STATE_VALID)
1292 goto out;
1293
1294 spin_lock_bh(&x->lock);
1295 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1296 spin_unlock_bh(&x->lock);
1297 if (err < 0)
1298 goto out;
1299
1300 c.event = nlh->nlmsg_type;
1301 c.seq = nlh->nlmsg_seq;
1302 c.pid = nlh->nlmsg_pid;
1303 c.data.aevent = XFRM_AE_CU;
1304 km_state_notify(x, &c);
1305 err = 0;
1306out:
1307 xfrm_state_put(x);
1308 return err;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1312{
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001313struct km_event c;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 xfrm_policy_flush();
Herbert Xuf60f6b82005-06-18 22:44:37 -07001316 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001317 c.seq = nlh->nlmsg_seq;
1318 c.pid = nlh->nlmsg_pid;
1319 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321}
1322
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001323static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1324{
1325 struct xfrm_policy *xp;
1326 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1327 struct xfrm_userpolicy_info *p = &up->pol;
1328 int err = -ENOENT;
1329
1330 if (p->index)
1331 xp = xfrm_policy_byid(p->dir, p->index, 0);
1332 else {
1333 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1334 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1335 struct xfrm_policy tmp;
1336
1337 err = verify_sec_ctx_len(rtattrs);
1338 if (err)
1339 return err;
1340
1341 memset(&tmp, 0, sizeof(struct xfrm_policy));
1342 if (rt) {
1343 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1344
1345 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1346 return err;
1347 }
1348 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
1349 security_xfrm_policy_free(&tmp);
1350 }
1351
1352 if (xp == NULL)
1353 return err;
1354 read_lock(&xp->lock);
1355 if (xp->dead) {
1356 read_unlock(&xp->lock);
1357 goto out;
1358 }
1359
1360 read_unlock(&xp->lock);
1361 err = 0;
1362 if (up->hard) {
1363 xfrm_policy_delete(xp, p->dir);
1364 } else {
1365 // reset the timers here?
1366 printk("Dont know what to do with soft policy expire\n");
1367 }
1368 km_policy_expired(xp, p->dir, up->hard, current->pid);
1369
1370out:
1371 xfrm_pol_put(xp);
1372 return err;
1373}
1374
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001375static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1376{
1377 struct xfrm_state *x;
1378 int err;
1379 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1380 struct xfrm_usersa_info *p = &ue->state;
1381
1382 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1383 err = -ENOENT;
1384
1385 if (x == NULL)
1386 return err;
1387
1388 err = -EINVAL;
1389
1390 spin_lock_bh(&x->lock);
1391 if (x->km.state != XFRM_STATE_VALID)
1392 goto out;
1393 km_state_expired(x, ue->hard, current->pid);
1394
1395 if (ue->hard)
1396 __xfrm_state_delete(x);
1397out:
1398 spin_unlock_bh(&x->lock);
1399 xfrm_state_put(x);
1400 return err;
1401}
1402
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001403static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1404{
1405 struct xfrm_policy *xp;
1406 struct xfrm_user_tmpl *ut;
1407 int i;
1408 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1409
1410 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1411 struct xfrm_state *x = xfrm_state_alloc();
1412 int err = -ENOMEM;
1413
1414 if (!x)
1415 return err;
1416
1417 err = verify_newpolicy_info(&ua->policy);
1418 if (err) {
1419 printk("BAD policy passed\n");
1420 kfree(x);
1421 return err;
1422 }
1423
1424 /* build an XP */
1425 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1426 kfree(x);
1427 return err;
1428 }
1429
1430 memcpy(&x->id, &ua->id, sizeof(ua->id));
1431 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1432 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1433
1434 ut = RTA_DATA(rt);
1435 /* extract the templates and for each call km_key */
1436 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1437 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1438 memcpy(&x->id, &t->id, sizeof(x->id));
1439 x->props.mode = t->mode;
1440 x->props.reqid = t->reqid;
1441 x->props.family = ut->family;
1442 t->aalgos = ua->aalgos;
1443 t->ealgos = ua->ealgos;
1444 t->calgos = ua->calgos;
1445 err = km_query(x, t, xp);
1446
1447 }
1448
1449 kfree(x);
1450 kfree(xp);
1451
1452 return 0;
1453}
1454
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455
Thomas Graf492b5582005-05-03 14:26:40 -07001456#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1457
1458static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1459 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1460 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1461 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1462 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1463 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1464 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1465 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001466 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001467 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001468 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1469 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001470 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001471 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1472 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001473 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1474 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475};
1476
Thomas Graf492b5582005-05-03 14:26:40 -07001477#undef XMSGSIZE
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static struct xfrm_link {
1480 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1481 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001482} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1483 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1484 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1485 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1486 .dump = xfrm_dump_sa },
1487 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1488 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1489 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1490 .dump = xfrm_dump_policy },
1491 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001492 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001493 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001494 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1495 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001496 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001497 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1498 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001499 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1500 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501};
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1504{
1505 struct rtattr *xfrma[XFRMA_MAX];
1506 struct xfrm_link *link;
1507 int type, min_len;
1508
1509 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1510 return 0;
1511
1512 type = nlh->nlmsg_type;
1513
1514 /* A control message: ignore them */
1515 if (type < XFRM_MSG_BASE)
1516 return 0;
1517
1518 /* Unknown message: reply with EINVAL */
1519 if (type > XFRM_MSG_MAX)
1520 goto err_einval;
1521
1522 type -= XFRM_MSG_BASE;
1523 link = &xfrm_dispatch[type];
1524
1525 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001526 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *errp = -EPERM;
1528 return -1;
1529 }
1530
Thomas Graf492b5582005-05-03 14:26:40 -07001531 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1532 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1533 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (link->dump == NULL)
1535 goto err_einval;
1536
1537 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001538 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return -1;
1540 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001541
1542 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return -1;
1544 }
1545
1546 memset(xfrma, 0, sizeof(xfrma));
1547
1548 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1549 goto err_einval;
1550
1551 if (nlh->nlmsg_len > min_len) {
1552 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1553 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1554
1555 while (RTA_OK(attr, attrlen)) {
1556 unsigned short flavor = attr->rta_type;
1557 if (flavor) {
1558 if (flavor > XFRMA_MAX)
1559 goto err_einval;
1560 xfrma[flavor - 1] = attr;
1561 }
1562 attr = RTA_NEXT(attr, attrlen);
1563 }
1564 }
1565
1566 if (link->doit == NULL)
1567 goto err_einval;
1568 *errp = link->doit(skb, nlh, (void **) &xfrma);
1569
1570 return *errp;
1571
1572err_einval:
1573 *errp = -EINVAL;
1574 return -1;
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static void xfrm_netlink_rcv(struct sock *sk, int len)
1578{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001579 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001582 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001583 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001584 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001586 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001589static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct xfrm_user_expire *ue;
1592 struct nlmsghdr *nlh;
1593 unsigned char *b = skb->tail;
1594
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001595 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 sizeof(*ue));
1597 ue = NLMSG_DATA(nlh);
1598 nlh->nlmsg_flags = 0;
1599
1600 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001601 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 nlh->nlmsg_len = skb->tail - b;
1604 return skb->len;
1605
1606nlmsg_failure:
1607 skb_trim(skb, b - skb->data);
1608 return -1;
1609}
1610
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001611static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001614 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001616 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (skb == NULL)
1618 return -ENOMEM;
1619
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001620 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 BUG();
1622
Patrick McHardyac6d4392005-08-14 19:29:52 -07001623 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1624 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001627static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1628{
1629 struct sk_buff *skb;
1630 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1631
1632 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1633 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1634 skb = alloc_skb(len, GFP_ATOMIC);
1635 if (skb == NULL)
1636 return -ENOMEM;
1637
1638 if (build_aevent(skb, x, c) < 0)
1639 BUG();
1640
1641 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1642 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1643}
1644
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001645static int xfrm_notify_sa_flush(struct km_event *c)
1646{
1647 struct xfrm_usersa_flush *p;
1648 struct nlmsghdr *nlh;
1649 struct sk_buff *skb;
1650 unsigned char *b;
1651 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1652
1653 skb = alloc_skb(len, GFP_ATOMIC);
1654 if (skb == NULL)
1655 return -ENOMEM;
1656 b = skb->tail;
1657
1658 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1659 XFRM_MSG_FLUSHSA, sizeof(*p));
1660 nlh->nlmsg_flags = 0;
1661
1662 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001663 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664
1665 nlh->nlmsg_len = skb->tail - b;
1666
Patrick McHardyac6d4392005-08-14 19:29:52 -07001667 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1668 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001669
1670nlmsg_failure:
1671 kfree_skb(skb);
1672 return -1;
1673}
1674
1675static int inline xfrm_sa_len(struct xfrm_state *x)
1676{
Herbert Xu0603eac2005-06-18 22:54:36 -07001677 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001678 if (x->aalg)
1679 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1680 if (x->ealg)
1681 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1682 if (x->calg)
1683 l += RTA_SPACE(sizeof(*x->calg));
1684 if (x->encap)
1685 l += RTA_SPACE(sizeof(*x->encap));
1686
1687 return l;
1688}
1689
1690static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1691{
1692 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001693 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001694 struct nlmsghdr *nlh;
1695 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 unsigned char *b;
1697 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001698 int headlen;
1699
1700 headlen = sizeof(*p);
1701 if (c->event == XFRM_MSG_DELSA) {
1702 len += RTA_SPACE(headlen);
1703 headlen = sizeof(*id);
1704 }
1705 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001706
1707 skb = alloc_skb(len, GFP_ATOMIC);
1708 if (skb == NULL)
1709 return -ENOMEM;
1710 b = skb->tail;
1711
Herbert Xu0603eac2005-06-18 22:54:36 -07001712 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713 nlh->nlmsg_flags = 0;
1714
1715 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001716 if (c->event == XFRM_MSG_DELSA) {
1717 id = NLMSG_DATA(nlh);
1718 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1719 id->spi = x->id.spi;
1720 id->family = x->props.family;
1721 id->proto = x->id.proto;
1722
1723 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1724 }
1725
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001726 copy_to_user_state(x, p);
1727
1728 if (x->aalg)
1729 RTA_PUT(skb, XFRMA_ALG_AUTH,
1730 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1731 if (x->ealg)
1732 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1733 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1734 if (x->calg)
1735 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1736
1737 if (x->encap)
1738 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1739
1740 nlh->nlmsg_len = skb->tail - b;
1741
Patrick McHardyac6d4392005-08-14 19:29:52 -07001742 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1743 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744
1745nlmsg_failure:
1746rtattr_failure:
1747 kfree_skb(skb);
1748 return -1;
1749}
1750
1751static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1752{
1753
1754 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001755 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001756 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001757 case XFRM_MSG_NEWAE:
1758 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001759 case XFRM_MSG_DELSA:
1760 case XFRM_MSG_UPDSA:
1761 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001762 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001763 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001764 return xfrm_notify_sa_flush(c);
1765 default:
1766 printk("xfrm_user: Unknown SA event %d\n", c->event);
1767 break;
1768 }
1769
1770 return 0;
1771
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1775 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1776 int dir)
1777{
1778 struct xfrm_user_acquire *ua;
1779 struct nlmsghdr *nlh;
1780 unsigned char *b = skb->tail;
1781 __u32 seq = xfrm_get_acqseq();
1782
1783 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1784 sizeof(*ua));
1785 ua = NLMSG_DATA(nlh);
1786 nlh->nlmsg_flags = 0;
1787
1788 memcpy(&ua->id, &x->id, sizeof(ua->id));
1789 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1790 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1791 copy_to_user_policy(xp, &ua->policy, dir);
1792 ua->aalgos = xt->aalgos;
1793 ua->ealgos = xt->ealgos;
1794 ua->calgos = xt->calgos;
1795 ua->seq = x->km.seq = seq;
1796
1797 if (copy_to_user_tmpl(xp, skb) < 0)
1798 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07001799 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08001800 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 nlh->nlmsg_len = skb->tail - b;
1803 return skb->len;
1804
1805nlmsg_failure:
1806 skb_trim(skb, b - skb->data);
1807 return -1;
1808}
1809
1810static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1811 struct xfrm_policy *xp, int dir)
1812{
1813 struct sk_buff *skb;
1814 size_t len;
1815
1816 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1817 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001818 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 skb = alloc_skb(len, GFP_ATOMIC);
1820 if (skb == NULL)
1821 return -ENOMEM;
1822
1823 if (build_acquire(skb, x, xt, xp, dir) < 0)
1824 BUG();
1825
Patrick McHardyac6d4392005-08-14 19:29:52 -07001826 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1827 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830/* User gives us xfrm_user_policy_info followed by an array of 0
1831 * or more templates.
1832 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001833static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 u8 *data, int len, int *dir)
1835{
1836 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1837 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1838 struct xfrm_policy *xp;
1839 int nr;
1840
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001841 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 case AF_INET:
1843 if (opt != IP_XFRM_POLICY) {
1844 *dir = -EOPNOTSUPP;
1845 return NULL;
1846 }
1847 break;
1848#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1849 case AF_INET6:
1850 if (opt != IPV6_XFRM_POLICY) {
1851 *dir = -EOPNOTSUPP;
1852 return NULL;
1853 }
1854 break;
1855#endif
1856 default:
1857 *dir = -EINVAL;
1858 return NULL;
1859 }
1860
1861 *dir = -EINVAL;
1862
1863 if (len < sizeof(*p) ||
1864 verify_newpolicy_info(p))
1865 return NULL;
1866
1867 nr = ((len - sizeof(*p)) / sizeof(*ut));
1868 if (nr > XFRM_MAX_DEPTH)
1869 return NULL;
1870
Herbert Xua4f1bac2005-07-26 15:43:17 -07001871 if (p->dir > XFRM_POLICY_OUT)
1872 return NULL;
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 xp = xfrm_policy_alloc(GFP_KERNEL);
1875 if (xp == NULL) {
1876 *dir = -ENOBUFS;
1877 return NULL;
1878 }
1879
1880 copy_from_user_policy(xp, p);
1881 copy_templates(xp, ut, nr);
1882
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001883 if (!xp->security) {
1884 int err = security_xfrm_sock_policy_alloc(xp, sk);
1885 if (err) {
1886 kfree(xp);
1887 *dir = err;
1888 return NULL;
1889 }
1890 }
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 *dir = p->dir;
1893
1894 return xp;
1895}
1896
1897static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001898 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct xfrm_user_polexpire *upe;
1901 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001902 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 unsigned char *b = skb->tail;
1904
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001905 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 upe = NLMSG_DATA(nlh);
1907 nlh->nlmsg_flags = 0;
1908
1909 copy_to_user_policy(xp, &upe->pol, dir);
1910 if (copy_to_user_tmpl(xp, skb) < 0)
1911 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001912 if (copy_to_user_sec_ctx(xp, skb))
1913 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 upe->hard = !!hard;
1915
1916 nlh->nlmsg_len = skb->tail - b;
1917 return skb->len;
1918
1919nlmsg_failure:
1920 skb_trim(skb, b - skb->data);
1921 return -1;
1922}
1923
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001924static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct sk_buff *skb;
1927 size_t len;
1928
1929 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1930 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001931 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 skb = alloc_skb(len, GFP_ATOMIC);
1933 if (skb == NULL)
1934 return -ENOMEM;
1935
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001936 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 BUG();
1938
Patrick McHardyac6d4392005-08-14 19:29:52 -07001939 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1940 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001943static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1944{
1945 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001946 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001947 struct nlmsghdr *nlh;
1948 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001949 unsigned char *b;
1950 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07001951 int headlen;
1952
1953 headlen = sizeof(*p);
1954 if (c->event == XFRM_MSG_DELPOLICY) {
1955 len += RTA_SPACE(headlen);
1956 headlen = sizeof(*id);
1957 }
1958 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001959
1960 skb = alloc_skb(len, GFP_ATOMIC);
1961 if (skb == NULL)
1962 return -ENOMEM;
1963 b = skb->tail;
1964
Herbert Xu0603eac2005-06-18 22:54:36 -07001965 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001966
1967 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001968 if (c->event == XFRM_MSG_DELPOLICY) {
1969 id = NLMSG_DATA(nlh);
1970 memset(id, 0, sizeof(*id));
1971 id->dir = dir;
1972 if (c->data.byid)
1973 id->index = xp->index;
1974 else
1975 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1976
1977 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1978 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001979
1980 nlh->nlmsg_flags = 0;
1981
1982 copy_to_user_policy(xp, p, dir);
1983 if (copy_to_user_tmpl(xp, skb) < 0)
1984 goto nlmsg_failure;
1985
1986 nlh->nlmsg_len = skb->tail - b;
1987
Patrick McHardyac6d4392005-08-14 19:29:52 -07001988 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1989 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001990
1991nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07001992rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001993 kfree_skb(skb);
1994 return -1;
1995}
1996
1997static int xfrm_notify_policy_flush(struct km_event *c)
1998{
1999 struct nlmsghdr *nlh;
2000 struct sk_buff *skb;
2001 unsigned char *b;
2002 int len = NLMSG_LENGTH(0);
2003
2004 skb = alloc_skb(len, GFP_ATOMIC);
2005 if (skb == NULL)
2006 return -ENOMEM;
2007 b = skb->tail;
2008
2009
2010 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
2011
2012 nlh->nlmsg_len = skb->tail - b;
2013
Patrick McHardyac6d4392005-08-14 19:29:52 -07002014 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2015 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002016
2017nlmsg_failure:
2018 kfree_skb(skb);
2019 return -1;
2020}
2021
2022static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2023{
2024
2025 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002026 case XFRM_MSG_NEWPOLICY:
2027 case XFRM_MSG_UPDPOLICY:
2028 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002029 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002030 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002031 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002032 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002033 return xfrm_exp_policy_notify(xp, dir, c);
2034 default:
2035 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2036 }
2037
2038 return 0;
2039
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042static struct xfrm_mgr netlink_mgr = {
2043 .id = "netlink",
2044 .notify = xfrm_send_state_notify,
2045 .acquire = xfrm_send_acquire,
2046 .compile_policy = xfrm_compile_policy,
2047 .notify_policy = xfrm_send_policy_notify,
2048};
2049
2050static int __init xfrm_user_init(void)
2051{
Patrick McHardybe336902006-03-20 22:40:54 -08002052 struct sock *nlsk;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 printk(KERN_INFO "Initializing IPsec netlink socket\n");
2055
Patrick McHardybe336902006-03-20 22:40:54 -08002056 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2057 xfrm_netlink_rcv, THIS_MODULE);
2058 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002060 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 xfrm_register_km(&netlink_mgr);
2063
2064 return 0;
2065}
2066
2067static void __exit xfrm_user_exit(void)
2068{
Patrick McHardybe336902006-03-20 22:40:54 -08002069 struct sock *nlsk = xfrm_nl;
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002072 rcu_assign_pointer(xfrm_nl, NULL);
2073 synchronize_rcu();
2074 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077module_init(xfrm_user_init);
2078module_exit(xfrm_user_exit);
2079MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002080MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002081