ifnet-flags-20060118
[openafs.git] / src / rx / rx_kcommon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * rx_kcommon.c - Common kernel RX code for all system types.
12  */
13
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17 RCSID
18     ("$Header$");
19
20 #include "rx/rx_kcommon.h"
21
22 #ifdef AFS_HPUX110_ENV
23 #include "h/tihdr.h"
24 #include <xti.h>
25 #endif
26 #include "afsint.h"
27
28 #ifndef RXK_LISTENER_ENV
29 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize);   /* set to packet allocation procedure */
30 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
31 #endif
32
33 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
34 extern struct interfaceAddr afs_cb_interface;
35
36 rxk_ports_t rxk_ports;
37 rxk_portRocks_t rxk_portRocks;
38
39 int rxk_initDone = 0;
40
41 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
42 #define ADDRSPERSITE 16
43 static afs_uint32 myNetAddrs[ADDRSPERSITE];
44 static int myNetMTUs[ADDRSPERSITE];
45 static int numMyNetAddrs = 0;
46 #endif
47
48 #if defined(AFS_DARWIN80_ENV)
49 #define sobind sock_bind
50 #define soclose sock_close
51 #endif
52
53 /* add a port to the monitored list, port # is in network order */
54 static int
55 rxk_AddPort(u_short aport, char *arock)
56 {
57     int i;
58     unsigned short *tsp, ts;
59     int zslot;
60
61     zslot = -1;                 /* look for an empty slot simultaneously */
62     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
63         if (((ts = *tsp) == 0) && (zslot == -1))
64             zslot = i;
65         if (ts == aport) {
66             return 0;
67         }
68     }
69     /* otherwise allocate a new port slot */
70     if (zslot < 0)
71         return E2BIG;           /* all full */
72     rxk_ports[zslot] = aport;
73     rxk_portRocks[zslot] = arock;
74     return 0;
75 }
76
77 /* remove as port from the monitored list, port # is in network order */
78 int
79 rxk_DelPort(u_short aport)
80 {
81     register int i;
82     register unsigned short *tsp;
83
84     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
85         if (*tsp == aport) {
86             /* found it, adjust ref count and free the port reference if all gone */
87             *tsp = 0;
88             return 0;
89         }
90     }
91     /* otherwise port not found */
92     return ENOENT;
93 }
94
95 void
96 rxk_shutdownPorts(void)
97 {
98     int i;
99     for (i = 0; i < MAXRXPORTS; i++) {
100         if (rxk_ports[i]) {
101             rxk_ports[i] = 0;
102 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
103             soclose((struct socket *)rxk_portRocks[i]);
104 #endif
105             rxk_portRocks[i] = NULL;
106         }
107     }
108 }
109
110 osi_socket
111 rxi_GetHostUDPSocket(u_int host, u_short port)
112 {
113     osi_socket *sockp;
114     sockp = (osi_socket *)rxk_NewSocketHost(host, port);
115     if (sockp == (osi_socket *)0)
116         return OSI_NULLSOCKET;
117     rxk_AddPort(port, (char *)sockp);
118     return (osi_socket) sockp;
119 }
120
121 osi_socket
122 rxi_GetUDPSocket(u_short port)
123 {
124     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
125 }
126
127 void
128 osi_Panic(msg, a1, a2, a3)
129      char *msg;
130 {
131     if (!msg)
132         msg = "Unknown AFS panic";
133
134     dpf((msg, a1, a2, a3));
135 #ifdef AFS_LINUX24_ENV
136     printk("AFS BUG at %s\n", msg); 
137     * ((char *) 0) = 0; 
138 #else
139     panic(msg);
140 #endif
141 }
142
143 /*
144  * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
145  * unsigned long value into the given buffer.  Returns 0 on success,
146  * and a value less than 0 on failure.  The contents of the buffer is
147  * defined only on success.
148  */
149
150 int
151 osi_utoa(char *buf, size_t len, unsigned long val)
152 {
153     long k;                     /* index of first byte of string value */
154
155     /* we definitely need room for at least one digit and NUL */
156
157     if (len < 2) {
158         return -1;
159     }
160
161     /* compute the string form from the high end of the buffer */
162
163     buf[len - 1] = '\0';
164     for (k = len - 2; k >= 0; k--) {
165         buf[k] = val % 10 + '0';
166         val /= 10;
167
168         if (val == 0)
169             break;
170     }
171
172     /* did we finish converting val to string form? */
173
174     if (val != 0) {
175         return -2;
176     }
177
178     /* this should never happen */
179
180     if (k < 0) {
181         return -3;
182     }
183
184     /* this should never happen */
185
186     if (k >= len) {
187         return -4;
188     }
189
190     /* if necessary, relocate string to beginning of buf[] */
191
192     if (k > 0) {
193
194         /*
195          * We need to achieve the effect of calling
196          *
197          * memmove(buf, &buf[k], len - k);
198          *
199          * However, since memmove() is not available in all
200          * kernels, we explicitly do an appropriate copy.
201          */
202
203         char *dst = buf;
204         char *src = buf + k;
205
206         while ((*dst++ = *src++) != '\0')
207             continue;
208     }
209
210     return 0;
211 }
212
213 /*
214  * osi_AssertFailK() -- used by the osi_Assert() macro.
215  *
216  * It essentially does
217  *
218  * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
219  *
220  * Since the kernel version of osi_Panic() only passes its first
221  * argument to the native panic(), we construct a single string and hand
222  * that to osi_Panic().
223  */
224 void
225 osi_AssertFailK(const char *expr, const char *file, int line)
226 {
227     static const char msg0[] = "assertion failed: ";
228     static const char msg1[] = ", file: ";
229     static const char msg2[] = ", line: ";
230     static const char msg3[] = "\n";
231
232     /*
233      * These buffers add up to 1K, which is a pleasantly nice round
234      * value, but probably not vital.
235      */
236     char buf[1008];
237     char linebuf[16];
238
239     /* check line number conversion */
240
241     if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
242         osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
243     }
244
245     /* okay, panic */
246
247 #define ADDBUF(BUF, STR)                                        \
248         if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) {    \
249                 strcat(BUF, (char *)(STR));                             \
250         }
251
252     buf[0] = '\0';
253     ADDBUF(buf, msg0);
254     ADDBUF(buf, expr);
255     ADDBUF(buf, msg1);
256     ADDBUF(buf, file);
257     ADDBUF(buf, msg2);
258     ADDBUF(buf, linebuf);
259     ADDBUF(buf, msg3);
260
261 #undef ADDBUF
262
263     osi_Panic(buf);
264 }
265
266 #ifndef UKERNEL
267 /* This is the server process request loop. Kernel server
268  * processes never become listener threads */
269 void
270 rx_ServerProc(void)
271 {
272     int threadID;
273
274     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
275     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
276     /* threadID is used for making decisions in GetCall.  Get it by bumping
277      * number of threads handling incoming calls */
278     threadID = rxi_availProcs++;
279
280 #ifdef RX_ENABLE_LOCKS
281     AFS_GUNLOCK();
282 #endif /* RX_ENABLE_LOCKS */
283     rxi_ServerProc(threadID, NULL, NULL);
284 #ifdef RX_ENABLE_LOCKS
285     AFS_GLOCK();
286 #endif /* RX_ENABLE_LOCKS */
287 }
288 #endif /* !UKERNEL */
289
290 #ifndef RXK_LISTENER_ENV
291 /* asize includes the Rx header */
292 static int
293 MyPacketProc(struct rx_packet **ahandle, int asize)
294 {
295     struct rx_packet *tp;
296
297     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
298     /* If we can't scrounge enough cbufs, then we have to drop the packet,
299      * but we should set a flag so we magic up some more at our leisure.
300      */
301
302     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
303         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
304         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
305             if (0 <
306                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
307                                  RX_PACKET_CLASS_RECV_CBUF)) {
308                 rxi_FreePacket(tp);
309                 tp = NULL;
310                 MUTEX_ENTER(&rx_stats_mutex);
311                 rx_stats.noPacketBuffersOnRead++;
312                 MUTEX_EXIT(&rx_stats_mutex);
313             }
314         }
315     } else {
316         /*
317          * XXX if packet is too long for our buffer,
318          * should do this at a higher layer and let other
319          * end know we're losing.
320          */
321         MUTEX_ENTER(&rx_stats_mutex);
322         rx_stats.bogusPacketOnRead++;
323         MUTEX_EXIT(&rx_stats_mutex);
324         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
325         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
326         tp = NULL;
327     }
328
329     if (!tp)
330         return -1;
331     /* otherwise we have a packet, set appropriate values */
332     *ahandle = tp;
333     return 0;
334 }
335
336 static int
337 MyArrivalProc(struct rx_packet *ahandle,
338               struct sockaddr_in *afrom,
339               struct socket *arock,
340               afs_int32 asize)
341 {
342     /* handle basic rx packet */
343     ahandle->length = asize - RX_HEADER_SIZE;
344     rxi_DecodePacketHeader(ahandle);
345     ahandle =
346         rxi_ReceivePacket(ahandle, arock,
347                           afrom->sin_addr.s_addr, afrom->sin_port, NULL,
348                           NULL);
349
350     /* free the packet if it has been returned */
351     if (ahandle)
352         rxi_FreePacket(ahandle);
353     return 0;
354 }
355 #endif /* !RXK_LISTENER_ENV */
356
357 void
358 rxi_StartListener(void)
359 {
360     /* if kernel, give name of appropriate procedures */
361 #ifndef RXK_LISTENER_ENV
362     rxk_GetPacketProc = MyPacketProc;
363     rxk_PacketArrivalProc = MyArrivalProc;
364     rxk_init();
365 #endif
366 }
367
368 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
369   to get interesting information. */
370 void
371 rxi_InitPeerParams(register struct rx_peer *pp)
372 {
373     u_short rxmtu;
374     afs_int32 i, mtu;
375
376 #ifdef  ADAPT_MTU
377 #ifndef AFS_SUN5_ENV
378 #ifdef AFS_USERSPACE_IP_ADDR
379     i = rxi_Findcbi(pp->host);
380     if (i == -1) {
381         pp->timeout.sec = 3;
382         /* pp->timeout.usec = 0; */
383         pp->ifMTU = RX_REMOTE_PACKET_SIZE;
384     } else {
385         pp->timeout.sec = 2;
386         /* pp->timeout.usec = 0; */
387         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
388     }
389     if (i != -1) {
390         mtu = ntohl(afs_cb_interface.mtu[i]);
391         /* Diminish the packet size to one based on the MTU given by
392          * the interface. */
393         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
394             rxmtu = mtu - RX_IPUDP_SIZE;
395             if (rxmtu < pp->ifMTU)
396                 pp->ifMTU = rxmtu;
397         }
398     } else {                    /* couldn't find the interface, so assume the worst */
399         pp->ifMTU = RX_REMOTE_PACKET_SIZE;
400     }
401 #else /* AFS_USERSPACE_IP_ADDR */
402     AFS_IFNET_T ifn;
403
404 #if !defined(AFS_SGI62_ENV)
405     if (numMyNetAddrs == 0)
406         (void)rxi_GetIFInfo();
407 #endif
408
409     ifn = rxi_FindIfnet(pp->host, NULL);
410     if (ifn) {
411         pp->timeout.sec = 2;
412         /* pp->timeout.usec = 0; */
413         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
414 #ifdef IFF_POINTOPOINT
415         if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
416             /* wish we knew the bit rate and the chunk size, sigh. */
417             pp->timeout.sec = 4;
418             pp->ifMTU = RX_PP_PACKET_SIZE;
419         }
420 #endif /* IFF_POINTOPOINT */
421         /* Diminish the packet size to one based on the MTU given by
422          * the interface. */
423         if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
424             rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
425             if (rxmtu < pp->ifMTU)
426                 pp->ifMTU = rxmtu;
427         }
428     } else {                    /* couldn't find the interface, so assume the worst */
429         pp->timeout.sec = 3;
430         /* pp->timeout.usec = 0; */
431         pp->ifMTU = RX_REMOTE_PACKET_SIZE;
432     }
433 #endif /* else AFS_USERSPACE_IP_ADDR */
434 #else /* AFS_SUN5_ENV */
435     mtu = rxi_FindIfMTU(pp->host);
436
437     if (mtu <= 0) {
438         pp->timeout.sec = 3;
439         /* pp->timeout.usec = 0; */
440         pp->ifMTU = RX_REMOTE_PACKET_SIZE;
441     } else {
442         pp->timeout.sec = 2;
443         /* pp->timeout.usec = 0; */
444         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
445     }
446
447     if (mtu > 0) {
448         /* Diminish the packet size to one based on the MTU given by
449          * the interface. */
450         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
451             rxmtu = mtu - RX_IPUDP_SIZE;
452             if (rxmtu < pp->ifMTU)
453                 pp->ifMTU = rxmtu;
454         }
455     } else {                    /* couldn't find the interface, so assume the worst */
456         pp->ifMTU = RX_REMOTE_PACKET_SIZE;
457     }
458 #endif /* AFS_SUN5_ENV */
459 #else /* ADAPT_MTU */
460     pp->rateFlag = 2;           /* start timing after two full packets */
461     pp->timeout.sec = 2;
462     pp->ifMTU = OLD_MAX_PACKET_SIZE;
463 #endif /* else ADAPT_MTU */
464     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
465     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
466     pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
467     pp->ifDgramPackets =
468         MIN(rxi_nDgramPackets,
469             rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
470     pp->maxDgramPackets = 1;
471
472     /* Initialize slow start parameters */
473     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
474     pp->cwind = 1;
475     pp->nDgramPackets = 1;
476     pp->congestSeq = 0;
477 }
478
479
480 /* The following code is common to several system types, but not all. The
481  * separate ones are found in the system specific subdirectories.
482  */
483
484
485 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
486 /* Routine called during the afsd "-shutdown" process to put things back to
487  * the initial state.
488  */
489 static struct protosw parent_proto;     /* udp proto switch */
490
491 void
492 shutdown_rxkernel(void)
493 {
494     register struct protosw *tpro, *last;
495     last = inetdomain.dom_protoswNPROTOSW;
496     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
497         if (tpro->pr_protocol == IPPROTO_UDP) {
498             /* restore original udp protocol switch */
499             memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
500             memset((void *)&parent_proto, 0, sizeof(parent_proto));
501             rxk_initDone = 0;
502             rxk_shutdownPorts();
503             return;
504         }
505     dpf(("shutdown_rxkernel: no udp proto"));
506 }
507 #endif /* !AIX && !SUN && !NCR  && !UKERNEL */
508
509 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
510 /* Determine what the network interfaces are for this machine. */
511
512 #ifdef AFS_USERSPACE_IP_ADDR
513 int
514 rxi_GetcbiInfo(void)
515 {
516     int i, j, different = 0;
517     int rxmtu, maxmtu;
518     afs_uint32 ifinaddr;
519     afs_uint32 addrs[ADDRSPERSITE];
520     int mtus[ADDRSPERSITE];
521
522     memset((void *)addrs, 0, sizeof(addrs));
523     memset((void *)mtus, 0, sizeof(mtus));
524
525     for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
526         if (!afs_cb_interface.mtu[i])
527             afs_cb_interface.mtu[i] = htonl(1500);
528         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
529         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
530         if (myNetAddrs[i] != ifinaddr)
531             different++;
532
533         mtus[i] = rxmtu;
534         rxmtu = rxi_AdjustIfMTU(rxmtu);
535         maxmtu =
536             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
537         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
538         addrs[i++] = ifinaddr;
539         if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
540             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
541             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
542         }
543     }
544
545     rx_maxJumboRecvSize =
546         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
547         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
548     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
549
550     if (different) {
551         for (j = 0; j < i; j++) {
552             myNetMTUs[j] = mtus[j];
553             myNetAddrs[j] = addrs[j];
554         }
555     }
556     return different;
557 }
558
559
560 /* Returns the afs_cb_interface inxex which best matches address.
561  * If none is found, we return -1.
562  */
563 afs_int32
564 rxi_Findcbi(afs_uint32 addr)
565 {
566     int j;
567     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
568     afs_int32 rvalue = -1;
569     int match_value = 0;
570
571     if (numMyNetAddrs == 0)
572         (void)rxi_GetcbiInfo();
573
574     myAddr = ntohl(addr);
575
576     if (IN_CLASSA(myAddr))
577         netMask = IN_CLASSA_NET;
578     else if (IN_CLASSB(myAddr))
579         netMask = IN_CLASSB_NET;
580     else if (IN_CLASSC(myAddr))
581         netMask = IN_CLASSC_NET;
582     else
583         netMask = 0;
584
585     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
586         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
587         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
588         if ((myAddr & netMask) == (thisAddr & netMask)) {
589             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
590                 if (myAddr == thisAddr) {
591                     match_value = 4;
592                     rvalue = j;
593                     break;
594                 }
595                 if (match_value < 3) {
596                     match_value = 3;
597                     rvalue = j;
598                 }
599             } else {
600                 if (match_value < 2) {
601                     match_value = 2;
602                     rvalue = j;
603                 }
604             }
605         }
606     }
607
608     return (rvalue);
609 }
610
611 #else /* AFS_USERSPACE_IP_ADDR */
612
613 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
614 #define IFADDR2SA(f) (&((f)->ifa_addr))
615 #else /* AFS_AIX41_ENV */
616 #define IFADDR2SA(f) ((f)->ifa_addr)
617 #endif
618
619 int
620 rxi_GetIFInfo(void)
621 {
622     int i = 0;
623     int different = 0;
624
625     register int rxmtu, maxmtu;
626     afs_uint32 addrs[ADDRSPERSITE];
627     int mtus[ADDRSPERSITE];
628     afs_uint32 ifinaddr;
629 #if defined(AFS_DARWIN80_ENV)
630     errno_t t;
631     int cnt=0;
632     ifaddr_t *ifads, ifad;
633     register ifnet_t ifn;
634     struct sockaddr sout;
635     struct sockaddr_in *sin;
636 #else
637     struct ifaddr *ifad;        /* ifnet points to a if_addrlist of ifaddrs */
638     register struct ifnet *ifn;
639 #endif
640
641     memset(addrs, 0, sizeof(addrs));
642     memset(mtus, 0, sizeof(mtus));
643
644 #if defined(AFS_DARWIN80_ENV)
645     t = ifnet_get_address_list_family(NULL, &ifads, AF_INET);
646     if (t == 0) {
647         rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
648         while((ifads[cnt] != NULL) && cnt < ADDRSPERSITE) {
649             t = ifaddr_address(ifads[cnt], &sout, sizeof(sout));
650             sin = (struct sockaddr_in *)&sout;
651             ifinaddr = ntohl(sin->sin_addr.s_addr);
652             if (myNetAddrs[i] != ifinaddr) {
653                 different++;
654             }
655             mtus[i] = rxmtu;
656             rxmtu = rxi_AdjustIfMTU(rxmtu);
657             maxmtu =
658                 rxmtu * rxi_nRecvFrags +
659                 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
660             maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
661             addrs[i++] = ifinaddr;
662             if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
663                 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
664                 rx_maxReceiveSize =
665                     MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
666             }
667             cnt++;
668         }
669         ifnet_free_address_list(ifads);
670     }
671 #else
672 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
673     TAILQ_FOREACH(ifn, &ifnet, if_link) {
674         if (i >= ADDRSPERSITE)
675             break;
676 #elif defined(AFS_OBSD_ENV)
677     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
678          ifn = ifn->if_list.tqe_next) {
679 #else
680     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
681 #endif
682         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
683 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
684         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
685             if (i >= ADDRSPERSITE)
686                 break;
687 #elif defined(AFS_OBSD_ENV)
688         for (ifad = ifn->if_addrlist.tqh_first;
689              ifad != NULL && i < ADDRSPERSITE;
690              ifad = ifad->ifa_list.tqe_next) {
691 #else
692         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
693              ifad = ifad->ifa_next) {
694 #endif
695             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
696                 ifinaddr =
697                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
698                           s_addr);
699                 if (myNetAddrs[i] != ifinaddr) {
700                     different++;
701                 }
702                 mtus[i] = rxmtu;
703                 rxmtu = rxi_AdjustIfMTU(rxmtu);
704                 maxmtu =
705                     rxmtu * rxi_nRecvFrags +
706                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
707                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
708                 addrs[i++] = ifinaddr;
709                 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
710                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
711                     rx_maxReceiveSize =
712                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
713                 }
714             }
715         }
716     }
717 #endif
718
719     rx_maxJumboRecvSize =
720         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
721         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
722     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
723
724     if (different) {
725         int j;
726         for (j = 0; j < i; j++) {
727             myNetMTUs[j] = mtus[j];
728             myNetAddrs[j] = addrs[j];
729         }
730     }
731     return different;
732 }
733
734 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
735 /* Returns ifnet which best matches address */
736 #ifdef AFS_DARWIN80_ENV
737 ifnet_t
738 #else
739 struct ifnet *
740 #endif
741 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
742 {
743     struct sockaddr_in s, sr;
744 #ifdef AFS_DARWIN80_ENV
745     ifaddr_t ifad;
746 #else
747     struct ifaddr *ifad;
748 #endif
749
750     s.sin_family = AF_INET;
751     s.sin_addr.s_addr = addr;
752 #ifdef AFS_DARWIN80_ENV
753     ifad = ifaddr_withnet((struct sockaddr *)&s);
754 #else
755     ifad = ifa_ifwithnet((struct sockaddr *)&s);
756 #endif
757
758 #ifdef AFS_DARWIN80_ENV
759     if (ifad && maskp) {
760         ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
761         *maskp = sr.sin_addr.s_addr;
762     }
763     return (ifad ? ifaddr_ifnet(ifad) : NULL);
764 #else
765     if (ifad && maskp)
766         *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
767     return (ifad ? ifad->ifa_ifp : NULL);
768 #endif
769 }
770
771 #else /* DARWIN60 || XBSD */
772
773 /* Returns ifnet which best matches address */
774 struct ifnet *
775 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
776 {
777     int match_value = 0;
778     extern struct in_ifaddr *in_ifaddr;
779     struct in_ifaddr *ifa, *ifad = NULL;
780
781     addr = ntohl(addr);
782
783 #if defined(AFS_DARWIN_ENV)
784     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
785          ifa = TAILQ_NEXT(ifa, ia_link)) {
786 #else
787     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
788 #endif
789         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
790             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
791                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
792                     match_value = 4;
793                     ifad = ifa;
794                     goto done;
795                 }
796                 if (match_value < 3) {
797                     ifad = ifa;
798                     match_value = 3;
799                 }
800             } else {
801                 if (match_value < 2) {
802                     ifad = ifa;
803                     match_value = 2;
804                 }
805             }
806         }                       /* if net matches */
807     }                           /* for all in_ifaddrs */
808
809   done:
810     if (ifad && maskp)
811         *maskp = ifad->ia_subnetmask;
812     return (ifad ? ifad->ia_ifp : NULL);
813 }
814 #endif /* else DARWIN60 || XBSD */
815 #endif /* else AFS_USERSPACE_IP_ADDR */
816 #endif /* !SUN5 && !SGI62 */
817
818
819 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
820  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
821  * system specific subdirectories for all systems. But for the moment,
822  * most of it is simple to follow common code.
823  */
824 #if !defined(UKERNEL)
825 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
826 /* rxk_NewSocket creates a new socket on the specified port. The port is
827  * in network byte order.
828  */
829 osi_socket *
830 rxk_NewSocketHost(afs_uint32 ahost, short aport)
831 {
832     register afs_int32 code;
833 #ifdef AFS_DARWIN80_ENV
834     socket_t newSocket;
835 #else
836     struct socket *newSocket;
837 #endif
838 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
839     struct mbuf *nam;
840 #endif
841     struct sockaddr_in myaddr;
842 #ifdef AFS_HPUX110_ENV
843     /* prototype copied from kernel source file streams/str_proto.h */
844     extern MBLKP allocb_wait(int, int);
845     MBLKP bindnam;
846     int addrsize = sizeof(struct sockaddr_in);
847     struct file *fp;
848     extern struct fileops socketops;
849 #endif
850 #ifdef AFS_SGI65_ENV
851     bhv_desc_t bhv;
852 #endif
853
854     AFS_STATCNT(osi_NewSocket);
855 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
856     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
857 #endif
858     AFS_ASSERT_GLOCK();
859     AFS_GUNLOCK();
860 #if     defined(AFS_HPUX102_ENV)
861 #if     defined(AFS_HPUX110_ENV)
862     /* we need a file associated with the socket so sosend in NetSend 
863      * will not fail */
864     /* blocking socket */
865     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
866     fp = falloc();
867     if (!fp)
868         goto bad;
869     fp->f_flag = FREAD | FWRITE;
870     fp->f_type = DTYPE_SOCKET;
871     fp->f_ops = &socketops;
872
873     fp->f_data = (void *)newSocket;
874     newSocket->so_fp = (void *)fp;
875
876 #else /* AFS_HPUX110_ENV */
877     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
878 #endif /* else AFS_HPUX110_ENV */
879 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
880     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
881 #elif defined(AFS_FBSD50_ENV)
882     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
883                     afs_osi_credp, curthread);
884 #elif defined(AFS_FBSD40_ENV)
885     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
886 #elif defined(AFS_DARWIN80_ENV)
887     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
888 #else
889     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
890 #endif /* AFS_HPUX102_ENV */
891     if (code)
892         goto bad;
893
894     memset(&myaddr, 0, sizeof myaddr);
895     myaddr.sin_family = AF_INET;
896     myaddr.sin_port = aport;
897     myaddr.sin_addr.s_addr = ahost;
898 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
899     myaddr.sin_len = sizeof(myaddr);
900 #endif
901
902 #ifdef AFS_HPUX110_ENV
903     bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
904     if (!bindnam) {
905         setuerror(ENOBUFS);
906         goto bad;
907     }
908     memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
909            addrsize);
910     bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
911
912     code = sobind(newSocket, bindnam, addrsize);
913     if (code) {
914         soclose(newSocket);
915 #if !defined(AFS_HPUX1122_ENV)
916         m_freem(nam);
917 #endif
918         goto bad;
919     }
920
921     freeb(bindnam);
922 #else /* AFS_HPUX110_ENV */
923 #if defined(AFS_DARWIN80_ENV)
924     { 
925        int buflen = 50000;
926        int i,code2;
927        for (i=0;i<2;i++) {
928            code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
929                                   &buflen, sizeof(buflen));
930            code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
931                                   &buflen, sizeof(buflen));
932            if (!code && !code2)
933                break;
934            if (i == 2)
935               osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
936            buflen = 32766;
937        }
938     }
939 #else
940     code = soreserve(newSocket, 50000, 50000);
941     if (code) {
942         code = soreserve(newSocket, 32766, 32766);
943         if (code)
944             osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
945     }
946 #endif
947 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
948 #if defined(AFS_FBSD50_ENV)
949     code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
950 #elif defined(AFS_FBSD40_ENV)
951     code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
952 #else
953     code = sobind(newSocket, (struct sockaddr *)&myaddr);
954 #endif
955     if (code) {
956         dpf(("sobind fails (%d)\n", (int)code));
957         soclose(newSocket);
958         AFS_GLOCK();
959         goto bad;
960     }
961 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
962 #ifdef  AFS_OSF_ENV
963     nam = m_getclr(M_WAIT, MT_SONAME);
964 #else /* AFS_OSF_ENV */
965     nam = m_get(M_WAIT, MT_SONAME);
966 #endif
967     if (nam == NULL) {
968 #if defined(KERNEL_HAVE_UERROR)
969         setuerror(ENOBUFS);
970 #endif
971         goto bad;
972     }
973     nam->m_len = sizeof(myaddr);
974     memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
975 #ifdef AFS_SGI65_ENV
976     BHV_PDATA(&bhv) = (void *)newSocket;
977     code = sobind(&bhv, nam);
978     m_freem(nam);
979 #else
980     code = sobind(newSocket, nam);
981 #endif
982     if (code) {
983         dpf(("sobind fails (%d)\n", (int)code));
984         soclose(newSocket);
985 #ifndef AFS_SGI65_ENV
986         m_freem(nam);
987 #endif
988         goto bad;
989     }
990 #endif /* else AFS_DARWIN_ENV */
991 #endif /* else AFS_HPUX110_ENV */
992
993     AFS_GLOCK();
994 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
995     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
996 #endif
997     return (osi_socket *)newSocket;
998
999   bad:
1000     AFS_GLOCK();
1001 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1002     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1003 #endif
1004     return (osi_socket *)0;
1005 }
1006
1007 osi_socket *
1008 rxk_NewSocket(short aport)
1009 {
1010     return rxk_NewSocketHost(0, aport);
1011 }
1012
1013 /* free socket allocated by rxk_NewSocket */
1014 int
1015 rxk_FreeSocket(register struct socket *asocket)
1016 {
1017     AFS_STATCNT(osi_FreeSocket);
1018 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1019     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1020 #endif
1021 #ifdef AFS_HPUX110_ENV
1022     if (asocket->so_fp) {
1023         struct file *fp = asocket->so_fp;
1024 #if !defined(AFS_HPUX1123_ENV)
1025         /* 11.23 still has falloc, but not FPENTRYFREE ! 
1026          * so for now if we shutdown, we will waist a file 
1027          * structure */
1028         FPENTRYFREE(fp);
1029         asocket->so_fp = NULL;
1030 #endif
1031     }
1032 #endif /* AFS_HPUX110_ENV */
1033     soclose(asocket);
1034 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1035     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1036 #endif
1037     return 0;
1038 }
1039 #endif /* !SUN5 && !LINUX20 */
1040
1041 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1042 /*
1043  * Run RX event daemon every second (5 times faster than rest of systems)
1044  */
1045 void
1046 afs_rxevent_daemon(void)
1047 {
1048     struct clock temp;
1049     SPLVAR;
1050
1051     while (1) {
1052 #ifdef RX_ENABLE_LOCKS
1053         AFS_GUNLOCK();
1054 #endif /* RX_ENABLE_LOCKS */
1055         NETPRI;
1056         rxevent_RaiseEvents(&temp);
1057         USERPRI;
1058 #ifdef RX_ENABLE_LOCKS
1059         AFS_GLOCK();
1060 #endif /* RX_ENABLE_LOCKS */
1061 #ifdef RX_KERNEL_TRACE
1062         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1063                    "before afs_osi_Wait()");
1064 #endif
1065         afs_osi_Wait(500, NULL, 0);
1066 #ifdef RX_KERNEL_TRACE
1067         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1068                    "after afs_osi_Wait()");
1069 #endif
1070         if (afs_termState == AFSOP_STOP_RXEVENT) {
1071 #ifdef RXK_LISTENER_ENV
1072             afs_termState = AFSOP_STOP_RXK_LISTENER;
1073 #else
1074             afs_termState = AFSOP_STOP_COMPLETE;
1075 #endif
1076             osi_rxWakeup(&afs_termState);
1077             return;
1078         }
1079     }
1080 }
1081 #endif
1082
1083 #ifdef RXK_LISTENER_ENV
1084
1085 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1086 int
1087 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1088 {
1089     int code;
1090     struct sockaddr_in from;
1091     int nbytes;
1092     afs_int32 rlen;
1093     register afs_int32 tlen;
1094     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1095     rx_computelen(p, tlen);
1096     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1097
1098     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1099     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1100                                  * it once in order to avoid races.  */
1101     tlen = rlen - tlen;
1102     if (tlen > 0) {
1103         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1104         if (tlen > 0) {
1105             tlen = rlen - tlen;
1106         } else
1107             tlen = rlen;
1108     } else
1109         tlen = rlen;
1110
1111     /* add some padding to the last iovec, it's just to make sure that the 
1112      * read doesn't return more data than we expect, and is done to get around
1113      * our problems caused by the lack of a length field in the rx header. */
1114     savelen = p->wirevec[p->niovecs - 1].iov_len;
1115     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1116
1117     nbytes = tlen + sizeof(afs_int32);
1118 #ifdef RX_KERNEL_TRACE
1119     if (ICL_SETACTIVE(afs_iclSetp)) {
1120         AFS_GLOCK();
1121         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1122                    "before osi_NetRecive()");
1123         AFS_GUNLOCK();
1124     }
1125 #endif
1126     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1127
1128 #ifdef RX_KERNEL_TRACE
1129     if (ICL_SETACTIVE(afs_iclSetp)) {
1130         AFS_GLOCK();
1131         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1132                    "after osi_NetRecive()");
1133         AFS_GUNLOCK();
1134     }
1135 #endif
1136     /* restore the vec to its correct state */
1137     p->wirevec[p->niovecs - 1].iov_len = savelen;
1138
1139     if (!code) {
1140         p->length = nbytes - RX_HEADER_SIZE;;
1141         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1142             if (nbytes > 0)
1143                 rxi_MorePackets(rx_initSendWindow);
1144             else {
1145                 MUTEX_ENTER(&rx_stats_mutex);
1146                 rx_stats.bogusPacketOnRead++;
1147                 rx_stats.bogusHost = from.sin_addr.s_addr;
1148                 MUTEX_EXIT(&rx_stats_mutex);
1149                 dpf(("B: bogus packet from [%x,%d] nb=%d",
1150                      from.sin_addr.s_addr, from.sin_port, nbytes));
1151             }
1152             return -1;
1153         } else {
1154             /* Extract packet header. */
1155             rxi_DecodePacketHeader(p);
1156
1157             *host = from.sin_addr.s_addr;
1158             *port = from.sin_port;
1159             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1160                 MUTEX_ENTER(&rx_stats_mutex);
1161                 rx_stats.packetsRead[p->header.type - 1]++;
1162                 MUTEX_EXIT(&rx_stats_mutex);
1163             }
1164
1165             /* Free any empty packet buffers at the end of this packet */
1166             rxi_TrimDataBufs(p, 1);
1167
1168             return 0;
1169         }
1170     } else
1171         return code;
1172 }
1173
1174 /* rxk_Listener() 
1175  *
1176  * Listen for packets on socket. This thread is typically started after
1177  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1178  * the start state is set before proceeding.
1179  *
1180  * Note that this thread is outside the AFS global lock for much of
1181  * it's existence.
1182  *
1183  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1184  * we want here. So we need to either block all signals (including SIGKILL
1185  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1186  * OS's socket receive routine returns as a result of a signal.
1187  */
1188 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1189
1190 #ifdef AFS_SUN5_ENV
1191 /*
1192  * Run the listener as a kernel thread.
1193  */
1194 void
1195 rxk_Listener(void)
1196 {
1197     extern id_t syscid;
1198     void rxk_ListenerProc(void);
1199     if (thread_create
1200         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1201          minclsyspri) == NULL)
1202         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1203 }
1204
1205 void
1206 rxk_ListenerProc(void)
1207 #else /* AFS_SUN5_ENV */
1208 void
1209 rxk_Listener(void)
1210 #endif                          /* AFS_SUN5_ENV */
1211 {
1212     struct rx_packet *rxp = NULL;
1213     int code;
1214     int host, port;
1215
1216 #ifdef AFS_LINUX20_ENV
1217     rxk_ListenerPid = current->pid;
1218 #endif
1219 #ifdef AFS_SUN5_ENV
1220     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1221 #endif /* AFS_SUN5_ENV */
1222 #ifdef AFS_XBSD_ENV
1223     rxk_ListenerPid = curproc->p_pid;
1224 #endif /* AFS_FBSD_ENV */
1225 #ifdef AFS_DARWIN80_ENV
1226     rxk_ListenerPid = proc_selfpid();
1227 #elif defined(AFS_DARWIN_ENV)
1228     rxk_ListenerPid = current_proc()->p_pid;
1229 #endif
1230 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1231     AFS_GUNLOCK();
1232 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1233     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1234         if (rxp) {
1235             rxi_RestoreDataBufs(rxp);
1236         } else {
1237             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1238             if (!rxp)
1239                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1240         }
1241         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1242             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1243         }
1244     }
1245
1246 #ifdef RX_ENABLE_LOCKS
1247     AFS_GLOCK();
1248 #endif /* RX_ENABLE_LOCKS */
1249     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1250         afs_termState = AFSOP_STOP_COMPLETE;
1251         osi_rxWakeup(&afs_termState);
1252     }
1253     rxk_ListenerPid = 0;
1254 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1255     osi_rxWakeup(&rxk_ListenerPid);
1256 #endif
1257 #ifdef AFS_SUN5_ENV
1258     AFS_GUNLOCK();
1259 #endif /* AFS_SUN5_ENV */
1260 }
1261
1262 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1263 /* The manner of stopping the rx listener thread may vary. Most unix's should
1264  * be able to call soclose.
1265  */
1266 void
1267 osi_StopListener(void)
1268 {
1269     soclose(rx_socket);
1270 }
1271 #endif
1272 #endif /* RXK_LISTENER_ENV */
1273
1274 #endif /* !NCR && !UKERNEL */