FBSD: VIMAGE support
[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 !defined(RXK_LISTENER_ENV) && !defined(RXK_UPCALL_ENV)
351     /* if kernel, give name of appropriate procedures */
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 #if defined(AFS_FBSD80_ENV)
671     TAILQ_FOREACH(ifn, &V_ifnet, if_link) {
672 #else
673     TAILQ_FOREACH(ifn, &ifnet, if_link) {
674 #endif
675         if (i >= ADDRSPERSITE)
676             break;
677 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
678     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
679          ifn = ifn->if_list.tqe_next) {
680 #else
681     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
682 #endif
683         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
684 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
685         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
686             if (i >= ADDRSPERSITE)
687                 break;
688 #elif defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
689         for (ifad = ifn->if_addrlist.tqh_first;
690              ifad != NULL && i < ADDRSPERSITE;
691              ifad = ifad->ifa_list.tqe_next) {
692 #else
693         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
694              ifad = ifad->ifa_next) {
695 #endif
696             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
697                 ifinaddr =
698                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
699                           s_addr);
700                 if (myNetAddrs[i] != ifinaddr) {
701                     different++;
702                 }
703                 mtus[i] = rxmtu;
704                 rxmtu = rxi_AdjustIfMTU(rxmtu);
705                 maxmtu =
706                     rxmtu * rxi_nRecvFrags +
707                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
708                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
709                 addrs[i++] = ifinaddr;
710                 if (!rx_IsLoopbackAddr(ifinaddr) && (maxmtu > rx_maxReceiveSize)) {
711                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
712                     rx_maxReceiveSize =
713                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
714                 }
715             }
716         }
717     }
718 #endif
719
720     rx_maxJumboRecvSize =
721         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
722         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
723     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
724
725     if (different) {
726         int l;
727         for (l = 0; l < i; l++) {
728             myNetMTUs[l] = mtus[l];
729             myNetAddrs[l] = addrs[l];
730         }
731     }
732     return different;
733 }
734
735 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
736 /* Returns ifnet which best matches address */
737 rx_ifnet_t
738 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
739 {
740     struct sockaddr_in s, sr;
741     rx_ifaddr_t ifad;
742
743     s.sin_family = AF_INET;
744     s.sin_addr.s_addr = addr;
745     ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
746
747     if (ifad && maskp) {
748         rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
749         *maskp = sr.sin_addr.s_addr;
750     }
751     return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
752 }
753
754 #else /* DARWIN || XBSD */
755
756 /* Returns ifnet which best matches address */
757 rx_ifnet_t
758 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
759 {
760     int match_value = 0;
761     extern struct in_ifaddr *in_ifaddr;
762     struct in_ifaddr *ifa, *ifad = NULL;
763
764     addr = ntohl(addr);
765
766 #if defined(AFS_DARWIN_ENV)
767     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
768          ifa = TAILQ_NEXT(ifa, ia_link)) {
769 #else
770     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
771 #endif
772         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
773             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
774                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
775                     match_value = 4;
776                     ifad = ifa;
777                     goto done;
778                 }
779                 if (match_value < 3) {
780                     ifad = ifa;
781                     match_value = 3;
782                 }
783             } else {
784                 if (match_value < 2) {
785                     ifad = ifa;
786                     match_value = 2;
787                 }
788             }
789         }                       /* if net matches */
790     }                           /* for all in_ifaddrs */
791
792   done:
793     if (ifad && maskp)
794         *maskp = ifad->ia_subnetmask;
795     return (ifad ? ifad->ia_ifp : NULL);
796 }
797 #endif /* else DARWIN || XBSD */
798 #endif /* else AFS_USERSPACE_IP_ADDR */
799 #endif /* !SUN5 && !SGI62 */
800
801
802 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
803  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
804  * system specific subdirectories for all systems. But for the moment,
805  * most of it is simple to follow common code.
806  */
807 #if !defined(UKERNEL)
808 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
809 /* rxk_NewSocket creates a new socket on the specified port. The port is
810  * in network byte order.
811  */
812 osi_socket *
813 rxk_NewSocketHost(afs_uint32 ahost, short aport)
814 {
815     afs_int32 code;
816 #ifdef AFS_DARWIN80_ENV
817     socket_t newSocket;
818 #else
819     struct socket *newSocket;
820 #endif
821 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD_ENV))
822     struct mbuf *nam;
823 #endif
824     struct sockaddr_in myaddr;
825 #ifdef AFS_HPUX110_ENV
826     /* prototype copied from kernel source file streams/str_proto.h */
827     extern MBLKP allocb_wait(int, int);
828     MBLKP bindnam;
829     int addrsize = sizeof(struct sockaddr_in);
830     struct file *fp;
831     extern struct fileops socketops;
832 #endif
833 #ifdef AFS_SGI65_ENV
834     bhv_desc_t bhv;
835 #endif
836
837     AFS_STATCNT(osi_NewSocket);
838 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
839     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
840 #endif
841     AFS_ASSERT_GLOCK();
842     AFS_GUNLOCK();
843 #if     defined(AFS_HPUX102_ENV)
844 #if     defined(AFS_HPUX110_ENV)
845     /* we need a file associated with the socket so sosend in NetSend
846      * will not fail */
847     /* blocking socket */
848     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
849     fp = falloc();
850     if (!fp)
851         goto bad;
852     fp->f_flag = FREAD | FWRITE;
853     fp->f_type = DTYPE_SOCKET;
854     fp->f_ops = &socketops;
855
856     fp->f_data = (void *)newSocket;
857     newSocket->so_fp = (void *)fp;
858
859 #else /* AFS_HPUX110_ENV */
860     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
861 #endif /* else AFS_HPUX110_ENV */
862 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
863     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
864 #elif defined(AFS_FBSD_ENV)
865     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
866                     afs_osi_credp, curthread);
867 #elif defined(AFS_DARWIN80_ENV)
868 #ifdef RXK_LISTENER_ENV
869     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
870 #else
871     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, rx_upcall, NULL, &newSocket);
872 #endif
873 #elif defined(AFS_NBSD50_ENV)
874     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc(), NULL);
875 #elif defined(AFS_NBSD40_ENV)
876     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, osi_curproc());
877 #else
878     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
879 #endif /* AFS_HPUX102_ENV */
880     if (code)
881         goto bad;
882
883     memset(&myaddr, 0, sizeof myaddr);
884     myaddr.sin_family = AF_INET;
885     myaddr.sin_port = aport;
886     myaddr.sin_addr.s_addr = ahost;
887 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
888     myaddr.sin_len = sizeof(myaddr);
889 #endif
890
891 #ifdef AFS_HPUX110_ENV
892     bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
893     if (!bindnam) {
894         setuerror(ENOBUFS);
895         goto bad;
896     }
897     memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
898            addrsize);
899     bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
900 #if defined(AFS_NBSD40_ENV)
901     code = sobind(newSocket, bindnam, addrsize, osi_curproc());
902 #else
903     code = sobind(newSocket, bindnam, addrsize);
904 #endif
905     if (code) {
906         soclose(newSocket);
907 #if !defined(AFS_HPUX1122_ENV)
908         m_freem(nam);
909 #endif
910         goto bad;
911     }
912
913     freeb(bindnam);
914 #else /* AFS_HPUX110_ENV */
915 #if defined(AFS_DARWIN80_ENV)
916     {
917        int buflen = 50000;
918        int i,code2;
919        for (i=0;i<2;i++) {
920            code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
921                                   &buflen, sizeof(buflen));
922            code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
923                                   &buflen, sizeof(buflen));
924            if (!code && !code2)
925                break;
926            if (i == 2)
927               osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
928            buflen = 32766;
929        }
930     }
931 #else
932     code = soreserve(newSocket, 50000, 50000);
933     if (code) {
934         code = soreserve(newSocket, 32766, 32766);
935         if (code)
936             osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
937     }
938 #endif
939 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
940 #if defined(AFS_FBSD_ENV)
941     code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
942 #else
943     code = sobind(newSocket, (struct sockaddr *)&myaddr);
944 #endif
945     if (code) {
946         dpf(("sobind fails (%d)\n", (int)code));
947         soclose(newSocket);
948         goto bad;
949     }
950 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
951 #ifdef  AFS_OSF_ENV
952     nam = m_getclr(M_WAIT, MT_SONAME);
953 #else /* AFS_OSF_ENV */
954     nam = m_get(M_WAIT, MT_SONAME);
955 #endif
956     if (nam == NULL) {
957 #if defined(KERNEL_HAVE_UERROR)
958         setuerror(ENOBUFS);
959 #endif
960         goto bad;
961     }
962     nam->m_len = sizeof(myaddr);
963     memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
964 #if defined(AFS_SGI65_ENV)
965     BHV_PDATA(&bhv) = (void *)newSocket;
966     code = sobind(&bhv, nam);
967     m_freem(nam);
968 #elif defined(AFS_OBSD44_ENV) || defined(AFS_NBSD40_ENV)
969     code = sobind(newSocket, nam, osi_curproc());
970 #else
971     code = sobind(newSocket, nam);
972 #endif
973     if (code) {
974         dpf(("sobind fails (%d)\n", (int)code));
975         soclose(newSocket);
976 #ifndef AFS_SGI65_ENV
977         m_freem(nam);
978 #endif
979         goto bad;
980     }
981 #endif /* else AFS_DARWIN_ENV */
982 #endif /* else AFS_HPUX110_ENV */
983
984     AFS_GLOCK();
985 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
986     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
987 #endif
988     return (osi_socket *)newSocket;
989
990   bad:
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 *)0;
996 }
997
998 osi_socket *
999 rxk_NewSocket(short aport)
1000 {
1001     return rxk_NewSocketHost(0, aport);
1002 }
1003
1004 /* free socket allocated by rxk_NewSocket */
1005 int
1006 rxk_FreeSocket(struct socket *asocket)
1007 {
1008     AFS_STATCNT(osi_FreeSocket);
1009 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1010     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1011 #endif
1012 #ifdef AFS_HPUX110_ENV
1013     if (asocket->so_fp) {
1014         struct file *fp = asocket->so_fp;
1015 #if !defined(AFS_HPUX1123_ENV)
1016         /* 11.23 still has falloc, but not FPENTRYFREE !
1017          * so for now if we shutdown, we will waist a file
1018          * structure */
1019         FPENTRYFREE(fp);
1020         asocket->so_fp = NULL;
1021 #endif
1022     }
1023 #endif /* AFS_HPUX110_ENV */
1024     soclose(asocket);
1025 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1026     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1027 #endif
1028     return 0;
1029 }
1030 #endif /* !SUN5 && !LINUX20 */
1031
1032 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV) || defined(RXK_UPCALL_ENV)
1033 #ifdef RXK_TIMEDSLEEP_ENV
1034 /* Shutting down should wake us up, as should an earlier event. */
1035 void
1036 rxi_ReScheduleEvents(void)
1037 {
1038     /* needed to allow startup */
1039     int glock = ISAFS_GLOCK();
1040     if (!glock)
1041         AFS_GLOCK();
1042     osi_rxWakeup(&afs_termState);
1043     if (!glock)
1044         AFS_GUNLOCK();
1045 }
1046 #endif
1047 /*
1048  * Run RX event daemon every second (5 times faster than rest of systems)
1049  */
1050 void
1051 afs_rxevent_daemon(void)
1052 {
1053     struct clock temp;
1054     SPLVAR;
1055
1056     while (1) {
1057 #ifdef RX_ENABLE_LOCKS
1058         AFS_GUNLOCK();
1059 #endif /* RX_ENABLE_LOCKS */
1060         NETPRI;
1061         rxevent_RaiseEvents(&temp);
1062         USERPRI;
1063 #ifdef RX_ENABLE_LOCKS
1064         AFS_GLOCK();
1065 #endif /* RX_ENABLE_LOCKS */
1066 #ifdef RX_KERNEL_TRACE
1067         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1068                    "before afs_osi_Wait()");
1069 #endif
1070 #ifdef RXK_TIMEDSLEEP_ENV
1071         afs_osi_TimedSleep(&afs_termState, MAX(500, ((temp.sec * 1000) +
1072                                                      (temp.usec / 1000))), 0);
1073 #else
1074         afs_osi_Wait(500, NULL, 0);
1075 #endif
1076 #ifdef RX_KERNEL_TRACE
1077         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1078                    "after afs_osi_Wait()");
1079 #endif
1080         if (afs_termState == AFSOP_STOP_RXEVENT) {
1081 #ifdef RXK_LISTENER_ENV
1082             afs_termState = AFSOP_STOP_RXK_LISTENER;
1083 #elif defined(AFS_SUN510_ENV) || defined(RXK_UPCALL_ENV)
1084             afs_termState = AFSOP_STOP_NETIF;
1085 #else
1086             afs_termState = AFSOP_STOP_COMPLETE;
1087 #endif
1088             osi_rxWakeup(&afs_termState);
1089             return;
1090         }
1091     }
1092 }
1093 #endif
1094
1095 #ifdef RXK_LISTENER_ENV
1096
1097 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1098 int
1099 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1100 {
1101     int code;
1102     struct sockaddr_in from;
1103     int nbytes;
1104     afs_int32 rlen;
1105     afs_int32 tlen;
1106     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1107     rx_computelen(p, tlen);
1108     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1109
1110     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1111     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1112                                  * it once in order to avoid races.  */
1113     tlen = rlen - tlen;
1114     if (tlen > 0) {
1115         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1116         if (tlen > 0) {
1117             tlen = rlen - tlen;
1118         } else
1119             tlen = rlen;
1120     } else
1121         tlen = rlen;
1122
1123     /* add some padding to the last iovec, it's just to make sure that the
1124      * read doesn't return more data than we expect, and is done to get around
1125      * our problems caused by the lack of a length field in the rx header. */
1126     savelen = p->wirevec[p->niovecs - 1].iov_len;
1127     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1128
1129     nbytes = tlen + sizeof(afs_int32);
1130 #ifdef RX_KERNEL_TRACE
1131     if (ICL_SETACTIVE(afs_iclSetp)) {
1132         AFS_GLOCK();
1133         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1134                    "before osi_NetRecive()");
1135         AFS_GUNLOCK();
1136     }
1137 #endif
1138     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1139
1140 #ifdef RX_KERNEL_TRACE
1141     if (ICL_SETACTIVE(afs_iclSetp)) {
1142         AFS_GLOCK();
1143         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1144                    "after osi_NetRecive()");
1145         AFS_GUNLOCK();
1146     }
1147 #endif
1148     /* restore the vec to its correct state */
1149     p->wirevec[p->niovecs - 1].iov_len = savelen;
1150
1151     if (!code) {
1152         p->length = nbytes - RX_HEADER_SIZE;;
1153         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1154             if (nbytes <= 0) {
1155                 if (rx_stats_active) {
1156                     MUTEX_ENTER(&rx_stats_mutex);
1157                     rx_atomic_inc(&rx_stats.bogusPacketOnRead);
1158                     rx_stats.bogusHost = from.sin_addr.s_addr;
1159                     MUTEX_EXIT(&rx_stats_mutex);
1160                 }
1161                 dpf(("B: bogus packet from [%x,%d] nb=%d\n",
1162                      from.sin_addr.s_addr, from.sin_port, nbytes));
1163             }
1164             return -1;
1165         } else {
1166             /* Extract packet header. */
1167             rxi_DecodePacketHeader(p);
1168
1169             *host = from.sin_addr.s_addr;
1170             *port = from.sin_port;
1171             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1172                 if (rx_stats_active) {
1173                     rx_atomic_inc(&rx_stats.packetsRead[p->header.type - 1]);
1174                 }
1175             }
1176
1177 #ifdef RX_TRIMDATABUFS
1178             /* Free any empty packet buffers at the end of this packet */
1179             rxi_TrimDataBufs(p, 1);
1180 #endif
1181             return 0;
1182         }
1183     } else
1184         return code;
1185 }
1186
1187 /* rxk_Listener()
1188  *
1189  * Listen for packets on socket. This thread is typically started after
1190  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1191  * the start state is set before proceeding.
1192  *
1193  * Note that this thread is outside the AFS global lock for much of
1194  * it's existence.
1195  *
1196  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1197  * we want here. So we need to either block all signals (including SIGKILL
1198  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1199  * OS's socket receive routine returns as a result of a signal.
1200  */
1201 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1202 #ifdef AFS_LINUX20_ENV
1203 struct task_struct *rxk_ListenerTask;
1204 #endif
1205
1206 void
1207 rxk_Listener(void)
1208 {
1209     struct rx_packet *rxp = NULL;
1210     int code;
1211     int host, port;
1212
1213 #ifdef AFS_LINUX20_ENV
1214     rxk_ListenerPid = current->pid;
1215     rxk_ListenerTask = current;
1216 #endif
1217 #ifdef AFS_SUN5_ENV
1218     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1219 #endif /* AFS_SUN5_ENV */
1220 #ifdef AFS_XBSD_ENV
1221     rxk_ListenerPid = curproc->p_pid;
1222 #endif /* AFS_FBSD_ENV */
1223 #ifdef AFS_DARWIN80_ENV
1224     rxk_ListenerPid = proc_selfpid();
1225 #elif defined(AFS_DARWIN_ENV)
1226     rxk_ListenerPid = current_proc()->p_pid;
1227 #endif
1228 #ifdef RX_ENABLE_LOCKS
1229     AFS_GUNLOCK();
1230 #endif /* RX_ENABLE_LOCKS */
1231     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1232         /* See if a check for additional packets was issued */
1233         rx_CheckPackets();
1234
1235         if (rxp) {
1236             rxi_RestoreDataBufs(rxp);
1237         } else {
1238             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1239             if (!rxp)
1240                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1241         }
1242         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1243             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1244         }
1245     }
1246
1247 #ifdef RX_ENABLE_LOCKS
1248     AFS_GLOCK();
1249 #endif /* RX_ENABLE_LOCKS */
1250     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1251 #ifdef AFS_SUN510_ENV
1252         afs_termState = AFSOP_STOP_NETIF;
1253 #else
1254         afs_termState = AFSOP_STOP_COMPLETE;
1255 #endif
1256         osi_rxWakeup(&afs_termState);
1257     }
1258     rxk_ListenerPid = 0;
1259 #ifdef AFS_LINUX20_ENV
1260     rxk_ListenerTask = 0;
1261     osi_rxWakeup(&rxk_ListenerTask);
1262 #endif
1263 #if defined(AFS_SUN5_ENV) || defined(AFS_FBSD_ENV)
1264     osi_rxWakeup(&rxk_ListenerPid);
1265 #endif
1266 }
1267
1268 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1269 /* The manner of stopping the rx listener thread may vary. Most unix's should
1270  * be able to call soclose.
1271  */
1272 void
1273 osi_StopListener(void)
1274 {
1275     soclose(rx_socket);
1276 }
1277 #endif
1278 #endif /* RXK_LISTENER_ENV */
1279 #endif /* !NCR && !UKERNEL */
1280
1281 #if !defined(AFS_LINUX26_ENV)
1282 void
1283 #if defined(AFS_AIX_ENV)
1284 osi_Panic(char *msg, void *a1, void *a2, void *a3)
1285 #else
1286 osi_Panic(char *msg, ...)
1287 #endif
1288 {
1289 #ifdef AFS_AIX_ENV
1290     if (!msg)
1291         msg = "Unknown AFS panic";
1292     /*
1293      * we should probably use the errsave facility here. it is not
1294      * varargs-aware
1295      */
1296
1297     printf(msg, a1, a2, a3);
1298     panic(msg);
1299 #elif defined(AFS_SGI_ENV)
1300     va_list ap;
1301
1302     /* Solaris has vcmn_err, Sol10 01/06 may have issues. Beware. */
1303     if (!msg) {
1304         cmn_err(CE_PANIC, "Unknown AFS panic");
1305     } else {
1306         va_start(ap, msg);
1307         icmn_err(CE_PANIC, msg, ap);
1308         va_end(ap);
1309     }
1310 #elif defined(AFS_DARWIN80_ENV) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
1311     char buf[256];
1312     va_list ap;
1313     if (!msg)
1314         msg = "Unknown AFS panic";
1315
1316     va_start(ap, msg);
1317     vsnprintf(buf, sizeof(buf), msg, ap);
1318     va_end(ap);
1319     printf("%s", buf);
1320     panic(buf);
1321 #else
1322     va_list ap;
1323     if (!msg)
1324         msg = "Unknown AFS panic";
1325
1326     va_start(ap, msg);
1327     vprintf(msg, ap);
1328     va_end(ap);
1329 # ifdef AFS_LINUX20_ENV
1330     * ((char *) 0) = 0;
1331 # else
1332     panic(msg);
1333 # endif
1334 #endif
1335 }
1336 #endif