2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sbin/routed/input.c,v 1.7.2.1 2001/08/01 09:01:45 obrien Exp $
34 * $DragonFly: src/sbin/routed/input.c,v 1.4 2004/12/18 21:43:40 swildner Exp $
39 #if !defined(sgi) && !defined(__NetBSD__)
40 static char sccsid
[] __attribute__((unused
)) = "@(#)input.c 8.1 (Berkeley) 6/5/93";
41 #elif defined(__NetBSD__)
42 #include <sys/cdefs.h>
45 #ident "$FreeBSD: src/sbin/routed/input.c,v 1.7.2.1 2001/08/01 09:01:45 obrien Exp $"
47 static void input(struct sockaddr_in
*, struct interface
*, struct interface
*,
49 static void input_route(naddr
, naddr
, struct rt_spare
*, struct netinfo
*);
50 static int ck_passwd(struct interface
*, struct rip
*, void *,
51 naddr
, struct msg_limit
*);
58 struct interface
*sifp
)
60 struct sockaddr_in from
;
61 struct interface
*aifp
;
64 static struct msg_limit bad_name
;
66 char ifname
[IFNAMSIZ
];
77 fromlen
= sizeof(from
);
78 cc
= recvfrom(sock
, &inbuf
, sizeof(inbuf
), 0,
79 (struct sockaddr
*)&from
, &fromlen
);
81 if (cc
< 0 && errno
!= EWOULDBLOCK
)
82 LOGERR("recvfrom(rip)");
85 if (fromlen
!= sizeof(struct sockaddr_in
))
86 logbad(1,"impossible recvfrom(rip) fromlen=%d",
89 /* aifp is the "authenticated" interface via which the packet
90 * arrived. In fact, it is only the interface on which
91 * the packet should have arrived based on is source
93 * sifp is interface associated with the socket through which
94 * the packet was received.
97 if ((cc
-= sizeof(inbuf
.ifname
)) < 0)
98 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
99 cc
+sizeof(inbuf
.ifname
));
101 /* check the remote interfaces first */
102 for (aifp
= remote_if
; aifp
; aifp
= aifp
->int_rlink
) {
103 if (aifp
->int_addr
== from
.sin_addr
.s_addr
)
107 aifp
= ifwithname(inbuf
.ifname
, 0);
109 msglim(&bad_name
, from
.sin_addr
.s_addr
,
110 "impossible interface name %.*s",
111 IFNAMSIZ
, inbuf
.ifname
);
112 } else if (((aifp
->int_if_flags
& IFF_POINTOPOINT
)
113 && aifp
->int_dstaddr
!=from
.sin_addr
.s_addr
)
114 || (!(aifp
->int_if_flags
& IFF_POINTOPOINT
)
115 && !on_net(from
.sin_addr
.s_addr
,
118 /* If it came via the wrong interface, do not
125 aifp
= iflookup(from
.sin_addr
.s_addr
);
130 input(&from
, sifp
, aifp
, &inbuf
.pbuf
.rip
, cc
);
135 /* Process a RIP packet
138 input(struct sockaddr_in
*from
, /* received from this IP address */
139 struct interface
*sifp
, /* interface of incoming socket */
140 struct interface
*aifp
, /* "authenticated" interface */
144 # define FROM_NADDR from->sin_addr.s_addr
145 static struct msg_limit use_auth
, bad_len
, bad_mask
;
146 static struct msg_limit unk_router
, bad_router
, bad_nhop
;
150 struct netinfo
*n
, *lim
;
151 struct interface
*ifp1
;
152 naddr gate
, mask
, v1_mask
, dst
, ddst_h
= 0;
154 struct tgate
*tg
= 0;
155 struct tgate_net
*tn
;
158 /* Notice when we hear from a remote gateway
161 && (aifp
->int_state
& IS_REMOTE
))
162 aifp
->int_act_time
= now
.tv_sec
;
164 trace_rip("Recv", "from", from
, sifp
, rip
, cc
);
166 if (rip
->rip_vers
== 0) {
167 msglim(&bad_router
, FROM_NADDR
,
168 "RIP version 0, cmd %d, packet received from %s",
169 rip
->rip_cmd
, naddr_ntoa(FROM_NADDR
));
171 } else if (rip
->rip_vers
> RIPv2
) {
172 rip
->rip_vers
= RIPv2
;
174 if (cc
> (int)OVER_MAXPACKETSIZE
) {
175 msglim(&bad_router
, FROM_NADDR
,
176 "packet at least %d bytes too long received from %s",
177 cc
-MAXPACKETSIZE
, naddr_ntoa(FROM_NADDR
));
182 lim
= (struct netinfo
*)((char*)rip
+ cc
);
184 /* Notice authentication.
185 * As required by section 4.2 in RFC 1723, discard authenticated
186 * RIPv2 messages, but only if configured for that silliness.
188 * RIPv2 authentication is lame. Why authenticate queries?
189 * Why should a RIPv2 implementation with authentication disabled
190 * not be able to listen to RIPv2 packets with authentication, while
191 * RIPv1 systems will listen? Crazy!
194 && rip
->rip_vers
== RIPv2
195 && n
< lim
&& n
->n_family
== RIP_AF_AUTH
) {
196 msglim(&use_auth
, FROM_NADDR
,
197 "RIPv2 message with authentication from %s discarded",
198 naddr_ntoa(FROM_NADDR
));
202 switch (rip
->rip_cmd
) {
204 /* For mere requests, be a little sloppy about the source
209 /* Are we talking to ourself or a remote gateway?
211 ifp1
= ifwithaddr(FROM_NADDR
, 0, 1);
213 if (ifp1
->int_state
& IS_REMOTE
) {
216 if (check_remote(aifp
)) {
217 aifp
->int_act_time
= now
.tv_sec
;
218 if_ok(aifp
, "remote ");
220 } else if (from
->sin_port
== htons(RIP_PORT
)) {
221 trace_pkt(" discard our own RIP request");
226 /* did the request come from a router?
228 if (from
->sin_port
== htons(RIP_PORT
)) {
229 /* yes, ignore the request if RIP is off so that
230 * the router does not depend on us.
234 && IS_RIP_OUT_OFF(aifp
->int_state
))) {
235 trace_pkt(" discard request while RIP off");
240 /* According to RFC 1723, we should ignore unauthenticated
241 * queries. That is too silly to bother with. Sheesh!
242 * Are forwarding tables supposed to be secret, when
243 * a bad guy can infer them with test traffic? When RIP
244 * is still the most common router-discovery protocol
245 * and so hosts need to send queries that will be answered?
246 * What about `rtquery`?
247 * Maybe on firewalls you'd care, but not enough to
248 * give up the diagnostic facilities of remote probing.
252 msglim(&bad_len
, FROM_NADDR
, "empty request from %s",
253 naddr_ntoa(FROM_NADDR
));
256 if (cc
%sizeof(*n
) != sizeof(struct rip
)%sizeof(*n
)) {
257 msglim(&bad_len
, FROM_NADDR
,
258 "request of bad length (%d) from %s",
259 cc
, naddr_ntoa(FROM_NADDR
));
262 if (rip
->rip_vers
== RIPv2
263 && (aifp
== 0 || (aifp
->int_state
& IS_NO_RIPV1_OUT
))) {
264 v12buf
.buf
->rip_vers
= RIPv2
;
265 /* If we have a secret but it is a cleartext secret,
266 * do not disclose our secret unless the other guy
269 ap
= find_auth(aifp
);
270 if (ap
!= 0 && ap
->type
== RIP_AUTH_PW
271 && n
->n_family
== RIP_AF_AUTH
272 && !ck_passwd(aifp
,rip
,lim
,FROM_NADDR
,&use_auth
))
275 v12buf
.buf
->rip_vers
= RIPv1
;
278 clr_ws_buf(&v12buf
, ap
);
281 n
->n_metric
= ntohl(n
->n_metric
);
283 /* A single entry with family RIP_AF_UNSPEC and
284 * metric HOPCNT_INFINITY means "all routes".
285 * We respond to routers only if we are acting
286 * as a supplier, or to anyone other than a router
289 if (n
->n_family
== RIP_AF_UNSPEC
290 && n
->n_metric
== HOPCNT_INFINITY
) {
291 /* Answer a query from a utility program
294 if (from
->sin_port
!= htons(RIP_PORT
)) {
295 supply(from
, aifp
, OUT_QUERY
, 0,
296 rip
->rip_vers
, ap
!= 0);
300 /* A router trying to prime its tables.
301 * Filter the answer in the about same way
302 * broadcasts are filtered.
304 * Only answer a router if we are a supplier
305 * to keep an unwary host that is just starting
306 * from picking us as a router.
309 trace_pkt("ignore distant router");
313 || IS_RIP_OFF(aifp
->int_state
)) {
314 trace_pkt("ignore; not supplying");
318 /* Do not answer a RIPv1 router if
319 * we are sending RIPv2. But do offer
320 * poor man's router discovery.
322 if ((aifp
->int_state
& IS_NO_RIPV1_OUT
)
323 && rip
->rip_vers
== RIPv1
) {
324 if (!(aifp
->int_state
& IS_PM_RDISC
)) {
325 trace_pkt("ignore; sending RIPv2");
329 v12buf
.n
->n_family
= RIP_AF_INET
;
330 v12buf
.n
->n_dst
= RIP_DEFAULT
;
331 i
= aifp
->int_d_metric
;
332 if (0 != (rt
= rtget(RIP_DEFAULT
, 0)))
333 i
= MIN(i
, (rt
->rt_metric
334 +aifp
->int_metric
+1));
335 v12buf
.n
->n_metric
= htonl(i
);
340 /* Respond with RIPv1 instead of RIPv2 if
341 * that is what we are broadcasting on the
342 * interface to keep the remote router from
343 * getting the wrong initial idea of the
346 supply(from
, aifp
, OUT_UNICAST
, 0,
347 (aifp
->int_state
& IS_NO_RIPV1_OUT
)
353 /* Ignore authentication */
354 if (n
->n_family
== RIP_AF_AUTH
)
357 if (n
->n_family
!= RIP_AF_INET
) {
358 msglim(&bad_router
, FROM_NADDR
,
359 "request from %s for unsupported"
361 naddr_ntoa(FROM_NADDR
),
363 naddr_ntoa(n
->n_dst
));
367 /* We are being asked about a specific destination.
370 if (!check_dst(dst
)) {
371 msglim(&bad_router
, FROM_NADDR
,
372 "bad queried destination %s from %s",
374 naddr_ntoa(FROM_NADDR
));
378 /* decide what mask was intended */
379 if (rip
->rip_vers
== RIPv1
380 || 0 == (mask
= ntohl(n
->n_mask
))
381 || 0 != (ntohl(dst
) & ~mask
))
382 mask
= ripv1_mask_host(dst
, aifp
);
384 /* try to find the answer */
385 rt
= rtget(dst
, mask
);
386 if (!rt
&& dst
!= RIP_DEFAULT
)
387 rt
= rtfind(n
->n_dst
);
389 if (v12buf
.buf
->rip_vers
!= RIPv1
)
390 v12buf
.n
->n_mask
= mask
;
392 /* we do not have the answer */
393 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
395 /* we have the answer, so compute the
396 * right metric and next hop.
398 v12buf
.n
->n_family
= RIP_AF_INET
;
399 v12buf
.n
->n_dst
= dst
;
400 v12buf
.n
->n_metric
= (rt
->rt_metric
+1
404 if (v12buf
.n
->n_metric
> HOPCNT_INFINITY
)
405 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
406 if (v12buf
.buf
->rip_vers
!= RIPv1
) {
407 v12buf
.n
->n_tag
= rt
->rt_tag
;
408 v12buf
.n
->n_mask
= mask
;
410 && on_net(rt
->rt_gate
,
413 && rt
->rt_gate
!= aifp
->int_addr
)
414 v12buf
.n
->n_nhop
= rt
->rt_gate
;
417 v12buf
.n
->n_metric
= htonl(v12buf
.n
->n_metric
);
419 /* Stop paying attention if we fill the output buffer.
421 if (++v12buf
.n
>= v12buf
.lim
)
425 /* Send the answer about specific routes.
427 if (ap
!= 0 && ap
->type
== RIP_AUTH_MD5
)
428 end_md5_auth(&v12buf
, ap
);
430 if (from
->sin_port
!= htons(RIP_PORT
)) {
432 output(OUT_QUERY
, from
, aifp
,
434 ((char *)v12buf
.n
- (char*)v12buf
.buf
));
435 } else if (supplier
) {
436 output(OUT_UNICAST
, from
, aifp
,
438 ((char *)v12buf
.n
- (char*)v12buf
.buf
));
440 /* Only answer a router if we are a supplier
441 * to keep an unwary host that is just starting
442 * from picking us an a router.
449 case RIPCMD_TRACEOFF
:
450 /* Notice that trace messages are turned off for all possible
451 * abuse if _PATH_TRACE is undefined in pathnames.h.
452 * Notice also that because of the way the trace file is
453 * handled in trace.c, no abuse is plausible even if
454 * _PATH_TRACE_ is defined.
456 * First verify message came from a privileged port. */
457 if (ntohs(from
->sin_port
) > IPPORT_RESERVED
) {
458 msglog("trace command from untrusted port on %s",
459 naddr_ntoa(FROM_NADDR
));
463 msglog("trace command from unknown router %s",
464 naddr_ntoa(FROM_NADDR
));
467 if (rip
->rip_cmd
== RIPCMD_TRACEON
) {
468 rip
->rip_tracefile
[cc
-4] = '\0';
469 set_tracefile((char*)rip
->rip_tracefile
,
470 "trace command: %s\n", 0);
472 trace_off("tracing turned off by %s",
473 naddr_ntoa(FROM_NADDR
));
477 case RIPCMD_RESPONSE
:
478 if (cc
%sizeof(*n
) != sizeof(struct rip
)%sizeof(*n
)) {
479 msglim(&bad_len
, FROM_NADDR
,
480 "response of bad length (%d) from %s",
481 cc
, naddr_ntoa(FROM_NADDR
));
484 /* verify message came from a router */
485 if (from
->sin_port
!= ntohs(RIP_PORT
)) {
486 msglim(&bad_router
, FROM_NADDR
,
487 " discard RIP response from unknown port"
488 " %d on host %s", ntohs(from
->sin_port
),
489 naddr_ntoa(FROM_NADDR
));
494 trace_pkt(" discard response while RIP off");
498 /* Are we talking to ourself or a remote gateway?
500 ifp1
= ifwithaddr(FROM_NADDR
, 0, 1);
502 if (ifp1
->int_state
& IS_REMOTE
) {
505 if (check_remote(aifp
)) {
506 aifp
->int_act_time
= now
.tv_sec
;
507 if_ok(aifp
, "remote ");
510 trace_pkt(" discard our own RIP response");
515 /* Accept routing packets from routers directly connected
516 * via broadcast or point-to-point networks, and from
517 * those listed in /etc/gateways.
520 msglim(&unk_router
, FROM_NADDR
,
521 " discard response from %s"
522 " via unexpected interface",
523 naddr_ntoa(FROM_NADDR
));
526 if (IS_RIP_IN_OFF(aifp
->int_state
)) {
527 trace_pkt(" discard RIPv%d response"
528 " via disabled interface %s",
529 rip
->rip_vers
, aifp
->int_name
);
534 msglim(&bad_len
, FROM_NADDR
, "empty response from %s",
535 naddr_ntoa(FROM_NADDR
));
539 if (((aifp
->int_state
& IS_NO_RIPV1_IN
)
540 && rip
->rip_vers
== RIPv1
)
541 || ((aifp
->int_state
& IS_NO_RIPV2_IN
)
542 && rip
->rip_vers
!= RIPv1
)) {
543 trace_pkt(" discard RIPv%d response",
548 /* Ignore routes via dead interface.
550 if (aifp
->int_state
& IS_BROKE
) {
551 trace_pkt("discard response via broken interface %s",
556 /* If the interface cares, ignore bad routers.
557 * Trace but do not log this problem, because where it
558 * happens, it happens frequently.
560 if (aifp
->int_state
& IS_DISTRUST
) {
562 while (tg
->tgate_addr
!= FROM_NADDR
) {
565 trace_pkt(" discard RIP response"
566 " from untrusted router %s",
567 naddr_ntoa(FROM_NADDR
));
573 /* Authenticate the packet if we have a secret.
574 * If we do not have any secrets, ignore the error in
575 * RFC 1723 and accept it regardless.
577 if (aifp
->int_auth
[0].type
!= RIP_AUTH_NONE
578 && rip
->rip_vers
!= RIPv1
579 && !ck_passwd(aifp
,rip
,lim
,FROM_NADDR
,&use_auth
))
583 if (n
->n_family
== RIP_AF_AUTH
)
586 n
->n_metric
= ntohl(n
->n_metric
);
588 if (n
->n_family
!= RIP_AF_INET
589 && (n
->n_family
!= RIP_AF_UNSPEC
590 || dst
!= RIP_DEFAULT
)) {
591 msglim(&bad_router
, FROM_NADDR
,
592 "route from %s to unsupported"
593 " address family=%d destination=%s",
594 naddr_ntoa(FROM_NADDR
),
599 if (!check_dst(dst
)) {
600 msglim(&bad_router
, FROM_NADDR
,
601 "bad destination %s from %s",
603 naddr_ntoa(FROM_NADDR
));
607 || n
->n_metric
> HOPCNT_INFINITY
) {
608 msglim(&bad_router
, FROM_NADDR
,
609 "bad metric %d from %s"
610 " for destination %s",
612 naddr_ntoa(FROM_NADDR
),
617 /* Notice the next-hop.
620 if (n
->n_nhop
!= 0) {
621 if (rip
->rip_vers
== RIPv1
) {
624 /* Use it only if it is valid. */
625 if (on_net(n
->n_nhop
,
626 aifp
->int_net
, aifp
->int_mask
)
627 && check_dst(n
->n_nhop
)) {
630 msglim(&bad_nhop
, FROM_NADDR
,
632 " has bad next hop %s",
633 naddr_ntoa(FROM_NADDR
),
635 naddr_ntoa(n
->n_nhop
));
641 if (rip
->rip_vers
== RIPv1
642 || 0 == (mask
= ntohl(n
->n_mask
))) {
643 mask
= ripv1_mask_host(dst
,aifp
);
644 } else if ((ntohl(dst
) & ~mask
) != 0) {
645 msglim(&bad_mask
, FROM_NADDR
,
646 "router %s sent bad netmask"
648 naddr_ntoa(FROM_NADDR
),
653 if (rip
->rip_vers
== RIPv1
)
656 /* Adjust metric according to incoming interface..
658 n
->n_metric
+= aifp
->int_metric
;
659 if (n
->n_metric
> HOPCNT_INFINITY
)
660 n
->n_metric
= HOPCNT_INFINITY
;
662 /* Should we trust this route from this router? */
663 if (tg
&& (tn
= tg
->tgate_nets
)->mask
!= 0) {
664 for (i
= 0; i
< MAX_TGATE_NETS
; i
++, tn
++) {
665 if (on_net(dst
, tn
->net
, tn
->mask
)
669 if (i
>= MAX_TGATE_NETS
|| tn
->mask
== 0) {
670 trace_pkt(" ignored unauthorized %s",
671 addrname(dst
,mask
,0));
676 /* Recognize and ignore a default route we faked
677 * which is being sent back to us by a machine with
678 * broken split-horizon.
679 * Be a little more paranoid than that, and reject
680 * default routes with the same metric we advertised.
682 if (aifp
->int_d_metric
!= 0
683 && dst
== RIP_DEFAULT
684 && (int)n
->n_metric
>= aifp
->int_d_metric
)
687 /* We can receive aggregated RIPv2 routes that must
688 * be broken down before they are transmitted by
689 * RIPv1 via an interface on a subnet.
690 * We might also receive the same routes aggregated
691 * via other RIPv2 interfaces.
692 * This could cause duplicate routes to be sent on
693 * the RIPv1 interfaces. "Longest matching variable
694 * length netmasks" lets RIPv2 listeners understand,
695 * but breaking down the aggregated routes for RIPv1
696 * listeners can produce duplicate routes.
698 * Breaking down aggregated routes here bloats
699 * the daemon table, but does not hurt the kernel
700 * table, since routes are always aggregated for
703 * Notice that this does not break down network
704 * routes corresponding to subnets. This is part
705 * of the defense against RS_NET_SYN.
708 && (((rt
= rtget(dst
,mask
)) == 0
709 || !(rt
->rt_state
& RS_NET_SYN
)))
710 && (v1_mask
= ripv1_mask_net(dst
,0)) > mask
) {
711 ddst_h
= v1_mask
& -v1_mask
;
712 i
= (v1_mask
& ~mask
)/ddst_h
;
714 /* Punt if we would have to generate
715 * an unreasonable number of routes.
718 trace_misc("accept %s-->%s as 1"
719 " instead of %d routes",
720 addrname(dst
,mask
,0),
721 naddr_ntoa(FROM_NADDR
),
732 new.rts_router
= FROM_NADDR
;
733 new.rts_metric
= n
->n_metric
;
734 new.rts_tag
= n
->n_tag
;
735 new.rts_time
= now
.tv_sec
;
740 input_route(dst
, mask
, &new, n
);
743 dst
= htonl(ntohl(dst
) + ddst_h
);
752 /* Process a single input route.
755 input_route(naddr dst
, /* network order */
757 struct rt_spare
*new,
762 struct rt_spare
*rts
, *rts0
;
763 struct interface
*ifp1
;
766 /* See if the other guy is telling us to send our packets to him.
767 * Sometimes network routes arrive over a point-to-point link for
768 * the network containing the address(es) of the link.
770 * If our interface is broken, switch to using the other guy.
772 ifp1
= ifwithaddr(dst
, 1, 1);
774 && (!(ifp1
->int_state
& IS_BROKE
)
775 || (ifp1
->int_state
& IS_PASSIVE
)))
778 /* Look for the route in our table.
780 rt
= rtget(dst
, mask
);
782 /* Consider adding the route if we do not already have it.
785 /* Ignore unknown routes being poisoned.
787 if (new->rts_metric
== HOPCNT_INFINITY
)
790 /* Ignore the route if it points to us */
792 && 0 != ifwithaddr(n
->n_nhop
, 1, 0))
795 /* If something has not gone crazy and tried to fill
796 * our memory, accept the new route.
798 if (total_routes
< MAX_ROUTES
)
799 rtadd(dst
, mask
, 0, new);
803 /* We already know about the route. Consider this update.
805 * If (rt->rt_state & RS_NET_SYN), then this route
806 * is the same as a network route we have inferred
807 * for subnets we know, in order to tell RIPv1 routers
810 * It is impossible to tell if the route is coming
811 * from a distant RIPv2 router with the standard
812 * netmask because that router knows about the entire
813 * network, or if it is a round-about echo of a
814 * synthetic, RIPv1 network route of our own.
815 * The worst is that both kinds of routes might be
816 * received, and the bad one might have the smaller
817 * metric. Partly solve this problem by never
818 * aggregating into such a route. Also keep it
819 * around as long as the interface exists.
822 rts0
= rt
->rt_spares
;
823 for (rts
= rts0
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
824 if (rts
->rts_router
== new->rts_router
)
826 /* Note the worst slot to reuse,
827 * other than the current slot.
829 if (rts0
== rt
->rt_spares
830 || BETTER_LINK(rt
, rts0
, rts
))
834 /* Found a route from the router already in the table.
837 /* If the new route is a route broken down from an
838 * aggregated route, and if the previous route is either
839 * not a broken down route or was broken down from a finer
840 * netmask, and if the previous route is current,
841 * then forget this one.
843 if (new->rts_de_ag
> rts
->rts_de_ag
844 && now_stale
<= rts
->rts_time
)
847 /* Keep poisoned routes around only long enough to pass
848 * the poison on. Use a new timestamp for good routes.
850 if (rts
->rts_metric
== HOPCNT_INFINITY
851 && new->rts_metric
== HOPCNT_INFINITY
)
852 new->rts_time
= rts
->rts_time
;
854 /* If this is an update for the router we currently prefer,
857 if (i
== NUM_SPARES
) {
858 rtchange(rt
, rt
->rt_state
, new, 0);
859 /* If the route got worse, check for something better.
861 if (new->rts_metric
> rts
->rts_metric
)
866 /* This is an update for a spare route.
867 * Finished if the route is unchanged.
869 if (rts
->rts_gate
== new->rts_gate
870 && rts
->rts_metric
== new->rts_metric
871 && rts
->rts_tag
== new->rts_tag
) {
872 trace_upslot(rt
, rts
, new);
876 /* Forget it if it has gone bad.
878 if (new->rts_metric
== HOPCNT_INFINITY
) {
884 /* The update is for a route we know about,
885 * but not from a familiar router.
887 * Ignore the route if it points to us.
890 && 0 != ifwithaddr(n
->n_nhop
, 1, 0))
893 /* the loop above set rts0=worst spare */
896 /* Save the route as a spare only if it has
897 * a better metric than our worst spare.
898 * This also ignores poisoned routes (those
899 * received with metric HOPCNT_INFINITY).
901 if (new->rts_metric
>= rts
->rts_metric
)
905 trace_upslot(rt
, rts
, new);
908 /* try to switch to a better route */
913 static int /* 0 if bad */
914 ck_passwd(struct interface
*aifp
,
918 struct msg_limit
*use_authp
)
920 # define NA (rip->rip_auths)
924 u_char hash
[RIP_AUTH_PW_LEN
];
928 if ((void *)NA
>= lim
|| NA
->a_family
!= RIP_AF_AUTH
) {
929 msglim(use_authp
, from
, "missing password from %s",
934 /* accept any current (+/- 24 hours) password
936 for (ap
= aifp
->int_auth
, i
= 0; i
< MAX_AUTH_KEYS
; i
++, ap
++) {
937 if (ap
->type
!= NA
->a_type
938 || (u_long
)ap
->start
> (u_long
)clk
.tv_sec
+DAY
939 || (u_long
)ap
->end
+DAY
< (u_long
)clk
.tv_sec
)
942 if (NA
->a_type
== RIP_AUTH_PW
) {
943 if (!memcmp(NA
->au
.au_pw
, ap
->key
, RIP_AUTH_PW_LEN
))
947 /* accept MD5 secret with the right key ID
949 if (NA
->au
.a_md5
.md5_keyid
!= ap
->keyid
)
952 len
= ntohs(NA
->au
.a_md5
.md5_pkt_len
);
953 if ((len
-sizeof(*rip
)) % sizeof(*NA
) != 0
954 || len
!= (char *)lim
-(char*)rip
-(int)sizeof(*NA
)) {
955 msglim(use_authp
, from
,
956 "wrong MD5 RIPv2 packet length of %d"
957 " instead of %d from %s",
958 len
, (int)((char *)lim
-(char *)rip
963 na2
= (struct netauth
*)((char *)rip
+len
);
965 /* Given a good hash value, these are not security
966 * problems so be generous and accept the routes,
970 if (NA
->au
.a_md5
.md5_auth_len
972 msglim(use_authp
, from
,
973 "unknown MD5 RIPv2 auth len %#x"
974 " instead of %#x from %s",
975 NA
->au
.a_md5
.md5_auth_len
,
978 if (na2
->a_family
!= RIP_AF_AUTH
)
979 msglim(use_authp
, from
,
980 "unknown MD5 RIPv2 family %#x"
981 " instead of %#x from %s",
982 na2
->a_family
, RIP_AF_AUTH
,
984 if (na2
->a_type
!= ntohs(1))
985 msglim(use_authp
, from
,
986 "MD5 RIPv2 hash has %#x"
987 " instead of %#x from %s",
988 na2
->a_type
, ntohs(1),
993 MD5Update(&md5_ctx
, (u_char
*)rip
, len
);
994 MD5Update(&md5_ctx
, ap
->key
, RIP_AUTH_MD5_LEN
);
995 MD5Final(hash
, &md5_ctx
);
996 if (!memcmp(hash
, na2
->au
.au_pw
, sizeof(hash
)))
1001 msglim(use_authp
, from
, "bad password from %s",