]> rtime.felk.cvut.cz Git - can-eth-gw.git/blobdiff - kernel/canethgw.c
improved rtnl processing, some error checks added
[can-eth-gw.git] / kernel / canethgw.c
index 992ded96ff50ba235af17fa47c7a03537026e4ca..28f8c2a1fee82da86baa3d3ef9a7b4891238afa6 100644 (file)
@@ -1,3 +1,5 @@
+#define DEBUG 1
+
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
 #include <linux/can/core.h>
 #include <linux/can.h>
 #include <net/rtnetlink.h>
+#include <net/sock.h>
 #include "canethgw.h"
+#include <linux/completion.h>
+#include <linux/mutex.h>
+#include <net/inet_common.h>
 
 /**
- * ToDo
- *  [ ] check every input
- *  [ ] refactor
+ * ToDo:
+ * [ ] encapsule module - check inputs
+ * [ ] refactor - chc .h
+ * [ ] dump callback
+ * [ ] rtnl vs nl functions
+ * [ ] stop threads
+ * [ ] change listening
+ * [ ] clean exit - threads, jobs
  */
 
 MODULE_LICENSE( "GPL" );
 
-static int  gw_udp_recv( void* data );
-static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port );
-static int  gw_can_recv( void* data );
-static void gw_can_send( struct can_frame* cf, int ifidx );
-static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port );
+static int  cegw_udp_can( void* data );
+inline static void cegw_udp_send( struct socket* udp_sock, struct can_frame* cf, struct in_addr ipaddr, u16 port );
+static int  cegw_can_udp( void* data );
+inline static void cegw_can_send( struct socket* can_sock, struct can_frame* cf, int ifindex );
+static int cegw_thread_start( void );
+static void cegw_thread_stop( void );
+static int  cegw_thread_restart( void* arg );
 
 #define CEGW_STOPPED 0
 #define CEGW_RUNNING 1
 
-static struct task_struct* eth_to_can, * can_to_eth;
-static struct socket* udp_sock = NULL;
-static struct socket* can_sock = NULL;
-static int gw_state = CEGW_STOPPED;
+static struct socket* can_sock = NULL, * udp_sock = NULL;
+static struct task_struct* eth_to_can = NULL, * can_to_eth = NULL;
+/* ToDo: protect with mutex */
+static int cegw_state = CEGW_STOPPED;
 
-struct can_eth_gw
+struct cegw_rule
 {
-       int src_if_idx;
-       struct in_addr dst_addr;
-       unsigned short dst_port;
+       int can_ifindex;
+       struct in_addr eth_ip;
+       unsigned short eth_port;
        struct hlist_node list;
 };
 
-struct eth_can_gw
+HLIST_HEAD( cegw_rule_can_eth );
+HLIST_HEAD( cegw_rule_eth_can );
+
+struct 
 {
-       int dst_if_idx;
-       struct hlist_node list;
-};
+       int can_ifindex;
+       struct in_addr eth_addr;
+       unsigned short eth_port;
+} cegw_setting;
 
-HLIST_HEAD( can_eth_job );
-HLIST_HEAD( eth_can_job );
+DEFINE_MUTEX( cegw_setting_mutex );
 
-struct cegw_setting
+inline static void cegw_udp_send( struct socket* udp_sock, struct can_frame* cf, struct in_addr ipaddr, u16 port )
 {
-       struct can_filter filter;
-       int src_idx;
-       /* bind on if */
-       struct in_addr dst_addr;
-       unsigned short dst_port;
-};
+       struct msghdr mh;
+       struct sockaddr_in addr;
+       struct kvec vec;
+       
+       addr.sin_family = AF_INET;
+       addr.sin_port = htons( port );
+       addr.sin_addr = ipaddr;
+       
+       mh.msg_name = &addr;
+       mh.msg_namelen = sizeof( addr );
+       mh.msg_control = NULL;
+       mh.msg_controllen = 0;
+       mh.msg_flags = 0;
+       
+       vec.iov_base = cf;
+       vec.iov_len = sizeof( *cf );
+       
+       kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) );
+}
 
