summaryrefslogtreecommitdiff
path: root/sys/compat/common/if_43.c
blob: 02d5275c23094306bda80fe800b4174b8e9467a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*	$NetBSD: if_43.c,v 1.27 2023/03/30 17:48:10 riastradh Exp $	*/

/*
 * Copyright (c) 1982, 1986, 1989, 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_43.c,v 1.27 2023/03/30 17:48:10 riastradh Exp $");

#if defined(_KERNEL_OPT)
#include "opt_compat_netbsd.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/syslog.h>
#include <sys/unistd.h>
#include <sys/resourcevar.h>
#include <sys/mbuf.h>		/* for MLEN */
#include <sys/protosw.h>
#include <sys/compat_stub.h>

#include <sys/syscallargs.h>

#include <net/if.h>
#include <net/bpf.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <net/if_gre.h>
#include <net/if_tap.h>
#include <net80211/ieee80211_ioctl.h>
#include <netinet6/in6_var.h>
#include <netinet6/nd6.h>
#include <compat/net/if.h>
#include <compat/sys/socket.h>
#include <compat/sys/sockio.h>

#include <compat/common/compat_util.h>
#include <compat/common/compat_mod.h>
#include <uvm/uvm_extern.h>

#if defined(COMPAT_43)

/* 
 * Use a wrapper so that the compat_cvtcmd() can return a u_long
 */
static int 
do_compat_cvtcmd(u_long *ncmd, u_long ocmd)
{ 

	*ncmd = compat_cvtcmd(ocmd);
	return 0;
}

u_long
compat_cvtcmd(u_long cmd)
{
	u_long ncmd;

	if (IOCPARM_LEN(cmd) != sizeof(struct oifreq))
		return cmd;

	switch (cmd) {
	case OSIOCSIFADDR:
		return SIOCSIFADDR;
	case OOSIOCGIFADDR:
		return SIOCGIFADDR;
	case OSIOCSIFDSTADDR:
		return SIOCSIFDSTADDR;
	case OOSIOCGIFDSTADDR:
		return SIOCGIFDSTADDR;
	case OSIOCSIFFLAGS:
		return SIOCSIFFLAGS;
	case OSIOCGIFFLAGS:
		return SIOCGIFFLAGS;
	case OOSIOCGIFBRDADDR:
		return SIOCGIFBRDADDR;
	case OSIOCSIFBRDADDR:
		return SIOCSIFBRDADDR;
	case OOSIOCGIFCONF:
		return SIOCGIFCONF;
	case OOSIOCGIFNETMASK:
		return SIOCGIFNETMASK;
	case OSIOCSIFNETMASK:
		return SIOCSIFNETMASK;
	case OSIOCGIFCONF:
		return SIOCGIFCONF;
	case OSIOCADDMULTI:
		return SIOCADDMULTI;
	case OSIOCDELMULTI:
		return SIOCDELMULTI;
	case SIOCSIFMEDIA_43:
		return SIOCSIFMEDIA_80;
	case OSIOCGIFMTU:
		return SIOCGIFMTU;
	case OSIOCGIFDATA:
		return SIOCGIFDATA;
	case OSIOCZIFDATA:
		return SIOCZIFDATA;
	case OBIOCGETIF:
		return BIOCGETIF;
	case OBIOCSETIF:
		return BIOCSETIF;
	case OTAPGIFNAME:
		return TAPGIFNAME;
	default:
		/*
		 * XXX: the following code should be removed and the
		 * needing treatment ioctls should move to the switch
		 * above.
		 */
		ncmd = ((cmd) & ~(IOCPARM_MASK << IOCPARM_SHIFT)) | 
		    (sizeof(struct ifreq) << IOCPARM_SHIFT);
		switch (ncmd) {
		case BIOCGETIF:
		case BIOCSETIF:
		case GREDSOCK:
		case GREGADDRD:
		case GREGADDRS:
		case GREGPROTO:
		case GRESADDRD:
		case GRESADDRS:
		case GRESPROTO:
		case GRESSOCK:
		case SIOCADDMULTI:
		case SIOCDELMULTI:
		case SIOCDIFADDR:
		case SIOCDIFADDR_IN6:
		case SIOCDIFPHYADDR:
		case SIOCG80211NWID:
		case SIOCG80211STATS:
		case SIOCG80211ZSTATS:
		case SIOCGIFADDR:
		case SIOCGIFADDR_IN6:
		case SIOCGIFAFLAG_IN6:
		case SIOCGIFALIFETIME_IN6:
		case SIOCGIFBRDADDR:
		case SIOCGIFDLT:
		case SIOCGIFDSTADDR:
		case SIOCGIFDSTADDR_IN6:
		case SIOCGIFFLAGS:
		case SIOCGIFGENERIC:
		case SIOCGIFMETRIC:
		case SIOCGIFMTU:
		case SIOCGIFNETMASK:
		case SIOCGIFNETMASK_IN6:
		case SIOCGIFPDSTADDR:
		case SIOCGIFPDSTADDR_IN6:
		case SIOCGIFPSRCADDR:
		case SIOCGIFPSRCADDR_IN6:
		case SIOCGIFSTAT_ICMP6:
		case SIOCGIFSTAT_IN6:
		case SIOCGVH:
		case SIOCIFCREATE:
		case SIOCIFDESTROY:
		case SIOCS80211NWID:
		case SIOCSIFADDR:
		case SIOCSIFADDR_IN6:
		case SIOCSIFBRDADDR:
		case SIOCSIFDSTADDR:
		case SIOCSIFDSTADDR_IN6:
		case SIOCSIFFLAGS:
		case SIOCSIFGENERIC:
		case SIOCSIFMEDIA:
		case SIOCSIFMETRIC:
		case SIOCSIFMTU:
		case SIOCSIFNETMASK:
		case SIOCSIFNETMASK_IN6:
		case SIOCSVH:
		case TAPGIFNAME:
			return ncmd;
		default:
		    {	int rv;

			MODULE_HOOK_CALL(if43_cvtcmd_20_hook, (ncmd), enosys(),
			    rv);
			if (rv == 0)
				return ncmd;
			return cmd;
		    }
		}
	}
}

