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