Home | History | Annotate | Line # | Download | only in common
if_43.c revision 1.14.2.5
      1 /*	$NetBSD: if_43.c,v 1.14.2.5 2018/09/21 02:56:22 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.5 2018/09/21 02:56:22 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_atm.h>
     69 #include <net/if_tap.h>
     70 #include <net80211/ieee80211_ioctl.h>
     71 #include <netinet6/in6_var.h>
     72 #include <netinet6/nd6.h>
     73 #include <compat/sys/socket.h>
     74 #include <compat/sys/sockio.h>
     75 
     76 #include <compat/common/compat_util.h>
     77 #include <compat/common/if_43.h>
     78 #include <uvm/uvm_extern.h>
     79 
     80 #if defined(COMPAT_43)
     81 
     82 /*
     83  * Hook for calling the if43_20 compatability routine.
     84  *
     85  * XXX The if43_20 routine doesn't really have any effect, since its
     86  * XXX return value is ignored (see compat/common/if_43.c)!
     87  */
     88 MODULE_CALL_HOOK_DECL(if43_20_hook, f, (u_long ncmd), (ncmd), enosys());
     89 MODULE_CALL_HOOK(if43_20_hook, f, (u_long ncmd), (ncmd), enosys());
     90 
     91 /*
     92  * Use a wrapper so that the compat_cvtcmd() can return a u_long
     93  */
     94 static int
     95 do_compat_cvtcmd(u_long *ncmd, u_long ocmd)
     96 {
     97 
     98 	*ncmd = compat_cvtcmd(ocmd);
     99 	return 0;
    100 }
    101 
    102 u_long
    103 compat_cvtcmd(u_long cmd)
    104 {
    105 	u_long ncmd;
    106 
    107 	if (IOCPARM_LEN(cmd) != sizeof(struct oifreq))
    108 		return cmd;
    109 
    110 	switch (cmd) {
    111 	case OSIOCSIFADDR:
    112 		return SIOCSIFADDR;
    113 	case OOSIOCGIFADDR:
    114 		return SIOCGIFADDR;
    115 	case OSIOCSIFDSTADDR:
    116 		return SIOCSIFDSTADDR;
    117 	case OOSIOCGIFDSTADDR:
    118 		return SIOCGIFDSTADDR;
    119 	case OSIOCSIFFLAGS:
    120 		return SIOCSIFFLAGS;
    121 	case OSIOCGIFFLAGS:
    122 		return SIOCGIFFLAGS;
    123 	case OOSIOCGIFBRDADDR:
    124 		return SIOCGIFBRDADDR;
    125 	case OSIOCSIFBRDADDR:
    126 		return SIOCSIFBRDADDR;
    127 	case OOSIOCGIFCONF:
    128 		return SIOCGIFCONF;
    129 	case OOSIOCGIFNETMASK:
    130 		return SIOCGIFNETMASK;
    131 	case OSIOCSIFNETMASK:
    132 		return SIOCSIFNETMASK;
    133 	case OSIOCGIFCONF:
    134 		return SIOCGIFCONF;
    135 	case OSIOCADDMULTI:
    136 		return SIOCADDMULTI;
    137 	case OSIOCDELMULTI:
    138 		return SIOCDELMULTI;
    139 	case OSIOCSIFMEDIA:
    140 		return SIOCSIFMEDIA;
    141 	case OSIOCGIFMTU:
    142 		return SIOCGIFMTU;
    143 	case OSIOCGIFDATA:
    144 		return SIOCGIFDATA;
    145 	case OSIOCZIFDATA:
    146 		return SIOCZIFDATA;
    147 	case OBIOCGETIF:
    148 		return BIOCGETIF;
    149 	case OBIOCSETIF:
    150 		return BIOCSETIF;
    151 	case OTAPGIFNAME:
    152 		return TAPGIFNAME;
    153 	default:
    154 		/*
    155 		 * XXX: the following code should be removed and the
    156 		 * needing treatment ioctls should move to the switch
    157 		 * above.
    158 		 */
    159 		ncmd = ((cmd) & ~(IOCPARM_MASK << IOCPARM_SHIFT)) |
    160 		    (sizeof(struct ifreq) << IOCPARM_SHIFT);
    161 		switch (ncmd) {
    162 		case BIOCGETIF:
    163 		case BIOCSETIF:
    164 		case GREDSOCK:
    165 		case GREGADDRD:
    166 		case GREGADDRS:
    167 		case GREGPROTO:
    168 		case GRESADDRD:
    169 		case GRESADDRS:
    170 		case GRESPROTO:
    171 		case GRESSOCK:
    172 		case SIOCADDMULTI:
    173 		case SIOCDELMULTI:
    174 		case SIOCDIFADDR:
    175 		case SIOCDIFADDR_IN6:
    176 		case SIOCDIFPHYADDR:
    177 		case SIOCGDEFIFACE_IN6:
    178 		case SIOCG80211NWID:
    179 		case SIOCG80211STATS:
    180 		case SIOCG80211ZSTATS:
    181 		case SIOCGIFADDR:
    182 		case SIOCGIFADDR_IN6:
    183 		case SIOCGIFAFLAG_IN6:
    184 		case SIOCGIFALIFETIME_IN6:
    185 		case SIOCGIFBRDADDR:
    186 		case SIOCGIFDLT:
    187 		case SIOCGIFDSTADDR:
    188 		case SIOCGIFDSTADDR_IN6:
    189 		case SIOCGIFFLAGS:
    190 		case SIOCGIFGENERIC:
    191 		case SIOCGIFMETRIC:
    192 		case SIOCGIFMTU:
    193 		case SIOCGIFNETMASK:
    194 		case SIOCGIFNETMASK_IN6:
    195 		case SIOCGIFPDSTADDR:
    196 		case SIOCGIFPDSTADDR_IN6:
    197 		case SIOCGIFPSRCADDR:
    198 		case SIOCGIFPSRCADDR_IN6:
    199 		case SIOCGIFSTAT_ICMP6:
    200 		case SIOCGIFSTAT_IN6:
    201 		case SIOCGPVCSIF:
    202 		case SIOCGVH:
    203 		case SIOCIFCREATE:
    204 		case SIOCIFDESTROY:
    205 		case SIOCS80211NWID:
    206 		case SIOCSDEFIFACE_IN6:
    207 		case SIOCSIFADDR:
    208 		case SIOCSIFADDR_IN6:
    209 		case SIOCSIFBRDADDR:
    210 		case SIOCSIFDSTADDR:
    211 		case SIOCSIFDSTADDR_IN6:
    212 		case SIOCSIFFLAGS:
    213 		case SIOCSIFGENERIC:
    214 		case SIOCSIFMEDIA:
    215 		case SIOCSIFMETRIC:
    216 		case SIOCSIFMTU:
    217 		case SIOCSIFNETMASK:
    218 		case SIOCSIFNETMASK_IN6:
    219 		case SIOCSNDFLUSH_IN6:
    220 		case SIOCSPFXFLUSH_IN6:
    221 		case SIOCSPVCSIF:
    222 		case SIOCSRTRFLUSH_IN6:
    223 		case SIOCSVH:
    224 		case TAPGIFNAME:
    225 			return ncmd;
    226 		default:
    227 			(void)if43_20_hook_f_call(ncmd);
    228 			return ncmd;
    229 		}
    230 	}
    231 }
    232 
    233 int
    234 compat_ifioctl(struct socket *so, u_long ocmd, u_long cmd, void *data,
    235     struct lwp *l)
    236 {
    237 	int error;
    238 	struct ifreq *ifr = (struct ifreq *)data;
    239 	struct ifreq ifrb;
    240 	struct oifreq *oifr = NULL;
    241 	struct ifnet *ifp;
    242 	struct sockaddr *sa;
    243 	struct psref psref;
    244 	int bound = curlwp_bind();
    245 
    246 	ifp = if_get(ifr->ifr_name, &psref);
    247 	if (ifp == NULL) {
    248 		curlwp_bindx(bound);
    249 		return ENXIO;
    250 	}
    251 
    252 	/*
    253 	 * If we have not been converted, make sure that we are.
    254 	 * (because the upper layer handles old socket calls, but
    255 	 * not oifreq calls.
    256 	 */
    257 	if (cmd == ocmd) {
    258 		cmd = compat_cvtcmd(ocmd);
    259 	}
    260 	if (cmd != ocmd) {
    261 		oifr = data;
    262 		data = ifr = &ifrb;
    263 		ifreqo2n(oifr, ifr);
    264 	}
    265 
    266 	switch (ocmd) {
    267 	case OSIOCSIFADDR:
    268 	case OSIOCSIFDSTADDR:
    269 	case OSIOCSIFBRDADDR:
    270 	case OSIOCSIFNETMASK:
    271 		sa = &ifr->ifr_addr;
    272 #if BYTE_ORDER != BIG_ENDIAN
    273 		if (sa->sa_family == 0 && sa->sa_len < 16) {
    274 			sa->sa_family = sa->sa_len;
    275 			sa->sa_len = 16;
    276 		}
    277 #else
    278 		if (sa->sa_len == 0)
    279 			sa->sa_len = 16;
    280 #endif
    281 		break;
    282 	}
    283 
    284 	error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, cmd, ifr, ifp);
    285 	if_put(ifp, &psref);
    286 	curlwp_bindx(bound);
    287 
    288 	switch (ocmd) {
    289 	case OOSIOCGIFADDR:
    290 	case OOSIOCGIFDSTADDR:
    291 	case OOSIOCGIFBRDADDR:
    292 	case OOSIOCGIFNETMASK:
    293 		*(u_int16_t *)&ifr->ifr_addr =
    294 		    ((struct sockaddr *)&ifr->ifr_addr)->sa_family;
    295 		break;
    296 	}
    297 
    298 	if (cmd != ocmd)
    299 		ifreqn2o(oifr, ifr);
    300 
    301 	return error;
    302 }
    303 
    304 MODULE_SET_HOOK2(if_43_hook, "if_43", do_compat_cvtcmd, compat_ifioctl);
    305 MODULE_UNSET_HOOK2(if_43_hook);
    306 
    307 void
    308 if_43_init(void)
    309 {
    310 
    311 	if_43_hook_set();
    312 }
    313 
    314 void
    315 if_43_fini(void)
    316 {
    317 
    318 	if_43_hook_unset();
    319 }
    320 #endif /* defined(COMPAT_43) */
    321