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