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