2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
17 #include "rx/rx_kcommon.h"
22 #include "inet/common.h"
23 #include "sys/tiuser.h"
24 #include "sys/t_kuser.h"
25 #include "sys/stropts.h"
26 #include "sys/stream.h"
27 #include "sys/tihdr.h"
28 #include "sys/fcntl.h"
30 #include "netinet/ip6.h"
31 #define ipif_local_addr ipif_lcl_addr
33 #define V4_PART_OF_V6(v6) v6.s6_addr32[3]
37 #include "inet/ip_if.h"
38 #include "netinet/udp.h"
41 * Function pointers for kernel socket routines
43 struct sonode *(*sockfs_socreate)
44 (vnode_t *, int, int, int, int, struct sonode *, int *) = NULL;
45 struct vnode *(*sockfs_solookup)
46 (int, int, int, char *, int *) = NULL;
48 (struct sonode *, struct sockaddr *, int, int, int) = NULL;
49 int (*sockfs_sorecvmsg)
50 (struct sonode *, struct nmsghdr *, struct uio *) = NULL;
51 int (*sockfs_sosendmsg)
52 (struct sonode *, struct nmsghdr *, struct uio *) = NULL;
53 int (*sockfs_sosetsockopt)
54 (struct sonode *, int, int, void *, int) = NULL;
55 #ifndef AFS_SUN510_ENV
56 int (*sockfs_sounbind)
57 (struct sonode *, int);
58 void (*sockfs_sockfree)
62 static afs_uint32 myNetAddrs[ADDRSPERSITE];
63 static int myNetMTUs[ADDRSPERSITE];
64 static int numMyNetAddrs = 0;
76 ill_walk_context_t ctx;
79 int mtus[ADDRSPERSITE];
80 afs_uint32 addrs[ADDRSPERSITE];
83 memset(mtus, 0, sizeof(mtus));
84 memset(addrs, 0, sizeof(addrs));
87 for (ill = ILL_START_WALK_ALL(&ctx) ; ill ; ill = ill_next(&ctx, ill)) {
89 for (ill = ill_g_head; ill; ill = ill->ill_next) {
92 /* Make sure this is an IPv4 ILL */
97 /* Iterate over all the addresses on this ILL */
98 for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
99 if (i >= ADDRSPERSITE)
102 /* Ignore addresses which are down.. */
103 if (!(ipif->ipif_flags & IFF_UP))
106 /* Compute the Rx interface MTU */
107 rxmtu = (ipif->ipif_mtu - RX_IPUDP_SIZE);
109 ifinaddr = ntohl(ipif->ipif_local_addr);
110 if (myNetAddrs[i] != ifinaddr)
113 /* Copy interface MTU and address; adjust maxmtu */
115 rxmtu = rxi_AdjustIfMTU(rxmtu);
117 rxmtu * rxi_nRecvFrags +
118 ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
119 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
123 if (ifinaddr != 0x7f000001 && maxmtu > rx_maxReceiveSize) {
124 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
126 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
131 rx_maxJumboRecvSize =
132 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
133 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
134 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
139 for (j = 0; j < i; j++) {
140 myNetMTUs[j] = mtus[j];
141 myNetAddrs[j] = addrs[j];
149 rxi_FindIfMTU(afs_uint32 addr)
153 afs_uint32 myAddr, netMask;
156 #ifdef AFS_SUN510_ENV
157 ill_walk_context_t ctx;
160 if (numMyNetAddrs == 0)
162 myAddr = ntohl(addr);
164 if (IN_CLASSA(myAddr))
165 netMask = IN_CLASSA_NET;
166 else if (IN_CLASSB(myAddr))
167 netMask = IN_CLASSB_NET;
168 else if (IN_CLASSC(myAddr))
169 netMask = IN_CLASSC_NET;
173 #ifdef AFS_SUN510_ENV
174 for (ill = ILL_START_WALK_ALL(&ctx) ; ill ; ill = ill_next(&ctx, ill)) {
176 for (ill = ill_g_head; ill; ill = ill->ill_next) {
179 /* Make sure this is an IPv4 ILL */
184 /* Iterate over all the addresses on this ILL */
185 for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) {
186 afs_uint32 thisAddr, subnetMask;
189 thisAddr = ipif->ipif_local_addr;
190 subnetMask = ipif->ipif_net_mask;
191 thisMtu = ipif->ipif_mtu;
193 if ((myAddr & netMask) == (thisAddr & netMask)) {
194 if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
195 if (myAddr == thisAddr) {
200 if (match_value < 3) {
206 if (match_value < 2) {
217 /* rxi_NewSocket, rxi_FreeSocket and osi_NetSend are from the now defunct
221 struct sockaddr_in rx_sockaddr;
223 /* Allocate a new socket at specified port in network byte order. */
225 rxk_NewSocket(short aport)
229 struct sockaddr_in addr;
233 AFS_STATCNT(osi_NewSocket);
235 if (sockfs_solookup == NULL) {
237 (struct vnode * (*)())modlookup("sockfs", "solookup");
238 if (sockfs_solookup == NULL) {
242 if (sockfs_socreate == NULL) {
244 (struct sonode * (*)())modlookup("sockfs", "socreate");
245 if (sockfs_socreate == NULL) {
249 if (sockfs_sobind == NULL) {
250 sockfs_sobind = (int (*)())modlookup("sockfs", "sobind");
251 if (sockfs_sobind == NULL) {
255 if (sockfs_sosetsockopt == NULL) {
256 sockfs_sosetsockopt = (int (*)())modlookup("sockfs", "sosetsockopt");
257 if (sockfs_sosetsockopt == NULL) {
261 if (sockfs_sosendmsg == NULL) {
262 sockfs_sosendmsg = (int (*)())modlookup("sockfs", "sosendmsg");
263 if (sockfs_sosendmsg == NULL) {
267 if (sockfs_sorecvmsg == NULL) {
268 sockfs_sorecvmsg = (int (*)())modlookup("sockfs", "sorecvmsg");
269 if (sockfs_sorecvmsg == NULL) {
273 #ifndef AFS_SUN510_ENV
274 if (sockfs_sounbind == NULL) {
275 sockfs_sounbind = (int (*)())modlookup("sockfs", "sounbind");
276 if (sockfs_sounbind == NULL)
279 if (sockfs_sockfree == NULL) {
280 sockfs_sockfree = (void (*)())modlookup("sockfs", "sockfree");
281 if (sockfs_sockfree == NULL)
286 accessvp = sockfs_solookup(AF_INET, SOCK_DGRAM, 0, "/dev/udp", &error);
287 if (accessvp == NULL) {
291 so = sockfs_socreate(accessvp, AF_INET, SOCK_DGRAM, 0, SOV_STREAM, NULL,
297 addr.sin_family = AF_INET;
298 addr.sin_port = aport;
299 addr.sin_addr.s_addr = INADDR_ANY;
300 error = sockfs_sobind(so, (struct sockaddr *)&addr, sizeof(addr), 0, 0);
306 error = sockfs_sosetsockopt(so, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len));
312 error = sockfs_sosetsockopt(so, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));
317 return (struct osi_socket *)so;
321 osi_FreeSocket(register struct osi_socket *asocket)
323 extern int rxk_ListenerPid;
324 struct sonode *so = (struct sonode *)asocket;
325 struct sockaddr_in taddr;
330 AFS_STATCNT(osi_FreeSocket);
332 taddr.sin_family = AF_INET;
333 taddr.sin_port = rx_port;
334 taddr.sin_addr.s_addr = htonl(0x7f000001);
339 while (rxk_ListenerPid) {
340 osi_NetSend(rx_socket, &taddr, &dvec, 1, 1, 0);
341 afs_osi_Sleep(&rxk_ListenerPid);
344 #ifdef AFS_SUN510_ENV
346 VOP_CLOSE(vp, FREAD|FWRITE, 1, (offset_t)0, CRED());
349 sockfs_sounbind(so, 0);
356 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
357 int nvecs, afs_int32 asize, int istack)
359 struct sonode *so = (struct sonode *)asocket;
362 struct iovec iov[RX_MAXIOVECS];
366 if (nvecs > RX_MAXIOVECS) {
367 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
370 msg.msg_name = (struct sockaddr *)addr;
371 msg.msg_namelen = sizeof(struct sockaddr_in);
373 msg.msg_iovlen = nvecs;
374 msg.msg_control = NULL;
375 msg.msg_controllen = 0;
378 for (i = 0; i < nvecs; i++) {
379 iov[i].iov_base = dvec[i].iov_base;
380 iov[i].iov_len = dvec[i].iov_len;
382 uio.uio_iov = &iov[0];
383 uio.uio_iovcnt = nvecs;
385 uio.uio_segflg = UIO_SYSSPACE;
386 uio.uio_fmode = FREAD | FWRITE;
388 uio.uio_resid = asize;
390 error = sockfs_sosendmsg(so, &msg, &uio);
396 osi_NetReceive(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
397 int nvecs, int *alength)
399 struct sonode *asocket = (struct sonode *)so;
402 struct iovec iov[RX_MAXIOVECS];
406 if (nvecs > RX_MAXIOVECS) {
407 osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
411 msg.msg_namelen = sizeof(struct sockaddr_in);
414 msg.msg_control = NULL;
415 msg.msg_controllen = 0;
418 for (i = 0; i < nvecs; i++) {
419 iov[i].iov_base = dvec[i].iov_base;
420 iov[i].iov_len = dvec[i].iov_len;
422 uio.uio_iov = &iov[0];
423 uio.uio_iovcnt = nvecs;
425 uio.uio_segflg = UIO_SYSSPACE;
428 uio.uio_resid = *alength;
430 error = sockfs_sorecvmsg(asocket, &msg, &uio);
432 if (msg.msg_name == NULL) {
435 memcpy(addr, msg.msg_name, msg.msg_namelen);
436 kmem_free(msg.msg_name, msg.msg_namelen);
437 *alength = *alength - uio.uio_resid;
441 if (error == EINTR && ISSIG(curthread, FORREAL)) {
442 klwp_t *lwp = ttolwp(curthread);
443 proc_t *p = ttoproc(curthread);
444 int sig = lwp->lwp_cursig;
446 if (sig == SIGKILL) {
447 mutex_enter(&p->p_lock);
448 p->p_flag &= ~SKILLED;
449 mutex_exit(&p->p_lock);
452 if (lwp->lwp_curinfo) {
453 siginfofree(lwp->lwp_curinfo);
454 lwp->lwp_curinfo = NULL;
462 shutdown_rxkernel(void)
467 osi_StopListener(void)
469 osi_FreeSocket(rx_socket);
472 #else /* AFS_SUN56_ENV */
474 #include "inet/common.h"
475 #include "sys/tiuser.h"
476 #include "sys/t_kuser.h"
477 #include "sys/ioctl.h"
478 #include "sys/stropts.h"
479 #include "sys/stream.h"
480 #include "sys/strsubr.h"
481 #include "sys/vnode.h"
482 #include "sys/stropts.h"
483 #include "sys/tihdr.h"
484 #include "sys/timod.h"
485 #include "sys/fcntl.h"
486 #include "sys/debug.h"
487 #include "inet/common.h"
489 #include "netinet/udp.h"
491 extern dev_t afs_udp_rdev;
501 /* rxi_NewSocket, rxi_FreeSocket and osi_NetSend are from the now defunct
505 dev_t afs_udp_rdev = (dev_t) 0;
507 /* Allocate a new socket at specified port in network byte order. */
509 rxk_NewSocket(short aport)
512 struct t_bind *reqp, *rspp;
514 struct sockaddr_in *myaddrp;
518 AFS_STATCNT(osi_NewSocket);
519 afs_udp_rdev = makedevice(11 /*CLONE*/, ddi_name_to_major("udp"));
520 code = t_kopen(NULL, afs_udp_rdev, FREAD | FWRITE, &udp_tiptr, CRED());
522 return (struct osi_socket *)0;
525 code = t_kalloc(udp_tiptr, T_BIND, T_ADDR, (char **)&reqp);
527 t_kclose(udp_tiptr, 0);
529 code = t_kalloc(udp_tiptr, T_BIND, T_ADDR, (char **)&rspp);
531 t_kfree(udp_tiptr, (char *)reqp, T_BIND);
532 t_kclose(udp_tiptr, 0);
533 return (struct osi_socket *)0;
536 reqp->addr.len = sizeof(struct sockaddr_in);
537 myaddrp = (struct sockaddr_in *)reqp->addr.buf;
538 myaddrp->sin_family = AF_INET;
539 myaddrp->sin_port = aport;
540 myaddrp->sin_addr.s_addr = INADDR_ANY; /* XXX Was 0 XXX */
542 code = t_kbind(udp_tiptr, reqp, rspp);
544 t_kfree(udp_tiptr, (char *)reqp, T_BIND);
545 t_kfree(udp_tiptr, (char *)rspp, T_BIND);
546 t_kclose(udp_tiptr, 0);
547 return (struct osi_socket *)0;
549 if (memcmp(reqp->addr.buf, rspp->addr.buf, rspp->addr.len)) {
550 t_kfree(udp_tiptr, (char *)reqp, T_BIND);
551 t_kfree(udp_tiptr, (char *)rspp, T_BIND);
552 t_kclose(udp_tiptr, 0);
553 return (struct osi_socket *)0;
555 t_kfree(udp_tiptr, (char *)reqp, T_BIND);
556 t_kfree(udp_tiptr, (char *)rspp, T_BIND);
559 * Set the send and receive buffer sizes.
561 stp = udp_tiptr->fp->f_vnode->v_stream;
563 q->q_hiwat = rx_UdpBufSize;
564 q->q_next->q_hiwat = rx_UdpBufSize;
565 RD(q)->q_hiwat = rx_UdpBufSize;
567 return (struct osi_socket *)udp_tiptr;
572 osi_FreeSocket(register struct osi_socket *asocket)
574 extern int rxk_ListenerPid;
575 TIUSER *udp_tiptr = (TIUSER *) asocket;
576 AFS_STATCNT(osi_FreeSocket);
578 if (rxk_ListenerPid) {
579 kill(rxk_ListenerPid, SIGUSR1);
580 afs_osi_Sleep(&rxk_ListenerPid);
587 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
588 int nvecs, afs_int32 asize, int istack)
592 TIUSER *udp_tiptr = (TIUSER *) asocket;
593 struct t_kunitdata *udreq;
594 struct sockaddr_in sin;
599 * XXX We don't do any checking on the family since it's assumed to be
602 sin.sin_family = AF_INET;
603 sin.sin_addr.s_addr = addr->sin_addr.s_addr;
604 sin.sin_port = addr->sin_port;
607 * Get a buffer for the RX header
610 osi_Panic("osi_NetSend, nvecs=%d\n", nvecs);
612 while (!(bp = allocb(dvec[0].iov_len, BPRI_LO))) {
613 if (strwaitbuf(dvec[i].iov_len, BPRI_LO)) {
618 /* Copy the data into the buffer */
619 memcpy((char *)bp->b_wptr, (char *)dvec[0].iov_base, dvec[0].iov_len);
620 bp->b_datap->db_type = M_DATA;
621 bp->b_wptr += dvec[0].iov_len;
624 * Append each element in the iovec to the buffer
626 for (i = 1; i < nvecs; i++) {
627 /* Get a buffer for the next chunk */
628 while (!(dbp = allocb(dvec[i].iov_len, BPRI_LO))) {
629 if (strwaitbuf(dvec[i].iov_len, BPRI_LO)) {
635 /* Copy the data into the buffer */
636 memcpy((char *)dbp->b_wptr, (char *)dvec[i].iov_base,
638 dbp->b_datap->db_type = M_DATA;
639 dbp->b_wptr += dvec[i].iov_len;
641 /* Append it to the message buffer */
646 * Allocate and format the unitdata structure.
648 code = t_kalloc(udp_tiptr, T_UNITDATA, T_UDATA, (char **)&udreq);
651 printf("osi_NetSend: t_kalloc failed %d\n", code);
654 udreq->addr.len = sizeof(struct sockaddr_in);
655 udreq->addr.maxlen = sizeof(struct sockaddr_in);
657 (char *)kmem_alloc(sizeof(struct sockaddr_in), KM_SLEEP);
659 udreq->opt.maxlen = 0;
660 memcpy(udreq->addr.buf, (char *)&sin, sizeof(struct sockaddr_in));
661 udreq->udata.udata_mp = bp;
662 udreq->udata.len = asize;
664 code = t_ksndudata(udp_tiptr, udreq, NULL);
666 printf("osi_NetSend: t_ksndudata failed %d\n", code);
669 t_kfree(udp_tiptr, (caddr_t) udreq, T_UNITDATA);
675 osi_NetReceive(struct osi_socket *asocket, struct sockaddr_in *addr,
676 struct iovec *dvec, int nvecs, int *alength)
679 TIUSER *udp_tiptr = (TIUSER *) asocket;
680 struct t_kunitdata *udreq;
694 * Allocate the unitdata structure.
696 code = t_kalloc(udp_tiptr, T_UNITDATA, T_UDATA, (char **)&udreq);
698 printf("osi_NetReceive: t_kalloc failed %d\n", code);
701 udreq->addr.len = sizeof(struct sockaddr_in);
702 udreq->addr.maxlen = sizeof(struct sockaddr_in);
704 (char *)kmem_alloc(sizeof(struct sockaddr_in), KM_SLEEP);
706 udreq->opt.maxlen = 0;
709 * Loop until we get an error or receive some data.
713 * Wait until there is something to do
715 code = t_kspoll(udp_tiptr, -1, READWAIT, &events);
717 osi_Panic("osi_NetReceive, infinite t_kspoll timed out\n");
720 * If there is a message then read it in
723 code = t_krcvudata(udp_tiptr, udreq, &type, &error);
727 * Block attempts to kill this thread
729 if (code == EINTR && ISSIG(curthread, FORREAL)) {
730 klwp_t *lwp = ttolwp(curthread);
731 proc_t *p = ttoproc(curthread);
732 int sig = lwp->lwp_cursig;
734 if (sig == SIGKILL) {
735 mutex_enter(&p->p_lock);
736 p->p_flag &= ~SKILLED;
737 mutex_exit(&p->p_lock);
740 if (lwp->lwp_curinfo) {
741 kmem_free((caddr_t) lwp->lwp_curinfo,
742 sizeof(*lwp->lwp_curinfo));
743 lwp->lwp_curinfo = NULL;
752 * Ignore non-data message types
754 if (type != T_DATA) {
759 * Save the source address
761 memcpy((char *)addr, udreq->addr.buf, sizeof(struct sockaddr_in));
764 * Copy out the message buffers, take care not to overflow
767 dbp = udreq->udata.udata_mp;
769 for (i = 0; dbp != NULL && length > 0 && i < nvecs; i++) {
770 tlen = dvec[i].iov_len;
771 tbase = dvec[i].iov_base;
775 while (dbp != NULL && tlen > 0) {
776 blen = dbp->b_wptr - dbp->b_rptr;
778 memcpy(tbase, (char *)dbp->b_rptr, tlen);
783 memcpy(tbase, (char *)dbp->b_rptr, blen);
791 *alength = *alength - length;
795 t_kfree(udp_tiptr, (caddr_t) udreq, T_UNITDATA);
801 osi_StopListener(void)
803 osi_FreeSocket(rx_socket);
808 shutdown_rxkernel(void)
813 #endif /* AFS_SUN56_ENV */
814 #endif /* AFS_SUN5_ENV */