1*c45c044cSriastradh /* $NetBSD: if_media_80.c,v 1.5 2022/08/03 01:38:51 riastradh Exp $ */
2bf354a07Smsaitoh
3bf354a07Smsaitoh /*-
4bf354a07Smsaitoh * Copyright (c) 1998 The NetBSD Foundation, Inc.
5bf354a07Smsaitoh * All rights reserved.
6bf354a07Smsaitoh *
7bf354a07Smsaitoh * This code is derived from software contributed to The NetBSD Foundation
8bf354a07Smsaitoh * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9bf354a07Smsaitoh * NASA Ames Research Center.
10bf354a07Smsaitoh *
11bf354a07Smsaitoh * Redistribution and use in source and binary forms, with or without
12bf354a07Smsaitoh * modification, are permitted provided that the following conditions
13bf354a07Smsaitoh * are met:
14bf354a07Smsaitoh * 1. Redistributions of source code must retain the above copyright
15bf354a07Smsaitoh * notice, this list of conditions and the following disclaimer.
16bf354a07Smsaitoh * 2. Redistributions in binary form must reproduce the above copyright
17bf354a07Smsaitoh * notice, this list of conditions and the following disclaimer in the
18bf354a07Smsaitoh * documentation and/or other materials provided with the distribution.
19bf354a07Smsaitoh *
20bf354a07Smsaitoh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21bf354a07Smsaitoh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22bf354a07Smsaitoh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23bf354a07Smsaitoh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24bf354a07Smsaitoh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25bf354a07Smsaitoh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26bf354a07Smsaitoh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27bf354a07Smsaitoh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28bf354a07Smsaitoh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29bf354a07Smsaitoh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30bf354a07Smsaitoh * POSSIBILITY OF SUCH DAMAGE.
31bf354a07Smsaitoh */
32bf354a07Smsaitoh
33bf354a07Smsaitoh /*
34bf354a07Smsaitoh * Copyright (c) 1997
35bf354a07Smsaitoh * Jonathan Stone and Jason R. Thorpe. All rights reserved.
36bf354a07Smsaitoh *
37bf354a07Smsaitoh * This software is derived from information provided by Matt Thomas.
38bf354a07Smsaitoh *
39bf354a07Smsaitoh * Redistribution and use in source and binary forms, with or without
40bf354a07Smsaitoh * modification, are permitted provided that the following conditions
41bf354a07Smsaitoh * are met:
42bf354a07Smsaitoh * 1. Redistributions of source code must retain the above copyright
43bf354a07Smsaitoh * notice, this list of conditions and the following disclaimer.
44bf354a07Smsaitoh * 2. Redistributions in binary form must reproduce the above copyright
45bf354a07Smsaitoh * notice, this list of conditions and the following disclaimer in the
46bf354a07Smsaitoh * documentation and/or other materials provided with the distribution.
47bf354a07Smsaitoh * 3. All advertising materials mentioning features or use of this software
48bf354a07Smsaitoh * must display the following acknowledgement:
49bf354a07Smsaitoh * This product includes software developed by Jonathan Stone
50bf354a07Smsaitoh * and Jason R. Thorpe for the NetBSD Project.
51bf354a07Smsaitoh * 4. The names of the authors may not be used to endorse or promote products
52bf354a07Smsaitoh * derived from this software without specific prior written permission.
53bf354a07Smsaitoh *
54bf354a07Smsaitoh * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
55bf354a07Smsaitoh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56bf354a07Smsaitoh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57bf354a07Smsaitoh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58bf354a07Smsaitoh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
59bf354a07Smsaitoh * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60bf354a07Smsaitoh * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
61bf354a07Smsaitoh * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62bf354a07Smsaitoh * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63bf354a07Smsaitoh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64bf354a07Smsaitoh * SUCH DAMAGE.
65bf354a07Smsaitoh */
66bf354a07Smsaitoh
67bf354a07Smsaitoh #include <sys/cdefs.h>
68*c45c044cSriastradh __KERNEL_RCSID(0, "$NetBSD: if_media_80.c,v 1.5 2022/08/03 01:38:51 riastradh Exp $");
69bf354a07Smsaitoh
70bf354a07Smsaitoh #include <sys/param.h>
71bf354a07Smsaitoh #include <sys/kernel.h>
72bf354a07Smsaitoh #include <sys/syscallargs.h>
73bf354a07Smsaitoh #include <sys/errno.h>
74bf354a07Smsaitoh #include <sys/malloc.h>
75bf354a07Smsaitoh #include <sys/proc.h>
76bf354a07Smsaitoh #include <sys/compat_stub.h>
77bf354a07Smsaitoh
78bf354a07Smsaitoh #include <net/if.h>
79bf354a07Smsaitoh #include <net/if_media.h>
80bf354a07Smsaitoh
81bf354a07Smsaitoh #include <compat/sys/sockio.h>
82bf354a07Smsaitoh #include <compat/common/compat_mod.h>
83bf354a07Smsaitoh
84bf354a07Smsaitoh static void
ifmword_n2o(int * oldwd,int * newwd)85bf354a07Smsaitoh ifmword_n2o(int *oldwd, int *newwd)
86bf354a07Smsaitoh {
87bf354a07Smsaitoh
88bf354a07Smsaitoh if (IFM_SUBTYPE(*newwd) > IFM_OTHER)
89bf354a07Smsaitoh *oldwd = (*newwd & ~(_IFM_ETH_XTMASK | IFM_TMASK)) | IFM_OTHER;
90bf354a07Smsaitoh else
91bf354a07Smsaitoh *oldwd = *newwd;
92bf354a07Smsaitoh }
93bf354a07Smsaitoh
94bf354a07Smsaitoh /*ARGSUSED*/
95bf354a07Smsaitoh static int
compat_ifmediareq_pre(struct ifreq * ifr,u_long * cmd,bool * do_post)96bf354a07Smsaitoh compat_ifmediareq_pre(struct ifreq *ifr, u_long *cmd, bool *do_post)
97bf354a07Smsaitoh {
98bf354a07Smsaitoh struct ifmediareq *ifmr = (struct ifmediareq *)ifr;
99bf354a07Smsaitoh
100bf354a07Smsaitoh switch (*cmd) {
101bf354a07Smsaitoh case SIOCSIFMEDIA_80:
102bf354a07Smsaitoh *cmd = SIOCSIFMEDIA; /* Convert to new one */
103bf354a07Smsaitoh if ((IFM_TYPE(ifr->ifr_media) == IFM_ETHER) &&
104bf354a07Smsaitoh IFM_SUBTYPE(ifr->ifr_media) > IFM_OTHER) {
105bf354a07Smsaitoh /* Clear unused bits to not to change to wrong media */
106bf354a07Smsaitoh ifr->ifr_media &= ~_IFM_ETH_XTMASK;
107bf354a07Smsaitoh }
108bf354a07Smsaitoh return 0;
109bf354a07Smsaitoh case SIOCGIFMEDIA_80:
110bf354a07Smsaitoh *cmd = SIOCGIFMEDIA; /* Convert to new one */
111bf354a07Smsaitoh if (ifmr->ifm_count != 0) {
112bf354a07Smsaitoh /*
113bf354a07Smsaitoh * Tell the upper layer to try to convert each ifmedia
114bf354a07Smsaitoh * entry in the post process.
115bf354a07Smsaitoh */
116bf354a07Smsaitoh *do_post = true;
117bf354a07Smsaitoh }
118bf354a07Smsaitoh return 0;
119bf354a07Smsaitoh default:
120bf354a07Smsaitoh return 0;
121bf354a07Smsaitoh }
122bf354a07Smsaitoh }
123bf354a07Smsaitoh
124bf354a07Smsaitoh /*ARGSUSED*/
125bf354a07Smsaitoh static int
compat_ifmediareq_post(struct ifreq * ifr,u_long cmd)126bf354a07Smsaitoh compat_ifmediareq_post(struct ifreq *ifr, u_long cmd)
127bf354a07Smsaitoh {
128bf354a07Smsaitoh struct ifmediareq *ifmr = (struct ifmediareq *)ifr;
129bf354a07Smsaitoh size_t minwords;
130535ba13aSchristos size_t count;
131535ba13aSchristos int error, *kptr;
132bf354a07Smsaitoh
133bf354a07Smsaitoh switch (cmd) {
134bf354a07Smsaitoh case SIOCSIFMEDIA:
135bf354a07Smsaitoh return 0;
136bf354a07Smsaitoh case SIOCGIFMEDIA:
137bf354a07Smsaitoh if (ifmr->ifm_count < 0)
138bf354a07Smsaitoh return EINVAL;
139bf354a07Smsaitoh
140bf354a07Smsaitoh /*
141bf354a07Smsaitoh * ifmr->ifm_count was already ajusted in ifmedia_ioctl(), so
142bf354a07Smsaitoh * there is no problem to trust ifm_count.
143bf354a07Smsaitoh */
144bf354a07Smsaitoh minwords = ifmr->ifm_count;
14541aa5859Sriastradh kptr = malloc(minwords * sizeof(*kptr), M_TEMP,
14641aa5859Sriastradh M_WAITOK|M_ZERO);
147bf354a07Smsaitoh if (kptr == NULL)
148bf354a07Smsaitoh return ENOMEM;
149bf354a07Smsaitoh
150bf354a07Smsaitoh /*
151bf354a07Smsaitoh * Convert ifm_current and ifm_active.
152bf354a07Smsaitoh * It's not required to convert ifm_mask.
153bf354a07Smsaitoh */
154bf354a07Smsaitoh ifmword_n2o(&ifmr->ifm_current, &ifmr->ifm_current);
155bf354a07Smsaitoh ifmword_n2o(&ifmr->ifm_active, &ifmr->ifm_active);
156bf354a07Smsaitoh
157bf354a07Smsaitoh /* Convert ifm_ulist array */
158bf354a07Smsaitoh for (count = 0; count < minwords; count++) {
159bf354a07Smsaitoh int oldmwd;
160bf354a07Smsaitoh
161bf354a07Smsaitoh error = ufetch_int(&ifmr->ifm_ulist[count], &oldmwd);
162bf354a07Smsaitoh if (error != 0)
163bf354a07Smsaitoh goto out;
164bf354a07Smsaitoh ifmword_n2o(&kptr[count], &oldmwd);
165bf354a07Smsaitoh }
166bf354a07Smsaitoh
167bf354a07Smsaitoh /* Copy to userland in old format */
168bf354a07Smsaitoh error = copyout(kptr, ifmr->ifm_ulist,
169bf354a07Smsaitoh minwords * sizeof(*kptr));
170bf354a07Smsaitoh out:
171bf354a07Smsaitoh free(kptr, M_TEMP);
172bf354a07Smsaitoh return error;
173bf354a07Smsaitoh default:
174bf354a07Smsaitoh return 0;
175bf354a07Smsaitoh }
176bf354a07Smsaitoh }
177bf354a07Smsaitoh
178bf354a07Smsaitoh void
ifmedia_80_init(void)179bf354a07Smsaitoh ifmedia_80_init(void)
180bf354a07Smsaitoh {
181bf354a07Smsaitoh
1828a031a1dSpgoyette MODULE_HOOK_SET(ifmedia_80_pre_hook, compat_ifmediareq_pre);
1838a031a1dSpgoyette MODULE_HOOK_SET(ifmedia_80_post_hook, compat_ifmediareq_post);
184bf354a07Smsaitoh }
185bf354a07Smsaitoh
186bf354a07Smsaitoh void
ifmedia_80_fini(void)187bf354a07Smsaitoh ifmedia_80_fini(void)
188bf354a07Smsaitoh {
189bf354a07Smsaitoh
190bf354a07Smsaitoh MODULE_HOOK_UNSET(ifmedia_80_post_hook);
191bf354a07Smsaitoh MODULE_HOOK_UNSET(ifmedia_80_pre_hook);
192bf354a07Smsaitoh }
193