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