try again; can't block subnet 0 with internal "network" 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
97cb3618 8static char sccsid[] = "@(#)input.c 5.6 (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;
09069ad0
MK
95 if (if_iflookup(from) == 0) {
96 syslog(LOG_ERR, "trace command from unknown router, %s",
97 (*afswitch[from->sa_family].af_format)(from));
98 return;
99 }
2e74322d
SL
100 packet[size] = '\0';
101 if (msg->rip_cmd == RIPCMD_TRACEON)
102 traceon(msg->rip_tracefile);
103 else
104 traceoff();
105 return;
106
107 case RIPCMD_RESPONSE:
108 /* verify message came from a router */
109 if ((*afp->af_portmatch)(from) == 0)
110 return;
111 (*afp->af_canon)(from);
112 /* are we talking to ourselves? */
113 ifp = if_ifwithaddr(from);
114 if (ifp) {
115 rt = rtfind(from);
b7e4f8be 116 if (rt == 0 || (rt->rt_state & RTS_INTERFACE) == 0)
2e74322d
SL
117 addrouteforif(ifp);
118 else
119 rt->rt_timer = 0;
120 return;
121 }
f1e15e15
MK
122 /*
123 * Update timer for interface on which the packet arrived.
124 * If from other end of a point-to-point link that isn't
125 * in the routing tables, (re-)add the route.
126 */
09069ad0
MK
127 if ((rt = rtfind(from)) &&
128 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
b7e4f8be 129 rt->rt_timer = 0;
f1e15e15
MK
130 else if (ifp = if_ifwithdstaddr(from))
131 addrouteforif(ifp);
09069ad0
MK
132 else if (if_iflookup(from) == 0) {
133 syslog(LOG_ERR, "packet from unknown router, %s",
134 (*afswitch[from->sa_family].af_format)(from));
135 return;
136 }
2e74322d
SL
137 size -= 4 * sizeof (char);
138 n = msg->rip_nets;
139 for (; size > 0; size -= sizeof (struct netinfo), n++) {
140 if (size < sizeof (struct netinfo))
141 break;
55d340a4
SL
142 if (msg->rip_vers > 0) {
143 n->rip_dst.sa_family =
144 ntohs(n->rip_dst.sa_family);
145 n->rip_metric = ntohl(n->rip_metric);
146 }
20df59f1 147 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
d5568f13 148 continue;
17fe297f
MK
149 if (n->rip_dst.sa_family >= af_max ||
150 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
151 (int (*)())0) {
152 syslog(LOG_INFO,
153 "route in unsupported address family (%d), from %s (af %d)\n",
154 n->rip_dst.sa_family,
155 (*afswitch[from->sa_family].af_format)(from),
156 from->sa_family);
ed36994a 157 continue;
17fe297f
MK
158 }
159 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
160 syslog(LOG_DEBUG,
161 "bad host in route from %s (af %d)\n",
162 (*afswitch[from->sa_family].af_format)(from),
163 from->sa_family);
2e74322d 164 continue;
17fe297f 165 }
2e74322d 166 rt = rtlookup(&n->rip_dst);
97cb3618
MK
167 if (rt == 0 ||
168 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
169 (RTS_INTERNAL|RTS_INTERFACE)) {
09069ad0
MK
170 rt = rtfind(&n->rip_dst);
171 if (rt && equal(from, &rt->rt_router) &&
172 rt->rt_metric == n->rip_metric)
173 continue;
20df59f1
MK
174 if (n->rip_metric < HOPCNT_INFINITY)
175 rtadd(&n->rip_dst, from, n->rip_metric, 0);
2e74322d
SL
176 continue;
177 }
178
179 /*
38ca17a8
MK
180 * Update if from gateway and different,
181 * shorter, or getting stale and equivalent.
2e74322d 182 */
15e56604 183 if (equal(from, &rt->rt_router)) {
20df59f1
MK
184 if (n->rip_metric == HOPCNT_INFINITY) {
185 rtdelete(rt);
186 continue;
187 }
15e56604
MK
188 if (n->rip_metric != rt->rt_metric)
189 rtchange(rt, from, n->rip_metric);
190 rt->rt_timer = 0;
191 } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
2e74322d
SL
192 (rt->rt_timer > (EXPIRE_TIME/2) &&
193 rt->rt_metric == n->rip_metric)) {
194 rtchange(rt, from, n->rip_metric);
195 rt->rt_timer = 0;
196 }
197 }
198 return;
199 }
200}