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