]> rtime.felk.cvut.cz Git - can-eth-gw.git/blobdiff - kernel/canethgw.c
authors and license information added
[can-eth-gw.git] / kernel / canethgw.c
index 1b4bb439e7be0e16b72ff34cff4196ff0bdf46f5..685ca6c56ae78ffc911dc6180cb113593a8778ae 100644 (file)
-#define DEBUG 1
+/*
+ * Copyright: (c) 2012 Czech Technical University in Prague
+ *
+ * Authors:
+ *     Radek Matějka <radek.matejka@gmail.com>
+ *     Michal Sojka  <sojkam1@fel.cvut.cz>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
 
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
-#include <linux/sched.h>
-#include <linux/delay.h>
-#include <linux/wait.h>
-#include <linux/netdevice.h>
-#include <linux/socket.h>
-#include <linux/if_arp.h>
-#include <linux/net.h>
-#include <linux/netdevice.h>
-#include <linux/can/core.h>
-#include <linux/can.h>
-#include <net/rtnetlink.h>
+#include <linux/file.h>
 #include <net/sock.h>
+#include <linux/can.h>
+#include <linux/miscdevice.h>
 #include "canethgw.h"
-#include <linux/completion.h>
-#include <linux/mutex.h>
-#include <net/inet_common.h>
-
-MODULE_LICENSE( "GPL" );
-
-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* data );
-static int cegw_thread_stop( void );
-
-enum __cegw_state
-{
-       CEGW_RUN,
-       CEGW_STOP,
-       CEGW_EXIT
-};
-
-struct cegw_rule
-{
-       int can_ifindex;
-       struct in_addr eth_ip;
-       unsigned short eth_port;
-       struct hlist_node list;
-};
-
-struct cegw_setting
-{
-       struct in_addr eth_ip;
-       unsigned short eth_port;
-};
 
-static int cegw_state = CEGW_STOP;
-static struct socket* can_sock = NULL, * udp_sock = NULL;
-static struct task_struct* eth_to_can = NULL, * can_to_eth = NULL;
-static struct notifier_block notifier;
+MODULE_LICENSE("GPL");
 
-HLIST_HEAD( cegw_rule_can_eth );
-HLIST_HEAD( cegw_rule_eth_can );
-DEFINE_MUTEX( cegw_mutex );
+static int cegw_udp2can(void *data);
+static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
+               struct sockaddr_in* addr);
+static int cegw_can2udp(void *data);
+static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
+static int cegw_thread_start(void *data);
+static int cegw_thread_stop(struct cegw_job *job);
+static void cegw_job_release(struct kref *ref);
 
-inline static void cegw_udp_send( struct socket* udp_sock, struct can_frame* cf, struct in_addr ipaddr, u16 port )
+static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
 {
        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 );
+       int err;
+
+       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 ) );
+       vec.iov_len = sizeof(*cf);
+
+       err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
+
+       return err;
 }
 
-inline static void cegw_can_send( struct socket* can_sock, struct can_frame* cf, int ifindex )
+static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
 {
        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 );
+       int err;
+
+       mh.msg_name = NULL;
+       mh.msg_namelen = 0;
        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 ) );
+       vec.iov_len = sizeof(*cf);
+
+       err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
+
+       return err;
 }
 
-/** 
- * cegw_udp_can - performs udp->can routing
+/**
+ * cegw_udp2can - performs udp->can routing
+ *
  * This function is run as a thread.
  */
