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