-/***********************
- *   UDP
- ***********************/
+inline static void cegw_can_send( struct socket* can_sock, struct can_frame* cf, int ifindex )
+{
+       struct msghdr mh;
+       struct kvec vec;
+       struct sockaddr_can addr;
+       
+       addr.can_family = AF_CAN;
+       addr.can_ifindex = ifindex;
+       
+       mh.msg_name = &addr;
+       mh.msg_namelen = sizeof( addr );
+       mh.msg_control = NULL;
+       mh.msg_controllen = 0;
+       mh.msg_flags = 0;
+       
+       vec.iov_base = cf;
+       vec.iov_len = sizeof( *cf );
+       
+       kernel_sendmsg( can_sock, &mh, &vec, 1, sizeof( *cf ) ); /* ToDo: handle error */
+}
 
-static int gw_udp_recv( void* data )
+/** 
+ * cegw_udp_can - performs udp->can routing
+ * This function is run as a thread.
+ */
+static int cegw_udp_can( void* data )
 {
        struct can_frame cf;
        struct kvec vec;
        struct msghdr mh;
-       struct eth_can_gw* job;
+       struct cegw_rule* job;
        struct hlist_node* pos;
        int can_ifidx;
-
-       vec.iov_base = &cf;
-       vec.iov_len = sizeof(cf);
+       int recv_size;
 
        mh.msg_name = NULL;
        mh.msg_namelen = 0;
@@ -86,226 +134,412 @@ static int gw_udp_recv( void* data )
 
        while( 1 )
        {
-               if( kthread_should_stop() ) /* up() ?, recv is blocking */
+               if( cegw_state == CEGW_STOPPED )
                        break;
-               kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */
-               printk( "received udp msg_id:%d\n", cf.can_id );
-               hlist_for_each_entry_rcu( job, pos, &eth_can_job, list )
+               vec.iov_base = &cf;
+               vec.iov_len = sizeof(cf);
+               recv_size = kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* ToDo: handle error, size check */
+               if( recv_size == 0 )
+               {
+                       continue;
+               }
+               hlist_for_each_entry_rcu( job, pos, &cegw_rule_eth_can, list )
                {
                        rcu_read_lock(); /**/
-                       can_ifidx = job->dst_if_idx;
+                       can_ifidx = job->can_ifindex;
                        rcu_read_unlock();
-                       /* ToDo from filter */
-                       gw_can_send( &cf, can_ifidx );
+                       /* ToDo: from filter */
+                       cegw_can_send( can_sock, &cf, can_ifidx );
                }
        }
 
        return 0;
 }
 
-inline static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port )
-{
-       struct msghdr mh;
-       struct sockaddr_in addr;
-       struct kvec vec;
-       
-       addr.sin_family = AF_INET;
-       addr.sin_port = htons( port );
-       addr.sin_addr = ipaddr;
-       
-       mh.msg_name = &addr;
-       mh.msg_namelen = sizeof( addr );
-       mh.msg_control = NULL;
-       mh.msg_controllen = 0;
-       mh.msg_flags = 0;
-       
-       vec.iov_base = cf;
-       vec.iov_len = sizeof( *cf );
-       
-       kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) );
-}
-
-/***********************
- *   CAN
- ***********************/
-
-static int gw_can_recv( void* data )
+/**
+ * cegw_can_udp - performs can->udp routing
+ */
+static int cegw_can_udp( void* data )
 {
        struct msghdr mh;
        struct kvec vec;
        struct can_frame cf;
        struct sockaddr_can ca;
-       struct can_eth_gw* job;
+       struct cegw_rule* rule;
        struct hlist_node* pos;
        struct in_addr eth_addr;
        u16 eth_port;
-       
+       int recv_size;
+
        mh.msg_name = &ca;
        mh.msg_namelen = sizeof( ca );
        mh.msg_control = NULL;
        mh.msg_controllen = 0;
        mh.msg_flags = 0;
-       
-       vec.iov_base = &cf;
-       vec.iov_len = sizeof( cf );
 
        while( 1 )
        {
-               if( kthread_should_stop() ) /**/
+               if( cegw_state == CEGW_STOPPED ) /**/
                        break;
-               kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
-               printk( "received can msg_id:%d, from:%d\n", cf.can_id, ca.can_ifindex );
-               hlist_for_each_entry_rcu( job, pos, &can_eth_job, list )
+               vec.iov_base = &cf;
+               vec.iov_len = sizeof( cf );
+
+               recv_size = kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
+               if( recv_size == 0 )
+               {
+                       continue;
+               }
+               hlist_for_each_entry_rcu( rule, pos, &cegw_rule_can_eth, list )
                {
                        rcu_read_lock();
-                       eth_addr = job->dst_addr;
-                       eth_port = job->dst_port;
+                       eth_addr = rule->eth_ip;
+                       eth_port = rule->eth_port;
                        rcu_read_unlock();
-                       printk( KERN_INFO "%x\n", eth_addr );
-                       if( job->src_if_idx == ca.can_ifindex )
-                               gw_udp_send( &cf, eth_addr, eth_port );
+                       if( rule->can_ifindex == ca.can_ifindex )
+                               cegw_udp_send( udp_sock, &cf, eth_addr, eth_port );
                }
        }
        
        return 0;
 }
 
