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