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