Home | History | Annotate | Line # | Download | only in common
uipc_syscalls_40.c revision 1.11.2.2
      1  1.11.2.2  pgoyette /*	$NetBSD: uipc_syscalls_40.c,v 1.11.2.2 2017/03/20 06:57:23 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.11.2.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls_40.c,v 1.11.2.2 2017/03/20 06:57:23 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.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.9     ozaki 	int s;
     42      1.10     ozaki 	int bound;
     43       1.9     ozaki 	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.10     ozaki 	bound = curlwp_bind();
     51       1.9     ozaki 	s = pserialize_read_enter();
     52       1.9     ozaki 	IFNET_READER_FOREACH(ifp) {
     53  1.11.2.1  pgoyette 		struct ifaddr *ifa;
     54  1.11.2.1  pgoyette 
     55  1.11.2.2  pgoyette 		if_acquire(ifp, &psref);
     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.11     ozaki 		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.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.11     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
     76       1.1  christos 			struct sockaddr *sa = ifa->ifa_addr;
     77  1.11.2.1  pgoyette 			struct psref psref_ifa;
     78  1.11.2.1  pgoyette 
     79  1.11.2.1  pgoyette 			ifa_acquire(ifa, &psref_ifa);
     80  1.11.2.1  pgoyette 			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.11.2.1  pgoyette 				if (sa->sa_len > sizeof(*osa)) {
     89  1.11.2.1  pgoyette 					s = pserialize_read_enter();
     90  1.11.2.1  pgoyette 					ifa_release(ifa, &psref_ifa);
     91       1.1  christos 					continue;
     92  1.11.2.1  pgoyette 				}
     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.11.2.1  pgoyette 			s = pserialize_read_enter();
    123  1.11.2.1  pgoyette 			ifa_release(ifa, &psref_ifa);
    124       1.1  christos 			if (error != 0)
    125       1.9     ozaki 				goto release_exit;
    126       1.4     enami 			space -= sz;
    127       1.1  christos 		}
    128       1.9     ozaki 
    129  1.11.2.2  pgoyette 		if_release(ifp, &psref);
    130       1.1  christos 	}
    131       1.9     ozaki 	pserialize_read_exit(s);
    132      1.10     ozaki 	curlwp_bindx(bound);
    133       1.9     ozaki 
    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.9     ozaki 
    140       1.9     ozaki release_exit:
    141  1.11.2.1  pgoyette 	pserialize_read_exit(s);
    142  1.11.2.2  pgoyette 	if_release(ifp, &psref);
    143      1.10     ozaki 	curlwp_bindx(bound);
    144       1.9     ozaki 	return error;
    145       1.1  christos }
    146       1.2  christos #endif
    147