ifconfig.c revision 1.169 1 /* $NetBSD: ifconfig.c,v 1.169 2005/08/11 20:56:05 rpaulo Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1983, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 #ifndef lint
71 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
72 The Regents of the University of California. All rights reserved.\n");
73 #endif /* not lint */
74
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
78 #else
79 __RCSID("$NetBSD: ifconfig.c,v 1.169 2005/08/11 20:56:05 rpaulo Exp $");
80 #endif
81 #endif /* not lint */
82
83 #include <sys/param.h>
84 #include <sys/socket.h>
85 #include <sys/ioctl.h>
86
87 #include <net/if.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
90 #include <net/if_ether.h>
91 #include <netinet/in.h> /* XXX */
92 #include <netinet/in_var.h> /* XXX */
93
94 #include <netdb.h>
95
96 #include <sys/protosw.h>
97
98 #include <ctype.h>
99 #include <err.h>
100 #include <errno.h>
101 #include <stddef.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <unistd.h>
106 #include <ifaddrs.h>
107 #include <util.h>
108
109 #include "extern.h"
110
111 #ifndef INET_ONLY
112 #include "af_atalk.h"
113 #include "af_iso.h"
114 #include "af_ns.h"
115 #endif /* ! INET_ONLY */
116 #include "af_inet.h"
117 #ifdef INET6
118 #include "af_inet6.h"
119 #endif /* INET6 */
120
121 #include "agr.h"
122 #include "ieee80211.h"
123 #include "tunnel.h"
124 #include "vlan.h"
125
126 struct ifreq ifr, ridreq;
127 struct ifaliasreq addreq __attribute__((aligned(4)));
128
129 char name[30];
130 u_short flags;
131 int setaddr, doalias;
132 u_long metric, mtu;
133 int clearaddr, s;
134 int newaddr = -1;
135 int conflicting = 0;
136 int af;
137 int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
138 int hflag;
139 #ifdef INET6
140 int Lflag;
141 #endif
142 int explicit_prefix = 0;
143
144 struct ifcapreq g_ifcr;
145 int g_ifcr_updated;
146
147 void notealias(const char *, int);
148 void notrailers(const char *, int);
149 void setifaddr(const char *, int);
150 void setifdstaddr(const char *, int);
151 void setifflags(const char *, int);
152 void setifcaps(const char *, int);
153 void setifbroadaddr(const char *, int);
154 void setifipdst(const char *, int);
155 void setifmetric(const char *, int);
156 void setifmtu(const char *, int);
157 void setifnetmask(const char *, int);
158 void setifprefixlen(const char *, int);
159 void setmedia(const char *, int);
160 void setmediamode(const char *, int);
161 void setmediaopt(const char *, int);
162 void unsetmediaopt(const char *, int);
163 void setmediainst(const char *, int);
164 void clone_create(const char *, int);
165 void clone_destroy(const char *, int);
166 int main(int, char *[]);
167
168 /*
169 * Media stuff. Whenever a media command is first performed, the
170 * currently select media is grabbed for this interface. If `media'
171 * is given, the current media word is modifed. `mediaopt' commands
172 * only modify the set and clear words. They then operate on the
173 * current media word later.
174 */
175 int media_current;
176 int mediaopt_set;
177 int mediaopt_clear;
178
179 int actions; /* Actions performed */
180
181 #define A_MEDIA 0x0001 /* media command */
182 #define A_MEDIAOPTSET 0x0002 /* mediaopt command */
183 #define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
184 #define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
185 #define A_MEDIAINST 0x0008 /* instance or inst command */
186 #define A_MEDIAMODE 0x0010 /* mode command */
187
188 #define NEXTARG 0xffffff
189 #define NEXTARG2 0xfffffe
190
191 const struct cmd {
192 const char *c_name;
193 int c_parameter; /* NEXTARG means next argv */
194 int c_action; /* defered action */
195 void (*c_func)(const char *, int);
196 void (*c_func2)(const char *, const char *);
197 } cmds[] = {
198 { "up", IFF_UP, 0, setifflags } ,
199 { "down", -IFF_UP, 0, setifflags },
200 { "trailers", -1, 0, notrailers },
201 { "-trailers", 1, 0, notrailers },
202 { "arp", -IFF_NOARP, 0, setifflags },
203 { "-arp", IFF_NOARP, 0, setifflags },
204 { "debug", IFF_DEBUG, 0, setifflags },
205 { "-debug", -IFF_DEBUG, 0, setifflags },
206 { "alias", IFF_UP, 0, notealias },
207 { "-alias", -IFF_UP, 0, notealias },
208 { "delete", -IFF_UP, 0, notealias },
209 #ifdef notdef
210 #define EN_SWABIPS 0x1000
211 { "swabips", EN_SWABIPS, 0, setifflags },
212 { "-swabips", -EN_SWABIPS, 0, setifflags },
213 #endif
214 { "netmask", NEXTARG, 0, setifnetmask },
215 { "metric", NEXTARG, 0, setifmetric },
216 { "mtu", NEXTARG, 0, setifmtu },
217 { "bssid", NEXTARG, 0, setifbssid },
218 { "-bssid", -1, 0, setifbssid },
219 { "chan", NEXTARG, 0, setifchan },
220 { "-chan", -1, 0, setifchan },
221 { "ssid", NEXTARG, 0, setifnwid },
222 { "nwid", NEXTARG, 0, setifnwid },
223 { "nwkey", NEXTARG, 0, setifnwkey },
224 { "-nwkey", -1, 0, setifnwkey },
225 { "powersave", 1, 0, setifpowersave },
226 { "-powersave", 0, 0, setifpowersave },
227 { "powersavesleep", NEXTARG, 0, setifpowersavesleep },
228 { "broadcast", NEXTARG, 0, setifbroadaddr },
229 { "ipdst", NEXTARG, 0, setifipdst },
230 { "prefixlen", NEXTARG, 0, setifprefixlen},
231 #ifdef INET6
232 { "anycast", IN6_IFF_ANYCAST, 0, setia6flags },
233 { "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags },
234 { "tentative", IN6_IFF_TENTATIVE, 0, setia6flags },
235 { "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags },
236 { "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
237 { "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
238 { "pltime", NEXTARG, 0, setia6pltime },
239 { "vltime", NEXTARG, 0, setia6vltime },
240 { "eui64", 0, 0, setia6eui64 },
241 #endif /*INET6*/
242 #ifndef INET_ONLY
243 { "range", NEXTARG, 0, setatrange },
244 { "phase", NEXTARG, 0, setatphase },
245 { "snpaoffset", NEXTARG, 0, setsnpaoffset },
246 { "nsellength", NEXTARG, 0, setnsellength },
247 #endif /* INET_ONLY */
248 { "tunnel", NEXTARG2, 0, NULL,
249 settunnel } ,
250 { "deletetunnel", 0, 0, deletetunnel },
251 { "vlan", NEXTARG, 0, setvlan } ,
252 { "vlanif", NEXTARG, 0, setvlanif } ,
253 { "-vlanif", 0, 0, unsetvlanif } ,
254 #if 0
255 /* XXX `create' special-cased below */
256 { "create", 0, 0, clone_create } ,
257 #endif
258 { "destroy", 0, 0, clone_destroy } ,
259 { "link0", IFF_LINK0, 0, setifflags } ,
260 { "-link0", -IFF_LINK0, 0, setifflags } ,
261 { "link1", IFF_LINK1, 0, setifflags } ,
262 { "-link1", -IFF_LINK1, 0, setifflags } ,
263 { "link2", IFF_LINK2, 0, setifflags } ,
264 { "-link2", -IFF_LINK2, 0, setifflags } ,
265 { "media", NEXTARG, A_MEDIA, setmedia },
266 { "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
267 { "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
268 { "mode", NEXTARG, A_MEDIAMODE, setmediamode },
269 { "instance", NEXTARG, A_MEDIAINST, setmediainst },
270 { "inst", NEXTARG, A_MEDIAINST, setmediainst },
271 { "ip4csum-tx", IFCAP_CSUM_IPv4_Tx,0, setifcaps },
272 { "-ip4csum-tx",-IFCAP_CSUM_IPv4_Tx,0, setifcaps },
273 { "ip4csum-rx", IFCAP_CSUM_IPv4_Rx,0, setifcaps },
274 { "-ip4csum-rx",-IFCAP_CSUM_IPv4_Rx,0, setifcaps },
275 { "tcp4csum-tx",IFCAP_CSUM_TCPv4_Tx,0, setifcaps },
276 { "-tcp4csum-tx",-IFCAP_CSUM_TCPv4_Tx,0, setifcaps },
277 { "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
278 { "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
279 { "udp4csum-tx",IFCAP_CSUM_UDPv4_Tx,0, setifcaps },
280 { "-udp4csum-tx",-IFCAP_CSUM_UDPv4_Tx,0, setifcaps },
281 { "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
282 { "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
283 { "tcp6csum-tx",IFCAP_CSUM_TCPv6_Tx,0, setifcaps },
284 { "-tcp6csum-tx",-IFCAP_CSUM_TCPv6_Tx,0, setifcaps },
285 { "tcp6csum-rx",IFCAP_CSUM_TCPv6_Rx,0, setifcaps },
286 { "-tcp6csum-rx",-IFCAP_CSUM_TCPv6_Rx,0, setifcaps },
287 { "udp6csum-tx",IFCAP_CSUM_UDPv6_Tx,0, setifcaps },
288 { "-udp6csum-tx",-IFCAP_CSUM_UDPv6_Tx,0, setifcaps },
289 { "udp6csum-rx",IFCAP_CSUM_UDPv6_Rx,0, setifcaps },
290 { "-udp6csum-rx",-IFCAP_CSUM_UDPv6_Rx,0, setifcaps },
291 { "ip4csum", IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx,
292 0, setifcaps },
293 { "-ip4csum", -(IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx),
294 0, setifcaps },
295 { "tcp4csum", IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx,
296 0, setifcaps },
297 { "-tcp4csum", -(IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx),
298 0, setifcaps },
299 { "udp4csum", IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx,
300 0, setifcaps },
301 { "-udp4csum", -(IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx),
302 0, setifcaps },
303 { "tcp6csum", IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx,
304 0, setifcaps },
305 { "-tcp6csum", -(IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx),
306 0, setifcaps },
307 { "udp6csum", IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx,
308 0, setifcaps },
309 { "-udp6csum", -(IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx),
310 0, setifcaps },
311 { "tso4", IFCAP_TSOv4, 0, setifcaps },
312 { "-tso4", -IFCAP_TSOv4, 0, setifcaps },
313 { "agrport", NEXTARG, 0, agraddport } ,
314 { "-agrport", NEXTARG, 0, agrremport } ,
315 { 0, 0, 0, setifaddr },
316 { 0, 0, 0, setifdstaddr },
317 };
318
319 int getinfo(struct ifreq *);
320 int carrier(void);
321 void printall(const char *);
322 void list_cloners(void);
323 void status(const struct sockaddr_dl *);
324 void usage(void);
325
326 void print_media_word(int, const char *);
327 void process_media_commands(void);
328 void init_current_media(void);
329
330 /* Known address families */
331 const struct afswtch afs[] = {
332 { "inet", AF_INET, in_status, in_getaddr, in_getprefix,
333 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
334 #ifdef INET6
335 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
336 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
337 /*
338 * Deleting the first address before setting new one is
339 * not prefered way in this protocol.
340 */
341 0,
342 &in6_ridreq, &in6_addreq },
343 #endif
344 #ifndef INET_ONLY /* small version, for boot media */
345 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
346 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
347 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
348 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
349 { "iso", AF_ISO, iso_status, iso_getaddr, NULL,
350 SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
351 &iso_ridreq, &iso_addreq },
352 #endif /* INET_ONLY */
353 { 0, 0, 0, 0 }
354 };
355
356 const struct afswtch *afp; /*the address family being set or asked about*/
357
358 int
359 main(int argc, char *argv[])
360 {
361 int ch;
362
363 /* Parse command-line options */
364 aflag = mflag = vflag = zflag = 0;
365 while ((ch = getopt(argc, argv, "AabCdhlmsuvz"
366 #ifdef INET6
367 "L"
368 #endif
369 )) != -1) {
370 switch (ch) {
371 case 'A':
372 warnx("-A is deprecated");
373 break;
374
375 case 'a':
376 aflag = 1;
377 break;
378
379 case 'b':
380 bflag = 1;
381 break;
382
383 case 'C':
384 Cflag = 1;
385 break;
386
387 case 'd':
388 dflag = 1;
389 break;
390 case 'h':
391 hflag = 1;
392 break;
393 #ifdef INET6
394 case 'L':
395 Lflag = 1;
396 break;
397 #endif
398
399 case 'l':
400 lflag = 1;
401 break;
402
403 case 'm':
404 mflag = 1;
405 break;
406
407 case 's':
408 sflag = 1;
409 break;
410
411 case 'u':
412 uflag = 1;
413 break;
414
415 case 'v':
416 vflag = 1;
417 break;
418
419 case 'z':
420 zflag = 1;
421 break;
422
423
424 default:
425 usage();
426 /* NOTREACHED */
427 }
428 }
429 argc -= optind;
430 argv += optind;
431
432 /*
433 * -l means "list all interfaces", and is mutally exclusive with
434 * all other flags/commands.
435 *
436 * -C means "list all names of cloners", and it mutually exclusive
437 * with all other flags/commands.
438 *
439 * -a means "print status of all interfaces".
440 */
441 if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
442 usage();
443 #ifdef INET6
444 if ((lflag || Cflag) && Lflag)
445 usage();
446 #endif
447 if (lflag && Cflag)
448 usage();
449 if (Cflag) {
450 if (argc)
451 usage();
452 list_cloners();
453 exit(0);
454 }
455 if (aflag || lflag) {
456 if (argc > 1)
457 usage();
458 else if (argc == 1) {
459 afp = lookup_af_byname(argv[0]);
460 if (afp == NULL)
461 usage();
462 }
463 if (afp)
464 af = ifr.ifr_addr.sa_family = afp->af_af;
465 else
466 af = ifr.ifr_addr.sa_family = afs[0].af_af;
467 printall(NULL);
468 exit(0);
469 }
470
471 /* Make sure there's an interface name. */
472 if (argc < 1)
473 usage();
474 if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
475 errx(1, "interface name '%s' too long", argv[0]);
476 argc--; argv++;
477
478 /*
479 * NOTE: We must special-case the `create' command right
480 * here as we would otherwise fail in getinfo().
481 */
482 if (argc > 0 && strcmp(argv[0], "create") == 0) {
483 clone_create(argv[0], 0);
484 argc--, argv++;
485 if (argc == 0)
486 exit(0);
487 }
488
489 /* Check for address family. */
490 afp = NULL;
491 if (argc > 0) {
492 afp = lookup_af_byname(argv[0]);
493 if (afp != NULL) {
494 argv++;
495 argc--;
496 }
497 }
498
499 /* Initialize af, just for use in getinfo(). */
500 if (afp == NULL)
501 af = afs->af_af;
502 else
503 af = afp->af_af;
504
505 /* Get information about the interface. */
506 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
507 if (getinfo(&ifr) < 0)
508 exit(1);
509
510 if (sflag) {
511 if (argc != 0)
512 usage();
513 else
514 exit(carrier());
515 }
516
517 /* No more arguments means interface status. */
518 if (argc == 0) {
519 printall(name);
520 exit(0);
521 }
522
523 /* The following operations assume inet family as the default. */
524 if (afp == NULL)
525 afp = afs;
526 af = ifr.ifr_addr.sa_family = afp->af_af;
527
528 #ifdef INET6
529 in6_init();
530 #endif
531
532 /* Process commands. */
533 while (argc > 0) {
534 const struct cmd *p;
535
536 for (p = cmds; p->c_name; p++)
537 if (strcmp(argv[0], p->c_name) == 0)
538 break;
539 if (p->c_name == 0 && setaddr) {
540 if ((flags & IFF_POINTOPOINT) == 0) {
541 errx(EXIT_FAILURE,
542 "can't set destination address %s",
543 "on non-point-to-point link");
544 }
545 p++; /* got src, do dst */
546 }
547 if (p->c_func != NULL || p->c_func2 != NULL) {
548 if (p->c_parameter == NEXTARG) {
549 if (argc < 2)
550 errx(EXIT_FAILURE,
551 "'%s' requires argument",
552 p->c_name);
553 (*p->c_func)(argv[1], 0);
554 argc--, argv++;
555 } else if (p->c_parameter == NEXTARG2) {
556 if (argc < 3)
557 errx(EXIT_FAILURE,
558 "'%s' requires 2 arguments",
559 p->c_name);
560 (*p->c_func2)(argv[1], argv[2]);
561 argc -= 2, argv += 2;
562 } else
563 (*p->c_func)(argv[0], p->c_parameter);
564 actions |= p->c_action;
565 }
566 argc--, argv++;
567 }
568
569 /*
570 * See if multiple alias, -alias, or delete commands were
571 * specified. More than one constitutes an invalid command line
572 */
573
574 if (conflicting > 1)
575 errx(EXIT_FAILURE,
576 "Only one use of alias, -alias or delete is valid.");
577
578 /* Process any media commands that may have been issued. */
579 process_media_commands();
580
581 if (af == AF_INET6 && explicit_prefix == 0) {
582 /*
583 * Aggregatable address architecture defines all prefixes
584 * are 64. So, it is convenient to set prefixlen to 64 if
585 * it is not specified.
586 */
587 setifprefixlen("64", 0);
588 /* in6_getprefix("64", MASK) if MASK is available here... */
589 }
590
591 #ifndef INET_ONLY
592 if (af == AF_ISO)
593 adjust_nsellength();
594
595 if (af == AF_APPLETALK)
596 checkatrange(&addreq.ifra_addr);
597
598 if (setipdst && af == AF_NS)
599 xns_set_nsip_route(&addreq.ifra_addr, &addreq.ifra_dstaddr);
600 #endif /* INET_ONLY */
601
602 if (clearaddr) {
603 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
604 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
605 err(EXIT_FAILURE, "SIOCDIFADDR");
606 }
607 if (newaddr > 0) {
608 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
609 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
610 warn("SIOCAIFADDR");
611 }
612
613 if (g_ifcr_updated) {
614 (void) strncpy(g_ifcr.ifcr_name, name,
615 sizeof(g_ifcr.ifcr_name));
616 if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
617 err(EXIT_FAILURE, "SIOCSIFCAP");
618 }
619
620 exit(0);
621 }
622
623 const struct afswtch *
624 lookup_af_byname(const char *cp)
625 {
626 const struct afswtch *a;
627
628 for (a = afs; a->af_name != NULL; a++)
629 if (strcmp(a->af_name, cp) == 0)
630 return (a);
631 return (NULL);
632 }
633
634 const struct afswtch *
635 lookup_af_bynum(int afnum)
636 {
637 const struct afswtch *a;
638
639 for (a = afs; a->af_name != NULL; a++)
640 if (a->af_af == afnum)
641 return (a);
642 return (NULL);
643 }
644
645 void
646 getsock(int naf)
647 {
648 static int oaf = -1;
649
650 if (oaf == naf)
651 return;
652 if (oaf != -1)
653 close(s);
654 s = socket(naf, SOCK_DGRAM, 0);
655 if (s < 0)
656 oaf = -1;
657 else
658 oaf = naf;
659 }
660
661 int
662 getinfo(struct ifreq *giifr)
663 {
664
665 getsock(af);
666 if (s < 0)
667 err(EXIT_FAILURE, "socket");
668 if (ioctl(s, SIOCGIFFLAGS, giifr) == -1) {
669 warn("SIOCGIFFLAGS %s", giifr->ifr_name);
670 return (-1);
671 }
672 flags = giifr->ifr_flags;
673 if (ioctl(s, SIOCGIFMETRIC, giifr) == -1) {
674 warn("SIOCGIFMETRIC %s", giifr->ifr_name);
675 metric = 0;
676 } else
677 metric = giifr->ifr_metric;
678 if (ioctl(s, SIOCGIFMTU, giifr) == -1)
679 mtu = 0;
680 else
681 mtu = giifr->ifr_mtu;
682
683 memset(&g_ifcr, 0, sizeof(g_ifcr));
684 strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
685 (void) ioctl(s, SIOCGIFCAP, &g_ifcr);
686
687 return (0);
688 }
689
690 void
691 printall(const char *ifname)
692 {
693 struct ifaddrs *ifap, *ifa;
694 struct ifreq paifr;
695 const struct sockaddr_dl *sdl = NULL;
696 int idx;
697 char *p;
698
699 if (getifaddrs(&ifap) != 0)
700 err(EXIT_FAILURE, "getifaddrs");
701 p = NULL;
702 idx = 0;
703 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
704 memset(&paifr, 0, sizeof(paifr));
705 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
706 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
707 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
708 ifa->ifa_addr->sa_len);
709 }
710
711 if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
712 continue;
713 if (ifa->ifa_addr->sa_family == AF_LINK)
714 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
715 if (p && strcmp(p, ifa->ifa_name) == 0)
716 continue;
717 if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
718 continue;
719 p = ifa->ifa_name;
720
721 if (getinfo(&paifr) < 0)
722 continue;
723 if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
724 continue;
725 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
726 continue;
727 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
728 continue;
729
730 if (sflag && carrier())
731 continue;
732 idx++;
733 /*
734 * Are we just listing the interfaces?
735 */
736 if (lflag) {
737 if (idx > 1)
738 printf(" ");
739 fputs(name, stdout);
740 continue;
741 }
742
743 status(sdl);
744 sdl = NULL;
745 }
746 if (lflag)
747 printf("\n");
748 freeifaddrs(ifap);
749 }
750
751 void
752 list_cloners(void)
753 {
754 struct if_clonereq ifcr;
755 char *cp, *buf;
756 int idx;
757
758 memset(&ifcr, 0, sizeof(ifcr));
759
760 getsock(AF_INET);
761
762 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
763 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
764
765 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
766 if (buf == NULL)
767 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
768
769 ifcr.ifcr_count = ifcr.ifcr_total;
770 ifcr.ifcr_buffer = buf;
771
772 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
773 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
774
775 /*
776 * In case some disappeared in the mean time, clamp it down.
777 */
778 if (ifcr.ifcr_count > ifcr.ifcr_total)
779 ifcr.ifcr_count = ifcr.ifcr_total;
780
781 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
782 if (idx > 0)
783 printf(" ");
784 printf("%s", cp);
785 }
786
787 printf("\n");
788 free(buf);
789 return;
790 }
791
792 /*ARGSUSED*/
793 void
794 clone_create(const char *addr, int param)
795 {
796
797 /* We're called early... */
798 getsock(AF_INET);
799
800 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
801 if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
802 err(EXIT_FAILURE, "SIOCIFCREATE");
803 }
804
805 /*ARGSUSED*/
806 void
807 clone_destroy(const char *addr, int param)
808 {
809
810 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
811 if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
812 err(EXIT_FAILURE, "SIOCIFDESTROY");
813 }
814
815 /*ARGSUSED*/
816 void
817 setifaddr(const char *addr, int param)
818 {
819 struct ifreq *siifr; /* XXX */
820
821 /*
822 * Delay the ioctl to set the interface addr until flags are all set.
823 * The address interpretation may depend on the flags,
824 * and the flags may change when the address is set.
825 */
826 setaddr++;
827 if (newaddr == -1)
828 newaddr = 1;
829 if (doalias == 0 && afp->af_gifaddr != 0) {
830 siifr = (struct ifreq *)afp->af_ridreq;
831 (void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
832 siifr->ifr_addr.sa_family = afp->af_af;
833 if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
834 clearaddr = 1;
835 else if (errno == EADDRNOTAVAIL)
836 /* No address was assigned yet. */
837 ;
838 else
839 err(EXIT_FAILURE, "SIOCGIFADDR");
840 }
841
842 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
843 }
844
845 void
846 setifnetmask(const char *addr, int d)
847 {
848 (*afp->af_getaddr)(addr, MASK);
849 }
850
851 void
852 setifbroadaddr(const char *addr, int d)
853 {
854 (*afp->af_getaddr)(addr, DSTADDR);
855 }
856
857 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
858 /*ARGSUSED*/
859 void
860 notealias(const char *addr, int param)
861 {
862 if (setaddr && doalias == 0 && param < 0)
863 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
864 rqtosa(af_addreq)->sa_len);
865 doalias = param;
866 if (param < 0) {
867 clearaddr = 1;
868 newaddr = 0;
869 conflicting++;
870 } else {
871 clearaddr = 0;
872 conflicting++;
873 }
874 }
875
876 /*ARGSUSED*/
877 void
878 notrailers(const char *vname, int value)
879 {
880 puts("Note: trailers are no longer sent, but always received");
881 }
882
883 /*ARGSUSED*/
884 void
885 setifdstaddr(const char *addr, int param)
886 {
887 (*afp->af_getaddr)(addr, DSTADDR);
888 }
889
890 void
891 setifflags(const char *vname, int value)
892 {
893 struct ifreq ifreq;
894
895 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
896 if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
897 err(EXIT_FAILURE, "SIOCGIFFLAGS");
898 flags = ifreq.ifr_flags;
899
900 if (value < 0) {
901 value = -value;
902 flags &= ~value;
903 } else
904 flags |= value;
905 ifreq.ifr_flags = flags;
906 if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
907 err(EXIT_FAILURE, "SIOCSIFFLAGS");
908 }
909
910 void
911 setifcaps(const char *vname, int value)
912 {
913
914 if (value < 0) {
915 value = -value;
916 g_ifcr.ifcr_capenable &= ~value;
917 } else
918 g_ifcr.ifcr_capenable |= value;
919
920 g_ifcr_updated = 1;
921 }
922
923 void
924 setifmetric(const char *val, int d)
925 {
926 char *ep = NULL;
927
928 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
929 ifr.ifr_metric = strtoul(val, &ep, 10);
930 if (!ep || *ep)
931 errx(EXIT_FAILURE, "%s: invalid metric", val);
932 if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
933 warn("SIOCSIFMETRIC");
934 }
935
936 void
937 setifmtu(const char *val, int d)
938 {
939 char *ep = NULL;
940
941 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
942 ifr.ifr_mtu = strtoul(val, &ep, 10);
943 if (!ep || *ep)
944 errx(EXIT_FAILURE, "%s: invalid mtu", val);
945 if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
946 warn("SIOCSIFMTU");
947 }
948
949 const char *
950 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
951 {
952 int len;
953 int hexstr;
954 u_int8_t *p;
955
956 len = *lenp;
957 p = buf;
958 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
959 if (hexstr)
960 val += 2;
961 for (;;) {
962 if (*val == '\0')
963 break;
964 if (sep != NULL && strchr(sep, *val) != NULL) {
965 val++;
966 break;
967 }
968 if (hexstr) {
969 if (!isxdigit((u_char)val[0]) ||
970 !isxdigit((u_char)val[1])) {
971 warnx("bad hexadecimal digits");
972 return NULL;
973 }
974 }
975 if (p > buf + len) {
976 if (hexstr)
977 warnx("hexadecimal digits too long");
978 else
979 warnx("strings too long");
980 return NULL;
981 }
982 if (hexstr) {
983 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
984 *p++ = (tohex((u_char)val[0]) << 4) |
985 tohex((u_char)val[1]);
986 #undef tohex
987 val += 2;
988 } else
989 *p++ = *val++;
990 }
991 len = p - buf;
992 if (len < *lenp)
993 memset(p, 0, *lenp - len);
994 *lenp = len;
995 return val;
996 }
997
998 void
999 print_string(const u_int8_t *buf, int len)
1000 {
1001 int i;
1002 int hasspc;
1003
1004 i = 0;
1005 hasspc = 0;
1006 if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
1007 for (; i < len; i++) {
1008 if (!isprint(buf[i]))
1009 break;
1010 if (isspace(buf[i]))
1011 hasspc++;
1012 }
1013 }
1014 if (i == len) {
1015 if (hasspc || len == 0)
1016 printf("\"%.*s\"", len, buf);
1017 else
1018 printf("%.*s", len, buf);
1019 } else {
1020 printf("0x");
1021 for (i = 0; i < len; i++)
1022 printf("%02x", buf[i]);
1023 }
1024 }
1025
1026 static void
1027 media_error(int type, const char *val, const char *opt)
1028 {
1029 errx(EXIT_FAILURE, "unknown %s media %s: %s",
1030 get_media_type_string(type), opt, val);
1031 }
1032
1033 void
1034 init_current_media(void)
1035 {
1036 struct ifmediareq ifmr;
1037
1038 /*
1039 * If we have not yet done so, grab the currently-selected
1040 * media.
1041 */
1042 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1043 (void) memset(&ifmr, 0, sizeof(ifmr));
1044 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1045
1046 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1047 /*
1048 * If we get E2BIG, the kernel is telling us
1049 * that there are more, so we can ignore it.
1050 */
1051 if (errno != E2BIG)
1052 err(EXIT_FAILURE, "SGIOCGIFMEDIA");
1053 }
1054
1055 media_current = ifmr.ifm_current;
1056 }
1057
1058 /* Sanity. */
1059 if (IFM_TYPE(media_current) == 0)
1060 errx(EXIT_FAILURE, "%s: no link type?", name);
1061 }
1062
1063 void
1064 process_media_commands(void)
1065 {
1066
1067 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1068 /* Nothing to do. */
1069 return;
1070 }
1071
1072 /*
1073 * Media already set up, and commands sanity-checked. Set/clear
1074 * any options, and we're ready to go.
1075 */
1076 media_current |= mediaopt_set;
1077 media_current &= ~mediaopt_clear;
1078
1079 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1080 ifr.ifr_media = media_current;
1081
1082 if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
1083 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1084 }
1085
1086 void
1087 setmedia(const char *val, int d)
1088 {
1089 int type, subtype, inst;
1090
1091 init_current_media();
1092
1093 /* Only one media command may be given. */
1094 if (actions & A_MEDIA)
1095 errx(EXIT_FAILURE, "only one `media' command may be issued");
1096
1097 /* Must not come after mode commands */
1098 if (actions & A_MEDIAMODE)
1099 errx(EXIT_FAILURE,
1100 "may not issue `media' after `mode' commands");
1101
1102 /* Must not come after mediaopt commands */
1103 if (actions & A_MEDIAOPT)
1104 errx(EXIT_FAILURE,
1105 "may not issue `media' after `mediaopt' commands");
1106
1107 /*
1108 * No need to check if `instance' has been issued; setmediainst()
1109 * craps out if `media' has not been specified.
1110 */
1111
1112 type = IFM_TYPE(media_current);
1113 inst = IFM_INST(media_current);
1114
1115 /* Look up the subtype. */
1116 subtype = get_media_subtype(type, val);
1117 if (subtype == -1)
1118 media_error(type, val, "subtype");
1119
1120 /* Build the new current media word. */
1121 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1122
1123 /* Media will be set after other processing is complete. */
1124 }
1125
1126 void
1127 setmediaopt(const char *val, int d)
1128 {
1129 char *invalid;
1130
1131 init_current_media();
1132
1133 /* Can only issue `mediaopt' once. */
1134 if (actions & A_MEDIAOPTSET)
1135 errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
1136
1137 /* Can't issue `mediaopt' if `instance' has already been issued. */
1138 if (actions & A_MEDIAINST)
1139 errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
1140
1141 mediaopt_set = get_media_options(media_current, val, &invalid);
1142 if (mediaopt_set == -1)
1143 media_error(media_current, invalid, "option");
1144
1145 /* Media will be set after other processing is complete. */
1146 }
1147
1148 void
1149 unsetmediaopt(const char *val, int d)
1150 {
1151 char *invalid;
1152
1153 init_current_media();
1154
1155 /* Can only issue `-mediaopt' once. */
1156 if (actions & A_MEDIAOPTCLR)
1157 errx(EXIT_FAILURE,
1158 "only one `-mediaopt' command may be issued");
1159
1160 /* May not issue `media' and `-mediaopt'. */
1161 if (actions & A_MEDIA)
1162 errx(EXIT_FAILURE,
1163 "may not issue both `media' and `-mediaopt'");
1164
1165 /*
1166 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1167 * implicitly checks for A_MEDIAINST.
1168 */
1169
1170 mediaopt_clear = get_media_options(media_current, val, &invalid);
1171 if (mediaopt_clear == -1)
1172 media_error(media_current, invalid, "option");
1173
1174 /* Media will be set after other processing is complete. */
1175 }
1176
1177 void
1178 setmediainst(const char *val, int d)
1179 {
1180 int type, subtype, options, inst;
1181
1182 init_current_media();
1183
1184 /* Can only issue `instance' once. */
1185 if (actions & A_MEDIAINST)
1186 errx(EXIT_FAILURE, "only one `instance' command may be issued");
1187
1188 /* Must have already specified `media' */
1189 if ((actions & A_MEDIA) == 0)
1190 errx(EXIT_FAILURE, "must specify `media' before `instance'");
1191
1192 type = IFM_TYPE(media_current);
1193 subtype = IFM_SUBTYPE(media_current);
1194 options = IFM_OPTIONS(media_current);
1195
1196 inst = atoi(val);
1197 if (inst < 0 || inst > IFM_INST_MAX)
1198 errx(EXIT_FAILURE, "invalid media instance: %s", val);
1199
1200 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1201
1202 /* Media will be set after other processing is complete. */
1203 }
1204
1205 void
1206 setmediamode(const char *val, int d)
1207 {
1208 int type, subtype, options, inst, mode;
1209
1210 init_current_media();
1211
1212 /* Can only issue `mode' once. */
1213 if (actions & A_MEDIAMODE)
1214 errx(EXIT_FAILURE, "only one `mode' command may be issued");
1215
1216 type = IFM_TYPE(media_current);
1217 subtype = IFM_SUBTYPE(media_current);
1218 options = IFM_OPTIONS(media_current);
1219 inst = IFM_INST(media_current);
1220
1221 mode = get_media_mode(type, val);
1222 if (mode == -1)
1223 media_error(type, val, "mode");
1224
1225 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1226
1227 /* Media will be set after other processing is complete. */
1228 }
1229
1230 void
1231 print_media_word(int ifmw, const char *opt_sep)
1232 {
1233 const char *str;
1234
1235 printf("%s", get_media_subtype_string(ifmw));
1236
1237 /* Find mode. */
1238 if (IFM_MODE(ifmw) != 0) {
1239 str = get_media_mode_string(ifmw);
1240 if (str != NULL)
1241 printf(" mode %s", str);
1242 }
1243
1244 /* Find options. */
1245 for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1246 printf("%s%s", opt_sep, str);
1247
1248 if (IFM_INST(ifmw) != 0)
1249 printf(" instance %d", IFM_INST(ifmw));
1250 }
1251
1252 int
1253 carrier(void)
1254 {
1255 struct ifmediareq ifmr;
1256
1257 (void) memset(&ifmr, 0, sizeof(ifmr));
1258 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1259
1260 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1261 /*
1262 * Interface doesn't support SIOC{G,S}IFMEDIA;
1263 * assume ok.
1264 */
1265 return 0;
1266 }
1267 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1268 /*
1269 * Interface doesn't report media-valid status.
1270 * assume ok.
1271 */
1272 return 0;
1273 }
1274 /* otherwise, return ok for active, not-ok if not active. */
1275 return !(ifmr.ifm_status & IFM_ACTIVE);
1276 }
1277
1278
1279 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1280
1281 const struct ifmedia_status_description ifm_status_descriptions[] =
1282 IFM_STATUS_DESCRIPTIONS;
1283
1284 /*
1285 * Print the status of the interface. If an address family was
1286 * specified, show it and it only; otherwise, show them all.
1287 */
1288 void
1289 status(const struct sockaddr_dl *sdl)
1290 {
1291 const struct afswtch *p = afp;
1292 struct ifmediareq ifmr;
1293 struct ifdatareq ifdr;
1294 int *media_list, i;
1295 char hbuf[NI_MAXHOST];
1296 char fbuf[BUFSIZ];
1297
1298 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1299 printf("%s: flags=%s", name, &fbuf[2]);
1300 if (metric)
1301 printf(" metric %lu", metric);
1302 if (mtu)
1303 printf(" mtu %lu", mtu);
1304 printf("\n");
1305
1306 if (g_ifcr.ifcr_capabilities) {
1307 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1308 g_ifcr.ifcr_capabilities);
1309 printf("\tcapabilities=%s\n", &fbuf[2]);
1310 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1311 g_ifcr.ifcr_capenable);
1312 printf("\tenabled=%s\n", &fbuf[2]);
1313 }
1314
1315 ieee80211_status();
1316 vlan_status();
1317 tunnel_status();
1318 agr_status();
1319
1320 if (sdl != NULL &&
1321 getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1322 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1323 hbuf[0] != '\0')
1324 printf("\taddress: %s\n", hbuf);
1325
1326 (void) memset(&ifmr, 0, sizeof(ifmr));
1327 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1328
1329 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1330 /*
1331 * Interface doesn't support SIOC{G,S}IFMEDIA.
1332 */
1333 goto iface_stats;
1334 }
1335
1336 if (ifmr.ifm_count == 0) {
1337 warnx("%s: no media types?", name);
1338 goto iface_stats;
1339 }
1340
1341 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1342 if (media_list == NULL)
1343 err(EXIT_FAILURE, "malloc");
1344 ifmr.ifm_ulist = media_list;
1345
1346 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1347 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1348
1349 printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1350 print_media_word(ifmr.ifm_current, " ");
1351 if (ifmr.ifm_active != ifmr.ifm_current) {
1352 printf(" (");
1353 print_media_word(ifmr.ifm_active, " ");
1354 printf(")");
1355 }
1356 printf("\n");
1357
1358 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1359 const struct ifmedia_status_description *ifms;
1360 int bitno, found = 0;
1361
1362 printf("\tstatus: ");
1363 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1364 for (ifms = ifm_status_descriptions;
1365 ifms->ifms_valid != 0; ifms++) {
1366 if (ifms->ifms_type !=
1367 IFM_TYPE(ifmr.ifm_current) ||
1368 ifms->ifms_valid !=
1369 ifm_status_valid_list[bitno])
1370 continue;
1371 printf("%s%s", found ? ", " : "",
1372 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1373 found = 1;
1374
1375 /*
1376 * For each valid indicator bit, there's
1377 * only one entry for each media type, so
1378 * terminate the inner loop now.
1379 */
1380 break;
1381 }
1382 }
1383
1384 if (found == 0)
1385 printf("unknown");
1386 printf("\n");
1387 }
1388
1389 if (mflag) {
1390 int type, printed_type;
1391
1392 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1393 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1394 if (IFM_TYPE(media_list[i]) != type)
1395 continue;
1396 if (printed_type == 0) {
1397 printf("\tsupported %s media:\n",
1398 get_media_type_string(type));
1399 printed_type = 1;
1400 }
1401 printf("\t\tmedia ");
1402 print_media_word(media_list[i], " mediaopt ");
1403 printf("\n");
1404 }
1405 }
1406 }
1407
1408 free(media_list);
1409
1410 iface_stats:
1411 if (!vflag && !zflag)
1412 goto proto_status;
1413
1414 (void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
1415
1416 if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1417 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1418 } else {
1419 struct if_data * const ifi = &ifdr.ifdr_data;
1420 char buf[5];
1421
1422 #define PLURAL(n) ((n) == 1 ? "" : "s")
1423 #define PLURALSTR(s) ((atof(s)) == 1.0 ? "" : "s")
1424 printf("\tinput: %llu packet%s, ",
1425 (unsigned long long) ifi->ifi_ipackets,
1426 PLURAL(ifi->ifi_ipackets));
1427 if (hflag) {
1428 (void) humanize_number(buf, sizeof(buf),
1429 (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
1430 HN_NOSPACE | HN_DECIMAL);
1431 printf("%s byte%s", buf,
1432 PLURALSTR(buf));
1433 } else
1434 printf("%llu byte%s",
1435 (unsigned long long) ifi->ifi_ibytes,
1436 PLURAL(ifi->ifi_ibytes));
1437 if (ifi->ifi_imcasts)
1438 printf(", %llu multicast%s",
1439 (unsigned long long) ifi->ifi_imcasts,
1440 PLURAL(ifi->ifi_imcasts));
1441 if (ifi->ifi_ierrors)
1442 printf(", %llu error%s",
1443 (unsigned long long) ifi->ifi_ierrors,
1444 PLURAL(ifi->ifi_ierrors));
1445 if (ifi->ifi_iqdrops)
1446 printf(", %llu queue drop%s",
1447 (unsigned long long) ifi->ifi_iqdrops,
1448 PLURAL(ifi->ifi_iqdrops));
1449 if (ifi->ifi_noproto)
1450 printf(", %llu unknown protocol",
1451 (unsigned long long) ifi->ifi_noproto);
1452 printf("\n\toutput: %llu packet%s, ",
1453 (unsigned long long) ifi->ifi_opackets,
1454 PLURAL(ifi->ifi_opackets));
1455 if (hflag) {
1456 (void) humanize_number(buf, sizeof(buf),
1457 (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
1458 HN_NOSPACE | HN_DECIMAL);
1459 printf("%s byte%s", buf,
1460 PLURALSTR(buf));
1461 } else
1462 printf("%llu byte%s",
1463 (unsigned long long) ifi->ifi_obytes,
1464 PLURAL(ifi->ifi_obytes));
1465 if (ifi->ifi_omcasts)
1466 printf(", %llu multicast%s",
1467 (unsigned long long) ifi->ifi_omcasts,
1468 PLURAL(ifi->ifi_omcasts));
1469 if (ifi->ifi_oerrors)
1470 printf(", %llu error%s",
1471 (unsigned long long) ifi->ifi_oerrors,
1472 PLURAL(ifi->ifi_oerrors));
1473 if (ifi->ifi_collisions)
1474 printf(", %llu collision%s",
1475 (unsigned long long) ifi->ifi_collisions,
1476 PLURAL(ifi->ifi_collisions));
1477 printf("\n");
1478 #undef PLURAL
1479 #undef PLURALSTR
1480 }
1481
1482 ieee80211_statistics();
1483
1484 proto_status:
1485 if ((p = afp) != NULL) {
1486 (*p->af_status)(1);
1487 } else for (p = afs; p->af_name; p++) {
1488 ifr.ifr_addr.sa_family = p->af_af;
1489 (*p->af_status)(0);
1490 }
1491 }
1492
1493 void
1494 setifprefixlen(const char *addr, int d)
1495 {
1496 if (*afp->af_getprefix)
1497 (*afp->af_getprefix)(addr, MASK);
1498 explicit_prefix = 1;
1499 }
1500
1501 void
1502 usage(void)
1503 {
1504 const char *progname = getprogname();
1505
1506 fprintf(stderr,
1507 "usage: %s [-h] [-m] [-v] [-z] "
1508 #ifdef INET6
1509 "[-L] "
1510 #endif
1511 "interface\n"
1512 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1513 "\t\t[ alias | -alias ] ]\n"
1514 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
1515 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
1516 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
1517 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
1518 "\t[ arp | -arp ]\n"
1519 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
1520 "[ instance minst ]\n"
1521 "\t[ vlan n vlanif i ]\n"
1522 "\t[ agrport i ] [ -agrport i ]\n"
1523 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
1524 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
1525 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1526 " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
1527 " %s -l [-b] [-d] [-u] [-s]\n"
1528 " %s -C\n"
1529 " %s interface create\n"
1530 " %s interface destroy\n",
1531 progname, progname, progname, progname, progname, progname);
1532 exit(1);
1533 }
1534