Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_40.c revision 1.7.16.1
      1  1.7.16.1  jdolecek /*	$NetBSD: uipc_syscalls_40.c,v 1.7.16.1 2017/12/03 11:36:53 jdolecek Exp $	*/
      2       1.1  christos 
      3       1.1  christos /* written by Pavel Cahyna, 2006. Public domain. */
      4       1.1  christos 
      5       1.1  christos #include <sys/cdefs.h>
      6  1.7.16.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.7.16.1 2017/12/03 11:36:53 jdolecek Exp $");
      7       1.1  christos 
      8       1.1  christos /*
      9       1.1  christos  * System call interface to the socket abstraction.
     10       1.1  christos  */
     11       1.1  christos 
     12       1.1  christos #include <sys/param.h>
     13       1.1  christos #include <sys/kernel.h>
     14       1.1  christos #include <sys/msg.h>
     15       1.1  christos #include <sys/sysctl.h>
     16       1.1  christos #include <sys/syscallargs.h>
     17       1.1  christos #include <sys/errno.h>
     18       1.1  christos 
     19       1.1  christos #include <net/if.h>
     20       1.1  christos 
     21       1.1  christos #include <compat/sys/socket.h>
     22       1.3  christos #include <compat/sys/sockio.h>
     23       1.1  christos 
     24       1.2  christos #ifdef COMPAT_OIFREQ
     25       1.1  christos /*
     26       1.1  christos  * Return interface configuration
     27       1.1  christos  * of system.  List may be used
     28       1.1  christos  * in later ioctl's (above) to get
     29       1.1  christos  * other information.
     30       1.1  christos  */
     31       1.1  christos /*ARGSUSED*/
     32       1.1  christos int
     33       1.1  christos compat_ifconf(u_long cmd, void *data)
     34       1.1  christos {
     35       1.1  christos 	struct oifconf *ifc = data;
     36       1.1  christos 	struct ifnet *ifp;
     37  1.7.16.1  jdolecek 	struct oifreq ifr, *ifrp = NULL;
     38  1.7.16.1  jdolecek 	int space = 0, error = 0;
     39       1.1  christos 	const int sz = (int)sizeof(ifr);
     40  1.7.16.1  jdolecek 	const bool docopy = ifc->ifc_req != NULL;
     41  1.7.16.1  jdolecek 	int s;
     42  1.7.16.1  jdolecek 	int bound;
     43  1.7.16.1  jdolecek 	struct psref psref;
     44       1.1  christos 
     45  1.7.16.1  jdolecek 	if (docopy) {
     46       1.4     enami 		space = ifc->ifc_len;
     47  1.7.16.1  jdolecek 		ifrp = ifc->ifc_req;
     48  1.7.16.1  jdolecek 	}
     49  1.7.16.1  jdolecek 
     50  1.7.16.1  jdolecek 	bound = curlwp_bind();
     51  1.7.16.1  jdolecek 	s = pserialize_read_enter();
     52  1.7.16.1  jdolecek 	IFNET_READER_FOREACH(ifp) {
     53  1.7.16.1  jdolecek 		struct ifaddr *ifa;
     54  1.7.16.1  jdolecek 
     55  1.7.16.1  jdolecek 		if_acquire(ifp, &psref);
     56  1.7.16.1  jdolecek 		pserialize_read_exit(s);
     57  1.7.16.1  jdolecek 
     58       1.1  christos 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
     59       1.1  christos 		    sizeof(ifr.ifr_name));
     60  1.7.16.1  jdolecek 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
     61  1.7.16.1  jdolecek 			error = ENAMETOOLONG;
     62  1.7.16.1  jdolecek 			goto release_exit;
     63  1.7.16.1  jdolecek 		}
     64  1.7.16.1  jdolecek 		if (IFADDR_READER_EMPTY(ifp)) {
     65       1.1  christos 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
     66       1.4     enami 			if (space >= sz) {
     67       1.1  christos 				error = copyout(&ifr, ifrp, sz);
     68       1.1  christos 				if (error != 0)
     69  1.7.16.1  jdolecek 					goto release_exit;
     70       1.1  christos 				ifrp++;
     71       1.1  christos 			}
     72       1.4     enami 			space -= sizeof(ifr);
     73  1.7.16.1  jdolecek 			goto next;
     74       1.1  christos 		}
     75       1.1  christos 
     76  1.7.16.1  jdolecek 		s = pserialize_read_enter();
     77  1.7.16.1  jdolecek 		IFADDR_READER_FOREACH(ifa, ifp) {
     78       1.1  christos 			struct sockaddr *sa = ifa->ifa_addr;
     79  1.7.16.1  jdolecek 			struct psref psref_ifa;
     80  1.7.16.1  jdolecek 
     81  1.7.16.1  jdolecek 			ifa_acquire(ifa, &psref_ifa);
     82  1.7.16.1  jdolecek 			pserialize_read_exit(s);
     83       1.1  christos #ifdef COMPAT_OSOCK
     84       1.1  christos 			if (cmd == OOSIOCGIFCONF) {
     85       1.1  christos 				struct osockaddr *osa =
     86       1.4     enami 				    (struct osockaddr *)&ifr.ifr_addr;
     87       1.1  christos 				/*
     88       1.1  christos 				 * If it does not fit, we don't bother with it
     89       1.1  christos 				 */
     90       1.1  christos 				if (sa->sa_len > sizeof(*osa))
     91  1.7.16.1  jdolecek 					goto next_ifa;
     92       1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
     93       1.1  christos 				osa->sa_family = sa->sa_family;
     94       1.4     enami 				if (space >= sz) {
     95       1.1  christos 					error = copyout(&ifr, ifrp, sz);
     96       1.1  christos 					ifrp++;
     97       1.1  christos 				}
     98       1.1  christos 			} else
     99       1.1  christos #endif
    100       1.1  christos 			if (sa->sa_len <= sizeof(*sa)) {
    101       1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
    102       1.4     enami 				if (space >= sz) {
    103       1.1  christos 					error = copyout(&ifr, ifrp, sz);
    104       1.1  christos 					ifrp++;
    105       1.1  christos 				}
    106       1.1  christos 			} else {
    107       1.4     enami 				space -= sa->sa_len - sizeof(*sa);
    108       1.4     enami 				if (space >= sz) {
    109       1.1  christos 					error = copyout(&ifr, ifrp,
    110       1.1  christos 					    sizeof(ifr.ifr_name));
    111       1.1  christos 					if (error == 0) {
    112       1.1  christos 						error = copyout(sa,
    113       1.1  christos 						    &ifrp->ifr_addr,
    114       1.1  christos 						    sa->sa_len);
    115       1.1  christos 					}
    116       1.1  christos 					ifrp = (struct oifreq *)
    117       1.1  christos 						(sa->sa_len +
    118       1.1  christos 						 (char *)&ifrp->ifr_addr);
    119       1.1  christos 				}
    120       1.1  christos 			}
    121  1.7.16.1  jdolecek 			if (error != 0) {
    122  1.7.16.1  jdolecek 				ifa_release(ifa, &psref_ifa);
    123  1.7.16.1  jdolecek 				goto release_exit;
    124  1.7.16.1  jdolecek 			}
    125       1.4     enami 			space -= sz;
    126  1.7.16.1  jdolecek 
    127  1.7.16.1  jdolecek #ifdef COMPAT_OSOCK
    128  1.7.16.1  jdolecek 		next_ifa:
    129  1.7.16.1  jdolecek #endif
    130  1.7.16.1  jdolecek 			s = pserialize_read_enter();
    131  1.7.16.1  jdolecek 			ifa_release(ifa, &psref_ifa);
    132       1.1  christos 		}
    133  1.7.16.1  jdolecek 		pserialize_read_exit(s);
    134  1.7.16.1  jdolecek 
    135  1.7.16.1  jdolecek 	next:
    136  1.7.16.1  jdolecek 		s = pserialize_read_enter();
    137  1.7.16.1  jdolecek 		if_release(ifp, &psref);
    138       1.1  christos 	}
    139  1.7.16.1  jdolecek 	pserialize_read_exit(s);
    140  1.7.16.1  jdolecek 	curlwp_bindx(bound);
    141  1.7.16.1  jdolecek 
    142  1.7.16.1  jdolecek 	if (docopy)
    143       1.1  christos 		ifc->ifc_len -= space;
    144       1.1  christos 	else
    145       1.4     enami 		ifc->ifc_len = -space;
    146       1.4     enami 	return (0);
    147  1.7.16.1  jdolecek 
    148  1.7.16.1  jdolecek release_exit:
    149  1.7.16.1  jdolecek 	if_release(ifp, &psref);
    150  1.7.16.1  jdolecek 	curlwp_bindx(bound);
    151  1.7.16.1  jdolecek 	return error;
    152       1.1  christos }
    153       1.2  christos #endif
    154