int
compat_ifioctl(struct socket *so, u_long ocmd, u_long cmd, void *data,
    struct lwp *l)
{
	int error;
	struct ifreq *ifr = (struct ifreq *)data;
	struct ifreq ifrb;
	struct oifreq *oifr = NULL;
	struct ifnet *ifp;
	struct sockaddr *sa;
	struct psref psref;
	int bound = curlwp_bind();

	ifp = if_get(ifr->ifr_name, &psref);
	if (ifp == NULL) {
		curlwp_bindx(bound);
		return ENXIO;
	}

	/*
	 * If we have not been converted, make sure that we are.
	 * (because the upper layer handles old socket calls, but
	 * not oifreq calls.
	 */
	if (cmd == ocmd) {
		cmd = compat_cvtcmd(ocmd);
	}
	if (cmd != ocmd) {
		oifr = data;
		ifr = &ifrb;
		IFREQO2N_43(oifr, ifr);
	}

	switch (ocmd) {
		enum { maxlen = sizeof(oifr->ifr_ifru) };
		CTASSERT(maxlen == 16);
		socklen_t famlen;
	case OSIOCSIFADDR:
	case OSIOCSIFDSTADDR:
	case OSIOCSIFBRDADDR:
	case OSIOCSIFNETMASK:
		sa = &ifr->ifr_addr;
#if BYTE_ORDER != BIG_ENDIAN
		if (sa->sa_family == 0 && sa->sa_len < maxlen) {
			sa->sa_family = sa->sa_len;
			sa->sa_len = maxlen;
		}
#else
		if (sa->sa_len == 0)
			sa->sa_len = maxlen;
#endif
		famlen = sockaddr_getsize_by_family(sa->sa_family);
		if (famlen > sa->sa_len) {
			curlwp_bindx(bound);
			return EAFNOSUPPORT;
		}

		break;
	}

	error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, cmd, ifr, ifp);
	if_put(ifp, &psref);
	curlwp_bindx(bound);

	switch (ocmd) {
	case OOSIOCGIFADDR:
	case OOSIOCGIFDSTADDR:
	case OOSIOCGIFBRDADDR:
	case OOSIOCGIFNETMASK:
		*(u_int16_t *)&ifr->ifr_addr = 
		    ((struct sockaddr *)&ifr->ifr_addr)->sa_family;
		break;
	}

	if (cmd != ocmd)
		IFREQN2O_43(oifr, ifr);

	return error;
}

int
if_43_init(void)
{

	MODULE_HOOK_SET(if_cvtcmd_43_hook, do_compat_cvtcmd);
	MODULE_HOOK_SET(if_ifioctl_43_hook, compat_ifioctl);
	return 0;
}

int
if_43_fini(void)
{

	MODULE_HOOK_UNSET(if_cvtcmd_43_hook);
	MODULE_HOOK_UNSET(if_ifioctl_43_hook);
	return 0;
}
#endif /* defined(COMPAT_43) */