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