[0.99] Version bump to 0.99.4
[jleu-quagga.git] / zebra / rtread_getmsg.c
blob1b0c8965f919135be1d582672f7cda0d16d58afc
1 /*
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
10 * later version.
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
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "prefix.h"
26 #include "log.h"
27 #include "if.h"
29 #include "zebra/rib.h"
30 #include "zebra/zserv.h"
32 #include <sys/stream.h>
33 #include <sys/tihdr.h>
35 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
36 #ifdef SUNOS_5
37 #include <sys/tiuser.h>
38 #ifndef T_CURRENT
39 #define T_CURRENT MI_T_CURRENT
40 #endif /* T_CURRENT */
41 #ifndef IRE_CACHE
42 #define IRE_CACHE 0x0020 /* Cached Route entry */
43 #endif /* IRE_CACHE */
44 #ifndef IRE_HOST_REDIRECT
45 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
46 #endif /* IRE_HOST_REDIRECT */
47 #ifndef IRE_CACHETABLE
48 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
49 IRE_LOOPBACK)
50 #endif /* IRE_CACHETABLE */
51 #undef IPOPT_EOL
52 #undef IPOPT_NOP
53 #undef IPOPT_LSRR
54 #undef IPOPT_RR
55 #undef IPOPT_SSRR
56 #endif /* SUNOS_5 */
58 #include <inet/common.h>
59 #include <inet/ip.h>
60 #include <inet/mib2.h>
62 /* device to read IP routing table from */
63 #ifndef _PATH_GETMSG_ROUTE
64 #define _PATH_GETMSG_ROUTE "/dev/ip"
65 #endif /* _PATH_GETMSG_ROUTE */
67 #define RT_BUFSIZ 8192
69 static void
70 handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
72 struct prefix_ipv4 prefix;
73 struct in_addr tmpaddr, gateway;
74 u_char zebra_flags = 0;
76 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
77 return;
79 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
80 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
82 prefix.family = AF_INET;
84 tmpaddr.s_addr = routeEntry->ipRouteDest;
85 prefix.prefix = tmpaddr;
87 tmpaddr.s_addr = routeEntry->ipRouteMask;
88 prefix.prefixlen = ip_masklen (tmpaddr);
90 gateway.s_addr = routeEntry->ipRouteNextHop;
92 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
93 &gateway, 0, 0, 0, 0);
96 void
97 route_read (void)
99 char storage[RT_BUFSIZ];
101 struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage;
102 struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage;
103 struct T_error_ack *TLIerr = (struct T_error_ack *) storage;
105 struct opthdr *MIB2hdr;
107 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
109 struct strbuf msgdata;
110 int flags, dev, retval, process;
112 if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
113 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
114 safe_strerror (errno));
115 return;
118 TLIreq->PRIM_type = T_OPTMGMT_REQ;
119 TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
120 TLIreq->OPT_length = sizeof (struct opthdr);
121 TLIreq->MGMT_flags = T_CURRENT;
123 MIB2hdr = (struct opthdr *) &TLIreq[1];
125 MIB2hdr->level = MIB2_IP;
126 MIB2hdr->name = 0;
127 MIB2hdr->len = 0;
129 msgdata.buf = storage;
130 msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
132 flags = 0;
134 if (putmsg (dev, &msgdata, NULL, flags) == -1) {
135 zlog_warn ("putmsg failed: %s", safe_strerror (errno));
136 goto exit;
139 MIB2hdr = (struct opthdr *) &TLIack[1];
140 msgdata.maxlen = sizeof (storage);
142 while (1) {
143 flags = 0;
144 retval = getmsg (dev, &msgdata, NULL, &flags);
146 if (retval == -1) {
147 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
148 goto exit;
151 /* This is normal loop termination */
152 if (retval == 0 &&
153 msgdata.len >= sizeof (struct T_optmgmt_ack) &&
154 TLIack->PRIM_type == T_OPTMGMT_ACK &&
155 TLIack->MGMT_flags == T_SUCCESS &&
156 MIB2hdr->len == 0)
157 break;
159 if (msgdata.len >= sizeof (struct T_error_ack) &&
160 TLIerr->PRIM_type == T_ERROR_ACK) {
161 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
162 safe_strerror ((TLIerr->TLI_error == TSYSERR)
163 ? TLIerr->UNIX_error : EPROTO));
164 break;
167 /* should dump more debugging info to the log statement,
168 like what GateD does in this instance, but not
169 critical yet. */
170 if (retval != MOREDATA ||
171 msgdata.len < sizeof (struct T_optmgmt_ack) ||
172 TLIack->PRIM_type != T_OPTMGMT_ACK ||
173 TLIack->MGMT_flags != T_SUCCESS) {
174 errno = ENOMSG;
175 zlog_warn ("getmsg(ctl) returned bizarreness");
176 break;
179 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
180 entry, see <inet/mib2.h>. "This isn't the MIB data
181 you're looking for." */
182 process = (MIB2hdr->level == MIB2_IP &&
183 MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
185 /* getmsg writes the data buffer out completely, not
186 to the closest smaller multiple. Unless reassembling
187 data structures across buffer boundaries is your idea
188 of a good time, set maxlen to the closest smaller
189 multiple of the size of the datastructure you're
190 retrieving. */
191 msgdata.maxlen = sizeof (storage) - (sizeof (storage)
192 % sizeof (mib2_ipRouteEntry_t));
194 msgdata.len = 0;
195 flags = 0;
197 do {
198 retval = getmsg (dev, NULL, &msgdata, &flags);
200 if (retval == -1) {
201 zlog_warn ("getmsg(data) failed: %s",
202 safe_strerror (errno));
203 goto exit;
206 if (!(retval == 0 || retval == MOREDATA)) {
207 zlog_warn ("getmsg(data) returned %d", retval);
208 goto exit;
211 if (process) {
212 if (msgdata.len %
213 sizeof (mib2_ipRouteEntry_t) != 0) {
214 zlog_warn ("getmsg(data) returned "
215 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
216 goto exit;
219 routeEntry = (mib2_ipRouteEntry_t *)
220 msgdata.buf;
221 lastRouteEntry = (mib2_ipRouteEntry_t *)
222 (msgdata.buf + msgdata.len);
223 do {
224 handle_route_entry (routeEntry);
225 } while (++routeEntry < lastRouteEntry);
227 } while (retval == MOREDATA);
230 exit:
231 close (dev);