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;
207 if (nlmsg_len(nlh) < sizeof(*r))
211 err = nlmsg_parse( nlh, sizeof( struct rtmsg ), tb, CGW_MAX, NULL );
214 printk( KERN_ERR "error: nlmsg_parse\n" );
218 if( tb[CGW_CMD_INFO] == NULL )
220 printk( "error: bad cmd\n" );
224 switch( *(int*)nla_data( tb[CGW_CMD_INFO] ) )
227 listen( 0, *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ),
228 *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) );
230 case CGW_TYPE_CAN_ETH_UDP:
231 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
232 printk( KERN_INFO "eth addr:%x\n", *(u32*)nla_data( tb[CGW_ETH_IP] ) );
233 printk( KERN_INFO "eth port:%hu\n", *(u16*)nla_data( tb[CGW_ETH_PORT] ) );
234 cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
237 printk( KERN_ERR "error: kmalloc\n" );
240 cethgw->src_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
241 cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CGW_ETH_IP] );
242 cethgw->dst_port = *(u16*)nla_data( tb[CGW_ETH_PORT] );
244 hlist_add_head_rcu( &cethgw->list, &can_eth_job );
246 case CGW_TYPE_ETH_CAN_UDP:
247 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
248 ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
251 printk( KERN_ERR "error: kmalloc\n" );
254 ecangw->dst_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
255 hlist_add_head_rcu( &ecangw->list, ð_can_job );
259 /* ToDo undef operation */
266 static int cegw_remove_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
269 struct nlattr* tb[ CGW_MAX+1 ];
270 struct hlist_node* pos,* n;
271 struct can_eth_gw* ceth;
272 struct eth_can_gw* ecan;
276 if( nlmsg_len(nlh) < sizeof(*r) )
279 r = nlmsg_data( nlh );
281 if( r->rtm_family != AF_CAN )
282 return -EPFNOSUPPORT;
285 if( r->gwtype != CGW_TYPE_CAN_ETH_UDP )
288 printk( "attrsize=%d\n", nlmsg_attrlen(nlh, sizeof(struct rtmsg)) );
290 err = nlmsg_parse( nlh, sizeof(struct rtmsg), tb, CGW_MAX, NULL );
294 if( tb[CGW_CMD_INFO] == NULL )
297 if( *(int*)nla_data( tb[CGW_CMD_INFO] ) == CEGW_FLUSH )
299 hlist_for_each_entry_safe( ceth, pos, n, &can_eth_job, list )
301 hlist_del( &ceth->list );
304 hlist_for_each_entry_safe( ecan, pos, n, ð_can_job, list )
306 hlist_del( &ecan->list );
314 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
316 struct sockaddr_in udp_addr;
317 struct sockaddr_can can_addr;
320 printk( KERN_INFO "listen called\n" );
322 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 )
324 printk( KERN_ERR "error: can_sock creation failed\n" );
328 can_addr.can_family = AF_CAN;
329 can_addr.can_ifindex = can_ifidx;
331 if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
333 printk( KERN_ERR "can_sock bind failed\n" );
337 printk( KERN_INFO "can socket success\n" );
339 udp_addr.sin_family = AF_INET;
340 udp_addr.sin_port = htons( eth_port );
341 udp_addr.sin_addr = eth_addr;
343 printk( KERN_INFO "trying to bind\n" );
344 if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
346 printk( "error: binding failed\n" );
347 sock_release( udp_sock );
348 sock_release( can_sock );
352 printk( KERN_INFO "socket established\n" );
355 eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" );
356 can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" );
358 printk( KERN_INFO "threads are running\n" );
360 gw_state = CEGW_RUNNING;
365 /***********************
367 ***********************/
369 static int __init cangw_init( void )
371 if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
373 printk( KERN_ERR "error: can_sock creation failed\n" );
377 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
379 printk( KERN_ERR "error: udp_sock creation failed\n" );
380 sock_release( can_sock );
384 /* subscribe to netlink */
385 //if( __rtnl_register( PF_CAN, RTM_GETROUTE, ) != 0 )
386 __rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL );
387 __rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL );
388 //__rtnl_register( PF_CAN, RTM_DELROUTE, )
391 if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
393 printk( "error: can_sock creation failed\n" );
400 static void __exit cangw_exit( void )
402 if( gw_state == CEGW_RUNNING )
404 sock_release( udp_sock );
405 sock_release( can_sock );
406 /* ToDo: stop threads */
409 /* ToDo: unregister netlink
411 printk( "cangw: exit\n" );
412 //kthread_stop( ts );
415 module_init( cangw_init );
416 module_exit( cangw_exit );