blob: 891539416fc4b54bbc7daabba4bb56c9492e30eb [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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"
Feng Lu0d0686f2015-05-22 11:40:02 +020028#include "vrf.h"
paul718e3742002-12-13 20:15:29 +000029
30#include "zebra/rib.h"
paul6621ca82005-11-23 13:02:08 +000031#include "zebra/zserv.h"
paul718e3742002-12-13 20:15:29 +000032
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
paul6621ca82005-11-23 13:02:08 +000070static void
71handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
paul718e3742002-12-13 20:15:29 +000072{
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
93 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &prefix,
Feng Lu0d0686f2015-05-22 11:40:02 +020094 &gateway, NULL, 0, VRF_DEFAULT, RT_TABLE_MAIN,
Donald Sharpeae18d12015-11-21 07:55:42 -050095 0, 0, 0, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +000096}
97
paul6621ca82005-11-23 13:02:08 +000098void
Feng Lu758fb8f2014-07-03 18:23:09 +080099route_read (struct zebra_vrf *zvrf)
paul718e3742002-12-13 20:15:29 +0000100{
101 char storage[RT_BUFSIZ];
102
103 struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage;
104 struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage;
105 struct T_error_ack *TLIerr = (struct T_error_ack *) storage;
106
107 struct opthdr *MIB2hdr;
108
109 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
110
111 struct strbuf msgdata;
112 int flags, dev, retval, process;
113
Feng Lu758fb8f2014-07-03 18:23:09 +0800114 if (zvrf->vrf_id != VRF_DEFAULT) {
115 return;
116 }
117
paul718e3742002-12-13 20:15:29 +0000118 if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
119 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
ajs6099b3b2004-11-20 02:06:59 +0000120 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000121 return;
122 }
123
124 TLIreq->PRIM_type = T_OPTMGMT_REQ;
125 TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
126 TLIreq->OPT_length = sizeof (struct opthdr);
127 TLIreq->MGMT_flags = T_CURRENT;
128
129 MIB2hdr = (struct opthdr *) &TLIreq[1];
130
131 MIB2hdr->level = MIB2_IP;
132 MIB2hdr->name = 0;
133 MIB2hdr->len = 0;
134
135 msgdata.buf = storage;
136 msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
137
138 flags = 0;
139
140 if (putmsg (dev, &msgdata, NULL, flags) == -1) {
ajs6099b3b2004-11-20 02:06:59 +0000141 zlog_warn ("putmsg failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000142 goto exit;
143 }
144
145 MIB2hdr = (struct opthdr *) &TLIack[1];
146 msgdata.maxlen = sizeof (storage);
147
148 while (1) {
149 flags = 0;
150 retval = getmsg (dev, &msgdata, NULL, &flags);
151
152 if (retval == -1) {
ajs6099b3b2004-11-20 02:06:59 +0000153 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000154 goto exit;
155 }
156
157 /* This is normal loop termination */
158 if (retval == 0 &&
David Lamparterebd26872015-09-15 21:40:31 -0700159 (size_t)msgdata.len >= sizeof (struct T_optmgmt_ack) &&
paul718e3742002-12-13 20:15:29 +0000160 TLIack->PRIM_type == T_OPTMGMT_ACK &&
161 TLIack->MGMT_flags == T_SUCCESS &&
162 MIB2hdr->len == 0)
163 break;
164
David Lamparterebd26872015-09-15 21:40:31 -0700165 if ((size_t)msgdata.len >= sizeof (struct T_error_ack) &&
paul718e3742002-12-13 20:15:29 +0000166 TLIerr->PRIM_type == T_ERROR_ACK) {
167 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
ajs6099b3b2004-11-20 02:06:59 +0000168 safe_strerror ((TLIerr->TLI_error == TSYSERR)
paul718e3742002-12-13 20:15:29 +0000169 ? TLIerr->UNIX_error : EPROTO));
170 break;
171 }
172
173 /* should dump more debugging info to the log statement,
174 like what GateD does in this instance, but not
175 critical yet. */
176 if (retval != MOREDATA ||
David Lamparterebd26872015-09-15 21:40:31 -0700177 (size_t)msgdata.len < sizeof (struct T_optmgmt_ack) ||
paul718e3742002-12-13 20:15:29 +0000178 TLIack->PRIM_type != T_OPTMGMT_ACK ||
179 TLIack->MGMT_flags != T_SUCCESS) {
180 errno = ENOMSG;
181 zlog_warn ("getmsg(ctl) returned bizarreness");
182 break;
183 }
184
185 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
186 entry, see <inet/mib2.h>. "This isn't the MIB data
187 you're looking for." */
188 process = (MIB2hdr->level == MIB2_IP &&
189 MIB2hdr->name == MIB2_IP_21) ? 1 : 0;
190
191 /* getmsg writes the data buffer out completely, not
192 to the closest smaller multiple. Unless reassembling
193 data structures across buffer boundaries is your idea
194 of a good time, set maxlen to the closest smaller
195 multiple of the size of the datastructure you're
196 retrieving. */
197 msgdata.maxlen = sizeof (storage) - (sizeof (storage)
198 % sizeof (mib2_ipRouteEntry_t));
199
200 msgdata.len = 0;
201 flags = 0;
202
203 do {
204 retval = getmsg (dev, NULL, &msgdata, &flags);
205
206 if (retval == -1) {
207 zlog_warn ("getmsg(data) failed: %s",
ajs6099b3b2004-11-20 02:06:59 +0000208 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000209 goto exit;
210 }
211
212 if (!(retval == 0 || retval == MOREDATA)) {
213 zlog_warn ("getmsg(data) returned %d", retval);
214 goto exit;
215 }
216
217 if (process) {
218 if (msgdata.len %
219 sizeof (mib2_ipRouteEntry_t) != 0) {
220 zlog_warn ("getmsg(data) returned "
221"msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
222 goto exit;
223 }
224
225 routeEntry = (mib2_ipRouteEntry_t *)
226 msgdata.buf;
227 lastRouteEntry = (mib2_ipRouteEntry_t *)
228 (msgdata.buf + msgdata.len);
229 do {
230 handle_route_entry (routeEntry);
231 } while (++routeEntry < lastRouteEntry);
232 }
233 } while (retval == MOREDATA);
234 }
235
236exit:
237 close (dev);
238}