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