4128f9e87ba588e299bb348b46516d5c30dd94c0
[openafs.git] / src / rx / IRIX / 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 RCSID("$Header$");
14
15 #include "../rx/rx_kcommon.h"
16 #include "../h/tcp-param.h"
17 /* This must be loaded after proc.h to avoid macro collision with a variable*/
18 #include "../netinet/udp_var.h"
19
20
21
22
23 #ifdef RXK_LISTENER_ENV
24 /* osi_NetReceive
25  * OS dependent part of kernel RX listener thread.
26  *
27  * Arguments:
28  *      so      socket to receive on, typically rx_socket
29  *      from    pointer to a sockaddr_in. 
30  *      iov     array of iovecs to fill in.
31  *      iovcnt  how many iovecs there are.
32  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
33  *
34  * Return
35  * 0 if successful
36  * error code (such as EINTR) if not
37  *
38  * Environment
39  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
40  *      so we have a little space to look for packets larger than 
41  *      rx_maxReceiveSize.
42  */
43 int rxk_lastSocketError = 0;
44 int rxk_nSocketErrors = 0;
45 int rxk_nSignalsCleared = 0;
46 int osi_NetReceive(osi_socket so, struct sockaddr_in *from, 
47                    struct iovec *iov, int iovcnt, int *lengthp)
48 {
49     struct uio tuio;
50     int code;
51     struct mbuf *maddr = NULL;
52     struct sockaddr_in *taddr;
53     struct iovec tmpvec[RX_MAXWVECS+2];
54 #ifdef AFS_SGI65_ENV
55     bhv_desc_t bhv;
56     BHV_PDATA(&bhv) = (void*)so;
57 #endif
58
59     tuio.uio_iov = tmpvec;
60     tuio.uio_iovcnt = iovcnt;
61     tuio.uio_offset = 0;
62     tuio.uio_segflg = AFS_UIOSYS;
63     tuio.uio_fmode = 0;
64     tuio.uio_resid = *lengthp;
65     tuio.uio_pio = 0;
66     tuio.uio_pbuf = 0;
67
68     if (iovcnt > RX_MAXWVECS+2) {
69         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
70     }
71     memcpy(tmpvec, (char*)iov, (RX_MAXWVECS+1) * sizeof(struct iovec));
72 #ifdef AFS_SGI65_ENV
73     code = soreceive(&bhv, &maddr, &tuio, NULL, NULL);
74 #else
75     code = soreceive(so, &maddr, &tuio, NULL, NULL);
76 #endif
77
78     if (code) {
79 #ifdef AFS_SGI65_ENV
80         /* Clear the error before using the socket again. I've tried being nice
81          * and blocking SIGKILL and SIGSTOP from the kernel, but they get
82          * delivered anyway. So, time to be crude and just clear the signals
83          * pending on this thread.
84          */
85         if (code == EINTR) {
86             uthread_t *ut = curuthread;
87             int s;
88             s = ut_lock(ut);
89             sigemptyset(&ut->ut_sig);
90             ut->ut_cursig = 0;
91             thread_interrupt_clear(UT_TO_KT(ut), 1);
92             ut_unlock(ut, s);
93             rxk_nSignalsCleared++;
94         }
95 #endif
96         /* Clear the error before using the socket again. */
97         so->so_error = 0;
98         rxk_lastSocketError = code;
99         rxk_nSocketErrors ++ ;
100         if (maddr)
101             m_freem(maddr);
102     }
103     else {
104         *lengthp = *lengthp - tuio.uio_resid;
105         if (maddr) {
106             memcpy((char*)from, (char*)mtod(maddr, struct sockaddr_in *),
107                   sizeof(struct sockaddr_in));
108             m_freem(maddr);
109         }
110         else {
111             return -1;
112         }
113     }
114     return code;
115 }
116 #else /* RXK_LISTENER_ENV */
117
118 static struct protosw parent_proto;     /* udp proto switch */
119
120 /*
121  * RX input, fast timer and initialization routines. 
122  */
123
124 #ifdef AFS_SGI64_ENV
125 static void rxk_input(struct mbuf *am, struct ifnet *aif, struct ipsec * spec)
126 #else
127 static void rxk_input(struct mbuf *am, struct ifnet *aif)
128 #endif
129 {
130     void (*tproc)();
131     register unsigned short *tsp;
132     int hdr;
133     struct udphdr *tu;
134     register struct ip *ti;
135     struct udpiphdr *tvu;
136     register int i;
137     char *phandle;
138     struct sockaddr_in taddr;
139     int tlen;
140     short port;
141     int data_len;
142
143     /* make sure we have base ip and udp headers in first mbuf */
144     if (am->m_off > MMAXOFF || am->m_len < 28) {
145         am = m_pullup(am, 28);
146         if (!am) return;
147     }
148
149     hdr = (mtod(am, struct ip *))->ip_hl;
150     if (hdr > 5) {
151         /* pull up more, the IP hdr is bigger than usual */
152         if (am->m_len < (8 + (hdr<<2))) {
153             am = m_pullup(am, 8+(hdr<<2));
154             if (!am) return;
155         }
156         ti = mtod(am, struct ip *); /* recompute, since m_pullup allocates new mbuf */
157         tu = (struct udphdr *)(((char *)ti) + (hdr<<2)); /* skip ip hdr */
158     }
159     else {
160         ti = mtod(am, struct ip *);
161         tu = (struct udphdr *)(((char *)ti) + 20);      /* skip basic ip hdr */
162     }
163     /* now read the port out */
164     port = tu->uh_dport;
165
166     if (port) {
167         for(tsp=rxk_ports, i=0; i<MAXRXPORTS;i++) {
168             if (*tsp++ == port) {
169                 /* checksum the packet */
170                 if (hdr > 5) {
171                     ip_stripoptions(am, (struct mbuf *) 0); /* get rid of anything we don't need */
172                     tu = (struct udphdr *)(((char *)ti) + 20);
173                 }
174                 /*
175                  * Make mbuf data length reflect UDP length.
176                  * If not enough data to reflect UDP length, drop.
177                  */
178                 tvu = (struct udpiphdr *)ti;
179                 tlen = ntohs((u_short)tvu->ui_ulen);
180                 if ((int)ti->ip_len != tlen) {
181                     if (tlen > (int)ti->ip_len) {
182                         m_free(am);
183                         return;
184                     }
185                     m_adj(am, tlen - (int)ti->ip_len);
186                 }
187                 /* deliver packet to rx */
188                 taddr.sin_family = AF_INET;         /* compute source address */
189                 taddr.sin_port = tu->uh_sport;
190                 taddr.sin_addr.s_addr = ti->ip_src.s_addr;
191                 /* handle the checksum.  Note that this code damages the actual ip
192                    header (replacing it with the virtual one, which is the same size),
193                    so we must ensure we get everything out we need, first */
194                 if ( tu->uh_sum != 0) {
195                         /* if the checksum is there, always check it. It's crazy not
196                          * to, unless you can really be sure that your
197                          * underlying network (and interfaces and drivers and
198                          * DMA hardware, etc!) is error-free. First, fill
199                          * in entire virtual ip header. */
200                         tvu->ui_next = 0;
201                         tvu->ui_prev = 0;
202                         tvu->ui_x1 = 0;
203                         tvu->ui_len = tvu->ui_ulen;
204                         tlen = ntohs((unsigned short)(tvu->ui_ulen));
205                 if ((!(am->m_flags & M_CKSUMMED)) &&
206                         in_cksum(am, sizeof(struct ip) + tlen)){
207                             /* checksum, including cksum field, doesn't come out 0, so
208                                this packet is bad */
209                             m_freem(am);
210                             return;
211                         }
212                       }
213
214                 /*
215                  * 28 is IP (20) + UDP (8) header.  ulen includes
216                  * udp header, and we *don't* tell RX about udp
217                  * header either.  So, we remove those 8 as well.
218                  */
219                 data_len = ntohs(tu->uh_ulen);
220                 data_len -= 8;
221                 if (!(*rxk_GetPacketProc)(&phandle, data_len)) {
222                   if (rx_mb_to_packet(am, m_freem, 28, data_len, phandle)) {
223                     /* XXX should just increment counter here.. */
224                     printf("rx: truncated UDP packet\n");
225                     rxi_FreePacket(phandle);
226                   }
227                   else 
228                     (*rxk_PacketArrivalProc)(phandle, &taddr,
229                                              rxk_portRocks[i], data_len);
230                 }else m_freem(am);
231                 return;
232                 }
233             }
234         }
235
236     /* if we get here, try to deliver packet to udp */
237     if (tproc = parent_proto.pr_input) (*tproc)(am, aif);
238     return;
239 }
240
241 /* 
242  * UDP fast timer to raise events for all but Solaris and NCR. 
243  * Called about 5 times per second (at unknown priority?).  Must go to
244  * splnet or obtain global lock before touching anything significant.
245  */
246 static void rxk_fasttimo (void)
247 {
248     int (*tproc)();
249     struct clock temp;
250
251     /* do rx fasttimo processing here */
252     rxevent_RaiseEvents(&temp);
253     if (tproc = parent_proto.pr_fasttimo) (*tproc)();
254 }
255
256
257 /* start intercepting basic calls */
258 void rxk_init(void) {
259     register struct protosw *tpro, *last;
260     if (rxk_initDone) return;
261
262     last = inetdomain.dom_protoswNPROTOSW;
263     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++) {
264         if (tpro->pr_protocol == IPPROTO_UDP) {
265             memcpy(&parent_proto, tpro, sizeof(parent_proto));
266             tpro->pr_input = rxk_input;
267             tpro->pr_fasttimo = rxk_fasttimo;
268             rxk_initDone = 1;
269             return;
270         }
271     }
272     osi_Panic("inet:no udp");
273 }
274 #endif /* RXK_LISTENER_ENV */
275
276 /*
277  * RX IP address routines.
278  */
279
280 static afs_uint32 myNetAddrs[ADDRSPERSITE];
281 static int myNetMTUs[ADDRSPERSITE];
282 static int myNetFlags[ADDRSPERSITE];
283 static int numMyNetAddrs = 0;
284
285 /* This version doesn't even begin to handle iterative requests, but then
286  * we don't yet use them anyway. Fix this when rxi_InitPeerParams is changed
287  * to find a true maximum.
288  */
289 static int rxi_MatchIfnet(struct hashbucket *h, caddr_t key, caddr_t arg1,
290               caddr_t arg2)
291 {
292     afs_uint32 ppaddr = *(afs_uint32*)key;
293     int match_value = *(int*)arg1;
294     struct in_ifaddr *ifa = (struct in_ifaddr*)h;
295     struct sockaddr_in *sin;
296
297     if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
298         if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
299             sin=IA_SIN(ifa);
300             if ( sin->sin_addr.s_addr == ppaddr) {   /* ie, ME!!!  */
301                 match_value = 4;
302                 *(struct in_ifaddr**)arg2 = ifa;
303             }
304             if (match_value < 3) {
305                 *(struct in_ifaddr**)arg2 = ifa;
306                 match_value = 3;
307             }
308         }
309         else {
310             if (match_value < 2) {
311                 *(struct in_ifaddr**)arg2 = ifa;
312                 match_value = 2;
313             }
314         }
315     }
316     *(int*)arg1 = match_value;
317     return 0;
318 }
319
320     
321 struct ifnet * rxi_FindIfnet(addr, pifad) 
322      afs_uint32 addr;
323      struct in_ifaddr **pifad;
324 {
325   afs_uint32 ppaddr;
326   int match_value = 0;
327
328   if (numMyNetAddrs == 0)
329     (void) rxi_GetIFInfo();
330
331   ppaddr = ntohl(addr);
332   *pifad = (struct in_ifaddr*)&hashinfo_inaddr;
333
334   (void) hash_enum(&hashinfo_inaddr, rxi_MatchIfnet, HTF_INET,
335                    (caddr_t)&ppaddr, (caddr_t)&match_value, (caddr_t)pifad);
336    
337   if (match_value)
338       return (*pifad)->ia_ifp;
339   else
340       return NULL;
341 }
342
343 static int rxi_EnumGetIfInfo(struct hashbucket *h, caddr_t key, caddr_t arg1,
344                   caddr_t arg2)
345 {
346     int different = *(int*)arg1;
347     int i = *(int*)arg2;
348     struct in_ifaddr *iap = (struct in_ifaddr*)h;
349     struct ifnet *ifnp;
350     afs_uint32 ifinaddr;
351     afs_uint32 rxmtu;
352
353     if (i>=ADDRSPERSITE)
354         return 0;
355     
356     ifnp = iap->ia_ifp;
357     rxmtu = (ifnp->if_mtu - RX_IPUDP_SIZE);
358     ifinaddr = ntohl(iap->ia_addr.sin_addr.s_addr);
359     if (myNetAddrs[i] != ifinaddr) { 
360         myNetAddrs[i] = ifinaddr;
361         myNetMTUs[i] = rxmtu;
362         different++;
363         *(int*)arg1 = different;
364     }
365     rxmtu = rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
366     if ( ( ifinaddr != 0x7f000001 ) &&
367         (rxmtu > rx_maxReceiveSize) ) {
368         rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, rxmtu);
369         rx_maxReceiveSize = MIN( rx_maxReceiveSize, rx_maxReceiveSizeUser);
370     }
371
372     *(int*)arg2 = i + 1;
373     return 0;
374 }
375
376 int rxi_GetIFInfo()
377 {
378     int i = 0;
379     int different = 0;
380
381     /* SGI 6.2 does not have a pointer from the ifnet to the list of
382      * of addresses (if_addrlist). So it's more efficient to run the
383      * in_ifaddr list and use the back pointers to the ifnet struct's.
384      */
385     (void) hash_enum(&hashinfo_inaddr, rxi_EnumGetIfInfo, HTF_INET,
386                      NULL, (caddr_t)&different, (caddr_t)&i);
387
388     rx_maxJumboRecvSize = RX_HEADER_SIZE
389                           + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
390                           + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
391     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
392
393     return different;
394 }
395
396 /* osi_NetSend - from the now defunct afs_osinet.c */
397 #ifdef DEBUG
398 #undef DEBUG
399 #endif
400 #ifdef MP
401 #define _MP_NETLOCKS
402 #endif
403
404 #ifdef AFS_SGI65_ENV
405 osi_NetSend(asocket, addr, dvec, nvec, asize, istack)
406      register struct osi_socket *asocket;
407      struct iovec *dvec;
408      int nvec;
409      register afs_int32 asize;
410      struct sockaddr_in *addr;
411      int istack;
412 {
413     int code;
414     struct iovec tvecs[RX_MAXWVECS+1];
415     struct iovec *iovp;
416     struct uio tuio;
417     struct mbuf *to;
418     int i;
419     bhv_desc_t bhv;
420
421     if (nvec > RX_MAXWVECS+1) {
422         osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvec);
423     }
424     memcpy((char*)tvecs, (char*)dvec, nvec * sizeof(struct iovec));
425
426     tuio.uio_iov = tvecs;
427     tuio.uio_iovcnt = nvec;
428     tuio.uio_segflg = UIO_SYSSPACE;
429     tuio.uio_offset = 0;
430     tuio.uio_sigpipe = 0;
431     tuio.uio_pio = 0;
432     tuio.uio_pbuf = 0;
433
434     tuio.uio_resid = 0;
435     for (i=0, iovp = tvecs; i<nvec; i++, iovp++)
436         tuio.uio_resid += iovp->iov_len;
437
438
439     to = m_get(M_WAIT, MT_SONAME);
440     to->m_len = sizeof(struct sockaddr_in);
441     memcpy(mtod(to, caddr_t), (char*)addr, to->m_len);
442
443     BHV_PDATA(&bhv) = (void*)asocket;
444     code = sosend(&bhv, to, &tuio, 0, NULL);
445
446     m_freem(to);
447     return code;
448 }
449 #else /* AFS_SGI65_ENV */
450
451 int dummy_sblock(struct sockbuf *a, int b,  struct socket *c, int *d, int e)
452 {
453     afs_warn("sblock was called before it was installed. Install proper afsd.\n");
454 }
455 void dummy_sbunlock(struct sockbuf *a, int b,  struct socket *c, int d)
456 {
457     afs_warn("sbunlock was called before it was installed. Install proper afsd.\n");
458 }
459
460 int (*afs_sblockp)(struct sockbuf*, int, struct socket*, int*, int) =
461      dummy_sblock;
462 void (*afs_sbunlockp)(struct sockbuf*, int, struct socket*, int) =
463      dummy_sbunlock;
464 #define AFS_SBUNLOCK(SB, EV, SO, O) (*afs_sbunlockp)(SB, EV, SO, O)
465
466 /* osi_NetSend - send asize bytes at adata from asocket to host at addr.
467  *
468  * Now, why do we allocate a new buffer when we could theoretically use the one
469  * pointed to by adata?  Because PRU_SEND returns after queueing the message,
470  * not after sending it.  If the sender changes the data after queueing it,
471  * we'd see the already-queued data change.  One attempt to fix this without
472  * adding a copy would be to have this function wait until the datagram is
473  * sent; however this doesn't work well.  In particular, if a host is down, and
474  * an ARP fails to that host, this packet will be queued until the ARP request
475  * comes back, which could be hours later.  We can't block in this routine that
476  * long, since it prevents RPC timeouts from happening.
477  */
478 /* XXX In the brave new world, steal the data bufs out of the rx_packet iovec,
479  * and just queue those.  XXX
480  */
481 int 
482 osi_NetSend(asocket, addr, dvec, nvec, asize, istack)
483      register struct socket *asocket;
484      struct iovec *dvec;
485      int nvec;
486      register afs_int32 asize;
487      struct sockaddr_in *addr;
488      int istack;
489 {
490     register struct mbuf *tm, *um;
491     register afs_int32 code;
492     int s;
493     struct mbuf *top = 0;
494     register struct mbuf *m, **mp;
495     int len;
496     char *tdata;
497     caddr_t tpa;
498     int i,tl,rlen;
499
500     NETSPL_DECL(s1)
501     AFS_STATCNT(osi_NetSend);
502
503     (*afs_sblockp)(&asocket->so_snd, NETEVENT_SODOWN, asocket, &s1, istack);
504
505     s = splnet();
506     mp = &top;
507     i = 0;
508     tdata = dvec[i].iov_base;
509     tl = dvec[i].iov_len;
510     while (1) {
511         if ((m = m_vget(M_DONTWAIT, MIN(asize, VCL_MAX), MT_DATA)) == NULL) {
512             if (top) m_freem(top);
513             splx(s);
514             AFS_SBUNLOCK(&asocket->so_snd, NETEVENT_SODOWN, asocket, s1);
515             return 1;
516         }
517         len = MIN(m->m_len, asize);
518         m->m_len = 0;
519         tpa = mtod(m, caddr_t);
520         while (len) {
521           rlen = MIN(len, tl);
522           memcpy(tpa, tdata, rlen);
523           asize -= rlen;
524           len -= rlen;
525           tpa += rlen;
526           m->m_len += rlen;
527           tdata += rlen;
528           tl -= rlen;
529           if (tl <= 0) {
530             i++;
531             if (i > nvec) {
532               /* shouldn't come here! */
533               asize = 0;   /* so we make progress toward completion */
534               break;
535             }
536             tdata = dvec[i].iov_base;
537             tl = dvec[i].iov_len;
538           }
539         }
540         *mp = m;
541         mp = &m->m_next;
542         if (asize <= 0)
543           break;
544     }
545     tm = top;
546
547     tm->m_act = (struct mbuf *) 0;
548
549     /* setup mbuf corresponding to destination address */
550     um = m_get(M_DONTWAIT, MT_SONAME);
551     if (!um) {
552         if (top) m_freem(top);  /* free mbuf chain */
553         /* if this were vfs40, we'd do sbunlock(asocket, &asocket->so_snd), but
554            we don't do the locking at all for vfs40 systems */
555         splx(s);
556         AFS_SBUNLOCK(&asocket->so_snd, NETEVENT_SODOWN, asocket, s1);
557         return 1;
558     }
559     memcpy(mtod(um, caddr_t), addr, sizeof(*addr));
560     um->m_len = sizeof(*addr);
561     /* note that udp_usrreq frees funny mbuf.  We hold onto data, but mbuf
562      * around it is gone.  we free address ourselves.  */
563     code = (*asocket->so_proto->pr_usrreq)(asocket, PRU_SEND, tm, um, 0);
564     splx(s);
565     m_free(um);
566     AFS_SBUNLOCK(&asocket->so_snd, NETEVENT_SODOWN, asocket, s1);
567
568     return code;
569 }
570 #endif /* AFS_SGI65_ENV */
571