FBSD: fix dumb panic when we can't pfind rxk_Listener
[openafs.git] / src / rx / FBSD / rx_knet.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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include <sys/malloc.h>
15 #include "rx/rx_kcommon.h"
16
17 #ifdef RXK_LISTENER_ENV
18 int
19 osi_NetReceive(osi_socket asocket, struct sockaddr_in *addr,
20                struct iovec *dvec, int nvecs, int *alength)
21 {
22     struct uio u;
23     int i;
24     struct iovec iov[RX_MAXIOVECS];
25     struct sockaddr *sa = NULL;
26     int code;
27
28     int haveGlock = ISAFS_GLOCK();
29     /*AFS_STATCNT(osi_NetReceive); */
30
31     if (nvecs > RX_MAXIOVECS)
32         osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
33
34     for (i = 0; i < nvecs; i++)
35         iov[i] = dvec[i];
36
37     u.uio_iov = &iov[0];
38     u.uio_iovcnt = nvecs;
39     u.uio_offset = 0;
40     u.uio_resid = *alength;
41     u.uio_segflg = UIO_SYSSPACE;
42     u.uio_rw = UIO_READ;
43     u.uio_td = NULL;
44
45     if (haveGlock)
46         AFS_GUNLOCK();
47     code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
48     if (haveGlock)
49         AFS_GLOCK();
50
51     if (code) {
52 #if KNET_DEBUG
53         if (code == EINVAL)
54             Debugger("afs NetReceive busted");
55         else
56             printf("y");
57 #else
58         return code;
59 #endif
60     }
61     *alength -= u.uio_resid;
62     if (sa) {
63         if (sa->sa_family == AF_INET) {
64             if (addr)
65                 *addr = *(struct sockaddr_in *)sa;
66         } else
67             printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
68         FREE(sa, M_SONAME);
69     }
70     return code;
71 }
72
73 #define so_is_disconn(so) ((so)->so_state & SS_ISDISCONNECTED)
74
75 extern int rxk_ListenerPid;
76 void
77 osi_StopListener(void)
78 {
79     struct proc *p;
80
81     /*
82      * Have to drop global lock to safely do this.
83      * soclose() is currently protected by Giant,
84      * but pfind and psignal are MPSAFE.
85      */
86     int haveGlock = ISAFS_GLOCK();
87     if (haveGlock)
88         AFS_GUNLOCK();
89     soshutdown(rx_socket, SHUT_RDWR);
90     p = pfind(rxk_ListenerPid);
91     afs_warn("osi_StopListener: rxk_ListenerPid %lx\n", p);
92     if (p) {
93         psignal(p, SIGUSR1);
94         PROC_UNLOCK(p);
95     }
96 #ifdef AFS_FBSD70_ENV
97     {
98       /* Avoid destroying socket until osi_NetReceive has
99        * had a chance to clean up */
100       int tries;
101       struct mtx s_mtx;
102
103       MUTEX_INIT(&s_mtx, "rx_shutdown_mutex", MUTEX_DEFAULT, 0);
104       MUTEX_ENTER(&s_mtx);
105       tries = 3;
106       while ((tries > 0) && (!so_is_disconn(rx_socket))) {
107           afs_warn("osi_StopListener: waiting (%d) ", tries);
108         msleep(&osi_StopListener, &s_mtx, PSOCK | PCATCH,
109                "rx_shutdown_timedwait", 1 * hz);
110         --tries;
111       }
112       soclose(rx_socket);
113       MUTEX_EXIT(&s_mtx);
114       MUTEX_DESTROY(&s_mtx);
115     }
116 #endif
117     if (haveGlock)
118         AFS_GLOCK();
119 }
120
121 int
122 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
123             int nvecs, afs_int32 alength, int istack)
124 {
125     afs_int32 code;
126     int i;
127     struct iovec iov[RX_MAXIOVECS];
128     struct uio u;
129     int haveGlock = ISAFS_GLOCK();
130
131     AFS_STATCNT(osi_NetSend);
132     if (nvecs > RX_MAXIOVECS)
133         osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
134
135     for (i = 0; i < nvecs; i++)
136         iov[i] = dvec[i];
137
138     u.uio_iov = &iov[0];
139     u.uio_iovcnt = nvecs;
140     u.uio_offset = 0;
141     u.uio_resid = alength;
142     u.uio_segflg = UIO_SYSSPACE;
143     u.uio_rw = UIO_WRITE;
144     u.uio_td = NULL;
145
146     addr->sin_len = sizeof(struct sockaddr_in);
147
148     if (haveGlock)
149         AFS_GUNLOCK();
150 #if KNET_DEBUG
151     printf("+");
152 #endif
153     code =
154         sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0,
155                curthread);
156 #if KNET_DEBUG
157     if (code) {
158         if (code == EINVAL)
159             Debugger("afs NetSend busted");
160         else
161             printf("z");
162     }
163 #endif
164     if (haveGlock)
165         AFS_GLOCK();
166     return code;
167 }
168 #else
169 /* This code *almost* works :( */
170 static struct protosw parent_proto;     /* udp proto switch */
171 static void rxk_input(struct mbuf *am, int iphlen);
172 static void rxk_fasttimo(void);
173
174 /* start intercepting basic calls */
175 rxk_init()
176 {
177     struct protosw *tpro, *last;
178     if (rxk_initDone)
179         return 0;
180
181     last = inetdomain.dom_protoswNPROTOSW;
182     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
183         if (tpro->pr_protocol == IPPROTO_UDP) {
184 #if 0                           /* not exported */
185             /* force UDP checksumming on for AFS    */
186             extern int udpcksum;
187             udpcksum = 1;
188 #endif
189             memcpy(&parent_proto, tpro, sizeof(parent_proto));
190             tpro->pr_input = rxk_input;
191             tpro->pr_fasttimo = rxk_fasttimo;
192             /*
193              * don't bother with pr_drain and pr_ctlinput
194              * until we have something to do
195              */
196             rxk_initDone = 1;
197             return 0;
198         }
199     osi_Panic("inet:no udp");
200 }
201
202
203 static void
204 rxk_input(struct mbuf *am, int iphlen)
205 {
206     void (*tproc) ();
207     unsigned short *tsp;
208     int hdr;
209     struct udphdr *tu;
210     struct ip *ti;
211     struct udpiphdr *tvu;
212     int i;
213     char *phandle;
214     afs_int32 code;
215     struct sockaddr_in taddr;
216     int tlen;
217     short port;
218     int data_len, comp_sum;
219
220     SPLVAR;
221     NETPRI;
222
223     /* make sure we have base ip and udp headers in first mbuf */
224     if (iphlen > sizeof(struct ip)) {
225         ip_stripoptions(am, NULL);
226         iphlen = sizeof(struct ip);
227     }
228
229     if (am->m_len < sizeof(struct udpiphdr)) {
230         am = m_pullup(am, sizeof(struct udpiphdr));
231         if (!am) {
232             USERPRI;
233             return;
234         }
235     }
236
237     ti = mtod(am, struct ip *);
238     /* skip basic ip hdr */
239     tu = (struct udphdr *)(((char *)ti) + sizeof(struct ip));
240
241     /* now read the port out */
242     port = tu->uh_dport;
243
244     if (port) {
245         for (tsp = rxk_ports, i = 0; i < MAXRXPORTS; i++) {
246             if (*tsp++ == port) {
247                 /* checksum the packet */
248                 /*
249                  * Make mbuf data length reflect UDP length.
250                  * If not enough data to reflect UDP length, drop.
251                  */
252                 tvu = (struct udpiphdr *)ti;
253                 tlen = ntohs((u_short) tvu->ui_ulen);
254                 if ((int)ti->ip_len != tlen) {
255                     if (tlen > (int)ti->ip_len) {
256                         m_free(am);
257                         USERPRI;
258                         return;
259                     }
260                     m_adj(am, tlen - (int)ti->ip_len);
261                 }
262                 /* deliver packet to rx */
263                 taddr.sin_family = AF_INET;     /* compute source address */
264                 taddr.sin_port = tu->uh_sport;
265                 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
266                 taddr.sin_len = sizeof(taddr);
267                 tvu = (struct udpiphdr *)ti;    /* virtual udp structure, for cksum */
268                 /* handle the checksum.  Note that this code damages the actual ip
269                  * header (replacing it with the virtual one, which is the same size),
270                  * so we must ensure we get everything out we need, first */
271                 if (tu->uh_sum != 0) {
272                     /* if the checksum is there, always check it. It's crazy not
273                      * to, unless you can really be sure that your
274                      * underlying network (and interfaces and drivers and
275                      * DMA hardware, etc!) is error-free. First, fill
276                      * in entire virtual ip header. */
277                     memset(tvu->ui_i.ih_x1, 0, 9);
278                     tvu->ui_len = tvu->ui_ulen;
279                     tlen = ntohs((unsigned short)(tvu->ui_ulen));
280                     if (in_cksum(am, sizeof(struct ip) + tlen)) {
281                         /* checksum, including cksum field, doesn't come out 0, so
282                          * this packet is bad */
283                         m_freem(am);
284                         USERPRI;
285                         return;
286                     }
287                 }
288
289                 /*
290                  * 28 is IP (20) + UDP (8) header.  ulen includes
291                  * udp header, and we *don't* tell RX about udp
292                  * header either.  So, we remove those 8 as well.
293                  */
294                 data_len = ntohs(tu->uh_ulen);
295                 data_len -= 8;
296                 if (!(*rxk_GetPacketProc) (&phandle, data_len)) {
297                     if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
298                         /* XXX should just increment counter here.. */
299                         printf("rx: truncated UDP packet\n");
300                         rxi_FreePacket(phandle);
301                     } else
302                         (*rxk_PacketArrivalProc) (phandle, &taddr,
303                                                   rxk_portRocks[i], data_len);
304                 } else
305                     m_freem(am);
306                 USERPRI;
307                 return;
308             }
309         }
310     }
311
312     /* if we get here, try to deliver packet to udp */
313     if (tproc = parent_proto.pr_input)
314         (*tproc) (am, iphlen);
315     USERPRI;
316     return;
317 }
318
319
320 /* 
321  * UDP fast timer to raise events for all but Solaris and NCR. 
322  * Called about 5 times per second (at unknown priority?).  Must go to
323  * splnet or obtain global lock before touching anything significant.
324  */
325 static void
326 rxk_fasttimo(void)
327 {
328     void (*tproc) ();
329     struct clock temp;
330
331     /* do rx fasttimo processing here */
332     rxevent_RaiseEvents(&temp);
333     if (tproc = parent_proto.pr_fasttimo)
334         (*tproc) ();
335 }
336
337 /* rx_NetSend - send asize bytes at adata from asocket to host at addr.
338  *
339  * Now, why do we allocate a new buffer when we could theoretically use the one
340  * pointed to by adata?  Because PRU_SEND returns after queueing the message,
341  * not after sending it.  If the sender changes the data after queueing it,
342  * we'd see the already-queued data change.  One attempt to fix this without
343  * adding a copy would be to have this function wait until the datagram is
344  * sent; however this doesn't work well.  In particular, if a host is down, and
345  * an ARP fails to that host, this packet will be queued until the ARP request
346  * comes back, which could be hours later.  We can't block in this routine that
347  * long, since it prevents RPC timeouts from happening.
348  */
349 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
350  * and just queue those.  XXX
351  */
352
353 /* set lock on sockbuf sb; can't call sblock since we're at interrupt level
354  * sometimes */
355 static
356 trysblock(sb)
357      struct sockbuf *sb;
358 {
359     AFS_STATCNT(trysblock);
360     if (sb->sb_flags & SB_LOCK) {
361         return -1;              /* can't lock socket */
362     }
363     sb->sb_flags |= SB_LOCK;
364     return 0;
365 }
366
367 /* We only have to do all the mbuf management ourselves if we can be called at
368    interrupt time. in RXK_LISTENER_ENV, we can just call sosend() */
369 int
370 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
371             int nvec, afs_int32 asize, int istack)
372 {
373     struct mbuf *tm, *um;
374     afs_int32 code;
375     int s;
376     struct mbuf *top = 0;
377     struct mbuf *m, **mp;
378     int len;
379     char *tdata;
380     caddr_t tpa;
381     int i, tl, rlen;
382     int mlen;
383     int haveGlock;
384 #if KNET_DEBUG
385     static int before = 0;
386 #endif
387
388     AFS_STATCNT(osi_NetSend);
389 /* Actually, the Ultrix way is as good as any for us, so we don't bother with
390  * special mbufs any more.  Used to think we could get away with not copying
391  * the data to the interface, but there's no way to tell the caller not to
392  * reuse the buffers after sending, so we lost out on that trick anyway */
393     s = splnet();
394     if (trysblock(&asocket->so_snd)) {
395         splx(s);
396         return 1;
397     }
398     mp = &top;
399     i = 0;
400     tdata = dvec[i].iov_base;
401     tl = dvec[i].iov_len;
402     while (1) {
403         mlen = MLEN;
404         if (top == 0) {
405             MGETHDR(m, M_DONTWAIT, MT_DATA);
406             if (!m) {
407                 sbunlock(&asocket->so_snd);
408                 splx(s);
409                 return 1;
410             }
411             mlen = MHLEN;
412             m->m_pkthdr.len = 0;
413             m->m_pkthdr.rcvif = NULL;
414         } else
415             MGET(m, M_DONTWAIT, MT_DATA);
416         if (!m) {
417             /* can't get an mbuf, give up */
418             if (top)
419                 m_freem(top);   /* free mbuf list we're building */
420             sbunlock(&asocket->so_snd);
421             splx(s);
422             return 1;
423         }
424         /*
425          * WARNING: the `4 * MLEN' is somewhat dubious.  It is better than
426          * `NBPG', which may have no relation to `CLBYTES'.  Also, `CLBYTES'
427          * may be so large that we never use clusters, resulting in far
428          * too many mbufs being used.  It is often better to briefly use
429          * a cluster, even if we are only using a portion of it.  Since
430          * we are on the xmit side, it shouldn't end up sitting on a queue
431          * for a potentially unbounded time (except perhaps if we are talking
432          * to ourself).
433          */
434         if (asize >= 4 * MLEN) {        /* try to get cluster mbuf */
435             /* different algorithms for getting cluster mbuf */
436             MCLGET(m, M_DONTWAIT);
437             if ((m->m_flags & M_EXT) == 0)
438                 goto nopages;
439             mlen = MCLBYTES;
440
441             /* now compute usable size */
442             len = MIN(mlen, asize);
443 /* Should I look at MAPPED_MBUFS??? */
444         } else {
445           nopages:
446             len = MIN(mlen, asize);
447         }
448         m->m_len = 0;
449         *mp = m;                /* XXXX */
450         top->m_pkthdr.len += len;
451         tpa = mtod(m, caddr_t);
452         while (len) {
453             rlen = MIN(len, tl);
454             memcpy(tpa, tdata, rlen);
455             asize -= rlen;
456             len -= rlen;
457             tpa += rlen;
458             m->m_len += rlen;
459             tdata += rlen;
460             tl -= rlen;
461             if (tl <= 0) {
462                 i++;
463                 if (i > nvec) {
464                     /* shouldn't come here! */
465                     asize = 0;  /* so we make progress toward completion */
466                     break;
467                 }
468                 tdata = dvec[i].iov_base;
469                 tl = dvec[i].iov_len;
470             }
471         }
472         *mp = m;
473         mp = &m->m_next;
474         if (asize <= 0)
475             break;
476     }
477     tm = top;
478
479     tm->m_act = NULL;
480
481     /* setup mbuf corresponding to destination address */
482     um = m_get(M_DONTWAIT, MT_SONAME);
483     if (!um) {
484         if (top)
485             m_freem(top);       /* free mbuf chain */
486         sbunlock(&asocket->so_snd);
487         splx(s);
488         return 1;
489     }
490     memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
491     addr->sin_len = um->m_len = sizeof(*addr);
492     /* note that udp_usrreq frees funny mbuf.  We hold onto data, but mbuf
493      * around it is gone. */
494     /*    haveGlock = ISAFS_GLOCK();
495      * if (haveGlock) {
496      * AFS_GUNLOCK();
497      * }  */
498     /* SOCKET_LOCK(asocket); */
499     /* code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0); */
500 #if KNET_DEBUG
501     if (before)
502         Debugger("afs NetSend before");
503 #endif
504     code =
505         (*asocket->so_proto->pr_usrreqs->pru_send) (asocket, 0, tm,
506                                                     (struct sockaddr *)
507                                                     addr, um, &proc0);
508     /* SOCKET_UNLOCK(asocket); */
509     /* if (haveGlock) {
510      * AFS_GLOCK();
511      * } */
512     sbunlock(&asocket->so_snd);
513     splx(s);
514 #if KNET_DEBUG
515     if (code) {
516         if (code == EINVAL)
517             Debugger("afs NetSend busted");
518         else
519             printf("z");
520     }
521 #endif
522     return code;
523 }
524 #endif