-#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/net.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>
-
-/**
- * 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 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 void 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 socket* can_sock, * udp_sock;
-static struct task_struct* eth_to_can, * can_to_eth;
-/* ToDo: protect with mutex */
-static int cegw_state = CEGW_STOPPED;
-
-struct can_eth_gw
-{
- int src_ifindex;
- struct in_addr dst_addr;
- unsigned short dst_port;
- struct hlist_node list;
-};
-
-struct eth_can_gw
-{
- int dst_if_idx;
- struct hlist_node list;
-};
+MODULE_LICENSE("GPL");
-HLIST_HEAD( can_eth_job );
-HLIST_HEAD( eth_can_job );
+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
-{
- struct can_filter filter;
- int can_idx;
- /* bind on if */
- struct in_addr eth_addr;
- unsigned short eth_port;
-} cegw_setting;
-
-DECLARE_COMPLETION( cegw_compl_init );
-DECLARE_COMPLETION( cegw_compl_exit );
-DEFINE_MUTEX( cegw_setting_mutex );
-
-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 ) ); /* ToDo: handle error */
+ 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 eth_can_gw* job;
- struct hlist_node* pos;
- int can_ifidx;
- int recv_size;
- struct sockaddr_in udp_addr;
- struct sockaddr_can can_addr;
-
- can_addr.can_family = AF_CAN;
- can_addr.can_ifindex = 0;
-
- udp_addr.sin_family = AF_INET;
- mutex_lock( &cegw_setting_mutex );
- udp_addr.sin_port = htons( cegw_setting.eth_port );
- udp_addr.sin_addr = cegw_setting.eth_addr;
- mutex_unlock( &cegw_setting_mutex );
-
- 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;
-
- if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock) != 0 )
- {
- printk( KERN_ERR "canethgw: udp socket creation failed\n" );
- return -1;
- }
-
- if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
- {
- printk( KERN_ERR "canethgw: can socket creation failed\n" );
- return -1;
- }
+ struct cegw_job *job = (struct cegw_job *)data;
+ struct socket *udp_sock = NULL, *can_sock = NULL;
+ int ret = 0;
- 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;
- }
+ memset(&mh, 0, sizeof(mh));
+ udp_sock = job->udp_sock;
+ can_sock = job->can_sock;
- if( kernel_bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
- {
- printk( KERN_ERR "canethgw: can socket binding failed\n" );
- sock_release( udp_sock );
- sock_release( can_sock );
- return -1;
- }
-
- pr_devel( "canethgw: cegw_udp_can init complete\n" );
- complete_all( &cegw_compl_init ); /* ToDo: why _all? */
- pr_devel( "canethgw: cegw_udp_can is entering working cycle\n" );
-
- while( 1 )
- {
- if( cegw_state == CEGW_STOPPED )
- break;
+ while (1) {
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, ð_can_job, list )
- {
- rcu_read_lock(); /**/
- can_ifidx = job->dst_if_idx;
- rcu_read_unlock();
- /* ToDo: from filter */
- cegw_can_send( can_sock, &cf, can_ifidx );
- }
+ ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
+ sizeof(cf), 0);
+ if (ret < 1)
+ break;
+
+ cf.can_id = be32_to_cpu(cf.can_id);
+ cegw_can_send(can_sock, &cf);
}
- pr_devel( "canethgw: cegw_udp_can is ready for termination\n" );
- wait_for_completion( &cegw_compl_exit );
- sock_release( udp_sock );
- sock_release( can_sock );
- cegw_state = CEGW_STOPPED; /* ToDo */
- pr_devel( "canethgw: cegw_udp_can terminates\n" );
- 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 can_eth_gw* job;
- 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;
+ 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;
- wait_for_completion( &cegw_compl_init );
- pr_devel( "canethgw: cegw_can_udp is entering working cycle\n" );
+ memset(&mh, 0, sizeof(mh));
- while( 1 )
- {
- if( cegw_state == CEGW_STOPPED ) /**/
- break;
+ while (1) {
vec.iov_base = &cf;
- vec.iov_len = sizeof( 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( job, pos, &can_eth_job, list )
- {
- rcu_read_lock();
- eth_addr = job->dst_addr;
- eth_port = job->dst_port;
- rcu_read_unlock();
- if( job->src_ifindex == ca.can_ifindex )
- cegw_udp_send( udp_sock, &cf, eth_addr, eth_port );
+ ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
+ sizeof(cf), 0);
+ if (ret < 1)
+ break;
+
+ 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]);
}
}
-
- complete_all( &cegw_compl_exit );
- pr_devel( "canethgw: cegw_can_udp terminates\n" );
- return 0;
+
+ cegw_thread_stop(job);
+ kref_put(&job->refcount, cegw_job_release);
+ do_exit(ret);
}
-/* NetLink */
+static void cegw_job_release(struct kref *ref)
+{
+ struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
+
+ fput(job->can_sock->file);
+ fput(job->udp_sock->file);
+ kfree(job);
+}
-static int cegw_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[ CEGW_MAX+1 ];
- struct can_eth_gw* cethgw = NULL;
- struct eth_can_gw* ecangw = NULL;
- int ifindex;
- u32 ip;
- u16 port;
- int err = 0;
-
- /* ToDo: size check
- if (nlmsg_len(nlh) < sizeof(*r))
- return -EINVAL;
- */
-
- err = nlmsg_parse( nlh, sizeof( struct rtmsg ), tb, CEGW_MAX, NULL );
- if( err < 0 )
- {
- printk( KERN_ERR "canethgw: nlmsg_parse error\n" );
- return err;
- }
+ struct task_struct *task = NULL;
+ struct cegw_job *job = (struct cegw_job *)data;
- if( tb[CEGW_CMD_INFO] == NULL )
- {
- printk( KERN_ERR "canethgw: bad cmd error\n" );
- return -EINVAL;
+ 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;
}
- switch( *(int*)nla_data( tb[CEGW_CMD_INFO] ) )
- {
- case CEGW_LISTEN:
- if( cegw_state == CEGW_RUNNING )
- {
- mutex_lock( &cegw_setting_mutex );
- cegw_setting.eth_addr = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
- cegw_setting.eth_port = *(u16*)nla_data( tb[CEGW_ETH_PORT] );
- mutex_unlock( &cegw_setting_mutex );
- kthread_run( cegw_thread_restart, NULL, "canethgw" );
- } else
- {
- mutex_lock( &cegw_setting_mutex );
- cegw_setting.eth_addr = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
- cegw_setting.eth_port = *(u16*)nla_data( tb[CEGW_ETH_PORT] );
- mutex_unlock( &cegw_setting_mutex );
- cegw_thread_start();
- }
- break;
- case CEGW_RULE_CAN_ETH: /* new can->eth rule */
- {
- ifindex = *(int*)nla_data( tb[CEGW_CAN_IFINDEX] );
- ip = *(u32*)nla_data( tb[CEGW_ETH_IP] );
- port = *(u16*)nla_data( tb[CEGW_ETH_PORT] );
- pr_devel( "canethgw: new can->eth rule - (%d)->(%x:%hu)\n", ifindex, ip, port );
-
- cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
- if( cethgw == NULL )
- {
- break;
- }
- cethgw->src_ifindex = ifindex;
- cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CEGW_ETH_IP] );
- cethgw->dst_port = *(u16*)nla_data( tb[CEGW_ETH_PORT] );
-
- hlist_add_head_rcu( &cethgw->list, &can_eth_job );
- }
- break;
- case CEGW_RULE_ETH_CAN: /* new eth->can rule */
- printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CEGW_CAN_IFINDEX] ) );
- ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
- if( ecangw == NULL )
- {
- break;
- }
- ecangw->dst_if_idx = *(int*)nla_data( tb[CEGW_CAN_IFINDEX] );
- hlist_add_head_rcu( &ecangw->list, ð_can_job );
- break;
- default:
- pr_devel( "canethgw: unknown CEGW_CMD_INFO\n" );
- /* 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;
}
-static int cegw_remove_job( 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 ];
- struct hlist_node* pos,* n;
- struct can_eth_gw* ceth;
- struct eth_can_gw* ecan;
+ int how = SHUT_RDWR;
+ struct sock *sk = NULL;
+ struct socket *udp_sock = job->udp_sock;
+ struct socket *can_sock = job->can_sock;
- int err = 0;
+ 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);
- if( nlmsg_len(nlh) < sizeof(*r) )
- return -EINVAL;
-
- r = nlmsg_data( nlh );
-
- if( r->rtm_family != AF_CAN )
- return -EPFNOSUPPORT;
-
- /*
- if( r->gwtype != CGW_TYPE_CAN_ETH_UDP )
- return -EINVAL;
- */
- printk( "attrsize=%d\n", nlmsg_attrlen(nlh, sizeof(struct rtmsg)) );
-
- err = nlmsg_parse( nlh, sizeof(struct rtmsg), tb, CEGW_MAX, NULL );
- if( err != 0 )
- return -EINVAL;
-
- if( tb[CEGW_CMD_INFO] == NULL )
- return -EINVAL;
-
- if( *(int*)nla_data( tb[CEGW_CMD_INFO] ) == CEGW_FLUSH )
- {
- hlist_for_each_entry_safe( ceth, pos, n, &can_eth_job, list )
- {
- hlist_del( &ceth->list );
- kfree( ceth );
- }
- hlist_for_each_entry_safe( ecan, pos, n, ð_can_job, list )
- {
- hlist_del( &ecan->list );
- kfree( ecan );
- }
- }
- // tb[]
return 0;
}
-static int cegw_dump_job( struct sk_buff* skb, struct netlink_callback* cb )
+static int cegw_open(struct inode *inode, struct file *file)
{
- struct can_eth_gw* ceth;
- struct eth_can_gw* ecan;
- struct hlist_node* pos;
- struct nlmsghdr* nlh;
- int idx = 0;
- int s_idx = cb->args[0];
- int ifidx, type;
- struct in_addr dst_ip;
- unsigned short dst_port;
-
- rcu_read_lock();
- hlist_for_each_entry_rcu( ecan, pos, ð_can_job, list )
- {
-
- // if( idx < s_idx )
- // goto cont1;
-
- nlh = nlmsg_put( skb, 0, 0, 0, 0, 0 );
-
- ifidx = ecan->dst_if_idx;
- type = CEGW_RULE_ETH_CAN;
- nla_put( skb, CEGW_TYPE, sizeof(type), &type );
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(type) );
-
- nla_put( skb, CEGW_CAN_IFINDEX, sizeof(ifidx), &ifidx ); /* ToDo return */
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ifidx) );
-cont1:
- idx++;
- }
- rcu_read_unlock();
-
- rcu_read_lock();
- hlist_for_each_entry_rcu( ceth, pos, &can_eth_job, list )
- {
- // if( idx < s_idx )
- // goto cont2;
-
- nlh = nlmsg_put( skb, 0, 0, 0, 0, 0 );
-
- ifidx = ceth->src_ifindex;
- type = CEGW_RULE_CAN_ETH;
- dst_ip = ceth->dst_addr;
- dst_port = ceth->dst_port;
-
- nla_put( skb, CEGW_TYPE, sizeof(type), &type );
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(type) );
-
- nla_put( skb, CEGW_CAN_IFINDEX, sizeof(ifidx), &ifidx ); /* ToDo return */
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ifidx) );
-
- nla_put( skb, CEGW_ETH_IP, sizeof(dst_ip), &dst_ip ); /* ToDo return */
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(dst_ip) );
-
- nla_put( skb, CEGW_ETH_PORT, sizeof(dst_port), &dst_port ); /* ToDo return */
- nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(dst_port) );
-
- //nla_put( skb, CGW_ETH_IP, sizeof() IP_ADDR )
-cont2:
- idx++;
- }
- rcu_read_unlock();
+ file->private_data = NULL;
- /* ToDo nlmsg_cancel */
- cb->args[0] = idx;
+ if (try_module_get(THIS_MODULE) == false)
+ return -EAGAIN;
- return skb->len;
+ return 0;
}
-static void cegw_thread_start( void )
+static int cegw_release(struct inode *inode, struct file *file)
{
- cegw_state = CEGW_RUNNING;
-
- INIT_COMPLETION( cegw_compl_init );
- INIT_COMPLETION( cegw_compl_exit );
-
- 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_run( cegw_can_udp, NULL, "canethgw" );
- if( !IS_ERR( can_to_eth ) )
- {
- get_task_struct( can_to_eth );
- wake_up_process( can_to_eth );
+ struct cegw_job *job = (struct cegw_job *)file->private_data;
+
+ if (job) {
+ cegw_thread_stop(job);
}
-
- /* ToDo: kthread creation fail */
- printk( KERN_INFO "threads are running\n" );
+
+ module_put(THIS_MODULE);
+ return 0;
}
-/* ToDo: stop when no threads started */
-static void cegw_thread_stop( void )
+/**
+ * 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 how = SHUT_RDWR;
- struct sock* sk = NULL;
+ 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;
+ }
- /* be sure sockets exist */
- wait_for_completion( &cegw_compl_init );
- cegw_state = CEGW_STOPPED;
+ for (i=0; i<dstcnt; i++) {
+ if (job->udp_dst[i].sin_family != AF_INET) {
+ kfree(job);
+ return -EAFNOSUPPORT;
+ }
+ }
- /* shut down socket */
- sk = can_sock->sk;
- how++;
- lock_sock( sk );
- sk->sk_shutdown |= how;
- sk->sk_state_change( sk );
- release_sock( sk );
+ job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
+ if (job->udp_sock == NULL) {
+ kfree(job);
+ return err;
+ }
+
+ job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
+ if (job->can_sock == NULL) {
+ fput(job->udp_sock->file);
+ kfree(job);
+ return err;
+ }
- kernel_sock_shutdown( udp_sock, SHUT_RDWR );
+ job->udp_dstcnt = dstcnt;
- /* wait for return to reuse port if restart */
- kthread_stop( eth_to_can );
- kthread_stop( can_to_eth );
+ err = cegw_thread_start(job);
+ if (err != 0)
+ return err;
+
+ file->private_data = job;
+ return 0;
}
-/**
- * cegw_thread_restart
- */
-static int cegw_thread_restart( void* data )
+static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
- printk( "restart\n" );
+ int err;
- cegw_thread_stop();
- cegw_thread_start();
+ switch (cmd) {
+ case CEGW_IOCTL_START:
+ err = cegw_ioctl_start(file, arg);
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ break;
+ }
- return 0;
+ return err;
}
-/***********************
- * module init/exit
- ***********************/
+static const struct file_operations cegw_fops = {
+ .owner = THIS_MODULE,
+ .open = cegw_open,
+ .release = cegw_release,
+ .unlocked_ioctl = cegw_ioctl
+};
-static int __init cangw_init( void )
+static struct miscdevice cegw_device = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "canethgw",
+ .fops = &cegw_fops
+};
+
+static int __init cegw_init(void)
{
- /* subscribe to netlink */
- if( __rtnl_register( PF_CAN, RTM_GETROUTE, NULL, cegw_dump_job, NULL ) != 0 )
- {
- printk( KERN_ERR "error: rtnl_register fail\n" );
- return -1;
- }
- __rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL );
- __rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL );
+ misc_register(&cegw_device);
return 0;
}
-static void __exit cangw_exit( void )
+static void __exit cegw_exit(void)
{
- if( cegw_state == CEGW_RUNNING )
- {
- cegw_thread_stop();
- /* ToDo: frees mem_cache? */
- /* udp must not exists */
- }
+ misc_deregister(&cegw_device);
- /* ToDo: unregister netlink
- * free jobs */
- printk( "cangw: exit\n" );
+ return;
}
-module_init( cangw_init );
-module_exit( cangw_exit );
+module_init(cegw_init);
+module_exit(cegw_exit);