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