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