ifconfig.c revision 1.130 1 /* $NetBSD: ifconfig.c,v 1.130 2002/09/20 21:21:53 christos 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. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
76 The Regents of the University of California. All rights reserved.\n");
77 #endif /* not lint */
78
79 #ifndef lint
80 #if 0
81 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
82 #else
83 __RCSID("$NetBSD: ifconfig.c,v 1.130 2002/09/20 21:21:53 christos Exp $");
84 #endif
85 #endif /* not lint */
86
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
90
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_media.h>
94 #include <net/if_ether.h>
95 #include <net/if_ieee80211.h>
96 #include <net/if_vlanvar.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #ifdef INET6
100 #include <netinet6/nd6.h>
101 #endif
102 #include <arpa/inet.h>
103
104 #include <netatalk/at.h>
105
106 #define NSIP
107 #include <netns/ns.h>
108 #include <netns/ns_if.h>
109 #include <netdb.h>
110
111 #define EON
112 #include <netiso/iso.h>
113 #include <netiso/iso_var.h>
114 #include <sys/protosw.h>
115
116 #include <ctype.h>
117 #include <err.h>
118 #include <errno.h>
119 #include <stddef.h>
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <string.h>
123 #include <unistd.h>
124 #include <ifaddrs.h>
125 #include <util.h>
126
127 struct ifreq ifr, ridreq;
128 struct ifaliasreq addreq __attribute__((aligned(4)));
129 struct in_aliasreq in_addreq;
130 #ifdef INET6
131 struct in6_ifreq ifr6;
132 struct in6_ifreq in6_ridreq;
133 struct in6_aliasreq in6_addreq;
134 #endif
135 struct iso_ifreq iso_ridreq;
136 struct iso_aliasreq iso_addreq;
137 struct sockaddr_in netmask;
138 struct netrange at_nr; /* AppleTalk net range */
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;
150 #ifdef INET6
151 int Lflag;
152 #endif
153 int reset_if_flags;
154 int explicit_prefix = 0;
155 u_int vlan_tag = (u_int)-1;
156
157 struct ifcapreq g_ifcr;
158 int g_ifcr_updated;
159
160 void notealias __P((const char *, int));
161 void notrailers __P((const char *, int));
162 void setifaddr __P((const char *, int));
163 void setifdstaddr __P((const char *, int));
164 void setifflags __P((const char *, int));
165 void setifcaps __P((const char *, int));
166 void setifbroadaddr __P((const char *, int));
167 void setifipdst __P((const char *, int));
168 void setifmetric __P((const char *, int));
169 void setifmtu __P((const char *, int));
170 void setifnwid __P((const char *, int));
171 void setifnwkey __P((const char *, int));
172 void setifpowersave __P((const char *, int));
173 void setifpowersavesleep __P((const char *, int));
174 void setifnetmask __P((const char *, int));
175 void setifprefixlen __P((const char *, int));
176 void setnsellength __P((const char *, int));
177 void setsnpaoffset __P((const char *, int));
178 void setatrange __P((const char *, int));
179 void setatphase __P((const char *, int));
180 void settunnel __P((const char *, const char *));
181 void deletetunnel __P((const char *, int));
182 #ifdef INET6
183 void setia6flags __P((const char *, int));
184 void setia6pltime __P((const char *, int));
185 void setia6vltime __P((const char *, int));
186 void setia6lifetime __P((const char *, const char *));
187 void setia6eui64 __P((const char *, int));
188 #endif
189 void checkatrange __P ((struct sockaddr_at *));
190 void setmedia __P((const char *, int));
191 void setmediaopt __P((const char *, int));
192 void unsetmediaopt __P((const char *, int));
193 void setmediainst __P((const char *, int));
194 void clone_create __P((const char *, int));
195 void clone_destroy __P((const char *, int));
196 void fixnsel __P((struct sockaddr_iso *));
197 void setvlan __P((const char *, int));
198 void setvlanif __P((const char *, int));
199 void unsetvlanif __P((const char *, int));
200 int main __P((int, char *[]));
201
202 /*
203 * Media stuff. Whenever a media command is first performed, the
204 * currently select media is grabbed for this interface. If `media'
205 * is given, the current media word is modifed. `mediaopt' commands
206 * only modify the set and clear words. They then operate on the
207 * current media word later.
208 */
209 int media_current;
210 int mediaopt_set;
211 int mediaopt_clear;
212
213 int actions; /* Actions performed */
214
215 #define A_MEDIA 0x0001 /* media command */
216 #define A_MEDIAOPTSET 0x0002 /* mediaopt command */
217 #define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
218 #define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
219 #define A_MEDIAINST 0x0008 /* instance or inst command */
220
221 #define NEXTARG 0xffffff
222 #define NEXTARG2 0xfffffe
223
224 const struct cmd {
225 const char *c_name;
226 int c_parameter; /* NEXTARG means next argv */
227 int c_action; /* defered action */
228 void (*c_func) __P((const char *, int));
229 void (*c_func2) __P((const char *, const char *));
230 } cmds[] = {
231 { "up", IFF_UP, 0, setifflags } ,
232 { "down", -IFF_UP, 0, setifflags },
233 { "trailers", -1, 0, notrailers },
234 { "-trailers", 1, 0, notrailers },
235 { "arp", -IFF_NOARP, 0, setifflags },
236 { "-arp", IFF_NOARP, 0, setifflags },
237 { "debug", IFF_DEBUG, 0, setifflags },
238 { "-debug", -IFF_DEBUG, 0, setifflags },
239 { "alias", IFF_UP, 0, notealias },
240 { "-alias", -IFF_UP, 0, notealias },
241 { "delete", -IFF_UP, 0, notealias },
242 #ifdef notdef
243 #define EN_SWABIPS 0x1000
244 { "swabips", EN_SWABIPS, 0, setifflags },
245 { "-swabips", -EN_SWABIPS, 0, setifflags },
246 #endif
247 { "netmask", NEXTARG, 0, setifnetmask },
248 { "metric", NEXTARG, 0, setifmetric },
249 { "mtu", NEXTARG, 0, setifmtu },
250 { "nwid", NEXTARG, 0, setifnwid },
251 { "nwkey", NEXTARG, 0, setifnwkey },
252 { "-nwkey", -1, 0, setifnwkey },
253 { "powersave", 1, 0, setifpowersave },
254 { "-powersave", 0, 0, setifpowersave },
255 { "powersavesleep", NEXTARG, 0, setifpowersavesleep },
256 { "broadcast", NEXTARG, 0, setifbroadaddr },
257 { "ipdst", NEXTARG, 0, setifipdst },
258 { "prefixlen", NEXTARG, 0, setifprefixlen},
259 #ifdef INET6
260 { "anycast", IN6_IFF_ANYCAST, 0, setia6flags },
261 { "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags },
262 { "tentative", IN6_IFF_TENTATIVE, 0, setia6flags },
263 { "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags },
264 { "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
265 { "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
266 { "pltime", NEXTARG, 0, setia6pltime },
267 { "vltime", NEXTARG, 0, setia6vltime },
268 { "eui64", 0, 0, setia6eui64 },
269 #endif /*INET6*/
270 #ifndef INET_ONLY
271 { "range", NEXTARG, 0, setatrange },
272 { "phase", NEXTARG, 0, setatphase },
273 { "snpaoffset", NEXTARG, 0, setsnpaoffset },
274 { "nsellength", NEXTARG, 0, setnsellength },
275 #endif /* INET_ONLY */
276 { "tunnel", NEXTARG2, 0, NULL,
277 settunnel } ,
278 { "deletetunnel", 0, 0, deletetunnel },
279 { "vlan", NEXTARG, 0, setvlan } ,
280 { "vlanif", NEXTARG, 0, setvlanif } ,
281 { "-vlanif", 0, 0, unsetvlanif } ,
282 #if 0
283 /* XXX `create' special-cased below */
284 { "create", 0, 0, clone_create } ,
285 #endif
286 { "destroy", 0, 0, clone_destroy } ,
287 { "link0", IFF_LINK0, 0, setifflags } ,
288 { "-link0", -IFF_LINK0, 0, setifflags } ,
289 { "link1", IFF_LINK1, 0, setifflags } ,
290 { "-link1", -IFF_LINK1, 0, setifflags } ,
291 { "link2", IFF_LINK2, 0, setifflags } ,
292 { "-link2", -IFF_LINK2, 0, setifflags } ,
293 { "media", NEXTARG, A_MEDIA, setmedia },
294 { "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
295 { "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
296 { "instance", NEXTARG, A_MEDIAINST, setmediainst },
297 { "inst", NEXTARG, A_MEDIAINST, setmediainst },
298 { "ip4csum", IFCAP_CSUM_IPv4,0, setifcaps },
299 { "-ip4csum", -IFCAP_CSUM_IPv4,0, setifcaps },
300 { "tcp4csum", IFCAP_CSUM_TCPv4,0, setifcaps },
301 { "-tcp4csum", -IFCAP_CSUM_TCPv4,0, setifcaps },
302 { "udp4csum", IFCAP_CSUM_UDPv4,0, setifcaps },
303 { "-udp4csum", -IFCAP_CSUM_UDPv4,0, setifcaps },
304 { "tcp6csum", IFCAP_CSUM_TCPv6,0, setifcaps },
305 { "-tcp6csum", -IFCAP_CSUM_TCPv6,0, setifcaps },
306 { "udp6csum", IFCAP_CSUM_UDPv6,0, setifcaps },
307 { "-udp6csum", -IFCAP_CSUM_UDPv6,0, setifcaps },
308 { "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
309 { "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
310 { "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
311 { "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
312 { 0, 0, 0, setifaddr },
313 { 0, 0, 0, setifdstaddr },
314 };
315
316 void adjust_nsellength __P((void));
317 int getinfo __P((struct ifreq *));
318 int carrier __P((void));
319 void getsock __P((int));
320 void printall __P((const char *));
321 void list_cloners __P((void));
322 int prefix __P((void *, int));
323 void status __P((const struct sockaddr_dl *));
324 void usage __P((void));
325 const char *get_string __P((const char *, const char *, u_int8_t *, int *));
326 void print_string __P((const u_int8_t *, int));
327 char *sec2str __P((time_t));
328
329 const char *get_media_type_string __P((int));
330 const char *get_media_subtype_string __P((int));
331 int get_media_subtype __P((int, const char *));
332 int get_media_options __P((int, const char *));
333 int lookup_media_word __P((struct ifmedia_description *, int,
334 const char *));
335 void print_media_word __P((int, int, int));
336 void process_media_commands __P((void));
337 void init_current_media __P((void));
338
339 /*
340 * XNS support liberally adapted from code written at the University of
341 * Maryland principally by James O'Toole and Chris Torek.
342 */
343 void in_alias __P((struct ifreq *));
344 void in_status __P((int));
345 void in_getaddr __P((const char *, int));
346 void in_getprefix __P((const char *, int));
347 #ifdef INET6
348 void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
349 void in6_alias __P((struct in6_ifreq *));
350 void in6_status __P((int));
351 void in6_getaddr __P((const char *, int));
352 void in6_getprefix __P((const char *, int));
353 #endif
354 void at_status __P((int));
355 void at_getaddr __P((const char *, int));
356 void xns_status __P((int));
357 void xns_getaddr __P((const char *, int));
358 void iso_status __P((int));
359 void iso_getaddr __P((const char *, int));
360
361 void ieee80211_status __P((void));
362 void tunnel_status __P((void));
363 void vlan_status __P((void));
364
365 /* Known address families */
366 struct afswtch {
367 const char *af_name;
368 short af_af;
369 void (*af_status) __P((int));
370 void (*af_getaddr) __P((const char *, int));
371 void (*af_getprefix) __P((const char *, int));
372 u_long af_difaddr;
373 u_long af_aifaddr;
374 u_long af_gifaddr;
375 caddr_t af_ridreq;
376 caddr_t af_addreq;
377 } afs[] = {
378 #define C(x) ((caddr_t) &x)
379 { "inet", AF_INET, in_status, in_getaddr, in_getprefix,
380 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(in_addreq) },
381 #ifdef INET6
382 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
383 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
384 /*
385 * Deleting the first address before setting new one is
386 * not prefered way in this protocol.
387 */
388 0,
389 C(in6_ridreq), C(in6_addreq) },
390 #endif
391 #ifndef INET_ONLY /* small version, for boot media */
392 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
393 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(addreq), C(addreq) },
394 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
395 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
396 { "iso", AF_ISO, iso_status, iso_getaddr, NULL,
397 SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
398 C(iso_ridreq), C(iso_addreq) },
399 #endif /* INET_ONLY */
400 { 0, 0, 0, 0 }
401 };
402
403 struct afswtch *afp; /*the address family being set or asked about*/
404
405 struct afswtch *lookup_af __P((const char *));
406
407 int
408 main(argc, argv)
409 int argc;
410 char *argv[];
411 {
412 struct ifreq ifreq;
413 int ch;
414
415 /* Parse command-line options */
416 aflag = mflag = vflag = 0;
417 while ((ch = getopt(argc, argv, "AabCdlmsuv"
418 #ifdef INET6
419 "L"
420 #endif
421 )) != -1) {
422 switch (ch) {
423 case 'A':
424 warnx("-A is deprecated");
425 break;
426
427 case 'a':
428 aflag = 1;
429 break;
430
431 case 'b':
432 bflag = 1;
433 break;
434
435
436 case 'C':
437 Cflag = 1;
438 break;
439
440 case 'd':
441 dflag = 1;
442 break;
443
444 #ifdef INET6
445 case 'L':
446 Lflag = 1;
447 break;
448 #endif
449
450 case 'l':
451 lflag = 1;
452 break;
453
454 case 'm':
455 mflag = 1;
456 break;
457
458 case 's':
459 sflag = 1;
460 break;
461
462 case 'u':
463 uflag = 1;
464 break;
465
466 case 'v':
467 vflag = 1;
468 break;
469
470
471 default:
472 usage();
473 /* NOTREACHED */
474 }
475 }
476 argc -= optind;
477 argv += optind;
478
479 /*
480 * -l means "list all interfaces", and is mutally exclusive with
481 * all other flags/commands.
482 *
483 * -C means "list all names of cloners", and it mutually exclusive
484 * with all other flags/commands.
485 *
486 * -a means "print status of all interfaces".
487 */
488 if ((lflag || Cflag) && (aflag || mflag || vflag || argc))
489 usage();
490 #ifdef INET6
491 if ((lflag || Cflag) && Lflag)
492 usage();
493 #endif
494 if (lflag && Cflag)
495 usage();
496 if (Cflag) {
497 if (argc)
498 usage();
499 list_cloners();
500 exit(0);
501 }
502 if (aflag || lflag) {
503 if (argc > 1)
504 usage();
505 else if (argc == 1) {
506 afp = lookup_af(argv[0]);
507 if (afp == NULL)
508 usage();
509 }
510 if (afp)
511 af = ifr.ifr_addr.sa_family = afp->af_af;
512 else
513 af = ifr.ifr_addr.sa_family = afs[0].af_af;
514 printall(NULL);
515 exit(0);
516 }
517
518 /* Make sure there's an interface name. */
519 if (argc < 1)
520 usage();
521 (void) strncpy(name, argv[0], sizeof(name));
522 argc--; argv++;
523
524 /*
525 * NOTE: We must special-case the `create' command right
526 * here as we would otherwise fail in getinfo().
527 */
528 if (argc > 0 && strcmp(argv[0], "create") == 0) {
529 clone_create(argv[0], 0);
530 argc--, argv++;
531 if (argc == 0)
532 exit(0);
533 }
534
535 /* Check for address family. */
536 afp = NULL;
537 if (argc > 0) {
538 afp = lookup_af(argv[0]);
539 if (afp != NULL) {
540 argv++;
541 argc--;
542 }
543 }
544
545 /* Initialize af, just for use in getinfo(). */
546 if (afp == NULL)
547 af = afs->af_af;
548 else
549 af = afp->af_af;
550
551 /* Get information about the interface. */
552 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
553 if (getinfo(&ifr) < 0)
554 exit(1);
555
556 if (sflag) {
557 if (argc != 0)
558 usage();
559 else
560 exit(carrier());
561 }
562
563 /* No more arguments means interface status. */
564 if (argc == 0) {
565 printall(name);
566 exit(0);
567 }
568
569 /* The following operations assume inet family as the default. */
570 if (afp == NULL)
571 afp = afs;
572 af = ifr.ifr_addr.sa_family = afp->af_af;
573
574 #ifdef INET6
575 /* initialization */
576 in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
577 in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
578 #endif
579
580 /* Process commands. */
581 while (argc > 0) {
582 const struct cmd *p;
583
584 for (p = cmds; p->c_name; p++)
585 if (strcmp(argv[0], p->c_name) == 0)
586 break;
587 if (p->c_name == 0 && setaddr) {
588 if ((flags & IFF_POINTOPOINT) == 0) {
589 errx(EXIT_FAILURE,
590 "can't set destination address %s",
591 "on non-point-to-point link");
592 }
593 p++; /* got src, do dst */
594 }
595 if (p->c_func != NULL || p->c_func2 != NULL) {
596 if (p->c_parameter == NEXTARG) {
597 if (argc < 2)
598 errx(EXIT_FAILURE,
599 "'%s' requires argument",
600 p->c_name);
601 (*p->c_func)(argv[1], 0);
602 argc--, argv++;
603 } else if (p->c_parameter == NEXTARG2) {
604 if (argc < 3)
605 errx(EXIT_FAILURE,
606 "'%s' requires 2 arguments",
607 p->c_name);
608 (*p->c_func2)(argv[1], argv[2]);
609 argc -= 2, argv += 2;
610 } else
611 (*p->c_func)(argv[0], p->c_parameter);
612 actions |= p->c_action;
613 }
614 argc--, argv++;
615 }
616
617 /*
618 * See if multiple alias, -alias, or delete commands were
619 * specified. More than one constitutes an invalid command line
620 */
621
622 if (conflicting > 1)
623 err(EXIT_FAILURE,
624 "Only one use of alias, -alias or delete is valid.");
625
626 /* Process any media commands that may have been issued. */
627 process_media_commands();
628
629 if (af == AF_INET6 && explicit_prefix == 0) {
630 /*
631 * Aggregatable address architecture defines all prefixes
632 * are 64. So, it is convenient to set prefixlen to 64 if
633 * it is not specified.
634 */
635 setifprefixlen("64", 0);
636 /* in6_getprefix("64", MASK) if MASK is available here... */
637 }
638
639 #ifndef INET_ONLY
640 if (af == AF_ISO)
641 adjust_nsellength();
642
643 if (af == AF_APPLETALK)
644 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
645
646 if (setipdst && af==AF_NS) {
647 struct nsip_req rq;
648 int size = sizeof(rq);
649
650 rq.rq_ns = addreq.ifra_addr;
651 rq.rq_ip = addreq.ifra_dstaddr;
652
653 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
654 warn("encapsulation routing");
655 }
656
657 #endif /* INET_ONLY */
658
659 if (clearaddr) {
660 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
661 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
662 err(EXIT_FAILURE, "SIOCDIFADDR");
663 }
664 if (newaddr > 0) {
665 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
666 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
667 warn("SIOCAIFADDR");
668 }
669
670 if (g_ifcr_updated) {
671 (void) strncpy(g_ifcr.ifcr_name, name,
672 sizeof(g_ifcr.ifcr_name));
673 if (ioctl(s, SIOCSIFCAP, (caddr_t) &g_ifcr) == -1)
674 err(EXIT_FAILURE, "SIOCSIFCAP");
675 }
676
677 if (reset_if_flags) {
678 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
679 ifreq.ifr_flags = flags;
680 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
681 err(EXIT_FAILURE, "SIOCSIFFLAGS");
682 }
683 exit(0);
684 }
685
686 struct afswtch *
687 lookup_af(cp)
688 const char *cp;
689 {
690 struct afswtch *a;
691
692 for (a = afs; a->af_name != NULL; a++)
693 if (strcmp(a->af_name, cp) == 0)
694 return (a);
695 return (NULL);
696 }
697
698 void
699 getsock(naf)
700 int naf;
701 {
702 static int oaf = -1;
703
704 if (oaf == naf)
705 return;
706 if (oaf != -1)
707 close(s);
708 s = socket(naf, SOCK_DGRAM, 0);
709 if (s < 0)
710 oaf = -1;
711 else
712 oaf = naf;
713 }
714
715 int
716 getinfo(giifr)
717 struct ifreq *giifr;
718 {
719
720 getsock(af);
721 if (s < 0)
722 err(EXIT_FAILURE, "socket");
723 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)giifr) == -1) {
724 warn("SIOCGIFFLAGS %s", giifr->ifr_name);
725 return (-1);
726 }
727 flags = giifr->ifr_flags;
728 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)giifr) == -1) {
729 warn("SIOCGIFMETRIC %s", giifr->ifr_name);
730 metric = 0;
731 } else
732 metric = giifr->ifr_metric;
733 if (ioctl(s, SIOCGIFMTU, (caddr_t)giifr) == -1)
734 mtu = 0;
735 else
736 mtu = giifr->ifr_mtu;
737
738 memset(&g_ifcr, 0, sizeof(g_ifcr));
739 strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
740 (void) ioctl(s, SIOCGIFCAP, (caddr_t) &g_ifcr);
741
742 return (0);
743 }
744
745 void
746 printall(ifname)
747 const char *ifname;
748 {
749 struct ifaddrs *ifap, *ifa;
750 struct ifreq paifr;
751 const struct sockaddr_dl *sdl = NULL;
752 int idx;
753 char *p;
754
755 if (getifaddrs(&ifap) != 0)
756 err(EXIT_FAILURE, "getifaddrs");
757 p = NULL;
758 idx = 0;
759 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
760 memset(&paifr, 0, sizeof(paifr));
761 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
762 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
763 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
764 ifa->ifa_addr->sa_len);
765 }
766
767 if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
768 continue;
769 if (ifa->ifa_addr->sa_family == AF_LINK)
770 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
771 if (p && strcmp(p, ifa->ifa_name) == 0)
772 continue;
773 (void) strncpy(name, ifa->ifa_name, sizeof(name));
774 name[sizeof(name) - 1] = '\0';
775 p = ifa->ifa_name;
776
777 if (getinfo(&paifr) < 0)
778 continue;
779 if (bflag && (ifa->ifa_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
780 continue;
781 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
782 continue;
783 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
784 continue;
785
786 if (sflag && carrier())
787 continue;
788 idx++;
789 /*
790 * Are we just listing the interfaces?
791 */
792 if (lflag) {
793 if (idx > 1)
794 putchar(' ');
795 fputs(name, stdout);
796 continue;
797 }
798
799 status(sdl);
800 sdl = NULL;
801 }
802 if (lflag)
803 putchar('\n');
804 freeifaddrs(ifap);
805 }
806
807 void
808 list_cloners(void)
809 {
810 struct if_clonereq ifcr;
811 char *cp, *buf;
812 int idx;
813
814 memset(&ifcr, 0, sizeof(ifcr));
815
816 getsock(AF_INET);
817
818 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
819 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
820
821 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
822 if (buf == NULL)
823 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
824
825 ifcr.ifcr_count = ifcr.ifcr_total;
826 ifcr.ifcr_buffer = buf;
827
828 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
829 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
830
831 /*
832 * In case some disappeared in the mean time, clamp it down.
833 */
834 if (ifcr.ifcr_count > ifcr.ifcr_total)
835 ifcr.ifcr_count = ifcr.ifcr_total;
836
837 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
838 if (idx > 0)
839 putchar(' ');
840 printf("%s", cp);
841 }
842
843 putchar('\n');
844 free(buf);
845 return;
846 }
847
848 /*ARGSUSED*/
849 void
850 clone_create(addr, param)
851 const char *addr;
852 int param;
853 {
854
855 /* We're called early... */
856 getsock(AF_INET);
857
858 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
859 if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
860 err(EXIT_FAILURE, "SIOCIFCREATE");
861 }
862
863 /*ARGSUSED*/
864 void
865 clone_destroy(addr, param)
866 const char *addr;
867 int param;
868 {
869
870 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
871 if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
872 err(EXIT_FAILURE, "SIOCIFDESTROY");
873 }
874
875 #define RIDADDR 0
876 #define ADDR 1
877 #define MASK 2
878 #define DSTADDR 3
879
880 /*ARGSUSED*/
881 void
882 setifaddr(addr, param)
883 const char *addr;
884 int param;
885 {
886 struct ifreq *siifr; /* XXX */
887
888 /*
889 * Delay the ioctl to set the interface addr until flags are all set.
890 * The address interpretation may depend on the flags,
891 * and the flags may change when the address is set.
892 */
893 setaddr++;
894 if (newaddr == -1)
895 newaddr = 1;
896 if (doalias == 0 && afp->af_gifaddr != 0) {
897 siifr = (struct ifreq *)afp->af_ridreq;
898 (void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
899 siifr->ifr_addr.sa_family = afp->af_af;
900 if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
901 clearaddr = 1;
902 else if (errno == EADDRNOTAVAIL)
903 /* No address was assigned yet. */
904 ;
905 else
906 err(EXIT_FAILURE, "SIOCGIFADDR");
907 }
908
909 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
910 }
911
912 void
913 settunnel(src, dst)
914 const char *src, *dst;
915 {
916 struct addrinfo hints, *srcres, *dstres;
917 int ecode;
918 struct if_laddrreq req;
919
920 memset(&hints, 0, sizeof(hints));
921 hints.ai_family = afp->af_af;
922 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
923
924 if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
925 errx(EXIT_FAILURE, "error in parsing address string: %s",
926 gai_strerror(ecode));
927
928 if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
929 errx(EXIT_FAILURE, "error in parsing address string: %s",
930 gai_strerror(ecode));
931
932 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
933 errx(EXIT_FAILURE,
934 "source and destination address families do not match");
935
936 if (srcres->ai_addrlen > sizeof(req.addr) ||
937 dstres->ai_addrlen > sizeof(req.dstaddr))
938 errx(EXIT_FAILURE, "invalid sockaddr");
939
940 memset(&req, 0, sizeof(req));
941 strncpy(req.iflr_name, name, sizeof(req.iflr_name));
942 memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
943 memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
944
945 #ifdef INET6
946 if (req.addr.ss_family == AF_INET6) {
947 struct sockaddr_in6 *s6, *d;
948
949 s6 = (struct sockaddr_in6 *)&req.addr;
950 d = (struct sockaddr_in6 *)&req.dstaddr;
951 if (s6->sin6_scope_id != d->sin6_scope_id) {
952 errx(EXIT_FAILURE, "scope mismatch");
953 /* NOTREACHED */
954 }
955 #ifdef __KAME__
956 /* embed scopeid */
957 if (s6->sin6_scope_id &&
958 (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
959 IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
960 *(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
961 htons(s6->sin6_scope_id);
962 }
963 if (d->sin6_scope_id &&
964 (IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
965 IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
966 *(u_int16_t *)&d->sin6_addr.s6_addr[2] =
967 htons(d->sin6_scope_id);
968 }
969 #endif
970 }
971 #endif
972
973 if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
974 warn("SIOCSLIFPHYADDR");
975
976 freeaddrinfo(srcres);
977 freeaddrinfo(dstres);
978 }
979
980 /* ARGSUSED */
981 void
982 deletetunnel(vname, param)
983 const char *vname;
984 int param;
985 {
986
987 if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
988 err(EXIT_FAILURE, "SIOCDIFPHYADDR");
989 }
990
991 void setvlan(val, d)
992 const char *val;
993 int d;
994 {
995 struct vlanreq vlr;
996
997 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
998 !isdigit(ifr.ifr_name[4]))
999 errx(EXIT_FAILURE,
1000 "``vlan'' valid only with vlan(4) interfaces");
1001
1002 vlan_tag = atoi(val);
1003
1004 memset(&vlr, 0, sizeof(vlr));
1005 ifr.ifr_data = (caddr_t)&vlr;
1006
1007 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1008 err(EXIT_FAILURE, "SIOCGETVLAN");
1009
1010 vlr.vlr_tag = vlan_tag;
1011
1012 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1013 err(EXIT_FAILURE, "SIOCSETVLAN");
1014 }
1015
1016 void setvlanif(val, d)
1017 const char *val;
1018 int d;
1019 {
1020 struct vlanreq vlr;
1021
1022 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
1023 !isdigit(ifr.ifr_name[4]))
1024 errx(EXIT_FAILURE,
1025 "``vlanif'' valid only with vlan(4) interfaces");
1026
1027 if (vlan_tag == (u_int)-1)
1028 errx(EXIT_FAILURE,
1029 "must specify both ``vlan'' and ``vlanif''");
1030
1031 memset(&vlr, 0, sizeof(vlr));
1032 ifr.ifr_data = (caddr_t)&vlr;
1033
1034 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1035 err(EXIT_FAILURE, "SIOCGETVLAN");
1036
1037 strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
1038 vlr.vlr_tag = vlan_tag;
1039
1040 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1041 err(EXIT_FAILURE, "SIOCSETVLAN");
1042 }
1043
1044 void unsetvlanif(val, d)
1045 const char *val;
1046 int d;
1047 {
1048 struct vlanreq vlr;
1049
1050 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
1051 !isdigit(ifr.ifr_name[4]))
1052 errx(EXIT_FAILURE,
1053 "``vlanif'' valid only with vlan(4) interfaces");
1054
1055 memset(&vlr, 0, sizeof(vlr));
1056 ifr.ifr_data = (caddr_t)&vlr;
1057
1058 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1059 err(EXIT_FAILURE, "SIOCGETVLAN");
1060
1061 vlr.vlr_parent[0] = '\0';
1062 vlr.vlr_tag = 0;
1063
1064 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1065 err(EXIT_FAILURE, "SIOCSETVLAN");
1066 }
1067
1068 void
1069 setifnetmask(addr, d)
1070 const char *addr;
1071 int d;
1072 {
1073 (*afp->af_getaddr)(addr, MASK);
1074 }
1075
1076 void
1077 setifbroadaddr(addr, d)
1078 const char *addr;
1079 int d;
1080 {
1081 (*afp->af_getaddr)(addr, DSTADDR);
1082 }
1083
1084 void
1085 setifipdst(addr, d)
1086 const char *addr;
1087 int d;
1088 {
1089 in_getaddr(addr, DSTADDR);
1090 setipdst++;
1091 clearaddr = 0;
1092 newaddr = 0;
1093 }
1094
1095 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1096 /*ARGSUSED*/
1097 void
1098 notealias(addr, param)
1099 const char *addr;
1100 int param;
1101 {
1102 if (setaddr && doalias == 0 && param < 0)
1103 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
1104 rqtosa(af_addreq)->sa_len);
1105 doalias = param;
1106 if (param < 0) {
1107 clearaddr = 1;
1108 newaddr = 0;
1109 conflicting++;
1110 } else {
1111 clearaddr = 0;
1112 conflicting++;
1113 }
1114 }
1115
1116 /*ARGSUSED*/
1117 void
1118 notrailers(vname, value)
1119 const char *vname;
1120 int value;
1121 {
1122 puts("Note: trailers are no longer sent, but always received");
1123 }
1124
1125 /*ARGSUSED*/
1126 void
1127 setifdstaddr(addr, param)
1128 const char *addr;
1129 int param;
1130 {
1131 (*afp->af_getaddr)(addr, DSTADDR);
1132 }
1133
1134 void
1135 setifflags(vname, value)
1136 const char *vname;
1137 int value;
1138 {
1139 struct ifreq ifreq;
1140
1141 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
1142 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) == -1)
1143 err(EXIT_FAILURE, "SIOCGIFFLAGS");
1144 flags = ifreq.ifr_flags;
1145
1146 if (value < 0) {
1147 value = -value;
1148 flags &= ~value;
1149 } else
1150 flags |= value;
1151 ifreq.ifr_flags = flags;
1152 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
1153 err(EXIT_FAILURE, "SIOCSIFFLAGS");
1154
1155 reset_if_flags = 1;
1156 }
1157
1158 void
1159 setifcaps(vname, value)
1160 const char *vname;
1161 int value;
1162 {
1163
1164 if (value < 0) {
1165 value = -value;
1166 g_ifcr.ifcr_capenable &= ~value;
1167 } else
1168 g_ifcr.ifcr_capenable |= value;
1169
1170 g_ifcr_updated = 1;
1171 }
1172
1173 #ifdef INET6
1174 void
1175 setia6flags(vname, value)
1176 const char *vname;
1177 int value;
1178 {
1179
1180 if (value < 0) {
1181 value = -value;
1182 in6_addreq.ifra_flags &= ~value;
1183 } else
1184 in6_addreq.ifra_flags |= value;
1185 }
1186
1187 void
1188 setia6pltime(val, d)
1189 const char *val;
1190 int d;
1191 {
1192
1193 setia6lifetime("pltime", val);
1194 }
1195
1196 void
1197 setia6vltime(val, d)
1198 const char *val;
1199 int d;
1200 {
1201
1202 setia6lifetime("vltime", val);
1203 }
1204
1205 void
1206 setia6lifetime(cmd, val)
1207 const char *cmd;
1208 const char *val;
1209 {
1210 time_t newval, t;
1211 char *ep;
1212
1213 t = time(NULL);
1214 newval = (time_t)strtoul(val, &ep, 0);
1215 if (val == ep)
1216 errx(EXIT_FAILURE, "invalid %s", cmd);
1217 if (afp->af_af != AF_INET6)
1218 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1219 if (strcmp(cmd, "vltime") == 0) {
1220 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
1221 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
1222 } else if (strcmp(cmd, "pltime") == 0) {
1223 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
1224 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
1225 }
1226 }
1227
1228 void
1229 setia6eui64(cmd, val)
1230 const char *cmd;
1231 int val;
1232 {
1233 struct ifaddrs *ifap, *ifa;
1234 const struct sockaddr_in6 *sin6 = NULL;
1235 const struct in6_addr *lladdr = NULL;
1236 struct in6_addr *in6;
1237
1238 if (afp->af_af != AF_INET6)
1239 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1240 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1241 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1242 errx(EXIT_FAILURE, "interface index is already filled");
1243 if (getifaddrs(&ifap) != 0)
1244 err(EXIT_FAILURE, "getifaddrs");
1245 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1246 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1247 strcmp(ifa->ifa_name, name) == 0) {
1248 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1249 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1250 lladdr = &sin6->sin6_addr;
1251 break;
1252 }
1253 }
1254 }
1255 if (!lladdr)
1256 errx(EXIT_FAILURE, "could not determine link local address");
1257
1258 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
1259
1260 freeifaddrs(ifap);
1261 }
1262 #endif
1263
1264 void
1265 setifmetric(val, d)
1266 const char *val;
1267 int d;
1268 {
1269 char *ep = NULL;
1270
1271 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1272 ifr.ifr_metric = strtoul(val, &ep, 10);
1273 if (!ep || *ep)
1274 errx(EXIT_FAILURE, "%s: invalid metric", val);
1275 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) == -1)
1276 warn("SIOCSIFMETRIC");
1277 }
1278
1279 void
1280 setifmtu(val, d)
1281 const char *val;
1282 int d;
1283 {
1284 char *ep = NULL;
1285
1286 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1287 ifr.ifr_mtu = strtoul(val, &ep, 10);
1288 if (!ep || *ep)
1289 errx(EXIT_FAILURE, "%s: invalid mtu", val);
1290 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) == -1)
1291 warn("SIOCSIFMTU");
1292 }
1293
1294 const char *
1295 get_string(val, sep, buf, lenp)
1296 const char *val, *sep;
1297 u_int8_t *buf;
1298 int *lenp;
1299 {
1300 int len;
1301 int hexstr;
1302 u_int8_t *p;
1303
1304 len = *lenp;
1305 p = buf;
1306 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1307 if (hexstr)
1308 val += 2;
1309 for (;;) {
1310 if (*val == '\0')
1311 break;
1312 if (sep != NULL && strchr(sep, *val) != NULL) {
1313 val++;
1314 break;
1315 }
1316 if (hexstr) {
1317 if (!isxdigit((u_char)val[0]) ||
1318 !isxdigit((u_char)val[1])) {
1319 warnx("bad hexadecimal digits");
1320 return NULL;
1321 }
1322 }
1323 if (p > buf + len) {
1324 if (hexstr)
1325 warnx("hexadecimal digits too long");
1326 else
1327 warnx("strings too long");
1328 return NULL;
1329 }
1330 if (hexstr) {
1331 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1332 *p++ = (tohex((u_char)val[0]) << 4) |
1333 tohex((u_char)val[1]);
1334 #undef tohex
1335 val += 2;
1336 } else
1337 *p++ = *val++;
1338 }
1339 len = p - buf;
1340 if (len < *lenp)
1341 memset(p, 0, *lenp - len);
1342 *lenp = len;
1343 return val;
1344 }
1345
1346 void
1347 print_string(buf, len)
1348 const u_int8_t *buf;
1349 int len;
1350 {
1351 int i;
1352 int hasspc;
1353
1354 i = 0;
1355 hasspc = 0;
1356 if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
1357 for (; i < len; i++) {
1358 if (!isprint(buf[i]))
1359 break;
1360 if (isspace(buf[i]))
1361 hasspc++;
1362 }
1363 }
1364 if (i == len) {
1365 if (hasspc || len == 0)
1366 printf("\"%.*s\"", len, buf);
1367 else
1368 printf("%.*s", len, buf);
1369 } else {
1370 printf("0x");
1371 for (i = 0; i < len; i++)
1372 printf("%02x", buf[i]);
1373 }
1374 }
1375
1376 void
1377 setifnwid(val, d)
1378 const char *val;
1379 int d;
1380 {
1381 struct ieee80211_nwid nwid;
1382 int len;
1383
1384 len = sizeof(nwid.i_nwid);
1385 if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
1386 return;
1387 nwid.i_len = len;
1388 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1389 ifr.ifr_data = (caddr_t)&nwid;
1390 if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) == -1)
1391 warn("SIOCS80211NWID");
1392 }
1393
1394 void
1395 setifnwkey(val, d)
1396 const char *val;
1397 int d;
1398 {
1399 struct ieee80211_nwkey nwkey;
1400 int i;
1401 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
1402
1403 nwkey.i_wepon = IEEE80211_NWKEY_WEP;
1404 nwkey.i_defkid = 1;
1405 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1406 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
1407 nwkey.i_key[i].i_keydat = keybuf[i];
1408 }
1409 if (d != 0) {
1410 /* disable WEP encryption */
1411 nwkey.i_wepon = 0;
1412 i = 0;
1413 } else if (strcasecmp("persist", val) == 0) {
1414 /* use all values from persistent memory */
1415 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
1416 nwkey.i_defkid = 0;
1417 for (i = 0; i < IEEE80211_WEP_NKID; i++)
1418 nwkey.i_key[i].i_keylen = -1;
1419 } else if (strncasecmp("persist:", val, 8) == 0) {
1420 val += 8;
1421 /* program keys in persistent memory */
1422 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
1423 goto set_nwkey;
1424 } else {
1425 set_nwkey:
1426 if (isdigit(val[0]) && val[1] == ':') {
1427 /* specifying a full set of four keys */
1428 nwkey.i_defkid = val[0] - '0';
1429 val += 2;
1430 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1431 val = get_string(val, ",", keybuf[i],
1432 &nwkey.i_key[i].i_keylen);
1433 if (val == NULL)
1434 return;
1435 }
1436 if (*val != '\0') {
1437 warnx("SIOCS80211NWKEY: too many keys.");
1438 return;
1439 }
1440 } else {
1441 val = get_string(val, NULL, keybuf[0],
1442 &nwkey.i_key[0].i_keylen);
1443 if (val == NULL)
1444 return;
1445 i = 1;
1446 }
1447 }
1448 for (; i < IEEE80211_WEP_NKID; i++)
1449 nwkey.i_key[i].i_keylen = 0;
1450 (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
1451 if (ioctl(s, SIOCS80211NWKEY, (caddr_t)&nwkey) == -1)
1452 warn("SIOCS80211NWKEY");
1453 }
1454
1455 void
1456 setifpowersave(val, d)
1457 const char *val;
1458 int d;
1459 {
1460 struct ieee80211_power power;
1461
1462 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1463 if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) == -1) {
1464 warn("SIOCG80211POWER");
1465 return;
1466 }
1467
1468 power.i_enabled = d;
1469 if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) == -1)
1470 warn("SIOCS80211POWER");
1471 }
1472
1473 void
1474 setifpowersavesleep(val, d)
1475 const char *val;
1476 int d;
1477 {
1478 struct ieee80211_power power;
1479
1480 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1481 if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) == -1) {
1482 warn("SIOCG80211POWER");
1483 return;
1484 }
1485
1486 power.i_maxsleep = atoi(val);
1487 if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) == -1)
1488 warn("SIOCS80211POWER");
1489 }
1490
1491 void
1492 ieee80211_status()
1493 {
1494 int i, nwkey_verbose;
1495 struct ieee80211_nwid nwid;
1496 struct ieee80211_nwkey nwkey;
1497 struct ieee80211_power power;
1498 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
1499
1500 memset(&ifr, 0, sizeof(ifr));
1501 ifr.ifr_data = (caddr_t)&nwid;
1502 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1503 if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == -1)
1504 return;
1505 if (nwid.i_len > IEEE80211_NWID_LEN) {
1506 warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
1507 return;
1508 }
1509 printf("\tnwid ");
1510 print_string(nwid.i_nwid, nwid.i_len);
1511 memset(&nwkey, 0, sizeof(nwkey));
1512 (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
1513 /* show nwkey only when WEP is enabled */
1514 if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) == -1 ||
1515 nwkey.i_wepon == 0) {
1516 printf("\n");
1517 goto skip_wep;
1518 }
1519
1520 printf(" nwkey ");
1521 /* try to retrieve WEP keys */
1522 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1523 nwkey.i_key[i].i_keydat = keybuf[i];
1524 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
1525 }
1526 if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) == -1) {
1527 printf("*****");
1528 } else {
1529 nwkey_verbose = 0;
1530 /* check to see non default key or multiple keys defined */
1531 if (nwkey.i_defkid != 1) {
1532 nwkey_verbose = 1;
1533 } else {
1534 for (i = 1; i < IEEE80211_WEP_NKID; i++) {
1535 if (nwkey.i_key[i].i_keylen != 0) {
1536 nwkey_verbose = 1;
1537 break;
1538 }
1539 }
1540 }
1541 /* check extra ambiguity with keywords */
1542 if (!nwkey_verbose) {
1543 if (nwkey.i_key[0].i_keylen >= 2 &&
1544 isdigit(nwkey.i_key[0].i_keydat[0]) &&
1545 nwkey.i_key[0].i_keydat[1] == ':')
1546 nwkey_verbose = 1;
1547 else if (nwkey.i_key[0].i_keylen >= 7 &&
1548 strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
1549 == 0)
1550 nwkey_verbose = 1;
1551 }
1552 if (nwkey_verbose)
1553 printf("%d:", nwkey.i_defkid);
1554 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1555 if (i > 0)
1556 printf(",");
1557 if (nwkey.i_key[i].i_keylen < 0)
1558 printf("persist");
1559 else
1560 print_string(nwkey.i_key[i].i_keydat,
1561 nwkey.i_key[i].i_keylen);
1562 if (!nwkey_verbose)
1563 break;
1564 }
1565 }
1566 printf("\n");
1567
1568 skip_wep:
1569 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1570 if (ioctl(s, SIOCG80211POWER, &power) == -1)
1571 return;
1572 printf("\tpowersave ");
1573 if (power.i_enabled)
1574 printf("on (%dms sleep)", power.i_maxsleep);
1575 else
1576 printf("off");
1577 printf("\n");
1578 }
1579
1580 void
1581 init_current_media()
1582 {
1583 struct ifmediareq ifmr;
1584
1585 /*
1586 * If we have not yet done so, grab the currently-selected
1587 * media.
1588 */
1589 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
1590 (void) memset(&ifmr, 0, sizeof(ifmr));
1591 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1592
1593 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1594 /*
1595 * If we get E2BIG, the kernel is telling us
1596 * that there are more, so we can ignore it.
1597 */
1598 if (errno != E2BIG)
1599 err(EXIT_FAILURE, "SGIOCGIFMEDIA");
1600 }
1601
1602 media_current = ifmr.ifm_current;
1603 }
1604
1605 /* Sanity. */
1606 if (IFM_TYPE(media_current) == 0)
1607 errx(EXIT_FAILURE, "%s: no link type?", name);
1608 }
1609
1610 void
1611 process_media_commands()
1612 {
1613
1614 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
1615 /* Nothing to do. */
1616 return;
1617 }
1618
1619 /*
1620 * Media already set up, and commands sanity-checked. Set/clear
1621 * any options, and we're ready to go.
1622 */
1623 media_current |= mediaopt_set;
1624 media_current &= ~mediaopt_clear;
1625
1626 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1627 ifr.ifr_media = media_current;
1628
1629 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) == -1)
1630 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1631 }
1632
1633 void
1634 setmedia(val, d)
1635 const char *val;
1636 int d;
1637 {
1638 int type, subtype, inst;
1639
1640 init_current_media();
1641
1642 /* Only one media command may be given. */
1643 if (actions & A_MEDIA)
1644 errx(EXIT_FAILURE, "only one `media' command may be issued");
1645
1646 /* Must not come after mediaopt commands */
1647 if (actions & A_MEDIAOPT)
1648 errx(EXIT_FAILURE,
1649 "may not issue `media' after `mediaopt' commands");
1650
1651 /*
1652 * No need to check if `instance' has been issued; setmediainst()
1653 * craps out if `media' has not been specified.
1654 */
1655
1656 type = IFM_TYPE(media_current);
1657 inst = IFM_INST(media_current);
1658
1659 /* Look up the subtype. */
1660 subtype = get_media_subtype(type, val);
1661
1662 /* Build the new current media word. */
1663 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1664
1665 /* Media will be set after other processing is complete. */
1666 }
1667
1668 void
1669 setmediaopt(val, d)
1670 const char *val;
1671 int d;
1672 {
1673
1674 init_current_media();
1675
1676 /* Can only issue `mediaopt' once. */
1677 if (actions & A_MEDIAOPTSET)
1678 errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
1679
1680 /* Can't issue `mediaopt' if `instance' has already been issued. */
1681 if (actions & A_MEDIAINST)
1682 errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
1683
1684 mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
1685
1686 /* Media will be set after other processing is complete. */
1687 }
1688
1689 void
1690 unsetmediaopt(val, d)
1691 const char *val;
1692 int d;
1693 {
1694
1695 init_current_media();
1696
1697 /* Can only issue `-mediaopt' once. */
1698 if (actions & A_MEDIAOPTCLR)
1699 errx(EXIT_FAILURE,
1700 "only one `-mediaopt' command may be issued");
1701
1702 /* May not issue `media' and `-mediaopt'. */
1703 if (actions & A_MEDIA)
1704 errx(EXIT_FAILURE,
1705 "may not issue both `media' and `-mediaopt'");
1706
1707 /*
1708 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1709 * implicitly checks for A_MEDIAINST.
1710 */
1711
1712 mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
1713
1714 /* Media will be set after other processing is complete. */
1715 }
1716
1717 void
1718 setmediainst(val, d)
1719 const char *val;
1720 int d;
1721 {
1722 int type, subtype, options, inst;
1723
1724 init_current_media();
1725
1726 /* Can only issue `instance' once. */
1727 if (actions & A_MEDIAINST)
1728 errx(EXIT_FAILURE, "only one `instance' command may be issued");
1729
1730 /* Must have already specified `media' */
1731 if ((actions & A_MEDIA) == 0)
1732 errx(EXIT_FAILURE, "must specify `media' before `instance'");
1733
1734 type = IFM_TYPE(media_current);
1735 subtype = IFM_SUBTYPE(media_current);
1736 options = IFM_OPTIONS(media_current);
1737
1738 inst = atoi(val);
1739 if (inst < 0 || inst > IFM_INST_MAX)
1740 errx(EXIT_FAILURE, "invalid media instance: %s", val);
1741
1742 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1743
1744 /* Media will be set after other processing is complete. */
1745 }
1746
1747 struct ifmedia_description ifm_type_descriptions[] =
1748 IFM_TYPE_DESCRIPTIONS;
1749
1750 struct ifmedia_description ifm_subtype_descriptions[] =
1751 IFM_SUBTYPE_DESCRIPTIONS;
1752
1753 struct ifmedia_description ifm_option_descriptions[] =
1754 IFM_OPTION_DESCRIPTIONS;
1755
1756 const char *
1757 get_media_type_string(mword)
1758 int mword;
1759 {
1760 struct ifmedia_description *desc;
1761
1762 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
1763 desc++) {
1764 if (IFM_TYPE(mword) == desc->ifmt_word)
1765 return (desc->ifmt_string);
1766 }
1767 return ("<unknown type>");
1768 }
1769
1770 const char *
1771 get_media_subtype_string(mword)
1772 int mword;
1773 {
1774 struct ifmedia_description *desc;
1775
1776 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
1777 desc++) {
1778 if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
1779 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
1780 return (desc->ifmt_string);
1781 }
1782 return ("<unknown subtype>");
1783 }
1784
1785 int
1786 get_media_subtype(type, val)
1787 int type;
1788 const char *val;
1789 {
1790 int rval;
1791
1792 rval = lookup_media_word(ifm_subtype_descriptions, type, val);
1793 if (rval == -1)
1794 errx(EXIT_FAILURE, "unknown %s media subtype: %s",
1795 get_media_type_string(type), val);
1796
1797 return (rval);
1798 }
1799
1800 int
1801 get_media_options(type, val)
1802 int type;
1803 const char *val;
1804 {
1805 char *optlist, *str;
1806 int option, rval = 0;
1807
1808 /* We muck with the string, so copy it. */
1809 optlist = strdup(val);
1810 if (optlist == NULL)
1811 err(EXIT_FAILURE, "strdup");
1812 str = optlist;
1813
1814 /*
1815 * Look up the options in the user-provided comma-separated list.
1816 */
1817 for (; (str = strtok(str, ",")) != NULL; str = NULL) {
1818 option = lookup_media_word(ifm_option_descriptions, type, str);
1819 if (option == -1)
1820 errx(EXIT_FAILURE, "unknown %s media option: %s",
1821 get_media_type_string(type), str);
1822 rval |= IFM_OPTIONS(option);
1823 }
1824
1825 free(optlist);
1826 return (rval);
1827 }
1828
1829 int
1830 lookup_media_word(desc, type, val)
1831 struct ifmedia_description *desc;
1832 int type;
1833 const char *val;
1834 {
1835
1836 for (; desc->ifmt_string != NULL; desc++) {
1837 if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
1838 strcasecmp(desc->ifmt_string, val) == 0)
1839 return (desc->ifmt_word);
1840 }
1841 return (-1);
1842 }
1843
1844 void
1845 print_media_word(ifmw, print_type, as_syntax)
1846 int ifmw, print_type, as_syntax;
1847 {
1848 struct ifmedia_description *desc;
1849 int seen_option = 0;
1850
1851 if (print_type)
1852 printf("%s ", get_media_type_string(ifmw));
1853 printf("%s%s", as_syntax ? "media " : "",
1854 get_media_subtype_string(ifmw));
1855
1856 /* Find options. */
1857 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
1858 desc++) {
1859 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
1860 (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 &&
1861 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
1862 if (seen_option == 0)
1863 printf(" %s", as_syntax ? "mediaopt " : "");
1864 printf("%s%s", seen_option ? "," : "",
1865 desc->ifmt_string);
1866 seen_option |= IFM_OPTIONS(desc->ifmt_word);
1867 }
1868 }
1869 if (IFM_INST(ifmw) != 0)
1870 printf(" instance %d", IFM_INST(ifmw));
1871 }
1872
1873 int carrier()
1874 {
1875 struct ifmediareq ifmr;
1876
1877 (void) memset(&ifmr, 0, sizeof(ifmr));
1878 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1879
1880 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1881 /*
1882 * Interface doesn't support SIOC{G,S}IFMEDIA;
1883 * assume ok.
1884 */
1885 return 0;
1886 }
1887 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1888 /*
1889 * Interface doesn't report media-valid status.
1890 * assume ok.
1891 */
1892 return 0;
1893 }
1894 /* otherwise, return ok for active, not-ok if not active. */
1895 return !(ifmr.ifm_status & IFM_ACTIVE);
1896 }
1897
1898
1899 #define IFFBITS \
1900 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
1901 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
1902
1903 #define IFCAPBITS \
1904 "\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx"
1905
1906 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1907
1908 const struct ifmedia_status_description ifm_status_descriptions[] =
1909 IFM_STATUS_DESCRIPTIONS;
1910
1911 /*
1912 * Print the status of the interface. If an address family was
1913 * specified, show it and it only; otherwise, show them all.
1914 */
1915 void
1916 status(sdl)
1917 const struct sockaddr_dl *sdl;
1918 {
1919 struct afswtch *p = afp;
1920 struct ifmediareq ifmr;
1921 struct ifdatareq ifdr;
1922 int *media_list, i;
1923 char hbuf[NI_MAXHOST];
1924 char fbuf[BUFSIZ];
1925
1926 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1927 printf("%s: flags=%s", name, &fbuf[2]);
1928 if (metric)
1929 printf(" metric %lu", metric);
1930 if (mtu)
1931 printf(" mtu %lu", mtu);
1932 putchar('\n');
1933
1934 if (g_ifcr.ifcr_capabilities) {
1935 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1936 g_ifcr.ifcr_capabilities);
1937 printf("\tcapabilities=%s\n", &fbuf[2]);
1938 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1939 g_ifcr.ifcr_capenable);
1940 printf("\tenabled=%s\n", &fbuf[2]);
1941 }
1942
1943 ieee80211_status();
1944 vlan_status();
1945 tunnel_status();
1946
1947 if (sdl != NULL &&
1948 getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
1949 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1950 hbuf[0] != '\0')
1951 printf("\taddress: %s\n", hbuf);
1952
1953 (void) memset(&ifmr, 0, sizeof(ifmr));
1954 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1955
1956 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1957 /*
1958 * Interface doesn't support SIOC{G,S}IFMEDIA.
1959 */
1960 goto iface_stats;
1961 }
1962
1963 if (ifmr.ifm_count == 0) {
1964 warnx("%s: no media types?", name);
1965 goto iface_stats;
1966 }
1967
1968 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1969 if (media_list == NULL)
1970 err(EXIT_FAILURE, "malloc");
1971 ifmr.ifm_ulist = media_list;
1972
1973 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1)
1974 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1975
1976 printf("\tmedia: ");
1977 print_media_word(ifmr.ifm_current, 1, 0);
1978 if (ifmr.ifm_active != ifmr.ifm_current) {
1979 putchar(' ');
1980 putchar('(');
1981 print_media_word(ifmr.ifm_active, 0, 0);
1982 putchar(')');
1983 }
1984 putchar('\n');
1985
1986 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1987 const struct ifmedia_status_description *ifms;
1988 int bitno, found = 0;
1989
1990 printf("\tstatus: ");
1991 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1992 for (ifms = ifm_status_descriptions;
1993 ifms->ifms_valid != 0; ifms++) {
1994 if (ifms->ifms_type !=
1995 IFM_TYPE(ifmr.ifm_current) ||
1996 ifms->ifms_valid !=
1997 ifm_status_valid_list[bitno])
1998 continue;
1999 printf("%s%s", found ? ", " : "",
2000 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
2001 found = 1;
2002
2003 /*
2004 * For each valid indicator bit, there's
2005 * only one entry for each media type, so
2006 * terminate the inner loop now.
2007 */
2008 break;
2009 }
2010 }
2011
2012 if (found == 0)
2013 printf("unknown");
2014 putchar('\n');
2015 }
2016
2017 if (mflag) {
2018 int type, printed_type;
2019
2020 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
2021 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
2022 if (IFM_TYPE(media_list[i]) == type) {
2023 if (printed_type == 0) {
2024 printf("\tsupported %s media:\n",
2025 get_media_type_string(type));
2026 printed_type = 1;
2027 }
2028 printf("\t\t");
2029 print_media_word(media_list[i], 0, 1);
2030 printf("\n");
2031 }
2032 }
2033 }
2034 }
2035
2036 free(media_list);
2037
2038 iface_stats:
2039 if (!vflag)
2040 goto proto_status;
2041
2042 (void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
2043
2044 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifdr) != -1) {
2045 struct if_data * const ifi = &ifdr.ifdr_data;
2046 #define PLURAL(n) ((n) == 1 ? "" : "s")
2047 printf("\tinput: %llu packet%s, %llu byte%s",
2048 (unsigned long long) ifi->ifi_ipackets,
2049 PLURAL(ifi->ifi_ipackets),
2050 (unsigned long long) ifi->ifi_ibytes,
2051 PLURAL(ifi->ifi_ibytes));
2052 if (ifi->ifi_imcasts)
2053 printf(", %llu multicast%s",
2054 (unsigned long long) ifi->ifi_imcasts,
2055 PLURAL(ifi->ifi_imcasts));
2056 if (ifi->ifi_ierrors)
2057 printf(", %llu error%s",
2058 (unsigned long long) ifi->ifi_ierrors,
2059 PLURAL(ifi->ifi_ierrors));
2060 if (ifi->ifi_iqdrops)
2061 printf(", %llu queue drop%s",
2062 (unsigned long long) ifi->ifi_iqdrops,
2063 PLURAL(ifi->ifi_iqdrops));
2064 if (ifi->ifi_noproto)
2065 printf(", %llu unknown protocol",
2066 (unsigned long long) ifi->ifi_noproto);
2067 printf("\n\toutput: %llu packet%s, %llu byte%s",
2068 (unsigned long long) ifi->ifi_opackets,
2069 PLURAL(ifi->ifi_opackets),
2070 (unsigned long long) ifi->ifi_obytes,
2071 PLURAL(ifi->ifi_obytes));
2072 if (ifi->ifi_omcasts)
2073 printf(", %llu multicast%s",
2074 (unsigned long long) ifi->ifi_omcasts,
2075 PLURAL(ifi->ifi_omcasts));
2076 if (ifi->ifi_oerrors)
2077 printf(", %llu error%s",
2078 (unsigned long long) ifi->ifi_oerrors,
2079 PLURAL(ifi->ifi_oerrors));
2080 if (ifi->ifi_collisions)
2081 printf(", %llu collision%s",
2082 (unsigned long long) ifi->ifi_collisions,
2083 PLURAL(ifi->ifi_collisions));
2084 printf("\n");
2085 #undef PLURAL
2086 }
2087
2088 proto_status:
2089 if ((p = afp) != NULL) {
2090 (*p->af_status)(1);
2091 } else for (p = afs; p->af_name; p++) {
2092 ifr.ifr_addr.sa_family = p->af_af;
2093 (*p->af_status)(0);
2094 }
2095 }
2096
2097 void
2098 tunnel_status()
2099 {
2100 char psrcaddr[NI_MAXHOST];
2101 char pdstaddr[NI_MAXHOST];
2102 const char *ver = "";
2103 #ifdef NI_WITHSCOPEID
2104 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
2105 #else
2106 const int niflag = NI_NUMERICHOST;
2107 #endif
2108 struct if_laddrreq req;
2109
2110 psrcaddr[0] = pdstaddr[0] = '\0';
2111
2112 memset(&req, 0, sizeof(req));
2113 strncpy(req.iflr_name, name, IFNAMSIZ);
2114 if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&req) == -1)
2115 return;
2116 #ifdef INET6
2117 if (req.addr.ss_family == AF_INET6)
2118 in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
2119 #endif
2120 getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
2121 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
2122 #ifdef INET6
2123 if (req.addr.ss_family == AF_INET6)
2124 ver = "6";
2125 #endif
2126
2127 #ifdef INET6
2128 if (req.dstaddr.ss_family == AF_INET6)
2129 in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
2130 #endif
2131 getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
2132 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
2133
2134 printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
2135 }
2136
2137 void
2138 vlan_status()
2139 {
2140 struct vlanreq vlr;
2141
2142 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
2143 !isdigit(ifr.ifr_name[4]))
2144 return;
2145
2146 memset(&vlr, 0, sizeof(vlr));
2147 ifr.ifr_data = (caddr_t)&vlr;
2148
2149 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
2150 return;
2151
2152 if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
2153 printf("\tvlan: %d parent: %s\n",
2154 vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
2155 "<none>" : vlr.vlr_parent);
2156 }
2157
2158 void
2159 in_alias(creq)
2160 struct ifreq *creq;
2161 {
2162 struct sockaddr_in *iasin;
2163 int alias;
2164
2165 if (lflag)
2166 return;
2167
2168 alias = 1;
2169
2170 /* Get the non-alias address for this interface. */
2171 getsock(AF_INET);
2172 if (s < 0) {
2173 if (errno == EPROTONOSUPPORT)
2174 return;
2175 err(EXIT_FAILURE, "socket");
2176 }
2177 (void) memset(&ifr, 0, sizeof(ifr));
2178 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2179 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2180 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2181 return;
2182 } else
2183 warn("SIOCGIFADDR");
2184 }
2185 /* If creq and ifr are the same address, this is not an alias. */
2186 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
2187 sizeof(creq->ifr_addr)) == 0)
2188 alias = 0;
2189 (void) memset(&in_addreq, 0, sizeof(in_addreq));
2190 (void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
2191 memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
2192 sizeof(in_addreq.ifra_addr));
2193 if (ioctl(s, SIOCGIFALIAS, (caddr_t)&in_addreq) == -1) {
2194 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2195 return;
2196 } else
2197 warn("SIOCGIFALIAS");
2198 }
2199
2200 iasin = &in_addreq.ifra_addr;
2201 printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
2202
2203 if (flags & IFF_POINTOPOINT) {
2204 iasin = &in_addreq.ifra_dstaddr;
2205 printf(" -> %s", inet_ntoa(iasin->sin_addr));
2206 }
2207
2208 iasin = &in_addreq.ifra_mask;
2209 printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
2210
2211 if (flags & IFF_BROADCAST) {
2212 iasin = &in_addreq.ifra_broadaddr;
2213 printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
2214 }
2215 printf("\n");
2216 }
2217
2218 void
2219 in_status(force)
2220 int force;
2221 {
2222 struct ifaddrs *ifap, *ifa;
2223 struct ifreq isifr;
2224
2225 if (getifaddrs(&ifap) != 0)
2226 err(EXIT_FAILURE, "getifaddrs");
2227 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2228 if (strcmp(name, ifa->ifa_name) != 0)
2229 continue;
2230 if (ifa->ifa_addr->sa_family != AF_INET)
2231 continue;
2232 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
2233 continue;
2234
2235 memset(&isifr, 0, sizeof(isifr));
2236 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
2237 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
2238 in_alias(&isifr);
2239 }
2240 freeifaddrs(ifap);
2241 }
2242
2243 void
2244 setifprefixlen(addr, d)
2245 const char *addr;
2246 int d;
2247 {
2248 if (*afp->af_getprefix)
2249 (*afp->af_getprefix)(addr, MASK);
2250 explicit_prefix = 1;
2251 }
2252
2253 #ifdef INET6
2254 void
2255 in6_fillscopeid(sin6)
2256 struct sockaddr_in6 *sin6;
2257 {
2258 #if defined(__KAME__) && defined(KAME_SCOPEID)
2259 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2260 sin6->sin6_scope_id =
2261 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
2262 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
2263 }
2264 #endif
2265 }
2266
2267 /* XXX not really an alias */
2268 void
2269 in6_alias(creq)
2270 struct in6_ifreq *creq;
2271 {
2272 struct sockaddr_in6 *sin6;
2273 char hbuf[NI_MAXHOST];
2274 u_int32_t scopeid;
2275 #ifdef NI_WITHSCOPEID
2276 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
2277 #else
2278 const int niflag = NI_NUMERICHOST;
2279 #endif
2280
2281 /* Get the non-alias address for this interface. */
2282 getsock(AF_INET6);
2283 if (s < 0) {
2284 if (errno == EPROTONOSUPPORT)
2285 return;
2286 err(EXIT_FAILURE, "socket");
2287 }
2288
2289 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
2290
2291 in6_fillscopeid(sin6);
2292 scopeid = sin6->sin6_scope_id;
2293 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
2294 hbuf, sizeof(hbuf), NULL, 0, niflag))
2295 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
2296 printf("\tinet6 %s", hbuf);
2297
2298 if (flags & IFF_POINTOPOINT) {
2299 (void) memset(&ifr6, 0, sizeof(ifr6));
2300 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2301 ifr6.ifr_addr = creq->ifr_addr;
2302 if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) == -1) {
2303 if (errno != EADDRNOTAVAIL)
2304 warn("SIOCGIFDSTADDR_IN6");
2305 (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
2306 ifr6.ifr_addr.sin6_family = AF_INET6;
2307 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
2308 }
2309 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
2310 in6_fillscopeid(sin6);
2311 hbuf[0] = '\0';
2312 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
2313 hbuf, sizeof(hbuf), NULL, 0, niflag))
2314 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
2315 printf(" -> %s", hbuf);
2316 }
2317
2318 (void) memset(&ifr6, 0, sizeof(ifr6));
2319 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2320 ifr6.ifr_addr = creq->ifr_addr;
2321 if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) == -1) {
2322 if (errno != EADDRNOTAVAIL)
2323 warn("SIOCGIFNETMASK_IN6");
2324 } else {
2325 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
2326 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
2327 sizeof(struct in6_addr)));
2328 }
2329
2330 (void) memset(&ifr6, 0, sizeof(ifr6));
2331 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2332 ifr6.ifr_addr = creq->ifr_addr;
2333 if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) == -1) {
2334 if (errno != EADDRNOTAVAIL)
2335 warn("SIOCGIFAFLAG_IN6");
2336 } else {
2337 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
2338 printf(" anycast");
2339 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
2340 printf(" tentative");
2341 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
2342 printf(" duplicated");
2343 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
2344 printf(" detached");
2345 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
2346 printf(" deprecated");
2347 }
2348
2349 if (scopeid)
2350 printf(" scopeid 0x%x", scopeid);
2351
2352 if (Lflag) {
2353 struct in6_addrlifetime *lifetime;
2354 (void) memset(&ifr6, 0, sizeof(ifr6));
2355 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2356 ifr6.ifr_addr = creq->ifr_addr;
2357 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
2358 if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) == -1) {
2359 if (errno != EADDRNOTAVAIL)
2360 warn("SIOCGIFALIFETIME_IN6");
2361 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
2362 time_t t = time(NULL);
2363 printf(" pltime ");
2364 if (lifetime->ia6t_preferred) {
2365 printf("%s", lifetime->ia6t_preferred < t
2366 ? "0"
2367 : sec2str(lifetime->ia6t_preferred - t));
2368 } else
2369 printf("infty");
2370
2371 printf(" vltime ");
2372 if (lifetime->ia6t_expire) {
2373 printf("%s", lifetime->ia6t_expire < t
2374 ? "0"
2375 : sec2str(lifetime->ia6t_expire - t));
2376 } else
2377 printf("infty");
2378 }
2379 }
2380
2381 printf("\n");
2382 }
2383
2384 void
2385 in6_status(force)
2386 int force;
2387 {
2388 struct ifaddrs *ifap, *ifa;
2389 struct in6_ifreq isifr;
2390
2391 if (getifaddrs(&ifap) != 0)
2392 err(EXIT_FAILURE, "getifaddrs");
2393 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2394 if (strcmp(name, ifa->ifa_name) != 0)
2395 continue;
2396 if (ifa->ifa_addr->sa_family != AF_INET6)
2397 continue;
2398 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
2399 continue;
2400
2401 memset(&isifr, 0, sizeof(isifr));
2402 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
2403 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
2404 in6_alias(&isifr);
2405 }
2406 freeifaddrs(ifap);
2407 }
2408 #endif /*INET6*/
2409
2410 #ifndef INET_ONLY
2411
2412 void
2413 at_status(force)
2414 int force;
2415 {
2416 struct sockaddr_at *sat, null_sat;
2417 struct netrange *nr;
2418
2419 getsock(AF_APPLETALK);
2420 if (s < 0) {
2421 if (errno == EPROTONOSUPPORT)
2422 return;
2423 err(EXIT_FAILURE, "socket");
2424 }
2425 (void) memset(&ifr, 0, sizeof(ifr));
2426 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2427 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2428 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2429 if (!force)
2430 return;
2431 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2432 } else
2433 warn("SIOCGIFADDR");
2434 }
2435 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2436 sat = (struct sockaddr_at *)&ifr.ifr_addr;
2437
2438 (void) memset(&null_sat, 0, sizeof(null_sat));
2439
2440 nr = (struct netrange *) &sat->sat_zero;
2441 printf("\tatalk %d.%d range %d-%d phase %d",
2442 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
2443 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
2444 if (flags & IFF_POINTOPOINT) {
2445 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) == -1) {
2446 if (errno == EADDRNOTAVAIL)
2447 (void) memset(&ifr.ifr_addr, 0,
2448 sizeof(ifr.ifr_addr));
2449 else
2450 warn("SIOCGIFDSTADDR");
2451 }
2452 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
2453 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
2454 if (!sat)
2455 sat = &null_sat;
2456 printf("--> %d.%d",
2457 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
2458 }
2459 if (flags & IFF_BROADCAST) {
2460 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
2461 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
2462 if (sat)
2463 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
2464 sat->sat_addr.s_node);
2465 }
2466 putchar('\n');
2467 }
2468
2469 void
2470 xns_status(force)
2471 int force;
2472 {
2473 struct sockaddr_ns *sns;
2474
2475 getsock(AF_NS);
2476 if (s < 0) {
2477 if (errno == EPROTONOSUPPORT)
2478 return;
2479 err(EXIT_FAILURE, "socket");
2480 }
2481 (void) memset(&ifr, 0, sizeof(ifr));
2482 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2483 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2484 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2485 if (!force)
2486 return;
2487 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2488 } else
2489 warn("SIOCGIFADDR");
2490 }
2491 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2492 sns = (struct sockaddr_ns *)&ifr.ifr_addr;
2493 printf("\tns %s ", ns_ntoa(sns->sns_addr));
2494 if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
2495 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) == -1) {
2496 if (errno == EADDRNOTAVAIL)
2497 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2498 else
2499 warn("SIOCGIFDSTADDR");
2500 }
2501 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
2502 sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
2503 printf("--> %s ", ns_ntoa(sns->sns_addr));
2504 }
2505 putchar('\n');
2506 }
2507
2508 void
2509 iso_status(force)
2510 int force;
2511 {
2512 struct sockaddr_iso *siso;
2513 struct iso_ifreq isoifr;
2514
2515 getsock(AF_ISO);
2516 if (s < 0) {
2517 if (errno == EPROTONOSUPPORT)
2518 return;
2519 err(EXIT_FAILURE, "socket");
2520 }
2521 (void) memset(&isoifr, 0, sizeof(isoifr));
2522 (void) strncpy(isoifr.ifr_name, name, sizeof(isoifr.ifr_name));
2523 if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&isoifr) == -1) {
2524 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2525 if (!force)
2526 return;
2527 (void) memset(&isoifr.ifr_Addr, 0,
2528 sizeof(isoifr.ifr_Addr));
2529 } else
2530 warn("SIOCGIFADDR_ISO");
2531 }
2532 (void) strncpy(isoifr.ifr_name, name, sizeof isoifr.ifr_name);
2533 siso = &isoifr.ifr_Addr;
2534 printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
2535 if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&isoifr) == -1) {
2536 if (errno == EADDRNOTAVAIL)
2537 memset(&isoifr.ifr_Addr, 0, sizeof(isoifr.ifr_Addr));
2538 else
2539 warn("SIOCGIFNETMASK_ISO");
2540 } else {
2541 if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
2542 siso->siso_addr.isoa_len = siso->siso_len
2543 - offsetof(struct sockaddr_iso, siso_addr);
2544 printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
2545 }
2546 if (flags & IFF_POINTOPOINT) {
2547 if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&isoifr) == -1) {
2548 if (errno == EADDRNOTAVAIL)
2549 memset(&isoifr.ifr_Addr, 0,
2550 sizeof(isoifr.ifr_Addr));
2551 else
2552 warn("SIOCGIFDSTADDR_ISO");
2553 }
2554 (void) strncpy(isoifr.ifr_name, name, sizeof (isoifr.ifr_name));
2555 siso = &isoifr.ifr_Addr;
2556 printf("--> %s ", iso_ntoa(&siso->siso_addr));
2557 }
2558 putchar('\n');
2559 }
2560
2561 #endif /* INET_ONLY */
2562
2563 #define SIN(x) ((struct sockaddr_in *) &(x))
2564 struct sockaddr_in *sintab[] = {
2565 SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
2566 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
2567
2568 void
2569 in_getaddr(str, which)
2570 const char *str;
2571 int which;
2572 {
2573 struct sockaddr_in *gasin = sintab[which];
2574 struct hostent *hp;
2575 struct netent *np;
2576
2577 gasin->sin_len = sizeof(*gasin);
2578 if (which != MASK)
2579 gasin->sin_family = AF_INET;
2580
2581 if (which == ADDR) {
2582 char *p = NULL;
2583 if ((p = strrchr(str, '/')) != NULL) {
2584 *p = '\0';
2585 in_getprefix(p + 1, MASK);
2586 }
2587 }
2588
2589 if (inet_aton(str, &gasin->sin_addr) == 0) {
2590 if ((hp = gethostbyname(str)) != NULL)
2591 (void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
2592 else if ((np = getnetbyname(str)) != NULL)
2593 gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
2594 else
2595 errx(EXIT_FAILURE, "%s: bad value", str);
2596 }
2597 }
2598
2599 void
2600 in_getprefix(plen, which)
2601 const char *plen;
2602 int which;
2603 {
2604 register struct sockaddr_in *igsin = sintab[which];
2605 register u_char *cp;
2606 int len = strtol(plen, (char **)NULL, 10);
2607
2608 if ((len < 0) || (len > 32))
2609 errx(EXIT_FAILURE, "%s: bad value", plen);
2610 igsin->sin_len = sizeof(*igsin);
2611 if (which != MASK)
2612 igsin->sin_family = AF_INET;
2613 if ((len == 0) || (len == 32)) {
2614 memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
2615 return;
2616 }
2617 memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
2618 for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
2619 *cp++ = 0xff;
2620 if (len)
2621 *cp = 0xff << (8 - len);
2622 }
2623
2624 #ifdef INET6
2625 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
2626 struct sockaddr_in6 *sin6tab[] = {
2627 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
2628 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
2629
2630 void
2631 in6_getaddr(str, which)
2632 const char *str;
2633 int which;
2634 {
2635 #if defined(__KAME__) && defined(KAME_SCOPEID)
2636 struct sockaddr_in6 *sin6 = sin6tab[which];
2637 struct addrinfo hints, *res;
2638 int error;
2639
2640 memset(&hints, 0, sizeof(hints));
2641 hints.ai_family = AF_INET6;
2642 hints.ai_socktype = SOCK_DGRAM;
2643 #if 0 /* in_getaddr() allows FQDN */
2644 hints.ai_flags = AI_NUMERICHOST;
2645 #endif
2646 error = getaddrinfo(str, "0", &hints, &res);
2647 if (error)
2648 errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
2649 if (res->ai_next)
2650 errx(EXIT_FAILURE, "%s: resolved to multiple hosts", str);
2651 if (res->ai_addrlen != sizeof(struct sockaddr_in6))
2652 errx(EXIT_FAILURE, "%s: bad value", str);
2653 memcpy(sin6, res->ai_addr, res->ai_addrlen);
2654 freeaddrinfo(res);
2655 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
2656 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
2657 htons(sin6->sin6_scope_id);
2658 sin6->sin6_scope_id = 0;
2659 }
2660 #else
2661 struct sockaddr_in6 *gasin = sin6tab[which];
2662
2663 gasin->sin6_len = sizeof(*gasin);
2664 if (which != MASK)
2665 gasin->sin6_family = AF_INET6;
2666
2667 if (which == ADDR) {
2668 char *p = NULL;
2669 if((p = strrchr(str, '/')) != NULL) {
2670 *p = '\0';
2671 in6_getprefix(p + 1, MASK);
2672 explicit_prefix = 1;
2673 }
2674 }
2675
2676 if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
2677 errx(EXIT_FAILURE, "%s: bad value", str);
2678 #endif
2679 }
2680
2681 void
2682 in6_getprefix(plen, which)
2683 const char *plen;
2684 int which;
2685 {
2686 register struct sockaddr_in6 *gpsin = sin6tab[which];
2687 register u_char *cp;
2688 int len = strtol(plen, (char **)NULL, 10);
2689
2690 if ((len < 0) || (len > 128))
2691 errx(EXIT_FAILURE, "%s: bad value", plen);
2692 gpsin->sin6_len = sizeof(*gpsin);
2693 if (which != MASK)
2694 gpsin->sin6_family = AF_INET6;
2695 if ((len == 0) || (len == 128)) {
2696 memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
2697 return;
2698 }
2699 memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
2700 for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
2701 *cp++ = 0xff;
2702 if (len)
2703 *cp = 0xff << (8 - len);
2704 }
2705
2706 int
2707 prefix(val, size)
2708 void *val;
2709 int size;
2710 {
2711 register u_char *pname = (u_char *)val;
2712 register int byte, bit, plen = 0;
2713
2714 for (byte = 0; byte < size; byte++, plen += 8)
2715 if (pname[byte] != 0xff)
2716 break;
2717 if (byte == size)
2718 return (plen);
2719 for (bit = 7; bit != 0; bit--, plen++)
2720 if (!(pname[byte] & (1 << bit)))
2721 break;
2722 for (; bit != 0; bit--)
2723 if (pname[byte] & (1 << bit))
2724 return(0);
2725 byte++;
2726 for (; byte < size; byte++)
2727 if (pname[byte])
2728 return(0);
2729 return (plen);
2730 }
2731 #endif /*INET6*/
2732
2733 #ifndef INET_ONLY
2734 void
2735 at_getaddr(addr, which)
2736 const char *addr;
2737 int which;
2738 {
2739 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
2740 u_int net, node;
2741
2742 sat->sat_family = AF_APPLETALK;
2743 sat->sat_len = sizeof(*sat);
2744 if (which == MASK)
2745 errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
2746 if (sscanf(addr, "%u.%u", &net, &node) != 2
2747 || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
2748 errx(EXIT_FAILURE, "%s: illegal address", addr);
2749 sat->sat_addr.s_net = htons(net);
2750 sat->sat_addr.s_node = node;
2751 }
2752
2753 void
2754 setatrange(range, d)
2755 const char *range;
2756 int d;
2757 {
2758 u_short first = 123, last = 123;
2759
2760 if (sscanf(range, "%hu-%hu", &first, &last) != 2
2761 || first == 0 /* || first > 0xffff */
2762 || last == 0 /* || last > 0xffff */ || first > last)
2763 errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
2764 first, last);
2765 at_nr.nr_firstnet = htons(first);
2766 at_nr.nr_lastnet = htons(last);
2767 }
2768
2769 void
2770 setatphase(phase, d)
2771 const char *phase;
2772 int d;
2773 {
2774 if (!strcmp(phase, "1"))
2775 at_nr.nr_phase = 1;
2776 else if (!strcmp(phase, "2"))
2777 at_nr.nr_phase = 2;
2778 else
2779 errx(EXIT_FAILURE, "%s: illegal phase", phase);
2780 }
2781
2782 void
2783 checkatrange(sat)
2784 struct sockaddr_at *sat;
2785 {
2786 if (at_nr.nr_phase == 0)
2787 at_nr.nr_phase = 2; /* Default phase 2 */
2788 if (at_nr.nr_firstnet == 0)
2789 at_nr.nr_firstnet = /* Default range of one */
2790 at_nr.nr_lastnet = sat->sat_addr.s_net;
2791 printf("\tatalk %d.%d range %d-%d phase %d\n",
2792 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
2793 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
2794 if ((u_short) ntohs(at_nr.nr_firstnet) >
2795 (u_short) ntohs(sat->sat_addr.s_net)
2796 || (u_short) ntohs(at_nr.nr_lastnet) <
2797 (u_short) ntohs(sat->sat_addr.s_net))
2798 errx(EXIT_FAILURE, "AppleTalk address is not in range");
2799 *((struct netrange *) &sat->sat_zero) = at_nr;
2800 }
2801
2802 #define SNS(x) ((struct sockaddr_ns *) &(x))
2803 struct sockaddr_ns *snstab[] = {
2804 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
2805 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
2806
2807 void
2808 xns_getaddr(addr, which)
2809 const char *addr;
2810 int which;
2811 {
2812 struct sockaddr_ns *sns = snstab[which];
2813
2814 sns->sns_family = AF_NS;
2815 sns->sns_len = sizeof(*sns);
2816 sns->sns_addr = ns_addr(addr);
2817 if (which == MASK)
2818 puts("Attempt to set XNS netmask will be ineffectual");
2819 }
2820
2821 #define SISO(x) ((struct sockaddr_iso *) &(x))
2822 struct sockaddr_iso *sisotab[] = {
2823 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
2824 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
2825
2826 void
2827 iso_getaddr(addr, which)
2828 const char *addr;
2829 int which;
2830 {
2831 struct sockaddr_iso *siso = sisotab[which];
2832 siso->siso_addr = *iso_addr(addr);
2833
2834 if (which == MASK) {
2835 siso->siso_len = TSEL(siso) - (caddr_t)(siso);
2836 siso->siso_nlen = 0;
2837 } else {
2838 siso->siso_len = sizeof(*siso);
2839 siso->siso_family = AF_ISO;
2840 }
2841 }
2842
2843 void
2844 setsnpaoffset(val, d)
2845 const char *val;
2846 int d;
2847 {
2848 iso_addreq.ifra_snpaoffset = atoi(val);
2849 }
2850
2851 void
2852 setnsellength(val, d)
2853 const char *val;
2854 int d;
2855 {
2856 nsellength = atoi(val);
2857 if (nsellength < 0)
2858 errx(EXIT_FAILURE, "Negative NSEL length is absurd");
2859 if (afp == 0 || afp->af_af != AF_ISO)
2860 errx(EXIT_FAILURE, "Setting NSEL length valid only for iso");
2861 }
2862
2863 void
2864 fixnsel(siso)
2865 struct sockaddr_iso *siso;
2866 {
2867 if (siso->siso_family == 0)
2868 return;
2869 siso->siso_tlen = nsellength;
2870 }
2871
2872 void
2873 adjust_nsellength()
2874 {
2875 fixnsel(sisotab[RIDADDR]);
2876 fixnsel(sisotab[ADDR]);
2877 fixnsel(sisotab[DSTADDR]);
2878 }
2879
2880 #endif /* INET_ONLY */
2881
2882 void
2883 usage()
2884 {
2885 const char *progname = getprogname();
2886
2887 fprintf(stderr,
2888 "usage: %s [ -m ] [ -v ]"
2889 #ifdef INET6
2890 "[ -L ] "
2891 #endif
2892 "interface\n"
2893 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
2894 "\t\t[ alias | -alias ] ]\n"
2895 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
2896 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
2897 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
2898 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
2899 "\t[ arp | -arp ]\n"
2900 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
2901 "[ instance minst ]\n"
2902 "\t[ vlan n vlanif i ]\n"
2903 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
2904 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
2905 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
2906 " %s -a [ -m ] [ -d ] [ -u ] [ -v ] [ af ]\n"
2907 " %s -l [ -b ] [ -d ] [ -u ] [ -s ]\n"
2908 " %s -C\n"
2909 " %s interface create\n"
2910 " %s interface destroy\n",
2911 progname, progname, progname, progname, progname, progname);
2912 exit(1);
2913 }
2914
2915 #ifdef INET6
2916 char *
2917 sec2str(total)
2918 time_t total;
2919 {
2920 static char result[256];
2921 int days, hours, mins, secs;
2922 int first = 1;
2923 char *p = result;
2924 char *end = &result[sizeof(result)];
2925 int n;
2926
2927 if (0) { /*XXX*/
2928 days = total / 3600 / 24;
2929 hours = (total / 3600) % 24;
2930 mins = (total / 60) % 60;
2931 secs = total % 60;
2932
2933 if (days) {
2934 first = 0;
2935 n = snprintf(p, end - p, "%dd", days);
2936 if (n < 0 || n >= end - p)
2937 return(result);
2938 p += n;
2939 }
2940 if (!first || hours) {
2941 first = 0;
2942 n = snprintf(p, end - p, "%dh", hours);
2943 if (n < 0 || n >= end - p)
2944 return(result);
2945 p += n;
2946 }
2947 if (!first || mins) {
2948 first = 0;
2949 n = snprintf(p, end - p, "%dm", mins);
2950 if (n < 0 || n >= end - p)
2951 return(result);
2952 p += n;
2953 }
2954 snprintf(p, end - p, "%ds", secs);
2955 } else
2956 snprintf(p, end - p, "%lu", (u_long)total);
2957
2958 return(result);
2959 }
2960 #endif
2961