]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | #include "prefix.h" | |
25 | #include "log.h" | |
26 | #include "if.h" | |
78104b9b | 27 | #include "vrf.h" |
82283592 | 28 | #include "vty.h" |
718e3744 | 29 | |
30 | #include "zebra/rib.h" | |
6621ca86 | 31 | #include "zebra/zserv.h" |
718e3744 | 32 | |
a5b38c5b DL |
33 | /* Thank you, Solaris, for polluting application symbol namespace. */ |
34 | #undef hook_register | |
35 | #undef hook_unregister | |
36 | ||
718e3744 | 37 | #include <sys/stream.h> |
38 | #include <sys/tihdr.h> | |
39 | ||
40 | /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */ | |
41 | #ifdef SUNOS_5 | |
42 | #include <sys/tiuser.h> | |
43 | #ifndef T_CURRENT | |
44 | #define T_CURRENT MI_T_CURRENT | |
45 | #endif /* T_CURRENT */ | |
46 | #ifndef IRE_CACHE | |
47 | #define IRE_CACHE 0x0020 /* Cached Route entry */ | |
48 | #endif /* IRE_CACHE */ | |
49 | #ifndef IRE_HOST_REDIRECT | |
50 | #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */ | |
51 | #endif /* IRE_HOST_REDIRECT */ | |
52 | #ifndef IRE_CACHETABLE | |
53 | #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \ | |
54 | IRE_LOOPBACK) | |
55 | #endif /* IRE_CACHETABLE */ | |
56 | #undef IPOPT_EOL | |
57 | #undef IPOPT_NOP | |
58 | #undef IPOPT_LSRR | |
59 | #undef IPOPT_RR | |
60 | #undef IPOPT_SSRR | |
61 | #endif /* SUNOS_5 */ | |
62 | ||
63 | #include <inet/common.h> | |
64 | #include <inet/ip.h> | |
65 | #include <inet/mib2.h> | |
66 | ||
67 | /* device to read IP routing table from */ | |
68 | #ifndef _PATH_GETMSG_ROUTE | |
69 | #define _PATH_GETMSG_ROUTE "/dev/ip" | |
70 | #endif /* _PATH_GETMSG_ROUTE */ | |
71 | ||
72 | #define RT_BUFSIZ 8192 | |
73 | ||
6621ca86 | 74 | static void |
75 | handle_route_entry (mib2_ipRouteEntry_t *routeEntry) | |
718e3744 | 76 | { |
3b1098be DS |
77 | struct prefix prefix; |
78 | struct in_addr tmpaddr, gateway; | |
79 | union g_addr *ggateway; | |
80 | u_char zebra_flags = 0; | |
718e3744 | 81 | |
3b1098be DS |
82 | if (routeEntry->ipRouteInfo.re_ire_type & IRE_CACHETABLE) |
83 | return; | |
718e3744 | 84 | |
3b1098be DS |
85 | if (routeEntry->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT) |
86 | zebra_flags |= ZEBRA_FLAG_SELFROUTE; | |
718e3744 | 87 | |
3b1098be | 88 | prefix.family = AF_INET; |
718e3744 | 89 | |
3b1098be DS |
90 | tmpaddr.s_addr = routeEntry->ipRouteDest; |
91 | prefix.u.prefix4 = tmpaddr; | |
718e3744 | 92 | |
3b1098be DS |
93 | tmpaddr.s_addr = routeEntry->ipRouteMask; |
94 | prefix.prefixlen = ip_masklen (tmpaddr); | |
718e3744 | 95 | |
3b1098be DS |
96 | gateway.s_addr = routeEntry->ipRouteNextHop; |
97 | ggateway = (union g_addr *)&gateway; | |
718e3744 | 98 | |
3b1098be | 99 | rib_add (AFI_IP, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL, 0, |
3c7c91d0 | 100 | zebra_flags, &prefix, NULL, ggateway, NULL, 0, 0, 0, 0, 0); |
718e3744 | 101 | } |
102 | ||
6621ca86 | 103 | void |
12f6fb97 | 104 | route_read (struct zebra_ns *zns) |
718e3744 | 105 | { |
106 | char storage[RT_BUFSIZ]; | |
107 | ||
108 | struct T_optmgmt_req *TLIreq = (struct T_optmgmt_req *) storage; | |
109 | struct T_optmgmt_ack *TLIack = (struct T_optmgmt_ack *) storage; | |
110 | struct T_error_ack *TLIerr = (struct T_error_ack *) storage; | |
111 | ||
112 | struct opthdr *MIB2hdr; | |
113 | ||
114 | mib2_ipRouteEntry_t *routeEntry, *lastRouteEntry; | |
115 | ||
116 | struct strbuf msgdata; | |
117 | int flags, dev, retval, process; | |
118 | ||
119 | if ((dev = open (_PATH_GETMSG_ROUTE, O_RDWR)) == -1) { | |
120 | zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE, | |
6099b3b5 | 121 | safe_strerror (errno)); |
718e3744 | 122 | return; |
123 | } | |
124 | ||
125 | TLIreq->PRIM_type = T_OPTMGMT_REQ; | |
126 | TLIreq->OPT_offset = sizeof (struct T_optmgmt_req); | |
127 | TLIreq->OPT_length = sizeof (struct opthdr); | |
128 | TLIreq->MGMT_flags = T_CURRENT; | |
129 | ||
130 | MIB2hdr = (struct opthdr *) &TLIreq[1]; | |
131 | ||
132 | MIB2hdr->level = MIB2_IP; | |
133 | MIB2hdr->name = 0; | |
134 | MIB2hdr->len = 0; | |
135 | ||
136 | msgdata.buf = storage; | |
137 | msgdata.len = sizeof (struct T_optmgmt_req) + sizeof (struct opthdr); | |
138 | ||
139 | flags = 0; | |
140 | ||
141 | if (putmsg (dev, &msgdata, NULL, flags) == -1) { | |
6099b3b5 | 142 | zlog_warn ("putmsg failed: %s", safe_strerror (errno)); |
718e3744 | 143 | goto exit; |
144 | } | |
145 | ||
146 | MIB2hdr = (struct opthdr *) &TLIack[1]; | |
147 | msgdata.maxlen = sizeof (storage); | |
148 | ||
149 | while (1) { | |
150 | flags = 0; | |
151 | retval = getmsg (dev, &msgdata, NULL, &flags); | |
152 | ||
153 | if (retval == -1) { | |
6099b3b5 | 154 | zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno)); |
718e3744 | 155 | goto exit; |
156 | } | |
157 | ||
158 | /* This is normal loop termination */ | |
159 | if (retval == 0 && | |
dc32cccb | 160 | (size_t)msgdata.len >= sizeof (struct T_optmgmt_ack) && |
718e3744 | 161 | TLIack->PRIM_type == T_OPTMGMT_ACK && |
162 | TLIack->MGMT_flags == T_SUCCESS && | |
163 | MIB2hdr->len == 0) | |
164 | break; | |
165 | ||
dc32cccb | 166 | if ((size_t)msgdata.len >= sizeof (struct T_error_ack) && |
718e3744 | 167 | TLIerr->PRIM_type == T_ERROR_ACK) { |
168 | zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s", | |
6099b3b5 | 169 | safe_strerror ((TLIerr->TLI_error == TSYSERR) |
718e3744 | 170 | ? TLIerr->UNIX_error : EPROTO)); |
171 | break; | |
172 | } | |
173 | ||
174 | /* should dump more debugging info to the log statement, | |
175 | like what GateD does in this instance, but not | |
176 | critical yet. */ | |
177 | if (retval != MOREDATA || | |
dc32cccb | 178 | (size_t)msgdata.len < sizeof (struct T_optmgmt_ack) || |
718e3744 | 179 | TLIack->PRIM_type != T_OPTMGMT_ACK || |
180 | TLIack->MGMT_flags != T_SUCCESS) { | |
181 | errno = ENOMSG; | |
182 | zlog_warn ("getmsg(ctl) returned bizarreness"); | |
183 | break; | |
184 | } | |
185 | ||
186 | /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable | |
187 | entry, see <inet/mib2.h>. "This isn't the MIB data | |
188 | you're looking for." */ | |
189 | process = (MIB2hdr->level == MIB2_IP && | |
190 | MIB2hdr->name == MIB2_IP_21) ? 1 : 0; | |
191 | ||
192 | /* getmsg writes the data buffer out completely, not | |
193 | to the closest smaller multiple. Unless reassembling | |
194 | data structures across buffer boundaries is your idea | |
195 | of a good time, set maxlen to the closest smaller | |
196 | multiple of the size of the datastructure you're | |
197 | retrieving. */ | |
198 | msgdata.maxlen = sizeof (storage) - (sizeof (storage) | |
199 | % sizeof (mib2_ipRouteEntry_t)); | |
200 | ||
201 | msgdata.len = 0; | |
202 | flags = 0; | |
203 | ||
204 | do { | |
205 | retval = getmsg (dev, NULL, &msgdata, &flags); | |
206 | ||
207 | if (retval == -1) { | |
208 | zlog_warn ("getmsg(data) failed: %s", | |
6099b3b5 | 209 | safe_strerror (errno)); |
718e3744 | 210 | goto exit; |
211 | } | |
212 | ||
213 | if (!(retval == 0 || retval == MOREDATA)) { | |
214 | zlog_warn ("getmsg(data) returned %d", retval); | |
215 | goto exit; | |
216 | } | |
217 | ||
218 | if (process) { | |
219 | if (msgdata.len % | |
220 | sizeof (mib2_ipRouteEntry_t) != 0) { | |
221 | zlog_warn ("getmsg(data) returned " | |
222 | "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata.len); | |
223 | goto exit; | |
224 | } | |
225 | ||
226 | routeEntry = (mib2_ipRouteEntry_t *) | |
227 | msgdata.buf; | |
228 | lastRouteEntry = (mib2_ipRouteEntry_t *) | |
229 | (msgdata.buf + msgdata.len); | |
230 | do { | |
231 | handle_route_entry (routeEntry); | |
232 | } while (++routeEntry < lastRouteEntry); | |
233 | } | |
234 | } while (retval == MOREDATA); | |
235 | } | |
236 | ||
237 | exit: | |
238 | close (dev); | |
239 | } |