-inline static void gw_can_send( struct can_frame* cf, int ifidx )
-{
-       struct msghdr mh;
-       struct kvec vec;
-       struct sockaddr_can ca =
-       {
-               .can_family = AF_CAN,
-               .can_ifindex = ifidx
-       };
-       
-       mh.msg_name = &ca;
-       mh.msg_namelen = sizeof( ca );
-       mh.msg_control = NULL;
-       mh.msg_controllen = 0;
-       mh.msg_flags = 0;
-       
-       vec.iov_base = cf;
-       vec.iov_len = sizeof( *cf );
-       
-       kernel_sendmsg( can_sock, &mh, &vec, 1, sizeof( *cf ) );
-}
-
 /* NetLink */
 
-static int cgw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh,
-                     void* arg )
+static int cegw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
 {
-       struct nlattr* tb[ CGW_MAX+1 ];
-       struct rtcanmsg *r;
-       struct can_eth_gw* cethgw = NULL;
-       struct eth_can_gw* ecangw = NULL;
+       struct nlattr* tb[ CEGW_MAX+1 ];
+       struct cegw_rule* rule = NULL;
+       int ifindex;
+       struct rtmsg* r;
+       struct in_addr ip;
+       unsigned short port;
        int err = 0;
        
-       if (nlmsg_len(nlh) < sizeof(*r))
+       if( nlmsg_len(nlh) < sizeof(*r) )
                return -EINVAL;
 
-       r = nlmsg_data(nlh);
-       if (r->can_family != AF_CAN)
+       r = nlmsg_data( nlh );
+
+       if( r->rtm_family != AF_CAN )
                return -EPFNOSUPPORT;
-       err = nlmsg_parse( nlh, sizeof( struct rtcanmsg ), tb, CGW_MAX, NULL );
+
+       err = nlmsg_parse( nlh, sizeof(*r), tb, CEGW_MAX, NULL );
        if( err < 0 )
        {
-               printk( KERN_ERR "error: nlmsg_parse\n" );
+               pr_devel( "canethgw: nlmsg_parse error\n" );
                return err;
        }
 
-       switch( r->gwtype )
+       if( tb[CEGW_CMD_INFO] == NULL )
+       {
+               pr_devel( "canethgw: CEGW_CMD_INFO is missing in rtmsg\n" );
+               return -EINVAL;
+       }
+
+       switch( *(int*)nla_data( tb[CEGW_CMD_INFO] ) )
        {
-               case CGW_TYPE_CONFIG:
-                       listen( 0,  *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ), 
-                                   *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) );
+               case CEGW_LISTEN:
+                       if( !tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] )
+                       {
+                               pr_devel( "canethgw: missing attribute for CEGW_LISTEN\n" );
+                               return -EINVAL;
+                       }
+
+                       mutex_lock( &cegw_setting_mutex );
+                       cegw_setting.eth_addr = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
+                       cegw_setting.eth_port = *(unsigned short*)nla_data( tb[CEGW_ETH_PORT] );
+                       kthread_run( cegw_thread_restart, NULL, "canethgw" );
+                       mutex_unlock( &cegw_setting_mutex );
                        break;
