Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_40.c revision 1.16
      1  1.16  christos /*	$NetBSD: uipc_syscalls_40.c,v 1.16 2018/04/12 18:50:13 christos 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.16  christos __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.16 2018/04/12 18:50:13 christos 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.1  christos /*
     25   1.1  christos  * Return interface configuration
     26   1.1  christos  * of system.  List may be used
     27   1.1  christos  * in later ioctl's (above) to get
     28   1.1  christos  * other information.
     29   1.1  christos  */
     30   1.1  christos /*ARGSUSED*/
     31  1.16  christos static int
     32  1.16  christos compat_ifconf(struct lwp *l, u_long cmd, void *data)
     33   1.1  christos {
     34   1.1  christos 	struct oifconf *ifc = data;
     35   1.1  christos 	struct ifnet *ifp;
     36   1.8     ozaki 	struct oifreq ifr, *ifrp = NULL;
     37   1.8     ozaki 	int space = 0, error = 0;
     38   1.1  christos 	const int sz = (int)sizeof(ifr);
     39   1.8     ozaki 	const bool docopy = ifc->ifc_req != NULL;
     40   1.9     ozaki 	int s;
     41  1.10     ozaki 	int bound;
     42   1.9     ozaki 	struct psref psref;
     43   1.1  christos 
     44  1.16  christos 	switch (cmd) {
     45  1.16  christos 	case OSIOCGIFCONF:
     46  1.16  christos 	case OOSIOCGIFCONF:
     47  1.16  christos 		break;
     48  1.16  christos 	default:
     49  1.16  christos 		return ENOSYS;
     50  1.16  christos 	}
     51  1.16  christos 
     52   1.8     ozaki 	if (docopy) {
     53   1.4     enami 		space = ifc->ifc_len;
     54   1.8     ozaki 		ifrp = ifc->ifc_req;
     55   1.8     ozaki 	}
     56   1.8     ozaki 
     57  1.10     ozaki 	bound = curlwp_bind();
     58   1.9     ozaki 	s = pserialize_read_enter();
     59   1.9     ozaki 	IFNET_READER_FOREACH(ifp) {
     60  1.12     ozaki 		struct ifaddr *ifa;
     61  1.12     ozaki 
     62  1.13     ozaki 		if_acquire(ifp, &psref);
     63  1.14     ozaki 		pserialize_read_exit(s);
     64   1.9     ozaki 
     65   1.1  christos 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
     66   1.1  christos 		    sizeof(ifr.ifr_name));
     67   1.9     ozaki 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
     68   1.9     ozaki 			error = ENAMETOOLONG;
     69   1.9     ozaki 			goto release_exit;
     70   1.9     ozaki 		}
     71  1.11     ozaki 		if (IFADDR_READER_EMPTY(ifp)) {
     72   1.1  christos 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
     73   1.4     enami 			if (space >= sz) {
     74   1.1  christos 				error = copyout(&ifr, ifrp, sz);
     75   1.1  christos 				if (error != 0)
     76   1.9     ozaki 					goto release_exit;
     77   1.1  christos 				ifrp++;
     78   1.1  christos 			}
     79   1.4     enami 			space -= sizeof(ifr);
     80  1.14     ozaki 			goto next;
     81   1.1  christos 		}
     82   1.1  christos 
     83  1.14     ozaki 		s = pserialize_read_enter();
     84  1.11     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
     85   1.1  christos 			struct sockaddr *sa = ifa->ifa_addr;
     86  1.12     ozaki 			struct psref psref_ifa;
     87  1.12     ozaki 
     88  1.12     ozaki 			ifa_acquire(ifa, &psref_ifa);
     89  1.12     ozaki 			pserialize_read_exit(s);
     90   1.1  christos #ifdef COMPAT_OSOCK
     91   1.1  christos 			if (cmd == OOSIOCGIFCONF) {
     92   1.1  christos 				struct osockaddr *osa =
     93   1.4     enami 				    (struct osockaddr *)&ifr.ifr_addr;
     94   1.1  christos 				/*
     95   1.1  christos 				 * If it does not fit, we don't bother with it
     96   1.1  christos 				 */
     97  1.14     ozaki 				if (sa->sa_len > sizeof(*osa))
     98  1.14     ozaki 					goto next_ifa;
     99   1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
    100   1.1  christos 				osa->sa_family = sa->sa_family;
    101   1.4     enami 				if (space >= sz) {
    102   1.1  christos 					error = copyout(&ifr, ifrp, sz);
    103   1.1  christos 					ifrp++;
    104   1.1  christos 				}
    105   1.1  christos 			} else
    106   1.1  christos #endif
    107   1.1  christos 			if (sa->sa_len <= sizeof(*sa)) {
    108   1.1  christos 				memcpy(&ifr.ifr_addr, sa, sa->sa_len);
    109   1.4     enami 				if (space >= sz) {
    110   1.1  christos 					error = copyout(&ifr, ifrp, sz);
    111   1.1  christos 					ifrp++;
    112   1.1  christos 				}
    113   1.1  christos 			} else {
    114   1.4     enami 				space -= sa->sa_len - sizeof(*sa);
    115   1.4     enami 				if (space >= sz) {
    116   1.1  christos 					error = copyout(&ifr, ifrp,
    117   1.1  christos 					    sizeof(ifr.ifr_name));
    118   1.1  christos 					if (error == 0) {
    119   1.1  christos 						error = copyout(sa,
    120   1.1  christos 						    &ifrp->ifr_addr,
    121   1.1  christos 						    sa->sa_len);
    122   1.1  christos 					}
    123   1.1  christos 					ifrp = (struct oifreq *)
    124   1.1  christos 						(sa->sa_len +
    125   1.1  christos 						 (char *)&ifrp->ifr_addr);
    126   1.1  christos 				}
    127   1.1  christos 			}
    128  1.14     ozaki 			if (error != 0) {
    129  1.14     ozaki 				ifa_release(ifa, &psref_ifa);
    130  1.14     ozaki 				goto release_exit;
    131  1.14     ozaki 			}
    132  1.14     ozaki 			space -= sz;
    133  1.14     ozaki 
    134  1.15    martin #ifdef COMPAT_OSOCK
    135  1.14     ozaki 		next_ifa:
    136  1.15    martin #endif
    137  1.12     ozaki 			s = pserialize_read_enter();
    138  1.12     ozaki 			ifa_release(ifa, &psref_ifa);
    139   1.1  christos 		}
    140  1.14     ozaki 		pserialize_read_exit(s);
    141   1.9     ozaki 
    142  1.14     ozaki 	next:
    143  1.14     ozaki 		s = pserialize_read_enter();
    144  1.13     ozaki 		if_release(ifp, &psref);
    145   1.1  christos 	}
    146   1.9     ozaki 	pserialize_read_exit(s);
    147  1.10     ozaki 	curlwp_bindx(bound);
    148   1.9     ozaki 
    149   1.8     ozaki 	if (docopy)
    150   1.1  christos 		ifc->ifc_len -= space;
    151   1.1  christos 	else
    152   1.4     enami 		ifc->ifc_len = -space;
    153   1.4     enami 	return (0);
    154   1.9     ozaki 
    155   1.9     ozaki release_exit:
    156  1.13     ozaki 	if_release(ifp, &psref);
    157  1.10     ozaki 	curlwp_bindx(bound);
    158   1.9     ozaki 	return error;
    159   1.1  christos }
    160  1.16  christos 
    161  1.16  christos void
    162  1.16  christos uipc_syscalls_40_init(void)
    163  1.16  christos {
    164  1.16  christos 	vec_compat_ifconf = compat_ifconf;
    165  1.16  christos }
    166  1.16  christos 
    167  1.16  christos void
    168  1.16  christos uipc_syscalls_40_fini(void)
    169  1.16  christos {
    170  1.16  christos 	vec_compat_ifconf = (void *)enosys;
    171  1.16  christos }
    172