Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


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

Rep: Reputation: 0
Adding Routes using Netlink Sockets

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 ( -----------------( ( --------(

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

route add gw 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

#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 <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 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
nladdr.nl_family= AF_NETLINK;

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;

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

mxrta = (struct rtattr*)(((char*)&req.n) + NLMSG_ALIGN(req.n.nlmsg_len));
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.


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

printf("error creating a socket\n");

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


return 0;

Output :

attr len=36

attr len=44

attr len=52

attr len=60



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 08:42 AM
Netlink Sockets Problem gio123 Programming 3 01-23-2005 07:17 PM
Adding static routes to fc1 router Tom123 Linux - Networking 1 08-20-2004 04:28 AM
comments and opinions on linux netlink routing sockets tutorial infamous41md Programming 3 11-20-2003 06:54 PM
netlink hacking infamous41md Linux - General 3 11-17-2003 12:10 AM

All times are GMT -5. The time now is 06:49 AM.

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