ifconfig.c revision 1.183 1 /* $NetBSD: ifconfig.c,v 1.183 2008/04/14 15:57:15 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.183 2008/04/14 15:57:15 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 #ifdef INET6
546 in6_init();
547 #endif
548
549 /* Process commands. */
550 for (; argc > 0; argc--, argv++) {
551 const struct cmd *p;
552
553 for (p = cmds; p->c_name != NULL; p++) {
554 if (strcmp(argv[0], p->c_name) == 0)
555 break;
556 }
557 if (p->c_name == NULL && setaddr) {
558 if ((flags & IFF_POINTOPOINT) == 0) {
559 errx(EXIT_FAILURE,
560 "can't set destination address %s",
561 "on non-point-to-point link");
562 }
563 p++; /* got src, do dst */
564 }
565 if (p->c_func == NULL)
566 continue;
567 if (p->c_parameter == NEXTARG) {
568 if (argc < 2)
569 errx(EXIT_FAILURE, "'%s' requires argument",
570 p->c_name);
571 (*p->c_func)(argv[1], 0);
572 argc--, argv++;
573 } else if (p->c_parameter == NEXTARG2) {
574 if (argc < 3)
575 errx(EXIT_FAILURE, "'%s' requires 2 arguments",
576 p->c_name);
577 ((void (*)(const char *, const char *))
578 *p->c_func)(argv[1], argv[2]);
579 argc -= 2, argv += 2;
580 } else
581 (*p->c_func)(argv[0], p->c_parameter);
582 actions |= p->c_action;
583 }
584
585 /*
586 * See if multiple alias, -alias, or delete commands were
587 * specified. More than one constitutes an invalid command line
588 */
589
590 if (conflicting > 1)
591 errx(EXIT_FAILURE,
592 "Only one use of alias, -alias or delete is valid.");
593
594 /* Process any media commands that may have been issued. */
595 process_media_commands();
596
597 if (af == AF_INET6 && explicit_prefix == 0) {
598 /*
599 * Aggregatable address architecture defines all prefixes
600 * are 64. So, it is convenient to set prefixlen to 64 if
601 * it is not specified.
602 */
603 setifprefixlen("64", 0);
604 /* in6_getprefix("64", MASK) if MASK is available here... */
605 }
606
607 #ifndef INET_ONLY
608 if (af == AF_ISO)
609 adjust_nsellength();
610
611 if (af == AF_APPLETALK)
612 checkatrange(&addreq.ifra_addr);
613 #endif /* INET_ONLY */
614
615 if (clearaddr) {
616 estrlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
617 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
618 err(EXIT_FAILURE, "SIOCDIFADDR");
619 }
620 if (newaddr > 0) {
621 estrlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
622 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
623 warn("SIOCAIFADDR");
624 else if (check_up_state < 0)
625 check_up_state = 1;
626 }
627
628 if (have_preference)
629 do_setifpreference();
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 setifpreference(const char *val, int d)
975 {
976 char *end = NULL;
977 if (setaddr <= 0) {
978 errx(EXIT_FAILURE,
979 "set address preference: first specify an address");
980 }
981 preference = strtoul(val, &end, 10);
982 if (end == NULL || *end != '\0' || preference > UINT16_MAX)
983 errx(EXIT_FAILURE, "invalid preference %s", val);
984 have_preference = 1;
985 }
986
987 void
988 do_setifpreference(void)
989 {
990 struct if_addrprefreq ifap;
991 (void)strncpy(ifap.ifap_name, name, sizeof(ifap.ifap_name));
992 ifap.ifap_preference = (uint16_t)preference;
993 (void)memcpy(&ifap.ifap_addr, rqtosa(af_addreq),
994 MIN(sizeof(ifap.ifap_addr), rqtosa(af_addreq)->sa_len));
995 if (ioctl(s, SIOCSIFADDRPREF, &ifap) == -1)
996 warn("SIOCSIFADDRPREF");
997 }
998
999 void
1000 setifmtu(const char *val, int d)
1001 {
1002 char *ep = NULL;
1003
1004 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1005 ifr.ifr_mtu = strtoul(val, &ep, 10);
1006 if (!ep || *ep)
1007 errx(EXIT_FAILURE, "%s: invalid mtu", val);
1008 if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
1009 warn("SIOCSIFMTU");
1010 }
1011
1012 const char *
1013 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
1014 {
1015 int len;
1016 int hexstr;
1017 u_int8_t *p;
1018
1019 len = *lenp;
1020 p = buf;
1021 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1022 if (hexstr)
1023 val += 2;
1024 for (;;) {
1025 if (*val == '\0')
1026 break;
1027 if (sep != NULL && strchr(sep, *val) != NULL) {
1028 val++;
1029 break;
1030 }
1031 if (hexstr) {
1032 if (!isxdigit((u_char)val[0]) ||
1033 !isxdigit((u_char)val[1])) {
1034 warnx("bad hexadecimal digits");
1035 return NULL;
1036 }
1037 }
1038 if (p > buf + len) {
1039 if (hexstr)
1040 warnx("hexadecimal digits too long");
1041 else
1042 warnx("strings too long");
1043 return NULL;
1044 }
1045 if (hexstr) {
1046 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1047 *p++ = (tohex((u_char)val[0]) << 4) |
1048 tohex((u_char)val[1]);
1049 #undef tohex
1050 val += 2;
1051 } else
1052 *p++ = *val++;
1053 }
1054 len = p - buf;
1055 if (len < *lenp)
1056 memset(p, 0, *lenp - len);
1057 *lenp = len;
1058 return val;
1059 }
1060
1061 void
1062 print_string(const u_int8_t *buf, int len)
1063 {
1064 int i;
1065 int hasspc;
1066
1067 i = 0;
1068 hasspc = 0;
1069 if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
1070 for (; i < len; i++) {
1071 if (!isprint(buf[i]))
1072 break;
1073 if (isspace(buf[i]))
1074 hasspc++;
1075 }
1076 }
1077 if (i == len) {
1078 if (hasspc || len == 0)
1079 printf("\"%.*s\"", len, buf);
1080 else
1081 printf("%.*s", len, buf);
1082 } else {
1083 printf("0x");
1084 for (i = 0; i < len; i++)
1085 printf("%02x", buf[i]);
1086 }
1087 }
1088
1089 static void
1090 media_error(int type, const char *val, const char *opt)
1091 {
1092 errx(EXIT_FAILURE, "unknown %s media %s: %s",
1093 get_media_type_string(type), opt, val);
1094 }
1095
1096 void
1097 init_current_media(void)
1098 {
1099 struct ifmediareq ifmr;
1100
1101 /*
1102 * If we have not yet done so, grab the currently-selected
1103 * media.
1104 */
1105 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1106 (void) memset(&ifmr, 0, sizeof(ifmr));
1107 estrlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1108
1109 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1110 /*
1111 * If we get E2BIG, the kernel is telling us
1112 * that there are more, so we can ignore it.
1113 */
1114 if (errno != E2BIG)
1115 err(EXIT_FAILURE, "SGIOCGIFMEDIA");
1116 }
1117
1118 media_current = ifmr.ifm_current;
1119 }
1120
1121 /* Sanity. */
1122 if (IFM_TYPE(media_current) == 0)
1123 errx(EXIT_FAILURE, "%s: no link type?", name);
1124 }
1125
1126 void
1127 process_media_commands(void)
1128 {
1129
1130 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1131 /* Nothing to do. */
1132 return;
1133 }
1134
1135 /*
1136 * Media already set up, and commands sanity-checked. Set/clear
1137 * any options, and we're ready to go.
1138 */
1139 media_current |= mediaopt_set;
1140 media_current &= ~mediaopt_clear;
1141
1142 estrlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1143 ifr.ifr_media = media_current;
1144
1145 if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
1146 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1147 }
1148
1149 void
1150 setmedia(const char *val, int d)
1151 {
1152 int type, subtype, inst;
1153
1154 init_current_media();
1155
1156 /* Only one media command may be given. */
1157 if (actions & A_MEDIA)
1158 errx(EXIT_FAILURE, "only one `media' command may be issued");
1159
1160 /* Must not come after mode commands */
1161 if (actions & A_MEDIAMODE)
1162 errx(EXIT_FAILURE,
1163 "may not issue `media' after `mode' commands");
1164
1165 /* Must not come after mediaopt commands */
1166 if (actions & A_MEDIAOPT)
1167 errx(EXIT_FAILURE,
1168 "may not issue `media' after `mediaopt' commands");
1169
1170 /*
1171 * No need to check if `instance' has been issued; setmediainst()
1172 * craps out if `media' has not been specified.
1173 */
1174
1175 type = IFM_TYPE(media_current);
1176 inst = IFM_INST(media_current);
1177
1178 /* Look up the subtype. */
1179 subtype = get_media_subtype(type, val);
1180 if (subtype == -1)
1181 media_error(type, val, "subtype");
1182
1183 /* Build the new current media word. */
1184 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1185
1186 /* Media will be set after other processing is complete. */
1187 }
1188
1189 void
1190 setmediaopt(const char *val, int d)
1191 {
1192 char *invalid;
1193
1194 init_current_media();
1195
1196 /* Can only issue `mediaopt' once. */
1197 if (actions & A_MEDIAOPTSET)
1198 errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
1199
1200 /* Can't issue `mediaopt' if `instance' has already been issued. */
1201 if (actions & A_MEDIAINST)
1202 errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
1203
1204 mediaopt_set = get_media_options(media_current, val, &invalid);
1205 if (mediaopt_set == -1)
1206 media_error(media_current, invalid, "option");
1207
1208 /* Media will be set after other processing is complete. */
1209 }
1210
1211 void
1212 unsetmediaopt(const char *val, int d)
1213 {
1214 char *invalid;
1215
1216 init_current_media();
1217
1218 /* Can only issue `-mediaopt' once. */
1219 if (actions & A_MEDIAOPTCLR)
1220 errx(EXIT_FAILURE,
1221 "only one `-mediaopt' command may be issued");
1222
1223 /* May not issue `media' and `-mediaopt'. */
1224 if (actions & A_MEDIA)
1225 errx(EXIT_FAILURE,
1226 "may not issue both `media' and `-mediaopt'");
1227
1228 /*
1229 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1230 * implicitly checks for A_MEDIAINST.
1231 */
1232
1233 mediaopt_clear = get_media_options(media_current, val, &invalid);
1234 if (mediaopt_clear == -1)
1235 media_error(media_current, invalid, "option");
1236
1237 /* Media will be set after other processing is complete. */
1238 }
1239
1240 void
1241 setmediainst(const char *val, int d)
1242 {
1243 int type, subtype, options, inst;
1244
1245 init_current_media();
1246
1247 /* Can only issue `instance' once. */
1248 if (actions & A_MEDIAINST)
1249 errx(EXIT_FAILURE, "only one `instance' command may be issued");
1250
1251 /* Must have already specified `media' */
1252 if ((actions & A_MEDIA) == 0)
1253 errx(EXIT_FAILURE, "must specify `media' before `instance'");
1254
1255 type = IFM_TYPE(media_current);
1256 subtype = IFM_SUBTYPE(media_current);
1257 options = IFM_OPTIONS(media_current);
1258
1259 inst = atoi(val);
1260 if (inst < 0 || inst > IFM_INST_MAX)
1261 errx(EXIT_FAILURE, "invalid media instance: %s", val);
1262
1263 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1264
1265 /* Media will be set after other processing is complete. */
1266 }
1267
1268 void
1269 setmediamode(const char *val, int d)
1270 {
1271 int type, subtype, options, inst, mode;
1272
1273 init_current_media();
1274
1275 /* Can only issue `mode' once. */
1276 if (actions & A_MEDIAMODE)
1277 errx(EXIT_FAILURE, "only one `mode' command may be issued");
1278
1279 type = IFM_TYPE(media_current);
1280 subtype = IFM_SUBTYPE(media_current);
1281 options = IFM_OPTIONS(media_current);
1282 inst = IFM_INST(media_current);
1283
1284 mode = get_media_mode(type, val);
1285 if (mode == -1)
1286 media_error(type, val, "mode");
1287
1288 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1289
1290 /* Media will be set after other processing is complete. */
1291 }
1292
1293 void
1294 print_media_word(int ifmw, const char *opt_sep)
1295 {
1296 const char *str;
1297
1298 printf("%s", get_media_subtype_string(ifmw));
1299
1300 /* Find mode. */
1301 if (IFM_MODE(ifmw) != 0) {
1302 str = get_media_mode_string(ifmw);
1303 if (str != NULL)
1304 printf(" mode %s", str);
1305 }
1306
1307 /* Find options. */
1308 for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1309 printf("%s%s", opt_sep, str);
1310
1311 if (IFM_INST(ifmw) != 0)
1312 printf(" instance %d", IFM_INST(ifmw));
1313 }
1314
1315 int
1316 carrier(void)
1317 {
1318 struct ifmediareq ifmr;
1319
1320 (void) memset(&ifmr, 0, sizeof(ifmr));
1321 estrlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1322
1323 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1324 /*
1325 * Interface doesn't support SIOC{G,S}IFMEDIA;
1326 * assume ok.
1327 */
1328 return 0;
1329 }
1330 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1331 /*
1332 * Interface doesn't report media-valid status.
1333 * assume ok.
1334 */
1335 return 0;
1336 }
1337 /* otherwise, return ok for active, not-ok if not active. */
1338 return !(ifmr.ifm_status & IFM_ACTIVE);
1339 }
1340
1341
1342 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1343
1344 const struct ifmedia_status_description ifm_status_descriptions[] =
1345 IFM_STATUS_DESCRIPTIONS;
1346
1347 /*
1348 * Print the status of the interface. If an address family was
1349 * specified, show it and it only; otherwise, show them all.
1350 */
1351 void
1352 status(const struct sockaddr_dl *sdl)
1353 {
1354 const struct afswtch *p = afp;
1355 struct ifmediareq ifmr;
1356 struct ifdatareq ifdr;
1357 int *media_list, i;
1358 char hbuf[NI_MAXHOST];
1359 char fbuf[BUFSIZ];
1360
1361 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1362 printf("%s: flags=%s", name, &fbuf[2]);
1363 if (metric)
1364 printf(" metric %lu", metric);
1365 if (mtu)
1366 printf(" mtu %lu", mtu);
1367 printf("\n");
1368
1369 if (g_ifcr.ifcr_capabilities) {
1370 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1371 g_ifcr.ifcr_capabilities);
1372 printf("\tcapabilities=%s\n", &fbuf[2]);
1373 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1374 g_ifcr.ifcr_capenable);
1375 printf("\tenabled=%s\n", &fbuf[2]);
1376 }
1377
1378 ieee80211_status();
1379 vlan_status();
1380 #ifndef INET_ONLY
1381 carp_status();
1382 #endif
1383 tunnel_status();
1384 agr_status();
1385
1386 if (sdl != NULL &&
1387 getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1388 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1389 hbuf[0] != '\0')
1390 printf("\taddress: %s\n", hbuf);
1391
1392 (void) memset(&ifmr, 0, sizeof(ifmr));
1393 estrlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1394
1395 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1396 /*
1397 * Interface doesn't support SIOC{G,S}IFMEDIA.
1398 */
1399 goto iface_stats;
1400 }
1401
1402 if (ifmr.ifm_count == 0) {
1403 warnx("%s: no media types?", name);
1404 goto iface_stats;
1405 }
1406
1407 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1408 if (media_list == NULL)
1409 err(EXIT_FAILURE, "malloc");
1410 ifmr.ifm_ulist = media_list;
1411
1412 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1413 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1414
1415 printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1416 print_media_word(ifmr.ifm_current, " ");
1417 if (ifmr.ifm_active != ifmr.ifm_current) {
1418 printf(" (");
1419 print_media_word(ifmr.ifm_active, " ");
1420 printf(")");
1421 }
1422 printf("\n");
1423
1424 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1425 const struct ifmedia_status_description *ifms;
1426 int bitno, found = 0;
1427
1428 printf("\tstatus: ");
1429 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1430 for (ifms = ifm_status_descriptions;
1431 ifms->ifms_valid != 0; ifms++) {
1432 if (ifms->ifms_type !=
1433 IFM_TYPE(ifmr.ifm_current) ||
1434 ifms->ifms_valid !=
1435 ifm_status_valid_list[bitno])
1436 continue;
1437 printf("%s%s", found ? ", " : "",
1438 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1439 found = 1;
1440
1441 /*
1442 * For each valid indicator bit, there's
1443 * only one entry for each media type, so
1444 * terminate the inner loop now.
1445 */
1446 break;
1447 }
1448 }
1449
1450 if (found == 0)
1451 printf("unknown");
1452 printf("\n");
1453 }
1454
1455 if (mflag) {
1456 int type, printed_type;
1457
1458 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1459 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1460 if (IFM_TYPE(media_list[i]) != type)
1461 continue;
1462 if (printed_type == 0) {
1463 printf("\tsupported %s media:\n",
1464 get_media_type_string(type));
1465 printed_type = 1;
1466 }
1467 printf("\t\tmedia ");
1468 print_media_word(media_list[i], " mediaopt ");
1469 printf("\n");
1470 }
1471 }
1472 }
1473
1474 free(media_list);
1475
1476 iface_stats:
1477 if (!vflag && !zflag)
1478 goto proto_status;
1479
1480 estrlcpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
1481
1482 if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1483 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1484 } else {
1485 struct if_data * const ifi = &ifdr.ifdr_data;
1486 char buf[5];
1487
1488 #define PLURAL(n) ((n) == 1 ? "" : "s")
1489 #define PLURALSTR(s) ((atof(s)) == 1.0 ? "" : "s")
1490 printf("\tinput: %llu packet%s, ",
1491 (unsigned long long) ifi->ifi_ipackets,
1492 PLURAL(ifi->ifi_ipackets));
1493 if (hflag) {
1494 (void) humanize_number(buf, sizeof(buf),
1495 (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
1496 HN_NOSPACE | HN_DECIMAL);
1497 printf("%s byte%s", buf,
1498 PLURALSTR(buf));
1499 } else
1500 printf("%llu byte%s",
1501 (unsigned long long) ifi->ifi_ibytes,
1502 PLURAL(ifi->ifi_ibytes));
1503 if (ifi->ifi_imcasts)
1504 printf(", %llu multicast%s",
1505 (unsigned long long) ifi->ifi_imcasts,
1506 PLURAL(ifi->ifi_imcasts));
1507 if (ifi->ifi_ierrors)
1508 printf(", %llu error%s",
1509 (unsigned long long) ifi->ifi_ierrors,
1510 PLURAL(ifi->ifi_ierrors));
1511 if (ifi->ifi_iqdrops)
1512 printf(", %llu queue drop%s",
1513 (unsigned long long) ifi->ifi_iqdrops,
1514 PLURAL(ifi->ifi_iqdrops));
1515 if (ifi->ifi_noproto)
1516 printf(", %llu unknown protocol",
1517 (unsigned long long) ifi->ifi_noproto);
1518 printf("\n\toutput: %llu packet%s, ",
1519 (unsigned long long) ifi->ifi_opackets,
1520 PLURAL(ifi->ifi_opackets));
1521 if (hflag) {
1522 (void) humanize_number(buf, sizeof(buf),
1523 (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
1524 HN_NOSPACE | HN_DECIMAL);
1525 printf("%s byte%s", buf,
1526 PLURALSTR(buf));
1527 } else
1528 printf("%llu byte%s",
1529 (unsigned long long) ifi->ifi_obytes,
1530 PLURAL(ifi->ifi_obytes));
1531 if (ifi->ifi_omcasts)
1532 printf(", %llu multicast%s",
1533 (unsigned long long) ifi->ifi_omcasts,
1534 PLURAL(ifi->ifi_omcasts));
1535 if (ifi->ifi_oerrors)
1536 printf(", %llu error%s",
1537 (unsigned long long) ifi->ifi_oerrors,
1538 PLURAL(ifi->ifi_oerrors));
1539 if (ifi->ifi_collisions)
1540 printf(", %llu collision%s",
1541 (unsigned long long) ifi->ifi_collisions,
1542 PLURAL(ifi->ifi_collisions));
1543 printf("\n");
1544 #undef PLURAL
1545 #undef PLURALSTR
1546 }
1547
1548 ieee80211_statistics();
1549
1550 proto_status:
1551 if ((p = afp) != NULL) {
1552 (*p->af_status)(1);
1553 } else for (p = afs; p->af_name; p++) {
1554 ifr.ifr_addr.sa_family = p->af_af;
1555 (*p->af_status)(0);
1556 }
1557 }
1558
1559 void
1560 setifprefixlen(const char *addr, int d)
1561 {
1562 if (*afp->af_getprefix)
1563 (*afp->af_getprefix)(addr, MASK);
1564 explicit_prefix = 1;
1565 }
1566
1567 void
1568 usage(void)
1569 {
1570 const char *progname = getprogname();
1571
1572 fprintf(stderr,
1573 "usage: %s [-h] [-m] [-v] [-z] "
1574 #ifdef INET6
1575 "[-L] "
1576 #endif
1577 "interface\n"
1578 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1579 "\t\t[ alias | -alias ] ]\n"
1580 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
1581 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
1582 "\t[ list scan ]\n"
1583 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
1584 "\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
1585 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
1586 "\t[ arp | -arp ]\n"
1587 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
1588 "[ instance minst ]\n"
1589 "\t[ preference n ]\n"
1590 "\t[ vlan n vlanif i ]\n"
1591 "\t[ agrport i ] [ -agrport i ]\n"
1592 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
1593 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
1594 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1595 " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
1596 " %s -l [-b] [-d] [-u] [-s]\n"
1597 " %s -C\n"
1598 " %s interface create\n"
1599 " %s interface destroy\n",
1600 progname, progname, progname, progname, progname, progname);
1601 exit(1);
1602 }
1603