понедельник, 5 сентября 2011 г.

The Catch of the Changing Routes For Ipv6 with Netlink.

IPv6 allows to make an auto configuring of the network interface via icmpv6 if forwarding is unsetted. In this case users can not track the change of theirs IP and routes in this mode. How can you detect this changes?

It is simple if you would use the netlink protocol. You can read about this protocol on http://www.ietf.org/rfc/rfc3549.txt.
I made the function "show_route" that was shows the changed routes into OS:



/*
buffer - the storage of netlink messages
size - the size of the received netlink messages 
*/
void show_route(char *buffer, int size)
{
/*
nlmp - the netlink header 
routemsg - the data of the netlink message
tb[RTA_MAX+1] -  attributes of the route
*/
 struct nlmsghdr *nlmp;
     struct rtmsg       *routemsg;
      struct rtattr * tb[RTA_MAX+1];

/*
The buffer has following structure:
|nlmsghdr|data|nlmsghdr|data|....|nlmsghdr|data|
|--------------------size----------------------|
*/

for(nlmp = (struct nlmsghdr *)buffer; size > sizeof(*nlmp);){  
   
   int len;
   int req_len;
   /*Checking the netlink message*/ 
   if (!NLMSG_OK(nlmp, size)) {
        perror("NLMSG not OK\n");
        return;
   }
   /*The length of netlink header*/
   int len = nlmp->nlmsg_len;
   /*Data length  of netlink message*/
   int req_len = len - sizeof(*nlmp);
   /*Check type of message*/
   /*If type is undefined then go to the next message*/ 
   switch(nlmp->nlmsg_type){
   case RTM_DELROUTE:
        printf("A route was deleted\n");
        break;
   case RTM_NEWROUTE:
        printf("A route was added\n");
        break;
   default:
        size -= NLMSG_ALIGN(len);
        nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len));
        continue;
   }
   /*Get data of netlink message and get its atributte after that*/
   routemsg = (struct rtmsg *)NLMSG_DATA(nlmp);

   parseRtattr(tb, RTA_MAX, RTM_RTA(routemsg), len);

   /*Printing the atributtes of route*/
   if(tb[RTA_IIF]){
        int iface;
        iface = *(int*)RTA_DATA(tb[RTA_IIF]);
        printf("An input iface is %d\n", iface);
        
   }
   if(tb[RTA_OIF]){
        int iface;
        iface = *(int*)RTA_DATA(tb[RTA_OIF]);
        printf("An output iface is %d\n", iface);
   }
   if(tb[RTA_DST]){
        char routeAddr[128];
        inet_ntop(AF_INET6, RTA_DATA(tb[RTA_DST]), routeAddr, sizeof(routeAddr));
        printf("Destination route is %s\n", routeAddr);
   }else if (routemsg->rtm_dst_len) {
  printf( "0/%d \n", routemsg->rtm_dst_len);
   } else {
        printf("Destination route is default\n");
   }
   
   if(tb[RTA_SRC]){
        char routeAddr[128];
        inet_ntop(AF_INET6, RTA_DATA(tb[RTA_SRC]), routeAddr, sizeof(routeAddr));
        printf("Source route is %s\n", routeAddr);
   }
   if(tb[RTA_GATEWAY]){
        char routeAddr[128];
        inet_ntop(AF_INET6, RTA_DATA(tb[RTA_GATEWAY]), routeAddr, sizeof(routeAddr));
        printf("Gateway is %s\n", routeAddr);
   }
   
   if(tb[RTA_PRIORITY]){
        int metric ;
        metric = *(int*)RTA_DATA(tb[RTA_PRIORITY]);
        printf("Mertic is %d\n", metric);
   }
   /*And go to the next message*/
   size -= NLMSG_ALIGN(len);
   nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len));
     }//for(nlmp = (struct nlmsghdr *)buffer; size > sizeof(*nlmp);){
}


Each segment of data of netlink message contains a header which defines its data type, and its length. Thereby the netlink message appear as following:





I use to parse attribute of route written bellow:

void parseRtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
{
     memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
     /*Verify the data integrity of the data which succedes this rtattr header.*/
     while(RTA_OK(rta, len)){
        if(rta->rta_type <= max){
           tb[rta->rta_type] = rta;
        }
          /*Return a pointer to the next rtattr header in the chain.*/
        rta = RTA_NEXT(rta, len);
     }
}
I created the main function after that:

int main(int argc, char *argv[])
{
     /*for socket description*/
     int nd = 0;
     /*
       The netlink socket address address structure, named struct sockaddr_nl is
       passed to all calls which send or receive netlink sockets. This structure both
       informs the kernel networking stack of a datagrams destination, and informs
       a user-space program of a received frames source.
     */ 
     struct sockaddr_nl *local;
     /*The buffer for recsiving netlink`s message*/ 
     char buffer[16384];
     /*
      The netlink address family uses the standard BSD socket API as its interface
      between user-space programs and various kernel components. The creation
      of a netlink socket is preformed in the exact same way as any other network
       socket,via the socket library call.

     */
     nd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

     local = malloc(sizeof(struct sockaddr_nl ));
     memset(local, 0, sizeof(struct sockaddr_nl));
          
     local->nl_family = AF_NETLINK;
     /*I want to catch  ipv6 routes*/
     local->nl_groups = RTMGRP_IPV6_ROUTE;
     local->nl_pid = getpid();
     
     if(bind(nd, (struct sockaddr *)local , sizeof(struct sockaddr_nl)) < 0){
        printf("Error bind\n");
        return 1;
     }

     /*After the socket is created and bound, I can read a using recvmsg() function*/
     while(1){
        /*msg is of struct msghdr defined in /include/linux/socket.h*/
        struct msghdr msg;
        /*
         This iovec structure is filled with the data pointer 
         and the length of the data to be passed to the kernel
        */
        struct iovec iov;
        int size = 0;
   
        iov.iov_base = buffer;
        iov.iov_len = 16000;
   
        msg.msg_name = &local;
        msg.msg_namelen = sizeof(*local);
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
       /*The MSG_DONTWAIT flag requests the call to return when it would block otherwise. */
        size = recvmsg(nd, &msg, MSG_DONTWAIT);
        if(size  < 0){
            if(errno == EINTR || errno == EAGAIN){
               usleep(250000);
               continue;
            }
        }

        show_route(buffer, size);
     }
 
} 

Комментариев нет:

Отправить комментарий