increase size of refer data structures (from muller@nprdc.arpa (Keith Muller))
[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
17fe297f 8static char sccsid[] = "@(#)input.c 5.2 (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;
2e74322d
SL
30
31 ifp = 0;
32 TRACE_INPUT(ifp, from, size);
17fe297f
MK
33 if (from->sa_family >= af_max ||
34 (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
35 syslog(LOG_INFO,
36 "\"from\" address in unsupported address family (%d), cmd %d\n",
37 from->sa_family, msg->rip_cmd);
2e74322d 38 return;
17fe297f 39 }
2e74322d
SL
40 switch (msg->rip_cmd) {
41
42 case RIPCMD_REQUEST:
43 newsize = 0;
44 size -= 4 * sizeof (char);
45 n = msg->rip_nets;
46 while (size > 0) {
47 if (size < sizeof (struct netinfo))
48 break;
49 size -= sizeof (struct netinfo);
50
55d340a4
SL
51 if (msg->rip_vers > 0) {
52 n->rip_dst.sa_family =
53 ntohs(n->rip_dst.sa_family);
54 n->rip_metric = ntohl(n->rip_metric);
55 }
2e74322d
SL
56 /*
57 * A single entry with sa_family == AF_UNSPEC and
58 * metric ``infinity'' means ``all routes''.
17fe297f
MK
59 * We respond to routers only if we are acting
60 * as a supplier, or to anyone other than a router
61 * (eg, query).
2e74322d
SL
62 */
63 if (n->rip_dst.sa_family == AF_UNSPEC &&
17fe297f
MK
64 n->rip_metric == HOPCNT_INFINITY && size == 0 &&
65 (supplier || (*afp->af_portcheck)(from) == 0)) {
2e74322d
SL
66 supply(from, 0, ifp);
67 return;
68 }
69 rt = rtlookup(&n->rip_dst);
70 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
71 min(rt->rt_metric+1, HOPCNT_INFINITY);
55d340a4
SL
72 if (msg->rip_vers > 0) {
73 n->rip_dst.sa_family =
74 htons(n->rip_dst.sa_family);
75 n->rip_metric = htonl(n->rip_metric);
76 }
2e74322d
SL
77 n++, newsize += sizeof (struct netinfo);
78 }
79 if (newsize > 0) {
80 msg->rip_cmd = RIPCMD_RESPONSE;
81 newsize += sizeof (int);
4f4bffaa 82 (*afp->af_output)(s, 0, from, newsize);
2e74322d
SL
83 }
84 return;
85
86 case RIPCMD_TRACEON:
87 case RIPCMD_TRACEOFF:
b7e4f8be 88 /* verify message came from a privileged port */
2e74322d
SL
89 if ((*afp->af_portcheck)(from) == 0)
90 return;
91 packet[size] = '\0';
92 if (msg->rip_cmd == RIPCMD_TRACEON)
93 traceon(msg->rip_tracefile);
94 else
95 traceoff();
96 return;
97
98 case RIPCMD_RESPONSE:
99 /* verify message came from a router */
100 if ((*afp->af_portmatch)(from) == 0)
101 return;
102 (*afp->af_canon)(from);
103 /* are we talking to ourselves? */
104 ifp = if_ifwithaddr(from);
105 if (ifp) {
106 rt = rtfind(from);
b7e4f8be 107 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0)
2e74322d
SL
108 addrouteforif(ifp);
109 else
110 rt->rt_timer = 0;
111 return;
112 }
f1e15e15
MK
113 /*
114 * Update timer for interface on which the packet arrived.
115 * If from other end of a point-to-point link that isn't
116 * in the routing tables, (re-)add the route.
117 */
b7e4f8be
MK
118 if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE))
119 rt->rt_timer = 0;
f1e15e15
MK
120 else if (ifp = if_ifwithdstaddr(from))
121 addrouteforif(ifp);
2e74322d
SL
122 size -= 4 * sizeof (char);
123 n = msg->rip_nets;
124 for (; size > 0; size -= sizeof (struct netinfo), n++) {
125 if (size < sizeof (struct netinfo))
126 break;
55d340a4
SL
127 if (msg->rip_vers > 0) {
128 n->rip_dst.sa_family =
129 ntohs(n->rip_dst.sa_family);
130 n->rip_metric = ntohl(n->rip_metric);
131 }
20df59f1 132 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
d5568f13 133 continue;
17fe297f
MK
134 if (n->rip_dst.sa_family >= af_max ||
135 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
136 (int (*)())0) {
137 syslog(LOG_INFO,
138 "route in unsupported address family (%d), from %s (af %d)\n",
139 n->rip_dst.sa_family,
140 (*afswitch[from->sa_family].af_format)(from),
141 from->sa_family);
ed36994a 142 continue;
17fe297f
MK
143 }
144 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
145 syslog(LOG_DEBUG,
146 "bad host in route from %s (af %d)\n",
147 (*afswitch[from->sa_family].af_format)(from),
148 from->sa_family);
2e74322d 149 continue;
17fe297f 150 }
2e74322d
SL
151 rt = rtlookup(&n->rip_dst);
152 if (rt == 0) {
20df59f1
MK
153 if (n->rip_metric < HOPCNT_INFINITY)
154 rtadd(&n->rip_dst, from, n->rip_metric, 0);
2e74322d
SL
155 continue;
156 }
157
158 /*
38ca17a8
MK
159 * Update if from gateway and different,
160 * shorter, or getting stale and equivalent.
2e74322d 161 */
15e56604 162 if (equal(from, &rt->rt_router)) {
20df59f1
MK
163 if (n->rip_metric == HOPCNT_INFINITY) {
164 rtdelete(rt);
165 continue;
166 }
15e56604
MK
167 if (n->rip_metric != rt->rt_metric)
168 rtchange(rt, from, n->rip_metric);
169 rt->rt_timer = 0;
170 } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
2e74322d
SL
171 (rt->rt_timer > (EXPIRE_TIME/2) &&
172 rt->rt_metric == n->rip_metric)) {
173 rtchange(rt, from, n->rip_metric);
174 rt->rt_timer = 0;
175 }
176 }
177 return;
178 }
179}