Changeset 4646

Show
Ignore:
Timestamp:
11/13/08 08:15:45 (2 months ago)
Author:
martin
Message:

ported socket enumerator to raw-socket.c
some cleanups in socket.c

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/charon/network/socket-raw.c

    r3870 r4646  
    11/* 
    22 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger 
    3  * Copyright (C) 2005-2006 Martin Willi 
     3 * Copyright (C) 2005-2008 Martin Willi 
    44 * Copyright (C) 2005 Jan Hutter 
    55 * Hochschule fuer Technik Rapperswil 
     
    3434#include <netinet/ip6.h> 
    3535#include <netinet/udp.h> 
    36 #include <linux/ipsec.h> 
     36#include <linux/types.h> 
    3737#include <linux/filter.h> 
    3838#include <net/if.h> 
     
    5454#define IKE_LENGTH_OFFSET 24 
    5555 
    56 /* from linux/in.h */ 
    57 #ifndef IP_IPSEC_POLICY 
    58 #define IP_IPSEC_POLICY 16 
    59 #endif /*IP_IPSEC_POLICY*/ 
    60  
    6156/* from linux/udp.h */ 
    6257#ifndef UDP_ENCAP 
     
    7267#define IPV6_2292PKTINFO 2 
    7368#endif /*IPV6_2292PKTINFO*/ 
    74  
    75 /* missing on uclibc */ 
    76 #ifndef IPV6_IPSEC_POLICY 
    77 #define IPV6_IPSEC_POLICY 34 
    78 #endif /*IPV6_IPSEC_POLICY*/ 
    7969 
    8070typedef struct private_socket_t private_socket_t; 
     
    441431    int type = UDP_ENCAP_ESPINUDP; 
    442432    struct sockaddr_storage addr; 
    443     u_int sol, ipsec_policy; 
    444     struct sadb_x_policy policy; 
     433    u_int sol; 
    445434    int skt; 
    446435     
     
    456445            sin->sin_port = htons(port); 
    457446            sol = SOL_IP; 
    458             ipsec_policy = IP_IPSEC_POLICY; 
    459447            break; 
    460448        } 
     
    466454            sin6->sin6_port = htons(port); 
    467455            sol = SOL_IPV6; 
    468             ipsec_policy = IPV6_IPSEC_POLICY; 
    469456            break; 
    470457        } 
     
    488475    } 
    489476     
    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      
    516477    /* bind the send socket */ 
    517478    if (bind(skt, (struct sockaddr *)&addr, sizeof(addr)) < 0) 
     
    543504    int skt; 
    544505    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; 
    547507     
    548508    /* precalculate constants depending on address family */ 
     
    553513            ip_len = IP_LEN; 
    554514            sol = SOL_IP; 
    555             ipsec_policy = IP_IPSEC_POLICY; 
    556515            break; 
    557516        case AF_INET6: 
     
    559518            ip_len = 0; /* IPv6 raw sockets contain no IP header */ 
    560519            sol = SOL_IPV6; 
    561             ipsec_policy = IPV6_IPSEC_POLICY; 
    562520            break; 
    563521        default: 
     
    634592    } 
    635593     
    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      
    651594    return skt; 
     595} 
     596 
     597/** 
     598 * enumerator for underlying sockets 
     599 */ 
     600typedef 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 */ 
     612static 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 */ 
     645static 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; 
    652655} 
    653656 
     
    689692socket_t *socket_create() 
    690693{ 
    691     int key; 
    692694    private_socket_t *this = malloc_thing(private_socket_t); 
    693  
     695     
    694696    /* public functions */ 
    695697    this->public.send = (status_t(*)(socket_t*, packet_t*))sender; 
    696698    this->public.receive = (status_t(*)(socket_t*, packet_t**))receiver; 
     699    this->public.create_enumerator = (enumerator_t*(*)(socket_t*))create_enumerator; 
    697700    this->public.destroy = (void(*)(socket_t*)) destroy; 
    698701     
     
    704707    this->send6_natt = 0; 
    705708     
    706     /* we open a AF_KEY socket to autoload the af_key module. Otherwise 
    707      * 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      
    715709    this->recv4 = open_recv_socket(this, AF_INET); 
    716710    if (this->recv4 == 0) 
  • trunk/src/charon/network/socket.c

    r4618 r4646  
    9696     int ipv6_natt; 
    9797}; 
    98  
    99 /** 
    100  * enumerator for underlying sockets 
    101  */ 
    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; 
    11098 
    11199/** 
     
    484472 
    485473/** 
     474 * enumerator for underlying sockets 
     475 */ 
     476typedef 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/** 
    486486 * enumerate function for socket_enumerator_t 
    487487 */ 
     
    493493        int port; 
    494494    } sockets[] = { 
    495         { 0, 0, 0 }, 
    496495        { offsetof(private_socket_t, ipv4), AF_INET, IKEV2_UDP_PORT }, 
    497496        { offsetof(private_socket_t, ipv6), AF_INET6, IKEV2_UDP_PORT }, 
     
    500499    }; 
    501500     
    502     while(++this->index <= 4
     501    while(++this->index < countof(sockets)
    503502    { 
    504503        int sock = *(int*)((char*)this->socket + sockets[this->index].fd_offset); 
     
    523522     
    524523    enumerator = malloc_thing(socket_enumerator_t); 
    525     enumerator->index = 0
     524    enumerator->index = -1
    526525    enumerator->socket = this; 
    527526    enumerator->public.enumerate = (void*)enumerate;