2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * Copyright (c) 1983, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgment:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/sbin/routed/input.c,v 1.9 2001/06/06 20:52:30 phk Exp $
43 * The size of the control buffer passed to recvmsg() used to receive
46 #define CONTROL_BUFSIZE 1024
48 static void input(struct sockaddr_in
*, struct interface
*, struct rip
*, int);
49 static boolean_t
ck_passwd(struct interface
*, struct rip
*, uint8_t *,
50 in_addr_t
, struct msg_limit
*);
54 * Find the interface which received the given message.
57 receiving_interface(struct msghdr
*msg
, boolean_t findremote
)
59 struct interface
*ifp
, *ifp1
, *ifp2
;
60 struct sockaddr_in
*from
;
64 from
= (struct sockaddr_in
*)msg
->msg_name
;
66 /* First see if this packet came from a remote gateway. */
67 if (findremote
&& ((ifp
= findremoteif(from
->sin_addr
.s_addr
)) != NULL
))
71 * It did not come from a remote gateway. Determine which
72 * physical interface this packet was received on by
73 * processing the message's ancillary data to find the
74 * IP_RECVIF option we requested.
76 if ((opt
= find_ancillary(msg
, IP_RECVIF
)) == NULL
) {
77 msglog("unable to retrieve IP_RECVIF");
79 ifindex
= *(uint_t
*)opt
;
80 if ((ifp
= ifwithindex(ifindex
, _B_TRUE
)) != NULL
) {
81 /* Find the best match of the aliases */
83 for (ifp1
= ifp
; ifp1
!= NULL
;
84 ifp1
= ifp1
->int_ilist
.hl_next
) {
85 if (ifp1
->int_addr
== from
->sin_addr
.s_addr
)
88 (ifp2
->int_state
& IS_ALIAS
)) &&
89 on_net(from
->sin_addr
.s_addr
, ifp1
->int_net
,
101 * As a last resort (for some reason, ip didn't give us the
102 * IP_RECVIF index we requested), try to deduce the receiving
103 * interface based on the source address of the packet.
105 return (iflookup(from
->sin_addr
.s_addr
));
109 * Process RIP input on rip_sock. Returns 0 for success, -1 for failure.
114 struct sockaddr_in from
;
115 struct interface
*ifp
;
120 uint8_t ancillary_data
[CONTROL_BUFSIZE
];
122 iov
.iov_base
= &inbuf
;
123 iov
.iov_len
= sizeof (inbuf
);
126 msg
.msg_name
= &from
;
127 msg
.msg_control
= &ancillary_data
;
130 msg
.msg_namelen
= sizeof (from
);
131 msg
.msg_controllen
= sizeof (ancillary_data
);
132 cc
= recvmsg(rip_sock
, &msg
, 0);
136 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
138 LOGERR("recvmsg(rip_sock)");
143 * ifp is the interface via which the packet arrived.
145 ifp
= receiving_interface(&msg
, _B_TRUE
);
147 input(&from
, ifp
, &inbuf
.rip
, cc
);
152 /* Process a RIP packet */
154 input(struct sockaddr_in
*from
, /* received from this IP address */
155 struct interface
*ifp
, /* interface of incoming socket */
159 #define FROM_NADDR from->sin_addr.s_addr
160 static struct msg_limit use_auth
, bad_len
, bad_mask
;
161 static struct msg_limit unk_router
, bad_router
, bad_nhop
;
165 struct netinfo
*n
, *lim
;
166 struct interface
*ifp1
;
167 in_addr_t gate
, mask
, v1_mask
, dst
, ddst_h
= 0;
169 struct tgate
*tg
= NULL
;
170 struct tgate_net
*tn
;
172 boolean_t poll_answer
= _B_FALSE
; /* Set to _B_TRUE if RIPCMD_POLL */
173 uint16_t rt_state
= 0; /* Extra route state to pass to input_route() */
176 (void) memset(&new, 0, sizeof (new));
177 /* Notice when we hear from a remote gateway */
178 if (ifp
!= NULL
&& (ifp
->int_state
& IS_REMOTE
))
179 ifp
->int_act_time
= now
.tv_sec
;
181 trace_rip("Recv", "from", from
, ifp
, rip
, cc
);
183 if (ifp
!= NULL
&& (ifp
->int_if_flags
& IFF_NORTEXCH
)) {
184 trace_misc("discard RIP packet received over %s (IFF_NORTEXCH)",
189 gate
= ntohl(FROM_NADDR
);
190 if (IN_CLASSD(gate
) || (gate
>> IN_CLASSA_NSHIFT
) == 0) {
191 msglim(&bad_router
, FROM_NADDR
, "source address %s unusable",
192 naddr_ntoa(FROM_NADDR
));
196 if (rip
->rip_vers
== 0) {
197 msglim(&bad_router
, FROM_NADDR
,
198 "RIP version 0, cmd %d, packet received from %s",
199 rip
->rip_cmd
, naddr_ntoa(FROM_NADDR
));
203 if (rip
->rip_vers
> RIPv2
) {
204 msglim(&bad_router
, FROM_NADDR
,
205 "Treating RIP version %d packet received from %s as "
206 "version %d", rip
->rip_vers
, naddr_ntoa(FROM_NADDR
),
208 rip
->rip_vers
= RIPv2
;
211 if (cc
> (int)OVER_MAXPACKETSIZE
) {
212 msglim(&bad_router
, FROM_NADDR
,
213 "packet at least %d bytes too long received from %s",
214 cc
-MAXPACKETSIZE
, naddr_ntoa(FROM_NADDR
));
218 lim
= n
+ (cc
- 4) / sizeof (struct netinfo
);
221 * Notice authentication.
222 * As required by section 5.2 of RFC 2453, discard authenticated
223 * RIPv2 messages, but only if configured for that silliness.
225 * RIPv2 authentication is lame. Why authenticate queries?
226 * Why should a RIPv2 implementation with authentication disabled
227 * not be able to listen to RIPv2 packets with authentication, while
228 * RIPv1 systems will listen? Crazy!
230 if (!auth_ok
&& rip
->rip_vers
== RIPv2
&& n
< lim
&&
231 n
->n_family
== RIP_AF_AUTH
) {
232 msglim(&use_auth
, FROM_NADDR
,
233 "RIPv2 message with authentication from %s discarded",
234 naddr_ntoa(FROM_NADDR
));
238 switch (rip
->rip_cmd
) {
241 * Similar to RIPCMD_REQUEST, this command is used to
242 * request either a full-table or a set of entries. Both
243 * silent processes and routers can respond to this
246 poll_answer
= _B_TRUE
;
249 /* Are we talking to ourself or a remote gateway? */
250 ifp1
= ifwithaddr(FROM_NADDR
, _B_FALSE
, _B_TRUE
);
252 if (ifp1
->int_state
& IS_REMOTE
) {
255 if (check_remote(ifp
)) {
256 ifp
->int_act_time
= now
.tv_sec
;
257 if_ok(ifp
, "remote ", _B_FALSE
);
259 } else if (from
->sin_port
== htons(RIP_PORT
)) {
260 trace_pkt(" discard our own RIP request");
265 /* did the request come from a router? */
266 if (!poll_answer
&& (from
->sin_port
== htons(RIP_PORT
))) {
268 * yes, ignore the request if RIP is off so that
269 * the router does not depend on us.
271 if (ripout_interfaces
== 0 ||
272 (ifp
!= NULL
&& (IS_RIP_OUT_OFF(ifp
->int_state
) ||
273 !IS_IFF_ROUTING(ifp
->int_if_flags
)))) {
274 trace_pkt(" discard request while RIP off");
280 * According to RFC 2453 section 5.2, we should ignore
281 * unauthenticated queries when authentication is
282 * configured. That is too silly to bother with. Sheesh!
283 * Are forwarding tables supposed to be secret even though
284 * a bad guy can infer them with test traffic? RIP is
285 * still the most common router-discovery protocol, so
286 * hosts need to send queries that will be answered. What
287 * about `rtquery`? Maybe on firewalls you'd care, but not
288 * enough to give up the diagnostic facilities of remote
293 msglim(&bad_len
, FROM_NADDR
, "empty request from %s",
294 naddr_ntoa(FROM_NADDR
));
297 if (cc
%sizeof (*n
) != sizeof (struct rip
)%sizeof (*n
)) {
298 msglim(&bad_len
, FROM_NADDR
,
299 "request of bad length (%d) from %s",
300 cc
, naddr_ntoa(FROM_NADDR
));
303 if (rip
->rip_vers
== RIPv2
&& (ifp
== NULL
||
304 (ifp
->int_state
& IS_NO_RIPV1_OUT
))) {
305 v12buf
.buf
->rip_vers
= RIPv2
;
307 * If we have a secret but it is a cleartext secret,
308 * do not disclose our secret unless the other guy
313 (ulong_t
)ap
->end
< (ulong_t
)clk
.tv_sec
) {
315 * Don't authenticate incoming packets
316 * using an expired key.
318 msglim(&use_auth
, FROM_NADDR
,
319 "%s attempting to authenticate using "
320 "an expired password.",
321 naddr_ntoa(FROM_NADDR
));
324 if (ap
!= NULL
&& ap
->type
== RIP_AUTH_PW
&&
325 (n
->n_family
!= RIP_AF_AUTH
||
326 !ck_passwd(ifp
, rip
, (uint8_t *)lim
, FROM_NADDR
,
330 v12buf
.buf
->rip_vers
= RIPv1
;
333 clr_ws_buf(&v12buf
, ap
);
336 n
->n_metric
= ntohl(n
->n_metric
);
339 * A single entry with family RIP_AF_UNSPEC and
340 * metric HOPCNT_INFINITY means "all routes".
341 * We respond to routers only if we are acting
342 * as a supplier, or to anyone other than a router
345 if (n
->n_family
== RIP_AF_UNSPEC
&&
346 n
->n_metric
== HOPCNT_INFINITY
) {
348 * Answer a full-table query from a utility
349 * program with all we know.
352 (from
->sin_port
!= htons(RIP_PORT
))) {
353 supply(from
, ifp
, OUT_QUERY
, 0,
354 rip
->rip_vers
, ap
!= NULL
);
359 * A router is trying to prime its tables.
360 * Filter the answer in the same way
361 * broadcasts are filtered.
363 * Only answer a router if we are a supplier
364 * to keep an unwary host that is just starting
365 * from picking us as a router.
368 trace_pkt("ignore distant router");
371 if (IS_RIP_OFF(ifp
->int_state
) ||
372 !should_supply(ifp
)) {
373 trace_pkt("ignore; not supplying");
378 * Do not answer a RIPv1 router if
379 * we are sending RIPv2. But do offer
380 * poor man's router discovery.
382 if ((ifp
->int_state
& IS_NO_RIPV1_OUT
) &&
383 rip
->rip_vers
== RIPv1
) {
384 if (!(ifp
->int_state
& IS_PM_RDISC
)) {
385 trace_pkt("ignore; sending "
390 v12buf
.n
->n_family
= RIP_AF_INET
;
391 v12buf
.n
->n_dst
= RIP_DEFAULT
;
392 metric
= ifp
->int_d_metric
;
394 (rt
= rtget(RIP_DEFAULT
, 0)))
396 (rt
->rt_metric
+ 1));
397 v12buf
.n
->n_metric
= htonl(metric
);
403 * Respond with RIPv1 instead of RIPv2 if
404 * that is what we are broadcasting on the
405 * interface to keep the remote router from
406 * getting the wrong initial idea of the
409 supply(from
, ifp
, OUT_UNICAST
, 0,
410 (ifp
->int_state
& IS_NO_RIPV1_OUT
)
416 /* Ignore authentication */
417 if (n
->n_family
== RIP_AF_AUTH
)
420 if (n
->n_family
!= RIP_AF_INET
) {
421 msglim(&bad_router
, FROM_NADDR
,
422 "request from %s for unsupported"
424 naddr_ntoa(FROM_NADDR
),
426 naddr_ntoa(n
->n_dst
));
430 /* We are being asked about a specific destination. */
431 v12buf
.n
->n_dst
= dst
= n
->n_dst
;
432 v12buf
.n
->n_family
= RIP_AF_INET
;
433 if (!check_dst(dst
)) {
434 msglim(&bad_router
, FROM_NADDR
,
435 "bad queried destination %s from %s",
437 naddr_ntoa(FROM_NADDR
));
438 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
442 /* decide what mask was intended */
443 if (rip
->rip_vers
== RIPv1
||
444 0 == (mask
= ntohl(n
->n_mask
)) ||
445 0 != (ntohl(dst
) & ~mask
))
446 mask
= ripv1_mask_host(dst
, ifp
);
449 * Try to find the answer. If we don't have an
450 * explicit route for the destination, use the best
451 * route to the destination.
453 rt
= rtget(dst
, mask
);
454 if (rt
== NULL
&& dst
!= RIP_DEFAULT
)
455 rt
= rtfind(n
->n_dst
);
457 if (v12buf
.buf
->rip_vers
!= RIPv1
)
458 v12buf
.n
->n_mask
= htonl(mask
);
460 /* we do not have the answer */
461 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
466 * we have the answer, so compute the right metric
469 v12buf
.n
->n_metric
= rt
->rt_metric
+ 1;
470 if (v12buf
.n
->n_metric
> HOPCNT_INFINITY
)
471 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
472 if (v12buf
.buf
->rip_vers
!= RIPv1
) {
473 v12buf
.n
->n_tag
= rt
->rt_tag
;
475 on_net(rt
->rt_gate
, ifp
->int_net
,
477 rt
->rt_gate
!= ifp
->int_addr
)
478 v12buf
.n
->n_nhop
= rt
->rt_gate
;
481 v12buf
.n
->n_metric
= htonl(v12buf
.n
->n_metric
);
484 * Stop paying attention if we fill the output buffer.
486 if (++v12buf
.n
>= v12buf
.lim
)
491 * If our response is authenticated with md5, complete the
494 if (ap
!= NULL
&& ap
->type
== RIP_AUTH_MD5
)
495 end_md5_auth(&v12buf
, ap
);
498 * Diagnostic programs make specific requests
499 * from ports other than 520. Log other types
500 * of specific requests as suspicious.
502 if (!poll_answer
&& (from
->sin_port
== htons(RIP_PORT
))) {
503 writelog(LOG_WARNING
,
504 "Received suspicious request from %s port %d",
505 naddr_ntoa(FROM_NADDR
), RIP_PORT
);
507 if (poll_answer
|| (from
->sin_port
!= htons(RIP_PORT
))) {
509 (void) output(OUT_QUERY
, from
, ifp
, v12buf
.buf
,
510 ((char *)v12buf
.n
- (char *)v12buf
.buf
));
512 (void) output(OUT_UNICAST
, from
, ifp
,
513 v12buf
.buf
, ((char *)v12buf
.n
-
514 (char *)v12buf
.buf
));
519 case RIPCMD_TRACEOFF
:
521 * Notice that trace messages are turned off for all possible
522 * abuse if PATH_TRACE is undefined in pathnames.h.
523 * Notice also that because of the way the trace file is
524 * handled in trace.c, no abuse is plausible even if
525 * PATH_TRACE is defined.
527 * First verify message came from a privileged port.
529 if (ntohs(from
->sin_port
) > IPPORT_RESERVED
) {
530 trace_pkt("trace command from untrusted port %d on %s",
531 ntohs(from
->sin_port
), naddr_ntoa(FROM_NADDR
));
534 if (ifp
== NULL
|| !remote_address_ok(ifp
, FROM_NADDR
)) {
536 * Use a message here to warn about strange
537 * messages from remote systems.
539 msglim(&bad_router
, FROM_NADDR
,
540 "trace command from non-local host %s",
541 naddr_ntoa(FROM_NADDR
));
544 if (ifp
->int_state
& IS_DISTRUST
) {
546 while (tg
->tgate_addr
!= FROM_NADDR
) {
549 trace_pkt("trace command from "
551 naddr_ntoa(FROM_NADDR
));
556 if (ifp
->int_auth
[0].type
!= RIP_AUTH_NONE
) {
558 * Technically, it would be fairly easy to add
559 * standard authentication to the existing
560 * trace commands -- just bracket the payload
561 * with the authentication information.
562 * However, the tracing message behavior
563 * itself is marginal enough that we don't
564 * actually care. Just discard if
565 * authentication is needed.
567 trace_pkt("trace command unauthenticated from %s",
568 naddr_ntoa(FROM_NADDR
));
571 if (rip
->rip_cmd
== RIPCMD_TRACEON
) {
572 rip
->rip_tracefile
[cc
-4] = '\0';
573 set_tracefile(rip
->rip_tracefile
,
574 "trace command: %s\n", 0);
576 trace_off("tracing turned off by %s",
577 naddr_ntoa(FROM_NADDR
));
581 case RIPCMD_RESPONSE
:
582 if (ifp
!= NULL
&& (ifp
->int_if_flags
& IFF_NOXMIT
)) {
583 trace_misc("discard RIP response received over %s "
584 "(IFF_NOXMIT)", ifp
->int_name
);
588 if (cc
%sizeof (*n
) != sizeof (struct rip
)%sizeof (*n
)) {
589 msglim(&bad_len
, FROM_NADDR
,
590 "response of bad length (%d) from %s",
591 cc
, naddr_ntoa(FROM_NADDR
));
594 if ((gate
>> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
595 IN_LINKLOCAL(gate
)) {
596 msglim(&bad_router
, FROM_NADDR
,
597 "discard RIP response from bad source address %s",
598 naddr_ntoa(FROM_NADDR
));
602 /* verify message came from a router */
603 if (from
->sin_port
!= htons(RIP_PORT
)) {
604 msglim(&bad_router
, FROM_NADDR
,
605 " discard RIP response from unknown port"
606 " %d on host %s", ntohs(from
->sin_port
),
607 naddr_ntoa(FROM_NADDR
));
612 trace_pkt(" discard response while RIP off");
616 /* Are we talking to ourself or a remote gateway? */
617 ifp1
= ifwithaddr(FROM_NADDR
, _B_FALSE
, _B_TRUE
);
619 if (ifp1
->int_state
& IS_REMOTE
) {
622 if (check_remote(ifp
)) {
623 ifp
->int_act_time
= now
.tv_sec
;
624 if_ok(ifp
, "remote ", _B_FALSE
);
627 trace_pkt(" discard our own RIP response");
632 * If it's not a remote gateway, then the
633 * remote address *must* be directly
634 * connected. Make sure that it is.
637 !remote_address_ok(ifp
, FROM_NADDR
)) {
638 msglim(&bad_router
, FROM_NADDR
,
639 "discard RIP response; source %s not on "
640 "interface %s", naddr_ntoa(FROM_NADDR
),
647 * Accept routing packets from routers directly connected
648 * via broadcast or point-to-point networks, and from
649 * those listed in /etc/gateways.
652 msglim(&unk_router
, FROM_NADDR
,
653 " discard response from %s"
654 " via unexpected interface",
655 naddr_ntoa(FROM_NADDR
));
659 if (IS_RIP_IN_OFF(ifp
->int_state
)) {
660 trace_pkt(" discard RIPv%d response"
661 " via disabled interface %s",
662 rip
->rip_vers
, ifp
->int_name
);
667 msglim(&bad_len
, FROM_NADDR
, "empty response from %s",
668 naddr_ntoa(FROM_NADDR
));
672 if (((ifp
->int_state
& IS_NO_RIPV1_IN
) &&
673 rip
->rip_vers
== RIPv1
) ||
674 ((ifp
->int_state
& IS_NO_RIPV2_IN
) &&
675 rip
->rip_vers
!= RIPv1
)) {
676 trace_pkt(" discard RIPv%d response",
682 * Continue to listen to routes via broken interfaces
683 * which might be declared IS_BROKE because of
684 * device-driver idiosyncracies, but might otherwise
685 * be perfectly healthy.
687 if (ifp
->int_state
& IS_BROKE
) {
688 trace_pkt("response via broken interface %s",
693 * If the interface cares, ignore bad routers.
694 * Trace but do not log this problem, because where it
695 * happens, it happens frequently.
697 if (ifp
->int_state
& IS_DISTRUST
) {
699 while (tg
->tgate_addr
!= FROM_NADDR
) {
702 trace_pkt(" discard RIP response"
703 " from untrusted router %s",
704 naddr_ntoa(FROM_NADDR
));
711 * Authenticate the packet if we have a secret.
712 * If we do not have any secrets, ignore the error in
713 * RFC 1723 and accept it regardless.
715 if (ifp
->int_auth
[0].type
!= RIP_AUTH_NONE
&&
716 rip
->rip_vers
!= RIPv1
&&
717 !ck_passwd(ifp
, rip
, (uint8_t *)lim
, FROM_NADDR
, &use_auth
))
721 * Do this only if we're supplying routes to *nobody*.
723 if (!should_supply(NULL
) && save_space
) {
725 * "-S" option. Instead of entering all routes,
726 * only enter a default route for the sender of
727 * this RESPONSE message
730 /* Should we trust this route from this router? */
731 if (tg
!= NULL
&& tg
->tgate_nets
->mask
!= 0) {
732 trace_pkt(" ignored unauthorized %s",
733 addrname(RIP_DEFAULT
, 0, 0));
737 new.rts_gate
= FROM_NADDR
;
738 new.rts_router
= FROM_NADDR
;
739 new.rts_metric
= HOPCNT_INFINITY
-1;
740 new.rts_tag
= n
->n_tag
;
741 new.rts_time
= now
.tv_sec
;
744 new.rts_origin
= RO_RIP
;
746 * Add the newly generated default route, but don't
747 * propagate the madness. Treat it the same way as
748 * default routes learned from Router Discovery.
750 input_route(RIP_DEFAULT
, 0, &new, n
, RS_NOPROPAGATE
);
754 if (!IS_IFF_ROUTING(ifp
->int_if_flags
)) {
756 * We don't want to propagate routes which would
757 * result in a black-hole.
759 rt_state
= RS_NOPROPAGATE
;
763 if (n
->n_family
== RIP_AF_AUTH
)
766 n
->n_metric
= ntohl(n
->n_metric
);
768 if (n
->n_family
!= RIP_AF_INET
&&
769 (n
->n_family
!= RIP_AF_UNSPEC
||
770 dst
!= RIP_DEFAULT
)) {
771 msglim(&bad_router
, FROM_NADDR
,
772 "route from %s to unsupported"
773 " address family=%d destination=%s",
774 naddr_ntoa(FROM_NADDR
), n
->n_family
,
778 if (!check_dst(dst
)) {
779 msglim(&bad_router
, FROM_NADDR
,
780 "bad destination %s from %s",
782 naddr_ntoa(FROM_NADDR
));
785 if (n
->n_metric
== 0 || n
->n_metric
> HOPCNT_INFINITY
) {
786 msglim(&bad_router
, FROM_NADDR
,
787 "bad metric %d from %s"
788 " for destination %s",
789 n
->n_metric
, naddr_ntoa(FROM_NADDR
),
795 * Notice the next-hop.
798 if (n
->n_nhop
!= 0) {
799 if (rip
->rip_vers
== RIPv1
) {
802 /* Use it only if it is valid. */
803 if (on_net(n
->n_nhop
,
804 ifp
->int_net
, ifp
->int_mask
) &&
805 check_dst(n
->n_nhop
)) {
811 " has bad next hop %s",
812 naddr_ntoa(FROM_NADDR
),
814 naddr_ntoa(n
->n_nhop
));
820 if (rip
->rip_vers
== RIPv1
||
821 0 == (mask
= ntohl(n
->n_mask
))) {
822 mask
= ripv1_mask_host(dst
, ifp
);
823 } else if ((ntohl(dst
) & ~mask
) != 0) {
824 msglim(&bad_mask
, FROM_NADDR
,
825 "router %s sent bad netmask %s with %s",
826 naddr_ntoa(FROM_NADDR
),
827 naddr_ntoa(htonl(mask
)),
832 if (mask
== HOST_MASK
&&
833 (ifp
->int_state
& IS_NO_HOST
)) {
834 trace_pkt(" ignored host route %s",
835 addrname(dst
, mask
, 0));
839 if (rip
->rip_vers
== RIPv1
)
843 * Adjust metric according to incoming interface cost.
844 * We intentionally don't drop incoming routes with
845 * metric 15 on the floor even though they will
846 * not be advertised to other routers. We can use
847 * such routes locally, resulting in a network with
848 * a maximum width of 15 hops rather than 14.
850 n
->n_metric
+= ifp
->int_metric
;
851 if (n
->n_metric
> HOPCNT_INFINITY
)
852 n
->n_metric
= HOPCNT_INFINITY
;
855 * Should we trust this route from this router?
857 if (tg
!= NULL
&& (tn
= tg
->tgate_nets
)->mask
!= 0) {
858 for (i
= 0; i
< MAX_TGATE_NETS
; i
++, tn
++) {
859 if (on_net(dst
, tn
->net
, tn
->mask
) &&
863 if (i
>= MAX_TGATE_NETS
|| tn
->mask
== 0) {
864 trace_pkt(" ignored unauthorized %s",
865 addrname(dst
, mask
, 0));
871 * Recognize and ignore a default route we faked
872 * which is being sent back to us by a machine with
873 * broken split-horizon. Be a little more paranoid
874 * than that, and reject default routes with the
875 * same metric we advertised.
877 if (ifp
->int_d_metric
!= 0 && dst
== RIP_DEFAULT
&&
878 n
->n_metric
>= ifp
->int_d_metric
)
882 * We can receive aggregated RIPv2 routes that must
883 * be broken down before they are transmitted by
884 * RIPv1 via an interface on a subnet. We might
885 * also receive the same routes aggregated via
886 * other RIPv2 interfaces. This could cause
887 * duplicate routes to be sent on the RIPv1
888 * interfaces. "Longest matching variable length
889 * netmasks" lets RIPv2 listeners understand, but
890 * breaking down the aggregated routes for RIPv1
891 * listeners can produce duplicate routes.
893 * Breaking down aggregated routes here bloats the
894 * daemon table, but does not hurt the kernel
895 * table, since routes are always aggregated for
898 * Notice that this does not break down network
899 * routes corresponding to subnets. This is part of
900 * the defense against RS_NET_SYN.
902 if (have_ripv1_out
&&
903 (((rt
= rtget(dst
, mask
)) == NULL
||
904 !(rt
->rt_state
& RS_NET_SYN
))) &&
905 (v1_mask
= ripv1_mask_net(dst
, 0)) > mask
) {
906 /* Get least significant set bit */
907 ddst_h
= v1_mask
& -v1_mask
;
908 i
= (v1_mask
& ~mask
)/ddst_h
;
910 * If you're going to make 512 or more
911 * routes, then that's just too many. The
912 * reason here is that breaking an old
913 * class B into /24 allocations is common
914 * enough that allowing for the creation of
915 * at least 256 deaggregated routes is
916 * good. The next power of 2 is 512.
920 * Punt if we would have to
921 * generate an unreasonable number
925 trace_misc("accept %s-->%s as 1"
926 " instead of %d routes",
927 addrname(dst
, mask
, 0),
928 naddr_ntoa(FROM_NADDR
),
939 new.rts_router
= FROM_NADDR
;
940 new.rts_metric
= n
->n_metric
;
941 new.rts_tag
= n
->n_tag
;
942 new.rts_time
= now
.tv_sec
;
945 new.rts_origin
= RO_RIP
;
948 input_route(dst
, mask
, &new, n
, rt_state
);
951 dst
= htonl(ntohl(dst
) + ddst_h
);
955 case RIPCMD_POLLENTRY
:
957 * With this command one can request a single entry.
958 * Both silent processes and routers can respond to this
963 msglim(&bad_len
, FROM_NADDR
, "empty request from %s",
964 naddr_ntoa(FROM_NADDR
));
967 if (cc
%sizeof (*n
) != sizeof (struct rip
)%sizeof (*n
)) {
968 msglim(&bad_len
, FROM_NADDR
,
969 "request of bad length (%d) from %s",
970 cc
, naddr_ntoa(FROM_NADDR
));
973 if (rip
->rip_vers
== RIPv2
&& (ifp
== NULL
||
974 (ifp
->int_state
& IS_NO_RIPV1_OUT
))) {
975 v12buf
.buf
->rip_vers
= RIPv2
;
977 v12buf
.buf
->rip_vers
= RIPv1
;
979 /* Dont bother with md5 authentication with POLLENTRY */
981 clr_ws_buf(&v12buf
, ap
);
983 n
->n_metric
= ntohl(n
->n_metric
);
985 if (n
->n_family
!= RIP_AF_INET
) {
986 msglim(&bad_router
, FROM_NADDR
,
987 "POLLENTRY request from %s for unsupported"
989 naddr_ntoa(FROM_NADDR
),
991 naddr_ntoa(n
->n_dst
));
995 /* We are being asked about a specific destination. */
996 v12buf
.n
->n_dst
= dst
= n
->n_dst
;
997 v12buf
.n
->n_family
= RIP_AF_INET
;
998 if (!check_dst(dst
)) {
999 msglim(&bad_router
, FROM_NADDR
,
1000 "bad queried destination %s from %s",
1002 naddr_ntoa(FROM_NADDR
));
1003 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
1004 goto pollentry_done
;
1007 /* decide what mask was intended */
1008 if (rip
->rip_vers
== RIPv1
||
1009 0 == (mask
= ntohl(n
->n_mask
)) ||
1010 0 != (ntohl(dst
) & ~mask
))
1011 mask
= ripv1_mask_host(dst
, ifp
);
1013 /* try to find the answer */
1014 rt
= rtget(dst
, mask
);
1015 if (rt
== NULL
&& dst
!= RIP_DEFAULT
)
1016 rt
= rtfind(n
->n_dst
);
1018 if (v12buf
.buf
->rip_vers
!= RIPv1
)
1019 v12buf
.n
->n_mask
= htonl(mask
);
1021 /* we do not have the answer */
1022 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
1023 goto pollentry_done
;
1028 * we have the answer, so compute the right metric and next
1031 v12buf
.n
->n_metric
= rt
->rt_metric
+ 1;
1032 if (v12buf
.n
->n_metric
> HOPCNT_INFINITY
)
1033 v12buf
.n
->n_metric
= HOPCNT_INFINITY
;
1034 if (v12buf
.buf
->rip_vers
!= RIPv1
) {
1035 v12buf
.n
->n_tag
= rt
->rt_tag
;
1037 on_net(rt
->rt_gate
, ifp
->int_net
, ifp
->int_mask
) &&
1038 rt
->rt_gate
!= ifp
->int_addr
)
1039 v12buf
.n
->n_nhop
= rt
->rt_gate
;
1042 v12buf
.n
->n_metric
= htonl(v12buf
.n
->n_metric
);
1045 * Send the answer about specific routes.
1047 (void) output(OUT_QUERY
, from
, ifp
, v12buf
.buf
,
1048 ((char *)v12buf
.n
- (char *)v12buf
.buf
));
1056 * Process a single input route.
1059 input_route(in_addr_t dst
, /* network order */
1061 struct rt_spare
*new,
1066 struct rt_entry
*rt
;
1067 struct rt_spare
*rts
, *rts0
;
1068 struct interface
*ifp1
;
1069 struct rt_spare
*ptr
;
1073 * See if we can already get there by a working interface. Ignore
1076 ifp1
= ifwithaddr(dst
, _B_TRUE
, _B_FALSE
);
1077 if (ifp1
!= NULL
&& (ifp1
->int_state
& IS_PASSIVE
))
1081 * Look for the route in our table.
1083 rt
= rtget(dst
, mask
);
1085 /* Consider adding the route if we do not already have it. */
1087 /* Ignore unknown routes being poisoned. */
1088 if (new->rts_metric
== HOPCNT_INFINITY
)
1091 /* Ignore the route if it points to us */
1092 if (n
!= NULL
&& n
->n_nhop
!= 0 &&
1093 NULL
!= ifwithaddr(n
->n_nhop
, _B_TRUE
, _B_FALSE
))
1097 * If something has not gone crazy and tried to fill
1098 * our memory, accept the new route.
1100 rtadd(dst
, mask
, rt_state
, new);
1105 * We already know about the route. Consider this update.
1107 * If (rt->rt_state & RS_NET_SYN), then this route
1108 * is the same as a network route we have inferred
1109 * for subnets we know, in order to tell RIPv1 routers
1110 * about the subnets.
1112 * It is impossible to tell if the route is coming
1113 * from a distant RIPv2 router with the standard
1114 * netmask because that router knows about the entire
1115 * network, or if it is a round-about echo of a
1116 * synthetic, RIPv1 network route of our own.
1117 * The worst is that both kinds of routes might be
1118 * received, and the bad one might have the smaller
1119 * metric. Partly solve this problem by never
1120 * aggregating into such a route. Also keep it
1121 * around as long as the interface exists.
1124 rts0
= rt
->rt_spares
;
1125 for (rts
= rts0
, i
= rt
->rt_num_spares
; i
!= 0; i
--, rts
++) {
1126 if (rts
->rts_router
== new->rts_router
)
1129 * Note the worst slot to reuse,
1130 * other than the current slot.
1132 if (BETTER_LINK(rt
, rts0
, rts
))
1137 * Found a route from the router already in the table.
1141 * If the new route is a route broken down from an
1142 * aggregated route, and if the previous route is either
1143 * not a broken down route or was broken down from a finer
1144 * netmask, and if the previous route is current,
1145 * then forget this one.
1147 if (new->rts_de_ag
> rts
->rts_de_ag
&&
1148 now_stale
<= rts
->rts_time
)
1152 * Keep poisoned routes around only long enough to pass
1153 * the poison on. Use a new timestamp for good routes.
1155 if (rts
->rts_metric
== HOPCNT_INFINITY
&&
1156 new->rts_metric
== HOPCNT_INFINITY
)
1157 new->rts_time
= rts
->rts_time
;
1160 * If this is an update for the router we currently prefer,
1163 if (i
== rt
->rt_num_spares
) {
1164 uint8_t old_metric
= rts
->rts_metric
;
1166 rtchange(rt
, rt
->rt_state
| rt_state
, new, 0);
1168 * If the route got worse, check for something better.
1170 if (new->rts_metric
!= old_metric
)
1176 * This is an update for a spare route.
1177 * Finished if the route is unchanged.
1179 if (rts
->rts_gate
== new->rts_gate
&&
1180 rts
->rts_metric
== new->rts_metric
&&
1181 rts
->rts_tag
== new->rts_tag
) {
1182 if ((rt
->rt_dst
== RIP_DEFAULT
) &&
1183 (rts
->rts_ifp
!= new->rts_ifp
))
1184 trace_misc("input_route update for spare");
1185 trace_upslot(rt
, rts
, new);
1191 * Forget it if it has gone bad.
1193 if (new->rts_metric
== HOPCNT_INFINITY
) {
1194 rts_delete(rt
, rts
);
1200 * The update is for a route we know about,
1201 * but not from a familiar router.
1203 * Ignore the route if it points to us.
1205 if (n
!= NULL
&& n
->n_nhop
!= 0 &&
1206 NULL
!= ifwithaddr(n
->n_nhop
, _B_TRUE
, _B_FALSE
))
1209 /* the loop above set rts0=worst spare */
1210 if (rts0
->rts_metric
< HOPCNT_INFINITY
) {
1211 ptrsize
= (rt
->rt_num_spares
+ SPARE_INC
) *
1212 sizeof (struct rt_spare
);
1213 ptr
= realloc(rt
->rt_spares
, ptrsize
);
1216 rt
->rt_spares
= ptr
;
1217 rts0
= &rt
->rt_spares
[rt
->rt_num_spares
];
1218 (void) memset(rts0
, 0,
1219 SPARE_INC
* sizeof (struct rt_spare
));
1220 rt
->rt_num_spares
+= SPARE_INC
;
1221 for (rts
= rts0
, i
= SPARE_INC
;
1223 rts
->rts_metric
= HOPCNT_INFINITY
;
1229 * Save the route as a spare only if it has
1230 * a better metric than our worst spare.
1231 * This also ignores poisoned routes (those
1232 * received with metric HOPCNT_INFINITY).
1234 if (new->rts_metric
>= rts
->rts_metric
)
1237 trace_upslot(rt
, rts
, new);
1240 /* try to switch to a better route */
1245 * Recorded information about peer's MD5 sequence numbers. This is
1246 * used to validate that received sequence numbers are in
1247 * non-decreasing order as per the RFC.
1250 struct peer_hash
*ph_next
;
1256 static struct peer_hash
**peer_hashes
;
1257 static int ph_index
;
1258 static int ph_num_peers
;
1261 * Get a peer_hash structure from the hash of known peers. Create a
1262 * new one if not found. Returns NULL on unrecoverable allocation
1265 static struct peer_hash
*
1266 get_peer_info(in_addr_t from
)
1268 struct peer_hash
*php
;
1269 struct peer_hash
*pnhp
;
1270 struct peer_hash
**ph_pp
;
1271 struct peer_hash
**ph2_pp
;
1272 struct peer_hash
**ph3_pp
;
1274 static uint_t failed_count
;
1276 if (peer_hashes
== NULL
) {
1277 peer_hashes
= calloc(hash_table_sizes
[0],
1278 sizeof (peer_hashes
[0]));
1279 if (peer_hashes
== NULL
) {
1280 if (++failed_count
% 100 == 1)
1281 msglog("no memory for peer hash");
1285 /* Search for peer in existing hash table */
1286 ph_pp
= peer_hashes
+ (from
% hash_table_sizes
[ph_index
]);
1287 for (php
= ph_pp
[0]; php
!= NULL
; php
= php
->ph_next
) {
1288 if (php
->ph_addr
== from
)
1292 * Not found; we need to add this peer to the table. If there
1293 * are already too many peers, then try to expand the table
1294 * first. It's not a big deal if we can't expand the table
1295 * right now due to memory constraints. We'll try again
1298 if (ph_num_peers
>= hash_table_sizes
[ph_index
] * 5 &&
1299 hash_table_sizes
[ph_index
+ 1] != 0 &&
1300 (ph_pp
= calloc(hash_table_sizes
[ph_index
+ 1],
1301 sizeof (peer_hashes
[0]))) != NULL
) {
1302 ph2_pp
= peer_hashes
;
1303 for (i
= hash_table_sizes
[ph_index
] - 1; i
>= 0; i
--) {
1304 for (php
= ph2_pp
[i
]; php
!= NULL
; php
= pnhp
) {
1305 pnhp
= php
->ph_next
;
1306 ph3_pp
= ph_pp
+ (php
->ph_addr
%
1307 hash_table_sizes
[ph_index
+ 1]);
1308 php
->ph_next
= ph3_pp
[0];
1314 peer_hashes
= ph_pp
;
1315 ph_pp
+= from
% hash_table_sizes
[ph_index
];
1317 php
= calloc(sizeof (*php
), 1);
1319 if (++failed_count
% 100 == 1)
1320 msglog("no memory for peer hash entry");
1322 php
->ph_addr
= from
;
1323 php
->ph_heard
= now
.tv_sec
;
1324 php
->ph_next
= ph_pp
[0];
1332 * Age out entries in the peer table. This is called every time we do
1333 * a normal 30 second broadcast.
1338 struct peer_hash
*php
;
1339 struct peer_hash
*next_ph
;
1340 struct peer_hash
*prev_ph
;
1341 struct peer_hash
**ph_pp
;
1345 * Scan through the list and remove peers that should not
1346 * still have valid authenticated entries in the routing
1349 if ((ph_pp
= peer_hashes
) == NULL
|| ph_num_peers
== 0)
1351 for (i
= hash_table_sizes
[ph_index
] - 1; i
>= 0; i
--) {
1353 for (php
= ph_pp
[i
]; php
!= NULL
; php
= next_ph
) {
1354 next_ph
= php
->ph_next
;
1355 if (php
->ph_heard
<= now_expire
) {
1356 if (prev_ph
== NULL
)
1359 prev_ph
->ph_next
= next_ph
;
1361 if (--ph_num_peers
== 0)
1370 static boolean_t
/* _B_FALSE if bad, _B_TRUE if good */
1371 ck_passwd(struct interface
*aifp
,
1375 struct msg_limit
*use_authp
)
1377 #define NA (rip->rip_auths)
1378 struct netauth
*na2
;
1381 uchar_t hash
[RIP_AUTH_PW_LEN
];
1383 struct peer_hash
*php
;
1386 if ((uint8_t *)NA
>= lim
|| NA
->a_family
!= RIP_AF_AUTH
) {
1387 msglim(use_authp
, from
, "missing auth data from %s",
1393 * Validate sequence number on RIPv2 responses using keyed MD5
1394 * authentication per RFC 2082 section 3.2.2. Note that if we
1395 * can't locate the peer information (due to transient
1396 * allocation problems), then we don't do the test. Also note
1397 * that we assume that all sequence numbers 0x80000000 or more
1398 * away are "less than."
1400 * We intentionally violate RFC 2082 with respect to one case:
1401 * restablishing contact. The RFC says that you should
1402 * continue to ignore old sequence numbers in this case but
1403 * make a special allowance for 0. This is extremely foolish.
1404 * The problem is that if the router has crashed, it's
1405 * entirely possible that either we'll miss sequence zero (or
1406 * that it might not even send it!) or that the peer doesn't
1407 * remember what it last used for a sequence number. In
1408 * either case, we'll create a failure state that persists
1409 * until the sequence number happens to advance past the last
1410 * one we saw. This is bad because it means that we may have
1411 * to wait until the router has been up for at least as long
1412 * as it was last time before we even pay attention to it.
1413 * Meanwhile, other routers may listen to it if they hadn't
1414 * seen it before (i.e., if they crashed in the meantime).
1415 * This means -- perversely -- that stable systems that stay
1416 * "up" for a long time pay a penalty for doing so.
1418 if (rip
->rip_cmd
== RIPCMD_RESPONSE
&& NA
->a_type
== RIP_AUTH_MD5
&&
1419 (php
= get_peer_info(from
)) != NULL
) {
1421 * If the entry that we find has been updated
1422 * recently enough that the routes are known
1423 * to still be good, but the sequence number
1424 * looks bad, then discard the packet.
1426 seqno
= ntohl(NA
->au
.a_md5
.md5_seqno
);
1427 if (php
->ph_heard
> now_expire
&& php
->ph_seqno
!= 0 &&
1428 (seqno
== 0 || ((seqno
- php
->ph_seqno
) & 0x80000000ul
))) {
1429 msglim(use_authp
, from
,
1430 "discarding sequence %x (older than %x)",
1431 (unsigned)seqno
, (unsigned)php
->ph_seqno
);
1434 php
->ph_heard
= now
.tv_sec
;
1435 php
->ph_seqno
= seqno
;
1439 * accept any current (+/- 24 hours) password
1441 for (ap
= aifp
->int_auth
, i
= 0; i
< MAX_AUTH_KEYS
; i
++, ap
++) {
1442 if (ap
->type
!= NA
->a_type
||
1443 (ulong_t
)ap
->start
> (ulong_t
)clk
.tv_sec
+DAY
||
1444 (ulong_t
)ap
->end
+DAY
< (ulong_t
)clk
.tv_sec
)
1447 if (NA
->a_type
== RIP_AUTH_PW
) {
1448 if (0 == memcmp(NA
->au
.au_pw
, ap
->key
, RIP_AUTH_PW_LEN
))
1453 * accept MD5 secret with the right key ID
1455 if (NA
->au
.a_md5
.md5_keyid
!= ap
->keyid
)
1458 len
= ntohs(NA
->au
.a_md5
.md5_pkt_len
);
1459 if ((len
- sizeof (*rip
)) % sizeof (*NA
) != 0 ||
1460 len
> (lim
- (uint8_t *)rip
- sizeof (*NA
))) {
1461 msglim(use_authp
, from
,
1462 "wrong MD5 RIPv2 packet length of %d"
1463 " instead of %d from %s",
1464 len
, lim
- (uint8_t *)rip
- sizeof (*NA
),
1468 na2
= (struct netauth
*)(rip
->rip_nets
+
1469 (len
- 4) / sizeof (struct netinfo
));
1472 * Given a good hash value, these are not security
1473 * problems so be generous and accept the routes,
1474 * after complaining.
1477 if (NA
->au
.a_md5
.md5_auth_len
!=
1479 msglim(use_authp
, from
,
1480 "unknown MD5 RIPv2 auth len %#x"
1481 " instead of %#x from %s",
1482 NA
->au
.a_md5
.md5_auth_len
,
1485 if (na2
->a_family
!= RIP_AF_AUTH
)
1486 msglim(use_authp
, from
,
1487 "unknown MD5 RIPv2 family %#x"
1488 " instead of %#x from %s",
1489 na2
->a_family
, RIP_AF_AUTH
,
1491 if (na2
->a_type
!= RIP_AUTH_TRAILER
)
1492 msglim(use_authp
, from
,
1493 "MD5 RIPv2 hash has %#x"
1494 " instead of %#x from %s",
1496 ntohs(RIP_AUTH_TRAILER
),
1502 * len+4 to include auth trailer's family/type in
1505 MD5Update(&md5_ctx
, (uchar_t
*)rip
, len
+ 4);
1506 MD5Update(&md5_ctx
, ap
->key
, RIP_AUTH_MD5_LEN
);
1507 MD5Final(hash
, &md5_ctx
);
1508 if (0 == memcmp(hash
, na2
->au
.au_pw
, sizeof (hash
)))
1513 msglim(use_authp
, from
, "bad auth data from %s",