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