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