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