]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rtread_getmsg.c
Merge branch 'cmaster' of ssh://stash.cumulusnetworks.com:7999/quag/quagga into cmaster
[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 *
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.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "log.h"
27#include "if.h"
78104b9b 28#include "vrf.h"
718e3744 29
30#include "zebra/rib.h"
6621ca86 31#include "zebra/zserv.h"
718e3744 32
33#include <sys/stream.h>
34#include <sys/tihdr.h>
35
36/* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
37#ifdef SUNOS_5
38#include <sys/tiuser.h>
39#ifndef T_CURRENT
40#define T_CURRENT MI_T_CURRENT
41#endif /* T_CURRENT */
42#ifndef IRE_CACHE
43#define IRE_CACHE 0x0020 /* Cached Route entry */
44#endif /* IRE_CACHE */
45#ifndef IRE_HOST_REDIRECT
46#define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
47#endif /* IRE_HOST_REDIRECT */
48#ifndef IRE_CACHETABLE
49#define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
50 IRE_LOOPBACK)
51#endif /* IRE_CACHETABLE */
52#undef IPOPT_EOL
53#undef IPOPT_NOP
54#undef IPOPT_LSRR
55#undef IPOPT_RR
56#undef IPOPT_SSRR
57#endif /* SUNOS_5 */
58
59#include <inet/common.h>
60#include <inet/ip.h>
61#include <inet/mib2.h>
62
63/* device to read IP routing table from */
64#ifndef _PATH_GETMSG_ROUTE
65#define _PATH_GETMSG_ROUTE "/dev/ip"
66#endif /* _PATH_GETMSG_ROUTE */
67
68#define RT_BUFSIZ 8192
69
6621ca86 70static void
71handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
718e3744 72{
73 struct prefix_ipv4 prefix;
74 struct in_addr tmpaddr, gateway;
75 u_char zebra_flags = 0;
76
77 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
78 return;
79
80 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
81 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
82
83 prefix.family = AF_INET;
84
85 tmpaddr.s_addr = routeEntry->ipRouteDest;
86 prefix.prefix = tmpaddr;
87
88 tmpaddr.s_addr = routeEntry->ipRouteMask;
89 prefix.prefixlen = ip_masklen (tmpaddr);
90
91 gateway.s_addr = routeEntry->ipRouteNextHop;
92
7c8ff89e 93 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &prefix,
78104b9b 94 &gateway, NULL, 0, VRF_DEFAULT, 0, 0, 0, SAFI_UNICAST);
718e3744 95}
96
6621ca86 97void
12f6fb97 98route_read (struct zebra_ns *zns)
718e3744 99{
100 char storage[RT_BUFSIZ];
101
102 struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage;
103 struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage;
104 struct T_error_ack *TLIerr = (struct T_error_ack *) storage;
105
106 struct opthdr *MIB2hdr;
107
108 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
109
110 struct strbuf msgdata;
111 int flags, dev, retval, process;
112
113 if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
114 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
6099b3b5 115 safe_strerror (errno));
718e3744 116 return;
117 }
118
119 TLIreq->PRIM_type = T_OPTMGMT_REQ;
120 TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
121 TLIreq->OPT_length = sizeof (struct opthdr);
122 TLIreq->MGMT_flags = T_CURRENT;
123
124 MIB2hdr = (struct opthdr *) &TLIreq[1];
125
126 MIB2hdr->level = MIB2_IP;
127 MIB2hdr->name = 0;
128 MIB2hdr->len = 0;
129
130 msgdata.buf = storage;
131 msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
132
133 flags = 0;
134
135 if (putmsg (dev, &msgdata, NULL, flags) == -1) {
6099b3b5 136 zlog_warn ("putmsg failed: %s", safe_strerror (errno));
718e3744 137 goto exit;
138 }
139
140 MIB2hdr = (struct opthdr *) &TLIack[1];
141 msgdata.maxlen = sizeof (storage);
142
143 while (1) {
144 flags = 0;
145 retval = getmsg (dev, &msgdata, NULL, &flags);
146
147 if (retval == -1) {
6099b3b5 148 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
718e3744 149 goto exit;
150 }
151
152 /* This is normal loop termination */
153 if (retval == 0 &&
154 msgdata.len >= sizeof (struct T_optmgmt_ack) &&
155 TLIack->PRIM_type == T_OPTMGMT_ACK &&
156 TLIack->MGMT_flags == T_SUCCESS &&
157 MIB2hdr->len == 0)
158 break;
159
160 if (msgdata.len >= sizeof (struct T_error_ack) &&
161 TLIerr->PRIM_type == T_ERROR_ACK) {
162 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
6099b3b5 163 safe_strerror ((TLIerr->TLI_error == TSYSERR)
718e3744 164 ? TLIerr->UNIX_error : EPROTO));
165 break;
166 }
167
168 /* should dump more debugging info to the log statement,
169 like what GateD does in this instance, but not
170 critical yet. */
171 if (retval != MOREDATA ||
172 msgdata.len < sizeof (struct T_optmgmt_ack) ||
173 TLIack->PRIM_type != T_OPTMGMT_ACK ||
174 TLIack->MGMT_flags != T_SUCCESS) {
175 errno = ENOMSG;
176 zlog_warn ("getmsg(ctl) returned bizarreness");
177 break;
178 }
179
180 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
181 entry, see <inet/mib2.h>. "This isn't the MIB data
182 you're looking for." */
183 process = (MIB2hdr->level == MIB2_IP &&
184 MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
185
186 /* getmsg writes the data buffer out completely, not
187 to the closest smaller multiple. Unless reassembling
188 data structures across buffer boundaries is your idea
189 of a good time, set maxlen to the closest smaller
190 multiple of the size of the datastructure you're
191 retrieving. */
192 msgdata.maxlen = sizeof (storage) - (sizeof (storage)
193 % sizeof (mib2_ipRouteEntry_t));
194
195 msgdata.len = 0;
196 flags = 0;
197
198 do {
199 retval = getmsg (dev, NULL, &msgdata, &flags);
200
201 if (retval == -1) {
202 zlog_warn ("getmsg(data) failed: %s",
6099b3b5 203 safe_strerror (errno));
718e3744 204 goto exit;
205 }
206
207 if (!(retval == 0 || retval == MOREDATA)) {
208 zlog_warn ("getmsg(data) returned %d", retval);
209 goto exit;
210 }
211
212 if (process) {
213 if (msgdata.len %
214 sizeof (mib2_ipRouteEntry_t) != 0) {
215 zlog_warn ("getmsg(data) returned "
216"msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
217 goto exit;
218 }
219
220 routeEntry = (mib2_ipRouteEntry_t *)
221 msgdata.buf;
222 lastRouteEntry = (mib2_ipRouteEntry_t *)
223 (msgdata.buf + msgdata.len);
224 do {
225 handle_route_entry (routeEntry);
226 } while (++routeEntry < lastRouteEntry);
227 }
228 } while (retval == MOREDATA);
229 }
230
231exit:
232 close (dev);
233}