/proc/net/route, /proc/net/rt_cach, sysctl ??

/proc/net/route, /proc/net/rt_cach, sysctl ??

Post by Frederic Pon » Wed, 21 Jul 1999 04:00:00



Hi all,

My C application (kernel 2.2.9) needs to figure out if there is an entry
for a given IP address in the kernel routing table, and if there is
one entry, what is its metric. (By "if there is an entry", I mean if the
kernel is able to route for this destination, and I'm not interested in
the default route)

What is the best way to do it ?
I can read /proc/net/route, or /proc/net/rt_cache, but I cannot figure
out what is the difference between them, if they are updated often etc..

I tried to use sysctl, (unix network programming by R Stevens, Section
17.4), but the names of figure 17.12 don't work with kernel 2.2.9 and I
don't know where to find out more about this hierarchical arrangement,
or maybe it's just not implemented on Linux..

Any idea, comment, suggestion or flame welcome :-)

Thanks for your help.
Fred

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 
 
 

/proc/net/route, /proc/net/rt_cach, sysctl ??

Post by Andi Klee » Wed, 21 Jul 1999 04:00:00



> Hi all,

> My C application (kernel 2.2.9) needs to figure out if there is an entry
> for a given IP address in the kernel routing table, and if there is
> one entry, what is its metric. (By "if there is an entry", I mean if the
> kernel is able to route for this destination, and I'm not interested in
> the default route)

Linux does not support traditional "metrics" for routes, so it is not
very interesting. Linux 2.2 has the concept of a routing scope
(host, link, site, universe) though.

Quote:> What is the best way to do it ?
> I can read /proc/net/route, or /proc/net/rt_cache, but I cannot figure
> out what is the difference between them, if they are updated often etc..

rt_cache is the routing cache that is used by active connections,
route is the backing routing table(s).
Note that route only contains a subset of the full 2.2 kernel routing
information.

Quote:

> I tried to use sysctl, (unix network programming by R Stevens, Section
> 17.4), but the names of figure 17.12 don't work with kernel 2.2.9 and I
> don't know where to find out more about this hierarchical arrangement,
> or maybe it's just not implemented on Linux..

Linux has sysctls, but not for routes.

Quote:

> Any idea, comment, suggestion or flame welcome :-)

rtnetlink can nicely do what you want. You send a query message (RTM_GETROUTE)
with a "struct rtmsg" to the (PF_NETLINK,NETLINK_ROUTE) socket and
the kernel will return all matching routes. The nice thing is that it works
with policy routing etc.

Available documentation is unfortunately a bit terse ATM (man-pages-1.24
has rather incomplete netlink(7)/rtnetlink(7) and there is
/usr/include/linux/rtnetlink.h), but you can steal the relevant source
from iproute2 (ftp.inr.ac.ru:/ip-routing/ip*)

Here is an example to find e.g. the outgoing interface for a destination
via rtnetlink (requires librtnetlink from iproute2). The ugly interface
(caller must free return instead of passing a buffer, static state) is
because it was originally writen to fit into a well known "network
security" program with very very ugly source code ;)

-Andi

/* Find the interface a packet would travel out on.  
 *
 * Needs librtnetlink
 *
 * Limits: when multiple interfaces are valid (e.g. for equal cost multipath
 *         routing) only the first is returned.
 *         Only queries the main routing table.
 *         Caller must free the interface name afterwards.
 */

char *routethrough(struct in_addr source, struct in_addr dst)
{
    static struct rtnl_handle hnd;
    static int seq;
    static int hnd_init;

    struct {
                struct nlmsghdr msg;
                struct rtmsg rt;
                char buf[512];
    } req;
    struct rtattr *rta;
    int len;

    if (hnd_init++ == 0 && (rtnl_open(&hnd,0) < 0)) {
                /* Could try the old stupid function here. */
                return NULL;
    }

    memset(&req,0,sizeof req);

    req.rt.rtm_family = AF_INET;
    req.rt.rtm_table = RT_TABLE_MAIN;
    req.rt.rtm_protocol = RTPROT_BOOT; /*?*/
    req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
    req.rt.rtm_type = RTN_UNICAST;

    len = sizeof(req.buf);
    rta = (struct rtattr *)(&req.buf);
    rta_addattr32(rta,len,RTA_SRC,source.s_addr);
    rta = RTA_NEXT(rta, len);
    rta_addattr32(rta,len,RTA_DST, dst.s_addr);

    req.msg.nlmsg_len = NLMSG_LENGTH(sizeof req);
    req.msg.nlmsg_type = RTM_GETROUTE;
    req.msg.nlmsg_flags = NLM_F_REQUEST;
    req.msg.nlmsg_seq = ++seq;

    if (rtnl_talk(&hnd, &req.msg, 0, 0, &req.msg, NULL, NULL) < 0)  
                return NULL;

    len = sizeof(req.buf);
    for (rta = (struct rtattr *)(&req.buf);
                 RTA_OK(rta, len);
                 rta = RTA_NEXT(rta, len)) {

                if (rta->rta_type == RTA_OIF) {
                        unsigned if_ind;
                        char *buf;

                        memcpy(&if_ind, RTA_DATA(rta), sizeof(unsigned));

                        buf = malloc(IFNAMSIZ);
                        if (buf && (iif_to_name(if_ind, buf) < 0)) {
                                free(buf);
                                buf = NULL;
                        }

                        return buf;
                }
    }

    return NULL;

Quote:}

-Andi

--
This is like TV. I don't like TV.

 
 
 

1. /proc/net/tcp and /proc/net/udp

I'm trying to figure out how to debug these files outputs. I have already
made my own tripwire ids component, I would like to now make a program that
makes certain the kernel isn't patched (i.e.: lkm root kit) and make sure I
can see my program listening on a port from netstat, and this file, I will
also send/recv data but thats not really related to /proc heh. I was just
wondering does someone know where the por is? I get each connection is a
line... and it has a number... but like almost everything is in hex I think
and "man proc" isn't very helpful in telling you what the information in
the files mean. Any help will be appreciated.

2. Large site UNIX security management

3. ipv4: move proc stuff from net/ipv4/af_inet.c to net/ipv4/proc.c

4. csh integers

5. Which controls packet forwarding - sysctl.conf or /proc/sys/net/ipv4/ip_forward ?

6. XTerminal logout errors

7. fix /proc/net/route missing the default route

8. Information needed about Linux > version 0.12 and < Version 0.99

9. HELP: Route can't file /proc/net/route

10. /proc/dev/net doesnt show all net device

11. move /proc/net/udp support back to net/ipv4/udp.c

12. Why don't I have /proc/net with net-2?

13. /proc/dev/net doesnt show all net devices