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