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