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