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