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>
19 #include <linux/completion.h>
20 #include <linux/mutex.h>
21 #include <net/inet_common.h>
23 MODULE_LICENSE("GPL");
25 static int cegw_udp_can(void *data);
26 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
27 struct in_addr ipaddr, u16 port);
28 static int cegw_can_udp(void *data);
29 static void cegw_can_send(struct socket *can_sock, struct can_frame *cf,
31 static int cegw_thread_start(void *data);
32 static int cegw_thread_stop(void);
44 struct in_addr eth_ip;
45 unsigned short eth_port;
46 struct hlist_node list;
51 struct in_addr eth_ip;
52 unsigned short eth_port;
55 static int cegw_state = CEGW_STOP;
56 static struct socket *can_sock = NULL, *udp_sock = NULL;
57 static struct task_struct *eth_to_can = NULL, *can_to_eth = NULL;
58 static struct notifier_block notifier;
60 static HLIST_HEAD(rule_eth_can);
61 static HLIST_HEAD(rule_can_eth);
62 static DEFINE_MUTEX(rule_eth_can_mutex);
63 static DEFINE_MUTEX(rule_can_eth_mutex);
64 static DEFINE_MUTEX(cegw_mutex);
66 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
67 struct in_addr ipaddr, u16 port)
70 struct sockaddr_in addr;
73 addr.sin_family = AF_INET;
74 addr.sin_port = htons(port);
75 addr.sin_addr = ipaddr;
78 mh.msg_namelen = sizeof(addr);
79 mh.msg_control = NULL;
80 mh.msg_controllen = 0;
84 vec.iov_len = sizeof(*cf);
86 kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
89 static void cegw_can_send(struct socket* can_sock, struct can_frame* cf,
94 struct sockaddr_can addr;
96 addr.can_family = AF_CAN;
97 addr.can_ifindex = ifindex;
100 mh.msg_namelen = sizeof(addr);
101 mh.msg_control = NULL;
102 mh.msg_controllen = 0;
106 vec.iov_len = sizeof(*cf);
108 kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
112 * cegw_udp_can - performs udp->can routing
114 * This function is run as a thread.
116 static int cegw_udp_can(void *data)
121 struct cegw_rule* rule;
122 struct hlist_node* pos;
130 mh.msg_control = NULL;
131 mh.msg_controllen = 0;
135 if (cegw_state == CEGW_STOP)
138 vec.iov_len = sizeof(cf);
139 recv_size = kernel_recvmsg(udp_sock, &mh, &vec, 1,
141 /* recv_size == 0 when shutting down */
142 if (recv_size != sizeof(cf) || recv_size == 0)
144 else if (recv_size < 0)
147 mutex_lock(&rule_eth_can_mutex);
148 hlist_for_each_entry(rule, pos, &rule_eth_can, list) {
149 can_ifidx = rule->can_ifindex;
150 /* ToDo: from filter */
151 cegw_can_send(can_sock, &cf, can_ifidx);
153 mutex_unlock(&rule_eth_can_mutex);
160 * cegw_can_udp - performs can->udp routing
162 static int cegw_can_udp(void* data)
167 struct sockaddr_can ca;
168 struct cegw_rule* rule;
169 struct hlist_node* pos;
170 struct in_addr eth_ip;
175 mh.msg_namelen = sizeof(ca);
176 mh.msg_control = NULL;
177 mh.msg_controllen = 0;
181 if (cegw_state == CEGW_STOP)
184 vec.iov_len = sizeof(cf);
186 recv_size = kernel_recvmsg(can_sock, &mh, &vec, 1,
188 if (recv_size != sizeof(cf) || recv_size == 0)
190 else if (recv_size < 0)
193 mutex_lock(&rule_can_eth_mutex);
194 hlist_for_each_entry(rule, pos, &rule_can_eth, list) {
195 eth_ip = rule->eth_ip;
196 eth_port = rule->eth_port;
197 if (rule->can_ifindex == ca.can_ifindex)
198 cegw_udp_send(udp_sock, &cf, eth_ip, eth_port);
200 mutex_unlock(&rule_can_eth_mutex);
206 static int cegw_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
208 struct nlattr* tb[ CEGW_MAX+1 ];
209 struct cegw_rule* rule = NULL;
214 struct cegw_setting* set;
217 if (nlmsg_len(nlh) < sizeof(*r))
222 if (r->rtm_family != AF_CAN)
223 return -EPFNOSUPPORT;
225 err = nlmsg_parse(nlh, sizeof(*r), tb, CEGW_MAX, NULL);
227 pr_devel("canethgw: nlmsg_parse error\n");
231 if (tb[CEGW_CMD_INFO] == NULL) {
232 pr_devel("canethgw: CEGW_CMD_INFO is missing in rtmsg\n");
236 switch (*(int*)nla_data(tb[CEGW_CMD_INFO])) {
238 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT]) {
239 pr_devel("canethgw: missing attribute for CEGW_LISTEN\n");
243 /* ToDo: valid listen address */
244 set = kmalloc(sizeof(*set), GFP_KERNEL);
247 set->eth_ip = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
248 set->eth_port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
249 kthread_run(cegw_thread_start, set, "canethgw");
251 case CEGW_RULE_ETH_CAN:
252 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] ||
253 !tb[CEGW_CAN_IFINDEX]) {
254 pr_devel("canethgw: missing attribute for"
255 "CEGW_RULE_ETH_CAN\n");
259 ifindex = *(int*)nla_data(tb[CEGW_CAN_IFINDEX]);
260 ip = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
261 port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
262 pr_devel("canethgw: new eth->can rule - (%x:%hu)->(%d)\n",
263 ip.s_addr, port, ifindex);
265 rule = kmalloc(sizeof(struct cegw_rule), GFP_KERNEL);
269 rule->can_ifindex = ifindex;
271 rule->eth_port = port;
273 mutex_lock(&rule_eth_can_mutex);
274 hlist_add_head(&rule->list, &rule_eth_can);
275 mutex_unlock(&rule_eth_can_mutex);
277 case CEGW_RULE_CAN_ETH:
278 if (!tb[CEGW_ETH_IP] || !tb[CEGW_ETH_PORT] ||
279 !tb[CEGW_CAN_IFINDEX]) {
280 pr_devel("canethgw: missing attribute for "
281 "CEGW_RULE_CAN_ETH\n");
285 ifindex = *(int*)nla_data(tb[CEGW_CAN_IFINDEX]);
286 ip = *(struct in_addr*)nla_data(tb[CEGW_ETH_IP]);
287 port = *(unsigned short*)nla_data(tb[CEGW_ETH_PORT]);
288 pr_devel("canethgw: new can->eth rule - (%d)->(%x:%hu)\n",
289 ifindex, ip.s_addr, port);
291 rule = kmalloc(sizeof(struct cegw_rule), GFP_KERNEL);
295 rule->can_ifindex = ifindex;
297 rule->eth_port = port;
299 mutex_lock(&rule_can_eth_mutex);
300 hlist_add_head(&rule->list, &rule_can_eth);
301 mutex_unlock(&rule_can_eth_mutex);
304 pr_devel("canethgw: unknown CEGW_CMD_INFO\n");
311 static void cegw_flush(void)
313 struct cegw_rule *rule;
314 struct hlist_node *pos, *n;
316 mutex_lock(&rule_eth_can_mutex);
317 hlist_for_each_entry_safe(rule, pos, n, &rule_eth_can, list) {
318 hlist_del(&rule->list);
321 mutex_unlock(&rule_eth_can_mutex);
323 mutex_lock(&rule_can_eth_mutex);
324 hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
325 hlist_del(&rule->list);
328 mutex_unlock(&rule_can_eth_mutex);
331 static int cegw_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
334 struct nlattr *tb[CEGW_MAX+1];
337 if (nlmsg_len(nlh) < sizeof(*r))
342 if (r->rtm_family != AF_CAN)
343 return -EPFNOSUPPORT;
345 err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, CEGW_MAX, NULL);
349 if (tb[CEGW_CMD_INFO] == NULL) {
350 pr_devel("canethgw: CEGW_CMD_INFO is missing in rtmsg\n");
354 if (*(int*)nla_data(tb[CEGW_CMD_INFO]) != CEGW_FLUSH) {
363 static int cegw_put_rule(struct sk_buff *skb, int type, struct cegw_rule *rule)
368 struct nlmsghdr *nlh;
370 ifindex = rule->can_ifindex;
372 port = rule->eth_port;
374 nlh = nlmsg_put(skb, 0, 0, 0, 0, 0);
379 if (nla_put(skb, CEGW_TYPE, sizeof(type), &type) < 0)
382 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(type));
385 if (nla_put(skb, CEGW_CAN_IFINDEX, sizeof(ifindex), &ifindex) < 0)
388 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(ifindex));
391 if (nla_put(skb, CEGW_ETH_IP, sizeof(ip), &ip) < 0)
394 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(ip));
397 if (nla_put(skb, CEGW_ETH_PORT, sizeof(port), &port) < 0)
400 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(port));
405 nlmsg_cancel(skb, nlh);
409 static int cegw_getroute(struct sk_buff *skb, struct netlink_callback *cb)
411 struct cegw_rule *rule;
412 struct hlist_node *pos;
414 int s_idx = cb->args[0];
416 mutex_lock(&rule_eth_can_mutex);
417 mutex_lock(&rule_can_eth_mutex);
418 hlist_for_each_entry(rule, pos, &rule_eth_can, list) {
422 if (cegw_put_rule(skb, CEGW_RULE_ETH_CAN, rule) < 0)
428 hlist_for_each_entry(rule, pos, &rule_can_eth, list) {
432 if (cegw_put_rule(skb, CEGW_RULE_CAN_ETH, rule) < 0)
439 mutex_unlock(&rule_eth_can_mutex);
440 mutex_unlock(&rule_can_eth_mutex);
446 static int cegw_notifier(struct notifier_block *nb, unsigned long msg, void *data)
448 struct net_device *dev = (struct net_device *)data;
449 struct cegw_rule *rule;
450 struct hlist_node *pos, *n;
452 if (!net_eq(dev_net(dev), &init_net))
454 if (dev->type != ARPHRD_CAN)
457 if (msg == NETDEV_UNREGISTER) {
458 hlist_for_each_entry_safe(rule, pos, n, &rule_eth_can, list) {
459 if (rule->can_ifindex == dev->ifindex) {
460 hlist_del(&rule->list);
465 hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
466 if (rule->can_ifindex == dev->ifindex) {
467 hlist_del(&rule->list);
477 * cegw_thread_start - start working threads
478 * @data: (struct cegw_setting *) with new listening address
480 * Two threads are started. One is serving udp->can routing and the other
483 static int cegw_thread_start(void *data)
485 struct sockaddr_in udp_addr;
486 struct sockaddr_can can_addr;
487 struct cegw_setting *set;
489 set = (struct cegw_setting *)data;
491 can_addr.can_family = AF_CAN;
492 can_addr.can_ifindex = 0;
494 udp_addr.sin_family = AF_INET;
495 udp_addr.sin_port = htons(set->eth_port);
496 udp_addr.sin_addr = set->eth_ip;
499 mutex_lock(&cegw_mutex);
500 if (cegw_state == CEGW_EXIT)
503 /* stops threads if exist */
506 /* create and bind sockets */
507 if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock)
509 printk(KERN_ERR "canethgw: udp socket creation failed\n");
513 if (sock_create_kern(PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0) {
514 printk(KERN_ERR "canethgw: can socket creation failed\n");
515 sock_release(udp_sock);
519 if (kernel_bind(udp_sock, (struct sockaddr*)&udp_addr,
520 sizeof(udp_addr)) != 0) {
521 printk(KERN_ERR "canethgw: udp socket binding failed\n");
522 sock_release(udp_sock);
523 sock_release(can_sock);
527 if (kernel_bind(can_sock, (struct sockaddr*) &can_addr,
528 sizeof(can_addr)) != 0) {
529 printk(KERN_ERR "canethgw: can socket binding failed\n");
530 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
531 sock_release(udp_sock);
532 sock_release(can_sock);
537 cegw_state = CEGW_RUN;
539 eth_to_can = kthread_create(cegw_udp_can, NULL, "canethgw");
540 if (IS_ERR(eth_to_can)) {
541 cegw_state = CEGW_STOP;
542 sock_release(udp_sock);
543 sock_release(can_sock);
546 get_task_struct(eth_to_can);
547 wake_up_process(eth_to_can);
549 can_to_eth = kthread_create(cegw_can_udp, NULL, "canethgw");
550 if (IS_ERR(can_to_eth)) {
551 cegw_state = CEGW_STOP;
552 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
553 kthread_stop(eth_to_can);
554 sock_release(udp_sock);
555 sock_release(can_sock);
558 get_task_struct(can_to_eth);
559 wake_up_process(can_to_eth);
561 mutex_unlock(&cegw_mutex);
562 pr_devel("threads are running\n");
565 mutex_unlock(&cegw_mutex);
570 * cegw_thread_stop - stops threads and wait for exit
572 * Waits for threads to stop. Does nothing if cegw_state == CEGW_STOP.
574 static int cegw_thread_stop(void)
577 struct sock *sk = NULL;
579 if (cegw_state == CEGW_STOP)
582 cegw_state = CEGW_STOP;
583 /* shut down socket */
587 sk->sk_shutdown |= how;
588 sk->sk_state_change(sk);
591 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
593 /* wait for return to reuse port if restart */
594 kthread_stop(eth_to_can);
595 kthread_stop(can_to_eth);
596 sock_release(udp_sock);
597 sock_release(can_sock);
604 static int __init cegw_init(void)
606 notifier.notifier_call = cegw_notifier;
607 register_netdevice_notifier(¬ifier);
609 /* subscribe to netlink */
610 rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cegw_getroute, NULL);
611 rtnl_register(PF_CAN, RTM_NEWROUTE, cegw_newroute, NULL, NULL);
612 rtnl_register(PF_CAN, RTM_DELROUTE, cegw_delroute, NULL, NULL);
617 static void __exit cegw_exit(void)
619 /* ToDo: effect on cangw? */
620 rtnl_unregister_all(PF_CAN);
622 /* wait for rtnl callbacks */
626 mutex_lock(&cegw_mutex);
628 cegw_state = CEGW_EXIT;
629 mutex_unlock(&cegw_mutex);
631 unregister_netdevice_notifier(¬ifier);
635 module_init(cegw_init);
636 module_exit(cegw_exit);