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