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