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;
41 static int cegw_udp2can(void *data);
42 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
43 struct sockaddr* addr, int addrlen);
44 static int cegw_can2udp(void *data);
45 static int cegw_can_send(struct socket *can_sock, struct can_frame *cf);
46 static int cegw_thread_start(void *data);
47 static int cegw_thread_stop(struct cegw_job *job);
48 static void cegw_job_release(struct kref *ref);
50 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr *addr,
56 __u16 type = CAN_FRAME;
59 mh.msg_namelen = addrlen;
60 mh.msg_control = NULL;
61 mh.msg_controllen = 0;
64 type = cpu_to_be16(type);
65 vec[0].iov_base = &type;
66 vec[0].iov_len = sizeof(type);
68 vec[1].iov_len = sizeof(*cf);
70 err = kernel_sendmsg(udp_sock, &mh, vec, 2, sizeof(type)+sizeof(*cf));
75 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
83 mh.msg_control = NULL;
84 mh.msg_controllen = 0;
88 vec.iov_len = sizeof(*cf);
90 err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
96 * cegw_udp2can - performs udp->can routing
98 * This function is run as a thread.
100 static int cegw_udp2can(void *data)
105 struct cegw_job *job = (struct cegw_job *)data;
106 struct socket *udp_sock = NULL, *can_sock = NULL;
110 memset(&mh, 0, sizeof(mh));
111 udp_sock = job->udp_sock;
112 can_sock = job->can_sock;
115 vec[0].iov_base = &type;
116 vec[0].iov_len = sizeof(type);
117 vec[1].iov_base = &cf;
118 vec[1].iov_len = sizeof(cf);
119 ret = kernel_recvmsg(udp_sock, &mh, vec, 2,
120 sizeof(type) + sizeof(cf), 0);
121 if (ret != sizeof(type) + sizeof(cf))
124 type = be16_to_cpu(type);
127 cf.can_id = be32_to_cpu(cf.can_id);
128 cegw_can_send(can_sock, &cf);
133 cegw_thread_stop(job);
134 kref_put(&job->refcount, cegw_job_release);
139 * cegw_can2udp - performs can->udp routing
141 * This function is run as a thread.
143 static int cegw_can2udp(void* data)
147 struct sockaddr *udst;
149 struct cegw_job* job = (struct cegw_job*)data;
150 struct socket* udp_sock = job->udp_sock;
151 struct socket* can_sock = job->can_sock;
155 memset(&mh, 0, sizeof(mh));
159 vec.iov_len = sizeof(cf);
161 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
163 if (ret != sizeof(cf))
166 cf.can_id = cpu_to_be32(cf.can_id);
167 for (i=0; i<job->udp_dstcnt; i++) {
168 udst = (struct sockaddr *)(job->udp_dst + i*job->udp_addrlen);
169 cegw_udp_send(udp_sock, &cf, udst, job->udp_addrlen);
173 cegw_thread_stop(job);
174 kref_put(&job->refcount, cegw_job_release);
178 static void cegw_job_release(struct kref *ref)
180 struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
182 fput(job->can_sock->file);
183 fput(job->udp_sock->file);
188 * cegw_thread_start - start working threads
189 * @data: (struct cegw_job *) with sockets and udp addresses filled in
191 * Two threads are started. One is serving udp->can routing and the other
194 static int cegw_thread_start(void *data)
196 struct task_struct *task = NULL;
197 struct cegw_job *job = (struct cegw_job *)data;
199 kref_init(&job->refcount);
201 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
203 kref_put(&job->refcount, cegw_job_release);
207 kref_get(&job->refcount);
208 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
210 cegw_thread_stop(job);
211 kref_put(&job->refcount, cegw_job_release);
219 * cegw_thread_stop - stops threads
221 static int cegw_thread_stop(struct cegw_job *job)
224 struct sock *sk = NULL;
225 struct socket *udp_sock = job->udp_sock;
226 struct socket *can_sock = job->can_sock;
228 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
230 /* PF_CAN sockets do not implement shutdown - do it manualy */
234 sk->sk_shutdown |= how;
235 sk->sk_state_change(sk);
241 static int cegw_open(struct inode *inode, struct file *file)
243 file->private_data = NULL;
245 if (try_module_get(THIS_MODULE) == false)
251 static int cegw_release(struct inode *inode, struct file *file)
253 struct cegw_job *job = (struct cegw_job *)file->private_data;
256 cegw_thread_stop(job);
259 module_put(THIS_MODULE);
264 * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
266 * The function takes over cegw_ioctl structure from userspace and
267 * prepares cegw_job structure. The cegw_job is stored in
268 * file->private_data and used by kernel threads to serve gateway
271 static long cegw_ioctl_start(struct file *file, unsigned long arg)
279 struct cegw_ioctl gwctl;
280 struct cegw_job *job = NULL;
282 err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
286 dstcnt = gwctl.udp_dstcnt;
287 addrlen = gwctl.udp_addrlen;
289 if (addrlen != sizeof(struct sockaddr_in) && addrlen != sizeof(struct sockaddr_in6))
290 return -EAFNOSUPPORT;
292 /* ToDo: consider dstcnt maximum */
293 job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen);
297 err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
305 sa = (struct sockaddr *)job->udp_dst;
306 chckfam = sa->sa_family;
308 for (i=1; i<dstcnt; i++) {
309 sa = (struct sockaddr *)(job->udp_dst + i*addrlen);
310 if (sa->sa_family != chckfam) {
316 job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
317 if (job->udp_sock == NULL)
320 job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
321 if (job->can_sock == NULL)
324 if (job->can_sock->ops->family != AF_CAN ||
325 job->can_sock->type != SOCK_RAW) {
330 job->udp_dstcnt = dstcnt;
331 job->udp_addrlen = addrlen;
333 err = cegw_thread_start(job);
335 return err; /* cegw_thread_start performs cleaup for us. */
337 file->private_data = job;
341 fput(job->can_sock->file);
343 fput(job->udp_sock->file);
349 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
354 case CEGW_IOCTL_START:
355 err = cegw_ioctl_start(file, arg);
365 static const struct file_operations cegw_fops = {
366 .owner = THIS_MODULE,
368 .release = cegw_release,
369 .unlocked_ioctl = cegw_ioctl,
370 .compat_ioctl = cegw_ioctl,
373 static struct miscdevice cegw_device = {
374 .minor = MISC_DYNAMIC_MINOR,
379 static int __init cegw_init(void)
381 pr_info("can: can-eth gateway\n");
382 return misc_register(&cegw_device);
385 static void __exit cegw_exit(void)
387 misc_deregister(&cegw_device);
392 module_init(cegw_init);
393 module_exit(cegw_exit);