ifconfig.c revision 1.43 1 /* $NetBSD: ifconfig.c,v 1.43 1998/08/06 03:47:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1983, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
76 The Regents of the University of California. All rights reserved.\n");
77 #endif /* not lint */
78
79 #ifndef lint
80 #if 0
81 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
82 #else
83 __RCSID("$NetBSD: ifconfig.c,v 1.43 1998/08/06 03:47:28 thorpej Exp $");
84 #endif
85 #endif /* not lint */
86
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
90
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_media.h>
94 #include <netinet/in.h>
95 #include <arpa/inet.h>
96
97 #include <netatalk/at.h>
98
99 #define NSIP
100 #include <netns/ns.h>
101 #include <netns/ns_if.h>
102 #include <netdb.h>
103
104 #define EON
105 #include <netiso/iso.h>
106 #include <netiso/iso_var.h>
107 #include <sys/protosw.h>
108
109 #include <ctype.h>
110 #include <err.h>
111 #include <errno.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <unistd.h>
116
117 struct ifreq ifr, ridreq;
118 struct ifaliasreq addreq __attribute__((aligned(4)));
119 struct iso_aliasreq iso_addreq;
120 struct sockaddr_in netmask;
121 struct netrange at_nr; /* AppleTalk net range */
122
123 char name[30];
124 int flags, metric, mtu, setaddr, setipdst, doalias;
125 int clearaddr, s;
126 int newaddr = -1;
127 int nsellength = 1;
128 int af;
129 int dflag, mflag, lflag, uflag;
130 int reset_if_flags;
131
132 void notealias __P((char *, int));
133 void notrailers __P((char *, int));
134 void setifaddr __P((char *, int));
135 void setifdstaddr __P((char *, int));
136 void setifflags __P((char *, int));
137 void setifbroadaddr __P((char *, int));
138 void setifipdst __P((char *, int));
139 void setifmetric __P((char *, int));
140 void setifmtu __P((char *, int));
141 void setifnetmask __P((char *, int));
142 void setnsellength __P((char *, int));
143 void setsnpaoffset __P((char *, int));
144 void setatrange __P((char *, int));
145 void setatphase __P((char *, int));
146 void checkatrange __P ((struct sockaddr_at *));
147 void setmedia __P((char *, int));
148 void setmediaopt __P((char *, int));
149 void unsetmediaopt __P((char *, int));
150 void fixnsel __P((struct sockaddr_iso *));
151 int main __P((int, char *[]));
152
153 #define NEXTARG 0xffffff
154
155 struct cmd {
156 char *c_name;
157 int c_parameter; /* NEXTARG means next argv */
158 void (*c_func) __P((char *, int));
159 } cmds[] = {
160 { "up", IFF_UP, setifflags } ,
161 { "down", -IFF_UP, setifflags },
162 { "trailers", -1, notrailers },
163 { "-trailers", 1, notrailers },
164 { "arp", -IFF_NOARP, setifflags },
165 { "-arp", IFF_NOARP, setifflags },
166 { "debug", IFF_DEBUG, setifflags },
167 { "-debug", -IFF_DEBUG, setifflags },
168 { "alias", IFF_UP, notealias },
169 { "-alias", -IFF_UP, notealias },
170 { "delete", -IFF_UP, notealias },
171 #ifdef notdef
172 #define EN_SWABIPS 0x1000
173 { "swabips", EN_SWABIPS, setifflags },
174 { "-swabips", -EN_SWABIPS, setifflags },
175 #endif
176 { "netmask", NEXTARG, setifnetmask },
177 { "metric", NEXTARG, setifmetric },
178 { "mtu", NEXTARG, setifmtu },
179 { "broadcast", NEXTARG, setifbroadaddr },
180 { "ipdst", NEXTARG, setifipdst },
181 #ifndef INET_ONLY
182 { "range", NEXTARG, setatrange },
183 { "phase", NEXTARG, setatphase },
184 { "snpaoffset", NEXTARG, setsnpaoffset },
185 { "nsellength", NEXTARG, setnsellength },
186 #endif /* INET_ONLY */
187 { "link0", IFF_LINK0, setifflags } ,
188 { "-link0", -IFF_LINK0, setifflags } ,
189 { "link1", IFF_LINK1, setifflags } ,
190 { "-link1", -IFF_LINK1, setifflags } ,
191 { "link2", IFF_LINK2, setifflags } ,
192 { "-link2", -IFF_LINK2, setifflags } ,
193 { "media", NEXTARG, setmedia },
194 { "mediaopt", NEXTARG, setmediaopt },
195 { "-mediaopt", NEXTARG, unsetmediaopt },
196 { 0, 0, setifaddr },
197 { 0, 0, setifdstaddr },
198 };
199
200 void adjust_nsellength __P((void));
201 int getinfo __P((struct ifreq *));
202 void getsock __P((int));
203 void printall __P((void));
204 void printb __P((char *, unsigned short, char *));
205 void status __P((const u_int8_t *, int));
206 void usage __P((void));
207
208 void domediaopt __P((char *, int));
209 const char *get_media_type_string __P((int));
210 const char *get_media_subtype_string __P((int));
211 int get_media_subtype __P((int, const char *));
212 int get_media_options __P((int, const char *));
213 int lookup_media_word __P((struct ifmedia_description *, int,
214 const char *));
215 void print_media_word __P((int, int));
216
217 /*
218 * XNS support liberally adapted from code written at the University of
219 * Maryland principally by James O'Toole and Chris Torek.
220 */
221 void in_status __P((int));
222 void in_getaddr __P((char *, int));
223 void at_status __P((int));
224 void at_getaddr __P((char *, int));
225 void xns_status __P((int));
226 void xns_getaddr __P((char *, int));
227 void iso_status __P((int));
228 void iso_getaddr __P((char *, int));
229
230 /* Known address families */
231 struct afswtch {
232 char *af_name;
233 short af_af;
234 void (*af_status) __P((int));
235 void (*af_getaddr) __P((char *, int));
236 u_long af_difaddr;
237 u_long af_aifaddr;
238 caddr_t af_ridreq;
239 caddr_t af_addreq;
240 } afs[] = {
241 #define C(x) ((caddr_t) &x)
242 { "inet", AF_INET, in_status, in_getaddr,
243 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
244 #ifndef INET_ONLY /* small version, for boot media */
245 { "atalk", AF_APPLETALK, at_status, at_getaddr,
246 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
247 { "ns", AF_NS, xns_status, xns_getaddr,
248 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
249 { "iso", AF_ISO, iso_status, iso_getaddr,
250 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(iso_addreq) },
251 #endif /* INET_ONLY */
252 { 0, 0, 0, 0 }
253 };
254
255 struct afswtch *afp; /*the address family being set or asked about*/
256
257 struct afswtch *lookup_af __P((const char *));
258
259 int
260 main(argc, argv)
261 int argc;
262 char *argv[];
263 {
264 int ch, aflag;
265
266 /* Parse command-line options */
267 aflag = mflag = 0;
268 while ((ch = getopt(argc, argv, "adlmu")) != -1) {
269 switch (ch) {
270 case 'a':
271 aflag = 1;
272 break;
273
274 case 'd':
275 dflag = 1;
276 break;
277
278 case 'l':
279 lflag = 1;
280 break;
281
282 case 'm':
283 mflag = 1;
284 break;
285
286 case 'u':
287 uflag = 1;
288 break;
289
290 default:
291 usage();
292 /* NOTREACHED */
293 }
294 }
295 argc -= optind;
296 argv += optind;
297
298 /*
299 * -l means "list all interfaces", and is mutally exclusive with
300 * all other flags/commands.
301 *
302 * -a means "print status of all interfaces".
303 */
304 if (lflag && (aflag || mflag || argc))
305 usage();
306 if (aflag || lflag) {
307 if (argc > 1)
308 usage();
309 else if (argc == 1) {
310 afp = lookup_af(argv[0]);
311 if (afp == NULL)
312 usage();
313 }
314 if (afp)
315 af = ifr.ifr_addr.sa_family = afp->af_af;
316 else
317 af = ifr.ifr_addr.sa_family = afs[0].af_af;
318 printall();
319 exit(0);
320 }
321
322 /* Make sure there's an interface name. */
323 if (argc < 1)
324 usage();
325 (void) strncpy(name, argv[0], sizeof(name));
326 argc--; argv++;
327
328 /* Check for address family. */
329 afp = NULL;
330 if (argc > 0) {
331 afp = lookup_af(argv[0]);
332 if (afp != NULL) {
333 argv++;
334 argc--;
335 }
336 }
337
338 if (afp == NULL)
339 afp = afs;
340 af = ifr.ifr_addr.sa_family = afp->af_af;
341
342 /* Get information about the interface. */
343 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
344 if (getinfo(&ifr) < 0)
345 exit(1);
346
347 /* No more arguments means interface status. */
348 if (argc == 0) {
349 status(NULL, 0);
350 exit(0);
351 }
352
353 /* Process commands. */
354 while (argc > 0) {
355 struct cmd *p;
356
357 for (p = cmds; p->c_name; p++)
358 if (strcmp(argv[0], p->c_name) == 0)
359 break;
360 if (p->c_name == 0 && setaddr)
361 p++; /* got src, do dst */
362 if (p->c_func) {
363 if (p->c_parameter == NEXTARG) {
364 if (argc < 2)
365 errx(1, "'%s' requires argument",
366 p->c_name);
367 (*p->c_func)(argv[1], 0);
368 argc--, argv++;
369 } else
370 (*p->c_func)(argv[0], p->c_parameter);
371 }
372 argc--, argv++;
373 }
374
375 #ifndef INET_ONLY
376
377 if (af == AF_ISO)
378 adjust_nsellength();
379
380 if (af == AF_APPLETALK)
381 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
382
383 if (setipdst && af==AF_NS) {
384 struct nsip_req rq;
385 int size = sizeof(rq);
386
387 rq.rq_ns = addreq.ifra_addr;
388 rq.rq_ip = addreq.ifra_dstaddr;
389
390 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
391 warn("encapsulation routing");
392 }
393
394 #endif /* INET_ONLY */
395
396 if (clearaddr) {
397 int ret;
398 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
399 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
400 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
401 /* means no previous address for interface */
402 } else
403 warn("SIOCDIFADDR");
404 }
405 }
406 if (newaddr > 0) {
407 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
408 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
409 warn("SIOCAIFADDR");
410 }
411 if (reset_if_flags && ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
412 err(1, "SIOCSIFFLAGS");
413 exit(0);
414 }
415
416 struct afswtch *
417 lookup_af(cp)
418 const char *cp;
419 {
420 struct afswtch *a;
421
422 for (a = afs; a->af_name != NULL; a++)
423 if (strcmp(a->af_name, cp) == 0)
424 return (a);
425 return (NULL);
426 }
427
428 void
429 getsock(naf)
430 int naf;
431 {
432 static int oaf = -1;
433
434 if (oaf == naf)
435 return;
436 if (oaf != -1)
437 close(s);
438 s = socket(naf, SOCK_DGRAM, 0);
439 if (s < 0)
440 oaf = -1;
441 else
442 oaf = naf;
443 }
444
445 int
446 getinfo(ifr)
447 struct ifreq *ifr;
448 {
449
450 getsock(af);
451 if (s < 0)
452 err(1, "socket");
453 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
454 warn("SIOCGIFFLAGS %s", ifr->ifr_name);
455 return (-1);
456 }
457 flags = ifr->ifr_flags;
458 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
459 warn("SIOCGIFMETRIC %s", ifr->ifr_name);
460 metric = 0;
461 } else
462 metric = ifr->ifr_metric;
463 if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
464 mtu = 0;
465 else
466 mtu = ifr->ifr_mtu;
467 return (0);
468 }
469
470 void
471 printall()
472 {
473 char inbuf[8192];
474 const struct sockaddr_dl *sdl = NULL;
475 struct ifconf ifc;
476 struct ifreq ifreq, *ifr;
477 int i, idx;
478
479 ifc.ifc_len = sizeof(inbuf);
480 ifc.ifc_buf = inbuf;
481 getsock(af);
482 if (s < 0)
483 err(1, "socket");
484 if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
485 err(1, "SIOCGIFCONF");
486 ifr = ifc.ifc_req;
487 ifreq.ifr_name[0] = '\0';
488 for (i = 0, idx = 0; i < ifc.ifc_len; ) {
489 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
490 i += sizeof(ifr->ifr_name) +
491 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
492 ? ifr->ifr_addr.sa_len
493 : sizeof(struct sockaddr));
494 if (ifr->ifr_addr.sa_family == AF_LINK)
495 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
496 if (!strncmp(ifreq.ifr_name, ifr->ifr_name,
497 sizeof(ifr->ifr_name)))
498 continue;
499 (void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name));
500 ifreq = *ifr;
501
502 if (getinfo(&ifreq) < 0)
503 continue;
504 if (dflag && (flags & IFF_UP) != 0)
505 continue;
506 if (uflag && (flags & IFF_UP) == 0)
507 continue;
508
509 idx++;
510 /*
511 * Are we just listing the interfaces?
512 */
513 if (lflag) {
514 if (idx > 1)
515 putchar(' ');
516 fputs(name, stdout);
517 continue;
518 }
519
520 if (sdl == NULL) {
521 status(NULL, 0);
522 } else {
523 status(LLADDR(sdl), sdl->sdl_alen);
524 sdl = NULL;
525 }
526 }
527 if (lflag)
528 putchar('\n');
529 }
530
531 #define RIDADDR 0
532 #define ADDR 1
533 #define MASK 2
534 #define DSTADDR 3
535
536 /*ARGSUSED*/
537 void
538 setifaddr(addr, param)
539 char *addr;
540 int param;
541 {
542 /*
543 * Delay the ioctl to set the interface addr until flags are all set.
544 * The address interpretation may depend on the flags,
545 * and the flags may change when the address is set.
546 */
547 setaddr++;
548 if (newaddr == -1)
549 newaddr = 1;
550 if (doalias == 0)
551 clearaddr = 1;
552 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
553 }
554
555 void
556 setifnetmask(addr, d)
557 char *addr;
558 int d;
559 {
560 (*afp->af_getaddr)(addr, MASK);
561 }
562
563 void
564 setifbroadaddr(addr, d)
565 char *addr;
566 int d;
567 {
568 (*afp->af_getaddr)(addr, DSTADDR);
569 }
570
571 void
572 setifipdst(addr, d)
573 char *addr;
574 int d;
575 {
576 in_getaddr(addr, DSTADDR);
577 setipdst++;
578 clearaddr = 0;
579 newaddr = 0;
580 }
581
582 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
583 /*ARGSUSED*/
584 void
585 notealias(addr, param)
586 char *addr;
587 int param;
588 {
589 if (setaddr && doalias == 0 && param < 0)
590 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
591 rqtosa(af_addreq)->sa_len);
592 doalias = param;
593 if (param < 0) {
594 clearaddr = 1;
595 newaddr = 0;
596 } else
597 clearaddr = 0;
598 }
599
600 /*ARGSUSED*/
601 void
602 notrailers(vname, value)
603 char *vname;
604 int value;
605 {
606 puts("Note: trailers are no longer sent, but always received");
607 }
608
609 /*ARGSUSED*/
610 void
611 setifdstaddr(addr, param)
612 char *addr;
613 int param;
614 {
615 (*afp->af_getaddr)(addr, DSTADDR);
616 }
617
618 void
619 setifflags(vname, value)
620 char *vname;
621 int value;
622 {
623 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0)
624 err(1, "SIOCGIFFLAGS");
625 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
626 flags = ifr.ifr_flags;
627
628 if (value < 0) {
629 value = -value;
630 flags &= ~value;
631 } else
632 flags |= value;
633 ifr.ifr_flags = flags;
634 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
635 err(1, "SIOCSIFFLAGS");
636
637 reset_if_flags = 1;
638 }
639
640 void
641 setifmetric(val, d)
642 char *val;
643 int d;
644 {
645 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
646 ifr.ifr_metric = atoi(val);
647 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
648 warn("SIOCSIFMETRIC");
649 }
650
651 void
652 setifmtu(val, d)
653 char *val;
654 int d;
655 {
656 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
657 ifr.ifr_mtu = atoi(val);
658 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
659 warn("SIOCSIFMTU");
660 }
661
662 void
663 setmedia(val, d)
664 char *val;
665 int d;
666 {
667 struct ifmediareq ifmr;
668 int first_type, subtype;
669
670 (void) memset(&ifmr, 0, sizeof(ifmr));
671 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
672
673 ifmr.ifm_count = 1;
674 ifmr.ifm_ulist = &first_type;
675 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
676 /*
677 * If we get E2BIG, the kernel is telling us
678 * that there are more, so we can ignore it.
679 */
680 if (errno != E2BIG)
681 err(1, "SIOCGIFMEDIA");
682 }
683
684 if (ifmr.ifm_count == 0)
685 errx(1, "%s: no media types?", name);
686
687 /*
688 * We are primarily concerned with the top-level type.
689 * However, "current" may be only IFM_NONE, so we just look
690 * for the top-level type in the first "supported type"
691 * entry.
692 *
693 * (I'm assuming that all supported media types for a given
694 * interface will be the same top-level type..)
695 */
696 subtype = get_media_subtype(IFM_TYPE(first_type), val);
697
698 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
699 ifr.ifr_media = (ifmr.ifm_current & ~(IFM_NMASK|IFM_TMASK)) |
700 IFM_TYPE(first_type) | subtype;
701
702 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
703 err(1, "SIOCSIFMEDIA");
704 }
705
706 void
707 setmediaopt(val, d)
708 char *val;
709 int d;
710 {
711
712 domediaopt(val, 0);
713 }
714
715 void
716 unsetmediaopt(val, d)
717 int d;
718 char *val;
719 {
720
721 domediaopt(val, 1);
722 }
723
724 void
725 domediaopt(val, clear)
726 char *val;
727 int clear;
728 {
729 struct ifmediareq ifmr;
730 int *mwords, options;
731
732 (void) memset(&ifmr, 0, sizeof(ifmr));
733 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
734
735 /*
736 * We must go through the motions of reading all
737 * supported media because we need to know both
738 * the current media type and the top-level type.
739 */
740
741 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
742 err(1, "SIOCGIFMEDIA");
743
744 if (ifmr.ifm_count == 0)
745 errx(1, "%s: no media types?", name);
746
747 mwords = (int *)malloc(ifmr.ifm_count * sizeof(int));
748 if (mwords == NULL)
749 err(1, "malloc");
750
751 ifmr.ifm_ulist = mwords;
752 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
753 err(1, "SIOCGIFMEDIA");
754
755 options = get_media_options(IFM_TYPE(mwords[0]), val);
756
757 free(mwords);
758
759 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
760 ifr.ifr_media = ifmr.ifm_current;
761 if (clear)
762 ifr.ifr_media &= ~options;
763 else
764 ifr.ifr_media |= options;
765
766 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
767 err(1, "SIOCSIFMEDIA");
768 }
769
770 struct ifmedia_description ifm_type_descriptions[] =
771 IFM_TYPE_DESCRIPTIONS;
772
773 struct ifmedia_description ifm_subtype_descriptions[] =
774 IFM_SUBTYPE_DESCRIPTIONS;
775
776 struct ifmedia_description ifm_option_descriptions[] =
777 IFM_OPTION_DESCRIPTIONS;
778
779 const char *
780 get_media_type_string(mword)
781 int mword;
782 {
783 struct ifmedia_description *desc;
784
785 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
786 desc++) {
787 if (IFM_TYPE(mword) == desc->ifmt_word)
788 return (desc->ifmt_string);
789 }
790 return ("<unknown type>");
791 }
792
793 const char *
794 get_media_subtype_string(mword)
795 int mword;
796 {
797 struct ifmedia_description *desc;
798
799 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
800 desc++) {
801 if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
802 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
803 return (desc->ifmt_string);
804 }
805 return ("<unknown subtype>");
806 }
807
808 int
809 get_media_subtype(type, val)
810 int type;
811 const char *val;
812 {
813 int rval;
814
815 rval = lookup_media_word(ifm_subtype_descriptions, type, val);
816 if (rval == -1)
817 errx(1, "unknown %s media subtype: %s",
818 get_media_type_string(type), val);
819
820 return (rval);
821 }
822
823 int
824 get_media_options(type, val)
825 int type;
826 const char *val;
827 {
828 char *optlist, *str;
829 int option, rval = 0;
830
831 /* We muck with the string, so copy it. */
832 optlist = strdup(val);
833 if (optlist == NULL)
834 err(1, "strdup");
835 str = optlist;
836
837 /*
838 * Look up the options in the user-provided comma-separated list.
839 */
840 for (; (str = strtok(str, ",")) != NULL; str = NULL) {
841 option = lookup_media_word(ifm_option_descriptions, type, str);
842 if (option == -1)
843 errx(1, "unknown %s media option: %s",
844 get_media_type_string(type), str);
845 rval |= option;
846 }
847
848 free(optlist);
849 return (rval);
850 }
851
852 int
853 lookup_media_word(desc, type, val)
854 struct ifmedia_description *desc;
855 int type;
856 const char *val;
857 {
858
859 for (; desc->ifmt_string != NULL; desc++) {
860 if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
861 strcasecmp(desc->ifmt_string, val) == 0)
862 return (desc->ifmt_word);
863 }
864 return (-1);
865 }
866
867 void
868 print_media_word(ifmw, print_type)
869 int ifmw, print_type;
870 {
871 struct ifmedia_description *desc;
872 int seen_option = 0;
873
874 if (print_type)
875 printf("%s ", get_media_type_string(ifmw));
876 printf(get_media_subtype_string(ifmw));
877
878 /* Find options. */
879 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
880 desc++) {
881 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
882 (ifmw & desc->ifmt_word) != 0 &&
883 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
884 if (seen_option == 0)
885 printf(" <");
886 printf("%s%s", seen_option ? "," : "",
887 desc->ifmt_string);
888 seen_option |= IFM_OPTIONS(desc->ifmt_word);
889 }
890 }
891 if (seen_option)
892 printf(">");
893 if (IFM_INST(ifmw) != 0)
894 printf(" [inst %d]", IFM_INST(ifmw));
895 }
896
897 #define IFFBITS \
898 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
899 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
900
901 /*
902 * Print the status of the interface. If an address family was
903 * specified, show it and it only; otherwise, show them all.
904 */
905 void
906 status(ap, alen)
907 const u_int8_t *ap;
908 int alen;
909 {
910 struct afswtch *p = afp;
911 struct ifmediareq ifmr;
912 int *media_list, i;
913
914 printf("%s: ", name);
915 printb("flags", flags, IFFBITS);
916 if (metric)
917 printf(" metric %d", metric);
918 if (mtu)
919 printf(" mtu %d", mtu);
920 putchar('\n');
921 if (ap && alen > 0) {
922 printf("\taddress:");
923 for (i = 0; i < alen; i++, ap++)
924 printf("%c%02x", i > 0 ? ':' : ' ', *ap);
925 putchar('\n');
926 }
927
928 (void) memset(&ifmr, 0, sizeof(ifmr));
929 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
930
931 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
932 /*
933 * Interface doesn't support SIOC{G,S}IFMEDIA.
934 */
935 goto proto_status;
936 }
937
938 if (ifmr.ifm_count == 0) {
939 warnx("%s: no media types?", name);
940 goto proto_status;
941 }
942
943 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
944 if (media_list == NULL)
945 err(1, "malloc");
946 ifmr.ifm_ulist = media_list;
947
948 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
949 err(1, "SIOCGIFMEDIA");
950
951 printf("\tmedia: ");
952 print_media_word(ifmr.ifm_current, 1);
953 if (ifmr.ifm_active != ifmr.ifm_current) {
954 putchar(' ');
955 putchar('(');
956 print_media_word(ifmr.ifm_active, 0);
957 putchar(')');
958 }
959 putchar('\n');
960
961 if (ifmr.ifm_status & IFM_AVALID) {
962 printf("\tstatus: ");
963 switch (IFM_TYPE(ifmr.ifm_active)) {
964 case IFM_ETHER:
965 if (ifmr.ifm_status & IFM_ACTIVE)
966 printf("active");
967 else
968 printf("no carrier");
969 break;
970
971 case IFM_FDDI:
972 case IFM_TOKEN:
973 if (ifmr.ifm_status & IFM_ACTIVE)
974 printf("inserted");
975 else
976 printf("no ring");
977 break;
978 default:
979 printf("unknown");
980 }
981 putchar('\n');
982 }
983
984 if (mflag) {
985 printf("\tsupported media:\n");
986 for (i = 0; i < ifmr.ifm_count; i++) {
987 printf("\t\t");
988 print_media_word(media_list[i], 1);
989 printf("\n");
990 }
991 }
992
993 free(media_list);
994
995 proto_status:
996 if ((p = afp) != NULL) {
997 (*p->af_status)(1);
998 } else for (p = afs; p->af_name; p++) {
999 ifr.ifr_addr.sa_family = p->af_af;
1000 (*p->af_status)(0);
1001 }
1002 }
1003
1004 void
1005 in_status(force)
1006 int force;
1007 {
1008 struct sockaddr_in *sin;
1009
1010 getsock(AF_INET);
1011 if (s < 0) {
1012 if (errno == EPROTONOSUPPORT)
1013 return;
1014 err(1, "socket");
1015 }
1016 (void) memset(&ifr, 0, sizeof(ifr));
1017 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1018 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1019 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1020 if (!force)
1021 return;
1022 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1023 } else
1024 warn("SIOCGIFADDR");
1025 }
1026 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1027 sin = (struct sockaddr_in *)&ifr.ifr_addr;
1028 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1029 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1030 if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
1031 if (errno != EADDRNOTAVAIL)
1032 warn("SIOCGIFNETMASK");
1033 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1034 } else
1035 netmask.sin_addr =
1036 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
1037 if (flags & IFF_POINTOPOINT) {
1038 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1039 if (errno == EADDRNOTAVAIL)
1040 (void) memset(&ifr.ifr_addr, 0,
1041 sizeof(ifr.ifr_addr));
1042 else
1043 warn("SIOCGIFDSTADDR");
1044 }
1045 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1046 sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
1047 printf("--> %s ", inet_ntoa(sin->sin_addr));
1048 }
1049 printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
1050 if (flags & IFF_BROADCAST) {
1051 if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
1052 if (errno == EADDRNOTAVAIL)
1053 (void) memset(&ifr.ifr_addr, 0,
1054 sizeof(ifr.ifr_addr));
1055 else
1056 warn("SIOCGIFBRDADDR");
1057 }
1058 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1059 sin = (struct sockaddr_in *)&ifr.ifr_addr;
1060 if (sin->sin_addr.s_addr != 0)
1061 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1062 }
1063 putchar('\n');
1064 }
1065
1066 #ifndef INET_ONLY
1067
1068 void
1069 at_status(force)
1070 int force;
1071 {
1072 struct sockaddr_at *sat, null_sat;
1073 struct netrange *nr;
1074
1075 getsock(AF_APPLETALK);
1076 if (s < 0) {
1077 if (errno == EPROTONOSUPPORT)
1078 return;
1079 err(1, "socket");
1080 }
1081 (void) memset(&ifr, 0, sizeof(ifr));
1082 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1083 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1084 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1085 if (!force)
1086 return;
1087 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1088 } else
1089 warn("SIOCGIFADDR");
1090 }
1091 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1092 sat = (struct sockaddr_at *)&ifr.ifr_addr;
1093
1094 (void) memset(&null_sat, 0, sizeof(null_sat));
1095
1096 nr = (struct netrange *) &sat->sat_zero;
1097 printf("\tatalk %d.%d range %d-%d phase %d",
1098 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1099 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1100 if (flags & IFF_POINTOPOINT) {
1101 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1102 if (errno == EADDRNOTAVAIL)
1103 (void) memset(&ifr.ifr_addr, 0,
1104 sizeof(ifr.ifr_addr));
1105 else
1106 warn("SIOCGIFDSTADDR");
1107 }
1108 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1109 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
1110 if (!sat)
1111 sat = &null_sat;
1112 printf("--> %d.%d",
1113 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1114 }
1115 if (flags & IFF_BROADCAST) {
1116 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1117 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
1118 if (sat)
1119 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
1120 sat->sat_addr.s_node);
1121 }
1122 putchar('\n');
1123 }
1124
1125 void
1126 xns_status(force)
1127 int force;
1128 {
1129 struct sockaddr_ns *sns;
1130
1131 getsock(AF_NS);
1132 if (s < 0) {
1133 if (errno == EPROTONOSUPPORT)
1134 return;
1135 err(1, "socket");
1136 }
1137 (void) memset(&ifr, 0, sizeof(ifr));
1138 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1139 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1140 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1141 if (!force)
1142 return;
1143 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1144 } else
1145 warn("SIOCGIFADDR");
1146 }
1147 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1148 sns = (struct sockaddr_ns *)&ifr.ifr_addr;
1149 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1150 if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
1151 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1152 if (errno == EADDRNOTAVAIL)
1153 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1154 else
1155 warn("SIOCGIFDSTADDR");
1156 }
1157 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1158 sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
1159 printf("--> %s ", ns_ntoa(sns->sns_addr));
1160 }
1161 putchar('\n');
1162 }
1163
1164 void
1165 iso_status(force)
1166 int force;
1167 {
1168 struct sockaddr_iso *siso;
1169
1170 getsock(AF_ISO);
1171 if (s < 0) {
1172 if (errno == EPROTONOSUPPORT)
1173 return;
1174 err(1, "socket");
1175 }
1176 (void) memset(&ifr, 0, sizeof(ifr));
1177 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1178 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1179 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1180 if (!force)
1181 return;
1182 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1183 } else
1184 warn("SIOCGIFADDR");
1185 }
1186 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1187 siso = (struct sockaddr_iso *)&ifr.ifr_addr;
1188 printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
1189 if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
1190 if (errno == EADDRNOTAVAIL)
1191 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1192 else
1193 warn("SIOCGIFNETMASK");
1194 } else {
1195 printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
1196 }
1197 if (flags & IFF_POINTOPOINT) {
1198 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1199 if (errno == EADDRNOTAVAIL)
1200 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1201 else
1202 warn("SIOCGIFDSTADDR");
1203 }
1204 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1205 siso = (struct sockaddr_iso *)&ifr.ifr_addr;
1206 printf("--> %s ", iso_ntoa(&siso->siso_addr));
1207 }
1208 putchar('\n');
1209 }
1210
1211 #endif /* INET_ONLY */
1212
1213 #define SIN(x) ((struct sockaddr_in *) &(x))
1214 struct sockaddr_in *sintab[] = {
1215 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1216 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1217
1218 void
1219 in_getaddr(s, which)
1220 char *s;
1221 int which;
1222 {
1223 struct sockaddr_in *sin = sintab[which];
1224 struct hostent *hp;
1225 struct netent *np;
1226
1227 sin->sin_len = sizeof(*sin);
1228 if (which != MASK)
1229 sin->sin_family = AF_INET;
1230
1231 if (inet_aton(s, &sin->sin_addr) == 0) {
1232 if ((hp = gethostbyname(s)) != NULL)
1233 (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
1234 else if ((np = getnetbyname(s)) != NULL)
1235 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1236 else
1237 errx(1, "%s: bad value", s);
1238 }
1239 }
1240
1241 /*
1242 * Print a value a la the %b format of the kernel's printf
1243 */
1244 void
1245 printb(s, v, bits)
1246 char *s;
1247 char *bits;
1248 unsigned short v;
1249 {
1250 int i, any = 0;
1251 char c;
1252
1253 if (bits && *bits == 8)
1254 printf("%s=%o", s, v);
1255 else
1256 printf("%s=%x", s, v);
1257 bits++;
1258 if (bits) {
1259 putchar('<');
1260 while ((i = *bits++) != 0) {
1261 if (v & (1 << (i-1))) {
1262 if (any)
1263 putchar(',');
1264 any = 1;
1265 for (; (c = *bits) > 32; bits++)
1266 putchar(c);
1267 } else
1268 for (; *bits > 32; bits++)
1269 ;
1270 }
1271 putchar('>');
1272 }
1273 }
1274
1275 #ifndef INET_ONLY
1276
1277 void
1278 at_getaddr(addr, which)
1279 char *addr;
1280 int which;
1281 {
1282 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1283 u_int net, node;
1284
1285 sat->sat_family = AF_APPLETALK;
1286 sat->sat_len = sizeof(*sat);
1287 if (which == MASK)
1288 errx(1, "AppleTalk does not use netmasks\n");
1289 if (sscanf(addr, "%u.%u", &net, &node) != 2
1290 || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
1291 errx(1, "%s: illegal address", addr);
1292 sat->sat_addr.s_net = htons(net);
1293 sat->sat_addr.s_node = node;
1294 }
1295
1296 void
1297 setatrange(range, d)
1298 char *range;
1299 int d;
1300 {
1301 u_short first = 123, last = 123;
1302
1303 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1304 || first == 0 || first > 0xffff
1305 || last == 0 || last > 0xffff || first > last)
1306 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1307 at_nr.nr_firstnet = htons(first);
1308 at_nr.nr_lastnet = htons(last);
1309 }
1310
1311 void
1312 setatphase(phase, d)
1313 char *phase;
1314 int d;
1315 {
1316 if (!strcmp(phase, "1"))
1317 at_nr.nr_phase = 1;
1318 else if (!strcmp(phase, "2"))
1319 at_nr.nr_phase = 2;
1320 else
1321 errx(1, "%s: illegal phase", phase);
1322 }
1323
1324 void
1325 checkatrange(sat)
1326 struct sockaddr_at *sat;
1327 {
1328 if (at_nr.nr_phase == 0)
1329 at_nr.nr_phase = 2; /* Default phase 2 */
1330 if (at_nr.nr_firstnet == 0)
1331 at_nr.nr_firstnet = /* Default range of one */
1332 at_nr.nr_lastnet = sat->sat_addr.s_net;
1333 printf("\tatalk %d.%d range %d-%d phase %d\n",
1334 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1335 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1336 if ((u_short) ntohs(at_nr.nr_firstnet) >
1337 (u_short) ntohs(sat->sat_addr.s_net)
1338 || (u_short) ntohs(at_nr.nr_lastnet) <
1339 (u_short) ntohs(sat->sat_addr.s_net))
1340 errx(1, "AppleTalk address is not in range");
1341 *((struct netrange *) &sat->sat_zero) = at_nr;
1342 }
1343
1344 #define SNS(x) ((struct sockaddr_ns *) &(x))
1345 struct sockaddr_ns *snstab[] = {
1346 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1347 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1348
1349 void
1350 xns_getaddr(addr, which)
1351 char *addr;
1352 int which;
1353 {
1354 struct sockaddr_ns *sns = snstab[which];
1355
1356 sns->sns_family = AF_NS;
1357 sns->sns_len = sizeof(*sns);
1358 sns->sns_addr = ns_addr(addr);
1359 if (which == MASK)
1360 puts("Attempt to set XNS netmask will be ineffectual");
1361 }
1362
1363 #define SISO(x) ((struct sockaddr_iso *) &(x))
1364 struct sockaddr_iso *sisotab[] = {
1365 SISO(ridreq.ifr_addr), SISO(iso_addreq.ifra_addr),
1366 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
1367
1368 void
1369 iso_getaddr(addr, which)
1370 char *addr;
1371 int which;
1372 {
1373 struct sockaddr_iso *siso = sisotab[which];
1374 siso->siso_addr = *iso_addr(addr);
1375
1376 if (which == MASK) {
1377 siso->siso_len = TSEL(siso) - (caddr_t)(siso);
1378 siso->siso_nlen = 0;
1379 } else {
1380 siso->siso_len = sizeof(*siso);
1381 siso->siso_family = AF_ISO;
1382 }
1383 }
1384
1385 void
1386 setsnpaoffset(val, d)
1387 char *val;
1388 int d;
1389 {
1390 iso_addreq.ifra_snpaoffset = atoi(val);
1391 }
1392
1393 void
1394 setnsellength(val, d)
1395 char *val;
1396 int d;
1397 {
1398 nsellength = atoi(val);
1399 if (nsellength < 0)
1400 errx(1, "Negative NSEL length is absurd");
1401 if (afp == 0 || afp->af_af != AF_ISO)
1402 errx(1, "Setting NSEL length valid only for iso");
1403 }
1404
1405 void
1406 fixnsel(s)
1407 struct sockaddr_iso *s;
1408 {
1409 if (s->siso_family == 0)
1410 return;
1411 s->siso_tlen = nsellength;
1412 }
1413
1414 void
1415 adjust_nsellength()
1416 {
1417 fixnsel(sisotab[RIDADDR]);
1418 fixnsel(sisotab[ADDR]);
1419 fixnsel(sisotab[DSTADDR]);
1420 }
1421
1422 #endif /* INET_ONLY */
1423
1424 void
1425 usage()
1426 {
1427 fprintf(stderr,
1428 "usage: ifconfig [ -m ] interface\n%s%s%s%s%s%s%s%s%s%s%s",
1429 "\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ",
1430 "[ netmask mask ] ]\n",
1431 "\t[ metric n ]\n",
1432 "\t[ mtu n ]\n",
1433 "\t[ arp | -arp ]\n",
1434 "\t[ media mtype ]\n",
1435 "\t[ mediaopt mopts ]\n",
1436 "\t[ -mediaopt mopts ]\n",
1437 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n",
1438 " ifconfig -a [ -m ] [ -d ] [ -u ] [ af ]\n",
1439 " ifconfig -l [ -d ] [ -u ]\n");
1440 exit(1);
1441 }
1442