rework interface metrics to invert perspective; other misc. fixes
[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
7892134c 8static char sccsid[] = "@(#)input.c 5.12 (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);
7892134c
MK
118 if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
119 rt->rt_metric >= ifp->int_metric)
2e74322d
SL
120 addrouteforif(ifp);
121 else
122 rt->rt_timer = 0;
123 return;
124 }
f1e15e15
MK
125 /*
126 * Update timer for interface on which the packet arrived.
127 * If from other end of a point-to-point link that isn't
128 * in the routing tables, (re-)add the route.
129 */
09069ad0
MK
130 if ((rt = rtfind(from)) &&
131 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
b7e4f8be 132 rt->rt_timer = 0;
7892134c
MK
133 else if ((ifp = if_ifwithdstaddr(from)) &&
134 (rt == 0 || rt->rt_metric >= ifp->int_metric))
f1e15e15 135 addrouteforif(ifp);
71786f50
MK
136 /*
137 * "Authenticate" router from which message originated.
138 * We accept routing packets from routers directly connected
139 * via broadcast or point-to-point networks,
140 * and from those listed in /etc/gateways.
141 */
142 if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
143 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0) {
eb39c032
MK
144 if (bcmp((char *)from, (char *)&badfrom,
145 sizeof(badfrom)) != 0) {
146 syslog(LOG_ERR,
147 "packet from unknown router, %s",
148 (*afswitch[from->sa_family].af_format)(from));
149 badfrom = *from;
150 }
09069ad0
MK
151 return;
152 }
2e74322d
SL
153 size -= 4 * sizeof (char);
154 n = msg->rip_nets;
155 for (; size > 0; size -= sizeof (struct netinfo), n++) {
156 if (size < sizeof (struct netinfo))
157 break;
55d340a4
SL
158 if (msg->rip_vers > 0) {
159 n->rip_dst.sa_family =
160 ntohs(n->rip_dst.sa_family);
161 n->rip_metric = ntohl(n->rip_metric);
162 }
20df59f1 163 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
d5568f13 164 continue;
17fe297f
MK
165 if (n->rip_dst.sa_family >= af_max ||
166 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
167 (int (*)())0) {
168 syslog(LOG_INFO,
169 "route in unsupported address family (%d), from %s (af %d)\n",
170 n->rip_dst.sa_family,
171 (*afswitch[from->sa_family].af_format)(from),
172 from->sa_family);
ed36994a 173 continue;
17fe297f
MK
174 }
175 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
176 syslog(LOG_DEBUG,
177 "bad host in route from %s (af %d)\n",
178 (*afswitch[from->sa_family].af_format)(from),
179 from->sa_family);
2e74322d 180 continue;
17fe297f 181 }
7892134c
MK
182 /*
183 * Adjust metric according to incoming interface.
184 */
185 if ((unsigned)n->rip_metric < HOPCNT_INFINITY)
186 n->rip_metric += ifp->int_metric;
2e74322d 187 rt = rtlookup(&n->rip_dst);
97cb3618
MK
188 if (rt == 0 ||
189 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
190 (RTS_INTERNAL|RTS_INTERFACE)) {
7892134c
MK
191 /*
192 * If we're hearing a logical network route
193 * back from a peer to which we sent it,
194 * ignore it.
195 */
196 if (rt && rt->rt_state & RTS_SUBNET &&
197 (*afp->af_sendroute)(rt, from))
198 continue;
199 /*
200 * Look for an equivalent route that includes
201 * this one before adding this route.
202 */
09069ad0 203 rt = rtfind(&n->rip_dst);
7892134c 204 if (rt && equal(from, &rt->rt_router))
09069ad0 205 continue;
20df59f1
MK
206 if (n->rip_metric < HOPCNT_INFINITY)
207 rtadd(&n->rip_dst, from, n->rip_metric, 0);
2e74322d
SL
208 continue;
209 }
210
211 /*
38ca17a8
MK
212 * Update if from gateway and different,
213 * shorter, or getting stale and equivalent.
2e74322d 214 */
15e56604 215 if (equal(from, &rt->rt_router)) {
88709531 216 if (n->rip_metric != rt->rt_metric) {
15e56604 217 rtchange(rt, from, n->rip_metric);
7892134c 218 if (rt->rt_metric >= HOPCNT_INFINITY)
88709531
MK
219 rt->rt_timer =
220 GARBAGE_TIME - EXPIRE_TIME;
221 } else if (rt->rt_metric < HOPCNT_INFINITY)
585e6328 222 rt->rt_timer = 0;
15e56604 223 } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
2e74322d 224 (rt->rt_timer > (EXPIRE_TIME/2) &&
7892134c
MK
225 rt->rt_metric == n->rip_metric &&
226 n->rip_metric < HOPCNT_INFINITY)) {
2e74322d
SL
227 rtchange(rt, from, n->rip_metric);
228 rt->rt_timer = 0;
229 }
230 }
231 return;
232 }
233}