]> 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 0beb89f2d30ba15c867bffbd79d32376c27f2e8e..685ca6c56ae78ffc911dc6180cb113593a8778ae 100644 (file)
-#include "gw.h" /* override */
+/*
+ * 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/net.h>
-#include <linux/can/core.h>
+#include <linux/file.h>
+#include <net/sock.h>
 #include <linux/can.h>
-#include <net/rtnetlink.h>
+#include <linux/miscdevice.h>
 #include "canethgw.h"
 
-MODULE_LICENSE( "GPL" );
+MODULE_LICENSE("GPL");
 
-static struct task_struct* eth_to_can, * can_to_eth;
-static struct socket* udp_sock;
-static struct socket* can_sock;
-static struct net_device* can_dev;
+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);
 
-struct can_eth_gw
+static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
 {
-       int src_if_idx;
-       struct in_addr dst_addr;
-       unsigned short dst_port;
-       struct hlist_node list;
-};
+       struct msghdr mh;
+       struct kvec vec;
+       int err;
 
-struct eth_can_gw
-{
-       int dst_if_idx;
-       struct hlist_node list;
-};
+       mh.msg_name = addr;
+       mh.msg_namelen = sizeof(*addr);
+       mh.msg_control = NULL;
+       mh.msg_controllen = 0;
+       mh.msg_flags = 0;
 
-HLIST_HEAD( can_eth_job );
-HLIST_HEAD( eth_can_job );
+       vec.iov_base = cf;
+       vec.iov_len = sizeof(*cf);
 
-struct cegw_setting
-{
-       struct can_filter filter;
-       int src_idx;
-       /* bind on if */
-       struct in_addr dst_addr;
-       unsigned short dst_port;
-};
+       err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
 
-/***********************
- *   UDP
- ***********************/
+       return err;
+}
 
-static int gw_udp_recv( void* data )
+static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
 {
-       struct can_frame cf;
-       struct kvec vec;
        struct msghdr mh;
-       struct eth_can_gw* job;
-       struct hlist_node* pos;
-
-       vec.iov_base = &cf;
-       vec.iov_len = sizeof(cf);
+       struct kvec vec;
+       int err;
 
        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;
 
-       while( 1 )
-       {
-               if( kthread_should_stop() ) /* up() ?, recv is blocking */
-                       break;
-               kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */
-               printk( "received udp msg_id:%d\n", cf.can_id );
-               rcu_read_lock();
-               hlist_for_each_entry_rcu( job, pos, &eth_can_job, list )
-               {
-                       /* ToDo from filter */
-                       gw_can_send( &cf, job->dst_if_idx );
-               }
-               rcu_read_unlock();
-       }
+       vec.iov_base = cf;
+       vec.iov_len = sizeof(*cf);
 
-       return 0;
+       err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
+
+       return err;
 }
 
-inline static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port )
+/**
+ * cegw_udp2can - performs udp->can routing
+ *
+ * This function is run as a thread.
+ */
+static int cegw_udp2can(void *data)
 {
-       struct msghdr mh;
-       struct sockaddr_in addr;
+       struct can_frame cf;
        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 ) );
-}
+       struct msghdr mh;
+       struct cegw_job *job = (struct cegw_job *)data;
+       struct socket *udp_sock = NULL, *can_sock = NULL;
+       int ret = 0;
+
+       memset(&mh, 0, sizeof(mh));
+       udp_sock = job->udp_sock;
+       can_sock = job->can_sock;
+
+       while (1) {
+               vec.iov_base = &cf;
+               vec.iov_len = sizeof(cf);
+               ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
+                               sizeof(cf), 0);
+               if (ret < 1)
+                       break;
 
-/***********************
- *   CAN
- ***********************/
+               cf.can_id = be32_to_cpu(cf.can_id);
+               cegw_can_send(can_sock, &cf);
+       }
+
+       cegw_thread_stop(job);
+       kref_put(&job->refcount, cegw_job_release);
+       do_exit(ret);
+}
 
