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