2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static void input(struct sockaddr_in *, struct interface *, struct interface *,
40 static void input_route(naddr, naddr, struct rt_spare *, struct netinfo *);
41 static int ck_passwd(struct interface *, struct rip *, void *,
42 naddr, struct msg_limit *);
49 struct interface *sifp)
51 struct sockaddr_in from;
52 struct interface *aifp;
56 static struct msg_limit bad_name;
58 char ifname[IFNAMSIZ];
69 fromlen = sizeof(from);
70 cc = recvfrom(sock, &inbuf, sizeof(inbuf), 0,
71 (struct sockaddr*)&from, &fromlen);
73 if (cc < 0 && errno != EWOULDBLOCK)
74 LOGERR("recvfrom(rip)");
77 if (fromlen != sizeof(struct sockaddr_in))
78 logbad(1,"impossible recvfrom(rip) fromlen=%d",
81 /* aifp is the "authenticated" interface via which the packet
82 * arrived. In fact, it is only the interface on which
83 * the packet should have arrived based on is source
85 * sifp is interface associated with the socket through which
86 * the packet was received.
89 if ((cc -= sizeof(inbuf.ifname)) < 0)
90 logbad(0,"missing USE_PASSIFNAME; only %d bytes",
91 cc+sizeof(inbuf.ifname));
93 /* check the remote interfaces first */
94 LIST_FOREACH(aifp, &remote_if, remote_list) {
95 if (aifp->int_addr == from.sin_addr.s_addr)
99 aifp = ifwithname(inbuf.ifname, 0);
101 msglim(&bad_name, from.sin_addr.s_addr,
102 "impossible interface name %.*s",
103 IFNAMSIZ, inbuf.ifname);
104 } else if (((aifp->int_if_flags & IFF_POINTOPOINT)
105 && aifp->int_dstaddr!=from.sin_addr.s_addr)
106 || (!(aifp->int_if_flags & IFF_POINTOPOINT)
107 && !on_net(from.sin_addr.s_addr,
110 /* If it came via the wrong interface, do not
117 aifp = iflookup(from.sin_addr.s_addr);
122 input(&from, sifp, aifp, &inbuf.pbuf.rip, cc);
127 /* Process a RIP packet
130 input(struct sockaddr_in *from, /* received from this IP address */
131 struct interface *sifp, /* interface of incoming socket */
132 struct interface *aifp, /* "authenticated" interface */
136 # define FROM_NADDR from->sin_addr.s_addr
137 static struct msg_limit use_auth, bad_len, bad_mask;
138 static struct msg_limit unk_router, bad_router, bad_nhop;
142 struct netinfo *n, *lim;
143 struct interface *ifp1;
144 naddr gate, mask, v1_mask, dst, ddst_h = 0;
146 struct tgate *tg = NULL;
147 struct tgate_net *tn;
150 /* Notice when we hear from a remote gateway
153 && (aifp->int_state & IS_REMOTE))
154 aifp->int_act_time = now.tv_sec;
156 trace_rip("Recv", "from", from, sifp, rip, cc);
159 trace_pkt(" discard a request from an indirect router"
160 " (possibly an attack)");
164 if (rip->rip_vers == 0) {
165 msglim(&bad_router, FROM_NADDR,
166 "RIP version 0, cmd %d, packet received from %s",
167 rip->rip_cmd, naddr_ntoa(FROM_NADDR));
169 } else if (rip->rip_vers > RIPv2) {
170 rip->rip_vers = RIPv2;
172 if (cc > (int)OVER_MAXPACKETSIZE) {
173 msglim(&bad_router, FROM_NADDR,
174 "packet at least %d bytes too long received from %s",
175 cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
180 lim = (struct netinfo *)((char*)rip + cc);
182 /* Notice authentication.
183 * As required by section 4.2 in RFC 1723, discard authenticated
184 * RIPv2 messages, but only if configured for that silliness.
186 * RIPv2 authentication is lame. Why authenticate queries?
187 * Why should a RIPv2 implementation with authentication disabled
188 * not be able to listen to RIPv2 packets with authentication, while
189 * RIPv1 systems will listen? Crazy!
192 && rip->rip_vers == RIPv2
193 && n < lim && n->n_family == RIP_AF_AUTH) {
194 msglim(&use_auth, FROM_NADDR,
195 "RIPv2 message with authentication from %s discarded",
196 naddr_ntoa(FROM_NADDR));
200 switch (rip->rip_cmd) {
202 /* For mere requests, be a little sloppy about the source
207 /* Are we talking to ourself or a remote gateway?
209 ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
211 if (ifp1->int_state & IS_REMOTE) {
214 if (check_remote(aifp)) {
215 aifp->int_act_time = now.tv_sec;
216 (void)if_ok(aifp, "remote ");
218 } else if (from->sin_port == htons(RIP_PORT)) {
219 trace_pkt(" discard our own RIP request");
224 /* did the request come from a router?
226 if (from->sin_port == htons(RIP_PORT)) {
227 /* yes, ignore the request if RIP is off so that
228 * the router does not depend on us.
232 && IS_RIP_OUT_OFF(aifp->int_state))) {
233 trace_pkt(" discard request while RIP off");
238 /* According to RFC 1723, we should ignore unauthenticated
239 * queries. That is too silly to bother with. Sheesh!
240 * Are forwarding tables supposed to be secret, when
241 * a bad guy can infer them with test traffic? When RIP
242 * is still the most common router-discovery protocol
243 * and so hosts need to send queries that will be answered?
244 * What about `rtquery`?
245 * Maybe on firewalls you'd care, but not enough to
246 * give up the diagnostic facilities of remote probing.
250 msglim(&bad_len, FROM_NADDR, "empty request from %s",
251 naddr_ntoa(FROM_NADDR));
254 if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
255 msglim(&bad_len, FROM_NADDR,
256 "request of bad length (%d) from %s",
257 cc, naddr_ntoa(FROM_NADDR));
260 if (rip->rip_vers == RIPv2
261 && (aifp == NULL || (aifp->int_state & IS_NO_RIPV1_OUT))) {
262 v12buf.buf->rip_vers = RIPv2;
263 /* If we have a secret but it is a cleartext secret,
264 * do not disclose our secret unless the other guy
267 ap = find_auth(aifp);
268 if (ap != NULL && ap->type == RIP_AUTH_PW
269 && n->n_family == RIP_AF_AUTH
270 && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
273 v12buf.buf->rip_vers = RIPv1;
276 clr_ws_buf(&v12buf, ap);
279 n->n_metric = ntohl(n->n_metric);
281 /* A single entry with family RIP_AF_UNSPEC and
282 * metric HOPCNT_INFINITY means "all routes".
283 * We respond to routers only if we are acting
284 * as a supplier, or to anyone other than a router
287 if (n->n_family == RIP_AF_UNSPEC
288 && n->n_metric == HOPCNT_INFINITY) {
289 /* Answer a query from a utility program
293 trace_pkt("ignore remote query");
296 if (from->sin_port != htons(RIP_PORT)) {
298 * insecure: query from non-router node
299 * > 1: allow from distant node
300 * > 0: allow from neighbor node
303 if ((aifp != NULL && insecure > 0) ||
304 (aifp == NULL && insecure > 1))
305 supply(from, aifp, OUT_QUERY, 0,
309 trace_pkt("Warning: "
310 "possible attack detected");
314 /* A router trying to prime its tables.
315 * Filter the answer in the about same way
316 * broadcasts are filtered.
318 * Only answer a router if we are a supplier
319 * to keep an unwary host that is just starting
320 * from picking us as a router.
323 trace_pkt("ignore distant router");
327 || IS_RIP_OFF(aifp->int_state)) {
328 trace_pkt("ignore; not supplying");
332 /* Do not answer a RIPv1 router if
333 * we are sending RIPv2. But do offer
334 * poor man's router discovery.
336 if ((aifp->int_state & IS_NO_RIPV1_OUT)
337 && rip->rip_vers == RIPv1) {
338 if (!(aifp->int_state & IS_PM_RDISC)) {
339 trace_pkt("ignore; sending RIPv2");
343 v12buf.n->n_family = RIP_AF_INET;
344 v12buf.n->n_dst = RIP_DEFAULT;
345 i = aifp->int_d_metric;
346 if (NULL != (rt = rtget(RIP_DEFAULT, 0))) {
349 +aifp->int_adj_outmetric
354 v12buf.n->n_metric = htonl(i);
359 /* Respond with RIPv1 instead of RIPv2 if
360 * that is what we are broadcasting on the
361 * interface to keep the remote router from
362 * getting the wrong initial idea of the
365 supply(from, aifp, OUT_UNICAST, 0,
366 (aifp->int_state & IS_NO_RIPV1_OUT)
372 /* Ignore authentication */
373 if (n->n_family == RIP_AF_AUTH)
376 if (n->n_family != RIP_AF_INET) {
377 msglim(&bad_router, FROM_NADDR,
378 "request from %s for unsupported"
380 naddr_ntoa(FROM_NADDR),
382 naddr_ntoa(n->n_dst));
386 /* We are being asked about a specific destination.
389 if (!check_dst(dst)) {
390 msglim(&bad_router, FROM_NADDR,
391 "bad queried destination %s from %s",
393 naddr_ntoa(FROM_NADDR));
397 /* decide what mask was intended */
398 if (rip->rip_vers == RIPv1
399 || 0 == (mask = ntohl(n->n_mask))
400 || 0 != (ntohl(dst) & ~mask))
401 mask = ripv1_mask_host(dst, aifp);
403 /* try to find the answer */
404 rt = rtget(dst, mask);
405 if (!rt && dst != RIP_DEFAULT)
406 rt = rtfind(n->n_dst);
408 if (v12buf.buf->rip_vers != RIPv1)
409 v12buf.n->n_mask = mask;
411 /* we do not have the answer */
412 v12buf.n->n_metric = HOPCNT_INFINITY;
414 /* we have the answer, so compute the
415 * right metric and next hop.
417 v12buf.n->n_family = RIP_AF_INET;
418 v12buf.n->n_dst = dst;
423 j += (aifp->int_metric
424 + aifp->int_adj_outmetric);
425 if (j < HOPCNT_INFINITY)
426 v12buf.n->n_metric = j;
428 v12buf.n->n_metric = HOPCNT_INFINITY;
429 if (v12buf.buf->rip_vers != RIPv1) {
430 v12buf.n->n_tag = rt->rt_tag;
431 v12buf.n->n_mask = mask;
433 && on_net(rt->rt_gate,
436 && rt->rt_gate != aifp->int_addr)
437 v12buf.n->n_nhop = rt->rt_gate;
440 v12buf.n->n_metric = htonl(v12buf.n->n_metric);
442 /* Stop paying attention if we fill the output buffer.
444 if (++v12buf.n >= v12buf.lim)
448 /* Send the answer about specific routes.
450 if (ap != NULL && ap->type == RIP_AUTH_MD5)
451 end_md5_auth(&v12buf, ap);
453 if (from->sin_port != htons(RIP_PORT)) {
455 (void)output(OUT_QUERY, from, aifp,
457 ((char *)v12buf.n - (char*)v12buf.buf));
458 } else if (supplier) {
459 (void)output(OUT_UNICAST, from, aifp,
461 ((char *)v12buf.n - (char*)v12buf.buf));
463 /* Only answer a router if we are a supplier
464 * to keep an unwary host that is just starting
465 * from picking us an a router.
472 case RIPCMD_TRACEOFF:
473 /* Notice that trace messages are turned off for all possible
474 * abuse if _PATH_TRACE is undefined in pathnames.h.
475 * Notice also that because of the way the trace file is
476 * handled in trace.c, no abuse is plausible even if
477 * _PATH_TRACE_ is defined.
479 * First verify message came from a privileged port. */
480 if (ntohs(from->sin_port) > IPPORT_RESERVED) {
481 msglog("trace command from untrusted port on %s",
482 naddr_ntoa(FROM_NADDR));
486 msglog("trace command from unknown router %s",
487 naddr_ntoa(FROM_NADDR));
490 if (rip->rip_cmd == RIPCMD_TRACEON) {
491 rip->rip_tracefile[cc-4] = '\0';
492 set_tracefile((char*)rip->rip_tracefile,
493 "trace command: %s\n", 0);
495 trace_off("tracing turned off by %s",
496 naddr_ntoa(FROM_NADDR));
500 case RIPCMD_RESPONSE:
501 if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
502 msglim(&bad_len, FROM_NADDR,
503 "response of bad length (%d) from %s",
504 cc, naddr_ntoa(FROM_NADDR));
507 /* verify message came from a router */
508 if (from->sin_port != ntohs(RIP_PORT)) {
509 msglim(&bad_router, FROM_NADDR,
510 " discard RIP response from unknown port"
512 ntohs(from->sin_port), naddr_ntoa(FROM_NADDR));
517 trace_pkt(" discard response while RIP off");
521 /* Are we talking to ourself or a remote gateway?
523 ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
525 if (ifp1->int_state & IS_REMOTE) {
528 if (check_remote(aifp)) {
529 aifp->int_act_time = now.tv_sec;
530 (void)if_ok(aifp, "remote ");
533 trace_pkt(" discard our own RIP response");
538 /* Accept routing packets from routers directly connected
539 * via broadcast or point-to-point networks, and from
540 * those listed in /etc/gateways.
543 msglim(&unk_router, FROM_NADDR,
544 " discard response from %s"
545 " via unexpected interface",
546 naddr_ntoa(FROM_NADDR));
549 if (IS_RIP_IN_OFF(aifp->int_state)) {
550 trace_pkt(" discard RIPv%d response"
551 " via disabled interface %s",
552 rip->rip_vers, aifp->int_name);
557 msglim(&bad_len, FROM_NADDR, "empty response from %s",
558 naddr_ntoa(FROM_NADDR));
562 if (((aifp->int_state & IS_NO_RIPV1_IN)
563 && rip->rip_vers == RIPv1)
564 || ((aifp->int_state & IS_NO_RIPV2_IN)
565 && rip->rip_vers != RIPv1)) {
566 trace_pkt(" discard RIPv%d response",
571 /* Ignore routes via dead interface.
573 if (aifp->int_state & IS_BROKE) {
574 trace_pkt("discard response via broken interface %s",
579 /* If the interface cares, ignore bad routers.
580 * Trace but do not log this problem, because where it
581 * happens, it happens frequently.
583 if (aifp->int_state & IS_DISTRUST) {
585 while (tg->tgate_addr != FROM_NADDR) {
588 trace_pkt(" discard RIP response"
589 " from untrusted router %s",
590 naddr_ntoa(FROM_NADDR));
596 /* Authenticate the packet if we have a secret.
597 * If we do not have any secrets, ignore the error in
598 * RFC 1723 and accept it regardless.
600 if (aifp->int_auth[0].type != RIP_AUTH_NONE
601 && rip->rip_vers != RIPv1
602 && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
606 if (n->n_family == RIP_AF_AUTH)
609 n->n_metric = ntohl(n->n_metric);
611 if (n->n_family != RIP_AF_INET
612 && (n->n_family != RIP_AF_UNSPEC
613 || dst != RIP_DEFAULT)) {
614 msglim(&bad_router, FROM_NADDR,
615 "route from %s to unsupported"
616 " address family=%d destination=%s",
617 naddr_ntoa(FROM_NADDR),
622 if (!check_dst(dst)) {
623 msglim(&bad_router, FROM_NADDR,
624 "bad destination %s from %s",
626 naddr_ntoa(FROM_NADDR));
630 || n->n_metric > HOPCNT_INFINITY) {
631 msglim(&bad_router, FROM_NADDR,
632 "bad metric %d from %s"
633 " for destination %s",
635 naddr_ntoa(FROM_NADDR),
640 /* Notice the next-hop.
643 if (n->n_nhop != 0) {
644 if (rip->rip_vers == RIPv1) {
647 /* Use it only if it is valid. */
648 if (on_net(n->n_nhop,
649 aifp->int_net, aifp->int_mask)
650 && check_dst(n->n_nhop)) {
653 msglim(&bad_nhop, FROM_NADDR,
655 " has bad next hop %s",
656 naddr_ntoa(FROM_NADDR),
658 naddr_ntoa(n->n_nhop));
664 if (rip->rip_vers == RIPv1
665 || 0 == (mask = ntohl(n->n_mask))) {
666 mask = ripv1_mask_host(dst,aifp);
667 } else if ((ntohl(dst) & ~mask) != 0) {
668 msglim(&bad_mask, FROM_NADDR,
669 "router %s sent bad netmask"
671 naddr_ntoa(FROM_NADDR),
676 if (rip->rip_vers == RIPv1)
679 /* Adjust metric according to incoming interface..
681 n->n_metric += (aifp->int_metric
682 + aifp->int_adj_inmetric);
683 if (n->n_metric > HOPCNT_INFINITY)
684 n->n_metric = HOPCNT_INFINITY;
686 /* Should we trust this route from this router? */
687 if (tg && (tn = tg->tgate_nets)->mask != 0) {
688 for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
689 if (on_net(dst, tn->net, tn->mask)
693 if (i >= MAX_TGATE_NETS || tn->mask == 0) {
694 trace_pkt(" ignored unauthorized %s",
695 addrname(dst,mask,0));
700 /* Recognize and ignore a default route we faked
701 * which is being sent back to us by a machine with
702 * broken split-horizon.
703 * Be a little more paranoid than that, and reject
704 * default routes with the same metric we advertised.
706 if (aifp->int_d_metric != 0
707 && dst == RIP_DEFAULT
708 && (int)n->n_metric >= aifp->int_d_metric)
711 /* We can receive aggregated RIPv2 routes that must
712 * be broken down before they are transmitted by
713 * RIPv1 via an interface on a subnet.
714 * We might also receive the same routes aggregated
715 * via other RIPv2 interfaces.
716 * This could cause duplicate routes to be sent on
717 * the RIPv1 interfaces. "Longest matching variable
718 * length netmasks" lets RIPv2 listeners understand,
719 * but breaking down the aggregated routes for RIPv1
720 * listeners can produce duplicate routes.
722 * Breaking down aggregated routes here bloats
723 * the daemon table, but does not hurt the kernel
724 * table, since routes are always aggregated for
727 * Notice that this does not break down network
728 * routes corresponding to subnets. This is part
729 * of the defense against RS_NET_SYN.
732 && (((rt = rtget(dst,mask)) == NULL
733 || !(rt->rt_state & RS_NET_SYN)))
734 && (v1_mask = ripv1_mask_net(dst,0)) > mask) {
735 ddst_h = v1_mask & -v1_mask;
736 i = (v1_mask & ~mask)/ddst_h;
738 /* Punt if we would have to generate
739 * an unreasonable number of routes.
742 trace_misc("accept %s-->%s as 1"
743 " instead of %d routes",
744 addrname(dst,mask,0),
745 naddr_ntoa(FROM_NADDR),
756 new.rts_router = FROM_NADDR;
757 new.rts_metric = n->n_metric;
758 new.rts_tag = n->n_tag;
759 new.rts_time = now.tv_sec;
764 input_route(dst, mask, &new, n);
767 dst = htonl(ntohl(dst) + ddst_h);
776 /* Process a single input route.
779 input_route(naddr dst, /* network order */
781 struct rt_spare *new,
786 struct rt_spare *rts, *rts0;
787 struct interface *ifp1;
790 /* See if the other guy is telling us to send our packets to him.
791 * Sometimes network routes arrive over a point-to-point link for
792 * the network containing the address(es) of the link.
794 * If our interface is broken, switch to using the other guy.
796 ifp1 = ifwithaddr(dst, 1, 1);
798 && (!(ifp1->int_state & IS_BROKE)
799 || (ifp1->int_state & IS_PASSIVE)))
802 /* Look for the route in our table.
804 rt = rtget(dst, mask);
806 /* Consider adding the route if we do not already have it.
809 /* Ignore unknown routes being poisoned.
811 if (new->rts_metric == HOPCNT_INFINITY)
814 /* Ignore the route if it points to us */
816 && ifwithaddr(n->n_nhop, 1, 0) != NULL)
819 /* If something has not gone crazy and tried to fill
820 * our memory, accept the new route.
822 if (total_routes < MAX_ROUTES)
823 rtadd(dst, mask, 0, new);
827 /* We already know about the route. Consider this update.
829 * If (rt->rt_state & RS_NET_SYN), then this route
830 * is the same as a network route we have inferred
831 * for subnets we know, in order to tell RIPv1 routers
834 * It is impossible to tell if the route is coming
835 * from a distant RIPv2 router with the standard
836 * netmask because that router knows about the entire
837 * network, or if it is a round-about echo of a
838 * synthetic, RIPv1 network route of our own.
839 * The worst is that both kinds of routes might be
840 * received, and the bad one might have the smaller
841 * metric. Partly solve this problem by never
842 * aggregating into such a route. Also keep it
843 * around as long as the interface exists.
846 rts0 = rt->rt_spares;
847 for (rts = rts0, i = NUM_SPARES; i != 0; i--, rts++) {
848 if (rts->rts_router == new->rts_router)
850 /* Note the worst slot to reuse,
851 * other than the current slot.
853 if (rts0 == rt->rt_spares
854 || BETTER_LINK(rt, rts0, rts))
858 /* Found a route from the router already in the table.
861 /* If the new route is a route broken down from an
862 * aggregated route, and if the previous route is either
863 * not a broken down route or was broken down from a finer
864 * netmask, and if the previous route is current,
865 * then forget this one.
867 if (new->rts_de_ag > rts->rts_de_ag
868 && now_stale <= rts->rts_time)
871 /* Keep poisoned routes around only long enough to pass
872 * the poison on. Use a new timestamp for good routes.
874 if (rts->rts_metric == HOPCNT_INFINITY
875 && new->rts_metric == HOPCNT_INFINITY)
876 new->rts_time = rts->rts_time;
878 /* If this is an update for the router we currently prefer,
881 if (i == NUM_SPARES) {
882 rtchange(rt, rt->rt_state, new, 0);
883 /* If the route got worse, check for something better.
885 if (new->rts_metric > rts->rts_metric)
890 /* This is an update for a spare route.
891 * Finished if the route is unchanged.
893 if (rts->rts_gate == new->rts_gate
894 && rts->rts_metric == new->rts_metric
895 && rts->rts_tag == new->rts_tag) {
896 trace_upslot(rt, rts, new);
900 /* Forget it if it has gone bad.
902 if (new->rts_metric == HOPCNT_INFINITY) {
908 /* The update is for a route we know about,
909 * but not from a familiar router.
911 * Ignore the route if it points to us.
914 && NULL != ifwithaddr(n->n_nhop, 1, 0))
917 /* the loop above set rts0=worst spare */
920 /* Save the route as a spare only if it has
921 * a better metric than our worst spare.
922 * This also ignores poisoned routes (those
923 * received with metric HOPCNT_INFINITY).
925 if (new->rts_metric >= rts->rts_metric)
929 trace_upslot(rt, rts, new);
932 /* try to switch to a better route */
937 static int /* 0 if bad */
938 ck_passwd(struct interface *aifp,
942 struct msg_limit *use_authp)
944 # define NA (rip->rip_auths)
948 u_char hash[RIP_AUTH_PW_LEN];
951 assert(aifp != NULL);
952 if ((void *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
953 msglim(use_authp, from, "missing password from %s",
958 /* accept any current (+/- 24 hours) password
960 for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
961 if (ap->type != NA->a_type
962 || (u_long)ap->start > (u_long)clk.tv_sec+DAY
963 || (u_long)ap->end+DAY < (u_long)clk.tv_sec)
966 if (NA->a_type == RIP_AUTH_PW) {
967 if (!memcmp(NA->au.au_pw, ap->key, RIP_AUTH_PW_LEN))
971 /* accept MD5 secret with the right key ID
973 if (NA->au.a_md5.md5_keyid != ap->keyid)
976 len = ntohs(NA->au.a_md5.md5_pkt_len);
977 if ((len-sizeof(*rip)) % sizeof(*NA) != 0
978 || len != (char *)lim-(char*)rip-(int)sizeof(*NA)) {
979 msglim(use_authp, from,
980 "wrong MD5 RIPv2 packet length of %d"
981 " instead of %d from %s",
982 len, (int)((char *)lim-(char *)rip
987 na2 = (struct netauth *)((char *)rip+len);
989 /* Given a good hash value, these are not security
990 * problems so be generous and accept the routes,
994 if (NA->au.a_md5.md5_auth_len
995 != RIP_AUTH_MD5_HASH_LEN)
996 msglim(use_authp, from,
997 "unknown MD5 RIPv2 auth len %#x"
998 " instead of %#x from %s",
999 NA->au.a_md5.md5_auth_len,
1000 (unsigned)RIP_AUTH_MD5_HASH_LEN,
1002 if (na2->a_family != RIP_AF_AUTH)
1003 msglim(use_authp, from,
1004 "unknown MD5 RIPv2 family %#x"
1005 " instead of %#x from %s",
1006 na2->a_family, RIP_AF_AUTH,
1008 if (na2->a_type != ntohs(1))
1009 msglim(use_authp, from,
1010 "MD5 RIPv2 hash has %#x"
1011 " instead of %#x from %s",
1012 na2->a_type, ntohs(1),
1017 MD5Update(&md5_ctx, (u_char *)rip,
1018 len + RIP_AUTH_MD5_HASH_XTRA);
1019 MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_KEY_LEN);
1020 MD5Final(hash, &md5_ctx);
1021 if (!memcmp(hash, na2->au.au_pw, sizeof(hash)))
1026 msglim(use_authp, from, "bad password from %s",