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