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