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