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>
12 #include <net/rtnetlink.h>
17 * [ ] check every input
21 MODULE_LICENSE( "GPL" );
23 static int gw_udp_recv( void* data );
24 static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port );
25 static int gw_can_recv( void* data );
26 static void gw_can_send( struct can_frame* cf, int ifidx );
27 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port );
29 #define CEGW_STOPPED 0
30 #define CEGW_RUNNING 1
32 static struct task_struct* eth_to_can, * can_to_eth;
33 static struct socket* udp_sock = NULL;
34 static struct socket* can_sock = NULL;
35 static int gw_state = CEGW_STOPPED;
40 struct in_addr dst_addr;
41 unsigned short dst_port;
42 struct hlist_node list;
48 struct hlist_node list;
51 HLIST_HEAD( can_eth_job );
52 HLIST_HEAD( eth_can_job );
56 struct can_filter filter;
59 struct in_addr dst_addr;
60 unsigned short dst_port;
63 /***********************
65 ***********************/
67 static int gw_udp_recv( void* data )
72 struct eth_can_gw* job;
73 struct hlist_node* pos;
77 vec.iov_len = sizeof(cf);
83 mh.msg_control = NULL;
84 mh.msg_controllen = 0;
89 if( kthread_should_stop() ) /* up() ?, recv is blocking */
91 kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */
92 printk( "received udp msg_id:%d\n", cf.can_id );
93 hlist_for_each_entry_rcu( job, pos, ð_can_job, list )
96 can_ifidx = job->dst_if_idx;
98 /* ToDo from filter */
99 gw_can_send( &cf, can_ifidx );
106 inline static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port )
109 struct sockaddr_in addr;
112 addr.sin_family = AF_INET;
113 addr.sin_port = htons( port );
114 addr.sin_addr = ipaddr;
117 mh.msg_namelen = sizeof( addr );
118 mh.msg_control = NULL;
119 mh.msg_controllen = 0;
123 vec.iov_len = sizeof( *cf );
125 kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) );
128 /***********************
130 ***********************/
132 static int gw_can_recv( void* data )
137 struct sockaddr_can ca;
138 struct can_eth_gw* job;
139 struct hlist_node* pos;
140 struct in_addr eth_addr;
144 mh.msg_namelen = sizeof( ca );
145 mh.msg_control = NULL;
146 mh.msg_controllen = 0;
150 vec.iov_len = sizeof( cf );
154 if( kthread_should_stop() ) /**/
156 kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
157 printk( "received can msg_id:%d, from:%d\n", cf.can_id, ca.can_ifindex );
158 hlist_for_each_entry_rcu( job, pos, &can_eth_job, list )
161 eth_addr = job->dst_addr;
162 eth_port = job->dst_port;
164 printk( KERN_INFO "%x\n", eth_addr );
165 if( job->src_if_idx == ca.can_ifindex )
166 gw_udp_send( &cf, eth_addr, eth_port );
173 inline static void gw_can_send( struct can_frame* cf, int ifidx )
177 struct sockaddr_can ca =
179 .can_family = AF_CAN,
184 mh.msg_namelen = sizeof( ca );
185 mh.msg_control = NULL;
186 mh.msg_controllen = 0;
190 vec.iov_len = sizeof( *cf );
192 kernel_sendmsg( can_sock, &mh, &vec, 1, sizeof( *cf ) );
197 static int cgw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh,
200 struct nlattr* tb[ CGW_MAX+1 ];
202 struct can_eth_gw* cethgw = NULL;
203 struct eth_can_gw* ecangw = NULL;
206 if (nlmsg_len(nlh) < sizeof(*r))
210 if (r->can_family != AF_CAN)
211 return -EPFNOSUPPORT;
213 err = nlmsg_parse( nlh, sizeof( struct rtcanmsg ), tb, CGW_MAX, NULL );
216 printk( KERN_ERR "error: nlmsg_parse\n" );
222 case CGW_TYPE_CONFIG:
223 listen( 0, *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ),
224 *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) );
226 case CGW_TYPE_CAN_ETH_UDP:
227 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
228 printk( KERN_INFO "eth addr:%x\n", *(u32*)nla_data( tb[CGW_ETH_IP] ) );
229 printk( KERN_INFO "eth port:%hu\n", *(u16*)nla_data( tb[CGW_ETH_PORT] ) );
230 cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
233 printk( KERN_ERR "error: kmalloc\n" );
236 cethgw->src_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
237 cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CGW_ETH_IP] );
238 cethgw->dst_port = *(u16*)nla_data( tb[CGW_ETH_PORT] );
240 hlist_add_head_rcu( &cethgw->list, &can_eth_job );
242 case CGW_TYPE_ETH_CAN_UDP:
243 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
244 ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
247 printk( KERN_ERR "error: kmalloc\n" );
250 ecangw->dst_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
251 hlist_add_head_rcu( &ecangw->list, ð_can_job );
255 /* ToDo undef operation */
262 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
264 struct sockaddr_in udp_addr;
265 struct sockaddr_can can_addr;
268 printk( KERN_INFO "listen called\n" );
270 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 )
272 printk( KERN_ERR "error: can_sock creation failed\n" );
276 can_addr.can_family = AF_CAN;
277 can_addr.can_ifindex = can_ifidx;
279 if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
281 printk( KERN_ERR "can_sock bind failed\n" );
285 printk( KERN_INFO "can socket success\n" );
287 udp_addr.sin_family = AF_INET;
288 udp_addr.sin_port = htons( eth_port );
289 udp_addr.sin_addr = eth_addr;
291 printk( KERN_INFO "trying to bind\n" );
292 if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
294 printk( "error: binding failed\n" );
295 sock_release( udp_sock );
296 sock_release( can_sock );
300 printk( KERN_INFO "socket established\n" );
303 eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" );
304 can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" );
306 printk( KERN_INFO "threads are running\n" );
308 gw_state = CEGW_RUNNING;
313 /***********************
315 ***********************/
317 static int __init cangw_init( void )
319 if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
321 printk( KERN_ERR "error: can_sock creation failed\n" );
325 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
327 printk( KERN_ERR "error: udp_sock creation failed\n" );
328 sock_release( can_sock );
332 /* subscribe to netlink */
333 //if( __rtnl_register( PF_CAN, RTM_GETROUTE, ) != 0 )
334 __rtnl_register( PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL );
335 //__rtnl_register( PF_CAN, RTM_DELROUTE, )
338 if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
340 printk( "error: can_sock creation failed\n" );
347 static void __exit cangw_exit( void )
349 if( gw_state == CEGW_RUNNING )
351 sock_release( udp_sock );
352 sock_release( can_sock );
353 /* ToDo: stop threads */
356 printk( "cangw: exit\n" );
357 //kthread_stop( ts );
360 module_init( cangw_init );
361 module_exit( cangw_exit );