xref: /netbsd-src/sys/compat/common/if_43.c (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: if_43.c,v 1.3 2010/11/06 17:20:07 christos 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.3 2010/11/06 17:20:07 christos 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/malloc.h>
53 #include <sys/syslog.h>
54 #include <sys/unistd.h>
55 #include <sys/resourcevar.h>
56 #include <sys/mbuf.h>		/* for MLEN */
57 #include <sys/protosw.h>
58 
59 #include <sys/mount.h>
60 #include <sys/syscallargs.h>
61 
62 #include <net/if.h>
63 #include <net/bpf.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
68 #include <net/if_gre.h>
69 #include <net/if_atm.h>
70 #include <net/if_tap.h>
71 #include <net80211/ieee80211_ioctl.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet6/nd6.h>
74 #include <compat/sys/socket.h>
75 #include <compat/sys/sockio.h>
76 
77 #include <compat/common/compat_util.h>
78 
79 #include <uvm/uvm_extern.h>
80 
81 u_long
82 compat_cvtcmd(u_long cmd)
83 {
84 	u_long ncmd;
85 
86 	if (IOCPARM_LEN(cmd) != sizeof(struct oifreq))
87 		return cmd;
88 
89 	switch (cmd) {
90 	case OSIOCSIFADDR:
91 		return SIOCSIFADDR;
92 	case OOSIOCGIFADDR:
93 		return SIOCGIFADDR;
94 	case OSIOCSIFDSTADDR:
95 		return SIOCSIFDSTADDR;
96 	case OOSIOCGIFDSTADDR:
97 		return SIOCGIFDSTADDR;
98 	case OSIOCSIFFLAGS:
99 		return SIOCSIFFLAGS;
100 	case OSIOCGIFFLAGS:
101 		return SIOCGIFFLAGS;
102 	case OOSIOCGIFBRDADDR:
103 		return SIOCGIFBRDADDR;
104 	case OSIOCSIFBRDADDR:
105 		return SIOCSIFBRDADDR;
106 	case OOSIOCGIFCONF:
107 		return SIOCGIFCONF;
108 	case OOSIOCGIFNETMASK:
109 		return SIOCGIFNETMASK;
110 	case OSIOCSIFNETMASK:
111 		return SIOCSIFNETMASK;
112 	case OSIOCGIFCONF:
113 		return SIOCGIFCONF;
114 	case OSIOCADDMULTI:
115 		return SIOCADDMULTI;
116 	case OSIOCDELMULTI:
117 		return SIOCDELMULTI;
118 	case OSIOCSIFMEDIA:
119 		return SIOCSIFMEDIA;
120 	case OSIOCGIFMTU:
121 		return SIOCGIFMTU;
122 	case OSIOCGIFDATA:
123 		return SIOCGIFDATA;
124 	case OSIOCZIFDATA:
125 		return SIOCZIFDATA;
126 	case OBIOCGETIF:
127 		return BIOCGETIF;
128 	case OBIOCSETIF:
129 		return BIOCSETIF;
130 	case OTAPGIFNAME:
131 		return TAPGIFNAME;
132 	default:
133 		/*
134 		 * XXX: the following code should be removed and the
135 		 * needing treatment ioctls should move to the switch
136 		 * above.
137 		 */
138 		ncmd = ((cmd) & ~(IOCPARM_MASK << IOCPARM_SHIFT)) |
139 		    (sizeof(struct ifreq) << IOCPARM_SHIFT);
140 		switch (ncmd) {
141 		case BIOCGETIF:
142 		case BIOCSETIF:
143 		case GREDSOCK:
144 		case GREGADDRD:
145 		case GREGADDRS:
146 		case GREGPROTO:
147 		case GRESADDRD:
148 		case GRESADDRS:
149 		case GRESPROTO:
150 		case GRESSOCK:
151 #ifdef COMPAT_20
152 		case OSIOCG80211STATS:
153 		case OSIOCG80211ZSTATS:
154 #endif /* COMPAT_20 */
155 		case SIOCADDMULTI:
156 		case SIOCDELMULTI:
157 		case SIOCDIFADDR:
158 		case SIOCDIFADDR_IN6:
159 		case SIOCDIFPHYADDR:
160 		case SIOCGDEFIFACE_IN6:
161 		case SIOCG80211NWID:
162 		case SIOCG80211STATS:
163 		case SIOCG80211ZSTATS:
164 		case SIOCGIFADDR:
165 		case SIOCGIFADDR_IN6:
166 		case SIOCGIFAFLAG_IN6:
167 		case SIOCGIFALIFETIME_IN6:
168 		case SIOCGIFBRDADDR:
169 		case SIOCGIFDLT:
170 		case SIOCGIFDSTADDR:
171 		case SIOCGIFDSTADDR_IN6:
172 		case SIOCGIFFLAGS:
173 		case SIOCGIFGENERIC:
174 		case SIOCGIFMETRIC:
175 		case SIOCGIFMTU:
176 		case SIOCGIFNETMASK:
177 		case SIOCGIFNETMASK_IN6:
178 		case SIOCGIFPDSTADDR:
179 		case SIOCGIFPDSTADDR_IN6:
180 		case SIOCGIFPSRCADDR:
181 		case SIOCGIFPSRCADDR_IN6:
182 		case SIOCGIFSTAT_ICMP6:
183 		case SIOCGIFSTAT_IN6:
184 		case SIOCGPVCSIF:
185 		case SIOCGVH:
186 		case SIOCIFCREATE:
187 		case SIOCIFDESTROY:
188 		case SIOCS80211NWID:
189 		case SIOCSDEFIFACE_IN6:
190 		case SIOCSIFADDR:
191 		case SIOCSIFADDR_IN6:
192 		case SIOCSIFBRDADDR:
193 		case SIOCSIFDSTADDR:
194 		case SIOCSIFDSTADDR_IN6:
195 		case SIOCSIFFLAGS:
196 		case SIOCSIFGENERIC:
197 		case SIOCSIFMEDIA:
198 		case SIOCSIFMETRIC:
199 		case SIOCSIFMTU:
200 		case SIOCSIFNETMASK:
201 		case SIOCSIFNETMASK_IN6:
202 		case SIOCSNDFLUSH_IN6:
203 		case SIOCSPFXFLUSH_IN6:
204 		case SIOCSPVCSIF:
205 		case SIOCSRTRFLUSH_IN6:
206 		case SIOCSVH:
207 		case TAPGIFNAME:
208 			return ncmd;
209 		default:
210 			return cmd;
211 		}
212 	}
213 }
214 
215 int
216 compat_ifioctl(struct socket *so, u_long ocmd, u_long cmd, void *data,
217     struct lwp *l)
218 {
219 	int error;
220 	struct ifreq *ifr = data;
221 	struct ifnet *ifp = ifunit(ifr->ifr_name);
222 	struct sockaddr *sa;
223 
224 	if (ifp == NULL)
225 		return ENXIO;
226 
227 	switch (ocmd) {
228 	case OSIOCSIFADDR:
229 	case OSIOCSIFDSTADDR:
230 	case OSIOCSIFBRDADDR:
231 	case OSIOCSIFNETMASK:
232 		sa = &ifr->ifr_addr;
233 #if BYTE_ORDER != BIG_ENDIAN
234 		if (sa->sa_family == 0 && sa->sa_len < 16) {
235 			sa->sa_family = sa->sa_len;
236 			sa->sa_len = 16;
237 		}
238 #else
239 		if (sa->sa_len == 0)
240 			sa->sa_len = 16;
241 #endif
242 		break;
243 
244 	case OOSIOCGIFADDR:
245 		cmd = SIOCGIFADDR;
246 		break;
247 
248 	case OOSIOCGIFDSTADDR:
249 		cmd = SIOCGIFDSTADDR;
250 		break;
251 
252 	case OOSIOCGIFBRDADDR:
253 		cmd = SIOCGIFBRDADDR;
254 		break;
255 
256 	case OOSIOCGIFNETMASK:
257 		cmd = SIOCGIFNETMASK;
258 	}
259 
260 	error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
261 	    (struct mbuf *)cmd, (struct mbuf *)ifr, (struct mbuf *)ifp, l);
262 
263 	switch (ocmd) {
264 	case OOSIOCGIFADDR:
265 	case OOSIOCGIFDSTADDR:
266 	case OOSIOCGIFBRDADDR:
267 	case OOSIOCGIFNETMASK:
268 		*(u_int16_t *)&ifr->ifr_addr =
269 		    ((struct sockaddr *)&ifr->ifr_addr)->sa_family;
270 	}
271 	return error;
272 }
273