From: Radek Matejka Date: Sun, 5 Aug 2012 20:28:59 +0000 (+0200) Subject: fixed bug: kernel_recvmsg NULL pointer X-Git-Url: http://rtime.felk.cvut.cz/gitweb/can-eth-gw.git/commitdiff_plain/407cd49f85050b132c7f13a54ea496cde3dd44b1 fixed bug: kernel_recvmsg NULL pointer kvec was not used properly in kernel_recvmsg --- diff --git a/kernel/canethgw.c b/kernel/canethgw.c index d5a928a..020f79e 100644 --- a/kernel/canethgw.c +++ b/kernel/canethgw.c @@ -10,7 +10,11 @@ #include #include #include +#include #include "canethgw.h" +#include +#include +#include /** * ToDo: @@ -29,7 +33,9 @@ static int gw_udp_recv( void* data ); static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 port ); static int gw_can_recv( void* data ); static void gw_can_send( struct can_frame* cf, int ifidx ); -static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port ); +static void cegw_thread_start( void ); +static void cegw_thread_stop( void ); +static int cegw_thread_restart( void* arg ); #define CEGW_STOPPED 0 #define CEGW_RUNNING 1 @@ -37,6 +43,7 @@ static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port ); static struct task_struct* eth_to_can, * can_to_eth; static struct socket* udp_sock = NULL; static struct socket* can_sock = NULL; +/* ToDo: protect with mutex */ static int gw_state = CEGW_STOPPED; struct can_eth_gw @@ -56,18 +63,21 @@ struct eth_can_gw HLIST_HEAD( can_eth_job ); HLIST_HEAD( eth_can_job ); -struct cegw_setting +struct { struct can_filter filter; - int src_idx; + int can_idx; /* bind on if */ - struct in_addr dst_addr; - unsigned short dst_port; -}; + struct in_addr eth_addr; + unsigned short eth_port; +} cegw_setting; -/*********************** - * UDP - ***********************/ +DECLARE_COMPLETION( udp_compl ); +DECLARE_COMPLETION( can_compl ); +DECLARE_COMPLETION( udp_fin ); +DECLARE_COMPLETION( can_fin ); +DEFINE_MUTEX( cegw_setting_mutex ); +/**/ static int gw_udp_recv( void* data ) { @@ -77,9 +87,8 @@ static int gw_udp_recv( void* data ) struct eth_can_gw* job; struct hlist_node* pos; int can_ifidx; - - vec.iov_base = &cf; - vec.iov_len = sizeof(cf); + int recv_size; + struct sockaddr_in udp_addr; mh.msg_name = NULL; mh.msg_namelen = 0; @@ -89,22 +98,56 @@ static int gw_udp_recv( void* data ) mh.msg_controllen = 0; mh.msg_flags = 0; + mutex_lock( &cegw_setting_mutex ); + udp_addr.sin_family = AF_INET; + udp_addr.sin_port = htons( cegw_setting.eth_port ); + udp_addr.sin_addr = cegw_setting.eth_addr; + mutex_unlock( &cegw_setting_mutex ); + + if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock) != 0 ) + { + printk( KERN_ERR "error: can_sock creation failed\n" ); + return -1; + } + + if( kernel_bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */ + { + printk( "error: binding failed\n" ); + sock_release( udp_sock ); + return -1; + } + + printk( "gw_udp_recv is complete\n" ); + complete_all( &udp_compl ); /* ToDo: why _all? */ + wait_for_completion( &can_compl ); + printk( "gw_udp_recv continues\n" ); + while( 1 ) { - if( kthread_should_stop() ) /* up() ?, recv is blocking */ + if( gw_state == CEGW_STOPPED ) break; - kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* todo: handle error */ + vec.iov_base = &cf; + vec.iov_len = sizeof(cf); + recv_size = kernel_recvmsg( udp_sock, &mh, &vec, 1, sizeof(cf), 0 ); /* ToDo: handle error */ + if( recv_size == 0 ) + { + continue; + } + printk( "yes" ); printk( "received udp msg_id:%d\n", cf.can_id ); hlist_for_each_entry_rcu( job, pos, ð_can_job, list ) { rcu_read_lock(); /**/ can_ifidx = job->dst_if_idx; rcu_read_unlock(); - /* ToDo from filter */ + /* ToDo: from filter */ gw_can_send( &cf, can_ifidx ); } } + sock_release( udp_sock ); + complete_all( &udp_fin ); + printk( "udp terminates\n" ); return 0; } @@ -130,9 +173,7 @@ inline static void gw_udp_send( struct can_frame* cf, struct in_addr ipaddr, u16 kernel_sendmsg( udp_sock, &mh, &vec, 1, sizeof( *cf ) ); } -/*********************** - * CAN - ***********************/ +/**/ static int gw_can_recv( void* data ) { @@ -144,21 +185,48 @@ static int gw_can_recv( void* data ) struct hlist_node* pos; struct in_addr eth_addr; u16 eth_port; - + int recv_size; + struct sockaddr_can can_addr; + mh.msg_name = &ca; mh.msg_namelen = sizeof( ca ); mh.msg_control = NULL; mh.msg_controllen = 0; mh.msg_flags = 0; - - vec.iov_base = &cf; - vec.iov_len = sizeof( cf ); + + can_addr.can_family = AF_CAN; + can_addr.can_ifindex = 0; + + if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 ) + { + printk( KERN_ERR "error: can_sock creation failed\n" ); + return -1; + } + + if( kernel_bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 ) + { + printk( KERN_ERR "can_sock bind failed\n" ); + sock_release( can_sock ); + return -1; + } + + printk( "gw_can_recv is complete\n" ); + complete_all( &can_compl ); + wait_for_completion( &udp_compl ); + printk( "gw_can_recv continues\n" ); while( 1 ) { - if( kthread_should_stop() ) /**/ + if( gw_state == CEGW_STOPPED ) /**/ break; - kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 ); + vec.iov_base = &cf; + vec.iov_len = sizeof( cf ); + + recv_size = kernel_recvmsg( can_sock, &mh, &vec, 1, sizeof( cf ), 0 ); + if( recv_size == 0 ) + { + continue; + } printk( "received can msg_id:%d, from:%d\n", cf.can_id, ca.can_ifindex ); hlist_for_each_entry_rcu( job, pos, &can_eth_job, list ) { @@ -172,6 +240,9 @@ static int gw_can_recv( void* data ) } } + sock_release( can_sock ); + complete_all( &can_fin ); + printk( "can terminates\n" ); return 0; } @@ -227,8 +298,21 @@ static int cegw_create_job( struct sk_buff* skb, struct nlmsghdr* nlh, void* arg switch( *(int*)nla_data( tb[CGW_CMD_INFO] ) ) { case CEGW_LISTEN: - listen( 0, *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ), - *(u16*)nla_data( tb[CGW_LISTEN_PORT] ) ); + if( gw_state == CEGW_RUNNING ) + { + mutex_lock( &cegw_setting_mutex ); + cegw_setting.eth_addr = *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ); + cegw_setting.eth_port = *(u16*)nla_data( tb[CGW_LISTEN_PORT] ); + mutex_unlock( &cegw_setting_mutex ); + kthread_run( cegw_thread_restart, NULL, "canethgw" ); + } else + { + mutex_lock( &cegw_setting_mutex ); + cegw_setting.eth_addr = *(struct in_addr*)nla_data( tb[CGW_LISTEN_IP] ); + cegw_setting.eth_port = *(u16*)nla_data( tb[CGW_LISTEN_PORT] ); + mutex_unlock( &cegw_setting_mutex ); + cegw_thread_start(); + } break; case CGW_TYPE_CAN_ETH_UDP: printk( KERN_INFO "can:%d\n", *(int*)nla_data( tb[CGW_CAN_IF] ) ); @@ -384,53 +468,52 @@ cont2: return skb->len; } -static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port ) +static void cegw_thread_start( void ) { - struct sockaddr_in udp_addr; - struct sockaddr_can can_addr; - struct socket* tmp; - - printk( KERN_INFO "listen called\n" ); - - if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &tmp) != 0 ) - { - printk( KERN_ERR "error: can_sock creation failed\n" ); - return -1; - } + gw_state = CEGW_RUNNING; - can_addr.can_family = AF_CAN; - can_addr.can_ifindex = can_ifidx; + INIT_COMPLETION( udp_compl ); + INIT_COMPLETION( can_compl ); + INIT_COMPLETION( udp_fin ); + INIT_COMPLETION( can_fin ); - if( can_sock->ops->bind( can_sock, (struct sockaddr*) &can_addr, sizeof(can_addr) ) != 0 ) - { - printk( KERN_ERR "can_sock bind failed\n" ); - return -1; - } + eth_to_can = kthread_run( gw_udp_recv, NULL, "canethgw" ); + can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" ); - printk( KERN_INFO "can socket success\n" ); - - udp_addr.sin_family = AF_INET; - udp_addr.sin_port = htons( eth_port ); - udp_addr.sin_addr = eth_addr; - - printk( KERN_INFO "trying to bind\n" ); - if( udp_sock->ops->bind( udp_sock, (struct sockaddr*)&udp_addr, sizeof( udp_addr ) ) != 0 ) /* ref impl ?!? */ - { - printk( "error: binding failed\n" ); - sock_release( udp_sock ); - sock_release( can_sock ); - return -1; - } + printk( KERN_INFO "threads are running\n" ); +} - printk( KERN_INFO "socket established\n" ); - - /* run threads */ - eth_to_can = kthread_run( gw_udp_recv, NULL, "ethcangw" ); - can_to_eth = kthread_run( gw_can_recv, NULL, "canethgw" ); +/* ToDo: stop when no threads started */ +static void cegw_thread_stop( void ) +{ + int how = SHUT_RDWR; + struct sock* sk = NULL; + + /* be sure sockets exist */ + wait_for_completion( &can_compl ); + wait_for_completion( &udp_compl ); + gw_state = CEGW_STOPPED; + + sk = can_sock->sk; + how++; + lock_sock( sk ); + sk->sk_shutdown |= how; + sk->sk_state_change( sk ); + release_sock( sk ); + + kernel_sock_shutdown( udp_sock, SHUT_RDWR ); + + /* wait for shutdown to be able to reuse port */ + wait_for_completion( &udp_fin ); + wait_for_completion( &can_fin ); +} - printk( KERN_INFO "threads are running\n" ); +static int cegw_thread_restart( void* data ) +{ + printk( "restart\n" ); - gw_state = CEGW_RUNNING; + cegw_thread_stop(); + cegw_thread_start(); return 0; } @@ -440,39 +523,16 @@ static int listen( int can_ifidx, struct in_addr eth_addr, u16 eth_port ) ***********************/ static int __init cangw_init( void ) -{ - if( sock_create_kern( PF_CAN, SOCK_RAW, CAN_RAW, &can_sock) != 0 ) - { - printk( KERN_ERR "error: can_sock creation failed\n" ); - return -1; - } - - if( sock_create_kern( PF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_sock ) != 0 ) - { - printk( KERN_ERR "error: udp_sock creation failed\n" ); - sock_release( can_sock ); - return -1; - } - +{ /* subscribe to netlink */ if( __rtnl_register( PF_CAN, RTM_GETROUTE, NULL, cegw_dump_job, NULL ) != 0 ) { printk( KERN_ERR "error: rtnl_register fail\n" ); - sock_release( udp_sock ); - sock_release( can_sock ); return -1; } __rtnl_register( PF_CAN, RTM_NEWROUTE, cegw_create_job, NULL, NULL ); __rtnl_register( PF_CAN, RTM_DELROUTE, cegw_remove_job, NULL, NULL ); - //__rtnl_register( PF_CAN, RTM_DELROUTE, ) - - /* - if( sock_create_kern( AF_CAN, SOCK_RAW, CAN_RAW, &can_sock ) != 0 ) - {s - printk( "error: can_sock creation failed\n" ); - } - */ - + return 0; } @@ -480,15 +540,14 @@ static void __exit cangw_exit( void ) { if( gw_state == CEGW_RUNNING ) { - sock_release( udp_sock ); - sock_release( can_sock ); - /* ToDo: stop threads */ + cegw_thread_stop(); + /* ToDo: frees mem_cache? */ + /* udp must not exists */ } /* ToDo: unregister netlink * free jobs */ printk( "cangw: exit\n" ); - //kthread_stop( ts ); } module_init( cangw_init );