1 /* $NetBSD: ifconfig.c,v 1.34 1997/04/21 01:17:58 lukem Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (c) 1997 Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Copyright (c) 1983, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, 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
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
74 #include <net/if_dl.h>
75 #include <net/if_types.h>
76 #include <net/if_media.h>
77 #include <net/route.h>
89 #include <libifconfig.h>
93 static void domediaopt(if_ctx *, const char *, bool);
94 static ifmedia_t get_media_subtype(ifmedia_t, const char *);
95 static ifmedia_t get_media_mode(ifmedia_t, const char *);
96 static ifmedia_t get_media_options(ifmedia_t, const char *);
97 static void print_media(ifmedia_t, bool);
98 static void print_media_ifconfig(ifmedia_t);
101 media_status(if_ctx *ctx)
103 struct ifmediareq *ifmr;
105 if (ifconfig_media_get_mediareq(lifh, ctx->ifname, &ifmr) == -1)
108 if (ifmr->ifm_count == 0) {
109 warnx("%s: no media types?", ctx->ifname);
114 print_media(ifmr->ifm_current, true);
115 if (ifmr->ifm_active != ifmr->ifm_current) {
118 print_media(ifmr->ifm_active, false);
124 if (ifmr->ifm_status & IFM_AVALID) {
125 struct ifdownreason ifdr;
128 status = ifconfig_media_get_status(ifmr);
129 printf("\tstatus: %s", status);
130 if (strcmp(status, "no carrier") == 0 &&
131 ifconfig_media_get_downreason(lifh, ctx->ifname, &ifdr) == 0) {
132 switch (ifdr.ifdr_reason) {
133 case IFDR_REASON_MSG:
134 printf(" (%s)", ifdr.ifdr_msg);
136 case IFDR_REASON_VENDOR:
137 printf(" (vendor code %d)",
147 if (ctx->args->supmedia) {
148 printf("\tsupported media:\n");
149 for (int i = 0; i < ifmr->ifm_count; ++i) {
151 print_media_ifconfig(ifmr->ifm_ulist[i]);
160 ifmedia_getstate(if_ctx *ctx)
162 static struct ifmediareq *ifmr = NULL;
167 if (ifconfig_media_get_mediareq(lifh, ctx->ifname, &ifmr) == -1)
168 errc(1, ifconfig_err_errno(lifh),
169 "%s: ifconfig_media_get_mediareq", ctx->ifname);
171 if (ifmr->ifm_count == 0)
172 errx(1, "%s: no media types?", ctx->ifname);
178 setifmediacallback(if_ctx *ctx, void *arg)
180 struct ifmediareq *ifmr = (struct ifmediareq *)arg;
181 static bool did_it = false;
182 struct ifreq ifr = {};
185 ifr.ifr_media = ifmr->ifm_current;
186 if (ioctl_ctx_ifr(ctx, SIOCSIFMEDIA, &ifr) < 0)
187 err(1, "SIOCSIFMEDIA (media)");
194 setmedia(if_ctx *ctx, const char *val, int d __unused)
196 struct ifmediareq *ifmr;
199 ifmr = ifmedia_getstate(ctx);
202 * We are primarily concerned with the top-level type.
203 * However, "current" may be only IFM_NONE, so we just look
204 * for the top-level type in the first "supported type"
207 * (I'm assuming that all supported media types for a given
208 * interface will be the same top-level type..)
210 subtype = get_media_subtype(ifmr->ifm_ulist[0], val);
212 ifmr->ifm_current = (ifmr->ifm_current & IFM_IMASK) |
213 IFM_TYPE(ifmr->ifm_ulist[0]) | subtype;
215 callback_register(setifmediacallback, (void *)ifmr);
219 setmediaopt(if_ctx *ctx, const char *val, int d __unused)
222 domediaopt(ctx, val, false);
226 unsetmediaopt(if_ctx *ctx, const char *val, int d __unused)
229 domediaopt(ctx, val, true);
233 domediaopt(if_ctx *ctx, const char *val, bool clear)
235 struct ifmediareq *ifmr;
238 ifmr = ifmedia_getstate(ctx);
240 options = get_media_options(ifmr->ifm_ulist[0], val);
243 ifmr->ifm_current &= ~options;
245 if (options & IFM_HDX) {
246 ifmr->ifm_current &= ~IFM_FDX;
249 ifmr->ifm_current |= options;
251 callback_register(setifmediacallback, (void *)ifmr);
255 setmediainst(if_ctx *ctx, const char *val, int d __unused)
257 struct ifmediareq *ifmr;
260 ifmr = ifmedia_getstate(ctx);
263 if (inst < 0 || inst > (int)IFM_INST_MAX)
264 errx(1, "invalid media instance: %s", val);
266 ifmr->ifm_current = (ifmr->ifm_current & ~IFM_IMASK) | inst << IFM_ISHIFT;
268 callback_register(setifmediacallback, (void *)ifmr);
272 setmediamode(if_ctx *ctx, const char *val, int d __unused)
274 struct ifmediareq *ifmr;
277 ifmr = ifmedia_getstate(ctx);
279 mode = get_media_mode(ifmr->ifm_ulist[0], val);
281 ifmr->ifm_current = (ifmr->ifm_current & ~IFM_MMASK) | mode;
283 callback_register(setifmediacallback, (void *)ifmr);
287 get_media_subtype(ifmedia_t media, const char *val)
291 subtype = ifconfig_media_lookup_subtype(media, val);
292 if (subtype != INVALID_IFMEDIA)
296 errx(EXIT_FAILURE, "unknown media type 0x%x", media);
298 errx(EXIT_FAILURE, "unknown media subtype: %s", val);
300 err(EXIT_FAILURE, "ifconfig_media_lookup_subtype");
306 get_media_mode(ifmedia_t media, const char *val)
310 mode = ifconfig_media_lookup_mode(media, val);
311 if (mode != INVALID_IFMEDIA)
315 errx(EXIT_FAILURE, "unknown media type 0x%x", media);
317 return (INVALID_IFMEDIA);
319 err(EXIT_FAILURE, "ifconfig_media_lookup_subtype");
325 get_media_options(ifmedia_t media, const char *val)
328 const char **optnames;
334 * We muck with the string, so copy it.
338 err(EXIT_FAILURE, "strdup");
341 * Split the comma-delimited list into separate strings.
344 for (opt = opts; (opt = strtok(opt, ",")) != NULL; opt = NULL)
350 optnames = calloc(nopts, sizeof(*optnames));
351 if (optnames == NULL)
352 err(EXIT_FAILURE, "calloc");
354 for (size_t i = 0; i < nopts; ++i) {
356 opt = strchr(opt, '\0') + 1;
360 * Look up the options in the user-provided list.
362 options = ifconfig_media_lookup_options(media, optnames, nopts);
364 err(EXIT_FAILURE, "ifconfig_media_lookup_options");
366 for (size_t i = 0; i < nopts; ++i) {
367 if (options[i] == INVALID_IFMEDIA)
368 errx(EXIT_FAILURE, "unknown option: %s", optnames[i]);
378 print_media(ifmedia_t media, bool print_toptype)
380 const char *val, **options;
382 val = ifconfig_media_get_type(media);
384 printf("<unknown type>");
386 } else if (print_toptype) {
390 val = ifconfig_media_get_subtype(media);
392 printf("<unknown subtype>");
402 val = ifconfig_media_get_mode(media);
403 if (val != NULL && strcasecmp("autoselect", val) != 0)
404 printf(" mode %s", val);
407 options = ifconfig_media_get_options(media);
408 if (options != NULL && options[0] != NULL) {
409 printf(" <%s", options[0]);
410 for (size_t i = 1; options[i] != NULL; ++i)
411 printf(",%s", options[i]);
416 if (print_toptype && IFM_INST(media) != 0)
417 printf(" instance %d", IFM_INST(media));
421 print_media_ifconfig(ifmedia_t media)
423 const char *val, **options;
425 val = ifconfig_media_get_type(media);
427 printf("<unknown type>");
432 * Don't print the top-level type; it's not like we can
433 * change it, or anything.
436 val = ifconfig_media_get_subtype(media);
438 printf("<unknown subtype>");
442 printf("media %s", val);
444 val = ifconfig_media_get_mode(media);
446 printf(" mode %s", val);
448 options = ifconfig_media_get_options(media);
449 if (options != NULL && options[0] != NULL) {
450 printf(" mediaopt %s", options[0]);
451 for (size_t i = 1; options[i] != NULL; ++i)
452 printf(",%s", options[i]);
456 if (IFM_INST(media) != 0)
457 printf(" instance %d", IFM_INST(media));
460 /**********************************************************************
462 **********************************************************************/
464 static struct cmd media_cmds[] = {
465 DEF_CMD_ARG("media", setmedia),
466 DEF_CMD_ARG("mode", setmediamode),
467 DEF_CMD_ARG("mediaopt", setmediaopt),
468 DEF_CMD_ARG("-mediaopt",unsetmediaopt),
469 DEF_CMD_ARG("inst", setmediainst),
470 DEF_CMD_ARG("instance", setmediainst),
472 static struct afswtch af_media = {
473 .af_name = "af_media",
475 .af_other_status = media_status,
478 static __constructor void
481 for (size_t i = 0; i < nitems(media_cmds); i++)
482 cmd_register(&media_cmds[i]);
483 af_register(&af_media);