-               case CGW_TYPE_CAN_ETH_UDP:
-                       printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
-                       printk( KERN_INFO "eth addr:%x\n", *(u32*)nla_data( tb[CGW_ETH_IP] ) );
-                       printk( KERN_INFO "eth port:%hu\n", *(u16*)nla_data( tb[CGW_ETH_PORT] ) );
-                       cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
-                       if( cethgw == NULL )
+               case CEGW_RULE_CAN_ETH:
+                       if( !tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] || !tb[CEGW_CAN_IFINDEX] )
+                       {
+                               pr_devel( "canethgw: missing attribute for CEGW_RULE_CAN_ETH\n" );
+                               return -EINVAL;
+                       }
+
+                       ifindex = *(int*)nla_data( tb[CEGW_CAN_IFINDEX] );
+                       ip = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
+                       port = *(unsigned short*)nla_data( tb[CEGW_ETH_PORT] );
+                       pr_devel( "canethgw: new can->eth rule - (%d)->(%x:%hu)\n", ifindex, ip.s_addr, port );
+
+                       rule = kmalloc( sizeof(struct cegw_rule), GFP_KERNEL );
+                       if( rule == NULL )
                        {
-                               printk( KERN_ERR "error: kmalloc\n" );
                                break;
                        }
-                       cethgw->src_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
-                       cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CGW_ETH_IP] );
-                       cethgw->dst_port = *(u16*)nla_data( tb[CGW_ETH_PORT] );
                        
-                       hlist_add_head_rcu( &cethgw->list, &can_eth_job );
+                       rule->can_ifindex = ifindex;
+                       rule->eth_ip = ip;
+                       rule->eth_port = port;
+                       
+                       hlist_add_head_rcu( &rule->list, &cegw_rule_can_eth );
                        break;
-               case CGW_TYPE_ETH_CAN_UDP:
-                       printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
-                       ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
-                       if( ecangw == NULL )
+               case CEGW_RULE_ETH_CAN:
+                       if( !tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] || !tb[CEGW_CAN_IFINDEX] )
+                       {
+                               pr_devel( "canethgw: missing attribute for CEGW_RULE_ETH_CAN\n" );
+                               return -EINVAL;
+                       }
+
+                       ifindex = *(int*)nla_data( tb[CEGW_CAN_IFINDEX] );
+                       ip = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
+                       port = *(unsigned short*)nla_data( tb[CEGW_ETH_PORT] );
+                       pr_devel( "canethgw: new eth->can rule - (%x:%hu)->(%d)\n", ip.s_addr, port, ifindex );
+
+                       rule = kmalloc( sizeof(struct cegw_rule), GFP_KERNEL );
+                       if( rule == NULL )
                        {
-                               printk( KERN_ERR "error: kmalloc\n" );
                                break;
                        }
-                       ecangw->dst_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
-                       hlist_add_head_rcu( &ecangw->list, &eth_can_job );
+
+                       rule->can_ifindex = ifindex;
+                       rule->eth_ip = ip;
+                       rule->eth_port = port;
+
+                       hlist_add_head_rcu( &rule->list, &cegw_rule_eth_can );
                        break;
                default:
-                       printk( "default" );
-                       /* ToDo undef operation */
+                       pr_devel( "canethgw: unknown CEGW_CMD_INFO\n" );
                        break;
        }
 
        return 0;
 }
 
