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