Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_40.c revision 1.4.2.2
      1  1.4.2.2  ad /*	$NetBSD: uipc_syscalls_40.c,v 1.4.2.2 2007/06/09 21:37:08 ad Exp $	*/
      2  1.4.2.2  ad 
      3  1.4.2.2  ad /* written by Pavel Cahyna, 2006. Public domain. */
      4  1.4.2.2  ad 
      5  1.4.2.2  ad #include <sys/cdefs.h>
      6  1.4.2.2  ad __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.4.2.2 2007/06/09 21:37:08 ad Exp $");
      7  1.4.2.2  ad 
      8  1.4.2.2  ad /*
      9  1.4.2.2  ad  * System call interface to the socket abstraction.
     10  1.4.2.2  ad  */
     11  1.4.2.2  ad 
     12  1.4.2.2  ad #include <sys/param.h>
     13  1.4.2.2  ad #include <sys/kernel.h>
     14  1.4.2.2  ad #include <sys/msg.h>
     15  1.4.2.2  ad #include <sys/sysctl.h>
     16  1.4.2.2  ad #include <sys/mount.h>
     17  1.4.2.2  ad #include <sys/syscallargs.h>
     18  1.4.2.2  ad #include <sys/errno.h>
     19  1.4.2.2  ad 
     20  1.4.2.2  ad #include <net/if.h>
     21  1.4.2.2  ad 
     22  1.4.2.2  ad #include <compat/sys/socket.h>
     23  1.4.2.2  ad #include <compat/sys/sockio.h>
     24  1.4.2.2  ad 
     25  1.4.2.2  ad #ifdef COMPAT_OIFREQ
     26  1.4.2.2  ad /*
     27  1.4.2.2  ad  * Return interface configuration
     28  1.4.2.2  ad  * of system.  List may be used
     29  1.4.2.2  ad  * in later ioctl's (above) to get
     30  1.4.2.2  ad  * other information.
     31  1.4.2.2  ad  */
     32  1.4.2.2  ad /*ARGSUSED*/
     33  1.4.2.2  ad int
     34  1.4.2.2  ad compat_ifconf(u_long cmd, void *data)
     35  1.4.2.2  ad {
     36  1.4.2.2  ad 	struct oifconf *ifc = data;
     37  1.4.2.2  ad 	struct ifnet *ifp;
     38  1.4.2.2  ad 	struct ifaddr *ifa;
     39  1.4.2.2  ad 	struct oifreq ifr, *ifrp;
     40  1.4.2.2  ad 	int space, error = 0;
     41  1.4.2.2  ad 	const int sz = (int)sizeof(ifr);
     42  1.4.2.2  ad 
     43  1.4.2.2  ad 	if ((ifrp = ifc->ifc_req) == NULL)
     44  1.4.2.2  ad 		space = 0;
     45  1.4.2.2  ad 	else
     46  1.4.2.2  ad 		space = ifc->ifc_len;
     47  1.4.2.2  ad 	IFNET_FOREACH(ifp) {
     48  1.4.2.2  ad 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
     49  1.4.2.2  ad 		    sizeof(ifr.ifr_name));
     50  1.4.2.2  ad 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
     51  1.4.2.2  ad 			return ENAMETOOLONG;
     52  1.4.2.2  ad 		if (TAILQ_EMPTY(&ifp->if_addrlist)) {
     53  1.4.2.2  ad 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
     54  1.4.2.2  ad 			if (space >= sz) {
     55  1.4.2.2  ad 				error = copyout(&ifr, ifrp, sz);
     56  1.4.2.2  ad 				if (error != 0)
     57  1.4.2.2  ad 					return (error);
     58  1.4.2.2  ad 				ifrp++;
     59  1.4.2.2  ad 			}
     60  1.4.2.2  ad 			space -= sizeof(ifr);
     61  1.4.2.2  ad 			continue;
     62  1.4.2.2  ad 		}
     63  1.4.2.2  ad 
     64  1.4.2.2  ad 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
     65  1.4.2.2  ad 			struct sockaddr *sa = ifa->ifa_addr;
     66  1.4.2.2  ad #ifdef COMPAT_OSOCK
     67  1.4.2.2  ad 			if (cmd == OOSIOCGIFCONF) {
     68  1.4.2.2  ad 				struct osockaddr *osa =
     69  1.4.2.2  ad 				    (struct osockaddr *)&ifr.ifr_addr;
     70  1.4.2.2  ad 				/*
     71  1.4.2.2  ad 				 * If it does not fit, we don't bother with it
     72  1.4.2.2  ad 				 */
     73  1.4.2.2  ad 				if (sa->sa_len > sizeof(*osa))
     74  1.4.2.2  ad 					continue;
     75  1.4.2.2  ad 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
     76  1.4.2.2  ad 				osa->sa_family = sa->sa_family;
     77  1.4.2.2  ad 				if (space >= sz) {
     78  1.4.2.2  ad 					error = copyout(&ifr, ifrp, sz);
     79  1.4.2.2  ad 					ifrp++;
     80  1.4.2.2  ad 				}
     81  1.4.2.2  ad 			} else
     82  1.4.2.2  ad #endif
     83  1.4.2.2  ad 			if (sa->sa_len <= sizeof(*sa)) {
     84  1.4.2.2  ad 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
     85  1.4.2.2  ad 				if (space >= sz) {
     86  1.4.2.2  ad 					error = copyout(&ifr, ifrp, sz);
     87  1.4.2.2  ad 					ifrp++;
     88  1.4.2.2  ad 				}
     89  1.4.2.2  ad 			} else {
     90  1.4.2.2  ad 				space -= sa->sa_len - sizeof(*sa);
     91  1.4.2.2  ad 				if (space >= sz) {
     92  1.4.2.2  ad 					error = copyout(&ifr, ifrp,
     93  1.4.2.2  ad 					    sizeof(ifr.ifr_name));
     94  1.4.2.2  ad 					if (error == 0) {
     95  1.4.2.2  ad 						error = copyout(sa,
     96  1.4.2.2  ad 						    &ifrp->ifr_addr,
     97  1.4.2.2  ad 						    sa->sa_len);
     98  1.4.2.2  ad 					}
     99  1.4.2.2  ad 					ifrp = (struct oifreq *)
    100  1.4.2.2  ad 						(sa->sa_len +
    101  1.4.2.2  ad 						 (char *)&ifrp->ifr_addr);
    102  1.4.2.2  ad 				}
    103  1.4.2.2  ad 			}
    104  1.4.2.2  ad 			if (error != 0)
    105  1.4.2.2  ad 				return (error);
    106  1.4.2.2  ad 			space -= sz;
    107  1.4.2.2  ad 		}
    108  1.4.2.2  ad 	}
    109  1.4.2.2  ad 	if (ifrp != NULL)
    110  1.4.2.2  ad 		ifc->ifc_len -= space;
    111  1.4.2.2  ad 	else
    112  1.4.2.2  ad 		ifc->ifc_len = -space;
    113  1.4.2.2  ad 	return (0);
    114  1.4.2.2  ad }
    115  1.4.2.2  ad #endif
    116