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