]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/ddp_rtmp.c
xnu-344.34.tar.gz
[apple/xnu.git] / bsd / netat / ddp_rtmp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1993-1998 Apple Computer, Inc.
24 * All Rights Reserved.
25 */
26
27 /*
28 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
29 */
30
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <machine/spl.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/filedesc.h>
39 #include <sys/fcntl.h>
40 #include <sys/mbuf.h>
41 #include <sys/ioctl.h>
42 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45
46 #include <net/if.h>
47
48 #include <netat/sysglue.h>
49 #include <netat/appletalk.h>
50 #include <netat/at_var.h>
51 #include <netat/ddp.h>
52 #include <netat/rtmp.h>
53 #include <netat/zip.h>
54 #include <netat/routing_tables.h>
55 #include <netat/debug.h>
56 #include <netat/at_pcb.h>
57
58 #include <sys/kern_event.h>
59
60 extern void rtmp_router_input();
61
62 /****************************************************************/
63 /* */
64 /* */
65 /* RTMP Protocol */
66 /* */
67 /* */
68 /****************************************************************/
69
70
71 /* rtmp.c: , 1.6; 2/26/93; Apple Computer, Inc." */
72
73
74 #define NROUTERS2TRAK 8
75 #define FIFTYSECS 10
76 #define NODE(r) ((r)->ifARouter.s_node)
77 #define NET(r) ((r)->ifARouter.s_net)
78 #define INUSE(r) (NODE(r))
79
80 void ddp_age_router();
81
82 static struct routerinfo {
83 struct at_addr ifARouter;
84 at_ifaddr_t *ifID;
85 void *tmo;
86 } trackedrouters[NROUTERS2TRAK];
87
88 void trackrouter_rem_if(ifID)
89 register at_ifaddr_t *ifID;
90 {
91 register i;
92 register struct routerinfo *router;
93
94 for (i = NROUTERS2TRAK; --i >= 0;) {
95 router = &trackedrouters[i];
96 if (trackedrouters[i].ifID == ifID) {
97 untimeout(ddp_age_router, (caddr_t)router);
98 break;
99 }
100 }
101 }
102
103
104 void routershutdown()
105 {
106 register i;
107
108 for (i = NROUTERS2TRAK; --i >= 0;) {
109 register struct routerinfo *router;
110
111 router = &trackedrouters[i];
112 if (INUSE(router)) {
113 untimeout(ddp_age_router, (caddr_t) router);
114 bzero((caddr_t) router, sizeof(struct routerinfo));
115 }
116 }
117 }
118
119 int router_added = 0;
120 int router_killed = 0;
121
122
123
124 void trackrouter(ifID, net, node)
125 register at_ifaddr_t *ifID;
126 register unsigned short net;
127 register unsigned char node;
128 {
129 register struct routerinfo *unused = NULL;
130 register i;
131
132 for (i = NROUTERS2TRAK; --i >= 0;) {
133 register struct routerinfo *router;
134
135 router = &trackedrouters[(i + node) & (NROUTERS2TRAK-1)];
136 if ((NODE(router) == node) && (NET(router) == net)) {
137 untimeout(ddp_age_router, (caddr_t) router);
138 timeout(ddp_age_router, (caddr_t) router, 50*SYS_HZ);
139 unused = NULL;
140 break;
141 }
142 else if (!INUSE(router) && !unused)
143 unused = router;
144 }
145 if (unused) {
146 router_added++;
147
148 if (ifID->ifARouter.s_net == 0) {
149 /* Send event that this interface just got a router. This does not
150 discriminate on whether this router is valid or not. If it is not
151 valid rtmp_input will send a KEV_ATALK_ROUTERUP_INVALID event. */
152 atalk_post_msg(ifID->aa_ifp, KEV_ATALK_ROUTERUP, 0, 0);
153 }
154
155 unused->ifID = ifID;
156 NET(unused) = net;
157 NODE(unused) = node;
158 ifID->ifRouterState = ROUTER_AROUND;
159 timeout(ddp_age_router, (caddr_t) unused, 50*SYS_HZ);
160
161 if (NET(ifID) == 0 && NODE(ifID) == 0) {
162 NET(ifID) = net;
163 NODE(ifID) = node;
164 }
165 }
166 }
167
168 /*
169 * This is the timeout function that is called after 50 seconds,
170 * if no router packets come in. That way we won't send extended
171 * frames to something that is not there. Untimeout is called if
172 * an RTMP packet comes in so this routine will not be called.
173 */
174 void ddp_age_router(deadrouter)
175 register struct routerinfo *deadrouter;
176 {
177 register at_ifaddr_t *ourrouter = deadrouter->ifID;
178 boolean_t funnel_state;
179
180 funnel_state = thread_funnel_set(network_flock, TRUE);
181
182 dPrintf(D_M_RTMP, D_L_INFO,
183 ("ddp_age_router called deadrouter=%d:%d\n", NODE(deadrouter), NET(deadrouter)));
184
185 router_killed++;
186
187 if (NODE(ourrouter) == NODE(deadrouter) &&
188 NET(ourrouter) == NET(deadrouter)) {
189 register unsigned long atrandom = random();
190 register struct routerinfo *newrouter;
191 register i;
192
193 bzero((caddr_t) deadrouter, sizeof(struct routerinfo));
194 for (i = NROUTERS2TRAK; --i >= 0;) {
195 newrouter = &trackedrouters[(i + atrandom) & (NROUTERS2TRAK-1)];
196 if (INUSE(newrouter))
197 break;
198 else
199 newrouter = NULL;
200 }
201 if (newrouter) {
202 /* Set our router to another on the list and go on with life */
203 NET(ourrouter) = NET(newrouter);
204 NODE(ourrouter) = NODE(newrouter);
205 }
206 else {
207 /* from gorouterless() */
208 /* We have no other routers. */
209 ATTRACE(AT_MID_DDP, AT_SID_TIMERS, AT_LV_WARNING, FALSE,
210 "ddp_age_router entry : ARouter = 0x%x, RouterState = 0x%x",
211 ATALK_VALUE(ourrouter->ifARouter), ourrouter->ifRouterState, 0);
212
213 switch (ourrouter->ifRouterState) {
214 case ROUTER_AROUND :
215 /* This is where we lose our cable.
216 Reset router fields and state accordingly. */
217 ourrouter->ifARouter.s_net = 0;
218 ourrouter->ifARouter.s_node = 0;
219 ourrouter->ifThisCableStart = DDP_MIN_NETWORK;
220 ourrouter->ifThisCableEnd = DDP_MAX_NETWORK;
221 ourrouter->ifRouterState = NO_ROUTER;
222
223 /* Send event to indicate that we've lost our seed router. */
224 atalk_post_msg(ourrouter->aa_ifp, KEV_ATALK_ROUTERDOWN, 0, 0);
225
226 zip_control(ourrouter, ZIP_NO_ROUTER);
227 break;
228 case ROUTER_WARNING :
229 /* there was a router that we were ignoring...
230 * now, even that's gone. But we want to tackle the
231 * case where another router may come up after all
232 * of them have died...
233 */
234 ourrouter->ifRouterState = NO_ROUTER;
235 break;
236 }
237 }
238 } else
239 bzero((caddr_t) deadrouter, sizeof(struct routerinfo));
240
241 (void) thread_funnel_set(network_flock, FALSE);
242
243 } /* ddp_age_router */
244
245 void rtmp_input (mp, ifID)
246 register gbuf_t *mp;
247 register at_ifaddr_t *ifID;
248 {
249 register at_net_al this_net;
250 register at_net_al range_start, range_end;
251 register at_ddp_t *ddp = (at_ddp_t *)gbuf_rptr(mp);
252 /* NOTE: there is an assumption here that the
253 * DATA follows the header. */
254 register at_rtmp *rtmp = (at_rtmp *)ddp->data;
255
256 if (gbuf_type(mp) != MSG_DATA) {
257 /* If this is a M_ERROR message, DDP is shutting down,
258 * nothing to do here...If it's something else, we don't
259 * understand what it is
260 */
261 gbuf_freem(mp);
262 return;
263 }
264
265 if (!ifID) {
266 gbuf_freem(mp);
267 return;
268 }
269 if (gbuf_len(mp) < (DDP_X_HDR_SIZE + sizeof(at_rtmp))) {
270 gbuf_freem(mp);
271 return;
272 }
273 this_net = ifID->ifThisNode.s_net;
274 if (rtmp->at_rtmp_id_length != 8) {
275 gbuf_freem(mp);
276 return;
277 }
278
279 {
280 at_rtmp_tuple *tp;
281 tp = ((at_rtmp_tuple *)&rtmp->at_rtmp_id[1]);
282 range_start = NET_VALUE(tp->at_rtmp_net);
283 tp = ((at_rtmp_tuple *)&rtmp->at_rtmp_id[4]);
284 range_end = NET_VALUE(tp->at_rtmp_net);
285
286 if (ifID->ifRouterState == ROUTER_AROUND) {
287 if ((ifID->ifThisCableStart == range_start) &&
288 (ifID->ifThisCableEnd == range_end)) {
289 trackrouter(ifID,
290 NET_VALUE(rtmp->at_rtmp_this_net),
291 rtmp->at_rtmp_id[0]
292 );
293 }
294 } else {
295 /* There was no router around earlier, one
296 * probably just came up.
297 */
298 if ((this_net >= DDP_STARTUP_LOW) &&
299 (this_net <= DDP_STARTUP_HIGH)) {
300 /* we're operating in the startup range,
301 * ignore the presence of router
302 */
303 if (ifID->ifRouterState == NO_ROUTER) {
304 dPrintf(D_M_RTMP, D_L_INFO, ("rtmp_input: new router came up, INVALID: net \
305 in startup range.\n"));
306 /* trackrouter sends a KEV_ATALK_ROUTERUP event to note that
307 a new router has come up when we had none before. */
308 trackrouter(ifID,
309 NET_VALUE(rtmp->at_rtmp_this_net),
310 rtmp->at_rtmp_id[0]
311 );
312 ifID->ifRouterState = ROUTER_WARNING;
313
314 /* This router is invalid. Send event. */
315 atalk_post_msg(ifID->aa_ifp, KEV_ATALK_ROUTERUP_INVALID, 0, 0);
316 }
317 } else {
318 /* our address
319 * is not in startup range; Is our
320 * address good for the cable??
321 */
322 if ((this_net >= range_start) &&
323 (this_net <= range_end)) {
324 /* Our address is in the range
325 * valid for this cable... Note
326 * the router address and then
327 * get ZIP rolling to get the
328 * zone info.
329 */
330 ifID->ifThisCableStart = range_start;
331 ifID->ifThisCableEnd = range_end;
332
333 /* A seed router that gives us back our cable range came up.
334 It's a valid router and gives us our network back. */
335 atalk_post_msg(ifID->aa_ifp, KEV_ATALK_ROUTERUP, 0, 0);
336
337 trackrouter(ifID,
338 NET_VALUE(rtmp->at_rtmp_this_net),
339 rtmp->at_rtmp_id[0]
340 );
341 zip_control(ifID, ZIP_LATE_ROUTER);
342 } else {
343 /* Our address is not in the
344 * range valid for this cable..
345 * ignore presence of the
346 * router
347 */
348 if (ifID->ifRouterState == NO_ROUTER) {
349 /* trackrouter sends a KEV_ATALK_ROUTERUP event to note that
350 a new router has come up when we had none before. */
351 trackrouter(ifID,
352 NET_VALUE(rtmp->at_rtmp_this_net),
353 rtmp->at_rtmp_id[0]
354 );
355 ifID->ifRouterState = ROUTER_WARNING;
356
357 /* A new seed router came up, but the cable range is different
358 than what we had before. */
359 atalk_post_msg(ifID->aa_ifp, KEV_ATALK_ROUTERUP_INVALID, 0, 0);
360 }
361 }
362 }
363 }
364 }
365
366 gbuf_freem(mp);
367 return;
368 }
369
370
371 void rtmp_init()
372 {
373 bzero((caddr_t)trackedrouters, sizeof(struct routerinfo)*NROUTERS2TRAK);
374 }
375
376