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