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 35static 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 61static 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 77static 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 93static 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 102static 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 117static 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 150static 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 262out: 263 return err; 264} 265 266static 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 292static 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 322static 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 352static 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 377static 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 395static 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 428static 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 439static 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*/ 461static 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 502static 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 582error: 583 x->km.state = XFRM_STATE_DEAD; 584 xfrm_state_put(x); 585error_no_put: 586 *errp = err; 587 return NULL; 588} 589 590static 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); 626out: 627 xfrm_state_put(x); 628 return err; 629} 630 631static 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 665static 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 696out: 697 xfrm_audit_state_delete(x, err ? 0 : 1, true); 698 xfrm_state_put(x); 699 return err; 700} 701 702static 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 719struct 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 726static 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 747static 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! */ 766static 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 if (ret) 832 goto out; 833 } 834 if (x->security) 835 ret = copy_sec_ctx(x->security, skb); 836out: 837 return ret; 838} 839 840static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 841{ 842 struct xfrm_dump_info *sp = ptr; 843 struct sk_buff *in_skb = sp->in_skb; 844 struct sk_buff *skb = sp->out_skb; 845 struct xfrm_usersa_info *p; 846 struct nlmsghdr *nlh; 847 int err; 848 849 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 850 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 851 if (nlh == NULL) 852 return -EMSGSIZE; 853 854 p = nlmsg_data(nlh); 855 856 err = copy_to_user_state_extra(x, p, skb); 857 if (err) { 858 nlmsg_cancel(skb, nlh); 859 return err; 860 } 861 nlmsg_end(skb, nlh); 862 return 0; 863} 864 865static int xfrm_dump_sa_done(struct netlink_callback *cb) 866{ 867 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 868 struct sock *sk = cb->skb->sk; 869 struct net *net = sock_net(sk); 870 871 xfrm_state_walk_done(walk, net); 872 return 0; 873} 874 875static const struct nla_policy xfrma_policy[XFRMA_MAX+1]; 876static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 877{ 878 struct net *net = sock_net(skb->sk); 879 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 880 struct xfrm_dump_info info; 881 882 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 883 sizeof(cb->args) - sizeof(cb->args[0])); 884 885 info.in_skb = cb->skb; 886 info.out_skb = skb; 887 info.nlmsg_seq = cb->nlh->nlmsg_seq; 888 info.nlmsg_flags = NLM_F_MULTI; 889 890 if (!cb->args[0]) { 891 struct nlattr *attrs[XFRMA_MAX+1]; 892 struct xfrm_address_filter *filter = NULL; 893 u8 proto = 0; 894 int err; 895 896 cb->args[0] = 1; 897 898 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, 899 xfrma_policy); 900 if (err < 0) 901 return err; 902 903 if (attrs[XFRMA_ADDRESS_FILTER]) { 904 filter = kmalloc(sizeof(*filter), GFP_KERNEL); 905 if (filter == NULL) 906 return -ENOMEM; 907 908 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]), 909 sizeof(*filter)); 910 } 911 912 if (attrs[XFRMA_PROTO]) 913 proto = nla_get_u8(attrs[XFRMA_PROTO]); 914 915 xfrm_state_walk_init(walk, proto, filter); 916 } 917 918 (void) xfrm_state_walk(net, walk, dump_one_state, &info); 919 920 return skb->len; 921} 922 923static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 924 struct xfrm_state *x, u32 seq) 925{ 926 struct xfrm_dump_info info; 927 struct sk_buff *skb; 928 int err; 929 930 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 931 if (!skb) 932 return ERR_PTR(-ENOMEM); 933 934 info.in_skb = in_skb; 935 info.out_skb = skb; 936 info.nlmsg_seq = seq; 937 info.nlmsg_flags = 0; 938 939 err = dump_one_state(x, 0, &info); 940 if (err) { 941 kfree_skb(skb); 942 return ERR_PTR(err); 943 } 944 945 return skb; 946} 947 948/* A wrapper for nlmsg_multicast() checking that nlsk is still available. 949 * Must be called with RCU read lock. 950 */ 951static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, 952 u32 pid, unsigned int group) 953{ 954 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); 955 956 if (nlsk) 957 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); 958 else 959 return -1; 960} 961 962static inline size_t xfrm_spdinfo_msgsize(void) 963{ 964 return NLMSG_ALIGN(4) 965 + nla_total_size(sizeof(struct xfrmu_spdinfo)) 966 + nla_total_size(sizeof(struct xfrmu_spdhinfo)) 967 + nla_total_size(sizeof(struct xfrmu_spdhthresh)) 968 + nla_total_size(sizeof(struct xfrmu_spdhthresh)); 969} 970 971static int build_spdinfo(struct sk_buff *skb, struct net *net, 972 u32 portid, u32 seq, u32 flags) 973{ 974 struct xfrmk_spdinfo si; 975 struct xfrmu_spdinfo spc; 976 struct xfrmu_spdhinfo sph; 977 struct xfrmu_spdhthresh spt4, spt6; 978 struct nlmsghdr *nlh; 979 int err; 980 u32 *f; 981 unsigned lseq; 982 983 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 984 if (nlh == NULL) /* shouldn't really happen ... */ 985 return -EMSGSIZE; 986 987 f = nlmsg_data(nlh); 988 *f = flags; 989 xfrm_spd_getinfo(net, &si); 990 spc.incnt = si.incnt; 991 spc.outcnt = si.outcnt; 992 spc.fwdcnt = si.fwdcnt; 993 spc.inscnt = si.inscnt; 994 spc.outscnt = si.outscnt; 995 spc.fwdscnt = si.fwdscnt; 996 sph.spdhcnt = si.spdhcnt; 997 sph.spdhmcnt = si.spdhmcnt; 998 999 do { 1000 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock); 1001 1002 spt4.lbits = net->xfrm.policy_hthresh.lbits4; 1003 spt4.rbits = net->xfrm.policy_hthresh.rbits4; 1004 spt6.lbits = net->xfrm.policy_hthresh.lbits6; 1005 spt6.rbits = net->xfrm.policy_hthresh.rbits6; 1006 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq)); 1007 1008 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 1009 if (!err) 1010 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 1011 if (!err) 1012 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4); 1013 if (!err) 1014 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6); 1015 if (err) { 1016 nlmsg_cancel(skb, nlh); 1017 return err; 1018 } 1019 1020 return nlmsg_end(skb, nlh); 1021} 1022 1023static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1024 struct nlattr **attrs) 1025{ 1026 struct net *net = sock_net(skb->sk); 1027 struct xfrmu_spdhthresh *thresh4 = NULL; 1028 struct xfrmu_spdhthresh *thresh6 = NULL; 1029 1030 /* selector prefixlen thresholds to hash policies */ 1031 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) { 1032 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH]; 1033 1034 if (nla_len(rta) < sizeof(*thresh4)) 1035 return -EINVAL; 1036 thresh4 = nla_data(rta); 1037 if (thresh4->lbits > 32 || thresh4->rbits > 32) 1038 return -EINVAL; 1039 } 1040 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1041 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1042 1043 if (nla_len(rta) < sizeof(*thresh6)) 1044 return -EINVAL; 1045 thresh6 = nla_data(rta); 1046 if (thresh6->lbits > 128 || thresh6->rbits > 128) 1047 return -EINVAL; 1048 } 1049 1050 if (thresh4 || thresh6) { 1051 write_seqlock(&net->xfrm.policy_hthresh.lock); 1052 if (thresh4) { 1053 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1054 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1055 } 1056 if (thresh6) { 1057 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1058 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1059 } 1060 write_sequnlock(&net->xfrm.policy_hthresh.lock); 1061 1062 xfrm_policy_hash_rebuild(net); 1063 } 1064 1065 return 0; 1066} 1067 1068static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1069 struct nlattr **attrs) 1070{ 1071 struct net *net = sock_net(skb->sk); 1072 struct sk_buff *r_skb; 1073 u32 *flags = nlmsg_data(nlh); 1074 u32 sportid = NETLINK_CB(skb).portid; 1075 u32 seq = nlh->nlmsg_seq; 1076 1077 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1078 if (r_skb == NULL) 1079 return -ENOMEM; 1080 1081 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0) 1082 BUG(); 1083 1084 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1085} 1086 1087static inline size_t xfrm_sadinfo_msgsize(void) 1088{ 1089 return NLMSG_ALIGN(4) 1090 + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 1091 + nla_total_size(4); /* XFRMA_SAD_CNT */ 1092} 1093 1094static int build_sadinfo(struct sk_buff *skb, struct net *net, 1095 u32 portid, u32 seq, u32 flags) 1096{ 1097 struct xfrmk_sadinfo si; 1098 struct xfrmu_sadhinfo sh; 1099 struct nlmsghdr *nlh; 1100 int err; 1101 u32 *f; 1102 1103 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 1104 if (nlh == NULL) /* shouldn't really happen ... */ 1105 return -EMSGSIZE; 1106 1107 f = nlmsg_data(nlh); 1108 *f = flags; 1109 xfrm_sad_getinfo(net, &si); 1110 1111 sh.sadhmcnt = si.sadhmcnt; 1112 sh.sadhcnt = si.sadhcnt; 1113 1114 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 1115 if (!err) 1116 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 1117 if (err) { 1118 nlmsg_cancel(skb, nlh); 1119 return err; 1120 } 1121 1122 return nlmsg_end(skb, nlh); 1123} 1124 1125static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1126 struct nlattr **attrs) 1127{ 1128 struct net *net = sock_net(skb->sk); 1129 struct sk_buff *r_skb; 1130 u32 *flags = nlmsg_data(nlh); 1131 u32 sportid = NETLINK_CB(skb).portid; 1132 u32 seq = nlh->nlmsg_seq; 1133 1134 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 1135 if (r_skb == NULL) 1136 return -ENOMEM; 1137 1138 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0) 1139 BUG(); 1140 1141 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1142} 1143 1144static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 1145 struct nlattr **attrs) 1146{ 1147 struct net *net = sock_net(skb->sk); 1148 struct xfrm_usersa_id *p = nlmsg_data(nlh); 1149 struct xfrm_state *x; 1150 struct sk_buff *resp_skb; 1151 int err = -ESRCH; 1152 1153 x = xfrm_user_state_lookup(net, p, attrs, &err); 1154 if (x == NULL) 1155 goto out_noput; 1156 1157 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 1158 if (IS_ERR(resp_skb)) { 1159 err = PTR_ERR(resp_skb); 1160 } else { 1161 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 1162 } 1163 xfrm_state_put(x); 1164out_noput: 1165 return err; 1166} 1167 1168static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 1169 struct nlattr **attrs) 1170{ 1171 struct net *net = sock_net(skb->sk); 1172 struct xfrm_state *x; 1173 struct xfrm_userspi_info *p; 1174 struct sk_buff *resp_skb; 1175 xfrm_address_t *daddr; 1176 int family; 1177 int err; 1178 u32 mark; 1179 struct xfrm_mark m; 1180 1181 p = nlmsg_data(nlh); 1182 err = verify_spi_info(p->info.id.proto, p->min, p->max); 1183 if (err) 1184 goto out_noput; 1185 1186 family = p->info.family; 1187 daddr = &p->info.id.daddr; 1188 1189 x = NULL; 1190 1191 mark = xfrm_mark_get(attrs, &m); 1192 if (p->info.seq) { 1193 x = xfrm_find_acq_byseq(net, mark, p->info.seq); 1194 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 1195 xfrm_state_put(x); 1196 x = NULL; 1197 } 1198 } 1199 1200 if (!x) 1201 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 1202 p->info.id.proto, daddr, 1203 &p->info.saddr, 1, 1204 family); 1205 err = -ENOENT; 1206 if (x == NULL) 1207 goto out_noput; 1208 1209 err = xfrm_alloc_spi(x, p->min, p->max); 1210 if (err) 1211 goto out; 1212 1213 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 1214 if (IS_ERR(resp_skb)) { 1215 err = PTR_ERR(resp_skb); 1216 goto out; 1217 } 1218 1219 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 1220 1221out: 1222 xfrm_state_put(x); 1223out_noput: 1224 return err; 1225} 1226 1227static int verify_policy_dir(u8 dir) 1228{ 1229 switch (dir) { 1230 case XFRM_POLICY_IN: 1231 case XFRM_POLICY_OUT: 1232 case XFRM_POLICY_FWD: 1233 break; 1234 1235 default: 1236 return -EINVAL; 1237 } 1238 1239 return 0; 1240} 1241 1242static int verify_policy_type(u8 type) 1243{ 1244 switch (type) { 1245 case XFRM_POLICY_TYPE_MAIN: 1246#ifdef CONFIG_XFRM_SUB_POLICY 1247 case XFRM_POLICY_TYPE_SUB: 1248#endif 1249 break; 1250 1251 default: 1252 return -EINVAL; 1253 } 1254 1255 return 0; 1256} 1257 1258static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 1259{ 1260 int ret; 1261 1262 switch (p->share) { 1263 case XFRM_SHARE_ANY: 1264 case XFRM_SHARE_SESSION: 1265 case XFRM_SHARE_USER: 1266 case XFRM_SHARE_UNIQUE: 1267 break; 1268 1269 default: 1270 return -EINVAL; 1271 } 1272 1273 switch (p->action) { 1274 case XFRM_POLICY_ALLOW: 1275 case XFRM_POLICY_BLOCK: 1276 break; 1277 1278 default: 1279 return -EINVAL; 1280 } 1281 1282 switch (p->sel.family) { 1283 case AF_INET: 1284 break; 1285 1286 case AF_INET6: 1287#if IS_ENABLED(CONFIG_IPV6) 1288 break; 1289#else 1290 return -EAFNOSUPPORT; 1291#endif 1292 1293 default: 1294 return -EINVAL; 1295 } 1296 1297 ret = verify_policy_dir(p->dir); 1298 if (ret) 1299 return ret; 1300 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir)) 1301 return -EINVAL; 1302 1303 return 0; 1304} 1305 1306static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1307{ 1308 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1309 struct xfrm_user_sec_ctx *uctx; 1310 1311 if (!rt) 1312 return 0; 1313 1314 uctx = nla_data(rt); 1315 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1316} 1317 1318static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 1319 int nr) 1320{ 1321 int i; 1322 1323 xp->xfrm_nr = nr; 1324 for (i = 0; i < nr; i++, ut++) { 1325 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 1326 1327 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 1328 memcpy(&t->saddr, &ut->saddr, 1329 sizeof(xfrm_address_t)); 1330 t->reqid = ut->reqid; 1331 t->mode = ut->mode; 1332 t->share = ut->share; 1333 t->optional = ut->optional; 1334 t->aalgos = ut->aalgos; 1335 t->ealgos = ut->ealgos; 1336 t->calgos = ut->calgos; 1337 /* If all masks are ~0, then we allow all algorithms. */ 1338 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 1339 t->encap_family = ut->family; 1340 } 1341} 1342 1343static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 1344{ 1345 int i; 1346 1347 if (nr > XFRM_MAX_DEPTH) 1348 return -EINVAL; 1349 1350 for (i = 0; i < nr; i++) { 1351 /* We never validated the ut->family value, so many 1352 * applications simply leave it at zero. The check was 1353 * never made and ut->family was ignored because all 1354 * templates could be assumed to have the same family as 1355 * the policy itself. Now that we will have ipv4-in-ipv6 1356 * and ipv6-in-ipv4 tunnels, this is no longer true. 1357 */ 1358 if (!ut[i].family) 1359 ut[i].family = family; 1360 1361 switch (ut[i].family) { 1362 case AF_INET: 1363 break; 1364#if IS_ENABLED(CONFIG_IPV6) 1365 case AF_INET6: 1366 break; 1367#endif 1368 default: 1369 return -EINVAL; 1370 } 1371 } 1372 1373 return 0; 1374} 1375 1376static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) 1377{ 1378 struct nlattr *rt = attrs[XFRMA_TMPL]; 1379 1380 if (!rt) { 1381 pol->xfrm_nr = 0; 1382 } else { 1383 struct xfrm_user_tmpl *utmpl = nla_data(rt); 1384 int nr = nla_len(rt) / sizeof(*utmpl); 1385 int err; 1386 1387 err = validate_tmpl(nr, utmpl, pol->family); 1388 if (err) 1389 return err; 1390 1391 copy_templates(pol, utmpl, nr); 1392 } 1393 return 0; 1394} 1395 1396static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) 1397{ 1398 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1399 struct xfrm_userpolicy_type *upt; 1400 u8 type = XFRM_POLICY_TYPE_MAIN; 1401 int err; 1402 1403 if (rt) { 1404 upt = nla_data(rt); 1405 type = upt->type; 1406 } 1407 1408 err = verify_policy_type(type); 1409 if (err) 1410 return err; 1411 1412 *tp = type; 1413 return 0; 1414} 1415 1416static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 1417{ 1418 xp->priority = p->priority; 1419 xp->index = p->index; 1420 memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 1421 memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 1422 xp->action = p->action; 1423 xp->flags = p->flags; 1424 xp->family = p->sel.family; 1425 /* XXX xp->share = p->share; */ 1426} 1427 1428static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 1429{ 1430 memset(p, 0, sizeof(*p)); 1431 memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 1432 memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 1433 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 1434 p->priority = xp->priority; 1435 p->index = xp->index; 1436 p->sel.family = xp->family; 1437 p->dir = dir; 1438 p->action = xp->action; 1439 p->flags = xp->flags; 1440 p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 1441} 1442 1443static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) 1444{ 1445 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 1446 int err; 1447 1448 if (!xp) { 1449 *errp = -ENOMEM; 1450 return NULL; 1451 } 1452 1453 copy_from_user_policy(xp, p); 1454 1455 err = copy_from_user_policy_type(&xp->type, attrs); 1456 if (err) 1457 goto error; 1458 1459 if (!(err = copy_from_user_tmpl(xp, attrs))) 1460 err = copy_from_user_sec_ctx(xp, attrs); 1461 if (err) 1462 goto error; 1463 1464 xfrm_mark_get(attrs, &xp->mark); 1465 1466 return xp; 1467 error: 1468 *errp = err; 1469 xp->walk.dead = 1; 1470 xfrm_policy_destroy(xp); 1471 return NULL; 1472} 1473 1474static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1475 struct nlattr **attrs) 1476{ 1477 struct net *net = sock_net(skb->sk); 1478 struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 1479 struct xfrm_policy *xp; 1480 struct km_event c; 1481 int err; 1482 int excl; 1483 1484 err = verify_newpolicy_info(p); 1485 if (err) 1486 return err; 1487 err = verify_sec_ctx_len(attrs); 1488 if (err) 1489 return err; 1490 1491 xp = xfrm_policy_construct(net, p, attrs, &err); 1492 if (!xp) 1493 return err; 1494 1495 /* shouldn't excl be based on nlh flags?? 1496 * Aha! this is anti-netlink really i.e more pfkey derived 1497 * in netlink excl is a flag and you wouldnt need 1498 * a type XFRM_MSG_UPDPOLICY - JHS */ 1499 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 1500 err = xfrm_policy_insert(p->dir, xp, excl); 1501 xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1502 1503 if (err) { 1504 security_xfrm_policy_free(xp->security); 1505 kfree(xp); 1506 return err; 1507 } 1508 1509 c.event = nlh->nlmsg_type; 1510 c.seq = nlh->nlmsg_seq; 1511 c.portid = nlh->nlmsg_pid; 1512 km_policy_notify(xp, p->dir, &c); 1513 1514 xfrm_pol_put(xp); 1515 1516 return 0; 1517} 1518 1519static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 1520{ 1521 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 1522 int i; 1523 1524 if (xp->xfrm_nr == 0) 1525 return 0; 1526 1527 for (i = 0; i < xp->xfrm_nr; i++) { 1528 struct xfrm_user_tmpl *up = &vec[i]; 1529 struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 1530 1531 memset(up, 0, sizeof(*up)); 1532 memcpy(&up->id, &kp->id, sizeof(up->id)); 1533 up->family = kp->encap_family; 1534 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 1535 up->reqid = kp->reqid; 1536 up->mode = kp->mode; 1537 up->share = kp->share; 1538 up->optional = kp->optional; 1539 up->aalgos = kp->aalgos; 1540 up->ealgos = kp->ealgos; 1541 up->calgos = kp->calgos; 1542 } 1543 1544 return nla_put(skb, XFRMA_TMPL, 1545 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1546} 1547 1548static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 1549{ 1550 if (x->security) { 1551 return copy_sec_ctx(x->security, skb); 1552 } 1553 return 0; 1554} 1555 1556static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 1557{ 1558 if (xp->security) 1559 return copy_sec_ctx(xp->security, skb); 1560 return 0; 1561} 1562static inline size_t userpolicy_type_attrsize(void) 1563{ 1564#ifdef CONFIG_XFRM_SUB_POLICY 1565 return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1566#else 1567 return 0; 1568#endif 1569} 1570 1571#ifdef CONFIG_XFRM_SUB_POLICY 1572static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1573{ 1574 struct xfrm_userpolicy_type upt = { 1575 .type = type, 1576 }; 1577 1578 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1579} 1580 1581#else 1582static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1583{ 1584 return 0; 1585} 1586#endif 1587 1588static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 1589{ 1590 struct xfrm_dump_info *sp = ptr; 1591 struct xfrm_userpolicy_info *p; 1592 struct sk_buff *in_skb = sp->in_skb; 1593 struct sk_buff *skb = sp->out_skb; 1594 struct nlmsghdr *nlh; 1595 int err; 1596 1597 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 1598 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 1599 if (nlh == NULL) 1600 return -EMSGSIZE; 1601 1602 p = nlmsg_data(nlh); 1603 copy_to_user_policy(xp, p, dir); 1604 err = copy_to_user_tmpl(xp, skb); 1605 if (!err) 1606 err = copy_to_user_sec_ctx(xp, skb); 1607 if (!err) 1608 err = copy_to_user_policy_type(xp->type, skb); 1609 if (!err) 1610 err = xfrm_mark_put(skb, &xp->mark); 1611 if (err) { 1612 nlmsg_cancel(skb, nlh); 1613 return err; 1614 } 1615 nlmsg_end(skb, nlh); 1616 return 0; 1617} 1618 1619static int xfrm_dump_policy_done(struct netlink_callback *cb) 1620{ 1621 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 1622 struct net *net = sock_net(cb->skb->sk); 1623 1624 xfrm_policy_walk_done(walk, net); 1625 return 0; 1626} 1627 1628static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 1629{ 1630 struct net *net = sock_net(skb->sk); 1631 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 1632 struct xfrm_dump_info info; 1633 1634 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > 1635 sizeof(cb->args) - sizeof(cb->args[0])); 1636 1637 info.in_skb = cb->skb; 1638 info.out_skb = skb; 1639 info.nlmsg_seq = cb->nlh->nlmsg_seq; 1640 info.nlmsg_flags = NLM_F_MULTI; 1641 1642 if (!cb->args[0]) { 1643 cb->args[0] = 1; 1644 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 1645 } 1646 1647 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 1648 1649 return skb->len; 1650} 1651 1652static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 1653 struct xfrm_policy *xp, 1654 int dir, u32 seq) 1655{ 1656 struct xfrm_dump_info info; 1657 struct sk_buff *skb; 1658 int err; 1659 1660 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1661 if (!skb) 1662 return ERR_PTR(-ENOMEM); 1663 1664 info.in_skb = in_skb; 1665 info.out_skb = skb; 1666 info.nlmsg_seq = seq; 1667 info.nlmsg_flags = 0; 1668 1669 err = dump_one_policy(xp, dir, 0, &info); 1670 if (err) { 1671 kfree_skb(skb); 1672 return ERR_PTR(err); 1673 } 1674 1675 return skb; 1676} 1677 1678static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1679 struct nlattr **attrs) 1680{ 1681 struct net *net = sock_net(skb->sk); 1682 struct xfrm_policy *xp; 1683 struct xfrm_userpolicy_id *p; 1684 u8 type = XFRM_POLICY_TYPE_MAIN; 1685 int err; 1686 struct km_event c; 1687 int delete; 1688 struct xfrm_mark m; 1689 u32 mark = xfrm_mark_get(attrs, &m); 1690 1691 p = nlmsg_data(nlh); 1692 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 1693 1694 err = copy_from_user_policy_type(&type, attrs); 1695 if (err) 1696 return err; 1697 1698 err = verify_policy_dir(p->dir); 1699 if (err) 1700 return err; 1701 1702 if (p->index) 1703 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); 1704 else { 1705 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1706 struct xfrm_sec_ctx *ctx; 1707 1708 err = verify_sec_ctx_len(attrs); 1709 if (err) 1710 return err; 1711 1712 ctx = NULL; 1713 if (rt) { 1714 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1715 1716 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 1717 if (err) 1718 return err; 1719 } 1720 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, 1721 ctx, delete, &err); 1722 security_xfrm_policy_free(ctx); 1723 } 1724 if (xp == NULL) 1725 return -ENOENT; 1726 1727 if (!delete) { 1728 struct sk_buff *resp_skb; 1729 1730 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 1731 if (IS_ERR(resp_skb)) { 1732 err = PTR_ERR(resp_skb); 1733 } else { 1734 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 1735 NETLINK_CB(skb).portid); 1736 } 1737 } else { 1738 xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 1739 1740 if (err != 0) 1741 goto out; 1742 1743 c.data.byid = p->index; 1744 c.event = nlh->nlmsg_type; 1745 c.seq = nlh->nlmsg_seq; 1746 c.portid = nlh->nlmsg_pid; 1747 km_policy_notify(xp, p->dir, &c); 1748 } 1749 1750out: 1751 xfrm_pol_put(xp); 1752 if (delete && err == 0) 1753 xfrm_garbage_collect(net); 1754 return err; 1755} 1756 1757static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 1758 struct nlattr **attrs) 1759{ 1760 struct net *net = sock_net(skb->sk); 1761 struct km_event c; 1762 struct xfrm_usersa_flush *p = nlmsg_data(nlh); 1763 int err; 1764 1765 err = xfrm_state_flush(net, p->proto, true); 1766 if (err) { 1767 if (err == -ESRCH) /* empty table */ 1768 return 0; 1769 return err; 1770 } 1771 c.data.proto = p->proto; 1772 c.event = nlh->nlmsg_type; 1773 c.seq = nlh->nlmsg_seq; 1774 c.portid = nlh->nlmsg_pid; 1775 c.net = net; 1776 km_state_notify(NULL, &c); 1777 1778 return 0; 1779} 1780 1781static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x) 1782{ 1783 size_t replay_size = x->replay_esn ? 1784 xfrm_replay_state_esn_len(x->replay_esn) : 1785 sizeof(struct xfrm_replay_state); 1786 1787 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 1788 + nla_total_size(replay_size) 1789 + nla_total_size(sizeof(struct xfrm_lifetime_cur)) 1790 + nla_total_size(sizeof(struct xfrm_mark)) 1791 + nla_total_size(4) /* XFRM_AE_RTHR */ 1792 + nla_total_size(4); /* XFRM_AE_ETHR */ 1793} 1794 1795static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 1796{ 1797 struct xfrm_aevent_id *id; 1798 struct nlmsghdr *nlh; 1799 int err; 1800 1801 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 1802 if (nlh == NULL) 1803 return -EMSGSIZE; 1804 1805 id = nlmsg_data(nlh); 1806 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 1807 id->sa_id.spi = x->id.spi; 1808 id->sa_id.family = x->props.family; 1809 id->sa_id.proto = x->id.proto; 1810 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 1811 id->reqid = x->props.reqid; 1812 id->flags = c->data.aevent; 1813 1814 if (x->replay_esn) { 1815 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1816 xfrm_replay_state_esn_len(x->replay_esn), 1817 x->replay_esn); 1818 } else { 1819 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1820 &x->replay); 1821 } 1822 if (err) 1823 goto out_cancel; 1824 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); 1825 if (err) 1826 goto out_cancel; 1827 1828 if (id->flags & XFRM_AE_RTHR) { 1829 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 1830 if (err) 1831 goto out_cancel; 1832 } 1833 if (id->flags & XFRM_AE_ETHR) { 1834 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 1835 x->replay_maxage * 10 / HZ); 1836 if (err) 1837 goto out_cancel; 1838 } 1839 err = xfrm_mark_put(skb, &x->mark); 1840 if (err) 1841 goto out_cancel; 1842 1843 return nlmsg_end(skb, nlh); 1844 1845out_cancel: 1846 nlmsg_cancel(skb, nlh); 1847 return err; 1848} 1849 1850static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1851 struct nlattr **attrs) 1852{ 1853 struct net *net = sock_net(skb->sk); 1854 struct xfrm_state *x; 1855 struct sk_buff *r_skb; 1856 int err; 1857 struct km_event c; 1858 u32 mark; 1859 struct xfrm_mark m; 1860 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1861 struct xfrm_usersa_id *id = &p->sa_id; 1862 1863 mark = xfrm_mark_get(attrs, &m); 1864 1865 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 1866 if (x == NULL) 1867 return -ESRCH; 1868 1869 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 1870 if (r_skb == NULL) { 1871 xfrm_state_put(x); 1872 return -ENOMEM; 1873 } 1874 1875 /* 1876 * XXX: is this lock really needed - none of the other 1877 * gets lock (the concern is things getting updated 1878 * while we are still reading) - jhs 1879 */ 1880 spin_lock_bh(&x->lock); 1881 c.data.aevent = p->flags; 1882 c.seq = nlh->nlmsg_seq; 1883 c.portid = nlh->nlmsg_pid; 1884 1885 if (build_aevent(r_skb, x, &c) < 0) 1886 BUG(); 1887 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 1888 spin_unlock_bh(&x->lock); 1889 xfrm_state_put(x); 1890 return err; 1891} 1892 1893static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1894 struct nlattr **attrs) 1895{ 1896 struct net *net = sock_net(skb->sk); 1897 struct xfrm_state *x; 1898 struct km_event c; 1899 int err = -EINVAL; 1900 u32 mark = 0; 1901 struct xfrm_mark m; 1902 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1903 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 1904 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 1905 struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 1906 1907 if (!lt && !rp && !re) 1908 return err; 1909 1910 /* pedantic mode - thou shalt sayeth replaceth */ 1911 if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1912 return err; 1913 1914 mark = xfrm_mark_get(attrs, &m); 1915 1916 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1917 if (x == NULL) 1918 return -ESRCH; 1919 1920 if (x->km.state != XFRM_STATE_VALID) 1921 goto out; 1922 1923 err = xfrm_replay_verify_len(x->replay_esn, re); 1924 if (err) 1925 goto out; 1926 1927 spin_lock_bh(&x->lock); 1928 xfrm_update_ae_params(x, attrs, 1); 1929 spin_unlock_bh(&x->lock); 1930 1931 c.event = nlh->nlmsg_type; 1932 c.seq = nlh->nlmsg_seq; 1933 c.portid = nlh->nlmsg_pid; 1934 c.data.aevent = XFRM_AE_CU; 1935 km_state_notify(x, &c); 1936 err = 0; 1937out: 1938 xfrm_state_put(x); 1939 return err; 1940} 1941 1942static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1943 struct nlattr **attrs) 1944{ 1945 struct net *net = sock_net(skb->sk); 1946 struct km_event c; 1947 u8 type = XFRM_POLICY_TYPE_MAIN; 1948 int err; 1949 1950 err = copy_from_user_policy_type(&type, attrs); 1951 if (err) 1952 return err; 1953 1954 err = xfrm_policy_flush(net, type, true); 1955 if (err) { 1956 if (err == -ESRCH) /* empty table */ 1957 return 0; 1958 return err; 1959 } 1960 1961 c.data.type = type; 1962 c.event = nlh->nlmsg_type; 1963 c.seq = nlh->nlmsg_seq; 1964 c.portid = nlh->nlmsg_pid; 1965 c.net = net; 1966 km_policy_notify(NULL, 0, &c); 1967 return 0; 1968} 1969 1970static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 1971 struct nlattr **attrs) 1972{ 1973 struct net *net = sock_net(skb->sk); 1974 struct xfrm_policy *xp; 1975 struct xfrm_user_polexpire *up = nlmsg_data(nlh); 1976 struct xfrm_userpolicy_info *p = &up->pol; 1977 u8 type = XFRM_POLICY_TYPE_MAIN; 1978 int err = -ENOENT; 1979 struct xfrm_mark m; 1980 u32 mark = xfrm_mark_get(attrs, &m); 1981 1982 err = copy_from_user_policy_type(&type, attrs); 1983 if (err) 1984 return err; 1985 1986 err = verify_policy_dir(p->dir); 1987 if (err) 1988 return err; 1989 1990 if (p->index) 1991 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); 1992 else { 1993 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1994 struct xfrm_sec_ctx *ctx; 1995 1996 err = verify_sec_ctx_len(attrs); 1997 if (err) 1998 return err; 1999 2000 ctx = NULL; 2001 if (rt) { 2002 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2003 2004 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 2005 if (err) 2006 return err; 2007 } 2008 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, 2009 &p->sel, ctx, 0, &err); 2010 security_xfrm_policy_free(ctx); 2011 } 2012 if (xp == NULL) 2013 return -ENOENT; 2014 2015 if (unlikely(xp->walk.dead)) 2016 goto out; 2017 2018 err = 0; 2019 if (up->hard) { 2020 xfrm_policy_delete(xp, p->dir); 2021 xfrm_audit_policy_delete(xp, 1, true); 2022 } else { 2023 // reset the timers here? 2024 WARN(1, "Dont know what to do with soft policy expire\n"); 2025 } 2026 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 2027 2028out: 2029 xfrm_pol_put(xp); 2030 return err; 2031} 2032 2033static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 2034 struct nlattr **attrs) 2035{ 2036 struct net *net = sock_net(skb->sk); 2037 struct xfrm_state *x; 2038 int err; 2039 struct xfrm_user_expire *ue = nlmsg_data(nlh); 2040 struct xfrm_usersa_info *p = &ue->state; 2041 struct xfrm_mark m; 2042 u32 mark = xfrm_mark_get(attrs, &m); 2043 2044 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 2045 2046 err = -ENOENT; 2047 if (x == NULL) 2048 return err; 2049 2050 spin_lock_bh(&x->lock); 2051 err = -EINVAL; 2052 if (x->km.state != XFRM_STATE_VALID) 2053 goto out; 2054 km_state_expired(x, ue->hard, nlh->nlmsg_pid); 2055 2056 if (ue->hard) { 2057 __xfrm_state_delete(x); 2058 xfrm_audit_state_delete(x, 1, true); 2059 } 2060 err = 0; 2061out: 2062 spin_unlock_bh(&x->lock); 2063 xfrm_state_put(x); 2064 return err; 2065} 2066 2067static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 2068 struct nlattr **attrs) 2069{ 2070 struct net *net = sock_net(skb->sk); 2071 struct xfrm_policy *xp; 2072 struct xfrm_user_tmpl *ut; 2073 int i; 2074 struct nlattr *rt = attrs[XFRMA_TMPL]; 2075 struct xfrm_mark mark; 2076 2077 struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2078 struct xfrm_state *x = xfrm_state_alloc(net); 2079 int err = -ENOMEM; 2080 2081 if (!x) 2082 goto nomem; 2083 2084 xfrm_mark_get(attrs, &mark); 2085 2086 err = verify_newpolicy_info(&ua->policy); 2087 if (err) 2088 goto bad_policy; 2089 2090 /* build an XP */ 2091 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 2092 if (!xp) 2093 goto free_state; 2094 2095 memcpy(&x->id, &ua->id, sizeof(ua->id)); 2096 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2097 memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 2098 xp->mark.m = x->mark.m = mark.m; 2099 xp->mark.v = x->mark.v = mark.v; 2100 ut = nla_data(rt); 2101 /* extract the templates and for each call km_key */ 2102 for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2103 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2104 memcpy(&x->id, &t->id, sizeof(x->id)); 2105 x->props.mode = t->mode; 2106 x->props.reqid = t->reqid; 2107 x->props.family = ut->family; 2108 t->aalgos = ua->aalgos; 2109 t->ealgos = ua->ealgos; 2110 t->calgos = ua->calgos; 2111 err = km_query(x, t, xp); 2112 2113 } 2114 2115 kfree(x); 2116 kfree(xp); 2117 2118 return 0; 2119 2120bad_policy: 2121 WARN(1, "BAD policy passed\n"); 2122free_state: 2123 kfree(x); 2124nomem: 2125 return err; 2126} 2127 2128#ifdef CONFIG_XFRM_MIGRATE 2129static int copy_from_user_migrate(struct xfrm_migrate *ma, 2130 struct xfrm_kmaddress *k, 2131 struct nlattr **attrs, int *num) 2132{ 2133 struct nlattr *rt = attrs[XFRMA_MIGRATE]; 2134 struct xfrm_user_migrate *um; 2135 int i, num_migrate; 2136 2137 if (k != NULL) { 2138 struct xfrm_user_kmaddress *uk; 2139 2140 uk = nla_data(attrs[XFRMA_KMADDRESS]); 2141 memcpy(&k->local, &uk->local, sizeof(k->local)); 2142 memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 2143 k->family = uk->family; 2144 k->reserved = uk->reserved; 2145 } 2146 2147 um = nla_data(rt); 2148 num_migrate = nla_len(rt) / sizeof(*um); 2149 2150 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 2151 return -EINVAL; 2152 2153 for (i = 0; i < num_migrate; i++, um++, ma++) { 2154 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 2155 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 2156 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 2157 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 2158 2159 ma->proto = um->proto; 2160 ma->mode = um->mode; 2161 ma->reqid = um->reqid; 2162 2163 ma->old_family = um->old_family; 2164 ma->new_family = um->new_family; 2165 } 2166 2167 *num = i; 2168 return 0; 2169} 2170 2171static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2172 struct nlattr **attrs) 2173{ 2174 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 2175 struct xfrm_migrate m[XFRM_MAX_DEPTH]; 2176 struct xfrm_kmaddress km, *kmp; 2177 u8 type; 2178 int err; 2179 int n = 0; 2180 struct net *net = sock_net(skb->sk); 2181 2182 if (attrs[XFRMA_MIGRATE] == NULL) 2183 return -EINVAL; 2184 2185 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 2186 2187 err = copy_from_user_policy_type(&type, attrs); 2188 if (err) 2189 return err; 2190 2191 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 2192 if (err) 2193 return err; 2194 2195 if (!n) 2196 return 0; 2197 2198 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net); 2199 2200 return 0; 2201} 2202#else 2203static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2204 struct nlattr **attrs) 2205{ 2206 return -ENOPROTOOPT; 2207} 2208#endif 2209 2210#ifdef CONFIG_XFRM_MIGRATE 2211static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 2212{ 2213 struct xfrm_user_migrate um; 2214 2215 memset(&um, 0, sizeof(um)); 2216 um.proto = m->proto; 2217 um.mode = m->mode; 2218 um.reqid = m->reqid; 2219 um.old_family = m->old_family; 2220 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 2221 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 2222 um.new_family = m->new_family; 2223 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 2224 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 2225 2226 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 2227} 2228 2229static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 2230{ 2231 struct xfrm_user_kmaddress uk; 2232 2233 memset(&uk, 0, sizeof(uk)); 2234 uk.family = k->family; 2235 uk.reserved = k->reserved; 2236 memcpy(&uk.local, &k->local, sizeof(uk.local)); 2237 memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 2238 2239 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 2240} 2241 2242static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) 2243{ 2244 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 2245 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 2246 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 2247 + userpolicy_type_attrsize(); 2248} 2249 2250static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2251 int num_migrate, const struct xfrm_kmaddress *k, 2252 const struct xfrm_selector *sel, u8 dir, u8 type) 2253{ 2254 const struct xfrm_migrate *mp; 2255 struct xfrm_userpolicy_id *pol_id; 2256 struct nlmsghdr *nlh; 2257 int i, err; 2258 2259 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 2260 if (nlh == NULL) 2261 return -EMSGSIZE; 2262 2263 pol_id = nlmsg_data(nlh); 2264 /* copy data from selector, dir, and type to the pol_id */ 2265 memset(pol_id, 0, sizeof(*pol_id)); 2266 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 2267 pol_id->dir = dir; 2268 2269 if (k != NULL) { 2270 err = copy_to_user_kmaddress(k, skb); 2271 if (err) 2272 goto out_cancel; 2273 } 2274 err = copy_to_user_policy_type(type, skb); 2275 if (err) 2276 goto out_cancel; 2277 for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 2278 err = copy_to_user_migrate(mp, skb); 2279 if (err) 2280 goto out_cancel; 2281 } 2282 2283 return nlmsg_end(skb, nlh); 2284 2285out_cancel: 2286 nlmsg_cancel(skb, nlh); 2287 return err; 2288} 2289 2290static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2291 const struct xfrm_migrate *m, int num_migrate, 2292 const struct xfrm_kmaddress *k) 2293{ 2294 struct net *net = &init_net; 2295 struct sk_buff *skb; 2296 2297 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); 2298 if (skb == NULL) 2299 return -ENOMEM; 2300 2301 /* build migrate */ 2302 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) 2303 BUG(); 2304 2305 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 2306} 2307#else 2308static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2309 const struct xfrm_migrate *m, int num_migrate, 2310 const struct xfrm_kmaddress *k) 2311{ 2312 return -ENOPROTOOPT; 2313} 2314#endif 2315 2316#define XMSGSIZE(type) sizeof(struct type) 2317 2318static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 2319 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2320 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2321 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2322 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2323 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2324 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2325 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2326 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 2327 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2328 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2329 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2330 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2331 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2332 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2333 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2334 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2335 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 2336 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2337 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2338 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2339 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2340}; 2341 2342#undef XMSGSIZE 2343 2344static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2345 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2346 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2347 [XFRMA_LASTUSED] = { .type = NLA_U64}, 2348 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 2349 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2350 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2351 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2352 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2353 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2354 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2355 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2356 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2357 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2358 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2359 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2360 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2361 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2362 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2363 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 2364 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 2365 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 2366 [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2367 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2368 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2369 [XFRMA_PROTO] = { .type = NLA_U8 }, 2370 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2371}; 2372 2373static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2374 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2375 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2376}; 2377 2378static const struct xfrm_link { 2379 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 2380 int (*dump)(struct sk_buff *, struct netlink_callback *); 2381 int (*done)(struct netlink_callback *); 2382 const struct nla_policy *nla_pol; 2383 int nla_max; 2384} xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2385 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2386 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2387 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 2388 .dump = xfrm_dump_sa, 2389 .done = xfrm_dump_sa_done }, 2390 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2391 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2392 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 2393 .dump = xfrm_dump_policy, 2394 .done = xfrm_dump_policy_done }, 2395 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2396 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 2397 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2398 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2399 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2400 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2401 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2402 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2403 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2404 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 2405 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 2406 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2407 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 2408 .nla_pol = xfrma_spd_policy, 2409 .nla_max = XFRMA_SPD_MAX }, 2410 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 2411}; 2412 2413static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 2414{ 2415 struct net *net = sock_net(skb->sk); 2416 struct nlattr *attrs[XFRMA_MAX+1]; 2417 const struct xfrm_link *link; 2418 int type, err; 2419 2420 type = nlh->nlmsg_type; 2421 if (type > XFRM_MSG_MAX) 2422 return -EINVAL; 2423 2424 type -= XFRM_MSG_BASE; 2425 link = &xfrm_dispatch[type]; 2426 2427 /* All operations require privileges, even GET */ 2428 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 2429 return -EPERM; 2430 2431 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2432 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2433 (nlh->nlmsg_flags & NLM_F_DUMP)) { 2434 if (link->dump == NULL) 2435 return -EINVAL; 2436 2437 { 2438 struct netlink_dump_control c = { 2439 .dump = link->dump, 2440 .done = link->done, 2441 }; 2442 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 2443 } 2444 } 2445 2446 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, 2447 link->nla_max ? : XFRMA_MAX, 2448 link->nla_pol ? : xfrma_policy); 2449 if (err < 0) 2450 return err; 2451 2452 if (link->doit == NULL) 2453 return -EINVAL; 2454 2455 return link->doit(skb, nlh, attrs); 2456} 2457 2458static void xfrm_netlink_rcv(struct sk_buff *skb) 2459{ 2460 struct net *net = sock_net(skb->sk); 2461 2462 mutex_lock(&net->xfrm.xfrm_cfg_mutex); 2463 netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 2464 mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 2465} 2466 2467static inline size_t xfrm_expire_msgsize(void) 2468{ 2469 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 2470 + nla_total_size(sizeof(struct xfrm_mark)); 2471} 2472 2473static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2474{ 2475 struct xfrm_user_expire *ue; 2476 struct nlmsghdr *nlh; 2477 int err; 2478 2479 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 2480 if (nlh == NULL) 2481 return -EMSGSIZE; 2482 2483 ue = nlmsg_data(nlh); 2484 copy_to_user_state(x, &ue->state); 2485 ue->hard = (c->data.hard != 0) ? 1 : 0; 2486 2487 err = xfrm_mark_put(skb, &x->mark); 2488 if (err) 2489 return err; 2490 2491 return nlmsg_end(skb, nlh); 2492} 2493 2494static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 2495{ 2496 struct net *net = xs_net(x); 2497 struct sk_buff *skb; 2498 2499 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 2500 if (skb == NULL) 2501 return -ENOMEM; 2502 2503 if (build_expire(skb, x, c) < 0) { 2504 kfree_skb(skb); 2505 return -EMSGSIZE; 2506 } 2507 2508 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 2509} 2510 2511static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 2512{ 2513 struct net *net = xs_net(x); 2514 struct sk_buff *skb; 2515 2516 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2517 if (skb == NULL) 2518 return -ENOMEM; 2519 2520 if (build_aevent(skb, x, c) < 0) 2521 BUG(); 2522 2523 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 2524} 2525 2526static int xfrm_notify_sa_flush(const struct km_event *c) 2527{ 2528 struct net *net = c->net; 2529 struct xfrm_usersa_flush *p; 2530 struct nlmsghdr *nlh; 2531 struct sk_buff *skb; 2532 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 2533 2534 skb = nlmsg_new(len, GFP_ATOMIC); 2535 if (skb == NULL) 2536 return -ENOMEM; 2537 2538 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 2539 if (nlh == NULL) { 2540 kfree_skb(skb); 2541 return -EMSGSIZE; 2542 } 2543 2544 p = nlmsg_data(nlh); 2545 p->proto = c->data.proto; 2546 2547 nlmsg_end(skb, nlh); 2548 2549 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 2550} 2551 2552static inline size_t xfrm_sa_len(struct xfrm_state *x) 2553{ 2554 size_t l = 0; 2555 if (x->aead) 2556 l += nla_total_size(aead_len(x->aead)); 2557 if (x->aalg) { 2558 l += nla_total_size(sizeof(struct xfrm_algo) + 2559 (x->aalg->alg_key_len + 7) / 8); 2560 l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 2561 } 2562 if (x->ealg) 2563 l += nla_total_size(xfrm_alg_len(x->ealg)); 2564 if (x->calg) 2565 l += nla_total_size(sizeof(*x->calg)); 2566 if (x->encap) 2567 l += nla_total_size(sizeof(*x->encap)); 2568 if (x->tfcpad) 2569 l += nla_total_size(sizeof(x->tfcpad)); 2570 if (x->replay_esn) 2571 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 2572 if (x->security) 2573 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 2574 x->security->ctx_len); 2575 if (x->coaddr) 2576 l += nla_total_size(sizeof(*x->coaddr)); 2577 if (x->props.extra_flags) 2578 l += nla_total_size(sizeof(x->props.extra_flags)); 2579 2580 /* Must count x->lastused as it may become non-zero behind our back. */ 2581 l += nla_total_size(sizeof(u64)); 2582 2583 return l; 2584} 2585 2586static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 2587{ 2588 struct net *net = xs_net(x); 2589 struct xfrm_usersa_info *p; 2590 struct xfrm_usersa_id *id; 2591 struct nlmsghdr *nlh; 2592 struct sk_buff *skb; 2593 int len = xfrm_sa_len(x); 2594 int headlen, err; 2595 2596 headlen = sizeof(*p); 2597 if (c->event == XFRM_MSG_DELSA) { 2598 len += nla_total_size(headlen); 2599 headlen = sizeof(*id); 2600 len += nla_total_size(sizeof(struct xfrm_mark)); 2601 } 2602 len += NLMSG_ALIGN(headlen); 2603 2604 skb = nlmsg_new(len, GFP_ATOMIC); 2605 if (skb == NULL) 2606 return -ENOMEM; 2607 2608 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 2609 err = -EMSGSIZE; 2610 if (nlh == NULL) 2611 goto out_free_skb; 2612 2613 p = nlmsg_data(nlh); 2614 if (c->event == XFRM_MSG_DELSA) { 2615 struct nlattr *attr; 2616 2617 id = nlmsg_data(nlh); 2618 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 2619 id->spi = x->id.spi; 2620 id->family = x->props.family; 2621 id->proto = x->id.proto; 2622 2623 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 2624 err = -EMSGSIZE; 2625 if (attr == NULL) 2626 goto out_free_skb; 2627 2628 p = nla_data(attr); 2629 } 2630 err = copy_to_user_state_extra(x, p, skb); 2631 if (err) 2632 goto out_free_skb; 2633 2634 nlmsg_end(skb, nlh); 2635 2636 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 2637 2638out_free_skb: 2639 kfree_skb(skb); 2640 return err; 2641} 2642 2643static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 2644{ 2645 2646 switch (c->event) { 2647 case XFRM_MSG_EXPIRE: 2648 return xfrm_exp_state_notify(x, c); 2649 case XFRM_MSG_NEWAE: 2650 return xfrm_aevent_state_notify(x, c); 2651 case XFRM_MSG_DELSA: 2652 case XFRM_MSG_UPDSA: 2653 case XFRM_MSG_NEWSA: 2654 return xfrm_notify_sa(x, c); 2655 case XFRM_MSG_FLUSHSA: 2656 return xfrm_notify_sa_flush(c); 2657 default: 2658 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 2659 c->event); 2660 break; 2661 } 2662 2663 return 0; 2664 2665} 2666 2667static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, 2668 struct xfrm_policy *xp) 2669{ 2670 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 2671 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2672 + nla_total_size(sizeof(struct xfrm_mark)) 2673 + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 2674 + userpolicy_type_attrsize(); 2675} 2676 2677static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 2678 struct xfrm_tmpl *xt, struct xfrm_policy *xp) 2679{ 2680 __u32 seq = xfrm_get_acqseq(); 2681 struct xfrm_user_acquire *ua; 2682 struct nlmsghdr *nlh; 2683 int err; 2684 2685 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 2686 if (nlh == NULL) 2687 return -EMSGSIZE; 2688 2689 ua = nlmsg_data(nlh); 2690 memcpy(&ua->id, &x->id, sizeof(ua->id)); 2691 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 2692 memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 2693 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 2694 ua->aalgos = xt->aalgos; 2695 ua->ealgos = xt->ealgos; 2696 ua->calgos = xt->calgos; 2697 ua->seq = x->km.seq = seq; 2698 2699 err = copy_to_user_tmpl(xp, skb); 2700 if (!err) 2701 err = copy_to_user_state_sec_ctx(x, skb); 2702 if (!err) 2703 err = copy_to_user_policy_type(xp->type, skb); 2704 if (!err) 2705 err = xfrm_mark_put(skb, &xp->mark); 2706 if (err) { 2707 nlmsg_cancel(skb, nlh); 2708 return err; 2709 } 2710 2711 return nlmsg_end(skb, nlh); 2712} 2713 2714static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 2715 struct xfrm_policy *xp) 2716{ 2717 struct net *net = xs_net(x); 2718 struct sk_buff *skb; 2719 2720 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 2721 if (skb == NULL) 2722 return -ENOMEM; 2723 2724 if (build_acquire(skb, x, xt, xp) < 0) 2725 BUG(); 2726 2727 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 2728} 2729 2730/* User gives us xfrm_user_policy_info followed by an array of 0 2731 * or more templates. 2732 */ 2733static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 2734 u8 *data, int len, int *dir) 2735{ 2736 struct net *net = sock_net(sk); 2737 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 2738 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 2739 struct xfrm_policy *xp; 2740 int nr; 2741 2742 switch (sk->sk_family) { 2743 case AF_INET: 2744 if (opt != IP_XFRM_POLICY) { 2745 *dir = -EOPNOTSUPP; 2746 return NULL; 2747 } 2748 break; 2749#if IS_ENABLED(CONFIG_IPV6) 2750 case AF_INET6: 2751 if (opt != IPV6_XFRM_POLICY) { 2752 *dir = -EOPNOTSUPP; 2753 return NULL; 2754 } 2755 break; 2756#endif 2757 default: 2758 *dir = -EINVAL; 2759 return NULL; 2760 } 2761 2762 *dir = -EINVAL; 2763 2764 if (len < sizeof(*p) || 2765 verify_newpolicy_info(p)) 2766 return NULL; 2767 2768 nr = ((len - sizeof(*p)) / sizeof(*ut)); 2769 if (validate_tmpl(nr, ut, p->sel.family)) 2770 return NULL; 2771 2772 if (p->dir > XFRM_POLICY_OUT) 2773 return NULL; 2774 2775 xp = xfrm_policy_alloc(net, GFP_ATOMIC); 2776 if (xp == NULL) { 2777 *dir = -ENOBUFS; 2778 return NULL; 2779 } 2780 2781 copy_from_user_policy(xp, p); 2782 xp->type = XFRM_POLICY_TYPE_MAIN; 2783 copy_templates(xp, ut, nr); 2784 2785 *dir = p->dir; 2786 2787 return xp; 2788} 2789 2790static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) 2791{ 2792 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 2793 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2794 + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 2795 + nla_total_size(sizeof(struct xfrm_mark)) 2796 + userpolicy_type_attrsize(); 2797} 2798 2799static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2800 int dir, const struct km_event *c) 2801{ 2802 struct xfrm_user_polexpire *upe; 2803 int hard = c->data.hard; 2804 struct nlmsghdr *nlh; 2805 int err; 2806 2807 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 2808 if (nlh == NULL) 2809 return -EMSGSIZE; 2810 2811 upe = nlmsg_data(nlh); 2812 copy_to_user_policy(xp, &upe->pol, dir); 2813 err = copy_to_user_tmpl(xp, skb); 2814 if (!err) 2815 err = copy_to_user_sec_ctx(xp, skb); 2816 if (!err) 2817 err = copy_to_user_policy_type(xp->type, skb); 2818 if (!err) 2819 err = xfrm_mark_put(skb, &xp->mark); 2820 if (err) { 2821 nlmsg_cancel(skb, nlh); 2822 return err; 2823 } 2824 upe->hard = !!hard; 2825 2826 return nlmsg_end(skb, nlh); 2827} 2828 2829static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 2830{ 2831 struct net *net = xp_net(xp); 2832 struct sk_buff *skb; 2833 2834 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 2835 if (skb == NULL) 2836 return -ENOMEM; 2837 2838 if (build_polexpire(skb, xp, dir, c) < 0) 2839 BUG(); 2840 2841 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 2842} 2843 2844static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 2845{ 2846 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2847 struct net *net = xp_net(xp); 2848 struct xfrm_userpolicy_info *p; 2849 struct xfrm_userpolicy_id *id; 2850 struct nlmsghdr *nlh; 2851 struct sk_buff *skb; 2852 int headlen, err; 2853 2854 headlen = sizeof(*p); 2855 if (c->event == XFRM_MSG_DELPOLICY) { 2856 len += nla_total_size(headlen); 2857 headlen = sizeof(*id); 2858 } 2859 len += userpolicy_type_attrsize(); 2860 len += nla_total_size(sizeof(struct xfrm_mark)); 2861 len += NLMSG_ALIGN(headlen); 2862 2863 skb = nlmsg_new(len, GFP_ATOMIC); 2864 if (skb == NULL) 2865 return -ENOMEM; 2866 2867 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 2868 err = -EMSGSIZE; 2869 if (nlh == NULL) 2870 goto out_free_skb; 2871 2872 p = nlmsg_data(nlh); 2873 if (c->event == XFRM_MSG_DELPOLICY) { 2874 struct nlattr *attr; 2875 2876 id = nlmsg_data(nlh); 2877 memset(id, 0, sizeof(*id)); 2878 id->dir = dir; 2879 if (c->data.byid) 2880 id->index = xp->index; 2881 else 2882 memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 2883 2884 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 2885 err = -EMSGSIZE; 2886 if (attr == NULL) 2887 goto out_free_skb; 2888 2889 p = nla_data(attr); 2890 } 2891 2892 copy_to_user_policy(xp, p, dir); 2893 err = copy_to_user_tmpl(xp, skb); 2894 if (!err) 2895 err = copy_to_user_policy_type(xp->type, skb); 2896 if (!err) 2897 err = xfrm_mark_put(skb, &xp->mark); 2898 if (err) 2899 goto out_free_skb; 2900 2901 nlmsg_end(skb, nlh); 2902 2903 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 2904 2905out_free_skb: 2906 kfree_skb(skb); 2907 return err; 2908} 2909 2910static int xfrm_notify_policy_flush(const struct km_event *c) 2911{ 2912 struct net *net = c->net; 2913 struct nlmsghdr *nlh; 2914 struct sk_buff *skb; 2915 int err; 2916 2917 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 2918 if (skb == NULL) 2919 return -ENOMEM; 2920 2921 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 2922 err = -EMSGSIZE; 2923 if (nlh == NULL) 2924 goto out_free_skb; 2925 err = copy_to_user_policy_type(c->data.type, skb); 2926 if (err) 2927 goto out_free_skb; 2928 2929 nlmsg_end(skb, nlh); 2930 2931 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 2932 2933out_free_skb: 2934 kfree_skb(skb); 2935 return err; 2936} 2937 2938static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 2939{ 2940 2941 switch (c->event) { 2942 case XFRM_MSG_NEWPOLICY: 2943 case XFRM_MSG_UPDPOLICY: 2944 case XFRM_MSG_DELPOLICY: 2945 return xfrm_notify_policy(xp, dir, c); 2946 case XFRM_MSG_FLUSHPOLICY: 2947 return xfrm_notify_policy_flush(c); 2948 case XFRM_MSG_POLEXPIRE: 2949 return xfrm_exp_policy_notify(xp, dir, c); 2950 default: 2951 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 2952 c->event); 2953 } 2954 2955 return 0; 2956 2957} 2958 2959static inline size_t xfrm_report_msgsize(void) 2960{ 2961 return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 2962} 2963 2964static int build_report(struct sk_buff *skb, u8 proto, 2965 struct xfrm_selector *sel, xfrm_address_t *addr) 2966{ 2967 struct xfrm_user_report *ur; 2968 struct nlmsghdr *nlh; 2969 2970 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 2971 if (nlh == NULL) 2972 return -EMSGSIZE; 2973 2974 ur = nlmsg_data(nlh); 2975 ur->proto = proto; 2976 memcpy(&ur->sel, sel, sizeof(ur->sel)); 2977 2978 if (addr) { 2979 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 2980 if (err) { 2981 nlmsg_cancel(skb, nlh); 2982 return err; 2983 } 2984 } 2985 return nlmsg_end(skb, nlh); 2986} 2987 2988static int xfrm_send_report(struct net *net, u8 proto, 2989 struct xfrm_selector *sel, xfrm_address_t *addr) 2990{ 2991 struct sk_buff *skb; 2992 2993 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 2994 if (skb == NULL) 2995 return -ENOMEM; 2996 2997 if (build_report(skb, proto, sel, addr) < 0) 2998 BUG(); 2999 3000 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 3001} 3002 3003static inline size_t xfrm_mapping_msgsize(void) 3004{ 3005 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 3006} 3007 3008static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 3009 xfrm_address_t *new_saddr, __be16 new_sport) 3010{ 3011 struct xfrm_user_mapping *um; 3012 struct nlmsghdr *nlh; 3013 3014 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 3015 if (nlh == NULL) 3016 return -EMSGSIZE; 3017 3018 um = nlmsg_data(nlh); 3019 3020 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 3021 um->id.spi = x->id.spi; 3022 um->id.family = x->props.family; 3023 um->id.proto = x->id.proto; 3024 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 3025 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 3026 um->new_sport = new_sport; 3027 um->old_sport = x->encap->encap_sport; 3028 um->reqid = x->props.reqid; 3029 3030 return nlmsg_end(skb, nlh); 3031} 3032 3033static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 3034 __be16 sport) 3035{ 3036 struct net *net = xs_net(x); 3037 struct sk_buff *skb; 3038 3039 if (x->id.proto != IPPROTO_ESP) 3040 return -EINVAL; 3041 3042 if (!x->encap) 3043 return -EINVAL; 3044 3045 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 3046 if (skb == NULL) 3047 return -ENOMEM; 3048 3049 if (build_mapping(skb, x, ipaddr, sport) < 0) 3050 BUG(); 3051 3052 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 3053} 3054 3055static bool xfrm_is_alive(const struct km_event *c) 3056{ 3057 return (bool)xfrm_acquire_is_on(c->net); 3058} 3059 3060static struct xfrm_mgr netlink_mgr = { 3061 .id = "netlink", 3062 .notify = xfrm_send_state_notify, 3063 .acquire = xfrm_send_acquire, 3064 .compile_policy = xfrm_compile_policy, 3065 .notify_policy = xfrm_send_policy_notify, 3066 .report = xfrm_send_report, 3067 .migrate = xfrm_send_migrate, 3068 .new_mapping = xfrm_send_mapping, 3069 .is_alive = xfrm_is_alive, 3070}; 3071 3072static int __net_init xfrm_user_net_init(struct net *net) 3073{ 3074 struct sock *nlsk; 3075 struct netlink_kernel_cfg cfg = { 3076 .groups = XFRMNLGRP_MAX, 3077 .input = xfrm_netlink_rcv, 3078 }; 3079 3080 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3081 if (nlsk == NULL) 3082 return -ENOMEM; 3083 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3084 rcu_assign_pointer(net->xfrm.nlsk, nlsk); 3085 return 0; 3086} 3087 3088static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3089{ 3090 struct net *net; 3091 list_for_each_entry(net, net_exit_list, exit_list) 3092 RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 3093 synchronize_net(); 3094 list_for_each_entry(net, net_exit_list, exit_list) 3095 netlink_kernel_release(net->xfrm.nlsk_stash); 3096} 3097 3098static struct pernet_operations xfrm_user_net_ops = { 3099 .init = xfrm_user_net_init, 3100 .exit_batch = xfrm_user_net_exit, 3101}; 3102 3103static int __init xfrm_user_init(void) 3104{ 3105 int rv; 3106 3107 printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3108 3109 rv = register_pernet_subsys(&xfrm_user_net_ops); 3110 if (rv < 0) 3111 return rv; 3112 rv = xfrm_register_km(&netlink_mgr); 3113 if (rv < 0) 3114 unregister_pernet_subsys(&xfrm_user_net_ops); 3115 return rv; 3116} 3117 3118static void __exit xfrm_user_exit(void) 3119{ 3120 xfrm_unregister_km(&netlink_mgr); 3121 unregister_pernet_subsys(&xfrm_user_net_ops); 3122} 3123 3124module_init(xfrm_user_init); 3125module_exit(xfrm_user_exit); 3126MODULE_LICENSE("GPL"); 3127MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3128 3129