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