blob: 311205ffa7750747197135e23984f0adb6e5178c [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 Beltrami0a69452c2006-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;
861 for (i = 0; i < nr; i++, ut++) {
862 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
863
864 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
865 memcpy(&t->saddr, &ut->saddr,
866 sizeof(xfrm_address_t));
867 t->reqid = ut->reqid;
868 t->mode = ut->mode;
869 t->share = ut->share;
870 t->optional = ut->optional;
871 t->aalgos = ut->aalgos;
872 t->ealgos = ut->ealgos;
873 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800874 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876}
877
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800878static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
879{
880 int i;
881
882 if (nr > XFRM_MAX_DEPTH)
883 return -EINVAL;
884
885 for (i = 0; i < nr; i++) {
886 /* We never validated the ut->family value, so many
887 * applications simply leave it at zero. The check was
888 * never made and ut->family was ignored because all
889 * templates could be assumed to have the same family as
890 * the policy itself. Now that we will have ipv4-in-ipv6
891 * and ipv6-in-ipv4 tunnels, this is no longer true.
892 */
893 if (!ut[i].family)
894 ut[i].family = family;
895
896 switch (ut[i].family) {
897 case AF_INET:
898 break;
899#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
900 case AF_INET6:
901 break;
902#endif
903 default:
904 return -EINVAL;
905 };
906 }
907
908 return 0;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
912{
913 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!rt) {
916 pol->xfrm_nr = 0;
917 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800918 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
919 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
920 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800922 err = validate_tmpl(nr, utmpl, pol->family);
923 if (err)
924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 copy_templates(pol, RTA_DATA(rt), nr);
927 }
928 return 0;
929}
930
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700931static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
932{
933 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
934 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800935 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700936 int err;
937
938 if (rt) {
939 if (rt->rta_len < sizeof(*upt))
940 return -EINVAL;
941
942 upt = RTA_DATA(rt);
943 type = upt->type;
944 }
945
946 err = verify_policy_type(type);
947 if (err)
948 return err;
949
950 *tp = type;
951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
955{
956 xp->priority = p->priority;
957 xp->index = p->index;
958 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
959 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
960 xp->action = p->action;
961 xp->flags = p->flags;
962 xp->family = p->sel.family;
963 /* XXX xp->share = p->share; */
964}
965
966static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
967{
968 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
969 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
970 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
971 p->priority = xp->priority;
972 p->index = xp->index;
973 p->sel.family = xp->family;
974 p->dir = dir;
975 p->action = xp->action;
976 p->flags = xp->flags;
977 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
978}
979
980static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
981{
982 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
983 int err;
984
985 if (!xp) {
986 *errp = -ENOMEM;
987 return NULL;
988 }
989
990 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800991
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700992 err = copy_from_user_policy_type(&xp->type, xfrma);
993 if (err)
994 goto error;
995
Trent Jaegerdf718372005-12-13 23:12:27 -0800996 if (!(err = copy_from_user_tmpl(xp, xfrma)))
997 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700998 if (err)
999 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001002 error:
1003 *errp = err;
1004 kfree(xp);
1005 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1009{
1010 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1011 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001012 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int err;
1014 int excl;
1015
1016 err = verify_newpolicy_info(p);
1017 if (err)
1018 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -08001019 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1020 if (err)
1021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Trent Jaegerdf718372005-12-13 23:12:27 -08001023 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!xp)
1025 return err;
1026
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001027 /* shouldnt excl be based on nlh flags??
1028 * Aha! this is anti-netlink really i.e more pfkey derived
1029 * in netlink excl is a flag and you wouldnt need
1030 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1032 err = xfrm_policy_insert(p->dir, xp, excl);
1033 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001034 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 kfree(xp);
1036 return err;
1037 }
1038
Herbert Xuf60f6b82005-06-18 22:44:37 -07001039 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001040 c.seq = nlh->nlmsg_seq;
1041 c.pid = nlh->nlmsg_pid;
1042 km_policy_notify(xp, p->dir, &c);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 xfrm_pol_put(xp);
1045
1046 return 0;
1047}
1048
1049static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1050{
1051 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1052 int i;
1053
1054 if (xp->xfrm_nr == 0)
1055 return 0;
1056
1057 for (i = 0; i < xp->xfrm_nr; i++) {
1058 struct xfrm_user_tmpl *up = &vec[i];
1059 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1060
1061 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001062 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1064 up->reqid = kp->reqid;
1065 up->mode = kp->mode;
1066 up->share = kp->share;
1067 up->optional = kp->optional;
1068 up->aalgos = kp->aalgos;
1069 up->ealgos = kp->ealgos;
1070 up->calgos = kp->calgos;
1071 }
1072 RTA_PUT(skb, XFRMA_TMPL,
1073 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1074 vec);
1075
1076 return 0;
1077
1078rtattr_failure:
1079 return -1;
1080}
1081
Serge Hallyn0d681622006-07-24 23:30:44 -07001082static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001083{
Serge Hallyn0d681622006-07-24 23:30:44 -07001084 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1085 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1086 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001087
Serge Hallyn0d681622006-07-24 23:30:44 -07001088 uctx->exttype = XFRMA_SEC_CTX;
1089 uctx->len = ctx_size;
1090 uctx->ctx_doi = s->ctx_doi;
1091 uctx->ctx_alg = s->ctx_alg;
1092 uctx->ctx_len = s->ctx_len;
1093 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1094 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001095
1096 rtattr_failure:
1097 return -1;
1098}
1099
Serge Hallyn0d681622006-07-24 23:30:44 -07001100static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1101{
1102 if (x->security) {
1103 return copy_sec_ctx(x->security, skb);
1104 }
1105 return 0;
1106}
1107
1108static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1109{
1110 if (xp->security) {
1111 return copy_sec_ctx(xp->security, skb);
1112 }
1113 return 0;
1114}
1115
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001116#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001117static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001118{
1119 struct xfrm_userpolicy_type upt;
1120
1121 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001122 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001123
1124 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1125
1126 return 0;
1127
1128rtattr_failure:
1129 return -1;
1130}
1131
1132#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001133static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134{
1135 return 0;
1136}
1137#endif
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1140{
1141 struct xfrm_dump_info *sp = ptr;
1142 struct xfrm_userpolicy_info *p;
1143 struct sk_buff *in_skb = sp->in_skb;
1144 struct sk_buff *skb = sp->out_skb;
1145 struct nlmsghdr *nlh;
1146 unsigned char *b = skb->tail;
1147
1148 if (sp->this_idx < sp->start_idx)
1149 goto out;
1150
1151 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1152 sp->nlmsg_seq,
1153 XFRM_MSG_NEWPOLICY, sizeof(*p));
1154 p = NLMSG_DATA(nlh);
1155 nlh->nlmsg_flags = sp->nlmsg_flags;
1156
1157 copy_to_user_policy(xp, p, dir);
1158 if (copy_to_user_tmpl(xp, skb) < 0)
1159 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001160 if (copy_to_user_sec_ctx(xp, skb))
1161 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001162 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001163 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 nlh->nlmsg_len = skb->tail - b;
1166out:
1167 sp->this_idx++;
1168 return 0;
1169
1170nlmsg_failure:
1171 skb_trim(skb, b - skb->data);
1172 return -1;
1173}
1174
1175static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1176{
1177 struct xfrm_dump_info info;
1178
1179 info.in_skb = cb->skb;
1180 info.out_skb = skb;
1181 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1182 info.nlmsg_flags = NLM_F_MULTI;
1183 info.this_idx = 0;
1184 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1186#ifdef CONFIG_XFRM_SUB_POLICY
1187 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cb->args[0] = info.this_idx;
1190
1191 return skb->len;
1192}
1193
1194static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1195 struct xfrm_policy *xp,
1196 int dir, u32 seq)
1197{
1198 struct xfrm_dump_info info;
1199 struct sk_buff *skb;
1200
1201 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1202 if (!skb)
1203 return ERR_PTR(-ENOMEM);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 info.in_skb = in_skb;
1206 info.out_skb = skb;
1207 info.nlmsg_seq = seq;
1208 info.nlmsg_flags = 0;
1209 info.this_idx = info.start_idx = 0;
1210
1211 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1212 kfree_skb(skb);
1213 return NULL;
1214 }
1215
1216 return skb;
1217}
1218
1219static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1220{
1221 struct xfrm_policy *xp;
1222 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001223 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001225 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int delete;
1227
1228 p = NLMSG_DATA(nlh);
1229 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1230
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001231 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1232 if (err)
1233 return err;
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 err = verify_policy_dir(p->dir);
1236 if (err)
1237 return err;
1238
1239 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001240 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001241 else {
1242 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1243 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1244 struct xfrm_policy tmp;
1245
1246 err = verify_sec_ctx_len(rtattrs);
1247 if (err)
1248 return err;
1249
1250 memset(&tmp, 0, sizeof(struct xfrm_policy));
1251 if (rt) {
1252 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1253
1254 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1255 return err;
1256 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001257 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001258 security_xfrm_policy_free(&tmp);
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (xp == NULL)
1261 return -ENOENT;
1262
1263 if (!delete) {
1264 struct sk_buff *resp_skb;
1265
1266 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1267 if (IS_ERR(resp_skb)) {
1268 err = PTR_ERR(resp_skb);
1269 } else {
1270 err = netlink_unicast(xfrm_nl, resp_skb,
1271 NETLINK_CB(skb).pid,
1272 MSG_DONTWAIT);
1273 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001274 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001275 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001276 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001277 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001278 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001279 c.seq = nlh->nlmsg_seq;
1280 c.pid = nlh->nlmsg_pid;
1281 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 xfrm_pol_put(xp);
1285
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001286out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return err;
1288}
1289
1290static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1291{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001292 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1294
1295 xfrm_state_flush(p->proto);
Herbert Xubf08867f92005-06-18 22:44:00 -07001296 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001297 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001298 c.seq = nlh->nlmsg_seq;
1299 c.pid = nlh->nlmsg_pid;
1300 km_state_notify(NULL, &c);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303}
1304
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001305
1306static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1307{
1308 struct xfrm_aevent_id *id;
1309 struct nlmsghdr *nlh;
1310 struct xfrm_lifetime_cur ltime;
1311 unsigned char *b = skb->tail;
1312
1313 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1314 id = NLMSG_DATA(nlh);
1315 nlh->nlmsg_flags = 0;
1316
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001317 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001318 id->sa_id.spi = x->id.spi;
1319 id->sa_id.family = x->props.family;
1320 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001321 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1322 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001323 id->flags = c->data.aevent;
1324
1325 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1326
1327 ltime.bytes = x->curlft.bytes;
1328 ltime.packets = x->curlft.packets;
1329 ltime.add_time = x->curlft.add_time;
1330 ltime.use_time = x->curlft.use_time;
1331
1332 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1333
1334 if (id->flags&XFRM_AE_RTHR) {
1335 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1336 }
1337
1338 if (id->flags&XFRM_AE_ETHR) {
1339 u32 etimer = x->replay_maxage*10/HZ;
1340 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1341 }
1342
1343 nlh->nlmsg_len = skb->tail - b;
1344 return skb->len;
1345
1346rtattr_failure:
1347nlmsg_failure:
1348 skb_trim(skb, b - skb->data);
1349 return -1;
1350}
1351
1352static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1353{
1354 struct xfrm_state *x;
1355 struct sk_buff *r_skb;
1356 int err;
1357 struct km_event c;
1358 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1359 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1360 struct xfrm_usersa_id *id = &p->sa_id;
1361
1362 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1363 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1364
1365 if (p->flags&XFRM_AE_RTHR)
1366 len+=RTA_SPACE(sizeof(u32));
1367
1368 if (p->flags&XFRM_AE_ETHR)
1369 len+=RTA_SPACE(sizeof(u32));
1370
1371 r_skb = alloc_skb(len, GFP_ATOMIC);
1372 if (r_skb == NULL)
1373 return -ENOMEM;
1374
1375 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1376 if (x == NULL) {
1377 kfree(r_skb);
1378 return -ESRCH;
1379 }
1380
1381 /*
1382 * XXX: is this lock really needed - none of the other
1383 * gets lock (the concern is things getting updated
1384 * while we are still reading) - jhs
1385 */
1386 spin_lock_bh(&x->lock);
1387 c.data.aevent = p->flags;
1388 c.seq = nlh->nlmsg_seq;
1389 c.pid = nlh->nlmsg_pid;
1390
1391 if (build_aevent(r_skb, x, &c) < 0)
1392 BUG();
1393 err = netlink_unicast(xfrm_nl, r_skb,
1394 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1395 spin_unlock_bh(&x->lock);
1396 xfrm_state_put(x);
1397 return err;
1398}
1399
1400static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1401{
1402 struct xfrm_state *x;
1403 struct km_event c;
1404 int err = - EINVAL;
1405 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1406 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1407 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1408
1409 if (!lt && !rp)
1410 return err;
1411
1412 /* pedantic mode - thou shalt sayeth replaceth */
1413 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1414 return err;
1415
1416 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1417 if (x == NULL)
1418 return -ESRCH;
1419
1420 if (x->km.state != XFRM_STATE_VALID)
1421 goto out;
1422
1423 spin_lock_bh(&x->lock);
1424 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1425 spin_unlock_bh(&x->lock);
1426 if (err < 0)
1427 goto out;
1428
1429 c.event = nlh->nlmsg_type;
1430 c.seq = nlh->nlmsg_seq;
1431 c.pid = nlh->nlmsg_pid;
1432 c.data.aevent = XFRM_AE_CU;
1433 km_state_notify(x, &c);
1434 err = 0;
1435out:
1436 xfrm_state_put(x);
1437 return err;
1438}
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1441{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001442 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001443 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001444 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001445
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001446 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1447 if (err)
1448 return err;
1449
1450 xfrm_policy_flush(type);
1451 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001452 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001453 c.seq = nlh->nlmsg_seq;
1454 c.pid = nlh->nlmsg_pid;
1455 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return 0;
1457}
1458
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001459static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1460{
1461 struct xfrm_policy *xp;
1462 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1463 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001464 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001465 int err = -ENOENT;
1466
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001467 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1468 if (err)
1469 return err;
1470
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001471 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001472 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001473 else {
1474 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1475 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1476 struct xfrm_policy tmp;
1477
1478 err = verify_sec_ctx_len(rtattrs);
1479 if (err)
1480 return err;
1481
1482 memset(&tmp, 0, sizeof(struct xfrm_policy));
1483 if (rt) {
1484 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1485
1486 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1487 return err;
1488 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001489 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001490 security_xfrm_policy_free(&tmp);
1491 }
1492
1493 if (xp == NULL)
1494 return err;
1495 read_lock(&xp->lock);
1496 if (xp->dead) {
1497 read_unlock(&xp->lock);
1498 goto out;
1499 }
1500
1501 read_unlock(&xp->lock);
1502 err = 0;
1503 if (up->hard) {
1504 xfrm_policy_delete(xp, p->dir);
1505 } else {
1506 // reset the timers here?
1507 printk("Dont know what to do with soft policy expire\n");
1508 }
1509 km_policy_expired(xp, p->dir, up->hard, current->pid);
1510
1511out:
1512 xfrm_pol_put(xp);
1513 return err;
1514}
1515
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001516static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1517{
1518 struct xfrm_state *x;
1519 int err;
1520 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1521 struct xfrm_usersa_info *p = &ue->state;
1522
1523 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1524 err = -ENOENT;
1525
1526 if (x == NULL)
1527 return err;
1528
1529 err = -EINVAL;
1530
1531 spin_lock_bh(&x->lock);
1532 if (x->km.state != XFRM_STATE_VALID)
1533 goto out;
1534 km_state_expired(x, ue->hard, current->pid);
1535
1536 if (ue->hard)
1537 __xfrm_state_delete(x);
1538out:
1539 spin_unlock_bh(&x->lock);
1540 xfrm_state_put(x);
1541 return err;
1542}
1543
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001544static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1545{
1546 struct xfrm_policy *xp;
1547 struct xfrm_user_tmpl *ut;
1548 int i;
1549 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1550
1551 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1552 struct xfrm_state *x = xfrm_state_alloc();
1553 int err = -ENOMEM;
1554
1555 if (!x)
1556 return err;
1557
1558 err = verify_newpolicy_info(&ua->policy);
1559 if (err) {
1560 printk("BAD policy passed\n");
1561 kfree(x);
1562 return err;
1563 }
1564
1565 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001566 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1567 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001568 kfree(x);
1569 return err;
1570 }
1571
1572 memcpy(&x->id, &ua->id, sizeof(ua->id));
1573 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1574 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1575
1576 ut = RTA_DATA(rt);
1577 /* extract the templates and for each call km_key */
1578 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1579 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1580 memcpy(&x->id, &t->id, sizeof(x->id));
1581 x->props.mode = t->mode;
1582 x->props.reqid = t->reqid;
1583 x->props.family = ut->family;
1584 t->aalgos = ua->aalgos;
1585 t->ealgos = ua->ealgos;
1586 t->calgos = ua->calgos;
1587 err = km_query(x, t, xp);
1588
1589 }
1590
1591 kfree(x);
1592 kfree(xp);
1593
1594 return 0;
1595}
1596
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001597
Thomas Graf492b5582005-05-03 14:26:40 -07001598#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1599
1600static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1601 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1602 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1603 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1604 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1605 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1606 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1607 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001608 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001609 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001610 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1611 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001612 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001613 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1614 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001615 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1616 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001617 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618};
1619
Thomas Graf492b5582005-05-03 14:26:40 -07001620#undef XMSGSIZE
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static struct xfrm_link {
1623 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1624 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001625} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1626 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1627 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1628 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1629 .dump = xfrm_dump_sa },
1630 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1631 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1632 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1633 .dump = xfrm_dump_policy },
1634 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001635 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001636 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001637 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1638 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001639 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001640 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1641 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001642 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1643 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644};
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1647{
1648 struct rtattr *xfrma[XFRMA_MAX];
1649 struct xfrm_link *link;
1650 int type, min_len;
1651
1652 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1653 return 0;
1654
1655 type = nlh->nlmsg_type;
1656
1657 /* A control message: ignore them */
1658 if (type < XFRM_MSG_BASE)
1659 return 0;
1660
1661 /* Unknown message: reply with EINVAL */
1662 if (type > XFRM_MSG_MAX)
1663 goto err_einval;
1664
1665 type -= XFRM_MSG_BASE;
1666 link = &xfrm_dispatch[type];
1667
1668 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001669 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 *errp = -EPERM;
1671 return -1;
1672 }
1673
Thomas Graf492b5582005-05-03 14:26:40 -07001674 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1675 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1676 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (link->dump == NULL)
1678 goto err_einval;
1679
1680 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001681 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return -1;
1683 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001684
1685 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -1;
1687 }
1688
1689 memset(xfrma, 0, sizeof(xfrma));
1690
1691 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1692 goto err_einval;
1693
1694 if (nlh->nlmsg_len > min_len) {
1695 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1696 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1697
1698 while (RTA_OK(attr, attrlen)) {
1699 unsigned short flavor = attr->rta_type;
1700 if (flavor) {
1701 if (flavor > XFRMA_MAX)
1702 goto err_einval;
1703 xfrma[flavor - 1] = attr;
1704 }
1705 attr = RTA_NEXT(attr, attrlen);
1706 }
1707 }
1708
1709 if (link->doit == NULL)
1710 goto err_einval;
1711 *errp = link->doit(skb, nlh, (void **) &xfrma);
1712
1713 return *errp;
1714
1715err_einval:
1716 *errp = -EINVAL;
1717 return -1;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static void xfrm_netlink_rcv(struct sock *sk, int len)
1721{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001722 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001725 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001726 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001727 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001729 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001732static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct xfrm_user_expire *ue;
1735 struct nlmsghdr *nlh;
1736 unsigned char *b = skb->tail;
1737
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001738 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 sizeof(*ue));
1740 ue = NLMSG_DATA(nlh);
1741 nlh->nlmsg_flags = 0;
1742
1743 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 nlh->nlmsg_len = skb->tail - b;
1747 return skb->len;
1748
1749nlmsg_failure:
1750 skb_trim(skb, b - skb->data);
1751 return -1;
1752}
1753
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001754static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001757 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001759 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (skb == NULL)
1761 return -ENOMEM;
1762
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001763 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 BUG();
1765
Patrick McHardyac6d4392005-08-14 19:29:52 -07001766 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1767 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001770static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1771{
1772 struct sk_buff *skb;
1773 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1774
1775 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1776 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1777 skb = alloc_skb(len, GFP_ATOMIC);
1778 if (skb == NULL)
1779 return -ENOMEM;
1780
1781 if (build_aevent(skb, x, c) < 0)
1782 BUG();
1783
1784 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1785 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1786}
1787
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001788static int xfrm_notify_sa_flush(struct km_event *c)
1789{
1790 struct xfrm_usersa_flush *p;
1791 struct nlmsghdr *nlh;
1792 struct sk_buff *skb;
1793 unsigned char *b;
1794 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1795
1796 skb = alloc_skb(len, GFP_ATOMIC);
1797 if (skb == NULL)
1798 return -ENOMEM;
1799 b = skb->tail;
1800
1801 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1802 XFRM_MSG_FLUSHSA, sizeof(*p));
1803 nlh->nlmsg_flags = 0;
1804
1805 p = NLMSG_DATA(nlh);
Herbert Xubf08867f92005-06-18 22:44:00 -07001806 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001807
1808 nlh->nlmsg_len = skb->tail - b;
1809
Patrick McHardyac6d4392005-08-14 19:29:52 -07001810 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1811 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001812
1813nlmsg_failure:
1814 kfree_skb(skb);
1815 return -1;
1816}
1817
1818static int inline xfrm_sa_len(struct xfrm_state *x)
1819{
Herbert Xu0603eac2005-06-18 22:54:36 -07001820 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001821 if (x->aalg)
1822 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1823 if (x->ealg)
1824 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1825 if (x->calg)
1826 l += RTA_SPACE(sizeof(*x->calg));
1827 if (x->encap)
1828 l += RTA_SPACE(sizeof(*x->encap));
1829
1830 return l;
1831}
1832
1833static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1834{
1835 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001836 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001837 struct nlmsghdr *nlh;
1838 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001839 unsigned char *b;
1840 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001841 int headlen;
1842
1843 headlen = sizeof(*p);
1844 if (c->event == XFRM_MSG_DELSA) {
1845 len += RTA_SPACE(headlen);
1846 headlen = sizeof(*id);
1847 }
1848 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001849
1850 skb = alloc_skb(len, GFP_ATOMIC);
1851 if (skb == NULL)
1852 return -ENOMEM;
1853 b = skb->tail;
1854
Herbert Xu0603eac2005-06-18 22:54:36 -07001855 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001856 nlh->nlmsg_flags = 0;
1857
1858 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001859 if (c->event == XFRM_MSG_DELSA) {
1860 id = NLMSG_DATA(nlh);
1861 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1862 id->spi = x->id.spi;
1863 id->family = x->props.family;
1864 id->proto = x->id.proto;
1865
1866 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1867 }
1868
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001869 copy_to_user_state(x, p);
1870
1871 if (x->aalg)
1872 RTA_PUT(skb, XFRMA_ALG_AUTH,
1873 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1874 if (x->ealg)
1875 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1876 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1877 if (x->calg)
1878 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1879
1880 if (x->encap)
1881 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1882
1883 nlh->nlmsg_len = skb->tail - b;
1884
Patrick McHardyac6d4392005-08-14 19:29:52 -07001885 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1886 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001887
1888nlmsg_failure:
1889rtattr_failure:
1890 kfree_skb(skb);
1891 return -1;
1892}
1893
1894static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1895{
1896
1897 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001898 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001899 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001900 case XFRM_MSG_NEWAE:
1901 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001902 case XFRM_MSG_DELSA:
1903 case XFRM_MSG_UPDSA:
1904 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001905 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001906 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001907 return xfrm_notify_sa_flush(c);
1908 default:
1909 printk("xfrm_user: Unknown SA event %d\n", c->event);
1910 break;
1911 }
1912
1913 return 0;
1914
1915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1918 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1919 int dir)
1920{
1921 struct xfrm_user_acquire *ua;
1922 struct nlmsghdr *nlh;
1923 unsigned char *b = skb->tail;
1924 __u32 seq = xfrm_get_acqseq();
1925
1926 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1927 sizeof(*ua));
1928 ua = NLMSG_DATA(nlh);
1929 nlh->nlmsg_flags = 0;
1930
1931 memcpy(&ua->id, &x->id, sizeof(ua->id));
1932 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1933 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1934 copy_to_user_policy(xp, &ua->policy, dir);
1935 ua->aalgos = xt->aalgos;
1936 ua->ealgos = xt->ealgos;
1937 ua->calgos = xt->calgos;
1938 ua->seq = x->km.seq = seq;
1939
1940 if (copy_to_user_tmpl(xp, skb) < 0)
1941 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07001942 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08001943 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001944 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001945 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 nlh->nlmsg_len = skb->tail - b;
1948 return skb->len;
1949
1950nlmsg_failure:
1951 skb_trim(skb, b - skb->data);
1952 return -1;
1953}
1954
1955static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1956 struct xfrm_policy *xp, int dir)
1957{
1958 struct sk_buff *skb;
1959 size_t len;
1960
1961 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1962 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001963 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08001964#ifdef CONFIG_XFRM_SUB_POLICY
1965 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 skb = alloc_skb(len, GFP_ATOMIC);
1968 if (skb == NULL)
1969 return -ENOMEM;
1970
1971 if (build_acquire(skb, x, xt, xp, dir) < 0)
1972 BUG();
1973
Patrick McHardyac6d4392005-08-14 19:29:52 -07001974 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1975 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978/* User gives us xfrm_user_policy_info followed by an array of 0
1979 * or more templates.
1980 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001981static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 u8 *data, int len, int *dir)
1983{
1984 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1985 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1986 struct xfrm_policy *xp;
1987 int nr;
1988
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001989 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 case AF_INET:
1991 if (opt != IP_XFRM_POLICY) {
1992 *dir = -EOPNOTSUPP;
1993 return NULL;
1994 }
1995 break;
1996#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1997 case AF_INET6:
1998 if (opt != IPV6_XFRM_POLICY) {
1999 *dir = -EOPNOTSUPP;
2000 return NULL;
2001 }
2002 break;
2003#endif
2004 default:
2005 *dir = -EINVAL;
2006 return NULL;
2007 }
2008
2009 *dir = -EINVAL;
2010
2011 if (len < sizeof(*p) ||
2012 verify_newpolicy_info(p))
2013 return NULL;
2014
2015 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002016 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return NULL;
2018
Herbert Xua4f1bac2005-07-26 15:43:17 -07002019 if (p->dir > XFRM_POLICY_OUT)
2020 return NULL;
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 xp = xfrm_policy_alloc(GFP_KERNEL);
2023 if (xp == NULL) {
2024 *dir = -ENOBUFS;
2025 return NULL;
2026 }
2027
2028 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002029 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 copy_templates(xp, ut, nr);
2031
2032 *dir = p->dir;
2033
2034 return xp;
2035}
2036
2037static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002038 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct xfrm_user_polexpire *upe;
2041 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002042 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 unsigned char *b = skb->tail;
2044
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002045 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 upe = NLMSG_DATA(nlh);
2047 nlh->nlmsg_flags = 0;
2048
2049 copy_to_user_policy(xp, &upe->pol, dir);
2050 if (copy_to_user_tmpl(xp, skb) < 0)
2051 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002052 if (copy_to_user_sec_ctx(xp, skb))
2053 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002054 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002055 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 upe->hard = !!hard;
2057
2058 nlh->nlmsg_len = skb->tail - b;
2059 return skb->len;
2060
2061nlmsg_failure:
2062 skb_trim(skb, b - skb->data);
2063 return -1;
2064}
2065
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct sk_buff *skb;
2069 size_t len;
2070
2071 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2072 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002073 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002074#ifdef CONFIG_XFRM_SUB_POLICY
2075 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 skb = alloc_skb(len, GFP_ATOMIC);
2078 if (skb == NULL)
2079 return -ENOMEM;
2080
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002081 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 BUG();
2083
Patrick McHardyac6d4392005-08-14 19:29:52 -07002084 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2085 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002088static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2089{
2090 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002091 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002092 struct nlmsghdr *nlh;
2093 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094 unsigned char *b;
2095 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002096 int headlen;
2097
2098 headlen = sizeof(*p);
2099 if (c->event == XFRM_MSG_DELPOLICY) {
2100 len += RTA_SPACE(headlen);
2101 headlen = sizeof(*id);
2102 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002103#ifdef CONFIG_XFRM_SUB_POLICY
2104 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2105#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002106 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002107
2108 skb = alloc_skb(len, GFP_ATOMIC);
2109 if (skb == NULL)
2110 return -ENOMEM;
2111 b = skb->tail;
2112
Herbert Xu0603eac2005-06-18 22:54:36 -07002113 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002114
2115 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002116 if (c->event == XFRM_MSG_DELPOLICY) {
2117 id = NLMSG_DATA(nlh);
2118 memset(id, 0, sizeof(*id));
2119 id->dir = dir;
2120 if (c->data.byid)
2121 id->index = xp->index;
2122 else
2123 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2124
2125 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2126 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002127
2128 nlh->nlmsg_flags = 0;
2129
2130 copy_to_user_policy(xp, p, dir);
2131 if (copy_to_user_tmpl(xp, skb) < 0)
2132 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002133 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002134 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002135
2136 nlh->nlmsg_len = skb->tail - b;
2137
Patrick McHardyac6d4392005-08-14 19:29:52 -07002138 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2139 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140
2141nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002142rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002143 kfree_skb(skb);
2144 return -1;
2145}
2146
2147static int xfrm_notify_policy_flush(struct km_event *c)
2148{
2149 struct nlmsghdr *nlh;
2150 struct sk_buff *skb;
2151 unsigned char *b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002152 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002153#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002154 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002155#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002156 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002157
2158 skb = alloc_skb(len, GFP_ATOMIC);
2159 if (skb == NULL)
2160 return -ENOMEM;
2161 b = skb->tail;
2162
2163
2164 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002165 nlh->nlmsg_flags = 0;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002166 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2167 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002168
2169 nlh->nlmsg_len = skb->tail - b;
2170
Patrick McHardyac6d4392005-08-14 19:29:52 -07002171 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2172 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002173
2174nlmsg_failure:
2175 kfree_skb(skb);
2176 return -1;
2177}
2178
2179static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2180{
2181
2182 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002183 case XFRM_MSG_NEWPOLICY:
2184 case XFRM_MSG_UPDPOLICY:
2185 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002186 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002187 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002189 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002190 return xfrm_exp_policy_notify(xp, dir, c);
2191 default:
2192 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2193 }
2194
2195 return 0;
2196
2197}
2198
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002199static int build_report(struct sk_buff *skb, u8 proto,
2200 struct xfrm_selector *sel, xfrm_address_t *addr)
2201{
2202 struct xfrm_user_report *ur;
2203 struct nlmsghdr *nlh;
2204 unsigned char *b = skb->tail;
2205
2206 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2207 ur = NLMSG_DATA(nlh);
2208 nlh->nlmsg_flags = 0;
2209
2210 ur->proto = proto;
2211 memcpy(&ur->sel, sel, sizeof(ur->sel));
2212
2213 if (addr)
2214 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2215
2216 nlh->nlmsg_len = skb->tail - b;
2217 return skb->len;
2218
2219nlmsg_failure:
2220rtattr_failure:
2221 skb_trim(skb, b - skb->data);
2222 return -1;
2223}
2224
2225static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2226 xfrm_address_t *addr)
2227{
2228 struct sk_buff *skb;
2229 size_t len;
2230
2231 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2232 skb = alloc_skb(len, GFP_ATOMIC);
2233 if (skb == NULL)
2234 return -ENOMEM;
2235
2236 if (build_report(skb, proto, sel, addr) < 0)
2237 BUG();
2238
2239 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2240 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243static struct xfrm_mgr netlink_mgr = {
2244 .id = "netlink",
2245 .notify = xfrm_send_state_notify,
2246 .acquire = xfrm_send_acquire,
2247 .compile_policy = xfrm_compile_policy,
2248 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002249 .report = xfrm_send_report,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250};
2251
2252static int __init xfrm_user_init(void)
2253{
Patrick McHardybe336902006-03-20 22:40:54 -08002254 struct sock *nlsk;
2255
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002256 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Patrick McHardybe336902006-03-20 22:40:54 -08002258 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2259 xfrm_netlink_rcv, THIS_MODULE);
2260 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002262 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 xfrm_register_km(&netlink_mgr);
2265
2266 return 0;
2267}
2268
2269static void __exit xfrm_user_exit(void)
2270{
Patrick McHardybe336902006-03-20 22:40:54 -08002271 struct sock *nlsk = xfrm_nl;
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002274 rcu_assign_pointer(xfrm_nl, NULL);
2275 synchronize_rcu();
2276 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279module_init(xfrm_user_init);
2280module_exit(xfrm_user_exit);
2281MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002282MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002283