2 * Copyright: (c) 2012 Czech Technical University in Prague
5 * Radek Matějka <radek.matejka@gmail.com>
6 * Michal Sojka <sojkam1@fel.cvut.cz>
8 * Funded by: Volkswagen Group Research
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/kthread.h>
19 #include <linux/file.h>
21 #include <linux/can.h>
22 #include <linux/miscdevice.h>
23 #include <linux/can/canethgw.h>
25 MODULE_LICENSE("GPL");
34 struct socket* can_sock;
35 struct socket* udp_sock;
37 struct sockaddr_in udp_dst[0];
40 static int cegw_udp2can(void *data);
41 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
42 struct sockaddr_in* addr);
43 static int cegw_can2udp(void *data);
44 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
45 static int cegw_thread_start(void *data);
46 static int cegw_thread_stop(struct cegw_job *job);
47 static void cegw_job_release(struct kref *ref);
49 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
54 __u16 type = CAN_FRAME;
57 mh.msg_namelen = sizeof(*addr);
58 mh.msg_control = NULL;
59 mh.msg_controllen = 0;
62 type = cpu_to_be16(type);
63 vec[0].iov_base = &type;
64 vec[0].iov_len = sizeof(type);
66 vec[1].iov_len = sizeof(*cf);
68 err = kernel_sendmsg(udp_sock, &mh, vec, 2, sizeof(type)+sizeof(*cf));
73 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
81 mh.msg_control = NULL;
82 mh.msg_controllen = 0;
86 vec.iov_len = sizeof(*cf);
88 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
94 * cegw_udp2can - performs udp->can routing
96 * This function is run as a thread.
98 static int cegw_udp2can(void *data)
103 struct cegw_job *job = (struct cegw_job *)data;
104 struct socket *udp_sock = NULL, *can_sock = NULL;
108 memset(&mh, 0, sizeof(mh));
109 udp_sock = job->udp_sock;
110 can_sock = job->can_sock;
113 vec[0].iov_base = &type;
114 vec[0].iov_len = sizeof(type);
115 vec[1].iov_base = &cf;
116 vec[1].iov_len = sizeof(cf);
117 ret = kernel_recvmsg(udp_sock, &mh, vec, 2,
118 sizeof(type) + sizeof(cf), 0);
119 if (ret != sizeof(type) + sizeof(cf))
122 type = be16_to_cpu(type);
125 cf.can_id = be32_to_cpu(cf.can_id);
126 cegw_can_send(can_sock, &cf);
131 cegw_thread_stop(job);
132 kref_put(&job->refcount, cegw_job_release);
137 * cegw_can2udp - performs can->udp routing
139 * This function is run as a thread.
141 static int cegw_can2udp(void* data)
146 struct cegw_job* job = (struct cegw_job*)data;
147 struct socket* udp_sock = job->udp_sock;
148 struct socket* can_sock = job->can_sock;
152 memset(&mh, 0, sizeof(mh));
156 vec.iov_len = sizeof(cf);
158 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
160 if (ret != sizeof(cf))
163 cf.can_id = cpu_to_be32(cf.can_id);
164 for (i=0; i<job->udp_dstcnt; i++) {
165 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
169 cegw_thread_stop(job);
170 kref_put(&job->refcount, cegw_job_release);
174 static void cegw_job_release(struct kref *ref)
176 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
178 fput(job->can_sock->file);
179 fput(job->udp_sock->file);
184 * cegw_thread_start - start working threads
185 * @data: (struct cegw_job *) with sockets and udp addresses filled in
187 * Two threads are started. One is serving udp->can routing and the other
190 static int cegw_thread_start(void *data)
192 struct task_struct *task = NULL;
193 struct cegw_job *job = (struct cegw_job *)data;
195 kref_init(&job->refcount);
197 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
199 kref_put(&job->refcount, cegw_job_release);
203 kref_get(&job->refcount);
204 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
206 cegw_thread_stop(job);
207 kref_put(&job->refcount, cegw_job_release);
215 * cegw_thread_stop - stops threads
217 static int cegw_thread_stop(struct cegw_job *job)
220 struct sock *sk = NULL;
221 struct socket *udp_sock = job->udp_sock;
222 struct socket *can_sock = job->can_sock;
224 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
226 /* PF_CAN sockets do not implement shutdown - do it manualy */
230 sk->sk_shutdown |= how;
231 sk->sk_state_change(sk);
237 static int cegw_open(struct inode *inode, struct file *file)
239 file->private_data = NULL;
241 if (try_module_get(THIS_MODULE) == false)
247 static int cegw_release(struct inode *inode, struct file *file)
249 struct cegw_job *job = (struct cegw_job *)file->private_data;
252 cegw_thread_stop(job);
255 module_put(THIS_MODULE);
260 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
262 * The function takes over cegw_ioctl structure from userspace and
263 * prepares cegw_job structure. The cegw_job is stored in
264 * file->private_data and used by kernel threads to serve gateway
267 static long cegw_ioctl_start(struct file *file, unsigned long arg)
273 struct cegw_ioctl gwctl;
274 struct cegw_job *job = NULL;
276 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
280 dstcnt = gwctl.udp_dstcnt;
281 addrlen = gwctl.udp_addrlen;
283 if (addrlen != sizeof(struct sockaddr_in))
284 return -EAFNOSUPPORT;
286 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
290 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
296 for (i=0; i<dstcnt; i++) {
297 if (job->udp_dst[i].sin_family != AF_INET) {
303 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
304 if (job->udp_sock == NULL)
307 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
308 if (job->can_sock == NULL)
311 if (job->can_sock->ops->family != AF_CAN ||
312 job->can_sock->type != SOCK_RAW) {
317 job->udp_dstcnt = dstcnt;
319 err = cegw_thread_start(job);
321 return err; /* cegw_thread_start performs cleaup for us. */
323 file->private_data = job;
327 fput(job->can_sock->file);
329 fput(job->udp_sock->file);
335 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
340 case CEGW_IOCTL_START:
341 err = cegw_ioctl_start(file, arg);
351 static const struct file_operations cegw_fops = {
352 .owner = THIS_MODULE,
354 .release = cegw_release,
355 .unlocked_ioctl = cegw_ioctl
358 static struct miscdevice cegw_device = {
359 .minor = MISC_DYNAMIC_MINOR,
364 static int __init cegw_init(void)
366 pr_info("can: can-eth gateway\n");
367 return misc_register(&cegw_device);
370 static void __exit cegw_exit(void)
372 misc_deregister(&cegw_device);
377 module_init(cegw_init);
378 module_exit(cegw_exit);