]> git.ipfire.org Git - thirdparty/linux.git/blame - net/xfrm/xfrm_user.c
Merge tag 'for-6.16-rc4-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[thirdparty/linux.git] / net / xfrm / xfrm_user.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/* xfrm_user.c: User interface to configure xfrm engine.
3 *
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 * Changes:
7 * Mitsuru KANDA @USAGI
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * IPv6 support
df71837d 11 *
1da177e4
LT
12 */
13
b6459415 14#include <linux/compat.h>
9409f38a 15#include <linux/crypto.h>
1da177e4
LT
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/slab.h>
20#include <linux/socket.h>
21#include <linux/string.h>
22#include <linux/net.h>
23#include <linux/skbuff.h>
1da177e4
LT
24#include <linux/pfkeyv2.h>
25#include <linux/ipsec.h>
26#include <linux/init.h>
27#include <linux/security.h>
28#include <net/sock.h>
29#include <net/xfrm.h>
88fc2c84 30#include <net/netlink.h>
fa6dd8a2 31#include <net/ah.h>
7c0f6ba6 32#include <linux/uaccess.h>
dfd56b8b 33#if IS_ENABLED(CONFIG_IPV6)
e23c7194
MN
34#include <linux/in6.h>
35#endif
5f60d5f6 36#include <linux/unaligned.h>
1da177e4 37
1fc8fde5
SD
38static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type,
39 struct netlink_ext_ack *extack)
1da177e4 40{
5424f32e 41 struct nlattr *rt = attrs[type];
1da177e4
LT
42 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
5424f32e 47 algp = nla_data(rt);
1fc8fde5
SD
48 if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
49 NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
31c26852 50 return -EINVAL;
1fc8fde5 51 }
31c26852 52
1da177e4
LT
53 switch (type) {
54 case XFRMA_ALG_AUTH:
1da177e4 55 case XFRMA_ALG_CRYPT:
1da177e4 56 case XFRMA_ALG_COMP:
1da177e4
LT
57 break;
58
59 default:
1fc8fde5 60 NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
1da177e4 61 return -EINVAL;
3ff50b79 62 }
1da177e4 63
633439f5 64 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
1da177e4
LT
65 return 0;
66}
67
1fc8fde5
SD
68static int verify_auth_trunc(struct nlattr **attrs,
69 struct netlink_ext_ack *extack)
4447bb33
MW
70{
71 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
72 struct xfrm_algo_auth *algp;
73
74 if (!rt)
75 return 0;
76
77 algp = nla_data(rt);
1fc8fde5
SD
78 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
79 NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
4447bb33 80 return -EINVAL;
1fc8fde5 81 }
4447bb33 82
633439f5 83 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
4447bb33
MW
84 return 0;
85}
86
1fc8fde5 87static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
1a6509d9
HX
88{
89 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
90 struct xfrm_algo_aead *algp;
91
92 if (!rt)
93 return 0;
94
95 algp = nla_data(rt);
1fc8fde5
SD
96 if (nla_len(rt) < (int)aead_len(algp)) {
97 NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
1a6509d9 98 return -EINVAL;
1fc8fde5 99 }
1a6509d9 100
633439f5 101 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
1a6509d9
HX
102 return 0;
103}
104
5424f32e 105static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
eb2971b6
MN
106 xfrm_address_t **addrp)
107{
5424f32e 108 struct nlattr *rt = attrs[type];
eb2971b6 109
cf5cb79f 110 if (rt && addrp)
5424f32e 111 *addrp = nla_data(rt);
eb2971b6 112}
df71837d 113
08a717e4 114static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
df71837d 115{
5424f32e 116 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
df71837d 117 struct xfrm_user_sec_ctx *uctx;
df71837d
TJ
118
119 if (!rt)
120 return 0;
121
5424f32e 122 uctx = nla_data(rt);
171d449a 123 if (uctx->len > nla_len(rt) ||
08a717e4
SD
124 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
125 NL_SET_ERR_MSG(extack, "Invalid security context length");
df71837d 126 return -EINVAL;
08a717e4 127 }
df71837d
TJ
128
129 return 0;
130}
131
d8647b79 132static inline int verify_replay(struct xfrm_usersa_info *p,
a4a87fa4 133 struct nlattr **attrs, u8 sa_dir,
785b87b2 134 struct netlink_ext_ack *extack)
d8647b79
SK
135{
136 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
ecd79187 137 struct xfrm_replay_state_esn *rs;
d8647b79 138
785b87b2
SD
139 if (!rt) {
140 if (p->flags & XFRM_STATE_ESN) {
141 NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
142 return -EINVAL;
143 }
144 return 0;
145 }
ecd79187 146
d97ca5d7 147 rs = nla_data(rt);
ecd79187 148
785b87b2
SD
149 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
150 NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
d97ca5d7 151 return -EINVAL;
785b87b2 152 }
7833aa05 153
d97ca5d7 154 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
785b87b2
SD
155 nla_len(rt) != sizeof(*rs)) {
156 NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
d97ca5d7 157 return -EINVAL;
785b87b2 158 }
d8647b79 159
01714109 160 /* As only ESP and AH support ESN feature. */
785b87b2
SD
161 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
162 NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
02aadf72 163 return -EINVAL;
785b87b2 164 }
02aadf72 165
785b87b2
SD
166 if (p->replay_window != 0) {
167 NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
d8647b79 168 return -EINVAL;
785b87b2 169 }
d8647b79 170
a4a87fa4
AA
171 if (sa_dir == XFRM_SA_DIR_OUT) {
172 if (rs->replay_window) {
173 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
174 return -EINVAL;
175 }
176 if (rs->seq || rs->seq_hi) {
177 NL_SET_ERR_MSG(extack,
178 "Replay seq and seq_hi should be 0 for output SA");
179 return -EINVAL;
180 }
e86212b6
LR
181
182 if (!(p->flags & XFRM_STATE_ESN)) {
183 if (rs->oseq_hi) {
184 NL_SET_ERR_MSG(
185 extack,
186 "Replay oseq_hi should be 0 in non-ESN mode for output SA");
187 return -EINVAL;
188 }
189 if (rs->oseq == U32_MAX) {
190 NL_SET_ERR_MSG(
191 extack,
192 "Replay oseq should be less than 0xFFFFFFFF in non-ESN mode for output SA");
193 return -EINVAL;
194 }
195 } else {
196 if (rs->oseq == U32_MAX && rs->oseq_hi == U32_MAX) {
197 NL_SET_ERR_MSG(
198 extack,
199 "Replay oseq and oseq_hi should be less than 0xFFFFFFFF for output SA");
200 return -EINVAL;
201 }
e3aa43a5 202 }
a4a87fa4
AA
203 if (rs->bmp_len) {
204 NL_SET_ERR_MSG(extack, "Replay bmp_len should 0 for output SA");
205 return -EINVAL;
206 }
207 }
208
209 if (sa_dir == XFRM_SA_DIR_IN) {
210 if (rs->oseq || rs->oseq_hi) {
211 NL_SET_ERR_MSG(extack,
212 "Replay oseq and oseq_hi should be 0 for input SA");
213 return -EINVAL;
214 }
e86212b6
LR
215 if (!(p->flags & XFRM_STATE_ESN)) {
216 if (rs->seq_hi) {
217 NL_SET_ERR_MSG(
218 extack,
219 "Replay seq_hi should be 0 in non-ESN mode for input SA");
220 return -EINVAL;
221 }
222
223 if (rs->seq == U32_MAX) {
224 NL_SET_ERR_MSG(
225 extack,
226 "Replay seq should be less than 0xFFFFFFFF in non-ESN mode for input SA");
227 return -EINVAL;
228 }
229 } else {
230 if (rs->seq == U32_MAX && rs->seq_hi == U32_MAX) {
231 NL_SET_ERR_MSG(
232 extack,
233 "Replay seq and seq_hi should be less than 0xFFFFFFFF for input SA");
234 return -EINVAL;
235 }
e3aa43a5 236 }
a4a87fa4
AA
237 }
238
d8647b79
SK
239 return 0;
240}
df71837d 241
1da177e4 242static int verify_newsa_info(struct xfrm_usersa_info *p,
6999aae1
SD
243 struct nlattr **attrs,
244 struct netlink_ext_ack *extack)
1da177e4
LT
245{
246 int err;
a885a6b2 247 u8 sa_dir = nla_get_u8_default(attrs[XFRMA_SA_DIR], 0);
3f0ab59e 248 u16 family = p->sel.family;
1da177e4
LT
249
250 err = -EINVAL;
251 switch (p->family) {
b38ff407
AG
252 case AF_INET:
253 break;
254
255 case AF_INET6:
256#if IS_ENABLED(CONFIG_IPV6)
257 break;
258#else
259 err = -EAFNOSUPPORT;
6999aae1 260 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
b38ff407
AG
261 goto out;
262#endif
263
264 default:
6999aae1 265 NL_SET_ERR_MSG(extack, "Invalid address family");
b38ff407
AG
266 goto out;
267 }
268
3f0ab59e
SD
269 if (!family && !(p->flags & XFRM_STATE_AF_UNSPEC))
270 family = p->family;
271
272 switch (family) {
b8d6d007
ND
273 case AF_UNSPEC:
274 break;
275
1da177e4 276 case AF_INET:
6999aae1
SD
277 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
278 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
07bf7908 279 goto out;
6999aae1 280 }
07bf7908 281
1da177e4
LT
282 break;
283
284 case AF_INET6:
dfd56b8b 285#if IS_ENABLED(CONFIG_IPV6)
6999aae1
SD
286 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
287 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
07bf7908 288 goto out;
6999aae1 289 }
07bf7908 290
1da177e4
LT
291 break;
292#else
6999aae1 293 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
1da177e4
LT
294 err = -EAFNOSUPPORT;
295 goto out;
296#endif
297
298 default:
6999aae1 299 NL_SET_ERR_MSG(extack, "Invalid address family in selector");
1da177e4 300 goto out;
3ff50b79 301 }
1da177e4
LT
302
303 err = -EINVAL;
304 switch (p->id.proto) {
305 case IPPROTO_AH:
6999aae1
SD
306 if (!attrs[XFRMA_ALG_AUTH] &&
307 !attrs[XFRMA_ALG_AUTH_TRUNC]) {
308 NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
309 goto out;
310 }
311
312 if (attrs[XFRMA_ALG_AEAD] ||
35a7aa08 313 attrs[XFRMA_ALG_CRYPT] ||
35d2856b 314 attrs[XFRMA_ALG_COMP] ||
6999aae1
SD
315 attrs[XFRMA_TFCPAD]) {
316 NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
1da177e4 317 goto out;
6999aae1 318 }
1da177e4
LT
319 break;
320
321 case IPPROTO_ESP:
6999aae1
SD
322 if (attrs[XFRMA_ALG_COMP]) {
323 NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
1a6509d9 324 goto out;
6999aae1
SD
325 }
326
1a6509d9 327 if (!attrs[XFRMA_ALG_AUTH] &&
4447bb33 328 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
1a6509d9 329 !attrs[XFRMA_ALG_CRYPT] &&
6999aae1
SD
330 !attrs[XFRMA_ALG_AEAD]) {
331 NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
1a6509d9 332 goto out;
6999aae1
SD
333 }
334
1a6509d9 335 if ((attrs[XFRMA_ALG_AUTH] ||
4447bb33 336 attrs[XFRMA_ALG_AUTH_TRUNC] ||
1a6509d9 337 attrs[XFRMA_ALG_CRYPT]) &&
6999aae1
SD
338 attrs[XFRMA_ALG_AEAD]) {
339 NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
1da177e4 340 goto out;
6999aae1
SD
341 }
342
35d2856b 343 if (attrs[XFRMA_TFCPAD] &&
6999aae1
SD
344 p->mode != XFRM_MODE_TUNNEL) {
345 NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
35d2856b 346 goto out;
6999aae1 347 }
f69eb4f6
CH
348 if ((attrs[XFRMA_IPTFS_DROP_TIME] ||
349 attrs[XFRMA_IPTFS_REORDER_WINDOW] ||
350 attrs[XFRMA_IPTFS_DONT_FRAG] ||
351 attrs[XFRMA_IPTFS_INIT_DELAY] ||
352 attrs[XFRMA_IPTFS_MAX_QSIZE] ||
353 attrs[XFRMA_IPTFS_PKT_SIZE]) &&
354 p->mode != XFRM_MODE_IPTFS) {
355 NL_SET_ERR_MSG(extack, "IP-TFS options can only be used in IP-TFS mode");
356 goto out;
357 }
1da177e4
LT
358 break;
359
360 case IPPROTO_COMP:
6999aae1
SD
361 if (!attrs[XFRMA_ALG_COMP]) {
362 NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
363 goto out;
364 }
365
366 if (attrs[XFRMA_ALG_AEAD] ||
35a7aa08 367 attrs[XFRMA_ALG_AUTH] ||
4447bb33 368 attrs[XFRMA_ALG_AUTH_TRUNC] ||
35d2856b 369 attrs[XFRMA_ALG_CRYPT] ||
6999aae1
SD
370 attrs[XFRMA_TFCPAD]) {
371 NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
372 goto out;
373 }
374
375 if (ntohl(p->id.spi) >= 0x10000) {
376 NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
1da177e4 377 goto out;
6999aae1 378 }
1da177e4
LT
379 break;
380
dfd56b8b 381#if IS_ENABLED(CONFIG_IPV6)
e23c7194
MN
382 case IPPROTO_DSTOPTS:
383 case IPPROTO_ROUTING:
35a7aa08
TG
384 if (attrs[XFRMA_ALG_COMP] ||
385 attrs[XFRMA_ALG_AUTH] ||
4447bb33 386 attrs[XFRMA_ALG_AUTH_TRUNC] ||
1a6509d9 387 attrs[XFRMA_ALG_AEAD] ||
35a7aa08
TG
388 attrs[XFRMA_ALG_CRYPT] ||
389 attrs[XFRMA_ENCAP] ||
390 attrs[XFRMA_SEC_CTX] ||
6999aae1
SD
391 attrs[XFRMA_TFCPAD]) {
392 NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
e23c7194 393 goto out;
6999aae1
SD
394 }
395
396 if (!attrs[XFRMA_COADDR]) {
397 NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
398 goto out;
399 }
e23c7194
MN
400 break;
401#endif
402
1da177e4 403 default:
6999aae1 404 NL_SET_ERR_MSG(extack, "Unsupported protocol");
1da177e4 405 goto out;
3ff50b79 406 }
1da177e4 407
1fc8fde5 408 if ((err = verify_aead(attrs, extack)))
1a6509d9 409 goto out;
1fc8fde5 410 if ((err = verify_auth_trunc(attrs, extack)))
4447bb33 411 goto out;
1fc8fde5 412 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
1da177e4 413 goto out;
1fc8fde5 414 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
1da177e4 415 goto out;
1fc8fde5 416 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
1da177e4 417 goto out;
6999aae1 418 if ((err = verify_sec_ctx_len(attrs, extack)))
df71837d 419 goto out;
a4a87fa4 420 if ((err = verify_replay(p, attrs, sa_dir, extack)))
d8647b79 421 goto out;
1da177e4
LT
422
423 err = -EINVAL;
424 switch (p->mode) {
7e49e6de
MN
425 case XFRM_MODE_TRANSPORT:
426 case XFRM_MODE_TUNNEL:
060f02a3 427 case XFRM_MODE_ROUTEOPTIMIZATION:
0a69452c 428 case XFRM_MODE_BEET:
1da177e4 429 break;
d1716d5a
CH
430 case XFRM_MODE_IPTFS:
431 if (p->id.proto != IPPROTO_ESP) {
432 NL_SET_ERR_MSG(extack, "IP-TFS mode only supported with ESP");
433 goto out;
434 }
435 if (sa_dir == 0) {
436 NL_SET_ERR_MSG(extack, "IP-TFS mode requires in or out direction attribute");
437 goto out;
438 }
439 break;
1da177e4
LT
440
441 default:
6999aae1 442 NL_SET_ERR_MSG(extack, "Unsupported mode");
1da177e4 443 goto out;
3ff50b79 444 }
1da177e4
LT
445
446 err = 0;
447
6999aae1
SD
448 if (attrs[XFRMA_MTIMER_THRESH]) {
449 if (!attrs[XFRMA_ENCAP]) {
450 NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
4e484b3e 451 err = -EINVAL;
6999aae1
SD
452 goto out;
453 }
a4a87fa4
AA
454
455 if (sa_dir == XFRM_SA_DIR_OUT) {
456 NL_SET_ERR_MSG(extack,
457 "MTIMER_THRESH attribute should not be set on output SA");
458 err = -EINVAL;
459 goto out;
460 }
461 }
462
463 if (sa_dir == XFRM_SA_DIR_OUT) {
464 if (p->flags & XFRM_STATE_DECAP_DSCP) {
465 NL_SET_ERR_MSG(extack, "Flag DECAP_DSCP should not be set for output SA");
466 err = -EINVAL;
467 goto out;
468 }
469
470 if (p->flags & XFRM_STATE_ICMP) {
471 NL_SET_ERR_MSG(extack, "Flag ICMP should not be set for output SA");
472 err = -EINVAL;
473 goto out;
474 }
475
476 if (p->flags & XFRM_STATE_WILDRECV) {
477 NL_SET_ERR_MSG(extack, "Flag WILDRECV should not be set for output SA");
478 err = -EINVAL;
479 goto out;
480 }
481
482 if (p->replay_window) {
483 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
484 err = -EINVAL;
485 goto out;
486 }
487
f69eb4f6
CH
488 if (attrs[XFRMA_IPTFS_DROP_TIME]) {
489 NL_SET_ERR_MSG(extack, "IP-TFS drop time should not be set for output SA");
490 err = -EINVAL;
491 goto out;
492 }
493
494 if (attrs[XFRMA_IPTFS_REORDER_WINDOW]) {
495 NL_SET_ERR_MSG(extack, "IP-TFS reorder window should not be set for output SA");
496 err = -EINVAL;
497 goto out;
498 }
499
a4a87fa4
AA
500 if (attrs[XFRMA_REPLAY_VAL]) {
501 struct xfrm_replay_state *replay;
502
503 replay = nla_data(attrs[XFRMA_REPLAY_VAL]);
504
505 if (replay->seq || replay->bitmap) {
506 NL_SET_ERR_MSG(extack,
507 "Replay seq and bitmap should be 0 for output SA");
508 err = -EINVAL;
509 goto out;
510 }
511 }
512 }
513
514 if (sa_dir == XFRM_SA_DIR_IN) {
515 if (p->flags & XFRM_STATE_NOPMTUDISC) {
516 NL_SET_ERR_MSG(extack, "Flag NOPMTUDISC should not be set for input SA");
517 err = -EINVAL;
518 goto out;
519 }
520
521 if (attrs[XFRMA_SA_EXTRA_FLAGS]) {
522 u32 xflags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
523
524 if (xflags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) {
525 NL_SET_ERR_MSG(extack, "Flag DONT_ENCAP_DSCP should not be set for input SA");
526 err = -EINVAL;
527 goto out;
528 }
529
530 if (xflags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP) {
531 NL_SET_ERR_MSG(extack, "Flag OSEQ_MAY_WRAP should not be set for input SA");
532 err = -EINVAL;
533 goto out;
534 }
535
536 }
f69eb4f6
CH
537
538 if (attrs[XFRMA_IPTFS_DONT_FRAG]) {
539 NL_SET_ERR_MSG(extack, "IP-TFS don't fragment should not be set for input SA");
540 err = -EINVAL;
541 goto out;
542 }
543
544 if (attrs[XFRMA_IPTFS_INIT_DELAY]) {
545 NL_SET_ERR_MSG(extack, "IP-TFS initial delay should not be set for input SA");
546 err = -EINVAL;
547 goto out;
548 }
549
550 if (attrs[XFRMA_IPTFS_MAX_QSIZE]) {
551 NL_SET_ERR_MSG(extack, "IP-TFS max queue size should not be set for input SA");
552 err = -EINVAL;
553 goto out;
554 }
555
556 if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
557 NL_SET_ERR_MSG(extack, "IP-TFS packet size should not be set for input SA");
558 err = -EINVAL;
559 goto out;
560 }
6999aae1 561 }
4e484b3e 562
1ddf9916
SK
563 if (!sa_dir && attrs[XFRMA_SA_PCPU]) {
564 NL_SET_ERR_MSG(extack, "SA_PCPU only supported with SA_DIR");
565 err = -EINVAL;
566 goto out;
567 }
568
1da177e4
LT
569out:
570 return err;
571}
572
573static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
6f2f19ed 574 struct xfrm_algo_desc *(*get_byname)(const char *, int),
2b916826 575 struct nlattr *rta, struct netlink_ext_ack *extack)
1da177e4 576{
1da177e4
LT
577 struct xfrm_algo *p, *ualg;
578 struct xfrm_algo_desc *algo;
579
580 if (!rta)
581 return 0;
582
5424f32e 583 ualg = nla_data(rta);
1da177e4
LT
584
585 algo = get_byname(ualg->alg_name, 1);
2b916826
SD
586 if (!algo) {
587 NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
1da177e4 588 return -ENOSYS;
2b916826 589 }
1da177e4
LT
590 *props = algo->desc.sadb_alg_id;
591
0f99be0d 592 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
1da177e4
LT
593 if (!p)
594 return -ENOMEM;
595
04ff1260 596 strcpy(p->alg_name, algo->name);
1da177e4
LT
597 *algpp = p;
598 return 0;
599}
600
2b916826
SD
601static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
602 struct netlink_ext_ack *extack)
69b0137f
HX
603{
604 struct xfrm_algo *p, *ualg;
605 struct xfrm_algo_desc *algo;
606
607 if (!rta)
608 return 0;
609
610 ualg = nla_data(rta);
611
612 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
2b916826
SD
613 if (!algo) {
614 NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
69b0137f 615 return -ENOSYS;
2b916826 616 }
69b0137f
HX
617 x->props.ealgo = algo->desc.sadb_alg_id;
618
619 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
620 if (!p)
621 return -ENOMEM;
622
623 strcpy(p->alg_name, algo->name);
624 x->ealg = p;
625 x->geniv = algo->uinfo.encr.geniv;
626 return 0;
627}
628
4447bb33 629static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
2b916826 630 struct nlattr *rta, struct netlink_ext_ack *extack)
4447bb33
MW
631{
632 struct xfrm_algo *ualg;
633 struct xfrm_algo_auth *p;
634 struct xfrm_algo_desc *algo;
635
636 if (!rta)
637 return 0;
638
639 ualg = nla_data(rta);
640
641 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
2b916826
SD
642 if (!algo) {
643 NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
4447bb33 644 return -ENOSYS;
2b916826 645 }
4447bb33
MW
646 *props = algo->desc.sadb_alg_id;
647
648 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
649 if (!p)
650 return -ENOMEM;
651
652 strcpy(p->alg_name, algo->name);
653 p->alg_key_len = ualg->alg_key_len;
654 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
655 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
656
657 *algpp = p;
658 return 0;
659}
660
661static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
2b916826 662 struct nlattr *rta, struct netlink_ext_ack *extack)
4447bb33
MW
663{
664 struct xfrm_algo_auth *p, *ualg;
665 struct xfrm_algo_desc *algo;
666
667 if (!rta)
668 return 0;
669
670 ualg = nla_data(rta);
671
672 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
2b916826
SD
673 if (!algo) {
674 NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
4447bb33 675 return -ENOSYS;
2b916826
SD
676 }
677 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
678 NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
4447bb33 679 return -EINVAL;
2b916826 680 }
4447bb33
MW
681 *props = algo->desc.sadb_alg_id;
682
683 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
684 if (!p)
685 return -ENOMEM;
686
687 strcpy(p->alg_name, algo->name);
688 if (!p->alg_trunc_len)
689 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
690
691 *algpp = p;
692 return 0;
693}
694
2b916826
SD
695static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
696 struct netlink_ext_ack *extack)
1a6509d9
HX
697{
698 struct xfrm_algo_aead *p, *ualg;
699 struct xfrm_algo_desc *algo;
700
701 if (!rta)
702 return 0;
703
704 ualg = nla_data(rta);
705
706 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
2b916826
SD
707 if (!algo) {
708 NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
1a6509d9 709 return -ENOSYS;
2b916826 710 }
69b0137f 711 x->props.ealgo = algo->desc.sadb_alg_id;
1a6509d9
HX
712
713 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
714 if (!p)
715 return -ENOMEM;
716
717 strcpy(p->alg_name, algo->name);
69b0137f
HX
718 x->aead = p;
719 x->geniv = algo->uinfo.aead.geniv;
1a6509d9
HX
720 return 0;
721}
722
e2b19125 723static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
643bc1a2
SD
724 struct nlattr *rp,
725 struct netlink_ext_ack *extack)
e2b19125
SK
726{
727 struct xfrm_replay_state_esn *up;
5e708e47 728 unsigned int ulen;
e2b19125
SK
729
730 if (!replay_esn || !rp)
731 return 0;
732
733 up = nla_data(rp);
ecd79187 734 ulen = xfrm_replay_state_esn_len(up);
e2b19125 735
f843ee6d
AW
736 /* Check the overall length and the internal bitmap length to avoid
737 * potential overflow. */
643bc1a2
SD
738 if (nla_len(rp) < (int)ulen) {
739 NL_SET_ERR_MSG(extack, "ESN attribute is too short");
e2b19125 740 return -EINVAL;
643bc1a2
SD
741 }
742
743 if (xfrm_replay_state_esn_len(replay_esn) != ulen) {
744 NL_SET_ERR_MSG(extack, "New ESN size doesn't match the existing SA's ESN size");
745 return -EINVAL;
746 }
e2b19125 747
643bc1a2
SD
748 if (replay_esn->bmp_len != up->bmp_len) {
749 NL_SET_ERR_MSG(extack, "New ESN bitmap size doesn't match the existing SA's ESN bitmap");
677e806d 750 return -EINVAL;
643bc1a2
SD
751 }
752
753 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) {
754 NL_SET_ERR_MSG(extack, "ESN replay window is longer than the bitmap");
755 return -EINVAL;
756 }
677e806d 757
e2b19125
SK
758 return 0;
759}
760
d8647b79
SK
761static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
762 struct xfrm_replay_state_esn **preplay_esn,
763 struct nlattr *rta)
764{
765 struct xfrm_replay_state_esn *p, *pp, *up;
5e708e47 766 unsigned int klen, ulen;
d8647b79
SK
767
768 if (!rta)
769 return 0;
770
771 up = nla_data(rta);
ecd79187 772 klen = xfrm_replay_state_esn_len(up);
5e708e47 773 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
d8647b79 774
ecd79187 775 p = kzalloc(klen, GFP_KERNEL);
d8647b79
SK
776 if (!p)
777 return -ENOMEM;
778
ecd79187 779 pp = kzalloc(klen, GFP_KERNEL);
d8647b79
SK
780 if (!pp) {
781 kfree(p);
782 return -ENOMEM;
783 }
784
ecd79187
MK
785 memcpy(p, up, ulen);
786 memcpy(pp, up, ulen);
787
d8647b79
SK
788 *replay_esn = p;
789 *preplay_esn = pp;
790
791 return 0;
792}
793
a1b831f2 794static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
df71837d 795{
a1b831f2 796 unsigned int len = 0;
df71837d
TJ
797
798 if (xfrm_ctx) {
799 len += sizeof(struct xfrm_user_sec_ctx);
800 len += xfrm_ctx->ctx_len;
801 }
802 return len;
803}
804
1da177e4
LT
805static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
806{
807 memcpy(&x->id, &p->id, sizeof(x->id));
808 memcpy(&x->sel, &p->sel, sizeof(x->sel));
809 memcpy(&x->lft, &p->lft, sizeof(x->lft));
810 x->props.mode = p->mode;
33fce60d
FD
811 x->props.replay_window = min_t(unsigned int, p->replay_window,
812 sizeof(x->replay.bitmap) * 8);
1da177e4
LT
813 x->props.reqid = p->reqid;
814 x->props.family = p->family;
54489c14 815 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
1da177e4 816 x->props.flags = p->flags;
196b0036 817
ccf9b3b8 818 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
196b0036 819 x->sel.family = p->family;
1da177e4
LT
820}
821
d51d081d
JHS
822/*
823 * someday when pfkey also has support, we could have the code
824 * somehow made shareable and move it to xfrm_state.c - JHS
825 *
826*/
e3ac104d
MK
827static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
828 int update_esn)
d51d081d 829{
5424f32e 830 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
e3ac104d 831 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
5424f32e
TG
832 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
833 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
834 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
4e484b3e 835 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
d51d081d 836
00374d9b 837 if (re && x->replay_esn && x->preplay_esn) {
d8647b79
SK
838 struct xfrm_replay_state_esn *replay_esn;
839 replay_esn = nla_data(re);
840 memcpy(x->replay_esn, replay_esn,
841 xfrm_replay_state_esn_len(replay_esn));
842 memcpy(x->preplay_esn, replay_esn,
843 xfrm_replay_state_esn_len(replay_esn));
844 }
845
d51d081d
JHS
846 if (rp) {
847 struct xfrm_replay_state *replay;
5424f32e 848 replay = nla_data(rp);
d51d081d
JHS
849 memcpy(&x->replay, replay, sizeof(*replay));
850 memcpy(&x->preplay, replay, sizeof(*replay));
851 }
852
853 if (lt) {
854 struct xfrm_lifetime_cur *ltime;
5424f32e 855 ltime = nla_data(lt);
d51d081d
JHS
856 x->curlft.bytes = ltime->bytes;
857 x->curlft.packets = ltime->packets;
858 x->curlft.add_time = ltime->add_time;
859 x->curlft.use_time = ltime->use_time;
860 }
861
cf5cb79f 862 if (et)
5424f32e 863 x->replay_maxage = nla_get_u32(et);
d51d081d 864
cf5cb79f 865 if (rt)
5424f32e 866 x->replay_maxdiff = nla_get_u32(rt);
4e484b3e
AA
867
868 if (mt)
869 x->mapping_maxage = nla_get_u32(mt);
d51d081d
JHS
870}
871
9b42c1f1
SK
872static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
873{
874 if (attrs[XFRMA_SET_MARK]) {
875 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
a885a6b2
JB
876 m->m = nla_get_u32_default(attrs[XFRMA_SET_MARK_MASK],
877 0xffffffff);
9b42c1f1
SK
878 } else {
879 m->v = m->m = 0;
880 }
881}
882
fc34acd3
AD
883static struct xfrm_state *xfrm_state_construct(struct net *net,
884 struct xfrm_usersa_info *p,
5424f32e 885 struct nlattr **attrs,
adb5c33e
SD
886 int *errp,
887 struct netlink_ext_ack *extack)
1da177e4 888{
fc34acd3 889 struct xfrm_state *x = xfrm_state_alloc(net);
1da177e4
LT
890 int err = -ENOMEM;
891
892 if (!x)
893 goto error_no_put;
894
895 copy_from_user_state(x, p);
896
6fd06963
SK
897 if (attrs[XFRMA_ENCAP]) {
898 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
899 sizeof(*x->encap), GFP_KERNEL);
900 if (x->encap == NULL)
901 goto error;
902 }
903
904 if (attrs[XFRMA_COADDR]) {
905 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
906 sizeof(*x->coaddr), GFP_KERNEL);
907 if (x->coaddr == NULL)
908 goto error;
909 }
910
a947b0a9
ND
911 if (attrs[XFRMA_SA_EXTRA_FLAGS])
912 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
913
2b916826 914 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
1a6509d9 915 goto error;
4447bb33 916 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
2b916826 917 attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
1da177e4 918 goto error;
4447bb33
MW
919 if (!x->props.aalgo) {
920 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
2b916826 921 attrs[XFRMA_ALG_AUTH], extack)))
4447bb33
MW
922 goto error;
923 }
2b916826 924 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
1da177e4
LT
925 goto error;
926 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
927 xfrm_calg_get_byname,
2b916826 928 attrs[XFRMA_ALG_COMP], extack)))
1da177e4 929 goto error;
fd21150a 930
35d2856b
MW
931 if (attrs[XFRMA_TFCPAD])
932 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
933
6f26b61e
JHS
934 xfrm_mark_get(attrs, &x->mark);
935
9b42c1f1 936 xfrm_smark_init(attrs, &x->props.smark);
077fbac4 937
a3d9001b 938 if (attrs[XFRMA_IF_ID])
7e652640 939 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
077fbac4 940
a4a87fa4
AA
941 if (attrs[XFRMA_SA_DIR])
942 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
943
f531d13b
EB
944 if (attrs[XFRMA_NAT_KEEPALIVE_INTERVAL])
945 x->nat_keepalive_interval =
946 nla_get_u32(attrs[XFRMA_NAT_KEEPALIVE_INTERVAL]);
947
1ddf9916
SK
948 if (attrs[XFRMA_SA_PCPU]) {
949 x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
950 if (x->pcpu_num >= num_possible_cpus())
951 goto error;
952 }
953
b6ccf61a 954 err = __xfrm_init_state(x, extack);
1da177e4
LT
955 if (err)
956 goto error;
957
2f30ea50
MK
958 if (attrs[XFRMA_SEC_CTX]) {
959 err = security_xfrm_state_alloc(x,
960 nla_data(attrs[XFRMA_SEC_CTX]));
961 if (err)
962 goto error;
963 }
df71837d 964
d8647b79
SK
965 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
966 attrs[XFRMA_REPLAY_ESN_VAL])))
967 goto error;
968
1da177e4 969 x->km.seq = p->seq;
b27aeadb 970 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
d51d081d 971 /* sysctl_xfrm_aevent_etime is in 100ms units */
b27aeadb 972 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
d51d081d 973
1cf9a3ae 974 if ((err = xfrm_init_replay(x, extack)))
9fdc4883 975 goto error;
d51d081d 976
9fdc4883 977 /* override default values from above */
e3ac104d 978 xfrm_update_ae_params(x, attrs, 0);
1da177e4 979
cc01572e
YK
980 /* configure the hardware if offload is requested */
981 if (attrs[XFRMA_OFFLOAD_DEV]) {
982 err = xfrm_dev_state_add(net, x,
adb5c33e
SD
983 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
984 extack);
cc01572e
YK
985 if (err)
986 goto error;
987 }
988
7ac64f45
CH
989 if (x->mode_cbs && x->mode_cbs->user_init) {
990 err = x->mode_cbs->user_init(net, x, attrs, extack);
991 if (err)
992 goto error;
993 }
994
1da177e4
LT
995 return x;
996
997error:
998 x->km.state = XFRM_STATE_DEAD;
999 xfrm_state_put(x);
1000error_no_put:
1001 *errp = err;
1002 return NULL;
1003}
1004
22e70050 1005static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 1006 struct nlattr **attrs, struct netlink_ext_ack *extack)
1da177e4 1007{
fc34acd3 1008 struct net *net = sock_net(skb->sk);
7b67c857 1009 struct xfrm_usersa_info *p = nlmsg_data(nlh);
1da177e4
LT
1010 struct xfrm_state *x;
1011 int err;
26b15dad 1012 struct km_event c;
1da177e4 1013
6999aae1 1014 err = verify_newsa_info(p, attrs, extack);
1da177e4
LT
1015 if (err)
1016 return err;
1017
adb5c33e 1018 x = xfrm_state_construct(net, p, attrs, &err, extack);
1da177e4
LT
1019 if (!x)
1020 return err;
1021
26b15dad 1022 xfrm_state_hold(x);
1da177e4
LT
1023 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
1024 err = xfrm_state_add(x);
1025 else
1026 err = xfrm_state_update(x);
1027
2e71029e 1028 xfrm_audit_state_add(x, err ? 0 : 1, true);
161a09e7 1029
1da177e4
LT
1030 if (err < 0) {
1031 x->km.state = XFRM_STATE_DEAD;
c5d4d7d8 1032 xfrm_dev_state_delete(x);
21380b81 1033 __xfrm_state_put(x);
7d6dfe1f 1034 goto out;
1da177e4
LT
1035 }
1036
cc01572e
YK
1037 if (x->km.state == XFRM_STATE_VOID)
1038 x->km.state = XFRM_STATE_VALID;
1039
26b15dad 1040 c.seq = nlh->nlmsg_seq;
15e47304 1041 c.portid = nlh->nlmsg_pid;
f60f6b8f 1042 c.event = nlh->nlmsg_type;
26b15dad
JHS
1043
1044 km_state_notify(x, &c);
7d6dfe1f 1045out:
26b15dad 1046 xfrm_state_put(x);
1da177e4
LT
1047 return err;
1048}
1049
fc34acd3
AD
1050static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
1051 struct xfrm_usersa_id *p,
5424f32e 1052 struct nlattr **attrs,
eb2971b6
MN
1053 int *errp)
1054{
1055 struct xfrm_state *x = NULL;
6f26b61e 1056 struct xfrm_mark m;
eb2971b6 1057 int err;
6f26b61e 1058 u32 mark = xfrm_mark_get(attrs, &m);
eb2971b6
MN
1059
1060 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
1061 err = -ESRCH;
6f26b61e 1062 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
eb2971b6
MN
1063 } else {
1064 xfrm_address_t *saddr = NULL;
1065
35a7aa08 1066 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
eb2971b6
MN
1067 if (!saddr) {
1068 err = -EINVAL;
1069 goto out;
1070 }
1071
9abbffee 1072 err = -ESRCH;
6f26b61e
JHS
1073 x = xfrm_state_lookup_byaddr(net, mark,
1074 &p->daddr, saddr,
221df1ed 1075 p->proto, p->family);
eb2971b6
MN
1076 }
1077
1078 out:
1079 if (!x && errp)
1080 *errp = err;
1081 return x;
1082}
1083
22e70050 1084static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 1085 struct nlattr **attrs, struct netlink_ext_ack *extack)
1da177e4 1086{
fc34acd3 1087 struct net *net = sock_net(skb->sk);
1da177e4 1088 struct xfrm_state *x;
eb2971b6 1089 int err = -ESRCH;
26b15dad 1090 struct km_event c;
7b67c857 1091 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1da177e4 1092
fc34acd3 1093 x = xfrm_user_state_lookup(net, p, attrs, &err);
1da177e4 1094 if (x == NULL)
eb2971b6 1095 return err;
1da177e4 1096
6f68dc37 1097 if ((err = security_xfrm_state_delete(x)) != 0)
c8c05a8e
CZ
1098 goto out;
1099
1da177e4 1100 if (xfrm_state_kern(x)) {
880e475d 1101 NL_SET_ERR_MSG(extack, "SA is in use by tunnels");
c8c05a8e
CZ
1102 err = -EPERM;
1103 goto out;
1da177e4
LT
1104 }
1105
26b15dad 1106 err = xfrm_state_delete(x);
c8c05a8e
CZ
1107 if (err < 0)
1108 goto out;
26b15dad
JHS
1109
1110 c.seq = nlh->nlmsg_seq;
15e47304 1111 c.portid = nlh->nlmsg_pid;
f60f6b8f 1112 c.event = nlh->nlmsg_type;
26b15dad 1113 km_state_notify(x, &c);
1da177e4 1114
c8c05a8e 1115out:
2e71029e 1116 xfrm_audit_state_delete(x, err ? 0 : 1, true);
c8c05a8e 1117 xfrm_state_put(x);
26b15dad 1118 return err;
1da177e4
LT
1119}
1120
1121static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
1122{
f778a636 1123 memset(p, 0, sizeof(*p));
1da177e4
LT
1124 memcpy(&p->id, &x->id, sizeof(p->id));
1125 memcpy(&p->sel, &x->sel, sizeof(p->sel));
1126 memcpy(&p->lft, &x->lft, sizeof(p->lft));
c9fa320b 1127 if (x->xso.dev)
fd2bc419 1128 xfrm_dev_state_update_stats(x);
1da177e4 1129 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
e33d4f13
SV
1130 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
1131 put_unaligned(x->stats.replay, &p->stats.replay);
1132 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
54489c14 1133 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
1da177e4
LT
1134 p->mode = x->props.mode;
1135 p->replay_window = x->props.replay_window;
1136 p->reqid = x->props.reqid;
1137 p->family = x->props.family;
1138 p->flags = x->props.flags;
1139 p->seq = x->km.seq;
1140}
1141
1142struct xfrm_dump_info {
1143 struct sk_buff *in_skb;
1144 struct sk_buff *out_skb;
1145 u32 nlmsg_seq;
1146 u16 nlmsg_flags;
1da177e4
LT
1147};
1148
c0144bea
TG
1149static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1150{
c0144bea
TG
1151 struct xfrm_user_sec_ctx *uctx;
1152 struct nlattr *attr;
68325d3b 1153 int ctx_size = sizeof(*uctx) + s->ctx_len;
c0144bea
TG
1154
1155 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
1156 if (attr == NULL)
1157 return -EMSGSIZE;
1158
1159 uctx = nla_data(attr);
1160 uctx->exttype = XFRMA_SEC_CTX;
1161 uctx->len = ctx_size;
1162 uctx->ctx_doi = s->ctx_doi;
1163 uctx->ctx_alg = s->ctx_alg;
1164 uctx->ctx_len = s->ctx_len;
1165 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1166
1167 return 0;
1168}
1169
87e0a94e 1170static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
d77e38e6
SK
1171{
1172 struct xfrm_user_offload *xuo;
1173 struct nlattr *attr;
1174
1175 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
1176 if (attr == NULL)
1177 return -EMSGSIZE;
1178
1179 xuo = nla_data(attr);
5fe0d4bd 1180 memset(xuo, 0, sizeof(*xuo));
d77e38e6 1181 xuo->ifindex = xso->dev->ifindex;
482db2f1
LR
1182 if (xso->dir == XFRM_DEV_OFFLOAD_IN)
1183 xuo->flags = XFRM_OFFLOAD_INBOUND;
d14f28b8
LR
1184 if (xso->type == XFRM_DEV_OFFLOAD_PACKET)
1185 xuo->flags |= XFRM_OFFLOAD_PACKET;
d77e38e6
SK
1186
1187 return 0;
1188}
1189
c7a5899e
AA
1190static bool xfrm_redact(void)
1191{
1192 return IS_ENABLED(CONFIG_SECURITY) &&
1193 security_locked_down(LOCKDOWN_XFRM_SECRET);
1194}
1195
4447bb33
MW
1196static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
1197{
1198 struct xfrm_algo *algo;
c7a5899e 1199 struct xfrm_algo_auth *ap;
4447bb33 1200 struct nlattr *nla;
c7a5899e 1201 bool redact_secret = xfrm_redact();
4447bb33
MW
1202
1203 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
1204 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
1205 if (!nla)
1206 return -EMSGSIZE;
4447bb33 1207 algo = nla_data(nla);
20eb35da 1208 strscpy_pad(algo->alg_name, auth->alg_name);
c7a5899e
AA
1209
1210 if (redact_secret && auth->alg_key_len)
1211 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
1212 else
1213 memcpy(algo->alg_key, auth->alg_key,
1214 (auth->alg_key_len + 7) / 8);
4447bb33
MW
1215 algo->alg_key_len = auth->alg_key_len;
1216
c7a5899e
AA
1217 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
1218 if (!nla)
1219 return -EMSGSIZE;
1220 ap = nla_data(nla);
20eb35da 1221 strscpy_pad(ap->alg_name, auth->alg_name);
6889cd2a
PV
1222 ap->alg_key_len = auth->alg_key_len;
1223 ap->alg_trunc_len = auth->alg_trunc_len;
c7a5899e
AA
1224 if (redact_secret && auth->alg_key_len)
1225 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
1226 else
1227 memcpy(ap->alg_key, auth->alg_key,
1228 (auth->alg_key_len + 7) / 8);
1229 return 0;
1230}
1231
1232static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
1233{
1234 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
1235 struct xfrm_algo_aead *ap;
1236 bool redact_secret = xfrm_redact();
1237
1238 if (!nla)
1239 return -EMSGSIZE;
1240
1241 ap = nla_data(nla);
20eb35da 1242 strscpy_pad(ap->alg_name, aead->alg_name);
8222d591
HX
1243 ap->alg_key_len = aead->alg_key_len;
1244 ap->alg_icv_len = aead->alg_icv_len;
c7a5899e
AA
1245
1246 if (redact_secret && aead->alg_key_len)
1247 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
1248 else
1249 memcpy(ap->alg_key, aead->alg_key,
1250 (aead->alg_key_len + 7) / 8);
1251 return 0;
1252}
1253
1254static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
1255{
1256 struct xfrm_algo *ap;
1257 bool redact_secret = xfrm_redact();
1258 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
1259 xfrm_alg_len(ealg));
1260 if (!nla)
1261 return -EMSGSIZE;
1262
1263 ap = nla_data(nla);
20eb35da 1264 strscpy_pad(ap->alg_name, ealg->alg_name);
8222d591 1265 ap->alg_key_len = ealg->alg_key_len;
c7a5899e
AA
1266
1267 if (redact_secret && ealg->alg_key_len)
1268 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
1269 else
1270 memcpy(ap->alg_key, ealg->alg_key,
1271 (ealg->alg_key_len + 7) / 8);
1272
4447bb33
MW
1273 return 0;
1274}
1275
8222d591
HX
1276static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
1277{
1278 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
1279 struct xfrm_algo *ap;
1280
1281 if (!nla)
1282 return -EMSGSIZE;
1283
1284 ap = nla_data(nla);
20eb35da 1285 strscpy_pad(ap->alg_name, calg->alg_name);
8222d591
HX
1286 ap->alg_key_len = 0;
1287
1288 return 0;
1289}
1290
1291static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
1292{
1293 struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
1294 struct xfrm_encap_tmpl *uep;
1295
1296 if (!nla)
1297 return -EMSGSIZE;
1298
1299 uep = nla_data(nla);
1300 memset(uep, 0, sizeof(*uep));
1301
1302 uep->encap_type = ep->encap_type;
1303 uep->encap_sport = ep->encap_sport;
1304 uep->encap_dport = ep->encap_dport;
1305 uep->encap_oa = ep->encap_oa;
1306
1307 return 0;
1308}
1309
9b42c1f1
SK
1310static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
1311{
1312 int ret = 0;
1313
1314 if (m->v | m->m) {
1315 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
1316 if (!ret)
1317 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
1318 }
1319 return ret;
1320}
1321
68325d3b
HX
1322/* Don't change this without updating xfrm_sa_len! */
1323static int copy_to_user_state_extra(struct xfrm_state *x,
1324 struct xfrm_usersa_info *p,
1325 struct sk_buff *skb)
1da177e4 1326{
1d1e34dd 1327 int ret = 0;
d0fde795 1328
1d1e34dd 1329 copy_to_user_state(x, p);
68325d3b 1330
a947b0a9
ND
1331 if (x->props.extra_flags) {
1332 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1333 x->props.extra_flags);
1334 if (ret)
1335 goto out;
1336 }
1337
1d1e34dd
DM
1338 if (x->coaddr) {
1339 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
1340 if (ret)
1341 goto out;
1342 }
1343 if (x->lastused) {
de95c4a4
ND
1344 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1345 XFRMA_PAD);
1d1e34dd
DM
1346 if (ret)
1347 goto out;
1348 }
1349 if (x->aead) {
c7a5899e 1350 ret = copy_to_user_aead(x->aead, skb);
1d1e34dd
DM
1351 if (ret)
1352 goto out;
1353 }
1354 if (x->aalg) {
1355 ret = copy_to_user_auth(x->aalg, skb);
1d1e34dd
DM
1356 if (ret)
1357 goto out;
1358 }
1359 if (x->ealg) {
c7a5899e 1360 ret = copy_to_user_ealg(x->ealg, skb);
1d1e34dd
DM
1361 if (ret)
1362 goto out;
1363 }
1364 if (x->calg) {
8222d591 1365 ret = copy_to_user_calg(x->calg, skb);
1d1e34dd
DM
1366 if (ret)
1367 goto out;
1368 }
1369 if (x->encap) {
8222d591 1370 ret = copy_to_user_encap(x->encap, skb);
1d1e34dd
DM
1371 if (ret)
1372 goto out;
1373 }
1374 if (x->tfcpad) {
1375 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1376 if (ret)
1377 goto out;
1378 }
1379 ret = xfrm_mark_put(skb, &x->mark);
1380 if (ret)
1381 goto out;
9b42c1f1
SK
1382
1383 ret = xfrm_smark_put(skb, &x->props.smark);
1384 if (ret)
1385 goto out;
1386
f293a5e3 1387 if (x->replay_esn)
1d1e34dd
DM
1388 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1389 xfrm_replay_state_esn_len(x->replay_esn),
1390 x->replay_esn);
f293a5e3 1391 else
1392 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1393 &x->replay);
1394 if (ret)
1395 goto out;
d77e38e6
SK
1396 if(x->xso.dev)
1397 ret = copy_user_offload(&x->xso, skb);
1398 if (ret)
1399 goto out;
7e652640
SK
1400 if (x->if_id) {
1401 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
077fbac4
LC
1402 if (ret)
1403 goto out;
1404 }
4e484b3e 1405 if (x->security) {
8598112d 1406 ret = copy_sec_ctx(x->security, skb);
4e484b3e
AA
1407 if (ret)
1408 goto out;
1409 }
7ac64f45
CH
1410 if (x->mode_cbs && x->mode_cbs->copy_to_user)
1411 ret = x->mode_cbs->copy_to_user(x, skb);
1412 if (ret)
1413 goto out;
a4a87fa4 1414 if (x->mapping_maxage) {
4e484b3e 1415 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
a4a87fa4
AA
1416 if (ret)
1417 goto out;
1418 }
1ddf9916
SK
1419 if (x->pcpu_num != UINT_MAX) {
1420 ret = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
1421 if (ret)
1422 goto out;
1423 }
a4a87fa4
AA
1424 if (x->dir)
1425 ret = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
f531d13b
EB
1426
1427 if (x->nat_keepalive_interval) {
1428 ret = nla_put_u32(skb, XFRMA_NAT_KEEPALIVE_INTERVAL,
1429 x->nat_keepalive_interval);
1430 if (ret)
1431 goto out;
1432 }
1d1e34dd
DM
1433out:
1434 return ret;
68325d3b
HX
1435}
1436
1437static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1438{
1439 struct xfrm_dump_info *sp = ptr;
1440 struct sk_buff *in_skb = sp->in_skb;
1441 struct sk_buff *skb = sp->out_skb;
5f3eea6b 1442 struct xfrm_translator *xtr;
68325d3b
HX
1443 struct xfrm_usersa_info *p;
1444 struct nlmsghdr *nlh;
1445 int err;
1446
15e47304 1447 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
68325d3b
HX
1448 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1449 if (nlh == NULL)
1450 return -EMSGSIZE;
1451
1452 p = nlmsg_data(nlh);
1453
1454 err = copy_to_user_state_extra(x, p, skb);
1d1e34dd
DM
1455 if (err) {
1456 nlmsg_cancel(skb, nlh);
1457 return err;
1458 }
9825069d 1459 nlmsg_end(skb, nlh);
5f3eea6b
DS
1460
1461 xtr = xfrm_get_translator();
1462 if (xtr) {
1463 err = xtr->alloc_compat(skb, nlh);
1464
1465 xfrm_put_translator(xtr);
1466 if (err) {
1467 nlmsg_cancel(skb, nlh);
1468 return err;
1469 }
1470 }
1471
1da177e4 1472 return 0;
1da177e4
LT
1473}
1474
4c563f76
TT
1475static int xfrm_dump_sa_done(struct netlink_callback *cb)
1476{
1477 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
283bc9f3
FD
1478 struct sock *sk = cb->skb->sk;
1479 struct net *net = sock_net(sk);
1480
1ba5bf99
VN
1481 if (cb->args[0])
1482 xfrm_state_walk_done(walk, net);
4c563f76
TT
1483 return 0;
1484}
1485
1da177e4
LT
1486static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1487{
fc34acd3 1488 struct net *net = sock_net(skb->sk);
4c563f76 1489 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1da177e4
LT
1490 struct xfrm_dump_info info;
1491
4c563f76
TT
1492 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1493 sizeof(cb->args) - sizeof(cb->args[0]));
1494
1da177e4
LT
1495 info.in_skb = cb->skb;
1496 info.out_skb = skb;
1497 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1498 info.nlmsg_flags = NLM_F_MULTI;
4c563f76
TT
1499
1500 if (!cb->args[0]) {
d3623099 1501 struct nlattr *attrs[XFRMA_MAX+1];
870a2df4 1502 struct xfrm_address_filter *filter = NULL;
d3623099
ND
1503 u8 proto = 0;
1504 int err;
1505
8cb08174
JB
1506 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1507 xfrma_policy, cb->extack);
d3623099
ND
1508 if (err < 0)
1509 return err;
1510
870a2df4 1511 if (attrs[XFRMA_ADDRESS_FILTER]) {
df367561
AH
1512 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1513 sizeof(*filter), GFP_KERNEL);
d3623099
ND
1514 if (filter == NULL)
1515 return -ENOMEM;
dfa73c17
LM
1516
1517 /* see addr_match(), (prefix length >> 5) << 2
1518 * will be used to compare xfrm_address_t
1519 */
1520 if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
1521 filter->dplen > (sizeof(xfrm_address_t) << 3)) {
1522 kfree(filter);
1523 return -EINVAL;
1524 }
d3623099
ND
1525 }
1526
1527 if (attrs[XFRMA_PROTO])
1528 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1529
1530 xfrm_state_walk_init(walk, proto, filter);
1ba5bf99 1531 cb->args[0] = 1;
4c563f76
TT
1532 }
1533
fc34acd3 1534 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1da177e4
LT
1535
1536 return skb->len;
1537}
1538
1539static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1540 struct xfrm_state *x, u32 seq)
1541{
1542 struct xfrm_dump_info info;
1543 struct sk_buff *skb;
864745d2 1544 int err;
1da177e4 1545
7deb2264 1546 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1da177e4
LT
1547 if (!skb)
1548 return ERR_PTR(-ENOMEM);
1549
1da177e4
LT
1550 info.in_skb = in_skb;
1551 info.out_skb = skb;
1552 info.nlmsg_seq = seq;
1553 info.nlmsg_flags = 0;
1da177e4 1554
864745d2
MK
1555 err = dump_one_state(x, 0, &info);
1556 if (err) {
1da177e4 1557 kfree_skb(skb);
864745d2 1558 return ERR_PTR(err);
1da177e4
LT
1559 }
1560
1561 return skb;
1562}
1563
21ee543e
MK
1564/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1565 * Must be called with RCU read lock.
1566 */
1567static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1568 u32 pid, unsigned int group)
1569{
1570 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
5461fc0c 1571 struct xfrm_translator *xtr;
21ee543e 1572
86126b77
FW
1573 if (!nlsk) {
1574 kfree_skb(skb);
1575 return -EPIPE;
1576 }
1577
5461fc0c
DS
1578 xtr = xfrm_get_translator();
1579 if (xtr) {
1580 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1581
1582 xfrm_put_translator(xtr);
1583 if (err) {
1584 kfree_skb(skb);
1585 return err;
1586 }
1587 }
1588
86126b77 1589 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
21ee543e
MK
1590}
1591
a1b831f2 1592static inline unsigned int xfrm_spdinfo_msgsize(void)
7deb2264
TG
1593{
1594 return NLMSG_ALIGN(4)
1595 + nla_total_size(sizeof(struct xfrmu_spdinfo))
880a6fab
CG
1596 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1597 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1598 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
7deb2264
TG
1599}
1600
e071041b 1601static int build_spdinfo(struct sk_buff *skb, struct net *net,
15e47304 1602 u32 portid, u32 seq, u32 flags)
ecfd6b18 1603{
5a6d3416
JHS
1604 struct xfrmk_spdinfo si;
1605 struct xfrmu_spdinfo spc;
1606 struct xfrmu_spdhinfo sph;
880a6fab 1607 struct xfrmu_spdhthresh spt4, spt6;
ecfd6b18 1608 struct nlmsghdr *nlh;
1d1e34dd 1609 int err;
ecfd6b18 1610 u32 *f;
880a6fab 1611 unsigned lseq;
ecfd6b18 1612
15e47304 1613 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
25985edc 1614 if (nlh == NULL) /* shouldn't really happen ... */
ecfd6b18
JHS
1615 return -EMSGSIZE;
1616
1617 f = nlmsg_data(nlh);
1618 *f = flags;
e071041b 1619 xfrm_spd_getinfo(net, &si);
5a6d3416
JHS
1620 spc.incnt = si.incnt;
1621 spc.outcnt = si.outcnt;
1622 spc.fwdcnt = si.fwdcnt;
1623 spc.inscnt = si.inscnt;
1624 spc.outscnt = si.outscnt;
1625 spc.fwdscnt = si.fwdscnt;
1626 sph.spdhcnt = si.spdhcnt;
1627 sph.spdhmcnt = si.spdhmcnt;
1628
880a6fab
CG
1629 do {
1630 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1631
1632 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1633 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1634 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1635 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1636 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1637
1d1e34dd
DM
1638 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1639 if (!err)
1640 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
880a6fab
CG
1641 if (!err)
1642 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1643 if (!err)
1644 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1d1e34dd
DM
1645 if (err) {
1646 nlmsg_cancel(skb, nlh);
1647 return err;
1648 }
ecfd6b18 1649
053c095a
JB
1650 nlmsg_end(skb, nlh);
1651 return 0;
ecfd6b18
JHS
1652}
1653
880a6fab 1654static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
1655 struct nlattr **attrs,
1656 struct netlink_ext_ack *extack)
880a6fab
CG
1657{
1658 struct net *net = sock_net(skb->sk);
1659 struct xfrmu_spdhthresh *thresh4 = NULL;
1660 struct xfrmu_spdhthresh *thresh6 = NULL;
1661
1662 /* selector prefixlen thresholds to hash policies */
1663 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1664 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1665
a7417216
SD
1666 if (nla_len(rta) < sizeof(*thresh4)) {
1667 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length");
880a6fab 1668 return -EINVAL;
a7417216 1669 }
880a6fab 1670 thresh4 = nla_data(rta);
a7417216
SD
1671 if (thresh4->lbits > 32 || thresh4->rbits > 32) {
1672 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)");
880a6fab 1673 return -EINVAL;
a7417216 1674 }
880a6fab
CG
1675 }
1676 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1677 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1678
a7417216
SD
1679 if (nla_len(rta) < sizeof(*thresh6)) {
1680 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length");
880a6fab 1681 return -EINVAL;
a7417216 1682 }
880a6fab 1683 thresh6 = nla_data(rta);
a7417216
SD
1684 if (thresh6->lbits > 128 || thresh6->rbits > 128) {
1685 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)");
880a6fab 1686 return -EINVAL;
a7417216 1687 }
880a6fab
CG
1688 }
1689
1690 if (thresh4 || thresh6) {
1691 write_seqlock(&net->xfrm.policy_hthresh.lock);
1692 if (thresh4) {
1693 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1694 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1695 }
1696 if (thresh6) {
1697 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1698 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1699 }
1700 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1701
1702 xfrm_policy_hash_rebuild(net);
1703 }
1704
1705 return 0;
1706}
1707
ecfd6b18 1708static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
1709 struct nlattr **attrs,
1710 struct netlink_ext_ack *extack)
ecfd6b18 1711{
a6483b79 1712 struct net *net = sock_net(skb->sk);
ecfd6b18 1713 struct sk_buff *r_skb;
7b67c857 1714 u32 *flags = nlmsg_data(nlh);
15e47304 1715 u32 sportid = NETLINK_CB(skb).portid;
ecfd6b18 1716 u32 seq = nlh->nlmsg_seq;
2fc5f83b 1717 int err;
ecfd6b18 1718
7deb2264 1719 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
ecfd6b18
JHS
1720 if (r_skb == NULL)
1721 return -ENOMEM;
1722
2fc5f83b
GS
1723 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1724 BUG_ON(err < 0);
ecfd6b18 1725
15e47304 1726 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
ecfd6b18
JHS
1727}
1728
a1b831f2 1729static inline unsigned int xfrm_sadinfo_msgsize(void)
7deb2264
TG
1730{
1731 return NLMSG_ALIGN(4)
1732 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1733 + nla_total_size(4); /* XFRMA_SAD_CNT */
1734}
1735
e071041b 1736static int build_sadinfo(struct sk_buff *skb, struct net *net,
15e47304 1737 u32 portid, u32 seq, u32 flags)
28d8909b 1738{
af11e316
JHS
1739 struct xfrmk_sadinfo si;
1740 struct xfrmu_sadhinfo sh;
28d8909b 1741 struct nlmsghdr *nlh;
1d1e34dd 1742 int err;
28d8909b
JHS
1743 u32 *f;
1744
15e47304 1745 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
25985edc 1746 if (nlh == NULL) /* shouldn't really happen ... */
28d8909b
JHS
1747 return -EMSGSIZE;
1748
1749 f = nlmsg_data(nlh);
1750 *f = flags;
e071041b 1751 xfrm_sad_getinfo(net, &si);
28d8909b 1752
af11e316
JHS
1753 sh.sadhmcnt = si.sadhmcnt;
1754 sh.sadhcnt = si.sadhcnt;
1755
1d1e34dd
DM
1756 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1757 if (!err)
1758 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1759 if (err) {
1760 nlmsg_cancel(skb, nlh);
1761 return err;
1762 }
28d8909b 1763
053c095a
JB
1764 nlmsg_end(skb, nlh);
1765 return 0;
28d8909b
JHS
1766}
1767
1768static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
1769 struct nlattr **attrs,
1770 struct netlink_ext_ack *extack)
28d8909b 1771{
a6483b79 1772 struct net *net = sock_net(skb->sk);
28d8909b 1773 struct sk_buff *r_skb;
7b67c857 1774 u32 *flags = nlmsg_data(nlh);
15e47304 1775 u32 sportid = NETLINK_CB(skb).portid;
28d8909b 1776 u32 seq = nlh->nlmsg_seq;
2fc5f83b 1777 int err;
28d8909b 1778
7deb2264 1779 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
28d8909b
JHS
1780 if (r_skb == NULL)
1781 return -ENOMEM;
1782
2fc5f83b
GS
1783 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1784 BUG_ON(err < 0);
28d8909b 1785
15e47304 1786 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
28d8909b
JHS
1787}
1788
22e70050 1789static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 1790 struct nlattr **attrs, struct netlink_ext_ack *extack)
1da177e4 1791{
fc34acd3 1792 struct net *net = sock_net(skb->sk);
7b67c857 1793 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1da177e4
LT
1794 struct xfrm_state *x;
1795 struct sk_buff *resp_skb;
eb2971b6 1796 int err = -ESRCH;
1da177e4 1797
fc34acd3 1798 x = xfrm_user_state_lookup(net, p, attrs, &err);
1da177e4
LT
1799 if (x == NULL)
1800 goto out_noput;
1801
1802 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1803 if (IS_ERR(resp_skb)) {
1804 err = PTR_ERR(resp_skb);
1805 } else {
15e47304 1806 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1da177e4
LT
1807 }
1808 xfrm_state_put(x);
1809out_noput:
1810 return err;
1811}
1812
22e70050 1813static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
1814 struct nlattr **attrs,
1815 struct netlink_ext_ack *extack)
1da177e4 1816{
fc34acd3 1817 struct net *net = sock_net(skb->sk);
1da177e4
LT
1818 struct xfrm_state *x;
1819 struct xfrm_userspi_info *p;
5f3eea6b 1820 struct xfrm_translator *xtr;
1da177e4
LT
1821 struct sk_buff *resp_skb;
1822 xfrm_address_t *daddr;
1823 int family;
1824 int err;
6f26b61e
JHS
1825 u32 mark;
1826 struct xfrm_mark m;
7e652640 1827 u32 if_id = 0;
1ddf9916 1828 u32 pcpu_num = UINT_MAX;
1da177e4 1829
7b67c857 1830 p = nlmsg_data(nlh);
c2dad11e 1831 err = verify_spi_info(p->info.id.proto, p->min, p->max, extack);
1da177e4
LT
1832 if (err)
1833 goto out_noput;
1834
1835 family = p->info.family;
1836 daddr = &p->info.id.daddr;
1837
1838 x = NULL;
6f26b61e
JHS
1839
1840 mark = xfrm_mark_get(attrs, &m);
7e652640 1841
a3d9001b 1842 if (attrs[XFRMA_IF_ID])
7e652640
SK
1843 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1844
1ddf9916
SK
1845 if (attrs[XFRMA_SA_PCPU]) {
1846 pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
1847 if (pcpu_num >= num_possible_cpus()) {
1848 err = -EINVAL;
1849 goto out_noput;
1850 }
1851 }
1852
1da177e4 1853 if (p->info.seq) {
1ddf9916 1854 x = xfrm_find_acq_byseq(net, mark, p->info.seq, pcpu_num);
70e94e66 1855 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1da177e4
LT
1856 xfrm_state_put(x);
1857 x = NULL;
1858 }
1859 }
1860
1861 if (!x)
6f26b61e 1862 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1ddf9916 1863 if_id, pcpu_num, p->info.id.proto, daddr,
1da177e4
LT
1864 &p->info.saddr, 1,
1865 family);
1866 err = -ENOENT;
c2dad11e
SD
1867 if (!x) {
1868 NL_SET_ERR_MSG(extack, "Target ACQUIRE not found");
1da177e4 1869 goto out_noput;
c2dad11e 1870 }
1da177e4 1871
c2dad11e 1872 err = xfrm_alloc_spi(x, p->min, p->max, extack);
658b219e
HX
1873 if (err)
1874 goto out;
1da177e4 1875
a4a87fa4
AA
1876 if (attrs[XFRMA_SA_DIR])
1877 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
1878
658b219e 1879 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1da177e4
LT
1880 if (IS_ERR(resp_skb)) {
1881 err = PTR_ERR(resp_skb);
1882 goto out;
1883 }
1884
5f3eea6b
DS
1885 xtr = xfrm_get_translator();
1886 if (xtr) {
1887 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1888
1889 xfrm_put_translator(xtr);
1890 if (err) {
1891 kfree_skb(resp_skb);
1892 goto out;
1893 }
1894 }
1895
15e47304 1896 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1da177e4
LT
1897
1898out:
1899 xfrm_state_put(x);
1900out_noput:
1901 return err;
1902}
1903
24fc544f 1904static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
1da177e4
LT
1905{
1906 switch (dir) {
1907 case XFRM_POLICY_IN:
1908 case XFRM_POLICY_OUT:
1909 case XFRM_POLICY_FWD:
1910 break;
1911
1912 default:
24fc544f 1913 NL_SET_ERR_MSG(extack, "Invalid policy direction");
1da177e4 1914 return -EINVAL;
3ff50b79 1915 }
1da177e4
LT
1916
1917 return 0;
1918}
1919
fb7deaba 1920static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
f7b6983f
MN
1921{
1922 switch (type) {
1923 case XFRM_POLICY_TYPE_MAIN:
1924#ifdef CONFIG_XFRM_SUB_POLICY
1925 case XFRM_POLICY_TYPE_SUB:
1926#endif
1927 break;
1928
1929 default:
fb7deaba 1930 NL_SET_ERR_MSG(extack, "Invalid policy type");
f7b6983f 1931 return -EINVAL;
3ff50b79 1932 }
f7b6983f
MN
1933
1934 return 0;
1935}
1936
ec2b4f01
SD
1937static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
1938 struct netlink_ext_ack *extack)
1da177e4 1939{
e682adf0
FD
1940 int ret;
1941
1da177e4
LT
1942 switch (p->share) {
1943 case XFRM_SHARE_ANY:
1944 case XFRM_SHARE_SESSION:
1945 case XFRM_SHARE_USER:
1946 case XFRM_SHARE_UNIQUE:
1947 break;
1948
1949 default:
ec2b4f01 1950 NL_SET_ERR_MSG(extack, "Invalid policy share");
1da177e4 1951 return -EINVAL;
3ff50b79 1952 }
1da177e4
LT
1953
1954 switch (p->action) {
1955 case XFRM_POLICY_ALLOW:
1956 case XFRM_POLICY_BLOCK:
1957 break;
1958
1959 default:
ec2b4f01 1960 NL_SET_ERR_MSG(extack, "Invalid policy action");
1da177e4 1961 return -EINVAL;
3ff50b79 1962 }
1da177e4
LT
1963
1964 switch (p->sel.family) {
1965 case AF_INET:
ec2b4f01
SD
1966 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
1967 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
07bf7908 1968 return -EINVAL;
ec2b4f01 1969 }
07bf7908 1970
1da177e4
LT
1971 break;
1972
1973 case AF_INET6:
dfd56b8b 1974#if IS_ENABLED(CONFIG_IPV6)
ec2b4f01
SD
1975 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
1976 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
07bf7908 1977 return -EINVAL;
ec2b4f01 1978 }
07bf7908 1979
1da177e4
LT
1980 break;
1981#else
ec2b4f01 1982 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
1da177e4
LT
1983 return -EAFNOSUPPORT;
1984#endif
1985
1986 default:
ec2b4f01 1987 NL_SET_ERR_MSG(extack, "Invalid selector family");
1da177e4 1988 return -EINVAL;
3ff50b79 1989 }
1da177e4 1990
24fc544f 1991 ret = verify_policy_dir(p->dir, extack);
e682adf0
FD
1992 if (ret)
1993 return ret;
ec2b4f01
SD
1994 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
1995 NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
e682adf0 1996 return -EINVAL;
ec2b4f01 1997 }
e682adf0
FD
1998
1999 return 0;
1da177e4
LT
2000}
2001
5424f32e 2002static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
df71837d 2003{
5424f32e 2004 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
df71837d
TJ
2005 struct xfrm_user_sec_ctx *uctx;
2006
2007 if (!rt)
2008 return 0;
2009
5424f32e 2010 uctx = nla_data(rt);
52a4c640 2011 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
df71837d
TJ
2012}
2013
1da177e4
LT
2014static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
2015 int nr)
2016{
2017 int i;
2018
2019 xp->xfrm_nr = nr;
2020 for (i = 0; i < nr; i++, ut++) {
2021 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2022
2023 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
2024 memcpy(&t->saddr, &ut->saddr,
2025 sizeof(xfrm_address_t));
2026 t->reqid = ut->reqid;
2027 t->mode = ut->mode;
2028 t->share = ut->share;
2029 t->optional = ut->optional;
2030 t->aalgos = ut->aalgos;
2031 t->ealgos = ut->ealgos;
2032 t->calgos = ut->calgos;
c5d18e98
HX
2033 /* If all masks are ~0, then we allow all algorithms. */
2034 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
8511d01d 2035 t->encap_family = ut->family;
1da177e4
LT
2036 }
2037}
2038
d37bed89 2039static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
3d776e31 2040 int dir, struct netlink_ext_ack *extack)
b4ad86bf 2041{
732706af 2042 u16 prev_family;
b4ad86bf
DM
2043 int i;
2044
d37bed89
SD
2045 if (nr > XFRM_MAX_DEPTH) {
2046 NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
b4ad86bf 2047 return -EINVAL;
d37bed89 2048 }
b4ad86bf 2049
732706af
SK
2050 prev_family = family;
2051
b4ad86bf
DM
2052 for (i = 0; i < nr; i++) {
2053 /* We never validated the ut->family value, so many
2054 * applications simply leave it at zero. The check was
2055 * never made and ut->family was ignored because all
2056 * templates could be assumed to have the same family as
2057 * the policy itself. Now that we will have ipv4-in-ipv6
2058 * and ipv6-in-ipv4 tunnels, this is no longer true.
2059 */
2060 if (!ut[i].family)
2061 ut[i].family = family;
2062
35e61038
FW
2063 switch (ut[i].mode) {
2064 case XFRM_MODE_TUNNEL:
2065 case XFRM_MODE_BEET:
3d776e31
TB
2066 if (ut[i].optional && dir == XFRM_POLICY_OUT) {
2067 NL_SET_ERR_MSG(extack, "Mode in optional template not allowed in outbound policy");
2068 return -EINVAL;
2069 }
35e61038 2070 break;
d1716d5a
CH
2071 case XFRM_MODE_IPTFS:
2072 break;
35e61038 2073 default:
d37bed89
SD
2074 if (ut[i].family != prev_family) {
2075 NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
35e61038 2076 return -EINVAL;
d37bed89 2077 }
35e61038
FW
2078 break;
2079 }
d37bed89
SD
2080 if (ut[i].mode >= XFRM_MODE_MAX) {
2081 NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
32bf94fb 2082 return -EINVAL;
d37bed89 2083 }
732706af
SK
2084
2085 prev_family = ut[i].family;
2086
b4ad86bf
DM
2087 switch (ut[i].family) {
2088 case AF_INET:
2089 break;
dfd56b8b 2090#if IS_ENABLED(CONFIG_IPV6)
b4ad86bf
DM
2091 case AF_INET6:
2092 break;
2093#endif
2094 default:
d37bed89 2095 NL_SET_ERR_MSG(extack, "Invalid family in template");
b4ad86bf 2096 return -EINVAL;
3ff50b79 2097 }
6a53b759 2098
d37bed89
SD
2099 if (!xfrm_id_proto_valid(ut[i].id.proto)) {
2100 NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
6a53b759 2101 return -EINVAL;
d37bed89 2102 }
b4ad86bf
DM
2103 }
2104
2105 return 0;
2106}
2107
d37bed89 2108static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
3d776e31 2109 int dir, struct netlink_ext_ack *extack)
1da177e4 2110{
5424f32e 2111 struct nlattr *rt = attrs[XFRMA_TMPL];
1da177e4
LT
2112
2113 if (!rt) {
2114 pol->xfrm_nr = 0;
2115 } else {
5424f32e
TG
2116 struct xfrm_user_tmpl *utmpl = nla_data(rt);
2117 int nr = nla_len(rt) / sizeof(*utmpl);
b4ad86bf 2118 int err;
1da177e4 2119
3d776e31 2120 err = validate_tmpl(nr, utmpl, pol->family, dir, extack);
b4ad86bf
DM
2121 if (err)
2122 return err;
1da177e4 2123
5424f32e 2124 copy_templates(pol, utmpl, nr);
1da177e4
LT
2125 }
2126 return 0;
2127}
2128
fb7deaba
SD
2129static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
2130 struct netlink_ext_ack *extack)
f7b6983f 2131{
5424f32e 2132 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
f7b6983f 2133 struct xfrm_userpolicy_type *upt;
b798a9ed 2134 u8 type = XFRM_POLICY_TYPE_MAIN;
f7b6983f
MN
2135 int err;
2136
2137 if (rt) {
5424f32e 2138 upt = nla_data(rt);
f7b6983f
MN
2139 type = upt->type;
2140 }
2141
fb7deaba 2142 err = verify_policy_type(type, extack);
f7b6983f
MN
2143 if (err)
2144 return err;
2145
2146 *tp = type;
2147 return 0;
2148}
2149
1da177e4
LT
2150static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
2151{
2152 xp->priority = p->priority;
2153 xp->index = p->index;
2154 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
2155 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
2156 xp->action = p->action;
2157 xp->flags = p->flags;
2158 xp->family = p->sel.family;
2159 /* XXX xp->share = p->share; */
2160}
2161
2162static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
2163{
7b789836 2164 memset(p, 0, sizeof(*p));
1da177e4
LT
2165 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
2166 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
2167 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
2168 p->priority = xp->priority;
2169 p->index = xp->index;
2170 p->sel.family = xp->family;
2171 p->dir = dir;
2172 p->action = xp->action;
2173 p->flags = xp->flags;
2174 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
2175}
2176
fb7deaba
SD
2177static struct xfrm_policy *xfrm_policy_construct(struct net *net,
2178 struct xfrm_userpolicy_info *p,
2179 struct nlattr **attrs,
2180 int *errp,
2181 struct netlink_ext_ack *extack)
1da177e4 2182{
fc34acd3 2183 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1da177e4
LT
2184 int err;
2185
2186 if (!xp) {
2187 *errp = -ENOMEM;
2188 return NULL;
2189 }
2190
2191 copy_from_user_policy(xp, p);
df71837d 2192
fb7deaba 2193 err = copy_from_user_policy_type(&xp->type, attrs, extack);
f7b6983f
MN
2194 if (err)
2195 goto error;
2196
3d776e31 2197 if (!(err = copy_from_user_tmpl(xp, attrs, p->dir, extack)))
35a7aa08 2198 err = copy_from_user_sec_ctx(xp, attrs);
f7b6983f
MN
2199 if (err)
2200 goto error;
1da177e4 2201
295fae56
JHS
2202 xfrm_mark_get(attrs, &xp->mark);
2203
a3d9001b 2204 if (attrs[XFRMA_IF_ID])
7e652640
SK
2205 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2206
919e43fa
LR
2207 /* configure the hardware if offload is requested */
2208 if (attrs[XFRMA_OFFLOAD_DEV]) {
2209 err = xfrm_dev_policy_add(net, xp,
2210 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
2211 p->dir, extack);
2212 if (err)
2213 goto error;
2214 }
2215
1da177e4 2216 return xp;
f7b6983f
MN
2217 error:
2218 *errp = err;
12a169e7 2219 xp->walk.dead = 1;
64c31b3f 2220 xfrm_policy_destroy(xp);
f7b6983f 2221 return NULL;
1da177e4
LT
2222}
2223
22e70050 2224static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2225 struct nlattr **attrs,
2226 struct netlink_ext_ack *extack)
1da177e4 2227{
fc34acd3 2228 struct net *net = sock_net(skb->sk);
7b67c857 2229 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1da177e4 2230 struct xfrm_policy *xp;
26b15dad 2231 struct km_event c;
1da177e4
LT
2232 int err;
2233 int excl;
2234
ec2b4f01 2235 err = verify_newpolicy_info(p, extack);
df71837d
TJ
2236 if (err)
2237 return err;
08a717e4 2238 err = verify_sec_ctx_len(attrs, extack);
1da177e4
LT
2239 if (err)
2240 return err;
2241
fb7deaba 2242 xp = xfrm_policy_construct(net, p, attrs, &err, extack);
1da177e4
LT
2243 if (!xp)
2244 return err;
2245
25985edc 2246 /* shouldn't excl be based on nlh flags??
26b15dad 2247 * Aha! this is anti-netlink really i.e more pfkey derived
a7fd0e6d 2248 * in netlink excl is a flag and you wouldn't need
26b15dad 2249 * a type XFRM_MSG_UPDPOLICY - JHS */
1da177e4
LT
2250 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
2251 err = xfrm_policy_insert(p->dir, xp, excl);
2e71029e 2252 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
161a09e7 2253
1da177e4 2254 if (err) {
919e43fa 2255 xfrm_dev_policy_delete(xp);
94b95dfa 2256 xfrm_dev_policy_free(xp);
03e1ad7b 2257 security_xfrm_policy_free(xp->security);
1da177e4
LT
2258 kfree(xp);
2259 return err;
2260 }
2261
f60f6b8f 2262 c.event = nlh->nlmsg_type;
26b15dad 2263 c.seq = nlh->nlmsg_seq;
15e47304 2264 c.portid = nlh->nlmsg_pid;
26b15dad
JHS
2265 km_policy_notify(xp, p->dir, &c);
2266
1da177e4
LT
2267 xfrm_pol_put(xp);
2268
2269 return 0;
2270}
2271
2272static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
2273{
2274 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
2275 int i;
2276
2277 if (xp->xfrm_nr == 0)
2278 return 0;
2279
1a807e46
NC
2280 if (xp->xfrm_nr > XFRM_MAX_DEPTH)
2281 return -ENOBUFS;
2282
1da177e4
LT
2283 for (i = 0; i < xp->xfrm_nr; i++) {
2284 struct xfrm_user_tmpl *up = &vec[i];
2285 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
2286
1f86840f 2287 memset(up, 0, sizeof(*up));
1da177e4 2288 memcpy(&up->id, &kp->id, sizeof(up->id));
8511d01d 2289 up->family = kp->encap_family;
1da177e4
LT
2290 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
2291 up->reqid = kp->reqid;
2292 up->mode = kp->mode;
2293 up->share = kp->share;
2294 up->optional = kp->optional;
2295 up->aalgos = kp->aalgos;
2296 up->ealgos = kp->ealgos;
2297 up->calgos = kp->calgos;
2298 }
df71837d 2299
c0144bea
TG
2300 return nla_put(skb, XFRMA_TMPL,
2301 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
0d681623
SH
2302}
2303
2304static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
2305{
2306 if (x->security) {
2307 return copy_sec_ctx(x->security, skb);
df71837d
TJ
2308 }
2309 return 0;
0d681623 2310}
df71837d 2311
0d681623
SH
2312static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
2313{
1d1e34dd 2314 if (xp->security)
0d681623 2315 return copy_sec_ctx(xp->security, skb);
0d681623 2316 return 0;
df71837d 2317}
a1b831f2 2318static inline unsigned int userpolicy_type_attrsize(void)
cfbfd45a
TG
2319{
2320#ifdef CONFIG_XFRM_SUB_POLICY
2321 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
2322#else
2323 return 0;
2324#endif
2325}
df71837d 2326
f7b6983f 2327#ifdef CONFIG_XFRM_SUB_POLICY
b798a9ed 2328static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
f7b6983f 2329{
45c180bc
ED
2330 struct xfrm_userpolicy_type upt;
2331
2332 /* Sadly there are two holes in struct xfrm_userpolicy_type */
2333 memset(&upt, 0, sizeof(upt));
2334 upt.type = type;
f7b6983f 2335
c0144bea 2336 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
f7b6983f
MN
2337}
2338
2339#else
b798a9ed 2340static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
f7b6983f
MN
2341{
2342 return 0;
2343}
2344#endif
2345
1da177e4
LT
2346static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
2347{
2348 struct xfrm_dump_info *sp = ptr;
2349 struct xfrm_userpolicy_info *p;
2350 struct sk_buff *in_skb = sp->in_skb;
2351 struct sk_buff *skb = sp->out_skb;
5f3eea6b 2352 struct xfrm_translator *xtr;
1da177e4 2353 struct nlmsghdr *nlh;
1d1e34dd 2354 int err;
1da177e4 2355
15e47304 2356 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
79b8b7f4
TG
2357 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
2358 if (nlh == NULL)
2359 return -EMSGSIZE;
1da177e4 2360
7b67c857 2361 p = nlmsg_data(nlh);
1da177e4 2362 copy_to_user_policy(xp, p, dir);
1d1e34dd
DM
2363 err = copy_to_user_tmpl(xp, skb);
2364 if (!err)
2365 err = copy_to_user_sec_ctx(xp, skb);
2366 if (!err)
2367 err = copy_to_user_policy_type(xp->type, skb);
2368 if (!err)
2369 err = xfrm_mark_put(skb, &xp->mark);
7e652640
SK
2370 if (!err)
2371 err = xfrm_if_id_put(skb, xp->if_id);
919e43fa
LR
2372 if (!err && xp->xdo.dev)
2373 err = copy_user_offload(&xp->xdo, skb);
1d1e34dd
DM
2374 if (err) {
2375 nlmsg_cancel(skb, nlh);
2376 return err;
2377 }
9825069d 2378 nlmsg_end(skb, nlh);
5f3eea6b
DS
2379
2380 xtr = xfrm_get_translator();
2381 if (xtr) {
2382 err = xtr->alloc_compat(skb, nlh);
2383
2384 xfrm_put_translator(xtr);
2385 if (err) {
2386 nlmsg_cancel(skb, nlh);
2387 return err;
2388 }
2389 }
2390
1da177e4 2391 return 0;
1da177e4
LT
2392}
2393
4c563f76
TT
2394static int xfrm_dump_policy_done(struct netlink_callback *cb)
2395{
1137b5e2 2396 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
283bc9f3 2397 struct net *net = sock_net(cb->skb->sk);
4c563f76 2398
283bc9f3 2399 xfrm_policy_walk_done(walk, net);
4c563f76
TT
2400 return 0;
2401}
2402
1137b5e2
HX
2403static int xfrm_dump_policy_start(struct netlink_callback *cb)
2404{
2405 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2406
2407 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
2408
2409 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
2410 return 0;
2411}
2412
1da177e4
LT
2413static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
2414{
fc34acd3 2415 struct net *net = sock_net(skb->sk);
1137b5e2 2416 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1da177e4
LT
2417 struct xfrm_dump_info info;
2418
2419 info.in_skb = cb->skb;
2420 info.out_skb = skb;
2421 info.nlmsg_seq = cb->nlh->nlmsg_seq;
2422 info.nlmsg_flags = NLM_F_MULTI;
4c563f76 2423
fc34acd3 2424 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1da177e4
LT
2425
2426 return skb->len;
2427}
2428
2429static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
2430 struct xfrm_policy *xp,
2431 int dir, u32 seq)
2432{
2433 struct xfrm_dump_info info;
2434 struct sk_buff *skb;
c2546372 2435 int err;
1da177e4 2436
7deb2264 2437 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1da177e4
LT
2438 if (!skb)
2439 return ERR_PTR(-ENOMEM);
2440
1da177e4
LT
2441 info.in_skb = in_skb;
2442 info.out_skb = skb;
2443 info.nlmsg_seq = seq;
2444 info.nlmsg_flags = 0;
1da177e4 2445
c2546372
MK
2446 err = dump_one_policy(xp, dir, 0, &info);
2447 if (err) {
1da177e4 2448 kfree_skb(skb);
c2546372 2449 return ERR_PTR(err);
1da177e4
LT
2450 }
2451
2452 return skb;
2453}
2454
88d0adb5
ND
2455static int xfrm_notify_userpolicy(struct net *net)
2456{
2457 struct xfrm_userpolicy_default *up;
2458 int len = NLMSG_ALIGN(sizeof(*up));
2459 struct nlmsghdr *nlh;
2460 struct sk_buff *skb;
93ec1320 2461 int err;
88d0adb5
ND
2462
2463 skb = nlmsg_new(len, GFP_ATOMIC);
2464 if (skb == NULL)
2465 return -ENOMEM;
2466
2467 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2468 if (nlh == NULL) {
2469 kfree_skb(skb);
2470 return -EMSGSIZE;
2471 }
2472
2473 up = nlmsg_data(nlh);
b58b1f56
ND
2474 up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2475 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2476 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
88d0adb5
ND
2477
2478 nlmsg_end(skb, nlh);
2479
93ec1320
ND
2480 rcu_read_lock();
2481 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2482 rcu_read_unlock();
2483
2484 return err;
88d0adb5
ND
2485}
2486
b58b1f56
ND
2487static bool xfrm_userpolicy_is_valid(__u8 policy)
2488{
2489 return policy == XFRM_USERPOLICY_BLOCK ||
2490 policy == XFRM_USERPOLICY_ACCEPT;
2491}
2492
2d151d39 2493static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 2494 struct nlattr **attrs, struct netlink_ext_ack *extack)
2d151d39
SK
2495{
2496 struct net *net = sock_net(skb->sk);
2497 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2d151d39 2498
b58b1f56
ND
2499 if (xfrm_userpolicy_is_valid(up->in))
2500 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
5d8dbb7f 2501
b58b1f56
ND
2502 if (xfrm_userpolicy_is_valid(up->fwd))
2503 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
5d8dbb7f 2504
b58b1f56
ND
2505 if (xfrm_userpolicy_is_valid(up->out))
2506 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
2d151d39
SK
2507
2508 rt_genid_bump_all(net);
2509
88d0adb5 2510 xfrm_notify_userpolicy(net);
2d151d39
SK
2511 return 0;
2512}
2513
2514static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 2515 struct nlattr **attrs, struct netlink_ext_ack *extack)
2d151d39
SK
2516{
2517 struct sk_buff *r_skb;
2518 struct nlmsghdr *r_nlh;
2519 struct net *net = sock_net(skb->sk);
f8d858e6 2520 struct xfrm_userpolicy_default *r_up;
2d151d39
SK
2521 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2522 u32 portid = NETLINK_CB(skb).portid;
2523 u32 seq = nlh->nlmsg_seq;
2524
2d151d39
SK
2525 r_skb = nlmsg_new(len, GFP_ATOMIC);
2526 if (!r_skb)
2527 return -ENOMEM;
2528
2529 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2530 if (!r_nlh) {
2531 kfree_skb(r_skb);
2532 return -EMSGSIZE;
2533 }
2534
2535 r_up = nlmsg_data(r_nlh);
b58b1f56
ND
2536 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2537 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2538 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2d151d39
SK
2539 nlmsg_end(r_skb, r_nlh);
2540
2541 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2542}
2543
22e70050 2544static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2545 struct nlattr **attrs,
2546 struct netlink_ext_ack *extack)
1da177e4 2547{
fc34acd3 2548 struct net *net = sock_net(skb->sk);
1da177e4
LT
2549 struct xfrm_policy *xp;
2550 struct xfrm_userpolicy_id *p;
b798a9ed 2551 u8 type = XFRM_POLICY_TYPE_MAIN;
1da177e4 2552 int err;
26b15dad 2553 struct km_event c;
1da177e4 2554 int delete;
295fae56 2555 struct xfrm_mark m;
7e652640 2556 u32 if_id = 0;
1da177e4 2557
7b67c857 2558 p = nlmsg_data(nlh);
1da177e4
LT
2559 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2560
fb7deaba 2561 err = copy_from_user_policy_type(&type, attrs, extack);
f7b6983f
MN
2562 if (err)
2563 return err;
2564
24fc544f 2565 err = verify_policy_dir(p->dir, extack);
1da177e4
LT
2566 if (err)
2567 return err;
2568
7e652640
SK
2569 if (attrs[XFRMA_IF_ID])
2570 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2571
4f47e8ab
XL
2572 xfrm_mark_get(attrs, &m);
2573
1da177e4 2574 if (p->index)
4f47e8ab
XL
2575 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2576 p->index, delete, &err);
df71837d 2577 else {
5424f32e 2578 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
03e1ad7b 2579 struct xfrm_sec_ctx *ctx;
df71837d 2580
08a717e4 2581 err = verify_sec_ctx_len(attrs, extack);
df71837d
TJ
2582 if (err)
2583 return err;
2584
2c8dd116 2585 ctx = NULL;
df71837d 2586 if (rt) {
5424f32e 2587 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
df71837d 2588
52a4c640 2589 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
03e1ad7b 2590 if (err)
df71837d 2591 return err;
2c8dd116 2592 }
4f47e8ab
XL
2593 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2594 &p->sel, ctx, delete, &err);
03e1ad7b 2595 security_xfrm_policy_free(ctx);
df71837d 2596 }
1da177e4
LT
2597 if (xp == NULL)
2598 return -ENOENT;
2599
2600 if (!delete) {
2601 struct sk_buff *resp_skb;
2602
2603 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2604 if (IS_ERR(resp_skb)) {
2605 err = PTR_ERR(resp_skb);
2606 } else {
a6483b79 2607 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
15e47304 2608 NETLINK_CB(skb).portid);
1da177e4 2609 }
26b15dad 2610 } else {
2e71029e 2611 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
13fcfbb0
DM
2612
2613 if (err != 0)
c8c05a8e 2614 goto out;
13fcfbb0 2615
e7443892 2616 c.data.byid = p->index;
f60f6b8f 2617 c.event = nlh->nlmsg_type;
26b15dad 2618 c.seq = nlh->nlmsg_seq;
15e47304 2619 c.portid = nlh->nlmsg_pid;
26b15dad 2620 km_policy_notify(xp, p->dir, &c);
1da177e4
LT
2621 }
2622
c8c05a8e 2623out:
ef41aaa0 2624 xfrm_pol_put(xp);
1da177e4
LT
2625 return err;
2626}
2627
22e70050 2628static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2629 struct nlattr **attrs,
2630 struct netlink_ext_ack *extack)
1da177e4 2631{
fc34acd3 2632 struct net *net = sock_net(skb->sk);
26b15dad 2633 struct km_event c;
7b67c857 2634 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
4aa2e62c 2635 int err;
1da177e4 2636
f75a2804 2637 err = xfrm_state_flush(net, p->proto, true, false);
9e64cc95
JHS
2638 if (err) {
2639 if (err == -ESRCH) /* empty table */
2640 return 0;
069c474e 2641 return err;
9e64cc95 2642 }
bf08867f 2643 c.data.proto = p->proto;
f60f6b8f 2644 c.event = nlh->nlmsg_type;
26b15dad 2645 c.seq = nlh->nlmsg_seq;
15e47304 2646 c.portid = nlh->nlmsg_pid;
7067802e 2647 c.net = net;
26b15dad
JHS
2648 km_state_notify(NULL, &c);
2649
1da177e4
LT
2650 return 0;
2651}
2652
a1b831f2 2653static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
7deb2264 2654{
a1b831f2 2655 unsigned int replay_size = x->replay_esn ?
d8647b79
SK
2656 xfrm_replay_state_esn_len(x->replay_esn) :
2657 sizeof(struct xfrm_replay_state);
2658
7deb2264 2659 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
d8647b79 2660 + nla_total_size(replay_size)
de95c4a4 2661 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
6f26b61e 2662 + nla_total_size(sizeof(struct xfrm_mark))
7deb2264 2663 + nla_total_size(4) /* XFRM_AE_RTHR */
a4a87fa4 2664 + nla_total_size(4) /* XFRM_AE_ETHR */
1ddf9916
SK
2665 + nla_total_size(sizeof(x->dir)) /* XFRMA_SA_DIR */
2666 + nla_total_size(4); /* XFRMA_SA_PCPU */
7deb2264 2667}
d51d081d 2668
214e005b 2669static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
d51d081d
JHS
2670{
2671 struct xfrm_aevent_id *id;
2672 struct nlmsghdr *nlh;
1d1e34dd 2673 int err;
d51d081d 2674
15e47304 2675 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
79b8b7f4
TG
2676 if (nlh == NULL)
2677 return -EMSGSIZE;
d51d081d 2678
7b67c857 2679 id = nlmsg_data(nlh);
931e79d7 2680 memset(&id->sa_id, 0, sizeof(id->sa_id));
9b7a787d 2681 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
d51d081d
JHS
2682 id->sa_id.spi = x->id.spi;
2683 id->sa_id.family = x->props.family;
2684 id->sa_id.proto = x->id.proto;
9b7a787d 2685 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2b5f6dcc 2686 id->reqid = x->props.reqid;
d51d081d
JHS
2687 id->flags = c->data.aevent;
2688
d0fde795 2689 if (x->replay_esn) {
1d1e34dd
DM
2690 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2691 xfrm_replay_state_esn_len(x->replay_esn),
2692 x->replay_esn);
d0fde795 2693 } else {
1d1e34dd
DM
2694 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2695 &x->replay);
d0fde795 2696 }
1d1e34dd
DM
2697 if (err)
2698 goto out_cancel;
de95c4a4
ND
2699 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2700 XFRMA_PAD);
1d1e34dd
DM
2701 if (err)
2702 goto out_cancel;
d51d081d 2703
1d1e34dd
DM
2704 if (id->flags & XFRM_AE_RTHR) {
2705 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2706 if (err)
2707 goto out_cancel;
2708 }
2709 if (id->flags & XFRM_AE_ETHR) {
2710 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2711 x->replay_maxage * 10 / HZ);
2712 if (err)
2713 goto out_cancel;
2714 }
2715 err = xfrm_mark_put(skb, &x->mark);
2716 if (err)
2717 goto out_cancel;
6f26b61e 2718
7e652640
SK
2719 err = xfrm_if_id_put(skb, x->if_id);
2720 if (err)
2721 goto out_cancel;
9d287e70 2722 if (x->pcpu_num != UINT_MAX) {
1ddf9916 2723 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
9d287e70
E
2724 if (err)
2725 goto out_cancel;
2726 }
7e652640 2727
a4a87fa4
AA
2728 if (x->dir) {
2729 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
2730 if (err)
2731 goto out_cancel;
2732 }
2733
053c095a
JB
2734 nlmsg_end(skb, nlh);
2735 return 0;
d51d081d 2736
1d1e34dd 2737out_cancel:
9825069d 2738 nlmsg_cancel(skb, nlh);
1d1e34dd 2739 return err;
d51d081d
JHS
2740}
2741
22e70050 2742static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 2743 struct nlattr **attrs, struct netlink_ext_ack *extack)
d51d081d 2744{
fc34acd3 2745 struct net *net = sock_net(skb->sk);
d51d081d
JHS
2746 struct xfrm_state *x;
2747 struct sk_buff *r_skb;
2748 int err;
2749 struct km_event c;
6f26b61e
JHS
2750 u32 mark;
2751 struct xfrm_mark m;
7b67c857 2752 struct xfrm_aevent_id *p = nlmsg_data(nlh);
d51d081d
JHS
2753 struct xfrm_usersa_id *id = &p->sa_id;
2754
6f26b61e
JHS
2755 mark = xfrm_mark_get(attrs, &m);
2756
2757 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
d8647b79 2758 if (x == NULL)
d51d081d 2759 return -ESRCH;
d8647b79
SK
2760
2761 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2762 if (r_skb == NULL) {
2763 xfrm_state_put(x);
2764 return -ENOMEM;
d51d081d
JHS
2765 }
2766
2767 /*
2768 * XXX: is this lock really needed - none of the other
2769 * gets lock (the concern is things getting updated
2770 * while we are still reading) - jhs
2771 */
2772 spin_lock_bh(&x->lock);
2773 c.data.aevent = p->flags;
2774 c.seq = nlh->nlmsg_seq;
15e47304 2775 c.portid = nlh->nlmsg_pid;
d51d081d 2776
2fc5f83b
GS
2777 err = build_aevent(r_skb, x, &c);
2778 BUG_ON(err < 0);
2779
15e47304 2780 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
d51d081d
JHS
2781 spin_unlock_bh(&x->lock);
2782 xfrm_state_put(x);
2783 return err;
2784}
2785
22e70050 2786static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 2787 struct nlattr **attrs, struct netlink_ext_ack *extack)
d51d081d 2788{
fc34acd3 2789 struct net *net = sock_net(skb->sk);
d51d081d
JHS
2790 struct xfrm_state *x;
2791 struct km_event c;
02d0892f 2792 int err = -EINVAL;
6f26b61e
JHS
2793 u32 mark = 0;
2794 struct xfrm_mark m;
7b67c857 2795 struct xfrm_aevent_id *p = nlmsg_data(nlh);
5424f32e 2796 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
d8647b79 2797 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
5424f32e 2798 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
4e077237
MR
2799 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2800 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
d51d081d 2801
643bc1a2
SD
2802 if (!lt && !rp && !re && !et && !rt) {
2803 NL_SET_ERR_MSG(extack, "Missing required attribute for AE");
d51d081d 2804 return err;
643bc1a2 2805 }
d51d081d
JHS
2806
2807 /* pedantic mode - thou shalt sayeth replaceth */
643bc1a2
SD
2808 if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) {
2809 NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required");
d51d081d 2810 return err;
643bc1a2 2811 }
d51d081d 2812
6f26b61e
JHS
2813 mark = xfrm_mark_get(attrs, &m);
2814
2815 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
d51d081d
JHS
2816 if (x == NULL)
2817 return -ESRCH;
2818
643bc1a2
SD
2819 if (x->km.state != XFRM_STATE_VALID) {
2820 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
d51d081d 2821 goto out;
643bc1a2 2822 }
d51d081d 2823
643bc1a2 2824 err = xfrm_replay_verify_len(x->replay_esn, re, extack);
e2b19125
SK
2825 if (err)
2826 goto out;
2827
d51d081d 2828 spin_lock_bh(&x->lock);
e3ac104d 2829 xfrm_update_ae_params(x, attrs, 1);
d51d081d 2830 spin_unlock_bh(&x->lock);
d51d081d
JHS
2831
2832 c.event = nlh->nlmsg_type;
2833 c.seq = nlh->nlmsg_seq;
15e47304 2834 c.portid = nlh->nlmsg_pid;
d51d081d
JHS
2835 c.data.aevent = XFRM_AE_CU;
2836 km_state_notify(x, &c);
2837 err = 0;
2838out:
2839 xfrm_state_put(x);
2840 return err;
2841}
2842
22e70050 2843static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2844 struct nlattr **attrs,
2845 struct netlink_ext_ack *extack)
1da177e4 2846{
fc34acd3 2847 struct net *net = sock_net(skb->sk);
f7b6983f 2848 struct km_event c;
b798a9ed 2849 u8 type = XFRM_POLICY_TYPE_MAIN;
f7b6983f
MN
2850 int err;
2851
fb7deaba 2852 err = copy_from_user_policy_type(&type, attrs, extack);
f7b6983f
MN
2853 if (err)
2854 return err;
26b15dad 2855
2e71029e 2856 err = xfrm_policy_flush(net, type, true);
2f1eb65f
JHS
2857 if (err) {
2858 if (err == -ESRCH) /* empty table */
2859 return 0;
069c474e 2860 return err;
2f1eb65f
JHS
2861 }
2862
f7b6983f 2863 c.data.type = type;
f60f6b8f 2864 c.event = nlh->nlmsg_type;
26b15dad 2865 c.seq = nlh->nlmsg_seq;
15e47304 2866 c.portid = nlh->nlmsg_pid;
7067802e 2867 c.net = net;
26b15dad 2868 km_policy_notify(NULL, 0, &c);
1da177e4
LT
2869 return 0;
2870}
2871
22e70050 2872static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2873 struct nlattr **attrs,
2874 struct netlink_ext_ack *extack)
6c5c8ca7 2875{
fc34acd3 2876 struct net *net = sock_net(skb->sk);
6c5c8ca7 2877 struct xfrm_policy *xp;
7b67c857 2878 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
6c5c8ca7 2879 struct xfrm_userpolicy_info *p = &up->pol;
b798a9ed 2880 u8 type = XFRM_POLICY_TYPE_MAIN;
6c5c8ca7 2881 int err = -ENOENT;
295fae56 2882 struct xfrm_mark m;
7e652640 2883 u32 if_id = 0;
6c5c8ca7 2884
fb7deaba 2885 err = copy_from_user_policy_type(&type, attrs, extack);
f7b6983f
MN
2886 if (err)
2887 return err;
2888
24fc544f 2889 err = verify_policy_dir(p->dir, extack);
c8bf4d04
TT
2890 if (err)
2891 return err;
2892
7e652640
SK
2893 if (attrs[XFRMA_IF_ID])
2894 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2895
4f47e8ab
XL
2896 xfrm_mark_get(attrs, &m);
2897
6c5c8ca7 2898 if (p->index)
4f47e8ab
XL
2899 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2900 0, &err);
6c5c8ca7 2901 else {
5424f32e 2902 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
03e1ad7b 2903 struct xfrm_sec_ctx *ctx;
6c5c8ca7 2904
08a717e4 2905 err = verify_sec_ctx_len(attrs, extack);
6c5c8ca7
JHS
2906 if (err)
2907 return err;
2908
2c8dd116 2909 ctx = NULL;
6c5c8ca7 2910 if (rt) {
5424f32e 2911 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
6c5c8ca7 2912
52a4c640 2913 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
03e1ad7b 2914 if (err)
6c5c8ca7 2915 return err;
2c8dd116 2916 }
4f47e8ab 2917 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
6f26b61e 2918 &p->sel, ctx, 0, &err);
03e1ad7b 2919 security_xfrm_policy_free(ctx);
6c5c8ca7 2920 }
6c5c8ca7 2921 if (xp == NULL)
ef41aaa0 2922 return -ENOENT;
03e1ad7b 2923
ea2dea9d 2924 if (unlikely(xp->walk.dead))
6c5c8ca7 2925 goto out;
6c5c8ca7 2926
6c5c8ca7
JHS
2927 err = 0;
2928 if (up->hard) {
2929 xfrm_policy_delete(xp, p->dir);
2e71029e 2930 xfrm_audit_policy_delete(xp, 1, true);
6c5c8ca7 2931 }
c6bb8136 2932 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
6c5c8ca7
JHS
2933
2934out:
2935 xfrm_pol_put(xp);
2936 return err;
2937}
2938
22e70050 2939static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2940 struct nlattr **attrs,
2941 struct netlink_ext_ack *extack)
53bc6b4d 2942{
fc34acd3 2943 struct net *net = sock_net(skb->sk);
53bc6b4d
JHS
2944 struct xfrm_state *x;
2945 int err;
7b67c857 2946 struct xfrm_user_expire *ue = nlmsg_data(nlh);
53bc6b4d 2947 struct xfrm_usersa_info *p = &ue->state;
6f26b61e 2948 struct xfrm_mark m;
928497f0 2949 u32 mark = xfrm_mark_get(attrs, &m);
53bc6b4d 2950
6f26b61e 2951 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
53bc6b4d 2952
3a765aa5 2953 err = -ENOENT;
53bc6b4d
JHS
2954 if (x == NULL)
2955 return err;
2956
53bc6b4d 2957 spin_lock_bh(&x->lock);
3a765aa5 2958 err = -EINVAL;
a25b19f3
SD
2959 if (x->km.state != XFRM_STATE_VALID) {
2960 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
53bc6b4d 2961 goto out;
a25b19f3
SD
2962 }
2963
c6bb8136 2964 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
53bc6b4d 2965
161a09e7 2966 if (ue->hard) {
53bc6b4d 2967 __xfrm_state_delete(x);
2e71029e 2968 xfrm_audit_state_delete(x, 1, true);
161a09e7 2969 }
3a765aa5 2970 err = 0;
53bc6b4d
JHS
2971out:
2972 spin_unlock_bh(&x->lock);
2973 xfrm_state_put(x);
2974 return err;
2975}
2976
22e70050 2977static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e
SD
2978 struct nlattr **attrs,
2979 struct netlink_ext_ack *extack)
980ebd25 2980{
fc34acd3 2981 struct net *net = sock_net(skb->sk);
980ebd25
JHS
2982 struct xfrm_policy *xp;
2983 struct xfrm_user_tmpl *ut;
2984 int i;
5424f32e 2985 struct nlattr *rt = attrs[XFRMA_TMPL];
6f26b61e 2986 struct xfrm_mark mark;
980ebd25 2987
7b67c857 2988 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
fc34acd3 2989 struct xfrm_state *x = xfrm_state_alloc(net);
980ebd25
JHS
2990 int err = -ENOMEM;
2991
2992 if (!x)
d8eb9307 2993 goto nomem;
980ebd25 2994
6f26b61e
JHS
2995 xfrm_mark_get(attrs, &mark);
2996
1ddf9916
SK
2997 if (attrs[XFRMA_SA_PCPU]) {
2998 x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
2999 err = -EINVAL;
3000 if (x->pcpu_num >= num_possible_cpus())
3001 goto free_state;
3002 }
3003
ec2b4f01 3004 err = verify_newpolicy_info(&ua->policy, extack);
a1a7e3a3
XL
3005 if (err)
3006 goto free_state;
08a717e4 3007 err = verify_sec_ctx_len(attrs, extack);
d8eb9307 3008 if (err)
73efc324 3009 goto free_state;
980ebd25
JHS
3010
3011 /* build an XP */
fb7deaba 3012 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
d8eb9307
IJ
3013 if (!xp)
3014 goto free_state;
980ebd25
JHS
3015
3016 memcpy(&x->id, &ua->id, sizeof(ua->id));
3017 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
3018 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
6f26b61e
JHS
3019 xp->mark.m = x->mark.m = mark.m;
3020 xp->mark.v = x->mark.v = mark.v;
5424f32e 3021 ut = nla_data(rt);
980ebd25
JHS
3022 /* extract the templates and for each call km_key */
3023 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
3024 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
3025 memcpy(&x->id, &t->id, sizeof(x->id));
3026 x->props.mode = t->mode;
3027 x->props.reqid = t->reqid;
3028 x->props.family = ut->family;
3029 t->aalgos = ua->aalgos;
3030 t->ealgos = ua->ealgos;
3031 t->calgos = ua->calgos;
3032 err = km_query(x, t, xp);
3033
3034 }
3035
4a135e53 3036 xfrm_state_free(x);
980ebd25
JHS
3037 kfree(xp);
3038
3039 return 0;
d8eb9307 3040
d8eb9307 3041free_state:
4a135e53 3042 xfrm_state_free(x);
d8eb9307
IJ
3043nomem:
3044 return err;
980ebd25
JHS
3045}
3046
5c79de6e 3047#ifdef CONFIG_XFRM_MIGRATE
5c79de6e 3048static int copy_from_user_migrate(struct xfrm_migrate *ma,
13c1d189 3049 struct xfrm_kmaddress *k,
bd122403
SD
3050 struct nlattr **attrs, int *num,
3051 struct netlink_ext_ack *extack)
5c79de6e 3052{
5424f32e 3053 struct nlattr *rt = attrs[XFRMA_MIGRATE];
5c79de6e
SS
3054 struct xfrm_user_migrate *um;
3055 int i, num_migrate;
3056
13c1d189
AE
3057 if (k != NULL) {
3058 struct xfrm_user_kmaddress *uk;
3059
3060 uk = nla_data(attrs[XFRMA_KMADDRESS]);
3061 memcpy(&k->local, &uk->local, sizeof(k->local));
3062 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
3063 k->family = uk->family;
3064 k->reserved = uk->reserved;
3065 }
3066
5424f32e
TG
3067 um = nla_data(rt);
3068 num_migrate = nla_len(rt) / sizeof(*um);
5c79de6e 3069
bd122403
SD
3070 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) {
3071 NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)");
5c79de6e 3072 return -EINVAL;
bd122403 3073 }
5c79de6e
SS
3074
3075 for (i = 0; i < num_migrate; i++, um++, ma++) {
3076 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
3077 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
3078 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
3079 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
3080
3081 ma->proto = um->proto;
3082 ma->mode = um->mode;
3083 ma->reqid = um->reqid;
3084
3085 ma->old_family = um->old_family;
3086 ma->new_family = um->new_family;
3087 }
3088
3089 *num = i;
3090 return 0;
3091}
3092
3093static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 3094 struct nlattr **attrs, struct netlink_ext_ack *extack)
5c79de6e 3095{
7b67c857 3096 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
5c79de6e 3097 struct xfrm_migrate m[XFRM_MAX_DEPTH];
13c1d189 3098 struct xfrm_kmaddress km, *kmp;
5c79de6e
SS
3099 u8 type;
3100 int err;
3101 int n = 0;
8d549c4f 3102 struct net *net = sock_net(skb->sk);
4ab47d47 3103 struct xfrm_encap_tmpl *encap = NULL;
ab244a39 3104 struct xfrm_user_offload *xuo = NULL;
c1aca308 3105 u32 if_id = 0;
5c79de6e 3106
bd122403
SD
3107 if (!attrs[XFRMA_MIGRATE]) {
3108 NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute");
cf5cb79f 3109 return -EINVAL;
bd122403 3110 }
5c79de6e 3111
13c1d189
AE
3112 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
3113
fb7deaba 3114 err = copy_from_user_policy_type(&type, attrs, extack);
5c79de6e
SS
3115 if (err)
3116 return err;
3117
bd122403 3118 err = copy_from_user_migrate(m, kmp, attrs, &n, extack);
5c79de6e
SS
3119 if (err)
3120 return err;
3121
3122 if (!n)
3123 return 0;
3124
4ab47d47
AA
3125 if (attrs[XFRMA_ENCAP]) {
3126 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
3127 sizeof(*encap), GFP_KERNEL);
3128 if (!encap)
4ac7a6ee 3129 return -ENOMEM;
4ab47d47 3130 }
5c79de6e 3131
c1aca308
YY
3132 if (attrs[XFRMA_IF_ID])
3133 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
3134
ab244a39
CW
3135 if (attrs[XFRMA_OFFLOAD_DEV]) {
3136 xuo = kmemdup(nla_data(attrs[XFRMA_OFFLOAD_DEV]),
3137 sizeof(*xuo), GFP_KERNEL);
3138 if (!xuo) {
3139 err = -ENOMEM;
3140 goto error;
3141 }
3142 }
bd122403 3143 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap,
ab244a39
CW
3144 if_id, extack, xuo);
3145error:
4ab47d47 3146 kfree(encap);
ab244a39 3147 kfree(xuo);
4ab47d47 3148 return err;
5c79de6e
SS
3149}
3150#else
3151static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
3bec6c3e 3152 struct nlattr **attrs, struct netlink_ext_ack *extack)
5c79de6e
SS
3153{
3154 return -ENOPROTOOPT;
3155}
3156#endif
3157
3158#ifdef CONFIG_XFRM_MIGRATE
183cad12 3159static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
5c79de6e
SS
3160{
3161 struct xfrm_user_migrate um;
3162
3163 memset(&um, 0, sizeof(um));
3164 um.proto = m->proto;
3165 um.mode = m->mode;
3166 um.reqid = m->reqid;
3167 um.old_family = m->old_family;
3168 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
3169 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
3170 um.new_family = m->new_family;
3171 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
3172 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
3173
c0144bea 3174 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
5c79de6e
SS
3175}
3176
183cad12 3177static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
13c1d189
AE
3178{
3179 struct xfrm_user_kmaddress uk;
3180
3181 memset(&uk, 0, sizeof(uk));
3182 uk.family = k->family;
3183 uk.reserved = k->reserved;
3184 memcpy(&uk.local, &k->local, sizeof(uk.local));
a1caa322 3185 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
13c1d189
AE
3186
3187 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
3188}
3189
a1b831f2
AD
3190static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
3191 int with_encp)
7deb2264
TG
3192{
3193 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
13c1d189 3194 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
8bafd730 3195 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
13c1d189
AE
3196 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
3197 + userpolicy_type_attrsize();
7deb2264
TG
3198}
3199
183cad12
DM
3200static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
3201 int num_migrate, const struct xfrm_kmaddress *k,
8bafd730
AA
3202 const struct xfrm_selector *sel,
3203 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
5c79de6e 3204{
183cad12 3205 const struct xfrm_migrate *mp;
5c79de6e
SS
3206 struct xfrm_userpolicy_id *pol_id;
3207 struct nlmsghdr *nlh;
1d1e34dd 3208 int i, err;
5c79de6e 3209
79b8b7f4
TG
3210 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
3211 if (nlh == NULL)
3212 return -EMSGSIZE;
5c79de6e 3213
7b67c857 3214 pol_id = nlmsg_data(nlh);
5c79de6e
SS
3215 /* copy data from selector, dir, and type to the pol_id */
3216 memset(pol_id, 0, sizeof(*pol_id));
3217 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
3218 pol_id->dir = dir;
3219
1d1e34dd
DM
3220 if (k != NULL) {
3221 err = copy_to_user_kmaddress(k, skb);
3222 if (err)
3223 goto out_cancel;
3224 }
8bafd730
AA
3225 if (encap) {
3226 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
3227 if (err)
3228 goto out_cancel;
3229 }
1d1e34dd
DM
3230 err = copy_to_user_policy_type(type, skb);
3231 if (err)
3232 goto out_cancel;
5c79de6e 3233 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1d1e34dd
DM
3234 err = copy_to_user_migrate(mp, skb);
3235 if (err)
3236 goto out_cancel;
5c79de6e
SS
3237 }
3238
053c095a
JB
3239 nlmsg_end(skb, nlh);
3240 return 0;
1d1e34dd
DM
3241
3242out_cancel:
9825069d 3243 nlmsg_cancel(skb, nlh);
1d1e34dd 3244 return err;
5c79de6e
SS
3245}
3246
183cad12
DM
3247static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3248 const struct xfrm_migrate *m, int num_migrate,
8bafd730
AA
3249 const struct xfrm_kmaddress *k,
3250 const struct xfrm_encap_tmpl *encap)
5c79de6e 3251{
a6483b79 3252 struct net *net = &init_net;
5c79de6e 3253 struct sk_buff *skb;
2fc5f83b 3254 int err;
5c79de6e 3255
8bafd730
AA
3256 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
3257 GFP_ATOMIC);
5c79de6e
SS
3258 if (skb == NULL)
3259 return -ENOMEM;
3260
3261 /* build migrate */
2fc5f83b
GS
3262 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
3263 BUG_ON(err < 0);
5c79de6e 3264
21ee543e 3265 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
5c79de6e
SS
3266}
3267#else
183cad12
DM
3268static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3269 const struct xfrm_migrate *m, int num_migrate,
8bafd730
AA
3270 const struct xfrm_kmaddress *k,
3271 const struct xfrm_encap_tmpl *encap)
5c79de6e
SS
3272{
3273 return -ENOPROTOOPT;
3274}
3275#endif
d51d081d 3276
a7bd9a45 3277#define XMSGSIZE(type) sizeof(struct type)
492b558b 3278
5461fc0c 3279const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
66f9a259 3280 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
492b558b
TG
3281 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3282 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3283 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
3284 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3285 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3286 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
980ebd25 3287 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
53bc6b4d 3288 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
492b558b 3289 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
66f9a259 3290 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
6c5c8ca7 3291 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
492b558b 3292 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
a7bd9a45 3293 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
d51d081d
JHS
3294 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
3295 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
97a64b45 3296 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
5c79de6e 3297 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
a7bd9a45 3298 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
880a6fab 3299 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
a7bd9a45 3300 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2d151d39
SK
3301 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
3302 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
1da177e4 3303};
5461fc0c 3304EXPORT_SYMBOL_GPL(xfrm_msg_min);
1da177e4 3305
492b558b
TG
3306#undef XMSGSIZE
3307
5106f4a8 3308const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
a4a87fa4 3309 [XFRMA_UNSPEC] = { .strict_start_type = XFRMA_SA_DIR },
c28e9304 3310 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
3311 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
3312 [XFRMA_LASTUSED] = { .type = NLA_U64},
3313 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
1a6509d9 3314 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
cf5cb79f
TG
3315 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
3316 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
3317 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
3318 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
3319 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
d1e0e61d 3320 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
cf5cb79f
TG
3321 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
3322 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
3323 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
3324 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
3325 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
3326 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
3327 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
3328 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
13c1d189 3329 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
6f26b61e 3330 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
35d2856b 3331 [XFRMA_TFCPAD] = { .type = NLA_U32 },
d8647b79 3332 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
a947b0a9 3333 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
d3623099 3334 [XFRMA_PROTO] = { .type = NLA_U8 },
870a2df4 3335 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
d77e38e6 3336 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
9b42c1f1
SK
3337 [XFRMA_SET_MARK] = { .type = NLA_U32 },
3338 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
7e652640 3339 [XFRMA_IF_ID] = { .type = NLA_U32 },
5e242470 3340 [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
a4a87fa4 3341 [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT),
f531d13b 3342 [XFRMA_NAT_KEEPALIVE_INTERVAL] = { .type = NLA_U32 },
1ddf9916 3343 [XFRMA_SA_PCPU] = { .type = NLA_U32 },
f69eb4f6
CH
3344 [XFRMA_IPTFS_DROP_TIME] = { .type = NLA_U32 },
3345 [XFRMA_IPTFS_REORDER_WINDOW] = { .type = NLA_U16 },
3346 [XFRMA_IPTFS_DONT_FRAG] = { .type = NLA_FLAG },
3347 [XFRMA_IPTFS_INIT_DELAY] = { .type = NLA_U32 },
3348 [XFRMA_IPTFS_MAX_QSIZE] = { .type = NLA_U32 },
3349 [XFRMA_IPTFS_PKT_SIZE] = { .type = NLA_U32 },
cf5cb79f 3350};
5106f4a8 3351EXPORT_SYMBOL_GPL(xfrma_policy);
cf5cb79f 3352
880a6fab
CG
3353static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
3354 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3355 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3356};
3357
05600a79 3358static const struct xfrm_link {
3bec6c3e
SD
3359 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
3360 struct netlink_ext_ack *);
1137b5e2 3361 int (*start)(struct netlink_callback *);
1da177e4 3362 int (*dump)(struct sk_buff *, struct netlink_callback *);
4c563f76 3363 int (*done)(struct netlink_callback *);
880a6fab
CG
3364 const struct nla_policy *nla_pol;
3365 int nla_max;
492b558b
TG
3366} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
3367 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
3368 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
3369 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
4c563f76
TT
3370 .dump = xfrm_dump_sa,
3371 .done = xfrm_dump_sa_done },
492b558b
TG
3372 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3373 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
3374 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1137b5e2 3375 .start = xfrm_dump_policy_start,
4c563f76
TT
3376 .dump = xfrm_dump_policy,
3377 .done = xfrm_dump_policy_done },
492b558b 3378 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
980ebd25 3379 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
53bc6b4d 3380 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
492b558b
TG
3381 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3382 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
6c5c8ca7 3383 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
492b558b
TG
3384 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
3385 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
d51d081d
JHS
3386 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
3387 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
5c79de6e 3388 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
566ec034 3389 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
880a6fab
CG
3390 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
3391 .nla_pol = xfrma_spd_policy,
3392 .nla_max = XFRMA_SPD_MAX },
ecfd6b18 3393 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2d151d39
SK
3394 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
3395 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
1da177e4
LT
3396};
3397
451b5096
AA
3398static int xfrm_reject_unused_attr(int type, struct nlattr **attrs,
3399 struct netlink_ext_ack *extack)
3400{
3401 if (attrs[XFRMA_SA_DIR]) {
3402 switch (type) {
3403 case XFRM_MSG_NEWSA:
3404 case XFRM_MSG_UPDSA:
3405 case XFRM_MSG_ALLOCSPI:
3406 break;
3407 default:
3408 NL_SET_ERR_MSG(extack, "Invalid attribute SA_DIR");
3409 return -EINVAL;
3410 }
3411 }
3412
83dfce38
SK
3413 if (attrs[XFRMA_SA_PCPU]) {
3414 switch (type) {
3415 case XFRM_MSG_NEWSA:
3416 case XFRM_MSG_UPDSA:
3417 case XFRM_MSG_ALLOCSPI:
3418 case XFRM_MSG_ACQUIRE:
3419
3420 break;
3421 default:
3422 NL_SET_ERR_MSG(extack, "Invalid attribute SA_PCPU");
3423 return -EINVAL;
3424 }
3425 }
3426
451b5096
AA
3427 return 0;
3428}
3429
2d4bc933
JB
3430static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
3431 struct netlink_ext_ack *extack)
1da177e4 3432{
a6483b79 3433 struct net *net = sock_net(skb->sk);
35a7aa08 3434 struct nlattr *attrs[XFRMA_MAX+1];
05600a79 3435 const struct xfrm_link *link;
5106f4a8 3436 struct nlmsghdr *nlh64 = NULL;
a7bd9a45 3437 int type, err;
1da177e4 3438
1da177e4 3439 type = nlh->nlmsg_type;
1da177e4 3440 if (type > XFRM_MSG_MAX)
1d00a4eb 3441 return -EINVAL;
1da177e4
LT
3442
3443 type -= XFRM_MSG_BASE;
3444 link = &xfrm_dispatch[type];
3445
3446 /* All operations require privileges, even GET */
90f62cf3 3447 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
1d00a4eb 3448 return -EPERM;
1da177e4 3449
5106f4a8
DS
3450 if (in_compat_syscall()) {
3451 struct xfrm_translator *xtr = xfrm_get_translator();
3452
3453 if (!xtr)
3454 return -EOPNOTSUPP;
3455
3456 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
3457 link->nla_pol, extack);
3458 xfrm_put_translator(xtr);
3459 if (IS_ERR(nlh64))
3460 return PTR_ERR(nlh64);
3461 if (nlh64)
3462 nlh = nlh64;
3463 }
3464
492b558b
TG
3465 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
3466 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
b8f3ab42 3467 (nlh->nlmsg_flags & NLM_F_DUMP)) {
5106f4a8
DS
3468 struct netlink_dump_control c = {
3469 .start = link->start,
3470 .dump = link->dump,
3471 .done = link->done,
3472 };
88fc2c84 3473
5106f4a8
DS
3474 if (link->dump == NULL) {
3475 err = -EINVAL;
3476 goto err;
80d326fa 3477 }
5106f4a8
DS
3478
3479 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
3480 goto err;
1da177e4
LT
3481 }
3482
8cb08174
JB
3483 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
3484 link->nla_max ? : XFRMA_MAX,
3485 link->nla_pol ? : xfrma_policy, extack);
a7bd9a45 3486 if (err < 0)
5106f4a8 3487 goto err;
1da177e4 3488
451b5096
AA
3489 if (!link->nla_pol || link->nla_pol == xfrma_policy) {
3490 err = xfrm_reject_unused_attr((type + XFRM_MSG_BASE), attrs, extack);
3491 if (err < 0)
3492 goto err;
3493 }
3494
5106f4a8
DS
3495 if (link->doit == NULL) {
3496 err = -EINVAL;
3497 goto err;
3498 }
3499
3bec6c3e 3500 err = link->doit(skb, nlh, attrs, extack);
1da177e4 3501
7c1a80e8
PS
3502 /* We need to free skb allocated in xfrm_alloc_compat() before
3503 * returning from this function, because consume_skb() won't take
3504 * care of frag_list since netlink destructor sets
3505 * sbk->head to NULL. (see netlink_skb_destructor())
3506 */
3507 if (skb_has_frag_list(skb)) {
3508 kfree_skb(skb_shinfo(skb)->frag_list);
3509 skb_shinfo(skb)->frag_list = NULL;
3510 }
3511
5106f4a8
DS
3512err:
3513 kvfree(nlh64);
3514 return err;
1da177e4
LT
3515}
3516
cd40b7d3 3517static void xfrm_netlink_rcv(struct sk_buff *skb)
1da177e4 3518{
283bc9f3
FD
3519 struct net *net = sock_net(skb->sk);
3520
3521 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
cd40b7d3 3522 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
283bc9f3 3523 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
1da177e4
LT
3524}
3525
a1b831f2 3526static inline unsigned int xfrm_expire_msgsize(void)
7deb2264 3527{
a4a87fa4
AA
3528 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) +
3529 nla_total_size(sizeof(struct xfrm_mark)) +
1ddf9916
SK
3530 nla_total_size(sizeof_field(struct xfrm_state, dir)) +
3531 nla_total_size(4); /* XFRMA_SA_PCPU */
7deb2264
TG
3532}
3533
214e005b 3534static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1da177e4
LT
3535{
3536 struct xfrm_user_expire *ue;
3537 struct nlmsghdr *nlh;
1d1e34dd 3538 int err;
1da177e4 3539
15e47304 3540 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
79b8b7f4
TG
3541 if (nlh == NULL)
3542 return -EMSGSIZE;
1da177e4 3543
7b67c857 3544 ue = nlmsg_data(nlh);
1da177e4 3545 copy_to_user_state(x, &ue->state);
d51d081d 3546 ue->hard = (c->data.hard != 0) ? 1 : 0;
e3e5fc16 3547 /* clear the padding bytes */
caf283d0 3548 memset_after(ue, 0, hard);
1da177e4 3549
1d1e34dd
DM
3550 err = xfrm_mark_put(skb, &x->mark);
3551 if (err)
3552 return err;
6f26b61e 3553
7e652640
SK
3554 err = xfrm_if_id_put(skb, x->if_id);
3555 if (err)
3556 return err;
1ddf9916
SK
3557 if (x->pcpu_num != UINT_MAX) {
3558 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
3559 if (err)
3560 return err;
3561 }
7e652640 3562
a4a87fa4
AA
3563 if (x->dir) {
3564 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
3565 if (err)
3566 return err;
3567 }
3568
053c095a
JB
3569 nlmsg_end(skb, nlh);
3570 return 0;
1da177e4
LT
3571}
3572
214e005b 3573static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
1da177e4 3574{
fc34acd3 3575 struct net *net = xs_net(x);
1da177e4
LT
3576 struct sk_buff *skb;
3577
7deb2264 3578 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1da177e4
LT
3579 if (skb == NULL)
3580 return -ENOMEM;
3581
6f26b61e
JHS
3582 if (build_expire(skb, x, c) < 0) {
3583 kfree_skb(skb);
3584 return -EMSGSIZE;
3585 }
1da177e4 3586
21ee543e 3587 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
1da177e4
LT
3588}
3589
214e005b 3590static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
d51d081d 3591{
fc34acd3 3592 struct net *net = xs_net(x);
d51d081d 3593 struct sk_buff *skb;
2fc5f83b 3594 int err;
d51d081d 3595
d8647b79 3596 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
d51d081d
JHS
3597 if (skb == NULL)
3598 return -ENOMEM;
3599
2fc5f83b
GS
3600 err = build_aevent(skb, x, c);
3601 BUG_ON(err < 0);
d51d081d 3602
21ee543e 3603 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
d51d081d
JHS
3604}
3605
214e005b 3606static int xfrm_notify_sa_flush(const struct km_event *c)
26b15dad 3607{
7067802e 3608 struct net *net = c->net;
26b15dad
JHS
3609 struct xfrm_usersa_flush *p;
3610 struct nlmsghdr *nlh;
3611 struct sk_buff *skb;
7deb2264 3612 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
26b15dad 3613
7deb2264 3614 skb = nlmsg_new(len, GFP_ATOMIC);
26b15dad
JHS
3615 if (skb == NULL)
3616 return -ENOMEM;
26b15dad 3617
15e47304 3618 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
79b8b7f4
TG
3619 if (nlh == NULL) {
3620 kfree_skb(skb);
3621 return -EMSGSIZE;
3622 }
26b15dad 3623
7b67c857 3624 p = nlmsg_data(nlh);
bf08867f 3625 p->proto = c->data.proto;
26b15dad 3626
9825069d 3627 nlmsg_end(skb, nlh);
26b15dad 3628
21ee543e 3629 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
26b15dad
JHS
3630}
3631
a1b831f2 3632static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
26b15dad 3633{
a1b831f2 3634 unsigned int l = 0;
1a6509d9
HX
3635 if (x->aead)
3636 l += nla_total_size(aead_len(x->aead));
4447bb33
MW
3637 if (x->aalg) {
3638 l += nla_total_size(sizeof(struct xfrm_algo) +
3639 (x->aalg->alg_key_len + 7) / 8);
3640 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3641 }
26b15dad 3642 if (x->ealg)
0f99be0d 3643 l += nla_total_size(xfrm_alg_len(x->ealg));
26b15dad 3644 if (x->calg)
7deb2264 3645 l += nla_total_size(sizeof(*x->calg));
26b15dad 3646 if (x->encap)
7deb2264 3647 l += nla_total_size(sizeof(*x->encap));
35d2856b
MW
3648 if (x->tfcpad)
3649 l += nla_total_size(sizeof(x->tfcpad));
d8647b79
SK
3650 if (x->replay_esn)
3651 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
f293a5e3 3652 else
3653 l += nla_total_size(sizeof(struct xfrm_replay_state));
68325d3b
HX
3654 if (x->security)
3655 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3656 x->security->ctx_len);
3657 if (x->coaddr)
3658 l += nla_total_size(sizeof(*x->coaddr));
a947b0a9
ND
3659 if (x->props.extra_flags)
3660 l += nla_total_size(sizeof(x->props.extra_flags));
d77e38e6 3661 if (x->xso.dev)
7770a39d 3662 l += nla_total_size(sizeof(struct xfrm_user_offload));
9b42c1f1
SK
3663 if (x->props.smark.v | x->props.smark.m) {
3664 l += nla_total_size(sizeof(x->props.smark.v));
3665 l += nla_total_size(sizeof(x->props.smark.m));
3666 }
7e652640
SK
3667 if (x->if_id)
3668 l += nla_total_size(sizeof(x->if_id));
1ddf9916
SK
3669 if (x->pcpu_num)
3670 l += nla_total_size(sizeof(x->pcpu_num));
68325d3b 3671
d26f3984 3672 /* Must count x->lastused as it may become non-zero behind our back. */
de95c4a4 3673 l += nla_total_size_64bit(sizeof(u64));
26b15dad 3674
4e484b3e
AA
3675 if (x->mapping_maxage)
3676 l += nla_total_size(sizeof(x->mapping_maxage));
3677
a4a87fa4
AA
3678 if (x->dir)
3679 l += nla_total_size(sizeof(x->dir));
3680
f531d13b
EB
3681 if (x->nat_keepalive_interval)
3682 l += nla_total_size(sizeof(x->nat_keepalive_interval));
3683
7ac64f45
CH
3684 if (x->mode_cbs && x->mode_cbs->sa_len)
3685 l += x->mode_cbs->sa_len(x);
3686
26b15dad
JHS
3687 return l;
3688}
3689
214e005b 3690static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
26b15dad 3691{
fc34acd3 3692 struct net *net = xs_net(x);
26b15dad 3693 struct xfrm_usersa_info *p;
0603eac0 3694 struct xfrm_usersa_id *id;
26b15dad
JHS
3695 struct nlmsghdr *nlh;
3696 struct sk_buff *skb;
a1b831f2
AD
3697 unsigned int len = xfrm_sa_len(x);
3698 unsigned int headlen;
3699 int err;
0603eac0
HX
3700
3701 headlen = sizeof(*p);
3702 if (c->event == XFRM_MSG_DELSA) {
7deb2264 3703 len += nla_total_size(headlen);
0603eac0 3704 headlen = sizeof(*id);
6f26b61e 3705 len += nla_total_size(sizeof(struct xfrm_mark));
0603eac0 3706 }
7deb2264 3707 len += NLMSG_ALIGN(headlen);
26b15dad 3708
7deb2264 3709 skb = nlmsg_new(len, GFP_ATOMIC);
26b15dad
JHS
3710 if (skb == NULL)
3711 return -ENOMEM;
26b15dad 3712
15e47304 3713 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
1d1e34dd 3714 err = -EMSGSIZE;
79b8b7f4 3715 if (nlh == NULL)
1d1e34dd 3716 goto out_free_skb;
26b15dad 3717
7b67c857 3718 p = nlmsg_data(nlh);
0603eac0 3719 if (c->event == XFRM_MSG_DELSA) {
c0144bea
TG
3720 struct nlattr *attr;
3721
7b67c857 3722 id = nlmsg_data(nlh);
50329c8a 3723 memset(id, 0, sizeof(*id));
0603eac0
HX
3724 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3725 id->spi = x->id.spi;
3726 id->family = x->props.family;
3727 id->proto = x->id.proto;
3728
c0144bea 3729 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
1d1e34dd 3730 err = -EMSGSIZE;
c0144bea 3731 if (attr == NULL)
1d1e34dd 3732 goto out_free_skb;
c0144bea
TG
3733
3734 p = nla_data(attr);
0603eac0 3735 }
1d1e34dd
DM
3736 err = copy_to_user_state_extra(x, p, skb);
3737 if (err)
3738 goto out_free_skb;
26b15dad 3739
9825069d 3740 nlmsg_end(skb, nlh);
26b15dad 3741
21ee543e 3742 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
26b15dad 3743
1d1e34dd 3744out_free_skb:
26b15dad 3745 kfree_skb(skb);
1d1e34dd 3746 return err;
26b15dad
JHS
3747}
3748
214e005b 3749static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
26b15dad
JHS
3750{
3751
3752 switch (c->event) {
f60f6b8f 3753 case XFRM_MSG_EXPIRE:
26b15dad 3754 return xfrm_exp_state_notify(x, c);
d51d081d
JHS
3755 case XFRM_MSG_NEWAE:
3756 return xfrm_aevent_state_notify(x, c);
f60f6b8f
HX
3757 case XFRM_MSG_DELSA:
3758 case XFRM_MSG_UPDSA:
3759 case XFRM_MSG_NEWSA:
26b15dad 3760 return xfrm_notify_sa(x, c);
f60f6b8f 3761 case XFRM_MSG_FLUSHSA:
26b15dad
JHS
3762 return xfrm_notify_sa_flush(c);
3763 default:
62db5cfd 3764 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3765 c->event);
3766 break;
26b15dad
JHS
3767 }
3768
3769 return 0;
3770
3771}
3772
a1b831f2
AD
3773static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3774 struct xfrm_policy *xp)
7deb2264
TG
3775{
3776 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3777 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
6f26b61e 3778 + nla_total_size(sizeof(struct xfrm_mark))
7deb2264 3779 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
1ddf9916 3780 + nla_total_size(4) /* XFRMA_SA_PCPU */
7deb2264
TG
3781 + userpolicy_type_attrsize();
3782}
3783
1da177e4 3784static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
65e0736b 3785 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
1da177e4 3786{
1d1e34dd 3787 __u32 seq = xfrm_get_acqseq();
1da177e4
LT
3788 struct xfrm_user_acquire *ua;
3789 struct nlmsghdr *nlh;
1d1e34dd 3790 int err;
1da177e4 3791
79b8b7f4
TG
3792 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3793 if (nlh == NULL)
3794 return -EMSGSIZE;
1da177e4 3795
7b67c857 3796 ua = nlmsg_data(nlh);
1da177e4
LT
3797 memcpy(&ua->id, &x->id, sizeof(ua->id));
3798 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3799 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
65e0736b 3800 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
1da177e4
LT
3801 ua->aalgos = xt->aalgos;
3802 ua->ealgos = xt->ealgos;
3803 ua->calgos = xt->calgos;
3804 ua->seq = x->km.seq = seq;
3805
1d1e34dd
DM
3806 err = copy_to_user_tmpl(xp, skb);
3807 if (!err)
3808 err = copy_to_user_state_sec_ctx(x, skb);
3809 if (!err)
3810 err = copy_to_user_policy_type(xp->type, skb);
3811 if (!err)
3812 err = xfrm_mark_put(skb, &xp->mark);
7e652640
SK
3813 if (!err)
3814 err = xfrm_if_id_put(skb, xp->if_id);
919e43fa
LR
3815 if (!err && xp->xdo.dev)
3816 err = copy_user_offload(&xp->xdo, skb);
1ddf9916
SK
3817 if (!err && x->pcpu_num != UINT_MAX)
3818 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
1d1e34dd
DM
3819 if (err) {
3820 nlmsg_cancel(skb, nlh);
3821 return err;
3822 }
1da177e4 3823
053c095a
JB
3824 nlmsg_end(skb, nlh);
3825 return 0;
1da177e4
LT
3826}
3827
3828static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
65e0736b 3829 struct xfrm_policy *xp)
1da177e4 3830{
a6483b79 3831 struct net *net = xs_net(x);
1da177e4 3832 struct sk_buff *skb;
2fc5f83b 3833 int err;
1da177e4 3834
7deb2264 3835 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
1da177e4
LT
3836 if (skb == NULL)
3837 return -ENOMEM;
3838
2fc5f83b
GS
3839 err = build_acquire(skb, x, xt, xp);
3840 BUG_ON(err < 0);
1da177e4 3841
21ee543e 3842 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
1da177e4
LT
3843}
3844
3845/* User gives us xfrm_user_policy_info followed by an array of 0
3846 * or more templates.
3847 */
cb969f07 3848static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
1da177e4
LT
3849 u8 *data, int len, int *dir)
3850{
fc34acd3 3851 struct net *net = sock_net(sk);
1da177e4
LT
3852 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3853 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3854 struct xfrm_policy *xp;
3855 int nr;
3856
cb969f07 3857 switch (sk->sk_family) {
1da177e4
LT
3858 case AF_INET:
3859 if (opt != IP_XFRM_POLICY) {
3860 *dir = -EOPNOTSUPP;
3861 return NULL;
3862 }
3863 break;
dfd56b8b 3864#if IS_ENABLED(CONFIG_IPV6)
1da177e4
LT
3865 case AF_INET6:
3866 if (opt != IPV6_XFRM_POLICY) {
3867 *dir = -EOPNOTSUPP;
3868 return NULL;
3869 }
3870 break;
3871#endif
3872 default:
3873 *dir = -EINVAL;
3874 return NULL;
3875 }
3876
3877 *dir = -EINVAL;
3878
3879 if (len < sizeof(*p) ||
ec2b4f01 3880 verify_newpolicy_info(p, NULL))
1da177e4
LT
3881 return NULL;
3882
3883 nr = ((len - sizeof(*p)) / sizeof(*ut));
3d776e31 3884 if (validate_tmpl(nr, ut, p->sel.family, p->dir, NULL))
1da177e4
LT
3885 return NULL;
3886
a4f1bac6
HX
3887 if (p->dir > XFRM_POLICY_OUT)
3888 return NULL;
3889
2f09a4d5 3890 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
1da177e4
LT
3891 if (xp == NULL) {
3892 *dir = -ENOBUFS;
3893 return NULL;
3894 }
3895
3896 copy_from_user_policy(xp, p);
f7b6983f 3897 xp->type = XFRM_POLICY_TYPE_MAIN;
1da177e4
LT
3898 copy_templates(xp, ut, nr);
3899
3900 *dir = p->dir;
3901
3902 return xp;
3903}
3904
a1b831f2 3905static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
7deb2264
TG
3906{
3907 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3908 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3909 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
295fae56 3910 + nla_total_size(sizeof(struct xfrm_mark))
7deb2264
TG
3911 + userpolicy_type_attrsize();
3912}
3913
1da177e4 3914static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
214e005b 3915 int dir, const struct km_event *c)
1da177e4
LT
3916{
3917 struct xfrm_user_polexpire *upe;
d51d081d 3918 int hard = c->data.hard;
1d1e34dd
DM
3919 struct nlmsghdr *nlh;
3920 int err;
1da177e4 3921
15e47304 3922 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
79b8b7f4
TG
3923 if (nlh == NULL)
3924 return -EMSGSIZE;
1da177e4 3925
7b67c857 3926 upe = nlmsg_data(nlh);
1da177e4 3927 copy_to_user_policy(xp, &upe->pol, dir);
1d1e34dd
DM
3928 err = copy_to_user_tmpl(xp, skb);
3929 if (!err)
3930 err = copy_to_user_sec_ctx(xp, skb);
3931 if (!err)
3932 err = copy_to_user_policy_type(xp->type, skb);
3933 if (!err)
3934 err = xfrm_mark_put(skb, &xp->mark);
7e652640
SK
3935 if (!err)
3936 err = xfrm_if_id_put(skb, xp->if_id);
919e43fa
LR
3937 if (!err && xp->xdo.dev)
3938 err = copy_user_offload(&xp->xdo, skb);
1d1e34dd
DM
3939 if (err) {
3940 nlmsg_cancel(skb, nlh);
3941 return err;
3942 }
1da177e4
LT
3943 upe->hard = !!hard;
3944
053c095a
JB
3945 nlmsg_end(skb, nlh);
3946 return 0;
1da177e4
LT
3947}
3948
214e005b 3949static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1da177e4 3950{
fc34acd3 3951 struct net *net = xp_net(xp);
1da177e4 3952 struct sk_buff *skb;
2fc5f83b 3953 int err;
1da177e4 3954
7deb2264 3955 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
1da177e4
LT
3956 if (skb == NULL)
3957 return -ENOMEM;
3958
2fc5f83b
GS
3959 err = build_polexpire(skb, xp, dir, c);
3960 BUG_ON(err < 0);
1da177e4 3961
21ee543e 3962 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
1da177e4
LT
3963}
3964
214e005b 3965static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
26b15dad 3966{
a1b831f2 3967 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
fc34acd3 3968 struct net *net = xp_net(xp);
26b15dad 3969 struct xfrm_userpolicy_info *p;
0603eac0 3970 struct xfrm_userpolicy_id *id;
26b15dad
JHS
3971 struct nlmsghdr *nlh;
3972 struct sk_buff *skb;
a1b831f2
AD
3973 unsigned int headlen;
3974 int err;
0603eac0
HX
3975
3976 headlen = sizeof(*p);
3977 if (c->event == XFRM_MSG_DELPOLICY) {
7deb2264 3978 len += nla_total_size(headlen);
0603eac0
HX
3979 headlen = sizeof(*id);
3980 }
cfbfd45a 3981 len += userpolicy_type_attrsize();
295fae56 3982 len += nla_total_size(sizeof(struct xfrm_mark));
7deb2264 3983 len += NLMSG_ALIGN(headlen);
26b15dad 3984
7deb2264 3985 skb = nlmsg_new(len, GFP_ATOMIC);
26b15dad
JHS
3986 if (skb == NULL)
3987 return -ENOMEM;
26b15dad 3988
15e47304 3989 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
1d1e34dd 3990 err = -EMSGSIZE;
79b8b7f4 3991 if (nlh == NULL)
1d1e34dd 3992 goto out_free_skb;
26b15dad 3993
7b67c857 3994 p = nlmsg_data(nlh);
0603eac0 3995 if (c->event == XFRM_MSG_DELPOLICY) {
c0144bea
TG
3996 struct nlattr *attr;
3997
7b67c857 3998 id = nlmsg_data(nlh);
0603eac0
HX
3999 memset(id, 0, sizeof(*id));
4000 id->dir = dir;
4001 if (c->data.byid)
4002 id->index = xp->index;
4003 else
4004 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
4005
c0144bea 4006 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
1d1e34dd 4007 err = -EMSGSIZE;
c0144bea 4008 if (attr == NULL)
1d1e34dd 4009 goto out_free_skb;
c0144bea
TG
4010
4011 p = nla_data(attr);
0603eac0 4012 }
26b15dad 4013
26b15dad 4014 copy_to_user_policy(xp, p, dir);
1d1e34dd
DM
4015 err = copy_to_user_tmpl(xp, skb);
4016 if (!err)
4017 err = copy_to_user_policy_type(xp->type, skb);
4018 if (!err)
4019 err = xfrm_mark_put(skb, &xp->mark);
7e652640
SK
4020 if (!err)
4021 err = xfrm_if_id_put(skb, xp->if_id);
919e43fa
LR
4022 if (!err && xp->xdo.dev)
4023 err = copy_user_offload(&xp->xdo, skb);
1d1e34dd
DM
4024 if (err)
4025 goto out_free_skb;
295fae56 4026
9825069d 4027 nlmsg_end(skb, nlh);
26b15dad 4028
21ee543e 4029 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
26b15dad 4030
1d1e34dd 4031out_free_skb:
26b15dad 4032 kfree_skb(skb);
1d1e34dd 4033 return err;
26b15dad
JHS
4034}
4035
214e005b 4036static int xfrm_notify_policy_flush(const struct km_event *c)
26b15dad 4037{
7067802e 4038 struct net *net = c->net;
26b15dad
JHS
4039 struct nlmsghdr *nlh;
4040 struct sk_buff *skb;
1d1e34dd 4041 int err;
26b15dad 4042
7deb2264 4043 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
26b15dad
JHS
4044 if (skb == NULL)
4045 return -ENOMEM;
26b15dad 4046
15e47304 4047 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
1d1e34dd 4048 err = -EMSGSIZE;
79b8b7f4 4049 if (nlh == NULL)
1d1e34dd
DM
4050 goto out_free_skb;
4051 err = copy_to_user_policy_type(c->data.type, skb);
4052 if (err)
4053 goto out_free_skb;
26b15dad 4054
9825069d 4055 nlmsg_end(skb, nlh);
26b15dad 4056
21ee543e 4057 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
26b15dad 4058
1d1e34dd 4059out_free_skb:
26b15dad 4060 kfree_skb(skb);
1d1e34dd 4061 return err;
26b15dad
JHS
4062}
4063
214e005b 4064static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
26b15dad
JHS
4065{
4066
4067 switch (c->event) {
f60f6b8f
HX
4068 case XFRM_MSG_NEWPOLICY:
4069 case XFRM_MSG_UPDPOLICY:
4070 case XFRM_MSG_DELPOLICY:
26b15dad 4071 return xfrm_notify_policy(xp, dir, c);
f60f6b8f 4072 case XFRM_MSG_FLUSHPOLICY:
26b15dad 4073 return xfrm_notify_policy_flush(c);
f60f6b8f 4074 case XFRM_MSG_POLEXPIRE:
26b15dad
JHS
4075 return xfrm_exp_policy_notify(xp, dir, c);
4076 default:
62db5cfd 4077 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
4078 c->event);
26b15dad
JHS
4079 }
4080
4081 return 0;
4082
4083}
4084
a1b831f2 4085static inline unsigned int xfrm_report_msgsize(void)
7deb2264
TG
4086{
4087 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
4088}
4089
97a64b45
MN
4090static int build_report(struct sk_buff *skb, u8 proto,
4091 struct xfrm_selector *sel, xfrm_address_t *addr)
4092{
4093 struct xfrm_user_report *ur;
4094 struct nlmsghdr *nlh;
97a64b45 4095
79b8b7f4
TG
4096 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
4097 if (nlh == NULL)
4098 return -EMSGSIZE;
97a64b45 4099
7b67c857 4100 ur = nlmsg_data(nlh);
97a64b45
MN
4101 ur->proto = proto;
4102 memcpy(&ur->sel, sel, sizeof(ur->sel));
4103
1d1e34dd
DM
4104 if (addr) {
4105 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
4106 if (err) {
4107 nlmsg_cancel(skb, nlh);
4108 return err;
4109 }
4110 }
053c095a
JB
4111 nlmsg_end(skb, nlh);
4112 return 0;
97a64b45
MN
4113}
4114
db983c11
AD
4115static int xfrm_send_report(struct net *net, u8 proto,
4116 struct xfrm_selector *sel, xfrm_address_t *addr)
97a64b45
MN
4117{
4118 struct sk_buff *skb;
2fc5f83b 4119 int err;
97a64b45 4120
7deb2264 4121 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
97a64b45
MN
4122 if (skb == NULL)
4123 return -ENOMEM;
4124
2fc5f83b
GS
4125 err = build_report(skb, proto, sel, addr);
4126 BUG_ON(err < 0);
97a64b45 4127
21ee543e 4128 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
97a64b45
MN
4129}
4130
a1b831f2 4131static inline unsigned int xfrm_mapping_msgsize(void)
3a2dfbe8
MW
4132{
4133 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
4134}
4135
4136static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
4137 xfrm_address_t *new_saddr, __be16 new_sport)
4138{
4139 struct xfrm_user_mapping *um;
4140 struct nlmsghdr *nlh;
4141
4142 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
4143 if (nlh == NULL)
4144 return -EMSGSIZE;
4145
4146 um = nlmsg_data(nlh);
4147
4148 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
4149 um->id.spi = x->id.spi;
4150 um->id.family = x->props.family;
4151 um->id.proto = x->id.proto;
4152 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
4153 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
4154 um->new_sport = new_sport;
4155 um->old_sport = x->encap->encap_sport;
4156 um->reqid = x->props.reqid;
4157
053c095a
JB
4158 nlmsg_end(skb, nlh);
4159 return 0;
3a2dfbe8
MW
4160}
4161
4162static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
4163 __be16 sport)
4164{
a6483b79 4165 struct net *net = xs_net(x);
3a2dfbe8 4166 struct sk_buff *skb;
2fc5f83b 4167 int err;
3a2dfbe8
MW
4168
4169 if (x->id.proto != IPPROTO_ESP)
4170 return -EINVAL;
4171
4172 if (!x->encap)
4173 return -EINVAL;
4174
4175 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
4176 if (skb == NULL)
4177 return -ENOMEM;
4178
2fc5f83b
GS
4179 err = build_mapping(skb, x, ipaddr, sport);
4180 BUG_ON(err < 0);
3a2dfbe8 4181
21ee543e 4182 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3a2dfbe8
MW
4183}
4184
0f24558e
HG
4185static bool xfrm_is_alive(const struct km_event *c)
4186{
4187 return (bool)xfrm_acquire_is_on(c->net);
4188}
4189
1da177e4 4190static struct xfrm_mgr netlink_mgr = {
1da177e4
LT
4191 .notify = xfrm_send_state_notify,
4192 .acquire = xfrm_send_acquire,
4193 .compile_policy = xfrm_compile_policy,
4194 .notify_policy = xfrm_send_policy_notify,
97a64b45 4195 .report = xfrm_send_report,
5c79de6e 4196 .migrate = xfrm_send_migrate,
3a2dfbe8 4197 .new_mapping = xfrm_send_mapping,
0f24558e 4198 .is_alive = xfrm_is_alive,
1da177e4
LT
4199};
4200
a6483b79 4201static int __net_init xfrm_user_net_init(struct net *net)
1da177e4 4202{
be33690d 4203 struct sock *nlsk;
a31f2d17
PNA
4204 struct netlink_kernel_cfg cfg = {
4205 .groups = XFRMNLGRP_MAX,
4206 .input = xfrm_netlink_rcv,
4207 };
be33690d 4208
9f00d977 4209 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
be33690d 4210 if (nlsk == NULL)
1da177e4 4211 return -ENOMEM;
d79d792e 4212 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
cf778b00 4213 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
1da177e4
LT
4214 return 0;
4215}
4216
6218fe18
FW
4217static void __net_exit xfrm_user_net_pre_exit(struct net *net)
4218{
4219 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
4220}
4221
d79d792e 4222static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
1da177e4 4223{
d79d792e 4224 struct net *net;
6218fe18 4225
d79d792e
EB
4226 list_for_each_entry(net, net_exit_list, exit_list)
4227 netlink_kernel_release(net->xfrm.nlsk_stash);
1da177e4
LT
4228}
4229
a6483b79 4230static struct pernet_operations xfrm_user_net_ops = {
d79d792e 4231 .init = xfrm_user_net_init,
6218fe18 4232 .pre_exit = xfrm_user_net_pre_exit,
d79d792e 4233 .exit_batch = xfrm_user_net_exit,
a6483b79
AD
4234};
4235
4236static int __init xfrm_user_init(void)
4237{
4238 int rv;
4239
4240 printk(KERN_INFO "Initializing XFRM netlink socket\n");
4241
4242 rv = register_pernet_subsys(&xfrm_user_net_ops);
4243 if (rv < 0)
4244 return rv;
f41b284a
ZS
4245 xfrm_register_km(&netlink_mgr);
4246 return 0;
a6483b79
AD
4247}
4248
4249static void __exit xfrm_user_exit(void)
4250{
4251 xfrm_unregister_km(&netlink_mgr);
4252 unregister_pernet_subsys(&xfrm_user_net_ops);
4253}
4254
1da177e4
LT
4255module_init(xfrm_user_init);
4256module_exit(xfrm_user_exit);
2599bb5e 4257MODULE_DESCRIPTION("XFRM User interface");
1da177e4 4258MODULE_LICENSE("GPL");
4fdb3bb7 4259MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);