#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/netdevice.h>
+#include <linux/file.h>
#include <linux/socket.h>
+#include <net/sock.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 <net/sock.h>
-#include "canethgw.h"
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <net/inet_common.h>
+#include "canethgw.h"
MODULE_LICENSE("GPL");
-static int cegw_udp2can(void *data);
-static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
+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 void cegw_can_send(struct socket *can_sock, struct can_frame *cf);
+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(void);
+static int cegw_thread_stop(struct cegw_job *job);
+static void cegw_job_release(struct kref *ref);
enum __cegw_state {
CEGW_RUN,
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;
-
-static HLIST_HEAD(rule_eth_can);
-static HLIST_HEAD(rule_can_eth);
-static DEFINE_MUTEX(rule_eth_can_mutex);
-static DEFINE_MUTEX(rule_can_eth_mutex);
-static DEFINE_MUTEX(cegw_mutex);
-
-static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
+static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
{
struct msghdr mh;
struct kvec vec;
+ int err;
mh.msg_name = addr;
mh.msg_namelen = sizeof(*addr);
vec.iov_len = sizeof(*cf);
/* FIXME: Convert endianing of cf->can_id */
- kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
+ err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
+
+ return err;
}
-static void cegw_can_send(struct socket* can_sock, struct can_frame* cf)
+static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
{
struct msghdr mh;
struct kvec vec;
+ int err;
mh.msg_name = NULL;
mh.msg_namelen = 0;
vec.iov_base = cf;
vec.iov_len = sizeof(*cf);
- kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
+ err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
+
+ return err;
}
/**
struct msghdr mh;
struct cegw_job *job = (struct cegw_job *)data;
struct socket *udp_sock = NULL, *can_sock = NULL;
- int recv_size;
+ int ret = 0;
memset(&mh, 0, sizeof(mh));
udp_sock = job->udp_sock;
can_sock = job->can_sock;
while (1) {
- printk( "recv\n" );
vec.iov_base = &cf;
vec.iov_len = sizeof(cf);
- recv_size = kernel_recvmsg(udp_sock, &mh, &vec, 1,
+ ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
sizeof(cf), 0);
- /* if(recv_size != sizeof(cf) */
- if (recv_size < 1) /* ToDo: split 0 and else */
- {
- printk("udp2can error\n");
+ if (ret < 1)
break;
- }
/* FIXME: Convert endianing of cf.can_id */
- printk( "sendit to can\n" );
cegw_can_send(can_sock, &cf);
}
- return 0;
+ cegw_thread_stop(job);
+ kref_put(&job->refcount, cegw_job_release);
+ do_exit(ret);
}
/**
struct kvec vec;
struct can_frame cf;
struct cegw_job* job = (struct cegw_job*)data;
- int recv_size;
struct socket* udp_sock = job->udp_sock;
struct socket* can_sock = job->can_sock;
int i;
+ int ret;
memset(&mh, 0, sizeof(mh));
vec.iov_base = &cf;
vec.iov_len = sizeof(cf);
- recv_size = kernel_recvmsg(can_sock, &mh, &vec, 1,
+ ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
sizeof(cf), 0);
-
- if (recv_size < 1)
+ if (ret < 1)
break;
- for( i=0; i<job->udp_dstcnt; i++ )
- {
+ 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_notifier(struct notifier_block *nb, unsigned long msg, void *data)
+static void cegw_job_release(struct kref *ref)
{
- 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, &rule_eth_can, list) {
- if (rule->can_ifindex == dev->ifindex) {
- hlist_del(&rule->list);
- kfree(rule);
- }
- }
-
- hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
- if (rule->can_ifindex == dev->ifindex) {
- hlist_del(&rule->list);
- kfree(rule);
- }
- }
- }
+ struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
- return NOTIFY_DONE;
+ fput(job->can_sock->file);
+ fput(job->udp_sock->file);
+ kfree(job);
}
/**
* cegw_thread_start - start working threads
- * @data: (struct cegw_setting *) with new listening address
+ * @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;
+
+ kref_init(&job->refcount);
+ kref_get(&job->refcount);
task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
if (IS_ERR(task)) {
- goto out_err;
+ kref_sub(&job->refcount, 2, cegw_job_release);
+ return -ENOMEM;
}
task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
if (IS_ERR(task)) {
- goto out_err;
+ cegw_thread_stop(job);
+ kref_put(&job->refcount, cegw_job_release);
+ return -ENOMEM;
}
return 0;
-out_err:
- return -ENOMEM;
}
/**
- * cegw_thread_stop - stops threads and wait for exit
- *
- * Waits for threads to stop. Does nothing if cegw_state == CEGW_STOP.
+ * cegw_thread_stop - stops threads
*/
-static int cegw_thread_stop(void)
+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;
- if (cegw_state == CEGW_STOP)
- return 0;
+ kernel_sock_shutdown(udp_sock, SHUT_RDWR);
- cegw_state = CEGW_STOP;
- /* shut down socket */
+ /* PF_CAN sockets do not implement shutdown - do it manualy */
sk = can_sock->sk;
how++;
lock_sock(sk);
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;
-
return 0;
}
static int cegw_open(struct inode *inode, struct file *file)
{
- file->private_data = "greetings";
+ file->private_data = NULL;
+
+ if (try_module_get(THIS_MODULE) == false)
+ return -EAGAIN;
- printk("cegw device opened\n");
return 0;
}
{
struct cegw_job *job = (struct cegw_job *)file->private_data;
- sock_release( job->udp_sock );
- sock_release( job->can_sock );
- printk("cegw device released, data=%s\n", (char *)file->private_data);
+ if (job) {
+ cegw_thread_stop(job);
+ }
+
+ module_put(THIS_MODULE);
return 0;
}
-static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+/**
+ * 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;
- int memsz = 0;
__u32 dstcnt = 0;
__u32 addrlen = 0;
- struct cegw_ioctl *gwctl = NULL;
+ struct cegw_ioctl gwctl;
struct cegw_job *job = NULL;
- /* ToDo: verify access and authorization */
- if( get_user(dstcnt, &((struct cegw_ioctl __user *)arg)->udp_dstcnt) != 0 )
- return -EFAULT;
- if( get_user(addrlen, &((struct cegw_ioctl __user *)arg)->udp_addrlen) != 0 )
+ err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
+ if (err != 0)
return -EFAULT;
- memsz = sizeof(*gwctl) + dstcnt * addrlen;
- /* ToDo: memory limit */
- gwctl = kmalloc(GFP_KERNEL, memsz);
- if (gwctl==NULL)
- return -ENOMEM;
+ dstcnt = gwctl.udp_dstcnt;
+ addrlen = gwctl.udp_addrlen;
- err = copy_from_user(gwctl, (void __user *)arg, memsz);
- if (err != 0)
- {
- return -EFAULT;
- }
- /**/
- job = kmalloc(GFP_KERNEL, sizeof(*job));
- if (job == NULL)
- return -ENOMEM;
+ if (addrlen != sizeof(struct sockaddr_in))
+ return -EAFNOSUPPORT;
- job->udp_dst = kmalloc(GFP_KERNEL, dstcnt * addrlen); /* ToDo: limit */
- if (job->udp_dst == NULL)
+ /* */
+ 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)
+ err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
+ if (err != 0) {
+ kfree(job);
return -EFAULT;
+ }
+
+ for (i=0; i<dstcnt; i++) {
+ if (job->udp_dst[i].sin_family != AF_INET) {
+ kfree(job);
+ return -EAFNOSUPPORT;
+ }
+ }
- job->udp_sock = sockfd_lookup( gwctl->udp_sock, &err );
- if (job->udp_sock == NULL)
+ 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)
+ job->can_sock = sockfd_lookup( gwctl.can_sock, &err );
+ if (job->can_sock == NULL) {
+ fput(job->udp_sock->file);
+ kfree(job);
return err;
+ }
job->udp_dstcnt = dstcnt;
- /* ToDo: sin_family? */
+ err = cegw_thread_start(job);
+ if (err != 0)
+ return err;
file->private_data = job;
- cegw_thread_start(job);
+ return 0;
+}
+
+static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ int err = 0;
- /* process udp destinations */
switch (cmd) {
case CEGW_IOCTL_START:
- printk("udp_dstcnt=%i\n", gwctl->udp_dstcnt);
- printk("udp_dst[0] family=%i\n", job->udp_dst[0].sin_family);
- printk("udp_dst[1] family=%i\n", job->udp_dst[1].sin_family);
+ err = cegw_ioctl_start(file, arg);
break;
default:
- printk("undefined ioctl command\n");
break;
}
- return 0;
+ return err;
}
static const struct file_operations cegw_fops = {
{
misc_register(&cegw_device);
- return 0;
- notifier.notifier_call = cegw_notifier;
- register_netdevice_notifier(¬ifier);
-
return 0;
}
{
misc_deregister(&cegw_device);
+ /* ToDo: exit threads */
return;
- /* 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);
-
- unregister_netdevice_notifier(¬ifier);
- //cegw_flush();
}
module_init(cegw_init);