blob: 6f97665983d274f60509109450d441c2b602b953 [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;
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200247 p = kmemdup(ualg, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!p)
249 return -ENOMEM;
250
Herbert Xu04ff1262006-08-13 08:50:00 +1000251 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *algpp = p;
253 return 0;
254}
255
256static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
257{
258 struct rtattr *rta = u_arg;
259 struct xfrm_encap_tmpl *p, *uencap;
260
261 if (!rta)
262 return 0;
263
264 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200265 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!p)
267 return -ENOMEM;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *encapp = p;
270 return 0;
271}
272
Trent Jaegerdf718372005-12-13 23:12:27 -0800273
274static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
275{
276 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
277 int len = 0;
278
279 if (xfrm_ctx) {
280 len += sizeof(struct xfrm_user_sec_ctx);
281 len += xfrm_ctx->ctx_len;
282 }
283 return len;
284}
285
286static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
287{
288 struct xfrm_user_sec_ctx *uctx;
289
290 if (!u_arg)
291 return 0;
292
293 uctx = RTA_DATA(u_arg);
294 return security_xfrm_state_alloc(x, uctx);
295}
296
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700297static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
298{
299 struct rtattr *rta = u_arg;
300 xfrm_address_t *p, *uaddrp;
301
302 if (!rta)
303 return 0;
304
305 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200306 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700307 if (!p)
308 return -ENOMEM;
309
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700310 *addrpp = p;
311 return 0;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
315{
316 memcpy(&x->id, &p->id, sizeof(x->id));
317 memcpy(&x->sel, &p->sel, sizeof(x->sel));
318 memcpy(&x->lft, &p->lft, sizeof(x->lft));
319 x->props.mode = p->mode;
320 x->props.replay_window = p->replay_window;
321 x->props.reqid = p->reqid;
322 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700323 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 x->props.flags = p->flags;
325}
326
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800327/*
328 * someday when pfkey also has support, we could have the code
329 * somehow made shareable and move it to xfrm_state.c - JHS
330 *
331*/
332static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
333{
334 int err = - EINVAL;
335 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
336 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
337 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
338 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
339
340 if (rp) {
341 struct xfrm_replay_state *replay;
342 if (RTA_PAYLOAD(rp) < sizeof(*replay))
343 goto error;
344 replay = RTA_DATA(rp);
345 memcpy(&x->replay, replay, sizeof(*replay));
346 memcpy(&x->preplay, replay, sizeof(*replay));
347 }
348
349 if (lt) {
350 struct xfrm_lifetime_cur *ltime;
351 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
352 goto error;
353 ltime = RTA_DATA(lt);
354 x->curlft.bytes = ltime->bytes;
355 x->curlft.packets = ltime->packets;
356 x->curlft.add_time = ltime->add_time;
357 x->curlft.use_time = ltime->use_time;
358 }
359
360 if (et) {
361 if (RTA_PAYLOAD(et) < sizeof(u32))
362 goto error;
363 x->replay_maxage = *(u32*)RTA_DATA(et);
364 }
365
366 if (rt) {
367 if (RTA_PAYLOAD(rt) < sizeof(u32))
368 goto error;
369 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
370 }
371
372 return 0;
373error:
374 return err;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
378 struct rtattr **xfrma,
379 int *errp)
380{
381 struct xfrm_state *x = xfrm_state_alloc();
382 int err = -ENOMEM;
383
384 if (!x)
385 goto error_no_put;
386
387 copy_from_user_state(x, p);
388
389 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
390 xfrm_aalg_get_byname,
391 xfrma[XFRMA_ALG_AUTH-1])))
392 goto error;
393 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
394 xfrm_ealg_get_byname,
395 xfrma[XFRMA_ALG_CRYPT-1])))
396 goto error;
397 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
398 xfrm_calg_get_byname,
399 xfrma[XFRMA_ALG_COMP-1])))
400 goto error;
401 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
402 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700403 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
404 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700405 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (err)
407 goto error;
408
Trent Jaegerdf718372005-12-13 23:12:27 -0800409 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
410 goto error;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800413 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
414 /* sysctl_xfrm_aevent_etime is in 100ms units */
415 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
416 x->preplay.bitmap = 0;
417 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
418 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
419
420 /* override default values from above */
421
422 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
423 if (err < 0)
424 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return x;
427
428error:
429 x->km.state = XFRM_STATE_DEAD;
430 xfrm_state_put(x);
431error_no_put:
432 *errp = err;
433 return NULL;
434}
435
436static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
437{
438 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
439 struct xfrm_state *x;
440 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700441 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Trent Jaegerdf718372005-12-13 23:12:27 -0800443 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (err)
445 return err;
446
Trent Jaegerdf718372005-12-13 23:12:27 -0800447 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!x)
449 return err;
450
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700451 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
453 err = xfrm_state_add(x);
454 else
455 err = xfrm_state_update(x);
456
457 if (err < 0) {
458 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800459 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700463 c.seq = nlh->nlmsg_seq;
464 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700465 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700466
467 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700468out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700469 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700473static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
474 struct rtattr **xfrma,
475 int *errp)
476{
477 struct xfrm_state *x = NULL;
478 int err;
479
480 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
481 err = -ESRCH;
482 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
483 } else {
484 xfrm_address_t *saddr = NULL;
485
486 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
487 if (err)
488 goto out;
489
490 if (!saddr) {
491 err = -EINVAL;
492 goto out;
493 }
494
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800495 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700496 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
497 p->family);
498 }
499
500 out:
501 if (!x && errp)
502 *errp = err;
503 return x;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
507{
508 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700509 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700510 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
512
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700513 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David S. Miller6f68dc32006-06-08 23:58:52 -0700517 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700518 goto out;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700521 err = -EPERM;
522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700525 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700526 if (err < 0)
527 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700528
529 c.seq = nlh->nlmsg_seq;
530 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700531 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700532 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700534out:
535 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
540{
541 memcpy(&p->id, &x->id, sizeof(p->id));
542 memcpy(&p->sel, &x->sel, sizeof(p->sel));
543 memcpy(&p->lft, &x->lft, sizeof(p->lft));
544 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
545 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700546 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 p->mode = x->props.mode;
548 p->replay_window = x->props.replay_window;
549 p->reqid = x->props.reqid;
550 p->family = x->props.family;
551 p->flags = x->props.flags;
552 p->seq = x->km.seq;
553}
554
555struct xfrm_dump_info {
556 struct sk_buff *in_skb;
557 struct sk_buff *out_skb;
558 u32 nlmsg_seq;
559 u16 nlmsg_flags;
560 int start_idx;
561 int this_idx;
562};
563
564static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
565{
566 struct xfrm_dump_info *sp = ptr;
567 struct sk_buff *in_skb = sp->in_skb;
568 struct sk_buff *skb = sp->out_skb;
569 struct xfrm_usersa_info *p;
570 struct nlmsghdr *nlh;
571 unsigned char *b = skb->tail;
572
573 if (sp->this_idx < sp->start_idx)
574 goto out;
575
576 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
577 sp->nlmsg_seq,
578 XFRM_MSG_NEWSA, sizeof(*p));
579 nlh->nlmsg_flags = sp->nlmsg_flags;
580
581 p = NLMSG_DATA(nlh);
582 copy_to_user_state(x, p);
583
584 if (x->aalg)
585 RTA_PUT(skb, XFRMA_ALG_AUTH,
586 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
587 if (x->ealg)
588 RTA_PUT(skb, XFRMA_ALG_CRYPT,
589 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
590 if (x->calg)
591 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
592
593 if (x->encap)
594 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
595
Trent Jaegerdf718372005-12-13 23:12:27 -0800596 if (x->security) {
597 int ctx_size = sizeof(struct xfrm_sec_ctx) +
598 x->security->ctx_len;
599 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
600 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
601
602 uctx->exttype = XFRMA_SEC_CTX;
603 uctx->len = ctx_size;
604 uctx->ctx_doi = x->security->ctx_doi;
605 uctx->ctx_alg = x->security->ctx_alg;
606 uctx->ctx_len = x->security->ctx_len;
607 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
608 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700609
610 if (x->coaddr)
611 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
612
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700613 if (x->lastused)
614 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 nlh->nlmsg_len = skb->tail - b;
617out:
618 sp->this_idx++;
619 return 0;
620
621nlmsg_failure:
622rtattr_failure:
623 skb_trim(skb, b - skb->data);
624 return -1;
625}
626
627static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
628{
629 struct xfrm_dump_info info;
630
631 info.in_skb = cb->skb;
632 info.out_skb = skb;
633 info.nlmsg_seq = cb->nlh->nlmsg_seq;
634 info.nlmsg_flags = NLM_F_MULTI;
635 info.this_idx = 0;
636 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700637 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 cb->args[0] = info.this_idx;
639
640 return skb->len;
641}
642
643static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
644 struct xfrm_state *x, u32 seq)
645{
646 struct xfrm_dump_info info;
647 struct sk_buff *skb;
648
649 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
650 if (!skb)
651 return ERR_PTR(-ENOMEM);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 info.in_skb = in_skb;
654 info.out_skb = skb;
655 info.nlmsg_seq = seq;
656 info.nlmsg_flags = 0;
657 info.this_idx = info.start_idx = 0;
658
659 if (dump_one_state(x, 0, &info)) {
660 kfree_skb(skb);
661 return NULL;
662 }
663
664 return skb;
665}
666
667static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
668{
669 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
670 struct xfrm_state *x;
671 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700672 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700674 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (x == NULL)
676 goto out_noput;
677
678 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
679 if (IS_ERR(resp_skb)) {
680 err = PTR_ERR(resp_skb);
681 } else {
682 err = netlink_unicast(xfrm_nl, resp_skb,
683 NETLINK_CB(skb).pid, MSG_DONTWAIT);
684 }
685 xfrm_state_put(x);
686out_noput:
687 return err;
688}
689
690static int verify_userspi_info(struct xfrm_userspi_info *p)
691{
692 switch (p->info.id.proto) {
693 case IPPROTO_AH:
694 case IPPROTO_ESP:
695 break;
696
697 case IPPROTO_COMP:
698 /* IPCOMP spi is 16-bits. */
699 if (p->max >= 0x10000)
700 return -EINVAL;
701 break;
702
703 default:
704 return -EINVAL;
705 };
706
707 if (p->min > p->max)
708 return -EINVAL;
709
710 return 0;
711}
712
713static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
714{
715 struct xfrm_state *x;
716 struct xfrm_userspi_info *p;
717 struct sk_buff *resp_skb;
718 xfrm_address_t *daddr;
719 int family;
720 int err;
721
722 p = NLMSG_DATA(nlh);
723 err = verify_userspi_info(p);
724 if (err)
725 goto out_noput;
726
727 family = p->info.family;
728 daddr = &p->info.id.daddr;
729
730 x = NULL;
731 if (p->info.seq) {
732 x = xfrm_find_acq_byseq(p->info.seq);
733 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
734 xfrm_state_put(x);
735 x = NULL;
736 }
737 }
738
739 if (!x)
740 x = xfrm_find_acq(p->info.mode, p->info.reqid,
741 p->info.id.proto, daddr,
742 &p->info.saddr, 1,
743 family);
744 err = -ENOENT;
745 if (x == NULL)
746 goto out_noput;
747
748 resp_skb = ERR_PTR(-ENOENT);
749
750 spin_lock_bh(&x->lock);
751 if (x->km.state != XFRM_STATE_DEAD) {
752 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
753 if (x->id.spi)
754 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
755 }
756 spin_unlock_bh(&x->lock);
757
758 if (IS_ERR(resp_skb)) {
759 err = PTR_ERR(resp_skb);
760 goto out;
761 }
762
763 err = netlink_unicast(xfrm_nl, resp_skb,
764 NETLINK_CB(skb).pid, MSG_DONTWAIT);
765
766out:
767 xfrm_state_put(x);
768out_noput:
769 return err;
770}
771
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800772static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 switch (dir) {
775 case XFRM_POLICY_IN:
776 case XFRM_POLICY_OUT:
777 case XFRM_POLICY_FWD:
778 break;
779
780 default:
781 return -EINVAL;
782 };
783
784 return 0;
785}
786
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800787static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700788{
789 switch (type) {
790 case XFRM_POLICY_TYPE_MAIN:
791#ifdef CONFIG_XFRM_SUB_POLICY
792 case XFRM_POLICY_TYPE_SUB:
793#endif
794 break;
795
796 default:
797 return -EINVAL;
798 };
799
800 return 0;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
804{
805 switch (p->share) {
806 case XFRM_SHARE_ANY:
807 case XFRM_SHARE_SESSION:
808 case XFRM_SHARE_USER:
809 case XFRM_SHARE_UNIQUE:
810 break;
811
812 default:
813 return -EINVAL;
814 };
815
816 switch (p->action) {
817 case XFRM_POLICY_ALLOW:
818 case XFRM_POLICY_BLOCK:
819 break;
820
821 default:
822 return -EINVAL;
823 };
824
825 switch (p->sel.family) {
826 case AF_INET:
827 break;
828
829 case AF_INET6:
830#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
831 break;
832#else
833 return -EAFNOSUPPORT;
834#endif
835
836 default:
837 return -EINVAL;
838 };
839
840 return verify_policy_dir(p->dir);
841}
842
Trent Jaegerdf718372005-12-13 23:12:27 -0800843static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
844{
845 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
846 struct xfrm_user_sec_ctx *uctx;
847
848 if (!rt)
849 return 0;
850
851 uctx = RTA_DATA(rt);
852 return security_xfrm_policy_alloc(pol, uctx);
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
856 int nr)
857{
858 int i;
859
860 xp->xfrm_nr = nr;
Miika Komu8511d012006-11-30 16:40:51 -0800861 xp->family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 for (i = 0; i < nr; i++, ut++) {
863 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
864
865 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
866 memcpy(&t->saddr, &ut->saddr,
867 sizeof(xfrm_address_t));
868 t->reqid = ut->reqid;
869 t->mode = ut->mode;
870 t->share = ut->share;
871 t->optional = ut->optional;
872 t->aalgos = ut->aalgos;
873 t->ealgos = ut->ealgos;
874 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800875 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877}
878
879static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
880{
881 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
882 struct xfrm_user_tmpl *utmpl;
883 int nr;
884
885 if (!rt) {
886 pol->xfrm_nr = 0;
887 } else {
888 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
889
890 if (nr > XFRM_MAX_DEPTH)
891 return -EINVAL;
892
893 copy_templates(pol, RTA_DATA(rt), nr);
894 }
895 return 0;
896}
897
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700898static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
899{
900 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
901 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800902 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700903 int err;
904
905 if (rt) {
906 if (rt->rta_len < sizeof(*upt))
907 return -EINVAL;
908
909 upt = RTA_DATA(rt);
910 type = upt->type;
911 }
912
913 err = verify_policy_type(type);
914 if (err)
915 return err;
916
917 *tp = type;
918 return 0;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
922{
923 xp->priority = p->priority;
924 xp->index = p->index;
925 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
926 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
927 xp->action = p->action;
928 xp->flags = p->flags;
929 xp->family = p->sel.family;
930 /* XXX xp->share = p->share; */
931}
932
933static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
934{
935 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
936 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
937 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
938 p->priority = xp->priority;
939 p->index = xp->index;
940 p->sel.family = xp->family;
941 p->dir = dir;
942 p->action = xp->action;
943 p->flags = xp->flags;
944 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
945}
946
947static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
948{
949 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
950 int err;
951
952 if (!xp) {
953 *errp = -ENOMEM;
954 return NULL;
955 }
956
957 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800958
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700959 err = copy_from_user_policy_type(&xp->type, xfrma);
960 if (err)
961 goto error;
962
Trent Jaegerdf718372005-12-13 23:12:27 -0800963 if (!(err = copy_from_user_tmpl(xp, xfrma)))
964 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700965 if (err)
966 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700969 error:
970 *errp = err;
971 kfree(xp);
972 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
976{
977 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
978 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700979 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int err;
981 int excl;
982
983 err = verify_newpolicy_info(p);
984 if (err)
985 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -0800986 err = verify_sec_ctx_len((struct rtattr **)xfrma);
987 if (err)
988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Trent Jaegerdf718372005-12-13 23:12:27 -0800990 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!xp)
992 return err;
993
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700994 /* shouldnt excl be based on nlh flags??
995 * Aha! this is anti-netlink really i.e more pfkey derived
996 * in netlink excl is a flag and you wouldnt need
997 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
999 err = xfrm_policy_insert(p->dir, xp, excl);
1000 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001001 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 kfree(xp);
1003 return err;
1004 }
1005
Herbert Xuf60f6b82005-06-18 22:44:37 -07001006 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001007 c.seq = nlh->nlmsg_seq;
1008 c.pid = nlh->nlmsg_pid;
1009 km_policy_notify(xp, p->dir, &c);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 xfrm_pol_put(xp);
1012
1013 return 0;
1014}
1015
1016static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1017{
1018 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1019 int i;
1020
1021 if (xp->xfrm_nr == 0)
1022 return 0;
1023
1024 for (i = 0; i < xp->xfrm_nr; i++) {
1025 struct xfrm_user_tmpl *up = &vec[i];
1026 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1027
1028 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001029 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1031 up->reqid = kp->reqid;
1032 up->mode = kp->mode;
1033 up->share = kp->share;
1034 up->optional = kp->optional;
1035 up->aalgos = kp->aalgos;
1036 up->ealgos = kp->ealgos;
1037 up->calgos = kp->calgos;
1038 }
1039 RTA_PUT(skb, XFRMA_TMPL,
1040 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1041 vec);
1042
1043 return 0;
1044
1045rtattr_failure:
1046 return -1;
1047}
1048
Serge Hallyn0d681622006-07-24 23:30:44 -07001049static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001050{
Serge Hallyn0d681622006-07-24 23:30:44 -07001051 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1052 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1053 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001054
Serge Hallyn0d681622006-07-24 23:30:44 -07001055 uctx->exttype = XFRMA_SEC_CTX;
1056 uctx->len = ctx_size;
1057 uctx->ctx_doi = s->ctx_doi;
1058 uctx->ctx_alg = s->ctx_alg;
1059 uctx->ctx_len = s->ctx_len;
1060 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1061 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001062
1063 rtattr_failure:
1064 return -1;
1065}
1066
Serge Hallyn0d681622006-07-24 23:30:44 -07001067static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1068{
1069 if (x->security) {
1070 return copy_sec_ctx(x->security, skb);
1071 }
1072 return 0;
1073}
1074
1075static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1076{
1077 if (xp->security) {
1078 return copy_sec_ctx(xp->security, skb);
1079 }
1080 return 0;
1081}
1082
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001083#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001084static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001085{
1086 struct xfrm_userpolicy_type upt;
1087
1088 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001089 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001090
1091 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1092
1093 return 0;
1094
1095rtattr_failure:
1096 return -1;
1097}
1098
1099#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001100static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001101{
1102 return 0;
1103}
1104#endif
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1107{
1108 struct xfrm_dump_info *sp = ptr;
1109 struct xfrm_userpolicy_info *p;
1110 struct sk_buff *in_skb = sp->in_skb;
1111 struct sk_buff *skb = sp->out_skb;
1112 struct nlmsghdr *nlh;
1113 unsigned char *b = skb->tail;
1114
1115 if (sp->this_idx < sp->start_idx)
1116 goto out;
1117
1118 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1119 sp->nlmsg_seq,
1120 XFRM_MSG_NEWPOLICY, sizeof(*p));
1121 p = NLMSG_DATA(nlh);
1122 nlh->nlmsg_flags = sp->nlmsg_flags;
1123
1124 copy_to_user_policy(xp, p, dir);
1125 if (copy_to_user_tmpl(xp, skb) < 0)
1126 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001127 if (copy_to_user_sec_ctx(xp, skb))
1128 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001129 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001130 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 nlh->nlmsg_len = skb->tail - b;
1133out:
1134 sp->this_idx++;
1135 return 0;
1136
1137nlmsg_failure:
1138 skb_trim(skb, b - skb->data);
1139 return -1;
1140}
1141
1142static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1143{
1144 struct xfrm_dump_info info;
1145
1146 info.in_skb = cb->skb;
1147 info.out_skb = skb;
1148 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1149 info.nlmsg_flags = NLM_F_MULTI;
1150 info.this_idx = 0;
1151 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001152 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1153#ifdef CONFIG_XFRM_SUB_POLICY
1154 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 cb->args[0] = info.this_idx;
1157
1158 return skb->len;
1159}
1160
1161static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1162 struct xfrm_policy *xp,
1163 int dir, u32 seq)
1164{
1165 struct xfrm_dump_info info;
1166 struct sk_buff *skb;
1167
1168 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1169 if (!skb)
1170 return ERR_PTR(-ENOMEM);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 info.in_skb = in_skb;
1173 info.out_skb = skb;
1174 info.nlmsg_seq = seq;
1175 info.nlmsg_flags = 0;
1176 info.this_idx = info.start_idx = 0;
1177
1178 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1179 kfree_skb(skb);
1180 return NULL;
1181 }
1182
1183 return skb;
1184}
1185
1186static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1187{
1188 struct xfrm_policy *xp;
1189 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001190 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001192 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int delete;
1194
1195 p = NLMSG_DATA(nlh);
1196 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1197
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001198 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1199 if (err)
1200 return err;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 err = verify_policy_dir(p->dir);
1203 if (err)
1204 return err;
1205
1206 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001207 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001208 else {
1209 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1210 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1211 struct xfrm_policy tmp;
1212
1213 err = verify_sec_ctx_len(rtattrs);
1214 if (err)
1215 return err;
1216
1217 memset(&tmp, 0, sizeof(struct xfrm_policy));
1218 if (rt) {
1219 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1220
1221 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1222 return err;
1223 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001224 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001225 security_xfrm_policy_free(&tmp);
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (xp == NULL)
1228 return -ENOENT;
1229
1230 if (!delete) {
1231 struct sk_buff *resp_skb;
1232
1233 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1234 if (IS_ERR(resp_skb)) {
1235 err = PTR_ERR(resp_skb);
1236 } else {
1237 err = netlink_unicast(xfrm_nl, resp_skb,
1238 NETLINK_CB(skb).pid,
1239 MSG_DONTWAIT);
1240 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001241 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001242 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001243 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001244 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001245 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001246 c.seq = nlh->nlmsg_seq;
1247 c.pid = nlh->nlmsg_pid;
1248 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 xfrm_pol_put(xp);
1252
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001253out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return err;
1255}
1256
1257static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1258{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001259 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1261
1262 xfrm_state_flush(p->proto);
Herbert Xubf088672005-06-18 22:44:00 -07001263 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001264 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001265 c.seq = nlh->nlmsg_seq;
1266 c.pid = nlh->nlmsg_pid;
1267 km_state_notify(NULL, &c);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001272
1273static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1274{
1275 struct xfrm_aevent_id *id;
1276 struct nlmsghdr *nlh;
1277 struct xfrm_lifetime_cur ltime;
1278 unsigned char *b = skb->tail;
1279
1280 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1281 id = NLMSG_DATA(nlh);
1282 nlh->nlmsg_flags = 0;
1283
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001284 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001285 id->sa_id.spi = x->id.spi;
1286 id->sa_id.family = x->props.family;
1287 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001288 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1289 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001290 id->flags = c->data.aevent;
1291
1292 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1293
1294 ltime.bytes = x->curlft.bytes;
1295 ltime.packets = x->curlft.packets;
1296 ltime.add_time = x->curlft.add_time;
1297 ltime.use_time = x->curlft.use_time;
1298
1299 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1300
1301 if (id->flags&XFRM_AE_RTHR) {
1302 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1303 }
1304
1305 if (id->flags&XFRM_AE_ETHR) {
1306 u32 etimer = x->replay_maxage*10/HZ;
1307 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1308 }
1309
1310 nlh->nlmsg_len = skb->tail - b;
1311 return skb->len;
1312
1313rtattr_failure:
1314nlmsg_failure:
1315 skb_trim(skb, b - skb->data);
1316 return -1;
1317}
1318
1319static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1320{
1321 struct xfrm_state *x;
1322 struct sk_buff *r_skb;
1323 int err;
1324 struct km_event c;
1325 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1326 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1327 struct xfrm_usersa_id *id = &p->sa_id;
1328
1329 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1330 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1331
1332 if (p->flags&XFRM_AE_RTHR)
1333 len+=RTA_SPACE(sizeof(u32));
1334
1335 if (p->flags&XFRM_AE_ETHR)
1336 len+=RTA_SPACE(sizeof(u32));
1337
1338 r_skb = alloc_skb(len, GFP_ATOMIC);
1339 if (r_skb == NULL)
1340 return -ENOMEM;
1341
1342 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1343 if (x == NULL) {
1344 kfree(r_skb);
1345 return -ESRCH;
1346 }
1347
1348 /*
1349 * XXX: is this lock really needed - none of the other
1350 * gets lock (the concern is things getting updated
1351 * while we are still reading) - jhs
1352 */
1353 spin_lock_bh(&x->lock);
1354 c.data.aevent = p->flags;
1355 c.seq = nlh->nlmsg_seq;
1356 c.pid = nlh->nlmsg_pid;
1357
1358 if (build_aevent(r_skb, x, &c) < 0)
1359 BUG();
1360 err = netlink_unicast(xfrm_nl, r_skb,
1361 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1362 spin_unlock_bh(&x->lock);
1363 xfrm_state_put(x);
1364 return err;
1365}
1366
1367static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1368{
1369 struct xfrm_state *x;
1370 struct km_event c;
1371 int err = - EINVAL;
1372 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1373 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1374 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1375
1376 if (!lt && !rp)
1377 return err;
1378
1379 /* pedantic mode - thou shalt sayeth replaceth */
1380 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1381 return err;
1382
1383 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1384 if (x == NULL)
1385 return -ESRCH;
1386
1387 if (x->km.state != XFRM_STATE_VALID)
1388 goto out;
1389
1390 spin_lock_bh(&x->lock);
1391 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1392 spin_unlock_bh(&x->lock);
1393 if (err < 0)
1394 goto out;
1395
1396 c.event = nlh->nlmsg_type;
1397 c.seq = nlh->nlmsg_seq;
1398 c.pid = nlh->nlmsg_pid;
1399 c.data.aevent = XFRM_AE_CU;
1400 km_state_notify(x, &c);
1401 err = 0;
1402out:
1403 xfrm_state_put(x);
1404 return err;
1405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1408{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001409 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001410 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001411 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001412
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001413 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1414 if (err)
1415 return err;
1416
1417 xfrm_policy_flush(type);
1418 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001419 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001420 c.seq = nlh->nlmsg_seq;
1421 c.pid = nlh->nlmsg_pid;
1422 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return 0;
1424}
1425
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001426static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1427{
1428 struct xfrm_policy *xp;
1429 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1430 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001431 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001432 int err = -ENOENT;
1433
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001434 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1435 if (err)
1436 return err;
1437
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001438 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001439 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001440 else {
1441 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1442 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1443 struct xfrm_policy tmp;
1444
1445 err = verify_sec_ctx_len(rtattrs);
1446 if (err)
1447 return err;
1448
1449 memset(&tmp, 0, sizeof(struct xfrm_policy));
1450 if (rt) {
1451 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1452
1453 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1454 return err;
1455 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001456 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001457 security_xfrm_policy_free(&tmp);
1458 }
1459
1460 if (xp == NULL)
1461 return err;
1462 read_lock(&xp->lock);
1463 if (xp->dead) {
1464 read_unlock(&xp->lock);
1465 goto out;
1466 }
1467
1468 read_unlock(&xp->lock);
1469 err = 0;
1470 if (up->hard) {
1471 xfrm_policy_delete(xp, p->dir);
1472 } else {
1473 // reset the timers here?
1474 printk("Dont know what to do with soft policy expire\n");
1475 }
1476 km_policy_expired(xp, p->dir, up->hard, current->pid);
1477
1478out:
1479 xfrm_pol_put(xp);
1480 return err;
1481}
1482
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001483static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1484{
1485 struct xfrm_state *x;
1486 int err;
1487 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1488 struct xfrm_usersa_info *p = &ue->state;
1489
1490 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1491 err = -ENOENT;
1492
1493 if (x == NULL)
1494 return err;
1495
1496 err = -EINVAL;
1497
1498 spin_lock_bh(&x->lock);
1499 if (x->km.state != XFRM_STATE_VALID)
1500 goto out;
1501 km_state_expired(x, ue->hard, current->pid);
1502
1503 if (ue->hard)
1504 __xfrm_state_delete(x);
1505out:
1506 spin_unlock_bh(&x->lock);
1507 xfrm_state_put(x);
1508 return err;
1509}
1510
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001511static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1512{
1513 struct xfrm_policy *xp;
1514 struct xfrm_user_tmpl *ut;
1515 int i;
1516 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1517
1518 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1519 struct xfrm_state *x = xfrm_state_alloc();
1520 int err = -ENOMEM;
1521
1522 if (!x)
1523 return err;
1524
1525 err = verify_newpolicy_info(&ua->policy);
1526 if (err) {
1527 printk("BAD policy passed\n");
1528 kfree(x);
1529 return err;
1530 }
1531
1532 /* build an XP */
1533 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1534 kfree(x);
1535 return err;
1536 }
1537
1538 memcpy(&x->id, &ua->id, sizeof(ua->id));
1539 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1540 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1541
1542 ut = RTA_DATA(rt);
1543 /* extract the templates and for each call km_key */
1544 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1545 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1546 memcpy(&x->id, &t->id, sizeof(x->id));
1547 x->props.mode = t->mode;
1548 x->props.reqid = t->reqid;
1549 x->props.family = ut->family;
1550 t->aalgos = ua->aalgos;
1551 t->ealgos = ua->ealgos;
1552 t->calgos = ua->calgos;
1553 err = km_query(x, t, xp);
1554
1555 }
1556
1557 kfree(x);
1558 kfree(xp);
1559
1560 return 0;
1561}
1562
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001563
Thomas Graf492b5582005-05-03 14:26:40 -07001564#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1565
1566static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1567 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1568 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1569 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1570 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1571 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1572 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1573 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001574 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001575 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001576 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1577 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001578 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001579 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1580 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001581 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1582 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001583 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584};
1585
Thomas Graf492b5582005-05-03 14:26:40 -07001586#undef XMSGSIZE
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588static struct xfrm_link {
1589 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1590 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001591} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1592 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1593 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1594 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1595 .dump = xfrm_dump_sa },
1596 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1597 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1598 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1599 .dump = xfrm_dump_policy },
1600 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001601 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001602 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001603 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1604 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001605 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001606 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1607 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001608 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1609 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610};
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1613{
1614 struct rtattr *xfrma[XFRMA_MAX];
1615 struct xfrm_link *link;
1616 int type, min_len;
1617
1618 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1619 return 0;
1620
1621 type = nlh->nlmsg_type;
1622
1623 /* A control message: ignore them */
1624 if (type < XFRM_MSG_BASE)
1625 return 0;
1626
1627 /* Unknown message: reply with EINVAL */
1628 if (type > XFRM_MSG_MAX)
1629 goto err_einval;
1630
1631 type -= XFRM_MSG_BASE;
1632 link = &xfrm_dispatch[type];
1633
1634 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001635 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 *errp = -EPERM;
1637 return -1;
1638 }
1639
Thomas Graf492b5582005-05-03 14:26:40 -07001640 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1641 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1642 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (link->dump == NULL)
1644 goto err_einval;
1645
1646 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001647 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return -1;
1649 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001650
1651 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return -1;
1653 }
1654
1655 memset(xfrma, 0, sizeof(xfrma));
1656
1657 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1658 goto err_einval;
1659
1660 if (nlh->nlmsg_len > min_len) {
1661 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1662 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1663
1664 while (RTA_OK(attr, attrlen)) {
1665 unsigned short flavor = attr->rta_type;
1666 if (flavor) {
1667 if (flavor > XFRMA_MAX)
1668 goto err_einval;
1669 xfrma[flavor - 1] = attr;
1670 }
1671 attr = RTA_NEXT(attr, attrlen);
1672 }
1673 }
1674
1675 if (link->doit == NULL)
1676 goto err_einval;
1677 *errp = link->doit(skb, nlh, (void **) &xfrma);
1678
1679 return *errp;
1680
1681err_einval:
1682 *errp = -EINVAL;
1683 return -1;
1684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686static void xfrm_netlink_rcv(struct sock *sk, int len)
1687{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001688 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001691 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001692 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001693 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001695 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 struct xfrm_user_expire *ue;
1701 struct nlmsghdr *nlh;
1702 unsigned char *b = skb->tail;
1703
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001704 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 sizeof(*ue));
1706 ue = NLMSG_DATA(nlh);
1707 nlh->nlmsg_flags = 0;
1708
1709 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001710 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 nlh->nlmsg_len = skb->tail - b;
1713 return skb->len;
1714
1715nlmsg_failure:
1716 skb_trim(skb, b - skb->data);
1717 return -1;
1718}
1719
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001720static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001723 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001725 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (skb == NULL)
1727 return -ENOMEM;
1728
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001729 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 BUG();
1731
Patrick McHardyac6d4392005-08-14 19:29:52 -07001732 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1733 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1737{
1738 struct sk_buff *skb;
1739 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1740
1741 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1742 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1743 skb = alloc_skb(len, GFP_ATOMIC);
1744 if (skb == NULL)
1745 return -ENOMEM;
1746
1747 if (build_aevent(skb, x, c) < 0)
1748 BUG();
1749
1750 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1751 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1752}
1753
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001754static int xfrm_notify_sa_flush(struct km_event *c)
1755{
1756 struct xfrm_usersa_flush *p;
1757 struct nlmsghdr *nlh;
1758 struct sk_buff *skb;
1759 unsigned char *b;
1760 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1761
1762 skb = alloc_skb(len, GFP_ATOMIC);
1763 if (skb == NULL)
1764 return -ENOMEM;
1765 b = skb->tail;
1766
1767 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1768 XFRM_MSG_FLUSHSA, sizeof(*p));
1769 nlh->nlmsg_flags = 0;
1770
1771 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001772 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001773
1774 nlh->nlmsg_len = skb->tail - b;
1775
Patrick McHardyac6d4392005-08-14 19:29:52 -07001776 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1777 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778
1779nlmsg_failure:
1780 kfree_skb(skb);
1781 return -1;
1782}
1783
1784static int inline xfrm_sa_len(struct xfrm_state *x)
1785{
Herbert Xu0603eac2005-06-18 22:54:36 -07001786 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001787 if (x->aalg)
1788 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1789 if (x->ealg)
1790 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1791 if (x->calg)
1792 l += RTA_SPACE(sizeof(*x->calg));
1793 if (x->encap)
1794 l += RTA_SPACE(sizeof(*x->encap));
1795
1796 return l;
1797}
1798
1799static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1800{
1801 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001802 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001803 struct nlmsghdr *nlh;
1804 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001805 unsigned char *b;
1806 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001807 int headlen;
1808
1809 headlen = sizeof(*p);
1810 if (c->event == XFRM_MSG_DELSA) {
1811 len += RTA_SPACE(headlen);
1812 headlen = sizeof(*id);
1813 }
1814 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001815
1816 skb = alloc_skb(len, GFP_ATOMIC);
1817 if (skb == NULL)
1818 return -ENOMEM;
1819 b = skb->tail;
1820
Herbert Xu0603eac2005-06-18 22:54:36 -07001821 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001822 nlh->nlmsg_flags = 0;
1823
1824 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001825 if (c->event == XFRM_MSG_DELSA) {
1826 id = NLMSG_DATA(nlh);
1827 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1828 id->spi = x->id.spi;
1829 id->family = x->props.family;
1830 id->proto = x->id.proto;
1831
1832 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1833 }
1834
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001835 copy_to_user_state(x, p);
1836
1837 if (x->aalg)
1838 RTA_PUT(skb, XFRMA_ALG_AUTH,
1839 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1840 if (x->ealg)
1841 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1842 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1843 if (x->calg)
1844 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1845
1846 if (x->encap)
1847 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1848
1849 nlh->nlmsg_len = skb->tail - b;
1850
Patrick McHardyac6d4392005-08-14 19:29:52 -07001851 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1852 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001853
1854nlmsg_failure:
1855rtattr_failure:
1856 kfree_skb(skb);
1857 return -1;
1858}
1859
1860static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1861{
1862
1863 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001864 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001865 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001866 case XFRM_MSG_NEWAE:
1867 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001868 case XFRM_MSG_DELSA:
1869 case XFRM_MSG_UPDSA:
1870 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001871 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001872 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001873 return xfrm_notify_sa_flush(c);
1874 default:
1875 printk("xfrm_user: Unknown SA event %d\n", c->event);
1876 break;
1877 }
1878
1879 return 0;
1880
1881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1884 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1885 int dir)
1886{
1887 struct xfrm_user_acquire *ua;
1888 struct nlmsghdr *nlh;
1889 unsigned char *b = skb->tail;
1890 __u32 seq = xfrm_get_acqseq();
1891
1892 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1893 sizeof(*ua));
1894 ua = NLMSG_DATA(nlh);
1895 nlh->nlmsg_flags = 0;
1896
1897 memcpy(&ua->id, &x->id, sizeof(ua->id));
1898 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1899 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1900 copy_to_user_policy(xp, &ua->policy, dir);
1901 ua->aalgos = xt->aalgos;
1902 ua->ealgos = xt->ealgos;
1903 ua->calgos = xt->calgos;
1904 ua->seq = x->km.seq = seq;
1905
1906 if (copy_to_user_tmpl(xp, skb) < 0)
1907 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07001908 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08001909 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001910 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001911 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 nlh->nlmsg_len = skb->tail - b;
1914 return skb->len;
1915
1916nlmsg_failure:
1917 skb_trim(skb, b - skb->data);
1918 return -1;
1919}
1920
1921static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1922 struct xfrm_policy *xp, int dir)
1923{
1924 struct sk_buff *skb;
1925 size_t len;
1926
1927 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1928 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001929 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08001930#ifdef CONFIG_XFRM_SUB_POLICY
1931 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 skb = alloc_skb(len, GFP_ATOMIC);
1934 if (skb == NULL)
1935 return -ENOMEM;
1936
1937 if (build_acquire(skb, x, xt, xp, dir) < 0)
1938 BUG();
1939
Patrick McHardyac6d4392005-08-14 19:29:52 -07001940 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1941 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944/* User gives us xfrm_user_policy_info followed by an array of 0
1945 * or more templates.
1946 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001947static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 u8 *data, int len, int *dir)
1949{
1950 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1951 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1952 struct xfrm_policy *xp;
1953 int nr;
1954
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001955 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 case AF_INET:
1957 if (opt != IP_XFRM_POLICY) {
1958 *dir = -EOPNOTSUPP;
1959 return NULL;
1960 }
1961 break;
1962#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1963 case AF_INET6:
1964 if (opt != IPV6_XFRM_POLICY) {
1965 *dir = -EOPNOTSUPP;
1966 return NULL;
1967 }
1968 break;
1969#endif
1970 default:
1971 *dir = -EINVAL;
1972 return NULL;
1973 }
1974
1975 *dir = -EINVAL;
1976
1977 if (len < sizeof(*p) ||
1978 verify_newpolicy_info(p))
1979 return NULL;
1980
1981 nr = ((len - sizeof(*p)) / sizeof(*ut));
1982 if (nr > XFRM_MAX_DEPTH)
1983 return NULL;
1984
Herbert Xua4f1bac2005-07-26 15:43:17 -07001985 if (p->dir > XFRM_POLICY_OUT)
1986 return NULL;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 xp = xfrm_policy_alloc(GFP_KERNEL);
1989 if (xp == NULL) {
1990 *dir = -ENOBUFS;
1991 return NULL;
1992 }
1993
1994 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001995 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 copy_templates(xp, ut, nr);
1997
1998 *dir = p->dir;
1999
2000 return xp;
2001}
2002
2003static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002004 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct xfrm_user_polexpire *upe;
2007 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002008 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 unsigned char *b = skb->tail;
2010
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002011 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 upe = NLMSG_DATA(nlh);
2013 nlh->nlmsg_flags = 0;
2014
2015 copy_to_user_policy(xp, &upe->pol, dir);
2016 if (copy_to_user_tmpl(xp, skb) < 0)
2017 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002018 if (copy_to_user_sec_ctx(xp, skb))
2019 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002020 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002021 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 upe->hard = !!hard;
2023
2024 nlh->nlmsg_len = skb->tail - b;
2025 return skb->len;
2026
2027nlmsg_failure:
2028 skb_trim(skb, b - skb->data);
2029 return -1;
2030}
2031
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct sk_buff *skb;
2035 size_t len;
2036
2037 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2038 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002039 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002040#ifdef CONFIG_XFRM_SUB_POLICY
2041 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 skb = alloc_skb(len, GFP_ATOMIC);
2044 if (skb == NULL)
2045 return -ENOMEM;
2046
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002047 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 BUG();
2049
Patrick McHardyac6d4392005-08-14 19:29:52 -07002050 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2051 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002054static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2055{
2056 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002057 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 struct nlmsghdr *nlh;
2059 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060 unsigned char *b;
2061 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002062 int headlen;
2063
2064 headlen = sizeof(*p);
2065 if (c->event == XFRM_MSG_DELPOLICY) {
2066 len += RTA_SPACE(headlen);
2067 headlen = sizeof(*id);
2068 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002069#ifdef CONFIG_XFRM_SUB_POLICY
2070 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2071#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002072 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073
2074 skb = alloc_skb(len, GFP_ATOMIC);
2075 if (skb == NULL)
2076 return -ENOMEM;
2077 b = skb->tail;
2078
Herbert Xu0603eac2005-06-18 22:54:36 -07002079 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002080
2081 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002082 if (c->event == XFRM_MSG_DELPOLICY) {
2083 id = NLMSG_DATA(nlh);
2084 memset(id, 0, sizeof(*id));
2085 id->dir = dir;
2086 if (c->data.byid)
2087 id->index = xp->index;
2088 else
2089 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2090
2091 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2092 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093
2094 nlh->nlmsg_flags = 0;
2095
2096 copy_to_user_policy(xp, p, dir);
2097 if (copy_to_user_tmpl(xp, skb) < 0)
2098 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002099 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002100 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002101
2102 nlh->nlmsg_len = skb->tail - b;
2103
Patrick McHardyac6d4392005-08-14 19:29:52 -07002104 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2105 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002106
2107nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002108rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002109 kfree_skb(skb);
2110 return -1;
2111}
2112
2113static int xfrm_notify_policy_flush(struct km_event *c)
2114{
2115 struct nlmsghdr *nlh;
2116 struct sk_buff *skb;
2117 unsigned char *b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002118 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002119#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002120 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002121#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002122 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002123
2124 skb = alloc_skb(len, GFP_ATOMIC);
2125 if (skb == NULL)
2126 return -ENOMEM;
2127 b = skb->tail;
2128
2129
2130 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002131 nlh->nlmsg_flags = 0;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002132 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2133 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002134
2135 nlh->nlmsg_len = skb->tail - b;
2136
Patrick McHardyac6d4392005-08-14 19:29:52 -07002137 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2138 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002139
2140nlmsg_failure:
2141 kfree_skb(skb);
2142 return -1;
2143}
2144
2145static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2146{
2147
2148 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002149 case XFRM_MSG_NEWPOLICY:
2150 case XFRM_MSG_UPDPOLICY:
2151 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002152 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002153 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002155 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002156 return xfrm_exp_policy_notify(xp, dir, c);
2157 default:
2158 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2159 }
2160
2161 return 0;
2162
2163}
2164
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002165static int build_report(struct sk_buff *skb, u8 proto,
2166 struct xfrm_selector *sel, xfrm_address_t *addr)
2167{
2168 struct xfrm_user_report *ur;
2169 struct nlmsghdr *nlh;
2170 unsigned char *b = skb->tail;
2171
2172 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2173 ur = NLMSG_DATA(nlh);
2174 nlh->nlmsg_flags = 0;
2175
2176 ur->proto = proto;
2177 memcpy(&ur->sel, sel, sizeof(ur->sel));
2178
2179 if (addr)
2180 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2181
2182 nlh->nlmsg_len = skb->tail - b;
2183 return skb->len;
2184
2185nlmsg_failure:
2186rtattr_failure:
2187 skb_trim(skb, b - skb->data);
2188 return -1;
2189}
2190
2191static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2192 xfrm_address_t *addr)
2193{
2194 struct sk_buff *skb;
2195 size_t len;
2196
2197 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2198 skb = alloc_skb(len, GFP_ATOMIC);
2199 if (skb == NULL)
2200 return -ENOMEM;
2201
2202 if (build_report(skb, proto, sel, addr) < 0)
2203 BUG();
2204
2205 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2206 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209static struct xfrm_mgr netlink_mgr = {
2210 .id = "netlink",
2211 .notify = xfrm_send_state_notify,
2212 .acquire = xfrm_send_acquire,
2213 .compile_policy = xfrm_compile_policy,
2214 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002215 .report = xfrm_send_report,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216};
2217
2218static int __init xfrm_user_init(void)
2219{
Patrick McHardybe336902006-03-20 22:40:54 -08002220 struct sock *nlsk;
2221
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002222 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Patrick McHardybe336902006-03-20 22:40:54 -08002224 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2225 xfrm_netlink_rcv, THIS_MODULE);
2226 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002228 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 xfrm_register_km(&netlink_mgr);
2231
2232 return 0;
2233}
2234
2235static void __exit xfrm_user_exit(void)
2236{
Patrick McHardybe336902006-03-20 22:40:54 -08002237 struct sock *nlsk = xfrm_nl;
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002240 rcu_assign_pointer(xfrm_nl, NULL);
2241 synchronize_rcu();
2242 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245module_init(xfrm_user_init);
2246module_exit(xfrm_user_exit);
2247MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002248MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002249