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 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/kthread.h>
17 #include <linux/file.h>
19 #include <linux/can.h>
20 #include <linux/miscdevice.h>
23 MODULE_LICENSE("GPL");
25 static int cegw_udp2can(void *data);
26 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
27 struct sockaddr_in* addr);
28 static int cegw_can2udp(void *data);
29 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
30 static int cegw_thread_start(void *data);
31 static int cegw_thread_stop(struct cegw_job *job);
32 static void cegw_job_release(struct kref *ref);
34 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
41 mh.msg_namelen = sizeof(*addr);
42 mh.msg_control = NULL;
43 mh.msg_controllen = 0;
47 vec.iov_len = sizeof(*cf);
49 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
54 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
62 mh.msg_control = NULL;
63 mh.msg_controllen = 0;
67 vec.iov_len = sizeof(*cf);
69 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
75 * cegw_udp2can - performs udp->can routing
77 * This function is run as a thread.
79 static int cegw_udp2can(void *data)
84 struct cegw_job *job = (struct cegw_job *)data;
85 struct socket *udp_sock = NULL, *can_sock = NULL;
88 memset(&mh, 0, sizeof(mh));
89 udp_sock = job->udp_sock;
90 can_sock = job->can_sock;
94 vec.iov_len = sizeof(cf);
95 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
100 cf.can_id = be32_to_cpu(cf.can_id);
101 cegw_can_send(can_sock, &cf);
104 cegw_thread_stop(job);
105 kref_put(&job->refcount, cegw_job_release);
110 * cegw_can2udp - performs can->udp routing
112 * This function is run as a thread.
114 static int cegw_can2udp(void* data)
119 struct cegw_job* job = (struct cegw_job*)data;
120 struct socket* udp_sock = job->udp_sock;
121 struct socket* can_sock = job->can_sock;
125 memset(&mh, 0, sizeof(mh));
129 vec.iov_len = sizeof(cf);
131 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
136 cf.can_id = cpu_to_be32(cf.can_id);
137 for (i=0; i<job->udp_dstcnt; i++) {
138 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
142 cegw_thread_stop(job);
143 kref_put(&job->refcount, cegw_job_release);
147 static void cegw_job_release(struct kref *ref)
149 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
151 fput(job->can_sock->file);
152 fput(job->udp_sock->file);
157 * cegw_thread_start - start working threads
158 * @data: (struct cegw_job *) with sockets and udp addresses filled in
160 * Two threads are started. One is serving udp->can routing and the other
163 static int cegw_thread_start(void *data)
165 struct task_struct *task = NULL;
166 struct cegw_job *job = (struct cegw_job *)data;
168 kref_init(&job->refcount);
169 kref_get(&job->refcount);
171 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
173 kref_sub(&job->refcount, 2, cegw_job_release);
177 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
179 cegw_thread_stop(job);
180 kref_put(&job->refcount, cegw_job_release);
188 * cegw_thread_stop - stops threads
190 static int cegw_thread_stop(struct cegw_job *job)
193 struct sock *sk = NULL;
194 struct socket *udp_sock = job->udp_sock;
195 struct socket *can_sock = job->can_sock;
197 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
199 /* PF_CAN sockets do not implement shutdown - do it manualy */
203 sk->sk_shutdown |= how;
204 sk->sk_state_change(sk);
210 static int cegw_open(struct inode *inode, struct file *file)
212 file->private_data = NULL;
214 if (try_module_get(THIS_MODULE) == false)
220 static int cegw_release(struct inode *inode, struct file *file)
222 struct cegw_job *job = (struct cegw_job *)file->private_data;
225 cegw_thread_stop(job);
228 module_put(THIS_MODULE);
233 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
235 * The function takes over cegw_ioctl structure from userspace and
236 * prepares cegw_job structure. The cegw_job is stored in
237 * file->private_data and used by kernel threads to serve gateway
240 static long cegw_ioctl_start(struct file *file, unsigned long arg)
246 struct cegw_ioctl gwctl;
247 struct cegw_job *job = NULL;
249 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
253 dstcnt = gwctl.udp_dstcnt;
254 addrlen = gwctl.udp_addrlen;
256 if (addrlen != sizeof(struct sockaddr_in))
257 return -EAFNOSUPPORT;
260 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
264 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
270 for (i=0; i<dstcnt; i++) {
271 if (job->udp_dst[i].sin_family != AF_INET) {
273 return -EAFNOSUPPORT;
277 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
278 if (job->udp_sock == NULL) {
283 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
284 if (job->can_sock == NULL) {
285 fput(job->udp_sock->file);
290 job->udp_dstcnt = dstcnt;
292 err = cegw_thread_start(job);
296 file->private_data = job;
300 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
305 case CEGW_IOCTL_START:
306 err = cegw_ioctl_start(file, arg);
316 static const struct file_operations cegw_fops = {
317 .owner = THIS_MODULE,
319 .release = cegw_release,
320 .unlocked_ioctl = cegw_ioctl
323 static struct miscdevice cegw_device = {
324 .minor = MISC_DYNAMIC_MINOR,
329 static int __init cegw_init(void)
331 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);