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