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");
27 static int cegw_udp2can(void *data);
28 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
29 struct sockaddr_in* addr);
30 static int cegw_can2udp(void *data);
31 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
32 static int cegw_thread_start(void *data);
33 static int cegw_thread_stop(struct cegw_job *job);
34 static void cegw_job_release(struct kref *ref);
36 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
43 mh.msg_namelen = sizeof(*addr);
44 mh.msg_control = NULL;
45 mh.msg_controllen = 0;
49 vec.iov_len = sizeof(*cf);
51 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
56 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
64 mh.msg_control = NULL;
65 mh.msg_controllen = 0;
69 vec.iov_len = sizeof(*cf);
71 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
77 * cegw_udp2can - performs udp->can routing
79 * This function is run as a thread.
81 static int cegw_udp2can(void *data)
86 struct cegw_job *job = (struct cegw_job *)data;
87 struct socket *udp_sock = NULL, *can_sock = NULL;
90 memset(&mh, 0, sizeof(mh));
91 udp_sock = job->udp_sock;
92 can_sock = job->can_sock;
96 vec.iov_len = sizeof(cf);
97 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
102 cf.can_id = be32_to_cpu(cf.can_id);
103 cegw_can_send(can_sock, &cf);
106 cegw_thread_stop(job);
107 kref_put(&job->refcount, cegw_job_release);
112 * cegw_can2udp - performs can->udp routing
114 * This function is run as a thread.
116 static int cegw_can2udp(void* data)
121 struct cegw_job* job = (struct cegw_job*)data;
122 struct socket* udp_sock = job->udp_sock;
123 struct socket* can_sock = job->can_sock;
127 memset(&mh, 0, sizeof(mh));
131 vec.iov_len = sizeof(cf);
133 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
138 cf.can_id = cpu_to_be32(cf.can_id);
139 for (i=0; i<job->udp_dstcnt; i++) {
140 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
144 cegw_thread_stop(job);
145 kref_put(&job->refcount, cegw_job_release);
149 static void cegw_job_release(struct kref *ref)
151 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
153 fput(job->can_sock->file);
154 fput(job->udp_sock->file);
159 * cegw_thread_start - start working threads
160 * @data: (struct cegw_job *) with sockets and udp addresses filled in
162 * Two threads are started. One is serving udp->can routing and the other
165 static int cegw_thread_start(void *data)
167 struct task_struct *task = NULL;
168 struct cegw_job *job = (struct cegw_job *)data;
170 kref_init(&job->refcount);
171 kref_get(&job->refcount);
173 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
175 kref_sub(&job->refcount, 2, cegw_job_release);
179 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
181 cegw_thread_stop(job);
182 kref_put(&job->refcount, cegw_job_release);
190 * cegw_thread_stop - stops threads
192 static int cegw_thread_stop(struct cegw_job *job)
195 struct sock *sk = NULL;
196 struct socket *udp_sock = job->udp_sock;
197 struct socket *can_sock = job->can_sock;
199 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
201 /* PF_CAN sockets do not implement shutdown - do it manualy */
205 sk->sk_shutdown |= how;
206 sk->sk_state_change(sk);
212 static int cegw_open(struct inode *inode, struct file *file)
214 file->private_data = NULL;
216 if (try_module_get(THIS_MODULE) == false)
222 static int cegw_release(struct inode *inode, struct file *file)
224 struct cegw_job *job = (struct cegw_job *)file->private_data;
227 cegw_thread_stop(job);
230 module_put(THIS_MODULE);
235 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
237 * The function takes over cegw_ioctl structure from userspace and
238 * prepares cegw_job structure. The cegw_job is stored in
239 * file->private_data and used by kernel threads to serve gateway
242 static long cegw_ioctl_start(struct file *file, unsigned long arg)
248 struct cegw_ioctl gwctl;
249 struct cegw_job *job = NULL;
251 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
255 dstcnt = gwctl.udp_dstcnt;
256 addrlen = gwctl.udp_addrlen;
258 if (addrlen != sizeof(struct sockaddr_in))
259 return -EAFNOSUPPORT;
262 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
266 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
272 for (i=0; i<dstcnt; i++) {
273 if (job->udp_dst[i].sin_family != AF_INET) {
275 return -EAFNOSUPPORT;
279 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
280 if (job->udp_sock == NULL) {
285 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
286 if (job->can_sock == NULL) {
287 fput(job->udp_sock->file);
292 job->udp_dstcnt = dstcnt;
294 err = cegw_thread_start(job);
298 file->private_data = job;
302 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
307 case CEGW_IOCTL_START:
308 err = cegw_ioctl_start(file, arg);
318 static const struct file_operations cegw_fops = {
319 .owner = THIS_MODULE,
321 .release = cegw_release,
322 .unlocked_ioctl = cegw_ioctl
325 static struct miscdevice cegw_device = {
326 .minor = MISC_DYNAMIC_MINOR,
331 static int __init cegw_init(void)
333 return misc_register(&cegw_device);
336 static void __exit cegw_exit(void)
338 misc_deregister(&cegw_device);
343 module_init(cegw_init);
344 module_exit(cegw_exit);