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