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