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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)input.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/routed/input.c,v 1.7.2.1 2001/08/01 09:01:45 obrien Exp $
35 static void input(struct sockaddr_in
*, struct interface
*, struct interface
*,
37 static void input_route(naddr
, naddr
, struct rt_spare
*, struct netinfo
*);
38 static int ck_passwd(struct interface
*, struct rip
*, void *,
39 naddr
, struct msg_limit
*);
46 struct interface
*sifp
)
48 struct sockaddr_in from
;
49 struct interface
*aifp
;
53 static struct msg_limit bad_name
;
55 char ifname
[IFNAMSIZ
];
66 fromlen
= sizeof(from
);
67 cc
= recvfrom(sock
, &inbuf
, sizeof(inbuf
), 0,
68 (struct sockaddr
*)&from
, &fromlen
);
70 if (cc
< 0 && errno
!= EWOULDBLOCK
)
71 LOGERR("recvfrom(rip)");
74 if (fromlen
!= sizeof(struct sockaddr_in
))
75 logbad(1,"impossible recvfrom(rip) fromlen=%d",
78 /* aifp is the "authenticated" interface via which the packet
79 * arrived. In fact, it is only the interface on which
80 * the packet should have arrived based on is source
82 * sifp is interface associated with the socket through which
83 * the packet was received.
86 if ((cc
-= sizeof(inbuf
.ifname
)) < 0)
87 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
88 cc
+sizeof(inbuf
.ifname
));
90 /* check the remote interfaces first */
91 for (aifp
= remote_if
; aifp
; aifp
= aifp
->int_rlink
) {
92 if (aifp
->int_addr
== from
.sin_addr
.s_addr
)
96 aifp
= ifwithname(inbuf
.ifname
, 0);
98 msglim(&bad_name
, from
.sin_addr
.s_addr
,
99 "impossible interface name %.*s",
100 IFNAMSIZ
, inbuf
.ifname
);
101 } else if (((aifp
->int_if_flags
& IFF_POINTOPOINT
)
102 && aifp
->int_dstaddr
!=from
.sin_addr
.s_addr
)
103 || (!(aifp
->int_if_flags
& IFF_POINTOPOINT
)
104 && !on_net(from
.sin_addr
.s_addr
,
107 /* If it came via the wrong interface, do not
114 aifp
= iflookup(from
.sin_addr
.s_addr
);
119 input(&from
, sifp
, aifp
, &inbuf
.pbuf
.rip
, cc
);
124 /* Process a RIP packet
127 input(struct sockaddr_in
*from
, /* received from this IP address */
128 struct interface
*sifp
, /* interface of incoming socket */
129 struct interface
*aifp
, /* "authenticated" interface */
133 # define FROM_NADDR from->sin_addr.s_addr
134 static struct msg_limit use_auth
, bad_len
, bad_mask
;
135 static struct msg_limit unk_router
, bad_router
, bad_nhop
;
139 struct netinfo
*n
, *lim
;
140 struct interface
*ifp1
;
141 naddr gate
, mask
, v1_mask
, dst
, ddst_h
= 0;
143 struct tgate
*tg
= NULL
;
144 struct tgate_net
*tn
;
147 /* Notice when we hear from a remote gateway
150 && (aifp
->int_state
& IS_REMOTE
))
151 aifp
->int_act_time
= now
.tv_sec
;
153 trace_rip("Recv", "from", from
, sifp
, rip
, cc
);
155 if (rip
->rip_vers
== 0) {
156 msglim(&bad_router
, FROM_NADDR
,
157 "RIP version 0, cmd %d, packet received from %s",
158 rip
->rip_cmd
, naddr_ntoa(FROM_NADDR
));
160 } else if (rip
->rip_vers
> RIPv2
) {
161 rip
->rip_vers
= RIPv2
;
163 if (cc
> (int)OVER_MAXPACKETSIZE
) {
164 msglim(&bad_router
, FROM_NADDR
,
165 "packet at least %d bytes too long received from %s",
166 cc
-MAXPACKETSIZE
, naddr_ntoa(FROM_NADDR
));
171 lim
= (struct netinfo
*)((char*)rip
+ cc
);
173 /* Notice authentication.
174 * As required by section 4.2 in RFC 1723, discard authenticated
175 * RIPv2 messages, but only if configured for that silliness.
177 * RIPv2 authentication is lame. Why authenticate queries?
178 * Why should a RIPv2 implementation with authentication disabled
179 * not be able to listen to RIPv2 packets with authentication, while
180 * RIPv1 systems will listen? Crazy!
183 && rip
->rip_vers
== RIPv2
184 && n
< lim
&& n
->n_family
== RIP_AF_AUTH
) {
185 msglim(&use_auth
, FROM_NADDR
,
186 "RIPv2 message with authentication from %s discarded",
187 naddr_ntoa(FROM_NADDR
));
191 switch (rip
->rip_cmd
) {
193 /* For mere requests, be a little sloppy about the source
198 /* Are we talking to ourself or a remote gateway?
200 ifp1
= ifwithaddr(FROM_NADDR
, 0, 1);
202 if (ifp1
->int_state
& IS_REMOTE
) {
205 if (check_remote(aifp
)) {
206 aifp
->int_act_time
= now
.tv_sec
;
207 if_ok(aifp
, "remote ");
209 } else if (from
->sin_port
== htons(RIP_PORT
)) {
210 trace_pkt(" discard our own RIP request");
215 /* did the request come from a router?
217 if (from
->sin_port
== htons(RIP_PORT
)) {
218 /* yes, ignore the request if RIP is off so that
219 * the router does not depend on us.
223 && IS_RIP_OUT_OFF(aifp
->int_state
))) {
224 trace_pkt(" discard request while RIP off");
229 /* According to RFC 1723, we should ignore unauthenticated
230 * queries. That is too silly to bother with. Sheesh!
231 * Are forwarding tables supposed to be secret, when
232 * a bad guy can infer them with test traffic? When RIP
233 * is still the most common router-discovery protocol
234 * and so hosts need to send queries that will be answered?
235 * What about `rtquery`?
236 * Maybe on firewalls you'd care, but not enough to
237 * give up the diagnostic facilities of remote probing.
241 msglim(&bad_len
, FROM_NADDR
, "empty request from %s",
242 naddr_ntoa(FROM_NADDR
));
245 if (cc
%sizeof(*n
) != sizeof(struct rip
)%sizeof(*n
)) {
246 msglim(&bad_len
, FROM_NADDR
,
247 "request of bad length (%d) from %s",
248 cc
, naddr_ntoa(FROM_NADDR
));
251 if (rip
->rip_vers
== RIPv2
252 && (aifp
== NULL
|| (aifp
->int_state
& IS_NO_RIPV1_OUT
))) {
253 v12buf
.buf
->rip_vers
= RIPv2
;
254 /* If we have a secret but it is a cleartext secret,
255 * do not disclose our secret unless the other guy
258 ap
= find_auth(aifp
);
259 if (ap
!= NULL
&& ap
->type
== RIP_AUTH_PW
260 && n
->n_family
== RIP_AF_AUTH
261 && !ck_passwd(aifp
,rip
,lim
,FROM_NADDR
,&use_auth
))
264 v12buf
.buf
->rip_vers
= RIPv1
;
267 clr_ws_buf(&v12buf
, ap
);
270 n
->n_metric
= ntohl(n
->n_metric
);
272 /* A single entry with family RIP_AF_UNSPEC and
273 * metric HOPCNT_INFINITY means "all routes".
274 * We respond to routers only if we are acting
275 * as a supplier, or to anyone other than a router
278 if (n
->n_family
== RIP_AF_UNSPEC
279 && n
->n_metric
== HOPCNT_INFINITY
) {
280 /* Answer a query from a utility program
284 trace_pkt("ignore remote query");
287 if (from
->sin_port
!= htons(RIP_PORT
)) {
288 supply(from
, aifp
, OUT_QUERY
, 0,
289 rip
->rip_vers
, ap
!= NULL
);
293 /* A router trying to prime its tables.
294 * Filter the answer in the about same way
295 * broadcasts are filtered.
297 * Only answer a router if we are a supplier
298 * to keep an unwary host that is just starting
299 * from picking us as a router.
302 trace_pkt("ignore distant router");
306 || IS_RIP_OFF(aifp
->int_state
)) {
307 trace_pkt("ignore; not supplying");
311 /* Do not answer a RIPv1 router if
312 * we are sending RIPv2. But do offer
313 * poor man's router discovery.
315 if ((aifp
->int_state
& IS_NO_RIPV1_OUT
)
316 && rip
->rip_vers
== RIPv1
) {
317 if (!(aifp
->int_state
& IS_PM_RDISC
)) {
318 trace_pkt("ignore; sending RIPv2");
322 v12buf
.n
->n_family
= RIP_AF_INET
;
323 v12buf
.n
->n_dst
= RIP_DEFAULT
;
324 i
= aifp
->int_d_metric
;
325 if (NULL
!= (rt
= rtget(RIP_DEFAULT
, 0)))
326 i
= MIN(i
, (rt
->rt_metric
327 +aifp
->int_metric
+1));
328 v12buf
.n
->n_metric
= htonl(i
);
333 /* Respond with RIPv1 instead of RIPv2 if
334 * that is what we are broadcasting on the
335 * interface to keep the remote router from
336 * getting the wrong initial idea of the
339 supply(from
, aifp
, OUT_UNICAST
, 0,
340 (aifp
->int_state
& IS_NO_RIPV1_OUT
)
346 /* Ignore authentication */
347 if (n
->n_family
== RIP_AF_AUTH
)
350 if (n
->n_family
!= RIP_AF_INET
) {
351 msglim(&bad_router
, FROM_NADDR
,
352 "request from %s for unsupported"
354 naddr_ntoa(FROM_NADDR
),
356 naddr_ntoa(n
->n_dst
));
360 /* We are being asked about a specific destination.
363 if (!check_dst(dst
)) {
364 msglim(&bad_router
, FROM_NADDR
,
365 "bad queried destination %s from %s",
367 naddr_ntoa(FROM_NADDR
));
371 /* decide what mask was intended */
372 if (rip
->rip_vers
== RIPv1
373 || 0 == (mask
= ntohl(n
->n_mask
))
374 || 0 != (ntohl(dst
) & ~mask
))
375 mask
= ripv1_mask_host(dst
, aifp
);
377 /* try to find the answer */
378 rt
= rtget(dst
, mask
);
379 if (!rt
&& dst
!= RIP_DEFAULT
)
380 rt
= rtfind(n
->n_dst
);
382 if (v12buf
.buf
->rip_vers
!= RIPv1
)
383 v12buf
.n
->n_mask
= mask
;
385 /* we do not have the answer */
386 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
388 /* we have the answer, so compute the
389 * right metric and next hop.
391 v12buf
.n
->n_family
= RIP_AF_INET
;
392 v12buf
.n
->n_dst
= dst
;
393 v12buf
.n
->n_metric
= (rt
->rt_metric
+1
397 if (v12buf
.n
->n_metric
> HOPCNT_INFINITY
)
398 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
399 if (v12buf
.buf
->rip_vers
!= RIPv1
) {
400 v12buf
.n
->n_tag
= rt
->rt_tag
;
401 v12buf
.n
->n_mask
= mask
;
403 && on_net(rt
->rt_gate
,
406 && rt
->rt_gate
!= aifp
->int_addr
)
407 v12buf
.n
->n_nhop
= rt
->rt_gate
;
410 v12buf
.n
->n_metric
= htonl(v12buf
.n
->n_metric
);
412 /* Stop paying attention if we fill the output buffer.
414 if (++v12buf
.n
>= v12buf
.lim
)
418 /* Send the answer about specific routes.
420 if (ap
!= NULL
&& ap
->type
== RIP_AUTH_MD5
)
421 end_md5_auth(&v12buf
, ap
);
423 if (from
->sin_port
!= htons(RIP_PORT
)) {
425 output(OUT_QUERY
, from
, aifp
,
427 ((char *)v12buf
.n
- (char*)v12buf
.buf
));
428 } else if (supplier
) {
429 output(OUT_UNICAST
, from
, aifp
,
431 ((char *)v12buf
.n
- (char*)v12buf
.buf
));
433 /* Only answer a router if we are a supplier
434 * to keep an unwary host that is just starting
435 * from picking us an a router.
442 case RIPCMD_TRACEOFF
:
443 /* Notice that trace messages are turned off for all possible
444 * abuse if _PATH_TRACE is undefined in pathnames.h.
445 * Notice also that because of the way the trace file is
446 * handled in trace.c, no abuse is plausible even if
447 * _PATH_TRACE_ is defined.
449 * First verify message came from a privileged port. */
450 if (ntohs(from
->sin_port
) > IPPORT_RESERVED
) {
451 msglog("trace command from untrusted port on %s",
452 naddr_ntoa(FROM_NADDR
));
456 msglog("trace command from unknown router %s",
457 naddr_ntoa(FROM_NADDR
));
460 if (rip
->rip_cmd
== RIPCMD_TRACEON
) {
461 rip
->rip_tracefile
[cc
-4] = '\0';
462 set_tracefile((char*)rip
->rip_tracefile
,
463 "trace command: %s\n", 0);
465 trace_off("tracing turned off by %s",
466 naddr_ntoa(FROM_NADDR
));
470 case RIPCMD_RESPONSE
:
471 if (cc
%sizeof(*n
) != sizeof(struct rip
)%sizeof(*n
)) {
472 msglim(&bad_len
, FROM_NADDR
,
473 "response of bad length (%d) from %s",
474 cc
, naddr_ntoa(FROM_NADDR
));
477 /* verify message came from a router */
478 if (from
->sin_port
!= ntohs(RIP_PORT
)) {
479 msglim(&bad_router
, FROM_NADDR
,
480 " discard RIP response from unknown port"
481 " %d on host %s", ntohs(from
->sin_port
),
482 naddr_ntoa(FROM_NADDR
));
487 trace_pkt(" discard response while RIP off");
491 /* Are we talking to ourself or a remote gateway?
493 ifp1
= ifwithaddr(FROM_NADDR
, 0, 1);
495 if (ifp1
->int_state
& IS_REMOTE
) {
498 if (check_remote(aifp
)) {
499 aifp
->int_act_time
= now
.tv_sec
;
500 if_ok(aifp
, "remote ");
503 trace_pkt(" discard our own RIP response");
508 /* Accept routing packets from routers directly connected
509 * via broadcast or point-to-point networks, and from
510 * those listed in /etc/gateways.
513 msglim(&unk_router
, FROM_NADDR
,
514 " discard response from %s"
515 " via unexpected interface",
516 naddr_ntoa(FROM_NADDR
));
519 if (IS_RIP_IN_OFF(aifp
->int_state
)) {
520 trace_pkt(" discard RIPv%d response"
521 " via disabled interface %s",
522 rip
->rip_vers
, aifp
->int_name
);
527 msglim(&bad_len
, FROM_NADDR
, "empty response from %s",
528 naddr_ntoa(FROM_NADDR
));
532 if (((aifp
->int_state
& IS_NO_RIPV1_IN
)
533 && rip
->rip_vers
== RIPv1
)
534 || ((aifp
->int_state
& IS_NO_RIPV2_IN
)
535 && rip
->rip_vers
!= RIPv1
)) {
536 trace_pkt(" discard RIPv%d response",
541 /* Ignore routes via dead interface.
543 if (aifp
->int_state
& IS_BROKE
) {
544 trace_pkt("discard response via broken interface %s",
549 /* If the interface cares, ignore bad routers.
550 * Trace but do not log this problem, because where it
551 * happens, it happens frequently.
553 if (aifp
->int_state
& IS_DISTRUST
) {
555 while (tg
->tgate_addr
!= FROM_NADDR
) {
558 trace_pkt(" discard RIP response"
559 " from untrusted router %s",
560 naddr_ntoa(FROM_NADDR
));
566 /* Authenticate the packet if we have a secret.
567 * If we do not have any secrets, ignore the error in
568 * RFC 1723 and accept it regardless.
570 if (aifp
->int_auth
[0].type
!= RIP_AUTH_NONE
571 && rip
->rip_vers
!= RIPv1
572 && !ck_passwd(aifp
,rip
,lim
,FROM_NADDR
,&use_auth
))
576 if (n
->n_family
== RIP_AF_AUTH
)
579 n
->n_metric
= ntohl(n
->n_metric
);
581 if (n
->n_family
!= RIP_AF_INET
582 && (n
->n_family
!= RIP_AF_UNSPEC
583 || dst
!= RIP_DEFAULT
)) {
584 msglim(&bad_router
, FROM_NADDR
,
585 "route from %s to unsupported"
586 " address family=%d destination=%s",
587 naddr_ntoa(FROM_NADDR
),
592 if (!check_dst(dst
)) {
593 msglim(&bad_router
, FROM_NADDR
,
594 "bad destination %s from %s",
596 naddr_ntoa(FROM_NADDR
));
600 || n
->n_metric
> HOPCNT_INFINITY
) {
601 msglim(&bad_router
, FROM_NADDR
,
602 "bad metric %d from %s"
603 " for destination %s",
605 naddr_ntoa(FROM_NADDR
),
610 /* Notice the next-hop.
613 if (n
->n_nhop
!= 0) {
614 if (rip
->rip_vers
== RIPv1
) {
617 /* Use it only if it is valid. */
618 if (on_net(n
->n_nhop
,
619 aifp
->int_net
, aifp
->int_mask
)
620 && check_dst(n
->n_nhop
)) {
623 msglim(&bad_nhop
, FROM_NADDR
,
625 " has bad next hop %s",
626 naddr_ntoa(FROM_NADDR
),
628 naddr_ntoa(n
->n_nhop
));
634 if (rip
->rip_vers
== RIPv1
635 || 0 == (mask
= ntohl(n
->n_mask
))) {
636 mask
= ripv1_mask_host(dst
,aifp
);
637 } else if ((ntohl(dst
) & ~mask
) != 0) {
638 msglim(&bad_mask
, FROM_NADDR
,
639 "router %s sent bad netmask"
641 naddr_ntoa(FROM_NADDR
),
646 if (rip
->rip_vers
== RIPv1
)
649 /* Adjust metric according to incoming interface..
651 n
->n_metric
+= aifp
->int_metric
;
652 if (n
->n_metric
> HOPCNT_INFINITY
)
653 n
->n_metric
= HOPCNT_INFINITY
;
655 /* Should we trust this route from this router? */
656 if (tg
&& (tn
= tg
->tgate_nets
)->mask
!= 0) {
657 for (i
= 0; i
< MAX_TGATE_NETS
; i
++, tn
++) {
658 if (on_net(dst
, tn
->net
, tn
->mask
)
662 if (i
>= MAX_TGATE_NETS
|| tn
->mask
== 0) {
663 trace_pkt(" ignored unauthorized %s",
664 addrname(dst
,mask
,0));
669 /* Recognize and ignore a default route we faked
670 * which is being sent back to us by a machine with
671 * broken split-horizon.
672 * Be a little more paranoid than that, and reject
673 * default routes with the same metric we advertised.
675 if (aifp
->int_d_metric
!= 0
676 && dst
== RIP_DEFAULT
677 && (int)n
->n_metric
>= aifp
->int_d_metric
)
680 /* We can receive aggregated RIPv2 routes that must
681 * be broken down before they are transmitted by
682 * RIPv1 via an interface on a subnet.
683 * We might also receive the same routes aggregated
684 * via other RIPv2 interfaces.
685 * This could cause duplicate routes to be sent on
686 * the RIPv1 interfaces. "Longest matching variable
687 * length netmasks" lets RIPv2 listeners understand,
688 * but breaking down the aggregated routes for RIPv1
689 * listeners can produce duplicate routes.
691 * Breaking down aggregated routes here bloats
692 * the daemon table, but does not hurt the kernel
693 * table, since routes are always aggregated for
696 * Notice that this does not break down network
697 * routes corresponding to subnets. This is part
698 * of the defense against RS_NET_SYN.
701 && (((rt
= rtget(dst
,mask
)) == NULL
702 || !(rt
->rt_state
& RS_NET_SYN
)))
703 && (v1_mask
= ripv1_mask_net(dst
,0)) > mask
) {
704 ddst_h
= v1_mask
& -v1_mask
;
705 i
= (v1_mask
& ~mask
)/ddst_h
;
707 /* Punt if we would have to generate
708 * an unreasonable number of routes.
711 trace_misc("accept %s-->%s as 1"
712 " instead of %d routes",
713 addrname(dst
,mask
,0),
714 naddr_ntoa(FROM_NADDR
),
725 new.rts_router
= FROM_NADDR
;
726 new.rts_metric
= n
->n_metric
;
727 new.rts_tag
= n
->n_tag
;
728 new.rts_time
= now
.tv_sec
;
733 input_route(dst
, mask
, &new, n
);
736 dst
= htonl(ntohl(dst
) + ddst_h
);
745 /* Process a single input route.
748 input_route(naddr dst
, /* network order */
750 struct rt_spare
*new,
755 struct rt_spare
*rts
, *rts0
;
756 struct interface
*ifp1
;
759 /* See if the other guy is telling us to send our packets to him.
760 * Sometimes network routes arrive over a point-to-point link for
761 * the network containing the address(es) of the link.
763 * If our interface is broken, switch to using the other guy.
765 ifp1
= ifwithaddr(dst
, 1, 1);
767 && (!(ifp1
->int_state
& IS_BROKE
)
768 || (ifp1
->int_state
& IS_PASSIVE
)))
771 /* Look for the route in our table.
773 rt
= rtget(dst
, mask
);
775 /* Consider adding the route if we do not already have it.
778 /* Ignore unknown routes being poisoned.
780 if (new->rts_metric
== HOPCNT_INFINITY
)
783 /* Ignore the route if it points to us */
785 && 0 != ifwithaddr(n
->n_nhop
, 1, 0))
788 /* If something has not gone crazy and tried to fill
789 * our memory, accept the new route.
791 if (total_routes
< MAX_ROUTES
)
792 rtadd(dst
, mask
, 0, new);
796 /* We already know about the route. Consider this update.
798 * If (rt->rt_state & RS_NET_SYN), then this route
799 * is the same as a network route we have inferred
800 * for subnets we know, in order to tell RIPv1 routers
803 * It is impossible to tell if the route is coming
804 * from a distant RIPv2 router with the standard
805 * netmask because that router knows about the entire
806 * network, or if it is a round-about echo of a
807 * synthetic, RIPv1 network route of our own.
808 * The worst is that both kinds of routes might be
809 * received, and the bad one might have the smaller
810 * metric. Partly solve this problem by never
811 * aggregating into such a route. Also keep it
812 * around as long as the interface exists.
815 rts0
= rt
->rt_spares
;
816 for (rts
= rts0
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
817 if (rts
->rts_router
== new->rts_router
)
819 /* Note the worst slot to reuse,
820 * other than the current slot.
822 if (rts0
== rt
->rt_spares
823 || BETTER_LINK(rt
, rts0
, rts
))
827 /* Found a route from the router already in the table.
830 /* If the new route is a route broken down from an
831 * aggregated route, and if the previous route is either
832 * not a broken down route or was broken down from a finer
833 * netmask, and if the previous route is current,
834 * then forget this one.
836 if (new->rts_de_ag
> rts
->rts_de_ag
837 && now_stale
<= rts
->rts_time
)
840 /* Keep poisoned routes around only long enough to pass
841 * the poison on. Use a new timestamp for good routes.
843 if (rts
->rts_metric
== HOPCNT_INFINITY
844 && new->rts_metric
== HOPCNT_INFINITY
)
845 new->rts_time
= rts
->rts_time
;
847 /* If this is an update for the router we currently prefer,
850 if (i
== NUM_SPARES
) {
851 rtchange(rt
, rt
->rt_state
, new, 0);
852 /* If the route got worse, check for something better.
854 if (new->rts_metric
> rts
->rts_metric
)
859 /* This is an update for a spare route.
860 * Finished if the route is unchanged.
862 if (rts
->rts_gate
== new->rts_gate
863 && rts
->rts_metric
== new->rts_metric
864 && rts
->rts_tag
== new->rts_tag
) {
865 trace_upslot(rt
, rts
, new);
869 /* Forget it if it has gone bad.
871 if (new->rts_metric
== HOPCNT_INFINITY
) {
877 /* The update is for a route we know about,
878 * but not from a familiar router.
880 * Ignore the route if it points to us.
883 && 0 != ifwithaddr(n
->n_nhop
, 1, 0))
886 /* the loop above set rts0=worst spare */
889 /* Save the route as a spare only if it has
890 * a better metric than our worst spare.
891 * This also ignores poisoned routes (those
892 * received with metric HOPCNT_INFINITY).
894 if (new->rts_metric
>= rts
->rts_metric
)
898 trace_upslot(rt
, rts
, new);
901 /* try to switch to a better route */
906 static int /* 0 if bad */
907 ck_passwd(struct interface
*aifp
,
911 struct msg_limit
*use_authp
)
913 # define NA (rip->rip_auths)
917 u_char hash
[RIP_AUTH_PW_LEN
];
921 if ((void *)NA
>= lim
|| NA
->a_family
!= RIP_AF_AUTH
) {
922 msglim(use_authp
, from
, "missing password from %s",
927 /* accept any current (+/- 24 hours) password
929 for (ap
= aifp
->int_auth
, i
= 0; i
< MAX_AUTH_KEYS
; i
++, ap
++) {
930 if (ap
->type
!= NA
->a_type
931 || (u_long
)ap
->start
> (u_long
)clk
.tv_sec
+DAY
932 || (u_long
)ap
->end
+DAY
< (u_long
)clk
.tv_sec
)
935 if (NA
->a_type
== RIP_AUTH_PW
) {
936 if (!memcmp(NA
->au
.au_pw
, ap
->key
, RIP_AUTH_PW_LEN
))
940 /* accept MD5 secret with the right key ID
942 if (NA
->au
.a_md5
.md5_keyid
!= ap
->keyid
)
945 len
= ntohs(NA
->au
.a_md5
.md5_pkt_len
);
946 if ((len
-sizeof(*rip
)) % sizeof(*NA
) != 0
947 || len
!= (char *)lim
-(char*)rip
-(int)sizeof(*NA
)) {
948 msglim(use_authp
, from
,
949 "wrong MD5 RIPv2 packet length of %d"
950 " instead of %d from %s",
951 len
, (int)((char *)lim
-(char *)rip
956 na2
= (struct netauth
*)((char *)rip
+len
);
958 /* Given a good hash value, these are not security
959 * problems so be generous and accept the routes,
963 if (NA
->au
.a_md5
.md5_auth_len
965 msglim(use_authp
, from
,
966 "unknown MD5 RIPv2 auth len %#x"
967 " instead of %#x from %s",
968 NA
->au
.a_md5
.md5_auth_len
,
971 if (na2
->a_family
!= RIP_AF_AUTH
)
972 msglim(use_authp
, from
,
973 "unknown MD5 RIPv2 family %#x"
974 " instead of %#x from %s",
975 na2
->a_family
, RIP_AF_AUTH
,
977 if (na2
->a_type
!= ntohs(1))
978 msglim(use_authp
, from
,
979 "MD5 RIPv2 hash has %#x"
980 " instead of %#x from %s",
981 na2
->a_type
, ntohs(1),
986 MD5Update(&md5_ctx
, (u_char
*)rip
, len
);
987 MD5Update(&md5_ctx
, ap
->key
, RIP_AUTH_MD5_LEN
);
988 MD5Final(hash
, &md5_ctx
);
989 if (!memcmp(hash
, na2
->au
.au_pw
, sizeof(hash
)))
994 msglim(use_authp
, from
, "bad password from %s",