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