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;
65 vec[1].iov_len = sizeof(*cf);
67 err = kernel_sendmsg(udp_sock, &mh, vec, 2, sizeof(*cf));
72 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
80 mh.msg_control = NULL;
81 mh.msg_controllen = 0;
85 vec.iov_len = sizeof(*cf);
87 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
93 * cegw_udp2can - performs udp->can routing
95 * This function is run as a thread.
97 static int cegw_udp2can(void *data)
102 struct cegw_job *job = (struct cegw_job *)data;
103 struct socket *udp_sock = NULL, *can_sock = NULL;
107 memset(&mh, 0, sizeof(mh));
108 udp_sock = job->udp_sock;
109 can_sock = job->can_sock;
112 vec[0].iov_base = &type;
113 vec[0].iov_len = sizeof(type);
114 vec[1].iov_base = &cf;
115 vec[1].iov_len = sizeof(cf);
116 ret = kernel_recvmsg(udp_sock, &mh, vec, 2,
118 if (ret != sizeof(type) + sizeof(cf))
121 type = be16_to_cpu(type);
124 cf.can_id = be32_to_cpu(cf.can_id);
125 cegw_can_send(can_sock, &cf);
130 cegw_thread_stop(job);
131 kref_put(&job->refcount, cegw_job_release);
136 * cegw_can2udp - performs can->udp routing
138 * This function is run as a thread.
140 static int cegw_can2udp(void* data)
145 struct cegw_job* job = (struct cegw_job*)data;
146 struct socket* udp_sock = job->udp_sock;
147 struct socket* can_sock = job->can_sock;
151 memset(&mh, 0, sizeof(mh));
155 vec.iov_len = sizeof(cf);
157 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
159 if (ret != sizeof(cf))
162 cf.can_id = cpu_to_be32(cf.can_id);
163 for (i=0; i<job->udp_dstcnt; i++) {
164 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
168 cegw_thread_stop(job);
169 kref_put(&job->refcount, cegw_job_release);
173 static void cegw_job_release(struct kref *ref)
175 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
177 fput(job->can_sock->file);
178 fput(job->udp_sock->file);
183 * cegw_thread_start - start working threads
184 * @data: (struct cegw_job *) with sockets and udp addresses filled in
186 * Two threads are started. One is serving udp->can routing and the other
189 static int cegw_thread_start(void *data)
191 struct task_struct *task = NULL;
192 struct cegw_job *job = (struct cegw_job *)data;
194 kref_init(&job->refcount);
196 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
198 kref_put(&job->refcount, cegw_job_release);
202 kref_get(&job->refcount);
203 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
205 cegw_thread_stop(job);
206 kref_put(&job->refcount, cegw_job_release);
214 * cegw_thread_stop - stops threads
216 static int cegw_thread_stop(struct cegw_job *job)
219 struct sock *sk = NULL;
220 struct socket *udp_sock = job->udp_sock;
221 struct socket *can_sock = job->can_sock;
223 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
225 /* PF_CAN sockets do not implement shutdown - do it manualy */
229 sk->sk_shutdown |= how;
230 sk->sk_state_change(sk);
236 static int cegw_open(struct inode *inode, struct file *file)
238 file->private_data = NULL;
240 if (try_module_get(THIS_MODULE) == false)
246 static int cegw_release(struct inode *inode, struct file *file)
248 struct cegw_job *job = (struct cegw_job *)file->private_data;
251 cegw_thread_stop(job);
254 module_put(THIS_MODULE);
259 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
261 * The function takes over cegw_ioctl structure from userspace and
262 * prepares cegw_job structure. The cegw_job is stored in
263 * file->private_data and used by kernel threads to serve gateway
266 static long cegw_ioctl_start(struct file *file, unsigned long arg)
272 struct cegw_ioctl gwctl;
273 struct cegw_job *job = NULL;
275 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
279 dstcnt = gwctl.udp_dstcnt;
280 addrlen = gwctl.udp_addrlen;
282 if (addrlen != sizeof(struct sockaddr_in))
283 return -EAFNOSUPPORT;
285 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
289 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
295 for (i=0; i<dstcnt; i++) {
296 if (job->udp_dst[i].sin_family != AF_INET) {
302 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
303 if (job->udp_sock == NULL)
306 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
307 if (job->can_sock == NULL)
310 if (job->can_sock->ops->family != AF_CAN ||
311 job->can_sock->type != SOCK_RAW) {
316 job->udp_dstcnt = dstcnt;
318 err = cegw_thread_start(job);
320 return err; /* cegw_thread_start performs cleaup for us. */
322 file->private_data = job;
326 fput(job->can_sock->file);
328 fput(job->udp_sock->file);
334 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
339 case CEGW_IOCTL_START:
340 err = cegw_ioctl_start(file, arg);
350 static const struct file_operations cegw_fops = {
351 .owner = THIS_MODULE,
353 .release = cegw_release,
354 .unlocked_ioctl = cegw_ioctl
357 static struct miscdevice cegw_device = {
358 .minor = MISC_DYNAMIC_MINOR,
363 static int __init cegw_init(void)
365 pr_info("can: can-eth gateway\n");
366 return misc_register(&cegw_device);
369 static void __exit cegw_exit(void)
371 misc_deregister(&cegw_device);
376 module_init(cegw_init);
377 module_exit(cegw_exit);