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 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length"); 1372 return -EINVAL; 1373 } 1374 thresh4 = nla_data(rta); 1375 if (thresh4->lbits > 32 || thresh4->rbits > 32) { 1376 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)"); 1377 return -EINVAL; 1378 } 1379 } 1380 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) { 1381 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH]; 1382 1383 if (nla_len(rta) < sizeof(*thresh6)) { 1384 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length"); 1385 return -EINVAL; 1386 } 1387 thresh6 = nla_data(rta); 1388 if (thresh6->lbits > 128 || thresh6->rbits > 128) { 1389 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)"); 1390 return -EINVAL; 1391 } 1392 } 1393 1394 if (thresh4 || thresh6) { 1395 write_seqlock(&net->xfrm.policy_hthresh.lock); 1396 if (thresh4) { 1397 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits; 1398 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits; 1399 } 1400 if (thresh6) { 1401 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits; 1402 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits; 1403 } 1404 write_sequnlock(&net->xfrm.policy_hthresh.lock); 1405 1406 xfrm_policy_hash_rebuild(net); 1407 } 1408 1409 return 0; 1410 } 1411 1412 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1413 struct nlattr **attrs, 1414 struct netlink_ext_ack *extack) 1415 { 1416 struct net *net = sock_net(skb->sk); 1417 struct sk_buff *r_skb; 1418 u32 *flags = nlmsg_data(nlh); 1419 u32 sportid = NETLINK_CB(skb).portid; 1420 u32 seq = nlh->nlmsg_seq; 1421 int err; 1422 1423 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 1424 if (r_skb == NULL) 1425 return -ENOMEM; 1426 1427 err = build_spdinfo(r_skb, net, sportid, seq, *flags); 1428 BUG_ON(err < 0); 1429 1430 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1431 } 1432 1433 static inline unsigned int xfrm_sadinfo_msgsize(void) 1434 { 1435 return NLMSG_ALIGN(4) 1436 + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 1437 + nla_total_size(4); /* XFRMA_SAD_CNT */ 1438 } 1439 1440 static int build_sadinfo(struct sk_buff *skb, struct net *net, 1441 u32 portid, u32 seq, u32 flags) 1442 { 1443 struct xfrmk_sadinfo si; 1444 struct xfrmu_sadhinfo sh; 1445 struct nlmsghdr *nlh; 1446 int err; 1447 u32 *f; 1448 1449 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 1450 if (nlh == NULL) /* shouldn't really happen ... */ 1451 return -EMSGSIZE; 1452 1453 f = nlmsg_data(nlh); 1454 *f = flags; 1455 xfrm_sad_getinfo(net, &si); 1456 1457 sh.sadhmcnt = si.sadhmcnt; 1458 sh.sadhcnt = si.sadhcnt; 1459 1460 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt); 1461 if (!err) 1462 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 1463 if (err) { 1464 nlmsg_cancel(skb, nlh); 1465 return err; 1466 } 1467 1468 nlmsg_end(skb, nlh); 1469 return 0; 1470 } 1471 1472 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 1473 struct nlattr **attrs, 1474 struct netlink_ext_ack *extack) 1475 { 1476 struct net *net = sock_net(skb->sk); 1477 struct sk_buff *r_skb; 1478 u32 *flags = nlmsg_data(nlh); 1479 u32 sportid = NETLINK_CB(skb).portid; 1480 u32 seq = nlh->nlmsg_seq; 1481 int err; 1482 1483 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 1484 if (r_skb == NULL) 1485 return -ENOMEM; 1486 1487 err = build_sadinfo(r_skb, net, sportid, seq, *flags); 1488 BUG_ON(err < 0); 1489 1490 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); 1491 } 1492 1493 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 1494 struct nlattr **attrs, struct netlink_ext_ack *extack) 1495 { 1496 struct net *net = sock_net(skb->sk); 1497 struct xfrm_usersa_id *p = nlmsg_data(nlh); 1498 struct xfrm_state *x; 1499 struct sk_buff *resp_skb; 1500 int err = -ESRCH; 1501 1502 x = xfrm_user_state_lookup(net, p, attrs, &err); 1503 if (x == NULL) 1504 goto out_noput; 1505 1506 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 1507 if (IS_ERR(resp_skb)) { 1508 err = PTR_ERR(resp_skb); 1509 } else { 1510 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 1511 } 1512 xfrm_state_put(x); 1513 out_noput: 1514 return err; 1515 } 1516 1517 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 1518 struct nlattr **attrs, 1519 struct netlink_ext_ack *extack) 1520 { 1521 struct net *net = sock_net(skb->sk); 1522 struct xfrm_state *x; 1523 struct xfrm_userspi_info *p; 1524 struct xfrm_translator *xtr; 1525 struct sk_buff *resp_skb; 1526 xfrm_address_t *daddr; 1527 int family; 1528 int err; 1529 u32 mark; 1530 struct xfrm_mark m; 1531 u32 if_id = 0; 1532 1533 p = nlmsg_data(nlh); 1534 err = verify_spi_info(p->info.id.proto, p->min, p->max, extack); 1535 if (err) 1536 goto out_noput; 1537 1538 family = p->info.family; 1539 daddr = &p->info.id.daddr; 1540 1541 x = NULL; 1542 1543 mark = xfrm_mark_get(attrs, &m); 1544 1545 if (attrs[XFRMA_IF_ID]) 1546 if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 1547 1548 if (p->info.seq) { 1549 x = xfrm_find_acq_byseq(net, mark, p->info.seq); 1550 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) { 1551 xfrm_state_put(x); 1552 x = NULL; 1553 } 1554 } 1555 1556 if (!x) 1557 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid, 1558 if_id, p->info.id.proto, daddr, 1559 &p->info.saddr, 1, 1560 family); 1561 err = -ENOENT; 1562 if (!x) { 1563 NL_SET_ERR_MSG(extack, "Target ACQUIRE not found"); 1564 goto out_noput; 1565 } 1566 1567 err = xfrm_alloc_spi(x, p->min, p->max, extack); 1568 if (err) 1569 goto out; 1570 1571 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 1572 if (IS_ERR(resp_skb)) { 1573 err = PTR_ERR(resp_skb); 1574 goto out; 1575 } 1576 1577 xtr = xfrm_get_translator(); 1578 if (xtr) { 1579 err = xtr->alloc_compat(skb, nlmsg_hdr(skb)); 1580 1581 xfrm_put_translator(xtr); 1582 if (err) { 1583 kfree_skb(resp_skb); 1584 goto out; 1585 } 1586 } 1587 1588 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); 1589 1590 out: 1591 xfrm_state_put(x); 1592 out_noput: 1593 return err; 1594 } 1595 1596 static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack) 1597 { 1598 switch (dir) { 1599 case XFRM_POLICY_IN: 1600 case XFRM_POLICY_OUT: 1601 case XFRM_POLICY_FWD: 1602 break; 1603 1604 default: 1605 NL_SET_ERR_MSG(extack, "Invalid policy direction"); 1606 return -EINVAL; 1607 } 1608 1609 return 0; 1610 } 1611 1612 static int verify_policy_type(u8 type, struct netlink_ext_ack *extack) 1613 { 1614 switch (type) { 1615 case XFRM_POLICY_TYPE_MAIN: 1616 #ifdef CONFIG_XFRM_SUB_POLICY 1617 case XFRM_POLICY_TYPE_SUB: 1618 #endif 1619 break; 1620 1621 default: 1622 NL_SET_ERR_MSG(extack, "Invalid policy type"); 1623 return -EINVAL; 1624 } 1625 1626 return 0; 1627 } 1628 1629 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p, 1630 struct netlink_ext_ack *extack) 1631 { 1632 int ret; 1633 1634 switch (p->share) { 1635 case XFRM_SHARE_ANY: 1636 case XFRM_SHARE_SESSION: 1637 case XFRM_SHARE_USER: 1638 case XFRM_SHARE_UNIQUE: 1639 break; 1640 1641 default: 1642 NL_SET_ERR_MSG(extack, "Invalid policy share"); 1643 return -EINVAL; 1644 } 1645 1646 switch (p->action) { 1647 case XFRM_POLICY_ALLOW: 1648 case XFRM_POLICY_BLOCK: 1649 break; 1650 1651 default: 1652 NL_SET_ERR_MSG(extack, "Invalid policy action"); 1653 return -EINVAL; 1654 } 1655 1656 switch (p->sel.family) { 1657 case AF_INET: 1658 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) { 1659 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)"); 1660 return -EINVAL; 1661 } 1662 1663 break; 1664 1665 case AF_INET6: 1666 #if IS_ENABLED(CONFIG_IPV6) 1667 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) { 1668 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)"); 1669 return -EINVAL; 1670 } 1671 1672 break; 1673 #else 1674 NL_SET_ERR_MSG(extack, "IPv6 support disabled"); 1675 return -EAFNOSUPPORT; 1676 #endif 1677 1678 default: 1679 NL_SET_ERR_MSG(extack, "Invalid selector family"); 1680 return -EINVAL; 1681 } 1682 1683 ret = verify_policy_dir(p->dir, extack); 1684 if (ret) 1685 return ret; 1686 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) { 1687 NL_SET_ERR_MSG(extack, "Policy index doesn't match direction"); 1688 return -EINVAL; 1689 } 1690 1691 return 0; 1692 } 1693 1694 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1695 { 1696 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1697 struct xfrm_user_sec_ctx *uctx; 1698 1699 if (!rt) 1700 return 0; 1701 1702 uctx = nla_data(rt); 1703 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL); 1704 } 1705 1706 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 1707 int nr) 1708 { 1709 int i; 1710 1711 xp->xfrm_nr = nr; 1712 for (i = 0; i < nr; i++, ut++) { 1713 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 1714 1715 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 1716 memcpy(&t->saddr, &ut->saddr, 1717 sizeof(xfrm_address_t)); 1718 t->reqid = ut->reqid; 1719 t->mode = ut->mode; 1720 t->share = ut->share; 1721 t->optional = ut->optional; 1722 t->aalgos = ut->aalgos; 1723 t->ealgos = ut->ealgos; 1724 t->calgos = ut->calgos; 1725 /* If all masks are ~0, then we allow all algorithms. */ 1726 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 1727 t->encap_family = ut->family; 1728 } 1729 } 1730 1731 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family, 1732 struct netlink_ext_ack *extack) 1733 { 1734 u16 prev_family; 1735 int i; 1736 1737 if (nr > XFRM_MAX_DEPTH) { 1738 NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")"); 1739 return -EINVAL; 1740 } 1741 1742 prev_family = family; 1743 1744 for (i = 0; i < nr; i++) { 1745 /* We never validated the ut->family value, so many 1746 * applications simply leave it at zero. The check was 1747 * never made and ut->family was ignored because all 1748 * templates could be assumed to have the same family as 1749 * the policy itself. Now that we will have ipv4-in-ipv6 1750 * and ipv6-in-ipv4 tunnels, this is no longer true. 1751 */ 1752 if (!ut[i].family) 1753 ut[i].family = family; 1754 1755 switch (ut[i].mode) { 1756 case XFRM_MODE_TUNNEL: 1757 case XFRM_MODE_BEET: 1758 break; 1759 default: 1760 if (ut[i].family != prev_family) { 1761 NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change"); 1762 return -EINVAL; 1763 } 1764 break; 1765 } 1766 if (ut[i].mode >= XFRM_MODE_MAX) { 1767 NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")"); 1768 return -EINVAL; 1769 } 1770 1771 prev_family = ut[i].family; 1772 1773 switch (ut[i].family) { 1774 case AF_INET: 1775 break; 1776 #if IS_ENABLED(CONFIG_IPV6) 1777 case AF_INET6: 1778 break; 1779 #endif 1780 default: 1781 NL_SET_ERR_MSG(extack, "Invalid family in template"); 1782 return -EINVAL; 1783 } 1784 1785 if (!xfrm_id_proto_valid(ut[i].id.proto)) { 1786 NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template"); 1787 return -EINVAL; 1788 } 1789 } 1790 1791 return 0; 1792 } 1793 1794 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs, 1795 struct netlink_ext_ack *extack) 1796 { 1797 struct nlattr *rt = attrs[XFRMA_TMPL]; 1798 1799 if (!rt) { 1800 pol->xfrm_nr = 0; 1801 } else { 1802 struct xfrm_user_tmpl *utmpl = nla_data(rt); 1803 int nr = nla_len(rt) / sizeof(*utmpl); 1804 int err; 1805 1806 err = validate_tmpl(nr, utmpl, pol->family, extack); 1807 if (err) 1808 return err; 1809 1810 copy_templates(pol, utmpl, nr); 1811 } 1812 return 0; 1813 } 1814 1815 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs, 1816 struct netlink_ext_ack *extack) 1817 { 1818 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1819 struct xfrm_userpolicy_type *upt; 1820 u8 type = XFRM_POLICY_TYPE_MAIN; 1821 int err; 1822 1823 if (rt) { 1824 upt = nla_data(rt); 1825 type = upt->type; 1826 } 1827 1828 err = verify_policy_type(type, extack); 1829 if (err) 1830 return err; 1831 1832 *tp = type; 1833 return 0; 1834 } 1835 1836 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 1837 { 1838 xp->priority = p->priority; 1839 xp->index = p->index; 1840 memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 1841 memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 1842 xp->action = p->action; 1843 xp->flags = p->flags; 1844 xp->family = p->sel.family; 1845 /* XXX xp->share = p->share; */ 1846 } 1847 1848 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 1849 { 1850 memset(p, 0, sizeof(*p)); 1851 memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 1852 memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 1853 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 1854 p->priority = xp->priority; 1855 p->index = xp->index; 1856 p->sel.family = xp->family; 1857 p->dir = dir; 1858 p->action = xp->action; 1859 p->flags = xp->flags; 1860 p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 1861 } 1862 1863 static struct xfrm_policy *xfrm_policy_construct(struct net *net, 1864 struct xfrm_userpolicy_info *p, 1865 struct nlattr **attrs, 1866 int *errp, 1867 struct netlink_ext_ack *extack) 1868 { 1869 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 1870 int err; 1871 1872 if (!xp) { 1873 *errp = -ENOMEM; 1874 return NULL; 1875 } 1876 1877 copy_from_user_policy(xp, p); 1878 1879 err = copy_from_user_policy_type(&xp->type, attrs, extack); 1880 if (err) 1881 goto error; 1882 1883 if (!(err = copy_from_user_tmpl(xp, attrs, extack))) 1884 err = copy_from_user_sec_ctx(xp, attrs); 1885 if (err) 1886 goto error; 1887 1888 xfrm_mark_get(attrs, &xp->mark); 1889 1890 if (attrs[XFRMA_IF_ID]) 1891 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 1892 1893 return xp; 1894 error: 1895 *errp = err; 1896 xp->walk.dead = 1; 1897 xfrm_policy_destroy(xp); 1898 return NULL; 1899 } 1900 1901 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1902 struct nlattr **attrs, 1903 struct netlink_ext_ack *extack) 1904 { 1905 struct net *net = sock_net(skb->sk); 1906 struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 1907 struct xfrm_policy *xp; 1908 struct km_event c; 1909 int err; 1910 int excl; 1911 1912 err = verify_newpolicy_info(p, extack); 1913 if (err) 1914 return err; 1915 err = verify_sec_ctx_len(attrs, extack); 1916 if (err) 1917 return err; 1918 1919 xp = xfrm_policy_construct(net, p, attrs, &err, extack); 1920 if (!xp) 1921 return err; 1922 1923 /* shouldn't excl be based on nlh flags?? 1924 * Aha! this is anti-netlink really i.e more pfkey derived 1925 * in netlink excl is a flag and you wouldn't need 1926 * a type XFRM_MSG_UPDPOLICY - JHS */ 1927 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 1928 err = xfrm_policy_insert(p->dir, xp, excl); 1929 xfrm_audit_policy_add(xp, err ? 0 : 1, true); 1930 1931 if (err) { 1932 security_xfrm_policy_free(xp->security); 1933 kfree(xp); 1934 return err; 1935 } 1936 1937 c.event = nlh->nlmsg_type; 1938 c.seq = nlh->nlmsg_seq; 1939 c.portid = nlh->nlmsg_pid; 1940 km_policy_notify(xp, p->dir, &c); 1941 1942 xfrm_pol_put(xp); 1943 1944 return 0; 1945 } 1946 1947 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 1948 { 1949 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 1950 int i; 1951 1952 if (xp->xfrm_nr == 0) 1953 return 0; 1954 1955 for (i = 0; i < xp->xfrm_nr; i++) { 1956 struct xfrm_user_tmpl *up = &vec[i]; 1957 struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 1958 1959 memset(up, 0, sizeof(*up)); 1960 memcpy(&up->id, &kp->id, sizeof(up->id)); 1961 up->family = kp->encap_family; 1962 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 1963 up->reqid = kp->reqid; 1964 up->mode = kp->mode; 1965 up->share = kp->share; 1966 up->optional = kp->optional; 1967 up->aalgos = kp->aalgos; 1968 up->ealgos = kp->ealgos; 1969 up->calgos = kp->calgos; 1970 } 1971 1972 return nla_put(skb, XFRMA_TMPL, 1973 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1974 } 1975 1976 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 1977 { 1978 if (x->security) { 1979 return copy_sec_ctx(x->security, skb); 1980 } 1981 return 0; 1982 } 1983 1984 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 1985 { 1986 if (xp->security) 1987 return copy_sec_ctx(xp->security, skb); 1988 return 0; 1989 } 1990 static inline unsigned int userpolicy_type_attrsize(void) 1991 { 1992 #ifdef CONFIG_XFRM_SUB_POLICY 1993 return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1994 #else 1995 return 0; 1996 #endif 1997 } 1998 1999 #ifdef CONFIG_XFRM_SUB_POLICY 2000 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 2001 { 2002 struct xfrm_userpolicy_type upt; 2003 2004 /* Sadly there are two holes in struct xfrm_userpolicy_type */ 2005 memset(&upt, 0, sizeof(upt)); 2006 upt.type = type; 2007 2008 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 2009 } 2010 2011 #else 2012 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 2013 { 2014 return 0; 2015 } 2016 #endif 2017 2018 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 2019 { 2020 struct xfrm_dump_info *sp = ptr; 2021 struct xfrm_userpolicy_info *p; 2022 struct sk_buff *in_skb = sp->in_skb; 2023 struct sk_buff *skb = sp->out_skb; 2024 struct xfrm_translator *xtr; 2025 struct nlmsghdr *nlh; 2026 int err; 2027 2028 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq, 2029 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 2030 if (nlh == NULL) 2031 return -EMSGSIZE; 2032 2033 p = nlmsg_data(nlh); 2034 copy_to_user_policy(xp, p, dir); 2035 err = copy_to_user_tmpl(xp, skb); 2036 if (!err) 2037 err = copy_to_user_sec_ctx(xp, skb); 2038 if (!err) 2039 err = copy_to_user_policy_type(xp->type, skb); 2040 if (!err) 2041 err = xfrm_mark_put(skb, &xp->mark); 2042 if (!err) 2043 err = xfrm_if_id_put(skb, xp->if_id); 2044 if (err) { 2045 nlmsg_cancel(skb, nlh); 2046 return err; 2047 } 2048 nlmsg_end(skb, nlh); 2049 2050 xtr = xfrm_get_translator(); 2051 if (xtr) { 2052 err = xtr->alloc_compat(skb, nlh); 2053 2054 xfrm_put_translator(xtr); 2055 if (err) { 2056 nlmsg_cancel(skb, nlh); 2057 return err; 2058 } 2059 } 2060 2061 return 0; 2062 } 2063 2064 static int xfrm_dump_policy_done(struct netlink_callback *cb) 2065 { 2066 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 2067 struct net *net = sock_net(cb->skb->sk); 2068 2069 xfrm_policy_walk_done(walk, net); 2070 return 0; 2071 } 2072 2073 static int xfrm_dump_policy_start(struct netlink_callback *cb) 2074 { 2075 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 2076 2077 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args)); 2078 2079 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 2080 return 0; 2081 } 2082 2083 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 2084 { 2085 struct net *net = sock_net(skb->sk); 2086 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args; 2087 struct xfrm_dump_info info; 2088 2089 info.in_skb = cb->skb; 2090 info.out_skb = skb; 2091 info.nlmsg_seq = cb->nlh->nlmsg_seq; 2092 info.nlmsg_flags = NLM_F_MULTI; 2093 2094 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 2095 2096 return skb->len; 2097 } 2098 2099 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 2100 struct xfrm_policy *xp, 2101 int dir, u32 seq) 2102 { 2103 struct xfrm_dump_info info; 2104 struct sk_buff *skb; 2105 int err; 2106 2107 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2108 if (!skb) 2109 return ERR_PTR(-ENOMEM); 2110 2111 info.in_skb = in_skb; 2112 info.out_skb = skb; 2113 info.nlmsg_seq = seq; 2114 info.nlmsg_flags = 0; 2115 2116 err = dump_one_policy(xp, dir, 0, &info); 2117 if (err) { 2118 kfree_skb(skb); 2119 return ERR_PTR(err); 2120 } 2121 2122 return skb; 2123 } 2124 2125 static int xfrm_notify_userpolicy(struct net *net) 2126 { 2127 struct xfrm_userpolicy_default *up; 2128 int len = NLMSG_ALIGN(sizeof(*up)); 2129 struct nlmsghdr *nlh; 2130 struct sk_buff *skb; 2131 int err; 2132 2133 skb = nlmsg_new(len, GFP_ATOMIC); 2134 if (skb == NULL) 2135 return -ENOMEM; 2136 2137 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0); 2138 if (nlh == NULL) { 2139 kfree_skb(skb); 2140 return -EMSGSIZE; 2141 } 2142 2143 up = nlmsg_data(nlh); 2144 up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2145 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2146 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 2147 2148 nlmsg_end(skb, nlh); 2149 2150 rcu_read_lock(); 2151 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 2152 rcu_read_unlock(); 2153 2154 return err; 2155 } 2156 2157 static bool xfrm_userpolicy_is_valid(__u8 policy) 2158 { 2159 return policy == XFRM_USERPOLICY_BLOCK || 2160 policy == XFRM_USERPOLICY_ACCEPT; 2161 } 2162 2163 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh, 2164 struct nlattr **attrs, struct netlink_ext_ack *extack) 2165 { 2166 struct net *net = sock_net(skb->sk); 2167 struct xfrm_userpolicy_default *up = nlmsg_data(nlh); 2168 2169 if (xfrm_userpolicy_is_valid(up->in)) 2170 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in; 2171 2172 if (xfrm_userpolicy_is_valid(up->fwd)) 2173 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd; 2174 2175 if (xfrm_userpolicy_is_valid(up->out)) 2176 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out; 2177 2178 rt_genid_bump_all(net); 2179 2180 xfrm_notify_userpolicy(net); 2181 return 0; 2182 } 2183 2184 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh, 2185 struct nlattr **attrs, struct netlink_ext_ack *extack) 2186 { 2187 struct sk_buff *r_skb; 2188 struct nlmsghdr *r_nlh; 2189 struct net *net = sock_net(skb->sk); 2190 struct xfrm_userpolicy_default *r_up; 2191 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default)); 2192 u32 portid = NETLINK_CB(skb).portid; 2193 u32 seq = nlh->nlmsg_seq; 2194 2195 r_skb = nlmsg_new(len, GFP_ATOMIC); 2196 if (!r_skb) 2197 return -ENOMEM; 2198 2199 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0); 2200 if (!r_nlh) { 2201 kfree_skb(r_skb); 2202 return -EMSGSIZE; 2203 } 2204 2205 r_up = nlmsg_data(r_nlh); 2206 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN]; 2207 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD]; 2208 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT]; 2209 nlmsg_end(r_skb, r_nlh); 2210 2211 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid); 2212 } 2213 2214 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 2215 struct nlattr **attrs, 2216 struct netlink_ext_ack *extack) 2217 { 2218 struct net *net = sock_net(skb->sk); 2219 struct xfrm_policy *xp; 2220 struct xfrm_userpolicy_id *p; 2221 u8 type = XFRM_POLICY_TYPE_MAIN; 2222 int err; 2223 struct km_event c; 2224 int delete; 2225 struct xfrm_mark m; 2226 u32 if_id = 0; 2227 2228 p = nlmsg_data(nlh); 2229 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 2230 2231 err = copy_from_user_policy_type(&type, attrs, extack); 2232 if (err) 2233 return err; 2234 2235 err = verify_policy_dir(p->dir, extack); 2236 if (err) 2237 return err; 2238 2239 if (attrs[XFRMA_IF_ID]) 2240 if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 2241 2242 xfrm_mark_get(attrs, &m); 2243 2244 if (p->index) 2245 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, 2246 p->index, delete, &err); 2247 else { 2248 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 2249 struct xfrm_sec_ctx *ctx; 2250 2251 err = verify_sec_ctx_len(attrs, extack); 2252 if (err) 2253 return err; 2254 2255 ctx = NULL; 2256 if (rt) { 2257 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2258 2259 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 2260 if (err) 2261 return err; 2262 } 2263 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 2264 &p->sel, ctx, delete, &err); 2265 security_xfrm_policy_free(ctx); 2266 } 2267 if (xp == NULL) 2268 return -ENOENT; 2269 2270 if (!delete) { 2271 struct sk_buff *resp_skb; 2272 2273 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 2274 if (IS_ERR(resp_skb)) { 2275 err = PTR_ERR(resp_skb); 2276 } else { 2277 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 2278 NETLINK_CB(skb).portid); 2279 } 2280 } else { 2281 xfrm_audit_policy_delete(xp, err ? 0 : 1, true); 2282 2283 if (err != 0) 2284 goto out; 2285 2286 c.data.byid = p->index; 2287 c.event = nlh->nlmsg_type; 2288 c.seq = nlh->nlmsg_seq; 2289 c.portid = nlh->nlmsg_pid; 2290 km_policy_notify(xp, p->dir, &c); 2291 } 2292 2293 out: 2294 xfrm_pol_put(xp); 2295 return err; 2296 } 2297 2298 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 2299 struct nlattr **attrs, 2300 struct netlink_ext_ack *extack) 2301 { 2302 struct net *net = sock_net(skb->sk); 2303 struct km_event c; 2304 struct xfrm_usersa_flush *p = nlmsg_data(nlh); 2305 int err; 2306 2307 err = xfrm_state_flush(net, p->proto, true, false); 2308 if (err) { 2309 if (err == -ESRCH) /* empty table */ 2310 return 0; 2311 return err; 2312 } 2313 c.data.proto = p->proto; 2314 c.event = nlh->nlmsg_type; 2315 c.seq = nlh->nlmsg_seq; 2316 c.portid = nlh->nlmsg_pid; 2317 c.net = net; 2318 km_state_notify(NULL, &c); 2319 2320 return 0; 2321 } 2322 2323 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x) 2324 { 2325 unsigned int replay_size = x->replay_esn ? 2326 xfrm_replay_state_esn_len(x->replay_esn) : 2327 sizeof(struct xfrm_replay_state); 2328 2329 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 2330 + nla_total_size(replay_size) 2331 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur)) 2332 + nla_total_size(sizeof(struct xfrm_mark)) 2333 + nla_total_size(4) /* XFRM_AE_RTHR */ 2334 + nla_total_size(4); /* XFRM_AE_ETHR */ 2335 } 2336 2337 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 2338 { 2339 struct xfrm_aevent_id *id; 2340 struct nlmsghdr *nlh; 2341 int err; 2342 2343 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 2344 if (nlh == NULL) 2345 return -EMSGSIZE; 2346 2347 id = nlmsg_data(nlh); 2348 memset(&id->sa_id, 0, sizeof(id->sa_id)); 2349 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr)); 2350 id->sa_id.spi = x->id.spi; 2351 id->sa_id.family = x->props.family; 2352 id->sa_id.proto = x->id.proto; 2353 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr)); 2354 id->reqid = x->props.reqid; 2355 id->flags = c->data.aevent; 2356 2357 if (x->replay_esn) { 2358 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL, 2359 xfrm_replay_state_esn_len(x->replay_esn), 2360 x->replay_esn); 2361 } else { 2362 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), 2363 &x->replay); 2364 } 2365 if (err) 2366 goto out_cancel; 2367 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft, 2368 XFRMA_PAD); 2369 if (err) 2370 goto out_cancel; 2371 2372 if (id->flags & XFRM_AE_RTHR) { 2373 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 2374 if (err) 2375 goto out_cancel; 2376 } 2377 if (id->flags & XFRM_AE_ETHR) { 2378 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH, 2379 x->replay_maxage * 10 / HZ); 2380 if (err) 2381 goto out_cancel; 2382 } 2383 err = xfrm_mark_put(skb, &x->mark); 2384 if (err) 2385 goto out_cancel; 2386 2387 err = xfrm_if_id_put(skb, x->if_id); 2388 if (err) 2389 goto out_cancel; 2390 2391 nlmsg_end(skb, nlh); 2392 return 0; 2393 2394 out_cancel: 2395 nlmsg_cancel(skb, nlh); 2396 return err; 2397 } 2398 2399 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 2400 struct nlattr **attrs, struct netlink_ext_ack *extack) 2401 { 2402 struct net *net = sock_net(skb->sk); 2403 struct xfrm_state *x; 2404 struct sk_buff *r_skb; 2405 int err; 2406 struct km_event c; 2407 u32 mark; 2408 struct xfrm_mark m; 2409 struct xfrm_aevent_id *p = nlmsg_data(nlh); 2410 struct xfrm_usersa_id *id = &p->sa_id; 2411 2412 mark = xfrm_mark_get(attrs, &m); 2413 2414 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family); 2415 if (x == NULL) 2416 return -ESRCH; 2417 2418 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 2419 if (r_skb == NULL) { 2420 xfrm_state_put(x); 2421 return -ENOMEM; 2422 } 2423 2424 /* 2425 * XXX: is this lock really needed - none of the other 2426 * gets lock (the concern is things getting updated 2427 * while we are still reading) - jhs 2428 */ 2429 spin_lock_bh(&x->lock); 2430 c.data.aevent = p->flags; 2431 c.seq = nlh->nlmsg_seq; 2432 c.portid = nlh->nlmsg_pid; 2433 2434 err = build_aevent(r_skb, x, &c); 2435 BUG_ON(err < 0); 2436 2437 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); 2438 spin_unlock_bh(&x->lock); 2439 xfrm_state_put(x); 2440 return err; 2441 } 2442 2443 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 2444 struct nlattr **attrs, struct netlink_ext_ack *extack) 2445 { 2446 struct net *net = sock_net(skb->sk); 2447 struct xfrm_state *x; 2448 struct km_event c; 2449 int err = -EINVAL; 2450 u32 mark = 0; 2451 struct xfrm_mark m; 2452 struct xfrm_aevent_id *p = nlmsg_data(nlh); 2453 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 2454 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL]; 2455 struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 2456 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 2457 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 2458 2459 if (!lt && !rp && !re && !et && !rt) { 2460 NL_SET_ERR_MSG(extack, "Missing required attribute for AE"); 2461 return err; 2462 } 2463 2464 /* pedantic mode - thou shalt sayeth replaceth */ 2465 if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) { 2466 NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required"); 2467 return err; 2468 } 2469 2470 mark = xfrm_mark_get(attrs, &m); 2471 2472 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 2473 if (x == NULL) 2474 return -ESRCH; 2475 2476 if (x->km.state != XFRM_STATE_VALID) { 2477 NL_SET_ERR_MSG(extack, "SA must be in VALID state"); 2478 goto out; 2479 } 2480 2481 err = xfrm_replay_verify_len(x->replay_esn, re, extack); 2482 if (err) 2483 goto out; 2484 2485 spin_lock_bh(&x->lock); 2486 xfrm_update_ae_params(x, attrs, 1); 2487 spin_unlock_bh(&x->lock); 2488 2489 c.event = nlh->nlmsg_type; 2490 c.seq = nlh->nlmsg_seq; 2491 c.portid = nlh->nlmsg_pid; 2492 c.data.aevent = XFRM_AE_CU; 2493 km_state_notify(x, &c); 2494 err = 0; 2495 out: 2496 xfrm_state_put(x); 2497 return err; 2498 } 2499 2500 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 2501 struct nlattr **attrs, 2502 struct netlink_ext_ack *extack) 2503 { 2504 struct net *net = sock_net(skb->sk); 2505 struct km_event c; 2506 u8 type = XFRM_POLICY_TYPE_MAIN; 2507 int err; 2508 2509 err = copy_from_user_policy_type(&type, attrs, extack); 2510 if (err) 2511 return err; 2512 2513 err = xfrm_policy_flush(net, type, true); 2514 if (err) { 2515 if (err == -ESRCH) /* empty table */ 2516 return 0; 2517 return err; 2518 } 2519 2520 c.data.type = type; 2521 c.event = nlh->nlmsg_type; 2522 c.seq = nlh->nlmsg_seq; 2523 c.portid = nlh->nlmsg_pid; 2524 c.net = net; 2525 km_policy_notify(NULL, 0, &c); 2526 return 0; 2527 } 2528 2529 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 2530 struct nlattr **attrs, 2531 struct netlink_ext_ack *extack) 2532 { 2533 struct net *net = sock_net(skb->sk); 2534 struct xfrm_policy *xp; 2535 struct xfrm_user_polexpire *up = nlmsg_data(nlh); 2536 struct xfrm_userpolicy_info *p = &up->pol; 2537 u8 type = XFRM_POLICY_TYPE_MAIN; 2538 int err = -ENOENT; 2539 struct xfrm_mark m; 2540 u32 if_id = 0; 2541 2542 err = copy_from_user_policy_type(&type, attrs, extack); 2543 if (err) 2544 return err; 2545 2546 err = verify_policy_dir(p->dir, extack); 2547 if (err) 2548 return err; 2549 2550 if (attrs[XFRMA_IF_ID]) 2551 if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 2552 2553 xfrm_mark_get(attrs, &m); 2554 2555 if (p->index) 2556 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index, 2557 0, &err); 2558 else { 2559 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 2560 struct xfrm_sec_ctx *ctx; 2561 2562 err = verify_sec_ctx_len(attrs, extack); 2563 if (err) 2564 return err; 2565 2566 ctx = NULL; 2567 if (rt) { 2568 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 2569 2570 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL); 2571 if (err) 2572 return err; 2573 } 2574 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir, 2575 &p->sel, ctx, 0, &err); 2576 security_xfrm_policy_free(ctx); 2577 } 2578 if (xp == NULL) 2579 return -ENOENT; 2580 2581 if (unlikely(xp->walk.dead)) 2582 goto out; 2583 2584 err = 0; 2585 if (up->hard) { 2586 xfrm_policy_delete(xp, p->dir); 2587 xfrm_audit_policy_delete(xp, 1, true); 2588 } 2589 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid); 2590 2591 out: 2592 xfrm_pol_put(xp); 2593 return err; 2594 } 2595 2596 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 2597 struct nlattr **attrs, 2598 struct netlink_ext_ack *extack) 2599 { 2600 struct net *net = sock_net(skb->sk); 2601 struct xfrm_state *x; 2602 int err; 2603 struct xfrm_user_expire *ue = nlmsg_data(nlh); 2604 struct xfrm_usersa_info *p = &ue->state; 2605 struct xfrm_mark m; 2606 u32 mark = xfrm_mark_get(attrs, &m); 2607 2608 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family); 2609 2610 err = -ENOENT; 2611 if (x == NULL) 2612 return err; 2613 2614 spin_lock_bh(&x->lock); 2615 err = -EINVAL; 2616 if (x->km.state != XFRM_STATE_VALID) { 2617 NL_SET_ERR_MSG(extack, "SA must be in VALID state"); 2618 goto out; 2619 } 2620 2621 km_state_expired(x, ue->hard, nlh->nlmsg_pid); 2622 2623 if (ue->hard) { 2624 __xfrm_state_delete(x); 2625 xfrm_audit_state_delete(x, 1, true); 2626 } 2627 err = 0; 2628 out: 2629 spin_unlock_bh(&x->lock); 2630 xfrm_state_put(x); 2631 return err; 2632 } 2633 2634 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 2635 struct nlattr **attrs, 2636 struct netlink_ext_ack *extack) 2637 { 2638 struct net *net = sock_net(skb->sk); 2639 struct xfrm_policy *xp; 2640 struct xfrm_user_tmpl *ut; 2641 int i; 2642 struct nlattr *rt = attrs[XFRMA_TMPL]; 2643 struct xfrm_mark mark; 2644 2645 struct xfrm_user_acquire *ua = nlmsg_data(nlh); 2646 struct xfrm_state *x = xfrm_state_alloc(net); 2647 int err = -ENOMEM; 2648 2649 if (!x) 2650 goto nomem; 2651 2652 xfrm_mark_get(attrs, &mark); 2653 2654 err = verify_newpolicy_info(&ua->policy, extack); 2655 if (err) 2656 goto free_state; 2657 err = verify_sec_ctx_len(attrs, extack); 2658 if (err) 2659 goto free_state; 2660 2661 /* build an XP */ 2662 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack); 2663 if (!xp) 2664 goto free_state; 2665 2666 memcpy(&x->id, &ua->id, sizeof(ua->id)); 2667 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 2668 memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 2669 xp->mark.m = x->mark.m = mark.m; 2670 xp->mark.v = x->mark.v = mark.v; 2671 ut = nla_data(rt); 2672 /* extract the templates and for each call km_key */ 2673 for (i = 0; i < xp->xfrm_nr; i++, ut++) { 2674 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 2675 memcpy(&x->id, &t->id, sizeof(x->id)); 2676 x->props.mode = t->mode; 2677 x->props.reqid = t->reqid; 2678 x->props.family = ut->family; 2679 t->aalgos = ua->aalgos; 2680 t->ealgos = ua->ealgos; 2681 t->calgos = ua->calgos; 2682 err = km_query(x, t, xp); 2683 2684 } 2685 2686 xfrm_state_free(x); 2687 kfree(xp); 2688 2689 return 0; 2690 2691 free_state: 2692 xfrm_state_free(x); 2693 nomem: 2694 return err; 2695 } 2696 2697 #ifdef CONFIG_XFRM_MIGRATE 2698 static int copy_from_user_migrate(struct xfrm_migrate *ma, 2699 struct xfrm_kmaddress *k, 2700 struct nlattr **attrs, int *num, 2701 struct netlink_ext_ack *extack) 2702 { 2703 struct nlattr *rt = attrs[XFRMA_MIGRATE]; 2704 struct xfrm_user_migrate *um; 2705 int i, num_migrate; 2706 2707 if (k != NULL) { 2708 struct xfrm_user_kmaddress *uk; 2709 2710 uk = nla_data(attrs[XFRMA_KMADDRESS]); 2711 memcpy(&k->local, &uk->local, sizeof(k->local)); 2712 memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 2713 k->family = uk->family; 2714 k->reserved = uk->reserved; 2715 } 2716 2717 um = nla_data(rt); 2718 num_migrate = nla_len(rt) / sizeof(*um); 2719 2720 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) { 2721 NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)"); 2722 return -EINVAL; 2723 } 2724 2725 for (i = 0; i < num_migrate; i++, um++, ma++) { 2726 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 2727 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 2728 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 2729 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 2730 2731 ma->proto = um->proto; 2732 ma->mode = um->mode; 2733 ma->reqid = um->reqid; 2734 2735 ma->old_family = um->old_family; 2736 ma->new_family = um->new_family; 2737 } 2738 2739 *num = i; 2740 return 0; 2741 } 2742 2743 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2744 struct nlattr **attrs, struct netlink_ext_ack *extack) 2745 { 2746 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 2747 struct xfrm_migrate m[XFRM_MAX_DEPTH]; 2748 struct xfrm_kmaddress km, *kmp; 2749 u8 type; 2750 int err; 2751 int n = 0; 2752 struct net *net = sock_net(skb->sk); 2753 struct xfrm_encap_tmpl *encap = NULL; 2754 u32 if_id = 0; 2755 2756 if (!attrs[XFRMA_MIGRATE]) { 2757 NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute"); 2758 return -EINVAL; 2759 } 2760 2761 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 2762 2763 err = copy_from_user_policy_type(&type, attrs, extack); 2764 if (err) 2765 return err; 2766 2767 err = copy_from_user_migrate(m, kmp, attrs, &n, extack); 2768 if (err) 2769 return err; 2770 2771 if (!n) 2772 return 0; 2773 2774 if (attrs[XFRMA_ENCAP]) { 2775 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 2776 sizeof(*encap), GFP_KERNEL); 2777 if (!encap) 2778 return -ENOMEM; 2779 } 2780 2781 if (attrs[XFRMA_IF_ID]) 2782 if_id = nla_get_u32(attrs[XFRMA_IF_ID]); 2783 2784 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, 2785 if_id, extack); 2786 2787 kfree(encap); 2788 2789 return err; 2790 } 2791 #else 2792 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 2793 struct nlattr **attrs, struct netlink_ext_ack *extack) 2794 { 2795 return -ENOPROTOOPT; 2796 } 2797 #endif 2798 2799 #ifdef CONFIG_XFRM_MIGRATE 2800 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb) 2801 { 2802 struct xfrm_user_migrate um; 2803 2804 memset(&um, 0, sizeof(um)); 2805 um.proto = m->proto; 2806 um.mode = m->mode; 2807 um.reqid = m->reqid; 2808 um.old_family = m->old_family; 2809 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 2810 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 2811 um.new_family = m->new_family; 2812 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 2813 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 2814 2815 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 2816 } 2817 2818 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb) 2819 { 2820 struct xfrm_user_kmaddress uk; 2821 2822 memset(&uk, 0, sizeof(uk)); 2823 uk.family = k->family; 2824 uk.reserved = k->reserved; 2825 memcpy(&uk.local, &k->local, sizeof(uk.local)); 2826 memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 2827 2828 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 2829 } 2830 2831 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma, 2832 int with_encp) 2833 { 2834 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 2835 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 2836 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) 2837 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 2838 + userpolicy_type_attrsize(); 2839 } 2840 2841 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m, 2842 int num_migrate, const struct xfrm_kmaddress *k, 2843 const struct xfrm_selector *sel, 2844 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type) 2845 { 2846 const struct xfrm_migrate *mp; 2847 struct xfrm_userpolicy_id *pol_id; 2848 struct nlmsghdr *nlh; 2849 int i, err; 2850 2851 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 2852 if (nlh == NULL) 2853 return -EMSGSIZE; 2854 2855 pol_id = nlmsg_data(nlh); 2856 /* copy data from selector, dir, and type to the pol_id */ 2857 memset(pol_id, 0, sizeof(*pol_id)); 2858 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 2859 pol_id->dir = dir; 2860 2861 if (k != NULL) { 2862 err = copy_to_user_kmaddress(k, skb); 2863 if (err) 2864 goto out_cancel; 2865 } 2866 if (encap) { 2867 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap); 2868 if (err) 2869 goto out_cancel; 2870 } 2871 err = copy_to_user_policy_type(type, skb); 2872 if (err) 2873 goto out_cancel; 2874 for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 2875 err = copy_to_user_migrate(mp, skb); 2876 if (err) 2877 goto out_cancel; 2878 } 2879 2880 nlmsg_end(skb, nlh); 2881 return 0; 2882 2883 out_cancel: 2884 nlmsg_cancel(skb, nlh); 2885 return err; 2886 } 2887 2888 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2889 const struct xfrm_migrate *m, int num_migrate, 2890 const struct xfrm_kmaddress *k, 2891 const struct xfrm_encap_tmpl *encap) 2892 { 2893 struct net *net = &init_net; 2894 struct sk_buff *skb; 2895 int err; 2896 2897 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap), 2898 GFP_ATOMIC); 2899 if (skb == NULL) 2900 return -ENOMEM; 2901 2902 /* build migrate */ 2903 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type); 2904 BUG_ON(err < 0); 2905 2906 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE); 2907 } 2908 #else 2909 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 2910 const struct xfrm_migrate *m, int num_migrate, 2911 const struct xfrm_kmaddress *k, 2912 const struct xfrm_encap_tmpl *encap) 2913 { 2914 return -ENOPROTOOPT; 2915 } 2916 #endif 2917 2918 #define XMSGSIZE(type) sizeof(struct type) 2919 2920 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 2921 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2922 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2923 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2924 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2925 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2926 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2927 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2928 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 2929 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2930 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2931 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2932 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2933 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2934 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2935 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2936 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2937 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 2938 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2939 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2940 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2941 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2942 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 2943 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default), 2944 }; 2945 EXPORT_SYMBOL_GPL(xfrm_msg_min); 2946 2947 #undef XMSGSIZE 2948 2949 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2950 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2951 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2952 [XFRMA_LASTUSED] = { .type = NLA_U64}, 2953 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 2954 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2955 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2956 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2957 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2958 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2959 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2960 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2961 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2962 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2963 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2964 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2965 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2966 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2967 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2968 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 2969 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 2970 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) }, 2971 [XFRMA_TFCPAD] = { .type = NLA_U32 }, 2972 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) }, 2973 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 }, 2974 [XFRMA_PROTO] = { .type = NLA_U8 }, 2975 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) }, 2976 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) }, 2977 [XFRMA_SET_MARK] = { .type = NLA_U32 }, 2978 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 }, 2979 [XFRMA_IF_ID] = { .type = NLA_U32 }, 2980 }; 2981 EXPORT_SYMBOL_GPL(xfrma_policy); 2982 2983 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = { 2984 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2985 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) }, 2986 }; 2987 2988 static const struct xfrm_link { 2989 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **, 2990 struct netlink_ext_ack *); 2991 int (*start)(struct netlink_callback *); 2992 int (*dump)(struct sk_buff *, struct netlink_callback *); 2993 int (*done)(struct netlink_callback *); 2994 const struct nla_policy *nla_pol; 2995 int nla_max; 2996 } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2997 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2998 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2999 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 3000 .dump = xfrm_dump_sa, 3001 .done = xfrm_dump_sa_done }, 3002 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 3003 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 3004 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 3005 .start = xfrm_dump_policy_start, 3006 .dump = xfrm_dump_policy, 3007 .done = xfrm_dump_policy_done }, 3008 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 3009 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 3010 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 3011 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 3012 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 3013 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 3014 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 3015 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 3016 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 3017 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 3018 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 3019 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 3020 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo, 3021 .nla_pol = xfrma_spd_policy, 3022 .nla_max = XFRMA_SPD_MAX }, 3023 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 3024 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default }, 3025 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default }, 3026 }; 3027 3028 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 3029 struct netlink_ext_ack *extack) 3030 { 3031 struct net *net = sock_net(skb->sk); 3032 struct nlattr *attrs[XFRMA_MAX+1]; 3033 const struct xfrm_link *link; 3034 struct nlmsghdr *nlh64 = NULL; 3035 int type, err; 3036 3037 type = nlh->nlmsg_type; 3038 if (type > XFRM_MSG_MAX) 3039 return -EINVAL; 3040 3041 type -= XFRM_MSG_BASE; 3042 link = &xfrm_dispatch[type]; 3043 3044 /* All operations require privileges, even GET */ 3045 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) 3046 return -EPERM; 3047 3048 if (in_compat_syscall()) { 3049 struct xfrm_translator *xtr = xfrm_get_translator(); 3050 3051 if (!xtr) 3052 return -EOPNOTSUPP; 3053 3054 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max, 3055 link->nla_pol, extack); 3056 xfrm_put_translator(xtr); 3057 if (IS_ERR(nlh64)) 3058 return PTR_ERR(nlh64); 3059 if (nlh64) 3060 nlh = nlh64; 3061 } 3062 3063 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 3064 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 3065 (nlh->nlmsg_flags & NLM_F_DUMP)) { 3066 struct netlink_dump_control c = { 3067 .start = link->start, 3068 .dump = link->dump, 3069 .done = link->done, 3070 }; 3071 3072 if (link->dump == NULL) { 3073 err = -EINVAL; 3074 goto err; 3075 } 3076 3077 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c); 3078 goto err; 3079 } 3080 3081 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs, 3082 link->nla_max ? : XFRMA_MAX, 3083 link->nla_pol ? : xfrma_policy, extack); 3084 if (err < 0) 3085 goto err; 3086 3087 if (link->doit == NULL) { 3088 err = -EINVAL; 3089 goto err; 3090 } 3091 3092 err = link->doit(skb, nlh, attrs, extack); 3093 3094 /* We need to free skb allocated in xfrm_alloc_compat() before 3095 * returning from this function, because consume_skb() won't take 3096 * care of frag_list since netlink destructor sets 3097 * sbk->head to NULL. (see netlink_skb_destructor()) 3098 */ 3099 if (skb_has_frag_list(skb)) { 3100 kfree_skb(skb_shinfo(skb)->frag_list); 3101 skb_shinfo(skb)->frag_list = NULL; 3102 } 3103 3104 err: 3105 kvfree(nlh64); 3106 return err; 3107 } 3108 3109 static void xfrm_netlink_rcv(struct sk_buff *skb) 3110 { 3111 struct net *net = sock_net(skb->sk); 3112 3113 mutex_lock(&net->xfrm.xfrm_cfg_mutex); 3114 netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 3115 mutex_unlock(&net->xfrm.xfrm_cfg_mutex); 3116 } 3117 3118 static inline unsigned int xfrm_expire_msgsize(void) 3119 { 3120 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) 3121 + nla_total_size(sizeof(struct xfrm_mark)); 3122 } 3123 3124 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c) 3125 { 3126 struct xfrm_user_expire *ue; 3127 struct nlmsghdr *nlh; 3128 int err; 3129 3130 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 3131 if (nlh == NULL) 3132 return -EMSGSIZE; 3133 3134 ue = nlmsg_data(nlh); 3135 copy_to_user_state(x, &ue->state); 3136 ue->hard = (c->data.hard != 0) ? 1 : 0; 3137 /* clear the padding bytes */ 3138 memset_after(ue, 0, hard); 3139 3140 err = xfrm_mark_put(skb, &x->mark); 3141 if (err) 3142 return err; 3143 3144 err = xfrm_if_id_put(skb, x->if_id); 3145 if (err) 3146 return err; 3147 3148 nlmsg_end(skb, nlh); 3149 return 0; 3150 } 3151 3152 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c) 3153 { 3154 struct net *net = xs_net(x); 3155 struct sk_buff *skb; 3156 3157 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 3158 if (skb == NULL) 3159 return -ENOMEM; 3160 3161 if (build_expire(skb, x, c) < 0) { 3162 kfree_skb(skb); 3163 return -EMSGSIZE; 3164 } 3165 3166 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 3167 } 3168 3169 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c) 3170 { 3171 struct net *net = xs_net(x); 3172 struct sk_buff *skb; 3173 int err; 3174 3175 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC); 3176 if (skb == NULL) 3177 return -ENOMEM; 3178 3179 err = build_aevent(skb, x, c); 3180 BUG_ON(err < 0); 3181 3182 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS); 3183 } 3184 3185 static int xfrm_notify_sa_flush(const struct km_event *c) 3186 { 3187 struct net *net = c->net; 3188 struct xfrm_usersa_flush *p; 3189 struct nlmsghdr *nlh; 3190 struct sk_buff *skb; 3191 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 3192 3193 skb = nlmsg_new(len, GFP_ATOMIC); 3194 if (skb == NULL) 3195 return -ENOMEM; 3196 3197 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 3198 if (nlh == NULL) { 3199 kfree_skb(skb); 3200 return -EMSGSIZE; 3201 } 3202 3203 p = nlmsg_data(nlh); 3204 p->proto = c->data.proto; 3205 3206 nlmsg_end(skb, nlh); 3207 3208 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 3209 } 3210 3211 static inline unsigned int xfrm_sa_len(struct xfrm_state *x) 3212 { 3213 unsigned int l = 0; 3214 if (x->aead) 3215 l += nla_total_size(aead_len(x->aead)); 3216 if (x->aalg) { 3217 l += nla_total_size(sizeof(struct xfrm_algo) + 3218 (x->aalg->alg_key_len + 7) / 8); 3219 l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 3220 } 3221 if (x->ealg) 3222 l += nla_total_size(xfrm_alg_len(x->ealg)); 3223 if (x->calg) 3224 l += nla_total_size(sizeof(*x->calg)); 3225 if (x->encap) 3226 l += nla_total_size(sizeof(*x->encap)); 3227 if (x->tfcpad) 3228 l += nla_total_size(sizeof(x->tfcpad)); 3229 if (x->replay_esn) 3230 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn)); 3231 else 3232 l += nla_total_size(sizeof(struct xfrm_replay_state)); 3233 if (x->security) 3234 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 3235 x->security->ctx_len); 3236 if (x->coaddr) 3237 l += nla_total_size(sizeof(*x->coaddr)); 3238 if (x->props.extra_flags) 3239 l += nla_total_size(sizeof(x->props.extra_flags)); 3240 if (x->xso.dev) 3241 l += nla_total_size(sizeof(struct xfrm_user_offload)); 3242 if (x->props.smark.v | x->props.smark.m) { 3243 l += nla_total_size(sizeof(x->props.smark.v)); 3244 l += nla_total_size(sizeof(x->props.smark.m)); 3245 } 3246 if (x->if_id) 3247 l += nla_total_size(sizeof(x->if_id)); 3248 3249 /* Must count x->lastused as it may become non-zero behind our back. */ 3250 l += nla_total_size_64bit(sizeof(u64)); 3251 3252 if (x->mapping_maxage) 3253 l += nla_total_size(sizeof(x->mapping_maxage)); 3254 3255 return l; 3256 } 3257 3258 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c) 3259 { 3260 struct net *net = xs_net(x); 3261 struct xfrm_usersa_info *p; 3262 struct xfrm_usersa_id *id; 3263 struct nlmsghdr *nlh; 3264 struct sk_buff *skb; 3265 unsigned int len = xfrm_sa_len(x); 3266 unsigned int headlen; 3267 int err; 3268 3269 headlen = sizeof(*p); 3270 if (c->event == XFRM_MSG_DELSA) { 3271 len += nla_total_size(headlen); 3272 headlen = sizeof(*id); 3273 len += nla_total_size(sizeof(struct xfrm_mark)); 3274 } 3275 len += NLMSG_ALIGN(headlen); 3276 3277 skb = nlmsg_new(len, GFP_ATOMIC); 3278 if (skb == NULL) 3279 return -ENOMEM; 3280 3281 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 3282 err = -EMSGSIZE; 3283 if (nlh == NULL) 3284 goto out_free_skb; 3285 3286 p = nlmsg_data(nlh); 3287 if (c->event == XFRM_MSG_DELSA) { 3288 struct nlattr *attr; 3289 3290 id = nlmsg_data(nlh); 3291 memset(id, 0, sizeof(*id)); 3292 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 3293 id->spi = x->id.spi; 3294 id->family = x->props.family; 3295 id->proto = x->id.proto; 3296 3297 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 3298 err = -EMSGSIZE; 3299 if (attr == NULL) 3300 goto out_free_skb; 3301 3302 p = nla_data(attr); 3303 } 3304 err = copy_to_user_state_extra(x, p, skb); 3305 if (err) 3306 goto out_free_skb; 3307 3308 nlmsg_end(skb, nlh); 3309 3310 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA); 3311 3312 out_free_skb: 3313 kfree_skb(skb); 3314 return err; 3315 } 3316 3317 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c) 3318 { 3319 3320 switch (c->event) { 3321 case XFRM_MSG_EXPIRE: 3322 return xfrm_exp_state_notify(x, c); 3323 case XFRM_MSG_NEWAE: 3324 return xfrm_aevent_state_notify(x, c); 3325 case XFRM_MSG_DELSA: 3326 case XFRM_MSG_UPDSA: 3327 case XFRM_MSG_NEWSA: 3328 return xfrm_notify_sa(x, c); 3329 case XFRM_MSG_FLUSHSA: 3330 return xfrm_notify_sa_flush(c); 3331 default: 3332 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n", 3333 c->event); 3334 break; 3335 } 3336 3337 return 0; 3338 3339 } 3340 3341 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x, 3342 struct xfrm_policy *xp) 3343 { 3344 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 3345 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 3346 + nla_total_size(sizeof(struct xfrm_mark)) 3347 + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 3348 + userpolicy_type_attrsize(); 3349 } 3350 3351 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 3352 struct xfrm_tmpl *xt, struct xfrm_policy *xp) 3353 { 3354 __u32 seq = xfrm_get_acqseq(); 3355 struct xfrm_user_acquire *ua; 3356 struct nlmsghdr *nlh; 3357 int err; 3358 3359 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 3360 if (nlh == NULL) 3361 return -EMSGSIZE; 3362 3363 ua = nlmsg_data(nlh); 3364 memcpy(&ua->id, &x->id, sizeof(ua->id)); 3365 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 3366 memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 3367 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT); 3368 ua->aalgos = xt->aalgos; 3369 ua->ealgos = xt->ealgos; 3370 ua->calgos = xt->calgos; 3371 ua->seq = x->km.seq = seq; 3372 3373 err = copy_to_user_tmpl(xp, skb); 3374 if (!err) 3375 err = copy_to_user_state_sec_ctx(x, skb); 3376 if (!err) 3377 err = copy_to_user_policy_type(xp->type, skb); 3378 if (!err) 3379 err = xfrm_mark_put(skb, &xp->mark); 3380 if (!err) 3381 err = xfrm_if_id_put(skb, xp->if_id); 3382 if (err) { 3383 nlmsg_cancel(skb, nlh); 3384 return err; 3385 } 3386 3387 nlmsg_end(skb, nlh); 3388 return 0; 3389 } 3390 3391 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 3392 struct xfrm_policy *xp) 3393 { 3394 struct net *net = xs_net(x); 3395 struct sk_buff *skb; 3396 int err; 3397 3398 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 3399 if (skb == NULL) 3400 return -ENOMEM; 3401 3402 err = build_acquire(skb, x, xt, xp); 3403 BUG_ON(err < 0); 3404 3405 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE); 3406 } 3407 3408 /* User gives us xfrm_user_policy_info followed by an array of 0 3409 * or more templates. 3410 */ 3411 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 3412 u8 *data, int len, int *dir) 3413 { 3414 struct net *net = sock_net(sk); 3415 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 3416 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 3417 struct xfrm_policy *xp; 3418 int nr; 3419 3420 switch (sk->sk_family) { 3421 case AF_INET: 3422 if (opt != IP_XFRM_POLICY) { 3423 *dir = -EOPNOTSUPP; 3424 return NULL; 3425 } 3426 break; 3427 #if IS_ENABLED(CONFIG_IPV6) 3428 case AF_INET6: 3429 if (opt != IPV6_XFRM_POLICY) { 3430 *dir = -EOPNOTSUPP; 3431 return NULL; 3432 } 3433 break; 3434 #endif 3435 default: 3436 *dir = -EINVAL; 3437 return NULL; 3438 } 3439 3440 *dir = -EINVAL; 3441 3442 if (len < sizeof(*p) || 3443 verify_newpolicy_info(p, NULL)) 3444 return NULL; 3445 3446 nr = ((len - sizeof(*p)) / sizeof(*ut)); 3447 if (validate_tmpl(nr, ut, p->sel.family, NULL)) 3448 return NULL; 3449 3450 if (p->dir > XFRM_POLICY_OUT) 3451 return NULL; 3452 3453 xp = xfrm_policy_alloc(net, GFP_ATOMIC); 3454 if (xp == NULL) { 3455 *dir = -ENOBUFS; 3456 return NULL; 3457 } 3458 3459 copy_from_user_policy(xp, p); 3460 xp->type = XFRM_POLICY_TYPE_MAIN; 3461 copy_templates(xp, ut, nr); 3462 3463 *dir = p->dir; 3464 3465 return xp; 3466 } 3467 3468 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp) 3469 { 3470 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 3471 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 3472 + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 3473 + nla_total_size(sizeof(struct xfrm_mark)) 3474 + userpolicy_type_attrsize(); 3475 } 3476 3477 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 3478 int dir, const struct km_event *c) 3479 { 3480 struct xfrm_user_polexpire *upe; 3481 int hard = c->data.hard; 3482 struct nlmsghdr *nlh; 3483 int err; 3484 3485 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 3486 if (nlh == NULL) 3487 return -EMSGSIZE; 3488 3489 upe = nlmsg_data(nlh); 3490 copy_to_user_policy(xp, &upe->pol, dir); 3491 err = copy_to_user_tmpl(xp, skb); 3492 if (!err) 3493 err = copy_to_user_sec_ctx(xp, skb); 3494 if (!err) 3495 err = copy_to_user_policy_type(xp->type, skb); 3496 if (!err) 3497 err = xfrm_mark_put(skb, &xp->mark); 3498 if (!err) 3499 err = xfrm_if_id_put(skb, xp->if_id); 3500 if (err) { 3501 nlmsg_cancel(skb, nlh); 3502 return err; 3503 } 3504 upe->hard = !!hard; 3505 3506 nlmsg_end(skb, nlh); 3507 return 0; 3508 } 3509 3510 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 3511 { 3512 struct net *net = xp_net(xp); 3513 struct sk_buff *skb; 3514 int err; 3515 3516 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 3517 if (skb == NULL) 3518 return -ENOMEM; 3519 3520 err = build_polexpire(skb, xp, dir, c); 3521 BUG_ON(err < 0); 3522 3523 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE); 3524 } 3525 3526 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 3527 { 3528 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 3529 struct net *net = xp_net(xp); 3530 struct xfrm_userpolicy_info *p; 3531 struct xfrm_userpolicy_id *id; 3532 struct nlmsghdr *nlh; 3533 struct sk_buff *skb; 3534 unsigned int headlen; 3535 int err; 3536 3537 headlen = sizeof(*p); 3538 if (c->event == XFRM_MSG_DELPOLICY) { 3539 len += nla_total_size(headlen); 3540 headlen = sizeof(*id); 3541 } 3542 len += userpolicy_type_attrsize(); 3543 len += nla_total_size(sizeof(struct xfrm_mark)); 3544 len += NLMSG_ALIGN(headlen); 3545 3546 skb = nlmsg_new(len, GFP_ATOMIC); 3547 if (skb == NULL) 3548 return -ENOMEM; 3549 3550 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0); 3551 err = -EMSGSIZE; 3552 if (nlh == NULL) 3553 goto out_free_skb; 3554 3555 p = nlmsg_data(nlh); 3556 if (c->event == XFRM_MSG_DELPOLICY) { 3557 struct nlattr *attr; 3558 3559 id = nlmsg_data(nlh); 3560 memset(id, 0, sizeof(*id)); 3561 id->dir = dir; 3562 if (c->data.byid) 3563 id->index = xp->index; 3564 else 3565 memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 3566 3567 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 3568 err = -EMSGSIZE; 3569 if (attr == NULL) 3570 goto out_free_skb; 3571 3572 p = nla_data(attr); 3573 } 3574 3575 copy_to_user_policy(xp, p, dir); 3576 err = copy_to_user_tmpl(xp, skb); 3577 if (!err) 3578 err = copy_to_user_policy_type(xp->type, skb); 3579 if (!err) 3580 err = xfrm_mark_put(skb, &xp->mark); 3581 if (!err) 3582 err = xfrm_if_id_put(skb, xp->if_id); 3583 if (err) 3584 goto out_free_skb; 3585 3586 nlmsg_end(skb, nlh); 3587 3588 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 3589 3590 out_free_skb: 3591 kfree_skb(skb); 3592 return err; 3593 } 3594 3595 static int xfrm_notify_policy_flush(const struct km_event *c) 3596 { 3597 struct net *net = c->net; 3598 struct nlmsghdr *nlh; 3599 struct sk_buff *skb; 3600 int err; 3601 3602 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 3603 if (skb == NULL) 3604 return -ENOMEM; 3605 3606 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 3607 err = -EMSGSIZE; 3608 if (nlh == NULL) 3609 goto out_free_skb; 3610 err = copy_to_user_policy_type(c->data.type, skb); 3611 if (err) 3612 goto out_free_skb; 3613 3614 nlmsg_end(skb, nlh); 3615 3616 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY); 3617 3618 out_free_skb: 3619 kfree_skb(skb); 3620 return err; 3621 } 3622 3623 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 3624 { 3625 3626 switch (c->event) { 3627 case XFRM_MSG_NEWPOLICY: 3628 case XFRM_MSG_UPDPOLICY: 3629 case XFRM_MSG_DELPOLICY: 3630 return xfrm_notify_policy(xp, dir, c); 3631 case XFRM_MSG_FLUSHPOLICY: 3632 return xfrm_notify_policy_flush(c); 3633 case XFRM_MSG_POLEXPIRE: 3634 return xfrm_exp_policy_notify(xp, dir, c); 3635 default: 3636 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n", 3637 c->event); 3638 } 3639 3640 return 0; 3641 3642 } 3643 3644 static inline unsigned int xfrm_report_msgsize(void) 3645 { 3646 return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 3647 } 3648 3649 static int build_report(struct sk_buff *skb, u8 proto, 3650 struct xfrm_selector *sel, xfrm_address_t *addr) 3651 { 3652 struct xfrm_user_report *ur; 3653 struct nlmsghdr *nlh; 3654 3655 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 3656 if (nlh == NULL) 3657 return -EMSGSIZE; 3658 3659 ur = nlmsg_data(nlh); 3660 ur->proto = proto; 3661 memcpy(&ur->sel, sel, sizeof(ur->sel)); 3662 3663 if (addr) { 3664 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr); 3665 if (err) { 3666 nlmsg_cancel(skb, nlh); 3667 return err; 3668 } 3669 } 3670 nlmsg_end(skb, nlh); 3671 return 0; 3672 } 3673 3674 static int xfrm_send_report(struct net *net, u8 proto, 3675 struct xfrm_selector *sel, xfrm_address_t *addr) 3676 { 3677 struct sk_buff *skb; 3678 int err; 3679 3680 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 3681 if (skb == NULL) 3682 return -ENOMEM; 3683 3684 err = build_report(skb, proto, sel, addr); 3685 BUG_ON(err < 0); 3686 3687 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT); 3688 } 3689 3690 static inline unsigned int xfrm_mapping_msgsize(void) 3691 { 3692 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 3693 } 3694 3695 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 3696 xfrm_address_t *new_saddr, __be16 new_sport) 3697 { 3698 struct xfrm_user_mapping *um; 3699 struct nlmsghdr *nlh; 3700 3701 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 3702 if (nlh == NULL) 3703 return -EMSGSIZE; 3704 3705 um = nlmsg_data(nlh); 3706 3707 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 3708 um->id.spi = x->id.spi; 3709 um->id.family = x->props.family; 3710 um->id.proto = x->id.proto; 3711 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 3712 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 3713 um->new_sport = new_sport; 3714 um->old_sport = x->encap->encap_sport; 3715 um->reqid = x->props.reqid; 3716 3717 nlmsg_end(skb, nlh); 3718 return 0; 3719 } 3720 3721 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 3722 __be16 sport) 3723 { 3724 struct net *net = xs_net(x); 3725 struct sk_buff *skb; 3726 int err; 3727 3728 if (x->id.proto != IPPROTO_ESP) 3729 return -EINVAL; 3730 3731 if (!x->encap) 3732 return -EINVAL; 3733 3734 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 3735 if (skb == NULL) 3736 return -ENOMEM; 3737 3738 err = build_mapping(skb, x, ipaddr, sport); 3739 BUG_ON(err < 0); 3740 3741 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING); 3742 } 3743 3744 static bool xfrm_is_alive(const struct km_event *c) 3745 { 3746 return (bool)xfrm_acquire_is_on(c->net); 3747 } 3748 3749 static struct xfrm_mgr netlink_mgr = { 3750 .notify = xfrm_send_state_notify, 3751 .acquire = xfrm_send_acquire, 3752 .compile_policy = xfrm_compile_policy, 3753 .notify_policy = xfrm_send_policy_notify, 3754 .report = xfrm_send_report, 3755 .migrate = xfrm_send_migrate, 3756 .new_mapping = xfrm_send_mapping, 3757 .is_alive = xfrm_is_alive, 3758 }; 3759 3760 static int __net_init xfrm_user_net_init(struct net *net) 3761 { 3762 struct sock *nlsk; 3763 struct netlink_kernel_cfg cfg = { 3764 .groups = XFRMNLGRP_MAX, 3765 .input = xfrm_netlink_rcv, 3766 }; 3767 3768 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg); 3769 if (nlsk == NULL) 3770 return -ENOMEM; 3771 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 3772 rcu_assign_pointer(net->xfrm.nlsk, nlsk); 3773 return 0; 3774 } 3775 3776 static void __net_exit xfrm_user_net_pre_exit(struct net *net) 3777 { 3778 RCU_INIT_POINTER(net->xfrm.nlsk, NULL); 3779 } 3780 3781 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 3782 { 3783 struct net *net; 3784 3785 list_for_each_entry(net, net_exit_list, exit_list) 3786 netlink_kernel_release(net->xfrm.nlsk_stash); 3787 } 3788 3789 static struct pernet_operations xfrm_user_net_ops = { 3790 .init = xfrm_user_net_init, 3791 .pre_exit = xfrm_user_net_pre_exit, 3792 .exit_batch = xfrm_user_net_exit, 3793 }; 3794 3795 static int __init xfrm_user_init(void) 3796 { 3797 int rv; 3798 3799 printk(KERN_INFO "Initializing XFRM netlink socket\n"); 3800 3801 rv = register_pernet_subsys(&xfrm_user_net_ops); 3802 if (rv < 0) 3803 return rv; 3804 xfrm_register_km(&netlink_mgr); 3805 return 0; 3806 } 3807 3808 static void __exit xfrm_user_exit(void) 3809 { 3810 xfrm_unregister_km(&netlink_mgr); 3811 unregister_pernet_subsys(&xfrm_user_net_ops); 3812 } 3813 3814 module_init(xfrm_user_init); 3815 module_exit(xfrm_user_exit); 3816 MODULE_LICENSE("GPL"); 3817 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 3818