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