]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rtread_getmsg.c
Merge pull request #3465 from donaldsharp/nexthop_active_update
[mirror_frr.git] / zebra / rtread_getmsg.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 20 */
21
22#include <zebra.h>
23
ddfeb486
DL
24#ifdef SUNOS_5
25
718e3744 26#include "prefix.h"
27#include "log.h"
28#include "if.h"
78104b9b 29#include "vrf.h"
82283592 30#include "vty.h"
364fed6b 31#include "lib_errors.h"
718e3744 32
33#include "zebra/rib.h"
05f7f5db 34#include "zebra/rt.h"
ae9eebca 35#include "zebra/zebra_pbr.h"
364fed6b 36#include "zebra/zebra_errors.h"
718e3744 37
a5b38c5b
DL
38/* Thank you, Solaris, for polluting application symbol namespace. */
39#undef hook_register
40#undef hook_unregister
41
718e3744 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
d62a17ae 58#define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
718e3744 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
d62a17ae 78static void handle_route_entry(mib2_ipRouteEntry_t *routeEntry)
718e3744 79{
d62a17ae 80 struct prefix prefix;
fd36be7e
DL
81 struct in_addr tmpaddr;
82 struct nexthop nh;
d7c0a89a 83 uint8_t zebra_flags = 0;
718e3744 84
d62a17ae 85 if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE)
86 return;
718e3744 87
d62a17ae 88 if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT)
89 zebra_flags |= ZEBRA_FLAG_SELFROUTE;
718e3744 90
d62a17ae 91 prefix.family = AF_INET;
718e3744 92
d62a17ae 93 tmpaddr.s_addr = routeEntry->ipRouteDest;
94 prefix.u.prefix4 = tmpaddr;
718e3744 95
d62a17ae 96 tmpaddr.s_addr = routeEntry->ipRouteMask;
97 prefix.prefixlen = ip_masklen(tmpaddr);
718e3744 98
fd36be7e 99 memset(&nh, 0, sizeof(nh));
4a7371e9 100 nh.vrf_id = VRF_DEFAULT;
fd36be7e
DL
101 nh.type = NEXTHOP_TYPE_IPV4;
102 nh.gate.ipv4.s_addr = routeEntry->ipRouteNextHop;
718e3744 103
4a7371e9
DS
104 rib_add(AFI_IP, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL, 0,
105 zebra_flags, &prefix, NULL, &nh, 0, 0, 0, 0, 0);
718e3744 106}
107
d62a17ae 108void route_read(struct zebra_ns *zns)
718e3744 109{
d62a17ae 110 char storage[RT_BUFSIZ];
718e3744 111
d62a17ae 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;
718e3744 115
d62a17ae 116 struct opthdr *MIB2hdr;
718e3744 117
d62a17ae 118 mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry;
718e3744 119
d62a17ae 120 struct strbuf msgdata;
121 int flags, dev, retval, process;
718e3744 122
d62a17ae 123 if ((dev = open(_PATH_GETMSG_ROUTE, O_RDWR)) == -1) {
450971aa 124 flog_err_sys(EC_LIB_SYSTEM_CALL, "can't open %s: %s",
9df414fe 125 _PATH_GETMSG_ROUTE, safe_strerror(errno));
718e3744 126 return;
127 }
128
129 TLIreq->PRIM_type = T_OPTMGMT_REQ;
d62a17ae 130 TLIreq->OPT_offset = sizeof(struct T_optmgmt_req);
131 TLIreq->OPT_length = sizeof(struct opthdr);
718e3744 132 TLIreq->MGMT_flags = T_CURRENT;
133
d62a17ae 134 MIB2hdr = (struct opthdr *)&TLIreq[1];
718e3744 135
136 MIB2hdr->level = MIB2_IP;
137 MIB2hdr->name = 0;
138 MIB2hdr->len = 0;
d62a17ae 139
718e3744 140 msgdata.buf = storage;
d62a17ae 141 msgdata.len = sizeof(struct T_optmgmt_req) + sizeof(struct opthdr);
718e3744 142
143 flags = 0;
144
d62a17ae 145 if (putmsg(dev, &msgdata, NULL, flags) == -1) {
450971aa 146 flog_err_sys(EC_LIB_SOCKET, "putmsg failed: %s",
9df414fe 147 safe_strerror(errno));
718e3744 148 goto exit;
149 }
150
d62a17ae 151 MIB2hdr = (struct opthdr *)&TLIack[1];
152 msgdata.maxlen = sizeof(storage);
718e3744 153
154 while (1) {
155 flags = 0;
d62a17ae 156 retval = getmsg(dev, &msgdata, NULL, &flags);
718e3744 157
158 if (retval == -1) {
450971aa 159 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe
QY
160 "getmsg(ctl) failed: %s",
161 safe_strerror(errno));
718e3744 162 goto exit;
163 }
164
165 /* This is normal loop termination */
d62a17ae 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)
718e3744 170 break;
171
d62a17ae 172 if ((size_t)msgdata.len >= sizeof(struct T_error_ack)
173 && TLIerr->PRIM_type == T_ERROR_ACK) {
9df414fe
QY
174 zlog_debug("getmsg(ctl) returned T_ERROR_ACK: %s",
175 safe_strerror((TLIerr->TLI_error == TSYSERR)
176 ? TLIerr->UNIX_error
177 : EPROTO));
718e3744 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. */
d62a17ae 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) {
718e3744 188 errno = ENOMSG;
9df414fe 189 zlog_debug("getmsg(ctl) returned bizarreness");
718e3744 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." */
d62a17ae 196 process = (MIB2hdr->level == MIB2_IP
197 && MIB2hdr->name == MIB2_IP_21)
198 ? 1
199 : 0;
718e3744 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. */
d62a17ae 207 msgdata.maxlen =
208 sizeof(storage)
209 - (sizeof(storage) % sizeof(mib2_ipRouteEntry_t));
718e3744 210
211 msgdata.len = 0;
212 flags = 0;
213
214 do {
d62a17ae 215 retval = getmsg(dev, NULL, &msgdata, &flags);
718e3744 216
217 if (retval == -1) {
450971aa 218 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe
QY
219 "getmsg(data) failed: %s",
220 safe_strerror(errno));
718e3744 221 goto exit;
222 }
223
224 if (!(retval == 0 || retval == MOREDATA)) {
9df414fe 225 zlog_debug("getmsg(data) returned %d", retval);
718e3744 226 goto exit;
227 }
228
229 if (process) {
d62a17ae 230 if (msgdata.len % sizeof(mib2_ipRouteEntry_t)
231 != 0) {
9df414fe 232 zlog_debug(
d62a17ae 233 "getmsg(data) returned "
234 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)",
235 msgdata.len);
718e3744 236 goto exit;
237 }
238
d62a17ae 239 routeEntry = (mib2_ipRouteEntry_t *)msgdata.buf;
240 lastRouteEntry =
241 (mib2_ipRouteEntry_t *)(msgdata.buf
242 + msgdata.len);
718e3744 243 do {
d62a17ae 244 handle_route_entry(routeEntry);
718e3744 245 } while (++routeEntry < lastRouteEntry);
246 }
247 } while (retval == MOREDATA);
248 }
249
250exit:
d62a17ae 251 close(dev);
718e3744 252}
2232a77c 253
254/* Only implemented for netlink method */
d62a17ae 255void macfdb_read(struct zebra_ns *zns)
2232a77c 256{
257}
258
d62a17ae 259void macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp,
260 struct interface *br_if)
2232a77c 261{
262}
263
d62a17ae 264void neigh_read(struct zebra_ns *zns)
2232a77c 265{
266}
267
d62a17ae 268void neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if)
2232a77c 269{
270}
ddfeb486 271
942bf97b 272void kernel_read_pbr_rules(struct zebra_ns *zns)
273{
274}
275
ddfeb486 276#endif /* SUNOS_5 */