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>
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* addr, int addrlen);
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 *addr,
44 mh.msg_namelen = addrlen;
45 mh.msg_control = NULL;
46 mh.msg_controllen = 0;
50 vec.iov_len = sizeof(*cf);
52 err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
57 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
65 mh.msg_control = NULL;
66 mh.msg_controllen = 0;
70 vec.iov_len = sizeof(*cf);
72 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
78 * cegw_udp2can - performs udp->can routing
80 * This function is run as a thread.
82 static int cegw_udp2can(void *data)
87 struct cegw_job *job = (struct cegw_job *)data;
88 struct socket *udp_sock = NULL, *can_sock = NULL;
91 memset(&mh, 0, sizeof(mh));
92 udp_sock = job->udp_sock;
93 can_sock = job->can_sock;
97 vec.iov_len = sizeof(cf);
98 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
103 cf.can_id = be32_to_cpu(cf.can_id);
104 cegw_can_send(can_sock, &cf);
107 cegw_thread_stop(job);
108 kref_put(&job->refcount, cegw_job_release);
113 * cegw_can2udp - performs can->udp routing
115 * This function is run as a thread.
117 static int cegw_can2udp(void* data)
121 struct sockaddr *udst;
123 struct cegw_job* job = (struct cegw_job*)data;
124 struct socket* udp_sock = job->udp_sock;
125 struct socket* can_sock = job->can_sock;
129 memset(&mh, 0, sizeof(mh));
133 vec.iov_len = sizeof(cf);
135 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
140 cf.can_id = cpu_to_be32(cf.can_id);
141 for (i=0; i<job->udp_dstcnt; i++) {
142 udst = (struct sockaddr *)(job->udp_dst + i*job->udp_addrlen);
143 cegw_udp_send(udp_sock, &cf, udst, job->udp_addrlen);
147 cegw_thread_stop(job);
148 kref_put(&job->refcount, cegw_job_release);
152 static void cegw_job_release(struct kref *ref)
154 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
156 fput(job->can_sock->file);
157 fput(job->udp_sock->file);
162 * cegw_thread_start - start working threads
163 * @data: (struct cegw_job *) with sockets and udp addresses filled in
165 * Two threads are started. One is serving udp->can routing and the other
168 static int cegw_thread_start(void *data)
170 struct task_struct *task = NULL;
171 struct cegw_job *job = (struct cegw_job *)data;
173 kref_init(&job->refcount);
174 kref_get(&job->refcount);
176 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
178 kref_sub(&job->refcount, 2, cegw_job_release);
182 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
184 cegw_thread_stop(job);
185 kref_put(&job->refcount, cegw_job_release);
193 * cegw_thread_stop - stops threads
195 static int cegw_thread_stop(struct cegw_job *job)
198 struct sock *sk = NULL;
199 struct socket *udp_sock = job->udp_sock;
200 struct socket *can_sock = job->can_sock;
202 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
204 /* PF_CAN sockets do not implement shutdown - do it manualy */
208 sk->sk_shutdown |= how;
209 sk->sk_state_change(sk);
215 static int cegw_open(struct inode *inode, struct file *file)
217 file->private_data = NULL;
219 if (try_module_get(THIS_MODULE) == false)
225 static int cegw_release(struct inode *inode, struct file *file)
227 struct cegw_job *job = (struct cegw_job *)file->private_data;
230 cegw_thread_stop(job);
233 module_put(THIS_MODULE);
238 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
240 * The function takes over cegw_ioctl structure from userspace and
241 * prepares cegw_job structure. The cegw_job is stored in
242 * file->private_data and used by kernel threads to serve gateway
245 static long cegw_ioctl_start(struct file *file, unsigned long arg)
253 struct cegw_ioctl gwctl;
254 struct cegw_job *job = NULL;
256 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
260 dstcnt = gwctl.udp_dstcnt;
261 addrlen = gwctl.udp_addrlen;
263 if (addrlen != sizeof(struct sockaddr_in) && addrlen != sizeof(struct sockaddr_in6))
264 return -EAFNOSUPPORT;
266 /* ToDo: consider dstcnt maximum */
267 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen);
271 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
279 sa = (struct sockaddr *)job->udp_dst;
280 chckfam = sa->sa_family;
282 for (i=1; i<dstcnt; i++) {
283 sa = (struct sockaddr *)(job->udp_dst + i*addrlen);
284 if (sa->sa_family != chckfam) {
286 return -EAFNOSUPPORT;
290 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
291 if (job->udp_sock == NULL) {
296 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
297 if (job->can_sock == NULL) {
298 fput(job->udp_sock->file);
303 job->udp_dstcnt = dstcnt;
304 job->udp_addrlen = addrlen;
306 err = cegw_thread_start(job);
310 file->private_data = job;
314 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
319 case CEGW_IOCTL_START:
320 err = cegw_ioctl_start(file, arg);
330 static const struct file_operations cegw_fops = {
331 .owner = THIS_MODULE,
333 .release = cegw_release,
334 .unlocked_ioctl = cegw_ioctl
337 static struct miscdevice cegw_device = {
338 .minor = MISC_DYNAMIC_MINOR,
343 static int __init cegw_init(void)
345 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);