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