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