-static int cegw_udp_can( void* data )
+static int cegw_udp2can(void *data)
 {
        struct can_frame cf;
        struct kvec vec;
        struct msghdr mh;
-       struct cegw_rule* rule;
-       struct hlist_node* pos;
-       int can_ifidx;
-       int recv_size;
+       struct cegw_job *job = (struct cegw_job *)data;
+       struct socket *udp_sock = NULL, *can_sock = NULL;
+       int ret = 0;
 
-       mh.msg_name = NULL;
-       mh.msg_namelen = 0;
-       mh.msg_iov = NULL;
-       mh.msg_iovlen = 0;
-       mh.msg_control = NULL;
-       mh.msg_controllen = 0;
-       mh.msg_flags = 0;
+       memset(&mh, 0, sizeof(mh));
+       udp_sock = job->udp_sock;
+       can_sock = job->can_sock;
 
-       while( 1 )
-       {
-               if( cegw_state == CEGW_STOP )
-                       break;
+       while (1) {
                vec.iov_base = &cf;
                vec.iov_len = sizeof(cf);
-               recv_size = kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 );
-               /* recv_size == 0 when shutting down */
-               if( recv_size != sizeof(cf) || recv_size == 0 )
-               {
-                       continue;
-               } else if( recv_size < 0 )
-               {
-                       return -1;
-               }
+               ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
+                               sizeof(cf), 0);
+               if (ret < 1)
+                       break;
 
-               hlist_for_each_entry_rcu( rule, pos, &cegw_rule_eth_can, list )
-               {
-                       rcu_read_lock();
-                       can_ifidx = rule->can_ifindex;
-                       rcu_read_unlock();
-                       /* ToDo: from filter */
-                       cegw_can_send( can_sock, &cf, can_ifidx );
-               }
+               cf.can_id = be32_to_cpu(cf.can_id);
+               cegw_can_send(can_sock, &cf);
        }
 
-       return 0;
+       cegw_thread_stop(job);
+       kref_put(&job->refcount, cegw_job_release);
+       do_exit(ret);
 }
 
 /**
- * cegw_can_udp - performs can->udp routing
+ * cegw_can2udp - performs can->udp routing
+ *
+ * This function is run as a thread.
  */
