Home | History | Annotate | Line # | Download | only in common
if_43.c revision 1.14.2.10
      1 /*	$NetBSD: if_43.c,v 1.14.2.10 2018/10/15 04:33:34 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: if_43.c,v 1.14.2.10 2018/10/15 04:33:34 pgoyette Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_compat_netbsd.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/stat.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/fcntl.h>
     52 #include <sys/syslog.h>
     53 #include <sys/unistd.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/mbuf.h>		/* for MLEN */
     56 #include <sys/protosw.h>
     57 #include <sys/compat_stub.h>
     58 
     59 #include <sys/syscallargs.h>
     60 
     61 #include <net/if.h>
     62 #include <net/bpf.h>
     63 #include <net/route.h>
     64 #include <netinet/in.h>
     65 #include <netinet/in_systm.h>
     66 #include <netinet/ip.h>
     67 #include <net/if_gre.h>
     68 #include <net/if_tap.h>
     69 #include <net80211/ieee80211_ioctl.h>
     70 #include <netinet6/in6_var.h>
     71 #include <netinet6/nd6.h>
     72 #include <compat/sys/socket.h>
     73 #include <compat/sys/sockio.h>
     74 
     75 #include <compat/common/compat_util.h>
     76 #include <compat/common/compat_mod.h>
     77 #include <uvm/uvm_extern.h>
     78 
     79 #if defined(COMPAT_43)
     80 
     81 /*
     82  * Hook for calling the if43_20 compatability routine.
     83  *
     84  * XXX The if43_20 routine doesn't really have any effect, since its
     85  * XXX return value is ignored (see compat/common/if_43.c)!
     86  */
     87 MODULE_CALL_HOOK_DECL(if43_20_hook, f, (u_long ncmd));
     88 MODULE_CALL_HOOK(if43_20_hook, f, (u_long ncmd), (ncmd), enosys());
     89 
     90 /*
     91  * Use a wrapper so that the compat_cvtcmd() can return a u_long
     92  */
     93 static int
     94 do_compat_cvtcmd(u_long *ncmd, u_long ocmd)
     95 {
     96 
     97 	*ncmd = compat_cvtcmd(ocmd);
     98 	return 0;
     99 }
    100 
    101 u_long
    102 compat_cvtcmd(u_long cmd)
    103 {
    104 	u_long ncmd;
    105 
    106 	if (IOCPARM_LEN(cmd) != sizeof(struct oifreq))
    107 		return cmd;
    108 
    109 	switch (cmd) {
    110 	case OSIOCSIFADDR:
    111 		return SIOCSIFADDR;
    112 	case OOSIOCGIFADDR:
    113 		return SIOCGIFADDR;
    114 	case OSIOCSIFDSTADDR:
    115 		return SIOCSIFDSTADDR;
    116 	case OOSIOCGIFDSTADDR:
    117 		return SIOCGIFDSTADDR;
    118 	case OSIOCSIFFLAGS:
    119 		return SIOCSIFFLAGS;
    120 	case OSIOCGIFFLAGS:
    121 		return SIOCGIFFLAGS;
    122 	case OOSIOCGIFBRDADDR:
    123 		return SIOCGIFBRDADDR;
    124 	case OSIOCSIFBRDADDR:
    125 		return SIOCSIFBRDADDR;
    126 	case OOSIOCGIFCONF:
    127 		return SIOCGIFCONF;
    128 	case OOSIOCGIFNETMASK:
    129 		return SIOCGIFNETMASK;
    130 	case OSIOCSIFNETMASK:
    131 		return SIOCSIFNETMASK;
    132 	case OSIOCGIFCONF:
    133 		return SIOCGIFCONF;
    134 	case OSIOCADDMULTI:
    135 		return SIOCADDMULTI;
    136 	case OSIOCDELMULTI:
    137 		return SIOCDELMULTI;
    138 	case OSIOCSIFMEDIA:
    139 		return SIOCSIFMEDIA;
    140 	case OSIOCGIFMTU:
    141 		return SIOCGIFMTU;
    142 	case OSIOCGIFDATA:
    143 		return SIOCGIFDATA;
    144 	case OSIOCZIFDATA:
    145 		return SIOCZIFDATA;
    146 	case OBIOCGETIF:
    147 		return BIOCGETIF;
    148 	case OBIOCSETIF:
    149 		return BIOCSETIF;
    150 	case OTAPGIFNAME:
    151 		return TAPGIFNAME;
    152 	default:
    153 		/*
    154 		 * XXX: the following code should be removed and the
    155 		 * needing treatment ioctls should move to the switch
    156 		 * above.
    157 		 */
    158 		ncmd = ((cmd) & ~(IOCPARM_MASK << IOCPARM_SHIFT)) |
    159 		    (sizeof(struct ifreq) << IOCPARM_SHIFT);
    160 		switch (ncmd) {
    161 		case BIOCGETIF:
    162 		case BIOCSETIF:
    163 		case GREDSOCK:
    164 		case GREGADDRD:
    165 		case GREGADDRS:
    166 		case GREGPROTO:
    167 		case GRESADDRD:
    168 		case GRESADDRS:
    169 		case GRESPROTO:
    170 		case GRESSOCK:
    171 		case SIOCADDMULTI:
    172 		case SIOCDELMULTI:
    173 		case SIOCDIFADDR:
    174 		case SIOCDIFADDR_IN6:
    175 		case SIOCDIFPHYADDR:
    176 		case SIOCGDEFIFACE_IN6:
    177 		case SIOCG80211NWID:
    178 		case SIOCG80211STATS:
    179 		case SIOCG80211ZSTATS:
    180 		case SIOCGIFADDR:
    181 		case SIOCGIFADDR_IN6:
    182 		case SIOCGIFAFLAG_IN6:
    183 		case SIOCGIFALIFETIME_IN6:
    184 		case SIOCGIFBRDADDR:
    185 		case SIOCGIFDLT:
    186 		case SIOCGIFDSTADDR:
    187 		case SIOCGIFDSTADDR_IN6:
    188 		case SIOCGIFFLAGS:
    189 		case SIOCGIFGENERIC:
    190 		case SIOCGIFMETRIC:
    191 		case SIOCGIFMTU:
    192 		case SIOCGIFNETMASK:
    193 		case SIOCGIFNETMASK_IN6:
    194 		case SIOCGIFPDSTADDR:
    195 		case SIOCGIFPDSTADDR_IN6:
    196 		case SIOCGIFPSRCADDR:
    197 		case SIOCGIFPSRCADDR_IN6:
    198 		case SIOCGIFSTAT_ICMP6:
    199 		case SIOCGIFSTAT_IN6:
    200 		case SIOCGVH:
    201 		case SIOCIFCREATE:
    202 		case SIOCIFDESTROY:
    203 		case SIOCS80211NWID:
    204 		case SIOCSDEFIFACE_IN6:
    205 		case SIOCSIFADDR:
    206 		case SIOCSIFADDR_IN6:
    207 		case SIOCSIFBRDADDR:
    208 		case SIOCSIFDSTADDR:
    209 		case SIOCSIFDSTADDR_IN6:
    210 		case SIOCSIFFLAGS:
    211 		case SIOCSIFGENERIC:
    212 		case SIOCSIFMEDIA:
    213 		case SIOCSIFMETRIC:
    214 		case SIOCSIFMTU:
    215 		case SIOCSIFNETMASK:
    216 		case SIOCSIFNETMASK_IN6:
    217 		case SIOCSNDFLUSH_IN6:
    218 		case SIOCSPFXFLUSH_IN6:
    219 		case SIOCSRTRFLUSH_IN6:
    220 		case SIOCSVH:
    221 		case TAPGIFNAME:
    222 			return ncmd;
    223 		default:
    224 			(void)if43_20_hook_f_call(ncmd);
    225 			return ncmd;
    226 		}
    227 	}
    228 }
    229 
    230 int
    231 compat_ifioctl(struct socket *so, u_long ocmd, u_long cmd, void *data,
    232     struct lwp *l)
    233 {
    234 	int error;
    235 	struct ifreq *ifr = (struct ifreq *)data;
    236 	struct ifreq ifrb;
    237 	struct oifreq *oifr = NULL;
    238 	struct ifnet *ifp;
    239 	struct sockaddr *sa;
    240 	struct psref psref;
    241 	int bound = curlwp_bind();
    242 
    243 	ifp = if_get(ifr->ifr_name, &psref);
    244 	if (ifp == NULL) {
    245 		curlwp_bindx(bound);
    246 		return ENXIO;
    247 	}
    248 
    249 	/*
    250 	 * If we have not been converted, make sure that we are.
    251 	 * (because the upper layer handles old socket calls, but
    252 	 * not oifreq calls.
    253 	 */
    254 	if (cmd == ocmd) {
    255 		cmd = compat_cvtcmd(ocmd);
    256 	}
    257 	if (cmd != ocmd) {
    258 		oifr = data;
    259 		data = ifr = &ifrb;
    260 		ifreqo2n(oifr, ifr);
    261 	}
    262 
    263 	switch (ocmd) {
    264 	case OSIOCSIFADDR:
    265 	case OSIOCSIFDSTADDR:
    266 	case OSIOCSIFBRDADDR:
    267 	case OSIOCSIFNETMASK:
    268 		sa = &ifr->ifr_addr;
    269 #if BYTE_ORDER != BIG_ENDIAN
    270 		if (sa->sa_family == 0 && sa->sa_len < 16) {
    271 			sa->sa_family = sa->sa_len;
    272 			sa->sa_len = 16;
    273 		}
    274 #else
    275 		if (sa->sa_len == 0)
    276 			sa->sa_len = 16;
    277 #endif
    278 		break;
    279 	}
    280 
    281 	error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, cmd, ifr, ifp);
    282 	if_put(ifp, &psref);
    283 	curlwp_bindx(bound);
    284 
    285 	switch (ocmd) {
    286 	case OOSIOCGIFADDR:
    287 	case OOSIOCGIFDSTADDR:
    288 	case OOSIOCGIFBRDADDR:
    289 	case OOSIOCGIFNETMASK:
    290 		*(u_int16_t *)&ifr->ifr_addr =
    291 		    ((struct sockaddr *)&ifr->ifr_addr)->sa_family;
    292 		break;
    293 	}
    294 
    295 	if (cmd != ocmd)
    296 		ifreqn2o(oifr, ifr);
    297 
    298 	return error;
    299 }
    300 
    301 MODULE_SET_HOOK2(if_43_hook, "if_43", do_compat_cvtcmd, compat_ifioctl);
    302 MODULE_UNSET_HOOK2(if_43_hook);
    303 
    304 int
    305 if_43_init(void)
    306 {
    307 
    308 	if_43_hook_set();
    309 	return 0;
    310 }
    311 
    312 int
    313 if_43_fini(void)
    314 {
    315 
    316 	if_43_hook_unset();
    317 	return 0;
    318 }
    319 #endif /* defined(COMPAT_43) */
    320