]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rtread_getmsg.c
*: reindent
[mirror_frr.git] / zebra / rtread_getmsg.c
CommitLineData
718e3744 1/*
2 * Kernel routing table readup by getmsg(2)
3 * Copyright (C) 1999 Michael Handler
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "log.h"
26#include "if.h"
78104b9b 27#include "vrf.h"
82283592 28#include "vty.h"
718e3744 29
30#include "zebra/rib.h"
6621ca86 31#include "zebra/zserv.h"
718e3744 32
a5b38c5b
DL
33/* Thank you, Solaris, for polluting application symbol namespace. */
34#undef hook_register
35#undef hook_unregister
36
718e3744 37#include <sys/stream.h>
38#include <sys/tihdr.h>
39
40/* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
41#ifdef SUNOS_5
42#include <sys/tiuser.h>
43#ifndef T_CURRENT
44#define T_CURRENT MI_T_CURRENT
45#endif /* T_CURRENT */
46#ifndef IRE_CACHE
47#define IRE_CACHE 0x0020 /* Cached Route entry */
48#endif /* IRE_CACHE */
49#ifndef IRE_HOST_REDIRECT
50#define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
51#endif /* IRE_HOST_REDIRECT */
52#ifndef IRE_CACHETABLE
d62a17ae 53#define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
718e3744 54#endif /* IRE_CACHETABLE */
55#undef IPOPT_EOL
56#undef IPOPT_NOP
57#undef IPOPT_LSRR
58#undef IPOPT_RR
59#undef IPOPT_SSRR
60#endif /* SUNOS_5 */
61
62#include <inet/common.h>
63#include <inet/ip.h>
64#include <inet/mib2.h>
65
66/* device to read IP routing table from */
67#ifndef _PATH_GETMSG_ROUTE
68#define _PATH_GETMSG_ROUTE "/dev/ip"
69#endif /* _PATH_GETMSG_ROUTE */
70
71#define RT_BUFSIZ 8192
72
d62a17ae 73static void handle_route_entry(mib2_ipRouteEntry_t *routeEntry)
718e3744 74{
d62a17ae 75 struct prefix prefix;
76 struct in_addr tmpaddr, gateway;
77 union g_addr *ggateway;
78 u_char zebra_flags = 0;
718e3744 79
d62a17ae 80 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
81 return;
718e3744 82
d62a17ae 83 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
84 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
718e3744 85
d62a17ae 86 prefix.family = AF_INET;
718e3744 87
d62a17ae 88 tmpaddr.s_addr = routeEntry->ipRouteDest;
89 prefix.u.prefix4 = tmpaddr;
718e3744 90
d62a17ae 91 tmpaddr.s_addr = routeEntry->ipRouteMask;
92 prefix.prefixlen = ip_masklen(tmpaddr);
718e3744 93
d62a17ae 94 gateway.s_addr = routeEntry->ipRouteNextHop;
95 ggateway = (union g_addr *)&gateway;
718e3744 96
d62a17ae 97 rib_add(AFI_IP, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL, 0,
98 zebra_flags, &prefix, NULL, ggateway, NULL, 0, 0, 0, 0, 0);
718e3744 99}
100
d62a17ae 101void route_read(struct zebra_ns *zns)
718e3744 102{
d62a17ae 103 char storage[RT_BUFSIZ];
718e3744 104
d62a17ae 105 struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *)storage;
106 struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *)storage;
107 struct T_error_ack *TLIerr = (struct T_error_ack *)storage;
718e3744 108
d62a17ae 109 struct opthdr *MIB2hdr;
718e3744 110
d62a17ae 111 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
718e3744 112
d62a17ae 113 struct strbuf msgdata;
114 int flags, dev, retval, process;
718e3744 115
d62a17ae 116 if ((dev = open(_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
117 zlog_warn("can't open %s: %s", _PATH_GETMSG_ROUTE,
118 safe_strerror(errno));
718e3744 119 return;
120 }
121
122 TLIreq->PRIM_type = T_OPTMGMT_REQ;
d62a17ae 123 TLIreq->OPT_offset = sizeof(struct T_optmgmt_req);
124 TLIreq->OPT_length = sizeof(struct opthdr);
718e3744 125 TLIreq->MGMT_flags = T_CURRENT;
126
d62a17ae 127 MIB2hdr = (struct opthdr *)&TLIreq[1];
718e3744 128
129 MIB2hdr->level = MIB2_IP;
130 MIB2hdr->name = 0;
131 MIB2hdr->len = 0;
d62a17ae 132
718e3744 133 msgdata.buf = storage;
d62a17ae 134 msgdata.len = sizeof(struct T_optmgmt_req) + sizeof(struct opthdr);
718e3744 135
136 flags = 0;
137
d62a17ae 138 if (putmsg(dev, &msgdata, NULL, flags) == -1) {
139 zlog_warn("putmsg failed: %s", safe_strerror(errno));
718e3744 140 goto exit;
141 }
142
d62a17ae 143 MIB2hdr = (struct opthdr *)&TLIack[1];
144 msgdata.maxlen = sizeof(storage);
718e3744 145
146 while (1) {
147 flags = 0;
d62a17ae 148 retval = getmsg(dev, &msgdata, NULL, &flags);
718e3744 149
150 if (retval == -1) {
d62a17ae 151 zlog_warn("getmsg(ctl) failed: %s",
152 safe_strerror(errno));
718e3744 153 goto exit;
154 }
155
156 /* This is normal loop termination */
d62a17ae 157 if (retval == 0
158 && (size_t)msgdata.len >= sizeof(struct T_optmgmt_ack)
159 && TLIack->PRIM_type == T_OPTMGMT_ACK
160 && TLIack->MGMT_flags == T_SUCCESS && MIB2hdr->len == 0)
718e3744 161 break;
162
d62a17ae 163 if ((size_t)msgdata.len >= sizeof(struct T_error_ack)
164 && TLIerr->PRIM_type == T_ERROR_ACK) {
165 zlog_warn("getmsg(ctl) returned T_ERROR_ACK: %s",
166 safe_strerror((TLIerr->TLI_error == TSYSERR)
167 ? TLIerr->UNIX_error
168 : EPROTO));
718e3744 169 break;
170 }
171
172 /* should dump more debugging info to the log statement,
173 like what GateD does in this instance, but not
174 critical yet. */
d62a17ae 175 if (retval != MOREDATA
176 || (size_t)msgdata.len < sizeof(struct T_optmgmt_ack)
177 || TLIack->PRIM_type != T_OPTMGMT_ACK
178 || TLIack->MGMT_flags != T_SUCCESS) {
718e3744 179 errno = ENOMSG;
d62a17ae 180 zlog_warn("getmsg(ctl) returned bizarreness");
718e3744 181 break;
182 }
183
184 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
185 entry, see <inet/mib2.h>. "This isn't the MIB data
186 you're looking for." */
d62a17ae 187 process = (MIB2hdr->level == MIB2_IP
188 && MIB2hdr->name == MIB2_IP_21)
189 ? 1
190 : 0;
718e3744 191
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
197 retrieving. */
d62a17ae 198 msgdata.maxlen =
199 sizeof(storage)
200 - (sizeof(storage) % sizeof(mib2_ipRouteEntry_t));
718e3744 201
202 msgdata.len = 0;
203 flags = 0;
204
205 do {
d62a17ae 206 retval = getmsg(dev, NULL, &msgdata, &flags);
718e3744 207
208 if (retval == -1) {
d62a17ae 209 zlog_warn("getmsg(data) failed: %s",
210 safe_strerror(errno));
718e3744 211 goto exit;
212 }
213
214 if (!(retval == 0 || retval == MOREDATA)) {
d62a17ae 215 zlog_warn("getmsg(data) returned %d", retval);
718e3744 216 goto exit;
217 }
218
219 if (process) {
d62a17ae 220 if (msgdata.len % sizeof(mib2_ipRouteEntry_t)
221 != 0) {
222 zlog_warn(
223 "getmsg(data) returned "
224 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)",
225 msgdata.len);
718e3744 226 goto exit;
227 }
228
d62a17ae 229 routeEntry = (mib2_ipRouteEntry_t *)msgdata.buf;
230 lastRouteEntry =
231 (mib2_ipRouteEntry_t *)(msgdata.buf
232 + msgdata.len);
718e3744 233 do {
d62a17ae 234 handle_route_entry(routeEntry);
718e3744 235 } while (++routeEntry < lastRouteEntry);
236 }
237 } while (retval == MOREDATA);
238 }
239
240exit:
d62a17ae 241 close(dev);
718e3744 242}
2232a77c 243
244/* Only implemented for netlink method */
d62a17ae 245void macfdb_read(struct zebra_ns *zns)
2232a77c 246{
247}
248
d62a17ae 249void macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
250 struct interface *br_if)
2232a77c 251{
252}
253
d62a17ae 254void neigh_read(struct zebra_ns *zns)
2232a77c 255{
256}
257
d62a17ae 258void neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 259{
260}