]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rtread_getmsg.c
Merge pull request #805 from Orange-OpenSource/master
[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 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
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "log.h"
26 #include "if.h"
27 #include "vrf.h"
28 #include "vty.h"
29
30 #include "zebra/rib.h"
31 #include "zebra/zserv.h"
32
33 /* Thank you, Solaris, for polluting application symbol namespace. */
34 #undef hook_register
35 #undef hook_unregister
36
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
53 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
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
73 static void handle_route_entry(mib2_ipRouteEntry_t *routeEntry)
74 {
75 struct prefix prefix;
76 struct in_addr tmpaddr, gateway;
77 union g_addr *ggateway;
78 u_char zebra_flags = 0;
79
80 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
81 return;
82
83 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
84 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
85
86 prefix.family = AF_INET;
87
88 tmpaddr.s_addr = routeEntry->ipRouteDest;
89 prefix.u.prefix4 = tmpaddr;
90
91 tmpaddr.s_addr = routeEntry->ipRouteMask;
92 prefix.prefixlen = ip_masklen(tmpaddr);
93
94 gateway.s_addr = routeEntry->ipRouteNextHop;
95 ggateway = (union g_addr *)&gateway;
96
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);
99 }
100
101 void 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",
152 safe_strerror(errno));
153 goto exit;
154 }
155
156 /* This is normal loop termination */
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)
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
168 : EPROTO));
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. */
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) {
179 errno = ENOMSG;
180 zlog_warn("getmsg(ctl) returned bizarreness");
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." */
187 process = (MIB2hdr->level == MIB2_IP
188 && MIB2hdr->name == MIB2_IP_21)
189 ? 1
190 : 0;
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. */
198 msgdata.maxlen =
199 sizeof(storage)
200 - (sizeof(storage) % 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 % 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);
226 goto exit;
227 }
228
229 routeEntry = (mib2_ipRouteEntry_t *)msgdata.buf;
230 lastRouteEntry =
231 (mib2_ipRouteEntry_t *)(msgdata.buf
232 + msgdata.len);
233 do {
234 handle_route_entry(routeEntry);
235 } while (++routeEntry < lastRouteEntry);
236 }
237 } while (retval == MOREDATA);
238 }
239
240 exit:
241 close(dev);
242 }
243
244 /* Only implemented for netlink method */
245 void macfdb_read(struct zebra_ns *zns)
246 {
247 }
248
249 void macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
250 struct interface *br_if)
251 {
252 }
253
254 void neigh_read(struct zebra_ns *zns)
255 {
256 }
257
258 void neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
259 {
260 }