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