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