fix tests for bogus routers to reject addresses on shared,
[unix-history] / usr / src / sbin / routed / input.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
2e74322d 7#ifndef lint
71786f50 8static char sccsid[] = "@(#)input.c 5.9 (Berkeley) %G%";
5ff67f98 9#endif not lint
2e74322d
SL
10
11/*
12 * Routing Table Management Daemon
13 */
7fe7fe74 14#include "defs.h"
17fe297f 15#include <sys/syslog.h>
2e74322d
SL
16
17/*
18 * Process a newly received packet.
19 */
20rip_input(from, size)
21 struct sockaddr *from;
22 int size;
23{
20df59f1
MK
24 register struct rt_entry *rt;
25 register struct netinfo *n;
26 register struct interface *ifp;
27 struct interface *if_ifwithdstaddr();
2e74322d 28 int newsize;
20df59f1 29 register struct afswitch *afp;
eb39c032 30 static struct sockaddr badfrom;
2e74322d
SL
31
32 ifp = 0;
33 TRACE_INPUT(ifp, from, size);
17fe297f
MK
34 if (from->sa_family >= af_max ||
35 (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
36 syslog(LOG_INFO,
37 "\"from\" address in unsupported address family (%d), cmd %d\n",
38 from->sa_family, msg->rip_cmd);
2e74322d 39 return;
17fe297f 40 }
2e74322d
SL
41 switch (msg->rip_cmd) {
42
43 case RIPCMD_REQUEST:
44 newsize = 0;
45 size -= 4 * sizeof (char);
46 n = msg->rip_nets;
47 while (size > 0) {
48 if (size < sizeof (struct netinfo))
49 break;
50 size -= sizeof (struct netinfo);
51
55d340a4
SL
52 if (msg->rip_vers > 0) {
53 n->rip_dst.sa_family =
54 ntohs(n->rip_dst.sa_family);
55 n->rip_metric = ntohl(n->rip_metric);
56 }
2e74322d
SL
57 /*
58 * A single entry with sa_family == AF_UNSPEC and
59 * metric ``infinity'' means ``all routes''.
17fe297f
MK
60 * We respond to routers only if we are acting
61 * as a supplier, or to anyone other than a router
62 * (eg, query).
2e74322d
SL
63 */
64 if (n->rip_dst.sa_family == AF_UNSPEC &&
17deb420
MK
65 n->rip_metric == HOPCNT_INFINITY && size == 0) {
66 if (supplier || (*afp->af_portmatch)(from) == 0)
4fad5a6e 67 supply(from, 0, 0);
2e74322d
SL
68 return;
69 }
17deb420
MK
70 if (n->rip_dst.sa_family < af_max &&
71 afswitch[n->rip_dst.sa_family].af_hash)
72 rt = rtlookup(&n->rip_dst);
73 else
74 rt = 0;
2e74322d
SL
75 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
76 min(rt->rt_metric+1, HOPCNT_INFINITY);
55d340a4
SL
77 if (msg->rip_vers > 0) {
78 n->rip_dst.sa_family =
79 htons(n->rip_dst.sa_family);
80 n->rip_metric = htonl(n->rip_metric);
81 }
2e74322d
SL
82 n++, newsize += sizeof (struct netinfo);
83 }
84 if (newsize > 0) {
85 msg->rip_cmd = RIPCMD_RESPONSE;
86 newsize += sizeof (int);
4f4bffaa 87 (*afp->af_output)(s, 0, from, newsize);
2e74322d
SL
88 }
89 return;
90
91 case RIPCMD_TRACEON:
92 case RIPCMD_TRACEOFF:
b7e4f8be 93 /* verify message came from a privileged port */
2e74322d
SL
94 if ((*afp->af_portcheck)(from) == 0)
95 return;
71786f50
MK
96 if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
97 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0) {
09069ad0 98 syslog(LOG_ERR, "trace command from unknown router, %s",
eb39c032 99 (*afswitch[from->sa_family].af_format)(from));
09069ad0
MK
100 return;
101 }
2e74322d
SL
102 packet[size] = '\0';
103 if (msg->rip_cmd == RIPCMD_TRACEON)
104 traceon(msg->rip_tracefile);
105 else
106 traceoff();
107 return;
108
109 case RIPCMD_RESPONSE:
110 /* verify message came from a router */
111 if ((*afp->af_portmatch)(from) == 0)
112 return;
113 (*afp->af_canon)(from);
114 /* are we talking to ourselves? */
115 ifp = if_ifwithaddr(from);
116 if (ifp) {
117 rt = rtfind(from);
b7e4f8be 118 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0)
2e74322d
SL
119 addrouteforif(ifp);
120 else
121 rt->rt_timer = 0;
122 return;
123 }
f1e15e15
MK
124 /*
125 * Update timer for interface on which the packet arrived.
126 * If from other end of a point-to-point link that isn't
127 * in the routing tables, (re-)add the route.
128 */
09069ad0
MK
129 if ((rt = rtfind(from)) &&
130 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
b7e4f8be 131 rt->rt_timer = 0;
f1e15e15
MK
132 else if (ifp = if_ifwithdstaddr(from))
133 addrouteforif(ifp);
71786f50
MK
134 /*
135 * "Authenticate" router from which message originated.
136 * We accept routing packets from routers directly connected
137 * via broadcast or point-to-point networks,
138 * and from those listed in /etc/gateways.
139 */
140 if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
141 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0) {
eb39c032
MK
142 if (bcmp((char *)from, (char *)&badfrom,
143 sizeof(badfrom)) != 0) {
144 syslog(LOG_ERR,
145 "packet from unknown router, %s",
146 (*afswitch[from->sa_family].af_format)(from));
147 badfrom = *from;
148 }
09069ad0
MK
149 return;
150 }
2e74322d
SL
151 size -= 4 * sizeof (char);
152 n = msg->rip_nets;
153 for (; size > 0; size -= sizeof (struct netinfo), n++) {
154 if (size < sizeof (struct netinfo))
155 break;
55d340a4
SL
156 if (msg->rip_vers > 0) {
157 n->rip_dst.sa_family =
158 ntohs(n->rip_dst.sa_family);
159 n->rip_metric = ntohl(n->rip_metric);
160 }
20df59f1 161 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
d5568f13 162 continue;
17fe297f
MK
163 if (n->rip_dst.sa_family >= af_max ||
164 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
165 (int (*)())0) {
166 syslog(LOG_INFO,
167 "route in unsupported address family (%d), from %s (af %d)\n",
168 n->rip_dst.sa_family,
169 (*afswitch[from->sa_family].af_format)(from),
170 from->sa_family);
ed36994a 171 continue;
17fe297f
MK
172 }
173 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
174 syslog(LOG_DEBUG,
175 "bad host in route from %s (af %d)\n",
176 (*afswitch[from->sa_family].af_format)(from),
177 from->sa_family);
2e74322d 178 continue;
17fe297f 179 }
2e74322d 180 rt = rtlookup(&n->rip_dst);
97cb3618
MK
181 if (rt == 0 ||
182 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
183 (RTS_INTERNAL|RTS_INTERFACE)) {
09069ad0
MK
184 rt = rtfind(&n->rip_dst);
185 if (rt && equal(from, &rt->rt_router) &&
186 rt->rt_metric == n->rip_metric)
187 continue;
20df59f1
MK
188 if (n->rip_metric < HOPCNT_INFINITY)
189 rtadd(&n->rip_dst, from, n->rip_metric, 0);
2e74322d
SL
190 continue;
191 }
192
193 /*
38ca17a8
MK
194 * Update if from gateway and different,
195 * shorter, or getting stale and equivalent.
2e74322d 196 */
15e56604
MK
197 if (equal(from, &rt->rt_router)) {
198 if (n->rip_metric != rt->rt_metric)
199 rtchange(rt, from, n->rip_metric);
585e6328
MK
200 if (rt->rt_metric < HOPCNT_INFINITY)
201 rt->rt_timer = 0;
15e56604 202 } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
2e74322d
SL
203 (rt->rt_timer > (EXPIRE_TIME/2) &&
204 rt->rt_metric == n->rip_metric)) {
205 rtchange(rt, from, n->rip_metric);
206 rt->rt_timer = 0;
207 }
208 }
209 return;
210 }
211}