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