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; 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_start(struct netlink_callback *cb) 1704 { 1705 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 1706 1707 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args)); 1708 1709 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 1710 return 0; 1711 } 1712 1713 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 1714 { 1715 struct net *net = sock_net(skb->sk); 1716 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 1717 struct xfrm_dump_info info; 1718 1719 info.in_skb = cb->skb; 1720 info.out_skb = skb; 1721 info.nlmsg_seq = cb->nlh->nlmsg_seq; 1722 info.nlmsg_flags = NLM_F_MULTI; 1723 1724 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 1725 1726 return skb->len; 1727 } 1728 1729 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 1730 struct xfrm_policy *xp, 1731 int dir, u32 seq) 1732 { 1733 struct xfrm_dump_info info; 1734 struct sk_buff *skb; 1735 int err; 1736 1737 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1738 if (!skb) 1739 return ERR_PTR(-ENOMEM); 1740 1741 info.in_skb = in_skb; 1742 info.out_skb = skb; 1743 info.nlmsg_seq = seq; 1744 info.nlmsg_flags = 0; 1745 1746 err = dump_one_policy(xp, dir, 0, &info); 1747 if (err) { 1748 kfree_skb(skb); 1749 return ERR_PTR(err); 1750 } 1751 1752 return skb; 1753 } 1754 1755 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1756 struct nlattr **attrs) 1757 { 1758 struct net *net = sock_net(skb->sk); 1759 struct xfrm_policy *xp; 1760 struct xfrm_userpolicy_id *p; 1761 u8 type = XFRM_POLICY_TYPE_MAIN; 1762 int err; 1763 struct km_event c; 1764 int delete; 1765 struct xfrm_mark m; 1766 u32 mark = xfrm_mark_get(attrs, &m); 1767 1768 p = nlmsg_data(nlh); 1769 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 1770 1771 err = copy_from_user_policy_type(&type, attrs); 1772 if (err) 1773 return err; 1774 1775 err = verify_policy_dir(p->dir); 1776 if (err) 1777 return err; 1778 1779 if (p->index) 1780 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err); 1781 else { 1782 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1783 struct xfrm_sec_ctx *ctx; 1784 1785 err = verify_sec_ctx_len(attrs); 1786 if (err) 1787 return err; 1788 1789 ctx = NULL; 1790 if (rt) { 1791 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1792 1793 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 1794 if (err) 1795 return err; 1796 } 1797 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel, 1798 ctx, delete, &err); 1799 security_xfrm_policy_free(ctx); 1800 } 1801 if (xp == NULL) 1802 return -ENOENT; 1803 1804 if (!delete) { 1805 struct sk_buff *resp_skb; 1806 1807 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 1808 if (IS_ERR(resp_skb)) { 1809 err = PTR_ERR(resp_skb); 1810 } else { 1811 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 1812 NETLINK_CB(skb).portid); 1813 } 1814 } else { 1815 xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 1816 1817 if (err != 0) 1818 goto out; 1819 1820 c.data.byid = p->index; 1821 c.event = nlh->nlmsg_type; 1822 c.seq = nlh->nlmsg_seq; 1823 c.portid = nlh->nlmsg_pid; 1824 km_policy_notify(xp, p->dir, &c); 1825 } 1826 1827 out: 1828 xfrm_pol_put(xp); 1829 return err; 1830 } 1831 1832 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 1833 struct nlattr **attrs) 1834 { 1835 struct net *net = sock_net(skb->sk); 1836 struct km_event c; 1837 struct xfrm_usersa_flush *p = nlmsg_data(nlh); 1838 int err; 1839 1840 err = xfrm_state_flush(net, p->proto, true); 1841 if (err) { 1842 if (err == -ESRCH) /* empty table */ 1843 return 0; 1844 return err; 1845 } 1846 c.data.proto = p->proto; 1847 c.event = nlh->nlmsg_type; 1848 c.seq = nlh->nlmsg_seq; 1849 c.portid = nlh->nlmsg_pid; 1850 c.net = net; 1851 km_state_notify(NULL, &c); 1852 1853 return 0; 1854 } 1855 1856 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x) 1857 { 1858 size_t replay_size = x->replay_esn ? 1859 xfrm_replay_state_esn_len(x->replay_esn) : 1860 sizeof(struct xfrm_replay_state); 1861 1862 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 1863 + nla_total_size(replay_size) 1864 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur)) 1865 + nla_total_size(sizeof(struct xfrm_mark)) 1866 + nla_total_size(4) /* XFRM_AE_RTHR */ 1867 + nla_total_size(4); /* XFRM_AE_ETHR */ 1868 } 1869 1870 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 1871 { 1872 struct xfrm_aevent_id *id; 1873 struct nlmsghdr *nlh; 1874 int err; 1875 1876 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 1877 if (nlh == NULL) 1878 return -EMSGSIZE; 1879 1880 id = nlmsg_data(nlh); 1881 memset(&id->sa_id, 0, sizeof(id->sa_id)); 1882 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 1883 id->sa_id.spi = x->id.spi; 1884 id->sa_id.family = x->props.family; 1885 id->sa_id.proto = x->id.proto; 1886 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 1887 id->reqid = x->props.reqid; 1888 id->flags = c->data.aevent; 1889 1890 if (x->replay_esn) { 1891 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 1892 xfrm_replay_state_esn_len(x->replay_esn), 1893 x->replay_esn); 1894 } else { 1895 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 1896 &x->replay); 1897 } 1898 if (err) 1899 goto out_cancel; 1900 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft, 1901 XFRMA_PAD); 1902 if (err) 1903 goto out_cancel; 1904 1905 if (id->flags & XFRM_AE_RTHR) { 1906 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 1907 if (err) 1908 goto out_cancel; 1909 } 1910 if (id->flags & XFRM_AE_ETHR) { 1911 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 1912 x->replay_maxage * 10 / HZ); 1913 if (err) 1914 goto out_cancel; 1915 } 1916 err = xfrm_mark_put(skb, &x->mark); 1917 if (err) 1918 goto out_cancel; 1919 1920 nlmsg_end(skb, nlh); 1921 return 0; 1922 1923 out_cancel: 1924 nlmsg_cancel(skb, nlh); 1925 return err; 1926 } 1927 1928 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1929 struct nlattr **attrs) 1930 { 1931 struct net *net = sock_net(skb->sk); 1932 struct xfrm_state *x; 1933 struct sk_buff *r_skb; 1934 int err; 1935 struct km_event c; 1936 u32 mark; 1937 struct xfrm_mark m; 1938 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1939 struct xfrm_usersa_id *id = &p->sa_id; 1940 1941 mark = xfrm_mark_get(attrs, &m); 1942 1943 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 1944 if (x == NULL) 1945 return -ESRCH; 1946 1947 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 1948 if (r_skb == NULL) { 1949 xfrm_state_put(x); 1950 return -ENOMEM; 1951 } 1952 1953 /* 1954 * XXX: is this lock really needed - none of the other 1955 * gets lock (the concern is things getting updated 1956 * while we are still reading) - jhs 1957 */ 1958 spin_lock_bh(&x->lock); 1959 c.data.aevent = p->flags; 1960 c.seq = nlh->nlmsg_seq; 1961 c.portid = nlh->nlmsg_pid; 1962 1963 if (build_aevent(r_skb, x, &c) < 0) 1964 BUG(); 1965 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 1966 spin_unlock_bh(&x->lock); 1967 xfrm_state_put(x); 1968 return err; 1969 } 1970 1971 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1972 struct nlattr **attrs) 1973 { 1974 struct net *net = sock_net(skb->sk); 1975 struct xfrm_state *x; 1976 struct km_event c; 1977 int err = -EINVAL; 1978 u32 mark = 0; 1979 struct xfrm_mark m; 1980 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1981 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 1982 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 1983 struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 1984 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 1985 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 1986 1987 if (!lt && !rp && !re && !et && !rt) 1988 return err; 1989 1990 /* pedantic mode - thou shalt sayeth replaceth */ 1991 if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1992 return err; 1993 1994 mark = xfrm_mark_get(attrs, &m); 1995 1996 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1997 if (x == NULL) 1998 return -ESRCH; 1999 2000 if (x->km.state != XFRM_STATE_VALID) 2001 goto out; 2002 2003 err = xfrm_replay_verify_len(x->replay_esn, re); 2004 if (err) 2005 goto out; 2006 2007 spin_lock_bh(&x->lock); 2008 xfrm_update_ae_params(x, attrs, 1); 2009 spin_unlock_bh(&x->lock); 2010 2011 c.event = nlh->nlmsg_type; 2012 c.seq = nlh->nlmsg_seq; 2013 c.portid = nlh->nlmsg_pid; 2014 c.data.aevent = XFRM_AE_CU; 2015 km_state_notify(x, &c); 2016 err = 0; 2017 out: 2018 xfrm_state_put(x); 2019 return err; 2020 } 2021 2022 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 2023 struct nlattr **attrs) 2024 { 2025 struct net *net = sock_net(skb->sk); 2026 struct km_event c; 2027 u8 type = XFRM_POLICY_TYPE_MAIN; 2028 int err; 2029 2030 err = copy_from_user_policy_type(&type, attrs); 2031 if (err) 2032 return err; 2033 2034 err = xfrm_policy_flush(net, type, true); 2035 if (err) { 2036 if (err == -ESRCH) /* empty table */ 2037 return 0; 2038 return err; 2039 } 2040 2041 c.data.type = type; 2042 c.event = nlh->nlmsg_type; 2043 c.seq = nlh->nlmsg_seq; 2044 c.portid = nlh->nlmsg_pid; 2045 c.net = net; 2046 km_policy_notify(NULL, 0, &c); 2047 return 0; 2048 } 2049 2050 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 2051 struct nlattr **attrs) 2052 { 2053 struct net *net = sock_net(skb->sk); 2054 struct xfrm_policy *xp; 2055 struct xfrm_user_polexpire *up = nlmsg_data(nlh); 2056 struct xfrm_userpolicy_info *p = &up->pol; 2057 u8 type = XFRM_POLICY_TYPE_MAIN; 2058 int err = -ENOENT; 2059 struct xfrm_mark m; 2060 u32 mark = xfrm_mark_get(attrs, &m); 2061 2062 err = copy_from_user_policy_type(&type, attrs); 2063 if (err) 2064 return err; 2065 2066 err = verify_policy_dir(p->dir); 2067 if (err) 2068 return err; 2069 2070 if (p->index) 2071 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err); 2072 else { 2073 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 2074 struct xfrm_sec_ctx *ctx; 2075 2076 err = verify_sec_ctx_len(attrs); 2077 if (err) 2078 return err; 2079 2080 ctx = NULL; 2081 if (rt) { 2082 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2083 2084 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 2085 if (err) 2086 return err; 2087 } 2088 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, 2089 &p->sel, ctx, 0, &err); 2090 security_xfrm_policy_free(ctx); 2091 } 2092 if (xp == NULL) 2093 return -ENOENT; 2094 2095 if (unlikely(xp->walk.dead)) 2096 goto out; 2097 2098 err = 0; 2099 if (up->hard) { 2100 xfrm_policy_delete(xp, p->dir); 2101 xfrm_audit_policy_delete(xp, 1, true); 2102 } 2103 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 2104 2105 out: 2106 xfrm_pol_put(xp); 2107 return err; 2108 } 2109 2110 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 2111 struct nlattr **attrs) 2112 { 2113 struct net *net = sock_net(skb->sk); 2114 struct xfrm_state *x; 2115 int err; 2116 struct xfrm_user_expire *ue = nlmsg_data(nlh); 2117 struct xfrm_usersa_info *p = &ue->state; 2118 struct xfrm_mark m; 2119 u32 mark = xfrm_mark_get(attrs, &m); 2120 2121 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 2122 2123 err = -ENOENT; 2124 if (x == NULL) 2125 return err; 2126 2127 spin_lock_bh(&x->lock); 2128 err = -EINVAL; 2129 if (x->km.state != XFRM_STATE_VALID) 2130 goto out; 2131 km_state_expired(x, ue->hard, nlh->nlmsg_pid); 2132 2133 if (ue->hard) { 2134 __xfrm_state_delete(x); 2135 xfrm_audit_state_delete(x, 1, true); 2136 } 2137 err = 0; 2138 out: 2139 spin_unlock_bh(&x->lock); 2140 xfrm_state_put(x); 2141 return err; 2142 } 2143 2144 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 2145 struct nlattr **attrs) 2146 { 2147 struct net *net = sock_net(skb->sk); 2148 struct xfrm_policy *xp; 2149 struct xfrm_user_tmpl *ut; 2150 int i; 2151 struct nlattr *rt = attrs[XFRMA_TMPL]; 2152 struct xfrm_mark mark; 2153 2154 struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2155 struct xfrm_state *x = xfrm_state_alloc(net); 2156 int err = -ENOMEM; 2157 2158 if (!x) 2159 goto nomem; 2160 2161 xfrm_mark_get(attrs, &mark); 2162 2163 err = verify_newpolicy_info(&ua->policy); 2164 if (err) 2165 goto free_state; 2166 2167 /* build an XP */ 2168 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 2169 if (!xp) 2170 goto free_state; 2171 2172 memcpy(&x->id, &ua->id, sizeof(ua->id)); 2173 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2174 memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 2175 xp->mark.m = x->mark.m = mark.m; 2176 xp->mark.v = x->mark.v = mark.v; 2177 ut = nla_data(rt); 2178 /* extract the templates and for each call km_key */ 2179 for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2180 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2181 memcpy(&x->id, &t->id, sizeof(x->id)); 2182 x->props.mode = t->mode; 2183 x->props.reqid = t->reqid; 2184 x->props.family = ut->family; 2185 t->aalgos = ua->aalgos; 2186 t->ealgos = ua->ealgos; 2187 t->calgos = ua->calgos; 2188 err = km_query(x, t, xp); 2189 2190 } 2191 2192 kfree(x); 2193 kfree(xp); 2194 2195 return 0; 2196 2197 free_state: 2198 kfree(x); 2199 nomem: 2200 return err; 2201 } 2202 2203 #ifdef CONFIG_XFRM_MIGRATE 2204 static int copy_from_user_migrate(struct xfrm_migrate *ma, 2205 struct xfrm_kmaddress *k, 2206 struct nlattr **attrs, int *num) 2207 { 2208 struct nlattr *rt = attrs[XFRMA_MIGRATE]; 2209 struct xfrm_user_migrate *um; 2210 int i, num_migrate; 2211 2212 if (k != NULL) { 2213 struct xfrm_user_kmaddress *uk; 2214 2215 uk = nla_data(attrs[XFRMA_KMADDRESS]); 2216 memcpy(&k->local, &uk->local, sizeof(k->local)); 2217 memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 2218 k->family = uk->family; 2219 k->reserved = uk->reserved; 2220 } 2221 2222 um = nla_data(rt); 2223 num_migrate = nla_len(rt) / sizeof(*um); 2224 2225 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 2226 return -EINVAL; 2227 2228 for (i = 0; i < num_migrate; i++, um++, ma++) { 2229 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 2230 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 2231 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 2232 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 2233 2234 ma->proto = um->proto; 2235 ma->mode = um->mode; 2236 ma->reqid = um->reqid; 2237 2238 ma->old_family = um->old_family; 2239 ma->new_family = um->new_family; 2240 } 2241 2242 *num = i; 2243 return 0; 2244 } 2245 2246 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2247 struct nlattr **attrs) 2248 { 2249 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 2250 struct xfrm_migrate m[XFRM_MAX_DEPTH]; 2251 struct xfrm_kmaddress km, *kmp; 2252 u8 type; 2253 int err; 2254 int n = 0; 2255 struct net *net = sock_net(skb->sk); 2256 struct xfrm_encap_tmpl *encap = NULL; 2257 2258 if (attrs[XFRMA_MIGRATE] == NULL) 2259 return -EINVAL; 2260 2261 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 2262 2263 err = copy_from_user_policy_type(&type, attrs); 2264 if (err) 2265 return err; 2266 2267 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 2268 if (err) 2269 return err; 2270 2271 if (!n) 2272 return 0; 2273 2274 if (attrs[XFRMA_ENCAP]) { 2275 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 2276 sizeof(*encap), GFP_KERNEL); 2277 if (!encap) 2278 return 0; 2279 } 2280 2281 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap); 2282 2283 kfree(encap); 2284 2285 return err; 2286 } 2287 #else 2288 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2289 struct nlattr **attrs) 2290 { 2291 return -ENOPROTOOPT; 2292 } 2293 #endif 2294 2295 #ifdef CONFIG_XFRM_MIGRATE 2296 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 2297 { 2298 struct xfrm_user_migrate um; 2299 2300 memset(&um, 0, sizeof(um)); 2301 um.proto = m->proto; 2302 um.mode = m->mode; 2303 um.reqid = m->reqid; 2304 um.old_family = m->old_family; 2305 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 2306 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 2307 um.new_family = m->new_family; 2308 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 2309 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 2310 2311 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 2312 } 2313 2314 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 2315 { 2316 struct xfrm_user_kmaddress uk; 2317 2318 memset(&uk, 0, sizeof(uk)); 2319 uk.family = k->family; 2320 uk.reserved = k->reserved; 2321 memcpy(&uk.local, &k->local, sizeof(uk.local)); 2322 memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 2323 2324 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 2325 } 2326 2327 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma, 2328 int with_encp) 2329 { 2330 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 2331 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 2332 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) 2333 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 2334 + userpolicy_type_attrsize(); 2335 } 2336 2337 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2338 int num_migrate, const struct xfrm_kmaddress *k, 2339 const struct xfrm_selector *sel, 2340 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type) 2341 { 2342 const struct xfrm_migrate *mp; 2343 struct xfrm_userpolicy_id *pol_id; 2344 struct nlmsghdr *nlh; 2345 int i, err; 2346 2347 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 2348 if (nlh == NULL) 2349 return -EMSGSIZE; 2350 2351 pol_id = nlmsg_data(nlh); 2352 /* copy data from selector, dir, and type to the pol_id */ 2353 memset(pol_id, 0, sizeof(*pol_id)); 2354 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 2355 pol_id->dir = dir; 2356 2357 if (k != NULL) { 2358 err = copy_to_user_kmaddress(k, skb); 2359 if (err) 2360 goto out_cancel; 2361 } 2362 if (encap) { 2363 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap); 2364 if (err) 2365 goto out_cancel; 2366 } 2367 err = copy_to_user_policy_type(type, skb); 2368 if (err) 2369 goto out_cancel; 2370 for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 2371 err = copy_to_user_migrate(mp, skb); 2372 if (err) 2373 goto out_cancel; 2374 } 2375 2376 nlmsg_end(skb, nlh); 2377 return 0; 2378 2379 out_cancel: 2380 nlmsg_cancel(skb, nlh); 2381 return err; 2382 } 2383 2384 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2385 const struct xfrm_migrate *m, int num_migrate, 2386 const struct xfrm_kmaddress *k, 2387 const struct xfrm_encap_tmpl *encap) 2388 { 2389 struct net *net = &init_net; 2390 struct sk_buff *skb; 2391 2392 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap), 2393 GFP_ATOMIC); 2394 if (skb == NULL) 2395 return -ENOMEM; 2396 2397 /* build migrate */ 2398 if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0) 2399 BUG(); 2400 2401 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 2402 } 2403 #else 2404 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2405 const struct xfrm_migrate *m, int num_migrate, 2406 const struct xfrm_kmaddress *k, 2407 const struct xfrm_encap_tmpl *encap) 2408 { 2409 return -ENOPROTOOPT; 2410 } 2411 #endif 2412 2413 #define XMSGSIZE(type) sizeof(struct type) 2414 2415 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 2416 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2417 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2418 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2419 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2420 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2421 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2422 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2423 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 2424 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2425 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2426 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2427 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2428 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2429 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2430 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2431 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2432 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 2433 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2434 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2435 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2436 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2437 }; 2438 2439 #undef XMSGSIZE 2440 2441 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2442 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2443 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2444 [XFRMA_LASTUSED] = { .type = NLA_U64}, 2445 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 2446 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2447 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2448 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2449 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2450 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2451 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2452 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2453 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2454 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2455 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2456 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2457 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2458 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2459 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2460 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 2461 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 2462 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 2463 [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2464 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2465 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2466 [XFRMA_PROTO] = { .type = NLA_U8 }, 2467 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2468 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) }, 2469 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 }, 2470 }; 2471 2472 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2473 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2474 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2475 }; 2476 2477 static const struct xfrm_link { 2478 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 2479 int (*start)(struct netlink_callback *); 2480 int (*dump)(struct sk_buff *, struct netlink_callback *); 2481 int (*done)(struct netlink_callback *); 2482 const struct nla_policy *nla_pol; 2483 int nla_max; 2484 } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2485 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2486 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2487 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 2488 .dump = xfrm_dump_sa, 2489 .done = xfrm_dump_sa_done }, 2490 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2491 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2492 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 2493 .start = xfrm_dump_policy_start, 2494 .dump = xfrm_dump_policy, 2495 .done = xfrm_dump_policy_done }, 2496 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2497 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 2498 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2499 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2500 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2501 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2502 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2503 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2504 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2505 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 2506 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 2507 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2508 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 2509 .nla_pol = xfrma_spd_policy, 2510 .nla_max = XFRMA_SPD_MAX }, 2511 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 2512 }; 2513 2514 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 2515 struct netlink_ext_ack *extack) 2516 { 2517 struct net *net = sock_net(skb->sk); 2518 struct nlattr *attrs[XFRMA_MAX+1]; 2519 const struct xfrm_link *link; 2520 int type, err; 2521 2522 #ifdef CONFIG_COMPAT 2523 if (in_compat_syscall()) 2524 return -EOPNOTSUPP; 2525 #endif 2526 2527 type = nlh->nlmsg_type; 2528 if (type > XFRM_MSG_MAX) 2529 return -EINVAL; 2530 2531 type -= XFRM_MSG_BASE; 2532 link = &xfrm_dispatch[type]; 2533 2534 /* All operations require privileges, even GET */ 2535 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 2536 return -EPERM; 2537 2538 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2539 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2540 (nlh->nlmsg_flags & NLM_F_DUMP)) { 2541 if (link->dump == NULL) 2542 return -EINVAL; 2543 2544 { 2545 struct netlink_dump_control c = { 2546 .start = link->start, 2547 .dump = link->dump, 2548 .done = link->done, 2549 }; 2550 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 2551 } 2552 } 2553 2554 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, 2555 link->nla_max ? : XFRMA_MAX, 2556 link->nla_pol ? : xfrma_policy, extack); 2557 if (err < 0) 2558 return err; 2559 2560 if (link->doit == NULL) 2561 return -EINVAL; 2562 2563 return link->doit(skb, nlh, attrs); 2564 } 2565 2566 static void xfrm_netlink_rcv(struct sk_buff *skb) 2567 { 2568 struct net *net = sock_net(skb->sk); 2569 2570 mutex_lock(&net->xfrm.xfrm_cfg_mutex); 2571 netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 2572 mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 2573 } 2574 2575 static inline size_t xfrm_expire_msgsize(void) 2576 { 2577 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 2578 + nla_total_size(sizeof(struct xfrm_mark)); 2579 } 2580 2581 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2582 { 2583 struct xfrm_user_expire *ue; 2584 struct nlmsghdr *nlh; 2585 int err; 2586 2587 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 2588 if (nlh == NULL) 2589 return -EMSGSIZE; 2590 2591 ue = nlmsg_data(nlh); 2592 copy_to_user_state(x, &ue->state); 2593 ue->hard = (c->data.hard != 0) ? 1 : 0; 2594 /* clear the padding bytes */ 2595 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard)); 2596 2597 err = xfrm_mark_put(skb, &x->mark); 2598 if (err) 2599 return err; 2600 2601 nlmsg_end(skb, nlh); 2602 return 0; 2603 } 2604 2605 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 2606 { 2607 struct net *net = xs_net(x); 2608 struct sk_buff *skb; 2609 2610 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 2611 if (skb == NULL) 2612 return -ENOMEM; 2613 2614 if (build_expire(skb, x, c) < 0) { 2615 kfree_skb(skb); 2616 return -EMSGSIZE; 2617 } 2618 2619 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 2620 } 2621 2622 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 2623 { 2624 struct net *net = xs_net(x); 2625 struct sk_buff *skb; 2626 2627 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2628 if (skb == NULL) 2629 return -ENOMEM; 2630 2631 if (build_aevent(skb, x, c) < 0) 2632 BUG(); 2633 2634 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 2635 } 2636 2637 static int xfrm_notify_sa_flush(const struct km_event *c) 2638 { 2639 struct net *net = c->net; 2640 struct xfrm_usersa_flush *p; 2641 struct nlmsghdr *nlh; 2642 struct sk_buff *skb; 2643 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 2644 2645 skb = nlmsg_new(len, GFP_ATOMIC); 2646 if (skb == NULL) 2647 return -ENOMEM; 2648 2649 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 2650 if (nlh == NULL) { 2651 kfree_skb(skb); 2652 return -EMSGSIZE; 2653 } 2654 2655 p = nlmsg_data(nlh); 2656 p->proto = c->data.proto; 2657 2658 nlmsg_end(skb, nlh); 2659 2660 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 2661 } 2662 2663 static inline size_t xfrm_sa_len(struct xfrm_state *x) 2664 { 2665 size_t l = 0; 2666 if (x->aead) 2667 l += nla_total_size(aead_len(x->aead)); 2668 if (x->aalg) { 2669 l += nla_total_size(sizeof(struct xfrm_algo) + 2670 (x->aalg->alg_key_len + 7) / 8); 2671 l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 2672 } 2673 if (x->ealg) 2674 l += nla_total_size(xfrm_alg_len(x->ealg)); 2675 if (x->calg) 2676 l += nla_total_size(sizeof(*x->calg)); 2677 if (x->encap) 2678 l += nla_total_size(sizeof(*x->encap)); 2679 if (x->tfcpad) 2680 l += nla_total_size(sizeof(x->tfcpad)); 2681 if (x->replay_esn) 2682 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 2683 else 2684 l += nla_total_size(sizeof(struct xfrm_replay_state)); 2685 if (x->security) 2686 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 2687 x->security->ctx_len); 2688 if (x->coaddr) 2689 l += nla_total_size(sizeof(*x->coaddr)); 2690 if (x->props.extra_flags) 2691 l += nla_total_size(sizeof(x->props.extra_flags)); 2692 if (x->xso.dev) 2693 l += nla_total_size(sizeof(x->xso)); 2694 if (x->props.output_mark) 2695 l += nla_total_size(sizeof(x->props.output_mark)); 2696 2697 /* Must count x->lastused as it may become non-zero behind our back. */ 2698 l += nla_total_size_64bit(sizeof(u64)); 2699 2700 return l; 2701 } 2702 2703 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 2704 { 2705 struct net *net = xs_net(x); 2706 struct xfrm_usersa_info *p; 2707 struct xfrm_usersa_id *id; 2708 struct nlmsghdr *nlh; 2709 struct sk_buff *skb; 2710 int len = xfrm_sa_len(x); 2711 int headlen, err; 2712 2713 headlen = sizeof(*p); 2714 if (c->event == XFRM_MSG_DELSA) { 2715 len += nla_total_size(headlen); 2716 headlen = sizeof(*id); 2717 len += nla_total_size(sizeof(struct xfrm_mark)); 2718 } 2719 len += NLMSG_ALIGN(headlen); 2720 2721 skb = nlmsg_new(len, GFP_ATOMIC); 2722 if (skb == NULL) 2723 return -ENOMEM; 2724 2725 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 2726 err = -EMSGSIZE; 2727 if (nlh == NULL) 2728 goto out_free_skb; 2729 2730 p = nlmsg_data(nlh); 2731 if (c->event == XFRM_MSG_DELSA) { 2732 struct nlattr *attr; 2733 2734 id = nlmsg_data(nlh); 2735 memset(id, 0, sizeof(*id)); 2736 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 2737 id->spi = x->id.spi; 2738 id->family = x->props.family; 2739 id->proto = x->id.proto; 2740 2741 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 2742 err = -EMSGSIZE; 2743 if (attr == NULL) 2744 goto out_free_skb; 2745 2746 p = nla_data(attr); 2747 } 2748 err = copy_to_user_state_extra(x, p, skb); 2749 if (err) 2750 goto out_free_skb; 2751 2752 nlmsg_end(skb, nlh); 2753 2754 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 2755 2756 out_free_skb: 2757 kfree_skb(skb); 2758 return err; 2759 } 2760 2761 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 2762 { 2763 2764 switch (c->event) { 2765 case XFRM_MSG_EXPIRE: 2766 return xfrm_exp_state_notify(x, c); 2767 case XFRM_MSG_NEWAE: 2768 return xfrm_aevent_state_notify(x, c); 2769 case XFRM_MSG_DELSA: 2770 case XFRM_MSG_UPDSA: 2771 case XFRM_MSG_NEWSA: 2772 return xfrm_notify_sa(x, c); 2773 case XFRM_MSG_FLUSHSA: 2774 return xfrm_notify_sa_flush(c); 2775 default: 2776 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 2777 c->event); 2778 break; 2779 } 2780 2781 return 0; 2782 2783 } 2784 2785 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, 2786 struct xfrm_policy *xp) 2787 { 2788 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 2789 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2790 + nla_total_size(sizeof(struct xfrm_mark)) 2791 + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 2792 + userpolicy_type_attrsize(); 2793 } 2794 2795 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 2796 struct xfrm_tmpl *xt, struct xfrm_policy *xp) 2797 { 2798 __u32 seq = xfrm_get_acqseq(); 2799 struct xfrm_user_acquire *ua; 2800 struct nlmsghdr *nlh; 2801 int err; 2802 2803 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 2804 if (nlh == NULL) 2805 return -EMSGSIZE; 2806 2807 ua = nlmsg_data(nlh); 2808 memcpy(&ua->id, &x->id, sizeof(ua->id)); 2809 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 2810 memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 2811 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 2812 ua->aalgos = xt->aalgos; 2813 ua->ealgos = xt->ealgos; 2814 ua->calgos = xt->calgos; 2815 ua->seq = x->km.seq = seq; 2816 2817 err = copy_to_user_tmpl(xp, skb); 2818 if (!err) 2819 err = copy_to_user_state_sec_ctx(x, skb); 2820 if (!err) 2821 err = copy_to_user_policy_type(xp->type, skb); 2822 if (!err) 2823 err = xfrm_mark_put(skb, &xp->mark); 2824 if (err) { 2825 nlmsg_cancel(skb, nlh); 2826 return err; 2827 } 2828 2829 nlmsg_end(skb, nlh); 2830 return 0; 2831 } 2832 2833 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 2834 struct xfrm_policy *xp) 2835 { 2836 struct net *net = xs_net(x); 2837 struct sk_buff *skb; 2838 2839 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 2840 if (skb == NULL) 2841 return -ENOMEM; 2842 2843 if (build_acquire(skb, x, xt, xp) < 0) 2844 BUG(); 2845 2846 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 2847 } 2848 2849 /* User gives us xfrm_user_policy_info followed by an array of 0 2850 * or more templates. 2851 */ 2852 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 2853 u8 *data, int len, int *dir) 2854 { 2855 struct net *net = sock_net(sk); 2856 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 2857 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 2858 struct xfrm_policy *xp; 2859 int nr; 2860 2861 switch (sk->sk_family) { 2862 case AF_INET: 2863 if (opt != IP_XFRM_POLICY) { 2864 *dir = -EOPNOTSUPP; 2865 return NULL; 2866 } 2867 break; 2868 #if IS_ENABLED(CONFIG_IPV6) 2869 case AF_INET6: 2870 if (opt != IPV6_XFRM_POLICY) { 2871 *dir = -EOPNOTSUPP; 2872 return NULL; 2873 } 2874 break; 2875 #endif 2876 default: 2877 *dir = -EINVAL; 2878 return NULL; 2879 } 2880 2881 *dir = -EINVAL; 2882 2883 if (len < sizeof(*p) || 2884 verify_newpolicy_info(p)) 2885 return NULL; 2886 2887 nr = ((len - sizeof(*p)) / sizeof(*ut)); 2888 if (validate_tmpl(nr, ut, p->sel.family)) 2889 return NULL; 2890 2891 if (p->dir > XFRM_POLICY_OUT) 2892 return NULL; 2893 2894 xp = xfrm_policy_alloc(net, GFP_ATOMIC); 2895 if (xp == NULL) { 2896 *dir = -ENOBUFS; 2897 return NULL; 2898 } 2899 2900 copy_from_user_policy(xp, p); 2901 xp->type = XFRM_POLICY_TYPE_MAIN; 2902 copy_templates(xp, ut, nr); 2903 2904 *dir = p->dir; 2905 2906 return xp; 2907 } 2908 2909 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) 2910 { 2911 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 2912 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2913 + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 2914 + nla_total_size(sizeof(struct xfrm_mark)) 2915 + userpolicy_type_attrsize(); 2916 } 2917 2918 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2919 int dir, const struct km_event *c) 2920 { 2921 struct xfrm_user_polexpire *upe; 2922 int hard = c->data.hard; 2923 struct nlmsghdr *nlh; 2924 int err; 2925 2926 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 2927 if (nlh == NULL) 2928 return -EMSGSIZE; 2929 2930 upe = nlmsg_data(nlh); 2931 copy_to_user_policy(xp, &upe->pol, dir); 2932 err = copy_to_user_tmpl(xp, skb); 2933 if (!err) 2934 err = copy_to_user_sec_ctx(xp, skb); 2935 if (!err) 2936 err = copy_to_user_policy_type(xp->type, skb); 2937 if (!err) 2938 err = xfrm_mark_put(skb, &xp->mark); 2939 if (err) { 2940 nlmsg_cancel(skb, nlh); 2941 return err; 2942 } 2943 upe->hard = !!hard; 2944 2945 nlmsg_end(skb, nlh); 2946 return 0; 2947 } 2948 2949 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 2950 { 2951 struct net *net = xp_net(xp); 2952 struct sk_buff *skb; 2953 2954 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 2955 if (skb == NULL) 2956 return -ENOMEM; 2957 2958 if (build_polexpire(skb, xp, dir, c) < 0) 2959 BUG(); 2960 2961 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 2962 } 2963 2964 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 2965 { 2966 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2967 struct net *net = xp_net(xp); 2968 struct xfrm_userpolicy_info *p; 2969 struct xfrm_userpolicy_id *id; 2970 struct nlmsghdr *nlh; 2971 struct sk_buff *skb; 2972 int headlen, err; 2973 2974 headlen = sizeof(*p); 2975 if (c->event == XFRM_MSG_DELPOLICY) { 2976 len += nla_total_size(headlen); 2977 headlen = sizeof(*id); 2978 } 2979 len += userpolicy_type_attrsize(); 2980 len += nla_total_size(sizeof(struct xfrm_mark)); 2981 len += NLMSG_ALIGN(headlen); 2982 2983 skb = nlmsg_new(len, GFP_ATOMIC); 2984 if (skb == NULL) 2985 return -ENOMEM; 2986 2987 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 2988 err = -EMSGSIZE; 2989 if (nlh == NULL) 2990 goto out_free_skb; 2991 2992 p = nlmsg_data(nlh); 2993 if (c->event == XFRM_MSG_DELPOLICY) { 2994 struct nlattr *attr; 2995 2996 id = nlmsg_data(nlh); 2997 memset(id, 0, sizeof(*id)); 2998 id->dir = dir; 2999 if (c->data.byid) 3000 id->index = xp->index; 3001 else 3002 memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 3003 3004 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 3005 err = -EMSGSIZE; 3006 if (attr == NULL) 3007 goto out_free_skb; 3008 3009 p = nla_data(attr); 3010 } 3011 3012 copy_to_user_policy(xp, p, dir); 3013 err = copy_to_user_tmpl(xp, skb); 3014 if (!err) 3015 err = copy_to_user_policy_type(xp->type, skb); 3016 if (!err) 3017 err = xfrm_mark_put(skb, &xp->mark); 3018 if (err) 3019 goto out_free_skb; 3020 3021 nlmsg_end(skb, nlh); 3022 3023 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 3024 3025 out_free_skb: 3026 kfree_skb(skb); 3027 return err; 3028 } 3029 3030 static int xfrm_notify_policy_flush(const struct km_event *c) 3031 { 3032 struct net *net = c->net; 3033 struct nlmsghdr *nlh; 3034 struct sk_buff *skb; 3035 int err; 3036 3037 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 3038 if (skb == NULL) 3039 return -ENOMEM; 3040 3041 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 3042 err = -EMSGSIZE; 3043 if (nlh == NULL) 3044 goto out_free_skb; 3045 err = copy_to_user_policy_type(c->data.type, skb); 3046 if (err) 3047 goto out_free_skb; 3048 3049 nlmsg_end(skb, nlh); 3050 3051 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 3052 3053 out_free_skb: 3054 kfree_skb(skb); 3055 return err; 3056 } 3057 3058 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 3059 { 3060 3061 switch (c->event) { 3062 case XFRM_MSG_NEWPOLICY: 3063 case XFRM_MSG_UPDPOLICY: 3064 case XFRM_MSG_DELPOLICY: 3065 return xfrm_notify_policy(xp, dir, c); 3066 case XFRM_MSG_FLUSHPOLICY: 3067 return xfrm_notify_policy_flush(c); 3068 case XFRM_MSG_POLEXPIRE: 3069 return xfrm_exp_policy_notify(xp, dir, c); 3070 default: 3071 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 3072 c->event); 3073 } 3074 3075 return 0; 3076 3077 } 3078 3079 static inline size_t xfrm_report_msgsize(void) 3080 { 3081 return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 3082 } 3083 3084 static int build_report(struct sk_buff *skb, u8 proto, 3085 struct xfrm_selector *sel, xfrm_address_t *addr) 3086 { 3087 struct xfrm_user_report *ur; 3088 struct nlmsghdr *nlh; 3089 3090 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 3091 if (nlh == NULL) 3092 return -EMSGSIZE; 3093 3094 ur = nlmsg_data(nlh); 3095 ur->proto = proto; 3096 memcpy(&ur->sel, sel, sizeof(ur->sel)); 3097 3098 if (addr) { 3099 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 3100 if (err) { 3101 nlmsg_cancel(skb, nlh); 3102 return err; 3103 } 3104 } 3105 nlmsg_end(skb, nlh); 3106 return 0; 3107 } 3108 3109 static int xfrm_send_report(struct net *net, u8 proto, 3110 struct xfrm_selector *sel, xfrm_address_t *addr) 3111 { 3112 struct sk_buff *skb; 3113 3114 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 3115 if (skb == NULL) 3116 return -ENOMEM; 3117 3118 if (build_report(skb, proto, sel, addr) < 0) 3119 BUG(); 3120 3121 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 3122 } 3123 3124 static inline size_t xfrm_mapping_msgsize(void) 3125 { 3126 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 3127 } 3128 3129 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 3130 xfrm_address_t *new_saddr, __be16 new_sport) 3131 { 3132 struct xfrm_user_mapping *um; 3133 struct nlmsghdr *nlh; 3134 3135 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 3136 if (nlh == NULL) 3137 return -EMSGSIZE; 3138 3139 um = nlmsg_data(nlh); 3140 3141 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 3142 um->id.spi = x->id.spi; 3143 um->id.family = x->props.family; 3144 um->id.proto = x->id.proto; 3145 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 3146 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 3147 um->new_sport = new_sport; 3148 um->old_sport = x->encap->encap_sport; 3149 um->reqid = x->props.reqid; 3150 3151 nlmsg_end(skb, nlh); 3152 return 0; 3153 } 3154 3155 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 3156 __be16 sport) 3157 { 3158 struct net *net = xs_net(x); 3159 struct sk_buff *skb; 3160 3161 if (x->id.proto != IPPROTO_ESP) 3162 return -EINVAL; 3163 3164 if (!x->encap) 3165 return -EINVAL; 3166 3167 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 3168 if (skb == NULL) 3169 return -ENOMEM; 3170 3171 if (build_mapping(skb, x, ipaddr, sport) < 0) 3172 BUG(); 3173 3174 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 3175 } 3176 3177 static bool xfrm_is_alive(const struct km_event *c) 3178 { 3179 return (bool)xfrm_acquire_is_on(c->net); 3180 } 3181 3182 static struct xfrm_mgr netlink_mgr = { 3183 .notify = xfrm_send_state_notify, 3184 .acquire = xfrm_send_acquire, 3185 .compile_policy = xfrm_compile_policy, 3186 .notify_policy = xfrm_send_policy_notify, 3187 .report = xfrm_send_report, 3188 .migrate = xfrm_send_migrate, 3189 .new_mapping = xfrm_send_mapping, 3190 .is_alive = xfrm_is_alive, 3191 }; 3192 3193 static int __net_init xfrm_user_net_init(struct net *net) 3194 { 3195 struct sock *nlsk; 3196 struct netlink_kernel_cfg cfg = { 3197 .groups = XFRMNLGRP_MAX, 3198 .input = xfrm_netlink_rcv, 3199 }; 3200 3201 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3202 if (nlsk == NULL) 3203 return -ENOMEM; 3204 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3205 rcu_assign_pointer(net->xfrm.nlsk, nlsk); 3206 return 0; 3207 } 3208 3209 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3210 { 3211 struct net *net; 3212 list_for_each_entry(net, net_exit_list, exit_list) 3213 RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 3214 synchronize_net(); 3215 list_for_each_entry(net, net_exit_list, exit_list) 3216 netlink_kernel_release(net->xfrm.nlsk_stash); 3217 } 3218 3219 static struct pernet_operations xfrm_user_net_ops = { 3220 .init = xfrm_user_net_init, 3221 .exit_batch = xfrm_user_net_exit, 3222 }; 3223 3224 static int __init xfrm_user_init(void) 3225 { 3226 int rv; 3227 3228 printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3229 3230 rv = register_pernet_subsys(&xfrm_user_net_ops); 3231 if (rv < 0) 3232 return rv; 3233 rv = xfrm_register_km(&netlink_mgr); 3234 if (rv < 0) 3235 unregister_pernet_subsys(&xfrm_user_net_ops); 3236 return rv; 3237 } 3238 3239 static void __exit xfrm_user_exit(void) 3240 { 3241 xfrm_unregister_km(&netlink_mgr); 3242 unregister_pernet_subsys(&xfrm_user_net_ops); 3243 } 3244 3245 module_init(xfrm_user_init); 3246 module_exit(xfrm_user_exit); 3247 MODULE_LICENSE("GPL"); 3248 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3249 3250