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 * [ ] encapsule module - check inputs
18 * [ ] refactor - chc .h
20 * [ ] rtnl vs nl functions
22 * [ ] change listening
23 * [ ] clean exit - threads, jobs
26 MODULE_LICENSE( "GPL" );
28 static int gw_udp_recv( void* data );
29 static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port );
30 static int gw_can_recv( void* data );
31 static void gw_can_send( struct can_frame* cf, int ifidx );
32 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port );
34 #define CEGW_STOPPED 0
35 #define CEGW_RUNNING 1
37 static struct task_struct* eth_to_can, * can_to_eth;
38 static struct socket* udp_sock = NULL;
39 static struct socket* can_sock = NULL;
40 static int gw_state = CEGW_STOPPED;
45 struct in_addr dst_addr;
46 unsigned short dst_port;
47 struct hlist_node list;
53 struct hlist_node list;
56 HLIST_HEAD( can_eth_job );
57 HLIST_HEAD( eth_can_job );
61 struct can_filter filter;
64 struct in_addr dst_addr;
65 unsigned short dst_port;
68 /***********************
70 ***********************/
72 static int gw_udp_recv( void* data )
77 struct eth_can_gw* job;
78 struct hlist_node* pos;
82 vec.iov_len = sizeof(cf);
88 mh.msg_control = NULL;
89 mh.msg_controllen = 0;
94 if( kthread_should_stop() ) /* up() ?, recv is blocking */
96 kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */
97 printk( "received udp msg_id:%d\n", cf.can_id );
98 hlist_for_each_entry_rcu( job, pos, ð_can_job, list )
100 rcu_read_lock(); /**/
101 can_ifidx = job->dst_if_idx;
103 /* ToDo from filter */
104 gw_can_send( &cf, can_ifidx );
111 inline static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port )
114 struct sockaddr_in addr;
117 addr.sin_family = AF_INET;
118 addr.sin_port = htons( port );
119 addr.sin_addr = ipaddr;
122 mh.msg_namelen = sizeof( addr );
123 mh.msg_control = NULL;
124 mh.msg_controllen = 0;
128 vec.iov_len = sizeof( *cf );
130 kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) );
133 /***********************
135 ***********************/
137 static int gw_can_recv( void* data )
142 struct sockaddr_can ca;
143 struct can_eth_gw* job;
144 struct hlist_node* pos;
145 struct in_addr eth_addr;
149 mh.msg_namelen = sizeof( ca );
150 mh.msg_control = NULL;
151 mh.msg_controllen = 0;
155 vec.iov_len = sizeof( cf );
159 if( kthread_should_stop() ) /**/
161 kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 );
162 printk( "received can msg_id:%d, from:%d\n", cf.can_id, ca.can_ifindex );
163 hlist_for_each_entry_rcu( job, pos, &can_eth_job, list )
166 eth_addr = job->dst_addr;
167 eth_port = job->dst_port;
169 printk( KERN_INFO "%x\n", eth_addr.s_addr );
170 if( job->src_if_idx == ca.can_ifindex )
171 gw_udp_send( &cf, eth_addr, eth_port );
178 inline static void gw_can_send( struct can_frame* cf, int ifidx )
182 struct sockaddr_can ca =
184 .can_family = AF_CAN,
189 mh.msg_namelen = sizeof( ca );
190 mh.msg_control = NULL;
191 mh.msg_controllen = 0;
195 vec.iov_len = sizeof( *cf );
197 kernel_sendmsg( can_sock, &mh, &vec, 1, sizeof( *cf ) );
202 static int cegw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
204 struct nlattr* tb[ CGW_MAX+1 ];
205 struct can_eth_gw* cethgw = NULL;
206 struct eth_can_gw* ecangw = NULL;
210 if (nlmsg_len(nlh) < sizeof(*r))
214 err = nlmsg_parse( nlh, sizeof( struct rtmsg ), tb, CGW_MAX, NULL );
217 printk( KERN_ERR "error: nlmsg_parse\n" );
221 if( tb[CGW_CMD_INFO] == NULL )
223 printk( "error: bad cmd\n" );
227 switch( *(int*)nla_data( tb[CGW_CMD_INFO] ) )
230 listen( 0, *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ),
231 *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) );
233 case CGW_TYPE_CAN_ETH_UDP:
234 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
235 printk( KERN_INFO "eth addr:%x\n", *(u32*)nla_data( tb[CGW_ETH_IP] ) );
236 printk( KERN_INFO "eth port:%hu\n", *(u16*)nla_data( tb[CGW_ETH_PORT] ) );
237 cethgw = kmalloc( sizeof(struct can_eth_gw), GFP_KERNEL );
240 printk( KERN_ERR "error: kmalloc\n" );
243 cethgw->src_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
244 cethgw->dst_addr = *(struct in_addr*)nla_data( tb[CGW_ETH_IP] );
245 cethgw->dst_port = *(u16*)nla_data( tb[CGW_ETH_PORT] );
247 hlist_add_head_rcu( &cethgw->list, &can_eth_job );
249 case CGW_TYPE_ETH_CAN_UDP:
250 printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) );
251 ecangw = kmalloc( sizeof(struct eth_can_gw), GFP_KERNEL );
254 printk( KERN_ERR "error: kmalloc\n" );
257 ecangw->dst_if_idx = *(int*)nla_data( tb[CGW_CAN_IF] );
258 hlist_add_head_rcu( &ecangw->list, ð_can_job );
262 /* ToDo undef operation */
269 static int cegw_remove_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg )
272 struct nlattr* tb[ CGW_MAX+1 ];
273 struct hlist_node* pos,* n;
274 struct can_eth_gw* ceth;
275 struct eth_can_gw* ecan;
279 if( nlmsg_len(nlh) < sizeof(*r) )
282 r = nlmsg_data( nlh );
284 if( r->rtm_family != AF_CAN )
285 return -EPFNOSUPPORT;
288 if( r->gwtype != CGW_TYPE_CAN_ETH_UDP )
291 printk( "attrsize=%d\n", nlmsg_attrlen(nlh, sizeof(struct rtmsg)) );
293 err = nlmsg_parse( nlh, sizeof(struct rtmsg), tb, CGW_MAX, NULL );
297 if( tb[CGW_CMD_INFO] == NULL )
300 if( *(int*)nla_data( tb[CGW_CMD_INFO] ) == CEGW_FLUSH )
302 hlist_for_each_entry_safe( ceth, pos, n, &can_eth_job, list )
304 hlist_del( &ceth->list );
307 hlist_for_each_entry_safe( ecan, pos, n, ð_can_job, list )
309 hlist_del( &ecan->list );
317 static int cegw_dump_job( struct sk_buff* skb, struct netlink_callback* cb )
319 struct can_eth_gw* ceth;
320 struct eth_can_gw* ecan;
321 struct hlist_node* pos;
322 struct nlmsghdr* nlh;
324 int s_idx = cb->args[0];
326 struct in_addr dst_ip;
327 unsigned short dst_port;
330 hlist_for_each_entry_rcu( ecan, pos, ð_can_job, list )
336 nlh = nlmsg_put( skb, 0, 0, 0, 0, 0 );
338 ifidx = ecan->dst_if_idx;
339 type = CGW_TYPE_ETH_CAN_UDP;
340 nla_put( skb, CGW_TYPE, sizeof(type), &type );
341 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(type) );
343 nla_put( skb, CGW_CAN_IF, sizeof(ifidx), &ifidx ); /* ToDo return */
344 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ifidx) );
351 hlist_for_each_entry_rcu( ceth, pos, &can_eth_job, list )
356 nlh = nlmsg_put( skb, 0, 0, 0, 0, 0 );
358 ifidx = ceth->src_if_idx;
359 type = CGW_TYPE_CAN_ETH_UDP;
360 dst_ip = ceth->dst_addr;
361 dst_port = ceth->dst_port;
363 nla_put( skb, CGW_TYPE, sizeof(type), &type );
364 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(type) );
366 nla_put( skb, CGW_CAN_IF, sizeof(ifidx), &ifidx ); /* ToDo return */
367 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(ifidx) );
369 nla_put( skb, CGW_ETH_IP, sizeof(dst_ip), &dst_ip ); /* ToDo return */
370 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(dst_ip) );
372 nla_put( skb, CGW_ETH_PORT, sizeof(dst_port), &dst_port ); /* ToDo return */
373 nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN( sizeof(dst_port) );
375 //nla_put( skb, CGW_ETH_IP, sizeof() IP_ADDR )
381 /* ToDo nlmsg_cancel */
387 static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port )
389 struct sockaddr_in udp_addr;
390 struct sockaddr_can can_addr;
393 printk( KERN_INFO "listen called\n" );
395 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 )
397 printk( KERN_ERR "error: can_sock creation failed\n" );
401 can_addr.can_family = AF_CAN;
402 can_addr.can_ifindex = can_ifidx;
404 if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 )
406 printk( KERN_ERR "can_sock bind failed\n" );
410 printk( KERN_INFO "can socket success\n" );
412 udp_addr.sin_family = AF_INET;
413 udp_addr.sin_port = htons( eth_port );
414 udp_addr.sin_addr = eth_addr;
416 printk( KERN_INFO "trying to bind\n" );
417 if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */
419 printk( "error: binding failed\n" );
420 sock_release( udp_sock );
421 sock_release( can_sock );
425 printk( KERN_INFO "socket established\n" );
428 eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" );
429 can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" );
431 printk( KERN_INFO "threads are running\n" );
433 gw_state = CEGW_RUNNING;
438 /***********************
440 ***********************/
442 static int __init cangw_init( void )
444 if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 )
446 printk( KERN_ERR "error: can_sock creation failed\n" );
450 if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 )
452 printk( KERN_ERR "error: udp_sock creation failed\n" );
453 sock_release( can_sock );
457 /* subscribe to netlink */
458 if( __rtnl_register( PF_CAN, RTM_GETROUTE, NULL, cegw_dump_job, NULL ) != 0 )
460 printk( KERN_ERR "error: rtnl_register fail\n" );
461 sock_release( udp_sock );
462 sock_release( can_sock );
465 __rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL );
466 __rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL );
467 //__rtnl_register( PF_CAN, RTM_DELROUTE, )
470 if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 )
472 printk( "error: can_sock creation failed\n" );
479 static void __exit cangw_exit( void )
481 if( gw_state == CEGW_RUNNING )
483 sock_release( udp_sock );
484 sock_release( can_sock );
485 /* ToDo: stop threads */
488 /* ToDo: unregister netlink
490 printk( "cangw: exit\n" );
491 //kthread_stop( ts );
494 module_init( cangw_init );
495 module_exit( cangw_exit );