Changeset 4646
- Timestamp:
- 11/13/08 08:15:45 (2 months ago)
- Files:
-
- trunk/src/charon/network/socket-raw.c (modified) (14 diffs)
- trunk/src/charon/network/socket.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/charon/network/socket-raw.c
r3870 r4646 1 1 /* 2 2 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger 3 * Copyright (C) 2005-200 6Martin Willi3 * Copyright (C) 2005-2008 Martin Willi 4 4 * Copyright (C) 2005 Jan Hutter 5 5 * Hochschule fuer Technik Rapperswil … … 34 34 #include <netinet/ip6.h> 35 35 #include <netinet/udp.h> 36 #include <linux/ ipsec.h>36 #include <linux/types.h> 37 37 #include <linux/filter.h> 38 38 #include <net/if.h> … … 54 54 #define IKE_LENGTH_OFFSET 24 55 55 56 /* from linux/in.h */57 #ifndef IP_IPSEC_POLICY58 #define IP_IPSEC_POLICY 1659 #endif /*IP_IPSEC_POLICY*/60 61 56 /* from linux/udp.h */ 62 57 #ifndef UDP_ENCAP … … 72 67 #define IPV6_2292PKTINFO 2 73 68 #endif /*IPV6_2292PKTINFO*/ 74 75 /* missing on uclibc */76 #ifndef IPV6_IPSEC_POLICY77 #define IPV6_IPSEC_POLICY 3478 #endif /*IPV6_IPSEC_POLICY*/79 69 80 70 typedef struct private_socket_t private_socket_t; … … 441 431 int type = UDP_ENCAP_ESPINUDP; 442 432 struct sockaddr_storage addr; 443 u_int sol, ipsec_policy; 444 struct sadb_x_policy policy; 433 u_int sol; 445 434 int skt; 446 435 … … 456 445 sin->sin_port = htons(port); 457 446 sol = SOL_IP; 458 ipsec_policy = IP_IPSEC_POLICY;459 447 break; 460 448 } … … 466 454 sin6->sin6_port = htons(port); 467 455 sol = SOL_IPV6; 468 ipsec_policy = IPV6_IPSEC_POLICY;469 456 break; 470 457 } … … 488 475 } 489 476 490 /* bypass outgoung IKE traffic on send socket */491 memset(&policy, 0, sizeof(policy));492 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);493 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;494 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;495 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;496 497 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)498 {499 DBG1(DBG_NET, "unable to set IPSEC_POLICY on send socket: %s",500 strerror(errno));501 close(skt);502 return 0;503 }504 505 /* We don't receive packets on the send socket, but we need a INBOUND policy.506 * Otherwise, UDP decapsulation does not work!!! */507 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;508 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)509 {510 DBG1(DBG_NET, "unable to set IPSEC_POLICY on send socket: %s",511 strerror(errno));512 close(skt);513 return 0;514 }515 516 477 /* bind the send socket */ 517 478 if (bind(skt, (struct sockaddr *)&addr, sizeof(addr)) < 0) … … 543 504 int skt; 544 505 int on = TRUE; 545 u_int proto_offset, ip_len, sol, ipsec_policy, udp_header, ike_header; 546 struct sadb_x_policy policy; 506 u_int proto_offset, ip_len, sol, udp_header, ike_header; 547 507 548 508 /* precalculate constants depending on address family */ … … 553 513 ip_len = IP_LEN; 554 514 sol = SOL_IP; 555 ipsec_policy = IP_IPSEC_POLICY;556 515 break; 557 516 case AF_INET6: … … 559 518 ip_len = 0; /* IPv6 raw sockets contain no IP header */ 560 519 sol = SOL_IPV6; 561 ipsec_policy = IPV6_IPSEC_POLICY;562 520 break; 563 521 default: … … 634 592 } 635 593 636 /* bypass incomining IKE traffic on this socket */637 memset(&policy, 0, sizeof(policy));638 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);639 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;640 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;641 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;642 643 if (setsockopt(skt, sol, ipsec_policy, &policy, sizeof(policy)) < 0)644 {645 DBG1(DBG_NET, "unable to set IPSEC_POLICY on raw socket: %s",646 strerror(errno));647 close(skt);648 return 0;649 }650 651 594 return skt; 595 } 596 597 /** 598 * enumerator for underlying sockets 599 */ 600 typedef struct { 601 /** implements enumerator_t */ 602 enumerator_t public; 603 /** sockets we enumerate */ 604 private_socket_t *socket; 605 /** counter */ 606 int index; 607 } socket_enumerator_t; 608 609 /** 610 * enumerate function for socket_enumerator_t 611 */ 612 static bool enumerate(socket_enumerator_t *this, int *fd, int *family, int *port) 613 { 614 static const struct { 615 int fd_offset; 616 int family; 617 int port; 618 } sockets[] = { 619 { offsetof(private_socket_t, recv4), AF_INET, IKEV2_UDP_PORT }, 620 { offsetof(private_socket_t, recv6), AF_INET6, IKEV2_UDP_PORT }, 621 { offsetof(private_socket_t, send4), AF_INET, IKEV2_UDP_PORT }, 622 { offsetof(private_socket_t, send6), AF_INET6, IKEV2_UDP_PORT }, 623 { offsetof(private_socket_t, send4_natt), AF_INET, IKEV2_NATT_PORT }, 624 { offsetof(private_socket_t, send6_natt), AF_INET6, IKEV2_NATT_PORT } 625 }; 626 627 while(++this->index < countof(sockets)) 628 { 629 int sock = *(int*)((char*)this->socket + sockets[this->index].fd_offset); 630 if (!sock) 631 { 632 continue; 633 } 634 *fd = sock; 635 *family = sockets[this->index].family; 636 *port = sockets[this->index].port; 637 return TRUE; 638 } 639 return FALSE; 640 } 641 642 /** 643 * implementation of socket_t.create_enumerator 644 */ 645 static enumerator_t *create_enumerator(private_socket_t *this) 646 { 647 socket_enumerator_t *enumerator; 648 649 enumerator = malloc_thing(socket_enumerator_t); 650 enumerator->index = -1; 651 enumerator->socket = this; 652 enumerator->public.enumerate = (void*)enumerate; 653 enumerator->public.destroy = (void*)free; 654 return &enumerator->public; 652 655 } 653 656 … … 689 692 socket_t *socket_create() 690 693 { 691 int key;692 694 private_socket_t *this = malloc_thing(private_socket_t); 693 695 694 696 /* public functions */ 695 697 this->public.send = (status_t(*)(socket_t*, packet_t*))sender; 696 698 this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver; 699 this->public.create_enumerator = (enumerator_t*(*)(socket_t*))create_enumerator; 697 700 this->public.destroy = (void(*)(socket_t*)) destroy; 698 701 … … 704 707 this->send6_natt = 0; 705 708 706 /* we open a AF_KEY socket to autoload the af_key module. Otherwise707 * setsockopt(IPSEC_POLICY) won't work. */708 key = socket(AF_KEY, SOCK_RAW, PF_KEY_V2);709 if (key == 0)710 {711 charon->kill(charon, "could not open AF_KEY socket");712 }713 close(key);714 715 709 this->recv4 = open_recv_socket(this, AF_INET); 716 710 if (this->recv4 == 0) trunk/src/charon/network/socket.c
r4618 r4646 96 96 int ipv6_natt; 97 97 }; 98 99 /**100 * enumerator for underlying sockets101 */102 typedef struct {103 /** implements enumerator_t */104 enumerator_t public;105 /** sockets we enumerate */106 private_socket_t *socket;107 /** counter */108 u_int8_t index;109 } socket_enumerator_t;110 98 111 99 /** … … 484 472 485 473 /** 474 * enumerator for underlying sockets 475 */ 476 typedef struct { 477 /** implements enumerator_t */ 478 enumerator_t public; 479 /** sockets we enumerate */ 480 private_socket_t *socket; 481 /** counter */ 482 int index; 483 } socket_enumerator_t; 484 485 /** 486 486 * enumerate function for socket_enumerator_t 487 487 */ … … 493 493 int port; 494 494 } sockets[] = { 495 { 0, 0, 0 },496 495 { offsetof(private_socket_t, ipv4), AF_INET, IKEV2_UDP_PORT }, 497 496 { offsetof(private_socket_t, ipv6), AF_INET6, IKEV2_UDP_PORT }, … … 500 499 }; 501 500 502 while(++this->index < = 4)501 while(++this->index < countof(sockets)) 503 502 { 504 503 int sock = *(int*)((char*)this->socket + sockets[this->index].fd_offset); … … 523 522 524 523 enumerator = malloc_thing(socket_enumerator_t); 525 enumerator->index = 0;524 enumerator->index = -1; 526 525 enumerator->socket = this; 527 526 enumerator->public.enumerate = (void*)enumerate;
