]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* 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 | |
df71837d | 10 | * |
1da177e4 LT |
11 | */ |
12 | ||
9409f38a | 13 | #include <linux/crypto.h> |
1da177e4 LT |
14 | #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> | |
1da177e4 LT |
22 | #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> | |
88fc2c84 | 29 | #include <net/netlink.h> |
1da177e4 | 30 | #include <asm/uaccess.h> |
e23c7194 MN |
31 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
32 | #include <linux/in6.h> | |
33 | #endif | |
1da177e4 | 34 | |
1da177e4 LT |
35 | static 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; | |
31c26852 | 39 | int len; |
1da177e4 LT |
40 | |
41 | if (!rt) | |
42 | return 0; | |
43 | ||
31c26852 HX |
44 | len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp); |
45 | if (len < 0) | |
1da177e4 LT |
46 | return -EINVAL; |
47 | ||
48 | algp = RTA_DATA(rt); | |
31c26852 HX |
49 | |
50 | len -= (algp->alg_key_len + 7U) / 8; | |
51 | if (len < 0) | |
52 | return -EINVAL; | |
53 | ||
1da177e4 LT |
54 | 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 | ||
79 | static 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 | ||
eb2971b6 MN |
93 | static 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 | } | |
df71837d TJ |
109 | |
110 | static 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 | ||
df71837d TJ |
124 | 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 | ||
1da177e4 LT |
134 | static 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 | ||
e23c7194 MN |
179 | #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 | ||
1da177e4 LT |
192 | 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; | |
df71837d TJ |
204 | if ((err = verify_sec_ctx_len(xfrma))) |
205 | goto out; | |
060f02a3 NT |
206 | if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL))) |
207 | goto out; | |
1da177e4 LT |
208 | |
209 | err = -EINVAL; | |
210 | switch (p->mode) { | |
7e49e6de MN |
211 | case XFRM_MODE_TRANSPORT: |
212 | case XFRM_MODE_TUNNEL: | |
060f02a3 | 213 | case XFRM_MODE_ROUTEOPTIMIZATION: |
0a69452c | 214 | case XFRM_MODE_BEET: |
1da177e4 LT |
215 | break; |
216 | ||
217 | default: | |
218 | goto out; | |
219 | }; | |
220 | ||
221 | err = 0; | |
222 | ||
223 | out: | |
224 | return err; | |
225 | } | |
226 | ||
227 | static 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; | |
b9e9dead | 234 | int len; |
1da177e4 LT |
235 | |
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 | ||
b9e9dead HX |
246 | len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8; |
247 | p = kmalloc(len, GFP_KERNEL); | |
1da177e4 LT |
248 | if (!p) |
249 | return -ENOMEM; | |
250 | ||
b9e9dead | 251 | memcpy(p, ualg, len); |
04ff1260 | 252 | strcpy(p->alg_name, algo->name); |
1da177e4 LT |
253 | *algpp = p; |
254 | return 0; | |
255 | } | |
256 | ||
257 | static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg) | |
258 | { | |
259 | struct rtattr *rta = u_arg; | |
260 | struct xfrm_encap_tmpl *p, *uencap; | |
261 | ||
262 | if (!rta) | |
263 | return 0; | |
264 | ||
265 | uencap = RTA_DATA(rta); | |
266 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
267 | if (!p) | |
268 | return -ENOMEM; | |
269 | ||
270 | memcpy(p, uencap, sizeof(*p)); | |
271 | *encapp = p; | |
272 | return 0; | |
273 | } | |
274 | ||
df71837d TJ |
275 | |
276 | static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp) | |
277 | { | |
278 | struct xfrm_sec_ctx *xfrm_ctx = xp->security; | |
279 | int len = 0; | |
280 | ||
281 | if (xfrm_ctx) { | |
282 | len += sizeof(struct xfrm_user_sec_ctx); | |
283 | len += xfrm_ctx->ctx_len; | |
284 | } | |
285 | return len; | |
286 | } | |
287 | ||
288 | static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg) | |
289 | { | |
290 | struct xfrm_user_sec_ctx *uctx; | |
291 | ||
292 | if (!u_arg) | |
293 | return 0; | |
294 | ||
295 | uctx = RTA_DATA(u_arg); | |
296 | return security_xfrm_state_alloc(x, uctx); | |
297 | } | |
298 | ||
060f02a3 NT |
299 | static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg) |
300 | { | |
301 | struct rtattr *rta = u_arg; | |
302 | xfrm_address_t *p, *uaddrp; | |
303 | ||
304 | if (!rta) | |
305 | return 0; | |
306 | ||
307 | uaddrp = RTA_DATA(rta); | |
308 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
309 | if (!p) | |
310 | return -ENOMEM; | |
311 | ||
312 | memcpy(p, uaddrp, sizeof(*p)); | |
313 | *addrpp = p; | |
314 | return 0; | |
315 | } | |
316 | ||
1da177e4 LT |
317 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
318 | { | |
319 | memcpy(&x->id, &p->id, sizeof(x->id)); | |
320 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); | |
321 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); | |
322 | x->props.mode = p->mode; | |
323 | x->props.replay_window = p->replay_window; | |
324 | x->props.reqid = p->reqid; | |
325 | x->props.family = p->family; | |
54489c14 | 326 | memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); |
1da177e4 LT |
327 | x->props.flags = p->flags; |
328 | } | |
329 | ||
d51d081d JHS |
330 | /* |
331 | * someday when pfkey also has support, we could have the code | |
332 | * somehow made shareable and move it to xfrm_state.c - JHS | |
333 | * | |
334 | */ | |
335 | static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma) | |
336 | { | |
337 | int err = - EINVAL; | |
338 | struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1]; | |
339 | struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1]; | |
340 | struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1]; | |
341 | struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1]; | |
342 | ||
343 | if (rp) { | |
344 | struct xfrm_replay_state *replay; | |
345 | if (RTA_PAYLOAD(rp) < sizeof(*replay)) | |
346 | goto error; | |
347 | replay = RTA_DATA(rp); | |
348 | memcpy(&x->replay, replay, sizeof(*replay)); | |
349 | memcpy(&x->preplay, replay, sizeof(*replay)); | |
350 | } | |
351 | ||
352 | if (lt) { | |
353 | struct xfrm_lifetime_cur *ltime; | |
354 | if (RTA_PAYLOAD(lt) < sizeof(*ltime)) | |
355 | goto error; | |
356 | ltime = RTA_DATA(lt); | |
357 | x->curlft.bytes = ltime->bytes; | |
358 | x->curlft.packets = ltime->packets; | |
359 | x->curlft.add_time = ltime->add_time; | |
360 | x->curlft.use_time = ltime->use_time; | |
361 | } | |
362 | ||
363 | if (et) { | |
364 | if (RTA_PAYLOAD(et) < sizeof(u32)) | |
365 | goto error; | |
366 | x->replay_maxage = *(u32*)RTA_DATA(et); | |
367 | } | |
368 | ||
369 | if (rt) { | |
370 | if (RTA_PAYLOAD(rt) < sizeof(u32)) | |
371 | goto error; | |
372 | x->replay_maxdiff = *(u32*)RTA_DATA(rt); | |
373 | } | |
374 | ||
375 | return 0; | |
376 | error: | |
377 | return err; | |
378 | } | |
379 | ||
1da177e4 LT |
380 | static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p, |
381 | struct rtattr **xfrma, | |
382 | int *errp) | |
383 | { | |
384 | struct xfrm_state *x = xfrm_state_alloc(); | |
385 | int err = -ENOMEM; | |
386 | ||
387 | if (!x) | |
388 | goto error_no_put; | |
389 | ||
390 | copy_from_user_state(x, p); | |
391 | ||
392 | if ((err = attach_one_algo(&x->aalg, &x->props.aalgo, | |
393 | xfrm_aalg_get_byname, | |
394 | xfrma[XFRMA_ALG_AUTH-1]))) | |
395 | goto error; | |
396 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, | |
397 | xfrm_ealg_get_byname, | |
398 | xfrma[XFRMA_ALG_CRYPT-1]))) | |
399 | goto error; | |
400 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, | |
401 | xfrm_calg_get_byname, | |
402 | xfrma[XFRMA_ALG_COMP-1]))) | |
403 | goto error; | |
404 | if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1]))) | |
405 | goto error; | |
060f02a3 NT |
406 | if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1]))) |
407 | goto error; | |
72cb6962 | 408 | err = xfrm_init_state(x); |
1da177e4 LT |
409 | if (err) |
410 | goto error; | |
411 | ||
df71837d TJ |
412 | if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1]))) |
413 | goto error; | |
414 | ||
1da177e4 | 415 | x->km.seq = p->seq; |
d51d081d JHS |
416 | x->replay_maxdiff = sysctl_xfrm_aevent_rseqth; |
417 | /* sysctl_xfrm_aevent_etime is in 100ms units */ | |
418 | x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M; | |
419 | x->preplay.bitmap = 0; | |
420 | x->preplay.seq = x->replay.seq+x->replay_maxdiff; | |
421 | x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; | |
422 | ||
423 | /* override default values from above */ | |
424 | ||
425 | err = xfrm_update_ae_params(x, (struct rtattr **)xfrma); | |
426 | if (err < 0) | |
427 | goto error; | |
1da177e4 LT |
428 | |
429 | return x; | |
430 | ||
431 | error: | |
432 | x->km.state = XFRM_STATE_DEAD; | |
433 | xfrm_state_put(x); | |
434 | error_no_put: | |
435 | *errp = err; | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
440 | { | |
441 | struct xfrm_usersa_info *p = NLMSG_DATA(nlh); | |
442 | struct xfrm_state *x; | |
443 | int err; | |
26b15dad | 444 | struct km_event c; |
1da177e4 | 445 | |
df71837d | 446 | err = verify_newsa_info(p, (struct rtattr **)xfrma); |
1da177e4 LT |
447 | if (err) |
448 | return err; | |
449 | ||
df71837d | 450 | x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err); |
1da177e4 LT |
451 | if (!x) |
452 | return err; | |
453 | ||
26b15dad | 454 | xfrm_state_hold(x); |
1da177e4 LT |
455 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) |
456 | err = xfrm_state_add(x); | |
457 | else | |
458 | err = xfrm_state_update(x); | |
459 | ||
460 | if (err < 0) { | |
461 | x->km.state = XFRM_STATE_DEAD; | |
21380b81 | 462 | __xfrm_state_put(x); |
7d6dfe1f | 463 | goto out; |
1da177e4 LT |
464 | } |
465 | ||
26b15dad JHS |
466 | c.seq = nlh->nlmsg_seq; |
467 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 468 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
469 | |
470 | km_state_notify(x, &c); | |
7d6dfe1f | 471 | out: |
26b15dad | 472 | xfrm_state_put(x); |
1da177e4 LT |
473 | return err; |
474 | } | |
475 | ||
eb2971b6 MN |
476 | static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p, |
477 | struct rtattr **xfrma, | |
478 | int *errp) | |
479 | { | |
480 | struct xfrm_state *x = NULL; | |
481 | int err; | |
482 | ||
483 | if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { | |
484 | err = -ESRCH; | |
485 | x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); | |
486 | } else { | |
487 | xfrm_address_t *saddr = NULL; | |
488 | ||
489 | err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr); | |
490 | if (err) | |
491 | goto out; | |
492 | ||
493 | if (!saddr) { | |
494 | err = -EINVAL; | |
495 | goto out; | |
496 | } | |
497 | ||
498 | x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto, | |
499 | p->family); | |
500 | } | |
501 | ||
502 | out: | |
503 | if (!x && errp) | |
504 | *errp = err; | |
505 | return x; | |
506 | } | |
507 | ||
1da177e4 LT |
508 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
509 | { | |
510 | struct xfrm_state *x; | |
eb2971b6 | 511 | int err = -ESRCH; |
26b15dad | 512 | struct km_event c; |
1da177e4 LT |
513 | struct xfrm_usersa_id *p = NLMSG_DATA(nlh); |
514 | ||
eb2971b6 | 515 | x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err); |
1da177e4 | 516 | if (x == NULL) |
eb2971b6 | 517 | return err; |
1da177e4 | 518 | |
6f68dc37 | 519 | if ((err = security_xfrm_state_delete(x)) != 0) |
c8c05a8e CZ |
520 | goto out; |
521 | ||
1da177e4 | 522 | if (xfrm_state_kern(x)) { |
c8c05a8e CZ |
523 | err = -EPERM; |
524 | goto out; | |
1da177e4 LT |
525 | } |
526 | ||
26b15dad | 527 | err = xfrm_state_delete(x); |
c8c05a8e CZ |
528 | if (err < 0) |
529 | goto out; | |
26b15dad JHS |
530 | |
531 | c.seq = nlh->nlmsg_seq; | |
532 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 533 | c.event = nlh->nlmsg_type; |
26b15dad | 534 | km_state_notify(x, &c); |
1da177e4 | 535 | |
c8c05a8e CZ |
536 | out: |
537 | xfrm_state_put(x); | |
26b15dad | 538 | return err; |
1da177e4 LT |
539 | } |
540 | ||
541 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) | |
542 | { | |
543 | memcpy(&p->id, &x->id, sizeof(p->id)); | |
544 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); | |
545 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); | |
546 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); | |
547 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); | |
54489c14 | 548 | memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); |
1da177e4 LT |
549 | p->mode = x->props.mode; |
550 | p->replay_window = x->props.replay_window; | |
551 | p->reqid = x->props.reqid; | |
552 | p->family = x->props.family; | |
553 | p->flags = x->props.flags; | |
554 | p->seq = x->km.seq; | |
555 | } | |
556 | ||
557 | struct xfrm_dump_info { | |
558 | struct sk_buff *in_skb; | |
559 | struct sk_buff *out_skb; | |
560 | u32 nlmsg_seq; | |
561 | u16 nlmsg_flags; | |
562 | int start_idx; | |
563 | int this_idx; | |
564 | }; | |
565 | ||
566 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) | |
567 | { | |
568 | struct xfrm_dump_info *sp = ptr; | |
569 | struct sk_buff *in_skb = sp->in_skb; | |
570 | struct sk_buff *skb = sp->out_skb; | |
571 | struct xfrm_usersa_info *p; | |
572 | struct nlmsghdr *nlh; | |
573 | unsigned char *b = skb->tail; | |
574 | ||
575 | if (sp->this_idx < sp->start_idx) | |
576 | goto out; | |
577 | ||
578 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, | |
579 | sp->nlmsg_seq, | |
580 | XFRM_MSG_NEWSA, sizeof(*p)); | |
581 | nlh->nlmsg_flags = sp->nlmsg_flags; | |
582 | ||
583 | p = NLMSG_DATA(nlh); | |
584 | copy_to_user_state(x, p); | |
585 | ||
586 | if (x->aalg) | |
587 | RTA_PUT(skb, XFRMA_ALG_AUTH, | |
588 | sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); | |
589 | if (x->ealg) | |
590 | RTA_PUT(skb, XFRMA_ALG_CRYPT, | |
591 | sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); | |
592 | if (x->calg) | |
593 | RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); | |
594 | ||
595 | if (x->encap) | |
596 | RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); | |
597 | ||
df71837d TJ |
598 | if (x->security) { |
599 | int ctx_size = sizeof(struct xfrm_sec_ctx) + | |
600 | x->security->ctx_len; | |
601 | struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size); | |
602 | struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); | |
603 | ||
604 | uctx->exttype = XFRMA_SEC_CTX; | |
605 | uctx->len = ctx_size; | |
606 | uctx->ctx_doi = x->security->ctx_doi; | |
607 | uctx->ctx_alg = x->security->ctx_alg; | |
608 | uctx->ctx_len = x->security->ctx_len; | |
609 | memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len); | |
610 | } | |
060f02a3 NT |
611 | |
612 | if (x->coaddr) | |
613 | RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); | |
614 | ||
9afaca05 MN |
615 | if (x->lastused) |
616 | RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused); | |
617 | ||
1da177e4 LT |
618 | nlh->nlmsg_len = skb->tail - b; |
619 | out: | |
620 | sp->this_idx++; | |
621 | return 0; | |
622 | ||
623 | nlmsg_failure: | |
624 | rtattr_failure: | |
625 | skb_trim(skb, b - skb->data); | |
626 | return -1; | |
627 | } | |
628 | ||
629 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) | |
630 | { | |
631 | struct xfrm_dump_info info; | |
632 | ||
633 | info.in_skb = cb->skb; | |
634 | info.out_skb = skb; | |
635 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | |
636 | info.nlmsg_flags = NLM_F_MULTI; | |
637 | info.this_idx = 0; | |
638 | info.start_idx = cb->args[0]; | |
dc00a525 | 639 | (void) xfrm_state_walk(0, dump_one_state, &info); |
1da177e4 LT |
640 | cb->args[0] = info.this_idx; |
641 | ||
642 | return skb->len; | |
643 | } | |
644 | ||
645 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, | |
646 | struct xfrm_state *x, u32 seq) | |
647 | { | |
648 | struct xfrm_dump_info info; | |
649 | struct sk_buff *skb; | |
650 | ||
651 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); | |
652 | if (!skb) | |
653 | return ERR_PTR(-ENOMEM); | |
654 | ||
655 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; | |
656 | info.in_skb = in_skb; | |
657 | info.out_skb = skb; | |
658 | info.nlmsg_seq = seq; | |
659 | info.nlmsg_flags = 0; | |
660 | info.this_idx = info.start_idx = 0; | |
661 | ||
662 | if (dump_one_state(x, 0, &info)) { | |
663 | kfree_skb(skb); | |
664 | return NULL; | |
665 | } | |
666 | ||
667 | return skb; | |
668 | } | |
669 | ||
670 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
671 | { | |
672 | struct xfrm_usersa_id *p = NLMSG_DATA(nlh); | |
673 | struct xfrm_state *x; | |
674 | struct sk_buff *resp_skb; | |
eb2971b6 | 675 | int err = -ESRCH; |
1da177e4 | 676 | |
eb2971b6 | 677 | x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err); |
1da177e4 LT |
678 | if (x == NULL) |
679 | goto out_noput; | |
680 | ||
681 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | |
682 | if (IS_ERR(resp_skb)) { | |
683 | err = PTR_ERR(resp_skb); | |
684 | } else { | |
685 | err = netlink_unicast(xfrm_nl, resp_skb, | |
686 | NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
687 | } | |
688 | xfrm_state_put(x); | |
689 | out_noput: | |
690 | return err; | |
691 | } | |
692 | ||
693 | static int verify_userspi_info(struct xfrm_userspi_info *p) | |
694 | { | |
695 | switch (p->info.id.proto) { | |
696 | case IPPROTO_AH: | |
697 | case IPPROTO_ESP: | |
698 | break; | |
699 | ||
700 | case IPPROTO_COMP: | |
701 | /* IPCOMP spi is 16-bits. */ | |
702 | if (p->max >= 0x10000) | |
703 | return -EINVAL; | |
704 | break; | |
705 | ||
706 | default: | |
707 | return -EINVAL; | |
708 | }; | |
709 | ||
710 | if (p->min > p->max) | |
711 | return -EINVAL; | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
716 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
717 | { | |
718 | struct xfrm_state *x; | |
719 | struct xfrm_userspi_info *p; | |
720 | struct sk_buff *resp_skb; | |
721 | xfrm_address_t *daddr; | |
722 | int family; | |
723 | int err; | |
724 | ||
725 | p = NLMSG_DATA(nlh); | |
726 | err = verify_userspi_info(p); | |
727 | if (err) | |
728 | goto out_noput; | |
729 | ||
730 | family = p->info.family; | |
731 | daddr = &p->info.id.daddr; | |
732 | ||
733 | x = NULL; | |
734 | if (p->info.seq) { | |
735 | x = xfrm_find_acq_byseq(p->info.seq); | |
736 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { | |
737 | xfrm_state_put(x); | |
738 | x = NULL; | |
739 | } | |
740 | } | |
741 | ||
742 | if (!x) | |
743 | x = xfrm_find_acq(p->info.mode, p->info.reqid, | |
744 | p->info.id.proto, daddr, | |
745 | &p->info.saddr, 1, | |
746 | family); | |
747 | err = -ENOENT; | |
748 | if (x == NULL) | |
749 | goto out_noput; | |
750 | ||
751 | resp_skb = ERR_PTR(-ENOENT); | |
752 | ||
753 | spin_lock_bh(&x->lock); | |
754 | if (x->km.state != XFRM_STATE_DEAD) { | |
755 | xfrm_alloc_spi(x, htonl(p->min), htonl(p->max)); | |
756 | if (x->id.spi) | |
757 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | |
758 | } | |
759 | spin_unlock_bh(&x->lock); | |
760 | ||
761 | if (IS_ERR(resp_skb)) { | |
762 | err = PTR_ERR(resp_skb); | |
763 | goto out; | |
764 | } | |
765 | ||
766 | err = netlink_unicast(xfrm_nl, resp_skb, | |
767 | NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
768 | ||
769 | out: | |
770 | xfrm_state_put(x); | |
771 | out_noput: | |
772 | return err; | |
773 | } | |
774 | ||
775 | static int verify_policy_dir(__u8 dir) | |
776 | { | |
777 | switch (dir) { | |
778 | case XFRM_POLICY_IN: | |
779 | case XFRM_POLICY_OUT: | |
780 | case XFRM_POLICY_FWD: | |
781 | break; | |
782 | ||
783 | default: | |
784 | return -EINVAL; | |
785 | }; | |
786 | ||
787 | return 0; | |
788 | } | |
789 | ||
f7b6983f MN |
790 | static int verify_policy_type(__u8 type) |
791 | { | |
792 | switch (type) { | |
793 | case XFRM_POLICY_TYPE_MAIN: | |
794 | #ifdef CONFIG_XFRM_SUB_POLICY | |
795 | case XFRM_POLICY_TYPE_SUB: | |
796 | #endif | |
797 | break; | |
798 | ||
799 | default: | |
800 | return -EINVAL; | |
801 | }; | |
802 | ||
803 | return 0; | |
804 | } | |
805 | ||
1da177e4 LT |
806 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) |
807 | { | |
808 | switch (p->share) { | |
809 | case XFRM_SHARE_ANY: | |
810 | case XFRM_SHARE_SESSION: | |
811 | case XFRM_SHARE_USER: | |
812 | case XFRM_SHARE_UNIQUE: | |
813 | break; | |
814 | ||
815 | default: | |
816 | return -EINVAL; | |
817 | }; | |
818 | ||
819 | switch (p->action) { | |
820 | case XFRM_POLICY_ALLOW: | |
821 | case XFRM_POLICY_BLOCK: | |
822 | break; | |
823 | ||
824 | default: | |
825 | return -EINVAL; | |
826 | }; | |
827 | ||
828 | switch (p->sel.family) { | |
829 | case AF_INET: | |
830 | break; | |
831 | ||
832 | case AF_INET6: | |
833 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
834 | break; | |
835 | #else | |
836 | return -EAFNOSUPPORT; | |
837 | #endif | |
838 | ||
839 | default: | |
840 | return -EINVAL; | |
841 | }; | |
842 | ||
843 | return verify_policy_dir(p->dir); | |
844 | } | |
845 | ||
df71837d TJ |
846 | static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma) |
847 | { | |
848 | struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1]; | |
849 | struct xfrm_user_sec_ctx *uctx; | |
850 | ||
851 | if (!rt) | |
852 | return 0; | |
853 | ||
854 | uctx = RTA_DATA(rt); | |
855 | return security_xfrm_policy_alloc(pol, uctx); | |
856 | } | |
857 | ||
1da177e4 LT |
858 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, |
859 | int nr) | |
860 | { | |
861 | int i; | |
862 | ||
863 | xp->xfrm_nr = nr; | |
864 | for (i = 0; i < nr; i++, ut++) { | |
865 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
866 | ||
867 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); | |
868 | memcpy(&t->saddr, &ut->saddr, | |
869 | sizeof(xfrm_address_t)); | |
870 | t->reqid = ut->reqid; | |
871 | t->mode = ut->mode; | |
872 | t->share = ut->share; | |
873 | t->optional = ut->optional; | |
874 | t->aalgos = ut->aalgos; | |
875 | t->ealgos = ut->ealgos; | |
876 | t->calgos = ut->calgos; | |
877 | } | |
878 | } | |
879 | ||
880 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma) | |
881 | { | |
882 | struct rtattr *rt = xfrma[XFRMA_TMPL-1]; | |
883 | struct xfrm_user_tmpl *utmpl; | |
884 | int nr; | |
885 | ||
886 | if (!rt) { | |
887 | pol->xfrm_nr = 0; | |
888 | } else { | |
889 | nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl); | |
890 | ||
891 | if (nr > XFRM_MAX_DEPTH) | |
892 | return -EINVAL; | |
893 | ||
894 | copy_templates(pol, RTA_DATA(rt), nr); | |
895 | } | |
896 | return 0; | |
897 | } | |
898 | ||
f7b6983f MN |
899 | static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma) |
900 | { | |
901 | struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1]; | |
902 | struct xfrm_userpolicy_type *upt; | |
903 | __u8 type = XFRM_POLICY_TYPE_MAIN; | |
904 | int err; | |
905 | ||
906 | if (rt) { | |
907 | if (rt->rta_len < sizeof(*upt)) | |
908 | return -EINVAL; | |
909 | ||
910 | upt = RTA_DATA(rt); | |
911 | type = upt->type; | |
912 | } | |
913 | ||
914 | err = verify_policy_type(type); | |
915 | if (err) | |
916 | return err; | |
917 | ||
918 | *tp = type; | |
919 | return 0; | |
920 | } | |
921 | ||
1da177e4 LT |
922 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) |
923 | { | |
924 | xp->priority = p->priority; | |
925 | xp->index = p->index; | |
926 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); | |
927 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); | |
928 | xp->action = p->action; | |
929 | xp->flags = p->flags; | |
930 | xp->family = p->sel.family; | |
931 | /* XXX xp->share = p->share; */ | |
932 | } | |
933 | ||
934 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) | |
935 | { | |
936 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); | |
937 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); | |
938 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); | |
939 | p->priority = xp->priority; | |
940 | p->index = xp->index; | |
941 | p->sel.family = xp->family; | |
942 | p->dir = dir; | |
943 | p->action = xp->action; | |
944 | p->flags = xp->flags; | |
945 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ | |
946 | } | |
947 | ||
948 | static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp) | |
949 | { | |
950 | struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL); | |
951 | int err; | |
952 | ||
953 | if (!xp) { | |
954 | *errp = -ENOMEM; | |
955 | return NULL; | |
956 | } | |
957 | ||
958 | copy_from_user_policy(xp, p); | |
df71837d | 959 | |
f7b6983f MN |
960 | err = copy_from_user_policy_type(&xp->type, xfrma); |
961 | if (err) | |
962 | goto error; | |
963 | ||
df71837d TJ |
964 | if (!(err = copy_from_user_tmpl(xp, xfrma))) |
965 | err = copy_from_user_sec_ctx(xp, xfrma); | |
f7b6983f MN |
966 | if (err) |
967 | goto error; | |
1da177e4 LT |
968 | |
969 | return xp; | |
f7b6983f MN |
970 | error: |
971 | *errp = err; | |
972 | kfree(xp); | |
973 | return NULL; | |
1da177e4 LT |
974 | } |
975 | ||
976 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
977 | { | |
978 | struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh); | |
979 | struct xfrm_policy *xp; | |
26b15dad | 980 | struct km_event c; |
1da177e4 LT |
981 | int err; |
982 | int excl; | |
983 | ||
984 | err = verify_newpolicy_info(p); | |
df71837d TJ |
985 | if (err) |
986 | return err; | |
987 | err = verify_sec_ctx_len((struct rtattr **)xfrma); | |
1da177e4 LT |
988 | if (err) |
989 | return err; | |
990 | ||
df71837d | 991 | xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err); |
1da177e4 LT |
992 | if (!xp) |
993 | return err; | |
994 | ||
26b15dad JHS |
995 | /* shouldnt excl be based on nlh flags?? |
996 | * Aha! this is anti-netlink really i.e more pfkey derived | |
997 | * in netlink excl is a flag and you wouldnt need | |
998 | * a type XFRM_MSG_UPDPOLICY - JHS */ | |
1da177e4 LT |
999 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; |
1000 | err = xfrm_policy_insert(p->dir, xp, excl); | |
1001 | if (err) { | |
5f8ac64b | 1002 | security_xfrm_policy_free(xp); |
1da177e4 LT |
1003 | kfree(xp); |
1004 | return err; | |
1005 | } | |
1006 | ||
f60f6b8f | 1007 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1008 | c.seq = nlh->nlmsg_seq; |
1009 | c.pid = nlh->nlmsg_pid; | |
1010 | km_policy_notify(xp, p->dir, &c); | |
1011 | ||
1da177e4 LT |
1012 | xfrm_pol_put(xp); |
1013 | ||
1014 | return 0; | |
1015 | } | |
1016 | ||
1017 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) | |
1018 | { | |
1019 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; | |
1020 | int i; | |
1021 | ||
1022 | if (xp->xfrm_nr == 0) | |
1023 | return 0; | |
1024 | ||
1025 | for (i = 0; i < xp->xfrm_nr; i++) { | |
1026 | struct xfrm_user_tmpl *up = &vec[i]; | |
1027 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; | |
1028 | ||
1029 | memcpy(&up->id, &kp->id, sizeof(up->id)); | |
1030 | up->family = xp->family; | |
1031 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); | |
1032 | up->reqid = kp->reqid; | |
1033 | up->mode = kp->mode; | |
1034 | up->share = kp->share; | |
1035 | up->optional = kp->optional; | |
1036 | up->aalgos = kp->aalgos; | |
1037 | up->ealgos = kp->ealgos; | |
1038 | up->calgos = kp->calgos; | |
1039 | } | |
1040 | RTA_PUT(skb, XFRMA_TMPL, | |
1041 | (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr), | |
1042 | vec); | |
1043 | ||
1044 | return 0; | |
1045 | ||
1046 | rtattr_failure: | |
1047 | return -1; | |
1048 | } | |
1049 | ||
0d681623 | 1050 | static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) |
df71837d | 1051 | { |
0d681623 SH |
1052 | int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len; |
1053 | struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size); | |
1054 | struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); | |
1055 | ||
1056 | uctx->exttype = XFRMA_SEC_CTX; | |
1057 | uctx->len = ctx_size; | |
1058 | uctx->ctx_doi = s->ctx_doi; | |
1059 | uctx->ctx_alg = s->ctx_alg; | |
1060 | uctx->ctx_len = s->ctx_len; | |
1061 | memcpy(uctx + 1, s->ctx_str, s->ctx_len); | |
1062 | return 0; | |
df71837d | 1063 | |
0d681623 SH |
1064 | rtattr_failure: |
1065 | return -1; | |
1066 | } | |
1067 | ||
1068 | static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) | |
1069 | { | |
1070 | if (x->security) { | |
1071 | return copy_sec_ctx(x->security, skb); | |
df71837d TJ |
1072 | } |
1073 | return 0; | |
0d681623 | 1074 | } |
df71837d | 1075 | |
0d681623 SH |
1076 | static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) |
1077 | { | |
1078 | if (xp->security) { | |
1079 | return copy_sec_ctx(xp->security, skb); | |
1080 | } | |
1081 | return 0; | |
df71837d TJ |
1082 | } |
1083 | ||
f7b6983f MN |
1084 | #ifdef CONFIG_XFRM_SUB_POLICY |
1085 | static int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb) | |
1086 | { | |
1087 | struct xfrm_userpolicy_type upt; | |
1088 | ||
1089 | memset(&upt, 0, sizeof(upt)); | |
1090 | upt.type = xp->type; | |
1091 | ||
1092 | RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); | |
1093 | ||
1094 | return 0; | |
1095 | ||
1096 | rtattr_failure: | |
1097 | return -1; | |
1098 | } | |
1099 | ||
1100 | #else | |
1101 | static inline int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb) | |
1102 | { | |
1103 | return 0; | |
1104 | } | |
1105 | #endif | |
1106 | ||
1da177e4 LT |
1107 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) |
1108 | { | |
1109 | struct xfrm_dump_info *sp = ptr; | |
1110 | struct xfrm_userpolicy_info *p; | |
1111 | struct sk_buff *in_skb = sp->in_skb; | |
1112 | struct sk_buff *skb = sp->out_skb; | |
1113 | struct nlmsghdr *nlh; | |
1114 | unsigned char *b = skb->tail; | |
1115 | ||
1116 | if (sp->this_idx < sp->start_idx) | |
1117 | goto out; | |
1118 | ||
1119 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, | |
1120 | sp->nlmsg_seq, | |
1121 | XFRM_MSG_NEWPOLICY, sizeof(*p)); | |
1122 | p = NLMSG_DATA(nlh); | |
1123 | nlh->nlmsg_flags = sp->nlmsg_flags; | |
1124 | ||
1125 | copy_to_user_policy(xp, p, dir); | |
1126 | if (copy_to_user_tmpl(xp, skb) < 0) | |
1127 | goto nlmsg_failure; | |
df71837d TJ |
1128 | if (copy_to_user_sec_ctx(xp, skb)) |
1129 | goto nlmsg_failure; | |
f7b6983f MN |
1130 | if (copy_to_user_policy_type(xp, skb) < 0) |
1131 | goto nlmsg_failure; | |
1da177e4 LT |
1132 | |
1133 | nlh->nlmsg_len = skb->tail - b; | |
1134 | out: | |
1135 | sp->this_idx++; | |
1136 | return 0; | |
1137 | ||
1138 | nlmsg_failure: | |
1139 | skb_trim(skb, b - skb->data); | |
1140 | return -1; | |
1141 | } | |
1142 | ||
1143 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) | |
1144 | { | |
1145 | struct xfrm_dump_info info; | |
1146 | ||
1147 | info.in_skb = cb->skb; | |
1148 | info.out_skb = skb; | |
1149 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | |
1150 | info.nlmsg_flags = NLM_F_MULTI; | |
1151 | info.this_idx = 0; | |
1152 | info.start_idx = cb->args[0]; | |
f7b6983f MN |
1153 | (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info); |
1154 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1155 | (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info); | |
1156 | #endif | |
1da177e4 LT |
1157 | cb->args[0] = info.this_idx; |
1158 | ||
1159 | return skb->len; | |
1160 | } | |
1161 | ||
1162 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, | |
1163 | struct xfrm_policy *xp, | |
1164 | int dir, u32 seq) | |
1165 | { | |
1166 | struct xfrm_dump_info info; | |
1167 | struct sk_buff *skb; | |
1168 | ||
1169 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1170 | if (!skb) | |
1171 | return ERR_PTR(-ENOMEM); | |
1172 | ||
1173 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; | |
1174 | info.in_skb = in_skb; | |
1175 | info.out_skb = skb; | |
1176 | info.nlmsg_seq = seq; | |
1177 | info.nlmsg_flags = 0; | |
1178 | info.this_idx = info.start_idx = 0; | |
1179 | ||
1180 | if (dump_one_policy(xp, dir, 0, &info) < 0) { | |
1181 | kfree_skb(skb); | |
1182 | return NULL; | |
1183 | } | |
1184 | ||
1185 | return skb; | |
1186 | } | |
1187 | ||
1188 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
1189 | { | |
1190 | struct xfrm_policy *xp; | |
1191 | struct xfrm_userpolicy_id *p; | |
f7b6983f | 1192 | __u8 type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 | 1193 | int err; |
26b15dad | 1194 | struct km_event c; |
1da177e4 LT |
1195 | int delete; |
1196 | ||
1197 | p = NLMSG_DATA(nlh); | |
1198 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; | |
1199 | ||
f7b6983f MN |
1200 | err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); |
1201 | if (err) | |
1202 | return err; | |
1203 | ||
1da177e4 LT |
1204 | err = verify_policy_dir(p->dir); |
1205 | if (err) | |
1206 | return err; | |
1207 | ||
1208 | if (p->index) | |
f7b6983f | 1209 | xp = xfrm_policy_byid(type, p->dir, p->index, delete); |
df71837d TJ |
1210 | else { |
1211 | struct rtattr **rtattrs = (struct rtattr **)xfrma; | |
1212 | struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1]; | |
1213 | struct xfrm_policy tmp; | |
1214 | ||
1215 | err = verify_sec_ctx_len(rtattrs); | |
1216 | if (err) | |
1217 | return err; | |
1218 | ||
1219 | memset(&tmp, 0, sizeof(struct xfrm_policy)); | |
1220 | if (rt) { | |
1221 | struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); | |
1222 | ||
1223 | if ((err = security_xfrm_policy_alloc(&tmp, uctx))) | |
1224 | return err; | |
1225 | } | |
f7b6983f | 1226 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete); |
df71837d TJ |
1227 | security_xfrm_policy_free(&tmp); |
1228 | } | |
1da177e4 LT |
1229 | if (xp == NULL) |
1230 | return -ENOENT; | |
1231 | ||
1232 | if (!delete) { | |
1233 | struct sk_buff *resp_skb; | |
1234 | ||
1235 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); | |
1236 | if (IS_ERR(resp_skb)) { | |
1237 | err = PTR_ERR(resp_skb); | |
1238 | } else { | |
1239 | err = netlink_unicast(xfrm_nl, resp_skb, | |
1240 | NETLINK_CB(skb).pid, | |
1241 | MSG_DONTWAIT); | |
1242 | } | |
26b15dad | 1243 | } else { |
6f68dc37 | 1244 | if ((err = security_xfrm_policy_delete(xp)) != 0) |
c8c05a8e | 1245 | goto out; |
e7443892 | 1246 | c.data.byid = p->index; |
f60f6b8f | 1247 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1248 | c.seq = nlh->nlmsg_seq; |
1249 | c.pid = nlh->nlmsg_pid; | |
1250 | km_policy_notify(xp, p->dir, &c); | |
1da177e4 LT |
1251 | } |
1252 | ||
1253 | xfrm_pol_put(xp); | |
1254 | ||
c8c05a8e | 1255 | out: |
1da177e4 LT |
1256 | return err; |
1257 | } | |
1258 | ||
1259 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
1260 | { | |
26b15dad | 1261 | struct km_event c; |
1da177e4 LT |
1262 | struct xfrm_usersa_flush *p = NLMSG_DATA(nlh); |
1263 | ||
1264 | xfrm_state_flush(p->proto); | |
bf08867f | 1265 | c.data.proto = p->proto; |
f60f6b8f | 1266 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1267 | c.seq = nlh->nlmsg_seq; |
1268 | c.pid = nlh->nlmsg_pid; | |
1269 | km_state_notify(NULL, &c); | |
1270 | ||
1da177e4 LT |
1271 | return 0; |
1272 | } | |
1273 | ||
d51d081d JHS |
1274 | |
1275 | static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) | |
1276 | { | |
1277 | struct xfrm_aevent_id *id; | |
1278 | struct nlmsghdr *nlh; | |
1279 | struct xfrm_lifetime_cur ltime; | |
1280 | unsigned char *b = skb->tail; | |
1281 | ||
1282 | nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id)); | |
1283 | id = NLMSG_DATA(nlh); | |
1284 | nlh->nlmsg_flags = 0; | |
1285 | ||
1286 | id->sa_id.daddr = x->id.daddr; | |
1287 | id->sa_id.spi = x->id.spi; | |
1288 | id->sa_id.family = x->props.family; | |
1289 | id->sa_id.proto = x->id.proto; | |
1290 | id->flags = c->data.aevent; | |
1291 | ||
1292 | RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); | |
1293 | ||
1294 | ltime.bytes = x->curlft.bytes; | |
1295 | ltime.packets = x->curlft.packets; | |
1296 | ltime.add_time = x->curlft.add_time; | |
1297 | ltime.use_time = x->curlft.use_time; | |
1298 | ||
1299 | RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), <ime); | |
1300 | ||
1301 | if (id->flags&XFRM_AE_RTHR) { | |
1302 | RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff); | |
1303 | } | |
1304 | ||
1305 | if (id->flags&XFRM_AE_ETHR) { | |
1306 | u32 etimer = x->replay_maxage*10/HZ; | |
1307 | RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer); | |
1308 | } | |
1309 | ||
1310 | nlh->nlmsg_len = skb->tail - b; | |
1311 | return skb->len; | |
1312 | ||
1313 | rtattr_failure: | |
1314 | nlmsg_failure: | |
1315 | skb_trim(skb, b - skb->data); | |
1316 | return -1; | |
1317 | } | |
1318 | ||
1319 | static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
1320 | { | |
1321 | struct xfrm_state *x; | |
1322 | struct sk_buff *r_skb; | |
1323 | int err; | |
1324 | struct km_event c; | |
1325 | struct xfrm_aevent_id *p = NLMSG_DATA(nlh); | |
1326 | int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id)); | |
1327 | struct xfrm_usersa_id *id = &p->sa_id; | |
1328 | ||
1329 | len += RTA_SPACE(sizeof(struct xfrm_replay_state)); | |
1330 | len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur)); | |
1331 | ||
1332 | if (p->flags&XFRM_AE_RTHR) | |
1333 | len+=RTA_SPACE(sizeof(u32)); | |
1334 | ||
1335 | if (p->flags&XFRM_AE_ETHR) | |
1336 | len+=RTA_SPACE(sizeof(u32)); | |
1337 | ||
1338 | r_skb = alloc_skb(len, GFP_ATOMIC); | |
1339 | if (r_skb == NULL) | |
1340 | return -ENOMEM; | |
1341 | ||
1342 | x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family); | |
1343 | if (x == NULL) { | |
1344 | kfree(r_skb); | |
1345 | return -ESRCH; | |
1346 | } | |
1347 | ||
1348 | /* | |
1349 | * XXX: is this lock really needed - none of the other | |
1350 | * gets lock (the concern is things getting updated | |
1351 | * while we are still reading) - jhs | |
1352 | */ | |
1353 | spin_lock_bh(&x->lock); | |
1354 | c.data.aevent = p->flags; | |
1355 | c.seq = nlh->nlmsg_seq; | |
1356 | c.pid = nlh->nlmsg_pid; | |
1357 | ||
1358 | if (build_aevent(r_skb, x, &c) < 0) | |
1359 | BUG(); | |
1360 | err = netlink_unicast(xfrm_nl, r_skb, | |
1361 | NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
1362 | spin_unlock_bh(&x->lock); | |
1363 | xfrm_state_put(x); | |
1364 | return err; | |
1365 | } | |
1366 | ||
1367 | static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) | |
1368 | { | |
1369 | struct xfrm_state *x; | |
1370 | struct km_event c; | |
1371 | int err = - EINVAL; | |
1372 | struct xfrm_aevent_id *p = NLMSG_DATA(nlh); | |
1373 | struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1]; | |
1374 | struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1]; | |
1375 | ||
1376 | if (!lt && !rp) | |
1377 | return err; | |
1378 | ||
1379 | /* pedantic mode - thou shalt sayeth replaceth */ | |
1380 | if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) | |
1381 | return err; | |
1382 | ||
1383 | x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); | |
1384 | if (x == NULL) | |
1385 | return -ESRCH; | |
1386 | ||
1387 | if (x->km.state != XFRM_STATE_VALID) | |
1388 | goto out; | |
1389 | ||
1390 | spin_lock_bh(&x->lock); | |
1391 | err = xfrm_update_ae_params(x,(struct rtattr **)xfrma); | |
1392 | spin_unlock_bh(&x->lock); | |
1393 | if (err < 0) | |
1394 | goto out; | |
1395 | ||
1396 | c.event = nlh->nlmsg_type; | |
1397 | c.seq = nlh->nlmsg_seq; | |
1398 | c.pid = nlh->nlmsg_pid; | |
1399 | c.data.aevent = XFRM_AE_CU; | |
1400 | km_state_notify(x, &c); | |
1401 | err = 0; | |
1402 | out: | |
1403 | xfrm_state_put(x); | |
1404 | return err; | |
1405 | } | |
1406 | ||
1da177e4 LT |
1407 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
1408 | { | |
f7b6983f MN |
1409 | struct km_event c; |
1410 | __u8 type = XFRM_POLICY_TYPE_MAIN; | |
1411 | int err; | |
1412 | ||
1413 | err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); | |
1414 | if (err) | |
1415 | return err; | |
26b15dad | 1416 | |
f7b6983f MN |
1417 | xfrm_policy_flush(type); |
1418 | c.data.type = type; | |
f60f6b8f | 1419 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1420 | c.seq = nlh->nlmsg_seq; |
1421 | c.pid = nlh->nlmsg_pid; | |
1422 | km_policy_notify(NULL, 0, &c); | |
1da177e4 LT |
1423 | return 0; |
1424 | } | |
1425 | ||
6c5c8ca7 JHS |
1426 | static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
1427 | { | |
1428 | struct xfrm_policy *xp; | |
1429 | struct xfrm_user_polexpire *up = NLMSG_DATA(nlh); | |
1430 | struct xfrm_userpolicy_info *p = &up->pol; | |
f7b6983f | 1431 | __u8 type = XFRM_POLICY_TYPE_MAIN; |
6c5c8ca7 JHS |
1432 | int err = -ENOENT; |
1433 | ||
f7b6983f MN |
1434 | err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma); |
1435 | if (err) | |
1436 | return err; | |
1437 | ||
6c5c8ca7 | 1438 | if (p->index) |
f7b6983f | 1439 | xp = xfrm_policy_byid(type, p->dir, p->index, 0); |
6c5c8ca7 JHS |
1440 | else { |
1441 | struct rtattr **rtattrs = (struct rtattr **)xfrma; | |
1442 | struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1]; | |
1443 | struct xfrm_policy tmp; | |
1444 | ||
1445 | err = verify_sec_ctx_len(rtattrs); | |
1446 | if (err) | |
1447 | return err; | |
1448 | ||
1449 | memset(&tmp, 0, sizeof(struct xfrm_policy)); | |
1450 | if (rt) { | |
1451 | struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt); | |
1452 | ||
1453 | if ((err = security_xfrm_policy_alloc(&tmp, uctx))) | |
1454 | return err; | |
1455 | } | |
f7b6983f | 1456 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0); |
6c5c8ca7 JHS |
1457 | security_xfrm_policy_free(&tmp); |
1458 | } | |
1459 | ||
1460 | if (xp == NULL) | |
1461 | return err; | |
1462 | read_lock(&xp->lock); | |
1463 | if (xp->dead) { | |
1464 | read_unlock(&xp->lock); | |
1465 | goto out; | |
1466 | } | |
1467 | ||
1468 | read_unlock(&xp->lock); | |
1469 | err = 0; | |
1470 | if (up->hard) { | |
1471 | xfrm_policy_delete(xp, p->dir); | |
1472 | } else { | |
1473 | // reset the timers here? | |
1474 | printk("Dont know what to do with soft policy expire\n"); | |
1475 | } | |
1476 | km_policy_expired(xp, p->dir, up->hard, current->pid); | |
1477 | ||
1478 | out: | |
1479 | xfrm_pol_put(xp); | |
1480 | return err; | |
1481 | } | |
1482 | ||
53bc6b4d JHS |
1483 | static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
1484 | { | |
1485 | struct xfrm_state *x; | |
1486 | int err; | |
1487 | struct xfrm_user_expire *ue = NLMSG_DATA(nlh); | |
1488 | struct xfrm_usersa_info *p = &ue->state; | |
1489 | ||
1490 | x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family); | |
1491 | err = -ENOENT; | |
1492 | ||
1493 | if (x == NULL) | |
1494 | return err; | |
1495 | ||
1496 | err = -EINVAL; | |
1497 | ||
1498 | spin_lock_bh(&x->lock); | |
1499 | if (x->km.state != XFRM_STATE_VALID) | |
1500 | goto out; | |
1501 | km_state_expired(x, ue->hard, current->pid); | |
1502 | ||
1503 | if (ue->hard) | |
1504 | __xfrm_state_delete(x); | |
1505 | out: | |
1506 | spin_unlock_bh(&x->lock); | |
1507 | xfrm_state_put(x); | |
1508 | return err; | |
1509 | } | |
1510 | ||
980ebd25 JHS |
1511 | static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma) |
1512 | { | |
1513 | struct xfrm_policy *xp; | |
1514 | struct xfrm_user_tmpl *ut; | |
1515 | int i; | |
1516 | struct rtattr *rt = xfrma[XFRMA_TMPL-1]; | |
1517 | ||
1518 | struct xfrm_user_acquire *ua = NLMSG_DATA(nlh); | |
1519 | struct xfrm_state *x = xfrm_state_alloc(); | |
1520 | int err = -ENOMEM; | |
1521 | ||
1522 | if (!x) | |
1523 | return err; | |
1524 | ||
1525 | err = verify_newpolicy_info(&ua->policy); | |
1526 | if (err) { | |
1527 | printk("BAD policy passed\n"); | |
1528 | kfree(x); | |
1529 | return err; | |
1530 | } | |
1531 | ||
1532 | /* build an XP */ | |
1533 | xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) { | |
1534 | kfree(x); | |
1535 | return err; | |
1536 | } | |
1537 | ||
1538 | memcpy(&x->id, &ua->id, sizeof(ua->id)); | |
1539 | memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); | |
1540 | memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); | |
1541 | ||
1542 | ut = RTA_DATA(rt); | |
1543 | /* extract the templates and for each call km_key */ | |
1544 | for (i = 0; i < xp->xfrm_nr; i++, ut++) { | |
1545 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
1546 | memcpy(&x->id, &t->id, sizeof(x->id)); | |
1547 | x->props.mode = t->mode; | |
1548 | x->props.reqid = t->reqid; | |
1549 | x->props.family = ut->family; | |
1550 | t->aalgos = ua->aalgos; | |
1551 | t->ealgos = ua->ealgos; | |
1552 | t->calgos = ua->calgos; | |
1553 | err = km_query(x, t, xp); | |
1554 | ||
1555 | } | |
1556 | ||
1557 | kfree(x); | |
1558 | kfree(xp); | |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | ||
d51d081d | 1563 | |
492b558b TG |
1564 | #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type)) |
1565 | ||
1566 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { | |
1567 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
1568 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1569 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1570 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), | |
1571 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1572 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1573 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), | |
980ebd25 | 1574 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), |
53bc6b4d | 1575 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), |
492b558b TG |
1576 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
1577 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
6c5c8ca7 | 1578 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), |
492b558b TG |
1579 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
1580 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0), | |
d51d081d JHS |
1581 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
1582 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), | |
97a64b45 | 1583 | [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), |
1da177e4 LT |
1584 | }; |
1585 | ||
492b558b TG |
1586 | #undef XMSGSIZE |
1587 | ||
1da177e4 LT |
1588 | static struct xfrm_link { |
1589 | int (*doit)(struct sk_buff *, struct nlmsghdr *, void **); | |
1590 | int (*dump)(struct sk_buff *, struct netlink_callback *); | |
492b558b TG |
1591 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { |
1592 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
1593 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, | |
1594 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, | |
1595 | .dump = xfrm_dump_sa }, | |
1596 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, | |
1597 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, | |
1598 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, | |
1599 | .dump = xfrm_dump_policy }, | |
1600 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, | |
980ebd25 | 1601 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, |
53bc6b4d | 1602 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, |
492b558b TG |
1603 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
1604 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
6c5c8ca7 | 1605 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, |
492b558b TG |
1606 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, |
1607 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, | |
d51d081d JHS |
1608 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, |
1609 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, | |
1da177e4 LT |
1610 | }; |
1611 | ||
1da177e4 LT |
1612 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) |
1613 | { | |
1614 | struct rtattr *xfrma[XFRMA_MAX]; | |
1615 | struct xfrm_link *link; | |
1616 | int type, min_len; | |
1617 | ||
1618 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) | |
1619 | return 0; | |
1620 | ||
1621 | type = nlh->nlmsg_type; | |
1622 | ||
1623 | /* A control message: ignore them */ | |
1624 | if (type < XFRM_MSG_BASE) | |
1625 | return 0; | |
1626 | ||
1627 | /* Unknown message: reply with EINVAL */ | |
1628 | if (type > XFRM_MSG_MAX) | |
1629 | goto err_einval; | |
1630 | ||
1631 | type -= XFRM_MSG_BASE; | |
1632 | link = &xfrm_dispatch[type]; | |
1633 | ||
1634 | /* All operations require privileges, even GET */ | |
c7bdb545 | 1635 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) { |
1da177e4 LT |
1636 | *errp = -EPERM; |
1637 | return -1; | |
1638 | } | |
1639 | ||
492b558b TG |
1640 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || |
1641 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && | |
1642 | (nlh->nlmsg_flags & NLM_F_DUMP)) { | |
1da177e4 LT |
1643 | if (link->dump == NULL) |
1644 | goto err_einval; | |
1645 | ||
1646 | if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh, | |
a8f74b22 | 1647 | link->dump, NULL)) != 0) { |
1da177e4 LT |
1648 | return -1; |
1649 | } | |
88fc2c84 TG |
1650 | |
1651 | netlink_queue_skip(nlh, skb); | |
1da177e4 LT |
1652 | return -1; |
1653 | } | |
1654 | ||
1655 | memset(xfrma, 0, sizeof(xfrma)); | |
1656 | ||
1657 | if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type])) | |
1658 | goto err_einval; | |
1659 | ||
1660 | if (nlh->nlmsg_len > min_len) { | |
1661 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
1662 | struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len); | |
1663 | ||
1664 | while (RTA_OK(attr, attrlen)) { | |
1665 | unsigned short flavor = attr->rta_type; | |
1666 | if (flavor) { | |
1667 | if (flavor > XFRMA_MAX) | |
1668 | goto err_einval; | |
1669 | xfrma[flavor - 1] = attr; | |
1670 | } | |
1671 | attr = RTA_NEXT(attr, attrlen); | |
1672 | } | |
1673 | } | |
1674 | ||
1675 | if (link->doit == NULL) | |
1676 | goto err_einval; | |
1677 | *errp = link->doit(skb, nlh, (void **) &xfrma); | |
1678 | ||
1679 | return *errp; | |
1680 | ||
1681 | err_einval: | |
1682 | *errp = -EINVAL; | |
1683 | return -1; | |
1684 | } | |
1685 | ||
1da177e4 LT |
1686 | static void xfrm_netlink_rcv(struct sock *sk, int len) |
1687 | { | |
88fc2c84 | 1688 | unsigned int qlen = 0; |
2a0a6ebe | 1689 | |
1da177e4 | 1690 | do { |
4a3e2f71 | 1691 | mutex_lock(&xfrm_cfg_mutex); |
88fc2c84 | 1692 | netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg); |
4a3e2f71 | 1693 | mutex_unlock(&xfrm_cfg_mutex); |
1da177e4 | 1694 | |
2a0a6ebe | 1695 | } while (qlen); |
1da177e4 LT |
1696 | } |
1697 | ||
d51d081d | 1698 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1699 | { |
1700 | struct xfrm_user_expire *ue; | |
1701 | struct nlmsghdr *nlh; | |
1702 | unsigned char *b = skb->tail; | |
1703 | ||
d51d081d | 1704 | nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE, |
1da177e4 LT |
1705 | sizeof(*ue)); |
1706 | ue = NLMSG_DATA(nlh); | |
1707 | nlh->nlmsg_flags = 0; | |
1708 | ||
1709 | copy_to_user_state(x, &ue->state); | |
d51d081d | 1710 | ue->hard = (c->data.hard != 0) ? 1 : 0; |
1da177e4 LT |
1711 | |
1712 | nlh->nlmsg_len = skb->tail - b; | |
1713 | return skb->len; | |
1714 | ||
1715 | nlmsg_failure: | |
1716 | skb_trim(skb, b - skb->data); | |
1717 | return -1; | |
1718 | } | |
1719 | ||
26b15dad | 1720 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1721 | { |
1722 | struct sk_buff *skb; | |
ee57eef9 | 1723 | int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire)); |
1da177e4 | 1724 | |
ee57eef9 | 1725 | skb = alloc_skb(len, GFP_ATOMIC); |
1da177e4 LT |
1726 | if (skb == NULL) |
1727 | return -ENOMEM; | |
1728 | ||
d51d081d | 1729 | if (build_expire(skb, x, c) < 0) |
1da177e4 LT |
1730 | BUG(); |
1731 | ||
ac6d439d PM |
1732 | NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE; |
1733 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); | |
1da177e4 LT |
1734 | } |
1735 | ||
d51d081d JHS |
1736 | static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) |
1737 | { | |
1738 | struct sk_buff *skb; | |
1739 | int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id)); | |
1740 | ||
1741 | len += RTA_SPACE(sizeof(struct xfrm_replay_state)); | |
1742 | len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur)); | |
1743 | skb = alloc_skb(len, GFP_ATOMIC); | |
1744 | if (skb == NULL) | |
1745 | return -ENOMEM; | |
1746 | ||
1747 | if (build_aevent(skb, x, c) < 0) | |
1748 | BUG(); | |
1749 | ||
1750 | NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS; | |
1751 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); | |
1752 | } | |
1753 | ||
26b15dad JHS |
1754 | static int xfrm_notify_sa_flush(struct km_event *c) |
1755 | { | |
1756 | struct xfrm_usersa_flush *p; | |
1757 | struct nlmsghdr *nlh; | |
1758 | struct sk_buff *skb; | |
1759 | unsigned char *b; | |
1760 | int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush)); | |
1761 | ||
1762 | skb = alloc_skb(len, GFP_ATOMIC); | |
1763 | if (skb == NULL) | |
1764 | return -ENOMEM; | |
1765 | b = skb->tail; | |
1766 | ||
1767 | nlh = NLMSG_PUT(skb, c->pid, c->seq, | |
1768 | XFRM_MSG_FLUSHSA, sizeof(*p)); | |
1769 | nlh->nlmsg_flags = 0; | |
1770 | ||
1771 | p = NLMSG_DATA(nlh); | |
bf08867f | 1772 | p->proto = c->data.proto; |
26b15dad JHS |
1773 | |
1774 | nlh->nlmsg_len = skb->tail - b; | |
1775 | ||
ac6d439d PM |
1776 | NETLINK_CB(skb).dst_group = XFRMNLGRP_SA; |
1777 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); | |
26b15dad JHS |
1778 | |
1779 | nlmsg_failure: | |
1780 | kfree_skb(skb); | |
1781 | return -1; | |
1782 | } | |
1783 | ||
1784 | static int inline xfrm_sa_len(struct xfrm_state *x) | |
1785 | { | |
0603eac0 | 1786 | int l = 0; |
26b15dad JHS |
1787 | if (x->aalg) |
1788 | l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8); | |
1789 | if (x->ealg) | |
1790 | l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8); | |
1791 | if (x->calg) | |
1792 | l += RTA_SPACE(sizeof(*x->calg)); | |
1793 | if (x->encap) | |
1794 | l += RTA_SPACE(sizeof(*x->encap)); | |
1795 | ||
1796 | return l; | |
1797 | } | |
1798 | ||
1799 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) | |
1800 | { | |
1801 | struct xfrm_usersa_info *p; | |
0603eac0 | 1802 | struct xfrm_usersa_id *id; |
26b15dad JHS |
1803 | struct nlmsghdr *nlh; |
1804 | struct sk_buff *skb; | |
26b15dad JHS |
1805 | unsigned char *b; |
1806 | int len = xfrm_sa_len(x); | |
0603eac0 HX |
1807 | int headlen; |
1808 | ||
1809 | headlen = sizeof(*p); | |
1810 | if (c->event == XFRM_MSG_DELSA) { | |
1811 | len += RTA_SPACE(headlen); | |
1812 | headlen = sizeof(*id); | |
1813 | } | |
1814 | len += NLMSG_SPACE(headlen); | |
26b15dad JHS |
1815 | |
1816 | skb = alloc_skb(len, GFP_ATOMIC); | |
1817 | if (skb == NULL) | |
1818 | return -ENOMEM; | |
1819 | b = skb->tail; | |
1820 | ||
0603eac0 | 1821 | nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); |
26b15dad JHS |
1822 | nlh->nlmsg_flags = 0; |
1823 | ||
1824 | p = NLMSG_DATA(nlh); | |
0603eac0 HX |
1825 | if (c->event == XFRM_MSG_DELSA) { |
1826 | id = NLMSG_DATA(nlh); | |
1827 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); | |
1828 | id->spi = x->id.spi; | |
1829 | id->family = x->props.family; | |
1830 | id->proto = x->id.proto; | |
1831 | ||
1832 | p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p))); | |
1833 | } | |
1834 | ||
26b15dad JHS |
1835 | copy_to_user_state(x, p); |
1836 | ||
1837 | if (x->aalg) | |
1838 | RTA_PUT(skb, XFRMA_ALG_AUTH, | |
1839 | sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg); | |
1840 | if (x->ealg) | |
1841 | RTA_PUT(skb, XFRMA_ALG_CRYPT, | |
1842 | sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg); | |
1843 | if (x->calg) | |
1844 | RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); | |
1845 | ||
1846 | if (x->encap) | |
1847 | RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); | |
1848 | ||
1849 | nlh->nlmsg_len = skb->tail - b; | |
1850 | ||
ac6d439d PM |
1851 | NETLINK_CB(skb).dst_group = XFRMNLGRP_SA; |
1852 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); | |
26b15dad JHS |
1853 | |
1854 | nlmsg_failure: | |
1855 | rtattr_failure: | |
1856 | kfree_skb(skb); | |
1857 | return -1; | |
1858 | } | |
1859 | ||
1860 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) | |
1861 | { | |
1862 | ||
1863 | switch (c->event) { | |
f60f6b8f | 1864 | case XFRM_MSG_EXPIRE: |
26b15dad | 1865 | return xfrm_exp_state_notify(x, c); |
d51d081d JHS |
1866 | case XFRM_MSG_NEWAE: |
1867 | return xfrm_aevent_state_notify(x, c); | |
f60f6b8f HX |
1868 | case XFRM_MSG_DELSA: |
1869 | case XFRM_MSG_UPDSA: | |
1870 | case XFRM_MSG_NEWSA: | |
26b15dad | 1871 | return xfrm_notify_sa(x, c); |
f60f6b8f | 1872 | case XFRM_MSG_FLUSHSA: |
26b15dad JHS |
1873 | return xfrm_notify_sa_flush(c); |
1874 | default: | |
1875 | printk("xfrm_user: Unknown SA event %d\n", c->event); | |
1876 | break; | |
1877 | } | |
1878 | ||
1879 | return 0; | |
1880 | ||
1881 | } | |
1882 | ||
1da177e4 LT |
1883 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, |
1884 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, | |
1885 | int dir) | |
1886 | { | |
1887 | struct xfrm_user_acquire *ua; | |
1888 | struct nlmsghdr *nlh; | |
1889 | unsigned char *b = skb->tail; | |
1890 | __u32 seq = xfrm_get_acqseq(); | |
1891 | ||
1892 | nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE, | |
1893 | sizeof(*ua)); | |
1894 | ua = NLMSG_DATA(nlh); | |
1895 | nlh->nlmsg_flags = 0; | |
1896 | ||
1897 | memcpy(&ua->id, &x->id, sizeof(ua->id)); | |
1898 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); | |
1899 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); | |
1900 | copy_to_user_policy(xp, &ua->policy, dir); | |
1901 | ua->aalgos = xt->aalgos; | |
1902 | ua->ealgos = xt->ealgos; | |
1903 | ua->calgos = xt->calgos; | |
1904 | ua->seq = x->km.seq = seq; | |
1905 | ||
1906 | if (copy_to_user_tmpl(xp, skb) < 0) | |
1907 | goto nlmsg_failure; | |
0d681623 | 1908 | if (copy_to_user_state_sec_ctx(x, skb)) |
df71837d | 1909 | goto nlmsg_failure; |
f7b6983f MN |
1910 | if (copy_to_user_policy_type(xp, skb) < 0) |
1911 | goto nlmsg_failure; | |
1da177e4 LT |
1912 | |
1913 | nlh->nlmsg_len = skb->tail - b; | |
1914 | return skb->len; | |
1915 | ||
1916 | nlmsg_failure: | |
1917 | skb_trim(skb, b - skb->data); | |
1918 | return -1; | |
1919 | } | |
1920 | ||
1921 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, | |
1922 | struct xfrm_policy *xp, int dir) | |
1923 | { | |
1924 | struct sk_buff *skb; | |
1925 | size_t len; | |
1926 | ||
1927 | len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); | |
1928 | len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire)); | |
df71837d | 1929 | len += RTA_SPACE(xfrm_user_sec_ctx_size(xp)); |
1da177e4 LT |
1930 | skb = alloc_skb(len, GFP_ATOMIC); |
1931 | if (skb == NULL) | |
1932 | return -ENOMEM; | |
1933 | ||
1934 | if (build_acquire(skb, x, xt, xp, dir) < 0) | |
1935 | BUG(); | |
1936 | ||
ac6d439d PM |
1937 | NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE; |
1938 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); | |
1da177e4 LT |
1939 | } |
1940 | ||
1941 | /* User gives us xfrm_user_policy_info followed by an array of 0 | |
1942 | * or more templates. | |
1943 | */ | |
cb969f07 | 1944 | static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, |
1da177e4 LT |
1945 | u8 *data, int len, int *dir) |
1946 | { | |
1947 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; | |
1948 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); | |
1949 | struct xfrm_policy *xp; | |
1950 | int nr; | |
1951 | ||
cb969f07 | 1952 | switch (sk->sk_family) { |
1da177e4 LT |
1953 | case AF_INET: |
1954 | if (opt != IP_XFRM_POLICY) { | |
1955 | *dir = -EOPNOTSUPP; | |
1956 | return NULL; | |
1957 | } | |
1958 | break; | |
1959 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
1960 | case AF_INET6: | |
1961 | if (opt != IPV6_XFRM_POLICY) { | |
1962 | *dir = -EOPNOTSUPP; | |
1963 | return NULL; | |
1964 | } | |
1965 | break; | |
1966 | #endif | |
1967 | default: | |
1968 | *dir = -EINVAL; | |
1969 | return NULL; | |
1970 | } | |
1971 | ||
1972 | *dir = -EINVAL; | |
1973 | ||
1974 | if (len < sizeof(*p) || | |
1975 | verify_newpolicy_info(p)) | |
1976 | return NULL; | |
1977 | ||
1978 | nr = ((len - sizeof(*p)) / sizeof(*ut)); | |
1979 | if (nr > XFRM_MAX_DEPTH) | |
1980 | return NULL; | |
1981 | ||
a4f1bac6 HX |
1982 | if (p->dir > XFRM_POLICY_OUT) |
1983 | return NULL; | |
1984 | ||
1da177e4 LT |
1985 | xp = xfrm_policy_alloc(GFP_KERNEL); |
1986 | if (xp == NULL) { | |
1987 | *dir = -ENOBUFS; | |
1988 | return NULL; | |
1989 | } | |
1990 | ||
1991 | copy_from_user_policy(xp, p); | |
f7b6983f | 1992 | xp->type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 LT |
1993 | copy_templates(xp, ut, nr); |
1994 | ||
1995 | *dir = p->dir; | |
1996 | ||
1997 | return xp; | |
1998 | } | |
1999 | ||
2000 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, | |
d51d081d | 2001 | int dir, struct km_event *c) |
1da177e4 LT |
2002 | { |
2003 | struct xfrm_user_polexpire *upe; | |
2004 | struct nlmsghdr *nlh; | |
d51d081d | 2005 | int hard = c->data.hard; |
1da177e4 LT |
2006 | unsigned char *b = skb->tail; |
2007 | ||
d51d081d | 2008 | nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe)); |
1da177e4 LT |
2009 | upe = NLMSG_DATA(nlh); |
2010 | nlh->nlmsg_flags = 0; | |
2011 | ||
2012 | copy_to_user_policy(xp, &upe->pol, dir); | |
2013 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2014 | goto nlmsg_failure; | |
df71837d TJ |
2015 | if (copy_to_user_sec_ctx(xp, skb)) |
2016 | goto nlmsg_failure; | |
f7b6983f MN |
2017 | if (copy_to_user_policy_type(xp, skb) < 0) |
2018 | goto nlmsg_failure; | |
1da177e4 LT |
2019 | upe->hard = !!hard; |
2020 | ||
2021 | nlh->nlmsg_len = skb->tail - b; | |
2022 | return skb->len; | |
2023 | ||
2024 | nlmsg_failure: | |
2025 | skb_trim(skb, b - skb->data); | |
2026 | return -1; | |
2027 | } | |
2028 | ||
26b15dad | 2029 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
1da177e4 LT |
2030 | { |
2031 | struct sk_buff *skb; | |
2032 | size_t len; | |
2033 | ||
2034 | len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); | |
2035 | len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire)); | |
df71837d | 2036 | len += RTA_SPACE(xfrm_user_sec_ctx_size(xp)); |
1da177e4 LT |
2037 | skb = alloc_skb(len, GFP_ATOMIC); |
2038 | if (skb == NULL) | |
2039 | return -ENOMEM; | |
2040 | ||
d51d081d | 2041 | if (build_polexpire(skb, xp, dir, c) < 0) |
1da177e4 LT |
2042 | BUG(); |
2043 | ||
ac6d439d PM |
2044 | NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE; |
2045 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); | |
1da177e4 LT |
2046 | } |
2047 | ||
26b15dad JHS |
2048 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
2049 | { | |
2050 | struct xfrm_userpolicy_info *p; | |
0603eac0 | 2051 | struct xfrm_userpolicy_id *id; |
26b15dad JHS |
2052 | struct nlmsghdr *nlh; |
2053 | struct sk_buff *skb; | |
26b15dad JHS |
2054 | unsigned char *b; |
2055 | int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); | |
0603eac0 HX |
2056 | int headlen; |
2057 | ||
2058 | headlen = sizeof(*p); | |
2059 | if (c->event == XFRM_MSG_DELPOLICY) { | |
2060 | len += RTA_SPACE(headlen); | |
2061 | headlen = sizeof(*id); | |
2062 | } | |
2063 | len += NLMSG_SPACE(headlen); | |
26b15dad JHS |
2064 | |
2065 | skb = alloc_skb(len, GFP_ATOMIC); | |
2066 | if (skb == NULL) | |
2067 | return -ENOMEM; | |
2068 | b = skb->tail; | |
2069 | ||
0603eac0 | 2070 | nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen); |
26b15dad JHS |
2071 | |
2072 | p = NLMSG_DATA(nlh); | |
0603eac0 HX |
2073 | if (c->event == XFRM_MSG_DELPOLICY) { |
2074 | id = NLMSG_DATA(nlh); | |
2075 | memset(id, 0, sizeof(*id)); | |
2076 | id->dir = dir; | |
2077 | if (c->data.byid) | |
2078 | id->index = xp->index; | |
2079 | else | |
2080 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); | |
2081 | ||
2082 | p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p))); | |
2083 | } | |
26b15dad JHS |
2084 | |
2085 | nlh->nlmsg_flags = 0; | |
2086 | ||
2087 | copy_to_user_policy(xp, p, dir); | |
2088 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2089 | goto nlmsg_failure; | |
f7b6983f MN |
2090 | if (copy_to_user_policy_type(xp, skb) < 0) |
2091 | goto nlmsg_failure; | |
26b15dad JHS |
2092 | |
2093 | nlh->nlmsg_len = skb->tail - b; | |
2094 | ||
ac6d439d PM |
2095 | NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY; |
2096 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); | |
26b15dad JHS |
2097 | |
2098 | nlmsg_failure: | |
0603eac0 | 2099 | rtattr_failure: |
26b15dad JHS |
2100 | kfree_skb(skb); |
2101 | return -1; | |
2102 | } | |
2103 | ||
2104 | static int xfrm_notify_policy_flush(struct km_event *c) | |
2105 | { | |
2106 | struct nlmsghdr *nlh; | |
2107 | struct sk_buff *skb; | |
2108 | unsigned char *b; | |
f7b6983f MN |
2109 | #ifdef CONFIG_XFRM_SUB_POLICY |
2110 | struct xfrm_userpolicy_type upt; | |
2111 | #endif | |
26b15dad JHS |
2112 | int len = NLMSG_LENGTH(0); |
2113 | ||
2114 | skb = alloc_skb(len, GFP_ATOMIC); | |
2115 | if (skb == NULL) | |
2116 | return -ENOMEM; | |
2117 | b = skb->tail; | |
2118 | ||
2119 | ||
2120 | nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0); | |
f7b6983f MN |
2121 | nlh->nlmsg_flags = 0; |
2122 | ||
2123 | #ifdef CONFIG_XFRM_SUB_POLICY | |
2124 | memset(&upt, 0, sizeof(upt)); | |
2125 | upt.type = c->data.type; | |
2126 | RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); | |
2127 | #endif | |
26b15dad JHS |
2128 | |
2129 | nlh->nlmsg_len = skb->tail - b; | |
2130 | ||
ac6d439d PM |
2131 | NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY; |
2132 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); | |
26b15dad JHS |
2133 | |
2134 | nlmsg_failure: | |
f7b6983f MN |
2135 | #ifdef CONFIG_XFRM_SUB_POLICY |
2136 | rtattr_failure: | |
2137 | #endif | |
26b15dad JHS |
2138 | kfree_skb(skb); |
2139 | return -1; | |
2140 | } | |
2141 | ||
2142 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | |
2143 | { | |
2144 | ||
2145 | switch (c->event) { | |
f60f6b8f HX |
2146 | case XFRM_MSG_NEWPOLICY: |
2147 | case XFRM_MSG_UPDPOLICY: | |
2148 | case XFRM_MSG_DELPOLICY: | |
26b15dad | 2149 | return xfrm_notify_policy(xp, dir, c); |
f60f6b8f | 2150 | case XFRM_MSG_FLUSHPOLICY: |
26b15dad | 2151 | return xfrm_notify_policy_flush(c); |
f60f6b8f | 2152 | case XFRM_MSG_POLEXPIRE: |
26b15dad JHS |
2153 | return xfrm_exp_policy_notify(xp, dir, c); |
2154 | default: | |
2155 | printk("xfrm_user: Unknown Policy event %d\n", c->event); | |
2156 | } | |
2157 | ||
2158 | return 0; | |
2159 | ||
2160 | } | |
2161 | ||
97a64b45 MN |
2162 | static int build_report(struct sk_buff *skb, u8 proto, |
2163 | struct xfrm_selector *sel, xfrm_address_t *addr) | |
2164 | { | |
2165 | struct xfrm_user_report *ur; | |
2166 | struct nlmsghdr *nlh; | |
2167 | unsigned char *b = skb->tail; | |
2168 | ||
2169 | nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur)); | |
2170 | ur = NLMSG_DATA(nlh); | |
2171 | nlh->nlmsg_flags = 0; | |
2172 | ||
2173 | ur->proto = proto; | |
2174 | memcpy(&ur->sel, sel, sizeof(ur->sel)); | |
2175 | ||
2176 | if (addr) | |
2177 | RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); | |
2178 | ||
2179 | nlh->nlmsg_len = skb->tail - b; | |
2180 | return skb->len; | |
2181 | ||
2182 | nlmsg_failure: | |
2183 | rtattr_failure: | |
2184 | skb_trim(skb, b - skb->data); | |
2185 | return -1; | |
2186 | } | |
2187 | ||
2188 | static int xfrm_send_report(u8 proto, struct xfrm_selector *sel, | |
2189 | xfrm_address_t *addr) | |
2190 | { | |
2191 | struct sk_buff *skb; | |
2192 | size_t len; | |
2193 | ||
2194 | len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report))); | |
2195 | skb = alloc_skb(len, GFP_ATOMIC); | |
2196 | if (skb == NULL) | |
2197 | return -ENOMEM; | |
2198 | ||
2199 | if (build_report(skb, proto, sel, addr) < 0) | |
2200 | BUG(); | |
2201 | ||
2202 | NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT; | |
2203 | return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); | |
2204 | } | |
2205 | ||
1da177e4 LT |
2206 | static struct xfrm_mgr netlink_mgr = { |
2207 | .id = "netlink", | |
2208 | .notify = xfrm_send_state_notify, | |
2209 | .acquire = xfrm_send_acquire, | |
2210 | .compile_policy = xfrm_compile_policy, | |
2211 | .notify_policy = xfrm_send_policy_notify, | |
97a64b45 | 2212 | .report = xfrm_send_report, |
1da177e4 LT |
2213 | }; |
2214 | ||
2215 | static int __init xfrm_user_init(void) | |
2216 | { | |
be33690d PM |
2217 | struct sock *nlsk; |
2218 | ||
654b32c6 | 2219 | printk(KERN_INFO "Initializing XFRM netlink socket\n"); |
1da177e4 | 2220 | |
be33690d PM |
2221 | nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX, |
2222 | xfrm_netlink_rcv, THIS_MODULE); | |
2223 | if (nlsk == NULL) | |
1da177e4 | 2224 | return -ENOMEM; |
be33690d | 2225 | rcu_assign_pointer(xfrm_nl, nlsk); |
1da177e4 LT |
2226 | |
2227 | xfrm_register_km(&netlink_mgr); | |
2228 | ||
2229 | return 0; | |
2230 | } | |
2231 | ||
2232 | static void __exit xfrm_user_exit(void) | |
2233 | { | |
be33690d PM |
2234 | struct sock *nlsk = xfrm_nl; |
2235 | ||
1da177e4 | 2236 | xfrm_unregister_km(&netlink_mgr); |
be33690d PM |
2237 | rcu_assign_pointer(xfrm_nl, NULL); |
2238 | synchronize_rcu(); | |
2239 | sock_release(nlsk->sk_socket); | |
1da177e4 LT |
2240 | } |
2241 | ||
2242 | module_init(xfrm_user_init); | |
2243 | module_exit(xfrm_user_exit); | |
2244 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 | 2245 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); |
f8cd5488 | 2246 |