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