]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rtread_getmsg.c
zebra: flog_warn conversion
[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
35 /* Thank you, Solaris, for polluting application symbol namespace. */
36 #undef hook_register
37 #undef hook_unregister
38
39 #include <sys/stream.h>
40 #include <sys/tihdr.h>
41
42 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
43 #ifdef SUNOS_5
44 #include <sys/tiuser.h>
45 #ifndef T_CURRENT
46 #define T_CURRENT MI_T_CURRENT
47 #endif /* T_CURRENT */
48 #ifndef IRE_CACHE
49 #define IRE_CACHE 0x0020 /* Cached Route entry */
50 #endif /* IRE_CACHE */
51 #ifndef IRE_HOST_REDIRECT
52 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
53 #endif /* IRE_HOST_REDIRECT */
54 #ifndef IRE_CACHETABLE
55 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | 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 handle_route_entry(mib2_ipRouteEntry_t *routeEntry)
76 {
77 struct prefix prefix;
78 struct in_addr tmpaddr;
79 struct nexthop nh;
80 uint8_t zebra_flags = 0;
81
82 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
83 return;
84
85 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
86 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
87
88 prefix.family = AF_INET;
89
90 tmpaddr.s_addr = routeEntry->ipRouteDest;
91 prefix.u.prefix4 = tmpaddr;
92
93 tmpaddr.s_addr = routeEntry->ipRouteMask;
94 prefix.prefixlen = ip_masklen(tmpaddr);
95
96 memset(&nh, 0, sizeof(nh));
97 nh.vrf_id = VRF_DEFAULT;
98 nh.type = NEXTHOP_TYPE_IPV4;
99 nh.gate.ipv4.s_addr = routeEntry->ipRouteNextHop;
100
101 rib_add(AFI_IP, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL, 0,
102 zebra_flags, &prefix, NULL, &nh, 0, 0, 0, 0, 0);
103 }
104
105 void 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 flog_err_sys(LIB_ERR_SYSTEM_CALL, "can't open %s: %s",
122 _PATH_GETMSG_ROUTE, 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 flog_err_sys(LIB_ERR_SOCKET, "putmsg failed: %s",
144 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 flog_err_sys(LIB_ERR_SYSTEM_CALL,
157 "getmsg(ctl) failed: %s",
158 safe_strerror(errno));
159 goto exit;
160 }
161
162 /* This is normal loop termination */
163 if (retval == 0
164 && (size_t)msgdata.len >= sizeof(struct T_optmgmt_ack)
165 && TLIack->PRIM_type == T_OPTMGMT_ACK
166 && TLIack->MGMT_flags == T_SUCCESS && MIB2hdr->len == 0)
167 break;
168
169 if ((size_t)msgdata.len >= sizeof(struct T_error_ack)
170 && TLIerr->PRIM_type == T_ERROR_ACK) {
171 zlog_debug("getmsg(ctl) returned T_ERROR_ACK: %s",
172 safe_strerror((TLIerr->TLI_error == TSYSERR)
173 ? TLIerr->UNIX_error
174 : EPROTO));
175 break;
176 }
177
178 /* should dump more debugging info to the log statement,
179 like what GateD does in this instance, but not
180 critical yet. */
181 if (retval != MOREDATA
182 || (size_t)msgdata.len < sizeof(struct T_optmgmt_ack)
183 || TLIack->PRIM_type != T_OPTMGMT_ACK
184 || TLIack->MGMT_flags != T_SUCCESS) {
185 errno = ENOMSG;
186 zlog_debug("getmsg(ctl) returned bizarreness");
187 break;
188 }
189
190 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
191 entry, see <inet/mib2.h>. "This isn't the MIB data
192 you're looking for." */
193 process = (MIB2hdr->level == MIB2_IP
194 && MIB2hdr->name == MIB2_IP_21)
195 ? 1
196 : 0;
197
198 /* getmsg writes the data buffer out completely, not
199 to the closest smaller multiple. Unless reassembling
200 data structures across buffer boundaries is your idea
201 of a good time, set maxlen to the closest smaller
202 multiple of the size of the datastructure you're
203 retrieving. */
204 msgdata.maxlen =
205 sizeof(storage)
206 - (sizeof(storage) % sizeof(mib2_ipRouteEntry_t));
207
208 msgdata.len = 0;
209 flags = 0;
210
211 do {
212 retval = getmsg(dev, NULL, &msgdata, &flags);
213
214 if (retval == -1) {
215 flog_err_sys(LIB_ERR_SYSTEM_CALL,
216 "getmsg(data) failed: %s",
217 safe_strerror(errno));
218 goto exit;
219 }
220
221 if (!(retval == 0 || retval == MOREDATA)) {
222 zlog_debug("getmsg(data) returned %d", retval);
223 goto exit;
224 }
225
226 if (process) {
227 if (msgdata.len % sizeof(mib2_ipRouteEntry_t)
228 != 0) {
229 zlog_debug(
230 "getmsg(data) returned "
231 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)",
232 msgdata.len);
233 goto exit;
234 }
235
236 routeEntry = (mib2_ipRouteEntry_t *)msgdata.buf;
237 lastRouteEntry =
238 (mib2_ipRouteEntry_t *)(msgdata.buf
239 + msgdata.len);
240 do {
241 handle_route_entry(routeEntry);
242 } while (++routeEntry < lastRouteEntry);
243 }
244 } while (retval == MOREDATA);
245 }
246
247 exit:
248 close(dev);
249 }
250
251 /* Only implemented for netlink method */
252 void macfdb_read(struct zebra_ns *zns)
253 {
254 }
255
256 void macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
257 struct interface *br_if)
258 {
259 }
260
261 void neigh_read(struct zebra_ns *zns)
262 {
263 }
264
265 void neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
266 {
267 }
268
269 void kernel_read_pbr_rules(struct zebra_ns *zns)
270 {
271 }
272
273 #endif /* SUNOS_5 */