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