blob: e5372b11fc8f105bd4ecf6071c0418f71855760c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/rtnetlink.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#include <linux/in6.h>
33#endif
Joy Latten161a09e2006-11-27 13:11:54 -060034#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
37{
38 struct rtattr *rt = xfrma[type - 1];
39 struct xfrm_algo *algp;
Herbert Xu31c26852005-05-19 12:39:49 -070040 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (!rt)
43 return 0;
44
Herbert Xu31c26852005-05-19 12:39:49 -070045 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
46 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return -EINVAL;
48
49 algp = RTA_DATA(rt);
Herbert Xu31c26852005-05-19 12:39:49 -070050
51 len -= (algp->alg_key_len + 7U) / 8;
52 if (len < 0)
53 return -EINVAL;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 switch (type) {
56 case XFRMA_ALG_AUTH:
57 if (!algp->alg_key_len &&
58 strcmp(algp->alg_name, "digest_null") != 0)
59 return -EINVAL;
60 break;
61
62 case XFRMA_ALG_CRYPT:
63 if (!algp->alg_key_len &&
64 strcmp(algp->alg_name, "cipher_null") != 0)
65 return -EINVAL;
66 break;
67
68 case XFRMA_ALG_COMP:
69 /* Zero length keys are legal. */
70 break;
71
72 default:
73 return -EINVAL;
74 };
75
76 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 return 0;
78}
79
80static int verify_encap_tmpl(struct rtattr **xfrma)
81{
82 struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
83 struct xfrm_encap_tmpl *encap;
84
85 if (!rt)
86 return 0;
87
88 if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
89 return -EINVAL;
90
91 return 0;
92}
93
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
96{
97 struct rtattr *rt = xfrma[type - 1];
98
99 if (!rt)
100 return 0;
101
102 if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
103 return -EINVAL;
104
105 if (addrp)
106 *addrp = RTA_DATA(rt);
107
108 return 0;
109}
Trent Jaegerdf718372005-12-13 23:12:27 -0800110
111static inline int verify_sec_ctx_len(struct rtattr **xfrma)
112{
113 struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
114 struct xfrm_user_sec_ctx *uctx;
115 int len = 0;
116
117 if (!rt)
118 return 0;
119
120 if (rt->rta_len < sizeof(*uctx))
121 return -EINVAL;
122
123 uctx = RTA_DATA(rt);
124
Trent Jaegerdf718372005-12-13 23:12:27 -0800125 len += sizeof(struct xfrm_user_sec_ctx);
126 len += uctx->ctx_len;
127
128 if (uctx->len != len)
129 return -EINVAL;
130
131 return 0;
132}
133
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int verify_newsa_info(struct xfrm_usersa_info *p,
136 struct rtattr **xfrma)
137{
138 int err;
139
140 err = -EINVAL;
141 switch (p->family) {
142 case AF_INET:
143 break;
144
145 case AF_INET6:
146#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 break;
148#else
149 err = -EAFNOSUPPORT;
150 goto out;
151#endif
152
153 default:
154 goto out;
155 };
156
157 err = -EINVAL;
158 switch (p->id.proto) {
159 case IPPROTO_AH:
160 if (!xfrma[XFRMA_ALG_AUTH-1] ||
161 xfrma[XFRMA_ALG_CRYPT-1] ||
162 xfrma[XFRMA_ALG_COMP-1])
163 goto out;
164 break;
165
166 case IPPROTO_ESP:
167 if ((!xfrma[XFRMA_ALG_AUTH-1] &&
168 !xfrma[XFRMA_ALG_CRYPT-1]) ||
169 xfrma[XFRMA_ALG_COMP-1])
170 goto out;
171 break;
172
173 case IPPROTO_COMP:
174 if (!xfrma[XFRMA_ALG_COMP-1] ||
175 xfrma[XFRMA_ALG_AUTH-1] ||
176 xfrma[XFRMA_ALG_CRYPT-1])
177 goto out;
178 break;
179
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700180#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181 case IPPROTO_DSTOPTS:
182 case IPPROTO_ROUTING:
183 if (xfrma[XFRMA_ALG_COMP-1] ||
184 xfrma[XFRMA_ALG_AUTH-1] ||
185 xfrma[XFRMA_ALG_CRYPT-1] ||
186 xfrma[XFRMA_ENCAP-1] ||
187 xfrma[XFRMA_SEC_CTX-1] ||
188 !xfrma[XFRMA_COADDR-1])
189 goto out;
190 break;
191#endif
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 default:
194 goto out;
195 };
196
197 if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
198 goto out;
199 if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
200 goto out;
201 if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
202 goto out;
203 if ((err = verify_encap_tmpl(xfrma)))
204 goto out;
Trent Jaegerdf718372005-12-13 23:12:27 -0800205 if ((err = verify_sec_ctx_len(xfrma)))
206 goto out;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700207 if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 err = -EINVAL;
211 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700212 case XFRM_MODE_TRANSPORT:
213 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700214 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700215 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217
218 default:
219 goto out;
220 };
221
222 err = 0;
223
224out:
225 return err;
226}
227
228static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
229 struct xfrm_algo_desc *(*get_byname)(char *, int),
230 struct rtattr *u_arg)
231{
232 struct rtattr *rta = u_arg;
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
Herbert Xub9e9dea2005-05-19 12:39:04 -0700235 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!rta)
238 return 0;
239
240 ualg = RTA_DATA(rta);
241
242 algo = get_byname(ualg->alg_name, 1);
243 if (!algo)
244 return -ENOSYS;
245 *props = algo->desc.sadb_alg_id;
246
Herbert Xub9e9dea2005-05-19 12:39:04 -0700247 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200248 p = kmemdup(ualg, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!p)
250 return -ENOMEM;
251
Herbert Xu04ff1262006-08-13 08:50:00 +1000252 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *algpp = p;
254 return 0;
255}
256
257static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
258{
259 struct rtattr *rta = u_arg;
260 struct xfrm_encap_tmpl *p, *uencap;
261
262 if (!rta)
263 return 0;
264
265 uencap = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200266 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!p)
268 return -ENOMEM;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *encapp = p;
271 return 0;
272}
273
Trent Jaegerdf718372005-12-13 23:12:27 -0800274
275static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
276{
277 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
278 int len = 0;
279
280 if (xfrm_ctx) {
281 len += sizeof(struct xfrm_user_sec_ctx);
282 len += xfrm_ctx->ctx_len;
283 }
284 return len;
285}
286
287static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
288{
289 struct xfrm_user_sec_ctx *uctx;
290
291 if (!u_arg)
292 return 0;
293
294 uctx = RTA_DATA(u_arg);
295 return security_xfrm_state_alloc(x, uctx);
296}
297
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700298static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
299{
300 struct rtattr *rta = u_arg;
301 xfrm_address_t *p, *uaddrp;
302
303 if (!rta)
304 return 0;
305
306 uaddrp = RTA_DATA(rta);
Arnaldo Carvalho de Melocdbc6da2006-11-21 01:22:51 -0200307 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700308 if (!p)
309 return -ENOMEM;
310
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700311 *addrpp = p;
312 return 0;
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
316{
317 memcpy(&x->id, &p->id, sizeof(x->id));
318 memcpy(&x->sel, &p->sel, sizeof(x->sel));
319 memcpy(&x->lft, &p->lft, sizeof(x->lft));
320 x->props.mode = p->mode;
321 x->props.replay_window = p->replay_window;
322 x->props.reqid = p->reqid;
323 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700324 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 x->props.flags = p->flags;
326}
327
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800328/*
329 * someday when pfkey also has support, we could have the code
330 * somehow made shareable and move it to xfrm_state.c - JHS
331 *
332*/
333static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
334{
335 int err = - EINVAL;
336 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
337 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
338 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
339 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
340
341 if (rp) {
342 struct xfrm_replay_state *replay;
343 if (RTA_PAYLOAD(rp) < sizeof(*replay))
344 goto error;
345 replay = RTA_DATA(rp);
346 memcpy(&x->replay, replay, sizeof(*replay));
347 memcpy(&x->preplay, replay, sizeof(*replay));
348 }
349
350 if (lt) {
351 struct xfrm_lifetime_cur *ltime;
352 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
353 goto error;
354 ltime = RTA_DATA(lt);
355 x->curlft.bytes = ltime->bytes;
356 x->curlft.packets = ltime->packets;
357 x->curlft.add_time = ltime->add_time;
358 x->curlft.use_time = ltime->use_time;
359 }
360
361 if (et) {
362 if (RTA_PAYLOAD(et) < sizeof(u32))
363 goto error;
364 x->replay_maxage = *(u32*)RTA_DATA(et);
365 }
366
367 if (rt) {
368 if (RTA_PAYLOAD(rt) < sizeof(u32))
369 goto error;
370 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
371 }
372
373 return 0;
374error:
375 return err;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
379 struct rtattr **xfrma,
380 int *errp)
381{
382 struct xfrm_state *x = xfrm_state_alloc();
383 int err = -ENOMEM;
384
385 if (!x)
386 goto error_no_put;
387
388 copy_from_user_state(x, p);
389
390 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
391 xfrm_aalg_get_byname,
392 xfrma[XFRMA_ALG_AUTH-1])))
393 goto error;
394 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
395 xfrm_ealg_get_byname,
396 xfrma[XFRMA_ALG_CRYPT-1])))
397 goto error;
398 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
399 xfrm_calg_get_byname,
400 xfrma[XFRMA_ALG_COMP-1])))
401 goto error;
402 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
403 goto error;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700404 if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
405 goto error;
Herbert Xu72cb6962005-06-20 13:18:08 -0700406 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (err)
408 goto error;
409
Trent Jaegerdf718372005-12-13 23:12:27 -0800410 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
411 goto error;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800414 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
415 /* sysctl_xfrm_aevent_etime is in 100ms units */
416 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
417 x->preplay.bitmap = 0;
418 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
419 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
420
421 /* override default values from above */
422
423 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
424 if (err < 0)
425 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return x;
428
429error:
430 x->km.state = XFRM_STATE_DEAD;
431 xfrm_state_put(x);
432error_no_put:
433 *errp = err;
434 return NULL;
435}
436
437static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
438{
439 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
440 struct xfrm_state *x;
441 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Trent Jaegerdf718372005-12-13 23:12:27 -0800444 err = verify_newsa_info(p, (struct rtattr **)xfrma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (err)
446 return err;
447
Trent Jaegerdf718372005-12-13 23:12:27 -0800448 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!x)
450 return err;
451
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700452 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
454 err = xfrm_state_add(x);
455 else
456 err = xfrm_state_update(x);
457
Joy Latten161a09e2006-11-27 13:11:54 -0600458 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
459 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (err < 0) {
462 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800463 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700467 c.seq = nlh->nlmsg_seq;
468 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700469 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700470
471 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700472out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700473 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return err;
475}
476
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700477static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
478 struct rtattr **xfrma,
479 int *errp)
480{
481 struct xfrm_state *x = NULL;
482 int err;
483
484 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
485 err = -ESRCH;
486 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
487 } else {
488 xfrm_address_t *saddr = NULL;
489
490 err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
491 if (err)
492 goto out;
493
494 if (!saddr) {
495 err = -EINVAL;
496 goto out;
497 }
498
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800499 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700500 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
501 p->family);
502 }
503
504 out:
505 if (!x && errp)
506 *errp = err;
507 return x;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
511{
512 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700513 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700514 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
516
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700517 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David S. Miller6f68dc32006-06-08 23:58:52 -0700521 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700522 goto out;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700525 err = -EPERM;
526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700529 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600530
531 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
532 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
533
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700534 if (err < 0)
535 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700536
537 c.seq = nlh->nlmsg_seq;
538 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700539 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700540 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700542out:
543 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
548{
549 memcpy(&p->id, &x->id, sizeof(p->id));
550 memcpy(&p->sel, &x->sel, sizeof(p->sel));
551 memcpy(&p->lft, &x->lft, sizeof(p->lft));
552 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
553 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700554 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 p->mode = x->props.mode;
556 p->replay_window = x->props.replay_window;
557 p->reqid = x->props.reqid;
558 p->family = x->props.family;
559 p->flags = x->props.flags;
560 p->seq = x->km.seq;
561}
562
563struct xfrm_dump_info {
564 struct sk_buff *in_skb;
565 struct sk_buff *out_skb;
566 u32 nlmsg_seq;
567 u16 nlmsg_flags;
568 int start_idx;
569 int this_idx;
570};
571
572static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
573{
574 struct xfrm_dump_info *sp = ptr;
575 struct sk_buff *in_skb = sp->in_skb;
576 struct sk_buff *skb = sp->out_skb;
577 struct xfrm_usersa_info *p;
578 struct nlmsghdr *nlh;
579 unsigned char *b = skb->tail;
580
581 if (sp->this_idx < sp->start_idx)
582 goto out;
583
584 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
585 sp->nlmsg_seq,
586 XFRM_MSG_NEWSA, sizeof(*p));
587 nlh->nlmsg_flags = sp->nlmsg_flags;
588
589 p = NLMSG_DATA(nlh);
590 copy_to_user_state(x, p);
591
592 if (x->aalg)
593 RTA_PUT(skb, XFRMA_ALG_AUTH,
594 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
595 if (x->ealg)
596 RTA_PUT(skb, XFRMA_ALG_CRYPT,
597 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
598 if (x->calg)
599 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
600
601 if (x->encap)
602 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
603
Trent Jaegerdf718372005-12-13 23:12:27 -0800604 if (x->security) {
605 int ctx_size = sizeof(struct xfrm_sec_ctx) +
606 x->security->ctx_len;
607 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
608 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
609
610 uctx->exttype = XFRMA_SEC_CTX;
611 uctx->len = ctx_size;
612 uctx->ctx_doi = x->security->ctx_doi;
613 uctx->ctx_alg = x->security->ctx_alg;
614 uctx->ctx_len = x->security->ctx_len;
615 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
616 }
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700617
618 if (x->coaddr)
619 RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
620
Masahide NAKAMURA9afaca02006-08-23 18:20:16 -0700621 if (x->lastused)
622 RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 nlh->nlmsg_len = skb->tail - b;
625out:
626 sp->this_idx++;
627 return 0;
628
629nlmsg_failure:
630rtattr_failure:
631 skb_trim(skb, b - skb->data);
632 return -1;
633}
634
635static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
636{
637 struct xfrm_dump_info info;
638
639 info.in_skb = cb->skb;
640 info.out_skb = skb;
641 info.nlmsg_seq = cb->nlh->nlmsg_seq;
642 info.nlmsg_flags = NLM_F_MULTI;
643 info.this_idx = 0;
644 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700645 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 cb->args[0] = info.this_idx;
647
648 return skb->len;
649}
650
651static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
652 struct xfrm_state *x, u32 seq)
653{
654 struct xfrm_dump_info info;
655 struct sk_buff *skb;
656
657 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
658 if (!skb)
659 return ERR_PTR(-ENOMEM);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 info.in_skb = in_skb;
662 info.out_skb = skb;
663 info.nlmsg_seq = seq;
664 info.nlmsg_flags = 0;
665 info.this_idx = info.start_idx = 0;
666
667 if (dump_one_state(x, 0, &info)) {
668 kfree_skb(skb);
669 return NULL;
670 }
671
672 return skb;
673}
674
675static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
676{
677 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
678 struct xfrm_state *x;
679 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700680 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700682 x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (x == NULL)
684 goto out_noput;
685
686 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
687 if (IS_ERR(resp_skb)) {
688 err = PTR_ERR(resp_skb);
689 } else {
690 err = netlink_unicast(xfrm_nl, resp_skb,
691 NETLINK_CB(skb).pid, MSG_DONTWAIT);
692 }
693 xfrm_state_put(x);
694out_noput:
695 return err;
696}
697
698static int verify_userspi_info(struct xfrm_userspi_info *p)
699{
700 switch (p->info.id.proto) {
701 case IPPROTO_AH:
702 case IPPROTO_ESP:
703 break;
704
705 case IPPROTO_COMP:
706 /* IPCOMP spi is 16-bits. */
707 if (p->max >= 0x10000)
708 return -EINVAL;
709 break;
710
711 default:
712 return -EINVAL;
713 };
714
715 if (p->min > p->max)
716 return -EINVAL;
717
718 return 0;
719}
720
721static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
722{
723 struct xfrm_state *x;
724 struct xfrm_userspi_info *p;
725 struct sk_buff *resp_skb;
726 xfrm_address_t *daddr;
727 int family;
728 int err;
729
730 p = NLMSG_DATA(nlh);
731 err = verify_userspi_info(p);
732 if (err)
733 goto out_noput;
734
735 family = p->info.family;
736 daddr = &p->info.id.daddr;
737
738 x = NULL;
739 if (p->info.seq) {
740 x = xfrm_find_acq_byseq(p->info.seq);
741 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
742 xfrm_state_put(x);
743 x = NULL;
744 }
745 }
746
747 if (!x)
748 x = xfrm_find_acq(p->info.mode, p->info.reqid,
749 p->info.id.proto, daddr,
750 &p->info.saddr, 1,
751 family);
752 err = -ENOENT;
753 if (x == NULL)
754 goto out_noput;
755
756 resp_skb = ERR_PTR(-ENOENT);
757
758 spin_lock_bh(&x->lock);
759 if (x->km.state != XFRM_STATE_DEAD) {
760 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
761 if (x->id.spi)
762 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
763 }
764 spin_unlock_bh(&x->lock);
765
766 if (IS_ERR(resp_skb)) {
767 err = PTR_ERR(resp_skb);
768 goto out;
769 }
770
771 err = netlink_unicast(xfrm_nl, resp_skb,
772 NETLINK_CB(skb).pid, MSG_DONTWAIT);
773
774out:
775 xfrm_state_put(x);
776out_noput:
777 return err;
778}
779
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800780static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 switch (dir) {
783 case XFRM_POLICY_IN:
784 case XFRM_POLICY_OUT:
785 case XFRM_POLICY_FWD:
786 break;
787
788 default:
789 return -EINVAL;
790 };
791
792 return 0;
793}
794
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800795static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700796{
797 switch (type) {
798 case XFRM_POLICY_TYPE_MAIN:
799#ifdef CONFIG_XFRM_SUB_POLICY
800 case XFRM_POLICY_TYPE_SUB:
801#endif
802 break;
803
804 default:
805 return -EINVAL;
806 };
807
808 return 0;
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
812{
813 switch (p->share) {
814 case XFRM_SHARE_ANY:
815 case XFRM_SHARE_SESSION:
816 case XFRM_SHARE_USER:
817 case XFRM_SHARE_UNIQUE:
818 break;
819
820 default:
821 return -EINVAL;
822 };
823
824 switch (p->action) {
825 case XFRM_POLICY_ALLOW:
826 case XFRM_POLICY_BLOCK:
827 break;
828
829 default:
830 return -EINVAL;
831 };
832
833 switch (p->sel.family) {
834 case AF_INET:
835 break;
836
837 case AF_INET6:
838#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
839 break;
840#else
841 return -EAFNOSUPPORT;
842#endif
843
844 default:
845 return -EINVAL;
846 };
847
848 return verify_policy_dir(p->dir);
849}
850
Trent Jaegerdf718372005-12-13 23:12:27 -0800851static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
852{
853 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
854 struct xfrm_user_sec_ctx *uctx;
855
856 if (!rt)
857 return 0;
858
859 uctx = RTA_DATA(rt);
860 return security_xfrm_policy_alloc(pol, uctx);
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
864 int nr)
865{
866 int i;
867
868 xp->xfrm_nr = nr;
869 for (i = 0; i < nr; i++, ut++) {
870 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
871
872 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
873 memcpy(&t->saddr, &ut->saddr,
874 sizeof(xfrm_address_t));
875 t->reqid = ut->reqid;
876 t->mode = ut->mode;
877 t->share = ut->share;
878 t->optional = ut->optional;
879 t->aalgos = ut->aalgos;
880 t->ealgos = ut->ealgos;
881 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800882 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884}
885
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800886static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
887{
888 int i;
889
890 if (nr > XFRM_MAX_DEPTH)
891 return -EINVAL;
892
893 for (i = 0; i < nr; i++) {
894 /* We never validated the ut->family value, so many
895 * applications simply leave it at zero. The check was
896 * never made and ut->family was ignored because all
897 * templates could be assumed to have the same family as
898 * the policy itself. Now that we will have ipv4-in-ipv6
899 * and ipv6-in-ipv4 tunnels, this is no longer true.
900 */
901 if (!ut[i].family)
902 ut[i].family = family;
903
904 switch (ut[i].family) {
905 case AF_INET:
906 break;
907#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
908 case AF_INET6:
909 break;
910#endif
911 default:
912 return -EINVAL;
913 };
914 }
915
916 return 0;
917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
920{
921 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!rt) {
924 pol->xfrm_nr = 0;
925 } else {
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800926 struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
927 int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
928 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800930 err = validate_tmpl(nr, utmpl, pol->family);
931 if (err)
932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 copy_templates(pol, RTA_DATA(rt), nr);
935 }
936 return 0;
937}
938
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700939static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
940{
941 struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
942 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800943 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700944 int err;
945
946 if (rt) {
947 if (rt->rta_len < sizeof(*upt))
948 return -EINVAL;
949
950 upt = RTA_DATA(rt);
951 type = upt->type;
952 }
953
954 err = verify_policy_type(type);
955 if (err)
956 return err;
957
958 *tp = type;
959 return 0;
960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
963{
964 xp->priority = p->priority;
965 xp->index = p->index;
966 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
967 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
968 xp->action = p->action;
969 xp->flags = p->flags;
970 xp->family = p->sel.family;
971 /* XXX xp->share = p->share; */
972}
973
974static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
975{
976 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
977 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
978 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
979 p->priority = xp->priority;
980 p->index = xp->index;
981 p->sel.family = xp->family;
982 p->dir = dir;
983 p->action = xp->action;
984 p->flags = xp->flags;
985 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
986}
987
988static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
989{
990 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
991 int err;
992
993 if (!xp) {
994 *errp = -ENOMEM;
995 return NULL;
996 }
997
998 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -0800999
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001000 err = copy_from_user_policy_type(&xp->type, xfrma);
1001 if (err)
1002 goto error;
1003
Trent Jaegerdf718372005-12-13 23:12:27 -08001004 if (!(err = copy_from_user_tmpl(xp, xfrma)))
1005 err = copy_from_user_sec_ctx(xp, xfrma);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001006 if (err)
1007 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001010 error:
1011 *errp = err;
1012 kfree(xp);
1013 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1017{
1018 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
1019 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001020 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int err;
1022 int excl;
1023
1024 err = verify_newpolicy_info(p);
1025 if (err)
1026 return err;
Trent Jaegerdf718372005-12-13 23:12:27 -08001027 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1028 if (err)
1029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Trent Jaegerdf718372005-12-13 23:12:27 -08001031 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!xp)
1033 return err;
1034
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001035 /* shouldnt excl be based on nlh flags??
1036 * Aha! this is anti-netlink really i.e more pfkey derived
1037 * in netlink excl is a flag and you wouldnt need
1038 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1040 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Latten161a09e2006-11-27 13:11:54 -06001041 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1042 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001045 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree(xp);
1047 return err;
1048 }
1049
Herbert Xuf60f6b82005-06-18 22:44:37 -07001050 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001051 c.seq = nlh->nlmsg_seq;
1052 c.pid = nlh->nlmsg_pid;
1053 km_policy_notify(xp, p->dir, &c);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfrm_pol_put(xp);
1056
1057 return 0;
1058}
1059
1060static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1061{
1062 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1063 int i;
1064
1065 if (xp->xfrm_nr == 0)
1066 return 0;
1067
1068 for (i = 0; i < xp->xfrm_nr; i++) {
1069 struct xfrm_user_tmpl *up = &vec[i];
1070 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1071
1072 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001073 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1075 up->reqid = kp->reqid;
1076 up->mode = kp->mode;
1077 up->share = kp->share;
1078 up->optional = kp->optional;
1079 up->aalgos = kp->aalgos;
1080 up->ealgos = kp->ealgos;
1081 up->calgos = kp->calgos;
1082 }
1083 RTA_PUT(skb, XFRMA_TMPL,
1084 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1085 vec);
1086
1087 return 0;
1088
1089rtattr_failure:
1090 return -1;
1091}
1092
Serge Hallyn0d681622006-07-24 23:30:44 -07001093static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
Trent Jaegerdf718372005-12-13 23:12:27 -08001094{
Serge Hallyn0d681622006-07-24 23:30:44 -07001095 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1096 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1097 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001098
Serge Hallyn0d681622006-07-24 23:30:44 -07001099 uctx->exttype = XFRMA_SEC_CTX;
1100 uctx->len = ctx_size;
1101 uctx->ctx_doi = s->ctx_doi;
1102 uctx->ctx_alg = s->ctx_alg;
1103 uctx->ctx_len = s->ctx_len;
1104 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1105 return 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001106
1107 rtattr_failure:
1108 return -1;
1109}
1110
Serge Hallyn0d681622006-07-24 23:30:44 -07001111static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1112{
1113 if (x->security) {
1114 return copy_sec_ctx(x->security, skb);
1115 }
1116 return 0;
1117}
1118
1119static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1120{
1121 if (xp->security) {
1122 return copy_sec_ctx(xp->security, skb);
1123 }
1124 return 0;
1125}
1126
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001127#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001128static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001129{
1130 struct xfrm_userpolicy_type upt;
1131
1132 memset(&upt, 0, sizeof(upt));
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001133 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134
1135 RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1136
1137 return 0;
1138
1139rtattr_failure:
1140 return -1;
1141}
1142
1143#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001144static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001145{
1146 return 0;
1147}
1148#endif
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1151{
1152 struct xfrm_dump_info *sp = ptr;
1153 struct xfrm_userpolicy_info *p;
1154 struct sk_buff *in_skb = sp->in_skb;
1155 struct sk_buff *skb = sp->out_skb;
1156 struct nlmsghdr *nlh;
1157 unsigned char *b = skb->tail;
1158
1159 if (sp->this_idx < sp->start_idx)
1160 goto out;
1161
1162 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
1163 sp->nlmsg_seq,
1164 XFRM_MSG_NEWPOLICY, sizeof(*p));
1165 p = NLMSG_DATA(nlh);
1166 nlh->nlmsg_flags = sp->nlmsg_flags;
1167
1168 copy_to_user_policy(xp, p, dir);
1169 if (copy_to_user_tmpl(xp, skb) < 0)
1170 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001171 if (copy_to_user_sec_ctx(xp, skb))
1172 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001173 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001174 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 nlh->nlmsg_len = skb->tail - b;
1177out:
1178 sp->this_idx++;
1179 return 0;
1180
1181nlmsg_failure:
1182 skb_trim(skb, b - skb->data);
1183 return -1;
1184}
1185
1186static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1187{
1188 struct xfrm_dump_info info;
1189
1190 info.in_skb = cb->skb;
1191 info.out_skb = skb;
1192 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1193 info.nlmsg_flags = NLM_F_MULTI;
1194 info.this_idx = 0;
1195 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001196 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1197#ifdef CONFIG_XFRM_SUB_POLICY
1198 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 cb->args[0] = info.this_idx;
1201
1202 return skb->len;
1203}
1204
1205static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1206 struct xfrm_policy *xp,
1207 int dir, u32 seq)
1208{
1209 struct xfrm_dump_info info;
1210 struct sk_buff *skb;
1211
1212 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1213 if (!skb)
1214 return ERR_PTR(-ENOMEM);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 info.in_skb = in_skb;
1217 info.out_skb = skb;
1218 info.nlmsg_seq = seq;
1219 info.nlmsg_flags = 0;
1220 info.this_idx = info.start_idx = 0;
1221
1222 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1223 kfree_skb(skb);
1224 return NULL;
1225 }
1226
1227 return skb;
1228}
1229
1230static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1231{
1232 struct xfrm_policy *xp;
1233 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001234 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001236 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int delete;
1238
1239 p = NLMSG_DATA(nlh);
1240 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1241
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001242 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1243 if (err)
1244 return err;
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 err = verify_policy_dir(p->dir);
1247 if (err)
1248 return err;
1249
1250 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001251 xp = xfrm_policy_byid(type, p->dir, p->index, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001252 else {
1253 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1254 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1255 struct xfrm_policy tmp;
1256
1257 err = verify_sec_ctx_len(rtattrs);
1258 if (err)
1259 return err;
1260
1261 memset(&tmp, 0, sizeof(struct xfrm_policy));
1262 if (rt) {
1263 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1264
1265 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1266 return err;
1267 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001268 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
Trent Jaegerdf718372005-12-13 23:12:27 -08001269 security_xfrm_policy_free(&tmp);
1270 }
Joy Latten161a09e2006-11-27 13:11:54 -06001271 if (delete)
1272 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1273 AUDIT_MAC_IPSEC_DELSPD, (xp) ? 1 : 0, xp, NULL);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (xp == NULL)
1276 return -ENOENT;
1277
1278 if (!delete) {
1279 struct sk_buff *resp_skb;
1280
1281 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1282 if (IS_ERR(resp_skb)) {
1283 err = PTR_ERR(resp_skb);
1284 } else {
1285 err = netlink_unicast(xfrm_nl, resp_skb,
1286 NETLINK_CB(skb).pid,
1287 MSG_DONTWAIT);
1288 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001289 } else {
David S. Miller6f68dc32006-06-08 23:58:52 -07001290 if ((err = security_xfrm_policy_delete(xp)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001291 goto out;
Herbert Xue7443892005-06-18 22:44:18 -07001292 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001293 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001294 c.seq = nlh->nlmsg_seq;
1295 c.pid = nlh->nlmsg_pid;
1296 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 xfrm_pol_put(xp);
1300
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return err;
1303}
1304
1305static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1306{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001307 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001309 struct xfrm_audit audit_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Joy Latten161a09e2006-11-27 13:11:54 -06001311 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1312 audit_info.secid = NETLINK_CB(skb).sid;
1313 xfrm_state_flush(p->proto, &audit_info);
Herbert Xubf088672005-06-18 22:44:00 -07001314 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001315 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001316 c.seq = nlh->nlmsg_seq;
1317 c.pid = nlh->nlmsg_pid;
1318 km_state_notify(NULL, &c);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321}
1322
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001323
1324static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1325{
1326 struct xfrm_aevent_id *id;
1327 struct nlmsghdr *nlh;
1328 struct xfrm_lifetime_cur ltime;
1329 unsigned char *b = skb->tail;
1330
1331 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1332 id = NLMSG_DATA(nlh);
1333 nlh->nlmsg_flags = 0;
1334
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001335 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001336 id->sa_id.spi = x->id.spi;
1337 id->sa_id.family = x->props.family;
1338 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001339 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1340 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001341 id->flags = c->data.aevent;
1342
1343 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1344
1345 ltime.bytes = x->curlft.bytes;
1346 ltime.packets = x->curlft.packets;
1347 ltime.add_time = x->curlft.add_time;
1348 ltime.use_time = x->curlft.use_time;
1349
1350 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1351
1352 if (id->flags&XFRM_AE_RTHR) {
1353 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1354 }
1355
1356 if (id->flags&XFRM_AE_ETHR) {
1357 u32 etimer = x->replay_maxage*10/HZ;
1358 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1359 }
1360
1361 nlh->nlmsg_len = skb->tail - b;
1362 return skb->len;
1363
1364rtattr_failure:
1365nlmsg_failure:
1366 skb_trim(skb, b - skb->data);
1367 return -1;
1368}
1369
1370static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1371{
1372 struct xfrm_state *x;
1373 struct sk_buff *r_skb;
1374 int err;
1375 struct km_event c;
1376 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1377 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1378 struct xfrm_usersa_id *id = &p->sa_id;
1379
1380 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1381 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1382
1383 if (p->flags&XFRM_AE_RTHR)
1384 len+=RTA_SPACE(sizeof(u32));
1385
1386 if (p->flags&XFRM_AE_ETHR)
1387 len+=RTA_SPACE(sizeof(u32));
1388
1389 r_skb = alloc_skb(len, GFP_ATOMIC);
1390 if (r_skb == NULL)
1391 return -ENOMEM;
1392
1393 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1394 if (x == NULL) {
1395 kfree(r_skb);
1396 return -ESRCH;
1397 }
1398
1399 /*
1400 * XXX: is this lock really needed - none of the other
1401 * gets lock (the concern is things getting updated
1402 * while we are still reading) - jhs
1403 */
1404 spin_lock_bh(&x->lock);
1405 c.data.aevent = p->flags;
1406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1408
1409 if (build_aevent(r_skb, x, &c) < 0)
1410 BUG();
1411 err = netlink_unicast(xfrm_nl, r_skb,
1412 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1413 spin_unlock_bh(&x->lock);
1414 xfrm_state_put(x);
1415 return err;
1416}
1417
1418static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1419{
1420 struct xfrm_state *x;
1421 struct km_event c;
1422 int err = - EINVAL;
1423 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1424 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1425 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1426
1427 if (!lt && !rp)
1428 return err;
1429
1430 /* pedantic mode - thou shalt sayeth replaceth */
1431 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1432 return err;
1433
1434 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1435 if (x == NULL)
1436 return -ESRCH;
1437
1438 if (x->km.state != XFRM_STATE_VALID)
1439 goto out;
1440
1441 spin_lock_bh(&x->lock);
1442 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1443 spin_unlock_bh(&x->lock);
1444 if (err < 0)
1445 goto out;
1446
1447 c.event = nlh->nlmsg_type;
1448 c.seq = nlh->nlmsg_seq;
1449 c.pid = nlh->nlmsg_pid;
1450 c.data.aevent = XFRM_AE_CU;
1451 km_state_notify(x, &c);
1452 err = 0;
1453out:
1454 xfrm_state_put(x);
1455 return err;
1456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1459{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001460 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001461 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001462 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001463 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001465 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1466 if (err)
1467 return err;
1468
Joy Latten161a09e2006-11-27 13:11:54 -06001469 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1470 audit_info.secid = NETLINK_CB(skb).sid;
1471 xfrm_policy_flush(type, &audit_info);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001472 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001473 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001474 c.seq = nlh->nlmsg_seq;
1475 c.pid = nlh->nlmsg_pid;
1476 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478}
1479
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001480static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1481{
1482 struct xfrm_policy *xp;
1483 struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
1484 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001485 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001486 int err = -ENOENT;
1487
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001488 err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
1489 if (err)
1490 return err;
1491
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001492 if (p->index)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001493 xp = xfrm_policy_byid(type, p->dir, p->index, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001494 else {
1495 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1496 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1497 struct xfrm_policy tmp;
1498
1499 err = verify_sec_ctx_len(rtattrs);
1500 if (err)
1501 return err;
1502
1503 memset(&tmp, 0, sizeof(struct xfrm_policy));
1504 if (rt) {
1505 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1506
1507 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1508 return err;
1509 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001510 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001511 security_xfrm_policy_free(&tmp);
1512 }
1513
1514 if (xp == NULL)
1515 return err;
1516 read_lock(&xp->lock);
1517 if (xp->dead) {
1518 read_unlock(&xp->lock);
1519 goto out;
1520 }
1521
1522 read_unlock(&xp->lock);
1523 err = 0;
1524 if (up->hard) {
1525 xfrm_policy_delete(xp, p->dir);
Joy Latten161a09e2006-11-27 13:11:54 -06001526 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1527 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1528
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001529 } else {
1530 // reset the timers here?
1531 printk("Dont know what to do with soft policy expire\n");
1532 }
1533 km_policy_expired(xp, p->dir, up->hard, current->pid);
1534
1535out:
1536 xfrm_pol_put(xp);
1537 return err;
1538}
1539
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001540static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1541{
1542 struct xfrm_state *x;
1543 int err;
1544 struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
1545 struct xfrm_usersa_info *p = &ue->state;
1546
1547 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1548 err = -ENOENT;
1549
1550 if (x == NULL)
1551 return err;
1552
1553 err = -EINVAL;
1554
1555 spin_lock_bh(&x->lock);
1556 if (x->km.state != XFRM_STATE_VALID)
1557 goto out;
1558 km_state_expired(x, ue->hard, current->pid);
1559
Joy Latten161a09e2006-11-27 13:11:54 -06001560 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001561 __xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001562 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1563 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1564 }
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001565out:
1566 spin_unlock_bh(&x->lock);
1567 xfrm_state_put(x);
1568 return err;
1569}
1570
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001571static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1572{
1573 struct xfrm_policy *xp;
1574 struct xfrm_user_tmpl *ut;
1575 int i;
1576 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1577
1578 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1579 struct xfrm_state *x = xfrm_state_alloc();
1580 int err = -ENOMEM;
1581
1582 if (!x)
1583 return err;
1584
1585 err = verify_newpolicy_info(&ua->policy);
1586 if (err) {
1587 printk("BAD policy passed\n");
1588 kfree(x);
1589 return err;
1590 }
1591
1592 /* build an XP */
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001593 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
1594 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001595 kfree(x);
1596 return err;
1597 }
1598
1599 memcpy(&x->id, &ua->id, sizeof(ua->id));
1600 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1601 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1602
1603 ut = RTA_DATA(rt);
1604 /* extract the templates and for each call km_key */
1605 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1606 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1607 memcpy(&x->id, &t->id, sizeof(x->id));
1608 x->props.mode = t->mode;
1609 x->props.reqid = t->reqid;
1610 x->props.family = ut->family;
1611 t->aalgos = ua->aalgos;
1612 t->ealgos = ua->ealgos;
1613 t->calgos = ua->calgos;
1614 err = km_query(x, t, xp);
1615
1616 }
1617
1618 kfree(x);
1619 kfree(xp);
1620
1621 return 0;
1622}
1623
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001624
Thomas Graf492b5582005-05-03 14:26:40 -07001625#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1626
1627static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1628 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1629 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1630 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1631 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1632 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1633 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1634 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001635 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001636 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001637 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1638 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001639 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001640 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1641 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001642 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1643 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001644 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645};
1646
Thomas Graf492b5582005-05-03 14:26:40 -07001647#undef XMSGSIZE
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static struct xfrm_link {
1650 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1651 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001652} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1653 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1654 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1655 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1656 .dump = xfrm_dump_sa },
1657 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1658 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1659 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1660 .dump = xfrm_dump_policy },
1661 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001662 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001663 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001664 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1665 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001666 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001667 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1668 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001669 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1670 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671};
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1674{
1675 struct rtattr *xfrma[XFRMA_MAX];
1676 struct xfrm_link *link;
1677 int type, min_len;
1678
1679 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1680 return 0;
1681
1682 type = nlh->nlmsg_type;
1683
1684 /* A control message: ignore them */
1685 if (type < XFRM_MSG_BASE)
1686 return 0;
1687
1688 /* Unknown message: reply with EINVAL */
1689 if (type > XFRM_MSG_MAX)
1690 goto err_einval;
1691
1692 type -= XFRM_MSG_BASE;
1693 link = &xfrm_dispatch[type];
1694
1695 /* All operations require privileges, even GET */
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07001696 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 *errp = -EPERM;
1698 return -1;
1699 }
1700
Thomas Graf492b5582005-05-03 14:26:40 -07001701 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1702 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1703 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (link->dump == NULL)
1705 goto err_einval;
1706
1707 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +01001708 link->dump, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return -1;
1710 }
Thomas Graf88fc2c82005-11-10 02:25:54 +01001711
1712 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return -1;
1714 }
1715
1716 memset(xfrma, 0, sizeof(xfrma));
1717
1718 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1719 goto err_einval;
1720
1721 if (nlh->nlmsg_len > min_len) {
1722 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1723 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1724
1725 while (RTA_OK(attr, attrlen)) {
1726 unsigned short flavor = attr->rta_type;
1727 if (flavor) {
1728 if (flavor > XFRMA_MAX)
1729 goto err_einval;
1730 xfrma[flavor - 1] = attr;
1731 }
1732 attr = RTA_NEXT(attr, attrlen);
1733 }
1734 }
1735
1736 if (link->doit == NULL)
1737 goto err_einval;
1738 *errp = link->doit(skb, nlh, (void **) &xfrma);
1739
1740 return *errp;
1741
1742err_einval:
1743 *errp = -EINVAL;
1744 return -1;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static void xfrm_netlink_rcv(struct sock *sk, int len)
1748{
Thomas Graf88fc2c82005-11-10 02:25:54 +01001749 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 do {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001752 mutex_lock(&xfrm_cfg_mutex);
Thomas Graf88fc2c82005-11-10 02:25:54 +01001753 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001754 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001756 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001759static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct xfrm_user_expire *ue;
1762 struct nlmsghdr *nlh;
1763 unsigned char *b = skb->tail;
1764
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001765 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 sizeof(*ue));
1767 ue = NLMSG_DATA(nlh);
1768 nlh->nlmsg_flags = 0;
1769
1770 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 nlh->nlmsg_len = skb->tail - b;
1774 return skb->len;
1775
1776nlmsg_failure:
1777 skb_trim(skb, b - skb->data);
1778 return -1;
1779}
1780
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001781static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 struct sk_buff *skb;
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001784 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Jamal Hadi Salimee57eef2005-06-18 22:45:56 -07001786 skb = alloc_skb(len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (skb == NULL)
1788 return -ENOMEM;
1789
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 BUG();
1792
Patrick McHardyac6d4392005-08-14 19:29:52 -07001793 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1794 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001797static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1798{
1799 struct sk_buff *skb;
1800 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1801
1802 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1803 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1804 skb = alloc_skb(len, GFP_ATOMIC);
1805 if (skb == NULL)
1806 return -ENOMEM;
1807
1808 if (build_aevent(skb, x, c) < 0)
1809 BUG();
1810
1811 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1812 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1813}
1814
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001815static int xfrm_notify_sa_flush(struct km_event *c)
1816{
1817 struct xfrm_usersa_flush *p;
1818 struct nlmsghdr *nlh;
1819 struct sk_buff *skb;
1820 unsigned char *b;
1821 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1822
1823 skb = alloc_skb(len, GFP_ATOMIC);
1824 if (skb == NULL)
1825 return -ENOMEM;
1826 b = skb->tail;
1827
1828 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1829 XFRM_MSG_FLUSHSA, sizeof(*p));
1830 nlh->nlmsg_flags = 0;
1831
1832 p = NLMSG_DATA(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07001833 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001834
1835 nlh->nlmsg_len = skb->tail - b;
1836
Patrick McHardyac6d4392005-08-14 19:29:52 -07001837 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1838 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001839
1840nlmsg_failure:
1841 kfree_skb(skb);
1842 return -1;
1843}
1844
1845static int inline xfrm_sa_len(struct xfrm_state *x)
1846{
Herbert Xu0603eac2005-06-18 22:54:36 -07001847 int l = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001848 if (x->aalg)
1849 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1850 if (x->ealg)
1851 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1852 if (x->calg)
1853 l += RTA_SPACE(sizeof(*x->calg));
1854 if (x->encap)
1855 l += RTA_SPACE(sizeof(*x->encap));
1856
1857 return l;
1858}
1859
1860static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1861{
1862 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07001863 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001864 struct nlmsghdr *nlh;
1865 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001866 unsigned char *b;
1867 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07001868 int headlen;
1869
1870 headlen = sizeof(*p);
1871 if (c->event == XFRM_MSG_DELSA) {
1872 len += RTA_SPACE(headlen);
1873 headlen = sizeof(*id);
1874 }
1875 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001876
1877 skb = alloc_skb(len, GFP_ATOMIC);
1878 if (skb == NULL)
1879 return -ENOMEM;
1880 b = skb->tail;
1881
Herbert Xu0603eac2005-06-18 22:54:36 -07001882 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001883 nlh->nlmsg_flags = 0;
1884
1885 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07001886 if (c->event == XFRM_MSG_DELSA) {
1887 id = NLMSG_DATA(nlh);
1888 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1889 id->spi = x->id.spi;
1890 id->family = x->props.family;
1891 id->proto = x->id.proto;
1892
1893 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1894 }
1895
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001896 copy_to_user_state(x, p);
1897
1898 if (x->aalg)
1899 RTA_PUT(skb, XFRMA_ALG_AUTH,
1900 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1901 if (x->ealg)
1902 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1903 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1904 if (x->calg)
1905 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1906
1907 if (x->encap)
1908 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1909
1910 nlh->nlmsg_len = skb->tail - b;
1911
Patrick McHardyac6d4392005-08-14 19:29:52 -07001912 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1913 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001914
1915nlmsg_failure:
1916rtattr_failure:
1917 kfree_skb(skb);
1918 return -1;
1919}
1920
1921static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1922{
1923
1924 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001925 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001926 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001927 case XFRM_MSG_NEWAE:
1928 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001929 case XFRM_MSG_DELSA:
1930 case XFRM_MSG_UPDSA:
1931 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07001933 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001934 return xfrm_notify_sa_flush(c);
1935 default:
1936 printk("xfrm_user: Unknown SA event %d\n", c->event);
1937 break;
1938 }
1939
1940 return 0;
1941
1942}
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1945 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1946 int dir)
1947{
1948 struct xfrm_user_acquire *ua;
1949 struct nlmsghdr *nlh;
1950 unsigned char *b = skb->tail;
1951 __u32 seq = xfrm_get_acqseq();
1952
1953 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1954 sizeof(*ua));
1955 ua = NLMSG_DATA(nlh);
1956 nlh->nlmsg_flags = 0;
1957
1958 memcpy(&ua->id, &x->id, sizeof(ua->id));
1959 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1960 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1961 copy_to_user_policy(xp, &ua->policy, dir);
1962 ua->aalgos = xt->aalgos;
1963 ua->ealgos = xt->ealgos;
1964 ua->calgos = xt->calgos;
1965 ua->seq = x->km.seq = seq;
1966
1967 if (copy_to_user_tmpl(xp, skb) < 0)
1968 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07001969 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08001970 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001971 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001972 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 nlh->nlmsg_len = skb->tail - b;
1975 return skb->len;
1976
1977nlmsg_failure:
1978 skb_trim(skb, b - skb->data);
1979 return -1;
1980}
1981
1982static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1983 struct xfrm_policy *xp, int dir)
1984{
1985 struct sk_buff *skb;
1986 size_t len;
1987
1988 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1989 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
Trent Jaegerdf718372005-12-13 23:12:27 -08001990 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08001991#ifdef CONFIG_XFRM_SUB_POLICY
1992 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
1993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 skb = alloc_skb(len, GFP_ATOMIC);
1995 if (skb == NULL)
1996 return -ENOMEM;
1997
1998 if (build_acquire(skb, x, xt, xp, dir) < 0)
1999 BUG();
2000
Patrick McHardyac6d4392005-08-14 19:29:52 -07002001 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
2002 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/* User gives us xfrm_user_policy_info followed by an array of 0
2006 * or more templates.
2007 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002008static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 u8 *data, int len, int *dir)
2010{
2011 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2012 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2013 struct xfrm_policy *xp;
2014 int nr;
2015
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002016 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case AF_INET:
2018 if (opt != IP_XFRM_POLICY) {
2019 *dir = -EOPNOTSUPP;
2020 return NULL;
2021 }
2022 break;
2023#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2024 case AF_INET6:
2025 if (opt != IPV6_XFRM_POLICY) {
2026 *dir = -EOPNOTSUPP;
2027 return NULL;
2028 }
2029 break;
2030#endif
2031 default:
2032 *dir = -EINVAL;
2033 return NULL;
2034 }
2035
2036 *dir = -EINVAL;
2037
2038 if (len < sizeof(*p) ||
2039 verify_newpolicy_info(p))
2040 return NULL;
2041
2042 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002043 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return NULL;
2045
Herbert Xua4f1bac2005-07-26 15:43:17 -07002046 if (p->dir > XFRM_POLICY_OUT)
2047 return NULL;
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xp = xfrm_policy_alloc(GFP_KERNEL);
2050 if (xp == NULL) {
2051 *dir = -ENOBUFS;
2052 return NULL;
2053 }
2054
2055 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002056 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 copy_templates(xp, ut, nr);
2058
2059 *dir = p->dir;
2060
2061 return xp;
2062}
2063
2064static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002065 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct xfrm_user_polexpire *upe;
2068 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002069 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 unsigned char *b = skb->tail;
2071
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002072 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 upe = NLMSG_DATA(nlh);
2074 nlh->nlmsg_flags = 0;
2075
2076 copy_to_user_policy(xp, &upe->pol, dir);
2077 if (copy_to_user_tmpl(xp, skb) < 0)
2078 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002079 if (copy_to_user_sec_ctx(xp, skb))
2080 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002081 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002082 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 upe->hard = !!hard;
2084
2085 nlh->nlmsg_len = skb->tail - b;
2086 return skb->len;
2087
2088nlmsg_failure:
2089 skb_trim(skb, b - skb->data);
2090 return -1;
2091}
2092
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct sk_buff *skb;
2096 size_t len;
2097
2098 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2099 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
Trent Jaegerdf718372005-12-13 23:12:27 -08002100 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002101#ifdef CONFIG_XFRM_SUB_POLICY
2102 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 skb = alloc_skb(len, GFP_ATOMIC);
2105 if (skb == NULL)
2106 return -ENOMEM;
2107
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002108 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 BUG();
2110
Patrick McHardyac6d4392005-08-14 19:29:52 -07002111 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
2112 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002115static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2116{
2117 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002118 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002119 struct nlmsghdr *nlh;
2120 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002121 unsigned char *b;
2122 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002123 int headlen;
2124
2125 headlen = sizeof(*p);
2126 if (c->event == XFRM_MSG_DELPOLICY) {
2127 len += RTA_SPACE(headlen);
2128 headlen = sizeof(*id);
2129 }
Jamal Hadi Salim334f3d42006-11-19 14:53:07 -08002130#ifdef CONFIG_XFRM_SUB_POLICY
2131 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
2132#endif
Herbert Xu0603eac2005-06-18 22:54:36 -07002133 len += NLMSG_SPACE(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002134
2135 skb = alloc_skb(len, GFP_ATOMIC);
2136 if (skb == NULL)
2137 return -ENOMEM;
2138 b = skb->tail;
2139
Herbert Xu0603eac2005-06-18 22:54:36 -07002140 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141
2142 p = NLMSG_DATA(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002143 if (c->event == XFRM_MSG_DELPOLICY) {
2144 id = NLMSG_DATA(nlh);
2145 memset(id, 0, sizeof(*id));
2146 id->dir = dir;
2147 if (c->data.byid)
2148 id->index = xp->index;
2149 else
2150 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2151
2152 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2153 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154
2155 nlh->nlmsg_flags = 0;
2156
2157 copy_to_user_policy(xp, p, dir);
2158 if (copy_to_user_tmpl(xp, skb) < 0)
2159 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002160 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002161 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002162
2163 nlh->nlmsg_len = skb->tail - b;
2164
Patrick McHardyac6d4392005-08-14 19:29:52 -07002165 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2166 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002167
2168nlmsg_failure:
Herbert Xu0603eac2005-06-18 22:54:36 -07002169rtattr_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002170 kfree_skb(skb);
2171 return -1;
2172}
2173
2174static int xfrm_notify_policy_flush(struct km_event *c)
2175{
2176 struct nlmsghdr *nlh;
2177 struct sk_buff *skb;
2178 unsigned char *b;
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002179 int len = 0;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002180#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002181 len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002182#endif
Jamal Hadi Salim785fd8b82006-11-19 14:55:30 -08002183 len += NLMSG_LENGTH(0);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002184
2185 skb = alloc_skb(len, GFP_ATOMIC);
2186 if (skb == NULL)
2187 return -ENOMEM;
2188 b = skb->tail;
2189
2190
2191 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002192 nlh->nlmsg_flags = 0;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002193 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2194 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002195
2196 nlh->nlmsg_len = skb->tail - b;
2197
Patrick McHardyac6d4392005-08-14 19:29:52 -07002198 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
2199 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002200
2201nlmsg_failure:
2202 kfree_skb(skb);
2203 return -1;
2204}
2205
2206static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2207{
2208
2209 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002210 case XFRM_MSG_NEWPOLICY:
2211 case XFRM_MSG_UPDPOLICY:
2212 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002213 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002214 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002215 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002216 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002217 return xfrm_exp_policy_notify(xp, dir, c);
2218 default:
2219 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2220 }
2221
2222 return 0;
2223
2224}
2225
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002226static int build_report(struct sk_buff *skb, u8 proto,
2227 struct xfrm_selector *sel, xfrm_address_t *addr)
2228{
2229 struct xfrm_user_report *ur;
2230 struct nlmsghdr *nlh;
2231 unsigned char *b = skb->tail;
2232
2233 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
2234 ur = NLMSG_DATA(nlh);
2235 nlh->nlmsg_flags = 0;
2236
2237 ur->proto = proto;
2238 memcpy(&ur->sel, sel, sizeof(ur->sel));
2239
2240 if (addr)
2241 RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2242
2243 nlh->nlmsg_len = skb->tail - b;
2244 return skb->len;
2245
2246nlmsg_failure:
2247rtattr_failure:
2248 skb_trim(skb, b - skb->data);
2249 return -1;
2250}
2251
2252static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2253 xfrm_address_t *addr)
2254{
2255 struct sk_buff *skb;
2256 size_t len;
2257
2258 len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
2259 skb = alloc_skb(len, GFP_ATOMIC);
2260 if (skb == NULL)
2261 return -ENOMEM;
2262
2263 if (build_report(skb, proto, sel, addr) < 0)
2264 BUG();
2265
2266 NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
2267 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270static struct xfrm_mgr netlink_mgr = {
2271 .id = "netlink",
2272 .notify = xfrm_send_state_notify,
2273 .acquire = xfrm_send_acquire,
2274 .compile_policy = xfrm_compile_policy,
2275 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002276 .report = xfrm_send_report,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277};
2278
2279static int __init xfrm_user_init(void)
2280{
Patrick McHardybe336902006-03-20 22:40:54 -08002281 struct sock *nlsk;
2282
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002283 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Patrick McHardybe336902006-03-20 22:40:54 -08002285 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2286 xfrm_netlink_rcv, THIS_MODULE);
2287 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002289 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 xfrm_register_km(&netlink_mgr);
2292
2293 return 0;
2294}
2295
2296static void __exit xfrm_user_exit(void)
2297{
Patrick McHardybe336902006-03-20 22:40:54 -08002298 struct sock *nlsk = xfrm_nl;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002301 rcu_assign_pointer(xfrm_nl, NULL);
2302 synchronize_rcu();
2303 sock_release(nlsk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306module_init(xfrm_user_init);
2307module_exit(xfrm_user_exit);
2308MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002309MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002310