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