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");
30 struct socket* can_sock;
31 struct socket* udp_sock;
33 struct sockaddr_in udp_dst[0];
36 static int cegw_udp2can(void *data);
37 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
38 struct sockaddr_in* addr);
39 static int cegw_can2udp(void *data);
40 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
41 static int cegw_thread_start(void *data);
42 static int cegw_thread_stop(struct cegw_job *job);
43 static void cegw_job_release(struct kref *ref);
45 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
52 mh.msg_namelen = sizeof(*addr);
53 mh.msg_control = NULL;
54 mh.msg_controllen = 0;
58 vec.iov_len = sizeof(*cf);
60 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
65 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
73 mh.msg_control = NULL;
74 mh.msg_controllen = 0;
78 vec.iov_len = sizeof(*cf);
80 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
86 * cegw_udp2can - performs udp->can routing
88 * This function is run as a thread.
90 static int cegw_udp2can(void *data)
95 struct cegw_job *job = (struct cegw_job *)data;
96 struct socket *udp_sock = NULL, *can_sock = NULL;
99 memset(&mh, 0, sizeof(mh));
100 udp_sock = job->udp_sock;
101 can_sock = job->can_sock;
105 vec.iov_len = sizeof(cf);
106 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
111 cf.can_id = be32_to_cpu(cf.can_id);
112 cegw_can_send(can_sock, &cf);
115 cegw_thread_stop(job);
116 kref_put(&job->refcount, cegw_job_release);
121 * cegw_can2udp - performs can->udp routing
123 * This function is run as a thread.
125 static int cegw_can2udp(void* data)
130 struct cegw_job* job = (struct cegw_job*)data;
131 struct socket* udp_sock = job->udp_sock;
132 struct socket* can_sock = job->can_sock;
136 memset(&mh, 0, sizeof(mh));
140 vec.iov_len = sizeof(cf);
142 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
144 if (ret != sizeof(cf))
147 cf.can_id = cpu_to_be32(cf.can_id);
148 for (i=0; i<job->udp_dstcnt; i++) {
149 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
153 cegw_thread_stop(job);
154 kref_put(&job->refcount, cegw_job_release);
158 static void cegw_job_release(struct kref *ref)
160 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
162 fput(job->can_sock->file);
163 fput(job->udp_sock->file);
168 * cegw_thread_start - start working threads
169 * @data: (struct cegw_job *) with sockets and udp addresses filled in
171 * Two threads are started. One is serving udp->can routing and the other
174 static int cegw_thread_start(void *data)
176 struct task_struct *task = NULL;
177 struct cegw_job *job = (struct cegw_job *)data;
179 kref_init(&job->refcount);
181 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
183 kref_put(&job->refcount, cegw_job_release);
187 kref_get(&job->refcount);
188 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
190 cegw_thread_stop(job);
191 kref_put(&job->refcount, cegw_job_release);
199 * cegw_thread_stop - stops threads
201 static int cegw_thread_stop(struct cegw_job *job)
204 struct sock *sk = NULL;
205 struct socket *udp_sock = job->udp_sock;
206 struct socket *can_sock = job->can_sock;
208 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
210 /* PF_CAN sockets do not implement shutdown - do it manualy */
214 sk->sk_shutdown |= how;
215 sk->sk_state_change(sk);
221 static int cegw_open(struct inode *inode, struct file *file)
223 file->private_data = NULL;
225 if (try_module_get(THIS_MODULE) == false)
231 static int cegw_release(struct inode *inode, struct file *file)
233 struct cegw_job *job = (struct cegw_job *)file->private_data;
236 cegw_thread_stop(job);
239 module_put(THIS_MODULE);
244 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
246 * The function takes over cegw_ioctl structure from userspace and
247 * prepares cegw_job structure. The cegw_job is stored in
248 * file->private_data and used by kernel threads to serve gateway
251 static long cegw_ioctl_start(struct file *file, unsigned long arg)
257 struct cegw_ioctl gwctl;
258 struct cegw_job *job = NULL;
260 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
264 dstcnt = gwctl.udp_dstcnt;
265 addrlen = gwctl.udp_addrlen;
267 if (addrlen != sizeof(struct sockaddr_in))
268 return -EAFNOSUPPORT;
270 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
274 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
280 for (i=0; i<dstcnt; i++) {
281 if (job->udp_dst[i].sin_family != AF_INET) {
283 return -EAFNOSUPPORT;
287 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
288 if (job->udp_sock == NULL) {
293 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
294 if (job->can_sock == NULL) {
295 fput(job->udp_sock->file);
300 if (job->can_sock->ops->family != AF_CAN ||
301 job->can_sock->type != CAN_RAW) {
306 job->udp_dstcnt = dstcnt;
308 err = cegw_thread_start(job);
312 file->private_data = job;
316 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
321 case CEGW_IOCTL_START:
322 err = cegw_ioctl_start(file, arg);
332 static const struct file_operations cegw_fops = {
333 .owner = THIS_MODULE,
335 .release = cegw_release,
336 .unlocked_ioctl = cegw_ioctl
339 static struct miscdevice cegw_device = {
340 .minor = MISC_DYNAMIC_MINOR,
345 static int __init cegw_init(void)
347 return misc_register(&cegw_device);
350 static void __exit cegw_exit(void)
352 misc_deregister(&cegw_device);
357 module_init(cegw_init);
358 module_exit(cegw_exit);