1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/kthread.h>
4 #include <linux/sched.h>
5 #include <linux/delay.h>
6 #include <linux/wait.h>
7 #include <linux/netdevice.h>
8 #include <linux/socket.h>
10 #include <linux/can/core.h>
11 #include <linux/can.h>
14 MODULE_LICENSE( "GPL" );
16 static struct task_struct* eth_to_can, * can_to_eth;
17 static struct socket* udp_sock;
18 static struct socket* can_sock;
19 static struct net_device* can_dev;
21 /***********************
23 ***********************/
25 int gw_udp_recv( void* data )
32 vec.iov_len = sizeof(cf);
38 mh.msg_control = NULL;
39 mh.msg_controllen = 0;
44 if( kthread_should_stop() ) /* up() ?, recv is blocking */
46 kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */
47 printk( "received udp msg_id:%d\n", cf.can_id );
54 void gw_udp_send( struct can_frame* cf )
57 struct sockaddr_in addr;
60 addr.sin_family = AF_INET;
61 addr.sin_port = htons( 10502 );
62 addr.sin_addr.s_addr = 0x0100007f;
65 mh.msg_namelen = sizeof( addr );
66 mh.msg_control = NULL;
67 mh.msg_controllen = 0;
71 vec.iov_len = sizeof( *cf );
73 kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) );
76 /***********************
78 ***********************/
80 int gw_can_recv( void* data )
88 mh.msg_control = NULL;
89 mh.msg_controllen = 0;
93 vec.iov_len = sizeof( cf );
97 if( kthread_should_stop() ) /**/
99 kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
100 printk( "received can msg_id:%d\n", cf.can_id );
107 void gw_can_send( struct can_frame* cf )
114 mh.msg_control = NULL;
115 mh.msg_controllen = 0;
119 vec.iov_len = sizeof( *cf );
121 kernel_sendmsg( can_sock, &mh, &vec, 1, sizeof( *cf ) );
124 /***********************
126 ***********************/
128 static int __init cangw_init( void )
130 struct sockaddr_in udp_addr;
131 struct sockaddr_can can_addr;
134 /* 1. create can socket and bind to it */
135 can_dev = dev_get_by_name( &init_net, "vcan0" ); /* net ns?, release counter! */
136 if( can_dev == NULL )
138 printk( KERN_ERR "error: vcan0 not found\n" );
141 ifidx = can_dev->ifindex;
144 if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
146 printk( KERN_ERR "error: can_sock creation failed\n" );
150 can_addr.can_family = AF_CAN;
151 can_addr.can_ifindex = ifidx;
153 if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
155 printk( KERN_ERR "can_sock bind failed\n" );
159 /* 2. create udp socket and bind to it */
160 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
162 printk( "error: udp_sock creation failed\n" );
163 sock_release( can_sock );
167 udp_addr.sin_family = AF_INET;
168 udp_addr.sin_port = htons( 10501 );
169 udp_addr.sin_addr.s_addr = INADDR_ANY;
171 if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
173 printk( "error: binding failed\n" );
174 sock_release( udp_sock );
175 sock_release( can_sock );
179 /* 3. run bridging threads */
180 eth_to_can = kthread_run( gw_udp_recv, NULL, "cangw" );
181 can_to_eth = kthread_run( gw_can_recv, NULL, "cangw" );
184 if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
186 printk( "error: can_sock creation failed\n" );
193 static void __exit cangw_exit( void )
195 sock_release( udp_sock );
196 sock_release( can_sock );
198 printk( "cangw: exit\n" );
199 //kthread_stop( ts );
202 module_init( cangw_init );
203 module_exit( cangw_exit );