bringing-rx-into-21st-century-20060504
[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(struct sockaddr_storage *saddr, int salen);
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(struct sockaddr_storage *saddr, int salen)
112 {
113     osi_socket *sockp;
114     sockp = (osi_socket *)rxk_NewSocketHost(saddr, salen);
115     if (sockp == (osi_socket *)0)
116         return OSI_NULLSOCKET;
117     rxk_AddPort(rx_ss2pn(saddr), (char *)sockp);
118     return (osi_socket) sockp;
119 }
120
121 osi_socket
122 rxi_GetUDPSocket(u_short port)
123 {
124     struct sockaddr_storage saddr;
125     struct sockaddr_in *sin = (struct sockaddr_in *) &saddr;
126
127     memset((void *) &saddr, 0, sizeof(saddr));
128
129     sin->sin_family = AF_INET;
130     sin->sin_addr.s_addr = htonl(INADDR_ANY);
131     sin->sin_port = port;
132
133     return rxi_GetHostUDPSocket(&saddr, sizeof(struct sockaddr_in));
134 }
135
136 void
137 osi_Panic(msg, a1, a2, a3)
138      char *msg;
139 {
140     if (!msg)
141         msg = "Unknown AFS panic";
142
143     printf(msg, a1, a2, a3);
144 #ifdef AFS_LINUX24_ENV
145     * ((char *) 0) = 0; 
146 #else
147     panic(msg);
148 #endif
149 }
150
151 /*
152  * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
153  * unsigned long value into the given buffer.  Returns 0 on success,
154  * and a value less than 0 on failure.  The contents of the buffer is
155  * defined only on success.
156  */
157
158 int
159 osi_utoa(char *buf, size_t len, unsigned long val)
160 {
161     long k;                     /* index of first byte of string value */
162
163     /* we definitely need room for at least one digit and NUL */
164
165     if (len < 2) {
166         return -1;
167     }
168
169     /* compute the string form from the high end of the buffer */
170
171     buf[len - 1] = '\0';
172     for (k = len - 2; k >= 0; k--) {
173         buf[k] = val % 10 + '0';
174         val /= 10;
175
176         if (val == 0)
177             break;
178     }
179
180     /* did we finish converting val to string form? */
181
182     if (val != 0) {
183         return -2;
184     }
185
186     /* this should never happen */
187
188     if (k < 0) {
189         return -3;
190     }
191
192     /* this should never happen */
193
194     if (k >= len) {
195         return -4;
196     }
197
198     /* if necessary, relocate string to beginning of buf[] */
199
200     if (k > 0) {
201
202         /*
203          * We need to achieve the effect of calling
204          *
205          * memmove(buf, &buf[k], len - k);
206          *
207          * However, since memmove() is not available in all
208          * kernels, we explicitly do an appropriate copy.
209          */
210
211         char *dst = buf;
212         char *src = buf + k;
213
214         while ((*dst++ = *src++) != '\0')
215             continue;
216     }
217
218     return 0;
219 }
220
221 /*
222  * osi_AssertFailK() -- used by the osi_Assert() macro.
223  *
224  * It essentially does
225  *
226  * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
227  *
228  * Since the kernel version of osi_Panic() only passes its first
229  * argument to the native panic(), we construct a single string and hand
230  * that to osi_Panic().
231  */
232 void
233 osi_AssertFailK(const char *expr, const char *file, int line)
234 {
235     static const char msg0[] = "assertion failed: ";
236     static const char msg1[] = ", file: ";
237     static const char msg2[] = ", line: ";
238     static const char msg3[] = "\n";
239
240     /*
241      * These buffers add up to 1K, which is a pleasantly nice round
242      * value, but probably not vital.
243      */
244     char buf[1008];
245     char linebuf[16];
246
247     /* check line number conversion */
248
249     if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
250         osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
251     }
252
253     /* okay, panic */
254
255 #define ADDBUF(BUF, STR)                                        \
256         if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) {    \
257                 strcat(BUF, (char *)(STR));                             \
258         }
259
260     buf[0] = '\0';
261     ADDBUF(buf, msg0);
262     ADDBUF(buf, expr);
263     ADDBUF(buf, msg1);
264     ADDBUF(buf, file);
265     ADDBUF(buf, msg2);
266     ADDBUF(buf, linebuf);
267     ADDBUF(buf, msg3);
268
269 #undef ADDBUF
270
271     osi_Panic(buf);
272 }
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)
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 #endif /* !UKERNEL */
297
298 #ifndef RXK_LISTENER_ENV
299 /* asize includes the Rx header */
300 static int
301 MyPacketProc(struct rx_packet **ahandle, int asize)
302 {
303     struct rx_packet *tp;
304
305     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
306     /* If we can't scrounge enough cbufs, then we have to drop the packet,
307      * but we should set a flag so we magic up some more at our leisure.
308      */
309
310     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
311         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
312         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
313             if (0 <
314                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
315                                  RX_PACKET_CLASS_RECV_CBUF)) {
316                 rxi_FreePacket(tp);
317                 tp = NULL;
318                 MUTEX_ENTER(&rx_stats_mutex);
319                 rx_stats.noPacketBuffersOnRead++;
320                 MUTEX_EXIT(&rx_stats_mutex);
321             }
322         }
323     } else {
324         /*
325          * XXX if packet is too long for our buffer,
326          * should do this at a higher layer and let other
327          * end know we're losing.
328          */
329         MUTEX_ENTER(&rx_stats_mutex);
330         rx_stats.bogusPacketOnRead++;
331         MUTEX_EXIT(&rx_stats_mutex);
332         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
333         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
334         tp = NULL;
335     }
336
337     if (!tp)
338         return -1;
339     /* otherwise we have a packet, set appropriate values */
340     *ahandle = tp;
341     return 0;
342 }
343
344 static int
345 MyArrivalProc(struct rx_packet *ahandle,
346               struct sockaddr_in *afrom,
347               struct socket *arock,
348               afs_int32 asize)
349 {
350     /* handle basic rx packet */
351     ahandle->length = asize - RX_HEADER_SIZE;
352     rxi_DecodePacketHeader(ahandle);
353     ahandle =
354         rxi_ReceivePacket(ahandle, arock, (struct sockaddr_storage *) afrom,
355                           NULL, 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(rx_HostOf(pp));
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(rx_HostOf(pp), 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(rx_HostOf(pp));
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;
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     for (i = 0; i < afs_cb_interface.numberOfInterfaces; i++) {
533         if (!afs_cb_interface.mtu[i])
534             afs_cb_interface.mtu[i] = htonl(1500);
535         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
536         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
537         if (myNetAddrs[i] != ifinaddr)
538             different++;
539
540         mtus[i] = rxmtu;
541         rxmtu = rxi_AdjustIfMTU(rxmtu);
542         maxmtu =
543             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
544         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
545         addrs[i++] = ifinaddr;
546         if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
547             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
548             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
549         }
550     }
551
552     rx_maxJumboRecvSize =
553         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
554         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
555     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
556
557     if (different) {
558         for (j = 0; j < i; j++) {
559             myNetMTUs[j] = mtus[j];
560             myNetAddrs[j] = addrs[j];
561         }
562     }
563     return different;
564 }
565
566
567 /* Returns the afs_cb_interface inxex which best matches address.
568  * If none is found, we return -1.
569  */
570 afs_int32
571 rxi_Findcbi(afs_uint32 addr)
572 {
573     int j;
574     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
575     afs_int32 rvalue = -1;
576     int match_value = 0;
577
578     if (numMyNetAddrs == 0)
579         (void)rxi_GetcbiInfo();
580
581     myAddr = ntohl(addr);
582
583     if (IN_CLASSA(myAddr))
584         netMask = IN_CLASSA_NET;
585     else if (IN_CLASSB(myAddr))
586         netMask = IN_CLASSB_NET;
587     else if (IN_CLASSC(myAddr))
588         netMask = IN_CLASSC_NET;
589     else
590         netMask = 0;
591
592     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
593         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
594         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
595         if ((myAddr & netMask) == (thisAddr & netMask)) {
596             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
597                 if (myAddr == thisAddr) {
598                     match_value = 4;
599                     rvalue = j;
600                     break;
601                 }
602                 if (match_value < 3) {
603                     match_value = 3;
604                     rvalue = j;
605                 }
606             } else {
607                 if (match_value < 2) {
608                     match_value = 2;
609                     rvalue = j;
610                 }
611             }
612         }
613     }
614
615     return (rvalue);
616 }
617
618 #else /* AFS_USERSPACE_IP_ADDR */
619
620 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
621 #define IFADDR2SA(f) (&((f)->ifa_addr))
622 #else /* AFS_AIX41_ENV */
623 #define IFADDR2SA(f) ((f)->ifa_addr)
624 #endif
625
626 int
627 rxi_GetIFInfo(void)
628 {
629     int i = 0;
630     int different = 0;
631
632     register int rxmtu, maxmtu;
633     afs_uint32 addrs[ADDRSPERSITE];
634     int mtus[ADDRSPERSITE];
635     afs_uint32 ifinaddr;
636 #if defined(AFS_DARWIN80_ENV)
637     errno_t t;
638     int cnt=0;
639     ifaddr_t *ifads, ifad;
640     register ifnet_t ifn;
641     struct sockaddr sout;
642     struct sockaddr_in *sin;
643 #else
644     struct ifaddr *ifad;        /* ifnet points to a if_addrlist of ifaddrs */
645     register struct ifnet *ifn;
646 #endif
647
648     memset(addrs, 0, sizeof(addrs));
649     memset(mtus, 0, sizeof(mtus));
650
651 #if defined(AFS_DARWIN80_ENV)
652     t = ifnet_get_address_list_family(NULL, &ifads, AF_INET);
653     if (t == 0) {
654         rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
655         while((ifads[cnt] != NULL) && cnt < ADDRSPERSITE) {
656             t = ifaddr_address(ifads[cnt], &sout, sizeof(sout));
657             sin = (struct sockaddr_in *)&sout;
658             ifinaddr = ntohl(sin->sin_addr.s_addr);
659             if (myNetAddrs[i] != ifinaddr) {
660                 different++;
661             }
662             mtus[i] = rxmtu;
663             rxmtu = rxi_AdjustIfMTU(rxmtu);
664             maxmtu =
665                 rxmtu * rxi_nRecvFrags +
666                 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
667             maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
668             addrs[i++] = ifinaddr;
669             if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
670                 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
671                 rx_maxReceiveSize =
672                     MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
673             }
674             cnt++;
675         }
676         ifnet_free_address_list(ifads);
677     }
678 #else
679 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
680     TAILQ_FOREACH(ifn, &ifnet, if_link) {
681         if (i >= ADDRSPERSITE)
682             break;
683 #elif defined(AFS_OBSD_ENV)
684     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
685          ifn = ifn->if_list.tqe_next) {
686 #else
687     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
688 #endif
689         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
690 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
691         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
692             if (i >= ADDRSPERSITE)
693                 break;
694 #elif defined(AFS_OBSD_ENV)
695         for (ifad = ifn->if_addrlist.tqh_first;
696              ifad != NULL && i < ADDRSPERSITE;
697              ifad = ifad->ifa_list.tqe_next) {
698 #else
699         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
700              ifad = ifad->ifa_next) {
701 #endif
702             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
703                 ifinaddr =
704                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
705                           s_addr);
706                 if (myNetAddrs[i] != ifinaddr) {
707                     different++;
708                 }
709                 mtus[i] = rxmtu;
710                 rxmtu = rxi_AdjustIfMTU(rxmtu);
711                 maxmtu =
712                     rxmtu * rxi_nRecvFrags +
713                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
714                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
715                 addrs[i++] = ifinaddr;
716                 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
717                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
718                     rx_maxReceiveSize =
719                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
720                 }
721             }
722         }
723     }
724 #endif
725
726     rx_maxJumboRecvSize =
727         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
728         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
729     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
730
731     if (different) {
732         int j;
733         for (j = 0; j < i; j++) {
734             myNetMTUs[j] = mtus[j];
735             myNetAddrs[j] = addrs[j];
736         }
737     }
738     return different;
739 }
740
741 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
742 /* Returns ifnet which best matches address */
743 #ifdef AFS_DARWIN80_ENV
744 ifnet_t
745 #else
746 struct ifnet *
747 #endif
748 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
749 {
750     struct sockaddr_in s, sr;
751 #ifdef AFS_DARWIN80_ENV
752     ifaddr_t ifad;
753 #else
754     struct ifaddr *ifad;
755 #endif
756
757     s.sin_family = AF_INET;
758     s.sin_addr.s_addr = addr;
759 #ifdef AFS_DARWIN80_ENV
760     ifad = ifaddr_withnet((struct sockaddr *)&s);
761 #else
762     ifad = ifa_ifwithnet((struct sockaddr *)&s);
763 #endif
764
765 #ifdef AFS_DARWIN80_ENV
766     if (ifad && maskp) {
767         ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
768         *maskp = sr.sin_addr.s_addr;
769     }
770     return (ifad ? ifaddr_ifnet(ifad) : NULL);
771 #else
772     if (ifad && maskp)
773         *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
774     return (ifad ? ifad->ifa_ifp : NULL);
775 #endif
776 }
777
778 #else /* DARWIN60 || XBSD */
779
780 /* Returns ifnet which best matches address */
781 struct ifnet *
782 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
783 {
784     int match_value = 0;
785     extern struct in_ifaddr *in_ifaddr;
786     struct in_ifaddr *ifa, *ifad = NULL;
787
788     addr = ntohl(addr);
789
790 #if defined(AFS_DARWIN_ENV)
791     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
792          ifa = TAILQ_NEXT(ifa, ia_link)) {
793 #else
794     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
795 #endif
796         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
797             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
798                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
799                     match_value = 4;
800                     ifad = ifa;
801                     goto done;
802                 }
803                 if (match_value < 3) {
804                     ifad = ifa;
805                     match_value = 3;
806                 }
807             } else {
808                 if (match_value < 2) {
809                     ifad = ifa;
810                     match_value = 2;
811                 }
812             }
813         }                       /* if net matches */
814     }                           /* for all in_ifaddrs */
815
816   done:
817     if (ifad && maskp)
818         *maskp = ifad->ia_subnetmask;
819     return (ifad ? ifad->ia_ifp : NULL);
820 }
821 #endif /* else DARWIN60 || XBSD */
822 #endif /* else AFS_USERSPACE_IP_ADDR */
823 #endif /* !SUN5 && !SGI62 */
824
825
826 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
827  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
828  * system specific subdirectories for all systems. But for the moment,
829  * most of it is simple to follow common code.
830  */
831 #if !defined(UKERNEL)
832 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
833 /* rxk_NewSocket creates a new socket on the specified port. The port is
834  * in network byte order.
835  */
836 osi_socket *
837 rxk_NewSocketHost(struct sockaddr_storage *saddr, int salen)
838 {
839     register afs_int32 code;
840 #ifdef AFS_DARWIN80_ENV
841     socket_t newSocket;
842 #else
843     struct socket *newSocket;
844 #endif
845 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
846     struct mbuf *nam;
847 #endif
848 #ifdef AFS_HPUX110_ENV
849     /* prototype copied from kernel source file streams/str_proto.h */
850     extern MBLKP allocb_wait(int, int);
851     MBLKP bindnam;
852     int addrsize = sizeof(struct sockaddr_in);
853     struct file *fp;
854     extern struct fileops socketops;
855 #endif
856 #ifdef AFS_SGI65_ENV
857     bhv_desc_t bhv;
858 #endif
859
860     AFS_STATCNT(osi_NewSocket);
861 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
862     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
863 #endif
864     AFS_ASSERT_GLOCK();
865     AFS_GUNLOCK();
866 #if     defined(AFS_HPUX102_ENV)
867 #if     defined(AFS_HPUX110_ENV)
868     /* we need a file associated with the socket so sosend in NetSend 
869      * will not fail */
870     /* blocking socket */
871     code = socreate(rx_ssfamily(saddr), &newSocket, SOCK_DGRAM, 0, 0);
872     fp = falloc();
873     if (!fp)
874         goto bad;
875     fp->f_flag = FREAD | FWRITE;
876     fp->f_type = DTYPE_SOCKET;
877     fp->f_ops = &socketops;
878
879     fp->f_data = (void *)newSocket;
880     newSocket->so_fp = (void *)fp;
881
882 #else /* AFS_HPUX110_ENV */
883     code = socreate(rx_ssfamilty(saddr), &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
884 #endif /* else AFS_HPUX110_ENV */
885 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
886     code = socreate(rx_ssfamily(saddr), &newSocket, SOCK_DGRAM, IPPROTO_UDP);
887 #elif defined(AFS_FBSD50_ENV)
888     code = socreate(rx_ssfamily(saddr), &newSocket, SOCK_DGRAM, IPPROTO_UDP,
889                     afs_osi_credp, curthread);
890 #elif defined(AFS_FBSD40_ENV)
891     code = socreate(rx_ssfamily(saddr), &newSocket, SOCK_DGRAM, IPPROTO_UDP,
892                     curproc);
893 #elif defined(AFS_DARWIN80_ENV)
894     code = sock_socket(rx_ssfamily(saddr), SOCK_DGRAM, IPPROTO_UDP, NULL, NULL,
895                        &newSocket);
896 #else
897     code = socreate(rx_ssfamily(saddr), &newSocket, SOCK_DGRAM, 0);
898 #endif /* AFS_HPUX102_ENV */
899     if (code)
900         goto bad;
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) addr,
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 *) saddr, curthread);
950 #elif defined(AFS_FBSD40_ENV)
951     code = sobind(newSocket, (struct sockaddr *) saddr, curproc);
952 #else
953     code = sobind(newSocket, (struct sockaddr *) saddr);
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 = salen;
974     memcpy(mtod(nam, caddr_t), saddr, salen);
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     struct sockaddr_storage saddr;
1011     struct sockaddr_in *sin = (struct sockaddr_in *) &saddr;
1012
1013     memset((void *) &saddr, 0, sizeof(saddr));
1014
1015     sin->sin_family = AF_INET;
1016     sin->sin_addr.s_addr = 0;
1017     sin->sin_port = aport;
1018
1019     return rxk_NewSocketHost(&saddr, sizeof(struct sockaddr_in));
1020 }
1021
1022 /* free socket allocated by rxk_NewSocket */
1023 int
1024 rxk_FreeSocket(register struct socket *asocket)
1025 {
1026     AFS_STATCNT(osi_FreeSocket);
1027 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1028     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1029 #endif
1030 #ifdef AFS_HPUX110_ENV
1031     if (asocket->so_fp) {
1032         struct file *fp = asocket->so_fp;
1033 #if !defined(AFS_HPUX1123_ENV)
1034         /* 11.23 still has falloc, but not FPENTRYFREE ! 
1035          * so for now if we shutdown, we will waist a file 
1036          * structure */
1037         FPENTRYFREE(fp);
1038         asocket->so_fp = NULL;
1039 #endif
1040     }
1041 #endif /* AFS_HPUX110_ENV */
1042     soclose(asocket);
1043 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1044     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1045 #endif
1046     return 0;
1047 }
1048 #endif /* !SUN5 && !LINUX20 */
1049
1050 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1051 /*
1052  * Run RX event daemon every second (5 times faster than rest of systems)
1053  */
1054 void
1055 afs_rxevent_daemon(void)
1056 {
1057     struct clock temp;
1058     SPLVAR;
1059
1060     while (1) {
1061 #ifdef RX_ENABLE_LOCKS
1062         AFS_GUNLOCK();
1063 #endif /* RX_ENABLE_LOCKS */
1064         NETPRI;
1065         rxevent_RaiseEvents(&temp);
1066         USERPRI;
1067 #ifdef RX_ENABLE_LOCKS
1068         AFS_GLOCK();
1069 #endif /* RX_ENABLE_LOCKS */
1070 #ifdef RX_KERNEL_TRACE
1071         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1072                    "before afs_osi_Wait()");
1073 #endif
1074         afs_osi_Wait(500, NULL, 0);
1075 #ifdef RX_KERNEL_TRACE
1076         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1077                    "after afs_osi_Wait()");
1078 #endif
1079         if (afs_termState == AFSOP_STOP_RXEVENT) {
1080 #ifdef RXK_LISTENER_ENV
1081             afs_termState = AFSOP_STOP_RXK_LISTENER;
1082 #else
1083             afs_termState = AFSOP_STOP_COMPLETE;
1084 #endif
1085             osi_rxWakeup(&afs_termState);
1086             return;
1087         }
1088     }
1089 }
1090 #endif
1091
1092 #ifdef RXK_LISTENER_ENV
1093
1094 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1095 int
1096 rxk_ReadPacket(osi_socket so, struct rx_packet *p,
1097                struct sockaddr_storage *saddr, int *slen)
1098 {
1099     int code;
1100     int nbytes;
1101     afs_int32 rlen;
1102     register afs_int32 tlen;
1103     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1104     rx_computelen(p, tlen);
1105     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1106
1107     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1108     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1109                                  * it once in order to avoid races.  */
1110     tlen = rlen - tlen;
1111     if (tlen > 0) {
1112         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1113         if (tlen > 0) {
1114             tlen = rlen - tlen;
1115         } else
1116             tlen = rlen;
1117     } else
1118         tlen = rlen;
1119
1120     /* add some padding to the last iovec, it's just to make sure that the 
1121      * read doesn't return more data than we expect, and is done to get around
1122      * our problems caused by the lack of a length field in the rx header. */
1123     savelen = p->wirevec[p->niovecs - 1].iov_len;
1124     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1125
1126     nbytes = tlen + sizeof(afs_int32);
1127 #ifdef RX_KERNEL_TRACE
1128     if (ICL_SETACTIVE(afs_iclSetp)) {
1129         AFS_GLOCK();
1130         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1131                    "before osi_NetRecive()");
1132         AFS_GUNLOCK();
1133     }
1134 #endif
1135     code = osi_NetReceive(rx_socket, saddr, slen, p->wirevec, p->niovecs,
1136                           &nbytes);
1137
1138 #ifdef RX_KERNEL_TRACE
1139     if (ICL_SETACTIVE(afs_iclSetp)) {
1140         AFS_GLOCK();
1141         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1142                    "after osi_NetRecive()");
1143         AFS_GUNLOCK();
1144     }
1145 #endif
1146     /* restore the vec to its correct state */
1147     p->wirevec[p->niovecs - 1].iov_len = savelen;
1148
1149     if (!code) {
1150         p->length = nbytes - RX_HEADER_SIZE;;
1151         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1152             if (nbytes > 0)
1153                 rxi_MorePackets(rx_initSendWindow);
1154             else {
1155                 MUTEX_ENTER(&rx_stats_mutex);
1156                 rx_stats.bogusPacketOnRead++;
1157                 switch (rx_ssfamily(saddr)) {
1158                 case AF_INET:
1159                     rx_stats.bogusHost = rx_ss2sin(saddr)->sin_addr.s_addr;
1160                     break;
1161                 default:
1162 #ifdef AF_INET6
1163                 case AF_INET6:
1164 #endif /* AF_INET6 */
1165                     rx_stats.bogusHost = 0xffffffff;
1166                 }
1167                 MUTEX_EXIT(&rx_stats_mutex);
1168                 dpf(("B: bogus packet from [%x,%d] nb=%d",
1169                     ntohl(rx_ss2v4addr(saddr)), ntohs(rx_ss2pn(saddr)),
1170                     nbytes));
1171             }
1172             return -1;
1173         } else {
1174             /* Extract packet header. */
1175             rxi_DecodePacketHeader(p);
1176
1177             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1178                 MUTEX_ENTER(&rx_stats_mutex);
1179                 rx_stats.packetsRead[p->header.type - 1]++;
1180                 MUTEX_EXIT(&rx_stats_mutex);
1181             }
1182
1183             /* Free any empty packet buffers at the end of this packet */
1184             rxi_TrimDataBufs(p, 1);
1185
1186             return 0;
1187         }
1188     } else
1189         return code;
1190 }
1191
1192 /* rxk_Listener() 
1193  *
1194  * Listen for packets on socket. This thread is typically started after
1195  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1196  * the start state is set before proceeding.
1197  *
1198  * Note that this thread is outside the AFS global lock for much of
1199  * it's existence.
1200  *
1201  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1202  * we want here. So we need to either block all signals (including SIGKILL
1203  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1204  * OS's socket receive routine returns as a result of a signal.
1205  */
1206 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1207
1208 #ifdef AFS_SUN5_ENV
1209 /*
1210  * Run the listener as a kernel thread.
1211  */
1212 void
1213 rxk_Listener(void)
1214 {
1215     extern id_t syscid;
1216     void rxk_ListenerProc(void);
1217     if (thread_create
1218         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1219          minclsyspri) == NULL)
1220         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1221 }
1222
1223 void
1224 rxk_ListenerProc(void)
1225 #else /* AFS_SUN5_ENV */
1226 void
1227 rxk_Listener(void)
1228 #endif                          /* AFS_SUN5_ENV */
1229 {
1230     struct rx_packet *rxp = NULL;
1231     struct sockaddr_storage saddr;
1232     int code, slen;
1233
1234 #ifdef AFS_LINUX20_ENV
1235     rxk_ListenerPid = current->pid;
1236 #endif
1237 #ifdef AFS_SUN5_ENV
1238     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1239 #endif /* AFS_SUN5_ENV */
1240 #ifdef AFS_XBSD_ENV
1241     rxk_ListenerPid = curproc->p_pid;
1242 #endif /* AFS_FBSD_ENV */
1243 #ifdef AFS_DARWIN80_ENV
1244     rxk_ListenerPid = proc_selfpid();
1245 #elif defined(AFS_DARWIN_ENV)
1246     rxk_ListenerPid = current_proc()->p_pid;
1247 #endif
1248 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1249     AFS_GUNLOCK();
1250 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1251     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1252         if (rxp) {
1253             rxi_RestoreDataBufs(rxp);
1254         } else {
1255             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1256             if (!rxp)
1257                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1258         }
1259         if (!(code = rxk_ReadPacket(rx_socket, rxp, &saddr, &slen))) {
1260             rxp = rxi_ReceivePacket(rxp, rx_socket, &saddr, slen, 0, 0);
1261         }
1262     }
1263
1264 #ifdef RX_ENABLE_LOCKS
1265     AFS_GLOCK();
1266 #endif /* RX_ENABLE_LOCKS */
1267     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1268         afs_termState = AFSOP_STOP_COMPLETE;
1269         osi_rxWakeup(&afs_termState);
1270     }
1271     rxk_ListenerPid = 0;
1272 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
1273     osi_rxWakeup(&rxk_ListenerPid);
1274 #endif
1275 #ifdef AFS_SUN5_ENV
1276     AFS_GUNLOCK();
1277 #endif /* AFS_SUN5_ENV */
1278 }
1279
1280 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1281 /* The manner of stopping the rx listener thread may vary. Most unix's should
1282  * be able to call soclose.
1283  */
1284 void
1285 osi_StopListener(void)
1286 {
1287     soclose(rx_socket);
1288 }
1289 #endif
1290 #endif /* RXK_LISTENER_ENV */
1291
1292 #endif /* !NCR && !UKERNEL */