]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - kernel/canethgw.c
canethgw micsdevice
[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/socket.h>
11 #include <linux/if_arp.h>
12 #include <linux/net.h>
13 #include <linux/netdevice.h>
14 #include <linux/can/core.h>
15 #include <linux/can.h>
16 #include <net/rtnetlink.h>
17 #include <net/sock.h>
18 #include "canethgw.h"
19 #include <linux/completion.h>
20 #include <linux/mutex.h>
21 #include <linux/miscdevice.h>
22 #include <net/inet_common.h>
23
24 MODULE_LICENSE("GPL");
25
26 static int  cegw_udp2can(void *data);
27 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
28                           struct in_addr ipaddr, u16 port);
29 static int  cegw_can2udp(void *data);
30 static void cegw_can_send(struct socket *can_sock, struct can_frame *cf,
31                           int ifindex);
32 static int cegw_thread_start(void *data);
33 static int cegw_thread_stop(void);
34
35 enum __cegw_state {
36         CEGW_RUN,
37         CEGW_STOP,
38         CEGW_EXIT
39 };
40
41 struct cegw_rule {
42         int can_ifindex;
43         struct in_addr eth_ip;
44         unsigned short eth_port;
45         struct hlist_node list;
46 };
47
48 struct cegw_setting {
49         struct in_addr eth_ip;
50         unsigned short eth_port;
51 };
52
53
54 static int cegw_state = CEGW_STOP;
55 static struct socket *can_sock = NULL, *udp_sock = NULL;
56 static struct task_struct *eth_to_can = NULL, *can_to_eth = NULL;
57 static struct notifier_block notifier;
58
59 static HLIST_HEAD(rule_eth_can);
60 static HLIST_HEAD(rule_can_eth);
61 static DEFINE_MUTEX(rule_eth_can_mutex);
62 static DEFINE_MUTEX(rule_can_eth_mutex);
63 static DEFINE_MUTEX(cegw_mutex);
64
65 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
66                           struct in_addr ipaddr, u16 port)
67 {
68         struct msghdr mh;
69         struct sockaddr_in addr;
70         struct kvec vec;
71
72         addr.sin_family = AF_INET;
73         addr.sin_port = htons(port);
74         addr.sin_addr = ipaddr;
75
76         mh.msg_name = &addr;
77         mh.msg_namelen = sizeof(addr);
78         mh.msg_control = NULL;
79         mh.msg_controllen = 0;
80         mh.msg_flags = 0;
81
82         vec.iov_base = cf;
83         vec.iov_len = sizeof(*cf);
84
85         /* FIXME: Convert endianing of cf->can_id */
86         kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
87 }
88
89 static void cegw_can_send(struct socket* can_sock, struct can_frame* cf,
90                 int ifindex)
91 {
92         struct msghdr mh;
93         struct kvec vec;
94         struct sockaddr_can addr;
95
96         addr.can_family = AF_CAN;
97         addr.can_ifindex = ifindex;
98
99         mh.msg_name = &addr;
100         mh.msg_namelen = sizeof(addr);
101         mh.msg_control = NULL;
102         mh.msg_controllen = 0;
103         mh.msg_flags = 0;
104
105         vec.iov_base = cf;
106         vec.iov_len = sizeof(*cf);
107
108         kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
109 }
110
111 /**
112  * cegw_udp2can - performs udp->can routing
113  *
114  * This function is run as a thread.
115  */
116 static int cegw_udp2can(void *data)
117 {
118         struct can_frame cf;
119         struct kvec vec;
120         struct msghdr mh;
121         struct cegw_rule* rule;
122         struct hlist_node* pos;
123         int can_ifidx;
124         int recv_size;
125
126         memset(&mh, 0, sizeof(mh));
127
128         while (cegw_state != CEGW_STOP) {
129                 vec.iov_base = &cf;
130                 vec.iov_len = sizeof(cf);
131                 recv_size = kernel_recvmsg(udp_sock, &mh, &vec, 1,
132                                 sizeof(cf), 0);
133                 /* recv_size == 0 when shutting down */
134                 if (recv_size != sizeof(cf) || recv_size == 0)
135                         continue;
136                 else if (recv_size < 0)
137                         return -1;
138
139                 /* FIXME: Convert endianing of cf.can_id */
140                 mutex_lock(&rule_eth_can_mutex);
141                 hlist_for_each_entry(rule, pos, &rule_eth_can, list) {
142                         can_ifidx = rule->can_ifindex;
143                         /* ToDo: from filter */
144                         cegw_can_send(can_sock, &cf, can_ifidx);
145                 }
146                 mutex_unlock(&rule_eth_can_mutex);
147         }
148
149         return 0;
150 }
151
152 /**
153  * cegw_can2udp - performs can->udp routing
154  *
155  * Runs as a thread.
156  */
157 static int cegw_can2udp(void* data)
158 {
159         struct msghdr mh;
160         struct kvec vec;
161         struct can_frame cf;
162         struct sockaddr_can ca;
163         struct cegw_rule* rule;
164         struct hlist_node* pos;
165         struct in_addr eth_ip;
166         u16 eth_port;
167         int recv_size;
168
169         mh.msg_name = &ca;
170         mh.msg_namelen = sizeof(ca);
171         mh.msg_control = NULL;
172         mh.msg_controllen = 0;
173         mh.msg_flags = 0;
174
175         while (cegw_state != CEGW_STOP) {
176                 vec.iov_base = &cf;
177                 vec.iov_len = sizeof(cf);
178
179                 recv_size = kernel_recvmsg(can_sock, &mh, &vec, 1,
180                                            sizeof(cf), 0);
181                 if (recv_size != sizeof(cf) || recv_size == 0)
182                         continue;
183                 else if (recv_size < 0)
184                         return -1;
185
186                 mutex_lock(&rule_can_eth_mutex);
187                 hlist_for_each_entry(rule, pos, &rule_can_eth, list) {
188                         eth_ip = rule->eth_ip;
189                         eth_port = rule->eth_port;
190                         if (rule->can_ifindex == ca.can_ifindex)
191                                 cegw_udp_send(udp_sock, &cf, eth_ip, eth_port);
192                 }
193                 mutex_unlock(&rule_can_eth_mutex);
194         }
195
196         return 0;
197 }
198
199 static int cegw_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
200 {
201         struct nlattr* tb[ CEGW_MAX+1 ];
202         struct cegw_rule* rule = NULL;
203         int ifindex;
204         struct rtmsg* r;
205         struct in_addr ip;
206         unsigned short port;
207         struct cegw_setting* set;
208         int err = 0;
209
210         if (nlmsg_len(nlh) < sizeof(*r))
211                 return -EINVAL;
212
213         r = nlmsg_data(nlh);
214
215         if (r->rtm_family != AF_CAN)
216                 return -EPFNOSUPPORT;
217
218         err = nlmsg_parse(nlh, sizeof(*r), tb, CEGW_MAX, NULL);
219         if (err < 0) {
220                 pr_devel("canethgw: nlmsg_parse error\n");
221                 return err;
222         }
223
224         if (tb[CEGW_CMD_INFO] == NULL) {
225                 pr_devel("canethgw: CEGW_CMD_INFO is missing in rtmsg\n");
226                 return -EINVAL;
227         }
228
229         switch (*(int*)nla_data(tb[CEGW_CMD_INFO])) {
230         case CEGW_LISTEN:
231                 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT]) {
232                         pr_devel("canethgw: missing attribute for CEGW_LISTEN\n");
233                         return -EINVAL;
234                 }
235
236                 /* ToDo: valid listen address */
237                 set = kmalloc(sizeof(*set), GFP_KERNEL);
238                 if (set == NULL)
239                         return -ENOMEM;
240                 set->eth_ip   = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
241                 set->eth_port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
242                 /* MS: It would be better to use workqueues here. */
243                 kthread_run(cegw_thread_start, set, "canethgw");
244                 break;
245         case CEGW_RULE_ETH_CAN:
246                 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] ||
247                     !tb[CEGW_CAN_IFINDEX]) {
248                         pr_devel("canethgw: missing attribute for"
249                                  "CEGW_RULE_ETH_CAN\n");
250                         return -EINVAL;
251                 }
252
253                 ifindex = *(int*)nla_data(tb[CEGW_CAN_IFINDEX]);
254                 ip = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
255                 port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
256                 pr_devel("canethgw: new eth->can rule - (%x:%hu)->(%d)\n",
257                          ip.s_addr, port, ifindex);
258
259                 rule = kmalloc(sizeof(struct cegw_rule), GFP_KERNEL);
260                 if (rule == NULL)
261                         return -ENOMEM;
262
263                 rule->can_ifindex = ifindex;
264                 rule->eth_ip = ip;
265                 rule->eth_port = port;
266
267                 mutex_lock(&rule_eth_can_mutex);
268                 hlist_add_head(&rule->list, &rule_eth_can);
269                 mutex_unlock(&rule_eth_can_mutex);
270                 break;
271         case CEGW_RULE_CAN_ETH:
272                 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] ||
273                     !tb[CEGW_CAN_IFINDEX]) {
274                         pr_devel("canethgw: missing attribute for "
275                                  "CEGW_RULE_CAN_ETH\n");
276                         return -EINVAL;
277                 }
278
279                 ifindex = *(int*)nla_data(tb[CEGW_CAN_IFINDEX]);
280                 ip = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
281                 port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
282                 pr_devel("canethgw: new can->eth rule - (%d)->(%x:%hu)\n",
283                          ifindex, ip.s_addr, port);
284
285                 rule = kmalloc(sizeof(struct cegw_rule), GFP_KERNEL);
286                 if (rule == NULL)
287                         return -ENOMEM;
288
289                 rule->can_ifindex = ifindex;
290                 rule->eth_ip = ip;
291                 rule->eth_port = port;
292
293                 mutex_lock(&rule_can_eth_mutex);
294                 hlist_add_head(&rule->list, &rule_can_eth);
295                 mutex_unlock(&rule_can_eth_mutex);
296                 break;
297         default:
298                 pr_devel("canethgw: unknown CEGW_CMD_INFO\n");
299                 break;
300         }
301
302         return 0;
303 }
304
305 static void cegw_flush(void)
306 {
307         struct cegw_rule *rule;
308         struct hlist_node *pos, *n;
309
310         mutex_lock(&rule_eth_can_mutex);
311         hlist_for_each_entry_safe(rule, pos, n, &rule_eth_can, list) {
312                 hlist_del(&rule->list);
313                 kfree(rule);
314         }
315         mutex_unlock(&rule_eth_can_mutex);
316
317         mutex_lock(&rule_can_eth_mutex);
318         hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
319                 hlist_del(&rule->list);
320                 kfree(rule);
321         }
322         mutex_unlock(&rule_can_eth_mutex);
323 }
324
325 static int cegw_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
326 {
327         struct rtmsg *r;
328         struct nlattr *tb[CEGW_MAX+1];
329         int err = 0;
330
331         if (nlmsg_len(nlh) < sizeof(*r))
332                 return -EINVAL;
333
334         r = nlmsg_data(nlh);
335
336         if (r->rtm_family != AF_CAN)
337                 return -EPFNOSUPPORT;
338
339         err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, CEGW_MAX, NULL);
340         if (err != 0)
341                 return -EINVAL;
342
343         if (tb[CEGW_CMD_INFO] == NULL) {
344                 pr_devel("canethgw: CEGW_CMD_INFO is missing in rtmsg\n");
345                 return -EINVAL;
346         }
347
348         if (*(int*)nla_data(tb[CEGW_CMD_INFO]) != CEGW_FLUSH) {
349                 return -EINVAL;
350         }
351
352         cegw_flush();
353
354         return 0;
355 }
356
357 static int cegw_put_rule(struct sk_buff *skb, int type, struct cegw_rule *rule)
358 {
359         int ifindex;
360         struct in_addr ip;
361         unsigned short port;
362         struct nlmsghdr *nlh;
363
364         ifindex = rule->can_ifindex;
365         ip = rule->eth_ip;
366         port = rule->eth_port;
367
368         nlh = nlmsg_put(skb, 0, 0, 0, 0, 0);
369         if (nlh == NULL)
370                 return -EMSGSIZE;
371
372         /* type */
373         if (nla_put(skb, CEGW_TYPE, sizeof(type), &type) < 0)
374                 goto cancel;
375         else
376                 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(type));
377
378         /* can ifindex */
379         if (nla_put(skb, CEGW_CAN_IFINDEX, sizeof(ifindex), &ifindex) < 0)
380                 goto cancel;
381         else
382                 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(ifindex));
383
384         /* ip adress */
385         if (nla_put(skb, CEGW_ETH_IP, sizeof(ip), &ip) < 0)
386                 goto cancel;
387         else
388                 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(ip));
389
390         /* port */
391         if (nla_put(skb, CEGW_ETH_PORT, sizeof(port), &port) < 0)
392                 goto cancel;
393         else
394                 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(port));
395
396         return skb->len;
397
398 cancel:
399         nlmsg_cancel(skb, nlh);
400         return -EMSGSIZE;
401 }
402
403 static int cegw_getroute(struct sk_buff *skb, struct netlink_callback *cb)
404 {
405         struct cegw_rule *rule;
406         struct hlist_node *pos;
407         int idx = 0;
408         int s_idx = cb->args[0];
409
410         mutex_lock(&rule_eth_can_mutex);
411         mutex_lock(&rule_can_eth_mutex);
412         hlist_for_each_entry(rule, pos, &rule_eth_can, list) {
413                 if (idx < s_idx)
414                         goto cont1;
415
416                 if (cegw_put_rule(skb, CEGW_RULE_ETH_CAN, rule) < 0)
417                         goto brk;
418 cont1:
419                 idx++;
420         }
421
422         hlist_for_each_entry(rule, pos, &rule_can_eth, list) {
423                 if (idx < s_idx)
424                         goto cont2;
425
426                 if (cegw_put_rule(skb, CEGW_RULE_CAN_ETH, rule) < 0)
427                         goto brk;
428 cont2:
429                 idx++;
430         }
431
432 brk:
433         mutex_unlock(&rule_eth_can_mutex);
434         mutex_unlock(&rule_can_eth_mutex);
435         cb->args[0] = idx;
436
437         return skb->len;
438 }
439
440 static int cegw_notifier(struct notifier_block *nb, unsigned long msg, void *data)
441 {
442         struct net_device *dev = (struct net_device *)data;
443         struct cegw_rule *rule;
444         struct hlist_node *pos, *n;
445
446         if (!net_eq(dev_net(dev), &init_net))
447                 return NOTIFY_DONE;
448         if (dev->type != ARPHRD_CAN)
449                 return NOTIFY_DONE;
450
451         if (msg == NETDEV_UNREGISTER) {
452                 hlist_for_each_entry_safe(rule, pos, n, &rule_eth_can, list) {
453                         if (rule->can_ifindex == dev->ifindex) {
454                                 hlist_del(&rule->list);
455                                 kfree(rule);
456                         }
457                 }
458
459                 hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
460                         if (rule->can_ifindex == dev->ifindex) {
461                                 hlist_del(&rule->list);
462                                 kfree(rule);
463                         }
464                 }
465         }
466
467         return NOTIFY_DONE;
468 }
469
470 /**
471  * cegw_thread_start - start working threads
472  * @data: (struct cegw_setting *) with new listening address
473  *
474  * Two threads are started. One is serving udp->can routing and the other
475  * can->udp.
476  */
477 static int cegw_thread_start(void *data)
478 {
479         struct sockaddr_in udp_addr;
480         struct sockaddr_can can_addr;
481         struct cegw_setting *set;
482
483         set = (struct cegw_setting *)data;
484
485         can_addr.can_family = AF_CAN;
486         can_addr.can_ifindex = 0;
487
488         udp_addr.sin_family = AF_INET;
489         udp_addr.sin_port = htons(set->eth_port);
490         udp_addr.sin_addr = set->eth_ip;
491
492         kfree(data);
493         mutex_lock(&cegw_mutex);
494         if (cegw_state == CEGW_EXIT)
495                 goto out_err;
496
497         /* stops threads if exist */
498         cegw_thread_stop();
499
500         /* create and bind sockets */
501         if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock)
502             != 0) {
503                 printk(KERN_ERR "canethgw: udp socket creation failed\n");
504                 goto out_err;
505         }
506
507         if (sock_create_kern(PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0) {
508                 printk(KERN_ERR "canethgw: can socket creation failed\n");
509                 sock_release(udp_sock);
510                 goto out_err;
511         }
512
513         if (kernel_bind(udp_sock, (struct sockaddr*)&udp_addr,
514                         sizeof(udp_addr)) != 0) {
515                 printk(KERN_ERR "canethgw: udp socket binding failed\n");
516                 sock_release(udp_sock);
517                 sock_release(can_sock);
518                 goto out_err;
519         }
520
521         if (kernel_bind(can_sock, (struct sockaddr*) &can_addr,
522                         sizeof(can_addr)) != 0) {
523                 printk(KERN_ERR "canethgw: can socket binding failed\n");
524                 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
525                 sock_release(udp_sock);
526                 sock_release(can_sock);
527                 goto out_err;
528         }
529
530         /* start threads */
531         cegw_state = CEGW_RUN;
532
533         eth_to_can = kthread_create(cegw_udp2can, NULL, "canethgw");
534         if (IS_ERR(eth_to_can)) {
535                 cegw_state = CEGW_STOP;
536                 sock_release(udp_sock);
537                 sock_release(can_sock);
538                 goto out_err;
539         }
540         get_task_struct(eth_to_can);
541         wake_up_process(eth_to_can);
542
543         can_to_eth = kthread_create(cegw_can2udp, NULL, "canethgw");
544         if (IS_ERR(can_to_eth)) {
545                 cegw_state = CEGW_STOP;
546                 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
547                 kthread_stop(eth_to_can);
548                 sock_release(udp_sock);
549                 sock_release(can_sock);
550                 goto out_err;
551         }
552         get_task_struct(can_to_eth);
553         wake_up_process(can_to_eth);
554
555         mutex_unlock(&cegw_mutex);
556         pr_devel("threads are running\n");
557         return 0;
558 out_err:
559         mutex_unlock(&cegw_mutex);
560         return -1;
561 }
562
563 /**
564  * cegw_thread_stop - stops threads and wait for exit
565  *
566  * Waits for threads to stop. Does nothing if cegw_state == CEGW_STOP.
567  */
568 static int cegw_thread_stop(void)
569 {
570         int how = SHUT_RDWR;
571         struct sock *sk = NULL;
572
573         if (cegw_state == CEGW_STOP)
574                 return 0;
575
576         cegw_state = CEGW_STOP;
577         /* shut down socket */
578         sk = can_sock->sk;
579         how++;
580         lock_sock(sk);
581         sk->sk_shutdown |= how;
582         sk->sk_state_change(sk);
583         release_sock(sk);
584
585         kernel_sock_shutdown(udp_sock, SHUT_RDWR);
586
587         /* wait for return to reuse port if restart */
588         kthread_stop(eth_to_can);
589         kthread_stop(can_to_eth);
590         sock_release(udp_sock);
591         sock_release(can_sock);
592         can_to_eth = NULL;
593         eth_to_can = NULL;
594
595         return 0;
596 }
597
598 static int cegw_open(struct inode *inode, struct file *file)
599 {
600         file->private_data = "greetings";
601
602         printk("cegw device opened\n");
603         return 0;
604 }
605
606 static int cegw_release(struct inode *inode, struct file *file)
607 {
608         printk("cegw device released, data=%s\n", file->private_data);
609         return 0;
610 }
611
612 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
613 {
614         switch (cmd) {
615                 case CEGW_IOCTL_CAN_SOCK:
616                         printk("ioctl can sock\n");
617                         break;
618                 case CEGW_IOCTL_UDP_SOCK:
619                         printk("ioctl udp sock\n");
620                         break;
621                 default:
622                         printk("undefined ioctl command\n");
623                         break;
624         }
625
626         return 0;
627 }
628
629 static const struct file_operations cegw_fops = {
630         .owner = THIS_MODULE,
631         .open = cegw_open,
632         .release = cegw_release,
633         .unlocked_ioctl = cegw_ioctl
634 };
635
636 static struct miscdevice cegw_device = {
637         .minor = MISC_DYNAMIC_MINOR,
638         .name = "cegw",
639         .fops = &cegw_fops
640 };
641
642 static int __init cegw_init(void)
643 {
644         misc_register(&cegw_device);
645
646         return 0;
647         notifier.notifier_call = cegw_notifier;
648         register_netdevice_notifier(&notifier);
649
650         /* subscribe to netlink */
651         rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cegw_getroute, NULL);
652         rtnl_register(PF_CAN, RTM_NEWROUTE, cegw_newroute, NULL, NULL);
653         rtnl_register(PF_CAN, RTM_DELROUTE, cegw_delroute, NULL, NULL);
654
655         return 0;
656 }
657
658 static void __exit cegw_exit(void)
659 {
660         misc_deregister(&cegw_device);
661
662         return;
663         /* ToDo: effect on cangw? */
664         rtnl_unregister_all(PF_CAN);
665
666         /* wait for rtnl callbacks */
667         rtnl_lock();
668         rtnl_unlock();
669
670         mutex_lock(&cegw_mutex);
671         cegw_thread_stop();
672         cegw_state = CEGW_EXIT;
673         mutex_unlock(&cegw_mutex);
674
675         unregister_netdevice_notifier(&notifier);
676         cegw_flush();
677 }
678
679 module_init(cegw_init);
680 module_exit(cegw_exit);
681