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