c63b8b3e21974674199ca6e2bba2875170c89510
[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 #else
251 void
252 osi_AssertFailK(const char *expr, const char *file, int line)
253 {
254     printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
255 }
256 #endif
257
258 #ifndef UKERNEL
259 /* This is the server process request loop. Kernel server
260  * processes never become listener threads */
261 void *
262 rx_ServerProc(void *unused)
263 {
264     int threadID;
265
266     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
267     MUTEX_ENTER(&rx_quota_mutex);
268     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
269     /* threadID is used for making decisions in GetCall.  Get it by bumping
270      * number of threads handling incoming calls */
271     threadID = rxi_availProcs++;
272     MUTEX_EXIT(&rx_quota_mutex);
273
274 #ifdef RX_ENABLE_LOCKS
275     AFS_GUNLOCK();
276 #endif /* RX_ENABLE_LOCKS */
277     rxi_ServerProc(threadID, NULL, NULL);
278 #ifdef RX_ENABLE_LOCKS
279     AFS_GLOCK();
280 #endif /* RX_ENABLE_LOCKS */
281
282     return NULL;
283 }
284 #endif /* !UKERNEL */
285
286 #ifndef RXK_LISTENER_ENV
287 /* asize includes the Rx header */
288 static int
289 MyPacketProc(struct rx_packet **ahandle, int asize)
290 {
291     struct rx_packet *tp;
292
293     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
294     /* If we can't scrounge enough cbufs, then we have to drop the packet,
295      * but we should set a flag so we magic up some more at our leisure.
296      */
297
298     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
299         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
300         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
301             if (0 <
302                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
303                                  RX_PACKET_CLASS_RECV_CBUF)) {
304                 rxi_FreePacket(tp);
305                 tp = NULL;
306                 if (rx_stats_active) {
307                     rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
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             rx_atomic_inc(&rx_stats.bogusPacketOnRead);
319         }
320         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
321         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
322         tp = NULL;
323     }
324
325     if (!tp)
326         return -1;
327     /* otherwise we have a packet, set appropriate values */
328     *ahandle = tp;
329     return 0;
330 }
331
332 static int
333 MyArrivalProc(struct rx_packet *ahandle,
334               struct sockaddr_in *afrom,
335               struct socket *arock,
336               afs_int32 asize)
337 {
338     /* handle basic rx packet */
339     ahandle->length = asize - RX_HEADER_SIZE;
340     rxi_DecodePacketHeader(ahandle);
341     ahandle =
342         rxi_ReceivePacket(ahandle, arock,
343                           afrom->sin_addr.s_addr, afrom->sin_port, NULL,
344                           NULL);
345
346     /* free the packet if it has been returned */
347     if (ahandle)
348         rxi_FreePacket(ahandle);
349     return 0;
350 }
351 #endif /* !RXK_LISTENER_ENV */
352
353 void
354 rxi_StartListener(void)
355 {
356     /* if kernel, give name of appropriate procedures */
357 #ifndef RXK_LISTENER_ENV
358     rxk_GetPacketProc = MyPacketProc;
359     rxk_PacketArrivalProc = MyArrivalProc;
360     rxk_init();
361 #endif
362 }
363
364 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
365   to get interesting information. */
366 void
367 rxi_InitPeerParams(struct rx_peer *pp)
368 {
369     u_short rxmtu;
370
371 #ifdef  ADAPT_MTU
372 # ifndef AFS_SUN5_ENV
373 #  ifdef AFS_USERSPACE_IP_ADDR
374     afs_int32 i;
375     afs_int32 mtu;
376
377     i = rxi_Findcbi(pp->host);
378     if (i == -1) {
379         pp->timeout.sec = 3;
380         /* pp->timeout.usec = 0; */
381         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
382     } else {
383         pp->timeout.sec = 2;
384         /* pp->timeout.usec = 0; */
385         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
386         mtu = ntohl(afs_cb_interface.mtu[i]);
387         /* Diminish the packet size to one based on the MTU given by
388          * the interface. */
389         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
390             rxmtu = mtu - RX_IPUDP_SIZE;
391             if (rxmtu < pp->ifMTU)
392                 pp->ifMTU = rxmtu;
393         }
394     }
395 #  else /* AFS_USERSPACE_IP_ADDR */
396     rx_ifnet_t ifn;
397
398 #   if !defined(AFS_SGI62_ENV)
399     if (numMyNetAddrs == 0)
400         (void)rxi_GetIFInfo();
401 #   endif
402
403     ifn = rxi_FindIfnet(pp->host, NULL);
404     if (ifn) {
405         pp->timeout.sec = 2;
406         /* pp->timeout.usec = 0; */
407         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
408 #   ifdef IFF_POINTOPOINT
409         if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
410             /* wish we knew the bit rate and the chunk size, sigh. */
411             pp->timeout.sec = 4;
412             pp->ifMTU = RX_PP_PACKET_SIZE;
413         }
414 #   endif /* IFF_POINTOPOINT */
415         /* Diminish the packet size to one based on the MTU given by
416          * the interface. */
417         if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
418             rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
419             if (rxmtu < pp->ifMTU)
420                 pp->ifMTU = rxmtu;
421         }
422     } else {                    /* couldn't find the interface, so assume the worst */
423         pp->timeout.sec = 3;
424         /* pp->timeout.usec = 0; */
425         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
426     }
427 #  endif /* else AFS_USERSPACE_IP_ADDR */
428 # else /* AFS_SUN5_ENV */
429     afs_int32 mtu;
430
431     mtu = rxi_FindIfMTU(pp->host);
432
433     if (mtu <= 0) {
434         pp->timeout.sec = 3;
435         /* pp->timeout.usec = 0; */
436         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
437     } else {
438         pp->timeout.sec = 2;
439         /* pp->timeout.usec = 0; */
440         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
441
442         /* Diminish the packet size to one based on the MTU given by
443          * the interface. */
444         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
445             rxmtu = mtu - RX_IPUDP_SIZE;
446             if (rxmtu < pp->ifMTU)
447                 pp->ifMTU = rxmtu;
448         }
449     }
450 # endif /* AFS_SUN5_ENV */
451 #else /* ADAPT_MTU */
452     pp->rateFlag = 2;           /* start timing after two full packets */
453     pp->timeout.sec = 2;
454     pp->ifMTU = OLD_MAX_PACKET_SIZE;
455 #endif /* else ADAPT_MTU */
456     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
457     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
458     pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
459     pp->ifDgramPackets =
460         MIN(rxi_nDgramPackets,
461             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
462     pp->maxDgramPackets = 1;
463
464     /* Initialize slow start parameters */
465     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
466     pp->cwind = 1;
467     pp->nDgramPackets = 1;
468     pp->congestSeq = 0;
469 }
470
471
472 /* The following code is common to several system types, but not all. The
473  * separate ones are found in the system specific subdirectories.
474  */
475
476
477 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
478 /* Routine called during the afsd "-shutdown" process to put things back to
479  * the initial state.
480  */
481 static struct protosw parent_proto;     /* udp proto switch */
482
483 void
484 shutdown_rxkernel(void)
485 {
486     struct protosw *tpro, *last;
487     last = inetdomain.dom_protoswNPROTOSW;
488     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
489         if (tpro->pr_protocol == IPPROTO_UDP) {
490             /* restore original udp protocol switch */
491             memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
492             memset((void *)&parent_proto, 0, sizeof(parent_proto));
493             rxk_initDone = 0;
494             rxk_shutdownPorts();
495             return;
496         }
497     dpf(("shutdown_rxkernel: no udp proto\n"));
498 }
499 #endif /* !AIX && !SUN && !NCR  && !UKERNEL */
500
501 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
502 /* Determine what the network interfaces are for this machine. */
503
504 #ifdef AFS_USERSPACE_IP_ADDR
505 int
506 rxi_GetcbiInfo(void)
507 {
508     int i, j, different = 0, num = ADDRSPERSITE;
509     int rxmtu, maxmtu;
510     afs_uint32 ifinaddr;
511     afs_uint32 addrs[ADDRSPERSITE];
512     int mtus[ADDRSPERSITE];
513
514     memset((void *)addrs, 0, sizeof(addrs));
515     memset((void *)mtus, 0, sizeof(mtus));
516
517     if (afs_cb_interface.numberOfInterfaces < num)
518         num = afs_cb_interface.numberOfInterfaces;
519     for (i = 0; i < num; i++) {
520         if (!afs_cb_interface.mtu[i])
521             afs_cb_interface.mtu[i] = htonl(1500);
522         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
523         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
524         if (myNetAddrs[i] != ifinaddr)
525             different++;
526
527         mtus[i] = rxmtu;
528         rxmtu = rxi_AdjustIfMTU(rxmtu);
529         maxmtu =
530             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
531         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
532         addrs[i++] = ifinaddr;
533         if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
534             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
535             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
536         }
537     }
538
539     rx_maxJumboRecvSize =
540         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
541         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
542     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
543
544     if (different) {
545         for (j = 0; j < i; j++) {
546             myNetMTUs[j] = mtus[j];
547             myNetAddrs[j] = addrs[j];
548         }
549     }
550     return different;
551 }
552
553
554 /* Returns the afs_cb_interface inxex which best matches address.
555  * If none is found, we return -1.
556  */
557 afs_int32
558 rxi_Findcbi(afs_uint32 addr)
559 {
560     int j;
561     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
562     afs_int32 rvalue = -1;
563     int match_value = 0;
564
565     if (numMyNetAddrs == 0)
566         (void)rxi_GetcbiInfo();
567
568     myAddr = ntohl(addr);
569
570     if (IN_CLASSA(myAddr))
571         netMask = IN_CLASSA_NET;
572     else if (IN_CLASSB(myAddr))
573         netMask = IN_CLASSB_NET;
574     else if (IN_CLASSC(myAddr))
575         netMask = IN_CLASSC_NET;
576     else
577         netMask = 0;
578
579     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
580         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
581         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
582         if ((myAddr & netMask) == (thisAddr & netMask)) {
583             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
584                 if (myAddr == thisAddr) {
585                     match_value = 4;
586                     rvalue = j;
587                     break;
588                 }
589                 if (match_value < 3) {
590                     match_value = 3;
591                     rvalue = j;
592                 }
593             } else {
594                 if (match_value < 2) {
595                     match_value = 2;
596                     rvalue = j;
597                 }
598             }
599         }
600     }
601
602     return (rvalue);
603 }
604
605 #else /* AFS_USERSPACE_IP_ADDR */
606
607 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
608 #define IFADDR2SA(f) (&((f)->ifa_addr))
609 #else /* AFS_AIX41_ENV */
610 #define IFADDR2SA(f) ((f)->ifa_addr)
611 #endif
612
613 int
614 rxi_GetIFInfo(void)
615 {
616     int i = 0;
617     int different = 0;
618
619     int rxmtu, maxmtu;
620     afs_uint32 addrs[ADDRSPERSITE];
621     int mtus[ADDRSPERSITE];
622     afs_uint32 ifinaddr;
623 #if defined(AFS_DARWIN80_ENV)
624     errno_t t;
625     unsigned int count;
626     int cnt=0, m, j;
627     rx_ifaddr_t *ifads;
628     rx_ifnet_t *ifns;
629     struct sockaddr sout;
630     struct sockaddr_in *sin;
631     struct in_addr pin;
632 #else
633     rx_ifaddr_t ifad;   /* ifnet points to a if_addrlist of ifaddrs */
634     rx_ifnet_t ifn;
635 #endif
636
637     memset(addrs, 0, sizeof(addrs));
638     memset(mtus, 0, sizeof(mtus));
639
640 #if defined(AFS_DARWIN80_ENV)
641     if (!ifnet_list_get(AF_INET, &ifns, &count)) {
642         for (m = 0; m < count; m++) {
643             if (!ifnet_get_address_list(ifns[m], &ifads)) {
644                 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
645                     if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
646                         sin = (struct sockaddr_in *)&sout;
647                         rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
648                         ifinaddr = ntohl(sin->sin_addr.s_addr);
649                         if (myNetAddrs[i] != ifinaddr) {
650                             different++;
651                         }
652                         mtus[i] = rxmtu;
653                         rxmtu = rxi_AdjustIfMTU(rxmtu);
654                         maxmtu =
655                             rxmtu * rxi_nRecvFrags +
656                             ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
657                         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
658                         addrs[i++] = ifinaddr;
659                         if (!rx_IsLoopbackAddr(ifinaddr) &&
660                             (maxmtu > rx_maxReceiveSize)) {
661                             rx_maxReceiveSize =
662                                 MIN(RX_MAX_PACKET_SIZE, maxmtu);
663                             rx_maxReceiveSize =
664                                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
665                         }
666                         cnt++;
667                     }
668                 }
669                 ifnet_free_address_list(ifads);
670             }
671         }
672         ifnet_list_free(ifns);
673     }
674 #else
675 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
676     TAILQ_FOREACH(ifn, &ifnet, if_link) {
677         if (i >= ADDRSPERSITE)
678             break;
679 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
680     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
681          ifn = ifn->if_list.tqe_next) {
682 #else
683     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
684 #endif
685         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
686 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
687         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
688             if (i >= ADDRSPERSITE)
689                 break;
690 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
691         for (ifad = ifn->if_addrlist.tqh_first;
692              ifad != NULL && i < ADDRSPERSITE;
693              ifad = ifad->ifa_list.tqe_next) {
694 #else
695         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
696              ifad = ifad->ifa_next) {
697 #endif
698             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
699                 ifinaddr =
700                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
701                           s_addr);
702                 if (myNetAddrs[i] != ifinaddr) {
703                     different++;
704                 }
705                 mtus[i] = rxmtu;
706                 rxmtu = rxi_AdjustIfMTU(rxmtu);
707                 maxmtu =
708                     rxmtu * rxi_nRecvFrags +
709                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
710                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
711                 addrs[i++] = ifinaddr;
712                 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
713                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
714                     rx_maxReceiveSize =
715                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
716                 }
717             }
718         }
719     }
720 #endif
721
722     rx_maxJumboRecvSize =
723         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
724         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
725     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
726
727     if (different) {
728         int l;
729         for (l = 0; l < i; l++) {
730             myNetMTUs[l] = mtus[l];
731             myNetAddrs[l] = addrs[l];
732         }
733     }
734     return different;
735 }
736
737 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
738 /* Returns ifnet which best matches address */
739 rx_ifnet_t
740 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
741 {
742     struct sockaddr_in s, sr;
743     rx_ifaddr_t ifad;
744
745     s.sin_family = AF_INET;
746     s.sin_addr.s_addr = addr;
747     ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
748
749     if (ifad && maskp) {
750         rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
751         *maskp = sr.sin_addr.s_addr;
752     }
753     return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
754 }
755
756 #else /* DARWIN || XBSD */
757
758 /* Returns ifnet which best matches address */
759 rx_ifnet_t
760 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
761 {
762     int match_value = 0;
763     extern struct in_ifaddr *in_ifaddr;
764     struct in_ifaddr *ifa, *ifad = NULL;
765
766     addr = ntohl(addr);
767
768 #if defined(AFS_DARWIN_ENV)
769     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
770          ifa = TAILQ_NEXT(ifa, ia_link)) {
771 #else
772     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
773 #endif
774         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
775             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
776                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
777                     match_value = 4;
778                     ifad = ifa;
779                     goto done;
780                 }
781                 if (match_value < 3) {
782                     ifad = ifa;
783                     match_value = 3;
784                 }
785             } else {
786                 if (match_value < 2) {
787                     ifad = ifa;
788                     match_value = 2;
789                 }
790             }
791         }                       /* if net matches */
792     }                           /* for all in_ifaddrs */
793
794   done:
795     if (ifad && maskp)
796         *maskp = ifad->ia_subnetmask;
797     return (ifad ? ifad->ia_ifp : NULL);
798 }
799 #endif /* else DARWIN || XBSD */
800 #endif /* else AFS_USERSPACE_IP_ADDR */
801 #endif /* !SUN5 && !SGI62 */
802
803
804 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
805  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
806  * system specific subdirectories for all systems. But for the moment,
807  * most of it is simple to follow common code.
808  */
809 #if !defined(UKERNEL)
810 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
811 /* rxk_NewSocket creates a new socket on the specified port. The port is
812  * in network byte order.
813  */
814 osi_socket *
815 rxk_NewSocketHost(afs_uint32 ahost, short aport)
816 {
817     afs_int32 code;
818 #ifdef AFS_DARWIN80_ENV
819     socket_t newSocket;
820 #else
821     struct socket *newSocket;
822 #endif
823 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
824     struct mbuf *nam;
825 #endif
826     struct sockaddr_in myaddr;
827 #ifdef AFS_HPUX110_ENV
828     /* prototype copied from kernel source file streams/str_proto.h */
829     extern MBLKP allocb_wait(int, int);
830     MBLKP bindnam;
831     int addrsize = sizeof(struct sockaddr_in);
832     struct file *fp;
833     extern struct fileops socketops;
834 #endif
835 #ifdef AFS_SGI65_ENV
836     bhv_desc_t bhv;
837 #endif
838
839     AFS_STATCNT(osi_NewSocket);
840 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
841     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
842 #endif
843     AFS_ASSERT_GLOCK();
844     AFS_GUNLOCK();
845 #if     defined(AFS_HPUX102_ENV)
846 #if     defined(AFS_HPUX110_ENV)
847     /* we need a file associated with the socket so sosend in NetSend
848      * will not fail */
849     /* blocking socket */
850     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
851     fp = falloc();
852     if (!fp)
853         goto bad;
854     fp->f_flag = FREAD | FWRITE;
855     fp->f_type = DTYPE_SOCKET;
856     fp->f_ops = &socketops;
857
858     fp->f_data = (void *)newSocket;
859     newSocket->so_fp = (void *)fp;
860
861 #else /* AFS_HPUX110_ENV */
862     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
863 #endif /* else AFS_HPUX110_ENV */
864 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
865     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
866 #elif defined(AFS_FBSD_ENV)
867     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
868                     afs_osi_credp, curthread);
869 #elif defined(AFS_DARWIN80_ENV)
870     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
871 #elif defined(AFS_NBSD50_ENV)
872         code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
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_atomic_inc(&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\n",
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                     rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1174                 }
1175             }
1176
1177 #ifdef RX_TRIMDATABUFS
1178             /* Free any empty packet buffers at the end of this packet */
1179             rxi_TrimDataBufs(p, 1);
1180 #endif
1181             return 0;
1182         }
1183     } else
1184         return code;
1185 }
1186
1187 /* rxk_Listener()
1188  *
1189  * Listen for packets on socket. This thread is typically started after
1190  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1191  * the start state is set before proceeding.
1192  *
1193  * Note that this thread is outside the AFS global lock for much of
1194  * it's existence.
1195  *
1196  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1197  * we want here. So we need to either block all signals (including SIGKILL
1198  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1199  * OS's socket receive routine returns as a result of a signal.
1200  */
1201 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1202 #ifdef AFS_LINUX20_ENV
1203 struct task_struct *rxk_ListenerTask;
1204 #endif
1205
1206 #ifdef AFS_SUN5_ENV
1207 /*
1208  * Run the listener as a kernel thread.
1209  */
1210 void
1211 rxk_Listener(void)
1212 {
1213     extern id_t syscid;
1214     void rxk_ListenerProc(void);
1215     if (thread_create
1216         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1217          minclsyspri) == NULL)
1218         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1219 }
1220
1221 void
1222 rxk_ListenerProc(void)
1223 #else /* AFS_SUN5_ENV */
1224 void
1225 rxk_Listener(void)
1226 #endif                          /* AFS_SUN5_ENV */
1227 {
1228     struct rx_packet *rxp = NULL;
1229     int code;
1230     int host, port;
1231
1232 #ifdef AFS_LINUX20_ENV
1233     rxk_ListenerPid = current->pid;
1234     rxk_ListenerTask = current;
1235 #endif
1236 #ifdef AFS_SUN5_ENV
1237     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1238 #endif /* AFS_SUN5_ENV */
1239 #ifdef AFS_XBSD_ENV
1240     rxk_ListenerPid = curproc->p_pid;
1241 #endif /* AFS_FBSD_ENV */
1242 #ifdef AFS_DARWIN80_ENV
1243     rxk_ListenerPid = proc_selfpid();
1244 #elif defined(AFS_DARWIN_ENV)
1245     rxk_ListenerPid = current_proc()->p_pid;
1246 #endif
1247 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1248     AFS_GUNLOCK();
1249 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1250     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1251         /* See if a check for additional packets was issued */
1252         rx_CheckPackets();
1253
1254         if (rxp) {
1255             rxi_RestoreDataBufs(rxp);
1256         } else {
1257             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1258             if (!rxp)
1259                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1260         }
1261         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1262             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1263         }
1264     }
1265
1266 #ifdef RX_ENABLE_LOCKS
1267     AFS_GLOCK();
1268 #endif /* RX_ENABLE_LOCKS */
1269     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1270 #ifdef AFS_SUN510_ENV
1271         afs_termState = AFSOP_STOP_NETIF;
1272 #else
1273         afs_termState = AFSOP_STOP_COMPLETE;
1274 #endif
1275         osi_rxWakeup(&afs_termState);
1276     }
1277     rxk_ListenerPid = 0;
1278 #ifdef AFS_LINUX20_ENV
1279     rxk_ListenerTask = 0;
1280     osi_rxWakeup(&rxk_ListenerTask);
1281 #endif
1282 #if defined(AFS_SUN5_ENV)
1283     osi_rxWakeup(&rxk_ListenerPid);
1284 #endif
1285 #ifdef AFS_SUN5_ENV
1286     AFS_GUNLOCK();
1287 #endif /* AFS_SUN5_ENV */
1288 }
1289
1290 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1291 /* The manner of stopping the rx listener thread may vary. Most unix's should
1292  * be able to call soclose.
1293  */
1294 void
1295 osi_StopListener(void)
1296 {
1297     soclose(rx_socket);
1298 }
1299 #endif
1300 #endif /* RXK_LISTENER_ENV */
1301 #endif /* !NCR && !UKERNEL */
1302
1303 #if !defined(AFS_LINUX26_ENV)
1304 void
1305 #if defined(AFS_AIX_ENV)
1306 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1307 #else
1308 osi_Panic(char *msg, ...)
1309 #endif
1310 {
1311 #ifdef AFS_AIX_ENV
1312     if (!msg)
1313         msg = "Unknown AFS panic";
1314     /*
1315      * we should probably use the errsave facility here. it is not
1316      * varargs-aware
1317      */
1318
1319     printf(msg, a1, a2, a3);
1320     panic(msg);
1321 #elif defined(AFS_SGI_ENV)
1322     va_list ap;
1323
1324     /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1325     if (!msg) {
1326         cmn_err(CE_PANIC, "Unknown AFS panic");
1327     } else {
1328         va_start(ap, msg);
1329         icmn_err(CE_PANIC, msg, ap);
1330         va_end(ap);
1331     }
1332 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1333     char buf[256];
1334     va_list ap;
1335     if (!msg)
1336         msg = "Unknown AFS panic";
1337
1338     va_start(ap, msg);
1339     vsnprintf(buf, sizeof(buf), msg, ap);
1340     va_end(ap);
1341     printf("%s", buf);
1342     panic(buf);
1343 #else
1344     va_list ap;
1345     if (!msg)
1346         msg = "Unknown AFS panic";
1347
1348     va_start(ap, msg);
1349     vprintf(msg, ap);
1350     va_end(ap);
1351 # ifdef AFS_LINUX20_ENV
1352     * ((char *) 0) = 0;
1353 # else
1354     panic(msg);
1355 # endif
1356 #endif
1357 }
1358 #endif