LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 05-22-2004, 09:14 PM   #1
mpls_linux
LQ Newbie
 
Registered: Apr 2004
Posts: 9

Rep: Reputation: 0
Adding Routes using Netlink Sockets


Hi,
I am trying to add a static routing entry into the routing table using netlink sockets. I use the following program to do so.

I have a network conf as shown


R1 (10.0.4.1)(e0) -----------------(10.0.4.0/24)------------(e0) (10.0.4.2)R2 --------(10.0.3.0/24)-----


I execute this program at R1 and want to add a static route for a host on 10.0.3.0/24 network which is done with the normal route add command as

route add 10.0.3.2/32 gw 10.0.4.2 eth0

But the sendmsg function returns a negative value and the route doesnt get added to the routing table.

I'd really appreciate if someone could help me with this. Am I missing any other options to setup a static route.

Please advise.
Thanks, in advance
Praveen


#include <stdio.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <sys/types.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
//#include <linux/mpls.h>
#include</usr/src/linux-2.4/include/linux/mpls.h>
#include <unistd.h>
#include <string.h>
#include <sys/uio.h>

typedef __u32 u32;


int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
{
int len = RTA_LENGTH(alen);
struct rtattr *rta;
if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
return -1;
rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
rta->rta_type = type;
rta->rta_len = len;
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
fprintf(stderr,"\nattr len=%d\n",n->nlmsg_len);
return 0;
}


void route_add(int fd, __u32* destination, __u32* gateway)
{
struct sockaddr_nl nladdr;
int status;

__u32 index=1; /* Output Interface ::: eth0 */
__u32 source=0;

// structure of the netlink packet.
struct
{
struct nlmsghdr n;
struct rtmsg r;
char buf[1024];
} req;

// Forming the iovector with the netlink packet.
struct iovec iov = { (void*)&req.n, req.n.nlmsg_len };

// Forming the message to be sent.
struct msghdr msg = { (void*)&nladdr, sizeof(nladdr), &iov, 1, NULL, 0, 0 };


char mxbuf[256];
struct rtattr * mxrta = (void*)mxbuf;
unsigned mxlock = 0;
memset(&req, 0, sizeof(req));

// Initialisation of a few parameters
memset(&nladdr,0,sizeof(nladdr));
nladdr.nl_family= AF_NETLINK;
nladdr.nl_pid=0;
nladdr.nl_groups=0;

req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE;
req.n.nlmsg_type = RTM_NEWROUTE;

req.r.rtm_family = AF_INET;
req.r.rtm_table = RT_TABLE_MAIN;
req.r.rtm_protocol = RTPROT_BOOT;
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
req.r.rtm_type = RTN_UNICAST;
req.r.rtm_dst_len=24;
req.r.rtm_src_len=0;
req.r.rtm_tos=0;
req.r.rtm_flags=RT_TABLE_MAIN;


mxrta->rta_type = RTA_METRICS;
mxrta->rta_len = RTA_LENGTH(0);

mxrta = (struct rtattr*)(((char*)&req.n) + NLMSG_ALIGN(req.n.nlmsg_len));
memcpy(RTA_DATA(mxrta),mxbuf,0);
req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + mxrta->rta_len;

// RTA_DST and RTA_GW are the two esential parameters for adding a route,
// there are other parameters too which are not discussed here. For ipv4,
// the length of the address is 4 bytes.

addattr_l(&req.n, sizeof(req), RTA_OIF,&index, 4);
addattr_l(&req.n, sizeof(req), RTA_SRC,&source, 4);
addattr_l(&req.n, sizeof(req), RTA_DST, destination, 4);
addattr_l(&req.n, sizeof(req), RTA_GATEWAY, gateway, 4);


// sending the packet to the kernel.
status=sendmsg(fd,&msg,0);
fprintf(stderr,"\nstatus=%d",status);

}

int main()
{
int fd;
__u32 dest,gate,source;

if((fd=rtnl_open())<0)
printf("error creating a socket\n");


dest=33751050; /* unsigned int value for dest Ip address 10.0.3.2 */
gate=33816586; /* unsigned int value for gateway address 10.0.4.2 */
source=0; /* no source specified */

route_add(fd,&dest,&gate,&source);

return 0;
}


Output :
--------------------------

attr len=36

attr len=44

attr len=52

attr len=60

status=-1
-----------------------------
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel module using netlink sockets arunka Programming 1 03-31-2005 07:42 AM
Netlink Sockets Problem gio123 Programming 3 01-23-2005 06:17 PM
Adding static routes to fc1 router Tom123 Linux - Networking 1 08-20-2004 03:28 AM
comments and opinions on linux netlink routing sockets tutorial infamous41md Programming 3 11-20-2003 05:54 PM
netlink hacking infamous41md Linux - General 3 11-16-2003 11:10 PM


All times are GMT -5. The time now is 09:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration