Skip to contentSkip to conversation

netlink socket problem

DISCUSSION Thomas Andrews 1 participant 2 messages Oct 22, 2005 netfilter

lore
Share & tools
lore
#0 lore raw

I hope this isn't considered off-topic. I'm trying to get some code to talk to a netlink socket, but I keep getting this error:

RTNETLINK answers: Invalid argument

In this example I am just trying to add a route:

-------------8<-------------------------------------------------------

int add_route(__u32 * destination, __u32 * gateway)

{
    struct rtnl_handle rth;

    struct {
        struct nlmsghdr n;
        struct rtmsg r;
        char buf[1024];
    } req;

    memset(&req, 0, sizeof(req));

    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;

    addattr_l(&req.n, sizeof(req), RTA_DST, destination, 4);
    addattr_l(&req.n, sizeof(req), RTA_GATEWAY, gateway, 4);

    if (rtnl_open(&rth,0) < 0)
        return (-1);

    if (rtnl_talk(&rth, &req.n, 0, 0, NULL,NULL,NULL) < 0)
        return (-1);

    return 0;
}

-------------8<-------------------------------------------------------

What am I doing wrong in this function ?

Many thanks,
Thomas

#1 lore raw
Quoted 4 lines · Thomas Andrews

I hope this isn't considered off-topic. I'm trying to get some code to talk to a netlink socket, but I keep getting this error:

RTNETLINK answers: Invalid argument

To answer my own question - the destination IP *must* match req.r.rtm_dst_len (which must also be set up, or it will be 0). By that I mean the IP must have already been ANDed with the netmask in rtm_dst_len.

Hopefully nobody else will waste a whole day figuring that out :p

Thomas