]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - kernel/canethgw.c
authors and license information added
[can-eth-gw.git] / kernel / canethgw.c
1 /*
2  * Copyright: (c) 2012 Czech Technical University in Prague
3  *
4  * Authors:
5  *      Radek Matějka <radek.matejka@gmail.com>
6  *      Michal Sojka  <sojkam1@fel.cvut.cz>
7  *
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.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/kthread.h>
17 #include <linux/file.h>
18 #include <net/sock.h>
19 #include <linux/can.h>
20 #include <linux/miscdevice.h>
21 #include "canethgw.h"
22
23 MODULE_LICENSE("GPL");
24
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);
33
34 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
35 {
36         struct msghdr mh;
37         struct kvec vec;
38         int err;
39
40         mh.msg_name = addr;
41         mh.msg_namelen = sizeof(*addr);
42         mh.msg_control = NULL;
43         mh.msg_controllen = 0;
44         mh.msg_flags = 0;
45
46         vec.iov_base = cf;
47         vec.iov_len = sizeof(*cf);
48
49         err = kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
50
51         return err;
52 }
53
54 static int cegw_can_send(struct socket* can_sock, struct can_frame* cf)
55 {
56         struct msghdr mh;
57         struct kvec vec;
58         int err;
59
60         mh.msg_name = NULL;
61         mh.msg_namelen = 0;
62         mh.msg_control = NULL;
63         mh.msg_controllen = 0;
64         mh.msg_flags = 0;
65
66         vec.iov_base = cf;
67         vec.iov_len = sizeof(*cf);
68
69         err = kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
70
71         return err;
72 }
73
74 /**
75  * cegw_udp2can - performs udp->can routing
76  *
77  * This function is run as a thread.
78  */
79 static int cegw_udp2can(void *data)
80 {
81         struct can_frame cf;
82         struct kvec vec;
83         struct msghdr mh;
84         struct cegw_job *job = (struct cegw_job *)data;
85         struct socket *udp_sock = NULL, *can_sock = NULL;
86         int ret = 0;
87
88         memset(&mh, 0, sizeof(mh));
89         udp_sock = job->udp_sock;
90         can_sock = job->can_sock;
91
92         while (1) {
93                 vec.iov_base = &cf;
94                 vec.iov_len = sizeof(cf);
95                 ret = kernel_recvmsg(udp_sock, &mh, &vec, 1,
96                                 sizeof(cf), 0);
97                 if (ret < 1)
98                         break;
99
100                 cf.can_id = be32_to_cpu(cf.can_id);
101                 cegw_can_send(can_sock, &cf);
102         }
103
104         cegw_thread_stop(job);
105         kref_put(&job->refcount, cegw_job_release);
106         do_exit(ret);
107 }
108
109 /**
110  * cegw_can2udp - performs can->udp routing
111  *
112  * This function is run as a thread.
113  */
114 static int cegw_can2udp(void* data)
115 {
116         struct msghdr mh;
117         struct kvec vec;
118         struct can_frame cf;
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;
122         int i;
123         int ret;
124
125         memset(&mh, 0, sizeof(mh));
126
127         while (1) {
128                 vec.iov_base = &cf;
129                 vec.iov_len = sizeof(cf);
130
131                 ret = kernel_recvmsg(can_sock, &mh, &vec, 1,
132                                            sizeof(cf), 0);
133                 if (ret < 1)
134                         break;
135
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]);
139                 }
140         }
141
142         cegw_thread_stop(job);
143         kref_put(&job->refcount, cegw_job_release);
144         do_exit(ret);
145 }
146
147 static void cegw_job_release(struct kref *ref)
148 {
149         struct cegw_job *job = container_of(ref, struct cegw_job, refcount);
150
151         fput(job->can_sock->file);
152         fput(job->udp_sock->file);
153         kfree(job);
154 }
155
156 /**
157  * cegw_thread_start - start working threads
158  * @data: (struct cegw_job *) with sockets and udp addresses filled in
159  *
160  * Two threads are started. One is serving udp->can routing and the other
161  * can->udp.
162  */
163 static int cegw_thread_start(void *data)
164 {
165         struct task_struct *task = NULL;
166         struct cegw_job *job = (struct cegw_job *)data;
167
168         kref_init(&job->refcount);
169         kref_get(&job->refcount);
170
171         task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
172         if (IS_ERR(task)) {
173                 kref_sub(&job->refcount, 2, cegw_job_release);
174                 return -ENOMEM;
175         }
176
177         task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
178         if (IS_ERR(task)) {
179                 cegw_thread_stop(job);
180                 kref_put(&job->refcount, cegw_job_release);
181                 return -ENOMEM;
182         }
183
184         return 0;
185 }
186
187 /**
188  * cegw_thread_stop - stops threads
189  */
190 static int cegw_thread_stop(struct cegw_job *job)
191 {
192         int how = SHUT_RDWR;
193         struct sock *sk = NULL;
194         struct socket *udp_sock = job->udp_sock;
195         struct socket *can_sock = job->can_sock;
196
197         kernel_sock_shutdown(udp_sock, SHUT_RDWR);
198
199         /* PF_CAN sockets do not implement shutdown - do it manualy */
200         sk = can_sock->sk;
201         how++;
202         lock_sock(sk);
203         sk->sk_shutdown |= how;
204         sk->sk_state_change(sk);
205         release_sock(sk);
206
207         return 0;
208 }
209
210 static int cegw_open(struct inode *inode, struct file *file)
211 {
212         file->private_data = NULL;
213
214         if (try_module_get(THIS_MODULE) == false)
215                 return -EAGAIN;
216
217         return 0;
218 }
219
220 static int cegw_release(struct inode *inode, struct file *file)
221 {
222         struct cegw_job *job = (struct cegw_job *)file->private_data;
223
224         if (job) {
225                 cegw_thread_stop(job);
226         }
227
228         module_put(THIS_MODULE);
229         return 0;
230 }
231
232 /**
233  * cegw_ioctl_start - processes ioctl CEGW_IOCTL_START call
234  *
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
238  * functionality.
239  */
240 static long cegw_ioctl_start(struct file *file, unsigned long arg)
241 {
242         int i;
243         int err = 0;
244         __u32 dstcnt = 0;
245         __u32 addrlen = 0;
246         struct cegw_ioctl gwctl;
247         struct cegw_job *job = NULL;
248
249         err = copy_from_user(&gwctl, (void __user *)arg, sizeof(gwctl));
250         if (err != 0)
251                 return -EFAULT;
252
253         dstcnt = gwctl.udp_dstcnt;
254         addrlen = gwctl.udp_addrlen;
255
256         if (addrlen != sizeof(struct sockaddr_in))
257                 return -EAFNOSUPPORT;
258
259         /* */
260         job = kmalloc(GFP_KERNEL, sizeof(*job) + dstcnt*addrlen );
261         if (job == NULL)
262                 return -ENOMEM;
263
264         err = copy_from_user(&job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
265         if (err != 0) {
266                 kfree(job);
267                 return -EFAULT;
268         }
269
270         for (i=0; i<dstcnt; i++) {
271                 if (job->udp_dst[i].sin_family != AF_INET) {
272                         kfree(job);
273                         return -EAFNOSUPPORT;
274                 }
275         }
276
277         job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
278         if (job->udp_sock == NULL) {
279                 kfree(job);
280                 return err;
281         }
282
283         job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
284         if (job->can_sock == NULL) {
285                 fput(job->udp_sock->file);
286                 kfree(job);
287                 return err;
288         }
289
290         job->udp_dstcnt = dstcnt;
291
292         err = cegw_thread_start(job);
293         if (err != 0)
294                 return err;
295
296         file->private_data = job;
297         return 0;
298 }
299
300 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
301 {
302         int err;
303
304         switch (cmd) {
305                 case CEGW_IOCTL_START:
306                         err = cegw_ioctl_start(file, arg);
307                         break;
308                 default:
309                         err = -EOPNOTSUPP;
310                         break;
311         }
312
313         return err;
314 }
315
316 static const struct file_operations cegw_fops = {
317         .owner = THIS_MODULE,
318         .open = cegw_open,
319         .release = cegw_release,
320         .unlocked_ioctl = cegw_ioctl
321 };
322
323 static struct miscdevice cegw_device = {
324         .minor = MISC_DYNAMIC_MINOR,
325         .name = "canethgw",
326         .fops = &cegw_fops
327 };
328
329 static int __init cegw_init(void)
330 {
331         misc_register(&cegw_device);
332
333         return 0;
334 }
335
336 static void __exit cegw_exit(void)
337 {
338         misc_deregister(&cegw_device);
339
340         return;
341 }
342
343 module_init(cegw_init);
344 module_exit(cegw_exit);
345