1 /* 2 * net/key/af_key.c An implementation of PF_KEYv2 sockets. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Maxim Giryaev <gem@asplinux.ru> 10 * David S. Miller <davem@redhat.com> 11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org> 14 * Derek Atkins <derek@ihtfp.com> 15 */ 16 17 #include <linux/capability.h> 18 #include <linux/module.h> 19 #include <linux/kernel.h> 20 #include <linux/socket.h> 21 #include <linux/pfkeyv2.h> 22 #include <linux/ipsec.h> 23 #include <linux/skbuff.h> 24 #include <linux/rtnetlink.h> 25 #include <linux/in.h> 26 #include <linux/in6.h> 27 #include <linux/proc_fs.h> 28 #include <linux/init.h> 29 #include <linux/slab.h> 30 #include <net/net_namespace.h> 31 #include <net/netns/generic.h> 32 #include <net/xfrm.h> 33 34 #include <net/sock.h> 35 36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x)) 37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x)) 38 39 static int pfkey_net_id __read_mostly; 40 struct netns_pfkey { 41 /* List of all pfkey sockets. */ 42 struct hlist_head table; 43 atomic_t socks_nr; 44 }; 45 static DEFINE_MUTEX(pfkey_mutex); 46 47 #define DUMMY_MARK 0 48 static struct xfrm_mark dummy_mark = {0, 0}; 49 struct pfkey_sock { 50 /* struct sock must be the first member of struct pfkey_sock */ 51 struct sock sk; 52 int registered; 53 int promisc; 54 55 struct { 56 uint8_t msg_version; 57 uint32_t msg_portid; 58 int (*dump)(struct pfkey_sock *sk); 59 void (*done)(struct pfkey_sock *sk); 60 union { 61 struct xfrm_policy_walk policy; 62 struct xfrm_state_walk state; 63 } u; 64 struct sk_buff *skb; 65 } dump; 66 }; 67 68 static inline struct pfkey_sock *pfkey_sk(struct sock *sk) 69 { 70 return (struct pfkey_sock *)sk; 71 } 72 73 static int pfkey_can_dump(const struct sock *sk) 74 { 75 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf) 76 return 1; 77 return 0; 78 } 79 80 static void pfkey_terminate_dump(struct pfkey_sock *pfk) 81 { 82 if (pfk->dump.dump) { 83 if (pfk->dump.skb) { 84 kfree_skb(pfk->dump.skb); 85 pfk->dump.skb = NULL; 86 } 87 pfk->dump.done(pfk); 88 pfk->dump.dump = NULL; 89 pfk->dump.done = NULL; 90 } 91 } 92 93 static void pfkey_sock_destruct(struct sock *sk) 94 { 95 struct net *net = sock_net(sk); 96 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 97 98 pfkey_terminate_dump(pfkey_sk(sk)); 99 skb_queue_purge(&sk->sk_receive_queue); 100 101 if (!sock_flag(sk, SOCK_DEAD)) { 102 pr_err("Attempt to release alive pfkey socket: %p\n", sk); 103 return; 104 } 105 106 WARN_ON(atomic_read(&sk->sk_rmem_alloc)); 107 WARN_ON(atomic_read(&sk->sk_wmem_alloc)); 108 109 atomic_dec(&net_pfkey->socks_nr); 110 } 111 112 static const struct proto_ops pfkey_ops; 113 114 static void pfkey_insert(struct sock *sk) 115 { 116 struct net *net = sock_net(sk); 117 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 118 119 mutex_lock(&pfkey_mutex); 120 sk_add_node_rcu(sk, &net_pfkey->table); 121 mutex_unlock(&pfkey_mutex); 122 } 123 124 static void pfkey_remove(struct sock *sk) 125 { 126 mutex_lock(&pfkey_mutex); 127 sk_del_node_init_rcu(sk); 128 mutex_unlock(&pfkey_mutex); 129 } 130 131 static struct proto key_proto = { 132 .name = "KEY", 133 .owner = THIS_MODULE, 134 .obj_size = sizeof(struct pfkey_sock), 135 }; 136 137 static int pfkey_create(struct net *net, struct socket *sock, int protocol, 138 int kern) 139 { 140 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 141 struct sock *sk; 142 int err; 143 144 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 145 return -EPERM; 146 if (sock->type != SOCK_RAW) 147 return -ESOCKTNOSUPPORT; 148 if (protocol != PF_KEY_V2) 149 return -EPROTONOSUPPORT; 150 151 err = -ENOMEM; 152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto); 153 if (sk == NULL) 154 goto out; 155 156 sock->ops = &pfkey_ops; 157 sock_init_data(sock, sk); 158 159 sk->sk_family = PF_KEY; 160 sk->sk_destruct = pfkey_sock_destruct; 161 162 atomic_inc(&net_pfkey->socks_nr); 163 164 pfkey_insert(sk); 165 166 return 0; 167 out: 168 return err; 169 } 170 171 static int pfkey_release(struct socket *sock) 172 { 173 struct sock *sk = sock->sk; 174 175 if (!sk) 176 return 0; 177 178 pfkey_remove(sk); 179 180 sock_orphan(sk); 181 sock->sk = NULL; 182 skb_queue_purge(&sk->sk_write_queue); 183 184 synchronize_rcu(); 185 sock_put(sk); 186 187 return 0; 188 } 189 190 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2, 191 gfp_t allocation, struct sock *sk) 192 { 193 int err = -ENOBUFS; 194 195 sock_hold(sk); 196 if (*skb2 == NULL) { 197 if (atomic_read(&skb->users) != 1) { 198 *skb2 = skb_clone(skb, allocation); 199 } else { 200 *skb2 = skb; 201 atomic_inc(&skb->users); 202 } 203 } 204 if (*skb2 != NULL) { 205 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) { 206 skb_set_owner_r(*skb2, sk); 207 skb_queue_tail(&sk->sk_receive_queue, *skb2); 208 sk->sk_data_ready(sk, (*skb2)->len); 209 *skb2 = NULL; 210 err = 0; 211 } 212 } 213 sock_put(sk); 214 return err; 215 } 216 217 /* Send SKB to all pfkey sockets matching selected criteria. */ 218 #define BROADCAST_ALL 0 219 #define BROADCAST_ONE 1 220 #define BROADCAST_REGISTERED 2 221 #define BROADCAST_PROMISC_ONLY 4 222 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, 223 int broadcast_flags, struct sock *one_sk, 224 struct net *net) 225 { 226 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 227 struct sock *sk; 228 struct hlist_node *node; 229 struct sk_buff *skb2 = NULL; 230 int err = -ESRCH; 231 232 /* XXX Do we need something like netlink_overrun? I think 233 * XXX PF_KEY socket apps will not mind current behavior. 234 */ 235 if (!skb) 236 return -ENOMEM; 237 238 rcu_read_lock(); 239 sk_for_each_rcu(sk, node, &net_pfkey->table) { 240 struct pfkey_sock *pfk = pfkey_sk(sk); 241 int err2; 242 243 /* Yes, it means that if you are meant to receive this 244 * pfkey message you receive it twice as promiscuous 245 * socket. 246 */ 247 if (pfk->promisc) 248 pfkey_broadcast_one(skb, &skb2, allocation, sk); 249 250 /* the exact target will be processed later */ 251 if (sk == one_sk) 252 continue; 253 if (broadcast_flags != BROADCAST_ALL) { 254 if (broadcast_flags & BROADCAST_PROMISC_ONLY) 255 continue; 256 if ((broadcast_flags & BROADCAST_REGISTERED) && 257 !pfk->registered) 258 continue; 259 if (broadcast_flags & BROADCAST_ONE) 260 continue; 261 } 262 263 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk); 264 265 /* Error is cleare after succecful sending to at least one 266 * registered KM */ 267 if ((broadcast_flags & BROADCAST_REGISTERED) && err) 268 err = err2; 269 } 270 rcu_read_unlock(); 271 272 if (one_sk != NULL) 273 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk); 274 275 kfree_skb(skb2); 276 kfree_skb(skb); 277 return err; 278 } 279 280 static int pfkey_do_dump(struct pfkey_sock *pfk) 281 { 282 struct sadb_msg *hdr; 283 int rc; 284 285 rc = pfk->dump.dump(pfk); 286 if (rc == -ENOBUFS) 287 return 0; 288 289 if (pfk->dump.skb) { 290 if (!pfkey_can_dump(&pfk->sk)) 291 return 0; 292 293 hdr = (struct sadb_msg *) pfk->dump.skb->data; 294 hdr->sadb_msg_seq = 0; 295 hdr->sadb_msg_errno = rc; 296 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, 297 &pfk->sk, sock_net(&pfk->sk)); 298 pfk->dump.skb = NULL; 299 } 300 301 pfkey_terminate_dump(pfk); 302 return rc; 303 } 304 305 static inline void pfkey_hdr_dup(struct sadb_msg *new, 306 const struct sadb_msg *orig) 307 { 308 *new = *orig; 309 } 310 311 static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk) 312 { 313 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL); 314 struct sadb_msg *hdr; 315 316 if (!skb) 317 return -ENOBUFS; 318 319 /* Woe be to the platform trying to support PFKEY yet 320 * having normal errnos outside the 1-255 range, inclusive. 321 */ 322 err = -err; 323 if (err == ERESTARTSYS || 324 err == ERESTARTNOHAND || 325 err == ERESTARTNOINTR) 326 err = EINTR; 327 if (err >= 512) 328 err = EINVAL; 329 BUG_ON(err <= 0 || err >= 256); 330 331 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 332 pfkey_hdr_dup(hdr, orig); 333 hdr->sadb_msg_errno = (uint8_t) err; 334 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / 335 sizeof(uint64_t)); 336 337 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk)); 338 339 return 0; 340 } 341 342 static u8 sadb_ext_min_len[] = { 343 [SADB_EXT_RESERVED] = (u8) 0, 344 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa), 345 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime), 346 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime), 347 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime), 348 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address), 349 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address), 350 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address), 351 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key), 352 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key), 353 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident), 354 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident), 355 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens), 356 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop), 357 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported), 358 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported), 359 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange), 360 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate), 361 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy), 362 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2), 363 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type), 364 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port), 365 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port), 366 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address), 367 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx), 368 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress), 369 }; 370 371 /* Verify sadb_address_{len,prefixlen} against sa_family. */ 372 static int verify_address_len(const void *p) 373 { 374 const struct sadb_address *sp = p; 375 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1); 376 const struct sockaddr_in *sin; 377 #if IS_ENABLED(CONFIG_IPV6) 378 const struct sockaddr_in6 *sin6; 379 #endif 380 int len; 381 382 switch (addr->sa_family) { 383 case AF_INET: 384 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t)); 385 if (sp->sadb_address_len != len || 386 sp->sadb_address_prefixlen > 32) 387 return -EINVAL; 388 break; 389 #if IS_ENABLED(CONFIG_IPV6) 390 case AF_INET6: 391 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t)); 392 if (sp->sadb_address_len != len || 393 sp->sadb_address_prefixlen > 128) 394 return -EINVAL; 395 break; 396 #endif 397 default: 398 /* It is user using kernel to keep track of security 399 * associations for another protocol, such as 400 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify 401 * lengths. 402 * 403 * XXX Actually, association/policy database is not yet 404 * XXX able to cope with arbitrary sockaddr families. 405 * XXX When it can, remove this -EINVAL. -DaveM 406 */ 407 return -EINVAL; 408 break; 409 } 410 411 return 0; 412 } 413 414 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx) 415 { 416 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) + 417 sec_ctx->sadb_x_ctx_len, 418 sizeof(uint64_t)); 419 } 420 421 static inline int verify_sec_ctx_len(const void *p) 422 { 423 const struct sadb_x_sec_ctx *sec_ctx = p; 424 int len = sec_ctx->sadb_x_ctx_len; 425 426 if (len > PAGE_SIZE) 427 return -EINVAL; 428 429 len = pfkey_sec_ctx_len(sec_ctx); 430 431 if (sec_ctx->sadb_x_sec_len != len) 432 return -EINVAL; 433 434 return 0; 435 } 436 437 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx) 438 { 439 struct xfrm_user_sec_ctx *uctx = NULL; 440 int ctx_size = sec_ctx->sadb_x_ctx_len; 441 442 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL); 443 444 if (!uctx) 445 return NULL; 446 447 uctx->len = pfkey_sec_ctx_len(sec_ctx); 448 uctx->exttype = sec_ctx->sadb_x_sec_exttype; 449 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi; 450 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg; 451 uctx->ctx_len = sec_ctx->sadb_x_ctx_len; 452 memcpy(uctx + 1, sec_ctx + 1, 453 uctx->ctx_len); 454 455 return uctx; 456 } 457 458 static int present_and_same_family(const struct sadb_address *src, 459 const struct sadb_address *dst) 460 { 461 const struct sockaddr *s_addr, *d_addr; 462 463 if (!src || !dst) 464 return 0; 465 466 s_addr = (const struct sockaddr *)(src + 1); 467 d_addr = (const struct sockaddr *)(dst + 1); 468 if (s_addr->sa_family != d_addr->sa_family) 469 return 0; 470 if (s_addr->sa_family != AF_INET 471 #if IS_ENABLED(CONFIG_IPV6) 472 && s_addr->sa_family != AF_INET6 473 #endif 474 ) 475 return 0; 476 477 return 1; 478 } 479 480 static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs) 481 { 482 const char *p = (char *) hdr; 483 int len = skb->len; 484 485 len -= sizeof(*hdr); 486 p += sizeof(*hdr); 487 while (len > 0) { 488 const struct sadb_ext *ehdr = (const struct sadb_ext *) p; 489 uint16_t ext_type; 490 int ext_len; 491 492 ext_len = ehdr->sadb_ext_len; 493 ext_len *= sizeof(uint64_t); 494 ext_type = ehdr->sadb_ext_type; 495 if (ext_len < sizeof(uint64_t) || 496 ext_len > len || 497 ext_type == SADB_EXT_RESERVED) 498 return -EINVAL; 499 500 if (ext_type <= SADB_EXT_MAX) { 501 int min = (int) sadb_ext_min_len[ext_type]; 502 if (ext_len < min) 503 return -EINVAL; 504 if (ext_hdrs[ext_type-1] != NULL) 505 return -EINVAL; 506 if (ext_type == SADB_EXT_ADDRESS_SRC || 507 ext_type == SADB_EXT_ADDRESS_DST || 508 ext_type == SADB_EXT_ADDRESS_PROXY || 509 ext_type == SADB_X_EXT_NAT_T_OA) { 510 if (verify_address_len(p)) 511 return -EINVAL; 512 } 513 if (ext_type == SADB_X_EXT_SEC_CTX) { 514 if (verify_sec_ctx_len(p)) 515 return -EINVAL; 516 } 517 ext_hdrs[ext_type-1] = (void *) p; 518 } 519 p += ext_len; 520 len -= ext_len; 521 } 522 523 return 0; 524 } 525 526 static uint16_t 527 pfkey_satype2proto(uint8_t satype) 528 { 529 switch (satype) { 530 case SADB_SATYPE_UNSPEC: 531 return IPSEC_PROTO_ANY; 532 case SADB_SATYPE_AH: 533 return IPPROTO_AH; 534 case SADB_SATYPE_ESP: 535 return IPPROTO_ESP; 536 case SADB_X_SATYPE_IPCOMP: 537 return IPPROTO_COMP; 538 break; 539 default: 540 return 0; 541 } 542 /* NOTREACHED */ 543 } 544 545 static uint8_t 546 pfkey_proto2satype(uint16_t proto) 547 { 548 switch (proto) { 549 case IPPROTO_AH: 550 return SADB_SATYPE_AH; 551 case IPPROTO_ESP: 552 return SADB_SATYPE_ESP; 553 case IPPROTO_COMP: 554 return SADB_X_SATYPE_IPCOMP; 555 break; 556 default: 557 return 0; 558 } 559 /* NOTREACHED */ 560 } 561 562 /* BTW, this scheme means that there is no way with PFKEY2 sockets to 563 * say specifically 'just raw sockets' as we encode them as 255. 564 */ 565 566 static uint8_t pfkey_proto_to_xfrm(uint8_t proto) 567 { 568 return proto == IPSEC_PROTO_ANY ? 0 : proto; 569 } 570 571 static uint8_t pfkey_proto_from_xfrm(uint8_t proto) 572 { 573 return proto ? proto : IPSEC_PROTO_ANY; 574 } 575 576 static inline int pfkey_sockaddr_len(sa_family_t family) 577 { 578 switch (family) { 579 case AF_INET: 580 return sizeof(struct sockaddr_in); 581 #if IS_ENABLED(CONFIG_IPV6) 582 case AF_INET6: 583 return sizeof(struct sockaddr_in6); 584 #endif 585 } 586 return 0; 587 } 588 589 static 590 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr) 591 { 592 switch (sa->sa_family) { 593 case AF_INET: 594 xaddr->a4 = 595 ((struct sockaddr_in *)sa)->sin_addr.s_addr; 596 return AF_INET; 597 #if IS_ENABLED(CONFIG_IPV6) 598 case AF_INET6: 599 memcpy(xaddr->a6, 600 &((struct sockaddr_in6 *)sa)->sin6_addr, 601 sizeof(struct in6_addr)); 602 return AF_INET6; 603 #endif 604 } 605 return 0; 606 } 607 608 static 609 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr) 610 { 611 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1), 612 xaddr); 613 } 614 615 static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs) 616 { 617 const struct sadb_sa *sa; 618 const struct sadb_address *addr; 619 uint16_t proto; 620 unsigned short family; 621 xfrm_address_t *xaddr; 622 623 sa = ext_hdrs[SADB_EXT_SA - 1]; 624 if (sa == NULL) 625 return NULL; 626 627 proto = pfkey_satype2proto(hdr->sadb_msg_satype); 628 if (proto == 0) 629 return NULL; 630 631 /* sadb_address_len should be checked by caller */ 632 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1]; 633 if (addr == NULL) 634 return NULL; 635 636 family = ((const struct sockaddr *)(addr + 1))->sa_family; 637 switch (family) { 638 case AF_INET: 639 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr; 640 break; 641 #if IS_ENABLED(CONFIG_IPV6) 642 case AF_INET6: 643 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr; 644 break; 645 #endif 646 default: 647 xaddr = NULL; 648 } 649 650 if (!xaddr) 651 return NULL; 652 653 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family); 654 } 655 656 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1))) 657 658 static int 659 pfkey_sockaddr_size(sa_family_t family) 660 { 661 return PFKEY_ALIGN8(pfkey_sockaddr_len(family)); 662 } 663 664 static inline int pfkey_mode_from_xfrm(int mode) 665 { 666 switch(mode) { 667 case XFRM_MODE_TRANSPORT: 668 return IPSEC_MODE_TRANSPORT; 669 case XFRM_MODE_TUNNEL: 670 return IPSEC_MODE_TUNNEL; 671 case XFRM_MODE_BEET: 672 return IPSEC_MODE_BEET; 673 default: 674 return -1; 675 } 676 } 677 678 static inline int pfkey_mode_to_xfrm(int mode) 679 { 680 switch(mode) { 681 case IPSEC_MODE_ANY: /*XXX*/ 682 case IPSEC_MODE_TRANSPORT: 683 return XFRM_MODE_TRANSPORT; 684 case IPSEC_MODE_TUNNEL: 685 return XFRM_MODE_TUNNEL; 686 case IPSEC_MODE_BEET: 687 return XFRM_MODE_BEET; 688 default: 689 return -1; 690 } 691 } 692 693 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port, 694 struct sockaddr *sa, 695 unsigned short family) 696 { 697 switch (family) { 698 case AF_INET: 699 { 700 struct sockaddr_in *sin = (struct sockaddr_in *)sa; 701 sin->sin_family = AF_INET; 702 sin->sin_port = port; 703 sin->sin_addr.s_addr = xaddr->a4; 704 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 705 return 32; 706 } 707 #if IS_ENABLED(CONFIG_IPV6) 708 case AF_INET6: 709 { 710 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; 711 sin6->sin6_family = AF_INET6; 712 sin6->sin6_port = port; 713 sin6->sin6_flowinfo = 0; 714 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6; 715 sin6->sin6_scope_id = 0; 716 return 128; 717 } 718 #endif 719 } 720 return 0; 721 } 722 723 static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x, 724 int add_keys, int hsc) 725 { 726 struct sk_buff *skb; 727 struct sadb_msg *hdr; 728 struct sadb_sa *sa; 729 struct sadb_lifetime *lifetime; 730 struct sadb_address *addr; 731 struct sadb_key *key; 732 struct sadb_x_sa2 *sa2; 733 struct sadb_x_sec_ctx *sec_ctx; 734 struct xfrm_sec_ctx *xfrm_ctx; 735 int ctx_size = 0; 736 int size; 737 int auth_key_size = 0; 738 int encrypt_key_size = 0; 739 int sockaddr_size; 740 struct xfrm_encap_tmpl *natt = NULL; 741 int mode; 742 743 /* address family check */ 744 sockaddr_size = pfkey_sockaddr_size(x->props.family); 745 if (!sockaddr_size) 746 return ERR_PTR(-EINVAL); 747 748 /* base, SA, (lifetime (HSC),) address(SD), (address(P),) 749 key(AE), (identity(SD),) (sensitivity)> */ 750 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) + 751 sizeof(struct sadb_lifetime) + 752 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) + 753 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) + 754 sizeof(struct sadb_address)*2 + 755 sockaddr_size*2 + 756 sizeof(struct sadb_x_sa2); 757 758 if ((xfrm_ctx = x->security)) { 759 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); 760 size += sizeof(struct sadb_x_sec_ctx) + ctx_size; 761 } 762 763 /* identity & sensitivity */ 764 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family)) 765 size += sizeof(struct sadb_address) + sockaddr_size; 766 767 if (add_keys) { 768 if (x->aalg && x->aalg->alg_key_len) { 769 auth_key_size = 770 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8); 771 size += sizeof(struct sadb_key) + auth_key_size; 772 } 773 if (x->ealg && x->ealg->alg_key_len) { 774 encrypt_key_size = 775 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8); 776 size += sizeof(struct sadb_key) + encrypt_key_size; 777 } 778 } 779 if (x->encap) 780 natt = x->encap; 781 782 if (natt && natt->encap_type) { 783 size += sizeof(struct sadb_x_nat_t_type); 784 size += sizeof(struct sadb_x_nat_t_port); 785 size += sizeof(struct sadb_x_nat_t_port); 786 } 787 788 skb = alloc_skb(size + 16, GFP_ATOMIC); 789 if (skb == NULL) 790 return ERR_PTR(-ENOBUFS); 791 792 /* call should fill header later */ 793 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 794 memset(hdr, 0, size); /* XXX do we need this ? */ 795 hdr->sadb_msg_len = size / sizeof(uint64_t); 796 797 /* sa */ 798 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa)); 799 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); 800 sa->sadb_sa_exttype = SADB_EXT_SA; 801 sa->sadb_sa_spi = x->id.spi; 802 sa->sadb_sa_replay = x->props.replay_window; 803 switch (x->km.state) { 804 case XFRM_STATE_VALID: 805 sa->sadb_sa_state = x->km.dying ? 806 SADB_SASTATE_DYING : SADB_SASTATE_MATURE; 807 break; 808 case XFRM_STATE_ACQ: 809 sa->sadb_sa_state = SADB_SASTATE_LARVAL; 810 break; 811 default: 812 sa->sadb_sa_state = SADB_SASTATE_DEAD; 813 break; 814 } 815 sa->sadb_sa_auth = 0; 816 if (x->aalg) { 817 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 818 sa->sadb_sa_auth = (a && a->pfkey_supported) ? 819 a->desc.sadb_alg_id : 0; 820 } 821 sa->sadb_sa_encrypt = 0; 822 BUG_ON(x->ealg && x->calg); 823 if (x->ealg) { 824 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0); 825 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ? 826 a->desc.sadb_alg_id : 0; 827 } 828 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */ 829 if (x->calg) { 830 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0); 831 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ? 832 a->desc.sadb_alg_id : 0; 833 } 834 835 sa->sadb_sa_flags = 0; 836 if (x->props.flags & XFRM_STATE_NOECN) 837 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN; 838 if (x->props.flags & XFRM_STATE_DECAP_DSCP) 839 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP; 840 if (x->props.flags & XFRM_STATE_NOPMTUDISC) 841 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC; 842 843 /* hard time */ 844 if (hsc & 2) { 845 lifetime = (struct sadb_lifetime *) skb_put(skb, 846 sizeof(struct sadb_lifetime)); 847 lifetime->sadb_lifetime_len = 848 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 849 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 850 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit); 851 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit); 852 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds; 853 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds; 854 } 855 /* soft time */ 856 if (hsc & 1) { 857 lifetime = (struct sadb_lifetime *) skb_put(skb, 858 sizeof(struct sadb_lifetime)); 859 lifetime->sadb_lifetime_len = 860 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 861 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; 862 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit); 863 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit); 864 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds; 865 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds; 866 } 867 /* current time */ 868 lifetime = (struct sadb_lifetime *) skb_put(skb, 869 sizeof(struct sadb_lifetime)); 870 lifetime->sadb_lifetime_len = 871 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 872 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 873 lifetime->sadb_lifetime_allocations = x->curlft.packets; 874 lifetime->sadb_lifetime_bytes = x->curlft.bytes; 875 lifetime->sadb_lifetime_addtime = x->curlft.add_time; 876 lifetime->sadb_lifetime_usetime = x->curlft.use_time; 877 /* src address */ 878 addr = (struct sadb_address*) skb_put(skb, 879 sizeof(struct sadb_address)+sockaddr_size); 880 addr->sadb_address_len = 881 (sizeof(struct sadb_address)+sockaddr_size)/ 882 sizeof(uint64_t); 883 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 884 /* "if the ports are non-zero, then the sadb_address_proto field, 885 normally zero, MUST be filled in with the transport 886 protocol's number." - RFC2367 */ 887 addr->sadb_address_proto = 0; 888 addr->sadb_address_reserved = 0; 889 890 addr->sadb_address_prefixlen = 891 pfkey_sockaddr_fill(&x->props.saddr, 0, 892 (struct sockaddr *) (addr + 1), 893 x->props.family); 894 if (!addr->sadb_address_prefixlen) 895 BUG(); 896 897 /* dst address */ 898 addr = (struct sadb_address*) skb_put(skb, 899 sizeof(struct sadb_address)+sockaddr_size); 900 addr->sadb_address_len = 901 (sizeof(struct sadb_address)+sockaddr_size)/ 902 sizeof(uint64_t); 903 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; 904 addr->sadb_address_proto = 0; 905 addr->sadb_address_reserved = 0; 906 907 addr->sadb_address_prefixlen = 908 pfkey_sockaddr_fill(&x->id.daddr, 0, 909 (struct sockaddr *) (addr + 1), 910 x->props.family); 911 if (!addr->sadb_address_prefixlen) 912 BUG(); 913 914 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, 915 x->props.family)) { 916 addr = (struct sadb_address*) skb_put(skb, 917 sizeof(struct sadb_address)+sockaddr_size); 918 addr->sadb_address_len = 919 (sizeof(struct sadb_address)+sockaddr_size)/ 920 sizeof(uint64_t); 921 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY; 922 addr->sadb_address_proto = 923 pfkey_proto_from_xfrm(x->sel.proto); 924 addr->sadb_address_prefixlen = x->sel.prefixlen_s; 925 addr->sadb_address_reserved = 0; 926 927 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport, 928 (struct sockaddr *) (addr + 1), 929 x->props.family); 930 } 931 932 /* auth key */ 933 if (add_keys && auth_key_size) { 934 key = (struct sadb_key *) skb_put(skb, 935 sizeof(struct sadb_key)+auth_key_size); 936 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) / 937 sizeof(uint64_t); 938 key->sadb_key_exttype = SADB_EXT_KEY_AUTH; 939 key->sadb_key_bits = x->aalg->alg_key_len; 940 key->sadb_key_reserved = 0; 941 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8); 942 } 943 /* encrypt key */ 944 if (add_keys && encrypt_key_size) { 945 key = (struct sadb_key *) skb_put(skb, 946 sizeof(struct sadb_key)+encrypt_key_size); 947 key->sadb_key_len = (sizeof(struct sadb_key) + 948 encrypt_key_size) / sizeof(uint64_t); 949 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT; 950 key->sadb_key_bits = x->ealg->alg_key_len; 951 key->sadb_key_reserved = 0; 952 memcpy(key + 1, x->ealg->alg_key, 953 (x->ealg->alg_key_len+7)/8); 954 } 955 956 /* sa */ 957 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2)); 958 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t); 959 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2; 960 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) { 961 kfree_skb(skb); 962 return ERR_PTR(-EINVAL); 963 } 964 sa2->sadb_x_sa2_mode = mode; 965 sa2->sadb_x_sa2_reserved1 = 0; 966 sa2->sadb_x_sa2_reserved2 = 0; 967 sa2->sadb_x_sa2_sequence = 0; 968 sa2->sadb_x_sa2_reqid = x->props.reqid; 969 970 if (natt && natt->encap_type) { 971 struct sadb_x_nat_t_type *n_type; 972 struct sadb_x_nat_t_port *n_port; 973 974 /* type */ 975 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type)); 976 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t); 977 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE; 978 n_type->sadb_x_nat_t_type_type = natt->encap_type; 979 n_type->sadb_x_nat_t_type_reserved[0] = 0; 980 n_type->sadb_x_nat_t_type_reserved[1] = 0; 981 n_type->sadb_x_nat_t_type_reserved[2] = 0; 982 983 /* source port */ 984 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); 985 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); 986 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; 987 n_port->sadb_x_nat_t_port_port = natt->encap_sport; 988 n_port->sadb_x_nat_t_port_reserved = 0; 989 990 /* dest port */ 991 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); 992 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); 993 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; 994 n_port->sadb_x_nat_t_port_port = natt->encap_dport; 995 n_port->sadb_x_nat_t_port_reserved = 0; 996 } 997 998 /* security context */ 999 if (xfrm_ctx) { 1000 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, 1001 sizeof(struct sadb_x_sec_ctx) + ctx_size); 1002 sec_ctx->sadb_x_sec_len = 1003 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); 1004 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; 1005 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; 1006 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; 1007 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; 1008 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, 1009 xfrm_ctx->ctx_len); 1010 } 1011 1012 return skb; 1013 } 1014 1015 1016 static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x) 1017 { 1018 struct sk_buff *skb; 1019 1020 skb = __pfkey_xfrm_state2msg(x, 1, 3); 1021 1022 return skb; 1023 } 1024 1025 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x, 1026 int hsc) 1027 { 1028 return __pfkey_xfrm_state2msg(x, 0, hsc); 1029 } 1030 1031 static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net, 1032 const struct sadb_msg *hdr, 1033 void * const *ext_hdrs) 1034 { 1035 struct xfrm_state *x; 1036 const struct sadb_lifetime *lifetime; 1037 const struct sadb_sa *sa; 1038 const struct sadb_key *key; 1039 const struct sadb_x_sec_ctx *sec_ctx; 1040 uint16_t proto; 1041 int err; 1042 1043 1044 sa = ext_hdrs[SADB_EXT_SA - 1]; 1045 if (!sa || 1046 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 1047 ext_hdrs[SADB_EXT_ADDRESS_DST-1])) 1048 return ERR_PTR(-EINVAL); 1049 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP && 1050 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]) 1051 return ERR_PTR(-EINVAL); 1052 if (hdr->sadb_msg_satype == SADB_SATYPE_AH && 1053 !ext_hdrs[SADB_EXT_KEY_AUTH-1]) 1054 return ERR_PTR(-EINVAL); 1055 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] != 1056 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) 1057 return ERR_PTR(-EINVAL); 1058 1059 proto = pfkey_satype2proto(hdr->sadb_msg_satype); 1060 if (proto == 0) 1061 return ERR_PTR(-EINVAL); 1062 1063 /* default error is no buffer space */ 1064 err = -ENOBUFS; 1065 1066 /* RFC2367: 1067 1068 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message. 1069 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not 1070 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state. 1071 Therefore, the sadb_sa_state field of all submitted SAs MUST be 1072 SADB_SASTATE_MATURE and the kernel MUST return an error if this is 1073 not true. 1074 1075 However, KAME setkey always uses SADB_SASTATE_LARVAL. 1076 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable. 1077 */ 1078 if (sa->sadb_sa_auth > SADB_AALG_MAX || 1079 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP && 1080 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) || 1081 sa->sadb_sa_encrypt > SADB_EALG_MAX) 1082 return ERR_PTR(-EINVAL); 1083 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1]; 1084 if (key != NULL && 1085 sa->sadb_sa_auth != SADB_X_AALG_NULL && 1086 ((key->sadb_key_bits+7) / 8 == 0 || 1087 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t))) 1088 return ERR_PTR(-EINVAL); 1089 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; 1090 if (key != NULL && 1091 sa->sadb_sa_encrypt != SADB_EALG_NULL && 1092 ((key->sadb_key_bits+7) / 8 == 0 || 1093 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t))) 1094 return ERR_PTR(-EINVAL); 1095 1096 x = xfrm_state_alloc(net); 1097 if (x == NULL) 1098 return ERR_PTR(-ENOBUFS); 1099 1100 x->id.proto = proto; 1101 x->id.spi = sa->sadb_sa_spi; 1102 x->props.replay_window = sa->sadb_sa_replay; 1103 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN) 1104 x->props.flags |= XFRM_STATE_NOECN; 1105 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP) 1106 x->props.flags |= XFRM_STATE_DECAP_DSCP; 1107 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC) 1108 x->props.flags |= XFRM_STATE_NOPMTUDISC; 1109 1110 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1]; 1111 if (lifetime != NULL) { 1112 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); 1113 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); 1114 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; 1115 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; 1116 } 1117 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1]; 1118 if (lifetime != NULL) { 1119 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); 1120 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); 1121 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; 1122 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; 1123 } 1124 1125 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; 1126 if (sec_ctx != NULL) { 1127 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); 1128 1129 if (!uctx) 1130 goto out; 1131 1132 err = security_xfrm_state_alloc(x, uctx); 1133 kfree(uctx); 1134 1135 if (err) 1136 goto out; 1137 } 1138 1139 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1]; 1140 if (sa->sadb_sa_auth) { 1141 int keysize = 0; 1142 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth); 1143 if (!a || !a->pfkey_supported) { 1144 err = -ENOSYS; 1145 goto out; 1146 } 1147 if (key) 1148 keysize = (key->sadb_key_bits + 7) / 8; 1149 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL); 1150 if (!x->aalg) 1151 goto out; 1152 strcpy(x->aalg->alg_name, a->name); 1153 x->aalg->alg_key_len = 0; 1154 if (key) { 1155 x->aalg->alg_key_len = key->sadb_key_bits; 1156 memcpy(x->aalg->alg_key, key+1, keysize); 1157 } 1158 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits; 1159 x->props.aalgo = sa->sadb_sa_auth; 1160 /* x->algo.flags = sa->sadb_sa_flags; */ 1161 } 1162 if (sa->sadb_sa_encrypt) { 1163 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 1164 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt); 1165 if (!a || !a->pfkey_supported) { 1166 err = -ENOSYS; 1167 goto out; 1168 } 1169 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL); 1170 if (!x->calg) 1171 goto out; 1172 strcpy(x->calg->alg_name, a->name); 1173 x->props.calgo = sa->sadb_sa_encrypt; 1174 } else { 1175 int keysize = 0; 1176 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt); 1177 if (!a || !a->pfkey_supported) { 1178 err = -ENOSYS; 1179 goto out; 1180 } 1181 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; 1182 if (key) 1183 keysize = (key->sadb_key_bits + 7) / 8; 1184 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL); 1185 if (!x->ealg) 1186 goto out; 1187 strcpy(x->ealg->alg_name, a->name); 1188 x->ealg->alg_key_len = 0; 1189 if (key) { 1190 x->ealg->alg_key_len = key->sadb_key_bits; 1191 memcpy(x->ealg->alg_key, key+1, keysize); 1192 } 1193 x->props.ealgo = sa->sadb_sa_encrypt; 1194 } 1195 } 1196 /* x->algo.flags = sa->sadb_sa_flags; */ 1197 1198 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 1199 &x->props.saddr); 1200 if (!x->props.family) { 1201 err = -EAFNOSUPPORT; 1202 goto out; 1203 } 1204 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], 1205 &x->id.daddr); 1206 1207 if (ext_hdrs[SADB_X_EXT_SA2-1]) { 1208 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1]; 1209 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); 1210 if (mode < 0) { 1211 err = -EINVAL; 1212 goto out; 1213 } 1214 x->props.mode = mode; 1215 x->props.reqid = sa2->sadb_x_sa2_reqid; 1216 } 1217 1218 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) { 1219 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]; 1220 1221 /* Nobody uses this, but we try. */ 1222 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr); 1223 x->sel.prefixlen_s = addr->sadb_address_prefixlen; 1224 } 1225 1226 if (!x->sel.family) 1227 x->sel.family = x->props.family; 1228 1229 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) { 1230 const struct sadb_x_nat_t_type* n_type; 1231 struct xfrm_encap_tmpl *natt; 1232 1233 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL); 1234 if (!x->encap) 1235 goto out; 1236 1237 natt = x->encap; 1238 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]; 1239 natt->encap_type = n_type->sadb_x_nat_t_type_type; 1240 1241 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) { 1242 const struct sadb_x_nat_t_port *n_port = 1243 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]; 1244 natt->encap_sport = n_port->sadb_x_nat_t_port_port; 1245 } 1246 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) { 1247 const struct sadb_x_nat_t_port *n_port = 1248 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]; 1249 natt->encap_dport = n_port->sadb_x_nat_t_port_port; 1250 } 1251 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa)); 1252 } 1253 1254 err = xfrm_init_state(x); 1255 if (err) 1256 goto out; 1257 1258 x->km.seq = hdr->sadb_msg_seq; 1259 return x; 1260 1261 out: 1262 x->km.state = XFRM_STATE_DEAD; 1263 xfrm_state_put(x); 1264 return ERR_PTR(err); 1265 } 1266 1267 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1268 { 1269 return -EOPNOTSUPP; 1270 } 1271 1272 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1273 { 1274 struct net *net = sock_net(sk); 1275 struct sk_buff *resp_skb; 1276 struct sadb_x_sa2 *sa2; 1277 struct sadb_address *saddr, *daddr; 1278 struct sadb_msg *out_hdr; 1279 struct sadb_spirange *range; 1280 struct xfrm_state *x = NULL; 1281 int mode; 1282 int err; 1283 u32 min_spi, max_spi; 1284 u32 reqid; 1285 u8 proto; 1286 unsigned short family; 1287 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL; 1288 1289 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 1290 ext_hdrs[SADB_EXT_ADDRESS_DST-1])) 1291 return -EINVAL; 1292 1293 proto = pfkey_satype2proto(hdr->sadb_msg_satype); 1294 if (proto == 0) 1295 return -EINVAL; 1296 1297 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) { 1298 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); 1299 if (mode < 0) 1300 return -EINVAL; 1301 reqid = sa2->sadb_x_sa2_reqid; 1302 } else { 1303 mode = 0; 1304 reqid = 0; 1305 } 1306 1307 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1]; 1308 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1]; 1309 1310 family = ((struct sockaddr *)(saddr + 1))->sa_family; 1311 switch (family) { 1312 case AF_INET: 1313 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr; 1314 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr; 1315 break; 1316 #if IS_ENABLED(CONFIG_IPV6) 1317 case AF_INET6: 1318 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr; 1319 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr; 1320 break; 1321 #endif 1322 } 1323 1324 if (hdr->sadb_msg_seq) { 1325 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq); 1326 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) { 1327 xfrm_state_put(x); 1328 x = NULL; 1329 } 1330 } 1331 1332 if (!x) 1333 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family); 1334 1335 if (x == NULL) 1336 return -ENOENT; 1337 1338 min_spi = 0x100; 1339 max_spi = 0x0fffffff; 1340 1341 range = ext_hdrs[SADB_EXT_SPIRANGE-1]; 1342 if (range) { 1343 min_spi = range->sadb_spirange_min; 1344 max_spi = range->sadb_spirange_max; 1345 } 1346 1347 err = xfrm_alloc_spi(x, min_spi, max_spi); 1348 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x); 1349 1350 if (IS_ERR(resp_skb)) { 1351 xfrm_state_put(x); 1352 return PTR_ERR(resp_skb); 1353 } 1354 1355 out_hdr = (struct sadb_msg *) resp_skb->data; 1356 out_hdr->sadb_msg_version = hdr->sadb_msg_version; 1357 out_hdr->sadb_msg_type = SADB_GETSPI; 1358 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); 1359 out_hdr->sadb_msg_errno = 0; 1360 out_hdr->sadb_msg_reserved = 0; 1361 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; 1362 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; 1363 1364 xfrm_state_put(x); 1365 1366 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net); 1367 1368 return 0; 1369 } 1370 1371 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1372 { 1373 struct net *net = sock_net(sk); 1374 struct xfrm_state *x; 1375 1376 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8) 1377 return -EOPNOTSUPP; 1378 1379 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0) 1380 return 0; 1381 1382 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq); 1383 if (x == NULL) 1384 return 0; 1385 1386 spin_lock_bh(&x->lock); 1387 if (x->km.state == XFRM_STATE_ACQ) { 1388 x->km.state = XFRM_STATE_ERROR; 1389 wake_up(&net->xfrm.km_waitq); 1390 } 1391 spin_unlock_bh(&x->lock); 1392 xfrm_state_put(x); 1393 return 0; 1394 } 1395 1396 static inline int event2poltype(int event) 1397 { 1398 switch (event) { 1399 case XFRM_MSG_DELPOLICY: 1400 return SADB_X_SPDDELETE; 1401 case XFRM_MSG_NEWPOLICY: 1402 return SADB_X_SPDADD; 1403 case XFRM_MSG_UPDPOLICY: 1404 return SADB_X_SPDUPDATE; 1405 case XFRM_MSG_POLEXPIRE: 1406 // return SADB_X_SPDEXPIRE; 1407 default: 1408 pr_err("pfkey: Unknown policy event %d\n", event); 1409 break; 1410 } 1411 1412 return 0; 1413 } 1414 1415 static inline int event2keytype(int event) 1416 { 1417 switch (event) { 1418 case XFRM_MSG_DELSA: 1419 return SADB_DELETE; 1420 case XFRM_MSG_NEWSA: 1421 return SADB_ADD; 1422 case XFRM_MSG_UPDSA: 1423 return SADB_UPDATE; 1424 case XFRM_MSG_EXPIRE: 1425 return SADB_EXPIRE; 1426 default: 1427 pr_err("pfkey: Unknown SA event %d\n", event); 1428 break; 1429 } 1430 1431 return 0; 1432 } 1433 1434 /* ADD/UPD/DEL */ 1435 static int key_notify_sa(struct xfrm_state *x, const struct km_event *c) 1436 { 1437 struct sk_buff *skb; 1438 struct sadb_msg *hdr; 1439 1440 skb = pfkey_xfrm_state2msg(x); 1441 1442 if (IS_ERR(skb)) 1443 return PTR_ERR(skb); 1444 1445 hdr = (struct sadb_msg *) skb->data; 1446 hdr->sadb_msg_version = PF_KEY_V2; 1447 hdr->sadb_msg_type = event2keytype(c->event); 1448 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); 1449 hdr->sadb_msg_errno = 0; 1450 hdr->sadb_msg_reserved = 0; 1451 hdr->sadb_msg_seq = c->seq; 1452 hdr->sadb_msg_pid = c->portid; 1453 1454 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x)); 1455 1456 return 0; 1457 } 1458 1459 static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1460 { 1461 struct net *net = sock_net(sk); 1462 struct xfrm_state *x; 1463 int err; 1464 struct km_event c; 1465 1466 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs); 1467 if (IS_ERR(x)) 1468 return PTR_ERR(x); 1469 1470 xfrm_state_hold(x); 1471 if (hdr->sadb_msg_type == SADB_ADD) 1472 err = xfrm_state_add(x); 1473 else 1474 err = xfrm_state_update(x); 1475 1476 xfrm_audit_state_add(x, err ? 0 : 1, 1477 audit_get_loginuid(current), 1478 audit_get_sessionid(current), 0); 1479 1480 if (err < 0) { 1481 x->km.state = XFRM_STATE_DEAD; 1482 __xfrm_state_put(x); 1483 goto out; 1484 } 1485 1486 if (hdr->sadb_msg_type == SADB_ADD) 1487 c.event = XFRM_MSG_NEWSA; 1488 else 1489 c.event = XFRM_MSG_UPDSA; 1490 c.seq = hdr->sadb_msg_seq; 1491 c.portid = hdr->sadb_msg_pid; 1492 km_state_notify(x, &c); 1493 out: 1494 xfrm_state_put(x); 1495 return err; 1496 } 1497 1498 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1499 { 1500 struct net *net = sock_net(sk); 1501 struct xfrm_state *x; 1502 struct km_event c; 1503 int err; 1504 1505 if (!ext_hdrs[SADB_EXT_SA-1] || 1506 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 1507 ext_hdrs[SADB_EXT_ADDRESS_DST-1])) 1508 return -EINVAL; 1509 1510 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs); 1511 if (x == NULL) 1512 return -ESRCH; 1513 1514 if ((err = security_xfrm_state_delete(x))) 1515 goto out; 1516 1517 if (xfrm_state_kern(x)) { 1518 err = -EPERM; 1519 goto out; 1520 } 1521 1522 err = xfrm_state_delete(x); 1523 1524 if (err < 0) 1525 goto out; 1526 1527 c.seq = hdr->sadb_msg_seq; 1528 c.portid = hdr->sadb_msg_pid; 1529 c.event = XFRM_MSG_DELSA; 1530 km_state_notify(x, &c); 1531 out: 1532 xfrm_audit_state_delete(x, err ? 0 : 1, 1533 audit_get_loginuid(current), 1534 audit_get_sessionid(current), 0); 1535 xfrm_state_put(x); 1536 1537 return err; 1538 } 1539 1540 static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1541 { 1542 struct net *net = sock_net(sk); 1543 __u8 proto; 1544 struct sk_buff *out_skb; 1545 struct sadb_msg *out_hdr; 1546 struct xfrm_state *x; 1547 1548 if (!ext_hdrs[SADB_EXT_SA-1] || 1549 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 1550 ext_hdrs[SADB_EXT_ADDRESS_DST-1])) 1551 return -EINVAL; 1552 1553 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs); 1554 if (x == NULL) 1555 return -ESRCH; 1556 1557 out_skb = pfkey_xfrm_state2msg(x); 1558 proto = x->id.proto; 1559 xfrm_state_put(x); 1560 if (IS_ERR(out_skb)) 1561 return PTR_ERR(out_skb); 1562 1563 out_hdr = (struct sadb_msg *) out_skb->data; 1564 out_hdr->sadb_msg_version = hdr->sadb_msg_version; 1565 out_hdr->sadb_msg_type = SADB_GET; 1566 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); 1567 out_hdr->sadb_msg_errno = 0; 1568 out_hdr->sadb_msg_reserved = 0; 1569 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; 1570 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; 1571 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk)); 1572 1573 return 0; 1574 } 1575 1576 static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig, 1577 gfp_t allocation) 1578 { 1579 struct sk_buff *skb; 1580 struct sadb_msg *hdr; 1581 int len, auth_len, enc_len, i; 1582 1583 auth_len = xfrm_count_pfkey_auth_supported(); 1584 if (auth_len) { 1585 auth_len *= sizeof(struct sadb_alg); 1586 auth_len += sizeof(struct sadb_supported); 1587 } 1588 1589 enc_len = xfrm_count_pfkey_enc_supported(); 1590 if (enc_len) { 1591 enc_len *= sizeof(struct sadb_alg); 1592 enc_len += sizeof(struct sadb_supported); 1593 } 1594 1595 len = enc_len + auth_len + sizeof(struct sadb_msg); 1596 1597 skb = alloc_skb(len + 16, allocation); 1598 if (!skb) 1599 goto out_put_algs; 1600 1601 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr)); 1602 pfkey_hdr_dup(hdr, orig); 1603 hdr->sadb_msg_errno = 0; 1604 hdr->sadb_msg_len = len / sizeof(uint64_t); 1605 1606 if (auth_len) { 1607 struct sadb_supported *sp; 1608 struct sadb_alg *ap; 1609 1610 sp = (struct sadb_supported *) skb_put(skb, auth_len); 1611 ap = (struct sadb_alg *) (sp + 1); 1612 1613 sp->sadb_supported_len = auth_len / sizeof(uint64_t); 1614 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; 1615 1616 for (i = 0; ; i++) { 1617 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); 1618 if (!aalg) 1619 break; 1620 if (!aalg->pfkey_supported) 1621 continue; 1622 if (aalg->available) 1623 *ap++ = aalg->desc; 1624 } 1625 } 1626 1627 if (enc_len) { 1628 struct sadb_supported *sp; 1629 struct sadb_alg *ap; 1630 1631 sp = (struct sadb_supported *) skb_put(skb, enc_len); 1632 ap = (struct sadb_alg *) (sp + 1); 1633 1634 sp->sadb_supported_len = enc_len / sizeof(uint64_t); 1635 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT; 1636 1637 for (i = 0; ; i++) { 1638 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); 1639 if (!ealg) 1640 break; 1641 if (!ealg->pfkey_supported) 1642 continue; 1643 if (ealg->available) 1644 *ap++ = ealg->desc; 1645 } 1646 } 1647 1648 out_put_algs: 1649 return skb; 1650 } 1651 1652 static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1653 { 1654 struct pfkey_sock *pfk = pfkey_sk(sk); 1655 struct sk_buff *supp_skb; 1656 1657 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX) 1658 return -EINVAL; 1659 1660 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) { 1661 if (pfk->registered&(1<<hdr->sadb_msg_satype)) 1662 return -EEXIST; 1663 pfk->registered |= (1<<hdr->sadb_msg_satype); 1664 } 1665 1666 xfrm_probe_algs(); 1667 1668 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL); 1669 if (!supp_skb) { 1670 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) 1671 pfk->registered &= ~(1<<hdr->sadb_msg_satype); 1672 1673 return -ENOBUFS; 1674 } 1675 1676 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk)); 1677 1678 return 0; 1679 } 1680 1681 static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr) 1682 { 1683 struct sk_buff *skb; 1684 struct sadb_msg *hdr; 1685 1686 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); 1687 if (!skb) 1688 return -ENOBUFS; 1689 1690 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 1691 memcpy(hdr, ihdr, sizeof(struct sadb_msg)); 1692 hdr->sadb_msg_errno = (uint8_t) 0; 1693 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); 1694 1695 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk)); 1696 } 1697 1698 static int key_notify_sa_flush(const struct km_event *c) 1699 { 1700 struct sk_buff *skb; 1701 struct sadb_msg *hdr; 1702 1703 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); 1704 if (!skb) 1705 return -ENOBUFS; 1706 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 1707 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto); 1708 hdr->sadb_msg_type = SADB_FLUSH; 1709 hdr->sadb_msg_seq = c->seq; 1710 hdr->sadb_msg_pid = c->portid; 1711 hdr->sadb_msg_version = PF_KEY_V2; 1712 hdr->sadb_msg_errno = (uint8_t) 0; 1713 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); 1714 1715 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net); 1716 1717 return 0; 1718 } 1719 1720 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1721 { 1722 struct net *net = sock_net(sk); 1723 unsigned int proto; 1724 struct km_event c; 1725 struct xfrm_audit audit_info; 1726 int err, err2; 1727 1728 proto = pfkey_satype2proto(hdr->sadb_msg_satype); 1729 if (proto == 0) 1730 return -EINVAL; 1731 1732 audit_info.loginuid = audit_get_loginuid(current); 1733 audit_info.sessionid = audit_get_sessionid(current); 1734 audit_info.secid = 0; 1735 err = xfrm_state_flush(net, proto, &audit_info); 1736 err2 = unicast_flush_resp(sk, hdr); 1737 if (err || err2) { 1738 if (err == -ESRCH) /* empty table - go quietly */ 1739 err = 0; 1740 return err ? err : err2; 1741 } 1742 1743 c.data.proto = proto; 1744 c.seq = hdr->sadb_msg_seq; 1745 c.portid = hdr->sadb_msg_pid; 1746 c.event = XFRM_MSG_FLUSHSA; 1747 c.net = net; 1748 km_state_notify(NULL, &c); 1749 1750 return 0; 1751 } 1752 1753 static int dump_sa(struct xfrm_state *x, int count, void *ptr) 1754 { 1755 struct pfkey_sock *pfk = ptr; 1756 struct sk_buff *out_skb; 1757 struct sadb_msg *out_hdr; 1758 1759 if (!pfkey_can_dump(&pfk->sk)) 1760 return -ENOBUFS; 1761 1762 out_skb = pfkey_xfrm_state2msg(x); 1763 if (IS_ERR(out_skb)) 1764 return PTR_ERR(out_skb); 1765 1766 out_hdr = (struct sadb_msg *) out_skb->data; 1767 out_hdr->sadb_msg_version = pfk->dump.msg_version; 1768 out_hdr->sadb_msg_type = SADB_DUMP; 1769 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); 1770 out_hdr->sadb_msg_errno = 0; 1771 out_hdr->sadb_msg_reserved = 0; 1772 out_hdr->sadb_msg_seq = count + 1; 1773 out_hdr->sadb_msg_pid = pfk->dump.msg_portid; 1774 1775 if (pfk->dump.skb) 1776 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, 1777 &pfk->sk, sock_net(&pfk->sk)); 1778 pfk->dump.skb = out_skb; 1779 1780 return 0; 1781 } 1782 1783 static int pfkey_dump_sa(struct pfkey_sock *pfk) 1784 { 1785 struct net *net = sock_net(&pfk->sk); 1786 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk); 1787 } 1788 1789 static void pfkey_dump_sa_done(struct pfkey_sock *pfk) 1790 { 1791 xfrm_state_walk_done(&pfk->dump.u.state); 1792 } 1793 1794 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1795 { 1796 u8 proto; 1797 struct pfkey_sock *pfk = pfkey_sk(sk); 1798 1799 if (pfk->dump.dump != NULL) 1800 return -EBUSY; 1801 1802 proto = pfkey_satype2proto(hdr->sadb_msg_satype); 1803 if (proto == 0) 1804 return -EINVAL; 1805 1806 pfk->dump.msg_version = hdr->sadb_msg_version; 1807 pfk->dump.msg_portid = hdr->sadb_msg_pid; 1808 pfk->dump.dump = pfkey_dump_sa; 1809 pfk->dump.done = pfkey_dump_sa_done; 1810 xfrm_state_walk_init(&pfk->dump.u.state, proto); 1811 1812 return pfkey_do_dump(pfk); 1813 } 1814 1815 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 1816 { 1817 struct pfkey_sock *pfk = pfkey_sk(sk); 1818 int satype = hdr->sadb_msg_satype; 1819 bool reset_errno = false; 1820 1821 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) { 1822 reset_errno = true; 1823 if (satype != 0 && satype != 1) 1824 return -EINVAL; 1825 pfk->promisc = satype; 1826 } 1827 if (reset_errno && skb_cloned(skb)) 1828 skb = skb_copy(skb, GFP_KERNEL); 1829 else 1830 skb = skb_clone(skb, GFP_KERNEL); 1831 1832 if (reset_errno && skb) { 1833 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data; 1834 new_hdr->sadb_msg_errno = 0; 1835 } 1836 1837 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk)); 1838 return 0; 1839 } 1840 1841 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr) 1842 { 1843 int i; 1844 u32 reqid = *(u32*)ptr; 1845 1846 for (i=0; i<xp->xfrm_nr; i++) { 1847 if (xp->xfrm_vec[i].reqid == reqid) 1848 return -EEXIST; 1849 } 1850 return 0; 1851 } 1852 1853 static u32 gen_reqid(struct net *net) 1854 { 1855 struct xfrm_policy_walk walk; 1856 u32 start; 1857 int rc; 1858 static u32 reqid = IPSEC_MANUAL_REQID_MAX; 1859 1860 start = reqid; 1861 do { 1862 ++reqid; 1863 if (reqid == 0) 1864 reqid = IPSEC_MANUAL_REQID_MAX+1; 1865 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN); 1866 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid); 1867 xfrm_policy_walk_done(&walk); 1868 if (rc != -EEXIST) 1869 return reqid; 1870 } while (reqid != start); 1871 return 0; 1872 } 1873 1874 static int 1875 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq) 1876 { 1877 struct net *net = xp_net(xp); 1878 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr; 1879 int mode; 1880 1881 if (xp->xfrm_nr >= XFRM_MAX_DEPTH) 1882 return -ELOOP; 1883 1884 if (rq->sadb_x_ipsecrequest_mode == 0) 1885 return -EINVAL; 1886 1887 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */ 1888 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0) 1889 return -EINVAL; 1890 t->mode = mode; 1891 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE) 1892 t->optional = 1; 1893 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) { 1894 t->reqid = rq->sadb_x_ipsecrequest_reqid; 1895 if (t->reqid > IPSEC_MANUAL_REQID_MAX) 1896 t->reqid = 0; 1897 if (!t->reqid && !(t->reqid = gen_reqid(net))) 1898 return -ENOBUFS; 1899 } 1900 1901 /* addresses present only in tunnel mode */ 1902 if (t->mode == XFRM_MODE_TUNNEL) { 1903 u8 *sa = (u8 *) (rq + 1); 1904 int family, socklen; 1905 1906 family = pfkey_sockaddr_extract((struct sockaddr *)sa, 1907 &t->saddr); 1908 if (!family) 1909 return -EINVAL; 1910 1911 socklen = pfkey_sockaddr_len(family); 1912 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen), 1913 &t->id.daddr) != family) 1914 return -EINVAL; 1915 t->encap_family = family; 1916 } else 1917 t->encap_family = xp->family; 1918 1919 /* No way to set this via kame pfkey */ 1920 t->allalgs = 1; 1921 xp->xfrm_nr++; 1922 return 0; 1923 } 1924 1925 static int 1926 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol) 1927 { 1928 int err; 1929 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy); 1930 struct sadb_x_ipsecrequest *rq = (void*)(pol+1); 1931 1932 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy)) 1933 return -EINVAL; 1934 1935 while (len >= sizeof(struct sadb_x_ipsecrequest)) { 1936 if ((err = parse_ipsecrequest(xp, rq)) < 0) 1937 return err; 1938 len -= rq->sadb_x_ipsecrequest_len; 1939 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len); 1940 } 1941 return 0; 1942 } 1943 1944 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp) 1945 { 1946 struct xfrm_sec_ctx *xfrm_ctx = xp->security; 1947 1948 if (xfrm_ctx) { 1949 int len = sizeof(struct sadb_x_sec_ctx); 1950 len += xfrm_ctx->ctx_len; 1951 return PFKEY_ALIGN8(len); 1952 } 1953 return 0; 1954 } 1955 1956 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp) 1957 { 1958 const struct xfrm_tmpl *t; 1959 int sockaddr_size = pfkey_sockaddr_size(xp->family); 1960 int socklen = 0; 1961 int i; 1962 1963 for (i=0; i<xp->xfrm_nr; i++) { 1964 t = xp->xfrm_vec + i; 1965 socklen += pfkey_sockaddr_len(t->encap_family); 1966 } 1967 1968 return sizeof(struct sadb_msg) + 1969 (sizeof(struct sadb_lifetime) * 3) + 1970 (sizeof(struct sadb_address) * 2) + 1971 (sockaddr_size * 2) + 1972 sizeof(struct sadb_x_policy) + 1973 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) + 1974 (socklen * 2) + 1975 pfkey_xfrm_policy2sec_ctx_size(xp); 1976 } 1977 1978 static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp) 1979 { 1980 struct sk_buff *skb; 1981 int size; 1982 1983 size = pfkey_xfrm_policy2msg_size(xp); 1984 1985 skb = alloc_skb(size + 16, GFP_ATOMIC); 1986 if (skb == NULL) 1987 return ERR_PTR(-ENOBUFS); 1988 1989 return skb; 1990 } 1991 1992 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir) 1993 { 1994 struct sadb_msg *hdr; 1995 struct sadb_address *addr; 1996 struct sadb_lifetime *lifetime; 1997 struct sadb_x_policy *pol; 1998 struct sadb_x_sec_ctx *sec_ctx; 1999 struct xfrm_sec_ctx *xfrm_ctx; 2000 int i; 2001 int size; 2002 int sockaddr_size = pfkey_sockaddr_size(xp->family); 2003 int socklen = pfkey_sockaddr_len(xp->family); 2004 2005 size = pfkey_xfrm_policy2msg_size(xp); 2006 2007 /* call should fill header later */ 2008 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 2009 memset(hdr, 0, size); /* XXX do we need this ? */ 2010 2011 /* src address */ 2012 addr = (struct sadb_address*) skb_put(skb, 2013 sizeof(struct sadb_address)+sockaddr_size); 2014 addr->sadb_address_len = 2015 (sizeof(struct sadb_address)+sockaddr_size)/ 2016 sizeof(uint64_t); 2017 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 2018 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); 2019 addr->sadb_address_prefixlen = xp->selector.prefixlen_s; 2020 addr->sadb_address_reserved = 0; 2021 if (!pfkey_sockaddr_fill(&xp->selector.saddr, 2022 xp->selector.sport, 2023 (struct sockaddr *) (addr + 1), 2024 xp->family)) 2025 BUG(); 2026 2027 /* dst address */ 2028 addr = (struct sadb_address*) skb_put(skb, 2029 sizeof(struct sadb_address)+sockaddr_size); 2030 addr->sadb_address_len = 2031 (sizeof(struct sadb_address)+sockaddr_size)/ 2032 sizeof(uint64_t); 2033 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; 2034 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); 2035 addr->sadb_address_prefixlen = xp->selector.prefixlen_d; 2036 addr->sadb_address_reserved = 0; 2037 2038 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport, 2039 (struct sockaddr *) (addr + 1), 2040 xp->family); 2041 2042 /* hard time */ 2043 lifetime = (struct sadb_lifetime *) skb_put(skb, 2044 sizeof(struct sadb_lifetime)); 2045 lifetime->sadb_lifetime_len = 2046 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 2047 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 2048 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit); 2049 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit); 2050 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds; 2051 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds; 2052 /* soft time */ 2053 lifetime = (struct sadb_lifetime *) skb_put(skb, 2054 sizeof(struct sadb_lifetime)); 2055 lifetime->sadb_lifetime_len = 2056 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 2057 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; 2058 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit); 2059 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit); 2060 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds; 2061 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds; 2062 /* current time */ 2063 lifetime = (struct sadb_lifetime *) skb_put(skb, 2064 sizeof(struct sadb_lifetime)); 2065 lifetime->sadb_lifetime_len = 2066 sizeof(struct sadb_lifetime)/sizeof(uint64_t); 2067 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 2068 lifetime->sadb_lifetime_allocations = xp->curlft.packets; 2069 lifetime->sadb_lifetime_bytes = xp->curlft.bytes; 2070 lifetime->sadb_lifetime_addtime = xp->curlft.add_time; 2071 lifetime->sadb_lifetime_usetime = xp->curlft.use_time; 2072 2073 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy)); 2074 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); 2075 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 2076 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD; 2077 if (xp->action == XFRM_POLICY_ALLOW) { 2078 if (xp->xfrm_nr) 2079 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; 2080 else 2081 pol->sadb_x_policy_type = IPSEC_POLICY_NONE; 2082 } 2083 pol->sadb_x_policy_dir = dir+1; 2084 pol->sadb_x_policy_id = xp->index; 2085 pol->sadb_x_policy_priority = xp->priority; 2086 2087 for (i=0; i<xp->xfrm_nr; i++) { 2088 const struct xfrm_tmpl *t = xp->xfrm_vec + i; 2089 struct sadb_x_ipsecrequest *rq; 2090 int req_size; 2091 int mode; 2092 2093 req_size = sizeof(struct sadb_x_ipsecrequest); 2094 if (t->mode == XFRM_MODE_TUNNEL) { 2095 socklen = pfkey_sockaddr_len(t->encap_family); 2096 req_size += socklen * 2; 2097 } else { 2098 size -= 2*socklen; 2099 } 2100 rq = (void*)skb_put(skb, req_size); 2101 pol->sadb_x_policy_len += req_size/8; 2102 memset(rq, 0, sizeof(*rq)); 2103 rq->sadb_x_ipsecrequest_len = req_size; 2104 rq->sadb_x_ipsecrequest_proto = t->id.proto; 2105 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0) 2106 return -EINVAL; 2107 rq->sadb_x_ipsecrequest_mode = mode; 2108 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE; 2109 if (t->reqid) 2110 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE; 2111 if (t->optional) 2112 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE; 2113 rq->sadb_x_ipsecrequest_reqid = t->reqid; 2114 2115 if (t->mode == XFRM_MODE_TUNNEL) { 2116 u8 *sa = (void *)(rq + 1); 2117 pfkey_sockaddr_fill(&t->saddr, 0, 2118 (struct sockaddr *)sa, 2119 t->encap_family); 2120 pfkey_sockaddr_fill(&t->id.daddr, 0, 2121 (struct sockaddr *) (sa + socklen), 2122 t->encap_family); 2123 } 2124 } 2125 2126 /* security context */ 2127 if ((xfrm_ctx = xp->security)) { 2128 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp); 2129 2130 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size); 2131 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t); 2132 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; 2133 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; 2134 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; 2135 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; 2136 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, 2137 xfrm_ctx->ctx_len); 2138 } 2139 2140 hdr->sadb_msg_len = size / sizeof(uint64_t); 2141 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt); 2142 2143 return 0; 2144 } 2145 2146 static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) 2147 { 2148 struct sk_buff *out_skb; 2149 struct sadb_msg *out_hdr; 2150 int err; 2151 2152 out_skb = pfkey_xfrm_policy2msg_prep(xp); 2153 if (IS_ERR(out_skb)) 2154 return PTR_ERR(out_skb); 2155 2156 err = pfkey_xfrm_policy2msg(out_skb, xp, dir); 2157 if (err < 0) 2158 return err; 2159 2160 out_hdr = (struct sadb_msg *) out_skb->data; 2161 out_hdr->sadb_msg_version = PF_KEY_V2; 2162 2163 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY) 2164 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2; 2165 else 2166 out_hdr->sadb_msg_type = event2poltype(c->event); 2167 out_hdr->sadb_msg_errno = 0; 2168 out_hdr->sadb_msg_seq = c->seq; 2169 out_hdr->sadb_msg_pid = c->portid; 2170 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp)); 2171 return 0; 2172 2173 } 2174 2175 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 2176 { 2177 struct net *net = sock_net(sk); 2178 int err = 0; 2179 struct sadb_lifetime *lifetime; 2180 struct sadb_address *sa; 2181 struct sadb_x_policy *pol; 2182 struct xfrm_policy *xp; 2183 struct km_event c; 2184 struct sadb_x_sec_ctx *sec_ctx; 2185 2186 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 2187 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || 2188 !ext_hdrs[SADB_X_EXT_POLICY-1]) 2189 return -EINVAL; 2190 2191 pol = ext_hdrs[SADB_X_EXT_POLICY-1]; 2192 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC) 2193 return -EINVAL; 2194 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) 2195 return -EINVAL; 2196 2197 xp = xfrm_policy_alloc(net, GFP_KERNEL); 2198 if (xp == NULL) 2199 return -ENOBUFS; 2200 2201 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? 2202 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); 2203 xp->priority = pol->sadb_x_policy_priority; 2204 2205 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 2206 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr); 2207 if (!xp->family) { 2208 err = -EINVAL; 2209 goto out; 2210 } 2211 xp->selector.family = xp->family; 2212 xp->selector.prefixlen_s = sa->sadb_address_prefixlen; 2213 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2214 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port; 2215 if (xp->selector.sport) 2216 xp->selector.sport_mask = htons(0xffff); 2217 2218 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 2219 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr); 2220 xp->selector.prefixlen_d = sa->sadb_address_prefixlen; 2221 2222 /* Amusing, we set this twice. KAME apps appear to set same value 2223 * in both addresses. 2224 */ 2225 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2226 2227 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port; 2228 if (xp->selector.dport) 2229 xp->selector.dport_mask = htons(0xffff); 2230 2231 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; 2232 if (sec_ctx != NULL) { 2233 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); 2234 2235 if (!uctx) { 2236 err = -ENOBUFS; 2237 goto out; 2238 } 2239 2240 err = security_xfrm_policy_alloc(&xp->security, uctx); 2241 kfree(uctx); 2242 2243 if (err) 2244 goto out; 2245 } 2246 2247 xp->lft.soft_byte_limit = XFRM_INF; 2248 xp->lft.hard_byte_limit = XFRM_INF; 2249 xp->lft.soft_packet_limit = XFRM_INF; 2250 xp->lft.hard_packet_limit = XFRM_INF; 2251 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) { 2252 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); 2253 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); 2254 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; 2255 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; 2256 } 2257 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) { 2258 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); 2259 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); 2260 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; 2261 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; 2262 } 2263 xp->xfrm_nr = 0; 2264 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && 2265 (err = parse_ipsecrequests(xp, pol)) < 0) 2266 goto out; 2267 2268 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp, 2269 hdr->sadb_msg_type != SADB_X_SPDUPDATE); 2270 2271 xfrm_audit_policy_add(xp, err ? 0 : 1, 2272 audit_get_loginuid(current), 2273 audit_get_sessionid(current), 0); 2274 2275 if (err) 2276 goto out; 2277 2278 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE) 2279 c.event = XFRM_MSG_UPDPOLICY; 2280 else 2281 c.event = XFRM_MSG_NEWPOLICY; 2282 2283 c.seq = hdr->sadb_msg_seq; 2284 c.portid = hdr->sadb_msg_pid; 2285 2286 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); 2287 xfrm_pol_put(xp); 2288 return 0; 2289 2290 out: 2291 xp->walk.dead = 1; 2292 xfrm_policy_destroy(xp); 2293 return err; 2294 } 2295 2296 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 2297 { 2298 struct net *net = sock_net(sk); 2299 int err; 2300 struct sadb_address *sa; 2301 struct sadb_x_policy *pol; 2302 struct xfrm_policy *xp; 2303 struct xfrm_selector sel; 2304 struct km_event c; 2305 struct sadb_x_sec_ctx *sec_ctx; 2306 struct xfrm_sec_ctx *pol_ctx = NULL; 2307 2308 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 2309 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || 2310 !ext_hdrs[SADB_X_EXT_POLICY-1]) 2311 return -EINVAL; 2312 2313 pol = ext_hdrs[SADB_X_EXT_POLICY-1]; 2314 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) 2315 return -EINVAL; 2316 2317 memset(&sel, 0, sizeof(sel)); 2318 2319 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 2320 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); 2321 sel.prefixlen_s = sa->sadb_address_prefixlen; 2322 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2323 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port; 2324 if (sel.sport) 2325 sel.sport_mask = htons(0xffff); 2326 2327 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 2328 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); 2329 sel.prefixlen_d = sa->sadb_address_prefixlen; 2330 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2331 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port; 2332 if (sel.dport) 2333 sel.dport_mask = htons(0xffff); 2334 2335 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; 2336 if (sec_ctx != NULL) { 2337 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); 2338 2339 if (!uctx) 2340 return -ENOMEM; 2341 2342 err = security_xfrm_policy_alloc(&pol_ctx, uctx); 2343 kfree(uctx); 2344 if (err) 2345 return err; 2346 } 2347 2348 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN, 2349 pol->sadb_x_policy_dir - 1, &sel, pol_ctx, 2350 1, &err); 2351 security_xfrm_policy_free(pol_ctx); 2352 if (xp == NULL) 2353 return -ENOENT; 2354 2355 xfrm_audit_policy_delete(xp, err ? 0 : 1, 2356 audit_get_loginuid(current), 2357 audit_get_sessionid(current), 0); 2358 2359 if (err) 2360 goto out; 2361 2362 c.seq = hdr->sadb_msg_seq; 2363 c.portid = hdr->sadb_msg_pid; 2364 c.data.byid = 0; 2365 c.event = XFRM_MSG_DELPOLICY; 2366 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); 2367 2368 out: 2369 xfrm_pol_put(xp); 2370 return err; 2371 } 2372 2373 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir) 2374 { 2375 int err; 2376 struct sk_buff *out_skb; 2377 struct sadb_msg *out_hdr; 2378 err = 0; 2379 2380 out_skb = pfkey_xfrm_policy2msg_prep(xp); 2381 if (IS_ERR(out_skb)) { 2382 err = PTR_ERR(out_skb); 2383 goto out; 2384 } 2385 err = pfkey_xfrm_policy2msg(out_skb, xp, dir); 2386 if (err < 0) 2387 goto out; 2388 2389 out_hdr = (struct sadb_msg *) out_skb->data; 2390 out_hdr->sadb_msg_version = hdr->sadb_msg_version; 2391 out_hdr->sadb_msg_type = hdr->sadb_msg_type; 2392 out_hdr->sadb_msg_satype = 0; 2393 out_hdr->sadb_msg_errno = 0; 2394 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; 2395 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; 2396 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp)); 2397 err = 0; 2398 2399 out: 2400 return err; 2401 } 2402 2403 #ifdef CONFIG_NET_KEY_MIGRATE 2404 static int pfkey_sockaddr_pair_size(sa_family_t family) 2405 { 2406 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2); 2407 } 2408 2409 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len, 2410 xfrm_address_t *saddr, xfrm_address_t *daddr, 2411 u16 *family) 2412 { 2413 int af, socklen; 2414 2415 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family)) 2416 return -EINVAL; 2417 2418 af = pfkey_sockaddr_extract(sa, saddr); 2419 if (!af) 2420 return -EINVAL; 2421 2422 socklen = pfkey_sockaddr_len(af); 2423 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen), 2424 daddr) != af) 2425 return -EINVAL; 2426 2427 *family = af; 2428 return 0; 2429 } 2430 2431 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len, 2432 struct xfrm_migrate *m) 2433 { 2434 int err; 2435 struct sadb_x_ipsecrequest *rq2; 2436 int mode; 2437 2438 if (len <= sizeof(struct sadb_x_ipsecrequest) || 2439 len < rq1->sadb_x_ipsecrequest_len) 2440 return -EINVAL; 2441 2442 /* old endoints */ 2443 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1), 2444 rq1->sadb_x_ipsecrequest_len, 2445 &m->old_saddr, &m->old_daddr, 2446 &m->old_family); 2447 if (err) 2448 return err; 2449 2450 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len); 2451 len -= rq1->sadb_x_ipsecrequest_len; 2452 2453 if (len <= sizeof(struct sadb_x_ipsecrequest) || 2454 len < rq2->sadb_x_ipsecrequest_len) 2455 return -EINVAL; 2456 2457 /* new endpoints */ 2458 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1), 2459 rq2->sadb_x_ipsecrequest_len, 2460 &m->new_saddr, &m->new_daddr, 2461 &m->new_family); 2462 if (err) 2463 return err; 2464 2465 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto || 2466 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode || 2467 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid) 2468 return -EINVAL; 2469 2470 m->proto = rq1->sadb_x_ipsecrequest_proto; 2471 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0) 2472 return -EINVAL; 2473 m->mode = mode; 2474 m->reqid = rq1->sadb_x_ipsecrequest_reqid; 2475 2476 return ((int)(rq1->sadb_x_ipsecrequest_len + 2477 rq2->sadb_x_ipsecrequest_len)); 2478 } 2479 2480 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, 2481 const struct sadb_msg *hdr, void * const *ext_hdrs) 2482 { 2483 int i, len, ret, err = -EINVAL; 2484 u8 dir; 2485 struct sadb_address *sa; 2486 struct sadb_x_kmaddress *kma; 2487 struct sadb_x_policy *pol; 2488 struct sadb_x_ipsecrequest *rq; 2489 struct xfrm_selector sel; 2490 struct xfrm_migrate m[XFRM_MAX_DEPTH]; 2491 struct xfrm_kmaddress k; 2492 2493 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1], 2494 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) || 2495 !ext_hdrs[SADB_X_EXT_POLICY - 1]) { 2496 err = -EINVAL; 2497 goto out; 2498 } 2499 2500 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1]; 2501 pol = ext_hdrs[SADB_X_EXT_POLICY - 1]; 2502 2503 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) { 2504 err = -EINVAL; 2505 goto out; 2506 } 2507 2508 if (kma) { 2509 /* convert sadb_x_kmaddress to xfrm_kmaddress */ 2510 k.reserved = kma->sadb_x_kmaddress_reserved; 2511 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1), 2512 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma), 2513 &k.local, &k.remote, &k.family); 2514 if (ret < 0) { 2515 err = ret; 2516 goto out; 2517 } 2518 } 2519 2520 dir = pol->sadb_x_policy_dir - 1; 2521 memset(&sel, 0, sizeof(sel)); 2522 2523 /* set source address info of selector */ 2524 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1]; 2525 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); 2526 sel.prefixlen_s = sa->sadb_address_prefixlen; 2527 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2528 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port; 2529 if (sel.sport) 2530 sel.sport_mask = htons(0xffff); 2531 2532 /* set destination address info of selector */ 2533 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1], 2534 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); 2535 sel.prefixlen_d = sa->sadb_address_prefixlen; 2536 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); 2537 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port; 2538 if (sel.dport) 2539 sel.dport_mask = htons(0xffff); 2540 2541 rq = (struct sadb_x_ipsecrequest *)(pol + 1); 2542 2543 /* extract ipsecrequests */ 2544 i = 0; 2545 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy); 2546 2547 while (len > 0 && i < XFRM_MAX_DEPTH) { 2548 ret = ipsecrequests_to_migrate(rq, len, &m[i]); 2549 if (ret < 0) { 2550 err = ret; 2551 goto out; 2552 } else { 2553 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret); 2554 len -= ret; 2555 i++; 2556 } 2557 } 2558 2559 if (!i || len > 0) { 2560 err = -EINVAL; 2561 goto out; 2562 } 2563 2564 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i, 2565 kma ? &k : NULL); 2566 2567 out: 2568 return err; 2569 } 2570 #else 2571 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, 2572 const struct sadb_msg *hdr, void * const *ext_hdrs) 2573 { 2574 return -ENOPROTOOPT; 2575 } 2576 #endif 2577 2578 2579 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 2580 { 2581 struct net *net = sock_net(sk); 2582 unsigned int dir; 2583 int err = 0, delete; 2584 struct sadb_x_policy *pol; 2585 struct xfrm_policy *xp; 2586 struct km_event c; 2587 2588 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL) 2589 return -EINVAL; 2590 2591 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id); 2592 if (dir >= XFRM_POLICY_MAX) 2593 return -EINVAL; 2594 2595 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2); 2596 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN, 2597 dir, pol->sadb_x_policy_id, delete, &err); 2598 if (xp == NULL) 2599 return -ENOENT; 2600 2601 if (delete) { 2602 xfrm_audit_policy_delete(xp, err ? 0 : 1, 2603 audit_get_loginuid(current), 2604 audit_get_sessionid(current), 0); 2605 2606 if (err) 2607 goto out; 2608 c.seq = hdr->sadb_msg_seq; 2609 c.portid = hdr->sadb_msg_pid; 2610 c.data.byid = 1; 2611 c.event = XFRM_MSG_DELPOLICY; 2612 km_policy_notify(xp, dir, &c); 2613 } else { 2614 err = key_pol_get_resp(sk, xp, hdr, dir); 2615 } 2616 2617 out: 2618 xfrm_pol_put(xp); 2619 return err; 2620 } 2621 2622 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr) 2623 { 2624 struct pfkey_sock *pfk = ptr; 2625 struct sk_buff *out_skb; 2626 struct sadb_msg *out_hdr; 2627 int err; 2628 2629 if (!pfkey_can_dump(&pfk->sk)) 2630 return -ENOBUFS; 2631 2632 out_skb = pfkey_xfrm_policy2msg_prep(xp); 2633 if (IS_ERR(out_skb)) 2634 return PTR_ERR(out_skb); 2635 2636 err = pfkey_xfrm_policy2msg(out_skb, xp, dir); 2637 if (err < 0) 2638 return err; 2639 2640 out_hdr = (struct sadb_msg *) out_skb->data; 2641 out_hdr->sadb_msg_version = pfk->dump.msg_version; 2642 out_hdr->sadb_msg_type = SADB_X_SPDDUMP; 2643 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC; 2644 out_hdr->sadb_msg_errno = 0; 2645 out_hdr->sadb_msg_seq = count + 1; 2646 out_hdr->sadb_msg_pid = pfk->dump.msg_portid; 2647 2648 if (pfk->dump.skb) 2649 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, 2650 &pfk->sk, sock_net(&pfk->sk)); 2651 pfk->dump.skb = out_skb; 2652 2653 return 0; 2654 } 2655 2656 static int pfkey_dump_sp(struct pfkey_sock *pfk) 2657 { 2658 struct net *net = sock_net(&pfk->sk); 2659 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk); 2660 } 2661 2662 static void pfkey_dump_sp_done(struct pfkey_sock *pfk) 2663 { 2664 xfrm_policy_walk_done(&pfk->dump.u.policy); 2665 } 2666 2667 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 2668 { 2669 struct pfkey_sock *pfk = pfkey_sk(sk); 2670 2671 if (pfk->dump.dump != NULL) 2672 return -EBUSY; 2673 2674 pfk->dump.msg_version = hdr->sadb_msg_version; 2675 pfk->dump.msg_portid = hdr->sadb_msg_pid; 2676 pfk->dump.dump = pfkey_dump_sp; 2677 pfk->dump.done = pfkey_dump_sp_done; 2678 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN); 2679 2680 return pfkey_do_dump(pfk); 2681 } 2682 2683 static int key_notify_policy_flush(const struct km_event *c) 2684 { 2685 struct sk_buff *skb_out; 2686 struct sadb_msg *hdr; 2687 2688 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); 2689 if (!skb_out) 2690 return -ENOBUFS; 2691 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg)); 2692 hdr->sadb_msg_type = SADB_X_SPDFLUSH; 2693 hdr->sadb_msg_seq = c->seq; 2694 hdr->sadb_msg_pid = c->portid; 2695 hdr->sadb_msg_version = PF_KEY_V2; 2696 hdr->sadb_msg_errno = (uint8_t) 0; 2697 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); 2698 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net); 2699 return 0; 2700 2701 } 2702 2703 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) 2704 { 2705 struct net *net = sock_net(sk); 2706 struct km_event c; 2707 struct xfrm_audit audit_info; 2708 int err, err2; 2709 2710 audit_info.loginuid = audit_get_loginuid(current); 2711 audit_info.sessionid = audit_get_sessionid(current); 2712 audit_info.secid = 0; 2713 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info); 2714 err2 = unicast_flush_resp(sk, hdr); 2715 if (err || err2) { 2716 if (err == -ESRCH) /* empty table - old silent behavior */ 2717 return 0; 2718 return err; 2719 } 2720 2721 c.data.type = XFRM_POLICY_TYPE_MAIN; 2722 c.event = XFRM_MSG_FLUSHPOLICY; 2723 c.portid = hdr->sadb_msg_pid; 2724 c.seq = hdr->sadb_msg_seq; 2725 c.net = net; 2726 km_policy_notify(NULL, 0, &c); 2727 2728 return 0; 2729 } 2730 2731 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb, 2732 const struct sadb_msg *hdr, void * const *ext_hdrs); 2733 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = { 2734 [SADB_RESERVED] = pfkey_reserved, 2735 [SADB_GETSPI] = pfkey_getspi, 2736 [SADB_UPDATE] = pfkey_add, 2737 [SADB_ADD] = pfkey_add, 2738 [SADB_DELETE] = pfkey_delete, 2739 [SADB_GET] = pfkey_get, 2740 [SADB_ACQUIRE] = pfkey_acquire, 2741 [SADB_REGISTER] = pfkey_register, 2742 [SADB_EXPIRE] = NULL, 2743 [SADB_FLUSH] = pfkey_flush, 2744 [SADB_DUMP] = pfkey_dump, 2745 [SADB_X_PROMISC] = pfkey_promisc, 2746 [SADB_X_PCHANGE] = NULL, 2747 [SADB_X_SPDUPDATE] = pfkey_spdadd, 2748 [SADB_X_SPDADD] = pfkey_spdadd, 2749 [SADB_X_SPDDELETE] = pfkey_spddelete, 2750 [SADB_X_SPDGET] = pfkey_spdget, 2751 [SADB_X_SPDACQUIRE] = NULL, 2752 [SADB_X_SPDDUMP] = pfkey_spddump, 2753 [SADB_X_SPDFLUSH] = pfkey_spdflush, 2754 [SADB_X_SPDSETIDX] = pfkey_spdadd, 2755 [SADB_X_SPDDELETE2] = pfkey_spdget, 2756 [SADB_X_MIGRATE] = pfkey_migrate, 2757 }; 2758 2759 static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr) 2760 { 2761 void *ext_hdrs[SADB_EXT_MAX]; 2762 int err; 2763 2764 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, 2765 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk)); 2766 2767 memset(ext_hdrs, 0, sizeof(ext_hdrs)); 2768 err = parse_exthdrs(skb, hdr, ext_hdrs); 2769 if (!err) { 2770 err = -EOPNOTSUPP; 2771 if (pfkey_funcs[hdr->sadb_msg_type]) 2772 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs); 2773 } 2774 return err; 2775 } 2776 2777 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp) 2778 { 2779 struct sadb_msg *hdr = NULL; 2780 2781 if (skb->len < sizeof(*hdr)) { 2782 *errp = -EMSGSIZE; 2783 } else { 2784 hdr = (struct sadb_msg *) skb->data; 2785 if (hdr->sadb_msg_version != PF_KEY_V2 || 2786 hdr->sadb_msg_reserved != 0 || 2787 (hdr->sadb_msg_type <= SADB_RESERVED || 2788 hdr->sadb_msg_type > SADB_MAX)) { 2789 hdr = NULL; 2790 *errp = -EINVAL; 2791 } else if (hdr->sadb_msg_len != (skb->len / 2792 sizeof(uint64_t)) || 2793 hdr->sadb_msg_len < (sizeof(struct sadb_msg) / 2794 sizeof(uint64_t))) { 2795 hdr = NULL; 2796 *errp = -EMSGSIZE; 2797 } else { 2798 *errp = 0; 2799 } 2800 } 2801 return hdr; 2802 } 2803 2804 static inline int aalg_tmpl_set(const struct xfrm_tmpl *t, 2805 const struct xfrm_algo_desc *d) 2806 { 2807 unsigned int id = d->desc.sadb_alg_id; 2808 2809 if (id >= sizeof(t->aalgos) * 8) 2810 return 0; 2811 2812 return (t->aalgos >> id) & 1; 2813 } 2814 2815 static inline int ealg_tmpl_set(const struct xfrm_tmpl *t, 2816 const struct xfrm_algo_desc *d) 2817 { 2818 unsigned int id = d->desc.sadb_alg_id; 2819 2820 if (id >= sizeof(t->ealgos) * 8) 2821 return 0; 2822 2823 return (t->ealgos >> id) & 1; 2824 } 2825 2826 static int count_ah_combs(const struct xfrm_tmpl *t) 2827 { 2828 int i, sz = 0; 2829 2830 for (i = 0; ; i++) { 2831 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); 2832 if (!aalg) 2833 break; 2834 if (!aalg->pfkey_supported) 2835 continue; 2836 if (aalg_tmpl_set(t, aalg) && aalg->available) 2837 sz += sizeof(struct sadb_comb); 2838 } 2839 return sz + sizeof(struct sadb_prop); 2840 } 2841 2842 static int count_esp_combs(const struct xfrm_tmpl *t) 2843 { 2844 int i, k, sz = 0; 2845 2846 for (i = 0; ; i++) { 2847 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); 2848 if (!ealg) 2849 break; 2850 2851 if (!ealg->pfkey_supported) 2852 continue; 2853 2854 if (!(ealg_tmpl_set(t, ealg) && ealg->available)) 2855 continue; 2856 2857 for (k = 1; ; k++) { 2858 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); 2859 if (!aalg) 2860 break; 2861 2862 if (!aalg->pfkey_supported) 2863 continue; 2864 2865 if (aalg_tmpl_set(t, aalg) && aalg->available) 2866 sz += sizeof(struct sadb_comb); 2867 } 2868 } 2869 return sz + sizeof(struct sadb_prop); 2870 } 2871 2872 static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t) 2873 { 2874 struct sadb_prop *p; 2875 int i; 2876 2877 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop)); 2878 p->sadb_prop_len = sizeof(struct sadb_prop)/8; 2879 p->sadb_prop_exttype = SADB_EXT_PROPOSAL; 2880 p->sadb_prop_replay = 32; 2881 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); 2882 2883 for (i = 0; ; i++) { 2884 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); 2885 if (!aalg) 2886 break; 2887 2888 if (!aalg->pfkey_supported) 2889 continue; 2890 2891 if (aalg_tmpl_set(t, aalg) && aalg->available) { 2892 struct sadb_comb *c; 2893 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb)); 2894 memset(c, 0, sizeof(*c)); 2895 p->sadb_prop_len += sizeof(struct sadb_comb)/8; 2896 c->sadb_comb_auth = aalg->desc.sadb_alg_id; 2897 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; 2898 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; 2899 c->sadb_comb_hard_addtime = 24*60*60; 2900 c->sadb_comb_soft_addtime = 20*60*60; 2901 c->sadb_comb_hard_usetime = 8*60*60; 2902 c->sadb_comb_soft_usetime = 7*60*60; 2903 } 2904 } 2905 } 2906 2907 static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t) 2908 { 2909 struct sadb_prop *p; 2910 int i, k; 2911 2912 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop)); 2913 p->sadb_prop_len = sizeof(struct sadb_prop)/8; 2914 p->sadb_prop_exttype = SADB_EXT_PROPOSAL; 2915 p->sadb_prop_replay = 32; 2916 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); 2917 2918 for (i=0; ; i++) { 2919 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); 2920 if (!ealg) 2921 break; 2922 2923 if (!ealg->pfkey_supported) 2924 continue; 2925 2926 if (!(ealg_tmpl_set(t, ealg) && ealg->available)) 2927 continue; 2928 2929 for (k = 1; ; k++) { 2930 struct sadb_comb *c; 2931 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); 2932 if (!aalg) 2933 break; 2934 if (!aalg->pfkey_supported) 2935 continue; 2936 if (!(aalg_tmpl_set(t, aalg) && aalg->available)) 2937 continue; 2938 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb)); 2939 memset(c, 0, sizeof(*c)); 2940 p->sadb_prop_len += sizeof(struct sadb_comb)/8; 2941 c->sadb_comb_auth = aalg->desc.sadb_alg_id; 2942 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; 2943 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; 2944 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id; 2945 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits; 2946 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits; 2947 c->sadb_comb_hard_addtime = 24*60*60; 2948 c->sadb_comb_soft_addtime = 20*60*60; 2949 c->sadb_comb_hard_usetime = 8*60*60; 2950 c->sadb_comb_soft_usetime = 7*60*60; 2951 } 2952 } 2953 } 2954 2955 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c) 2956 { 2957 return 0; 2958 } 2959 2960 static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c) 2961 { 2962 struct sk_buff *out_skb; 2963 struct sadb_msg *out_hdr; 2964 int hard; 2965 int hsc; 2966 2967 hard = c->data.hard; 2968 if (hard) 2969 hsc = 2; 2970 else 2971 hsc = 1; 2972 2973 out_skb = pfkey_xfrm_state2msg_expire(x, hsc); 2974 if (IS_ERR(out_skb)) 2975 return PTR_ERR(out_skb); 2976 2977 out_hdr = (struct sadb_msg *) out_skb->data; 2978 out_hdr->sadb_msg_version = PF_KEY_V2; 2979 out_hdr->sadb_msg_type = SADB_EXPIRE; 2980 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); 2981 out_hdr->sadb_msg_errno = 0; 2982 out_hdr->sadb_msg_reserved = 0; 2983 out_hdr->sadb_msg_seq = 0; 2984 out_hdr->sadb_msg_pid = 0; 2985 2986 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x)); 2987 return 0; 2988 } 2989 2990 static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c) 2991 { 2992 struct net *net = x ? xs_net(x) : c->net; 2993 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 2994 2995 if (atomic_read(&net_pfkey->socks_nr) == 0) 2996 return 0; 2997 2998 switch (c->event) { 2999 case XFRM_MSG_EXPIRE: 3000 return key_notify_sa_expire(x, c); 3001 case XFRM_MSG_DELSA: 3002 case XFRM_MSG_NEWSA: 3003 case XFRM_MSG_UPDSA: 3004 return key_notify_sa(x, c); 3005 case XFRM_MSG_FLUSHSA: 3006 return key_notify_sa_flush(c); 3007 case XFRM_MSG_NEWAE: /* not yet supported */ 3008 break; 3009 default: 3010 pr_err("pfkey: Unknown SA event %d\n", c->event); 3011 break; 3012 } 3013 3014 return 0; 3015 } 3016 3017 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 3018 { 3019 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN) 3020 return 0; 3021 3022 switch (c->event) { 3023 case XFRM_MSG_POLEXPIRE: 3024 return key_notify_policy_expire(xp, c); 3025 case XFRM_MSG_DELPOLICY: 3026 case XFRM_MSG_NEWPOLICY: 3027 case XFRM_MSG_UPDPOLICY: 3028 return key_notify_policy(xp, dir, c); 3029 case XFRM_MSG_FLUSHPOLICY: 3030 if (c->data.type != XFRM_POLICY_TYPE_MAIN) 3031 break; 3032 return key_notify_policy_flush(c); 3033 default: 3034 pr_err("pfkey: Unknown policy event %d\n", c->event); 3035 break; 3036 } 3037 3038 return 0; 3039 } 3040 3041 static u32 get_acqseq(void) 3042 { 3043 u32 res; 3044 static atomic_t acqseq; 3045 3046 do { 3047 res = atomic_inc_return(&acqseq); 3048 } while (!res); 3049 return res; 3050 } 3051 3052 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp) 3053 { 3054 struct sk_buff *skb; 3055 struct sadb_msg *hdr; 3056 struct sadb_address *addr; 3057 struct sadb_x_policy *pol; 3058 int sockaddr_size; 3059 int size; 3060 struct sadb_x_sec_ctx *sec_ctx; 3061 struct xfrm_sec_ctx *xfrm_ctx; 3062 int ctx_size = 0; 3063 3064 sockaddr_size = pfkey_sockaddr_size(x->props.family); 3065 if (!sockaddr_size) 3066 return -EINVAL; 3067 3068 size = sizeof(struct sadb_msg) + 3069 (sizeof(struct sadb_address) * 2) + 3070 (sockaddr_size * 2) + 3071 sizeof(struct sadb_x_policy); 3072 3073 if (x->id.proto == IPPROTO_AH) 3074 size += count_ah_combs(t); 3075 else if (x->id.proto == IPPROTO_ESP) 3076 size += count_esp_combs(t); 3077 3078 if ((xfrm_ctx = x->security)) { 3079 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); 3080 size += sizeof(struct sadb_x_sec_ctx) + ctx_size; 3081 } 3082 3083 skb = alloc_skb(size + 16, GFP_ATOMIC); 3084 if (skb == NULL) 3085 return -ENOMEM; 3086 3087 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 3088 hdr->sadb_msg_version = PF_KEY_V2; 3089 hdr->sadb_msg_type = SADB_ACQUIRE; 3090 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); 3091 hdr->sadb_msg_len = size / sizeof(uint64_t); 3092 hdr->sadb_msg_errno = 0; 3093 hdr->sadb_msg_reserved = 0; 3094 hdr->sadb_msg_seq = x->km.seq = get_acqseq(); 3095 hdr->sadb_msg_pid = 0; 3096 3097 /* src address */ 3098 addr = (struct sadb_address*) skb_put(skb, 3099 sizeof(struct sadb_address)+sockaddr_size); 3100 addr->sadb_address_len = 3101 (sizeof(struct sadb_address)+sockaddr_size)/ 3102 sizeof(uint64_t); 3103 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 3104 addr->sadb_address_proto = 0; 3105 addr->sadb_address_reserved = 0; 3106 addr->sadb_address_prefixlen = 3107 pfkey_sockaddr_fill(&x->props.saddr, 0, 3108 (struct sockaddr *) (addr + 1), 3109 x->props.family); 3110 if (!addr->sadb_address_prefixlen) 3111 BUG(); 3112 3113 /* dst address */ 3114 addr = (struct sadb_address*) skb_put(skb, 3115 sizeof(struct sadb_address)+sockaddr_size); 3116 addr->sadb_address_len = 3117 (sizeof(struct sadb_address)+sockaddr_size)/ 3118 sizeof(uint64_t); 3119 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; 3120 addr->sadb_address_proto = 0; 3121 addr->sadb_address_reserved = 0; 3122 addr->sadb_address_prefixlen = 3123 pfkey_sockaddr_fill(&x->id.daddr, 0, 3124 (struct sockaddr *) (addr + 1), 3125 x->props.family); 3126 if (!addr->sadb_address_prefixlen) 3127 BUG(); 3128 3129 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy)); 3130 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); 3131 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 3132 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; 3133 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1; 3134 pol->sadb_x_policy_id = xp->index; 3135 3136 /* Set sadb_comb's. */ 3137 if (x->id.proto == IPPROTO_AH) 3138 dump_ah_combs(skb, t); 3139 else if (x->id.proto == IPPROTO_ESP) 3140 dump_esp_combs(skb, t); 3141 3142 /* security context */ 3143 if (xfrm_ctx) { 3144 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, 3145 sizeof(struct sadb_x_sec_ctx) + ctx_size); 3146 sec_ctx->sadb_x_sec_len = 3147 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); 3148 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; 3149 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; 3150 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; 3151 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; 3152 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, 3153 xfrm_ctx->ctx_len); 3154 } 3155 3156 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x)); 3157 } 3158 3159 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt, 3160 u8 *data, int len, int *dir) 3161 { 3162 struct net *net = sock_net(sk); 3163 struct xfrm_policy *xp; 3164 struct sadb_x_policy *pol = (struct sadb_x_policy*)data; 3165 struct sadb_x_sec_ctx *sec_ctx; 3166 3167 switch (sk->sk_family) { 3168 case AF_INET: 3169 if (opt != IP_IPSEC_POLICY) { 3170 *dir = -EOPNOTSUPP; 3171 return NULL; 3172 } 3173 break; 3174 #if IS_ENABLED(CONFIG_IPV6) 3175 case AF_INET6: 3176 if (opt != IPV6_IPSEC_POLICY) { 3177 *dir = -EOPNOTSUPP; 3178 return NULL; 3179 } 3180 break; 3181 #endif 3182 default: 3183 *dir = -EINVAL; 3184 return NULL; 3185 } 3186 3187 *dir = -EINVAL; 3188 3189 if (len < sizeof(struct sadb_x_policy) || 3190 pol->sadb_x_policy_len*8 > len || 3191 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS || 3192 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND)) 3193 return NULL; 3194 3195 xp = xfrm_policy_alloc(net, GFP_ATOMIC); 3196 if (xp == NULL) { 3197 *dir = -ENOBUFS; 3198 return NULL; 3199 } 3200 3201 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? 3202 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); 3203 3204 xp->lft.soft_byte_limit = XFRM_INF; 3205 xp->lft.hard_byte_limit = XFRM_INF; 3206 xp->lft.soft_packet_limit = XFRM_INF; 3207 xp->lft.hard_packet_limit = XFRM_INF; 3208 xp->family = sk->sk_family; 3209 3210 xp->xfrm_nr = 0; 3211 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && 3212 (*dir = parse_ipsecrequests(xp, pol)) < 0) 3213 goto out; 3214 3215 /* security context too */ 3216 if (len >= (pol->sadb_x_policy_len*8 + 3217 sizeof(struct sadb_x_sec_ctx))) { 3218 char *p = (char *)pol; 3219 struct xfrm_user_sec_ctx *uctx; 3220 3221 p += pol->sadb_x_policy_len*8; 3222 sec_ctx = (struct sadb_x_sec_ctx *)p; 3223 if (len < pol->sadb_x_policy_len*8 + 3224 sec_ctx->sadb_x_sec_len) { 3225 *dir = -EINVAL; 3226 goto out; 3227 } 3228 if ((*dir = verify_sec_ctx_len(p))) 3229 goto out; 3230 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx); 3231 *dir = security_xfrm_policy_alloc(&xp->security, uctx); 3232 kfree(uctx); 3233 3234 if (*dir) 3235 goto out; 3236 } 3237 3238 *dir = pol->sadb_x_policy_dir-1; 3239 return xp; 3240 3241 out: 3242 xp->walk.dead = 1; 3243 xfrm_policy_destroy(xp); 3244 return NULL; 3245 } 3246 3247 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) 3248 { 3249 struct sk_buff *skb; 3250 struct sadb_msg *hdr; 3251 struct sadb_sa *sa; 3252 struct sadb_address *addr; 3253 struct sadb_x_nat_t_port *n_port; 3254 int sockaddr_size; 3255 int size; 3256 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0); 3257 struct xfrm_encap_tmpl *natt = NULL; 3258 3259 sockaddr_size = pfkey_sockaddr_size(x->props.family); 3260 if (!sockaddr_size) 3261 return -EINVAL; 3262 3263 if (!satype) 3264 return -EINVAL; 3265 3266 if (!x->encap) 3267 return -EINVAL; 3268 3269 natt = x->encap; 3270 3271 /* Build an SADB_X_NAT_T_NEW_MAPPING message: 3272 * 3273 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) | 3274 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port) 3275 */ 3276 3277 size = sizeof(struct sadb_msg) + 3278 sizeof(struct sadb_sa) + 3279 (sizeof(struct sadb_address) * 2) + 3280 (sockaddr_size * 2) + 3281 (sizeof(struct sadb_x_nat_t_port) * 2); 3282 3283 skb = alloc_skb(size + 16, GFP_ATOMIC); 3284 if (skb == NULL) 3285 return -ENOMEM; 3286 3287 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg)); 3288 hdr->sadb_msg_version = PF_KEY_V2; 3289 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING; 3290 hdr->sadb_msg_satype = satype; 3291 hdr->sadb_msg_len = size / sizeof(uint64_t); 3292 hdr->sadb_msg_errno = 0; 3293 hdr->sadb_msg_reserved = 0; 3294 hdr->sadb_msg_seq = x->km.seq = get_acqseq(); 3295 hdr->sadb_msg_pid = 0; 3296 3297 /* SA */ 3298 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa)); 3299 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); 3300 sa->sadb_sa_exttype = SADB_EXT_SA; 3301 sa->sadb_sa_spi = x->id.spi; 3302 sa->sadb_sa_replay = 0; 3303 sa->sadb_sa_state = 0; 3304 sa->sadb_sa_auth = 0; 3305 sa->sadb_sa_encrypt = 0; 3306 sa->sadb_sa_flags = 0; 3307 3308 /* ADDRESS_SRC (old addr) */ 3309 addr = (struct sadb_address*) 3310 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size); 3311 addr->sadb_address_len = 3312 (sizeof(struct sadb_address)+sockaddr_size)/ 3313 sizeof(uint64_t); 3314 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; 3315 addr->sadb_address_proto = 0; 3316 addr->sadb_address_reserved = 0; 3317 addr->sadb_address_prefixlen = 3318 pfkey_sockaddr_fill(&x->props.saddr, 0, 3319 (struct sockaddr *) (addr + 1), 3320 x->props.family); 3321 if (!addr->sadb_address_prefixlen) 3322 BUG(); 3323 3324 /* NAT_T_SPORT (old port) */ 3325 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); 3326 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); 3327 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; 3328 n_port->sadb_x_nat_t_port_port = natt->encap_sport; 3329 n_port->sadb_x_nat_t_port_reserved = 0; 3330 3331 /* ADDRESS_DST (new addr) */ 3332 addr = (struct sadb_address*) 3333 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size); 3334 addr->sadb_address_len = 3335 (sizeof(struct sadb_address)+sockaddr_size)/ 3336 sizeof(uint64_t); 3337 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; 3338 addr->sadb_address_proto = 0; 3339 addr->sadb_address_reserved = 0; 3340 addr->sadb_address_prefixlen = 3341 pfkey_sockaddr_fill(ipaddr, 0, 3342 (struct sockaddr *) (addr + 1), 3343 x->props.family); 3344 if (!addr->sadb_address_prefixlen) 3345 BUG(); 3346 3347 /* NAT_T_DPORT (new port) */ 3348 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port)); 3349 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); 3350 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; 3351 n_port->sadb_x_nat_t_port_port = sport; 3352 n_port->sadb_x_nat_t_port_reserved = 0; 3353 3354 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x)); 3355 } 3356 3357 #ifdef CONFIG_NET_KEY_MIGRATE 3358 static int set_sadb_address(struct sk_buff *skb, int sasize, int type, 3359 const struct xfrm_selector *sel) 3360 { 3361 struct sadb_address *addr; 3362 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize); 3363 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8; 3364 addr->sadb_address_exttype = type; 3365 addr->sadb_address_proto = sel->proto; 3366 addr->sadb_address_reserved = 0; 3367 3368 switch (type) { 3369 case SADB_EXT_ADDRESS_SRC: 3370 addr->sadb_address_prefixlen = sel->prefixlen_s; 3371 pfkey_sockaddr_fill(&sel->saddr, 0, 3372 (struct sockaddr *)(addr + 1), 3373 sel->family); 3374 break; 3375 case SADB_EXT_ADDRESS_DST: 3376 addr->sadb_address_prefixlen = sel->prefixlen_d; 3377 pfkey_sockaddr_fill(&sel->daddr, 0, 3378 (struct sockaddr *)(addr + 1), 3379 sel->family); 3380 break; 3381 default: 3382 return -EINVAL; 3383 } 3384 3385 return 0; 3386 } 3387 3388 3389 static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k) 3390 { 3391 struct sadb_x_kmaddress *kma; 3392 u8 *sa; 3393 int family = k->family; 3394 int socklen = pfkey_sockaddr_len(family); 3395 int size_req; 3396 3397 size_req = (sizeof(struct sadb_x_kmaddress) + 3398 pfkey_sockaddr_pair_size(family)); 3399 3400 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req); 3401 memset(kma, 0, size_req); 3402 kma->sadb_x_kmaddress_len = size_req / 8; 3403 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS; 3404 kma->sadb_x_kmaddress_reserved = k->reserved; 3405 3406 sa = (u8 *)(kma + 1); 3407 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) || 3408 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family)) 3409 return -EINVAL; 3410 3411 return 0; 3412 } 3413 3414 static int set_ipsecrequest(struct sk_buff *skb, 3415 uint8_t proto, uint8_t mode, int level, 3416 uint32_t reqid, uint8_t family, 3417 const xfrm_address_t *src, const xfrm_address_t *dst) 3418 { 3419 struct sadb_x_ipsecrequest *rq; 3420 u8 *sa; 3421 int socklen = pfkey_sockaddr_len(family); 3422 int size_req; 3423 3424 size_req = sizeof(struct sadb_x_ipsecrequest) + 3425 pfkey_sockaddr_pair_size(family); 3426 3427 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req); 3428 memset(rq, 0, size_req); 3429 rq->sadb_x_ipsecrequest_len = size_req; 3430 rq->sadb_x_ipsecrequest_proto = proto; 3431 rq->sadb_x_ipsecrequest_mode = mode; 3432 rq->sadb_x_ipsecrequest_level = level; 3433 rq->sadb_x_ipsecrequest_reqid = reqid; 3434 3435 sa = (u8 *) (rq + 1); 3436 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) || 3437 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family)) 3438 return -EINVAL; 3439 3440 return 0; 3441 } 3442 #endif 3443 3444 #ifdef CONFIG_NET_KEY_MIGRATE 3445 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 3446 const struct xfrm_migrate *m, int num_bundles, 3447 const struct xfrm_kmaddress *k) 3448 { 3449 int i; 3450 int sasize_sel; 3451 int size = 0; 3452 int size_pol = 0; 3453 struct sk_buff *skb; 3454 struct sadb_msg *hdr; 3455 struct sadb_x_policy *pol; 3456 const struct xfrm_migrate *mp; 3457 3458 if (type != XFRM_POLICY_TYPE_MAIN) 3459 return 0; 3460 3461 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH) 3462 return -EINVAL; 3463 3464 if (k != NULL) { 3465 /* addresses for KM */ 3466 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) + 3467 pfkey_sockaddr_pair_size(k->family)); 3468 } 3469 3470 /* selector */ 3471 sasize_sel = pfkey_sockaddr_size(sel->family); 3472 if (!sasize_sel) 3473 return -EINVAL; 3474 size += (sizeof(struct sadb_address) + sasize_sel) * 2; 3475 3476 /* policy info */ 3477 size_pol += sizeof(struct sadb_x_policy); 3478 3479 /* ipsecrequests */ 3480 for (i = 0, mp = m; i < num_bundles; i++, mp++) { 3481 /* old locator pair */ 3482 size_pol += sizeof(struct sadb_x_ipsecrequest) + 3483 pfkey_sockaddr_pair_size(mp->old_family); 3484 /* new locator pair */ 3485 size_pol += sizeof(struct sadb_x_ipsecrequest) + 3486 pfkey_sockaddr_pair_size(mp->new_family); 3487 } 3488 3489 size += sizeof(struct sadb_msg) + size_pol; 3490 3491 /* alloc buffer */ 3492 skb = alloc_skb(size, GFP_ATOMIC); 3493 if (skb == NULL) 3494 return -ENOMEM; 3495 3496 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg)); 3497 hdr->sadb_msg_version = PF_KEY_V2; 3498 hdr->sadb_msg_type = SADB_X_MIGRATE; 3499 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto); 3500 hdr->sadb_msg_len = size / 8; 3501 hdr->sadb_msg_errno = 0; 3502 hdr->sadb_msg_reserved = 0; 3503 hdr->sadb_msg_seq = 0; 3504 hdr->sadb_msg_pid = 0; 3505 3506 /* Addresses to be used by KM for negotiation, if ext is available */ 3507 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0)) 3508 goto err; 3509 3510 /* selector src */ 3511 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel); 3512 3513 /* selector dst */ 3514 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel); 3515 3516 /* policy information */ 3517 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy)); 3518 pol->sadb_x_policy_len = size_pol / 8; 3519 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; 3520 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; 3521 pol->sadb_x_policy_dir = dir + 1; 3522 pol->sadb_x_policy_id = 0; 3523 pol->sadb_x_policy_priority = 0; 3524 3525 for (i = 0, mp = m; i < num_bundles; i++, mp++) { 3526 /* old ipsecrequest */ 3527 int mode = pfkey_mode_from_xfrm(mp->mode); 3528 if (mode < 0) 3529 goto err; 3530 if (set_ipsecrequest(skb, mp->proto, mode, 3531 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), 3532 mp->reqid, mp->old_family, 3533 &mp->old_saddr, &mp->old_daddr) < 0) 3534 goto err; 3535 3536 /* new ipsecrequest */ 3537 if (set_ipsecrequest(skb, mp->proto, mode, 3538 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), 3539 mp->reqid, mp->new_family, 3540 &mp->new_saddr, &mp->new_daddr) < 0) 3541 goto err; 3542 } 3543 3544 /* broadcast migrate message to sockets */ 3545 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net); 3546 3547 return 0; 3548 3549 err: 3550 kfree_skb(skb); 3551 return -EINVAL; 3552 } 3553 #else 3554 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 3555 const struct xfrm_migrate *m, int num_bundles, 3556 const struct xfrm_kmaddress *k) 3557 { 3558 return -ENOPROTOOPT; 3559 } 3560 #endif 3561 3562 static int pfkey_sendmsg(struct kiocb *kiocb, 3563 struct socket *sock, struct msghdr *msg, size_t len) 3564 { 3565 struct sock *sk = sock->sk; 3566 struct sk_buff *skb = NULL; 3567 struct sadb_msg *hdr = NULL; 3568 int err; 3569 3570 err = -EOPNOTSUPP; 3571 if (msg->msg_flags & MSG_OOB) 3572 goto out; 3573 3574 err = -EMSGSIZE; 3575 if ((unsigned int)len > sk->sk_sndbuf - 32) 3576 goto out; 3577 3578 err = -ENOBUFS; 3579 skb = alloc_skb(len, GFP_KERNEL); 3580 if (skb == NULL) 3581 goto out; 3582 3583 err = -EFAULT; 3584 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) 3585 goto out; 3586 3587 hdr = pfkey_get_base_msg(skb, &err); 3588 if (!hdr) 3589 goto out; 3590 3591 mutex_lock(&xfrm_cfg_mutex); 3592 err = pfkey_process(sk, skb, hdr); 3593 mutex_unlock(&xfrm_cfg_mutex); 3594 3595 out: 3596 if (err && hdr && pfkey_error(hdr, err, sk) == 0) 3597 err = 0; 3598 kfree_skb(skb); 3599 3600 return err ? : len; 3601 } 3602 3603 static int pfkey_recvmsg(struct kiocb *kiocb, 3604 struct socket *sock, struct msghdr *msg, size_t len, 3605 int flags) 3606 { 3607 struct sock *sk = sock->sk; 3608 struct pfkey_sock *pfk = pfkey_sk(sk); 3609 struct sk_buff *skb; 3610 int copied, err; 3611 3612 err = -EINVAL; 3613 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) 3614 goto out; 3615 3616 msg->msg_namelen = 0; 3617 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); 3618 if (skb == NULL) 3619 goto out; 3620 3621 copied = skb->len; 3622 if (copied > len) { 3623 msg->msg_flags |= MSG_TRUNC; 3624 copied = len; 3625 } 3626 3627 skb_reset_transport_header(skb); 3628 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 3629 if (err) 3630 goto out_free; 3631 3632 sock_recv_ts_and_drops(msg, sk, skb); 3633 3634 err = (flags & MSG_TRUNC) ? skb->len : copied; 3635 3636 if (pfk->dump.dump != NULL && 3637 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) 3638 pfkey_do_dump(pfk); 3639 3640 out_free: 3641 skb_free_datagram(sk, skb); 3642 out: 3643 return err; 3644 } 3645 3646 static const struct proto_ops pfkey_ops = { 3647 .family = PF_KEY, 3648 .owner = THIS_MODULE, 3649 /* Operations that make no sense on pfkey sockets. */ 3650 .bind = sock_no_bind, 3651 .connect = sock_no_connect, 3652 .socketpair = sock_no_socketpair, 3653 .accept = sock_no_accept, 3654 .getname = sock_no_getname, 3655 .ioctl = sock_no_ioctl, 3656 .listen = sock_no_listen, 3657 .shutdown = sock_no_shutdown, 3658 .setsockopt = sock_no_setsockopt, 3659 .getsockopt = sock_no_getsockopt, 3660 .mmap = sock_no_mmap, 3661 .sendpage = sock_no_sendpage, 3662 3663 /* Now the operations that really occur. */ 3664 .release = pfkey_release, 3665 .poll = datagram_poll, 3666 .sendmsg = pfkey_sendmsg, 3667 .recvmsg = pfkey_recvmsg, 3668 }; 3669 3670 static const struct net_proto_family pfkey_family_ops = { 3671 .family = PF_KEY, 3672 .create = pfkey_create, 3673 .owner = THIS_MODULE, 3674 }; 3675 3676 #ifdef CONFIG_PROC_FS 3677 static int pfkey_seq_show(struct seq_file *f, void *v) 3678 { 3679 struct sock *s = sk_entry(v); 3680 3681 if (v == SEQ_START_TOKEN) 3682 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n"); 3683 else 3684 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n", 3685 s, 3686 atomic_read(&s->sk_refcnt), 3687 sk_rmem_alloc_get(s), 3688 sk_wmem_alloc_get(s), 3689 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)), 3690 sock_i_ino(s) 3691 ); 3692 return 0; 3693 } 3694 3695 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos) 3696 __acquires(rcu) 3697 { 3698 struct net *net = seq_file_net(f); 3699 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 3700 3701 rcu_read_lock(); 3702 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos); 3703 } 3704 3705 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos) 3706 { 3707 struct net *net = seq_file_net(f); 3708 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 3709 3710 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos); 3711 } 3712 3713 static void pfkey_seq_stop(struct seq_file *f, void *v) 3714 __releases(rcu) 3715 { 3716 rcu_read_unlock(); 3717 } 3718 3719 static const struct seq_operations pfkey_seq_ops = { 3720 .start = pfkey_seq_start, 3721 .next = pfkey_seq_next, 3722 .stop = pfkey_seq_stop, 3723 .show = pfkey_seq_show, 3724 }; 3725 3726 static int pfkey_seq_open(struct inode *inode, struct file *file) 3727 { 3728 return seq_open_net(inode, file, &pfkey_seq_ops, 3729 sizeof(struct seq_net_private)); 3730 } 3731 3732 static const struct file_operations pfkey_proc_ops = { 3733 .open = pfkey_seq_open, 3734 .read = seq_read, 3735 .llseek = seq_lseek, 3736 .release = seq_release_net, 3737 }; 3738 3739 static int __net_init pfkey_init_proc(struct net *net) 3740 { 3741 struct proc_dir_entry *e; 3742 3743 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops); 3744 if (e == NULL) 3745 return -ENOMEM; 3746 3747 return 0; 3748 } 3749 3750 static void __net_exit pfkey_exit_proc(struct net *net) 3751 { 3752 remove_proc_entry("pfkey", net->proc_net); 3753 } 3754 #else 3755 static inline int pfkey_init_proc(struct net *net) 3756 { 3757 return 0; 3758 } 3759 3760 static inline void pfkey_exit_proc(struct net *net) 3761 { 3762 } 3763 #endif 3764 3765 static struct xfrm_mgr pfkeyv2_mgr = 3766 { 3767 .id = "pfkeyv2", 3768 .notify = pfkey_send_notify, 3769 .acquire = pfkey_send_acquire, 3770 .compile_policy = pfkey_compile_policy, 3771 .new_mapping = pfkey_send_new_mapping, 3772 .notify_policy = pfkey_send_policy_notify, 3773 .migrate = pfkey_send_migrate, 3774 }; 3775 3776 static int __net_init pfkey_net_init(struct net *net) 3777 { 3778 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 3779 int rv; 3780 3781 INIT_HLIST_HEAD(&net_pfkey->table); 3782 atomic_set(&net_pfkey->socks_nr, 0); 3783 3784 rv = pfkey_init_proc(net); 3785 3786 return rv; 3787 } 3788 3789 static void __net_exit pfkey_net_exit(struct net *net) 3790 { 3791 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); 3792 3793 pfkey_exit_proc(net); 3794 BUG_ON(!hlist_empty(&net_pfkey->table)); 3795 } 3796 3797 static struct pernet_operations pfkey_net_ops = { 3798 .init = pfkey_net_init, 3799 .exit = pfkey_net_exit, 3800 .id = &pfkey_net_id, 3801 .size = sizeof(struct netns_pfkey), 3802 }; 3803 3804 static void __exit ipsec_pfkey_exit(void) 3805 { 3806 xfrm_unregister_km(&pfkeyv2_mgr); 3807 sock_unregister(PF_KEY); 3808 unregister_pernet_subsys(&pfkey_net_ops); 3809 proto_unregister(&key_proto); 3810 } 3811 3812 static int __init ipsec_pfkey_init(void) 3813 { 3814 int err = proto_register(&key_proto, 0); 3815 3816 if (err != 0) 3817 goto out; 3818 3819 err = register_pernet_subsys(&pfkey_net_ops); 3820 if (err != 0) 3821 goto out_unregister_key_proto; 3822 err = sock_register(&pfkey_family_ops); 3823 if (err != 0) 3824 goto out_unregister_pernet; 3825 err = xfrm_register_km(&pfkeyv2_mgr); 3826 if (err != 0) 3827 goto out_sock_unregister; 3828 out: 3829 return err; 3830 3831 out_sock_unregister: 3832 sock_unregister(PF_KEY); 3833 out_unregister_pernet: 3834 unregister_pernet_subsys(&pfkey_net_ops); 3835 out_unregister_key_proto: 3836 proto_unregister(&key_proto); 3837 goto out; 3838 } 3839 3840 module_init(ipsec_pfkey_init); 3841 module_exit(ipsec_pfkey_exit); 3842 MODULE_LICENSE("GPL"); 3843 MODULE_ALIAS_NETPROTO(PF_KEY); 3844