input.c revision 1.1.1.6 1 1.1 cgd /*
2 1.1.1.2 mycroft * Copyright (c) 1983, 1988, 1993
3 1.1.1.2 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1.1.4 christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
35 1.1.1.2 mycroft static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 6/5/93";
36 1.1.1.4 christos #elif defined(__NetBSD__)
37 1.1.1.4 christos static char rcsid[] = "$NetBSD: input.c,v 1.1.1.6 1998/06/02 17:41:25 thorpej Exp $";
38 1.1.1.4 christos #endif
39 1.1.1.6 thorpej #ident "$Revision: 1.1.1.6 $"
40 1.1.1.3 thorpej
41 1.1 cgd #include "defs.h"
42 1.1 cgd
43 1.1.1.5 christos static void input(struct sockaddr_in *, struct interface *, struct interface *,
44 1.1.1.5 christos struct rip *, int);
45 1.1.1.6 thorpej static void input_route(naddr, naddr, struct rt_spare *, struct netinfo *);
46 1.1.1.5 christos static int ck_passwd(struct interface *, struct rip *, void *,
47 1.1.1.5 christos naddr, struct msg_limit *);
48 1.1.1.3 thorpej
49 1.1.1.3 thorpej
50 1.1.1.3 thorpej /* process RIP input
51 1.1 cgd */
52 1.1.1.3 thorpej void
53 1.1.1.3 thorpej read_rip(int sock,
54 1.1.1.5 christos struct interface *sifp)
55 1.1 cgd {
56 1.1.1.3 thorpej struct sockaddr_in from;
57 1.1.1.5 christos struct interface *aifp;
58 1.1.1.3 thorpej int fromlen, cc;
59 1.1.1.5 christos #ifdef USE_PASSIFNAME
60 1.1.1.5 christos static struct msg_limit bad_name;
61 1.1.1.5 christos struct {
62 1.1.1.5 christos char ifname[IFNAMSIZ];
63 1.1.1.5 christos union pkt_buf pbuf;
64 1.1.1.5 christos } inbuf;
65 1.1.1.5 christos #else
66 1.1.1.5 christos struct {
67 1.1.1.5 christos union pkt_buf pbuf;
68 1.1.1.5 christos } inbuf;
69 1.1.1.5 christos #endif
70 1.1.1.3 thorpej
71 1.1.1.3 thorpej
72 1.1.1.3 thorpej for (;;) {
73 1.1.1.3 thorpej fromlen = sizeof(from);
74 1.1.1.3 thorpej cc = recvfrom(sock, &inbuf, sizeof(inbuf), 0,
75 1.1.1.3 thorpej (struct sockaddr*)&from, &fromlen);
76 1.1.1.3 thorpej if (cc <= 0) {
77 1.1.1.3 thorpej if (cc < 0 && errno != EWOULDBLOCK)
78 1.1.1.3 thorpej LOGERR("recvfrom(rip)");
79 1.1.1.3 thorpej break;
80 1.1.1.3 thorpej }
81 1.1.1.3 thorpej if (fromlen != sizeof(struct sockaddr_in))
82 1.1.1.3 thorpej logbad(1,"impossible recvfrom(rip) fromlen=%d",
83 1.1.1.3 thorpej fromlen);
84 1.1.1.3 thorpej
85 1.1.1.5 christos /* aifp is the "authenticated" interface via which the packet
86 1.1.1.5 christos * arrived. In fact, it is only the interface on which
87 1.1.1.5 christos * the packet should have arrived based on is source
88 1.1.1.5 christos * address.
89 1.1.1.5 christos * sifp is interface associated with the socket through which
90 1.1.1.5 christos * the packet was received.
91 1.1.1.5 christos */
92 1.1.1.5 christos #ifdef USE_PASSIFNAME
93 1.1.1.5 christos if ((cc -= sizeof(inbuf.ifname)) < 0)
94 1.1.1.5 christos logbad(0,"missing USE_PASSIFNAME; only %d bytes",
95 1.1.1.5 christos cc+sizeof(inbuf.ifname));
96 1.1.1.5 christos
97 1.1.1.5 christos /* check the remote interfaces first */
98 1.1.1.5 christos for (aifp = remote_if; aifp; aifp = aifp->int_rlink) {
99 1.1.1.5 christos if (aifp->int_addr == from.sin_addr.s_addr)
100 1.1.1.5 christos break;
101 1.1.1.5 christos }
102 1.1.1.5 christos if (aifp == 0) {
103 1.1.1.5 christos aifp = ifwithname(inbuf.ifname, 0);
104 1.1.1.5 christos if (aifp == 0) {
105 1.1.1.5 christos msglim(&bad_name, from.sin_addr.s_addr,
106 1.1.1.5 christos "impossible interface name %.*s",
107 1.1.1.5 christos IFNAMSIZ, inbuf.ifname);
108 1.1.1.5 christos } else if (((aifp->int_if_flags & IFF_POINTOPOINT)
109 1.1.1.5 christos && aifp->int_dstaddr!=from.sin_addr.s_addr)
110 1.1.1.5 christos || (!(aifp->int_if_flags & IFF_POINTOPOINT)
111 1.1.1.5 christos && !on_net(from.sin_addr.s_addr,
112 1.1.1.5 christos aifp->int_net,
113 1.1.1.5 christos aifp->int_mask))) {
114 1.1.1.5 christos /* If it came via the wrong interface, do not
115 1.1.1.5 christos * trust it.
116 1.1.1.5 christos */
117 1.1.1.5 christos aifp = 0;
118 1.1.1.5 christos }
119 1.1.1.5 christos }
120 1.1.1.5 christos #else
121 1.1.1.5 christos aifp = iflookup(from.sin_addr.s_addr);
122 1.1.1.5 christos #endif
123 1.1.1.5 christos if (sifp == 0)
124 1.1.1.5 christos sifp = aifp;
125 1.1.1.5 christos
126 1.1.1.5 christos input(&from, sifp, aifp, &inbuf.pbuf.rip, cc);
127 1.1 cgd }
128 1.1.1.3 thorpej }
129 1.1.1.3 thorpej
130 1.1.1.3 thorpej
131 1.1.1.3 thorpej /* Process a RIP packet
132 1.1.1.3 thorpej */
133 1.1.1.3 thorpej static void
134 1.1.1.3 thorpej input(struct sockaddr_in *from, /* received from this IP address */
135 1.1.1.5 christos struct interface *sifp, /* interface of incoming socket */
136 1.1.1.5 christos struct interface *aifp, /* "authenticated" interface */
137 1.1.1.3 thorpej struct rip *rip,
138 1.1.1.5 christos int cc)
139 1.1.1.3 thorpej {
140 1.1.1.3 thorpej # define FROM_NADDR from->sin_addr.s_addr
141 1.1.1.5 christos static struct msg_limit use_auth, bad_len, bad_mask;
142 1.1.1.6 thorpej static struct msg_limit unk_router, bad_router, bad_nhop;
143 1.1.1.3 thorpej
144 1.1.1.3 thorpej struct rt_entry *rt;
145 1.1.1.6 thorpej struct rt_spare new;
146 1.1.1.3 thorpej struct netinfo *n, *lim;
147 1.1.1.3 thorpej struct interface *ifp1;
148 1.1.1.3 thorpej naddr gate, mask, v1_mask, dst, ddst_h;
149 1.1.1.5 christos struct auth *ap;
150 1.1.1.6 thorpej struct tgate *tg = 0;
151 1.1.1.6 thorpej struct tgate_net *tn;
152 1.1.1.6 thorpej int i, j;
153 1.1.1.3 thorpej
154 1.1.1.5 christos /* Notice when we hear from a remote gateway
155 1.1.1.5 christos */
156 1.1.1.5 christos if (aifp != 0
157 1.1.1.5 christos && (aifp->int_state & IS_REMOTE))
158 1.1.1.5 christos aifp->int_act_time = now.tv_sec;
159 1.1.1.3 thorpej
160 1.1.1.5 christos trace_rip("Recv", "from", from, sifp, rip, cc);
161 1.1.1.3 thorpej
162 1.1 cgd if (rip->rip_vers == 0) {
163 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
164 1.1.1.5 christos "RIP version 0, cmd %d, packet received from %s",
165 1.1.1.5 christos rip->rip_cmd, naddr_ntoa(FROM_NADDR));
166 1.1.1.3 thorpej return;
167 1.1.1.4 christos } else if (rip->rip_vers > RIPv2) {
168 1.1.1.4 christos rip->rip_vers = RIPv2;
169 1.1.1.3 thorpej }
170 1.1.1.5 christos if (cc > OVER_MAXPACKETSIZE) {
171 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
172 1.1.1.5 christos "packet at least %d bytes too long received from %s",
173 1.1.1.5 christos cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
174 1.1.1.3 thorpej return;
175 1.1.1.3 thorpej }
176 1.1.1.3 thorpej
177 1.1.1.3 thorpej n = rip->rip_nets;
178 1.1.1.5 christos lim = (struct netinfo *)((char*)rip + cc);
179 1.1.1.3 thorpej
180 1.1.1.3 thorpej /* Notice authentication.
181 1.1.1.3 thorpej * As required by section 4.2 in RFC 1723, discard authenticated
182 1.1.1.3 thorpej * RIPv2 messages, but only if configured for that silliness.
183 1.1.1.3 thorpej *
184 1.1.1.5 christos * RIPv2 authentication is lame. Why authenticate queries?
185 1.1.1.3 thorpej * Why should a RIPv2 implementation with authentication disabled
186 1.1.1.3 thorpej * not be able to listen to RIPv2 packets with authenication, while
187 1.1.1.3 thorpej * RIPv1 systems will listen? Crazy!
188 1.1.1.3 thorpej */
189 1.1.1.3 thorpej if (!auth_ok
190 1.1.1.4 christos && rip->rip_vers == RIPv2
191 1.1.1.3 thorpej && n < lim && n->n_family == RIP_AF_AUTH) {
192 1.1.1.5 christos msglim(&use_auth, FROM_NADDR,
193 1.1.1.5 christos "RIPv2 message with authentication from %s discarded",
194 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
195 1.1 cgd return;
196 1.1 cgd }
197 1.1 cgd
198 1.1.1.3 thorpej switch (rip->rip_cmd) {
199 1.1 cgd case RIPCMD_REQUEST:
200 1.1.1.5 christos /* For mere requests, be a little sloppy about the source
201 1.1.1.3 thorpej */
202 1.1.1.5 christos if (aifp == 0)
203 1.1.1.5 christos aifp = sifp;
204 1.1.1.5 christos
205 1.1.1.5 christos /* Are we talking to ourself or a remote gateway?
206 1.1.1.5 christos */
207 1.1.1.5 christos ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
208 1.1.1.5 christos if (ifp1) {
209 1.1.1.5 christos if (ifp1->int_state & IS_REMOTE) {
210 1.1.1.5 christos /* remote gateway */
211 1.1.1.5 christos aifp = ifp1;
212 1.1.1.5 christos if (check_remote(aifp)) {
213 1.1.1.5 christos aifp->int_act_time = now.tv_sec;
214 1.1.1.5 christos (void)if_ok(aifp, "remote ");
215 1.1.1.5 christos }
216 1.1.1.5 christos } else if (from->sin_port == htons(RIP_PORT)) {
217 1.1.1.5 christos trace_pkt(" discard our own RIP request");
218 1.1.1.3 thorpej return;
219 1.1.1.3 thorpej }
220 1.1.1.5 christos }
221 1.1.1.3 thorpej
222 1.1.1.5 christos /* did the request come from a router?
223 1.1.1.5 christos */
224 1.1.1.5 christos if (from->sin_port == htons(RIP_PORT)) {
225 1.1.1.5 christos /* yes, ignore the request if RIP is off so that
226 1.1.1.5 christos * the router does not depend on us.
227 1.1.1.3 thorpej */
228 1.1.1.5 christos if (rip_sock < 0
229 1.1.1.5 christos || (aifp != 0
230 1.1.1.5 christos && IS_RIP_OUT_OFF(aifp->int_state))) {
231 1.1.1.5 christos trace_pkt(" discard request while RIP off");
232 1.1.1.3 thorpej return;
233 1.1.1.3 thorpej }
234 1.1.1.3 thorpej }
235 1.1.1.3 thorpej
236 1.1.1.3 thorpej /* According to RFC 1723, we should ignore unathenticated
237 1.1.1.3 thorpej * queries. That is too silly to bother with. Sheesh!
238 1.1.1.5 christos * Are forwarding tables supposed to be secret, when
239 1.1.1.5 christos * a bad guy can infer them with test traffic? When RIP
240 1.1.1.5 christos * is still the most common router-discovery protocol
241 1.1.1.5 christos * and so hosts need to send queries that will be answered?
242 1.1.1.5 christos * What about `rtquery`?
243 1.1.1.3 thorpej * Maybe on firewalls you'd care, but not enough to
244 1.1.1.3 thorpej * give up the diagnostic facilities of remote probing.
245 1.1.1.3 thorpej */
246 1.1.1.3 thorpej
247 1.1.1.5 christos if (n >= lim) {
248 1.1.1.5 christos msglim(&bad_len, FROM_NADDR, "empty request from %s",
249 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
250 1.1.1.5 christos return;
251 1.1.1.5 christos }
252 1.1.1.5 christos if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
253 1.1.1.5 christos msglim(&bad_len, FROM_NADDR,
254 1.1.1.5 christos "request of bad length (%d) from %s",
255 1.1.1.5 christos cc, naddr_ntoa(FROM_NADDR));
256 1.1.1.5 christos }
257 1.1.1.5 christos
258 1.1.1.5 christos if (rip->rip_vers == RIPv2
259 1.1.1.5 christos && (aifp == 0 || (aifp->int_state & IS_NO_RIPV1_OUT))) {
260 1.1.1.5 christos v12buf.buf->rip_vers = RIPv2;
261 1.1.1.5 christos /* If we have a secret but it is a cleartext secret,
262 1.1.1.5 christos * do not disclose our secret unless the other guy
263 1.1.1.5 christos * already knows it.
264 1.1.1.5 christos */
265 1.1.1.5 christos ap = find_auth(aifp);
266 1.1.1.5 christos if (ap != 0 && ap->type == RIP_AUTH_PW
267 1.1.1.5 christos && n->n_family == RIP_AF_AUTH
268 1.1.1.5 christos && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
269 1.1.1.5 christos ap = 0;
270 1.1.1.5 christos } else {
271 1.1.1.5 christos v12buf.buf->rip_vers = RIPv1;
272 1.1.1.5 christos ap = 0;
273 1.1.1.3 thorpej }
274 1.1.1.5 christos clr_ws_buf(&v12buf, ap);
275 1.1.1.5 christos
276 1.1.1.5 christos do {
277 1.1.1.5 christos NTOHL(n->n_metric);
278 1.1.1.3 thorpej
279 1.1.1.3 thorpej /* A single entry with family RIP_AF_UNSPEC and
280 1.1.1.3 thorpej * metric HOPCNT_INFINITY means "all routes".
281 1.1 cgd * We respond to routers only if we are acting
282 1.1 cgd * as a supplier, or to anyone other than a router
283 1.1.1.3 thorpej * (i.e. a query).
284 1.1 cgd */
285 1.1.1.3 thorpej if (n->n_family == RIP_AF_UNSPEC
286 1.1.1.5 christos && n->n_metric == HOPCNT_INFINITY) {
287 1.1.1.6 thorpej /* Answer a query from a utility program
288 1.1.1.6 thorpej * with all we know.
289 1.1.1.6 thorpej */
290 1.1.1.3 thorpej if (from->sin_port != htons(RIP_PORT)) {
291 1.1.1.5 christos supply(from, aifp, OUT_QUERY, 0,
292 1.1.1.5 christos rip->rip_vers, ap != 0);
293 1.1.1.4 christos return;
294 1.1.1.3 thorpej }
295 1.1.1.5 christos
296 1.1.1.4 christos /* A router trying to prime its tables.
297 1.1.1.4 christos * Filter the answer in the about same way
298 1.1.1.4 christos * broadcasts are filtered.
299 1.1.1.4 christos *
300 1.1.1.4 christos * Only answer a router if we are a supplier
301 1.1.1.4 christos * to keep an unwary host that is just starting
302 1.1.1.6 thorpej * from picking us as a router.
303 1.1.1.4 christos */
304 1.1.1.5 christos if (aifp == 0) {
305 1.1.1.5 christos trace_pkt("ignore distant router");
306 1.1.1.5 christos return;
307 1.1.1.5 christos }
308 1.1.1.4 christos if (!supplier
309 1.1.1.5 christos || IS_RIP_OFF(aifp->int_state)) {
310 1.1.1.5 christos trace_pkt("ignore; not supplying");
311 1.1.1.4 christos return;
312 1.1.1.5 christos }
313 1.1.1.4 christos
314 1.1.1.6 thorpej /* Do not answer a RIPv1 router if
315 1.1.1.6 thorpej * we are sending RIPv2. But do offer
316 1.1.1.6 thorpej * poor man's router discovery.
317 1.1.1.6 thorpej */
318 1.1.1.6 thorpej if ((aifp->int_state & IS_NO_RIPV1_OUT)
319 1.1.1.6 thorpej && rip->rip_vers == RIPv1) {
320 1.1.1.6 thorpej if (!(aifp->int_state & IS_PM_RDISC)) {
321 1.1.1.6 thorpej trace_pkt("ignore; sending RIPv2");
322 1.1.1.6 thorpej return;
323 1.1.1.6 thorpej }
324 1.1.1.6 thorpej
325 1.1.1.6 thorpej v12buf.n->n_family = RIP_AF_INET;
326 1.1.1.6 thorpej v12buf.n->n_dst = RIP_DEFAULT;
327 1.1.1.6 thorpej i = aifp->int_d_metric;
328 1.1.1.6 thorpej if (0 != (rt = rtget(RIP_DEFAULT, 0)))
329 1.1.1.6 thorpej i = MIN(i, (rt->rt_metric
330 1.1.1.6 thorpej +aifp->int_metric+1));
331 1.1.1.6 thorpej v12buf.n->n_metric = htonl(i);
332 1.1.1.6 thorpej v12buf.n++;
333 1.1.1.6 thorpej break;
334 1.1.1.6 thorpej }
335 1.1.1.6 thorpej
336 1.1.1.6 thorpej /* Respond with RIPv1 instead of RIPv2 if
337 1.1.1.6 thorpej * that is what we are broadcasting on the
338 1.1.1.6 thorpej * interface to keep the remote router from
339 1.1.1.6 thorpej * getting the wrong initial idea of the
340 1.1.1.6 thorpej * routes we send.
341 1.1.1.6 thorpej */
342 1.1.1.4 christos supply(from, aifp, OUT_UNICAST, 0,
343 1.1.1.6 thorpej (aifp->int_state & IS_NO_RIPV1_OUT)
344 1.1.1.5 christos ? RIPv2 : RIPv1,
345 1.1.1.5 christos ap != 0);
346 1.1 cgd return;
347 1.1 cgd }
348 1.1.1.3 thorpej
349 1.1.1.5 christos /* Ignore authentication */
350 1.1.1.5 christos if (n->n_family == RIP_AF_AUTH)
351 1.1.1.5 christos continue;
352 1.1.1.5 christos
353 1.1.1.3 thorpej if (n->n_family != RIP_AF_INET) {
354 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
355 1.1.1.6 thorpej "request from %s for unsupported"
356 1.1.1.6 thorpej " (af %d) %s",
357 1.1.1.5 christos naddr_ntoa(FROM_NADDR),
358 1.1.1.5 christos ntohs(n->n_family),
359 1.1.1.5 christos naddr_ntoa(n->n_dst));
360 1.1.1.3 thorpej return;
361 1.1.1.3 thorpej }
362 1.1.1.3 thorpej
363 1.1.1.5 christos /* We are being asked about a specific destination.
364 1.1.1.5 christos */
365 1.1.1.3 thorpej dst = n->n_dst;
366 1.1.1.3 thorpej if (!check_dst(dst)) {
367 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
368 1.1.1.5 christos "bad queried destination %s from %s",
369 1.1.1.5 christos naddr_ntoa(dst),
370 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
371 1.1.1.3 thorpej return;
372 1.1.1.3 thorpej }
373 1.1.1.3 thorpej
374 1.1.1.5 christos /* decide what mask was intended */
375 1.1.1.3 thorpej if (rip->rip_vers == RIPv1
376 1.1.1.3 thorpej || 0 == (mask = ntohl(n->n_mask))
377 1.1.1.3 thorpej || 0 != (ntohl(dst) & ~mask))
378 1.1.1.5 christos mask = ripv1_mask_host(dst, aifp);
379 1.1.1.3 thorpej
380 1.1.1.5 christos /* try to find the answer */
381 1.1.1.3 thorpej rt = rtget(dst, mask);
382 1.1.1.3 thorpej if (!rt && dst != RIP_DEFAULT)
383 1.1.1.3 thorpej rt = rtfind(n->n_dst);
384 1.1.1.3 thorpej
385 1.1.1.5 christos if (v12buf.buf->rip_vers != RIPv1)
386 1.1.1.5 christos v12buf.n->n_mask = mask;
387 1.1.1.3 thorpej if (rt == 0) {
388 1.1.1.5 christos /* we do not have the answer */
389 1.1.1.5 christos v12buf.n->n_metric = HOPCNT_INFINITY;
390 1.1.1.3 thorpej } else {
391 1.1.1.5 christos /* we have the answer, so compute the
392 1.1.1.5 christos * right metric and next hop.
393 1.1.1.5 christos */
394 1.1.1.5 christos v12buf.n->n_family = RIP_AF_INET;
395 1.1.1.5 christos v12buf.n->n_dst = dst;
396 1.1.1.5 christos v12buf.n->n_metric = (rt->rt_metric+1
397 1.1.1.5 christos + ((aifp!=0)
398 1.1.1.5 christos ? aifp->int_metric
399 1.1.1.5 christos : 1));
400 1.1.1.5 christos if (v12buf.n->n_metric > HOPCNT_INFINITY)
401 1.1.1.5 christos v12buf.n->n_metric = HOPCNT_INFINITY;
402 1.1.1.5 christos if (v12buf.buf->rip_vers != RIPv1) {
403 1.1.1.5 christos v12buf.n->n_tag = rt->rt_tag;
404 1.1.1.5 christos v12buf.n->n_mask = mask;
405 1.1.1.5 christos if (aifp != 0
406 1.1.1.3 thorpej && on_net(rt->rt_gate,
407 1.1.1.5 christos aifp->int_net,
408 1.1.1.5 christos aifp->int_mask)
409 1.1.1.5 christos && rt->rt_gate != aifp->int_addr)
410 1.1.1.5 christos v12buf.n->n_nhop = rt->rt_gate;
411 1.1.1.3 thorpej }
412 1.1.1.3 thorpej }
413 1.1.1.5 christos HTONL(v12buf.n->n_metric);
414 1.1.1.5 christos
415 1.1.1.5 christos /* Stop paying attention if we fill the output buffer.
416 1.1.1.5 christos */
417 1.1.1.5 christos if (++v12buf.n >= v12buf.lim)
418 1.1.1.5 christos break;
419 1.1.1.5 christos } while (++n < lim);
420 1.1.1.5 christos
421 1.1.1.5 christos /* Send the answer about specific routes.
422 1.1.1.5 christos */
423 1.1.1.5 christos if (ap != 0 && ap->type == RIP_AUTH_MD5)
424 1.1.1.5 christos end_md5_auth(&v12buf, ap);
425 1.1.1.5 christos
426 1.1.1.3 thorpej if (from->sin_port != htons(RIP_PORT)) {
427 1.1.1.3 thorpej /* query */
428 1.1.1.5 christos (void)output(OUT_QUERY, from, aifp,
429 1.1.1.5 christos v12buf.buf,
430 1.1.1.5 christos ((char *)v12buf.n - (char*)v12buf.buf));
431 1.1.1.3 thorpej } else if (supplier) {
432 1.1.1.5 christos (void)output(OUT_UNICAST, from, aifp,
433 1.1.1.5 christos v12buf.buf,
434 1.1.1.5 christos ((char *)v12buf.n - (char*)v12buf.buf));
435 1.1.1.5 christos } else {
436 1.1.1.5 christos /* Only answer a router if we are a supplier
437 1.1.1.5 christos * to keep an unwary host that is just starting
438 1.1.1.5 christos * from picking us an a router.
439 1.1.1.5 christos */
440 1.1.1.5 christos ;
441 1.1.1.3 thorpej }
442 1.1 cgd return;
443 1.1 cgd
444 1.1 cgd case RIPCMD_TRACEON:
445 1.1 cgd case RIPCMD_TRACEOFF:
446 1.1 cgd /* verify message came from a privileged port */
447 1.1.1.3 thorpej if (ntohs(from->sin_port) > IPPORT_RESERVED) {
448 1.1.1.3 thorpej msglog("trace command from untrusted port on %s",
449 1.1.1.3 thorpej naddr_ntoa(FROM_NADDR));
450 1.1 cgd return;
451 1.1.1.3 thorpej }
452 1.1.1.4 christos if (aifp == 0) {
453 1.1.1.3 thorpej msglog("trace command from unknown router %s",
454 1.1.1.3 thorpej naddr_ntoa(FROM_NADDR));
455 1.1 cgd return;
456 1.1 cgd }
457 1.1.1.3 thorpej if (rip->rip_cmd == RIPCMD_TRACEON) {
458 1.1.1.5 christos rip->rip_tracefile[cc-4] = '\0';
459 1.1.1.5 christos set_tracefile((char*)rip->rip_tracefile,
460 1.1.1.5 christos "trace command: %s\n", 0);
461 1.1.1.3 thorpej } else {
462 1.1.1.5 christos trace_off("tracing turned off by %s",
463 1.1.1.3 thorpej naddr_ntoa(FROM_NADDR));
464 1.1.1.3 thorpej }
465 1.1 cgd return;
466 1.1 cgd
467 1.1 cgd case RIPCMD_RESPONSE:
468 1.1.1.5 christos if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
469 1.1.1.5 christos msglim(&bad_len, FROM_NADDR,
470 1.1.1.5 christos "response of bad length (%d) from %s",
471 1.1.1.5 christos cc, naddr_ntoa(FROM_NADDR));
472 1.1.1.3 thorpej }
473 1.1.1.3 thorpej
474 1.1 cgd /* verify message came from a router */
475 1.1.1.3 thorpej if (from->sin_port != ntohs(RIP_PORT)) {
476 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
477 1.1.1.5 christos " discard RIP response from unknown port"
478 1.1.1.5 christos " %d", from->sin_port);
479 1.1 cgd return;
480 1.1.1.3 thorpej }
481 1.1.1.3 thorpej
482 1.1.1.3 thorpej if (rip_sock < 0) {
483 1.1.1.5 christos trace_pkt(" discard response while RIP off");
484 1.1.1.3 thorpej return;
485 1.1.1.3 thorpej }
486 1.1.1.3 thorpej
487 1.1.1.3 thorpej /* Are we talking to ourself or a remote gateway?
488 1.1.1.3 thorpej */
489 1.1.1.3 thorpej ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
490 1.1.1.3 thorpej if (ifp1) {
491 1.1.1.3 thorpej if (ifp1->int_state & IS_REMOTE) {
492 1.1.1.5 christos /* remote gateway */
493 1.1.1.5 christos aifp = ifp1;
494 1.1.1.5 christos if (check_remote(aifp)) {
495 1.1.1.5 christos aifp->int_act_time = now.tv_sec;
496 1.1.1.5 christos (void)if_ok(aifp, "remote ");
497 1.1.1.3 thorpej }
498 1.1.1.3 thorpej } else {
499 1.1.1.5 christos trace_pkt(" discard our own RIP response");
500 1.1.1.5 christos return;
501 1.1 cgd }
502 1.1 cgd }
503 1.1.1.3 thorpej
504 1.1.1.5 christos /* Accept routing packets from routers directly connected
505 1.1.1.5 christos * via broadcast or point-to-point networks, and from
506 1.1.1.3 thorpej * those listed in /etc/gateways.
507 1.1.1.3 thorpej */
508 1.1.1.5 christos if (aifp == 0) {
509 1.1.1.5 christos msglim(&unk_router, FROM_NADDR,
510 1.1.1.5 christos " discard response from %s"
511 1.1.1.5 christos " via unexpected interface",
512 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
513 1.1.1.3 thorpej return;
514 1.1.1.3 thorpej }
515 1.1.1.5 christos if (IS_RIP_IN_OFF(aifp->int_state)) {
516 1.1.1.5 christos trace_pkt(" discard RIPv%d response"
517 1.1.1.5 christos " via disabled interface %s",
518 1.1.1.5 christos rip->rip_vers, aifp->int_name);
519 1.1.1.5 christos return;
520 1.1.1.5 christos }
521 1.1.1.5 christos
522 1.1.1.5 christos if (n >= lim) {
523 1.1.1.5 christos msglim(&bad_len, FROM_NADDR, "empty response from %s",
524 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
525 1.1.1.3 thorpej return;
526 1.1.1.3 thorpej }
527 1.1.1.3 thorpej
528 1.1.1.4 christos if (((aifp->int_state & IS_NO_RIPV1_IN)
529 1.1.1.3 thorpej && rip->rip_vers == RIPv1)
530 1.1.1.4 christos || ((aifp->int_state & IS_NO_RIPV2_IN)
531 1.1.1.3 thorpej && rip->rip_vers != RIPv1)) {
532 1.1.1.5 christos trace_pkt(" discard RIPv%d response",
533 1.1.1.3 thorpej rip->rip_vers);
534 1.1.1.3 thorpej return;
535 1.1.1.3 thorpej }
536 1.1.1.3 thorpej
537 1.1.1.3 thorpej /* Ignore routes via dead interface.
538 1.1.1.3 thorpej */
539 1.1.1.4 christos if (aifp->int_state & IS_BROKE) {
540 1.1.1.6 thorpej trace_pkt("discard response via broken interface %s",
541 1.1.1.4 christos aifp->int_name);
542 1.1.1.3 thorpej return;
543 1.1.1.3 thorpej }
544 1.1.1.3 thorpej
545 1.1.1.5 christos /* If the interface cares, ignore bad routers.
546 1.1.1.5 christos * Trace but do not log this problem, because where it
547 1.1.1.5 christos * happens, it happens frequently.
548 1.1.1.5 christos */
549 1.1.1.5 christos if (aifp->int_state & IS_DISTRUST) {
550 1.1.1.6 thorpej tg = tgates;
551 1.1.1.5 christos while (tg->tgate_addr != FROM_NADDR) {
552 1.1.1.5 christos tg = tg->tgate_next;
553 1.1.1.5 christos if (tg == 0) {
554 1.1.1.5 christos trace_pkt(" discard RIP response"
555 1.1.1.5 christos " from untrusted router %s",
556 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
557 1.1.1.5 christos return;
558 1.1.1.5 christos }
559 1.1.1.4 christos }
560 1.1.1.3 thorpej }
561 1.1.1.3 thorpej
562 1.1.1.5 christos /* Authenticate the packet if we have a secret.
563 1.1.1.5 christos * If we do not have any secrets, ignore the error in
564 1.1.1.5 christos * RFC 1723 and accept it regardless.
565 1.1.1.5 christos */
566 1.1.1.5 christos if (aifp->int_auth[0].type != RIP_AUTH_NONE
567 1.1.1.5 christos && rip->rip_vers != RIPv1
568 1.1.1.5 christos && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
569 1.1.1.5 christos return;
570 1.1.1.5 christos
571 1.1.1.5 christos do {
572 1.1.1.3 thorpej if (n->n_family == RIP_AF_AUTH)
573 1.1 cgd continue;
574 1.1.1.3 thorpej
575 1.1.1.3 thorpej NTOHL(n->n_metric);
576 1.1.1.3 thorpej dst = n->n_dst;
577 1.1.1.3 thorpej if (n->n_family != RIP_AF_INET
578 1.1.1.3 thorpej && (n->n_family != RIP_AF_UNSPEC
579 1.1.1.3 thorpej || dst != RIP_DEFAULT)) {
580 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
581 1.1.1.5 christos "route from %s to unsupported"
582 1.1.1.5 christos " address family=%d destination=%s",
583 1.1.1.5 christos naddr_ntoa(FROM_NADDR),
584 1.1.1.5 christos n->n_family,
585 1.1.1.5 christos naddr_ntoa(dst));
586 1.1 cgd continue;
587 1.1 cgd }
588 1.1.1.3 thorpej if (!check_dst(dst)) {
589 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
590 1.1.1.5 christos "bad destination %s from %s",
591 1.1.1.5 christos naddr_ntoa(dst),
592 1.1.1.5 christos naddr_ntoa(FROM_NADDR));
593 1.1.1.3 thorpej return;
594 1.1 cgd }
595 1.1.1.3 thorpej if (n->n_metric == 0
596 1.1.1.3 thorpej || n->n_metric > HOPCNT_INFINITY) {
597 1.1.1.5 christos msglim(&bad_router, FROM_NADDR,
598 1.1.1.5 christos "bad metric %d from %s"
599 1.1.1.5 christos " for destination %s",
600 1.1.1.5 christos n->n_metric,
601 1.1.1.5 christos naddr_ntoa(FROM_NADDR),
602 1.1.1.5 christos naddr_ntoa(dst));
603 1.1.1.3 thorpej return;
604 1.1.1.3 thorpej }
605 1.1.1.3 thorpej
606 1.1.1.3 thorpej /* Notice the next-hop.
607 1.1 cgd */
608 1.1.1.5 christos gate = FROM_NADDR;
609 1.1.1.4 christos if (n->n_nhop != 0) {
610 1.1.1.4 christos if (rip->rip_vers == RIPv2) {
611 1.1.1.4 christos n->n_nhop = 0;
612 1.1.1.3 thorpej } else {
613 1.1.1.4 christos /* Use it only if it is valid. */
614 1.1.1.4 christos if (on_net(n->n_nhop,
615 1.1.1.4 christos aifp->int_net, aifp->int_mask)
616 1.1.1.4 christos && check_dst(n->n_nhop)) {
617 1.1.1.4 christos gate = n->n_nhop;
618 1.1.1.4 christos } else {
619 1.1.1.5 christos msglim(&bad_nhop, FROM_NADDR,
620 1.1.1.5 christos "router %s to %s"
621 1.1.1.5 christos " has bad next hop %s",
622 1.1.1.5 christos naddr_ntoa(FROM_NADDR),
623 1.1.1.5 christos naddr_ntoa(dst),
624 1.1.1.5 christos naddr_ntoa(n->n_nhop));
625 1.1.1.5 christos n->n_nhop = 0;
626 1.1.1.4 christos }
627 1.1.1.3 thorpej }
628 1.1.1.3 thorpej }
629 1.1.1.3 thorpej
630 1.1.1.3 thorpej if (rip->rip_vers == RIPv1
631 1.1.1.3 thorpej || 0 == (mask = ntohl(n->n_mask))) {
632 1.1.1.4 christos mask = ripv1_mask_host(dst,aifp);
633 1.1.1.3 thorpej } else if ((ntohl(dst) & ~mask) != 0) {
634 1.1.1.5 christos msglim(&bad_mask, FROM_NADDR,
635 1.1.1.5 christos "router %s sent bad netmask"
636 1.1.1.5 christos " %#x with %s",
637 1.1.1.5 christos naddr_ntoa(FROM_NADDR),
638 1.1.1.5 christos mask,
639 1.1.1.5 christos naddr_ntoa(dst));
640 1.1 cgd continue;
641 1.1 cgd }
642 1.1.1.3 thorpej if (rip->rip_vers == RIPv1)
643 1.1.1.3 thorpej n->n_tag = 0;
644 1.1.1.3 thorpej
645 1.1.1.3 thorpej /* Adjust metric according to incoming interface..
646 1.1.1.3 thorpej */
647 1.1.1.4 christos n->n_metric += aifp->int_metric;
648 1.1.1.3 thorpej if (n->n_metric > HOPCNT_INFINITY)
649 1.1.1.3 thorpej n->n_metric = HOPCNT_INFINITY;
650 1.1.1.3 thorpej
651 1.1.1.6 thorpej /* Should we trust this route from this router? */
652 1.1.1.6 thorpej if (tg && (tn = tg->tgate_nets)->mask != 0) {
653 1.1.1.6 thorpej for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
654 1.1.1.6 thorpej if (on_net(dst, tn->net, tn->mask)
655 1.1.1.6 thorpej && tn->mask <= mask)
656 1.1.1.6 thorpej break;
657 1.1.1.6 thorpej }
658 1.1.1.6 thorpej if (i >= MAX_TGATE_NETS || tn->mask == 0) {
659 1.1.1.6 thorpej trace_pkt(" ignored unauthorized %s",
660 1.1.1.6 thorpej addrname(dst,mask,0));
661 1.1.1.6 thorpej continue;
662 1.1.1.6 thorpej }
663 1.1.1.6 thorpej }
664 1.1.1.6 thorpej
665 1.1.1.3 thorpej /* Recognize and ignore a default route we faked
666 1.1.1.3 thorpej * which is being sent back to us by a machine with
667 1.1.1.3 thorpej * broken split-horizon.
668 1.1.1.3 thorpej * Be a little more paranoid than that, and reject
669 1.1.1.3 thorpej * default routes with the same metric we advertised.
670 1.1.1.3 thorpej */
671 1.1.1.4 christos if (aifp->int_d_metric != 0
672 1.1.1.3 thorpej && dst == RIP_DEFAULT
673 1.1.1.4 christos && n->n_metric >= aifp->int_d_metric)
674 1.1.1.3 thorpej continue;
675 1.1 cgd
676 1.1.1.3 thorpej /* We can receive aggregated RIPv2 routes that must
677 1.1.1.3 thorpej * be broken down before they are transmitted by
678 1.1.1.3 thorpej * RIPv1 via an interface on a subnet.
679 1.1.1.3 thorpej * We might also receive the same routes aggregated
680 1.1.1.3 thorpej * via other RIPv2 interfaces.
681 1.1.1.3 thorpej * This could cause duplicate routes to be sent on
682 1.1.1.3 thorpej * the RIPv1 interfaces. "Longest matching variable
683 1.1.1.3 thorpej * length netmasks" lets RIPv2 listeners understand,
684 1.1.1.3 thorpej * but breaking down the aggregated routes for RIPv1
685 1.1.1.3 thorpej * listeners can produce duplicate routes.
686 1.1.1.3 thorpej *
687 1.1.1.3 thorpej * Breaking down aggregated routes here bloats
688 1.1.1.3 thorpej * the daemon table, but does not hurt the kernel
689 1.1.1.3 thorpej * table, since routes are always aggregated for
690 1.1.1.3 thorpej * the kernel.
691 1.1.1.3 thorpej *
692 1.1.1.3 thorpej * Notice that this does not break down network
693 1.1.1.3 thorpej * routes corresponding to subnets. This is part
694 1.1.1.3 thorpej * of the defense against RS_NET_SYN.
695 1.1 cgd */
696 1.1.1.3 thorpej if (have_ripv1_out
697 1.1.1.3 thorpej && (((rt = rtget(dst,mask)) == 0
698 1.1.1.5 christos || !(rt->rt_state & RS_NET_SYN)))
699 1.1.1.5 christos && (v1_mask = ripv1_mask_net(dst,0)) > mask) {
700 1.1.1.3 thorpej ddst_h = v1_mask & -v1_mask;
701 1.1.1.3 thorpej i = (v1_mask & ~mask)/ddst_h;
702 1.1.1.4 christos if (i >= 511) {
703 1.1.1.3 thorpej /* Punt if we would have to generate
704 1.1.1.3 thorpej * an unreasonable number of routes.
705 1.1.1.3 thorpej */
706 1.1.1.6 thorpej if (TRACECONTENTS)
707 1.1.1.6 thorpej trace_misc("accept %s-->%s as 1"
708 1.1.1.6 thorpej " instead of %d routes",
709 1.1.1.6 thorpej addrname(dst,mask,0),
710 1.1.1.6 thorpej naddr_ntoa(FROM_NADDR),
711 1.1.1.6 thorpej i+1);
712 1.1.1.3 thorpej i = 0;
713 1.1.1.3 thorpej } else {
714 1.1.1.3 thorpej mask = v1_mask;
715 1.1.1.3 thorpej }
716 1.1.1.3 thorpej } else {
717 1.1.1.3 thorpej i = 0;
718 1.1.1.3 thorpej }
719 1.1.1.3 thorpej
720 1.1.1.6 thorpej new.rts_gate = gate;
721 1.1.1.6 thorpej new.rts_router = FROM_NADDR;
722 1.1.1.6 thorpej new.rts_metric = n->n_metric;
723 1.1.1.6 thorpej new.rts_tag = n->n_tag;
724 1.1.1.6 thorpej new.rts_time = now.tv_sec;
725 1.1.1.6 thorpej new.rts_ifp = aifp;
726 1.1.1.6 thorpej new.rts_de_ag = i;
727 1.1.1.6 thorpej j = 0;
728 1.1.1.3 thorpej for (;;) {
729 1.1.1.6 thorpej input_route(dst, mask, &new, n);
730 1.1.1.6 thorpej if (++j > i)
731 1.1.1.3 thorpej break;
732 1.1.1.3 thorpej dst = htonl(ntohl(dst) + ddst_h);
733 1.1 cgd }
734 1.1.1.5 christos } while (++n < lim);
735 1.1 cgd break;
736 1.1 cgd }
737 1.1.1.5 christos #undef FROM_NADDR
738 1.1.1.3 thorpej }
739 1.1.1.3 thorpej
740 1.1.1.3 thorpej
741 1.1.1.3 thorpej /* Process a single input route.
742 1.1.1.3 thorpej */
743 1.1.1.3 thorpej static void
744 1.1.1.6 thorpej input_route(naddr dst, /* network order */
745 1.1.1.3 thorpej naddr mask,
746 1.1.1.6 thorpej struct rt_spare *new,
747 1.1.1.3 thorpej struct netinfo *n)
748 1.1.1.3 thorpej {
749 1.1.1.3 thorpej int i;
750 1.1.1.3 thorpej struct rt_entry *rt;
751 1.1.1.3 thorpej struct rt_spare *rts, *rts0;
752 1.1.1.3 thorpej struct interface *ifp1;
753 1.1.1.3 thorpej
754 1.1.1.3 thorpej
755 1.1.1.3 thorpej /* See if the other guy is telling us to send our packets to him.
756 1.1.1.3 thorpej * Sometimes network routes arrive over a point-to-point link for
757 1.1.1.3 thorpej * the network containing the address(es) of the link.
758 1.1.1.3 thorpej *
759 1.1.1.3 thorpej * If our interface is broken, switch to using the other guy.
760 1.1.1.3 thorpej */
761 1.1.1.3 thorpej ifp1 = ifwithaddr(dst, 1, 1);
762 1.1.1.3 thorpej if (ifp1 != 0
763 1.1.1.5 christos && (!(ifp1->int_state & IS_BROKE)
764 1.1.1.5 christos || (ifp1->int_state & IS_PASSIVE)))
765 1.1.1.3 thorpej return;
766 1.1.1.3 thorpej
767 1.1.1.3 thorpej /* Look for the route in our table.
768 1.1.1.3 thorpej */
769 1.1.1.3 thorpej rt = rtget(dst, mask);
770 1.1.1.3 thorpej
771 1.1.1.3 thorpej /* Consider adding the route if we do not already have it.
772 1.1.1.3 thorpej */
773 1.1.1.3 thorpej if (rt == 0) {
774 1.1.1.3 thorpej /* Ignore unknown routes being poisoned.
775 1.1.1.3 thorpej */
776 1.1.1.6 thorpej if (new->rts_metric == HOPCNT_INFINITY)
777 1.1.1.3 thorpej return;
778 1.1.1.3 thorpej
779 1.1.1.4 christos /* Ignore the route if it points to us */
780 1.1.1.4 christos if (n->n_nhop != 0
781 1.1.1.4 christos && 0 != ifwithaddr(n->n_nhop, 1, 0))
782 1.1.1.4 christos return;
783 1.1.1.4 christos
784 1.1.1.4 christos /* If something has not gone crazy and tried to fill
785 1.1.1.4 christos * our memory, accept the new route.
786 1.1.1.4 christos */
787 1.1.1.4 christos if (total_routes < MAX_ROUTES)
788 1.1.1.6 thorpej rtadd(dst, mask, 0, new);
789 1.1.1.3 thorpej return;
790 1.1.1.3 thorpej }
791 1.1 cgd
792 1.1.1.3 thorpej /* We already know about the route. Consider this update.
793 1.1.1.3 thorpej *
794 1.1.1.3 thorpej * If (rt->rt_state & RS_NET_SYN), then this route
795 1.1.1.3 thorpej * is the same as a network route we have inferred
796 1.1.1.3 thorpej * for subnets we know, in order to tell RIPv1 routers
797 1.1.1.3 thorpej * about the subnets.
798 1.1.1.3 thorpej *
799 1.1.1.3 thorpej * It is impossible to tell if the route is coming
800 1.1.1.3 thorpej * from a distant RIPv2 router with the standard
801 1.1.1.3 thorpej * netmask because that router knows about the entire
802 1.1.1.3 thorpej * network, or if it is a round-about echo of a
803 1.1.1.3 thorpej * synthetic, RIPv1 network route of our own.
804 1.1.1.3 thorpej * The worst is that both kinds of routes might be
805 1.1.1.3 thorpej * received, and the bad one might have the smaller
806 1.1.1.4 christos * metric. Partly solve this problem by never
807 1.1.1.4 christos * aggregating into such a route. Also keep it
808 1.1.1.3 thorpej * around as long as the interface exists.
809 1.1 cgd */
810 1.1.1.3 thorpej
811 1.1.1.3 thorpej rts0 = rt->rt_spares;
812 1.1.1.3 thorpej for (rts = rts0, i = NUM_SPARES; i != 0; i--, rts++) {
813 1.1.1.6 thorpej if (rts->rts_router == new->rts_router)
814 1.1.1.3 thorpej break;
815 1.1.1.3 thorpej /* Note the worst slot to reuse,
816 1.1.1.3 thorpej * other than the current slot.
817 1.1.1.3 thorpej */
818 1.1.1.3 thorpej if (rts0 == rt->rt_spares
819 1.1.1.3 thorpej || BETTER_LINK(rt, rts0, rts))
820 1.1.1.3 thorpej rts0 = rts;
821 1.1.1.3 thorpej }
822 1.1.1.3 thorpej if (i != 0) {
823 1.1.1.6 thorpej /* Found a route from the router already in the table.
824 1.1.1.3 thorpej */
825 1.1.1.6 thorpej
826 1.1.1.6 thorpej /* If the new route is a route broken down from an
827 1.1.1.6 thorpej * aggregated route, and if the previous route is either
828 1.1.1.6 thorpej * not a broken down route or was broken down from a finer
829 1.1.1.6 thorpej * netmask, and if the previous route is current,
830 1.1.1.6 thorpej * then forget this one.
831 1.1.1.6 thorpej */
832 1.1.1.6 thorpej if (new->rts_de_ag > rts->rts_de_ag
833 1.1.1.6 thorpej && now_stale <= rts->rts_time)
834 1.1.1.6 thorpej return;
835 1.1.1.3 thorpej
836 1.1.1.4 christos /* Keep poisoned routes around only long enough to pass
837 1.1.1.6 thorpej * the poison on. Use a new timestamp for good routes.
838 1.1.1.3 thorpej */
839 1.1.1.6 thorpej if (rts->rts_metric == HOPCNT_INFINITY
840 1.1.1.6 thorpej && new->rts_metric == HOPCNT_INFINITY)
841 1.1.1.6 thorpej new->rts_time = rts->rts_time;
842 1.1.1.3 thorpej
843 1.1.1.3 thorpej /* If this is an update for the router we currently prefer,
844 1.1.1.3 thorpej * then note it.
845 1.1.1.3 thorpej */
846 1.1.1.3 thorpej if (i == NUM_SPARES) {
847 1.1.1.6 thorpej rtchange(rt, rt->rt_state, new, 0);
848 1.1.1.3 thorpej /* If the route got worse, check for something better.
849 1.1 cgd */
850 1.1.1.6 thorpej if (new->rts_metric > rts->rts_metric)
851 1.1.1.3 thorpej rtswitch(rt, 0);
852 1.1.1.3 thorpej return;
853 1.1.1.3 thorpej }
854 1.1.1.3 thorpej
855 1.1.1.3 thorpej /* This is an update for a spare route.
856 1.1.1.3 thorpej * Finished if the route is unchanged.
857 1.1.1.3 thorpej */
858 1.1.1.6 thorpej if (rts->rts_gate == new->rts_gate
859 1.1.1.6 thorpej && rts->rts_metric == new->rts_metric
860 1.1.1.6 thorpej && rts->rts_tag == new->rts_tag) {
861 1.1.1.6 thorpej trace_upslot(rt, rts, new);
862 1.1.1.6 thorpej *rts = *new;
863 1.1.1.3 thorpej return;
864 1.1.1.6 thorpej }
865 1.1.1.6 thorpej /* Forget it if it has gone bad.
866 1.1.1.6 thorpej */
867 1.1.1.6 thorpej if (new->rts_metric == HOPCNT_INFINITY) {
868 1.1.1.5 christos rts_delete(rt, rts);
869 1.1.1.5 christos return;
870 1.1 cgd }
871 1.1.1.3 thorpej
872 1.1.1.3 thorpej } else {
873 1.1.1.3 thorpej /* The update is for a route we know about,
874 1.1.1.3 thorpej * but not from a familiar router.
875 1.1.1.4 christos *
876 1.1.1.4 christos * Ignore the route if it points to us.
877 1.1.1.3 thorpej */
878 1.1.1.4 christos if (n->n_nhop != 0
879 1.1.1.4 christos && 0 != ifwithaddr(n->n_nhop, 1, 0))
880 1.1.1.4 christos return;
881 1.1.1.4 christos
882 1.1.1.6 thorpej /* the loop above set rts0=worst spare */
883 1.1.1.3 thorpej rts = rts0;
884 1.1.1.3 thorpej
885 1.1.1.3 thorpej /* Save the route as a spare only if it has
886 1.1.1.3 thorpej * a better metric than our worst spare.
887 1.1.1.3 thorpej * This also ignores poisoned routes (those
888 1.1.1.3 thorpej * received with metric HOPCNT_INFINITY).
889 1.1.1.3 thorpej */
890 1.1.1.6 thorpej if (new->rts_metric >= rts->rts_metric)
891 1.1.1.3 thorpej return;
892 1.1 cgd }
893 1.1.1.3 thorpej
894 1.1.1.6 thorpej trace_upslot(rt, rts, new);
895 1.1.1.6 thorpej *rts = *new;
896 1.1.1.3 thorpej
897 1.1.1.3 thorpej /* try to switch to a better route */
898 1.1.1.3 thorpej rtswitch(rt, rts);
899 1.1.1.5 christos }
900 1.1.1.5 christos
901 1.1.1.5 christos
902 1.1.1.5 christos static int /* 0 if bad */
903 1.1.1.5 christos ck_passwd(struct interface *aifp,
904 1.1.1.5 christos struct rip *rip,
905 1.1.1.5 christos void *lim,
906 1.1.1.5 christos naddr from,
907 1.1.1.5 christos struct msg_limit *use_authp)
908 1.1.1.5 christos {
909 1.1.1.5 christos # define NA (rip->rip_auths)
910 1.1.1.5 christos struct netauth *na2;
911 1.1.1.5 christos struct auth *ap;
912 1.1.1.5 christos MD5_CTX md5_ctx;
913 1.1.1.5 christos u_char hash[RIP_AUTH_PW_LEN];
914 1.1.1.5 christos int i;
915 1.1.1.5 christos
916 1.1.1.5 christos
917 1.1.1.5 christos if ((void *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
918 1.1.1.5 christos msglim(use_authp, from, "missing password from %s",
919 1.1.1.5 christos naddr_ntoa(from));
920 1.1.1.5 christos return 0;
921 1.1.1.5 christos }
922 1.1.1.5 christos
923 1.1.1.5 christos /* accept any current (+/- 24 hours) password
924 1.1.1.5 christos */
925 1.1.1.5 christos for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
926 1.1.1.5 christos if (ap->type != NA->a_type
927 1.1.1.5 christos || (u_long)ap->start > (u_long)clk.tv_sec+DAY
928 1.1.1.5 christos || (u_long)ap->end+DAY < (u_long)clk.tv_sec)
929 1.1.1.5 christos continue;
930 1.1.1.5 christos
931 1.1.1.5 christos if (NA->a_type == RIP_AUTH_PW) {
932 1.1.1.5 christos if (!bcmp(NA->au.au_pw, ap->key, RIP_AUTH_PW_LEN))
933 1.1.1.5 christos return 1;
934 1.1.1.5 christos
935 1.1.1.5 christos } else {
936 1.1.1.5 christos /* accept MD5 secret with the right key ID
937 1.1.1.5 christos */
938 1.1.1.5 christos if (NA->au.a_md5.md5_keyid != ap->keyid)
939 1.1.1.5 christos continue;
940 1.1.1.5 christos
941 1.1.1.5 christos na2 = (struct netauth *)((char *)(NA+1)
942 1.1.1.5 christos + NA->au.a_md5.md5_pkt_len);
943 1.1.1.5 christos if (NA->au.a_md5.md5_pkt_len % sizeof(*NA) != 0
944 1.1.1.5 christos || lim < (void *)(na2+1)) {
945 1.1.1.5 christos msglim(use_authp, from,
946 1.1.1.5 christos "bad MD5 RIP-II pkt length %d from %s",
947 1.1.1.5 christos NA->au.a_md5.md5_pkt_len,
948 1.1.1.5 christos naddr_ntoa(from));
949 1.1.1.5 christos return 0;
950 1.1.1.5 christos }
951 1.1.1.5 christos MD5Init(&md5_ctx);
952 1.1.1.5 christos MD5Update(&md5_ctx, (u_char *)NA,
953 1.1.1.5 christos (char *)na2->au.au_pw - (char *)NA);
954 1.1.1.5 christos MD5Update(&md5_ctx,
955 1.1.1.5 christos (u_char *)ap->key, sizeof(ap->key));
956 1.1.1.5 christos MD5Final(hash, &md5_ctx);
957 1.1.1.5 christos if (na2->a_family != RIP_AF_AUTH
958 1.1.1.5 christos || na2->a_type != 1
959 1.1.1.5 christos || NA->au.a_md5.md5_auth_len != RIP_AUTH_PW_LEN
960 1.1.1.5 christos || bcmp(hash, na2->au.au_pw, sizeof(hash)))
961 1.1.1.5 christos return 0;
962 1.1.1.5 christos return 1;
963 1.1.1.5 christos }
964 1.1.1.5 christos }
965 1.1.1.5 christos
966 1.1.1.5 christos msglim(use_authp, from, "bad password from %s",
967 1.1.1.5 christos naddr_ntoa(from));
968 1.1.1.5 christos return 0;
969 1.1.1.5 christos #undef NA
970 1.1 cgd }
971