-static int cegw_can_udp( void* data )
+static int cegw_can2udp(void* data)
 {
        struct msghdr mh;
        struct kvec vec;
        struct can_frame cf;
-       struct sockaddr_can ca;
-       struct cegw_rule* rule;
-       struct hlist_node* pos;
-       struct in_addr eth_ip;
-       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;
+       struct cegw_job* job = (struct cegw_job*)data;
+       struct socket* udp_sock = job->udp_sock;
+       struct socket* can_sock = job->can_sock;
+       int i;
+       int ret;
 
-       while( 1 )
-       {
-               if( cegw_state == CEGW_STOP )
-                       break;
+       memset(&mh, 0, sizeof(mh));
+
+       while (1) {
                vec.iov_base = &cf;
-               vec.iov_len = sizeof( cf );
-
-               recv_size = kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
-               if( recv_size != sizeof(cf) || recv_size == 0 )
-               {
-                       continue;
-               } else if( recv_size < 0 )
-               {
-                       return -1;
-               }
+               vec.iov_len = sizeof(cf);
+
+               ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
+                                          sizeof(cf), 0);
+               if (ret < 1)
+                       break;
 
-               hlist_for_each_entry_rcu( rule, pos, &cegw_rule_can_eth, list )
-               {
-                       rcu_read_lock();
-                       eth_ip = rule->eth_ip;
-                       eth_port = rule->eth_port;
-                       rcu_read_unlock();
-                       if( rule->can_ifindex == ca.can_ifindex )
-                               cegw_udp_send( udp_sock, &cf, eth_ip, eth_port );
+               cf.can_id = cpu_to_be32(cf.can_id);
+               for (i=0; i<job->udp_dstcnt; i++) {
+                       cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
                }
        }
-       
-       return 0;
+
+       cegw_thread_stop(job);
+       kref_put(&job->refcount, cegw_job_release);
+       do_exit(ret);
 }
 
-static int cegw_newroute( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
+static void cegw_job_release(struct kref *ref)
 {
-       struct nlattr* tb[ CEGW_MAX+1 ];
-       struct cegw_rule* rule = NULL;
-       int ifindex;
-       struct rtmsg* r;
-       struct in_addr ip;
-       unsigned short port;
-       struct cegw_setting* set;
-       int err = 0;
-       
-       if( nlmsg_len(nlh) < sizeof(*r) )
-               return -EINVAL;
+       struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
 
-       r = nlmsg_data( nlh );
+       fput(job->can_sock->file);
+       fput(job->udp_sock->file);
+       kfree(job);
+}
 
-       if( r->rtm_family != AF_CAN )
-               return -EPFNOSUPPORT;
+/**
+ * cegw_thread_start - start working threads
+ * @data: (struct cegw_job *) with sockets and udp addresses filled in
+ *
+ * Two threads are started. One is serving udp->can routing and the other
+ * can->udp.
+ */
+static int cegw_thread_start(void *data)
+{
+       struct task_struct *task = NULL;
+       struct cegw_job *job = (struct cegw_job *)data;
 
-       err = nlmsg_parse( nlh, sizeof(*r), tb, CEGW_MAX, NULL );
-       if( err < 0 )
-       {
-               pr_devel( "canethgw: nlmsg_parse error\n" );
-               return err;
-       }
+       kref_init(&job->refcount);
+       kref_get(&job->refcount);
 
-       if( tb[CEGW_CMD_INFO] == NULL )
-       {
-               pr_devel( "canethgw: CEGW_CMD_INFO is missing in rtmsg\n" );
-               return -EINVAL;
+       task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
+       if (IS_ERR(task)) {
+               kref_sub(&job->refcount, 2, cegw_job_release);
+               return -ENOMEM;
        }
 
-       switch( *(int*)nla_data( tb[CEGW_CMD_INFO] ) )
-       {
-               case CEGW_LISTEN:
-                       if( !tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] )
-                       {
-                               pr_devel( "canethgw: missing attribute for CEGW_LISTEN\n" );
-                               return -EINVAL;
-                       }
-
-                       /* ToDo: valid listen address */
-                       set = kmalloc( sizeof(*set), GFP_KERNEL );
-                       set->eth_ip   = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
-                       set->eth_port = *(unsigned short*)nla_data( tb[CEGW_ETH_PORT] );
-                       kthread_run( cegw_thread_start, set, "canethgw" );
-                       break;
-               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 )
-                       {
-                               break;
-                       }
-                       
-                       rule->can_ifindex = ifindex;
-                       rule->eth_ip = ip;
-                       rule->eth_port = port;
-                       
-                       hlist_add_head_rcu( &rule->list, &cegw_rule_can_eth );
-                       break;
-               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 )
-                       {
-                               break;
-                       }
-
-                       rule->can_ifindex = ifindex;
-                       rule->eth_ip = ip;
-                       rule->eth_port = port;
-
-                       hlist_add_head_rcu( &rule->list, &cegw_rule_eth_can );
-                       break;
-               default:
-                       pr_devel( "canethgw: unknown CEGW_CMD_INFO\n" );
-                       break;
+       task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
+       if (IS_ERR(task)) {
+               cegw_thread_stop(job);
+               kref_put(&job->refcount, cegw_job_release);
+               return -ENOMEM;
        }
 
        return 0;
 }
 
-static void cegw_flush( void )
-{
-       struct cegw_rule* rule;
-       struct hlist_node* pos,* n;
-
-       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 );
-       }       
-}
-
-static int cegw_delroute( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
+/**
+ * cegw_thread_stop - stops threads
+ */
+static int cegw_thread_stop(struct cegw_job *job)
 {
-       struct rtmsg* r;
-       struct nlattr* tb[ CEGW_MAX+1 ];
-       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;
+       int how = SHUT_RDWR;
+       struct sock *sk = NULL;
+       struct socket *udp_sock = job->udp_sock;
+       struct socket *can_sock = job->can_sock;
 
-       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;
-       }
+       kernel_sock_shutdown(udp_sock, SHUT_RDWR);
 
-       cegw_flush();
+       /* PF_CAN sockets do not implement shutdown - do it manualy */
+       sk = can_sock->sk;
+       how++;
+       lock_sock(sk);
+       sk->sk_shutdown |= how;
+       sk->sk_state_change(sk);
+       release_sock(sk);
 
        return 0;
 }
 
