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