]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rtread_getmsg.c
zebra, lib/memtypes.c: the netlink sockets work per VRF
[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
30 #include "zebra/rib.h"
31 #include "zebra/zserv.h"
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
70 static void
71 handle_route_entry (mib2_ipRouteEntry_t *routeEntry)
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
93 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &prefix,
94 &gateway, NULL, 0, VRF_DEFAULT, 0, 0, 0, SAFI_UNICAST);
95 }
96
97 void
98 route_read (struct zebra_vrf *zvrf)
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 (zvrf->vrf_id != VRF_DEFAULT) {
114 return;
115 }
116
117 if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
118 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE,
119 safe_strerror (errno));
120 return;
121 }
122
123 TLIreq->PRIM_type = T_OPTMGMT_REQ;
124 TLIreq->OPT_offset = sizeof (struct T_optmgmt_req);
125 TLIreq->OPT_length = sizeof (struct opthdr);
126 TLIreq->MGMT_flags = T_CURRENT;
127
128 MIB2hdr = (struct opthdr *) &TLIreq[1];
129
130 MIB2hdr->level = MIB2_IP;
131 MIB2hdr->name = 0;
132 MIB2hdr->len = 0;
133
134 msgdata.buf = storage;
135 msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr);
136
137 flags = 0;
138
139 if (putmsg (dev, &msgdata, NULL, flags) == -1) {
140 zlog_warn ("putmsg failed: %s", safe_strerror (errno));
141 goto exit;
142 }
143
144 MIB2hdr = (struct opthdr *) &TLIack[1];
145 msgdata.maxlen = sizeof (storage);
146
147 while (1) {
148 flags = 0;
149 retval = getmsg (dev, &msgdata, NULL, &flags);
150
151 if (retval == -1) {
152 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno));
153 goto exit;
154 }
155
156 /* This is normal loop termination */
157 if (retval == 0 &&
158 msgdata.len >= sizeof (struct T_optmgmt_ack) &&
159 TLIack->PRIM_type == T_OPTMGMT_ACK &&
160 TLIack->MGMT_flags == T_SUCCESS &&
161 MIB2hdr->len == 0)
162 break;
163
164 if (msgdata.len >= sizeof (struct T_error_ack) &&
165 TLIerr->PRIM_type == T_ERROR_ACK) {
166 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
167 safe_strerror ((TLIerr->TLI_error == TSYSERR)
168 ? TLIerr->UNIX_error : 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 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) ? 1 : 0;
189
190 /* getmsg writes the data buffer out completely, not
191 to the closest smaller multiple. Unless reassembling
192 data structures across buffer boundaries is your idea
193 of a good time, set maxlen to the closest smaller
194 multiple of the size of the datastructure you're
195 retrieving. */
196 msgdata.maxlen = sizeof (storage) - (sizeof (storage)
197 % sizeof (mib2_ipRouteEntry_t));
198
199 msgdata.len = 0;
200 flags = 0;
201
202 do {
203 retval = getmsg (dev, NULL, &msgdata, &flags);
204
205 if (retval == -1) {
206 zlog_warn ("getmsg(data) failed: %s",
207 safe_strerror (errno));
208 goto exit;
209 }
210
211 if (!(retval == 0 || retval == MOREDATA)) {
212 zlog_warn ("getmsg(data) returned %d", retval);
213 goto exit;
214 }
215
216 if (process) {
217 if (msgdata.len %
218 sizeof (mib2_ipRouteEntry_t) != 0) {
219 zlog_warn ("getmsg(data) returned "
220 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len);
221 goto exit;
222 }
223
224 routeEntry = (mib2_ipRouteEntry_t *)
225 msgdata.buf;
226 lastRouteEntry = (mib2_ipRouteEntry_t *)
227 (msgdata.buf + msgdata.len);
228 do {
229 handle_route_entry (routeEntry);
230 } while (++routeEntry < lastRouteEntry);
231 }
232 } while (retval == MOREDATA);
233 }
234
235 exit:
236 close (dev);
237 }