Wed Jan 25 07:19:24 2017 UTC ()
ifmedia_removeall(): Clear ifm_cur and ifm_media after removing all ifmedia
entries.


(msaitoh)
diff -r1.31 -r1.32 src/sys/net/if_media.c

cvs diff -r1.31 -r1.32 src/sys/net/if_media.c (switch to unified diff)

--- src/sys/net/if_media.c 2017/01/25 07:17:19 1.31
+++ src/sys/net/if_media.c 2017/01/25 07:19:24 1.32
@@ -1,506 +1,508 @@ @@ -1,506 +1,508 @@
1/* $NetBSD: if_media.c,v 1.31 2017/01/25 07:17:19 msaitoh Exp $ */ 1/* $NetBSD: if_media.c,v 1.32 2017/01/25 07:19:24 msaitoh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center. 9 * NASA Ames Research Center.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Copyright (c) 1997 34 * Copyright (c) 1997
35 * Jonathan Stone and Jason R. Thorpe. All rights reserved. 35 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
36 * 36 *
37 * This software is derived from information provided by Matt Thomas. 37 * This software is derived from information provided by Matt Thomas.
38 * 38 *
39 * Redistribution and use in source and binary forms, with or without 39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions 40 * modification, are permitted provided that the following conditions
41 * are met: 41 * are met:
42 * 1. Redistributions of source code must retain the above copyright 42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer. 43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright 44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the 45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution. 46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software 47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement: 48 * must display the following acknowledgement:
49 * This product includes software developed by Jonathan Stone 49 * This product includes software developed by Jonathan Stone
50 * and Jason R. Thorpe for the NetBSD Project. 50 * and Jason R. Thorpe for the NetBSD Project.
51 * 4. The names of the authors may not be used to endorse or promote products 51 * 4. The names of the authors may not be used to endorse or promote products
52 * derived from this software without specific prior written permission. 52 * derived from this software without specific prior written permission.
53 * 53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
59 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 59 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 60 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
61 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 61 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE. 64 * SUCH DAMAGE.
65 */ 65 */
66 66
67/* 67/*
68 * BSD/OS-compatible network interface media selection. 68 * BSD/OS-compatible network interface media selection.
69 * 69 *
70 * Where it is safe to do so, this code strays slightly from the BSD/OS 70 * Where it is safe to do so, this code strays slightly from the BSD/OS
71 * design. Software which uses the API (device drivers, basically) 71 * design. Software which uses the API (device drivers, basically)
72 * shouldn't notice any difference. 72 * shouldn't notice any difference.
73 * 73 *
74 * Many thanks to Matt Thomas for providing the information necessary 74 * Many thanks to Matt Thomas for providing the information necessary
75 * to implement this interface. 75 * to implement this interface.
76 */ 76 */
77 77
78#include <sys/cdefs.h> 78#include <sys/cdefs.h>
79__KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.31 2017/01/25 07:17:19 msaitoh Exp $"); 79__KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.32 2017/01/25 07:19:24 msaitoh Exp $");
80 80
81#include <sys/param.h> 81#include <sys/param.h>
82#include <sys/systm.h> 82#include <sys/systm.h>
83#include <sys/errno.h> 83#include <sys/errno.h>
84#include <sys/ioctl.h> 84#include <sys/ioctl.h>
85#include <sys/socket.h> 85#include <sys/socket.h>
86#include <sys/malloc.h> 86#include <sys/malloc.h>
87 87
88#include <net/if.h> 88#include <net/if.h>
89#include <net/if_media.h> 89#include <net/if_media.h>
90#include <net/netisr.h> 90#include <net/netisr.h>
91 91
92/* 92/*
93 * Compile-time options: 93 * Compile-time options:
94 * IFMEDIA_DEBUG: 94 * IFMEDIA_DEBUG:
95 * turn on implementation-level debug printfs. 95 * turn on implementation-level debug printfs.
96 * Useful for debugging newly-ported drivers. 96 * Useful for debugging newly-ported drivers.
97 */ 97 */
98 98
99#ifdef IFMEDIA_DEBUG 99#ifdef IFMEDIA_DEBUG
100int ifmedia_debug = 0; 100int ifmedia_debug = 0;
101static void ifmedia_printword(int); 101static void ifmedia_printword(int);
102#endif 102#endif
103 103
104MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state"); 104MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state");
105 105
106/* 106/*
107 * Initialize if_media struct for a specific interface instance. 107 * Initialize if_media struct for a specific interface instance.
108 */ 108 */
109void 109void
110ifmedia_init(struct ifmedia *ifm, int dontcare_mask, 110ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
111 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback) 111 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
112{ 112{
113 113
114 TAILQ_INIT(&ifm->ifm_list); 114 TAILQ_INIT(&ifm->ifm_list);
115 ifm->ifm_cur = NULL; 115 ifm->ifm_cur = NULL;
116 ifm->ifm_media = IFM_NONE; 116 ifm->ifm_media = IFM_NONE;
117 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */ 117 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
118 ifm->ifm_change = change_callback; 118 ifm->ifm_change = change_callback;
119 ifm->ifm_status = status_callback; 119 ifm->ifm_status = status_callback;
120} 120}
121 121
122int 122int
123ifmedia_change(struct ifmedia *ifm, struct ifnet *ifp) 123ifmedia_change(struct ifmedia *ifm, struct ifnet *ifp)
124{ 124{
125 return (*ifm->ifm_change)(ifp); 125 return (*ifm->ifm_change)(ifp);
126} 126}
127 127
128/* 128/*
129 * Add a media configuration to the list of supported media 129 * Add a media configuration to the list of supported media
130 * for a specific interface instance. 130 * for a specific interface instance.
131 */ 131 */
132void 132void
133ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux) 133ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
134{ 134{
135 struct ifmedia_entry *entry; 135 struct ifmedia_entry *entry;
136 136
137#ifdef IFMEDIA_DEBUG 137#ifdef IFMEDIA_DEBUG
138 if (ifmedia_debug) { 138 if (ifmedia_debug) {
139 if (ifm == NULL) { 139 if (ifm == NULL) {
140 printf("ifmedia_add: null ifm\n"); 140 printf("ifmedia_add: null ifm\n");
141 return; 141 return;
142 } 142 }
143 printf("Adding entry for "); 143 printf("Adding entry for ");
144 ifmedia_printword(mword); 144 ifmedia_printword(mword);
145 } 145 }
146#endif 146#endif
147 147
148 entry = malloc(sizeof(*entry), M_IFMEDIA, M_NOWAIT); 148 entry = malloc(sizeof(*entry), M_IFMEDIA, M_NOWAIT);
149 if (entry == NULL) 149 if (entry == NULL)
150 panic("ifmedia_add: can't malloc entry"); 150 panic("ifmedia_add: can't malloc entry");
151 151
152 entry->ifm_media = mword; 152 entry->ifm_media = mword;
153 entry->ifm_data = data; 153 entry->ifm_data = data;
154 entry->ifm_aux = aux; 154 entry->ifm_aux = aux;
155 155
156 TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list); 156 TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
157} 157}
158 158
159/* 159/*
160 * Add an array of media configurations to the list of 160 * Add an array of media configurations to the list of
161 * supported media for a specific interface instance. 161 * supported media for a specific interface instance.
162 */ 162 */
163void 163void
164ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count) 164ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
165{ 165{
166 int i; 166 int i;
167 167
168 for (i = 0; i < count; i++) 168 for (i = 0; i < count; i++)
169 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data, 169 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
170 lp[i].ifm_aux); 170 lp[i].ifm_aux);
171} 171}
172 172
173/* 173/*
174 * Set the default active media. 174 * Set the default active media.
175 * 175 *
176 * Called by device-specific code which is assumed to have already 176 * Called by device-specific code which is assumed to have already
177 * selected the default media in hardware. We do _not_ call the 177 * selected the default media in hardware. We do _not_ call the
178 * media-change callback. 178 * media-change callback.
179 */ 179 */
180void 180void
181ifmedia_set(struct ifmedia *ifm, int target) 181ifmedia_set(struct ifmedia *ifm, int target)
182{ 182{
183 struct ifmedia_entry *match; 183 struct ifmedia_entry *match;
184 184
185 match = ifmedia_match(ifm, target, ifm->ifm_mask); 185 match = ifmedia_match(ifm, target, ifm->ifm_mask);
186 186
187 /* 187 /*
188 * If we didn't find the requested media, then we try to fall 188 * If we didn't find the requested media, then we try to fall
189 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE. If that's 189 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE. If that's
190 * not on the list, then we add it and set the media to it. 190 * not on the list, then we add it and set the media to it.
191 * 191 *
192 * Since ifmedia_set is almost always called with IFM_AUTO or 192 * Since ifmedia_set is almost always called with IFM_AUTO or
193 * with a known-good media, this really should only occur if we: 193 * with a known-good media, this really should only occur if we:
194 * 194 *
195 * a) didn't find any PHYs, or 195 * a) didn't find any PHYs, or
196 * b) didn't find an autoselect option on the PHY when the 196 * b) didn't find an autoselect option on the PHY when the
197 * parent ethernet driver expected to. 197 * parent ethernet driver expected to.
198 * 198 *
199 * In either case, it makes sense to select no media. 199 * In either case, it makes sense to select no media.
200 */ 200 */
201 if (match == NULL) { 201 if (match == NULL) {
202 printf("ifmedia_set: no match for 0x%x/0x%x\n", 202 printf("ifmedia_set: no match for 0x%x/0x%x\n",
203 target, ~ifm->ifm_mask); 203 target, ~ifm->ifm_mask);
204 target = (target & IFM_NMASK) | IFM_NONE; 204 target = (target & IFM_NMASK) | IFM_NONE;
205 match = ifmedia_match(ifm, target, ifm->ifm_mask); 205 match = ifmedia_match(ifm, target, ifm->ifm_mask);
206 if (match == NULL) { 206 if (match == NULL) {
207 ifmedia_add(ifm, target, 0, NULL); 207 ifmedia_add(ifm, target, 0, NULL);
208 match = ifmedia_match(ifm, target, ifm->ifm_mask); 208 match = ifmedia_match(ifm, target, ifm->ifm_mask);
209 if (match == NULL) { 209 if (match == NULL) {
210 panic("ifmedia_set failed"); 210 panic("ifmedia_set failed");
211 } 211 }
212 } 212 }
213 } 213 }
214 ifm->ifm_cur = match; 214 ifm->ifm_cur = match;
215 215
216#ifdef IFMEDIA_DEBUG 216#ifdef IFMEDIA_DEBUG
217 if (ifmedia_debug) { 217 if (ifmedia_debug) {
218 printf("ifmedia_set: target "); 218 printf("ifmedia_set: target ");
219 ifmedia_printword(target); 219 ifmedia_printword(target);
220 printf("ifmedia_set: setting to "); 220 printf("ifmedia_set: setting to ");
221 ifmedia_printword(ifm->ifm_cur->ifm_media); 221 ifmedia_printword(ifm->ifm_cur->ifm_media);
222 } 222 }
223#endif 223#endif
224} 224}
225 225
226/* 226/*
227 * Device-independent media ioctl support function. 227 * Device-independent media ioctl support function.
228 */ 228 */
229int 229int
230ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm, 230ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
231 u_long cmd) 231 u_long cmd)
232{ 232{
233 struct ifmedia_entry *match; 233 struct ifmedia_entry *match;
234 struct ifmediareq *ifmr = (struct ifmediareq *) ifr; 234 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
235 int error = 0; 235 int error = 0;
236#ifdef OSIOCSIFMEDIA 236#ifdef OSIOCSIFMEDIA
237 struct oifreq *oifr = (struct oifreq *)ifr; 237 struct oifreq *oifr = (struct oifreq *)ifr;
238#endif 238#endif
239 239
240 if (ifp == NULL || ifr == NULL || ifm == NULL) 240 if (ifp == NULL || ifr == NULL || ifm == NULL)
241 return (EINVAL); 241 return (EINVAL);
242 242
243 switch (cmd) { 243 switch (cmd) {
244 244
245#ifdef OSIOCSIFMEDIA 245#ifdef OSIOCSIFMEDIA
246 case OSIOCSIFMEDIA: 246 case OSIOCSIFMEDIA:
247 ifr->ifr_media = oifr->ifr_media; 247 ifr->ifr_media = oifr->ifr_media;
248 /*FALLTHROUGH*/ 248 /*FALLTHROUGH*/
249#endif 249#endif
250 /* 250 /*
251 * Set the current media. 251 * Set the current media.
252 */ 252 */
253 case SIOCSIFMEDIA: 253 case SIOCSIFMEDIA:
254 { 254 {
255 struct ifmedia_entry *oldentry; 255 struct ifmedia_entry *oldentry;
256 u_int oldmedia; 256 u_int oldmedia;
257 u_int newmedia = ifr->ifr_media; 257 u_int newmedia = ifr->ifr_media;
258 258
259 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask); 259 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
260 if (match == NULL) { 260 if (match == NULL) {
261#ifdef IFMEDIA_DEBUG 261#ifdef IFMEDIA_DEBUG
262 if (ifmedia_debug) { 262 if (ifmedia_debug) {
263 printf( 263 printf(
264 "ifmedia_ioctl: no media found for 0x%x\n", 264 "ifmedia_ioctl: no media found for 0x%x\n",
265 newmedia); 265 newmedia);
266 } 266 }
267#endif 267#endif
268 return (EINVAL); 268 return (EINVAL);
269 } 269 }
270 270
271 /* 271 /*
272 * If no change, we're done. 272 * If no change, we're done.
273 * XXX Automedia may involve software intervention. 273 * XXX Automedia may involve software intervention.
274 * Keep going in case the connected media changed. 274 * Keep going in case the connected media changed.
275 * Similarly, if best match changed (kernel debugger?). 275 * Similarly, if best match changed (kernel debugger?).
276 */ 276 */
277 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) && 277 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
278 (newmedia == ifm->ifm_media) && 278 (newmedia == ifm->ifm_media) &&
279 (match == ifm->ifm_cur)) 279 (match == ifm->ifm_cur))
280 return 0; 280 return 0;
281 281
282 /* 282 /*
283 * We found a match, now make the driver switch to it. 283 * We found a match, now make the driver switch to it.
284 * Make sure to preserve our old media type in case the 284 * Make sure to preserve our old media type in case the
285 * driver can't switch. 285 * driver can't switch.
286 */ 286 */
287#ifdef IFMEDIA_DEBUG 287#ifdef IFMEDIA_DEBUG
288 if (ifmedia_debug) { 288 if (ifmedia_debug) {
289 printf("ifmedia_ioctl: switching %s to ", 289 printf("ifmedia_ioctl: switching %s to ",
290 ifp->if_xname); 290 ifp->if_xname);
291 ifmedia_printword(match->ifm_media); 291 ifmedia_printword(match->ifm_media);
292 } 292 }
293#endif 293#endif
294 oldentry = ifm->ifm_cur; 294 oldentry = ifm->ifm_cur;
295 oldmedia = ifm->ifm_media; 295 oldmedia = ifm->ifm_media;
296 ifm->ifm_cur = match; 296 ifm->ifm_cur = match;
297 ifm->ifm_media = newmedia; 297 ifm->ifm_media = newmedia;
298 error = ifmedia_change(ifm, ifp); 298 error = ifmedia_change(ifm, ifp);
299 if (error) { 299 if (error) {
300 ifm->ifm_cur = oldentry; 300 ifm->ifm_cur = oldentry;
301 ifm->ifm_media = oldmedia; 301 ifm->ifm_media = oldmedia;
302 } 302 }
303 break; 303 break;
304 } 304 }
305 305
306 /* 306 /*
307 * Get list of available media and current media on interface. 307 * Get list of available media and current media on interface.
308 */ 308 */
309 case SIOCGIFMEDIA: 309 case SIOCGIFMEDIA:
310 { 310 {
311 struct ifmedia_entry *ep; 311 struct ifmedia_entry *ep;
312 size_t nwords; 312 size_t nwords;
313 313
314 if (ifmr->ifm_count < 0) 314 if (ifmr->ifm_count < 0)
315 return EINVAL; 315 return EINVAL;
316 316
317 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ? 317 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
318 ifm->ifm_cur->ifm_media : IFM_NONE; 318 ifm->ifm_cur->ifm_media : IFM_NONE;
319 ifmr->ifm_mask = ifm->ifm_mask; 319 ifmr->ifm_mask = ifm->ifm_mask;
320 ifmr->ifm_status = 0; 320 ifmr->ifm_status = 0;
321 /* ifmedia_status */ 321 /* ifmedia_status */
322 (*ifm->ifm_status)(ifp, ifmr); 322 (*ifm->ifm_status)(ifp, ifmr);
323 323
324 /* 324 /*
325 * Count them so we know a-priori how much is the max we'll 325 * Count them so we know a-priori how much is the max we'll
326 * need. 326 * need.
327 */ 327 */
328 ep = TAILQ_FIRST(&ifm->ifm_list); 328 ep = TAILQ_FIRST(&ifm->ifm_list);
329 for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list)) 329 for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
330 nwords++; 330 nwords++;
331 331
332 if (ifmr->ifm_count != 0) { 332 if (ifmr->ifm_count != 0) {
333 size_t count; 333 size_t count;
334 size_t minwords = nwords > (size_t)ifmr->ifm_count 334 size_t minwords = nwords > (size_t)ifmr->ifm_count
335 ? (size_t)ifmr->ifm_count 335 ? (size_t)ifmr->ifm_count
336 : nwords; 336 : nwords;
337 int *kptr = (int *)malloc(minwords * sizeof(int), 337 int *kptr = (int *)malloc(minwords * sizeof(int),
338 M_TEMP, M_WAITOK); 338 M_TEMP, M_WAITOK);
339 /* 339 /*
340 * Get the media words from the interface's list. 340 * Get the media words from the interface's list.
341 */ 341 */
342 ep = TAILQ_FIRST(&ifm->ifm_list); 342 ep = TAILQ_FIRST(&ifm->ifm_list);
343 for (count = 0; ep != NULL && count < minwords; 343 for (count = 0; ep != NULL && count < minwords;
344 ep = TAILQ_NEXT(ep, ifm_list), count++) 344 ep = TAILQ_NEXT(ep, ifm_list), count++)
345 kptr[count] = ep->ifm_media; 345 kptr[count] = ep->ifm_media;
346 346
347 error = copyout(kptr, ifmr->ifm_ulist, 347 error = copyout(kptr, ifmr->ifm_ulist,
348 minwords * sizeof(int)); 348 minwords * sizeof(int));
349 if (error == 0 && ep != NULL) 349 if (error == 0 && ep != NULL)
350 error = E2BIG; /* oops! */ 350 error = E2BIG; /* oops! */
351 free(kptr, M_TEMP); 351 free(kptr, M_TEMP);
352 } 352 }
353 ifmr->ifm_count = nwords; 353 ifmr->ifm_count = nwords;
354 break; 354 break;
355 } 355 }
356 356
357 default: 357 default:
358 return (EINVAL); 358 return (EINVAL);
359 } 359 }
360 360
361 return (error); 361 return (error);
362} 362}
363 363
364/* 364/*
365 * Find media entry matching a given ifm word. 365 * Find media entry matching a given ifm word.
366 */ 366 */
367struct ifmedia_entry * 367struct ifmedia_entry *
368ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask) 368ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
369{ 369{
370 struct ifmedia_entry *match, *next; 370 struct ifmedia_entry *match, *next;
371 371
372 match = NULL; 372 match = NULL;
373 mask = ~mask; 373 mask = ~mask;
374 374
375 for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL; 375 for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
376 next = TAILQ_NEXT(next, ifm_list)) { 376 next = TAILQ_NEXT(next, ifm_list)) {
377 if ((next->ifm_media & mask) == (target & mask)) { 377 if ((next->ifm_media & mask) == (target & mask)) {
378 if (match) { 378 if (match) {
379#if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC) 379#if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
380 printf("ifmedia_match: multiple match for " 380 printf("ifmedia_match: multiple match for "
381 "0x%x/0x%x, selected instance %d\n", 381 "0x%x/0x%x, selected instance %d\n",
382 target, mask, IFM_INST(match->ifm_media)); 382 target, mask, IFM_INST(match->ifm_media));
383#endif 383#endif
384 break; 384 break;
385 } 385 }
386 match = next; 386 match = next;
387 } 387 }
388 } 388 }
389 389
390 return match; 390 return match;
391} 391}
392 392
393/* 393/*
394 * Delete all media for a given instance. 394 * Delete all media for a given instance.
395 */ 395 */
396void 396void
397ifmedia_delete_instance(struct ifmedia *ifm, u_int inst) 397ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
398{ 398{
399 struct ifmedia_entry *ife, *nife; 399 struct ifmedia_entry *ife, *nife;
400 400
401 for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL; 401 for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
402 ife = nife) { 402 ife = nife) {
403 nife = TAILQ_NEXT(ife, ifm_list); 403 nife = TAILQ_NEXT(ife, ifm_list);
404 if (inst == IFM_INST_ANY || 404 if (inst == IFM_INST_ANY ||
405 inst == IFM_INST(ife->ifm_media)) { 405 inst == IFM_INST(ife->ifm_media)) {
406 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list); 406 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
407 free(ife, M_IFMEDIA); 407 free(ife, M_IFMEDIA);
408 } 408 }
409 } 409 }
410} 410}
411 411
412void 412void
413ifmedia_removeall(struct ifmedia *ifm) 413ifmedia_removeall(struct ifmedia *ifm)
414{ 414{
415 struct ifmedia_entry *ife, *nife; 415 struct ifmedia_entry *ife, *nife;
416 416
417 for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL; ife = nife) { 417 for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL; ife = nife) {
418 nife = TAILQ_NEXT(ife, ifm_list); 418 nife = TAILQ_NEXT(ife, ifm_list);
419 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list); 419 TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
420 free(ife, M_IFMEDIA); 420 free(ife, M_IFMEDIA);
421 } 421 }
 422 ifm->ifm_cur = NULL;
 423 ifm->ifm_media = IFM_NONE;
