changes to allow subnets to remain local, propogate net route
[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
4fad5a6e 8static char sccsid[] = "@(#)input.c 5.4 (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 &&
17deb420
MK
64 n->rip_metric == HOPCNT_INFINITY && size == 0) {
65 if (supplier || (*afp->af_portmatch)(from) == 0)
4fad5a6e 66 supply(from, 0, 0);
2e74322d
SL
67 return;
68 }
17deb420
MK
69 if (n->rip_dst.sa_family < af_max &&
70 afswitch[n->rip_dst.sa_family].af_hash)
71 rt = rtlookup(&n->rip_dst);
72 else
73 rt = 0;
2e74322d
SL
74 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
75 min(rt->rt_metric+1, HOPCNT_INFINITY);
55d340a4
SL
76 if (msg->rip_vers > 0) {
77 n->rip_dst.sa_family =
78 htons(n->rip_dst.sa_family);
79 n->rip_metric = htonl(n->rip_metric);
80 }
2e74322d
SL
81 n++, newsize += sizeof (struct netinfo);
82 }
83 if (newsize > 0) {
84 msg->rip_cmd = RIPCMD_RESPONSE;
85 newsize += sizeof (int);
4f4bffaa 86 (*afp->af_output)(s, 0, from, newsize);
2e74322d
SL
87 }
88 return;
89
90 case RIPCMD_TRACEON:
91 case RIPCMD_TRACEOFF:
b7e4f8be 92 /* verify message came from a privileged port */
2e74322d
SL
93 if ((*afp->af_portcheck)(from) == 0)
94 return;
95 packet[size] = '\0';
96 if (msg->rip_cmd == RIPCMD_TRACEON)
97 traceon(msg->rip_tracefile);
98 else
99 traceoff();
100 return;
101
102 case RIPCMD_RESPONSE:
103 /* verify message came from a router */
104 if ((*afp->af_portmatch)(from) == 0)
105 return;
106 (*afp->af_canon)(from);
107 /* are we talking to ourselves? */
108 ifp = if_ifwithaddr(from);
109 if (ifp) {
110 rt = rtfind(from);
b7e4f8be 111 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0)
2e74322d
SL
112 addrouteforif(ifp);
113 else
114 rt->rt_timer = 0;
115 return;
116 }
f1e15e15
MK
117 /*
118 * Update timer for interface on which the packet arrived.
119 * If from other end of a point-to-point link that isn't
120 * in the routing tables, (re-)add the route.
121 */
b7e4f8be
MK
122 if ((rt = rtfind(from)) && (rt->rt_state & RTS_INTERFACE))
123 rt->rt_timer = 0;
f1e15e15
MK
124 else if (ifp = if_ifwithdstaddr(from))
125 addrouteforif(ifp);
2e74322d
SL
126 size -= 4 * sizeof (char);
127 n = msg->rip_nets;
128 for (; size > 0; size -= sizeof (struct netinfo), n++) {
129 if (size < sizeof (struct netinfo))
130 break;
55d340a4
SL
131 if (msg->rip_vers > 0) {
132 n->rip_dst.sa_family =
133 ntohs(n->rip_dst.sa_family);
134 n->rip_metric = ntohl(n->rip_metric);
135 }
20df59f1 136 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
d5568f13 137 continue;
17fe297f
MK
138 if (n->rip_dst.sa_family >= af_max ||
139 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
140 (int (*)())0) {
141 syslog(LOG_INFO,
142 "route in unsupported address family (%d), from %s (af %d)\n",
143 n->rip_dst.sa_family,
144 (*afswitch[from->sa_family].af_format)(from),
145 from->sa_family);
ed36994a 146 continue;
17fe297f
MK
147 }
148 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
149 syslog(LOG_DEBUG,
150 "bad host in route from %s (af %d)\n",
151 (*afswitch[from->sa_family].af_format)(from),
152 from->sa_family);
2e74322d 153 continue;
17fe297f 154 }
2e74322d
SL
155 rt = rtlookup(&n->rip_dst);
156 if (rt == 0) {
20df59f1
MK
157 if (n->rip_metric < HOPCNT_INFINITY)
158 rtadd(&n->rip_dst, from, n->rip_metric, 0);
2e74322d
SL
159 continue;
160 }
161
162 /*
38ca17a8
MK
163 * Update if from gateway and different,
164 * shorter, or getting stale and equivalent.
2e74322d 165 */
15e56604 166 if (equal(from, &rt->rt_router)) {
20df59f1
MK
167 if (n->rip_metric == HOPCNT_INFINITY) {
168 rtdelete(rt);
169 continue;
170 }
15e56604
MK
171 if (n->rip_metric != rt->rt_metric)
172 rtchange(rt, from, n->rip_metric);
173 rt->rt_timer = 0;
174 } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
2e74322d
SL
175 (rt->rt_timer > (EXPIRE_TIME/2) &&
176 rt->rt_metric == n->rip_metric)) {
177 rtchange(rt, from, n->rip_metric);
178 rt->rt_timer = 0;
179 }
180 }
181 return;
182 }
183}