-static int gw_can_recv( void* data )
+/**
+ * cegw_can2udp - performs can->udp routing
+ *
+ * This function is run as a thread.
+ */
+static int cegw_can2udp(void* data)
 {
        struct msghdr mh;
        struct kvec vec;
        struct can_frame cf;
-       struct sockaddr_can ca;
-       struct can_eth_gw* job;
-       struct hlist_node* pos;
-       
-       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 );
+       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;
+
+       memset(&mh, 0, sizeof(mh));
 
-       while( 1 )
-       {
-               if( kthread_should_stop() ) /**/
+       while (1) {
+               vec.iov_base = &cf;
+               vec.iov_len = sizeof(cf);
+
+               ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
+                                          sizeof(cf), 0);
+               if (ret < 1)
                        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 );
-               rcu_read_lock();
-               hlist_for_each_entry_rcu( job, pos, &can_eth_job, list )
-               {
-                       printk( KERN_INFO "%x\n", job->dst_addr );
-                       if( job->src_if_idx == ca.can_ifindex )
-                               gw_udp_send( &cf, job->dst_addr, job->dst_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]);
                }
-               rcu_read_unlock();
        }
-       
-       return 0;
+
+       cegw_thread_stop(job);
+       kref_put(&job->refcount, cegw_job_release);
+       do_exit(ret);
 }
 
-inline static void gw_can_send( struct can_frame* cf, int ifidx )
+static void cegw_job_release(struct kref *ref)
 {
-       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 ) );
-}
+       struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
 
-/* NetLink */
+       fput(job->can_sock->file);
+       fput(job->udp_sock->file);
+       kfree(job);
+}
 
-static int cgw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh,
-                     void* arg )
+/**
+ * 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 nlattr* tb[ CGW_MAX+1 ];
-       struct rtcanmsg *r;
-       struct cgw_job *gwj;
-       struct can_eth_gw* cethgw = NULL;
-       struct eth_can_gw* ecangw = NULL;
-       int err = 0;
-       
-       if (nlmsg_len(nlh) < sizeof(*r))
-               return -EINVAL;
-
-       r = nlmsg_data(nlh);
-       if (r->can_family != AF_CAN)
-               return -EPFNOSUPPORT;
-       err = nlmsg_parse( nlh, sizeof( struct rtcanmsg ), tb, CGW_MAX, NULL );
-       if( err < 0 )
-       {
-               printk( KERN_ERR "error: nlmsg_parse\n" );
-               return err;
+       struct task_struct *task = NULL;
+       struct cegw_job *job = (struct cegw_job *)data;
+
+       kref_init(&job->refcount);
+       kref_get(&job->refcount);
+
+       task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
+       if (IS_ERR(task)) {
+               kref_sub(&job->refcount, 2, cegw_job_release);
+               return -ENOMEM;
        }
 
-       /* so far we only support CAN -> CAN routings */
-       switch( r->gwtype )
-       {
-               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 )
-                       {
-                               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 );
-                       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 )
-                       {
-                               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 );
-                       break;
-               default:
-                       /* ToDo undef operation */
-                       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;
 }
 
