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