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 <linux/miscdevice.h>
22 #include <net/inet_common.h>
24 MODULE_LICENSE("GPL");
26 static int cegw_udp2can(void *data);
27 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf,
28 struct sockaddr_in* addr);
29 static int cegw_can2udp(void *data);
30 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);
42 struct in_addr eth_ip;
43 unsigned short eth_port;
44 struct hlist_node list;
48 struct in_addr eth_ip;
49 unsigned short eth_port;
53 static int cegw_state = CEGW_STOP;
54 static struct socket *can_sock = NULL, *udp_sock = NULL;
55 static struct task_struct *eth_to_can = NULL, *can_to_eth = NULL;
56 static struct notifier_block notifier;
58 static HLIST_HEAD(rule_eth_can);
59 static HLIST_HEAD(rule_can_eth);
60 static DEFINE_MUTEX(rule_eth_can_mutex);
61 static DEFINE_MUTEX(rule_can_eth_mutex);
62 static DEFINE_MUTEX(cegw_mutex);
64 static void cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
70 mh.msg_namelen = sizeof(*addr);
71 mh.msg_control = NULL;
72 mh.msg_controllen = 0;
76 vec.iov_len = sizeof(*cf);
78 /* FIXME: Convert endianing of cf->can_id */
79 kernel_sendmsg(udp_sock, &mh, &vec, 1, sizeof(*cf));
82 static void cegw_can_send(struct socket* can_sock, struct can_frame* cf)
89 mh.msg_control = NULL;
90 mh.msg_controllen = 0;
94 vec.iov_len = sizeof(*cf);
96 kernel_sendmsg(can_sock, &mh, &vec, 1, sizeof(*cf));
100 * cegw_udp2can - performs udp->can routing
102 * This function is run as a thread.
104 static int cegw_udp2can(void *data)
109 struct cegw_job *job = (struct cegw_job *)data;
110 struct socket *udp_sock = NULL, *can_sock = NULL;
113 memset(&mh, 0, sizeof(mh));
114 udp_sock = job->udp_sock;
115 can_sock = job->can_sock;
120 vec.iov_len = sizeof(cf);
121 recv_size = kernel_recvmsg(udp_sock, &mh, &vec, 1,
123 /* if(recv_size != sizeof(cf) */
124 if (recv_size < 1) /* ToDo: split 0 and else */
126 printk("udp2can error\n");
130 /* FIXME: Convert endianing of cf.can_id */
131 printk( "sendit to can\n" );
132 cegw_can_send(can_sock, &cf);
139 * cegw_can2udp - performs can->udp routing
143 static int cegw_can2udp(void* data)
148 struct cegw_job* job = (struct cegw_job*)data;
150 struct socket* udp_sock = job->udp_sock;
151 struct socket* can_sock = job->can_sock;
154 memset(&mh, 0, sizeof(mh));
158 vec.iov_len = sizeof(cf);
160 recv_size = kernel_recvmsg(can_sock, &mh, &vec, 1,
166 for( i=0; i<job->udp_dstcnt; i++ )
168 cegw_udp_send(udp_sock, &cf, &job->udp_dst[i]);
175 static int cegw_notifier(struct notifier_block *nb, unsigned long msg, void *data)
177 struct net_device *dev = (struct net_device *)data;
178 struct cegw_rule *rule;
179 struct hlist_node *pos, *n;
181 if (!net_eq(dev_net(dev), &init_net))
183 if (dev->type != ARPHRD_CAN)
186 if (msg == NETDEV_UNREGISTER) {
187 hlist_for_each_entry_safe(rule, pos, n, &rule_eth_can, list) {
188 if (rule->can_ifindex == dev->ifindex) {
189 hlist_del(&rule->list);
194 hlist_for_each_entry_safe(rule, pos, n, &rule_can_eth, list) {
195 if (rule->can_ifindex == dev->ifindex) {
196 hlist_del(&rule->list);
206 * cegw_thread_start - start working threads
207 * @data: (struct cegw_setting *) with new listening address
209 * Two threads are started. One is serving udp->can routing and the other
212 static int cegw_thread_start(void *data)
214 struct task_struct *task = NULL;
216 task = kthread_run(cegw_udp2can, data, "canethgw_udp2can");
221 task = kthread_run(cegw_can2udp, data, "canethgw_can2udp");
232 * cegw_thread_stop - stops threads and wait for exit
234 * Waits for threads to stop. Does nothing if cegw_state == CEGW_STOP.
236 static int cegw_thread_stop(void)
239 struct sock *sk = NULL;
241 if (cegw_state == CEGW_STOP)
244 cegw_state = CEGW_STOP;
245 /* shut down socket */
249 sk->sk_shutdown |= how;
250 sk->sk_state_change(sk);
253 kernel_sock_shutdown(udp_sock, SHUT_RDWR);
255 /* wait for return to reuse port if restart */
256 kthread_stop(eth_to_can);
257 kthread_stop(can_to_eth);
258 sock_release(udp_sock);
259 sock_release(can_sock);
266 static int cegw_open(struct inode *inode, struct file *file)
268 file->private_data = "greetings";
270 printk("cegw device opened\n");
274 static int cegw_release(struct inode *inode, struct file *file)
276 struct cegw_job *job = (struct cegw_job *)file->private_data;
278 sock_release( job->udp_sock );
279 sock_release( job->can_sock );
280 printk("cegw device released, data=%s\n", (char *)file->private_data);
284 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
290 struct cegw_ioctl *gwctl = NULL;
291 struct cegw_job *job = NULL;
293 /* ToDo: verify access and authorization */
294 if( get_user(dstcnt, &((struct cegw_ioctl __user *)arg)->udp_dstcnt) != 0 )
296 if( get_user(addrlen, &((struct cegw_ioctl __user *)arg)->udp_addrlen) != 0 )
299 memsz = sizeof(*gwctl) + dstcnt * addrlen;
300 /* ToDo: memory limit */
301 gwctl = kmalloc(GFP_KERNEL, memsz);
305 err = copy_from_user(gwctl, (void __user *)arg, memsz);
311 job = kmalloc(GFP_KERNEL, sizeof(*job));
315 job->udp_dst = kmalloc(GFP_KERNEL, dstcnt * addrlen); /* ToDo: limit */
316 if (job->udp_dst == NULL)
319 err = copy_from_user(job->udp_dst, (void __user *)(arg + sizeof(struct cegw_ioctl)), dstcnt*addrlen);
323 job->udp_sock = sockfd_lookup( gwctl->udp_sock, &err );
324 if (job->udp_sock == NULL)
327 job->can_sock = sockfd_lookup( gwctl->can_sock, &err );
328 if (job->can_sock == NULL)
331 job->udp_dstcnt = dstcnt;
333 /* ToDo: sin_family? */
335 file->private_data = job;
336 cegw_thread_start(job);
338 /* process udp destinations */
340 case CEGW_IOCTL_START:
341 printk("udp_dstcnt=%i\n", gwctl->udp_dstcnt);
342 printk("udp_dst[0] family=%i\n", job->udp_dst[0].sin_family);
343 printk("udp_dst[1] family=%i\n", job->udp_dst[1].sin_family);
346 printk("undefined ioctl command\n");
353 static const struct file_operations cegw_fops = {
354 .owner = THIS_MODULE,
356 .release = cegw_release,
357 .unlocked_ioctl = cegw_ioctl
360 static struct miscdevice cegw_device = {
361 .minor = MISC_DYNAMIC_MINOR,
366 static int __init cegw_init(void)
368 misc_register(&cegw_device);
371 notifier.notifier_call = cegw_notifier;
372 register_netdevice_notifier(¬ifier);
377 static void __exit cegw_exit(void)
379 misc_deregister(&cegw_device);
382 /* ToDo: effect on cangw? */
383 rtnl_unregister_all(PF_CAN);
385 /* wait for rtnl callbacks */
389 mutex_lock(&cegw_mutex);
391 cegw_state = CEGW_EXIT;
392 mutex_unlock(&cegw_mutex);
394 unregister_netdevice_notifier(¬ifier);
398 module_init(cegw_init);
399 module_exit(cegw_exit);