-static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
+static int cegw_remove_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
+{
+       struct rtmsg* r;
+       struct nlattr* tb[ CEGW_MAX+1 ];
+       struct hlist_node* pos,* n;
+       struct cegw_rule* rule;
+
+       int err = 0;
+
+       if( nlmsg_len(nlh) < sizeof(*r) )
+               return -EINVAL;
+       
+       r = nlmsg_data( nlh );
+
+       if( r->rtm_family != AF_CAN )
+               return -EPFNOSUPPORT;
+
+       err = nlmsg_parse( nlh, sizeof(struct rtmsg), tb, CEGW_MAX, NULL );
+       if( err != 0 )
+               return -EINVAL;
+
+       if( tb[CEGW_CMD_INFO] == NULL )
+       {
+               pr_devel( "canethgw: CEGW_CMD_INFO is missing in rtmsg\n" );
+               return -EINVAL;
+       }
+       
+       if( *(int*)nla_data( tb[CEGW_CMD_INFO] ) != CEGW_FLUSH )
+       {
+               return -EINVAL;
+       }
+
+       hlist_for_each_entry_safe( rule, pos, n, &cegw_rule_can_eth, list )
+       {
+               hlist_del( &rule->list );
+               kfree( rule );
+       }
+       hlist_for_each_entry_safe( rule, pos, n, &cegw_rule_eth_can, list )
+       {
+               hlist_del( &rule->list );
+               kfree( rule );
+       }
+       
+       return 0;
+}
+
+static int cegw_put_rule( struct sk_buff* skb, int type, struct cegw_rule* rule )
+{
+       int ifindex;
+       struct in_addr ip;
+       unsigned short port;
+       struct nlmsghdr* nlh;
+
+       ifindex = rule->can_ifindex;
+       ip = rule->eth_ip;
+       port = rule->eth_port;
+
+       nlh = nlmsg_put( skb, 0, 0, 0, 0, 0 );
+       if( nlh == NULL )
+               return -EMSGSIZE;
+
+       /* type */
+       if( nla_put( skb, CEGW_TYPE, sizeof(type), &type ) < 0 )
+               goto cancel;
+       else
+               nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(type) );
+
+       /* can ifindex */
+       if( nla_put( skb, CEGW_CAN_IFINDEX, sizeof(ifindex), &ifindex ) < 0 )
+               goto cancel;
+       else
+               nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ifindex) );
+
+       /* ip adress */
+       if( nla_put( skb, CEGW_ETH_IP, sizeof(ip), &ip) < 0 )
+               goto cancel;
+       else
+               nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ip) );
+
+       /* port */
+       if( nla_put( skb, CEGW_ETH_PORT, sizeof(port), &port ) < 0 )
+               goto cancel;
+       else
+               nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(port) );
+
+       return skb->len;
+
+cancel:
+       nlmsg_cancel( skb, nlh );
+       return -EMSGSIZE;
+}
+
+static int cegw_dump_job( struct sk_buff* skb, struct netlink_callback* cb )
+{
+       struct cegw_rule* rule;
+       struct hlist_node* pos;
+       int idx = 0;
+       int s_idx = cb->args[0];
+
+       /* ToDo: skb max size */
+
+       rcu_read_lock();
+       hlist_for_each_entry_rcu( rule, pos, &cegw_rule_eth_can, list )
+       {
+               if( idx < s_idx )
+                       goto cont1;
+
+               if( cegw_put_rule( skb, CEGW_RULE_ETH_CAN, rule ) < 0 )
+                       goto brk;
+cont1:
+               idx++;
+       }
+       rcu_read_unlock();
+
+       rcu_read_lock();
+       hlist_for_each_entry_rcu( rule, pos, &cegw_rule_can_eth, list )
+       {
+               if( idx < s_idx )
+                       goto cont2;
+
+               if( cegw_put_rule( skb, CEGW_RULE_CAN_ETH, rule ) < 0 )
+                       goto brk;
+
+cont2:
+               idx++;
+       }
+       rcu_read_unlock();
+
+brk:
+       cb->args[0] = idx;
+
+       return skb->len;
+}
+
+static int cegw_thread_start( void )
 {
        struct sockaddr_in udp_addr;
        struct sockaddr_can can_addr;
-       struct socket* tmp;
 
-       printk( KERN_INFO "listen called\n" );
+       can_addr.can_family = AF_CAN;
+       can_addr.can_ifindex = 0;
+       
+       udp_addr.sin_family = AF_INET;
+       udp_addr.sin_port = htons( cegw_setting.eth_port );
+       udp_addr.sin_addr = cegw_setting.eth_addr;
 
-       if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 )
+       /* create and bind sockets */
+       if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock) != 0 )
        {
-               printk( KERN_ERR "error: can_sock creation failed\n" );
+               printk( KERN_ERR "canethgw: udp socket creation failed\n" );
                return -1;
        }
 
-       can_addr.can_family = AF_CAN;
-       can_addr.can_ifindex = can_ifidx;
-       
-       if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
+       if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
        {
-               printk( KERN_ERR "can_sock bind failed\n" );
+               printk( KERN_ERR "canethgw: can socket creation failed\n" );
                return -1;
        }
-       
-       printk( KERN_INFO "can socket success\n" );
 
-       udp_addr.sin_family = AF_INET;
-       udp_addr.sin_port = htons( eth_port );
-       udp_addr.sin_addr = eth_addr;
+       if( kernel_bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 )
+       {
+               printk( KERN_ERR "canethgw: udp socket binding failed\n" );
+               sock_release( udp_sock );
+               sock_release( can_sock );
+               return -1; /* ToDo: state==RUNNING and return? */
+       }
 