-static int cegw_put_rule( struct sk_buff* skb, int type, struct cegw_rule* rule )
+static int cegw_open(struct inode *inode, struct file *file)
 {
-       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_getroute( 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();
+       file->private_data = NULL;
 
-       rcu_read_lock();
-       hlist_for_each_entry_rcu( rule, pos, &cegw_rule_can_eth, list )
-       {
-               if( idx < s_idx )
-                       goto cont2;
+       if (try_module_get(THIS_MODULE) == false)
+               return -EAGAIN;
 
-               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;
+       return 0;
 }
 
-static int cegw_notifier( struct notifier_block* nb, unsigned long msg, void* data )
+static int cegw_release(struct inode *inode, struct file *file)
 {
-       struct net_device* dev = (struct net_device*)data;
-       struct cegw_rule* rule;
-       struct hlist_node* pos,* n;
-
-       if( !net_eq(dev_net(dev), &init_net) )
-               return NOTIFY_DONE;
-       if( dev->type != ARPHRD_CAN )
-               return NOTIFY_DONE;
-
-       if( msg == NETDEV_UNREGISTER )
-       {
-               hlist_for_each_entry_safe( rule, pos, n, &cegw_rule_eth_can, list )
-               {
-                       if( rule->can_ifindex == dev->ifindex )
-                       {
-                               hlist_del( &rule->list );
-                               kfree( rule );
-                       }
-               }
+       struct cegw_job *job = (struct cegw_job *)file->private_data;
 
-               hlist_for_each_entry_safe( rule, pos, n, &cegw_rule_can_eth, list )
-               {
-                       if( rule->can_ifindex == dev->ifindex )
-                       {
-                               hlist_del( &rule->list );
-                               kfree( rule );
-                       }
-               }
+       if (job) {
+               cegw_thread_stop(job);
        }
 
-       return NOTIFY_DONE;
+       module_put(THIS_MODULE);
+       return 0;
 }
 
 /**
- * cegw_thread_start - start working threads
- * Two threads are started. One is serving udp->can routing and the other
- * can->udp.
+ * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
  *
- * @return 0 on success, -1 otherwise
+ * The function takes over cegw_ioctl structure from userspace and
+ * prepares cegw_job structure. The cegw_job is stored in
+ * file->private_data and used by kernel threads to serve gateway
+ * functionality.
  */
-static int cegw_thread_start( void* data )
+static long cegw_ioctl_start(struct file *file, unsigned long arg)
 {
-       struct sockaddr_in udp_addr;
-       struct sockaddr_can can_addr;
-       struct cegw_setting* set;
-
-       set = (struct cegw_setting*)data;
-
-       can_addr.can_family = AF_CAN;
-       can_addr.can_ifindex = 0;
-       
-       udp_addr.sin_family = AF_INET;
-       udp_addr.sin_port = htons( set->eth_port );
-       udp_addr.sin_addr = set->eth_ip;
-
-       kfree( set );
-       mutex_lock( &cegw_mutex );
-       if( cegw_state == CEGW_EXIT )
-               return -1;
-       /* stops threads if exist */
-       cegw_thread_stop();
-
-       /* create and bind sockets */
-       if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock) != 0 )
-       {
-               printk( KERN_ERR "canethgw: udp socket creation failed\n" );
-               return -1;
+       int i;
+       int err = 0;
+       __u32 dstcnt = 0;
+       __u32 addrlen = 0;
+       struct cegw_ioctl gwctl;
+       struct cegw_job *job = NULL;
+
+       err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
+       if (err != 0)
+               return -EFAULT;
+
+       dstcnt = gwctl.udp_dstcnt;
+       addrlen = gwctl.udp_addrlen;
+
+       if (addrlen != sizeof(struct sockaddr_in))
+               return -EAFNOSUPPORT;
+
+       /* */
+       job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
+       if (job == NULL)
+               return -ENOMEM;
+
+       err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
+       if (err != 0) {
+               kfree(job);
+               return -EFAULT;
        }
 
-       if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
-       {
-               printk( KERN_ERR "canethgw: can socket creation failed\n" );
-               return -1;
+       for (i=0; i<dstcnt; i++) {
+               if (job->udp_dst[i].sin_family != AF_INET) {
+                       kfree(job);
+                       return -EAFNOSUPPORT;
+               }
        }
 
-       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;
+       job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
+       if (job->udp_sock == NULL) {
+               kfree(job);
+               return err;
        }
 
-       if( kernel_bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
-       {
-               printk( KERN_ERR "canethgw: can socket binding failed\n" );
-               kernel_sock_shutdown( udp_sock, SHUT_RDWR );
-               sock_release( udp_sock );
-               sock_release( can_sock );
-               return -1;
+       job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
+       if (job->can_sock == NULL) {
+               fput(job->udp_sock->file);
+               kfree(job);
+               return err;
        }
 
-       /* start threads */
-       cegw_state = CEGW_RUN;
-
-       eth_to_can = kthread_create( cegw_udp_can, NULL, "canethgw" );
-       if( IS_ERR( eth_to_can ) )
-       {
-               cegw_state = CEGW_STOP;
-               sock_release( udp_sock );
-               sock_release( can_sock );
-               return -1;
-       }       
-       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 ) )
-       {
-               cegw_state = CEGW_STOP;
-               kernel_sock_shutdown( udp_sock, SHUT_RDWR );
-               kthread_stop( eth_to_can );
-               sock_release( udp_sock );
-               sock_release( can_sock );
-               return -1;
-       }
-       /* ToDo: free this? */
-       get_task_struct( can_to_eth );
-       wake_up_process( can_to_eth );
-       
-       mutex_unlock( &cegw_mutex );
-       pr_devel( "threads are running\n" );
+       job->udp_dstcnt = dstcnt;
+
+       err = cegw_thread_start(job);
+       if (err != 0)
+               return err;
+
+       file->private_data = job;
        return 0;
 }
 
-/**
- * cegw_thread_stop
- * Waits for threads to stop. Does nothing if cegw_state == CEGW_STOP.
- *
- * @return 0
- */
-static int cegw_thread_stop( void )
+static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       int how = SHUT_RDWR;
-       struct sock* sk = NULL;
-
-       if( cegw_state == CEGW_STOP )
-               return 0;
+       int err;
 
-       cegw_state = CEGW_STOP;
-       /* shut down socket */
-       sk = can_sock->sk;
-       how++;
-       lock_sock( sk );
-       sk->sk_shutdown |= how;
-       sk->sk_state_change( sk );
-       release_sock( sk );
+       switch (cmd) {
+               case CEGW_IOCTL_START:
+                       err = cegw_ioctl_start(file, arg);
+                       break;
+               default:
+                       err = -EOPNOTSUPP;
+                       break;
+       }
 
-       kernel_sock_shutdown( udp_sock, SHUT_RDWR );
+       return err;
+}
 
-       /* 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;
+static const struct file_operations cegw_fops = {
+       .owner = THIS_MODULE,
+       .open = cegw_open,
+       .release = cegw_release,
+       .unlocked_ioctl = cegw_ioctl
+};
 
-       return 0;
-}
+static struct miscdevice cegw_device = {
+       .minor = MISC_DYNAMIC_MINOR,
+       .name = "canethgw",
+       .fops = &cegw_fops
+};
 
-static int __init cegw_init( void )
+static int __init cegw_init(void)
 {
-       notifier.notifier_call = cegw_notifier;
-       register_netdevice_notifier( &notifier );
-
-       /* subscribe to netlink */
-       rtnl_register( PF_CAN, RTM_GETROUTE, NULL, cegw_getroute, NULL );
-       rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_newroute, NULL, NULL );
-       rtnl_register( PF_CAN, RTM_DELROUTE, cegw_delroute, NULL, NULL );
+       misc_register(&cegw_device);
 
        return 0;
 }
 
-static void __exit cegw_exit( void )
+static void __exit cegw_exit(void)
 {
-       /* ToDo: effect on cangw? */
-       rtnl_unregister_all( PF_CAN );
-
-       /* wait for rtnl callbacks */
-       rtnl_lock();
-       rtnl_unlock();
-
-       mutex_lock( &cegw_mutex );
-       cegw_thread_stop();
-       cegw_state = CEGW_EXIT;
-       mutex_unlock( &cegw_mutex );
+       misc_deregister(&cegw_device);
 
-       unregister_netdevice_notifier( &notifier );
-       cegw_flush();
+       return;
 }
 
-module_init( cegw_init );
-module_exit( cegw_exit );
+module_init(cegw_init);
+module_exit(cegw_exit);