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