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