422} 424}
423 425
424 426
425/* 427/*
426 * Compute the interface `baudrate' from the media, for the interface 428 * Compute the interface `baudrate' from the media, for the interface
427 * metrics (used by routing daemons). 429 * metrics (used by routing daemons).
428 */ 430 */
429static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] = 431static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
430 IFM_BAUDRATE_DESCRIPTIONS; 432 IFM_BAUDRATE_DESCRIPTIONS;
431 433
432uint64_t 434uint64_t
433ifmedia_baudrate(int mword) 435ifmedia_baudrate(int mword)
434{ 436{
435 int i; 437 int i;
436 438
437 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) { 439 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
438 if ((mword & (IFM_NMASK|IFM_TMASK)) == 440 if ((mword & (IFM_NMASK|IFM_TMASK)) ==
439 ifmedia_baudrate_descriptions[i].ifmb_word) 441 ifmedia_baudrate_descriptions[i].ifmb_word)
440 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate); 442 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
441 } 443 }
442 444
443 /* Not known. */ 445 /* Not known. */
444 return (0); 446 return (0);
445} 447}
446 448
447#ifdef IFMEDIA_DEBUG 449#ifdef IFMEDIA_DEBUG
448 450
449static const struct ifmedia_description ifm_type_descriptions[] = 451static const struct ifmedia_description ifm_type_descriptions[] =
450 IFM_TYPE_DESCRIPTIONS; 452 IFM_TYPE_DESCRIPTIONS;
451 453
452static const struct ifmedia_description ifm_subtype_descriptions[] = 454static const struct ifmedia_description ifm_subtype_descriptions[] =
453 IFM_SUBTYPE_DESCRIPTIONS; 455 IFM_SUBTYPE_DESCRIPTIONS;
454 456
455static const struct ifmedia_description ifm_option_descriptions[] = 457static const struct ifmedia_description ifm_option_descriptions[] =
456 IFM_OPTION_DESCRIPTIONS; 458 IFM_OPTION_DESCRIPTIONS;
457 459
458/* 460/*
459 * print a media word. 461 * print a media word.
460 */ 462 */
461static void 463static void
462ifmedia_printword(int ifmw) 464ifmedia_printword(int ifmw)
463{ 465{
464 const struct ifmedia_description *desc; 466 const struct ifmedia_description *desc;
465 int seen_option = 0; 467 int seen_option = 0;
466 468
467 /* Print the top-level interface type. */ 469 /* Print the top-level interface type. */
468 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; 470 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
469 desc++) { 471 desc++) {
470 if (IFM_TYPE(ifmw) == desc->ifmt_word) 472 if (IFM_TYPE(ifmw) == desc->ifmt_word)
471 break; 473 break;
472 } 474 }
473 if (desc->ifmt_string == NULL) 475 if (desc->ifmt_string == NULL)
474 printf("<unknown type> "); 476 printf("<unknown type> ");
475 else 477 else
476 printf("%s ", desc->ifmt_string); 478 printf("%s ", desc->ifmt_string);
477 479
478 /* Print the subtype. */ 480 /* Print the subtype. */
479 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; 481 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
480 desc++) { 482 desc++) {
481 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 483 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
482 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw)) 484 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
483 break; 485 break;
484 } 486 }
485 if (desc->ifmt_string == NULL) 487 if (desc->ifmt_string == NULL)
486 printf("<unknown subtype>"); 488 printf("<unknown subtype>");
487 else 489 else
488 printf("%s", desc->ifmt_string); 490 printf("%s", desc->ifmt_string);
489 491
490 /* Print any options. */ 492 /* Print any options. */
491 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL; 493 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
492 desc++) { 494 desc++) {
493 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 495 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
494 (ifmw & desc->ifmt_word) != 0 && 496 (ifmw & desc->ifmt_word) != 0 &&
495 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) { 497 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
496 if (seen_option == 0) 498 if (seen_option == 0)
497 printf(" <"); 499 printf(" <");
498 printf("%s%s", seen_option ? "," : "", 500 printf("%s%s", seen_option ? "," : "",
499 desc->ifmt_string); 501 desc->ifmt_string);
500 seen_option |= IFM_OPTIONS(desc->ifmt_word); 502 seen_option |= IFM_OPTIONS(desc->ifmt_word);
501 } 503 }
502 } 504 }
503 printf("%s\n", seen_option ? ">" : ""); 505 printf("%s\n", seen_option ? ">" : "");
504} 506}
505 507
506#endif /* IFMEDIA_DEBUG */ 508#endif /* IFMEDIA_DEBUG */