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