Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_40.c revision 1.8.2.3
      1  1.8.2.3     skrll /*	$NetBSD: uipc_syscalls_40.c,v 1.8.2.3 2016/10/05 20:55:37 skrll 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.8.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.8.2.3 2016/10/05 20:55:37 skrll 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.8     ozaki 	struct oifreq ifr, *ifrp = NULL;
     38      1.8     ozaki 	int space = 0, error = 0;
     39      1.1  christos 	const int sz = (int)sizeof(ifr);
     40      1.8     ozaki 	const bool docopy = ifc->ifc_req != NULL;
     41  1.8.2.1     skrll 	int s;
     42  1.8.2.2     skrll 	int bound;
     43  1.8.2.1     skrll 	struct psref psref;
     44      1.1  christos 
     45      1.8     ozaki 	if (docopy) {
     46      1.4     enami 		space = ifc->ifc_len;
     47      1.8     ozaki 		ifrp = ifc->ifc_req;
     48      1.8     ozaki 	}
     49      1.8     ozaki 
     50  1.8.2.2     skrll 	bound = curlwp_bind();
     51  1.8.2.1     skrll 	s = pserialize_read_enter();
     52  1.8.2.1     skrll 	IFNET_READER_FOREACH(ifp) {
     53  1.8.2.3     skrll 		struct ifaddr *ifa;
     54  1.8.2.3     skrll 
     55  1.8.2.1     skrll 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
     56  1.8.2.1     skrll 
     57      1.1  christos 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
     58      1.1  christos 		    sizeof(ifr.ifr_name));
     59  1.8.2.1     skrll 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
     60  1.8.2.1     skrll 			error = ENAMETOOLONG;
     61  1.8.2.1     skrll 			goto release_exit;
     62  1.8.2.1     skrll 		}
     63  1.8.2.2     skrll 		if (IFADDR_READER_EMPTY(ifp)) {
     64      1.1  christos 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
     65      1.4     enami 			if (space >= sz) {
     66      1.1  christos 				error = copyout(&ifr, ifrp, sz);
     67      1.1  christos 				if (error != 0)
     68  1.8.2.1     skrll 					goto release_exit;
     69      1.1  christos 				ifrp++;
     70      1.1  christos 			}
     71      1.4     enami 			space -= sizeof(ifr);
     72      1.1  christos 			continue;
     73      1.1  christos 		}
     74      1.1  christos 
     75  1.8.2.2     skrll 		IFADDR_READER_FOREACH(ifa, ifp) {
     76      1.1  christos 			struct sockaddr *sa = ifa->ifa_addr;
     77  1.8.2.3     skrll 			struct psref psref_ifa;
     78  1.8.2.3     skrll 
     79  1.8.2.3     skrll 			ifa_acquire(ifa, &psref_ifa);
     80  1.8.2.3     skrll 			pserialize_read_exit(s);
     81      1.1  christos #ifdef COMPAT_OSOCK
     82      1.1  christos 			if (cmd == OOSIOCGIFCONF) {
     83      1.1  christos 				struct osockaddr *osa =
     84      1.4     enami 				    (struct osockaddr *)&ifr.ifr_addr;
     85      1.1  christos 				/*
     86      1.1  christos 				 * If it does not fit, we don't bother with it
     87      1.1  christos 				 */
     88  1.8.2.3     skrll 				if (sa->sa_len > sizeof(*osa)) {
     89  1.8.2.3     skrll 					s = pserialize_read_enter();
     90  1.8.2.3     skrll 					ifa_release(ifa, &psref_ifa);
     91      1.1  christos 					continue;
     92  1.8.2.3     skrll 				}
     93      1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
     94      1.1  christos 				osa->sa_family = sa->sa_family;
     95      1.4     enami 				if (space >= sz) {
     96      1.1  christos 					error = copyout(&ifr, ifrp, sz);
     97      1.1  christos 					ifrp++;
     98      1.1  christos 				}
     99      1.1  christos 			} else
    100      1.1  christos #endif
    101      1.1  christos 			if (sa->sa_len <= sizeof(*sa)) {
    102      1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
    103      1.4     enami 				if (space >= sz) {
    104      1.1  christos 					error = copyout(&ifr, ifrp, sz);
    105      1.1  christos 					ifrp++;
    106      1.1  christos 				}
    107      1.1  christos 			} else {
    108      1.4     enami 				space -= sa->sa_len - sizeof(*sa);
    109      1.4     enami 				if (space >= sz) {
    110      1.1  christos 					error = copyout(&ifr, ifrp,
    111      1.1  christos 					    sizeof(ifr.ifr_name));
    112      1.1  christos 					if (error == 0) {
    113      1.1  christos 						error = copyout(sa,
    114      1.1  christos 						    &ifrp->ifr_addr,
    115      1.1  christos 						    sa->sa_len);
    116      1.1  christos 					}
    117      1.1  christos 					ifrp = (struct oifreq *)
    118      1.1  christos 						(sa->sa_len +
    119      1.1  christos 						 (char *)&ifrp->ifr_addr);
    120      1.1  christos 				}
    121      1.1  christos 			}
    122  1.8.2.3     skrll 			s = pserialize_read_enter();
    123  1.8.2.3     skrll 			ifa_release(ifa, &psref_ifa);
    124      1.1  christos 			if (error != 0)
    125  1.8.2.1     skrll 				goto release_exit;
    126      1.4     enami 			space -= sz;
    127      1.1  christos 		}
    128  1.8.2.1     skrll 
    129  1.8.2.1     skrll 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
    130      1.1  christos 	}
    131  1.8.2.1     skrll 	pserialize_read_exit(s);
    132  1.8.2.2     skrll 	curlwp_bindx(bound);
    133  1.8.2.1     skrll 
    134      1.8     ozaki 	if (docopy)
    135      1.1  christos 		ifc->ifc_len -= space;
    136      1.1  christos 	else
    137      1.4     enami 		ifc->ifc_len = -space;
    138      1.4     enami 	return (0);
    139  1.8.2.1     skrll 
    140  1.8.2.1     skrll release_exit:
    141  1.8.2.3     skrll 	pserialize_read_exit(s);
    142  1.8.2.1     skrll 	psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
    143  1.8.2.2     skrll 	curlwp_bindx(bound);
    144  1.8.2.1     skrll 	return error;
    145      1.1  christos }
    146      1.2  christos #endif
    147