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