blob: 2ee14f8a1908ee6c7d54fa4e74f31a5ef89a23ca [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
36{
37 struct rtattr *rt = xfrma[type - 1];
38 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070039 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (!rt)
42 return 0;
43
Herbert Xu31c26852005-05-19 12:39:49 -070044 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
45 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -EINVAL;
47
48 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070049
50 len -= (algp->alg_key_len + 7U) / 8;
51 if (len < 0)
52 return -EINVAL;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 switch (type) {
55 case XFRMA_ALG_AUTH:
56 if (!algp->alg_key_len &&
57 strcmp(algp->alg_name, "digest_null") != 0)
58 return -EINVAL;
59 break;
60
61 case XFRMA_ALG_CRYPT:
62 if (!algp->alg_key_len &&
63 strcmp(algp->alg_name, "cipher_null") != 0)
64 return -EINVAL;
65 break;
66
67 case XFRMA_ALG_COMP:
68 /* Zero length keys are legal. */
69 break;
70
71 default:
72 return -EINVAL;
73 };
74
75 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
76 return 0;
77}
78
79static int verify_encap_tmpl(struct rtattr **xfrma)
80{
81 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
82 struct xfrm_encap_tmpl *encap;
83
84 if (!rt)
85 return 0;
86
87 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
88 return -EINVAL;
89
90 return 0;
91}
92
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070093static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
95{
96 struct rtattr *rt = xfrma[type - 1];
97
98 if (!rt)
99 return 0;
100
101 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
102 return -EINVAL;
103
104 if (addrp)
105 *addrp = RTA_DATA(rt);
106
107 return 0;
108}
Trent Jaegerdf718372005-12-13 23:12:27 -0800109
110static inline int verify_sec_ctx_len(struct rtattr **xfrma)
111{
112 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
113 struct xfrm_user_sec_ctx *uctx;
114 int len = 0;
115
116 if (!rt)
117 return 0;
118
119 if (rt->rta_len < sizeof(*uctx))
120 return -EINVAL;
121
122 uctx = RTA_DATA(rt);
123
Trent Jaegerdf718372005-12-13 23:12:27 -0800124 len += sizeof(struct xfrm_user_sec_ctx);
125 len += uctx->ctx_len;
126
127 if (uctx->len != len)
128 return -EINVAL;
129
130 return 0;
131}
132
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int verify_newsa_info(struct xfrm_usersa_info *p,
135 struct rtattr **xfrma)
136{
137 int err;
138
139 err = -EINVAL;
140 switch (p->family) {
141 case AF_INET:
142 break;
143
144 case AF_INET6:
145#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
146 break;
147#else
148 err = -EAFNOSUPPORT;
149 goto out;
150#endif
151
152 default:
153 goto out;
154 };
155
156 err = -EINVAL;
157 switch (p->id.proto) {
158 case IPPROTO_AH:
159 if (!xfrma[XFRMA_ALG_AUTH-1] ||
160 xfrma[XFRMA_ALG_CRYPT-1] ||
161 xfrma[XFRMA_ALG_COMP-1])
162 goto out;
163 break;
164
165 case IPPROTO_ESP:
166 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
167 !xfrma[XFRMA_ALG_CRYPT-1]) ||
168 xfrma[XFRMA_ALG_COMP-1])
169 goto out;
170 break;
171
172 case IPPROTO_COMP:
173 if (!xfrma[XFRMA_ALG_COMP-1] ||
174 xfrma[XFRMA_ALG_AUTH-1] ||
175 xfrma[XFRMA_ALG_CRYPT-1])
176 goto out;
177 break;
178
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700179#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
182 if (xfrma[XFRMA_ALG_COMP-1] ||
183 xfrma[XFRMA_ALG_AUTH-1] ||
184 xfrma[XFRMA_ALG_CRYPT-1] ||
185 xfrma[XFRMA_ENCAP-1] ||
186 xfrma[XFRMA_SEC_CTX-1] ||
187 !xfrma[XFRMA_COADDR-1])
188 goto out;
189 break;
190#endif
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 default:
193 goto out;
194 };
195
196 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
197 goto out;
198 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
199 goto out;
200 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
201 goto out;
202 if ((err = verify_encap_tmpl(xfrma)))
203 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800204 if ((err = verify_sec_ctx_len(xfrma)))
205 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700206 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 err = -EINVAL;
210 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700211 case XFRM_MODE_TRANSPORT:
212 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700213 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700214 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
217 default:
218 goto out;
219 };
220
221 err = 0;
222
223out:
224 return err;
225}
226
227static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
228 struct xfrm_algo_desc *(*get_byname)(char *, int),
229 struct rtattr *u_arg)
230{
231 struct rtattr *rta = u_arg;
232 struct xfrm_algo *p, *ualg;
233 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700234 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!rta)
237 return 0;
238
239 ualg = RTA_DATA(rta);
240
241 algo = get_byname(ualg->alg_name, 1);
242 if (!algo)
243 return -ENOSYS;
244 *props = algo->desc.sadb_alg_id;
245
Herbert Xub9e9dea2005-05-19 12:39:04 -0700246 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
247 p = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!p)
249 return -ENOMEM;
250
Herbert Xub9e9dea2005-05-19 12:39:04 -0700251 memcpy(p, ualg, len);
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);
266 p = kmalloc(sizeof(*p), GFP_KERNEL);
267 if (!p)
268 return -ENOMEM;
269
270 memcpy(p, uencap, sizeof(*p));
271 *encapp = p;
272 return 0;
273}
274
Trent Jaegerdf718372005-12-13 23:12:27 -0800275
276static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
277{
278 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
279 int len = 0;
280
281 if (xfrm_ctx) {
282 len += sizeof(struct xfrm_user_sec_ctx);
283 len += xfrm_ctx->ctx_len;
284 }
285 return len;
286}
287
288static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
289{
290 struct xfrm_user_sec_ctx *uctx;
291
292 if (!u_arg)
293 return 0;
294
295 uctx = RTA_DATA(u_arg);
296 return security_xfrm_state_alloc(x, uctx);
297}
298
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700299static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
300{
301 struct rtattr *rta = u_arg;
302 xfrm_address_t *p, *uaddrp;
303
304 if (!rta)
305 return 0;
306
307 uaddrp = RTA_DATA(rta);
308 p = kmalloc(sizeof(*p), GFP_KERNEL);
309 if (!p)
310 return -ENOMEM;
311
312 memcpy(p, uaddrp, sizeof(*p));
313 *addrpp = p;
314 return 0;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
318{
319 memcpy(&x->id, &p->id, sizeof(x->id));
320 memcpy(&x->sel, &p->sel, sizeof(x->sel));
321 memcpy(&x->lft, &p->lft, sizeof(x->lft));
322 x->props.mode = p->mode;
323 x->props.replay_window = p->replay_window;
324 x->props.reqid = p->reqid;
325 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700326 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 x->props.flags = p->flags;
328}
329
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800330/*
331 * someday when pfkey also has support, we could have the code
332 * somehow made shareable and move it to xfrm_state.c - JHS
333 *
334*/
335static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
336{
337 int err = - EINVAL;
338 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
339 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
340 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
341 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
342
343 if (rp) {
344 struct xfrm_replay_state *replay;
345 if (RTA_PAYLOAD(rp) < sizeof(*replay))
346 goto error;
347 replay = RTA_DATA(rp);
348 memcpy(&x->replay, replay, sizeof(*replay));
349 memcpy(&x->preplay, replay, sizeof(*replay));
350 }
351
352 if (lt) {
353 struct xfrm_lifetime_cur *ltime;
354 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
355 goto error;
356 ltime = RTA_DATA(lt);
357 x->curlft.bytes = ltime->bytes;
358 x->curlft.packets = ltime->packets;
359 x->curlft.add_time = ltime->add_time;
360 x->curlft.use_time = ltime->use_time;
361 }
362
363 if (et) {
364 if (RTA_PAYLOAD(et) < sizeof(u32))
365 goto error;
366 x->replay_maxage = *(u32*)RTA_DATA(et);
367 }
368
369 if (rt) {
370 if (RTA_PAYLOAD(rt) < sizeof(u32))
371 goto error;
372 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
373 }
374
375 return 0;
376error:
377 return err;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
381 struct rtattr **xfrma,
382 int *errp)
383{
384 struct xfrm_state *x = xfrm_state_alloc();
385 int err = -ENOMEM;
386
387 if (!x)
388 goto error_no_put;
389
390 copy_from_user_state(x, p);
391
392 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
393 xfrm_aalg_get_byname,
394 xfrma[XFRMA_ALG_AUTH-1])))
395 goto error;
396 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
397 xfrm_ealg_get_byname,
398 xfrma[XFRMA_ALG_CRYPT-1])))
399 goto error;
400 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
401 xfrm_calg_get_byname,
402 xfrma[XFRMA_ALG_COMP-1])))
403 goto error;
404 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
405 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700406 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
407 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700408 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (err)
410 goto error;
411
Trent Jaegerdf718372005-12-13 23:12:27 -0800412 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
413 goto error;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800416 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
417 /* sysctl_xfrm_aevent_etime is in 100ms units */
418 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
419 x->preplay.bitmap = 0;
420 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
421 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
422
423 /* override default values from above */
424
425 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
426 if (err < 0)
427 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return x;
430
431error:
432 x->km.state = XFRM_STATE_DEAD;
433 xfrm_state_put(x);
434error_no_put:
435 *errp = err;
436 return NULL;
437}
438
439static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
440{
441 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
442 struct xfrm_state *x;
443 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700444 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Trent Jaegerdf718372005-12-13 23:12:27 -0800446 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (err)
448 return err;
449
Trent Jaegerdf718372005-12-13 23:12:27 -0800450 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!x)
452 return err;
453
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700454 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
456 err = xfrm_state_add(x);
457 else
458 err = xfrm_state_update(x);
459
460 if (err < 0) {
461 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800462 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700466 c.seq = nlh->nlmsg_seq;
467 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700468 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700469
470 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700471out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700472 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return err;
474}
475
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700476static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
477 struct rtattr **xfrma,
478 int *errp)
479{
480 struct xfrm_state *x = NULL;
481 int err;
482
483 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
484 err = -ESRCH;
485 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
486 } else {
487 xfrm_address_t *saddr = NULL;
488
489 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
490 if (err)
491 goto out;
492
493 if (!saddr) {
494 err = -EINVAL;
495 goto out;
496 }
497
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800498 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700499 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
500 p->family);
501 }
502
503 out:
504 if (!x && errp)
505 *errp = err;
506 return x;
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
510{
511 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700512 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700513 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
515
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700516 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David S. Miller6f68dc32006-06-08 23:58:52 -0700520 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700521 goto out;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700524 err = -EPERM;
525 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700528 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700529 if (err < 0)
530 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700531
532 c.seq = nlh->nlmsg_seq;
533 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700534 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700535 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700537out:
538 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
543{
544 memcpy(&p->id, &x->id, sizeof(p->id));
545 memcpy(&p->sel, &x->sel, sizeof(p->sel));
546 memcpy(&p->lft, &x->lft, sizeof(p->lft));
547 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
548 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700549 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 p->mode = x->props.mode;
551 p->replay_window = x->props.replay_window;
552 p->reqid = x->props.reqid;
553 p->family = x->props.family;
554 p->flags = x->props.flags;
555 p->seq = x->km.seq;
556}
557
558struct xfrm_dump_info {
559 struct sk_buff *in_skb;
560 struct sk_buff *out_skb;
561 u32 nlmsg_seq;
562 u16 nlmsg_flags;
563 int start_idx;
564 int this_idx;
565};
566
567static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
568{
569 struct xfrm_dump_info *sp = ptr;
570 struct sk_buff *in_skb = sp->in_skb;
571 struct sk_buff *skb = sp->out_skb;
572 struct xfrm_usersa_info *p;
573 struct nlmsghdr *nlh;
574 unsigned char *b = skb->tail;
575
576 if (sp->this_idx < sp->start_idx)
577 goto out;
578
579 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
580 sp->nlmsg_seq,
581 XFRM_MSG_NEWSA, sizeof(*p));
582 nlh->nlmsg_flags = sp->nlmsg_flags;
583
584 p = NLMSG_DATA(nlh);
585 copy_to_user_state(x, p);
586
587 if (x->aalg)
588 RTA_PUT(skb, XFRMA_ALG_AUTH,
589 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
590 if (x->ealg)
591 RTA_PUT(skb, XFRMA_ALG_CRYPT,
592 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
593 if (x->calg)
594 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
595
596 if (x->encap)
597 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
598
Trent Jaegerdf718372005-12-13 23:12:27 -0800599 if (x->security) {
600 int ctx_size = sizeof(struct xfrm_sec_ctx) +
601 x->security->ctx_len;
602 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
603 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
604
605 uctx->exttype = XFRMA_SEC_CTX;
606 uctx->len = ctx_size;
607 uctx->ctx_doi = x->security->ctx_doi;
608 uctx->ctx_alg = x->security->ctx_alg;
609 uctx->ctx_len = x->security->ctx_len;
610 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
611 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700612
613 if (x->coaddr)
614 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
615
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700616 if (x->lastused)
617 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 nlh->nlmsg_len = skb->tail - b;
620out:
621 sp->this_idx++;
622 return 0;
623
624nlmsg_failure:
625rtattr_failure:
626 skb_trim(skb, b - skb->data);
627 return -1;
628}
629
630static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
631{
632 struct xfrm_dump_info info;
633
634 info.in_skb = cb->skb;
635 info.out_skb = skb;
636 info.nlmsg_seq = cb->nlh->nlmsg_seq;
637 info.nlmsg_flags = NLM_F_MULTI;
638 info.this_idx = 0;
639 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700640 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 cb->args[0] = info.this_idx;
642
643 return skb->len;
644}
645
646static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
647 struct xfrm_state *x, u32 seq)
648{
649 struct xfrm_dump_info info;
650 struct sk_buff *skb;
651
652 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
653 if (!skb)
654 return ERR_PTR(-ENOMEM);
655
656 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
657 info.in_skb = in_skb;
658 info.out_skb = skb;
659 info.nlmsg_seq = seq;
660 info.nlmsg_flags = 0;
661 info.this_idx = info.start_idx = 0;
662
663 if (dump_one_state(x, 0, &info)) {
664 kfree_skb(skb);
665 return NULL;
666 }
667
668 return skb;
669}
670
671static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
672{
673 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
674 struct xfrm_state *x;
675 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700676 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700678 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (x == NULL)
680 goto out_noput;
681
682 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
683 if (IS_ERR(resp_skb)) {
684 err = PTR_ERR(resp_skb);
685 } else {
686 err = netlink_unicast(xfrm_nl, resp_skb,
687 NETLINK_CB(skb).pid, MSG_DONTWAIT);
688 }
689 xfrm_state_put(x);
690out_noput:
691 return err;
692}
693
694static int verify_userspi_info(struct xfrm_userspi_info *p)
695{
696 switch (p->info.id.proto) {
697 case IPPROTO_AH:
698 case IPPROTO_ESP:
699 break;
700
701 case IPPROTO_COMP:
702 /* IPCOMP spi is 16-bits. */
703 if (p->max >= 0x10000)
704 return -EINVAL;
705 break;
706
707 default:
708 return -EINVAL;
709 };
710
711 if (p->min > p->max)
712 return -EINVAL;
713
714 return 0;
715}
716
717static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
718{
719 struct xfrm_state *x;
720 struct xfrm_userspi_info *p;
721 struct sk_buff *resp_skb;
722 xfrm_address_t *daddr;
723 int family;
724 int err;
725
726 p = NLMSG_DATA(nlh);
727 err = verify_userspi_info(p);
728 if (err)
729 goto out_noput;
730
731 family = p->info.family;
732 daddr = &p->info.id.daddr;
733
734 x = NULL;
735 if (p->info.seq) {
736 x = xfrm_find_acq_byseq(p->info.seq);
737 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
738 xfrm_state_put(x);
739 x = NULL;
740 }
741 }
742
743 if (!x)
744 x = xfrm_find_acq(p->info.mode, p->info.reqid,
745 p->info.id.proto, daddr,
746 &p->info.saddr, 1,
747 family);
748 err = -ENOENT;
749 if (x == NULL)
750 goto out_noput;
751
752 resp_skb = ERR_PTR(-ENOENT);
753
754 spin_lock_bh(&x->lock);
755 if (x->km.state != XFRM_STATE_DEAD) {
756 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
757 if (x->id.spi)
758 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
759 }
760 spin_unlock_bh(&x->lock);
761
762 if (IS_ERR(resp_skb)) {
763 err = PTR_ERR(resp_skb);
764 goto out;
765 }
766
767 err = netlink_unicast(xfrm_nl, resp_skb,
768 NETLINK_CB(skb).pid, MSG_DONTWAIT);
769
770out:
771 xfrm_state_put(x);
772out_noput:
773 return err;
774}
775
776static int verify_policy_dir(__u8 dir)
777{
778 switch (dir) {
779 case XFRM_POLICY_IN:
780 case XFRM_POLICY_OUT:
781 case XFRM_POLICY_FWD:
782 break;
783
784 default:
785 return -EINVAL;
786 };
787
788 return 0;
789}
790
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700791static int verify_policy_type(__u8 type)
792{
793 switch (type) {
794 case XFRM_POLICY_TYPE_MAIN:
795#ifdef CONFIG_XFRM_SUB_POLICY
796 case XFRM_POLICY_TYPE_SUB:
797#endif
798 break;
799
800 default:
801 return -EINVAL;
802 };
803
804 return 0;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
808{
809 switch (p->share) {
810 case XFRM_SHARE_ANY:
811 case XFRM_SHARE_SESSION:
812 case XFRM_SHARE_USER:
813 case XFRM_SHARE_UNIQUE:
814 break;
815
816 default:
817 return -EINVAL;
818 };
819
820 switch (p->action) {
821 case XFRM_POLICY_ALLOW:
822 case XFRM_POLICY_BLOCK:
823 break;
824
825 default:
826 return -EINVAL;
827 };
828
829 switch (p->sel.family) {
830 case AF_INET:
831 break;
832
833 case AF_INET6:
834#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
835 break;
836#else
837 return -EAFNOSUPPORT;
838#endif
839
840 default:
841 return -EINVAL;
842 };
843
844 return verify_policy_dir(p->dir);
845}
846
Trent Jaegerdf718372005-12-13 23:12:27 -0800847static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
848{
849 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
850 struct xfrm_user_sec_ctx *uctx;
851
852 if (!rt)
853 return 0;
854
855 uctx = RTA_DATA(rt);
856 return security_xfrm_policy_alloc(pol, uctx);
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
860 int nr)
861{
862 int i;
863
864 xp->xfrm_nr = nr;
865 for (i = 0; i < nr; i++, ut++) {
866 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
867
868 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
869 memcpy(&t->saddr, &ut->saddr,
870 sizeof(xfrm_address_t));
871 t->reqid = ut->reqid;
872 t->mode = ut->mode;
873 t->share = ut->share;
874 t->optional = ut->optional;
875 t->aalgos = ut->aalgos;
876 t->ealgos = ut->ealgos;
877 t->calgos = ut->calgos;
878 }
879}
880
881static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
882{
883 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
884 struct xfrm_user_tmpl *utmpl;
885 int nr;
886
887 if (!rt) {
888 pol->xfrm_nr = 0;
889 } else {
890 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
891
892 if (nr > XFRM_MAX_DEPTH)
893 return -EINVAL;
894
895 copy_templates(pol, RTA_DATA(rt), nr);
896 }
897 return 0;
898}
899
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700900static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
901{
902 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
903 struct xfrm_userpolicy_type *upt;
904 __u8 type = XFRM_POLICY_TYPE_MAIN;
905 int err;
906
907 if (rt) {
908 if (rt->rta_len < sizeof(*upt))
909 return -EINVAL;
910
911 upt = RTA_DATA(rt);
912 type = upt->type;
913 }
914
915 err = verify_policy_type(type);
916 if (err)
917 return err;
918
919 *tp = type;
920 return 0;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
924{
925 xp->priority = p->priority;
926 xp->index = p->index;
927 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
928 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
929 xp->action = p->action;
930 xp->flags = p->flags;
931 xp->family = p->sel.family;
932 /* XXX xp->share = p->share; */
933}
934
935static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
936{
937 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
938 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
939 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
940 p->priority = xp->priority;
941 p->index = xp->index;
942 p->sel.family = xp->family;
943 p->dir = dir;
944 p->action = xp->action;
945 p->flags = xp->flags;
946 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
947}
948
949static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
950{
951 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
952 int err;
953
954 if (!xp) {
955 *errp = -ENOMEM;
956 return NULL;
957 }
958
959 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800960
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700961 err = copy_from_user_policy_type(&xp->type, xfrma);
962 if (err)
963 goto error;
964
Trent Jaegerdf718372005-12-13 23:12:27 -0800965 if (!(err = copy_from_user_tmpl(xp, xfrma)))
966 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700967 if (err)
968 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700971 error:
972 *errp = err;
973 kfree(xp);
974 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
978{
979 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
980 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700981 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int err;
983 int excl;
984
985 err = verify_newpolicy_info(p);
986 if (err)
987 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800988 err = verify_sec_ctx_len((struct rtattr **)xfrma);
989 if (err)
990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Trent Jaegerdf718372005-12-13 23:12:27 -0800992 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!xp)
994 return err;
995
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700996 /* shouldnt excl be based on nlh flags??
997 * Aha! this is anti-netlink really i.e more pfkey derived
998 * in netlink excl is a flag and you wouldnt need
999 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1001 err = xfrm_policy_insert(p->dir, xp, excl);
1002 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001003 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree(xp);
1005 return err;
1006 }
1007
Herbert Xuf60f6b82005-06-18 22:44:37 -07001008 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001009 c.seq = nlh->nlmsg_seq;
1010 c.pid = nlh->nlmsg_pid;
1011 km_policy_notify(xp, p->dir, &c);
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 xfrm_pol_put(xp);
1014
1015 return 0;
1016}
1017
1018static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1019{
1020 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1021 int i;
1022
1023 if (xp->xfrm_nr == 0)
1024 return 0;
1025
1026 for (i = 0; i < xp->xfrm_nr; i++) {
1027 struct xfrm_user_tmpl *up = &vec[i];
1028 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1029
1030 memcpy(&up->id, &kp->id, sizeof(up->id));
1031 up->family = xp->family;
1032 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1033 up->reqid = kp->reqid;
1034 up->mode = kp->mode;
1035 up->share = kp->share;
1036 up->optional = kp->optional;
1037 up->aalgos = kp->aalgos;
1038 up->ealgos = kp->ealgos;
1039 up->calgos = kp->calgos;
1040 }
1041 RTA_PUT(skb, XFRMA_TMPL,
1042 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1043 vec);
1044
1045 return 0;
1046
1047rtattr_failure:
1048 return -1;
1049}
1050
Serge Hallyn0d681622006-07-24 23:30:44 -07001051static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001052{
Serge Hallyn0d681622006-07-24 23:30:44 -07001053 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1054 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1055 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001056
Serge Hallyn0d681622006-07-24 23:30:44 -07001057 uctx->exttype = XFRMA_SEC_CTX;
1058 uctx->len = ctx_size;
1059 uctx->ctx_doi = s->ctx_doi;
1060 uctx->ctx_alg = s->ctx_alg;
1061 uctx->ctx_len = s->ctx_len;
1062 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1063 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001064
1065 rtattr_failure:
1066 return -1;
1067}
1068
Serge Hallyn0d681622006-07-24 23:30:44 -07001069static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1070{
1071 if (x->security) {
1072 return copy_sec_ctx(x->security, skb);
1073 }
1074 return 0;
1075}
1076
1077static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1078{
1079 if (xp->security) {
1080 return copy_sec_ctx(xp->security, skb);
1081 }
1082 return 0;
1083}
1084
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001085#ifdef CONFIG_XFRM_SUB_POLICY
1086static int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
1087{
1088 struct xfrm_userpolicy_type upt;
1089
1090 memset(&upt, 0, sizeof(upt));
1091 upt.type = xp->type;
1092
1093 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1094
1095 return 0;
1096
1097rtattr_failure:
1098 return -1;
1099}
1100
1101#else
1102static inline int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
1103{
1104 return 0;
1105}
1106#endif
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1109{
1110 struct xfrm_dump_info *sp = ptr;
1111 struct xfrm_userpolicy_info *p;
1112 struct sk_buff *in_skb = sp->in_skb;
1113 struct sk_buff *skb = sp->out_skb;
1114 struct nlmsghdr *nlh;
1115 unsigned char *b = skb->tail;
1116
1117 if (sp->this_idx < sp->start_idx)
1118 goto out;
1119
1120 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1121 sp->nlmsg_seq,
1122 XFRM_MSG_NEWPOLICY, sizeof(*p));
1123 p = NLMSG_DATA(nlh);
1124 nlh->nlmsg_flags = sp->nlmsg_flags;
1125
1126 copy_to_user_policy(xp, p, dir);
1127 if (copy_to_user_tmpl(xp, skb) < 0)
1128 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001129 if (copy_to_user_sec_ctx(xp, skb))
1130 goto nlmsg_failure;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001131 if (copy_to_user_policy_type(xp, skb) < 0)
1132 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 nlh->nlmsg_len = skb->tail - b;
1135out:
1136 sp->this_idx++;
1137 return 0;
1138
1139nlmsg_failure:
1140 skb_trim(skb, b - skb->data);
1141 return -1;
1142}
1143
1144static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1145{
1146 struct xfrm_dump_info info;
1147
1148 info.in_skb = cb->skb;
1149 info.out_skb = skb;
1150 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1151 info.nlmsg_flags = NLM_F_MULTI;
1152 info.this_idx = 0;
1153 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001154 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1155#ifdef CONFIG_XFRM_SUB_POLICY
1156 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 cb->args[0] = info.this_idx;
1159
1160 return skb->len;
1161}
1162
1163static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1164 struct xfrm_policy *xp,
1165 int dir, u32 seq)
1166{
1167 struct xfrm_dump_info info;
1168 struct sk_buff *skb;
1169
1170 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1171 if (!skb)
1172 return ERR_PTR(-ENOMEM);
1173
1174 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1175 info.in_skb = in_skb;
1176 info.out_skb = skb;
1177 info.nlmsg_seq = seq;
1178 info.nlmsg_flags = 0;
1179 info.this_idx = info.start_idx = 0;
1180
1181 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1182 kfree_skb(skb);
1183 return NULL;
1184 }
1185
1186 return skb;
1187}
1188
1189static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1190{
1191 struct xfrm_policy *xp;
1192 struct xfrm_userpolicy_id *p;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001193 __u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001195 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int delete;
1197
1198 p = NLMSG_DATA(nlh);
1199 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1200
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001201 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1202 if (err)
1203 return err;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 err = verify_policy_dir(p->dir);
1206 if (err)
1207 return err;
1208
1209 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001210 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001211 else {
1212 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1213 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1214 struct xfrm_policy tmp;
1215
1216 err = verify_sec_ctx_len(rtattrs);
1217 if (err)
1218 return err;
1219
1220 memset(&tmp, 0, sizeof(struct xfrm_policy));
1221 if (rt) {
1222 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1223
1224 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1225 return err;
1226 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001227 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001228 security_xfrm_policy_free(&tmp);
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (xp == NULL)
1231 return -ENOENT;
1232
1233 if (!delete) {
1234 struct sk_buff *resp_skb;
1235
1236 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1237 if (IS_ERR(resp_skb)) {
1238 err = PTR_ERR(resp_skb);
1239 } else {
1240 err = netlink_unicast(xfrm_nl, resp_skb,
1241 NETLINK_CB(skb).pid,
1242 MSG_DONTWAIT);
1243 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001244 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001245 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001246 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001247 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001248 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001249 c.seq = nlh->nlmsg_seq;
1250 c.pid = nlh->nlmsg_pid;
1251 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 xfrm_pol_put(xp);
1255
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return err;
1258}
1259
1260static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1261{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001262 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1264
1265 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001266 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001267 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001268 c.seq = nlh->nlmsg_seq;
1269 c.pid = nlh->nlmsg_pid;
1270 km_state_notify(NULL, &c);
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 0;
1273}
1274
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001275
1276static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1277{
1278 struct xfrm_aevent_id *id;
1279 struct nlmsghdr *nlh;
1280 struct xfrm_lifetime_cur ltime;
1281 unsigned char *b = skb->tail;
1282
1283 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1284 id = NLMSG_DATA(nlh);
1285 nlh->nlmsg_flags = 0;
1286
1287 id->sa_id.daddr = x->id.daddr;
1288 id->sa_id.spi = x->id.spi;
1289 id->sa_id.family = x->props.family;
1290 id->sa_id.proto = x->id.proto;
1291 id->flags = c->data.aevent;
1292
1293 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1294
1295 ltime.bytes = x->curlft.bytes;
1296 ltime.packets = x->curlft.packets;
1297 ltime.add_time = x->curlft.add_time;
1298 ltime.use_time = x->curlft.use_time;
1299
1300 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1301
1302 if (id->flags&XFRM_AE_RTHR) {
1303 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1304 }
1305
1306 if (id->flags&XFRM_AE_ETHR) {
1307 u32 etimer = x->replay_maxage*10/HZ;
1308 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1309 }
1310
1311 nlh->nlmsg_len = skb->tail - b;
1312 return skb->len;
1313
1314rtattr_failure:
1315nlmsg_failure:
1316 skb_trim(skb, b - skb->data);
1317 return -1;
1318}
1319
1320static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1321{
1322 struct xfrm_state *x;
1323 struct sk_buff *r_skb;
1324 int err;
1325 struct km_event c;
1326 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1327 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1328 struct xfrm_usersa_id *id = &p->sa_id;
1329
1330 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1331 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1332
1333 if (p->flags&XFRM_AE_RTHR)
1334 len+=RTA_SPACE(sizeof(u32));
1335
1336 if (p->flags&XFRM_AE_ETHR)
1337 len+=RTA_SPACE(sizeof(u32));
1338
1339 r_skb = alloc_skb(len, GFP_ATOMIC);
1340 if (r_skb == NULL)
1341 return -ENOMEM;
1342
1343 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1344 if (x == NULL) {
1345 kfree(r_skb);
1346 return -ESRCH;
1347 }
1348
1349 /*
1350 * XXX: is this lock really needed - none of the other
1351 * gets lock (the concern is things getting updated
1352 * while we are still reading) - jhs
1353 */
1354 spin_lock_bh(&x->lock);
1355 c.data.aevent = p->flags;
1356 c.seq = nlh->nlmsg_seq;
1357 c.pid = nlh->nlmsg_pid;
1358
1359 if (build_aevent(r_skb, x, &c) < 0)
1360 BUG();
1361 err = netlink_unicast(xfrm_nl, r_skb,
1362 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1363 spin_unlock_bh(&x->lock);
1364 xfrm_state_put(x);
1365 return err;
1366}
1367
1368static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1369{
1370 struct xfrm_state *x;
1371 struct km_event c;
1372 int err = - EINVAL;
1373 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1374 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1375 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1376
1377 if (!lt && !rp)
1378 return err;
1379
1380 /* pedantic mode - thou shalt sayeth replaceth */
1381 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1382 return err;
1383
1384 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1385 if (x == NULL)
1386 return -ESRCH;
1387
1388 if (x->km.state != XFRM_STATE_VALID)
1389 goto out;
1390
1391 spin_lock_bh(&x->lock);
1392 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1393 spin_unlock_bh(&x->lock);
1394 if (err < 0)
1395 goto out;
1396
1397 c.event = nlh->nlmsg_type;
1398 c.seq = nlh->nlmsg_seq;
1399 c.pid = nlh->nlmsg_pid;
1400 c.data.aevent = XFRM_AE_CU;
1401 km_state_notify(x, &c);
1402 err = 0;
1403out:
1404 xfrm_state_put(x);
1405 return err;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1409{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001410 struct km_event c;
1411 __u8 type = XFRM_POLICY_TYPE_MAIN;
1412 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001413
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001414 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1415 if (err)
1416 return err;
1417
1418 xfrm_policy_flush(type);
1419 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001420 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421 c.seq = nlh->nlmsg_seq;
1422 c.pid = nlh->nlmsg_pid;
1423 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return 0;
1425}
1426
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001427static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1428{
1429 struct xfrm_policy *xp;
1430 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1431 struct xfrm_userpolicy_info *p = &up->pol;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001432 __u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001433 int err = -ENOENT;
1434
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001435 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1436 if (err)
1437 return err;
1438
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001439 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001440 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001441 else {
1442 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1443 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1444 struct xfrm_policy tmp;
1445
1446 err = verify_sec_ctx_len(rtattrs);
1447 if (err)
1448 return err;
1449
1450 memset(&tmp, 0, sizeof(struct xfrm_policy));
1451 if (rt) {
1452 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1453
1454 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1455 return err;
1456 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001457 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001458 security_xfrm_policy_free(&tmp);
1459 }
1460
1461 if (xp == NULL)
1462 return err;
1463 read_lock(&xp->lock);
1464 if (xp->dead) {
1465 read_unlock(&xp->lock);
1466 goto out;
1467 }
1468
1469 read_unlock(&xp->lock);
1470 err = 0;
1471 if (up->hard) {
1472 xfrm_policy_delete(xp, p->dir);
1473 } else {
1474 // reset the timers here?
1475 printk("Dont know what to do with soft policy expire\n");
1476 }
1477 km_policy_expired(xp, p->dir, up->hard, current->pid);
1478
1479out:
1480 xfrm_pol_put(xp);
1481 return err;
1482}
1483
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001484static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1485{
1486 struct xfrm_state *x;
1487 int err;
1488 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1489 struct xfrm_usersa_info *p = &ue->state;
1490
1491 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1492 err = -ENOENT;
1493
1494 if (x == NULL)
1495 return err;
1496
1497 err = -EINVAL;
1498
1499 spin_lock_bh(&x->lock);
1500 if (x->km.state != XFRM_STATE_VALID)
1501 goto out;
1502 km_state_expired(x, ue->hard, current->pid);
1503
1504 if (ue->hard)
1505 __xfrm_state_delete(x);
1506out:
1507 spin_unlock_bh(&x->lock);
1508 xfrm_state_put(x);
1509 return err;
1510}
1511
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001512static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1513{
1514 struct xfrm_policy *xp;
1515 struct xfrm_user_tmpl *ut;
1516 int i;
1517 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1518
1519 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1520 struct xfrm_state *x = xfrm_state_alloc();
1521 int err = -ENOMEM;
1522
1523 if (!x)
1524 return err;
1525
1526 err = verify_newpolicy_info(&ua->policy);
1527 if (err) {
1528 printk("BAD policy passed\n");
1529 kfree(x);
1530 return err;
1531 }
1532
1533 /* build an XP */
1534 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1535 kfree(x);
1536 return err;
1537 }
1538
1539 memcpy(&x->id, &ua->id, sizeof(ua->id));
1540 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1541 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1542
1543 ut = RTA_DATA(rt);
1544 /* extract the templates and for each call km_key */
1545 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1546 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1547 memcpy(&x->id, &t->id, sizeof(x->id));
1548 x->props.mode = t->mode;
1549 x->props.reqid = t->reqid;
1550 x->props.family = ut->family;
1551 t->aalgos = ua->aalgos;
1552 t->ealgos = ua->ealgos;
1553 t->calgos = ua->calgos;
1554 err = km_query(x, t, xp);
1555
1556 }
1557
1558 kfree(x);
1559 kfree(xp);
1560
1561 return 0;
1562}
1563
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001564
Thomas Graf492b5582005-05-03 14:26:40 -07001565#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1566
1567static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1568 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1569 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1570 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1571 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1572 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1573 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1574 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001575 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001576 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001577 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1578 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001579 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001580 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1581 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001582 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1583 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001584 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585};
1586
Thomas Graf492b5582005-05-03 14:26:40 -07001587#undef XMSGSIZE
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589static struct xfrm_link {
1590 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1591 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001592} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1593 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1594 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1595 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1596 .dump = xfrm_dump_sa },
1597 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1598 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1599 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1600 .dump = xfrm_dump_policy },
1601 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001602 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001603 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001604 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1605 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001606 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001607 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1608 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001609 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1610 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1614{
1615 struct rtattr *xfrma[XFRMA_MAX];
1616 struct xfrm_link *link;
1617 int type, min_len;
1618
1619 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1620 return 0;
1621
1622 type = nlh->nlmsg_type;
1623
1624 /* A control message: ignore them */
1625 if (type < XFRM_MSG_BASE)
1626 return 0;
1627
1628 /* Unknown message: reply with EINVAL */
1629 if (type > XFRM_MSG_MAX)
1630 goto err_einval;
1631
1632 type -= XFRM_MSG_BASE;
1633 link = &xfrm_dispatch[type];
1634
1635 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001636 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 *errp = -EPERM;
1638 return -1;
1639 }
1640
Thomas Graf492b5582005-05-03 14:26:40 -07001641 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1642 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1643 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (link->dump == NULL)
1645 goto err_einval;
1646
1647 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001648 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return -1;
1650 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001651
1652 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return -1;
1654 }
1655
1656 memset(xfrma, 0, sizeof(xfrma));
1657
1658 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1659 goto err_einval;
1660
1661 if (nlh->nlmsg_len > min_len) {
1662 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1663 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1664
1665 while (RTA_OK(attr, attrlen)) {
1666 unsigned short flavor = attr->rta_type;
1667 if (flavor) {
1668 if (flavor > XFRMA_MAX)
1669 goto err_einval;
1670 xfrma[flavor - 1] = attr;
1671 }
1672 attr = RTA_NEXT(attr, attrlen);
1673 }
1674 }
1675
1676 if (link->doit == NULL)
1677 goto err_einval;
1678 *errp = link->doit(skb, nlh, (void **) &xfrma);
1679
1680 return *errp;
1681
1682err_einval:
1683 *errp = -EINVAL;
1684 return -1;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687static void xfrm_netlink_rcv(struct sock *sk, int len)
1688{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001689 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001692 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001693 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001694 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001696 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001699static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct xfrm_user_expire *ue;
1702 struct nlmsghdr *nlh;
1703 unsigned char *b = skb->tail;
1704
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001705 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sizeof(*ue));
1707 ue = NLMSG_DATA(nlh);
1708 nlh->nlmsg_flags = 0;
1709
1710 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001711 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 nlh->nlmsg_len = skb->tail - b;
1714 return skb->len;
1715
1716nlmsg_failure:
1717 skb_trim(skb, b - skb->data);
1718 return -1;
1719}
1720
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001724 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001726 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (skb == NULL)
1728 return -ENOMEM;
1729
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 BUG();
1732
Patrick McHardyac6d4392005-08-14 19:29:52 -07001733 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1734 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001737static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1738{
1739 struct sk_buff *skb;
1740 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1741
1742 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1743 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1744 skb = alloc_skb(len, GFP_ATOMIC);
1745 if (skb == NULL)
1746 return -ENOMEM;
1747
1748 if (build_aevent(skb, x, c) < 0)
1749 BUG();
1750
1751 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1752 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1753}
1754
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001755static int xfrm_notify_sa_flush(struct km_event *c)
1756{
1757 struct xfrm_usersa_flush *p;
1758 struct nlmsghdr *nlh;
1759 struct sk_buff *skb;
1760 unsigned char *b;
1761 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1762
1763 skb = alloc_skb(len, GFP_ATOMIC);
1764 if (skb == NULL)
1765 return -ENOMEM;
1766 b = skb->tail;
1767
1768 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1769 XFRM_MSG_FLUSHSA, sizeof(*p));
1770 nlh->nlmsg_flags = 0;
1771
1772 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001773 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001774
1775 nlh->nlmsg_len = skb->tail - b;
1776
Patrick McHardyac6d4392005-08-14 19:29:52 -07001777 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1778 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001779
1780nlmsg_failure:
1781 kfree_skb(skb);
1782 return -1;
1783}
1784
1785static int inline xfrm_sa_len(struct xfrm_state *x)
1786{
Herbert Xu0603eac2005-06-18 22:54:36 -07001787 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001788 if (x->aalg)
1789 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1790 if (x->ealg)
1791 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1792 if (x->calg)
1793 l += RTA_SPACE(sizeof(*x->calg));
1794 if (x->encap)
1795 l += RTA_SPACE(sizeof(*x->encap));
1796
1797 return l;
1798}
1799
1800static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1801{
1802 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001803 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001804 struct nlmsghdr *nlh;
1805 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001806 unsigned char *b;
1807 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001808 int headlen;
1809
1810 headlen = sizeof(*p);
1811 if (c->event == XFRM_MSG_DELSA) {
1812 len += RTA_SPACE(headlen);
1813 headlen = sizeof(*id);
1814 }
1815 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001816
1817 skb = alloc_skb(len, GFP_ATOMIC);
1818 if (skb == NULL)
1819 return -ENOMEM;
1820 b = skb->tail;
1821
Herbert Xu0603eac2005-06-18 22:54:36 -07001822 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001823 nlh->nlmsg_flags = 0;
1824
1825 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001826 if (c->event == XFRM_MSG_DELSA) {
1827 id = NLMSG_DATA(nlh);
1828 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1829 id->spi = x->id.spi;
1830 id->family = x->props.family;
1831 id->proto = x->id.proto;
1832
1833 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1834 }
1835
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001836 copy_to_user_state(x, p);
1837
1838 if (x->aalg)
1839 RTA_PUT(skb, XFRMA_ALG_AUTH,
1840 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1841 if (x->ealg)
1842 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1843 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1844 if (x->calg)
1845 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1846
1847 if (x->encap)
1848 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1849
1850 nlh->nlmsg_len = skb->tail - b;
1851
Patrick McHardyac6d4392005-08-14 19:29:52 -07001852 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1853 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001854
1855nlmsg_failure:
1856rtattr_failure:
1857 kfree_skb(skb);
1858 return -1;
1859}
1860
1861static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1862{
1863
1864 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001865 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001866 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001867 case XFRM_MSG_NEWAE:
1868 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001869 case XFRM_MSG_DELSA:
1870 case XFRM_MSG_UPDSA:
1871 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001872 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001873 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001874 return xfrm_notify_sa_flush(c);
1875 default:
1876 printk("xfrm_user: Unknown SA event %d\n", c->event);
1877 break;
1878 }
1879
1880 return 0;
1881
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1885 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1886 int dir)
1887{
1888 struct xfrm_user_acquire *ua;
1889 struct nlmsghdr *nlh;
1890 unsigned char *b = skb->tail;
1891 __u32 seq = xfrm_get_acqseq();
1892
1893 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1894 sizeof(*ua));
1895 ua = NLMSG_DATA(nlh);
1896 nlh->nlmsg_flags = 0;
1897
1898 memcpy(&ua->id, &x->id, sizeof(ua->id));
1899 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1900 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1901 copy_to_user_policy(xp, &ua->policy, dir);
1902 ua->aalgos = xt->aalgos;
1903 ua->ealgos = xt->ealgos;
1904 ua->calgos = xt->calgos;
1905 ua->seq = x->km.seq = seq;
1906
1907 if (copy_to_user_tmpl(xp, skb) < 0)
1908 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07001909 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08001910 goto nlmsg_failure;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001911 if (copy_to_user_policy_type(xp, skb) < 0)
1912 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 nlh->nlmsg_len = skb->tail - b;
1915 return skb->len;
1916
1917nlmsg_failure:
1918 skb_trim(skb, b - skb->data);
1919 return -1;
1920}
1921
1922static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1923 struct xfrm_policy *xp, int dir)
1924{
1925 struct sk_buff *skb;
1926 size_t len;
1927
1928 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1929 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001930 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08001931#ifdef CONFIG_XFRM_SUB_POLICY
1932 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 skb = alloc_skb(len, GFP_ATOMIC);
1935 if (skb == NULL)
1936 return -ENOMEM;
1937
1938 if (build_acquire(skb, x, xt, xp, dir) < 0)
1939 BUG();
1940
Patrick McHardyac6d4392005-08-14 19:29:52 -07001941 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1942 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/* User gives us xfrm_user_policy_info followed by an array of 0
1946 * or more templates.
1947 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001948static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 u8 *data, int len, int *dir)
1950{
1951 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1952 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1953 struct xfrm_policy *xp;
1954 int nr;
1955
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001956 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 case AF_INET:
1958 if (opt != IP_XFRM_POLICY) {
1959 *dir = -EOPNOTSUPP;
1960 return NULL;
1961 }
1962 break;
1963#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1964 case AF_INET6:
1965 if (opt != IPV6_XFRM_POLICY) {
1966 *dir = -EOPNOTSUPP;
1967 return NULL;
1968 }
1969 break;
1970#endif
1971 default:
1972 *dir = -EINVAL;
1973 return NULL;
1974 }
1975
1976 *dir = -EINVAL;
1977
1978 if (len < sizeof(*p) ||
1979 verify_newpolicy_info(p))
1980 return NULL;
1981
1982 nr = ((len - sizeof(*p)) / sizeof(*ut));
1983 if (nr > XFRM_MAX_DEPTH)
1984 return NULL;
1985
Herbert Xua4f1bac2005-07-26 15:43:17 -07001986 if (p->dir > XFRM_POLICY_OUT)
1987 return NULL;
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 xp = xfrm_policy_alloc(GFP_KERNEL);
1990 if (xp == NULL) {
1991 *dir = -ENOBUFS;
1992 return NULL;
1993 }
1994
1995 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001996 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 copy_templates(xp, ut, nr);
1998
1999 *dir = p->dir;
2000
2001 return xp;
2002}
2003
2004static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002005 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 struct xfrm_user_polexpire *upe;
2008 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002009 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 unsigned char *b = skb->tail;
2011
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002012 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 upe = NLMSG_DATA(nlh);
2014 nlh->nlmsg_flags = 0;
2015
2016 copy_to_user_policy(xp, &upe->pol, dir);
2017 if (copy_to_user_tmpl(xp, skb) < 0)
2018 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002019 if (copy_to_user_sec_ctx(xp, skb))
2020 goto nlmsg_failure;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002021 if (copy_to_user_policy_type(xp, skb) < 0)
2022 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 upe->hard = !!hard;
2024
2025 nlh->nlmsg_len = skb->tail - b;
2026 return skb->len;
2027
2028nlmsg_failure:
2029 skb_trim(skb, b - skb->data);
2030 return -1;
2031}
2032
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002033static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct sk_buff *skb;
2036 size_t len;
2037
2038 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2039 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002040 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002041#ifdef CONFIG_XFRM_SUB_POLICY
2042 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2043#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 skb = alloc_skb(len, GFP_ATOMIC);
2045 if (skb == NULL)
2046 return -ENOMEM;
2047
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002048 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 BUG();
2050
Patrick McHardyac6d4392005-08-14 19:29:52 -07002051 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2052 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002055static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2056{
2057 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002058 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002059 struct nlmsghdr *nlh;
2060 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002061 unsigned char *b;
2062 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002063 int headlen;
2064
2065 headlen = sizeof(*p);
2066 if (c->event == XFRM_MSG_DELPOLICY) {
2067 len += RTA_SPACE(headlen);
2068 headlen = sizeof(*id);
2069 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002070#ifdef CONFIG_XFRM_SUB_POLICY
2071 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2072#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002073 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002074
2075 skb = alloc_skb(len, GFP_ATOMIC);
2076 if (skb == NULL)
2077 return -ENOMEM;
2078 b = skb->tail;
2079
Herbert Xu0603eac2005-06-18 22:54:36 -07002080 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002081
2082 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002083 if (c->event == XFRM_MSG_DELPOLICY) {
2084 id = NLMSG_DATA(nlh);
2085 memset(id, 0, sizeof(*id));
2086 id->dir = dir;
2087 if (c->data.byid)
2088 id->index = xp->index;
2089 else
2090 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2091
2092 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2093 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094
2095 nlh->nlmsg_flags = 0;
2096
2097 copy_to_user_policy(xp, p, dir);
2098 if (copy_to_user_tmpl(xp, skb) < 0)
2099 goto nlmsg_failure;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002100 if (copy_to_user_policy_type(xp, skb) < 0)
2101 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002102
2103 nlh->nlmsg_len = skb->tail - b;
2104
Patrick McHardyac6d4392005-08-14 19:29:52 -07002105 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2106 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002107
2108nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002109rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002110 kfree_skb(skb);
2111 return -1;
2112}
2113
2114static int xfrm_notify_policy_flush(struct km_event *c)
2115{
2116 struct nlmsghdr *nlh;
2117 struct sk_buff *skb;
2118 unsigned char *b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002119 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002120#ifdef CONFIG_XFRM_SUB_POLICY
2121 struct xfrm_userpolicy_type upt;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002122 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002123#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002124 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002125
2126 skb = alloc_skb(len, GFP_ATOMIC);
2127 if (skb == NULL)
2128 return -ENOMEM;
2129 b = skb->tail;
2130
2131
2132 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002133 nlh->nlmsg_flags = 0;
2134
2135#ifdef CONFIG_XFRM_SUB_POLICY
2136 memset(&upt, 0, sizeof(upt));
2137 upt.type = c->data.type;
2138 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
2139#endif
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140
2141 nlh->nlmsg_len = skb->tail - b;
2142
Patrick McHardyac6d4392005-08-14 19:29:52 -07002143 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2144 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002145
2146nlmsg_failure:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002147#ifdef CONFIG_XFRM_SUB_POLICY
2148rtattr_failure:
2149#endif
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002150 kfree_skb(skb);
2151 return -1;
2152}
2153
2154static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2155{
2156
2157 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002158 case XFRM_MSG_NEWPOLICY:
2159 case XFRM_MSG_UPDPOLICY:
2160 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002161 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002162 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002163 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002164 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002165 return xfrm_exp_policy_notify(xp, dir, c);
2166 default:
2167 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2168 }
2169
2170 return 0;
2171
2172}
2173
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002174static int build_report(struct sk_buff *skb, u8 proto,
2175 struct xfrm_selector *sel, xfrm_address_t *addr)
2176{
2177 struct xfrm_user_report *ur;
2178 struct nlmsghdr *nlh;
2179 unsigned char *b = skb->tail;
2180
2181 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2182 ur = NLMSG_DATA(nlh);
2183 nlh->nlmsg_flags = 0;
2184
2185 ur->proto = proto;
2186 memcpy(&ur->sel, sel, sizeof(ur->sel));
2187
2188 if (addr)
2189 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2190
2191 nlh->nlmsg_len = skb->tail - b;
2192 return skb->len;
2193
2194nlmsg_failure:
2195rtattr_failure:
2196 skb_trim(skb, b - skb->data);
2197 return -1;
2198}
2199
2200static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2201 xfrm_address_t *addr)
2202{
2203 struct sk_buff *skb;
2204 size_t len;
2205
2206 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2207 skb = alloc_skb(len, GFP_ATOMIC);
2208 if (skb == NULL)
2209 return -ENOMEM;
2210
2211 if (build_report(skb, proto, sel, addr) < 0)
2212 BUG();
2213
2214 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2215 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218static struct xfrm_mgr netlink_mgr = {
2219 .id = "netlink",
2220 .notify = xfrm_send_state_notify,
2221 .acquire = xfrm_send_acquire,
2222 .compile_policy = xfrm_compile_policy,
2223 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002224 .report = xfrm_send_report,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225};
2226
2227static int __init xfrm_user_init(void)
2228{
Patrick McHardybe336902006-03-20 22:40:54 -08002229 struct sock *nlsk;
2230
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002231 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Patrick McHardybe336902006-03-20 22:40:54 -08002233 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2234 xfrm_netlink_rcv, THIS_MODULE);
2235 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002237 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 xfrm_register_km(&netlink_mgr);
2240
2241 return 0;
2242}
2243
2244static void __exit xfrm_user_exit(void)
2245{
Patrick McHardybe336902006-03-20 22:40:54 -08002246 struct sock *nlsk = xfrm_nl;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002249 rcu_assign_pointer(xfrm_nl, NULL);
2250 synchronize_rcu();
2251 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
2254module_init(xfrm_user_init);
2255module_exit(xfrm_user_exit);
2256MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002257MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002258