2 * Kernel routing readup by /proc filesystem
3 * Copyright (C) 1997 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "zebra/zserv.h"
33 /* Proc file system to read IPv4 routing table. */
34 #ifndef _PATH_PROCNET_ROUTE
35 #define _PATH_PROCNET_ROUTE "/proc/net/route"
36 #endif /* _PATH_PROCNET_ROUTE */
38 /* Proc file system to read IPv6 routing table. */
39 #ifndef _PATH_PROCNET_ROUTE6
40 #define _PATH_PROCNET_ROUTE6 "/proc/net/ipv6_route"
41 #endif /* _PATH_PROCNET_ROUTE6 */
43 /* To read interface's name */
44 #define INTERFACE_NAMSIZ 20
46 /* Reading buffer for one routing entry. */
47 #define RT_BUFSIZ 1024
49 /* Kernel routing table read up by /proc filesystem. */
51 proc_route_read (void)
55 char iface
[INTERFACE_NAMSIZ
], dest
[9], gate
[9], mask
[9];
56 int flags
, refcnt
, use
, metric
, mtu
, window
, rtt
;
58 /* Open /proc filesystem */
59 fp
= fopen (_PATH_PROCNET_ROUTE
, "r");
62 zlog_warn ("Can't open %s : %s\n", _PATH_PROCNET_ROUTE
, safe_strerror (errno
));
66 /* Drop first label line. */
67 fgets (buf
, RT_BUFSIZ
, fp
);
69 while (fgets (buf
, RT_BUFSIZ
, fp
) != NULL
)
73 struct in_addr tmpmask
;
74 struct in_addr gateway
;
75 u_char zebra_flags
= 0;
77 n
= sscanf (buf
, "%s %s %s %x %d %d %d %s %d %d %d",
78 iface
, dest
, gate
, &flags
, &refcnt
, &use
, &metric
,
79 mask
, &mtu
, &window
, &rtt
);
82 zlog_warn ("can't read all of routing information\n");
85 if (! (flags
& RTF_UP
))
87 if (! (flags
& RTF_GATEWAY
))
90 if (flags
& RTF_DYNAMIC
)
91 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
94 sscanf (dest
, "%lX", (unsigned long *)&p
.prefix
);
95 sscanf (mask
, "%lX", (unsigned long *)&tmpmask
);
96 p
.prefixlen
= ip_masklen (tmpmask
);
97 sscanf (gate
, "%lX", (unsigned long *)&gateway
);
99 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
, &gateway
, NULL
, 0, 0, 0, 0);
108 proc_ipv6_route_read ()
111 char buf
[RT_BUFSIZ
];
113 /* Open /proc filesystem */
114 fp
= fopen (_PATH_PROCNET_ROUTE6
, "r");
117 zlog_warn ("Can't open %s : %s", _PATH_PROCNET_ROUTE6
,
118 safe_strerror (errno
));
122 /* There is no title line, so we don't drop first line. */
123 while (fgets (buf
, RT_BUFSIZ
, fp
) != NULL
)
126 char dest
[33], src
[33], gate
[33];
127 char iface
[INTERFACE_NAMSIZ
];
128 int dest_plen
, src_plen
;
129 int metric
, use
, refcnt
, flags
;
130 struct prefix_ipv6 p
;
131 struct in6_addr gateway
;
132 u_char zebra_flags
= 0;
134 /* Linux 2.1.x write this information at net/ipv6/route.c
136 n
= sscanf (buf
, "%32s %02x %32s %02x %32s %08x %08x %08x %08x %s",
137 dest
, &dest_plen
, src
, &src_plen
, gate
,
138 &metric
, &use
, &refcnt
, &flags
, iface
);
142 /* zlog_warn ("can't read all of routing information %d\n%s\n", n, buf); */
146 if (! (flags
& RTF_UP
))
148 if (! (flags
& RTF_GATEWAY
))
151 if (flags
& RTF_DYNAMIC
)
152 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
155 str2in6_addr (dest
, &p
.prefix
);
156 str2in6_addr (gate
, &gateway
);
157 p
.prefixlen
= dest_plen
;
159 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &p
, &gateway
, 0, 0,
166 #endif /* HAVE_IPV6 */
173 proc_ipv6_route_read ();
174 #endif /* HAVE_IPV6 */