2 * Kernel routing table readup by getmsg(2)
3 * Copyright (C) 1999 Michael Handler
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
31 #include "zebra/rib.h"
32 #include "zebra/zserv.h"
34 #include <sys/stream.h>
35 #include <sys/tihdr.h>
37 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
39 #include <sys/tiuser.h>
41 #define T_CURRENT MI_T_CURRENT
42 #endif /* T_CURRENT */
44 #define IRE_CACHE 0x0020 /* Cached Route entry */
45 #endif /* IRE_CACHE */
46 #ifndef IRE_HOST_REDIRECT
47 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
48 #endif /* IRE_HOST_REDIRECT */
49 #ifndef IRE_CACHETABLE
50 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
52 #endif /* IRE_CACHETABLE */
60 #include <inet/common.h>
62 #include <inet/mib2.h>
64 /* device to read IP routing table from */
65 #ifndef _PATH_GETMSG_ROUTE
66 #define _PATH_GETMSG_ROUTE "/dev/ip"
67 #endif /* _PATH_GETMSG_ROUTE */
69 #define RT_BUFSIZ 8192
72 handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
74 struct prefix_ipv4 prefix;
75 struct in_addr tmpaddr, gateway;
76 u_char zebra_flags = 0;
78 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
81 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
82 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
84 prefix.family = AF_INET;
86 tmpaddr.s_addr = routeEntry->ipRouteDest;
87 prefix.prefix = tmpaddr;
89 tmpaddr.s_addr = routeEntry->ipRouteMask;
90 prefix.prefixlen = ip_masklen (tmpaddr);
92 gateway.s_addr = routeEntry->ipRouteNextHop;
94 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
95 &gateway, NULL, 0, VRF_DEFAULT, RT_TABLE_MAIN,
96 0, 0, 0, SAFI_UNICAST);
100 route_read (struct zebra_vrf *zvrf)
102 char storage[RT_BUFSIZ];
104 struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage;
105 struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage;
106 struct T_error_ack *TLIerr = (struct T_error_ack *) storage;
108 struct opthdr *MIB2hdr;
110 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
112 struct strbuf msgdata;
113 int flags, dev, retval, process;
115 if (zvrf->vrf_id != VRF_DEFAULT) {
119 if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
120 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
121 safe_strerror (errno));
125 TLIreq->PRIM_type = T_OPTMGMT_REQ;
126 TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
127 TLIreq->OPT_length = sizeof (struct opthdr);
128 TLIreq->MGMT_flags = T_CURRENT;
130 MIB2hdr = (struct opthdr *) &TLIreq[1];
132 MIB2hdr->level = MIB2_IP;
136 msgdata.buf = storage;
137 msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
141 if (putmsg (dev, &msgdata, NULL, flags) == -1) {
142 zlog_warn ("putmsg failed: %s", safe_strerror (errno));
146 MIB2hdr = (struct opthdr *) &TLIack[1];
147 msgdata.maxlen = sizeof (storage);
151 retval = getmsg (dev, &msgdata, NULL, &flags);
154 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
158 /* This is normal loop termination */
160 (size_t)msgdata.len >= sizeof (struct T_optmgmt_ack) &&
161 TLIack->PRIM_type == T_OPTMGMT_ACK &&
162 TLIack->MGMT_flags == T_SUCCESS &&
166 if ((size_t)msgdata.len >= sizeof (struct T_error_ack) &&
167 TLIerr->PRIM_type == T_ERROR_ACK) {
168 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
169 safe_strerror ((TLIerr->TLI_error == TSYSERR)
170 ? TLIerr->UNIX_error : EPROTO));
174 /* should dump more debugging info to the log statement,
175 like what GateD does in this instance, but not
177 if (retval != MOREDATA ||
178 (size_t)msgdata.len < sizeof (struct T_optmgmt_ack) ||
179 TLIack->PRIM_type != T_OPTMGMT_ACK ||
180 TLIack->MGMT_flags != T_SUCCESS) {
182 zlog_warn ("getmsg(ctl) returned bizarreness");
186 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
187 entry, see <inet/mib2.h>. "This isn't the MIB data
188 you're looking for." */
189 process = (MIB2hdr->level == MIB2_IP &&
190 MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
192 /* getmsg writes the data buffer out completely, not
193 to the closest smaller multiple. Unless reassembling
194 data structures across buffer boundaries is your idea
195 of a good time, set maxlen to the closest smaller
196 multiple of the size of the datastructure you're
198 msgdata.maxlen = sizeof (storage) - (sizeof (storage)
199 % sizeof (mib2_ipRouteEntry_t));
205 retval = getmsg (dev, NULL, &msgdata, &flags);
208 zlog_warn ("getmsg(data) failed: %s",
209 safe_strerror (errno));
213 if (!(retval == 0 || retval == MOREDATA)) {
214 zlog_warn ("getmsg(data) returned %d", retval);
220 sizeof (mib2_ipRouteEntry_t) != 0) {
221 zlog_warn ("getmsg(data) returned "
222 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
226 routeEntry = (mib2_ipRouteEntry_t *)
228 lastRouteEntry = (mib2_ipRouteEntry_t *)
229 (msgdata.buf + msgdata.len);
231 handle_route_entry (routeEntry);
232 } while (++routeEntry < lastRouteEntry);
234 } while (retval == MOREDATA);