1 /* xfrm_user.c: User interface to configure xfrm engine. 2 * 3 * Copyright (C) 2002 David S. Miller (davem@redhat.com) 4 * 5 * Changes: 6 * Mitsuru KANDA @USAGI 7 * Kazunori MIYAZAWA @USAGI 8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 9 * IPv6 support 10 * 11 */ 12 13 #include <linux/crypto.h> 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/types.h> 17 #include <linux/slab.h> 18 #include <linux/socket.h> 19 #include <linux/string.h> 20 #include <linux/net.h> 21 #include <linux/skbuff.h> 22 #include <linux/pfkeyv2.h> 23 #include <linux/ipsec.h> 24 #include <linux/init.h> 25 #include <linux/security.h> 26 #include <net/sock.h> 27 #include <net/xfrm.h> 28 #include <net/netlink.h> 29 #include <asm/uaccess.h> 30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 31 #include <linux/in6.h> 32 #endif 33 34 static inline int aead_len(struct xfrm_algo_aead *alg) 35 { 36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); 37 } 38 39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) 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) < xfrm_alg_len(algp)) 49 return -EINVAL; 50 51 switch (type) { 52 case XFRMA_ALG_AUTH: 53 case XFRMA_ALG_CRYPT: 54 case XFRMA_ALG_COMP: 55 break; 56 57 default: 58 return -EINVAL; 59 } 60 61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 62 return 0; 63 } 64 65 static int verify_auth_trunc(struct nlattr **attrs) 66 { 67 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC]; 68 struct xfrm_algo_auth *algp; 69 70 if (!rt) 71 return 0; 72 73 algp = nla_data(rt); 74 if (nla_len(rt) < xfrm_alg_auth_len(algp)) 75 return -EINVAL; 76 77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 78 return 0; 79 } 80 81 static int verify_aead(struct nlattr **attrs) 82 { 83 struct nlattr *rt = attrs[XFRMA_ALG_AEAD]; 84 struct xfrm_algo_aead *algp; 85 86 if (!rt) 87 return 0; 88 89 algp = nla_data(rt); 90 if (nla_len(rt) < aead_len(algp)) 91 return -EINVAL; 92 93 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; 94 return 0; 95 } 96 97 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, 98 xfrm_address_t **addrp) 99 { 100 struct nlattr *rt = attrs[type]; 101 102 if (rt && addrp) 103 *addrp = nla_data(rt); 104 } 105 106 static inline int verify_sec_ctx_len(struct nlattr **attrs) 107 { 108 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 109 struct xfrm_user_sec_ctx *uctx; 110 111 if (!rt) 112 return 0; 113 114 uctx = nla_data(rt); 115 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) 116 return -EINVAL; 117 118 return 0; 119 } 120 121 122 static int verify_newsa_info(struct xfrm_usersa_info *p, 123 struct nlattr **attrs) 124 { 125 int err; 126 127 err = -EINVAL; 128 switch (p->family) { 129 case AF_INET: 130 break; 131 132 case AF_INET6: 133 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 134 break; 135 #else 136 err = -EAFNOSUPPORT; 137 goto out; 138 #endif 139 140 default: 141 goto out; 142 } 143 144 err = -EINVAL; 145 switch (p->id.proto) { 146 case IPPROTO_AH: 147 if ((!attrs[XFRMA_ALG_AUTH] && 148 !attrs[XFRMA_ALG_AUTH_TRUNC]) || 149 attrs[XFRMA_ALG_AEAD] || 150 attrs[XFRMA_ALG_CRYPT] || 151 attrs[XFRMA_ALG_COMP]) 152 goto out; 153 break; 154 155 case IPPROTO_ESP: 156 if (attrs[XFRMA_ALG_COMP]) 157 goto out; 158 if (!attrs[XFRMA_ALG_AUTH] && 159 !attrs[XFRMA_ALG_AUTH_TRUNC] && 160 !attrs[XFRMA_ALG_CRYPT] && 161 !attrs[XFRMA_ALG_AEAD]) 162 goto out; 163 if ((attrs[XFRMA_ALG_AUTH] || 164 attrs[XFRMA_ALG_AUTH_TRUNC] || 165 attrs[XFRMA_ALG_CRYPT]) && 166 attrs[XFRMA_ALG_AEAD]) 167 goto out; 168 break; 169 170 case IPPROTO_COMP: 171 if (!attrs[XFRMA_ALG_COMP] || 172 attrs[XFRMA_ALG_AEAD] || 173 attrs[XFRMA_ALG_AUTH] || 174 attrs[XFRMA_ALG_AUTH_TRUNC] || 175 attrs[XFRMA_ALG_CRYPT]) 176 goto out; 177 break; 178 179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 180 case IPPROTO_DSTOPTS: 181 case IPPROTO_ROUTING: 182 if (attrs[XFRMA_ALG_COMP] || 183 attrs[XFRMA_ALG_AUTH] || 184 attrs[XFRMA_ALG_AUTH_TRUNC] || 185 attrs[XFRMA_ALG_AEAD] || 186 attrs[XFRMA_ALG_CRYPT] || 187 attrs[XFRMA_ENCAP] || 188 attrs[XFRMA_SEC_CTX] || 189 !attrs[XFRMA_COADDR]) 190 goto out; 191 break; 192 #endif 193 194 default: 195 goto out; 196 } 197 198 if ((err = verify_aead(attrs))) 199 goto out; 200 if ((err = verify_auth_trunc(attrs))) 201 goto out; 202 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) 203 goto out; 204 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) 205 goto out; 206 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) 207 goto out; 208 if ((err = verify_sec_ctx_len(attrs))) 209 goto out; 210 211 err = -EINVAL; 212 switch (p->mode) { 213 case XFRM_MODE_TRANSPORT: 214 case XFRM_MODE_TUNNEL: 215 case XFRM_MODE_ROUTEOPTIMIZATION: 216 case XFRM_MODE_BEET: 217 break; 218 219 default: 220 goto out; 221 } 222 223 err = 0; 224 225 out: 226 return err; 227 } 228 229 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, 230 struct xfrm_algo_desc *(*get_byname)(char *, int), 231 struct nlattr *rta) 232 { 233 struct xfrm_algo *p, *ualg; 234 struct xfrm_algo_desc *algo; 235 236 if (!rta) 237 return 0; 238 239 ualg = nla_data(rta); 240 241 algo = get_byname(ualg->alg_name, 1); 242 if (!algo) 243 return -ENOSYS; 244 *props = algo->desc.sadb_alg_id; 245 246 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL); 247 if (!p) 248 return -ENOMEM; 249 250 strcpy(p->alg_name, algo->name); 251 *algpp = p; 252 return 0; 253 } 254 255 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props, 256 struct nlattr *rta) 257 { 258 struct xfrm_algo *ualg; 259 struct xfrm_algo_auth *p; 260 struct xfrm_algo_desc *algo; 261 262 if (!rta) 263 return 0; 264 265 ualg = nla_data(rta); 266 267 algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 268 if (!algo) 269 return -ENOSYS; 270 *props = algo->desc.sadb_alg_id; 271 272 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL); 273 if (!p) 274 return -ENOMEM; 275 276 strcpy(p->alg_name, algo->name); 277 p->alg_key_len = ualg->alg_key_len; 278 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 279 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8); 280 281 *algpp = p; 282 return 0; 283 } 284 285 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props, 286 struct nlattr *rta) 287 { 288 struct xfrm_algo_auth *p, *ualg; 289 struct xfrm_algo_desc *algo; 290 291 if (!rta) 292 return 0; 293 294 ualg = nla_data(rta); 295 296 algo = xfrm_aalg_get_byname(ualg->alg_name, 1); 297 if (!algo) 298 return -ENOSYS; 299 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) 300 return -EINVAL; 301 *props = algo->desc.sadb_alg_id; 302 303 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL); 304 if (!p) 305 return -ENOMEM; 306 307 strcpy(p->alg_name, algo->name); 308 if (!p->alg_trunc_len) 309 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits; 310 311 *algpp = p; 312 return 0; 313 } 314 315 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props, 316 struct nlattr *rta) 317 { 318 struct xfrm_algo_aead *p, *ualg; 319 struct xfrm_algo_desc *algo; 320 321 if (!rta) 322 return 0; 323 324 ualg = nla_data(rta); 325 326 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1); 327 if (!algo) 328 return -ENOSYS; 329 *props = algo->desc.sadb_alg_id; 330 331 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL); 332 if (!p) 333 return -ENOMEM; 334 335 strcpy(p->alg_name, algo->name); 336 *algpp = p; 337 return 0; 338 } 339 340 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) 341 { 342 int len = 0; 343 344 if (xfrm_ctx) { 345 len += sizeof(struct xfrm_user_sec_ctx); 346 len += xfrm_ctx->ctx_len; 347 } 348 return len; 349 } 350 351 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 352 { 353 memcpy(&x->id, &p->id, sizeof(x->id)); 354 memcpy(&x->sel, &p->sel, sizeof(x->sel)); 355 memcpy(&x->lft, &p->lft, sizeof(x->lft)); 356 x->props.mode = p->mode; 357 x->props.replay_window = p->replay_window; 358 x->props.reqid = p->reqid; 359 x->props.family = p->family; 360 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); 361 x->props.flags = p->flags; 362 363 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC)) 364 x->sel.family = p->family; 365 } 366 367 /* 368 * someday when pfkey also has support, we could have the code 369 * somehow made shareable and move it to xfrm_state.c - JHS 370 * 371 */ 372 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) 373 { 374 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 375 struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 376 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; 377 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; 378 379 if (rp) { 380 struct xfrm_replay_state *replay; 381 replay = nla_data(rp); 382 memcpy(&x->replay, replay, sizeof(*replay)); 383 memcpy(&x->preplay, replay, sizeof(*replay)); 384 } 385 386 if (lt) { 387 struct xfrm_lifetime_cur *ltime; 388 ltime = nla_data(lt); 389 x->curlft.bytes = ltime->bytes; 390 x->curlft.packets = ltime->packets; 391 x->curlft.add_time = ltime->add_time; 392 x->curlft.use_time = ltime->use_time; 393 } 394 395 if (et) 396 x->replay_maxage = nla_get_u32(et); 397 398 if (rt) 399 x->replay_maxdiff = nla_get_u32(rt); 400 } 401 402 static struct xfrm_state *xfrm_state_construct(struct net *net, 403 struct xfrm_usersa_info *p, 404 struct nlattr **attrs, 405 int *errp) 406 { 407 struct xfrm_state *x = xfrm_state_alloc(net); 408 int err = -ENOMEM; 409 410 if (!x) 411 goto error_no_put; 412 413 copy_from_user_state(x, p); 414 415 if ((err = attach_aead(&x->aead, &x->props.ealgo, 416 attrs[XFRMA_ALG_AEAD]))) 417 goto error; 418 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo, 419 attrs[XFRMA_ALG_AUTH_TRUNC]))) 420 goto error; 421 if (!x->props.aalgo) { 422 if ((err = attach_auth(&x->aalg, &x->props.aalgo, 423 attrs[XFRMA_ALG_AUTH]))) 424 goto error; 425 } 426 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, 427 xfrm_ealg_get_byname, 428 attrs[XFRMA_ALG_CRYPT]))) 429 goto error; 430 if ((err = attach_one_algo(&x->calg, &x->props.calgo, 431 xfrm_calg_get_byname, 432 attrs[XFRMA_ALG_COMP]))) 433 goto error; 434 435 if (attrs[XFRMA_ENCAP]) { 436 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), 437 sizeof(*x->encap), GFP_KERNEL); 438 if (x->encap == NULL) 439 goto error; 440 } 441 442 if (attrs[XFRMA_COADDR]) { 443 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), 444 sizeof(*x->coaddr), GFP_KERNEL); 445 if (x->coaddr == NULL) 446 goto error; 447 } 448 449 err = xfrm_init_state(x); 450 if (err) 451 goto error; 452 453 if (attrs[XFRMA_SEC_CTX] && 454 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) 455 goto error; 456 457 x->km.seq = p->seq; 458 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth; 459 /* sysctl_xfrm_aevent_etime is in 100ms units */ 460 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M; 461 x->preplay.bitmap = 0; 462 x->preplay.seq = x->replay.seq+x->replay_maxdiff; 463 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; 464 465 /* override default values from above */ 466 467 xfrm_update_ae_params(x, attrs); 468 469 return x; 470 471 error: 472 x->km.state = XFRM_STATE_DEAD; 473 xfrm_state_put(x); 474 error_no_put: 475 *errp = err; 476 return NULL; 477 } 478 479 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 480 struct nlattr **attrs) 481 { 482 struct net *net = sock_net(skb->sk); 483 struct xfrm_usersa_info *p = nlmsg_data(nlh); 484 struct xfrm_state *x; 485 int err; 486 struct km_event c; 487 uid_t loginuid = NETLINK_CB(skb).loginuid; 488 u32 sessionid = NETLINK_CB(skb).sessionid; 489 u32 sid = NETLINK_CB(skb).sid; 490 491 err = verify_newsa_info(p, attrs); 492 if (err) 493 return err; 494 495 x = xfrm_state_construct(net, p, attrs, &err); 496 if (!x) 497 return err; 498 499 xfrm_state_hold(x); 500 if (nlh->nlmsg_type == XFRM_MSG_NEWSA) 501 err = xfrm_state_add(x); 502 else 503 err = xfrm_state_update(x); 504 505 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid); 506 507 if (err < 0) { 508 x->km.state = XFRM_STATE_DEAD; 509 __xfrm_state_put(x); 510 goto out; 511 } 512 513 c.seq = nlh->nlmsg_seq; 514 c.pid = nlh->nlmsg_pid; 515 c.event = nlh->nlmsg_type; 516 517 km_state_notify(x, &c); 518 out: 519 xfrm_state_put(x); 520 return err; 521 } 522 523 static struct xfrm_state *xfrm_user_state_lookup(struct net *net, 524 struct xfrm_usersa_id *p, 525 struct nlattr **attrs, 526 int *errp) 527 { 528 struct xfrm_state *x = NULL; 529 int err; 530 531 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { 532 err = -ESRCH; 533 x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family); 534 } else { 535 xfrm_address_t *saddr = NULL; 536 537 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); 538 if (!saddr) { 539 err = -EINVAL; 540 goto out; 541 } 542 543 err = -ESRCH; 544 x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr, 545 p->proto, p->family); 546 } 547 548 out: 549 if (!x && errp) 550 *errp = err; 551 return x; 552 } 553 554 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 555 struct nlattr **attrs) 556 { 557 struct net *net = sock_net(skb->sk); 558 struct xfrm_state *x; 559 int err = -ESRCH; 560 struct km_event c; 561 struct xfrm_usersa_id *p = nlmsg_data(nlh); 562 uid_t loginuid = NETLINK_CB(skb).loginuid; 563 u32 sessionid = NETLINK_CB(skb).sessionid; 564 u32 sid = NETLINK_CB(skb).sid; 565 566 x = xfrm_user_state_lookup(net, p, attrs, &err); 567 if (x == NULL) 568 return err; 569 570 if ((err = security_xfrm_state_delete(x)) != 0) 571 goto out; 572 573 if (xfrm_state_kern(x)) { 574 err = -EPERM; 575 goto out; 576 } 577 578 err = xfrm_state_delete(x); 579 580 if (err < 0) 581 goto out; 582 583 c.seq = nlh->nlmsg_seq; 584 c.pid = nlh->nlmsg_pid; 585 c.event = nlh->nlmsg_type; 586 km_state_notify(x, &c); 587 588 out: 589 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid); 590 xfrm_state_put(x); 591 return err; 592 } 593 594 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) 595 { 596 memcpy(&p->id, &x->id, sizeof(p->id)); 597 memcpy(&p->sel, &x->sel, sizeof(p->sel)); 598 memcpy(&p->lft, &x->lft, sizeof(p->lft)); 599 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); 600 memcpy(&p->stats, &x->stats, sizeof(p->stats)); 601 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); 602 p->mode = x->props.mode; 603 p->replay_window = x->props.replay_window; 604 p->reqid = x->props.reqid; 605 p->family = x->props.family; 606 p->flags = x->props.flags; 607 p->seq = x->km.seq; 608 } 609 610 struct xfrm_dump_info { 611 struct sk_buff *in_skb; 612 struct sk_buff *out_skb; 613 u32 nlmsg_seq; 614 u16 nlmsg_flags; 615 }; 616 617 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) 618 { 619 struct xfrm_user_sec_ctx *uctx; 620 struct nlattr *attr; 621 int ctx_size = sizeof(*uctx) + s->ctx_len; 622 623 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); 624 if (attr == NULL) 625 return -EMSGSIZE; 626 627 uctx = nla_data(attr); 628 uctx->exttype = XFRMA_SEC_CTX; 629 uctx->len = ctx_size; 630 uctx->ctx_doi = s->ctx_doi; 631 uctx->ctx_alg = s->ctx_alg; 632 uctx->ctx_len = s->ctx_len; 633 memcpy(uctx + 1, s->ctx_str, s->ctx_len); 634 635 return 0; 636 } 637 638 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb) 639 { 640 struct xfrm_algo *algo; 641 struct nlattr *nla; 642 643 nla = nla_reserve(skb, XFRMA_ALG_AUTH, 644 sizeof(*algo) + (auth->alg_key_len + 7) / 8); 645 if (!nla) 646 return -EMSGSIZE; 647 648 algo = nla_data(nla); 649 strcpy(algo->alg_name, auth->alg_name); 650 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8); 651 algo->alg_key_len = auth->alg_key_len; 652 653 return 0; 654 } 655 656 /* Don't change this without updating xfrm_sa_len! */ 657 static int copy_to_user_state_extra(struct xfrm_state *x, 658 struct xfrm_usersa_info *p, 659 struct sk_buff *skb) 660 { 661 copy_to_user_state(x, p); 662 663 if (x->coaddr) 664 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); 665 666 if (x->lastused) 667 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused); 668 669 if (x->aead) 670 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead); 671 if (x->aalg) { 672 if (copy_to_user_auth(x->aalg, skb)) 673 goto nla_put_failure; 674 675 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC, 676 xfrm_alg_auth_len(x->aalg), x->aalg); 677 } 678 if (x->ealg) 679 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg); 680 if (x->calg) 681 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); 682 683 if (x->encap) 684 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); 685 686 if (x->security && copy_sec_ctx(x->security, skb) < 0) 687 goto nla_put_failure; 688 689 return 0; 690 691 nla_put_failure: 692 return -EMSGSIZE; 693 } 694 695 static int dump_one_state(struct xfrm_state *x, int count, void *ptr) 696 { 697 struct xfrm_dump_info *sp = ptr; 698 struct sk_buff *in_skb = sp->in_skb; 699 struct sk_buff *skb = sp->out_skb; 700 struct xfrm_usersa_info *p; 701 struct nlmsghdr *nlh; 702 int err; 703 704 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, 705 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); 706 if (nlh == NULL) 707 return -EMSGSIZE; 708 709 p = nlmsg_data(nlh); 710 711 err = copy_to_user_state_extra(x, p, skb); 712 if (err) 713 goto nla_put_failure; 714 715 nlmsg_end(skb, nlh); 716 return 0; 717 718 nla_put_failure: 719 nlmsg_cancel(skb, nlh); 720 return err; 721 } 722 723 static int xfrm_dump_sa_done(struct netlink_callback *cb) 724 { 725 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 726 xfrm_state_walk_done(walk); 727 return 0; 728 } 729 730 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) 731 { 732 struct net *net = sock_net(skb->sk); 733 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1]; 734 struct xfrm_dump_info info; 735 736 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) > 737 sizeof(cb->args) - sizeof(cb->args[0])); 738 739 info.in_skb = cb->skb; 740 info.out_skb = skb; 741 info.nlmsg_seq = cb->nlh->nlmsg_seq; 742 info.nlmsg_flags = NLM_F_MULTI; 743 744 if (!cb->args[0]) { 745 cb->args[0] = 1; 746 xfrm_state_walk_init(walk, 0); 747 } 748 749 (void) xfrm_state_walk(net, walk, dump_one_state, &info); 750 751 return skb->len; 752 } 753 754 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, 755 struct xfrm_state *x, u32 seq) 756 { 757 struct xfrm_dump_info info; 758 struct sk_buff *skb; 759 760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 761 if (!skb) 762 return ERR_PTR(-ENOMEM); 763 764 info.in_skb = in_skb; 765 info.out_skb = skb; 766 info.nlmsg_seq = seq; 767 info.nlmsg_flags = 0; 768 769 if (dump_one_state(x, 0, &info)) { 770 kfree_skb(skb); 771 return NULL; 772 } 773 774 return skb; 775 } 776 777 static inline size_t xfrm_spdinfo_msgsize(void) 778 { 779 return NLMSG_ALIGN(4) 780 + nla_total_size(sizeof(struct xfrmu_spdinfo)) 781 + nla_total_size(sizeof(struct xfrmu_spdhinfo)); 782 } 783 784 static int build_spdinfo(struct sk_buff *skb, struct net *net, 785 u32 pid, u32 seq, u32 flags) 786 { 787 struct xfrmk_spdinfo si; 788 struct xfrmu_spdinfo spc; 789 struct xfrmu_spdhinfo sph; 790 struct nlmsghdr *nlh; 791 u32 *f; 792 793 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); 794 if (nlh == NULL) /* shouldnt really happen ... */ 795 return -EMSGSIZE; 796 797 f = nlmsg_data(nlh); 798 *f = flags; 799 xfrm_spd_getinfo(net, &si); 800 spc.incnt = si.incnt; 801 spc.outcnt = si.outcnt; 802 spc.fwdcnt = si.fwdcnt; 803 spc.inscnt = si.inscnt; 804 spc.outscnt = si.outscnt; 805 spc.fwdscnt = si.fwdscnt; 806 sph.spdhcnt = si.spdhcnt; 807 sph.spdhmcnt = si.spdhmcnt; 808 809 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); 810 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); 811 812 return nlmsg_end(skb, nlh); 813 814 nla_put_failure: 815 nlmsg_cancel(skb, nlh); 816 return -EMSGSIZE; 817 } 818 819 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 820 struct nlattr **attrs) 821 { 822 struct net *net = sock_net(skb->sk); 823 struct sk_buff *r_skb; 824 u32 *flags = nlmsg_data(nlh); 825 u32 spid = NETLINK_CB(skb).pid; 826 u32 seq = nlh->nlmsg_seq; 827 828 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); 829 if (r_skb == NULL) 830 return -ENOMEM; 831 832 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0) 833 BUG(); 834 835 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); 836 } 837 838 static inline size_t xfrm_sadinfo_msgsize(void) 839 { 840 return NLMSG_ALIGN(4) 841 + nla_total_size(sizeof(struct xfrmu_sadhinfo)) 842 + nla_total_size(4); /* XFRMA_SAD_CNT */ 843 } 844 845 static int build_sadinfo(struct sk_buff *skb, struct net *net, 846 u32 pid, u32 seq, u32 flags) 847 { 848 struct xfrmk_sadinfo si; 849 struct xfrmu_sadhinfo sh; 850 struct nlmsghdr *nlh; 851 u32 *f; 852 853 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); 854 if (nlh == NULL) /* shouldnt really happen ... */ 855 return -EMSGSIZE; 856 857 f = nlmsg_data(nlh); 858 *f = flags; 859 xfrm_sad_getinfo(net, &si); 860 861 sh.sadhmcnt = si.sadhmcnt; 862 sh.sadhcnt = si.sadhcnt; 863 864 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt); 865 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); 866 867 return nlmsg_end(skb, nlh); 868 869 nla_put_failure: 870 nlmsg_cancel(skb, nlh); 871 return -EMSGSIZE; 872 } 873 874 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, 875 struct nlattr **attrs) 876 { 877 struct net *net = sock_net(skb->sk); 878 struct sk_buff *r_skb; 879 u32 *flags = nlmsg_data(nlh); 880 u32 spid = NETLINK_CB(skb).pid; 881 u32 seq = nlh->nlmsg_seq; 882 883 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); 884 if (r_skb == NULL) 885 return -ENOMEM; 886 887 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0) 888 BUG(); 889 890 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid); 891 } 892 893 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 894 struct nlattr **attrs) 895 { 896 struct net *net = sock_net(skb->sk); 897 struct xfrm_usersa_id *p = nlmsg_data(nlh); 898 struct xfrm_state *x; 899 struct sk_buff *resp_skb; 900 int err = -ESRCH; 901 902 x = xfrm_user_state_lookup(net, p, attrs, &err); 903 if (x == NULL) 904 goto out_noput; 905 906 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 907 if (IS_ERR(resp_skb)) { 908 err = PTR_ERR(resp_skb); 909 } else { 910 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); 911 } 912 xfrm_state_put(x); 913 out_noput: 914 return err; 915 } 916 917 static int verify_userspi_info(struct xfrm_userspi_info *p) 918 { 919 switch (p->info.id.proto) { 920 case IPPROTO_AH: 921 case IPPROTO_ESP: 922 break; 923 924 case IPPROTO_COMP: 925 /* IPCOMP spi is 16-bits. */ 926 if (p->max >= 0x10000) 927 return -EINVAL; 928 break; 929 930 default: 931 return -EINVAL; 932 } 933 934 if (p->min > p->max) 935 return -EINVAL; 936 937 return 0; 938 } 939 940 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, 941 struct nlattr **attrs) 942 { 943 struct net *net = sock_net(skb->sk); 944 struct xfrm_state *x; 945 struct xfrm_userspi_info *p; 946 struct sk_buff *resp_skb; 947 xfrm_address_t *daddr; 948 int family; 949 int err; 950 951 p = nlmsg_data(nlh); 952 err = verify_userspi_info(p); 953 if (err) 954 goto out_noput; 955 956 family = p->info.family; 957 daddr = &p->info.id.daddr; 958 959 x = NULL; 960 if (p->info.seq) { 961 x = xfrm_find_acq_byseq(net, p->info.seq); 962 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { 963 xfrm_state_put(x); 964 x = NULL; 965 } 966 } 967 968 if (!x) 969 x = xfrm_find_acq(net, p->info.mode, p->info.reqid, 970 p->info.id.proto, daddr, 971 &p->info.saddr, 1, 972 family); 973 err = -ENOENT; 974 if (x == NULL) 975 goto out_noput; 976 977 err = xfrm_alloc_spi(x, p->min, p->max); 978 if (err) 979 goto out; 980 981 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); 982 if (IS_ERR(resp_skb)) { 983 err = PTR_ERR(resp_skb); 984 goto out; 985 } 986 987 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid); 988 989 out: 990 xfrm_state_put(x); 991 out_noput: 992 return err; 993 } 994 995 static int verify_policy_dir(u8 dir) 996 { 997 switch (dir) { 998 case XFRM_POLICY_IN: 999 case XFRM_POLICY_OUT: 1000 case XFRM_POLICY_FWD: 1001 break; 1002 1003 default: 1004 return -EINVAL; 1005 } 1006 1007 return 0; 1008 } 1009 1010 static int verify_policy_type(u8 type) 1011 { 1012 switch (type) { 1013 case XFRM_POLICY_TYPE_MAIN: 1014 #ifdef CONFIG_XFRM_SUB_POLICY 1015 case XFRM_POLICY_TYPE_SUB: 1016 #endif 1017 break; 1018 1019 default: 1020 return -EINVAL; 1021 } 1022 1023 return 0; 1024 } 1025 1026 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) 1027 { 1028 switch (p->share) { 1029 case XFRM_SHARE_ANY: 1030 case XFRM_SHARE_SESSION: 1031 case XFRM_SHARE_USER: 1032 case XFRM_SHARE_UNIQUE: 1033 break; 1034 1035 default: 1036 return -EINVAL; 1037 } 1038 1039 switch (p->action) { 1040 case XFRM_POLICY_ALLOW: 1041 case XFRM_POLICY_BLOCK: 1042 break; 1043 1044 default: 1045 return -EINVAL; 1046 } 1047 1048 switch (p->sel.family) { 1049 case AF_INET: 1050 break; 1051 1052 case AF_INET6: 1053 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 1054 break; 1055 #else 1056 return -EAFNOSUPPORT; 1057 #endif 1058 1059 default: 1060 return -EINVAL; 1061 } 1062 1063 return verify_policy_dir(p->dir); 1064 } 1065 1066 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) 1067 { 1068 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1069 struct xfrm_user_sec_ctx *uctx; 1070 1071 if (!rt) 1072 return 0; 1073 1074 uctx = nla_data(rt); 1075 return security_xfrm_policy_alloc(&pol->security, uctx); 1076 } 1077 1078 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, 1079 int nr) 1080 { 1081 int i; 1082 1083 xp->xfrm_nr = nr; 1084 for (i = 0; i < nr; i++, ut++) { 1085 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 1086 1087 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); 1088 memcpy(&t->saddr, &ut->saddr, 1089 sizeof(xfrm_address_t)); 1090 t->reqid = ut->reqid; 1091 t->mode = ut->mode; 1092 t->share = ut->share; 1093 t->optional = ut->optional; 1094 t->aalgos = ut->aalgos; 1095 t->ealgos = ut->ealgos; 1096 t->calgos = ut->calgos; 1097 /* If all masks are ~0, then we allow all algorithms. */ 1098 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos); 1099 t->encap_family = ut->family; 1100 } 1101 } 1102 1103 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) 1104 { 1105 int i; 1106 1107 if (nr > XFRM_MAX_DEPTH) 1108 return -EINVAL; 1109 1110 for (i = 0; i < nr; i++) { 1111 /* We never validated the ut->family value, so many 1112 * applications simply leave it at zero. The check was 1113 * never made and ut->family was ignored because all 1114 * templates could be assumed to have the same family as 1115 * the policy itself. Now that we will have ipv4-in-ipv6 1116 * and ipv6-in-ipv4 tunnels, this is no longer true. 1117 */ 1118 if (!ut[i].family) 1119 ut[i].family = family; 1120 1121 switch (ut[i].family) { 1122 case AF_INET: 1123 break; 1124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 1125 case AF_INET6: 1126 break; 1127 #endif 1128 default: 1129 return -EINVAL; 1130 } 1131 } 1132 1133 return 0; 1134 } 1135 1136 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) 1137 { 1138 struct nlattr *rt = attrs[XFRMA_TMPL]; 1139 1140 if (!rt) { 1141 pol->xfrm_nr = 0; 1142 } else { 1143 struct xfrm_user_tmpl *utmpl = nla_data(rt); 1144 int nr = nla_len(rt) / sizeof(*utmpl); 1145 int err; 1146 1147 err = validate_tmpl(nr, utmpl, pol->family); 1148 if (err) 1149 return err; 1150 1151 copy_templates(pol, utmpl, nr); 1152 } 1153 return 0; 1154 } 1155 1156 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) 1157 { 1158 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; 1159 struct xfrm_userpolicy_type *upt; 1160 u8 type = XFRM_POLICY_TYPE_MAIN; 1161 int err; 1162 1163 if (rt) { 1164 upt = nla_data(rt); 1165 type = upt->type; 1166 } 1167 1168 err = verify_policy_type(type); 1169 if (err) 1170 return err; 1171 1172 *tp = type; 1173 return 0; 1174 } 1175 1176 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) 1177 { 1178 xp->priority = p->priority; 1179 xp->index = p->index; 1180 memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); 1181 memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); 1182 xp->action = p->action; 1183 xp->flags = p->flags; 1184 xp->family = p->sel.family; 1185 /* XXX xp->share = p->share; */ 1186 } 1187 1188 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) 1189 { 1190 memcpy(&p->sel, &xp->selector, sizeof(p->sel)); 1191 memcpy(&p->lft, &xp->lft, sizeof(p->lft)); 1192 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); 1193 p->priority = xp->priority; 1194 p->index = xp->index; 1195 p->sel.family = xp->family; 1196 p->dir = dir; 1197 p->action = xp->action; 1198 p->flags = xp->flags; 1199 p->share = XFRM_SHARE_ANY; /* XXX xp->share */ 1200 } 1201 1202 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) 1203 { 1204 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL); 1205 int err; 1206 1207 if (!xp) { 1208 *errp = -ENOMEM; 1209 return NULL; 1210 } 1211 1212 copy_from_user_policy(xp, p); 1213 1214 err = copy_from_user_policy_type(&xp->type, attrs); 1215 if (err) 1216 goto error; 1217 1218 if (!(err = copy_from_user_tmpl(xp, attrs))) 1219 err = copy_from_user_sec_ctx(xp, attrs); 1220 if (err) 1221 goto error; 1222 1223 return xp; 1224 error: 1225 *errp = err; 1226 xp->walk.dead = 1; 1227 xfrm_policy_destroy(xp); 1228 return NULL; 1229 } 1230 1231 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1232 struct nlattr **attrs) 1233 { 1234 struct net *net = sock_net(skb->sk); 1235 struct xfrm_userpolicy_info *p = nlmsg_data(nlh); 1236 struct xfrm_policy *xp; 1237 struct km_event c; 1238 int err; 1239 int excl; 1240 uid_t loginuid = NETLINK_CB(skb).loginuid; 1241 u32 sessionid = NETLINK_CB(skb).sessionid; 1242 u32 sid = NETLINK_CB(skb).sid; 1243 1244 err = verify_newpolicy_info(p); 1245 if (err) 1246 return err; 1247 err = verify_sec_ctx_len(attrs); 1248 if (err) 1249 return err; 1250 1251 xp = xfrm_policy_construct(net, p, attrs, &err); 1252 if (!xp) 1253 return err; 1254 1255 /* shouldnt excl be based on nlh flags?? 1256 * Aha! this is anti-netlink really i.e more pfkey derived 1257 * in netlink excl is a flag and you wouldnt need 1258 * a type XFRM_MSG_UPDPOLICY - JHS */ 1259 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; 1260 err = xfrm_policy_insert(p->dir, xp, excl); 1261 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid); 1262 1263 if (err) { 1264 security_xfrm_policy_free(xp->security); 1265 kfree(xp); 1266 return err; 1267 } 1268 1269 c.event = nlh->nlmsg_type; 1270 c.seq = nlh->nlmsg_seq; 1271 c.pid = nlh->nlmsg_pid; 1272 km_policy_notify(xp, p->dir, &c); 1273 1274 xfrm_pol_put(xp); 1275 1276 return 0; 1277 } 1278 1279 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) 1280 { 1281 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; 1282 int i; 1283 1284 if (xp->xfrm_nr == 0) 1285 return 0; 1286 1287 for (i = 0; i < xp->xfrm_nr; i++) { 1288 struct xfrm_user_tmpl *up = &vec[i]; 1289 struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; 1290 1291 memcpy(&up->id, &kp->id, sizeof(up->id)); 1292 up->family = kp->encap_family; 1293 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); 1294 up->reqid = kp->reqid; 1295 up->mode = kp->mode; 1296 up->share = kp->share; 1297 up->optional = kp->optional; 1298 up->aalgos = kp->aalgos; 1299 up->ealgos = kp->ealgos; 1300 up->calgos = kp->calgos; 1301 } 1302 1303 return nla_put(skb, XFRMA_TMPL, 1304 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); 1305 } 1306 1307 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) 1308 { 1309 if (x->security) { 1310 return copy_sec_ctx(x->security, skb); 1311 } 1312 return 0; 1313 } 1314 1315 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) 1316 { 1317 if (xp->security) { 1318 return copy_sec_ctx(xp->security, skb); 1319 } 1320 return 0; 1321 } 1322 static inline size_t userpolicy_type_attrsize(void) 1323 { 1324 #ifdef CONFIG_XFRM_SUB_POLICY 1325 return nla_total_size(sizeof(struct xfrm_userpolicy_type)); 1326 #else 1327 return 0; 1328 #endif 1329 } 1330 1331 #ifdef CONFIG_XFRM_SUB_POLICY 1332 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1333 { 1334 struct xfrm_userpolicy_type upt = { 1335 .type = type, 1336 }; 1337 1338 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); 1339 } 1340 1341 #else 1342 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) 1343 { 1344 return 0; 1345 } 1346 #endif 1347 1348 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) 1349 { 1350 struct xfrm_dump_info *sp = ptr; 1351 struct xfrm_userpolicy_info *p; 1352 struct sk_buff *in_skb = sp->in_skb; 1353 struct sk_buff *skb = sp->out_skb; 1354 struct nlmsghdr *nlh; 1355 1356 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, 1357 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); 1358 if (nlh == NULL) 1359 return -EMSGSIZE; 1360 1361 p = nlmsg_data(nlh); 1362 copy_to_user_policy(xp, p, dir); 1363 if (copy_to_user_tmpl(xp, skb) < 0) 1364 goto nlmsg_failure; 1365 if (copy_to_user_sec_ctx(xp, skb)) 1366 goto nlmsg_failure; 1367 if (copy_to_user_policy_type(xp->type, skb) < 0) 1368 goto nlmsg_failure; 1369 1370 nlmsg_end(skb, nlh); 1371 return 0; 1372 1373 nlmsg_failure: 1374 nlmsg_cancel(skb, nlh); 1375 return -EMSGSIZE; 1376 } 1377 1378 static int xfrm_dump_policy_done(struct netlink_callback *cb) 1379 { 1380 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 1381 1382 xfrm_policy_walk_done(walk); 1383 return 0; 1384 } 1385 1386 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) 1387 { 1388 struct net *net = sock_net(skb->sk); 1389 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1]; 1390 struct xfrm_dump_info info; 1391 1392 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) > 1393 sizeof(cb->args) - sizeof(cb->args[0])); 1394 1395 info.in_skb = cb->skb; 1396 info.out_skb = skb; 1397 info.nlmsg_seq = cb->nlh->nlmsg_seq; 1398 info.nlmsg_flags = NLM_F_MULTI; 1399 1400 if (!cb->args[0]) { 1401 cb->args[0] = 1; 1402 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY); 1403 } 1404 1405 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info); 1406 1407 return skb->len; 1408 } 1409 1410 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, 1411 struct xfrm_policy *xp, 1412 int dir, u32 seq) 1413 { 1414 struct xfrm_dump_info info; 1415 struct sk_buff *skb; 1416 1417 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1418 if (!skb) 1419 return ERR_PTR(-ENOMEM); 1420 1421 info.in_skb = in_skb; 1422 info.out_skb = skb; 1423 info.nlmsg_seq = seq; 1424 info.nlmsg_flags = 0; 1425 1426 if (dump_one_policy(xp, dir, 0, &info) < 0) { 1427 kfree_skb(skb); 1428 return NULL; 1429 } 1430 1431 return skb; 1432 } 1433 1434 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1435 struct nlattr **attrs) 1436 { 1437 struct net *net = sock_net(skb->sk); 1438 struct xfrm_policy *xp; 1439 struct xfrm_userpolicy_id *p; 1440 u8 type = XFRM_POLICY_TYPE_MAIN; 1441 int err; 1442 struct km_event c; 1443 int delete; 1444 1445 p = nlmsg_data(nlh); 1446 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; 1447 1448 err = copy_from_user_policy_type(&type, attrs); 1449 if (err) 1450 return err; 1451 1452 err = verify_policy_dir(p->dir); 1453 if (err) 1454 return err; 1455 1456 if (p->index) 1457 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err); 1458 else { 1459 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1460 struct xfrm_sec_ctx *ctx; 1461 1462 err = verify_sec_ctx_len(attrs); 1463 if (err) 1464 return err; 1465 1466 ctx = NULL; 1467 if (rt) { 1468 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1469 1470 err = security_xfrm_policy_alloc(&ctx, uctx); 1471 if (err) 1472 return err; 1473 } 1474 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 1475 delete, &err); 1476 security_xfrm_policy_free(ctx); 1477 } 1478 if (xp == NULL) 1479 return -ENOENT; 1480 1481 if (!delete) { 1482 struct sk_buff *resp_skb; 1483 1484 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); 1485 if (IS_ERR(resp_skb)) { 1486 err = PTR_ERR(resp_skb); 1487 } else { 1488 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, 1489 NETLINK_CB(skb).pid); 1490 } 1491 } else { 1492 uid_t loginuid = NETLINK_CB(skb).loginuid; 1493 u32 sessionid = NETLINK_CB(skb).sessionid; 1494 u32 sid = NETLINK_CB(skb).sid; 1495 1496 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid, 1497 sid); 1498 1499 if (err != 0) 1500 goto out; 1501 1502 c.data.byid = p->index; 1503 c.event = nlh->nlmsg_type; 1504 c.seq = nlh->nlmsg_seq; 1505 c.pid = nlh->nlmsg_pid; 1506 km_policy_notify(xp, p->dir, &c); 1507 } 1508 1509 out: 1510 xfrm_pol_put(xp); 1511 return err; 1512 } 1513 1514 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, 1515 struct nlattr **attrs) 1516 { 1517 struct net *net = sock_net(skb->sk); 1518 struct km_event c; 1519 struct xfrm_usersa_flush *p = nlmsg_data(nlh); 1520 struct xfrm_audit audit_info; 1521 int err; 1522 1523 audit_info.loginuid = NETLINK_CB(skb).loginuid; 1524 audit_info.sessionid = NETLINK_CB(skb).sessionid; 1525 audit_info.secid = NETLINK_CB(skb).sid; 1526 err = xfrm_state_flush(net, p->proto, &audit_info); 1527 if (err) { 1528 if (err == -ESRCH) /* empty table */ 1529 return 0; 1530 return err; 1531 } 1532 c.data.proto = p->proto; 1533 c.event = nlh->nlmsg_type; 1534 c.seq = nlh->nlmsg_seq; 1535 c.pid = nlh->nlmsg_pid; 1536 c.net = net; 1537 km_state_notify(NULL, &c); 1538 1539 return 0; 1540 } 1541 1542 static inline size_t xfrm_aevent_msgsize(void) 1543 { 1544 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) 1545 + nla_total_size(sizeof(struct xfrm_replay_state)) 1546 + nla_total_size(sizeof(struct xfrm_lifetime_cur)) 1547 + nla_total_size(4) /* XFRM_AE_RTHR */ 1548 + nla_total_size(4); /* XFRM_AE_ETHR */ 1549 } 1550 1551 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) 1552 { 1553 struct xfrm_aevent_id *id; 1554 struct nlmsghdr *nlh; 1555 1556 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); 1557 if (nlh == NULL) 1558 return -EMSGSIZE; 1559 1560 id = nlmsg_data(nlh); 1561 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); 1562 id->sa_id.spi = x->id.spi; 1563 id->sa_id.family = x->props.family; 1564 id->sa_id.proto = x->id.proto; 1565 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); 1566 id->reqid = x->props.reqid; 1567 id->flags = c->data.aevent; 1568 1569 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); 1570 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); 1571 1572 if (id->flags & XFRM_AE_RTHR) 1573 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); 1574 1575 if (id->flags & XFRM_AE_ETHR) 1576 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH, 1577 x->replay_maxage * 10 / HZ); 1578 1579 return nlmsg_end(skb, nlh); 1580 1581 nla_put_failure: 1582 nlmsg_cancel(skb, nlh); 1583 return -EMSGSIZE; 1584 } 1585 1586 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1587 struct nlattr **attrs) 1588 { 1589 struct net *net = sock_net(skb->sk); 1590 struct xfrm_state *x; 1591 struct sk_buff *r_skb; 1592 int err; 1593 struct km_event c; 1594 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1595 struct xfrm_usersa_id *id = &p->sa_id; 1596 1597 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); 1598 if (r_skb == NULL) 1599 return -ENOMEM; 1600 1601 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family); 1602 if (x == NULL) { 1603 kfree_skb(r_skb); 1604 return -ESRCH; 1605 } 1606 1607 /* 1608 * XXX: is this lock really needed - none of the other 1609 * gets lock (the concern is things getting updated 1610 * while we are still reading) - jhs 1611 */ 1612 spin_lock_bh(&x->lock); 1613 c.data.aevent = p->flags; 1614 c.seq = nlh->nlmsg_seq; 1615 c.pid = nlh->nlmsg_pid; 1616 1617 if (build_aevent(r_skb, x, &c) < 0) 1618 BUG(); 1619 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid); 1620 spin_unlock_bh(&x->lock); 1621 xfrm_state_put(x); 1622 return err; 1623 } 1624 1625 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, 1626 struct nlattr **attrs) 1627 { 1628 struct net *net = sock_net(skb->sk); 1629 struct xfrm_state *x; 1630 struct km_event c; 1631 int err = - EINVAL; 1632 struct xfrm_aevent_id *p = nlmsg_data(nlh); 1633 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; 1634 struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; 1635 1636 if (!lt && !rp) 1637 return err; 1638 1639 /* pedantic mode - thou shalt sayeth replaceth */ 1640 if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) 1641 return err; 1642 1643 x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); 1644 if (x == NULL) 1645 return -ESRCH; 1646 1647 if (x->km.state != XFRM_STATE_VALID) 1648 goto out; 1649 1650 spin_lock_bh(&x->lock); 1651 xfrm_update_ae_params(x, attrs); 1652 spin_unlock_bh(&x->lock); 1653 1654 c.event = nlh->nlmsg_type; 1655 c.seq = nlh->nlmsg_seq; 1656 c.pid = nlh->nlmsg_pid; 1657 c.data.aevent = XFRM_AE_CU; 1658 km_state_notify(x, &c); 1659 err = 0; 1660 out: 1661 xfrm_state_put(x); 1662 return err; 1663 } 1664 1665 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, 1666 struct nlattr **attrs) 1667 { 1668 struct net *net = sock_net(skb->sk); 1669 struct km_event c; 1670 u8 type = XFRM_POLICY_TYPE_MAIN; 1671 int err; 1672 struct xfrm_audit audit_info; 1673 1674 err = copy_from_user_policy_type(&type, attrs); 1675 if (err) 1676 return err; 1677 1678 audit_info.loginuid = NETLINK_CB(skb).loginuid; 1679 audit_info.sessionid = NETLINK_CB(skb).sessionid; 1680 audit_info.secid = NETLINK_CB(skb).sid; 1681 err = xfrm_policy_flush(net, type, &audit_info); 1682 if (err) { 1683 if (err == -ESRCH) /* empty table */ 1684 return 0; 1685 return err; 1686 } 1687 1688 c.data.type = type; 1689 c.event = nlh->nlmsg_type; 1690 c.seq = nlh->nlmsg_seq; 1691 c.pid = nlh->nlmsg_pid; 1692 c.net = net; 1693 km_policy_notify(NULL, 0, &c); 1694 return 0; 1695 } 1696 1697 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 1698 struct nlattr **attrs) 1699 { 1700 struct net *net = sock_net(skb->sk); 1701 struct xfrm_policy *xp; 1702 struct xfrm_user_polexpire *up = nlmsg_data(nlh); 1703 struct xfrm_userpolicy_info *p = &up->pol; 1704 u8 type = XFRM_POLICY_TYPE_MAIN; 1705 int err = -ENOENT; 1706 1707 err = copy_from_user_policy_type(&type, attrs); 1708 if (err) 1709 return err; 1710 1711 if (p->index) 1712 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err); 1713 else { 1714 struct nlattr *rt = attrs[XFRMA_SEC_CTX]; 1715 struct xfrm_sec_ctx *ctx; 1716 1717 err = verify_sec_ctx_len(attrs); 1718 if (err) 1719 return err; 1720 1721 ctx = NULL; 1722 if (rt) { 1723 struct xfrm_user_sec_ctx *uctx = nla_data(rt); 1724 1725 err = security_xfrm_policy_alloc(&ctx, uctx); 1726 if (err) 1727 return err; 1728 } 1729 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err); 1730 security_xfrm_policy_free(ctx); 1731 } 1732 if (xp == NULL) 1733 return -ENOENT; 1734 1735 read_lock(&xp->lock); 1736 if (xp->walk.dead) { 1737 read_unlock(&xp->lock); 1738 goto out; 1739 } 1740 1741 read_unlock(&xp->lock); 1742 err = 0; 1743 if (up->hard) { 1744 uid_t loginuid = NETLINK_CB(skb).loginuid; 1745 uid_t sessionid = NETLINK_CB(skb).sessionid; 1746 u32 sid = NETLINK_CB(skb).sid; 1747 xfrm_policy_delete(xp, p->dir); 1748 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid); 1749 1750 } else { 1751 // reset the timers here? 1752 printk("Dont know what to do with soft policy expire\n"); 1753 } 1754 km_policy_expired(xp, p->dir, up->hard, current->pid); 1755 1756 out: 1757 xfrm_pol_put(xp); 1758 return err; 1759 } 1760 1761 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, 1762 struct nlattr **attrs) 1763 { 1764 struct net *net = sock_net(skb->sk); 1765 struct xfrm_state *x; 1766 int err; 1767 struct xfrm_user_expire *ue = nlmsg_data(nlh); 1768 struct xfrm_usersa_info *p = &ue->state; 1769 1770 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family); 1771 1772 err = -ENOENT; 1773 if (x == NULL) 1774 return err; 1775 1776 spin_lock_bh(&x->lock); 1777 err = -EINVAL; 1778 if (x->km.state != XFRM_STATE_VALID) 1779 goto out; 1780 km_state_expired(x, ue->hard, current->pid); 1781 1782 if (ue->hard) { 1783 uid_t loginuid = NETLINK_CB(skb).loginuid; 1784 uid_t sessionid = NETLINK_CB(skb).sessionid; 1785 u32 sid = NETLINK_CB(skb).sid; 1786 __xfrm_state_delete(x); 1787 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid); 1788 } 1789 err = 0; 1790 out: 1791 spin_unlock_bh(&x->lock); 1792 xfrm_state_put(x); 1793 return err; 1794 } 1795 1796 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, 1797 struct nlattr **attrs) 1798 { 1799 struct net *net = sock_net(skb->sk); 1800 struct xfrm_policy *xp; 1801 struct xfrm_user_tmpl *ut; 1802 int i; 1803 struct nlattr *rt = attrs[XFRMA_TMPL]; 1804 1805 struct xfrm_user_acquire *ua = nlmsg_data(nlh); 1806 struct xfrm_state *x = xfrm_state_alloc(net); 1807 int err = -ENOMEM; 1808 1809 if (!x) 1810 goto nomem; 1811 1812 err = verify_newpolicy_info(&ua->policy); 1813 if (err) 1814 goto bad_policy; 1815 1816 /* build an XP */ 1817 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err); 1818 if (!xp) 1819 goto free_state; 1820 1821 memcpy(&x->id, &ua->id, sizeof(ua->id)); 1822 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); 1823 memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); 1824 1825 ut = nla_data(rt); 1826 /* extract the templates and for each call km_key */ 1827 for (i = 0; i < xp->xfrm_nr; i++, ut++) { 1828 struct xfrm_tmpl *t = &xp->xfrm_vec[i]; 1829 memcpy(&x->id, &t->id, sizeof(x->id)); 1830 x->props.mode = t->mode; 1831 x->props.reqid = t->reqid; 1832 x->props.family = ut->family; 1833 t->aalgos = ua->aalgos; 1834 t->ealgos = ua->ealgos; 1835 t->calgos = ua->calgos; 1836 err = km_query(x, t, xp); 1837 1838 } 1839 1840 kfree(x); 1841 kfree(xp); 1842 1843 return 0; 1844 1845 bad_policy: 1846 printk("BAD policy passed\n"); 1847 free_state: 1848 kfree(x); 1849 nomem: 1850 return err; 1851 } 1852 1853 #ifdef CONFIG_XFRM_MIGRATE 1854 static int copy_from_user_migrate(struct xfrm_migrate *ma, 1855 struct xfrm_kmaddress *k, 1856 struct nlattr **attrs, int *num) 1857 { 1858 struct nlattr *rt = attrs[XFRMA_MIGRATE]; 1859 struct xfrm_user_migrate *um; 1860 int i, num_migrate; 1861 1862 if (k != NULL) { 1863 struct xfrm_user_kmaddress *uk; 1864 1865 uk = nla_data(attrs[XFRMA_KMADDRESS]); 1866 memcpy(&k->local, &uk->local, sizeof(k->local)); 1867 memcpy(&k->remote, &uk->remote, sizeof(k->remote)); 1868 k->family = uk->family; 1869 k->reserved = uk->reserved; 1870 } 1871 1872 um = nla_data(rt); 1873 num_migrate = nla_len(rt) / sizeof(*um); 1874 1875 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) 1876 return -EINVAL; 1877 1878 for (i = 0; i < num_migrate; i++, um++, ma++) { 1879 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); 1880 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); 1881 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); 1882 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); 1883 1884 ma->proto = um->proto; 1885 ma->mode = um->mode; 1886 ma->reqid = um->reqid; 1887 1888 ma->old_family = um->old_family; 1889 ma->new_family = um->new_family; 1890 } 1891 1892 *num = i; 1893 return 0; 1894 } 1895 1896 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 1897 struct nlattr **attrs) 1898 { 1899 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); 1900 struct xfrm_migrate m[XFRM_MAX_DEPTH]; 1901 struct xfrm_kmaddress km, *kmp; 1902 u8 type; 1903 int err; 1904 int n = 0; 1905 1906 if (attrs[XFRMA_MIGRATE] == NULL) 1907 return -EINVAL; 1908 1909 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL; 1910 1911 err = copy_from_user_policy_type(&type, attrs); 1912 if (err) 1913 return err; 1914 1915 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n); 1916 if (err) 1917 return err; 1918 1919 if (!n) 1920 return 0; 1921 1922 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp); 1923 1924 return 0; 1925 } 1926 #else 1927 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, 1928 struct nlattr **attrs) 1929 { 1930 return -ENOPROTOOPT; 1931 } 1932 #endif 1933 1934 #ifdef CONFIG_XFRM_MIGRATE 1935 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb) 1936 { 1937 struct xfrm_user_migrate um; 1938 1939 memset(&um, 0, sizeof(um)); 1940 um.proto = m->proto; 1941 um.mode = m->mode; 1942 um.reqid = m->reqid; 1943 um.old_family = m->old_family; 1944 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); 1945 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); 1946 um.new_family = m->new_family; 1947 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); 1948 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); 1949 1950 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); 1951 } 1952 1953 static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb) 1954 { 1955 struct xfrm_user_kmaddress uk; 1956 1957 memset(&uk, 0, sizeof(uk)); 1958 uk.family = k->family; 1959 uk.reserved = k->reserved; 1960 memcpy(&uk.local, &k->local, sizeof(uk.local)); 1961 memcpy(&uk.remote, &k->remote, sizeof(uk.remote)); 1962 1963 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk); 1964 } 1965 1966 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma) 1967 { 1968 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) 1969 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0) 1970 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) 1971 + userpolicy_type_attrsize(); 1972 } 1973 1974 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m, 1975 int num_migrate, struct xfrm_kmaddress *k, 1976 struct xfrm_selector *sel, u8 dir, u8 type) 1977 { 1978 struct xfrm_migrate *mp; 1979 struct xfrm_userpolicy_id *pol_id; 1980 struct nlmsghdr *nlh; 1981 int i; 1982 1983 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); 1984 if (nlh == NULL) 1985 return -EMSGSIZE; 1986 1987 pol_id = nlmsg_data(nlh); 1988 /* copy data from selector, dir, and type to the pol_id */ 1989 memset(pol_id, 0, sizeof(*pol_id)); 1990 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); 1991 pol_id->dir = dir; 1992 1993 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0)) 1994 goto nlmsg_failure; 1995 1996 if (copy_to_user_policy_type(type, skb) < 0) 1997 goto nlmsg_failure; 1998 1999 for (i = 0, mp = m ; i < num_migrate; i++, mp++) { 2000 if (copy_to_user_migrate(mp, skb) < 0) 2001 goto nlmsg_failure; 2002 } 2003 2004 return nlmsg_end(skb, nlh); 2005 nlmsg_failure: 2006 nlmsg_cancel(skb, nlh); 2007 return -EMSGSIZE; 2008 } 2009 2010 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, 2011 struct xfrm_migrate *m, int num_migrate, 2012 struct xfrm_kmaddress *k) 2013 { 2014 struct net *net = &init_net; 2015 struct sk_buff *skb; 2016 2017 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC); 2018 if (skb == NULL) 2019 return -ENOMEM; 2020 2021 /* build migrate */ 2022 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0) 2023 BUG(); 2024 2025 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); 2026 } 2027 #else 2028 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, 2029 struct xfrm_migrate *m, int num_migrate, 2030 struct xfrm_kmaddress *k) 2031 { 2032 return -ENOPROTOOPT; 2033 } 2034 #endif 2035 2036 #define XMSGSIZE(type) sizeof(struct type) 2037 2038 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { 2039 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2040 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2041 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), 2042 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2043 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2044 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2045 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), 2046 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), 2047 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), 2048 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), 2049 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), 2050 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), 2051 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), 2052 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, 2053 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2054 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), 2055 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), 2056 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), 2057 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), 2058 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), 2059 }; 2060 2061 #undef XMSGSIZE 2062 2063 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { 2064 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)}, 2065 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)}, 2066 [XFRMA_LASTUSED] = { .type = NLA_U64}, 2067 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)}, 2068 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) }, 2069 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, 2070 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, 2071 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, 2072 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, 2073 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, 2074 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, 2075 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, 2076 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, 2077 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, 2078 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, 2079 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, 2080 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, 2081 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, 2082 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, 2083 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) }, 2084 }; 2085 2086 static struct xfrm_link { 2087 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); 2088 int (*dump)(struct sk_buff *, struct netlink_callback *); 2089 int (*done)(struct netlink_callback *); 2090 } xfrm_dispatch[XFRM_NR_MSGTYPES] = { 2091 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2092 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, 2093 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, 2094 .dump = xfrm_dump_sa, 2095 .done = xfrm_dump_sa_done }, 2096 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2097 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, 2098 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, 2099 .dump = xfrm_dump_policy, 2100 .done = xfrm_dump_policy_done }, 2101 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, 2102 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, 2103 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, 2104 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, 2105 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, 2106 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, 2107 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, 2108 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, 2109 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, 2110 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, 2111 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, 2112 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, 2113 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, 2114 }; 2115 2116 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 2117 { 2118 struct net *net = sock_net(skb->sk); 2119 struct nlattr *attrs[XFRMA_MAX+1]; 2120 struct xfrm_link *link; 2121 int type, err; 2122 2123 type = nlh->nlmsg_type; 2124 if (type > XFRM_MSG_MAX) 2125 return -EINVAL; 2126 2127 type -= XFRM_MSG_BASE; 2128 link = &xfrm_dispatch[type]; 2129 2130 /* All operations require privileges, even GET */ 2131 if (security_netlink_recv(skb, CAP_NET_ADMIN)) 2132 return -EPERM; 2133 2134 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || 2135 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && 2136 (nlh->nlmsg_flags & NLM_F_DUMP)) { 2137 if (link->dump == NULL) 2138 return -EINVAL; 2139 2140 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done); 2141 } 2142 2143 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, 2144 xfrma_policy); 2145 if (err < 0) 2146 return err; 2147 2148 if (link->doit == NULL) 2149 return -EINVAL; 2150 2151 return link->doit(skb, nlh, attrs); 2152 } 2153 2154 static void xfrm_netlink_rcv(struct sk_buff *skb) 2155 { 2156 mutex_lock(&xfrm_cfg_mutex); 2157 netlink_rcv_skb(skb, &xfrm_user_rcv_msg); 2158 mutex_unlock(&xfrm_cfg_mutex); 2159 } 2160 2161 static inline size_t xfrm_expire_msgsize(void) 2162 { 2163 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)); 2164 } 2165 2166 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) 2167 { 2168 struct xfrm_user_expire *ue; 2169 struct nlmsghdr *nlh; 2170 2171 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); 2172 if (nlh == NULL) 2173 return -EMSGSIZE; 2174 2175 ue = nlmsg_data(nlh); 2176 copy_to_user_state(x, &ue->state); 2177 ue->hard = (c->data.hard != 0) ? 1 : 0; 2178 2179 return nlmsg_end(skb, nlh); 2180 } 2181 2182 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) 2183 { 2184 struct net *net = xs_net(x); 2185 struct sk_buff *skb; 2186 2187 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); 2188 if (skb == NULL) 2189 return -ENOMEM; 2190 2191 if (build_expire(skb, x, c) < 0) 2192 BUG(); 2193 2194 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 2195 } 2196 2197 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) 2198 { 2199 struct net *net = xs_net(x); 2200 struct sk_buff *skb; 2201 2202 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); 2203 if (skb == NULL) 2204 return -ENOMEM; 2205 2206 if (build_aevent(skb, x, c) < 0) 2207 BUG(); 2208 2209 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); 2210 } 2211 2212 static int xfrm_notify_sa_flush(struct km_event *c) 2213 { 2214 struct net *net = c->net; 2215 struct xfrm_usersa_flush *p; 2216 struct nlmsghdr *nlh; 2217 struct sk_buff *skb; 2218 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); 2219 2220 skb = nlmsg_new(len, GFP_ATOMIC); 2221 if (skb == NULL) 2222 return -ENOMEM; 2223 2224 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); 2225 if (nlh == NULL) { 2226 kfree_skb(skb); 2227 return -EMSGSIZE; 2228 } 2229 2230 p = nlmsg_data(nlh); 2231 p->proto = c->data.proto; 2232 2233 nlmsg_end(skb, nlh); 2234 2235 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 2236 } 2237 2238 static inline size_t xfrm_sa_len(struct xfrm_state *x) 2239 { 2240 size_t l = 0; 2241 if (x->aead) 2242 l += nla_total_size(aead_len(x->aead)); 2243 if (x->aalg) { 2244 l += nla_total_size(sizeof(struct xfrm_algo) + 2245 (x->aalg->alg_key_len + 7) / 8); 2246 l += nla_total_size(xfrm_alg_auth_len(x->aalg)); 2247 } 2248 if (x->ealg) 2249 l += nla_total_size(xfrm_alg_len(x->ealg)); 2250 if (x->calg) 2251 l += nla_total_size(sizeof(*x->calg)); 2252 if (x->encap) 2253 l += nla_total_size(sizeof(*x->encap)); 2254 if (x->security) 2255 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + 2256 x->security->ctx_len); 2257 if (x->coaddr) 2258 l += nla_total_size(sizeof(*x->coaddr)); 2259 2260 /* Must count x->lastused as it may become non-zero behind our back. */ 2261 l += nla_total_size(sizeof(u64)); 2262 2263 return l; 2264 } 2265 2266 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) 2267 { 2268 struct net *net = xs_net(x); 2269 struct xfrm_usersa_info *p; 2270 struct xfrm_usersa_id *id; 2271 struct nlmsghdr *nlh; 2272 struct sk_buff *skb; 2273 int len = xfrm_sa_len(x); 2274 int headlen; 2275 2276 headlen = sizeof(*p); 2277 if (c->event == XFRM_MSG_DELSA) { 2278 len += nla_total_size(headlen); 2279 headlen = sizeof(*id); 2280 } 2281 len += NLMSG_ALIGN(headlen); 2282 2283 skb = nlmsg_new(len, GFP_ATOMIC); 2284 if (skb == NULL) 2285 return -ENOMEM; 2286 2287 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 2288 if (nlh == NULL) 2289 goto nla_put_failure; 2290 2291 p = nlmsg_data(nlh); 2292 if (c->event == XFRM_MSG_DELSA) { 2293 struct nlattr *attr; 2294 2295 id = nlmsg_data(nlh); 2296 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); 2297 id->spi = x->id.spi; 2298 id->family = x->props.family; 2299 id->proto = x->id.proto; 2300 2301 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); 2302 if (attr == NULL) 2303 goto nla_put_failure; 2304 2305 p = nla_data(attr); 2306 } 2307 2308 if (copy_to_user_state_extra(x, p, skb)) 2309 goto nla_put_failure; 2310 2311 nlmsg_end(skb, nlh); 2312 2313 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); 2314 2315 nla_put_failure: 2316 /* Somebody screwed up with xfrm_sa_len! */ 2317 WARN_ON(1); 2318 kfree_skb(skb); 2319 return -1; 2320 } 2321 2322 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) 2323 { 2324 2325 switch (c->event) { 2326 case XFRM_MSG_EXPIRE: 2327 return xfrm_exp_state_notify(x, c); 2328 case XFRM_MSG_NEWAE: 2329 return xfrm_aevent_state_notify(x, c); 2330 case XFRM_MSG_DELSA: 2331 case XFRM_MSG_UPDSA: 2332 case XFRM_MSG_NEWSA: 2333 return xfrm_notify_sa(x, c); 2334 case XFRM_MSG_FLUSHSA: 2335 return xfrm_notify_sa_flush(c); 2336 default: 2337 printk("xfrm_user: Unknown SA event %d\n", c->event); 2338 break; 2339 } 2340 2341 return 0; 2342 2343 } 2344 2345 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, 2346 struct xfrm_policy *xp) 2347 { 2348 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) 2349 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2350 + nla_total_size(xfrm_user_sec_ctx_size(x->security)) 2351 + userpolicy_type_attrsize(); 2352 } 2353 2354 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, 2355 struct xfrm_tmpl *xt, struct xfrm_policy *xp, 2356 int dir) 2357 { 2358 struct xfrm_user_acquire *ua; 2359 struct nlmsghdr *nlh; 2360 __u32 seq = xfrm_get_acqseq(); 2361 2362 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); 2363 if (nlh == NULL) 2364 return -EMSGSIZE; 2365 2366 ua = nlmsg_data(nlh); 2367 memcpy(&ua->id, &x->id, sizeof(ua->id)); 2368 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); 2369 memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); 2370 copy_to_user_policy(xp, &ua->policy, dir); 2371 ua->aalgos = xt->aalgos; 2372 ua->ealgos = xt->ealgos; 2373 ua->calgos = xt->calgos; 2374 ua->seq = x->km.seq = seq; 2375 2376 if (copy_to_user_tmpl(xp, skb) < 0) 2377 goto nlmsg_failure; 2378 if (copy_to_user_state_sec_ctx(x, skb)) 2379 goto nlmsg_failure; 2380 if (copy_to_user_policy_type(xp->type, skb) < 0) 2381 goto nlmsg_failure; 2382 2383 return nlmsg_end(skb, nlh); 2384 2385 nlmsg_failure: 2386 nlmsg_cancel(skb, nlh); 2387 return -EMSGSIZE; 2388 } 2389 2390 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, 2391 struct xfrm_policy *xp, int dir) 2392 { 2393 struct net *net = xs_net(x); 2394 struct sk_buff *skb; 2395 2396 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); 2397 if (skb == NULL) 2398 return -ENOMEM; 2399 2400 if (build_acquire(skb, x, xt, xp, dir) < 0) 2401 BUG(); 2402 2403 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); 2404 } 2405 2406 /* User gives us xfrm_user_policy_info followed by an array of 0 2407 * or more templates. 2408 */ 2409 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, 2410 u8 *data, int len, int *dir) 2411 { 2412 struct net *net = sock_net(sk); 2413 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; 2414 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); 2415 struct xfrm_policy *xp; 2416 int nr; 2417 2418 switch (sk->sk_family) { 2419 case AF_INET: 2420 if (opt != IP_XFRM_POLICY) { 2421 *dir = -EOPNOTSUPP; 2422 return NULL; 2423 } 2424 break; 2425 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 2426 case AF_INET6: 2427 if (opt != IPV6_XFRM_POLICY) { 2428 *dir = -EOPNOTSUPP; 2429 return NULL; 2430 } 2431 break; 2432 #endif 2433 default: 2434 *dir = -EINVAL; 2435 return NULL; 2436 } 2437 2438 *dir = -EINVAL; 2439 2440 if (len < sizeof(*p) || 2441 verify_newpolicy_info(p)) 2442 return NULL; 2443 2444 nr = ((len - sizeof(*p)) / sizeof(*ut)); 2445 if (validate_tmpl(nr, ut, p->sel.family)) 2446 return NULL; 2447 2448 if (p->dir > XFRM_POLICY_OUT) 2449 return NULL; 2450 2451 xp = xfrm_policy_alloc(net, GFP_KERNEL); 2452 if (xp == NULL) { 2453 *dir = -ENOBUFS; 2454 return NULL; 2455 } 2456 2457 copy_from_user_policy(xp, p); 2458 xp->type = XFRM_POLICY_TYPE_MAIN; 2459 copy_templates(xp, ut, nr); 2460 2461 *dir = p->dir; 2462 2463 return xp; 2464 } 2465 2466 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) 2467 { 2468 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) 2469 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) 2470 + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) 2471 + userpolicy_type_attrsize(); 2472 } 2473 2474 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, 2475 int dir, struct km_event *c) 2476 { 2477 struct xfrm_user_polexpire *upe; 2478 struct nlmsghdr *nlh; 2479 int hard = c->data.hard; 2480 2481 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); 2482 if (nlh == NULL) 2483 return -EMSGSIZE; 2484 2485 upe = nlmsg_data(nlh); 2486 copy_to_user_policy(xp, &upe->pol, dir); 2487 if (copy_to_user_tmpl(xp, skb) < 0) 2488 goto nlmsg_failure; 2489 if (copy_to_user_sec_ctx(xp, skb)) 2490 goto nlmsg_failure; 2491 if (copy_to_user_policy_type(xp->type, skb) < 0) 2492 goto nlmsg_failure; 2493 upe->hard = !!hard; 2494 2495 return nlmsg_end(skb, nlh); 2496 2497 nlmsg_failure: 2498 nlmsg_cancel(skb, nlh); 2499 return -EMSGSIZE; 2500 } 2501 2502 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) 2503 { 2504 struct net *net = xp_net(xp); 2505 struct sk_buff *skb; 2506 2507 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); 2508 if (skb == NULL) 2509 return -ENOMEM; 2510 2511 if (build_polexpire(skb, xp, dir, c) < 0) 2512 BUG(); 2513 2514 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); 2515 } 2516 2517 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) 2518 { 2519 struct net *net = xp_net(xp); 2520 struct xfrm_userpolicy_info *p; 2521 struct xfrm_userpolicy_id *id; 2522 struct nlmsghdr *nlh; 2523 struct sk_buff *skb; 2524 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); 2525 int headlen; 2526 2527 headlen = sizeof(*p); 2528 if (c->event == XFRM_MSG_DELPOLICY) { 2529 len += nla_total_size(headlen); 2530 headlen = sizeof(*id); 2531 } 2532 len += userpolicy_type_attrsize(); 2533 len += NLMSG_ALIGN(headlen); 2534 2535 skb = nlmsg_new(len, GFP_ATOMIC); 2536 if (skb == NULL) 2537 return -ENOMEM; 2538 2539 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); 2540 if (nlh == NULL) 2541 goto nlmsg_failure; 2542 2543 p = nlmsg_data(nlh); 2544 if (c->event == XFRM_MSG_DELPOLICY) { 2545 struct nlattr *attr; 2546 2547 id = nlmsg_data(nlh); 2548 memset(id, 0, sizeof(*id)); 2549 id->dir = dir; 2550 if (c->data.byid) 2551 id->index = xp->index; 2552 else 2553 memcpy(&id->sel, &xp->selector, sizeof(id->sel)); 2554 2555 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); 2556 if (attr == NULL) 2557 goto nlmsg_failure; 2558 2559 p = nla_data(attr); 2560 } 2561 2562 copy_to_user_policy(xp, p, dir); 2563 if (copy_to_user_tmpl(xp, skb) < 0) 2564 goto nlmsg_failure; 2565 if (copy_to_user_policy_type(xp->type, skb) < 0) 2566 goto nlmsg_failure; 2567 2568 nlmsg_end(skb, nlh); 2569 2570 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 2571 2572 nlmsg_failure: 2573 kfree_skb(skb); 2574 return -1; 2575 } 2576 2577 static int xfrm_notify_policy_flush(struct km_event *c) 2578 { 2579 struct net *net = c->net; 2580 struct nlmsghdr *nlh; 2581 struct sk_buff *skb; 2582 2583 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); 2584 if (skb == NULL) 2585 return -ENOMEM; 2586 2587 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); 2588 if (nlh == NULL) 2589 goto nlmsg_failure; 2590 if (copy_to_user_policy_type(c->data.type, skb) < 0) 2591 goto nlmsg_failure; 2592 2593 nlmsg_end(skb, nlh); 2594 2595 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); 2596 2597 nlmsg_failure: 2598 kfree_skb(skb); 2599 return -1; 2600 } 2601 2602 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) 2603 { 2604 2605 switch (c->event) { 2606 case XFRM_MSG_NEWPOLICY: 2607 case XFRM_MSG_UPDPOLICY: 2608 case XFRM_MSG_DELPOLICY: 2609 return xfrm_notify_policy(xp, dir, c); 2610 case XFRM_MSG_FLUSHPOLICY: 2611 return xfrm_notify_policy_flush(c); 2612 case XFRM_MSG_POLEXPIRE: 2613 return xfrm_exp_policy_notify(xp, dir, c); 2614 default: 2615 printk("xfrm_user: Unknown Policy event %d\n", c->event); 2616 } 2617 2618 return 0; 2619 2620 } 2621 2622 static inline size_t xfrm_report_msgsize(void) 2623 { 2624 return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); 2625 } 2626 2627 static int build_report(struct sk_buff *skb, u8 proto, 2628 struct xfrm_selector *sel, xfrm_address_t *addr) 2629 { 2630 struct xfrm_user_report *ur; 2631 struct nlmsghdr *nlh; 2632 2633 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); 2634 if (nlh == NULL) 2635 return -EMSGSIZE; 2636 2637 ur = nlmsg_data(nlh); 2638 ur->proto = proto; 2639 memcpy(&ur->sel, sel, sizeof(ur->sel)); 2640 2641 if (addr) 2642 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); 2643 2644 return nlmsg_end(skb, nlh); 2645 2646 nla_put_failure: 2647 nlmsg_cancel(skb, nlh); 2648 return -EMSGSIZE; 2649 } 2650 2651 static int xfrm_send_report(struct net *net, u8 proto, 2652 struct xfrm_selector *sel, xfrm_address_t *addr) 2653 { 2654 struct sk_buff *skb; 2655 2656 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); 2657 if (skb == NULL) 2658 return -ENOMEM; 2659 2660 if (build_report(skb, proto, sel, addr) < 0) 2661 BUG(); 2662 2663 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); 2664 } 2665 2666 static inline size_t xfrm_mapping_msgsize(void) 2667 { 2668 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping)); 2669 } 2670 2671 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x, 2672 xfrm_address_t *new_saddr, __be16 new_sport) 2673 { 2674 struct xfrm_user_mapping *um; 2675 struct nlmsghdr *nlh; 2676 2677 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0); 2678 if (nlh == NULL) 2679 return -EMSGSIZE; 2680 2681 um = nlmsg_data(nlh); 2682 2683 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr)); 2684 um->id.spi = x->id.spi; 2685 um->id.family = x->props.family; 2686 um->id.proto = x->id.proto; 2687 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr)); 2688 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr)); 2689 um->new_sport = new_sport; 2690 um->old_sport = x->encap->encap_sport; 2691 um->reqid = x->props.reqid; 2692 2693 return nlmsg_end(skb, nlh); 2694 } 2695 2696 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, 2697 __be16 sport) 2698 { 2699 struct net *net = xs_net(x); 2700 struct sk_buff *skb; 2701 2702 if (x->id.proto != IPPROTO_ESP) 2703 return -EINVAL; 2704 2705 if (!x->encap) 2706 return -EINVAL; 2707 2708 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC); 2709 if (skb == NULL) 2710 return -ENOMEM; 2711 2712 if (build_mapping(skb, x, ipaddr, sport) < 0) 2713 BUG(); 2714 2715 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC); 2716 } 2717 2718 static struct xfrm_mgr netlink_mgr = { 2719 .id = "netlink", 2720 .notify = xfrm_send_state_notify, 2721 .acquire = xfrm_send_acquire, 2722 .compile_policy = xfrm_compile_policy, 2723 .notify_policy = xfrm_send_policy_notify, 2724 .report = xfrm_send_report, 2725 .migrate = xfrm_send_migrate, 2726 .new_mapping = xfrm_send_mapping, 2727 }; 2728 2729 static int __net_init xfrm_user_net_init(struct net *net) 2730 { 2731 struct sock *nlsk; 2732 2733 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX, 2734 xfrm_netlink_rcv, NULL, THIS_MODULE); 2735 if (nlsk == NULL) 2736 return -ENOMEM; 2737 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */ 2738 rcu_assign_pointer(net->xfrm.nlsk, nlsk); 2739 return 0; 2740 } 2741 2742 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list) 2743 { 2744 struct net *net; 2745 list_for_each_entry(net, net_exit_list, exit_list) 2746 rcu_assign_pointer(net->xfrm.nlsk, NULL); 2747 synchronize_net(); 2748 list_for_each_entry(net, net_exit_list, exit_list) 2749 netlink_kernel_release(net->xfrm.nlsk_stash); 2750 } 2751 2752 static struct pernet_operations xfrm_user_net_ops = { 2753 .init = xfrm_user_net_init, 2754 .exit_batch = xfrm_user_net_exit, 2755 }; 2756 2757 static int __init xfrm_user_init(void) 2758 { 2759 int rv; 2760 2761 printk(KERN_INFO "Initializing XFRM netlink socket\n"); 2762 2763 rv = register_pernet_subsys(&xfrm_user_net_ops); 2764 if (rv < 0) 2765 return rv; 2766 rv = xfrm_register_km(&netlink_mgr); 2767 if (rv < 0) 2768 unregister_pernet_subsys(&xfrm_user_net_ops); 2769 return rv; 2770 } 2771 2772 static void __exit xfrm_user_exit(void) 2773 { 2774 xfrm_unregister_km(&netlink_mgr); 2775 unregister_pernet_subsys(&xfrm_user_net_ops); 2776 } 2777 2778 module_init(xfrm_user_init); 2779 module_exit(xfrm_user_exit); 2780 MODULE_LICENSE("GPL"); 2781 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); 2782 2783