ifconfig.c revision 1.162 1 /* $NetBSD: ifconfig.c,v 1.162 2005/03/19 23:46:03 thorpej 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.162 2005/03/19 23:46:03 thorpej 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>
92 #include <netinet/in_var.h>
93 #ifdef INET6
94 #include <netinet6/nd6.h>
95 #endif
96 #include <arpa/inet.h>
97
98 #include <netdb.h>
99
100 #define EON
101 #include <netiso/iso.h>
102 #include <netiso/iso_var.h>
103 #include <sys/protosw.h>
104
105 #include <ctype.h>
106 #include <err.h>
107 #include <errno.h>
108 #include <stddef.h>
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 #include <unistd.h>
113 #include <ifaddrs.h>
114 #include <util.h>
115
116 #include "extern.h"
117
118 #ifndef INET_ONLY
119 #include "af_atalk.h"
120 #include "af_ns.h"
121 #endif /* ! INET_ONLY */
122
123 #include "agr.h"
124 #include "ieee80211.h"
125 #include "tunnel.h"
126 #include "vlan.h"
127
128 struct ifreq ifr, ridreq;
129 struct ifaliasreq addreq __attribute__((aligned(4)));
130 struct in_aliasreq in_addreq;
131 #ifdef INET6
132 struct in6_ifreq ifr6;
133 struct in6_ifreq in6_ridreq;
134 struct in6_aliasreq in6_addreq;
135 #endif
136 struct iso_ifreq iso_ridreq;
137 struct iso_aliasreq iso_addreq;
138 struct sockaddr_in netmask;
139
140 char name[30];
141 u_short flags;
142 int setaddr, setipdst, doalias;
143 u_long metric, mtu;
144 int clearaddr, s;
145 int newaddr = -1;
146 int conflicting = 0;
147 int nsellength = 1;
148 int af;
149 int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag;
150 #ifdef INET6
151 int Lflag;
152 #endif
153 int explicit_prefix = 0;
154
155 struct ifcapreq g_ifcr;
156 int g_ifcr_updated;
157
158 void notealias(const char *, int);
159 void notrailers(const char *, int);
160 void setifaddr(const char *, int);
161 void setifdstaddr(const char *, int);
162 void setifflags(const char *, int);
163 void setifcaps(const char *, int);
164 void setifbroadaddr(const char *, int);
165 void setifipdst(const char *, int);
166 void setifmetric(const char *, int);
167 void setifmtu(const char *, int);
168 void setifnetmask(const char *, int);
169 void setifprefixlen(const char *, int);
170 void setnsellength(const char *, int);
171 void setsnpaoffset(const char *, int);
172 #ifdef INET6
173 void setia6flags(const char *, int);
174 void setia6pltime(const char *, int);
175 void setia6vltime(const char *, int);
176 void setia6lifetime(const char *, const char *);
177 void setia6eui64(const char *, int);
178 #endif
179 void setmedia(const char *, int);
180 void setmediamode(const char *, int);
181 void setmediaopt(const char *, int);
182 void unsetmediaopt(const char *, int);
183 void setmediainst(const char *, int);
184 void clone_create(const char *, int);
185 void clone_destroy(const char *, int);
186 void fixnsel(struct sockaddr_iso *);
187 int main(int, char *[]);
188
189 /*
190 * Media stuff. Whenever a media command is first performed, the
191 * currently select media is grabbed for this interface. If `media'
192 * is given, the current media word is modifed. `mediaopt' commands
193 * only modify the set and clear words. They then operate on the
194 * current media word later.
195 */
196 int media_current;
197 int mediaopt_set;
198 int mediaopt_clear;
199
200 int actions; /* Actions performed */
201
202 #define A_MEDIA 0x0001 /* media command */
203 #define A_MEDIAOPTSET 0x0002 /* mediaopt command */
204 #define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
205 #define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
206 #define A_MEDIAINST 0x0008 /* instance or inst command */
207 #define A_MEDIAMODE 0x0010 /* mode command */
208
209 #define NEXTARG 0xffffff
210 #define NEXTARG2 0xfffffe
211
212 const struct cmd {
213 const char *c_name;
214 int c_parameter; /* NEXTARG means next argv */
215 int c_action; /* defered action */
216 void (*c_func)(const char *, int);
217 void (*c_func2)(const char *, const char *);
218 } cmds[] = {
219 { "up", IFF_UP, 0, setifflags } ,
220 { "down", -IFF_UP, 0, setifflags },
221 { "trailers", -1, 0, notrailers },
222 { "-trailers", 1, 0, notrailers },
223 { "arp", -IFF_NOARP, 0, setifflags },
224 { "-arp", IFF_NOARP, 0, setifflags },
225 { "debug", IFF_DEBUG, 0, setifflags },
226 { "-debug", -IFF_DEBUG, 0, setifflags },
227 { "alias", IFF_UP, 0, notealias },
228 { "-alias", -IFF_UP, 0, notealias },
229 { "delete", -IFF_UP, 0, notealias },
230 #ifdef notdef
231 #define EN_SWABIPS 0x1000
232 { "swabips", EN_SWABIPS, 0, setifflags },
233 { "-swabips", -EN_SWABIPS, 0, setifflags },
234 #endif
235 { "netmask", NEXTARG, 0, setifnetmask },
236 { "metric", NEXTARG, 0, setifmetric },
237 { "mtu", NEXTARG, 0, setifmtu },
238 { "bssid", NEXTARG, 0, setifbssid },
239 { "-bssid", -1, 0, setifbssid },
240 { "chan", NEXTARG, 0, setifchan },
241 { "-chan", -1, 0, setifchan },
242 { "ssid", NEXTARG, 0, setifnwid },
243 { "nwid", NEXTARG, 0, setifnwid },
244 { "nwkey", NEXTARG, 0, setifnwkey },
245 { "-nwkey", -1, 0, setifnwkey },
246 { "powersave", 1, 0, setifpowersave },
247 { "-powersave", 0, 0, setifpowersave },
248 { "powersavesleep", NEXTARG, 0, setifpowersavesleep },
249 { "broadcast", NEXTARG, 0, setifbroadaddr },
250 { "ipdst", NEXTARG, 0, setifipdst },
251 { "prefixlen", NEXTARG, 0, setifprefixlen},
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, NULL,
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", IFCAP_CSUM_IPv4,0, setifcaps },
293 { "-ip4csum", -IFCAP_CSUM_IPv4,0, setifcaps },
294 { "tcp4csum", IFCAP_CSUM_TCPv4,0, setifcaps },
295 { "-tcp4csum", -IFCAP_CSUM_TCPv4,0, setifcaps },
296 { "udp4csum", IFCAP_CSUM_UDPv4,0, setifcaps },
297 { "-udp4csum", -IFCAP_CSUM_UDPv4,0, setifcaps },
298 { "tcp6csum", IFCAP_CSUM_TCPv6,0, setifcaps },
299 { "-tcp6csum", -IFCAP_CSUM_TCPv6,0, setifcaps },
300 { "udp6csum", IFCAP_CSUM_UDPv6,0, setifcaps },
301 { "-udp6csum", -IFCAP_CSUM_UDPv6,0, setifcaps },
302 { "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
303 { "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
304 { "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
305 { "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
306 { "tso4", IFCAP_TSOv4, 0, setifcaps },
307 { "-tso4", -IFCAP_TSOv4, 0, setifcaps },
308 { "agrport", NEXTARG, 0, agraddport } ,
309 { "-agrport", NEXTARG, 0, agrremport } ,
310 { 0, 0, 0, setifaddr },
311 { 0, 0, 0, setifdstaddr },
312 };
313
314 void adjust_nsellength(void);
315 int getinfo(struct ifreq *);
316 int carrier(void);
317 void printall(const char *);
318 void list_cloners(void);
319 int prefix(void *, int);
320 void status(const struct sockaddr_dl *);
321 void usage(void);
322 char *sec2str(time_t);
323
324 void print_media_word(int, const char *);
325 void process_media_commands(void);
326 void init_current_media(void);
327
328 /*
329 * XNS support liberally adapted from code written at the University of
330 * Maryland principally by James O'Toole and Chris Torek.
331 */
332 void in_alias(struct ifreq *);
333 void in_status(int);
334 void in_getaddr(const char *, int);
335 void in_getprefix(const char *, int);
336 #ifdef INET6
337 void in6_fillscopeid(struct sockaddr_in6 *sin6);
338 void in6_alias(struct in6_ifreq *);
339 void in6_status(int);
340 void in6_getaddr(const char *, int);
341 void in6_getprefix(const char *, int);
342 #endif
343 void iso_status(int);
344 void iso_getaddr(const char *, int);
345
346 /* Known address families */
347 const struct afswtch afs[] = {
348 { "inet", AF_INET, in_status, in_getaddr, in_getprefix,
349 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &in_addreq },
350 #ifdef INET6
351 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
352 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
353 /*
354 * Deleting the first address before setting new one is
355 * not prefered way in this protocol.
356 */
357 0,
358 &in6_ridreq, &in6_addreq },
359 #endif
360 #ifndef INET_ONLY /* small version, for boot media */
361 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
362 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &addreq, &addreq },
363 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
364 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, &ridreq, &addreq },
365 { "iso", AF_ISO, iso_status, iso_getaddr, NULL,
366 SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
367 &iso_ridreq, &iso_addreq },
368 #endif /* INET_ONLY */
369 { 0, 0, 0, 0 }
370 };
371
372 const struct afswtch *afp; /*the address family being set or asked about*/
373
374 int
375 main(int argc, char *argv[])
376 {
377 int ch;
378
379 /* Parse command-line options */
380 aflag = mflag = vflag = zflag = 0;
381 while ((ch = getopt(argc, argv, "AabCdlmsuvz"
382 #ifdef INET6
383 "L"
384 #endif
385 )) != -1) {
386 switch (ch) {
387 case 'A':
388 warnx("-A is deprecated");
389 break;
390
391 case 'a':
392 aflag = 1;
393 break;
394
395 case 'b':
396 bflag = 1;
397 break;
398
399 case 'C':
400 Cflag = 1;
401 break;
402
403 case 'd':
404 dflag = 1;
405 break;
406
407 #ifdef INET6
408 case 'L':
409 Lflag = 1;
410 break;
411 #endif
412
413 case 'l':
414 lflag = 1;
415 break;
416
417 case 'm':
418 mflag = 1;
419 break;
420
421 case 's':
422 sflag = 1;
423 break;
424
425 case 'u':
426 uflag = 1;
427 break;
428
429 case 'v':
430 vflag = 1;
431 break;
432
433 case 'z':
434 zflag = 1;
435 break;
436
437
438 default:
439 usage();
440 /* NOTREACHED */
441 }
442 }
443 argc -= optind;
444 argv += optind;
445
446 /*
447 * -l means "list all interfaces", and is mutally exclusive with
448 * all other flags/commands.
449 *
450 * -C means "list all names of cloners", and it mutually exclusive
451 * with all other flags/commands.
452 *
453 * -a means "print status of all interfaces".
454 */
455 if ((lflag || Cflag) && (aflag || mflag || vflag || argc || zflag))
456 usage();
457 #ifdef INET6
458 if ((lflag || Cflag) && Lflag)
459 usage();
460 #endif
461 if (lflag && Cflag)
462 usage();
463 if (Cflag) {
464 if (argc)
465 usage();
466 list_cloners();
467 exit(0);
468 }
469 if (aflag || lflag) {
470 if (argc > 1)
471 usage();
472 else if (argc == 1) {
473 afp = lookup_af_byname(argv[0]);
474 if (afp == NULL)
475 usage();
476 }
477 if (afp)
478 af = ifr.ifr_addr.sa_family = afp->af_af;
479 else
480 af = ifr.ifr_addr.sa_family = afs[0].af_af;
481 printall(NULL);
482 exit(0);
483 }
484
485 /* Make sure there's an interface name. */
486 if (argc < 1)
487 usage();
488 if (strlcpy(name, argv[0], sizeof(name)) >= sizeof(name))
489 errx(1, "interface name '%s' too long", argv[0]);
490 argc--; argv++;
491
492 /*
493 * NOTE: We must special-case the `create' command right
494 * here as we would otherwise fail in getinfo().
495 */
496 if (argc > 0 && strcmp(argv[0], "create") == 0) {
497 clone_create(argv[0], 0);
498 argc--, argv++;
499 if (argc == 0)
500 exit(0);
501 }
502
503 /* Check for address family. */
504 afp = NULL;
505 if (argc > 0) {
506 afp = lookup_af_byname(argv[0]);
507 if (afp != NULL) {
508 argv++;
509 argc--;
510 }
511 }
512
513 /* Initialize af, just for use in getinfo(). */
514 if (afp == NULL)
515 af = afs->af_af;
516 else
517 af = afp->af_af;
518
519 /* Get information about the interface. */
520 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
521 if (getinfo(&ifr) < 0)
522 exit(1);
523
524 if (sflag) {
525 if (argc != 0)
526 usage();
527 else
528 exit(carrier());
529 }
530
531 /* No more arguments means interface status. */
532 if (argc == 0) {
533 printall(name);
534 exit(0);
535 }
536
537 /* The following operations assume inet family as the default. */
538 if (afp == NULL)
539 afp = afs;
540 af = ifr.ifr_addr.sa_family = afp->af_af;
541
542 #ifdef INET6
543 /* initialization */
544 in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
545 in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
546 #endif
547
548 /* Process commands. */
549 while (argc > 0) {
550 const struct cmd *p;
551
552 for (p = cmds; p->c_name; p++)
553 if (strcmp(argv[0], p->c_name) == 0)
554 break;
555 if (p->c_name == 0 && setaddr) {
556 if ((flags & IFF_POINTOPOINT) == 0) {
557 errx(EXIT_FAILURE,
558 "can't set destination address %s",
559 "on non-point-to-point link");
560 }
561 p++; /* got src, do dst */
562 }
563 if (p->c_func != NULL || p->c_func2 != NULL) {
564 if (p->c_parameter == NEXTARG) {
565 if (argc < 2)
566 errx(EXIT_FAILURE,
567 "'%s' requires argument",
568 p->c_name);
569 (*p->c_func)(argv[1], 0);
570 argc--, argv++;
571 } else if (p->c_parameter == NEXTARG2) {
572 if (argc < 3)
573 errx(EXIT_FAILURE,
574 "'%s' requires 2 arguments",
575 p->c_name);
576 (*p->c_func2)(argv[1], argv[2]);
577 argc -= 2, argv += 2;
578 } else
579 (*p->c_func)(argv[0], p->c_parameter);
580 actions |= p->c_action;
581 }
582 argc--, argv++;
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
614 if (setipdst && af == AF_NS)
615 xns_set_nsip_route(&addreq.ifra_addr, &addreq.ifra_dstaddr);
616 #endif /* INET_ONLY */
617
618 if (clearaddr) {
619 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
620 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
621 err(EXIT_FAILURE, "SIOCDIFADDR");
622 }
623 if (newaddr > 0) {
624 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
625 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
626 warn("SIOCAIFADDR");
627 }
628
629 if (g_ifcr_updated) {
630 (void) strncpy(g_ifcr.ifcr_name, name,
631 sizeof(g_ifcr.ifcr_name));
632 if (ioctl(s, SIOCSIFCAP, &g_ifcr) == -1)
633 err(EXIT_FAILURE, "SIOCSIFCAP");
634 }
635
636 exit(0);
637 }
638
639 const struct afswtch *
640 lookup_af_byname(const char *cp)
641 {
642 const struct afswtch *a;
643
644 for (a = afs; a->af_name != NULL; a++)
645 if (strcmp(a->af_name, cp) == 0)
646 return (a);
647 return (NULL);
648 }
649
650 const struct afswtch *
651 lookup_af_bynum(int afnum)
652 {
653 const struct afswtch *a;
654
655 for (a = afs; a->af_name != NULL; a++)
656 if (a->af_af == afnum)
657 return (a);
658 return (NULL);
659 }
660
661 void
662 getsock(int naf)
663 {
664 static int oaf = -1;
665
666 if (oaf == naf)
667 return;
668 if (oaf != -1)
669 close(s);
670 s = socket(naf, SOCK_DGRAM, 0);
671 if (s < 0)
672 oaf = -1;
673 else
674 oaf = naf;
675 }
676
677 int
678 getinfo(struct ifreq *giifr)
679 {
680
681 getsock(af);
682 if (s < 0)
683 err(EXIT_FAILURE, "socket");
684 if (ioctl(s, SIOCGIFFLAGS, giifr) == -1) {
685 warn("SIOCGIFFLAGS %s", giifr->ifr_name);
686 return (-1);
687 }
688 flags = giifr->ifr_flags;
689 if (ioctl(s, SIOCGIFMETRIC, giifr) == -1) {
690 warn("SIOCGIFMETRIC %s", giifr->ifr_name);
691 metric = 0;
692 } else
693 metric = giifr->ifr_metric;
694 if (ioctl(s, SIOCGIFMTU, giifr) == -1)
695 mtu = 0;
696 else
697 mtu = giifr->ifr_mtu;
698
699 memset(&g_ifcr, 0, sizeof(g_ifcr));
700 strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
701 (void) ioctl(s, SIOCGIFCAP, &g_ifcr);
702
703 return (0);
704 }
705
706 void
707 printall(const char *ifname)
708 {
709 struct ifaddrs *ifap, *ifa;
710 struct ifreq paifr;
711 const struct sockaddr_dl *sdl = NULL;
712 int idx;
713 char *p;
714
715 if (getifaddrs(&ifap) != 0)
716 err(EXIT_FAILURE, "getifaddrs");
717 p = NULL;
718 idx = 0;
719 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
720 memset(&paifr, 0, sizeof(paifr));
721 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
722 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
723 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
724 ifa->ifa_addr->sa_len);
725 }
726
727 if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
728 continue;
729 if (ifa->ifa_addr->sa_family == AF_LINK)
730 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
731 if (p && strcmp(p, ifa->ifa_name) == 0)
732 continue;
733 if (strlcpy(name, ifa->ifa_name, sizeof(name)) >= sizeof(name))
734 continue;
735 p = ifa->ifa_name;
736
737 if (getinfo(&paifr) < 0)
738 continue;
739 if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
740 continue;
741 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
742 continue;
743 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
744 continue;
745
746 if (sflag && carrier())
747 continue;
748 idx++;
749 /*
750 * Are we just listing the interfaces?
751 */
752 if (lflag) {
753 if (idx > 1)
754 printf(" ");
755 fputs(name, stdout);
756 continue;
757 }
758
759 status(sdl);
760 sdl = NULL;
761 }
762 if (lflag)
763 printf("\n");
764 freeifaddrs(ifap);
765 }
766
767 void
768 list_cloners(void)
769 {
770 struct if_clonereq ifcr;
771 char *cp, *buf;
772 int idx;
773
774 memset(&ifcr, 0, sizeof(ifcr));
775
776 getsock(AF_INET);
777
778 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
779 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
780
781 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
782 if (buf == NULL)
783 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
784
785 ifcr.ifcr_count = ifcr.ifcr_total;
786 ifcr.ifcr_buffer = buf;
787
788 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
789 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
790
791 /*
792 * In case some disappeared in the mean time, clamp it down.
793 */
794 if (ifcr.ifcr_count > ifcr.ifcr_total)
795 ifcr.ifcr_count = ifcr.ifcr_total;
796
797 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
798 if (idx > 0)
799 printf(" ");
800 printf("%s", cp);
801 }
802
803 printf("\n");
804 free(buf);
805 return;
806 }
807
808 /*ARGSUSED*/
809 void
810 clone_create(const char *addr, int param)
811 {
812
813 /* We're called early... */
814 getsock(AF_INET);
815
816 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
817 if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
818 err(EXIT_FAILURE, "SIOCIFCREATE");
819 }
820
821 /*ARGSUSED*/
822 void
823 clone_destroy(const char *addr, int param)
824 {
825
826 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
827 if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
828 err(EXIT_FAILURE, "SIOCIFDESTROY");
829 }
830
831 /*ARGSUSED*/
832 void
833 setifaddr(const char *addr, int param)
834 {
835 struct ifreq *siifr; /* XXX */
836
837 /*
838 * Delay the ioctl to set the interface addr until flags are all set.
839 * The address interpretation may depend on the flags,
840 * and the flags may change when the address is set.
841 */
842 setaddr++;
843 if (newaddr == -1)
844 newaddr = 1;
845 if (doalias == 0 && afp->af_gifaddr != 0) {
846 siifr = (struct ifreq *)afp->af_ridreq;
847 (void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
848 siifr->ifr_addr.sa_family = afp->af_af;
849 if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
850 clearaddr = 1;
851 else if (errno == EADDRNOTAVAIL)
852 /* No address was assigned yet. */
853 ;
854 else
855 err(EXIT_FAILURE, "SIOCGIFADDR");
856 }
857
858 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
859 }
860
861 void
862 setifnetmask(const char *addr, int d)
863 {
864 (*afp->af_getaddr)(addr, MASK);
865 }
866
867 void
868 setifbroadaddr(const char *addr, int d)
869 {
870 (*afp->af_getaddr)(addr, DSTADDR);
871 }
872
873 void
874 setifipdst(const char *addr, int d)
875 {
876 in_getaddr(addr, DSTADDR);
877 setipdst++;
878 clearaddr = 0;
879 newaddr = 0;
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 setifflags(const char *vname, int value)
917 {
918 struct ifreq ifreq;
919
920 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
921 if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
922 err(EXIT_FAILURE, "SIOCGIFFLAGS");
923 flags = ifreq.ifr_flags;
924
925 if (value < 0) {
926 value = -value;
927 flags &= ~value;
928 } else
929 flags |= value;
930 ifreq.ifr_flags = flags;
931 if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
932 err(EXIT_FAILURE, "SIOCSIFFLAGS");
933 }
934
935 void
936 setifcaps(const char *vname, int value)
937 {
938
939 if (value < 0) {
940 value = -value;
941 g_ifcr.ifcr_capenable &= ~value;
942 } else
943 g_ifcr.ifcr_capenable |= value;
944
945 g_ifcr_updated = 1;
946 }
947
948 #ifdef INET6
949 void
950 setia6flags(const char *vname, int value)
951 {
952
953 if (value < 0) {
954 value = -value;
955 in6_addreq.ifra_flags &= ~value;
956 } else
957 in6_addreq.ifra_flags |= value;
958 }
959
960 void
961 setia6pltime(const char *val, int d)
962 {
963
964 setia6lifetime("pltime", val);
965 }
966
967 void
968 setia6vltime(const char *val, int d)
969 {
970
971 setia6lifetime("vltime", val);
972 }
973
974 void
975 setia6lifetime(const char *cmd, const char *val)
976 {
977 time_t newval, t;
978 char *ep;
979
980 t = time(NULL);
981 newval = (time_t)strtoul(val, &ep, 0);
982 if (val == ep)
983 errx(EXIT_FAILURE, "invalid %s", cmd);
984 if (afp->af_af != AF_INET6)
985 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
986 if (strcmp(cmd, "vltime") == 0) {
987 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
988 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
989 } else if (strcmp(cmd, "pltime") == 0) {
990 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
991 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
992 }
993 }
994
995 void
996 setia6eui64(const char *cmd, int val)
997 {
998 struct ifaddrs *ifap, *ifa;
999 const struct sockaddr_in6 *sin6 = NULL;
1000 const struct in6_addr *lladdr = NULL;
1001 struct in6_addr *in6;
1002
1003 if (afp->af_af != AF_INET6)
1004 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1005 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1006 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1007 errx(EXIT_FAILURE, "interface index is already filled");
1008 if (getifaddrs(&ifap) != 0)
1009 err(EXIT_FAILURE, "getifaddrs");
1010 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1011 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1012 strcmp(ifa->ifa_name, name) == 0) {
1013 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1014 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1015 lladdr = &sin6->sin6_addr;
1016 break;
1017 }
1018 }
1019 }
1020 if (!lladdr)
1021 errx(EXIT_FAILURE, "could not determine link local address");
1022
1023 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
1024
1025 freeifaddrs(ifap);
1026 }
1027 #endif
1028
1029 void
1030 setifmetric(const char *val, int d)
1031 {
1032 char *ep = NULL;
1033
1034 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1035 ifr.ifr_metric = strtoul(val, &ep, 10);
1036 if (!ep || *ep)
1037 errx(EXIT_FAILURE, "%s: invalid metric", val);
1038 if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
1039 warn("SIOCSIFMETRIC");
1040 }
1041
1042 void
1043 setifmtu(const char *val, int d)
1044 {
1045 char *ep = NULL;
1046
1047 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1048 ifr.ifr_mtu = strtoul(val, &ep, 10);
1049 if (!ep || *ep)
1050 errx(EXIT_FAILURE, "%s: invalid mtu", val);
1051 if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
1052 warn("SIOCSIFMTU");
1053 }
1054
1055 const char *
1056 get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp)
1057 {
1058 int len;
1059 int hexstr;
1060 u_int8_t *p;
1061
1062 len = *lenp;
1063 p = buf;
1064 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1065 if (hexstr)
1066 val += 2;
1067 for (;;) {
1068 if (*val == '\0')
1069 break;
1070 if (sep != NULL && strchr(sep, *val) != NULL) {
1071 val++;
1072 break;
1073 }
1074 if (hexstr) {
1075 if (!isxdigit((u_char)val[0]) ||
1076 !isxdigit((u_char)val[1])) {
1077 warnx("bad hexadecimal digits");
1078 return NULL;
1079 }
1080 }
1081 if (p > buf + len) {
1082 if (hexstr)
1083 warnx("hexadecimal digits too long");
1084 else
1085 warnx("strings too long");
1086 return NULL;
1087 }
1088 if (hexstr) {
1089 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1090 *p++ = (tohex((u_char)val[0]) << 4) |
1091 tohex((u_char)val[1]);
1092 #undef tohex
1093 val += 2;
1094 } else
1095 *p++ = *val++;
1096 }
1097 len = p - buf;
1098 if (len < *lenp)
1099 memset(p, 0, *lenp - len);
1100 *lenp = len;
1101 return val;
1102 }
1103
1104 void
1105 print_string(const u_int8_t *buf, int len)
1106 {
1107 int i;
1108 int hasspc;
1109
1110 i = 0;
1111 hasspc = 0;
1112 if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
1113 for (; i < len; i++) {
1114 if (!isprint(buf[i]))
1115 break;
1116 if (isspace(buf[i]))
1117 hasspc++;
1118 }
1119 }
1120 if (i == len) {
1121 if (hasspc || len == 0)
1122 printf("\"%.*s\"", len, buf);
1123 else
1124 printf("%.*s", len, buf);
1125 } else {
1126 printf("0x");
1127 for (i = 0; i < len; i++)
1128 printf("%02x", buf[i]);
1129 }
1130 }
1131
1132 static void
1133 media_error(int type, const char *val, const char *opt)
1134 {
1135 errx(EXIT_FAILURE, "unknown %s media %s: %s",
1136 get_media_type_string(type), opt, val);
1137 }
1138
1139 void
1140 init_current_media(void)
1141 {
1142 struct ifmediareq ifmr;
1143
1144 /*
1145 * If we have not yet done so, grab the currently-selected
1146 * media.
1147 */
1148 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1149 (void) memset(&ifmr, 0, sizeof(ifmr));
1150 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1151
1152 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1153 /*
1154 * If we get E2BIG, the kernel is telling us
1155 * that there are more, so we can ignore it.
1156 */
1157 if (errno != E2BIG)
1158 err(EXIT_FAILURE, "SGIOCGIFMEDIA");
1159 }
1160
1161 media_current = ifmr.ifm_current;
1162 }
1163
1164 /* Sanity. */
1165 if (IFM_TYPE(media_current) == 0)
1166 errx(EXIT_FAILURE, "%s: no link type?", name);
1167 }
1168
1169 void
1170 process_media_commands(void)
1171 {
1172
1173 if ((actions & (A_MEDIA|A_MEDIAOPT|A_MEDIAMODE)) == 0) {
1174 /* Nothing to do. */
1175 return;
1176 }
1177
1178 /*
1179 * Media already set up, and commands sanity-checked. Set/clear
1180 * any options, and we're ready to go.
1181 */
1182 media_current |= mediaopt_set;
1183 media_current &= ~mediaopt_clear;
1184
1185 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1186 ifr.ifr_media = media_current;
1187
1188 if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
1189 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1190 }
1191
1192 void
1193 setmedia(const char *val, int d)
1194 {
1195 int type, subtype, inst;
1196
1197 init_current_media();
1198
1199 /* Only one media command may be given. */
1200 if (actions & A_MEDIA)
1201 errx(EXIT_FAILURE, "only one `media' command may be issued");
1202
1203 /* Must not come after mode commands */
1204 if (actions & A_MEDIAMODE)
1205 errx(EXIT_FAILURE,
1206 "may not issue `media' after `mode' commands");
1207
1208 /* Must not come after mediaopt commands */
1209 if (actions & A_MEDIAOPT)
1210 errx(EXIT_FAILURE,
1211 "may not issue `media' after `mediaopt' commands");
1212
1213 /*
1214 * No need to check if `instance' has been issued; setmediainst()
1215 * craps out if `media' has not been specified.
1216 */
1217
1218 type = IFM_TYPE(media_current);
1219 inst = IFM_INST(media_current);
1220
1221 /* Look up the subtype. */
1222 subtype = get_media_subtype(type, val);
1223 if (subtype == -1)
1224 media_error(type, val, "subtype");
1225
1226 /* Build the new current media word. */
1227 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1228
1229 /* Media will be set after other processing is complete. */
1230 }
1231
1232 void
1233 setmediaopt(const char *val, int d)
1234 {
1235 char *invalid;
1236
1237 init_current_media();
1238
1239 /* Can only issue `mediaopt' once. */
1240 if (actions & A_MEDIAOPTSET)
1241 errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
1242
1243 /* Can't issue `mediaopt' if `instance' has already been issued. */
1244 if (actions & A_MEDIAINST)
1245 errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
1246
1247 mediaopt_set = get_media_options(media_current, val, &invalid);
1248 if (mediaopt_set == -1)
1249 media_error(media_current, invalid, "option");
1250
1251 /* Media will be set after other processing is complete. */
1252 }
1253
1254 void
1255 unsetmediaopt(const char *val, int d)
1256 {
1257 char *invalid;
1258
1259 init_current_media();
1260
1261 /* Can only issue `-mediaopt' once. */
1262 if (actions & A_MEDIAOPTCLR)
1263 errx(EXIT_FAILURE,
1264 "only one `-mediaopt' command may be issued");
1265
1266 /* May not issue `media' and `-mediaopt'. */
1267 if (actions & A_MEDIA)
1268 errx(EXIT_FAILURE,
1269 "may not issue both `media' and `-mediaopt'");
1270
1271 /*
1272 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1273 * implicitly checks for A_MEDIAINST.
1274 */
1275
1276 mediaopt_clear = get_media_options(media_current, val, &invalid);
1277 if (mediaopt_clear == -1)
1278 media_error(media_current, invalid, "option");
1279
1280 /* Media will be set after other processing is complete. */
1281 }
1282
1283 void
1284 setmediainst(const char *val, int d)
1285 {
1286 int type, subtype, options, inst;
1287
1288 init_current_media();
1289
1290 /* Can only issue `instance' once. */
1291 if (actions & A_MEDIAINST)
1292 errx(EXIT_FAILURE, "only one `instance' command may be issued");
1293
1294 /* Must have already specified `media' */
1295 if ((actions & A_MEDIA) == 0)
1296 errx(EXIT_FAILURE, "must specify `media' before `instance'");
1297
1298 type = IFM_TYPE(media_current);
1299 subtype = IFM_SUBTYPE(media_current);
1300 options = IFM_OPTIONS(media_current);
1301
1302 inst = atoi(val);
1303 if (inst < 0 || inst > IFM_INST_MAX)
1304 errx(EXIT_FAILURE, "invalid media instance: %s", val);
1305
1306 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1307
1308 /* Media will be set after other processing is complete. */
1309 }
1310
1311 void
1312 setmediamode(const char *val, int d)
1313 {
1314 int type, subtype, options, inst, mode;
1315
1316 init_current_media();
1317
1318 /* Can only issue `mode' once. */
1319 if (actions & A_MEDIAMODE)
1320 errx(EXIT_FAILURE, "only one `mode' command may be issued");
1321
1322 type = IFM_TYPE(media_current);
1323 subtype = IFM_SUBTYPE(media_current);
1324 options = IFM_OPTIONS(media_current);
1325 inst = IFM_INST(media_current);
1326
1327 mode = get_media_mode(type, val);
1328 if (mode == -1)
1329 media_error(type, val, "mode");
1330
1331 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1332
1333 /* Media will be set after other processing is complete. */
1334 }
1335
1336 void
1337 print_media_word(int ifmw, const char *opt_sep)
1338 {
1339 const char *str;
1340
1341 printf("%s", get_media_subtype_string(ifmw));
1342
1343 /* Find mode. */
1344 if (IFM_MODE(ifmw) != 0) {
1345 str = get_media_mode_string(ifmw);
1346 if (str != NULL)
1347 printf(" mode %s", str);
1348 }
1349
1350 /* Find options. */
1351 for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1352 printf("%s%s", opt_sep, str);
1353
1354 if (IFM_INST(ifmw) != 0)
1355 printf(" instance %d", IFM_INST(ifmw));
1356 }
1357
1358 int
1359 carrier(void)
1360 {
1361 struct ifmediareq ifmr;
1362
1363 (void) memset(&ifmr, 0, sizeof(ifmr));
1364 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1365
1366 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1367 /*
1368 * Interface doesn't support SIOC{G,S}IFMEDIA;
1369 * assume ok.
1370 */
1371 return 0;
1372 }
1373 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1374 /*
1375 * Interface doesn't report media-valid status.
1376 * assume ok.
1377 */
1378 return 0;
1379 }
1380 /* otherwise, return ok for active, not-ok if not active. */
1381 return !(ifmr.ifm_status & IFM_ACTIVE);
1382 }
1383
1384
1385 #define IFFBITS \
1386 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
1387 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
1388
1389 #define IFCAPBITS \
1390 "\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx\10TSO4"
1391
1392 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1393
1394 const struct ifmedia_status_description ifm_status_descriptions[] =
1395 IFM_STATUS_DESCRIPTIONS;
1396
1397 /*
1398 * Print the status of the interface. If an address family was
1399 * specified, show it and it only; otherwise, show them all.
1400 */
1401 void
1402 status(const struct sockaddr_dl *sdl)
1403 {
1404 const struct afswtch *p = afp;
1405 struct ifmediareq ifmr;
1406 struct ifdatareq ifdr;
1407 int *media_list, i;
1408 char hbuf[NI_MAXHOST];
1409 char fbuf[BUFSIZ];
1410
1411 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1412 printf("%s: flags=%s", name, &fbuf[2]);
1413 if (metric)
1414 printf(" metric %lu", metric);
1415 if (mtu)
1416 printf(" mtu %lu", mtu);
1417 printf("\n");
1418
1419 if (g_ifcr.ifcr_capabilities) {
1420 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1421 g_ifcr.ifcr_capabilities);
1422 printf("\tcapabilities=%s\n", &fbuf[2]);
1423 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1424 g_ifcr.ifcr_capenable);
1425 printf("\tenabled=%s\n", &fbuf[2]);
1426 }
1427
1428 ieee80211_status();
1429 vlan_status();
1430 tunnel_status();
1431 agr_status();
1432
1433 if (sdl != NULL &&
1434 getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1435 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1436 hbuf[0] != '\0')
1437 printf("\taddress: %s\n", hbuf);
1438
1439 (void) memset(&ifmr, 0, sizeof(ifmr));
1440 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1441
1442 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1443 /*
1444 * Interface doesn't support SIOC{G,S}IFMEDIA.
1445 */
1446 goto iface_stats;
1447 }
1448
1449 if (ifmr.ifm_count == 0) {
1450 warnx("%s: no media types?", name);
1451 goto iface_stats;
1452 }
1453
1454 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1455 if (media_list == NULL)
1456 err(EXIT_FAILURE, "malloc");
1457 ifmr.ifm_ulist = media_list;
1458
1459 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1460 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1461
1462 printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1463 print_media_word(ifmr.ifm_current, " ");
1464 if (ifmr.ifm_active != ifmr.ifm_current) {
1465 printf(" (");
1466 print_media_word(ifmr.ifm_active, " ");
1467 printf(")");
1468 }
1469 printf("\n");
1470
1471 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1472 const struct ifmedia_status_description *ifms;
1473 int bitno, found = 0;
1474
1475 printf("\tstatus: ");
1476 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1477 for (ifms = ifm_status_descriptions;
1478 ifms->ifms_valid != 0; ifms++) {
1479 if (ifms->ifms_type !=
1480 IFM_TYPE(ifmr.ifm_current) ||
1481 ifms->ifms_valid !=
1482 ifm_status_valid_list[bitno])
1483 continue;
1484 printf("%s%s", found ? ", " : "",
1485 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1486 found = 1;
1487
1488 /*
1489 * For each valid indicator bit, there's
1490 * only one entry for each media type, so
1491 * terminate the inner loop now.
1492 */
1493 break;
1494 }
1495 }
1496
1497 if (found == 0)
1498 printf("unknown");
1499 printf("\n");
1500 }
1501
1502 if (mflag) {
1503 int type, printed_type;
1504
1505 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1506 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1507 if (IFM_TYPE(media_list[i]) != type)
1508 continue;
1509 if (printed_type == 0) {
1510 printf("\tsupported %s media:\n",
1511 get_media_type_string(type));
1512 printed_type = 1;
1513 }
1514 printf("\t\tmedia ");
1515 print_media_word(media_list[i], " mediaopt ");
1516 printf("\n");
1517 }
1518 }
1519 }
1520
1521 free(media_list);
1522
1523 iface_stats:
1524 if (!vflag && !zflag)
1525 goto proto_status;
1526
1527 (void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
1528
1529 if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1530 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1531 } else {
1532 struct if_data * const ifi = &ifdr.ifdr_data;
1533 #define PLURAL(n) ((n) == 1 ? "" : "s")
1534 printf("\tinput: %llu packet%s, %llu byte%s",
1535 (unsigned long long) ifi->ifi_ipackets,
1536 PLURAL(ifi->ifi_ipackets),
1537 (unsigned long long) ifi->ifi_ibytes,
1538 PLURAL(ifi->ifi_ibytes));
1539 if (ifi->ifi_imcasts)
1540 printf(", %llu multicast%s",
1541 (unsigned long long) ifi->ifi_imcasts,
1542 PLURAL(ifi->ifi_imcasts));
1543 if (ifi->ifi_ierrors)
1544 printf(", %llu error%s",
1545 (unsigned long long) ifi->ifi_ierrors,
1546 PLURAL(ifi->ifi_ierrors));
1547 if (ifi->ifi_iqdrops)
1548 printf(", %llu queue drop%s",
1549 (unsigned long long) ifi->ifi_iqdrops,
1550 PLURAL(ifi->ifi_iqdrops));
1551 if (ifi->ifi_noproto)
1552 printf(", %llu unknown protocol",
1553 (unsigned long long) ifi->ifi_noproto);
1554 printf("\n\toutput: %llu packet%s, %llu byte%s",
1555 (unsigned long long) ifi->ifi_opackets,
1556 PLURAL(ifi->ifi_opackets),
1557 (unsigned long long) ifi->ifi_obytes,
1558 PLURAL(ifi->ifi_obytes));
1559 if (ifi->ifi_omcasts)
1560 printf(", %llu multicast%s",
1561 (unsigned long long) ifi->ifi_omcasts,
1562 PLURAL(ifi->ifi_omcasts));
1563 if (ifi->ifi_oerrors)
1564 printf(", %llu error%s",
1565 (unsigned long long) ifi->ifi_oerrors,
1566 PLURAL(ifi->ifi_oerrors));
1567 if (ifi->ifi_collisions)
1568 printf(", %llu collision%s",
1569 (unsigned long long) ifi->ifi_collisions,
1570 PLURAL(ifi->ifi_collisions));
1571 printf("\n");
1572 #undef PLURAL
1573 }
1574
1575 ieee80211_statistics();
1576
1577 proto_status:
1578 if ((p = afp) != NULL) {
1579 (*p->af_status)(1);
1580 } else for (p = afs; p->af_name; p++) {
1581 ifr.ifr_addr.sa_family = p->af_af;
1582 (*p->af_status)(0);
1583 }
1584 }
1585
1586 void
1587 in_alias(struct ifreq *creq)
1588 {
1589 struct sockaddr_in *iasin;
1590 int alias;
1591
1592 if (lflag)
1593 return;
1594
1595 alias = 1;
1596
1597 /* Get the non-alias address for this interface. */
1598 getsock(AF_INET);
1599 if (s < 0) {
1600 if (errno == EPROTONOSUPPORT)
1601 return;
1602 err(EXIT_FAILURE, "socket");
1603 }
1604 (void) memset(&ifr, 0, sizeof(ifr));
1605 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1606 if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
1607 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1608 return;
1609 } else
1610 warn("SIOCGIFADDR");
1611 }
1612 /* If creq and ifr are the same address, this is not an alias. */
1613 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
1614 sizeof(creq->ifr_addr)) == 0)
1615 alias = 0;
1616 (void) memset(&in_addreq, 0, sizeof(in_addreq));
1617 (void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
1618 memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
1619 sizeof(in_addreq.ifra_addr));
1620 if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
1621 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1622 return;
1623 } else
1624 warn("SIOCGIFALIAS");
1625 }
1626
1627 iasin = &in_addreq.ifra_addr;
1628 printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
1629
1630 if (flags & IFF_POINTOPOINT) {
1631 iasin = &in_addreq.ifra_dstaddr;
1632 printf(" -> %s", inet_ntoa(iasin->sin_addr));
1633 }
1634
1635 iasin = &in_addreq.ifra_mask;
1636 printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
1637
1638 if (flags & IFF_BROADCAST) {
1639 iasin = &in_addreq.ifra_broadaddr;
1640 printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
1641 }
1642 printf("\n");
1643 }
1644
1645 void
1646 in_status(int force)
1647 {
1648 struct ifaddrs *ifap, *ifa;
1649 struct ifreq isifr;
1650
1651 if (getifaddrs(&ifap) != 0)
1652 err(EXIT_FAILURE, "getifaddrs");
1653 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1654 if (strcmp(name, ifa->ifa_name) != 0)
1655 continue;
1656 if (ifa->ifa_addr->sa_family != AF_INET)
1657 continue;
1658 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
1659 continue;
1660
1661 memset(&isifr, 0, sizeof(isifr));
1662 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
1663 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
1664 in_alias(&isifr);
1665 }
1666 freeifaddrs(ifap);
1667 }
1668
1669 void
1670 setifprefixlen(const char *addr, int d)
1671 {
1672 if (*afp->af_getprefix)
1673 (*afp->af_getprefix)(addr, MASK);
1674 explicit_prefix = 1;
1675 }
1676
1677 #ifdef INET6
1678 void
1679 in6_fillscopeid(struct sockaddr_in6 *sin6)
1680 {
1681 #if defined(__KAME__) && defined(KAME_SCOPEID)
1682 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1683 sin6->sin6_scope_id =
1684 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1685 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1686 }
1687 #endif
1688 }
1689
1690 /* XXX not really an alias */
1691 void
1692 in6_alias(struct in6_ifreq *creq)
1693 {
1694 struct sockaddr_in6 *sin6;
1695 char hbuf[NI_MAXHOST];
1696 u_int32_t scopeid;
1697 const int niflag = NI_NUMERICHOST;
1698
1699 /* Get the non-alias address for this interface. */
1700 getsock(AF_INET6);
1701 if (s < 0) {
1702 if (errno == EPROTONOSUPPORT)
1703 return;
1704 err(EXIT_FAILURE, "socket");
1705 }
1706
1707 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
1708
1709 in6_fillscopeid(sin6);
1710 scopeid = sin6->sin6_scope_id;
1711 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
1712 hbuf, sizeof(hbuf), NULL, 0, niflag))
1713 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
1714 printf("\tinet6 %s", hbuf);
1715
1716 if (flags & IFF_POINTOPOINT) {
1717 (void) memset(&ifr6, 0, sizeof(ifr6));
1718 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1719 ifr6.ifr_addr = creq->ifr_addr;
1720 if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
1721 if (errno != EADDRNOTAVAIL)
1722 warn("SIOCGIFDSTADDR_IN6");
1723 (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
1724 ifr6.ifr_addr.sin6_family = AF_INET6;
1725 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1726 }
1727 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
1728 in6_fillscopeid(sin6);
1729 hbuf[0] = '\0';
1730 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
1731 hbuf, sizeof(hbuf), NULL, 0, niflag))
1732 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
1733 printf(" -> %s", hbuf);
1734 }
1735
1736 (void) memset(&ifr6, 0, sizeof(ifr6));
1737 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1738 ifr6.ifr_addr = creq->ifr_addr;
1739 if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
1740 if (errno != EADDRNOTAVAIL)
1741 warn("SIOCGIFNETMASK_IN6");
1742 } else {
1743 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
1744 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
1745 sizeof(struct in6_addr)));
1746 }
1747
1748 (void) memset(&ifr6, 0, sizeof(ifr6));
1749 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1750 ifr6.ifr_addr = creq->ifr_addr;
1751 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
1752 if (errno != EADDRNOTAVAIL)
1753 warn("SIOCGIFAFLAG_IN6");
1754 } else {
1755 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
1756 printf(" anycast");
1757 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
1758 printf(" tentative");
1759 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
1760 printf(" duplicated");
1761 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
1762 printf(" detached");
1763 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
1764 printf(" deprecated");
1765 }
1766
1767 if (scopeid)
1768 printf(" scopeid 0x%x", scopeid);
1769
1770 if (Lflag) {
1771 struct in6_addrlifetime *lifetime;
1772 (void) memset(&ifr6, 0, sizeof(ifr6));
1773 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1774 ifr6.ifr_addr = creq->ifr_addr;
1775 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
1776 if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
1777 if (errno != EADDRNOTAVAIL)
1778 warn("SIOCGIFALIFETIME_IN6");
1779 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
1780 time_t t = time(NULL);
1781 printf(" pltime ");
1782 if (lifetime->ia6t_preferred) {
1783 printf("%s", lifetime->ia6t_preferred < t
1784 ? "0"
1785 : sec2str(lifetime->ia6t_preferred - t));
1786 } else
1787 printf("infty");
1788
1789 printf(" vltime ");
1790 if (lifetime->ia6t_expire) {
1791 printf("%s", lifetime->ia6t_expire < t
1792 ? "0"
1793 : sec2str(lifetime->ia6t_expire - t));
1794 } else
1795 printf("infty");
1796 }
1797 }
1798
1799 printf("\n");
1800 }
1801
1802 void
1803 in6_status(int force)
1804 {
1805 struct ifaddrs *ifap, *ifa;
1806 struct in6_ifreq isifr;
1807
1808 if (getifaddrs(&ifap) != 0)
1809 err(EXIT_FAILURE, "getifaddrs");
1810 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1811 if (strcmp(name, ifa->ifa_name) != 0)
1812 continue;
1813 if (ifa->ifa_addr->sa_family != AF_INET6)
1814 continue;
1815 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
1816 continue;
1817
1818 memset(&isifr, 0, sizeof(isifr));
1819 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
1820 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
1821 in6_alias(&isifr);
1822 }
1823 freeifaddrs(ifap);
1824 }
1825 #endif /*INET6*/
1826
1827 #ifndef INET_ONLY
1828
1829 void
1830 iso_status(int force)
1831 {
1832 struct sockaddr_iso *siso;
1833 struct iso_ifreq isoifr;
1834
1835 getsock(AF_ISO);
1836 if (s < 0) {
1837 if (errno == EPROTONOSUPPORT)
1838 return;
1839 err(EXIT_FAILURE, "socket");
1840 }
1841 (void) memset(&isoifr, 0, sizeof(isoifr));
1842 (void) strncpy(isoifr.ifr_name, name, sizeof(isoifr.ifr_name));
1843 if (ioctl(s, SIOCGIFADDR_ISO, &isoifr) == -1) {
1844 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1845 if (!force)
1846 return;
1847 (void) memset(&isoifr.ifr_Addr, 0,
1848 sizeof(isoifr.ifr_Addr));
1849 } else
1850 warn("SIOCGIFADDR_ISO");
1851 }
1852 (void) strncpy(isoifr.ifr_name, name, sizeof isoifr.ifr_name);
1853 siso = &isoifr.ifr_Addr;
1854 printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
1855 if (ioctl(s, SIOCGIFNETMASK_ISO, &isoifr) == -1) {
1856 if (errno == EADDRNOTAVAIL)
1857 memset(&isoifr.ifr_Addr, 0, sizeof(isoifr.ifr_Addr));
1858 else
1859 warn("SIOCGIFNETMASK_ISO");
1860 } else {
1861 if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
1862 siso->siso_addr.isoa_len = siso->siso_len
1863 - offsetof(struct sockaddr_iso, siso_addr);
1864 printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
1865 }
1866 if (flags & IFF_POINTOPOINT) {
1867 if (ioctl(s, SIOCGIFDSTADDR_ISO, &isoifr) == -1) {
1868 if (errno == EADDRNOTAVAIL)
1869 memset(&isoifr.ifr_Addr, 0,
1870 sizeof(isoifr.ifr_Addr));
1871 else
1872 warn("SIOCGIFDSTADDR_ISO");
1873 }
1874 (void) strncpy(isoifr.ifr_name, name, sizeof (isoifr.ifr_name));
1875 siso = &isoifr.ifr_Addr;
1876 printf("--> %s ", iso_ntoa(&siso->siso_addr));
1877 }
1878 printf("\n");
1879 }
1880
1881 #endif /* INET_ONLY */
1882
1883 #define SIN(x) ((struct sockaddr_in *) &(x))
1884 struct sockaddr_in *sintab[] = {
1885 SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
1886 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
1887
1888 void
1889 in_getaddr(const char *str, int which)
1890 {
1891 struct sockaddr_in *gasin = sintab[which];
1892 struct hostent *hp;
1893 struct netent *np;
1894
1895 gasin->sin_len = sizeof(*gasin);
1896 if (which != MASK)
1897 gasin->sin_family = AF_INET;
1898
1899 if (which == ADDR) {
1900 char *p = NULL;
1901 if ((p = strrchr(str, '/')) != NULL) {
1902 *p = '\0';
1903 in_getprefix(p + 1, MASK);
1904 }
1905 }
1906
1907 if (inet_aton(str, &gasin->sin_addr) == 0) {
1908 if ((hp = gethostbyname(str)) != NULL)
1909 (void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
1910 else if ((np = getnetbyname(str)) != NULL)
1911 gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1912 else
1913 errx(EXIT_FAILURE, "%s: bad value", str);
1914 }
1915 }
1916
1917 void
1918 in_getprefix(const char *plen, int which)
1919 {
1920 struct sockaddr_in *igsin = sintab[which];
1921 u_char *cp;
1922 int len = strtol(plen, (char **)NULL, 10);
1923
1924 if ((len < 0) || (len > 32))
1925 errx(EXIT_FAILURE, "%s: bad value", plen);
1926 igsin->sin_len = sizeof(*igsin);
1927 if (which != MASK)
1928 igsin->sin_family = AF_INET;
1929 if ((len == 0) || (len == 32)) {
1930 memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
1931 return;
1932 }
1933 memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
1934 for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
1935 *cp++ = 0xff;
1936 if (len)
1937 *cp = 0xff << (8 - len);
1938 }
1939
1940 #ifdef INET6
1941 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1942 struct sockaddr_in6 *sin6tab[] = {
1943 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1944 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1945
1946 void
1947 in6_getaddr(const char *str, int which)
1948 {
1949 #if defined(__KAME__) && defined(KAME_SCOPEID)
1950 struct sockaddr_in6 *sin6 = sin6tab[which];
1951 struct addrinfo hints, *res;
1952 int error;
1953 char *slash = NULL;
1954
1955 if (which == ADDR) {
1956 if ((slash = strrchr(str, '/')) != NULL)
1957 *slash = '\0';
1958 }
1959
1960 memset(&hints, 0, sizeof(hints));
1961 hints.ai_family = AF_INET6;
1962 hints.ai_socktype = SOCK_DGRAM;
1963 #if 0 /* in_getaddr() allows FQDN */
1964 hints.ai_flags = AI_NUMERICHOST;
1965 #endif
1966 error = getaddrinfo(str, "0", &hints, &res);
1967 if (error && slash) {
1968 /* try again treating the '/' as part of the name */
1969 *slash = '/';
1970 slash = NULL;
1971 error = getaddrinfo(str, "0", &hints, &res);
1972 }
1973 if (error)
1974 errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
1975 if (res->ai_next)
1976 errx(EXIT_FAILURE, "%s: resolved to multiple addresses", str);
1977 if (res->ai_addrlen != sizeof(struct sockaddr_in6))
1978 errx(EXIT_FAILURE, "%s: bad value", str);
1979 memcpy(sin6, res->ai_addr, res->ai_addrlen);
1980 freeaddrinfo(res);
1981 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
1982 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
1983 htons(sin6->sin6_scope_id);
1984 sin6->sin6_scope_id = 0;
1985 }
1986 if (slash) {
1987 in6_getprefix(slash + 1, MASK);
1988 explicit_prefix = 1;
1989 }
1990 #else
1991 struct sockaddr_in6 *gasin = sin6tab[which];
1992
1993 gasin->sin6_len = sizeof(*gasin);
1994 if (which != MASK)
1995 gasin->sin6_family = AF_INET6;
1996
1997 if (which == ADDR) {
1998 char *p = NULL;
1999 if((p = strrchr(str, '/')) != NULL) {
2000 *p = '\0';
2001 in6_getprefix(p + 1, MASK);
2002 explicit_prefix = 1;
2003 }
2004 }
2005
2006 if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
2007 errx(EXIT_FAILURE, "%s: bad value", str);
2008 #endif
2009 }
2010
2011 void
2012 in6_getprefix(const char *plen, int which)
2013 {
2014 struct sockaddr_in6 *gpsin = sin6tab[which];
2015 u_char *cp;
2016 int len = strtol(plen, (char **)NULL, 10);
2017
2018 if ((len < 0) || (len > 128))
2019 errx(EXIT_FAILURE, "%s: bad value", plen);
2020 gpsin->sin6_len = sizeof(*gpsin);
2021 if (which != MASK)
2022 gpsin->sin6_family = AF_INET6;
2023 if ((len == 0) || (len == 128)) {
2024 memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
2025 return;
2026 }
2027 memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
2028 for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
2029 *cp++ = 0xff;
2030 if (len)
2031 *cp = 0xff << (8 - len);
2032 }
2033
2034 int
2035 prefix(void *val, int size)
2036 {
2037 u_char *pname = (u_char *)val;
2038 int byte, bit, plen = 0;
2039
2040 for (byte = 0; byte < size; byte++, plen += 8)
2041 if (pname[byte] != 0xff)
2042 break;
2043 if (byte == size)
2044 return (plen);
2045 for (bit = 7; bit != 0; bit--, plen++)
2046 if (!(pname[byte] & (1 << bit)))
2047 break;
2048 for (; bit != 0; bit--)
2049 if (pname[byte] & (1 << bit))
2050 return(0);
2051 byte++;
2052 for (; byte < size; byte++)
2053 if (pname[byte])
2054 return(0);
2055 return (plen);
2056 }
2057 #endif /*INET6*/
2058
2059 #ifndef INET_ONLY
2060 #define SISO(x) ((struct sockaddr_iso *) &(x))
2061 struct sockaddr_iso *sisotab[] = {
2062 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
2063 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
2064
2065 void
2066 iso_getaddr(const char *addr, int which)
2067 {
2068 struct sockaddr_iso *siso = sisotab[which];
2069 siso->siso_addr = *iso_addr(addr);
2070
2071 if (which == MASK) {
2072 siso->siso_len = TSEL(siso) - (char *)(siso);
2073 siso->siso_nlen = 0;
2074 } else {
2075 siso->siso_len = sizeof(*siso);
2076 siso->siso_family = AF_ISO;
2077 }
2078 }
2079
2080 void
2081 setsnpaoffset(const char *val, int d)
2082 {
2083 iso_addreq.ifra_snpaoffset = atoi(val);
2084 }
2085
2086 void
2087 setnsellength(const char *val, int d)
2088 {
2089 nsellength = atoi(val);
2090 if (nsellength < 0)
2091 errx(EXIT_FAILURE, "Negative NSEL length is absurd");
2092 if (afp == 0 || afp->af_af != AF_ISO)
2093 errx(EXIT_FAILURE, "Setting NSEL length valid only for iso");
2094 }
2095
2096 void
2097 fixnsel(struct sockaddr_iso *siso)
2098 {
2099 if (siso->siso_family == 0)
2100 return;
2101 siso->siso_tlen = nsellength;
2102 }
2103
2104 void
2105 adjust_nsellength(void)
2106 {
2107 fixnsel(sisotab[RIDADDR]);
2108 fixnsel(sisotab[ADDR]);
2109 fixnsel(sisotab[DSTADDR]);
2110 }
2111
2112 #endif /* INET_ONLY */
2113
2114 void
2115 usage(void)
2116 {
2117 const char *progname = getprogname();
2118
2119 fprintf(stderr,
2120 "usage: %s [-m] [-v] [-z] "
2121 #ifdef INET6
2122 "[-L] "
2123 #endif
2124 "interface\n"
2125 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
2126 "\t\t[ alias | -alias ] ]\n"
2127 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
2128 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
2129 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
2130 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
2131 "\t[ arp | -arp ]\n"
2132 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
2133 "[ instance minst ]\n"
2134 "\t[ vlan n vlanif i ]\n"
2135 "\t[ agrport i ] [ -agrport i ]\n"
2136 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
2137 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
2138 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
2139 " %s -a [-b] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
2140 " %s -l [-b] [-d] [-u] [-s]\n"
2141 " %s -C\n"
2142 " %s interface create\n"
2143 " %s interface destroy\n",
2144 progname, progname, progname, progname, progname, progname);
2145 exit(1);
2146 }
2147
2148 #ifdef INET6
2149 char *
2150 sec2str(total)
2151 time_t total;
2152 {
2153 static char result[256];
2154 int days, hours, mins, secs;
2155 int first = 1;
2156 char *p = result;
2157 char *end = &result[sizeof(result)];
2158 int n;
2159
2160 if (0) { /*XXX*/
2161 days = total / 3600 / 24;
2162 hours = (total / 3600) % 24;
2163 mins = (total / 60) % 60;
2164 secs = total % 60;
2165
2166 if (days) {
2167 first = 0;
2168 n = snprintf(p, end - p, "%dd", days);
2169 if (n < 0 || n >= end - p)
2170 return(result);
2171 p += n;
2172 }
2173 if (!first || hours) {
2174 first = 0;
2175 n = snprintf(p, end - p, "%dh", hours);
2176 if (n < 0 || n >= end - p)
2177 return(result);
2178 p += n;
2179 }
2180 if (!first || mins) {
2181 first = 0;
2182 n = snprintf(p, end - p, "%dm", mins);
2183 if (n < 0 || n >= end - p)
2184 return(result);
2185 p += n;
2186 }
2187 snprintf(p, end - p, "%ds", secs);
2188 } else
2189 snprintf(p, end - p, "%lu", (u_long)total);
2190
2191 return(result);
2192 }
2193 #endif
2194