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