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 cegw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
199 struct nlattr* tb[ CGW_MAX+1 ];
201 struct can_eth_gw* cethgw = NULL;
202 struct eth_can_gw* ecangw = NULL;
205 if (nlmsg_len(nlh) < sizeof(*r))
209 if (r->can_family != AF_CAN)
210 return -EPFNOSUPPORT;
212 err = nlmsg_parse( nlh, sizeof( struct rtcanmsg ), tb, CGW_MAX, NULL );
215 printk( KERN_ERR "error: nlmsg_parse\n" );
221 case CGW_TYPE_CONFIG:
222 listen( 0, *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ),
223 *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) );
225 case CGW_TYPE_CAN_ETH_UDP:
226 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
227 printk( KERN_INFO "eth addr:%x\n", *(u32*)nla_data( tb[CGW_ETH_IP] ) );
228 printk( KERN_INFO "eth port:%hu\n", *(u16*)nla_data( tb[CGW_ETH_PORT] ) );
229 cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
232 printk( KERN_ERR "error: kmalloc\n" );
235 cethgw->src_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
236 cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CGW_ETH_IP] );
237 cethgw->dst_port = *(u16*)nla_data( tb[CGW_ETH_PORT] );
239 hlist_add_head_rcu( &cethgw->list, &can_eth_job );
241 case CGW_TYPE_ETH_CAN_UDP:
242 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
243 ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
246 printk( KERN_ERR "error: kmalloc\n" );
249 ecangw->dst_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
250 hlist_add_head_rcu( &ecangw->list, ð_can_job );
254 /* ToDo undef operation */
261 static int cegw_remove_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
264 struct nlattr* tb[ CGW_MAX+1 ];
266 if( nlmsg_len(nlh) < sizeof(*r) )
269 r = nlmsg_data( nlh );
271 if( r->can_family != AF_CAN )
272 return -EPFNOSUPPORT;
274 if( r->gwtype != CGW_TYPE_CAN_ETH_UDP )
277 printk( "flags:%x",r->flags );
278 //nlmsg_parse( nlh, sizeof(*r), tb, CGW_MAX, NULL );
284 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
286 struct sockaddr_in udp_addr;
287 struct sockaddr_can can_addr;
290 printk( KERN_INFO "listen called\n" );
292 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 )
294 printk( KERN_ERR "error: can_sock creation failed\n" );
298 can_addr.can_family = AF_CAN;
299 can_addr.can_ifindex = can_ifidx;
301 if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
303 printk( KERN_ERR "can_sock bind failed\n" );
307 printk( KERN_INFO "can socket success\n" );
309 udp_addr.sin_family = AF_INET;
310 udp_addr.sin_port = htons( eth_port );
311 udp_addr.sin_addr = eth_addr;
313 printk( KERN_INFO "trying to bind\n" );
314 if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
316 printk( "error: binding failed\n" );
317 sock_release( udp_sock );
318 sock_release( can_sock );
322 printk( KERN_INFO "socket established\n" );
325 eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" );
326 can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" );
328 printk( KERN_INFO "threads are running\n" );
330 gw_state = CEGW_RUNNING;
335 /***********************
337 ***********************/
339 static int __init cangw_init( void )
341 if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
343 printk( KERN_ERR "error: can_sock creation failed\n" );
347 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
349 printk( KERN_ERR "error: udp_sock creation failed\n" );
350 sock_release( can_sock );
354 /* subscribe to netlink */
355 //if( __rtnl_register( PF_CAN, RTM_GETROUTE, ) != 0 )
356 __rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL );
357 __rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL );
358 //__rtnl_register( PF_CAN, RTM_DELROUTE, )
361 if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
363 printk( "error: can_sock creation failed\n" );
370 static void __exit cangw_exit( void )
372 if( gw_state == CEGW_RUNNING )
374 sock_release( udp_sock );
375 sock_release( can_sock );
376 /* ToDo: stop threads */
379 /* ToDo: unregister netlink */
380 printk( "cangw: exit\n" );
381 //kthread_stop( ts );
384 module_init( cangw_init );
385 module_exit( cangw_exit );