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