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