]>
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/pfkeyv2.h> |
23 | #include <linux/ipsec.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/security.h> | |
26 | #include <net/sock.h> | |
27 | #include <net/xfrm.h> | |
88fc2c84 | 28 | #include <net/netlink.h> |
1da177e4 | 29 | #include <asm/uaccess.h> |
e23c7194 MN |
30 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
31 | #include <linux/in6.h> | |
32 | #endif | |
1da177e4 | 33 | |
1a6509d9 HX |
34 | static inline int aead_len(struct xfrm_algo_aead *alg) |
35 | { | |
36 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | |
37 | } | |
38 | ||
5424f32e | 39 | static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) |
1da177e4 | 40 | { |
5424f32e | 41 | struct nlattr *rt = attrs[type]; |
1da177e4 LT |
42 | struct xfrm_algo *algp; |
43 | ||
44 | if (!rt) | |
45 | return 0; | |
46 | ||
5424f32e | 47 | algp = nla_data(rt); |
0f99be0d | 48 | if (nla_len(rt) < xfrm_alg_len(algp)) |
31c26852 HX |
49 | return -EINVAL; |
50 | ||
1da177e4 LT |
51 | switch (type) { |
52 | case XFRMA_ALG_AUTH: | |
53 | if (!algp->alg_key_len && | |
54 | strcmp(algp->alg_name, "digest_null") != 0) | |
55 | return -EINVAL; | |
56 | break; | |
57 | ||
58 | case XFRMA_ALG_CRYPT: | |
59 | if (!algp->alg_key_len && | |
60 | strcmp(algp->alg_name, "cipher_null") != 0) | |
61 | return -EINVAL; | |
62 | break; | |
63 | ||
64 | case XFRMA_ALG_COMP: | |
65 | /* Zero length keys are legal. */ | |
66 | break; | |
67 | ||
68 | default: | |
69 | return -EINVAL; | |
3ff50b79 | 70 | } |
1da177e4 LT |
71 | |
72 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | |
73 | return 0; | |
74 | } | |
75 | ||
1a6509d9 HX |
76 | static int verify_aead(struct nlattr **attrs) |
77 | { | |
78 | struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; | |
79 | struct xfrm_algo_aead *algp; | |
80 | ||
81 | if (!rt) | |
82 | return 0; | |
83 | ||
84 | algp = nla_data(rt); | |
85 | if (nla_len(rt) < aead_len(algp)) | |
86 | return -EINVAL; | |
87 | ||
88 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | |
89 | return 0; | |
90 | } | |
91 | ||
5424f32e | 92 | static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, |
eb2971b6 MN |
93 | xfrm_address_t **addrp) |
94 | { | |
5424f32e | 95 | struct nlattr *rt = attrs[type]; |
eb2971b6 | 96 | |
cf5cb79f | 97 | if (rt && addrp) |
5424f32e | 98 | *addrp = nla_data(rt); |
eb2971b6 | 99 | } |
df71837d | 100 | |
5424f32e | 101 | static inline int verify_sec_ctx_len(struct nlattr **attrs) |
df71837d | 102 | { |
5424f32e | 103 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
df71837d | 104 | struct xfrm_user_sec_ctx *uctx; |
df71837d TJ |
105 | |
106 | if (!rt) | |
107 | return 0; | |
108 | ||
5424f32e | 109 | uctx = nla_data(rt); |
cf5cb79f | 110 | if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) |
df71837d TJ |
111 | return -EINVAL; |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | ||
1da177e4 | 117 | static int verify_newsa_info(struct xfrm_usersa_info *p, |
5424f32e | 118 | struct nlattr **attrs) |
1da177e4 LT |
119 | { |
120 | int err; | |
121 | ||
122 | err = -EINVAL; | |
123 | switch (p->family) { | |
124 | case AF_INET: | |
125 | break; | |
126 | ||
127 | case AF_INET6: | |
128 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
129 | break; | |
130 | #else | |
131 | err = -EAFNOSUPPORT; | |
132 | goto out; | |
133 | #endif | |
134 | ||
135 | default: | |
136 | goto out; | |
3ff50b79 | 137 | } |
1da177e4 LT |
138 | |
139 | err = -EINVAL; | |
140 | switch (p->id.proto) { | |
141 | case IPPROTO_AH: | |
35a7aa08 | 142 | if (!attrs[XFRMA_ALG_AUTH] || |
1a6509d9 | 143 | attrs[XFRMA_ALG_AEAD] || |
35a7aa08 TG |
144 | attrs[XFRMA_ALG_CRYPT] || |
145 | attrs[XFRMA_ALG_COMP]) | |
1da177e4 LT |
146 | goto out; |
147 | break; | |
148 | ||
149 | case IPPROTO_ESP: | |
1a6509d9 HX |
150 | if (attrs[XFRMA_ALG_COMP]) |
151 | goto out; | |
152 | if (!attrs[XFRMA_ALG_AUTH] && | |
153 | !attrs[XFRMA_ALG_CRYPT] && | |
154 | !attrs[XFRMA_ALG_AEAD]) | |
155 | goto out; | |
156 | if ((attrs[XFRMA_ALG_AUTH] || | |
157 | attrs[XFRMA_ALG_CRYPT]) && | |
158 | attrs[XFRMA_ALG_AEAD]) | |
1da177e4 LT |
159 | goto out; |
160 | break; | |
161 | ||
162 | case IPPROTO_COMP: | |
35a7aa08 | 163 | if (!attrs[XFRMA_ALG_COMP] || |
1a6509d9 | 164 | attrs[XFRMA_ALG_AEAD] || |
35a7aa08 TG |
165 | attrs[XFRMA_ALG_AUTH] || |
166 | attrs[XFRMA_ALG_CRYPT]) | |
1da177e4 LT |
167 | goto out; |
168 | break; | |
169 | ||
e23c7194 MN |
170 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
171 | case IPPROTO_DSTOPTS: | |
172 | case IPPROTO_ROUTING: | |
35a7aa08 TG |
173 | if (attrs[XFRMA_ALG_COMP] || |
174 | attrs[XFRMA_ALG_AUTH] || | |
1a6509d9 | 175 | attrs[XFRMA_ALG_AEAD] || |
35a7aa08 TG |
176 | attrs[XFRMA_ALG_CRYPT] || |
177 | attrs[XFRMA_ENCAP] || | |
178 | attrs[XFRMA_SEC_CTX] || | |
179 | !attrs[XFRMA_COADDR]) | |
e23c7194 MN |
180 | goto out; |
181 | break; | |
182 | #endif | |
183 | ||
1da177e4 LT |
184 | default: |
185 | goto out; | |
3ff50b79 | 186 | } |
1da177e4 | 187 | |
1a6509d9 HX |
188 | if ((err = verify_aead(attrs))) |
189 | goto out; | |
35a7aa08 | 190 | if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) |
1da177e4 | 191 | goto out; |
35a7aa08 | 192 | if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) |
1da177e4 | 193 | goto out; |
35a7aa08 | 194 | if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) |
1da177e4 | 195 | goto out; |
35a7aa08 | 196 | if ((err = verify_sec_ctx_len(attrs))) |
df71837d | 197 | goto out; |
1da177e4 LT |
198 | |
199 | err = -EINVAL; | |
200 | switch (p->mode) { | |
7e49e6de MN |
201 | case XFRM_MODE_TRANSPORT: |
202 | case XFRM_MODE_TUNNEL: | |
060f02a3 | 203 | case XFRM_MODE_ROUTEOPTIMIZATION: |
0a69452c | 204 | case XFRM_MODE_BEET: |
1da177e4 LT |
205 | break; |
206 | ||
207 | default: | |
208 | goto out; | |
3ff50b79 | 209 | } |
1da177e4 LT |
210 | |
211 | err = 0; | |
212 | ||
213 | out: | |
214 | return err; | |
215 | } | |
216 | ||
217 | static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, | |
218 | struct xfrm_algo_desc *(*get_byname)(char *, int), | |
5424f32e | 219 | struct nlattr *rta) |
1da177e4 | 220 | { |
1da177e4 LT |
221 | struct xfrm_algo *p, *ualg; |
222 | struct xfrm_algo_desc *algo; | |
223 | ||
224 | if (!rta) | |
225 | return 0; | |
226 | ||
5424f32e | 227 | ualg = nla_data(rta); |
1da177e4 LT |
228 | |
229 | algo = get_byname(ualg->alg_name, 1); | |
230 | if (!algo) | |
231 | return -ENOSYS; | |
232 | *props = algo->desc.sadb_alg_id; | |
233 | ||
0f99be0d | 234 | p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); |
1da177e4 LT |
235 | if (!p) |
236 | return -ENOMEM; | |
237 | ||
04ff1260 | 238 | strcpy(p->alg_name, algo->name); |
1da177e4 LT |
239 | *algpp = p; |
240 | return 0; | |
241 | } | |
242 | ||
1a6509d9 HX |
243 | static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props, |
244 | struct nlattr *rta) | |
245 | { | |
246 | struct xfrm_algo_aead *p, *ualg; | |
247 | struct xfrm_algo_desc *algo; | |
248 | ||
249 | if (!rta) | |
250 | return 0; | |
251 | ||
252 | ualg = nla_data(rta); | |
253 | ||
254 | algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); | |
255 | if (!algo) | |
256 | return -ENOSYS; | |
257 | *props = algo->desc.sadb_alg_id; | |
258 | ||
259 | p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); | |
260 | if (!p) | |
261 | return -ENOMEM; | |
262 | ||
263 | strcpy(p->alg_name, algo->name); | |
264 | *algpp = p; | |
265 | return 0; | |
266 | } | |
267 | ||
661697f7 | 268 | static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) |
df71837d | 269 | { |
df71837d TJ |
270 | int len = 0; |
271 | ||
272 | if (xfrm_ctx) { | |
273 | len += sizeof(struct xfrm_user_sec_ctx); | |
274 | len += xfrm_ctx->ctx_len; | |
275 | } | |
276 | return len; | |
277 | } | |
278 | ||
1da177e4 LT |
279 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
280 | { | |
281 | memcpy(&x->id, &p->id, sizeof(x->id)); | |
282 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); | |
283 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); | |
284 | x->props.mode = p->mode; | |
285 | x->props.replay_window = p->replay_window; | |
286 | x->props.reqid = p->reqid; | |
287 | x->props.family = p->family; | |
54489c14 | 288 | memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); |
1da177e4 | 289 | x->props.flags = p->flags; |
196b0036 | 290 | |
bcf0dda8 | 291 | if (!x->sel.family) |
196b0036 | 292 | x->sel.family = p->family; |
df9dcb45 | 293 | |
1da177e4 LT |
294 | } |
295 | ||
d51d081d JHS |
296 | /* |
297 | * someday when pfkey also has support, we could have the code | |
298 | * somehow made shareable and move it to xfrm_state.c - JHS | |
299 | * | |
300 | */ | |
5424f32e | 301 | static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) |
d51d081d | 302 | { |
5424f32e TG |
303 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
304 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | |
305 | struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; | |
306 | struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; | |
d51d081d JHS |
307 | |
308 | if (rp) { | |
309 | struct xfrm_replay_state *replay; | |
5424f32e | 310 | replay = nla_data(rp); |
d51d081d JHS |
311 | memcpy(&x->replay, replay, sizeof(*replay)); |
312 | memcpy(&x->preplay, replay, sizeof(*replay)); | |
313 | } | |
314 | ||
315 | if (lt) { | |
316 | struct xfrm_lifetime_cur *ltime; | |
5424f32e | 317 | ltime = nla_data(lt); |
d51d081d JHS |
318 | x->curlft.bytes = ltime->bytes; |
319 | x->curlft.packets = ltime->packets; | |
320 | x->curlft.add_time = ltime->add_time; | |
321 | x->curlft.use_time = ltime->use_time; | |
322 | } | |
323 | ||
cf5cb79f | 324 | if (et) |
5424f32e | 325 | x->replay_maxage = nla_get_u32(et); |
d51d081d | 326 | |
cf5cb79f | 327 | if (rt) |
5424f32e | 328 | x->replay_maxdiff = nla_get_u32(rt); |
d51d081d JHS |
329 | } |
330 | ||
1da177e4 | 331 | static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p, |
5424f32e | 332 | struct nlattr **attrs, |
1da177e4 LT |
333 | int *errp) |
334 | { | |
335 | struct xfrm_state *x = xfrm_state_alloc(); | |
336 | int err = -ENOMEM; | |
337 | ||
338 | if (!x) | |
339 | goto error_no_put; | |
340 | ||
341 | copy_from_user_state(x, p); | |
342 | ||
1a6509d9 HX |
343 | if ((err = attach_aead(&x->aead, &x->props.ealgo, |
344 | attrs[XFRMA_ALG_AEAD]))) | |
345 | goto error; | |
1da177e4 LT |
346 | if ((err = attach_one_algo(&x->aalg, &x->props.aalgo, |
347 | xfrm_aalg_get_byname, | |
35a7aa08 | 348 | attrs[XFRMA_ALG_AUTH]))) |
1da177e4 LT |
349 | goto error; |
350 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, | |
351 | xfrm_ealg_get_byname, | |
35a7aa08 | 352 | attrs[XFRMA_ALG_CRYPT]))) |
1da177e4 LT |
353 | goto error; |
354 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, | |
355 | xfrm_calg_get_byname, | |
35a7aa08 | 356 | attrs[XFRMA_ALG_COMP]))) |
1da177e4 | 357 | goto error; |
fd21150a TG |
358 | |
359 | if (attrs[XFRMA_ENCAP]) { | |
360 | x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), | |
361 | sizeof(*x->encap), GFP_KERNEL); | |
362 | if (x->encap == NULL) | |
363 | goto error; | |
364 | } | |
365 | ||
366 | if (attrs[XFRMA_COADDR]) { | |
367 | x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), | |
368 | sizeof(*x->coaddr), GFP_KERNEL); | |
369 | if (x->coaddr == NULL) | |
370 | goto error; | |
371 | } | |
372 | ||
72cb6962 | 373 | err = xfrm_init_state(x); |
1da177e4 LT |
374 | if (err) |
375 | goto error; | |
376 | ||
fd21150a TG |
377 | if (attrs[XFRMA_SEC_CTX] && |
378 | security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) | |
df71837d TJ |
379 | goto error; |
380 | ||
1da177e4 | 381 | x->km.seq = p->seq; |
d51d081d JHS |
382 | x->replay_maxdiff = sysctl_xfrm_aevent_rseqth; |
383 | /* sysctl_xfrm_aevent_etime is in 100ms units */ | |
384 | x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M; | |
385 | x->preplay.bitmap = 0; | |
386 | x->preplay.seq = x->replay.seq+x->replay_maxdiff; | |
387 | x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; | |
388 | ||
389 | /* override default values from above */ | |
390 | ||
5424f32e | 391 | xfrm_update_ae_params(x, attrs); |
1da177e4 LT |
392 | |
393 | return x; | |
394 | ||
395 | error: | |
396 | x->km.state = XFRM_STATE_DEAD; | |
397 | xfrm_state_put(x); | |
398 | error_no_put: | |
399 | *errp = err; | |
400 | return NULL; | |
401 | } | |
402 | ||
22e70050 | 403 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 404 | struct nlattr **attrs) |
1da177e4 | 405 | { |
7b67c857 | 406 | struct xfrm_usersa_info *p = nlmsg_data(nlh); |
1da177e4 LT |
407 | struct xfrm_state *x; |
408 | int err; | |
26b15dad | 409 | struct km_event c; |
1da177e4 | 410 | |
35a7aa08 | 411 | err = verify_newsa_info(p, attrs); |
1da177e4 LT |
412 | if (err) |
413 | return err; | |
414 | ||
35a7aa08 | 415 | x = xfrm_state_construct(p, attrs, &err); |
1da177e4 LT |
416 | if (!x) |
417 | return err; | |
418 | ||
26b15dad | 419 | xfrm_state_hold(x); |
1da177e4 LT |
420 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) |
421 | err = xfrm_state_add(x); | |
422 | else | |
423 | err = xfrm_state_update(x); | |
424 | ||
ab5f5e8b JL |
425 | xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
426 | NETLINK_CB(skb).sid); | |
161a09e7 | 427 | |
1da177e4 LT |
428 | if (err < 0) { |
429 | x->km.state = XFRM_STATE_DEAD; | |
21380b81 | 430 | __xfrm_state_put(x); |
7d6dfe1f | 431 | goto out; |
1da177e4 LT |
432 | } |
433 | ||
26b15dad JHS |
434 | c.seq = nlh->nlmsg_seq; |
435 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 436 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
437 | |
438 | km_state_notify(x, &c); | |
7d6dfe1f | 439 | out: |
26b15dad | 440 | xfrm_state_put(x); |
1da177e4 LT |
441 | return err; |
442 | } | |
443 | ||
eb2971b6 | 444 | static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p, |
5424f32e | 445 | struct nlattr **attrs, |
eb2971b6 MN |
446 | int *errp) |
447 | { | |
448 | struct xfrm_state *x = NULL; | |
449 | int err; | |
450 | ||
451 | if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { | |
452 | err = -ESRCH; | |
453 | x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); | |
454 | } else { | |
455 | xfrm_address_t *saddr = NULL; | |
456 | ||
35a7aa08 | 457 | verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); |
eb2971b6 MN |
458 | if (!saddr) { |
459 | err = -EINVAL; | |
460 | goto out; | |
461 | } | |
462 | ||
9abbffee | 463 | err = -ESRCH; |
eb2971b6 MN |
464 | x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto, |
465 | p->family); | |
466 | } | |
467 | ||
468 | out: | |
469 | if (!x && errp) | |
470 | *errp = err; | |
471 | return x; | |
472 | } | |
473 | ||
22e70050 | 474 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 475 | struct nlattr **attrs) |
1da177e4 LT |
476 | { |
477 | struct xfrm_state *x; | |
eb2971b6 | 478 | int err = -ESRCH; |
26b15dad | 479 | struct km_event c; |
7b67c857 | 480 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
1da177e4 | 481 | |
35a7aa08 | 482 | x = xfrm_user_state_lookup(p, attrs, &err); |
1da177e4 | 483 | if (x == NULL) |
eb2971b6 | 484 | return err; |
1da177e4 | 485 | |
6f68dc37 | 486 | if ((err = security_xfrm_state_delete(x)) != 0) |
c8c05a8e CZ |
487 | goto out; |
488 | ||
1da177e4 | 489 | if (xfrm_state_kern(x)) { |
c8c05a8e CZ |
490 | err = -EPERM; |
491 | goto out; | |
1da177e4 LT |
492 | } |
493 | ||
26b15dad | 494 | err = xfrm_state_delete(x); |
161a09e7 | 495 | |
c8c05a8e CZ |
496 | if (err < 0) |
497 | goto out; | |
26b15dad JHS |
498 | |
499 | c.seq = nlh->nlmsg_seq; | |
500 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 501 | c.event = nlh->nlmsg_type; |
26b15dad | 502 | km_state_notify(x, &c); |
1da177e4 | 503 | |
c8c05a8e | 504 | out: |
ab5f5e8b JL |
505 | xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
506 | NETLINK_CB(skb).sid); | |
c8c05a8e | 507 | xfrm_state_put(x); |
26b15dad | 508 | return err; |
1da177e4 LT |
509 | } |
510 | ||
511 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) | |
512 | { | |
513 | memcpy(&p->id, &x->id, sizeof(p->id)); | |
514 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); | |
515 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); | |
516 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); | |
517 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); | |
54489c14 | 518 | memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); |
1da177e4 LT |
519 | p->mode = x->props.mode; |
520 | p->replay_window = x->props.replay_window; | |
521 | p->reqid = x->props.reqid; | |
522 | p->family = x->props.family; | |
523 | p->flags = x->props.flags; | |
524 | p->seq = x->km.seq; | |
525 | } | |
526 | ||
527 | struct xfrm_dump_info { | |
528 | struct sk_buff *in_skb; | |
529 | struct sk_buff *out_skb; | |
530 | u32 nlmsg_seq; | |
531 | u16 nlmsg_flags; | |
1da177e4 LT |
532 | }; |
533 | ||
c0144bea TG |
534 | static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) |
535 | { | |
c0144bea TG |
536 | struct xfrm_user_sec_ctx *uctx; |
537 | struct nlattr *attr; | |
68325d3b | 538 | int ctx_size = sizeof(*uctx) + s->ctx_len; |
c0144bea TG |
539 | |
540 | attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); | |
541 | if (attr == NULL) | |
542 | return -EMSGSIZE; | |
543 | ||
544 | uctx = nla_data(attr); | |
545 | uctx->exttype = XFRMA_SEC_CTX; | |
546 | uctx->len = ctx_size; | |
547 | uctx->ctx_doi = s->ctx_doi; | |
548 | uctx->ctx_alg = s->ctx_alg; | |
549 | uctx->ctx_len = s->ctx_len; | |
550 | memcpy(uctx + 1, s->ctx_str, s->ctx_len); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
68325d3b HX |
555 | /* Don't change this without updating xfrm_sa_len! */ |
556 | static int copy_to_user_state_extra(struct xfrm_state *x, | |
557 | struct xfrm_usersa_info *p, | |
558 | struct sk_buff *skb) | |
1da177e4 | 559 | { |
1da177e4 LT |
560 | copy_to_user_state(x, p); |
561 | ||
050f009e HX |
562 | if (x->coaddr) |
563 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); | |
564 | ||
565 | if (x->lastused) | |
566 | NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused); | |
050f009e | 567 | |
1a6509d9 HX |
568 | if (x->aead) |
569 | NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); | |
1da177e4 | 570 | if (x->aalg) |
0f99be0d | 571 | NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg); |
1da177e4 | 572 | if (x->ealg) |
0f99be0d | 573 | NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); |
1da177e4 | 574 | if (x->calg) |
c0144bea | 575 | NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); |
1da177e4 LT |
576 | |
577 | if (x->encap) | |
c0144bea | 578 | NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); |
1da177e4 | 579 | |
c0144bea TG |
580 | if (x->security && copy_sec_ctx(x->security, skb) < 0) |
581 | goto nla_put_failure; | |
060f02a3 | 582 | |
68325d3b HX |
583 | return 0; |
584 | ||
585 | nla_put_failure: | |
586 | return -EMSGSIZE; | |
587 | } | |
588 | ||
589 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) | |
590 | { | |
591 | struct xfrm_dump_info *sp = ptr; | |
592 | struct sk_buff *in_skb = sp->in_skb; | |
593 | struct sk_buff *skb = sp->out_skb; | |
594 | struct xfrm_usersa_info *p; | |
595 | struct nlmsghdr *nlh; | |
596 | int err; | |
597 | ||
68325d3b HX |
598 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, |
599 | XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); | |
600 | if (nlh == NULL) | |
601 | return -EMSGSIZE; | |
602 | ||
603 | p = nlmsg_data(nlh); | |
604 | ||
605 | err = copy_to_user_state_extra(x, p, skb); | |
606 | if (err) | |
607 | goto nla_put_failure; | |
608 | ||
9825069d | 609 | nlmsg_end(skb, nlh); |
1da177e4 LT |
610 | return 0; |
611 | ||
c0144bea | 612 | nla_put_failure: |
9825069d | 613 | nlmsg_cancel(skb, nlh); |
68325d3b | 614 | return err; |
1da177e4 LT |
615 | } |
616 | ||
4c563f76 TT |
617 | static int xfrm_dump_sa_done(struct netlink_callback *cb) |
618 | { | |
619 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; | |
620 | xfrm_state_walk_done(walk); | |
621 | return 0; | |
622 | } | |
623 | ||
1da177e4 LT |
624 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) |
625 | { | |
4c563f76 | 626 | struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; |
1da177e4 LT |
627 | struct xfrm_dump_info info; |
628 | ||
4c563f76 TT |
629 | BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > |
630 | sizeof(cb->args) - sizeof(cb->args[0])); | |
631 | ||
1da177e4 LT |
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; | |
4c563f76 TT |
636 | |
637 | if (!cb->args[0]) { | |
638 | cb->args[0] = 1; | |
639 | xfrm_state_walk_init(walk, 0); | |
640 | } | |
641 | ||
642 | (void) xfrm_state_walk(walk, dump_one_state, &info); | |
1da177e4 LT |
643 | |
644 | return skb->len; | |
645 | } | |
646 | ||
647 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, | |
648 | struct xfrm_state *x, u32 seq) | |
649 | { | |
650 | struct xfrm_dump_info info; | |
651 | struct sk_buff *skb; | |
652 | ||
7deb2264 | 653 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1da177e4 LT |
654 | if (!skb) |
655 | return ERR_PTR(-ENOMEM); | |
656 | ||
1da177e4 LT |
657 | info.in_skb = in_skb; |
658 | info.out_skb = skb; | |
659 | info.nlmsg_seq = seq; | |
660 | info.nlmsg_flags = 0; | |
1da177e4 LT |
661 | |
662 | if (dump_one_state(x, 0, &info)) { | |
663 | kfree_skb(skb); | |
664 | return NULL; | |
665 | } | |
666 | ||
667 | return skb; | |
668 | } | |
669 | ||
7deb2264 TG |
670 | static inline size_t xfrm_spdinfo_msgsize(void) |
671 | { | |
672 | return NLMSG_ALIGN(4) | |
673 | + nla_total_size(sizeof(struct xfrmu_spdinfo)) | |
674 | + nla_total_size(sizeof(struct xfrmu_spdhinfo)); | |
675 | } | |
676 | ||
ecfd6b18 JHS |
677 | static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags) |
678 | { | |
5a6d3416 JHS |
679 | struct xfrmk_spdinfo si; |
680 | struct xfrmu_spdinfo spc; | |
681 | struct xfrmu_spdhinfo sph; | |
ecfd6b18 JHS |
682 | struct nlmsghdr *nlh; |
683 | u32 *f; | |
684 | ||
685 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); | |
686 | if (nlh == NULL) /* shouldnt really happen ... */ | |
687 | return -EMSGSIZE; | |
688 | ||
689 | f = nlmsg_data(nlh); | |
690 | *f = flags; | |
691 | xfrm_spd_getinfo(&si); | |
5a6d3416 JHS |
692 | spc.incnt = si.incnt; |
693 | spc.outcnt = si.outcnt; | |
694 | spc.fwdcnt = si.fwdcnt; | |
695 | spc.inscnt = si.inscnt; | |
696 | spc.outscnt = si.outscnt; | |
697 | spc.fwdscnt = si.fwdscnt; | |
698 | sph.spdhcnt = si.spdhcnt; | |
699 | sph.spdhmcnt = si.spdhmcnt; | |
700 | ||
701 | NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); | |
702 | NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); | |
ecfd6b18 JHS |
703 | |
704 | return nlmsg_end(skb, nlh); | |
705 | ||
706 | nla_put_failure: | |
707 | nlmsg_cancel(skb, nlh); | |
708 | return -EMSGSIZE; | |
709 | } | |
710 | ||
711 | static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 712 | struct nlattr **attrs) |
ecfd6b18 JHS |
713 | { |
714 | struct sk_buff *r_skb; | |
7b67c857 | 715 | u32 *flags = nlmsg_data(nlh); |
ecfd6b18 JHS |
716 | u32 spid = NETLINK_CB(skb).pid; |
717 | u32 seq = nlh->nlmsg_seq; | |
ecfd6b18 | 718 | |
7deb2264 | 719 | r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); |
ecfd6b18 JHS |
720 | if (r_skb == NULL) |
721 | return -ENOMEM; | |
722 | ||
723 | if (build_spdinfo(r_skb, spid, seq, *flags) < 0) | |
724 | BUG(); | |
725 | ||
726 | return nlmsg_unicast(xfrm_nl, r_skb, spid); | |
727 | } | |
728 | ||
7deb2264 TG |
729 | static inline size_t xfrm_sadinfo_msgsize(void) |
730 | { | |
731 | return NLMSG_ALIGN(4) | |
732 | + nla_total_size(sizeof(struct xfrmu_sadhinfo)) | |
733 | + nla_total_size(4); /* XFRMA_SAD_CNT */ | |
734 | } | |
735 | ||
28d8909b JHS |
736 | static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags) |
737 | { | |
af11e316 JHS |
738 | struct xfrmk_sadinfo si; |
739 | struct xfrmu_sadhinfo sh; | |
28d8909b JHS |
740 | struct nlmsghdr *nlh; |
741 | u32 *f; | |
742 | ||
743 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); | |
744 | if (nlh == NULL) /* shouldnt really happen ... */ | |
745 | return -EMSGSIZE; | |
746 | ||
747 | f = nlmsg_data(nlh); | |
748 | *f = flags; | |
749 | xfrm_sad_getinfo(&si); | |
750 | ||
af11e316 JHS |
751 | sh.sadhmcnt = si.sadhmcnt; |
752 | sh.sadhcnt = si.sadhcnt; | |
753 | ||
754 | NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt); | |
755 | NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); | |
28d8909b JHS |
756 | |
757 | return nlmsg_end(skb, nlh); | |
758 | ||
759 | nla_put_failure: | |
760 | nlmsg_cancel(skb, nlh); | |
761 | return -EMSGSIZE; | |
762 | } | |
763 | ||
764 | static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 765 | struct nlattr **attrs) |
28d8909b JHS |
766 | { |
767 | struct sk_buff *r_skb; | |
7b67c857 | 768 | u32 *flags = nlmsg_data(nlh); |
28d8909b JHS |
769 | u32 spid = NETLINK_CB(skb).pid; |
770 | u32 seq = nlh->nlmsg_seq; | |
28d8909b | 771 | |
7deb2264 | 772 | r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); |
28d8909b JHS |
773 | if (r_skb == NULL) |
774 | return -ENOMEM; | |
775 | ||
776 | if (build_sadinfo(r_skb, spid, seq, *flags) < 0) | |
777 | BUG(); | |
778 | ||
779 | return nlmsg_unicast(xfrm_nl, r_skb, spid); | |
780 | } | |
781 | ||
22e70050 | 782 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 783 | struct nlattr **attrs) |
1da177e4 | 784 | { |
7b67c857 | 785 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
1da177e4 LT |
786 | struct xfrm_state *x; |
787 | struct sk_buff *resp_skb; | |
eb2971b6 | 788 | int err = -ESRCH; |
1da177e4 | 789 | |
35a7aa08 | 790 | x = xfrm_user_state_lookup(p, attrs, &err); |
1da177e4 LT |
791 | if (x == NULL) |
792 | goto out_noput; | |
793 | ||
794 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | |
795 | if (IS_ERR(resp_skb)) { | |
796 | err = PTR_ERR(resp_skb); | |
797 | } else { | |
082a1ad5 | 798 | err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid); |
1da177e4 LT |
799 | } |
800 | xfrm_state_put(x); | |
801 | out_noput: | |
802 | return err; | |
803 | } | |
804 | ||
805 | static int verify_userspi_info(struct xfrm_userspi_info *p) | |
806 | { | |
807 | switch (p->info.id.proto) { | |
808 | case IPPROTO_AH: | |
809 | case IPPROTO_ESP: | |
810 | break; | |
811 | ||
812 | case IPPROTO_COMP: | |
813 | /* IPCOMP spi is 16-bits. */ | |
814 | if (p->max >= 0x10000) | |
815 | return -EINVAL; | |
816 | break; | |
817 | ||
818 | default: | |
819 | return -EINVAL; | |
3ff50b79 | 820 | } |
1da177e4 LT |
821 | |
822 | if (p->min > p->max) | |
823 | return -EINVAL; | |
824 | ||
825 | return 0; | |
826 | } | |
827 | ||
22e70050 | 828 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 829 | struct nlattr **attrs) |
1da177e4 LT |
830 | { |
831 | struct xfrm_state *x; | |
832 | struct xfrm_userspi_info *p; | |
833 | struct sk_buff *resp_skb; | |
834 | xfrm_address_t *daddr; | |
835 | int family; | |
836 | int err; | |
837 | ||
7b67c857 | 838 | p = nlmsg_data(nlh); |
1da177e4 LT |
839 | err = verify_userspi_info(p); |
840 | if (err) | |
841 | goto out_noput; | |
842 | ||
843 | family = p->info.family; | |
844 | daddr = &p->info.id.daddr; | |
845 | ||
846 | x = NULL; | |
847 | if (p->info.seq) { | |
848 | x = xfrm_find_acq_byseq(p->info.seq); | |
849 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { | |
850 | xfrm_state_put(x); | |
851 | x = NULL; | |
852 | } | |
853 | } | |
854 | ||
855 | if (!x) | |
856 | x = xfrm_find_acq(p->info.mode, p->info.reqid, | |
857 | p->info.id.proto, daddr, | |
858 | &p->info.saddr, 1, | |
859 | family); | |
860 | err = -ENOENT; | |
861 | if (x == NULL) | |
862 | goto out_noput; | |
863 | ||
658b219e HX |
864 | err = xfrm_alloc_spi(x, p->min, p->max); |
865 | if (err) | |
866 | goto out; | |
1da177e4 | 867 | |
658b219e | 868 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
1da177e4 LT |
869 | if (IS_ERR(resp_skb)) { |
870 | err = PTR_ERR(resp_skb); | |
871 | goto out; | |
872 | } | |
873 | ||
082a1ad5 | 874 | err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid); |
1da177e4 LT |
875 | |
876 | out: | |
877 | xfrm_state_put(x); | |
878 | out_noput: | |
879 | return err; | |
880 | } | |
881 | ||
b798a9ed | 882 | static int verify_policy_dir(u8 dir) |
1da177e4 LT |
883 | { |
884 | switch (dir) { | |
885 | case XFRM_POLICY_IN: | |
886 | case XFRM_POLICY_OUT: | |
887 | case XFRM_POLICY_FWD: | |
888 | break; | |
889 | ||
890 | default: | |
891 | return -EINVAL; | |
3ff50b79 | 892 | } |
1da177e4 LT |
893 | |
894 | return 0; | |
895 | } | |
896 | ||
b798a9ed | 897 | static int verify_policy_type(u8 type) |
f7b6983f MN |
898 | { |
899 | switch (type) { | |
900 | case XFRM_POLICY_TYPE_MAIN: | |
901 | #ifdef CONFIG_XFRM_SUB_POLICY | |
902 | case XFRM_POLICY_TYPE_SUB: | |
903 | #endif | |
904 | break; | |
905 | ||
906 | default: | |
907 | return -EINVAL; | |
3ff50b79 | 908 | } |
f7b6983f MN |
909 | |
910 | return 0; | |
911 | } | |
912 | ||
1da177e4 LT |
913 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) |
914 | { | |
915 | switch (p->share) { | |
916 | case XFRM_SHARE_ANY: | |
917 | case XFRM_SHARE_SESSION: | |
918 | case XFRM_SHARE_USER: | |
919 | case XFRM_SHARE_UNIQUE: | |
920 | break; | |
921 | ||
922 | default: | |
923 | return -EINVAL; | |
3ff50b79 | 924 | } |
1da177e4 LT |
925 | |
926 | switch (p->action) { | |
927 | case XFRM_POLICY_ALLOW: | |
928 | case XFRM_POLICY_BLOCK: | |
929 | break; | |
930 | ||
931 | default: | |
932 | return -EINVAL; | |
3ff50b79 | 933 | } |
1da177e4 LT |
934 | |
935 | switch (p->sel.family) { | |
936 | case AF_INET: | |
937 | break; | |
938 | ||
939 | case AF_INET6: | |
940 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
941 | break; | |
942 | #else | |
943 | return -EAFNOSUPPORT; | |
944 | #endif | |
945 | ||
946 | default: | |
947 | return -EINVAL; | |
3ff50b79 | 948 | } |
1da177e4 LT |
949 | |
950 | return verify_policy_dir(p->dir); | |
951 | } | |
952 | ||
5424f32e | 953 | static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) |
df71837d | 954 | { |
5424f32e | 955 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
df71837d TJ |
956 | struct xfrm_user_sec_ctx *uctx; |
957 | ||
958 | if (!rt) | |
959 | return 0; | |
960 | ||
5424f32e | 961 | uctx = nla_data(rt); |
03e1ad7b | 962 | return security_xfrm_policy_alloc(&pol->security, uctx); |
df71837d TJ |
963 | } |
964 | ||
1da177e4 LT |
965 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, |
966 | int nr) | |
967 | { | |
968 | int i; | |
969 | ||
970 | xp->xfrm_nr = nr; | |
971 | for (i = 0; i < nr; i++, ut++) { | |
972 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
973 | ||
974 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); | |
975 | memcpy(&t->saddr, &ut->saddr, | |
976 | sizeof(xfrm_address_t)); | |
977 | t->reqid = ut->reqid; | |
978 | t->mode = ut->mode; | |
979 | t->share = ut->share; | |
980 | t->optional = ut->optional; | |
981 | t->aalgos = ut->aalgos; | |
982 | t->ealgos = ut->ealgos; | |
983 | t->calgos = ut->calgos; | |
8511d01d | 984 | t->encap_family = ut->family; |
1da177e4 LT |
985 | } |
986 | } | |
987 | ||
b4ad86bf DM |
988 | static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) |
989 | { | |
990 | int i; | |
991 | ||
992 | if (nr > XFRM_MAX_DEPTH) | |
993 | return -EINVAL; | |
994 | ||
995 | for (i = 0; i < nr; i++) { | |
996 | /* We never validated the ut->family value, so many | |
997 | * applications simply leave it at zero. The check was | |
998 | * never made and ut->family was ignored because all | |
999 | * templates could be assumed to have the same family as | |
1000 | * the policy itself. Now that we will have ipv4-in-ipv6 | |
1001 | * and ipv6-in-ipv4 tunnels, this is no longer true. | |
1002 | */ | |
1003 | if (!ut[i].family) | |
1004 | ut[i].family = family; | |
1005 | ||
1006 | switch (ut[i].family) { | |
1007 | case AF_INET: | |
1008 | break; | |
1009 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
1010 | case AF_INET6: | |
1011 | break; | |
1012 | #endif | |
1013 | default: | |
1014 | return -EINVAL; | |
3ff50b79 | 1015 | } |
b4ad86bf DM |
1016 | } |
1017 | ||
1018 | return 0; | |
1019 | } | |
1020 | ||
5424f32e | 1021 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) |
1da177e4 | 1022 | { |
5424f32e | 1023 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
1da177e4 LT |
1024 | |
1025 | if (!rt) { | |
1026 | pol->xfrm_nr = 0; | |
1027 | } else { | |
5424f32e TG |
1028 | struct xfrm_user_tmpl *utmpl = nla_data(rt); |
1029 | int nr = nla_len(rt) / sizeof(*utmpl); | |
b4ad86bf | 1030 | int err; |
1da177e4 | 1031 | |
b4ad86bf DM |
1032 | err = validate_tmpl(nr, utmpl, pol->family); |
1033 | if (err) | |
1034 | return err; | |
1da177e4 | 1035 | |
5424f32e | 1036 | copy_templates(pol, utmpl, nr); |
1da177e4 LT |
1037 | } |
1038 | return 0; | |
1039 | } | |
1040 | ||
5424f32e | 1041 | static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) |
f7b6983f | 1042 | { |
5424f32e | 1043 | struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; |
f7b6983f | 1044 | struct xfrm_userpolicy_type *upt; |
b798a9ed | 1045 | u8 type = XFRM_POLICY_TYPE_MAIN; |
f7b6983f MN |
1046 | int err; |
1047 | ||
1048 | if (rt) { | |
5424f32e | 1049 | upt = nla_data(rt); |
f7b6983f MN |
1050 | type = upt->type; |
1051 | } | |
1052 | ||
1053 | err = verify_policy_type(type); | |
1054 | if (err) | |
1055 | return err; | |
1056 | ||
1057 | *tp = type; | |
1058 | return 0; | |
1059 | } | |
1060 | ||
1da177e4 LT |
1061 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) |
1062 | { | |
1063 | xp->priority = p->priority; | |
1064 | xp->index = p->index; | |
1065 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); | |
1066 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); | |
1067 | xp->action = p->action; | |
1068 | xp->flags = p->flags; | |
1069 | xp->family = p->sel.family; | |
1070 | /* XXX xp->share = p->share; */ | |
1071 | } | |
1072 | ||
1073 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) | |
1074 | { | |
1075 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); | |
1076 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); | |
1077 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); | |
1078 | p->priority = xp->priority; | |
1079 | p->index = xp->index; | |
1080 | p->sel.family = xp->family; | |
1081 | p->dir = dir; | |
1082 | p->action = xp->action; | |
1083 | p->flags = xp->flags; | |
1084 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ | |
1085 | } | |
1086 | ||
5424f32e | 1087 | static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) |
1da177e4 LT |
1088 | { |
1089 | struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL); | |
1090 | int err; | |
1091 | ||
1092 | if (!xp) { | |
1093 | *errp = -ENOMEM; | |
1094 | return NULL; | |
1095 | } | |
1096 | ||
1097 | copy_from_user_policy(xp, p); | |
df71837d | 1098 | |
35a7aa08 | 1099 | err = copy_from_user_policy_type(&xp->type, attrs); |
f7b6983f MN |
1100 | if (err) |
1101 | goto error; | |
1102 | ||
35a7aa08 TG |
1103 | if (!(err = copy_from_user_tmpl(xp, attrs))) |
1104 | err = copy_from_user_sec_ctx(xp, attrs); | |
f7b6983f MN |
1105 | if (err) |
1106 | goto error; | |
1da177e4 LT |
1107 | |
1108 | return xp; | |
f7b6983f MN |
1109 | error: |
1110 | *errp = err; | |
073a3719 | 1111 | xp->dead = 1; |
64c31b3f | 1112 | xfrm_policy_destroy(xp); |
f7b6983f | 1113 | return NULL; |
1da177e4 LT |
1114 | } |
1115 | ||
22e70050 | 1116 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1117 | struct nlattr **attrs) |
1da177e4 | 1118 | { |
7b67c857 | 1119 | struct xfrm_userpolicy_info *p = nlmsg_data(nlh); |
1da177e4 | 1120 | struct xfrm_policy *xp; |
26b15dad | 1121 | struct km_event c; |
1da177e4 LT |
1122 | int err; |
1123 | int excl; | |
1124 | ||
1125 | err = verify_newpolicy_info(p); | |
df71837d TJ |
1126 | if (err) |
1127 | return err; | |
35a7aa08 | 1128 | err = verify_sec_ctx_len(attrs); |
1da177e4 LT |
1129 | if (err) |
1130 | return err; | |
1131 | ||
35a7aa08 | 1132 | xp = xfrm_policy_construct(p, attrs, &err); |
1da177e4 LT |
1133 | if (!xp) |
1134 | return err; | |
1135 | ||
26b15dad JHS |
1136 | /* shouldnt excl be based on nlh flags?? |
1137 | * Aha! this is anti-netlink really i.e more pfkey derived | |
1138 | * in netlink excl is a flag and you wouldnt need | |
1139 | * a type XFRM_MSG_UPDPOLICY - JHS */ | |
1da177e4 LT |
1140 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; |
1141 | err = xfrm_policy_insert(p->dir, xp, excl); | |
ab5f5e8b JL |
1142 | xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
1143 | NETLINK_CB(skb).sid); | |
161a09e7 | 1144 | |
1da177e4 | 1145 | if (err) { |
03e1ad7b | 1146 | security_xfrm_policy_free(xp->security); |
1da177e4 LT |
1147 | kfree(xp); |
1148 | return err; | |
1149 | } | |
1150 | ||
f60f6b8f | 1151 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1152 | c.seq = nlh->nlmsg_seq; |
1153 | c.pid = nlh->nlmsg_pid; | |
1154 | km_policy_notify(xp, p->dir, &c); | |
1155 | ||
1da177e4 LT |
1156 | xfrm_pol_put(xp); |
1157 | ||
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) | |
1162 | { | |
1163 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; | |
1164 | int i; | |
1165 | ||
1166 | if (xp->xfrm_nr == 0) | |
1167 | return 0; | |
1168 | ||
1169 | for (i = 0; i < xp->xfrm_nr; i++) { | |
1170 | struct xfrm_user_tmpl *up = &vec[i]; | |
1171 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; | |
1172 | ||
1173 | memcpy(&up->id, &kp->id, sizeof(up->id)); | |
8511d01d | 1174 | up->family = kp->encap_family; |
1da177e4 LT |
1175 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); |
1176 | up->reqid = kp->reqid; | |
1177 | up->mode = kp->mode; | |
1178 | up->share = kp->share; | |
1179 | up->optional = kp->optional; | |
1180 | up->aalgos = kp->aalgos; | |
1181 | up->ealgos = kp->ealgos; | |
1182 | up->calgos = kp->calgos; | |
1183 | } | |
df71837d | 1184 | |
c0144bea TG |
1185 | return nla_put(skb, XFRMA_TMPL, |
1186 | sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); | |
0d681623 SH |
1187 | } |
1188 | ||
1189 | static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) | |
1190 | { | |
1191 | if (x->security) { | |
1192 | return copy_sec_ctx(x->security, skb); | |
df71837d TJ |
1193 | } |
1194 | return 0; | |
0d681623 | 1195 | } |
df71837d | 1196 | |
0d681623 SH |
1197 | static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) |
1198 | { | |
1199 | if (xp->security) { | |
1200 | return copy_sec_ctx(xp->security, skb); | |
1201 | } | |
1202 | return 0; | |
df71837d | 1203 | } |
cfbfd45a TG |
1204 | static inline size_t userpolicy_type_attrsize(void) |
1205 | { | |
1206 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1207 | return nla_total_size(sizeof(struct xfrm_userpolicy_type)); | |
1208 | #else | |
1209 | return 0; | |
1210 | #endif | |
1211 | } | |
df71837d | 1212 | |
f7b6983f | 1213 | #ifdef CONFIG_XFRM_SUB_POLICY |
b798a9ed | 1214 | static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
f7b6983f | 1215 | { |
c0144bea TG |
1216 | struct xfrm_userpolicy_type upt = { |
1217 | .type = type, | |
1218 | }; | |
f7b6983f | 1219 | |
c0144bea | 1220 | return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); |
f7b6983f MN |
1221 | } |
1222 | ||
1223 | #else | |
b798a9ed | 1224 | static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
f7b6983f MN |
1225 | { |
1226 | return 0; | |
1227 | } | |
1228 | #endif | |
1229 | ||
1da177e4 LT |
1230 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) |
1231 | { | |
1232 | struct xfrm_dump_info *sp = ptr; | |
1233 | struct xfrm_userpolicy_info *p; | |
1234 | struct sk_buff *in_skb = sp->in_skb; | |
1235 | struct sk_buff *skb = sp->out_skb; | |
1236 | struct nlmsghdr *nlh; | |
1da177e4 | 1237 | |
79b8b7f4 TG |
1238 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, |
1239 | XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); | |
1240 | if (nlh == NULL) | |
1241 | return -EMSGSIZE; | |
1da177e4 | 1242 | |
7b67c857 | 1243 | p = nlmsg_data(nlh); |
1da177e4 LT |
1244 | copy_to_user_policy(xp, p, dir); |
1245 | if (copy_to_user_tmpl(xp, skb) < 0) | |
1246 | goto nlmsg_failure; | |
df71837d TJ |
1247 | if (copy_to_user_sec_ctx(xp, skb)) |
1248 | goto nlmsg_failure; | |
1459bb36 | 1249 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 1250 | goto nlmsg_failure; |
1da177e4 | 1251 | |
9825069d | 1252 | nlmsg_end(skb, nlh); |
1da177e4 LT |
1253 | return 0; |
1254 | ||
1255 | nlmsg_failure: | |
9825069d TG |
1256 | nlmsg_cancel(skb, nlh); |
1257 | return -EMSGSIZE; | |
1da177e4 LT |
1258 | } |
1259 | ||
4c563f76 TT |
1260 | static int xfrm_dump_policy_done(struct netlink_callback *cb) |
1261 | { | |
1262 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; | |
1263 | ||
1264 | xfrm_policy_walk_done(walk); | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1da177e4 LT |
1268 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) |
1269 | { | |
4c563f76 | 1270 | struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; |
1da177e4 LT |
1271 | struct xfrm_dump_info info; |
1272 | ||
4c563f76 TT |
1273 | BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > |
1274 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1275 | ||
1da177e4 LT |
1276 | info.in_skb = cb->skb; |
1277 | info.out_skb = skb; | |
1278 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | |
1279 | info.nlmsg_flags = NLM_F_MULTI; | |
4c563f76 TT |
1280 | |
1281 | if (!cb->args[0]) { | |
1282 | cb->args[0] = 1; | |
1283 | xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); | |
1284 | } | |
1285 | ||
1286 | (void) xfrm_policy_walk(walk, dump_one_policy, &info); | |
1da177e4 LT |
1287 | |
1288 | return skb->len; | |
1289 | } | |
1290 | ||
1291 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, | |
1292 | struct xfrm_policy *xp, | |
1293 | int dir, u32 seq) | |
1294 | { | |
1295 | struct xfrm_dump_info info; | |
1296 | struct sk_buff *skb; | |
1297 | ||
7deb2264 | 1298 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1da177e4 LT |
1299 | if (!skb) |
1300 | return ERR_PTR(-ENOMEM); | |
1301 | ||
1da177e4 LT |
1302 | info.in_skb = in_skb; |
1303 | info.out_skb = skb; | |
1304 | info.nlmsg_seq = seq; | |
1305 | info.nlmsg_flags = 0; | |
1da177e4 LT |
1306 | |
1307 | if (dump_one_policy(xp, dir, 0, &info) < 0) { | |
1308 | kfree_skb(skb); | |
1309 | return NULL; | |
1310 | } | |
1311 | ||
1312 | return skb; | |
1313 | } | |
1314 | ||
22e70050 | 1315 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1316 | struct nlattr **attrs) |
1da177e4 LT |
1317 | { |
1318 | struct xfrm_policy *xp; | |
1319 | struct xfrm_userpolicy_id *p; | |
b798a9ed | 1320 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 | 1321 | int err; |
26b15dad | 1322 | struct km_event c; |
1da177e4 LT |
1323 | int delete; |
1324 | ||
7b67c857 | 1325 | p = nlmsg_data(nlh); |
1da177e4 LT |
1326 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; |
1327 | ||
35a7aa08 | 1328 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1329 | if (err) |
1330 | return err; | |
1331 | ||
1da177e4 LT |
1332 | err = verify_policy_dir(p->dir); |
1333 | if (err) | |
1334 | return err; | |
1335 | ||
1336 | if (p->index) | |
ef41aaa0 | 1337 | xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err); |
df71837d | 1338 | else { |
5424f32e | 1339 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
03e1ad7b | 1340 | struct xfrm_sec_ctx *ctx; |
df71837d | 1341 | |
35a7aa08 | 1342 | err = verify_sec_ctx_len(attrs); |
df71837d TJ |
1343 | if (err) |
1344 | return err; | |
1345 | ||
2c8dd116 | 1346 | ctx = NULL; |
df71837d | 1347 | if (rt) { |
5424f32e | 1348 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
df71837d | 1349 | |
03e1ad7b PM |
1350 | err = security_xfrm_policy_alloc(&ctx, uctx); |
1351 | if (err) | |
df71837d | 1352 | return err; |
2c8dd116 | 1353 | } |
03e1ad7b | 1354 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, |
ef41aaa0 | 1355 | delete, &err); |
03e1ad7b | 1356 | security_xfrm_policy_free(ctx); |
df71837d | 1357 | } |
1da177e4 LT |
1358 | if (xp == NULL) |
1359 | return -ENOENT; | |
1360 | ||
1361 | if (!delete) { | |
1362 | struct sk_buff *resp_skb; | |
1363 | ||
1364 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); | |
1365 | if (IS_ERR(resp_skb)) { | |
1366 | err = PTR_ERR(resp_skb); | |
1367 | } else { | |
082a1ad5 TG |
1368 | err = nlmsg_unicast(xfrm_nl, resp_skb, |
1369 | NETLINK_CB(skb).pid); | |
1da177e4 | 1370 | } |
26b15dad | 1371 | } else { |
ab5f5e8b JL |
1372 | xfrm_audit_policy_delete(xp, err ? 0 : 1, |
1373 | NETLINK_CB(skb).loginuid, | |
1374 | NETLINK_CB(skb).sid); | |
13fcfbb0 DM |
1375 | |
1376 | if (err != 0) | |
c8c05a8e | 1377 | goto out; |
13fcfbb0 | 1378 | |
e7443892 | 1379 | c.data.byid = p->index; |
f60f6b8f | 1380 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1381 | c.seq = nlh->nlmsg_seq; |
1382 | c.pid = nlh->nlmsg_pid; | |
1383 | km_policy_notify(xp, p->dir, &c); | |
1da177e4 LT |
1384 | } |
1385 | ||
c8c05a8e | 1386 | out: |
ef41aaa0 | 1387 | xfrm_pol_put(xp); |
1da177e4 LT |
1388 | return err; |
1389 | } | |
1390 | ||
22e70050 | 1391 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1392 | struct nlattr **attrs) |
1da177e4 | 1393 | { |
26b15dad | 1394 | struct km_event c; |
7b67c857 | 1395 | struct xfrm_usersa_flush *p = nlmsg_data(nlh); |
161a09e7 | 1396 | struct xfrm_audit audit_info; |
4aa2e62c | 1397 | int err; |
1da177e4 | 1398 | |
161a09e7 JL |
1399 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1400 | audit_info.secid = NETLINK_CB(skb).sid; | |
4aa2e62c JL |
1401 | err = xfrm_state_flush(p->proto, &audit_info); |
1402 | if (err) | |
1403 | return err; | |
bf08867f | 1404 | c.data.proto = p->proto; |
f60f6b8f | 1405 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1406 | c.seq = nlh->nlmsg_seq; |
1407 | c.pid = nlh->nlmsg_pid; | |
1408 | km_state_notify(NULL, &c); | |
1409 | ||
1da177e4 LT |
1410 | return 0; |
1411 | } | |
1412 | ||
7deb2264 TG |
1413 | static inline size_t xfrm_aevent_msgsize(void) |
1414 | { | |
1415 | return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) | |
1416 | + nla_total_size(sizeof(struct xfrm_replay_state)) | |
1417 | + nla_total_size(sizeof(struct xfrm_lifetime_cur)) | |
1418 | + nla_total_size(4) /* XFRM_AE_RTHR */ | |
1419 | + nla_total_size(4); /* XFRM_AE_ETHR */ | |
1420 | } | |
d51d081d JHS |
1421 | |
1422 | static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) | |
1423 | { | |
1424 | struct xfrm_aevent_id *id; | |
1425 | struct nlmsghdr *nlh; | |
d51d081d | 1426 | |
79b8b7f4 TG |
1427 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); |
1428 | if (nlh == NULL) | |
1429 | return -EMSGSIZE; | |
d51d081d | 1430 | |
7b67c857 | 1431 | id = nlmsg_data(nlh); |
2b5f6dcc | 1432 | memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); |
d51d081d JHS |
1433 | id->sa_id.spi = x->id.spi; |
1434 | id->sa_id.family = x->props.family; | |
1435 | id->sa_id.proto = x->id.proto; | |
2b5f6dcc JHS |
1436 | memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); |
1437 | id->reqid = x->props.reqid; | |
d51d081d JHS |
1438 | id->flags = c->data.aevent; |
1439 | ||
c0144bea TG |
1440 | NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); |
1441 | NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); | |
d51d081d | 1442 | |
c0144bea TG |
1443 | if (id->flags & XFRM_AE_RTHR) |
1444 | NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); | |
d51d081d | 1445 | |
c0144bea TG |
1446 | if (id->flags & XFRM_AE_ETHR) |
1447 | NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH, | |
1448 | x->replay_maxage * 10 / HZ); | |
d51d081d | 1449 | |
9825069d | 1450 | return nlmsg_end(skb, nlh); |
d51d081d | 1451 | |
c0144bea | 1452 | nla_put_failure: |
9825069d TG |
1453 | nlmsg_cancel(skb, nlh); |
1454 | return -EMSGSIZE; | |
d51d081d JHS |
1455 | } |
1456 | ||
22e70050 | 1457 | static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1458 | struct nlattr **attrs) |
d51d081d JHS |
1459 | { |
1460 | struct xfrm_state *x; | |
1461 | struct sk_buff *r_skb; | |
1462 | int err; | |
1463 | struct km_event c; | |
7b67c857 | 1464 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
d51d081d JHS |
1465 | struct xfrm_usersa_id *id = &p->sa_id; |
1466 | ||
7deb2264 | 1467 | r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
d51d081d JHS |
1468 | if (r_skb == NULL) |
1469 | return -ENOMEM; | |
1470 | ||
1471 | x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family); | |
1472 | if (x == NULL) { | |
b08d5840 | 1473 | kfree_skb(r_skb); |
d51d081d JHS |
1474 | return -ESRCH; |
1475 | } | |
1476 | ||
1477 | /* | |
1478 | * XXX: is this lock really needed - none of the other | |
1479 | * gets lock (the concern is things getting updated | |
1480 | * while we are still reading) - jhs | |
1481 | */ | |
1482 | spin_lock_bh(&x->lock); | |
1483 | c.data.aevent = p->flags; | |
1484 | c.seq = nlh->nlmsg_seq; | |
1485 | c.pid = nlh->nlmsg_pid; | |
1486 | ||
1487 | if (build_aevent(r_skb, x, &c) < 0) | |
1488 | BUG(); | |
082a1ad5 | 1489 | err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid); |
d51d081d JHS |
1490 | spin_unlock_bh(&x->lock); |
1491 | xfrm_state_put(x); | |
1492 | return err; | |
1493 | } | |
1494 | ||
22e70050 | 1495 | static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1496 | struct nlattr **attrs) |
d51d081d JHS |
1497 | { |
1498 | struct xfrm_state *x; | |
1499 | struct km_event c; | |
1500 | int err = - EINVAL; | |
7b67c857 | 1501 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
5424f32e TG |
1502 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
1503 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | |
d51d081d JHS |
1504 | |
1505 | if (!lt && !rp) | |
1506 | return err; | |
1507 | ||
1508 | /* pedantic mode - thou shalt sayeth replaceth */ | |
1509 | if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) | |
1510 | return err; | |
1511 | ||
1512 | x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); | |
1513 | if (x == NULL) | |
1514 | return -ESRCH; | |
1515 | ||
1516 | if (x->km.state != XFRM_STATE_VALID) | |
1517 | goto out; | |
1518 | ||
1519 | spin_lock_bh(&x->lock); | |
35a7aa08 | 1520 | xfrm_update_ae_params(x, attrs); |
d51d081d | 1521 | spin_unlock_bh(&x->lock); |
d51d081d JHS |
1522 | |
1523 | c.event = nlh->nlmsg_type; | |
1524 | c.seq = nlh->nlmsg_seq; | |
1525 | c.pid = nlh->nlmsg_pid; | |
1526 | c.data.aevent = XFRM_AE_CU; | |
1527 | km_state_notify(x, &c); | |
1528 | err = 0; | |
1529 | out: | |
1530 | xfrm_state_put(x); | |
1531 | return err; | |
1532 | } | |
1533 | ||
22e70050 | 1534 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1535 | struct nlattr **attrs) |
1da177e4 | 1536 | { |
f7b6983f | 1537 | struct km_event c; |
b798a9ed | 1538 | u8 type = XFRM_POLICY_TYPE_MAIN; |
f7b6983f | 1539 | int err; |
161a09e7 | 1540 | struct xfrm_audit audit_info; |
f7b6983f | 1541 | |
35a7aa08 | 1542 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1543 | if (err) |
1544 | return err; | |
26b15dad | 1545 | |
161a09e7 JL |
1546 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1547 | audit_info.secid = NETLINK_CB(skb).sid; | |
4aa2e62c JL |
1548 | err = xfrm_policy_flush(type, &audit_info); |
1549 | if (err) | |
1550 | return err; | |
f7b6983f | 1551 | c.data.type = type; |
f60f6b8f | 1552 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1553 | c.seq = nlh->nlmsg_seq; |
1554 | c.pid = nlh->nlmsg_pid; | |
1555 | km_policy_notify(NULL, 0, &c); | |
1da177e4 LT |
1556 | return 0; |
1557 | } | |
1558 | ||
22e70050 | 1559 | static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1560 | struct nlattr **attrs) |
6c5c8ca7 JHS |
1561 | { |
1562 | struct xfrm_policy *xp; | |
7b67c857 | 1563 | struct xfrm_user_polexpire *up = nlmsg_data(nlh); |
6c5c8ca7 | 1564 | struct xfrm_userpolicy_info *p = &up->pol; |
b798a9ed | 1565 | u8 type = XFRM_POLICY_TYPE_MAIN; |
6c5c8ca7 JHS |
1566 | int err = -ENOENT; |
1567 | ||
35a7aa08 | 1568 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1569 | if (err) |
1570 | return err; | |
1571 | ||
6c5c8ca7 | 1572 | if (p->index) |
ef41aaa0 | 1573 | xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err); |
6c5c8ca7 | 1574 | else { |
5424f32e | 1575 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
03e1ad7b | 1576 | struct xfrm_sec_ctx *ctx; |
6c5c8ca7 | 1577 | |
35a7aa08 | 1578 | err = verify_sec_ctx_len(attrs); |
6c5c8ca7 JHS |
1579 | if (err) |
1580 | return err; | |
1581 | ||
2c8dd116 | 1582 | ctx = NULL; |
6c5c8ca7 | 1583 | if (rt) { |
5424f32e | 1584 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
6c5c8ca7 | 1585 | |
03e1ad7b PM |
1586 | err = security_xfrm_policy_alloc(&ctx, uctx); |
1587 | if (err) | |
6c5c8ca7 | 1588 | return err; |
2c8dd116 | 1589 | } |
03e1ad7b PM |
1590 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err); |
1591 | security_xfrm_policy_free(ctx); | |
6c5c8ca7 | 1592 | } |
6c5c8ca7 | 1593 | if (xp == NULL) |
ef41aaa0 | 1594 | return -ENOENT; |
03e1ad7b | 1595 | |
ef41aaa0 | 1596 | read_lock(&xp->lock); |
6c5c8ca7 JHS |
1597 | if (xp->dead) { |
1598 | read_unlock(&xp->lock); | |
1599 | goto out; | |
1600 | } | |
1601 | ||
1602 | read_unlock(&xp->lock); | |
1603 | err = 0; | |
1604 | if (up->hard) { | |
1605 | xfrm_policy_delete(xp, p->dir); | |
ab5f5e8b JL |
1606 | xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid, |
1607 | NETLINK_CB(skb).sid); | |
161a09e7 | 1608 | |
6c5c8ca7 JHS |
1609 | } else { |
1610 | // reset the timers here? | |
1611 | printk("Dont know what to do with soft policy expire\n"); | |
1612 | } | |
1613 | km_policy_expired(xp, p->dir, up->hard, current->pid); | |
1614 | ||
1615 | out: | |
1616 | xfrm_pol_put(xp); | |
1617 | return err; | |
1618 | } | |
1619 | ||
22e70050 | 1620 | static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1621 | struct nlattr **attrs) |
53bc6b4d JHS |
1622 | { |
1623 | struct xfrm_state *x; | |
1624 | int err; | |
7b67c857 | 1625 | struct xfrm_user_expire *ue = nlmsg_data(nlh); |
53bc6b4d JHS |
1626 | struct xfrm_usersa_info *p = &ue->state; |
1627 | ||
1628 | x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family); | |
53bc6b4d | 1629 | |
3a765aa5 | 1630 | err = -ENOENT; |
53bc6b4d JHS |
1631 | if (x == NULL) |
1632 | return err; | |
1633 | ||
53bc6b4d | 1634 | spin_lock_bh(&x->lock); |
3a765aa5 | 1635 | err = -EINVAL; |
53bc6b4d JHS |
1636 | if (x->km.state != XFRM_STATE_VALID) |
1637 | goto out; | |
1638 | km_state_expired(x, ue->hard, current->pid); | |
1639 | ||
161a09e7 | 1640 | if (ue->hard) { |
53bc6b4d | 1641 | __xfrm_state_delete(x); |
ab5f5e8b JL |
1642 | xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid, |
1643 | NETLINK_CB(skb).sid); | |
161a09e7 | 1644 | } |
3a765aa5 | 1645 | err = 0; |
53bc6b4d JHS |
1646 | out: |
1647 | spin_unlock_bh(&x->lock); | |
1648 | xfrm_state_put(x); | |
1649 | return err; | |
1650 | } | |
1651 | ||
22e70050 | 1652 | static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1653 | struct nlattr **attrs) |
980ebd25 JHS |
1654 | { |
1655 | struct xfrm_policy *xp; | |
1656 | struct xfrm_user_tmpl *ut; | |
1657 | int i; | |
5424f32e | 1658 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
980ebd25 | 1659 | |
7b67c857 | 1660 | struct xfrm_user_acquire *ua = nlmsg_data(nlh); |
980ebd25 JHS |
1661 | struct xfrm_state *x = xfrm_state_alloc(); |
1662 | int err = -ENOMEM; | |
1663 | ||
1664 | if (!x) | |
1665 | return err; | |
1666 | ||
1667 | err = verify_newpolicy_info(&ua->policy); | |
1668 | if (err) { | |
1669 | printk("BAD policy passed\n"); | |
1670 | kfree(x); | |
1671 | return err; | |
1672 | } | |
1673 | ||
1674 | /* build an XP */ | |
5424f32e | 1675 | xp = xfrm_policy_construct(&ua->policy, attrs, &err); |
b4ad86bf | 1676 | if (!xp) { |
980ebd25 JHS |
1677 | kfree(x); |
1678 | return err; | |
1679 | } | |
1680 | ||
1681 | memcpy(&x->id, &ua->id, sizeof(ua->id)); | |
1682 | memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); | |
1683 | memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); | |
1684 | ||
5424f32e | 1685 | ut = nla_data(rt); |
980ebd25 JHS |
1686 | /* extract the templates and for each call km_key */ |
1687 | for (i = 0; i < xp->xfrm_nr; i++, ut++) { | |
1688 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
1689 | memcpy(&x->id, &t->id, sizeof(x->id)); | |
1690 | x->props.mode = t->mode; | |
1691 | x->props.reqid = t->reqid; | |
1692 | x->props.family = ut->family; | |
1693 | t->aalgos = ua->aalgos; | |
1694 | t->ealgos = ua->ealgos; | |
1695 | t->calgos = ua->calgos; | |
1696 | err = km_query(x, t, xp); | |
1697 | ||
1698 | } | |
1699 | ||
1700 | kfree(x); | |
1701 | kfree(xp); | |
1702 | ||
1703 | return 0; | |
1704 | } | |
1705 | ||
5c79de6e | 1706 | #ifdef CONFIG_XFRM_MIGRATE |
5c79de6e | 1707 | static int copy_from_user_migrate(struct xfrm_migrate *ma, |
5424f32e | 1708 | struct nlattr **attrs, int *num) |
5c79de6e | 1709 | { |
5424f32e | 1710 | struct nlattr *rt = attrs[XFRMA_MIGRATE]; |
5c79de6e SS |
1711 | struct xfrm_user_migrate *um; |
1712 | int i, num_migrate; | |
1713 | ||
5424f32e TG |
1714 | um = nla_data(rt); |
1715 | num_migrate = nla_len(rt) / sizeof(*um); | |
5c79de6e SS |
1716 | |
1717 | if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) | |
1718 | return -EINVAL; | |
1719 | ||
1720 | for (i = 0; i < num_migrate; i++, um++, ma++) { | |
1721 | memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); | |
1722 | memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); | |
1723 | memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); | |
1724 | memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); | |
1725 | ||
1726 | ma->proto = um->proto; | |
1727 | ma->mode = um->mode; | |
1728 | ma->reqid = um->reqid; | |
1729 | ||
1730 | ma->old_family = um->old_family; | |
1731 | ma->new_family = um->new_family; | |
1732 | } | |
1733 | ||
1734 | *num = i; | |
1735 | return 0; | |
1736 | } | |
1737 | ||
1738 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 1739 | struct nlattr **attrs) |
5c79de6e | 1740 | { |
7b67c857 | 1741 | struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); |
5c79de6e SS |
1742 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; |
1743 | u8 type; | |
1744 | int err; | |
1745 | int n = 0; | |
1746 | ||
35a7aa08 | 1747 | if (attrs[XFRMA_MIGRATE] == NULL) |
cf5cb79f | 1748 | return -EINVAL; |
5c79de6e | 1749 | |
5424f32e | 1750 | err = copy_from_user_policy_type(&type, attrs); |
5c79de6e SS |
1751 | if (err) |
1752 | return err; | |
1753 | ||
1754 | err = copy_from_user_migrate((struct xfrm_migrate *)m, | |
5424f32e | 1755 | attrs, &n); |
5c79de6e SS |
1756 | if (err) |
1757 | return err; | |
1758 | ||
1759 | if (!n) | |
1760 | return 0; | |
1761 | ||
1762 | xfrm_migrate(&pi->sel, pi->dir, type, m, n); | |
1763 | ||
1764 | return 0; | |
1765 | } | |
1766 | #else | |
1767 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 1768 | struct nlattr **attrs) |
5c79de6e SS |
1769 | { |
1770 | return -ENOPROTOOPT; | |
1771 | } | |
1772 | #endif | |
1773 | ||
1774 | #ifdef CONFIG_XFRM_MIGRATE | |
1775 | static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb) | |
1776 | { | |
1777 | struct xfrm_user_migrate um; | |
1778 | ||
1779 | memset(&um, 0, sizeof(um)); | |
1780 | um.proto = m->proto; | |
1781 | um.mode = m->mode; | |
1782 | um.reqid = m->reqid; | |
1783 | um.old_family = m->old_family; | |
1784 | memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); | |
1785 | memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); | |
1786 | um.new_family = m->new_family; | |
1787 | memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); | |
1788 | memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); | |
1789 | ||
c0144bea | 1790 | return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); |
5c79de6e SS |
1791 | } |
1792 | ||
7deb2264 TG |
1793 | static inline size_t xfrm_migrate_msgsize(int num_migrate) |
1794 | { | |
1795 | return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) | |
1796 | + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) | |
1797 | + userpolicy_type_attrsize(); | |
1798 | } | |
1799 | ||
5c79de6e SS |
1800 | static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m, |
1801 | int num_migrate, struct xfrm_selector *sel, | |
1802 | u8 dir, u8 type) | |
1803 | { | |
1804 | struct xfrm_migrate *mp; | |
1805 | struct xfrm_userpolicy_id *pol_id; | |
1806 | struct nlmsghdr *nlh; | |
5c79de6e SS |
1807 | int i; |
1808 | ||
79b8b7f4 TG |
1809 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); |
1810 | if (nlh == NULL) | |
1811 | return -EMSGSIZE; | |
5c79de6e | 1812 | |
7b67c857 | 1813 | pol_id = nlmsg_data(nlh); |
5c79de6e SS |
1814 | /* copy data from selector, dir, and type to the pol_id */ |
1815 | memset(pol_id, 0, sizeof(*pol_id)); | |
1816 | memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); | |
1817 | pol_id->dir = dir; | |
1818 | ||
1819 | if (copy_to_user_policy_type(type, skb) < 0) | |
1820 | goto nlmsg_failure; | |
1821 | ||
1822 | for (i = 0, mp = m ; i < num_migrate; i++, mp++) { | |
1823 | if (copy_to_user_migrate(mp, skb) < 0) | |
1824 | goto nlmsg_failure; | |
1825 | } | |
1826 | ||
9825069d | 1827 | return nlmsg_end(skb, nlh); |
5c79de6e | 1828 | nlmsg_failure: |
9825069d TG |
1829 | nlmsg_cancel(skb, nlh); |
1830 | return -EMSGSIZE; | |
5c79de6e SS |
1831 | } |
1832 | ||
1833 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
1834 | struct xfrm_migrate *m, int num_migrate) | |
1835 | { | |
1836 | struct sk_buff *skb; | |
5c79de6e | 1837 | |
7deb2264 | 1838 | skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC); |
5c79de6e SS |
1839 | if (skb == NULL) |
1840 | return -ENOMEM; | |
1841 | ||
1842 | /* build migrate */ | |
1843 | if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0) | |
1844 | BUG(); | |
1845 | ||
082a1ad5 | 1846 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); |
5c79de6e SS |
1847 | } |
1848 | #else | |
1849 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
1850 | struct xfrm_migrate *m, int num_migrate) | |
1851 | { | |
1852 | return -ENOPROTOOPT; | |
1853 | } | |
1854 | #endif | |
d51d081d | 1855 | |
a7bd9a45 | 1856 | #define XMSGSIZE(type) sizeof(struct type) |
492b558b TG |
1857 | |
1858 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { | |
1859 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
1860 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1861 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1862 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), | |
1863 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1864 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1865 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), | |
980ebd25 | 1866 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), |
53bc6b4d | 1867 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), |
492b558b TG |
1868 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
1869 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
6c5c8ca7 | 1870 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), |
492b558b | 1871 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
a7bd9a45 | 1872 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, |
d51d081d JHS |
1873 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
1874 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), | |
97a64b45 | 1875 | [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), |
5c79de6e | 1876 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
a7bd9a45 TG |
1877 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), |
1878 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), | |
1da177e4 LT |
1879 | }; |
1880 | ||
492b558b TG |
1881 | #undef XMSGSIZE |
1882 | ||
cf5cb79f | 1883 | static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { |
1a6509d9 | 1884 | [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, |
cf5cb79f TG |
1885 | [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, |
1886 | [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, | |
1887 | [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, | |
1888 | [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, | |
1889 | [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, | |
1890 | [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, | |
1891 | [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, | |
1892 | [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, | |
1893 | [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, | |
1894 | [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, | |
1895 | [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, | |
1896 | [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, | |
1897 | [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, | |
1898 | [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, | |
1899 | }; | |
1900 | ||
1da177e4 | 1901 | static struct xfrm_link { |
5424f32e | 1902 | int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); |
1da177e4 | 1903 | int (*dump)(struct sk_buff *, struct netlink_callback *); |
4c563f76 | 1904 | int (*done)(struct netlink_callback *); |
492b558b TG |
1905 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { |
1906 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
1907 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, | |
1908 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, | |
4c563f76 TT |
1909 | .dump = xfrm_dump_sa, |
1910 | .done = xfrm_dump_sa_done }, | |
492b558b TG |
1911 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
1912 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, | |
1913 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, | |
4c563f76 TT |
1914 | .dump = xfrm_dump_policy, |
1915 | .done = xfrm_dump_policy_done }, | |
492b558b | 1916 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, |
980ebd25 | 1917 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, |
53bc6b4d | 1918 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, |
492b558b TG |
1919 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
1920 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
6c5c8ca7 | 1921 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, |
492b558b TG |
1922 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, |
1923 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, | |
d51d081d JHS |
1924 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, |
1925 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, | |
5c79de6e | 1926 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, |
566ec034 | 1927 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, |
ecfd6b18 | 1928 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, |
1da177e4 LT |
1929 | }; |
1930 | ||
1d00a4eb | 1931 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 1932 | { |
35a7aa08 | 1933 | struct nlattr *attrs[XFRMA_MAX+1]; |
1da177e4 | 1934 | struct xfrm_link *link; |
a7bd9a45 | 1935 | int type, err; |
1da177e4 | 1936 | |
1da177e4 | 1937 | type = nlh->nlmsg_type; |
1da177e4 | 1938 | if (type > XFRM_MSG_MAX) |
1d00a4eb | 1939 | return -EINVAL; |
1da177e4 LT |
1940 | |
1941 | type -= XFRM_MSG_BASE; | |
1942 | link = &xfrm_dispatch[type]; | |
1943 | ||
1944 | /* All operations require privileges, even GET */ | |
1d00a4eb TG |
1945 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) |
1946 | return -EPERM; | |
1da177e4 | 1947 | |
492b558b TG |
1948 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || |
1949 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && | |
1950 | (nlh->nlmsg_flags & NLM_F_DUMP)) { | |
1da177e4 | 1951 | if (link->dump == NULL) |
1d00a4eb | 1952 | return -EINVAL; |
88fc2c84 | 1953 | |
4c563f76 | 1954 | return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done); |
1da177e4 LT |
1955 | } |
1956 | ||
35a7aa08 | 1957 | err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, |
cf5cb79f | 1958 | xfrma_policy); |
a7bd9a45 TG |
1959 | if (err < 0) |
1960 | return err; | |
1da177e4 LT |
1961 | |
1962 | if (link->doit == NULL) | |
1d00a4eb | 1963 | return -EINVAL; |
1da177e4 | 1964 | |
5424f32e | 1965 | return link->doit(skb, nlh, attrs); |
1da177e4 LT |
1966 | } |
1967 | ||
cd40b7d3 | 1968 | static void xfrm_netlink_rcv(struct sk_buff *skb) |
1da177e4 | 1969 | { |
cd40b7d3 DL |
1970 | mutex_lock(&xfrm_cfg_mutex); |
1971 | netlink_rcv_skb(skb, &xfrm_user_rcv_msg); | |
1972 | mutex_unlock(&xfrm_cfg_mutex); | |
1da177e4 LT |
1973 | } |
1974 | ||
7deb2264 TG |
1975 | static inline size_t xfrm_expire_msgsize(void) |
1976 | { | |
1977 | return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)); | |
1978 | } | |
1979 | ||
d51d081d | 1980 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1981 | { |
1982 | struct xfrm_user_expire *ue; | |
1983 | struct nlmsghdr *nlh; | |
1da177e4 | 1984 | |
79b8b7f4 TG |
1985 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); |
1986 | if (nlh == NULL) | |
1987 | return -EMSGSIZE; | |
1da177e4 | 1988 | |
7b67c857 | 1989 | ue = nlmsg_data(nlh); |
1da177e4 | 1990 | copy_to_user_state(x, &ue->state); |
d51d081d | 1991 | ue->hard = (c->data.hard != 0) ? 1 : 0; |
1da177e4 | 1992 | |
9825069d | 1993 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
1994 | } |
1995 | ||
26b15dad | 1996 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1997 | { |
1998 | struct sk_buff *skb; | |
1999 | ||
7deb2264 | 2000 | skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); |
1da177e4 LT |
2001 | if (skb == NULL) |
2002 | return -ENOMEM; | |
2003 | ||
d51d081d | 2004 | if (build_expire(skb, x, c) < 0) |
1da177e4 LT |
2005 | BUG(); |
2006 | ||
082a1ad5 | 2007 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
1da177e4 LT |
2008 | } |
2009 | ||
d51d081d JHS |
2010 | static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) |
2011 | { | |
2012 | struct sk_buff *skb; | |
d51d081d | 2013 | |
7deb2264 | 2014 | skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
d51d081d JHS |
2015 | if (skb == NULL) |
2016 | return -ENOMEM; | |
2017 | ||
2018 | if (build_aevent(skb, x, c) < 0) | |
2019 | BUG(); | |
2020 | ||
082a1ad5 | 2021 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); |
d51d081d JHS |
2022 | } |
2023 | ||
26b15dad JHS |
2024 | static int xfrm_notify_sa_flush(struct km_event *c) |
2025 | { | |
2026 | struct xfrm_usersa_flush *p; | |
2027 | struct nlmsghdr *nlh; | |
2028 | struct sk_buff *skb; | |
7deb2264 | 2029 | int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); |
26b15dad | 2030 | |
7deb2264 | 2031 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
2032 | if (skb == NULL) |
2033 | return -ENOMEM; | |
26b15dad | 2034 | |
79b8b7f4 TG |
2035 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); |
2036 | if (nlh == NULL) { | |
2037 | kfree_skb(skb); | |
2038 | return -EMSGSIZE; | |
2039 | } | |
26b15dad | 2040 | |
7b67c857 | 2041 | p = nlmsg_data(nlh); |
bf08867f | 2042 | p->proto = c->data.proto; |
26b15dad | 2043 | |
9825069d | 2044 | nlmsg_end(skb, nlh); |
26b15dad | 2045 | |
082a1ad5 | 2046 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
26b15dad JHS |
2047 | } |
2048 | ||
7deb2264 | 2049 | static inline size_t xfrm_sa_len(struct xfrm_state *x) |
26b15dad | 2050 | { |
7deb2264 | 2051 | size_t l = 0; |
1a6509d9 HX |
2052 | if (x->aead) |
2053 | l += nla_total_size(aead_len(x->aead)); | |
26b15dad | 2054 | if (x->aalg) |
0f99be0d | 2055 | l += nla_total_size(xfrm_alg_len(x->aalg)); |
26b15dad | 2056 | if (x->ealg) |
0f99be0d | 2057 | l += nla_total_size(xfrm_alg_len(x->ealg)); |
26b15dad | 2058 | if (x->calg) |
7deb2264 | 2059 | l += nla_total_size(sizeof(*x->calg)); |
26b15dad | 2060 | if (x->encap) |
7deb2264 | 2061 | l += nla_total_size(sizeof(*x->encap)); |
68325d3b HX |
2062 | if (x->security) |
2063 | l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + | |
2064 | x->security->ctx_len); | |
2065 | if (x->coaddr) | |
2066 | l += nla_total_size(sizeof(*x->coaddr)); | |
2067 | ||
d26f3984 HX |
2068 | /* Must count x->lastused as it may become non-zero behind our back. */ |
2069 | l += nla_total_size(sizeof(u64)); | |
26b15dad JHS |
2070 | |
2071 | return l; | |
2072 | } | |
2073 | ||
2074 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) | |
2075 | { | |
2076 | struct xfrm_usersa_info *p; | |
0603eac0 | 2077 | struct xfrm_usersa_id *id; |
26b15dad JHS |
2078 | struct nlmsghdr *nlh; |
2079 | struct sk_buff *skb; | |
26b15dad | 2080 | int len = xfrm_sa_len(x); |
0603eac0 HX |
2081 | int headlen; |
2082 | ||
2083 | headlen = sizeof(*p); | |
2084 | if (c->event == XFRM_MSG_DELSA) { | |
7deb2264 | 2085 | len += nla_total_size(headlen); |
0603eac0 HX |
2086 | headlen = sizeof(*id); |
2087 | } | |
7deb2264 | 2088 | len += NLMSG_ALIGN(headlen); |
26b15dad | 2089 | |
7deb2264 | 2090 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
2091 | if (skb == NULL) |
2092 | return -ENOMEM; | |
26b15dad | 2093 | |
79b8b7f4 TG |
2094 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2095 | if (nlh == NULL) | |
c0144bea | 2096 | goto nla_put_failure; |
26b15dad | 2097 | |
7b67c857 | 2098 | p = nlmsg_data(nlh); |
0603eac0 | 2099 | if (c->event == XFRM_MSG_DELSA) { |
c0144bea TG |
2100 | struct nlattr *attr; |
2101 | ||
7b67c857 | 2102 | id = nlmsg_data(nlh); |
0603eac0 HX |
2103 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); |
2104 | id->spi = x->id.spi; | |
2105 | id->family = x->props.family; | |
2106 | id->proto = x->id.proto; | |
2107 | ||
c0144bea TG |
2108 | attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); |
2109 | if (attr == NULL) | |
2110 | goto nla_put_failure; | |
2111 | ||
2112 | p = nla_data(attr); | |
0603eac0 HX |
2113 | } |
2114 | ||
68325d3b HX |
2115 | if (copy_to_user_state_extra(x, p, skb)) |
2116 | goto nla_put_failure; | |
26b15dad | 2117 | |
9825069d | 2118 | nlmsg_end(skb, nlh); |
26b15dad | 2119 | |
082a1ad5 | 2120 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
26b15dad | 2121 | |
c0144bea | 2122 | nla_put_failure: |
68325d3b HX |
2123 | /* Somebody screwed up with xfrm_sa_len! */ |
2124 | WARN_ON(1); | |
26b15dad JHS |
2125 | kfree_skb(skb); |
2126 | return -1; | |
2127 | } | |
2128 | ||
2129 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) | |
2130 | { | |
2131 | ||
2132 | switch (c->event) { | |
f60f6b8f | 2133 | case XFRM_MSG_EXPIRE: |
26b15dad | 2134 | return xfrm_exp_state_notify(x, c); |
d51d081d JHS |
2135 | case XFRM_MSG_NEWAE: |
2136 | return xfrm_aevent_state_notify(x, c); | |
f60f6b8f HX |
2137 | case XFRM_MSG_DELSA: |
2138 | case XFRM_MSG_UPDSA: | |
2139 | case XFRM_MSG_NEWSA: | |
26b15dad | 2140 | return xfrm_notify_sa(x, c); |
f60f6b8f | 2141 | case XFRM_MSG_FLUSHSA: |
26b15dad JHS |
2142 | return xfrm_notify_sa_flush(c); |
2143 | default: | |
2144 | printk("xfrm_user: Unknown SA event %d\n", c->event); | |
2145 | break; | |
2146 | } | |
2147 | ||
2148 | return 0; | |
2149 | ||
2150 | } | |
2151 | ||
7deb2264 TG |
2152 | static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, |
2153 | struct xfrm_policy *xp) | |
2154 | { | |
2155 | return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) | |
2156 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | |
2157 | + nla_total_size(xfrm_user_sec_ctx_size(x->security)) | |
2158 | + userpolicy_type_attrsize(); | |
2159 | } | |
2160 | ||
1da177e4 LT |
2161 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, |
2162 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, | |
2163 | int dir) | |
2164 | { | |
2165 | struct xfrm_user_acquire *ua; | |
2166 | struct nlmsghdr *nlh; | |
1da177e4 LT |
2167 | __u32 seq = xfrm_get_acqseq(); |
2168 | ||
79b8b7f4 TG |
2169 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); |
2170 | if (nlh == NULL) | |
2171 | return -EMSGSIZE; | |
1da177e4 | 2172 | |
7b67c857 | 2173 | ua = nlmsg_data(nlh); |
1da177e4 LT |
2174 | memcpy(&ua->id, &x->id, sizeof(ua->id)); |
2175 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); | |
2176 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); | |
2177 | copy_to_user_policy(xp, &ua->policy, dir); | |
2178 | ua->aalgos = xt->aalgos; | |
2179 | ua->ealgos = xt->ealgos; | |
2180 | ua->calgos = xt->calgos; | |
2181 | ua->seq = x->km.seq = seq; | |
2182 | ||
2183 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2184 | goto nlmsg_failure; | |
0d681623 | 2185 | if (copy_to_user_state_sec_ctx(x, skb)) |
df71837d | 2186 | goto nlmsg_failure; |
1459bb36 | 2187 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2188 | goto nlmsg_failure; |
1da177e4 | 2189 | |
9825069d | 2190 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
2191 | |
2192 | nlmsg_failure: | |
9825069d TG |
2193 | nlmsg_cancel(skb, nlh); |
2194 | return -EMSGSIZE; | |
1da177e4 LT |
2195 | } |
2196 | ||
2197 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, | |
2198 | struct xfrm_policy *xp, int dir) | |
2199 | { | |
2200 | struct sk_buff *skb; | |
1da177e4 | 2201 | |
7deb2264 | 2202 | skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); |
1da177e4 LT |
2203 | if (skb == NULL) |
2204 | return -ENOMEM; | |
2205 | ||
2206 | if (build_acquire(skb, x, xt, xp, dir) < 0) | |
2207 | BUG(); | |
2208 | ||
082a1ad5 | 2209 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); |
1da177e4 LT |
2210 | } |
2211 | ||
2212 | /* User gives us xfrm_user_policy_info followed by an array of 0 | |
2213 | * or more templates. | |
2214 | */ | |
cb969f07 | 2215 | static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, |
1da177e4 LT |
2216 | u8 *data, int len, int *dir) |
2217 | { | |
2218 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; | |
2219 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); | |
2220 | struct xfrm_policy *xp; | |
2221 | int nr; | |
2222 | ||
cb969f07 | 2223 | switch (sk->sk_family) { |
1da177e4 LT |
2224 | case AF_INET: |
2225 | if (opt != IP_XFRM_POLICY) { | |
2226 | *dir = -EOPNOTSUPP; | |
2227 | return NULL; | |
2228 | } | |
2229 | break; | |
2230 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
2231 | case AF_INET6: | |
2232 | if (opt != IPV6_XFRM_POLICY) { | |
2233 | *dir = -EOPNOTSUPP; | |
2234 | return NULL; | |
2235 | } | |
2236 | break; | |
2237 | #endif | |
2238 | default: | |
2239 | *dir = -EINVAL; | |
2240 | return NULL; | |
2241 | } | |
2242 | ||
2243 | *dir = -EINVAL; | |
2244 | ||
2245 | if (len < sizeof(*p) || | |
2246 | verify_newpolicy_info(p)) | |
2247 | return NULL; | |
2248 | ||
2249 | nr = ((len - sizeof(*p)) / sizeof(*ut)); | |
b4ad86bf | 2250 | if (validate_tmpl(nr, ut, p->sel.family)) |
1da177e4 LT |
2251 | return NULL; |
2252 | ||
a4f1bac6 HX |
2253 | if (p->dir > XFRM_POLICY_OUT) |
2254 | return NULL; | |
2255 | ||
1da177e4 LT |
2256 | xp = xfrm_policy_alloc(GFP_KERNEL); |
2257 | if (xp == NULL) { | |
2258 | *dir = -ENOBUFS; | |
2259 | return NULL; | |
2260 | } | |
2261 | ||
2262 | copy_from_user_policy(xp, p); | |
f7b6983f | 2263 | xp->type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 LT |
2264 | copy_templates(xp, ut, nr); |
2265 | ||
2266 | *dir = p->dir; | |
2267 | ||
2268 | return xp; | |
2269 | } | |
2270 | ||
7deb2264 TG |
2271 | static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) |
2272 | { | |
2273 | return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) | |
2274 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | |
2275 | + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) | |
2276 | + userpolicy_type_attrsize(); | |
2277 | } | |
2278 | ||
1da177e4 | 2279 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, |
d51d081d | 2280 | int dir, struct km_event *c) |
1da177e4 LT |
2281 | { |
2282 | struct xfrm_user_polexpire *upe; | |
2283 | struct nlmsghdr *nlh; | |
d51d081d | 2284 | int hard = c->data.hard; |
1da177e4 | 2285 | |
79b8b7f4 TG |
2286 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); |
2287 | if (nlh == NULL) | |
2288 | return -EMSGSIZE; | |
1da177e4 | 2289 | |
7b67c857 | 2290 | upe = nlmsg_data(nlh); |
1da177e4 LT |
2291 | copy_to_user_policy(xp, &upe->pol, dir); |
2292 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2293 | goto nlmsg_failure; | |
df71837d TJ |
2294 | if (copy_to_user_sec_ctx(xp, skb)) |
2295 | goto nlmsg_failure; | |
1459bb36 | 2296 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2297 | goto nlmsg_failure; |
1da177e4 LT |
2298 | upe->hard = !!hard; |
2299 | ||
9825069d | 2300 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
2301 | |
2302 | nlmsg_failure: | |
9825069d TG |
2303 | nlmsg_cancel(skb, nlh); |
2304 | return -EMSGSIZE; | |
1da177e4 LT |
2305 | } |
2306 | ||
26b15dad | 2307 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
1da177e4 LT |
2308 | { |
2309 | struct sk_buff *skb; | |
1da177e4 | 2310 | |
7deb2264 | 2311 | skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); |
1da177e4 LT |
2312 | if (skb == NULL) |
2313 | return -ENOMEM; | |
2314 | ||
d51d081d | 2315 | if (build_polexpire(skb, xp, dir, c) < 0) |
1da177e4 LT |
2316 | BUG(); |
2317 | ||
082a1ad5 | 2318 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
1da177e4 LT |
2319 | } |
2320 | ||
26b15dad JHS |
2321 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
2322 | { | |
2323 | struct xfrm_userpolicy_info *p; | |
0603eac0 | 2324 | struct xfrm_userpolicy_id *id; |
26b15dad JHS |
2325 | struct nlmsghdr *nlh; |
2326 | struct sk_buff *skb; | |
7deb2264 | 2327 | int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
0603eac0 HX |
2328 | int headlen; |
2329 | ||
2330 | headlen = sizeof(*p); | |
2331 | if (c->event == XFRM_MSG_DELPOLICY) { | |
7deb2264 | 2332 | len += nla_total_size(headlen); |
0603eac0 HX |
2333 | headlen = sizeof(*id); |
2334 | } | |
cfbfd45a | 2335 | len += userpolicy_type_attrsize(); |
7deb2264 | 2336 | len += NLMSG_ALIGN(headlen); |
26b15dad | 2337 | |
7deb2264 | 2338 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
2339 | if (skb == NULL) |
2340 | return -ENOMEM; | |
26b15dad | 2341 | |
79b8b7f4 TG |
2342 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2343 | if (nlh == NULL) | |
2344 | goto nlmsg_failure; | |
26b15dad | 2345 | |
7b67c857 | 2346 | p = nlmsg_data(nlh); |
0603eac0 | 2347 | if (c->event == XFRM_MSG_DELPOLICY) { |
c0144bea TG |
2348 | struct nlattr *attr; |
2349 | ||
7b67c857 | 2350 | id = nlmsg_data(nlh); |
0603eac0 HX |
2351 | memset(id, 0, sizeof(*id)); |
2352 | id->dir = dir; | |
2353 | if (c->data.byid) | |
2354 | id->index = xp->index; | |
2355 | else | |
2356 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); | |
2357 | ||
c0144bea TG |
2358 | attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); |
2359 | if (attr == NULL) | |
2360 | goto nlmsg_failure; | |
2361 | ||
2362 | p = nla_data(attr); | |
0603eac0 | 2363 | } |
26b15dad | 2364 | |
26b15dad JHS |
2365 | copy_to_user_policy(xp, p, dir); |
2366 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2367 | goto nlmsg_failure; | |
1459bb36 | 2368 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2369 | goto nlmsg_failure; |
26b15dad | 2370 | |
9825069d | 2371 | nlmsg_end(skb, nlh); |
26b15dad | 2372 | |
082a1ad5 | 2373 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
26b15dad JHS |
2374 | |
2375 | nlmsg_failure: | |
2376 | kfree_skb(skb); | |
2377 | return -1; | |
2378 | } | |
2379 | ||
2380 | static int xfrm_notify_policy_flush(struct km_event *c) | |
2381 | { | |
2382 | struct nlmsghdr *nlh; | |
2383 | struct sk_buff *skb; | |
26b15dad | 2384 | |
7deb2264 | 2385 | skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); |
26b15dad JHS |
2386 | if (skb == NULL) |
2387 | return -ENOMEM; | |
26b15dad | 2388 | |
79b8b7f4 TG |
2389 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); |
2390 | if (nlh == NULL) | |
2391 | goto nlmsg_failure; | |
0c51f53c JHS |
2392 | if (copy_to_user_policy_type(c->data.type, skb) < 0) |
2393 | goto nlmsg_failure; | |
26b15dad | 2394 | |
9825069d | 2395 | nlmsg_end(skb, nlh); |
26b15dad | 2396 | |
082a1ad5 | 2397 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
26b15dad JHS |
2398 | |
2399 | nlmsg_failure: | |
2400 | kfree_skb(skb); | |
2401 | return -1; | |
2402 | } | |
2403 | ||
2404 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | |
2405 | { | |
2406 | ||
2407 | switch (c->event) { | |
f60f6b8f HX |
2408 | case XFRM_MSG_NEWPOLICY: |
2409 | case XFRM_MSG_UPDPOLICY: | |
2410 | case XFRM_MSG_DELPOLICY: | |
26b15dad | 2411 | return xfrm_notify_policy(xp, dir, c); |
f60f6b8f | 2412 | case XFRM_MSG_FLUSHPOLICY: |
26b15dad | 2413 | return xfrm_notify_policy_flush(c); |
f60f6b8f | 2414 | case XFRM_MSG_POLEXPIRE: |
26b15dad JHS |
2415 | return xfrm_exp_policy_notify(xp, dir, c); |
2416 | default: | |
2417 | printk("xfrm_user: Unknown Policy event %d\n", c->event); | |
2418 | } | |
2419 | ||
2420 | return 0; | |
2421 | ||
2422 | } | |
2423 | ||
7deb2264 TG |
2424 | static inline size_t xfrm_report_msgsize(void) |
2425 | { | |
2426 | return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); | |
2427 | } | |
2428 | ||
97a64b45 MN |
2429 | static int build_report(struct sk_buff *skb, u8 proto, |
2430 | struct xfrm_selector *sel, xfrm_address_t *addr) | |
2431 | { | |
2432 | struct xfrm_user_report *ur; | |
2433 | struct nlmsghdr *nlh; | |
97a64b45 | 2434 | |
79b8b7f4 TG |
2435 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); |
2436 | if (nlh == NULL) | |
2437 | return -EMSGSIZE; | |
97a64b45 | 2438 | |
7b67c857 | 2439 | ur = nlmsg_data(nlh); |
97a64b45 MN |
2440 | ur->proto = proto; |
2441 | memcpy(&ur->sel, sel, sizeof(ur->sel)); | |
2442 | ||
2443 | if (addr) | |
c0144bea | 2444 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); |
97a64b45 | 2445 | |
9825069d | 2446 | return nlmsg_end(skb, nlh); |
97a64b45 | 2447 | |
c0144bea | 2448 | nla_put_failure: |
9825069d TG |
2449 | nlmsg_cancel(skb, nlh); |
2450 | return -EMSGSIZE; | |
97a64b45 MN |
2451 | } |
2452 | ||
2453 | static int xfrm_send_report(u8 proto, struct xfrm_selector *sel, | |
2454 | xfrm_address_t *addr) | |
2455 | { | |
2456 | struct sk_buff *skb; | |
97a64b45 | 2457 | |
7deb2264 | 2458 | skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); |
97a64b45 MN |
2459 | if (skb == NULL) |
2460 | return -ENOMEM; | |
2461 | ||
2462 | if (build_report(skb, proto, sel, addr) < 0) | |
2463 | BUG(); | |
2464 | ||
082a1ad5 | 2465 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); |
97a64b45 MN |
2466 | } |
2467 | ||
1da177e4 LT |
2468 | static struct xfrm_mgr netlink_mgr = { |
2469 | .id = "netlink", | |
2470 | .notify = xfrm_send_state_notify, | |
2471 | .acquire = xfrm_send_acquire, | |
2472 | .compile_policy = xfrm_compile_policy, | |
2473 | .notify_policy = xfrm_send_policy_notify, | |
97a64b45 | 2474 | .report = xfrm_send_report, |
5c79de6e | 2475 | .migrate = xfrm_send_migrate, |
1da177e4 LT |
2476 | }; |
2477 | ||
2478 | static int __init xfrm_user_init(void) | |
2479 | { | |
be33690d PM |
2480 | struct sock *nlsk; |
2481 | ||
654b32c6 | 2482 | printk(KERN_INFO "Initializing XFRM netlink socket\n"); |
1da177e4 | 2483 | |
b4b51029 | 2484 | nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX, |
af65bdfc | 2485 | xfrm_netlink_rcv, NULL, THIS_MODULE); |
be33690d | 2486 | if (nlsk == NULL) |
1da177e4 | 2487 | return -ENOMEM; |
be33690d | 2488 | rcu_assign_pointer(xfrm_nl, nlsk); |
1da177e4 LT |
2489 | |
2490 | xfrm_register_km(&netlink_mgr); | |
2491 | ||
2492 | return 0; | |
2493 | } | |
2494 | ||
2495 | static void __exit xfrm_user_exit(void) | |
2496 | { | |
be33690d PM |
2497 | struct sock *nlsk = xfrm_nl; |
2498 | ||
1da177e4 | 2499 | xfrm_unregister_km(&netlink_mgr); |
be33690d PM |
2500 | rcu_assign_pointer(xfrm_nl, NULL); |
2501 | synchronize_rcu(); | |
b7c6ba6e | 2502 | netlink_kernel_release(nlsk); |
1da177e4 LT |
2503 | } |
2504 | ||
2505 | module_init(xfrm_user_init); | |
2506 | module_exit(xfrm_user_exit); | |
2507 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 | 2508 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); |
f8cd5488 | 2509 |