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