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