Hi all Iam trying to implement "route add " using netlink. The changes are not reflected in the routing table. I have given my code and screen shots of the routing tables. Can anybody tell me is there any mistake iam making in defining the fields . or any other mistake iam commiting thanxs viji //////////////////////////////////// CODE ////////////////////////////////////////////////////// #include <asm/types.h> #include <netinet/ether.h> #include <netinet/in.h> #include <net/if.h> #include <stdio.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <sys/types.h> #define BUFSIZE 192 struct route_info{ u_int dstAddr; u_int srcAddr; u_int gateWay; char ifName[IF_NAMESIZE]; }; void fillRoute (struct route_info *rinfo, const char* dstAddr, const char* srcAddr, const char* gateway, const char* ifName) { /* Convert from the standrad numbers and dots notation to binary data */ inet_aton("192.168.51.0", (struct in_addr *)&rinfo->dstAddr); inet_aton("192.168.51.90", (struct in_addr *)&rinfo->gateWay); } int addAttr (struct nlmsghdr *nlhdr, int maxlen, int type, void *data, int alen) { struct rtattr *rta; int len = RTA_LENGTH(alen); if (NLMSG_ALIGN(nlhdr->nlmsg_len) + len > maxlen) return -1; rta = (struct rtattr*)((char *)nlhdr + NLMSG_ALIGN(nlhdr->nlmsg_len)); rta->rta_type = type; rta->rta_len = len; memcpy(RTA_DATA(rta), data, alen); nlhdr->nlmsg_len = NLMSG_ALIGN(nlhdr->nlmsg_len) + len; return 0; } int main() { struct nlmsghdr *nlMsg; struct rtmsg *rtMsg; char dstAddr[30] ; char srcAddr[30] ; char gateway[30] ; char ifName[30]; char msgBuf[BUFSIZE]; struct route_info rinfo; int sock, len, msgSeq = 0; int val, i; /* Create Socket */ if((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) perror("Socket Creation: "); /* Initialize the buffer */ memset(msgBuf, 0, BUFSIZE); /* point the header and the msg structure pointers into the buffer */ nlMsg = (struct nlmsghdr *)msgBuf; rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg); /* Fill in the nlmsg header*/ nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length ofmessage. nlMsg->nlmsg_type = RTM_NEWROUTE; // Get the routes from kernel routing table . nlMsg->nlmsg_flags = NLM_F_CREATE ; // The message is a request for dump. nlMsg->nlmsg_seq = msgSeq++; // Sequence of the message packet. nlMsg->nlmsg_pid = getpid(); // PID of process sending the request. rtMsg->rtm_family = AF_INET; rtMsg->rtm_table = RT_TABLE_UNSPEC; rtMsg->rtm_dst_len = 16; rtMsg->rtm_src_len = 16; rtMsg->rtm_scope = RT_SCOPE_UNIVERSE; rtMsg->rtm_type = RTN_UNICAST; rtMsg->rtm_protocol = RTPROT_UNSPEC; rtMsg->rtm_flags = RTM_F_NOTIFY; fillRoute (&rinfo, dstAddr, srcAddr, gateway, ifName); addAttr (nlMsg, BUFSIZE, RTA_DST, &rinfo.dstAddr, 4); addAttr (nlMsg, BUFSIZE, RTA_GATEWAY, &rinfo.gateWay, 4); /* Send the request */ if((val = send(sock, nlMsg, nlMsg->nlmsg_len,0 )) < 0){ printf("Write To Socket Failed...\n"); return -1; } printf (" No of Bytes sent %d \n", val); printf (" Value that is sent \n " ); for (i =0 ; i < val ; i ++) printf ("%u", msgBuf[i]); printf ("\n"); close(sock); return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////////////// OUTPUT [root@vijdom]gcc netlink_addroute.c -o addroute [root@vijdom]# ./addroute No of Bytes sent 44 Value that is sent 4400024004000042949672398800216160000101008010429496723242949672085108050429496723242949672085190 ////////////////////////////////////////////////////////////////////////////////////////////////////////// SCREEN SHOTS Routing table before execution of program Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.51.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 127.0.0.0 * 255.0.0.0 U 0 0 0 lo default embedded 0.0.0.0 UG 0 0 0 eth0 Routing table after the execution of program Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.51.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 127.0.0.0 * 255.0.0.0 U 0 0 0 lo default embedded 0.0.0.0 UG 0 0 0 eth0 --------------------------------- Find out what India is talking about on Yahoo! Answers India. SMS memory full? Store all your important SMS in your Yahoo! Mail. Register for SMS BAK UP now! _______________________________________________ LARTC mailing list LARTC@mailman.ds9a.nl http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc
VijayaLakshmi Seshadri wrote:> Hi all > Iam trying to implement "route add " using netlink. The changes are > not reflected in the routing table. I have given my code and screen > shots of the routing tables. > > Can anybody tell me is there any mistake iam making in defining the > fields . > or any other mistake iam commiting > > thanxs > > viji > > //////////////////////////////////// CODE > ////////////////////////////////////////////////////// > #include <asm/types.h> > #include <netinet/ether.h> > #include <netinet/in.h> > #include <net/if.h> > #include <stdio.h> > #include <sys/socket.h> > #include <sys/ioctl.h> > #include <linux/netlink.h> > #include <linux/rtnetlink.h> > #include <sys/types.h> > #define BUFSIZE 192 > struct route_info{ > u_int dstAddr; > u_int srcAddr; > u_int gateWay; > char ifName[IF_NAMESIZE]; > }; > void fillRoute (struct route_info *rinfo, const char* dstAddr, > const char* srcAddr, const char* gateway, const char* > ifName) > { > /* Convert from the standrad numbers and dots notation > to binary data */ > inet_aton("192.168.51.0", (struct in_addr *)&rinfo->dstAddr); > inet_aton("192.168.51.90", (struct in_addr *)&rinfo->gateWay); > } > int addAttr (struct nlmsghdr *nlhdr, int maxlen, int type, > void *data, int alen) > { > struct rtattr *rta; > int len = RTA_LENGTH(alen); > if (NLMSG_ALIGN(nlhdr->nlmsg_len) + len > maxlen) > return -1; > rta = (struct rtattr*)((char *)nlhdr + > NLMSG_ALIGN(nlhdr->nlmsg_len)); > rta->rta_type = type; > rta->rta_len = len; > memcpy(RTA_DATA(rta), data, alen); > nlhdr->nlmsg_len = NLMSG_ALIGN(nlhdr->nlmsg_len) + len; > return 0; > } > int main() > { > struct nlmsghdr *nlMsg; > struct rtmsg *rtMsg; > char dstAddr[30] ; > char srcAddr[30] ; > char gateway[30] ; > char ifName[30]; > char msgBuf[BUFSIZE]; > struct route_info rinfo; > int sock, len, msgSeq = 0; > int val, i; > /* Create Socket */ > if((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) > perror("Socket Creation: "); > /* Initialize the buffer */ > memset(msgBuf, 0, BUFSIZE); > /* point the header and the msg structure pointers into the > buffer */ > nlMsg = (struct nlmsghdr *)msgBuf; > rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg); > /* Fill in the nlmsg header*/ > nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // > Length ofmessage. > nlMsg->nlmsg_type = RTM_NEWROUTE; // Get > the routes from kernel routing table . > nlMsg->nlmsg_flags = NLM_F_CREATE ; // The message is a > request for dump. > nlMsg->nlmsg_seq = msgSeq++; // > Sequence of the message packet. > nlMsg->nlmsg_pid = getpid(); // PID > of process sending the request. > rtMsg->rtm_family = AF_INET; > rtMsg->rtm_table = RT_TABLE_UNSPEC; > rtMsg->rtm_dst_len = 16; > rtMsg->rtm_src_len = 16; > rtMsg->rtm_scope = RT_SCOPE_UNIVERSE; > rtMsg->rtm_type = RTN_UNICAST; > rtMsg->rtm_protocol = RTPROT_UNSPEC; > rtMsg->rtm_flags = RTM_F_NOTIFY; > fillRoute (&rinfo, dstAddr, srcAddr, gateway, ifName); > addAttr (nlMsg, BUFSIZE, RTA_DST, > &rinfo.dstAddr, 4); > addAttr (nlMsg, BUFSIZE, RTA_GATEWAY, > &rinfo.gateWay, 4); > /* Send the request */ > if((val = send(sock, nlMsg, nlMsg->nlmsg_len,0 )) < 0){ > printf("Write To Socket Failed...\n"); > return -1; > } > printf (" No of Bytes sent %d \n", val); > printf (" Value that is sent \n " ); > for (i =0 ; i < val ; i ++) > printf ("%u", msgBuf[i]); > printf ("\n"); > close(sock); > return 0; > } > > ////////////////////////////////////////////////////////////////////////////////////////////////////////// > OUTPUT > [root@vijdom]gcc netlink_addroute.c -o addroute > [root@vijdom]# ./addroute > No of Bytes sent 44 > Value that is sent > 4400024004000042949672398800216160000101008010429496723242949672085108050429496723242949672085190 > ////////////////////////////////////////////////////////////////////////////////////////////////////////// > > SCREEN SHOTS > > *Routing table before execution of program* > Kernel IP routing table > Destination Gateway Genmask Flags Metric Ref > Use Iface > 192.168.51.0 * 255.255.255.0 U 0 0 > 0 eth0 > 169.254.0.0 * 255.255.0.0 U 0 0 > 0 eth0 > 127.0.0.0 * 255.0.0.0 U 0 0 0 lo > default embedded 0.0.0.0 UG 0 0 > 0 eth0 > > *Routing table after the execution of program > *Kernel IP routing table > Destination Gateway Genmask Flags Metric Ref > Use Iface > 192.168.51.0 * 255.255.255.0 U 0 0 > 0 eth0 > 169.254.0.0 * 255.255.0.0 U 0 0 > 0 eth0 > 127.0.0.0 * 255.0.0.0 U 0 0 0 lo > default embedded 0.0.0.0 UG 0 0 > 0 eth0 > > > > > > > ------------------------------------------------------------------------ > Find out what India is talking about on Yahoo! Answers India. > <http://us.rd.yahoo.com/mail/in/mailanswersshare/*http://in.answers.yahoo.com/> > SMS memory full? Store all your important SMS in your Yahoo! Mail. > Register for SMS BAK UP now! > <http://us.rd.yahoo.com/mail/in/mailbackup/*http://in.mobile.yahoo.com/smsbakup.html> > > ------------------------------------------------------------------------ > > _______________________________________________ > LARTC mailing list > LARTC@mailman.ds9a.nl > http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc >I have a similar network 4 /24 ip classess splitet in 7 regions. rhus i have to do routing. For this i use::: /sbin/ip route add class via ip for eatch region. ex::: /sbin/ip route add 89.34.23.0/26 via 81.181.180.21 89.34.23.0/26 > region ip address 81.181.180.21 > router ip This i what i use.... looking forward to learning about netlink, Good luck, Adorean Alexandru Raul
On 25-07-2006 16:59, VijayaLakshmi Seshadri wrote:> Hi all > Iam trying to implement "route add " using netlink. The changes are not > reflected in the routing table. I have given my code and screen shots of > the routing tables. > > Can anybody tell me is there any mistake iam making in defining the fields . > or any other mistake iam commiting > > thanxs > > vijiI had some free time at the weekend - it''s probably to late and I hope you''ve found this bugs yet, but maybe someone else (like me) will be looking here some day with similar problem, so here is what I''ve found. Jarek P> > //////////////////////////////////// CODE > ////////////////////////////////////////////////////// > #include <asm/types.h> > #include <netinet/ether.h> > #include <netinet/in.h> > #include <net/if.h> > #include <stdio.h> > #include <sys/socket.h> > #include <sys/ioctl.h> > #include <linux/netlink.h> > #include <linux/rtnetlink.h> > #include <sys/types.h> > #define BUFSIZE 192 > struct route_info{ > u_int dstAddr; > u_int srcAddr; > u_int gateWay; > char ifName[IF_NAMESIZE]; > }; > void fillRoute (struct route_info *rinfo, const char* dstAddr, > const char* srcAddr, const char* gateway, const char* > ifName) > { > /* Convert from the standrad numbers and dots notation > to binary data */ > inet_aton("192.168.51.0", (struct in_addr *)&rinfo->dstAddr); > inet_aton("192.168.51.90", (struct in_addr *)&rinfo->gateWay); > }Of corse you always have to be sure to have the valid route to 192.168.51.90 on the testing box...> int addAttr (struct nlmsghdr *nlhdr, int maxlen, int type, > void *data, int alen) > { > struct rtattr *rta; > int len = RTA_LENGTH(alen); > if (NLMSG_ALIGN(nlhdr->nlmsg_len) + len > maxlen) > return -1; > rta = (struct rtattr*)((char *)nlhdr + > NLMSG_ALIGN(nlhdr->nlmsg_len)); > rta->rta_type = type; > rta->rta_len = len; > memcpy(RTA_DATA(rta), data, alen); > nlhdr->nlmsg_len = NLMSG_ALIGN(nlhdr->nlmsg_len) + len; > return 0; > } > int main() > { > struct nlmsghdr *nlMsg; > struct rtmsg *rtMsg; > char dstAddr[30] ; > char srcAddr[30] ; > char gateway[30] ; > char ifName[30]; > char msgBuf[BUFSIZE]; > struct route_info rinfo; > int sock, len, msgSeq = 0; > int val, i; > /* Create Socket */ > if((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) > perror("Socket Creation: "); > /* Initialize the buffer */ > memset(msgBuf, 0, BUFSIZE); > /* point the header and the msg structure pointers into the > buffer */ > nlMsg = (struct nlmsghdr *)msgBuf; > rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg); > /* Fill in the nlmsg header*/ > nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length > ofmessage. > nlMsg->nlmsg_type = RTM_NEWROUTE; // Get > the routes from kernel routing table . > nlMsg->nlmsg_flags = NLM_F_CREATE ; // The message is a// the flag is needed here nlMsg->nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;> request for dump. > nlMsg->nlmsg_seq = msgSeq++; // > Sequence of the message packet. > nlMsg->nlmsg_pid = getpid(); // PID of > process sending the request. > rtMsg->rtm_family = AF_INET; > rtMsg->rtm_table = RT_TABLE_UNSPEC; > rtMsg->rtm_dst_len = 16; > rtMsg->rtm_src_len = 16;// this should be address'' lenghts in bits so: rtMsg->rtm_dst_len = 32; rtMsg->rtm_src_len = 32;> rtMsg->rtm_scope = RT_SCOPE_UNIVERSE; > rtMsg->rtm_type = RTN_UNICAST; > rtMsg->rtm_protocol = RTPROT_UNSPEC; > rtMsg->rtm_flags = RTM_F_NOTIFY; > fillRoute (&rinfo, dstAddr, srcAddr, gateway, ifName); > addAttr (nlMsg, BUFSIZE, RTA_DST, > &rinfo.dstAddr, 4); > addAttr (nlMsg, BUFSIZE, RTA_GATEWAY, > &rinfo.gateWay, 4); > /* Send the request */ > if((val = send(sock, nlMsg, nlMsg->nlmsg_len,0 )) < 0){ > printf("Write To Socket Failed...\n"); > return -1; > } > printf (" No of Bytes sent %d \n", val); > printf (" Value that is sent \n " ); > for (i =0 ; i < val ; i ++) > printf ("%u", msgBuf[i]); > printf ("\n"); > close(sock); > return 0; > } > > ////////////////////////////////////////////////////////////////////////////////////////////////////////// > OUTPUT > [root@vijdom]gcc netlink_addroute.c -o addroute > [root@vijdom]# ./addroute > No of Bytes sent 44 > Value that is sent > 4400024004000042949672398800216160000101008010429496723242949672085108050429496723242949672085190 > ////////////////////////////////////////////////////////////////////////////////////////////////////////// > > SCREEN SHOTS > > *Routing table before execution of program* > Kernel IP routing table > Destination Gateway Genmask Flags Metric Ref Use > Iface > 192.168.51.0 * 255.255.255.0 U 0 0 0 eth0 > 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 > 127.0.0.0 * 255.0.0.0 U 0 0 0 lo > default embedded 0.0.0.0 UG 0 0 0 eth0 > > *Routing table after the execution of program > *Kernel IP routing table > Destination Gateway Genmask Flags Metric Ref Use > Iface > 192.168.51.0 * 255.255.255.0 U 0 0 0 eth0 > 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0 > 127.0.0.0 * 255.0.0.0 U 0 0 0 lo > default embedded 0.0.0.0 UG 0 0 0 eth0 > > > > > > > ------------------------------------------------------------------------ > Find out what India is talking about on Yahoo! Answers India. > <http://us.rd.yahoo.com/mail/in/mailanswersshare/*http://in.answers.yahoo.com/> > SMS memory full? Store all your important SMS in your Yahoo! Mail. > Register for SMS BAK UP now! > <http://us.rd.yahoo.com/mail/in/mailbackup/*http://in.mobile.yahoo.com/smsbakup.html> > > > > ------------------------------------------------------------------------ > > _______________________________________________ > LARTC mailing list > LARTC@mailman.ds9a.nl > http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc
Reasonably Related Threads
- [Bug 1691] New: mnl_nlmsg_ok returns true on malformed/incomplete messages leading to potential runtime issues
- [Bug 714] New: Kernel panics in same_src()
- [Bridge] [PATCH v5] iproute2: add mdb sub-command to bridge
- [Bridge] [PATCH net-next v3] bridge: export multicast database via netlink
- Asterisk as Cisco Call-Manager - dial out to PSTN