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