-/***********************
- *   module init/exit
- ***********************/
-
-static int __init cangw_init( void )
-{      
-       struct sockaddr_in udp_addr;
-       struct sockaddr_can can_addr;
-       int ifidx = 0;
-       
-       /* 1. create can socket and bind to it */
-       can_dev = dev_get_by_name( &init_net, "vcan0" ); /* net ns?, release counter! */
-       if( can_dev == NULL )
-       {
-               printk( KERN_ERR "error: vcan0 not found\n" );
-               return -1;
+/**
+ * cegw_thread_stop - stops threads
+ */
+static int cegw_thread_stop(struct cegw_job *job)
+{
+       int how = SHUT_RDWR;
+       struct sock *sk = NULL;
+       struct socket *udp_sock = job->udp_sock;
+       struct socket *can_sock = job->can_sock;
+
+       kernel_sock_shutdown(udp_sock, SHUT_RDWR);
+
+       /* 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_open(struct inode *inode, struct file *file)
+{
+       file->private_data = NULL;
+
+       if (try_module_get(THIS_MODULE) == false)
+               return -EAGAIN;
+
+       return 0;
+}
+
+static int cegw_release(struct inode *inode, struct file *file)
+{
+       struct cegw_job *job = (struct cegw_job *)file->private_data;
+
+       if (job) {
+               cegw_thread_stop(job);
        }
-       ifidx = can_dev->ifindex;
-       dev_put( can_dev );
-       
-       if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
-       {
-               printk( KERN_ERR "error: can_sock creation failed\n" );
-               return -1;
+
+       module_put(THIS_MODULE);
+       return 0;
+}
+
+/**
+ * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
+ *
+ * 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 long cegw_ioctl_start(struct file *file, unsigned long arg)
+{
+       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;
        }
-       
-       can_addr.can_family = AF_CAN;
-       can_addr.can_ifindex = ifidx;
-       
-       if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
-       {
-               printk( KERN_ERR "can_sock bind failed\n" );
-               return -1;
+
+       for (i=0; i<dstcnt; i++) {
+               if (job->udp_dst[i].sin_family != AF_INET) {
+                       kfree(job);
+                       return -EAFNOSUPPORT;
+               }
        }
-       
-       /* 2. create udp socket and bind to it */
-       if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
-       {
-               printk( "error: udp_sock creation failed\n" );
-               sock_release( can_sock );
-               return -1;
+
+       job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
+       if (job->udp_sock == NULL) {
+               kfree(job);
+               return err;
        }
-       
-       udp_addr.sin_family = AF_INET;
-       udp_addr.sin_port = htons( 10501 );
-       udp_addr.sin_addr.s_addr = INADDR_ANY;
-
-       if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
-       {
-               printk( "error: binding failed\n" );
-               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;
        }
-       
-       /* 3. 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,  )     
-       
-       /* 4. run threads */
-       eth_to_can = kthread_run( gw_udp_recv, NULL, "cangw" );
-       can_to_eth = kthread_run( gw_can_recv, NULL, "cangw" );
-
-       /*
-       if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
-       {s
-               printk( "error: can_sock creation failed\n" );
+
+       job->udp_dstcnt = dstcnt;
+
+       err = cegw_thread_start(job);
+       if (err != 0)
+               return err;
+
+       file->private_data = job;
+       return 0;
+}
+
+static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       int err;
+
+       switch (cmd) {
+               case CEGW_IOCTL_START:
+                       err = cegw_ioctl_start(file, arg);
+                       break;
+               default:
+                       err = -EOPNOTSUPP;
+                       break;
        }
-       */
-       
+
+       return err;
+}
+
+static const struct file_operations cegw_fops = {
+       .owner = THIS_MODULE,
+       .open = cegw_open,
+       .release = cegw_release,
+       .unlocked_ioctl = cegw_ioctl
+};
+
+static struct miscdevice cegw_device = {
+       .minor = MISC_DYNAMIC_MINOR,
+       .name = "canethgw",
+       .fops = &cegw_fops
+};
+
+static int __init cegw_init(void)
+{
+       misc_register(&cegw_device);
+
        return 0;
 }
 
-static void __exit cangw_exit( void )
+static void __exit cegw_exit(void)
 {
-       sock_release( udp_sock );
-       sock_release( can_sock );
-       
-       printk( "cangw: exit\n" );
-       //kthread_stop( ts );
+       misc_deregister(&cegw_device);
+
+       return;
 }
 
-module_init( cangw_init );
-module_exit( cangw_exit );
+module_init(cegw_init);
+module_exit(cegw_exit);