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