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