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