-       printk( KERN_INFO "trying to bind\n" );
-       if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
+       if( kernel_bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
        {
-               printk( "error: binding failed\n" );
+               printk( KERN_ERR "canethgw: can socket binding failed\n" );
                sock_release( udp_sock );
                sock_release( can_sock );
                return -1;
        }
 
-       printk( KERN_INFO "socket established\n" );
+       /* start threads */
+       cegw_state = CEGW_RUNNING;
+       eth_to_can = kthread_create( cegw_udp_can, NULL, "canethgw" );
+       if( !IS_ERR( eth_to_can ) )
+       { 
+               get_task_struct( eth_to_can );
+               wake_up_process( eth_to_can );
+       }
+       can_to_eth = kthread_create( cegw_can_udp, NULL, "canethgw" );
+       if( !IS_ERR( can_to_eth ) )
+       {
+               /* ToDo: free this? */
+               get_task_struct( can_to_eth );
+               wake_up_process( can_to_eth );
+       }
        
-       /* run threads */
-       eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" );
-       can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" );
+       /* ToDo: kthread creation fail */
+       pr_devel( "threads are running\n" );
 
-       printk( KERN_INFO "threads are running\n" );
+       return 0;
+}
 
-       gw_state = CEGW_RUNNING;
+static void cegw_thread_stop( void )
+{
+       int how = SHUT_RDWR;
+       struct sock* sk = NULL;
+
+       /* no threads are running */
+       if( !can_to_eth && !eth_to_can )
+               return;
+       
+       cegw_state = CEGW_STOPPED;
+       /* shut down socket */
+       sk = can_sock->sk;
+       how++;
+       lock_sock( sk );
+       sk->sk_shutdown |= how;
+       sk->sk_state_change( sk );
+       release_sock( sk );
+
+       kernel_sock_shutdown( udp_sock, SHUT_RDWR );
+
+       /* wait for return to reuse port if restart */
+       kthread_stop( eth_to_can );
+       kthread_stop( can_to_eth );
+       sock_release( udp_sock );
+       sock_release( can_sock );
+       can_to_eth = NULL;
+       eth_to_can = NULL;
+}
+
+/**
+ * cegw_thread_restart
+ */
+static int cegw_thread_restart( void* data )
+{
+       cegw_thread_stop();
+       cegw_thread_start();
 
        return 0;
 }
@@ -314,49 +548,29 @@ static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
  *   module init/exit
  ***********************/
 
-static int __init cangw_init( void )
-{      
-       if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
-       {
-               printk( KERN_ERR "error: can_sock creation failed\n" );
-               return -1;
-       }
-
-       if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
-       {
-               printk( KERN_ERR "error: udp_sock creation failed\n" );
-               sock_release( can_sock );
-               return -1;
-       }
-       
+static int __init cegw_init( void )
+{
        /* subscribe to netlink */
-       //if( __rtnl_register( PF_CAN, RTM_GETROUTE,  ) != 0 )
-       __rtnl_register( PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL );
-       //__rtnl_register( PF_CAN, RTM_DELROUTE,  )     
-       
-       /*
-       if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
-       {s
-               printk( "error: can_sock creation failed\n" );
-       }
-       */
-       
+       rtnl_register( PF_CAN, RTM_GETROUTE, NULL, cegw_dump_job, NULL );
+       rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL );
+       rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL );
+
        return 0;
 }
 
-static void __exit cangw_exit( void )
+static void __exit cegw_exit( void )
 {
-       if( gw_state == CEGW_RUNNING )
-       {
-               sock_release( udp_sock );
-               sock_release( can_sock );
-               /* ToDo: stop threads */
-       }
+       /* ToDo: effect on others? */
+       rtnl_unregister_all( PF_CAN );
+
+       /* ToDo: no rtnl pending? */
+       cegw_thread_stop();
+       /* ToDo: frees mem_cache?    */
+       /*       udp must not exists */
 
        printk( "cangw: exit\n" );
-       //kthread_stop( ts );
 }
 
-module_init( cangw_init );
-module_exit( cangw_exit );
+module_init( cegw_init );
+module_exit( cegw_exit );