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