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