ifconfig.c revision 1.127 1 /* $NetBSD: ifconfig.c,v 1.127 2002/06/14 09:02:00 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.127 2002/06/14 09:02:00 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 <net/if_vlanvar.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #ifdef INET6
100 #include <netinet6/nd6.h>
101 #endif
102 #include <arpa/inet.h>
103
104 #include <netatalk/at.h>
105
106 #define NSIP
107 #include <netns/ns.h>
108 #include <netns/ns_if.h>
109 #include <netdb.h>
110
111 #define EON
112 #include <netiso/iso.h>
113 #include <netiso/iso_var.h>
114 #include <sys/protosw.h>
115
116 #include <ctype.h>
117 #include <err.h>
118 #include <errno.h>
119 #include <stddef.h>
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <string.h>
123 #include <unistd.h>
124 #include <ifaddrs.h>
125
126 struct ifreq ifr, ridreq;
127 struct ifaliasreq addreq __attribute__((aligned(4)));
128 struct in_aliasreq in_addreq;
129 #ifdef INET6
130 struct in6_ifreq ifr6;
131 struct in6_ifreq in6_ridreq;
132 struct in6_aliasreq in6_addreq;
133 #endif
134 struct iso_ifreq iso_ridreq;
135 struct iso_aliasreq iso_addreq;
136 struct sockaddr_in netmask;
137 struct netrange at_nr; /* AppleTalk net range */
138
139 char name[30];
140 int flags, setaddr, setipdst, doalias;
141 u_long metric, mtu;
142 int clearaddr, s;
143 int newaddr = -1;
144 int conflicting = 0;
145 int nsellength = 1;
146 int af;
147 int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag;
148 #ifdef INET6
149 int Lflag;
150 #endif
151 int reset_if_flags;
152 int explicit_prefix = 0;
153 u_int vlan_tag = (u_int)-1;
154
155 struct ifcapreq g_ifcr;
156 int g_ifcr_updated;
157
158 void notealias __P((const char *, int));
159 void notrailers __P((const char *, int));
160 void setifaddr __P((const char *, int));
161 void setifdstaddr __P((const char *, int));
162 void setifflags __P((const char *, int));
163 void setifcaps __P((const char *, int));
164 void setifbroadaddr __P((const char *, int));
165 void setifipdst __P((const char *, int));
166 void setifmetric __P((const char *, int));
167 void setifmtu __P((const char *, int));
168 void setifnwid __P((const char *, int));
169 void setifnwkey __P((const char *, int));
170 void setifpowersave __P((const char *, int));
171 void setifpowersavesleep __P((const char *, int));
172 void setifnetmask __P((const char *, int));
173 void setifprefixlen __P((const char *, int));
174 void setnsellength __P((const char *, int));
175 void setsnpaoffset __P((const char *, int));
176 void setatrange __P((const char *, int));
177 void setatphase __P((const char *, int));
178 void settunnel __P((const char *, const char *));
179 void deletetunnel __P((const char *, int));
180 #ifdef INET6
181 void setia6flags __P((const char *, int));
182 void setia6pltime __P((const char *, int));
183 void setia6vltime __P((const char *, int));
184 void setia6lifetime __P((const char *, const char *));
185 void setia6eui64 __P((const char *, int));
186 #endif
187 void checkatrange __P ((struct sockaddr_at *));
188 void setmedia __P((const char *, int));
189 void setmediaopt __P((const char *, int));
190 void unsetmediaopt __P((const char *, int));
191 void setmediainst __P((const char *, int));
192 void clone_create __P((const char *, int));
193 void clone_destroy __P((const char *, int));
194 void fixnsel __P((struct sockaddr_iso *));
195 void setvlan __P((const char *, int));
196 void setvlanif __P((const char *, int));
197 void unsetvlanif __P((const char *, int));
198 int main __P((int, char *[]));
199
200 /*
201 * Media stuff. Whenever a media command is first performed, the
202 * currently select media is grabbed for this interface. If `media'
203 * is given, the current media word is modifed. `mediaopt' commands
204 * only modify the set and clear words. They then operate on the
205 * current media word later.
206 */
207 int media_current;
208 int mediaopt_set;
209 int mediaopt_clear;
210
211 int actions; /* Actions performed */
212
213 #define A_MEDIA 0x0001 /* media command */
214 #define A_MEDIAOPTSET 0x0002 /* mediaopt command */
215 #define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */
216 #define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR)
217 #define A_MEDIAINST 0x0008 /* instance or inst command */
218
219 #define NEXTARG 0xffffff
220 #define NEXTARG2 0xfffffe
221
222 const struct cmd {
223 const char *c_name;
224 int c_parameter; /* NEXTARG means next argv */
225 int c_action; /* defered action */
226 void (*c_func) __P((const char *, int));
227 void (*c_func2) __P((const char *, const char *));
228 } cmds[] = {
229 { "up", IFF_UP, 0, setifflags } ,
230 { "down", -IFF_UP, 0, setifflags },
231 { "trailers", -1, 0, notrailers },
232 { "-trailers", 1, 0, notrailers },
233 { "arp", -IFF_NOARP, 0, setifflags },
234 { "-arp", IFF_NOARP, 0, setifflags },
235 { "debug", IFF_DEBUG, 0, setifflags },
236 { "-debug", -IFF_DEBUG, 0, setifflags },
237 { "alias", IFF_UP, 0, notealias },
238 { "-alias", -IFF_UP, 0, notealias },
239 { "delete", -IFF_UP, 0, notealias },
240 #ifdef notdef
241 #define EN_SWABIPS 0x1000
242 { "swabips", EN_SWABIPS, 0, setifflags },
243 { "-swabips", -EN_SWABIPS, 0, setifflags },
244 #endif
245 { "netmask", NEXTARG, 0, setifnetmask },
246 { "metric", NEXTARG, 0, setifmetric },
247 { "mtu", NEXTARG, 0, setifmtu },
248 { "nwid", NEXTARG, 0, setifnwid },
249 { "nwkey", NEXTARG, 0, setifnwkey },
250 { "-nwkey", -1, 0, setifnwkey },
251 { "powersave", 1, 0, setifpowersave },
252 { "-powersave", 0, 0, setifpowersave },
253 { "powersavesleep", NEXTARG, 0, setifpowersavesleep },
254 { "broadcast", NEXTARG, 0, setifbroadaddr },
255 { "ipdst", NEXTARG, 0, setifipdst },
256 { "prefixlen", NEXTARG, 0, setifprefixlen},
257 #ifdef INET6
258 { "anycast", IN6_IFF_ANYCAST, 0, setia6flags },
259 { "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags },
260 { "tentative", IN6_IFF_TENTATIVE, 0, setia6flags },
261 { "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags },
262 { "deprecated", IN6_IFF_DEPRECATED, 0, setia6flags },
263 { "-deprecated", -IN6_IFF_DEPRECATED, 0, setia6flags },
264 { "pltime", NEXTARG, 0, setia6pltime },
265 { "vltime", NEXTARG, 0, setia6vltime },
266 { "eui64", 0, 0, setia6eui64 },
267 #endif /*INET6*/
268 #ifndef INET_ONLY
269 { "range", NEXTARG, 0, setatrange },
270 { "phase", NEXTARG, 0, setatphase },
271 { "snpaoffset", NEXTARG, 0, setsnpaoffset },
272 { "nsellength", NEXTARG, 0, setnsellength },
273 #endif /* INET_ONLY */
274 { "tunnel", NEXTARG2, 0, NULL,
275 settunnel } ,
276 { "deletetunnel", 0, 0, deletetunnel },
277 { "vlan", NEXTARG, 0, setvlan } ,
278 { "vlanif", NEXTARG, 0, setvlanif } ,
279 { "-vlanif", 0, 0, unsetvlanif } ,
280 #if 0
281 /* XXX `create' special-cased below */
282 { "create", 0, 0, clone_create } ,
283 #endif
284 { "destroy", 0, 0, clone_destroy } ,
285 { "link0", IFF_LINK0, 0, setifflags } ,
286 { "-link0", -IFF_LINK0, 0, setifflags } ,
287 { "link1", IFF_LINK1, 0, setifflags } ,
288 { "-link1", -IFF_LINK1, 0, setifflags } ,
289 { "link2", IFF_LINK2, 0, setifflags } ,
290 { "-link2", -IFF_LINK2, 0, setifflags } ,
291 { "media", NEXTARG, A_MEDIA, setmedia },
292 { "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt },
293 { "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt },
294 { "instance", NEXTARG, A_MEDIAINST, setmediainst },
295 { "inst", NEXTARG, A_MEDIAINST, setmediainst },
296 { "ip4csum", IFCAP_CSUM_IPv4,0, setifcaps },
297 { "-ip4csum", -IFCAP_CSUM_IPv4,0, setifcaps },
298 { "tcp4csum", IFCAP_CSUM_TCPv4,0, setifcaps },
299 { "-tcp4csum", -IFCAP_CSUM_TCPv4,0, setifcaps },
300 { "udp4csum", IFCAP_CSUM_UDPv4,0, setifcaps },
301 { "-udp4csum", -IFCAP_CSUM_UDPv4,0, setifcaps },
302 { "tcp6csum", IFCAP_CSUM_TCPv6,0, setifcaps },
303 { "-tcp6csum", -IFCAP_CSUM_TCPv6,0, setifcaps },
304 { "udp6csum", IFCAP_CSUM_UDPv6,0, setifcaps },
305 { "-udp6csum", -IFCAP_CSUM_UDPv6,0, setifcaps },
306 { "tcp4csum-rx",IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
307 { "-tcp4csum-rx",-IFCAP_CSUM_TCPv4_Rx,0, setifcaps },
308 { "udp4csum-rx",IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
309 { "-udp4csum-rx",-IFCAP_CSUM_UDPv4_Rx,0, setifcaps },
310 { 0, 0, 0, setifaddr },
311 { 0, 0, 0, setifdstaddr },
312 };
313
314 void adjust_nsellength __P((void));
315 int getinfo __P((struct ifreq *));
316 int carrier __P((void));
317 void getsock __P((int));
318 void printall __P((const char *));
319 void list_cloners __P((void));
320 void printb __P((const char *, unsigned short, const char *));
321 int prefix __P((void *, int));
322 void status __P((const struct sockaddr_dl *));
323 void usage __P((void));
324 const char *get_string __P((const char *, const char *, u_int8_t *, int *));
325 void print_string __P((const u_int8_t *, int));
326 char *sec2str __P((time_t));
327
328 const char *get_media_type_string __P((int));
329 const char *get_media_subtype_string __P((int));
330 int get_media_subtype __P((int, const char *));
331 int get_media_options __P((int, const char *));
332 int lookup_media_word __P((struct ifmedia_description *, int,
333 const char *));
334 void print_media_word __P((int, int, int));
335 void process_media_commands __P((void));
336 void init_current_media __P((void));
337
338 /*
339 * XNS support liberally adapted from code written at the University of
340 * Maryland principally by James O'Toole and Chris Torek.
341 */
342 void in_alias __P((struct ifreq *));
343 void in_status __P((int));
344 void in_getaddr __P((const char *, int));
345 void in_getprefix __P((const char *, int));
346 #ifdef INET6
347 void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
348 void in6_alias __P((struct in6_ifreq *));
349 void in6_status __P((int));
350 void in6_getaddr __P((const char *, int));
351 void in6_getprefix __P((const char *, int));
352 #endif
353 void at_status __P((int));
354 void at_getaddr __P((const char *, int));
355 void xns_status __P((int));
356 void xns_getaddr __P((const char *, int));
357 void iso_status __P((int));
358 void iso_getaddr __P((const char *, int));
359
360 void ieee80211_status __P((void));
361 void tunnel_status __P((void));
362 void vlan_status __P((void));
363
364 /* Known address families */
365 struct afswtch {
366 const char *af_name;
367 short af_af;
368 void (*af_status) __P((int));
369 void (*af_getaddr) __P((const char *, int));
370 void (*af_getprefix) __P((const char *, int));
371 u_long af_difaddr;
372 u_long af_aifaddr;
373 u_long af_gifaddr;
374 caddr_t af_ridreq;
375 caddr_t af_addreq;
376 } afs[] = {
377 #define C(x) ((caddr_t) &x)
378 { "inet", AF_INET, in_status, in_getaddr, in_getprefix,
379 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(in_addreq) },
380 #ifdef INET6
381 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
382 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
383 /*
384 * Deleting the first address before setting new one is
385 * not prefered way in this protocol.
386 */
387 0,
388 C(in6_ridreq), C(in6_addreq) },
389 #endif
390 #ifndef INET_ONLY /* small version, for boot media */
391 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
392 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(addreq), C(addreq) },
393 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
394 SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
395 { "iso", AF_ISO, iso_status, iso_getaddr, NULL,
396 SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
397 C(iso_ridreq), C(iso_addreq) },
398 #endif /* INET_ONLY */
399 { 0, 0, 0, 0 }
400 };
401
402 struct afswtch *afp; /*the address family being set or asked about*/
403
404 struct afswtch *lookup_af __P((const char *));
405
406 int
407 main(argc, argv)
408 int argc;
409 char *argv[];
410 {
411 struct ifreq ifreq;
412 int ch;
413
414 /* Parse command-line options */
415 aflag = mflag = vflag = 0;
416 while ((ch = getopt(argc, argv, "AabCdlmsuv"
417 #ifdef INET6
418 "L"
419 #endif
420 )) != -1) {
421 switch (ch) {
422 case 'A':
423 warnx("-A is deprecated");
424 break;
425
426 case 'a':
427 aflag = 1;
428 break;
429
430 case 'b':
431 bflag = 1;
432 break;
433
434
435 case 'C':
436 Cflag = 1;
437 break;
438
439 case 'd':
440 dflag = 1;
441 break;
442
443 #ifdef INET6
444 case 'L':
445 Lflag = 1;
446 break;
447 #endif
448
449 case 'l':
450 lflag = 1;
451 break;
452
453 case 'm':
454 mflag = 1;
455 break;
456
457 case 's':
458 sflag = 1;
459 break;
460
461 case 'u':
462 uflag = 1;
463 break;
464
465 case 'v':
466 vflag = 1;
467 break;
468
469
470 default:
471 usage();
472 /* NOTREACHED */
473 }
474 }
475 argc -= optind;
476 argv += optind;
477
478 /*
479 * -l means "list all interfaces", and is mutally exclusive with
480 * all other flags/commands.
481 *
482 * -C means "list all names of cloners", and it mutually exclusive
483 * with all other flags/commands.
484 *
485 * -a means "print status of all interfaces".
486 */
487 if ((lflag || Cflag) && (aflag || mflag || vflag || argc))
488 usage();
489 #ifdef INET6
490 if ((lflag || Cflag) && Lflag)
491 usage();
492 #endif
493 if (lflag && Cflag)
494 usage();
495 if (Cflag) {
496 if (argc)
497 usage();
498 list_cloners();
499 exit(0);
500 }
501 if (aflag || lflag) {
502 if (argc > 1)
503 usage();
504 else if (argc == 1) {
505 afp = lookup_af(argv[0]);
506 if (afp == NULL)
507 usage();
508 }
509 if (afp)
510 af = ifr.ifr_addr.sa_family = afp->af_af;
511 else
512 af = ifr.ifr_addr.sa_family = afs[0].af_af;
513 printall(NULL);
514 exit(0);
515 }
516
517 /* Make sure there's an interface name. */
518 if (argc < 1)
519 usage();
520 (void) strncpy(name, argv[0], sizeof(name));
521 argc--; argv++;
522
523 /*
524 * NOTE: We must special-case the `create' command right
525 * here as we would otherwise fail in getinfo().
526 */
527 if (argc > 0 && strcmp(argv[0], "create") == 0) {
528 clone_create(argv[0], 0);
529 argc--, argv++;
530 if (argc == 0)
531 exit(0);
532 }
533
534 /* Check for address family. */
535 afp = NULL;
536 if (argc > 0) {
537 afp = lookup_af(argv[0]);
538 if (afp != NULL) {
539 argv++;
540 argc--;
541 }
542 }
543
544 /* Initialize af, just for use in getinfo(). */
545 if (afp == NULL)
546 af = afs->af_af;
547 else
548 af = afp->af_af;
549
550 /* Get information about the interface. */
551 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
552 if (getinfo(&ifr) < 0)
553 exit(1);
554
555 if (sflag) {
556 if (argc != 0)
557 usage();
558 else
559 exit(carrier());
560 }
561
562 /* No more arguments means interface status. */
563 if (argc == 0) {
564 printall(name);
565 exit(0);
566 }
567
568 /* The following operations assume inet family as the default. */
569 if (afp == NULL)
570 afp = afs;
571 af = ifr.ifr_addr.sa_family = afp->af_af;
572
573 #ifdef INET6
574 /* initialization */
575 in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
576 in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
577 #endif
578
579 /* Process commands. */
580 while (argc > 0) {
581 const struct cmd *p;
582
583 for (p = cmds; p->c_name; p++)
584 if (strcmp(argv[0], p->c_name) == 0)
585 break;
586 if (p->c_name == 0 && setaddr) {
587 if ((flags & IFF_POINTOPOINT) == 0) {
588 errx(EXIT_FAILURE,
589 "can't set destination address %s",
590 "on non-point-to-point link");
591 }
592 p++; /* got src, do dst */
593 }
594 if (p->c_func != NULL || p->c_func2 != NULL) {
595 if (p->c_parameter == NEXTARG) {
596 if (argc < 2)
597 errx(EXIT_FAILURE,
598 "'%s' requires argument",
599 p->c_name);
600 (*p->c_func)(argv[1], 0);
601 argc--, argv++;
602 } else if (p->c_parameter == NEXTARG2) {
603 if (argc < 3)
604 errx(EXIT_FAILURE,
605 "'%s' requires 2 arguments",
606 p->c_name);
607 (*p->c_func2)(argv[1], argv[2]);
608 argc -= 2, argv += 2;
609 } else
610 (*p->c_func)(argv[0], p->c_parameter);
611 actions |= p->c_action;
612 }
613 argc--, argv++;
614 }
615
616 /*
617 * See if multiple alias, -alias, or delete commands were
618 * specified. More than one constitutes an invalid command line
619 */
620
621 if (conflicting > 1)
622 err(EXIT_FAILURE,
623 "Only one use of alias, -alias or delete is valid.");
624
625 /* Process any media commands that may have been issued. */
626 process_media_commands();
627
628 if (af == AF_INET6 && explicit_prefix == 0) {
629 /*
630 * Aggregatable address architecture defines all prefixes
631 * are 64. So, it is convenient to set prefixlen to 64 if
632 * it is not specified.
633 */
634 setifprefixlen("64", 0);
635 /* in6_getprefix("64", MASK) if MASK is available here... */
636 }
637
638 #ifndef INET_ONLY
639 if (af == AF_ISO)
640 adjust_nsellength();
641
642 if (af == AF_APPLETALK)
643 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
644
645 if (setipdst && af==AF_NS) {
646 struct nsip_req rq;
647 int size = sizeof(rq);
648
649 rq.rq_ns = addreq.ifra_addr;
650 rq.rq_ip = addreq.ifra_dstaddr;
651
652 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
653 warn("encapsulation routing");
654 }
655
656 #endif /* INET_ONLY */
657
658 if (clearaddr) {
659 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
660 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
661 err(EXIT_FAILURE, "SIOCDIFADDR");
662 }
663 if (newaddr > 0) {
664 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
665 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
666 warn("SIOCAIFADDR");
667 }
668
669 if (g_ifcr_updated) {
670 (void) strncpy(g_ifcr.ifcr_name, name,
671 sizeof(g_ifcr.ifcr_name));
672 if (ioctl(s, SIOCSIFCAP, (caddr_t) &g_ifcr) == -1)
673 err(EXIT_FAILURE, "SIOCSIFCAP");
674 }
675
676 if (reset_if_flags) {
677 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
678 ifreq.ifr_flags = flags;
679 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
680 err(EXIT_FAILURE, "SIOCSIFFLAGS");
681 }
682 exit(0);
683 }
684
685 struct afswtch *
686 lookup_af(cp)
687 const char *cp;
688 {
689 struct afswtch *a;
690
691 for (a = afs; a->af_name != NULL; a++)
692 if (strcmp(a->af_name, cp) == 0)
693 return (a);
694 return (NULL);
695 }
696
697 void
698 getsock(naf)
699 int naf;
700 {
701 static int oaf = -1;
702
703 if (oaf == naf)
704 return;
705 if (oaf != -1)
706 close(s);
707 s = socket(naf, SOCK_DGRAM, 0);
708 if (s < 0)
709 oaf = -1;
710 else
711 oaf = naf;
712 }
713
714 int
715 getinfo(giifr)
716 struct ifreq *giifr;
717 {
718
719 getsock(af);
720 if (s < 0)
721 err(EXIT_FAILURE, "socket");
722 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)giifr) == -1) {
723 warn("SIOCGIFFLAGS %s", giifr->ifr_name);
724 return (-1);
725 }
726 flags = giifr->ifr_flags;
727 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)giifr) == -1) {
728 warn("SIOCGIFMETRIC %s", giifr->ifr_name);
729 metric = 0;
730 } else
731 metric = giifr->ifr_metric;
732 if (ioctl(s, SIOCGIFMTU, (caddr_t)giifr) == -1)
733 mtu = 0;
734 else
735 mtu = giifr->ifr_mtu;
736
737 memset(&g_ifcr, 0, sizeof(g_ifcr));
738 strcpy(g_ifcr.ifcr_name, giifr->ifr_name);
739 (void) ioctl(s, SIOCGIFCAP, (caddr_t) &g_ifcr);
740
741 return (0);
742 }
743
744 void
745 printall(ifname)
746 const char *ifname;
747 {
748 struct ifaddrs *ifap, *ifa;
749 struct ifreq paifr;
750 const struct sockaddr_dl *sdl = NULL;
751 int idx;
752 char *p;
753
754 if (getifaddrs(&ifap) != 0)
755 err(EXIT_FAILURE, "getifaddrs");
756 p = NULL;
757 idx = 0;
758 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
759 memset(&paifr, 0, sizeof(paifr));
760 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
761 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
762 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
763 ifa->ifa_addr->sa_len);
764 }
765
766 if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
767 continue;
768 if (ifa->ifa_addr->sa_family == AF_LINK)
769 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
770 if (p && strcmp(p, ifa->ifa_name) == 0)
771 continue;
772 (void) strncpy(name, ifa->ifa_name, sizeof(name));
773 name[sizeof(name) - 1] = '\0';
774 p = ifa->ifa_name;
775
776 if (getinfo(&paifr) < 0)
777 continue;
778 if (bflag && (ifa->ifa_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
779 continue;
780 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
781 continue;
782 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
783 continue;
784
785 if (sflag && carrier())
786 continue;
787 idx++;
788 /*
789 * Are we just listing the interfaces?
790 */
791 if (lflag) {
792 if (idx > 1)
793 putchar(' ');
794 fputs(name, stdout);
795 continue;
796 }
797
798 status(sdl);
799 sdl = NULL;
800 }
801 if (lflag)
802 putchar('\n');
803 freeifaddrs(ifap);
804 }
805
806 void
807 list_cloners(void)
808 {
809 struct if_clonereq ifcr;
810 char *cp, *buf;
811 int idx;
812
813 memset(&ifcr, 0, sizeof(ifcr));
814
815 getsock(AF_INET);
816
817 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
818 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
819
820 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
821 if (buf == NULL)
822 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
823
824 ifcr.ifcr_count = ifcr.ifcr_total;
825 ifcr.ifcr_buffer = buf;
826
827 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
828 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
829
830 /*
831 * In case some disappeared in the mean time, clamp it down.
832 */
833 if (ifcr.ifcr_count > ifcr.ifcr_total)
834 ifcr.ifcr_count = ifcr.ifcr_total;
835
836 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
837 if (idx > 0)
838 putchar(' ');
839 printf("%s", cp);
840 }
841
842 putchar('\n');
843 free(buf);
844 return;
845 }
846
847 /*ARGSUSED*/
848 void
849 clone_create(addr, param)
850 const char *addr;
851 int param;
852 {
853
854 /* We're called early... */
855 getsock(AF_INET);
856
857 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
858 if (ioctl(s, SIOCIFCREATE, &ifr) == -1)
859 err(EXIT_FAILURE, "SIOCIFCREATE");
860 }
861
862 /*ARGSUSED*/
863 void
864 clone_destroy(addr, param)
865 const char *addr;
866 int param;
867 {
868
869 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
870 if (ioctl(s, SIOCIFDESTROY, &ifr) == -1)
871 err(EXIT_FAILURE, "SIOCIFDESTROY");
872 }
873
874 #define RIDADDR 0
875 #define ADDR 1
876 #define MASK 2
877 #define DSTADDR 3
878
879 /*ARGSUSED*/
880 void
881 setifaddr(addr, param)
882 const char *addr;
883 int param;
884 {
885 struct ifreq *siifr; /* XXX */
886
887 /*
888 * Delay the ioctl to set the interface addr until flags are all set.
889 * The address interpretation may depend on the flags,
890 * and the flags may change when the address is set.
891 */
892 setaddr++;
893 if (newaddr == -1)
894 newaddr = 1;
895 if (doalias == 0 && afp->af_gifaddr != 0) {
896 siifr = (struct ifreq *)afp->af_ridreq;
897 (void) strncpy(siifr->ifr_name, name, sizeof(siifr->ifr_name));
898 siifr->ifr_addr.sa_family = afp->af_af;
899 if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
900 clearaddr = 1;
901 else if (errno == EADDRNOTAVAIL)
902 /* No address was assigned yet. */
903 ;
904 else
905 err(EXIT_FAILURE, "SIOCGIFADDR");
906 }
907
908 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
909 }
910
911 void
912 settunnel(src, dst)
913 const char *src, *dst;
914 {
915 struct addrinfo hints, *srcres, *dstres;
916 int ecode;
917 struct if_laddrreq req;
918
919 memset(&hints, 0, sizeof(hints));
920 hints.ai_family = afp->af_af;
921 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
922
923 if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
924 errx(EXIT_FAILURE, "error in parsing address string: %s",
925 gai_strerror(ecode));
926
927 if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
928 errx(EXIT_FAILURE, "error in parsing address string: %s",
929 gai_strerror(ecode));
930
931 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
932 errx(EXIT_FAILURE,
933 "source and destination address families do not match");
934
935 if (srcres->ai_addrlen > sizeof(req.addr) ||
936 dstres->ai_addrlen > sizeof(req.dstaddr))
937 errx(EXIT_FAILURE, "invalid sockaddr");
938
939 memset(&req, 0, sizeof(req));
940 strncpy(req.iflr_name, name, sizeof(req.iflr_name));
941 memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
942 memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
943
944 #ifdef INET6
945 if (req.addr.ss_family == AF_INET6) {
946 struct sockaddr_in6 *s6, *d;
947
948 s6 = (struct sockaddr_in6 *)&req.addr;
949 d = (struct sockaddr_in6 *)&req.dstaddr;
950 if (s6->sin6_scope_id != d->sin6_scope_id) {
951 errx(EXIT_FAILURE, "scope mismatch");
952 /* NOTREACHED */
953 }
954 #ifdef __KAME__
955 /* embed scopeid */
956 if (s6->sin6_scope_id &&
957 (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ||
958 IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))) {
959 *(u_int16_t *)&s6->sin6_addr.s6_addr[2] =
960 htons(s6->sin6_scope_id);
961 }
962 if (d->sin6_scope_id &&
963 (IN6_IS_ADDR_LINKLOCAL(&d->sin6_addr) ||
964 IN6_IS_ADDR_MC_LINKLOCAL(&d->sin6_addr))) {
965 *(u_int16_t *)&d->sin6_addr.s6_addr[2] =
966 htons(d->sin6_scope_id);
967 }
968 #endif
969 }
970 #endif
971
972 if (ioctl(s, SIOCSLIFPHYADDR, &req) == -1)
973 warn("SIOCSLIFPHYADDR");
974
975 freeaddrinfo(srcres);
976 freeaddrinfo(dstres);
977 }
978
979 /* ARGSUSED */
980 void
981 deletetunnel(vname, param)
982 const char *vname;
983 int param;
984 {
985
986 if (ioctl(s, SIOCDIFPHYADDR, &ifr) == -1)
987 err(EXIT_FAILURE, "SIOCDIFPHYADDR");
988 }
989
990 void setvlan(val, d)
991 const char *val;
992 int d;
993 {
994 struct vlanreq vlr;
995
996 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
997 !isdigit(ifr.ifr_name[4]))
998 errx(EXIT_FAILURE,
999 "``vlan'' valid only with vlan(4) interfaces");
1000
1001 vlan_tag = atoi(val);
1002
1003 memset(&vlr, 0, sizeof(vlr));
1004 ifr.ifr_data = (caddr_t)&vlr;
1005
1006 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1007 err(EXIT_FAILURE, "SIOCGETVLAN");
1008
1009 vlr.vlr_tag = vlan_tag;
1010
1011 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1012 err(EXIT_FAILURE, "SIOCSETVLAN");
1013 }
1014
1015 void setvlanif(val, d)
1016 const char *val;
1017 int d;
1018 {
1019 struct vlanreq vlr;
1020
1021 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
1022 !isdigit(ifr.ifr_name[4]))
1023 errx(EXIT_FAILURE,
1024 "``vlanif'' valid only with vlan(4) interfaces");
1025
1026 if (vlan_tag == (u_int)-1)
1027 errx(EXIT_FAILURE,
1028 "must specify both ``vlan'' and ``vlanif''");
1029
1030 memset(&vlr, 0, sizeof(vlr));
1031 ifr.ifr_data = (caddr_t)&vlr;
1032
1033 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1034 err(EXIT_FAILURE, "SIOCGETVLAN");
1035
1036 strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
1037 vlr.vlr_tag = vlan_tag;
1038
1039 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1040 err(EXIT_FAILURE, "SIOCSETVLAN");
1041 }
1042
1043 void unsetvlanif(val, d)
1044 const char *val;
1045 int d;
1046 {
1047 struct vlanreq vlr;
1048
1049 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
1050 !isdigit(ifr.ifr_name[4]))
1051 errx(EXIT_FAILURE,
1052 "``vlanif'' valid only with vlan(4) interfaces");
1053
1054 memset(&vlr, 0, sizeof(vlr));
1055 ifr.ifr_data = (caddr_t)&vlr;
1056
1057 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
1058 err(EXIT_FAILURE, "SIOCGETVLAN");
1059
1060 vlr.vlr_parent[0] = '\0';
1061 vlr.vlr_tag = 0;
1062
1063 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
1064 err(EXIT_FAILURE, "SIOCSETVLAN");
1065 }
1066
1067 void
1068 setifnetmask(addr, d)
1069 const char *addr;
1070 int d;
1071 {
1072 (*afp->af_getaddr)(addr, MASK);
1073 }
1074
1075 void
1076 setifbroadaddr(addr, d)
1077 const char *addr;
1078 int d;
1079 {
1080 (*afp->af_getaddr)(addr, DSTADDR);
1081 }
1082
1083 void
1084 setifipdst(addr, d)
1085 const char *addr;
1086 int d;
1087 {
1088 in_getaddr(addr, DSTADDR);
1089 setipdst++;
1090 clearaddr = 0;
1091 newaddr = 0;
1092 }
1093
1094 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1095 /*ARGSUSED*/
1096 void
1097 notealias(addr, param)
1098 const char *addr;
1099 int param;
1100 {
1101 if (setaddr && doalias == 0 && param < 0)
1102 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
1103 rqtosa(af_addreq)->sa_len);
1104 doalias = param;
1105 if (param < 0) {
1106 clearaddr = 1;
1107 newaddr = 0;
1108 conflicting++;
1109 } else {
1110 clearaddr = 0;
1111 conflicting++;
1112 }
1113 }
1114
1115 /*ARGSUSED*/
1116 void
1117 notrailers(vname, value)
1118 const char *vname;
1119 int value;
1120 {
1121 puts("Note: trailers are no longer sent, but always received");
1122 }
1123
1124 /*ARGSUSED*/
1125 void
1126 setifdstaddr(addr, param)
1127 const char *addr;
1128 int param;
1129 {
1130 (*afp->af_getaddr)(addr, DSTADDR);
1131 }
1132
1133 void
1134 setifflags(vname, value)
1135 const char *vname;
1136 int value;
1137 {
1138 struct ifreq ifreq;
1139
1140 (void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
1141 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) == -1)
1142 err(EXIT_FAILURE, "SIOCGIFFLAGS");
1143 flags = ifreq.ifr_flags;
1144
1145 if (value < 0) {
1146 value = -value;
1147 flags &= ~value;
1148 } else
1149 flags |= value;
1150 ifreq.ifr_flags = flags;
1151 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) == -1)
1152 err(EXIT_FAILURE, "SIOCSIFFLAGS");
1153
1154 reset_if_flags = 1;
1155 }
1156
1157 void
1158 setifcaps(vname, value)
1159 const char *vname;
1160 int value;
1161 {
1162
1163 if (value < 0) {
1164 value = -value;
1165 g_ifcr.ifcr_capenable &= ~value;
1166 } else
1167 g_ifcr.ifcr_capenable |= value;
1168
1169 g_ifcr_updated = 1;
1170 }
1171
1172 #ifdef INET6
1173 void
1174 setia6flags(vname, value)
1175 const char *vname;
1176 int value;
1177 {
1178
1179 if (value < 0) {
1180 value = -value;
1181 in6_addreq.ifra_flags &= ~value;
1182 } else
1183 in6_addreq.ifra_flags |= value;
1184 }
1185
1186 void
1187 setia6pltime(val, d)
1188 const char *val;
1189 int d;
1190 {
1191
1192 setia6lifetime("pltime", val);
1193 }
1194
1195 void
1196 setia6vltime(val, d)
1197 const char *val;
1198 int d;
1199 {
1200
1201 setia6lifetime("vltime", val);
1202 }
1203
1204 void
1205 setia6lifetime(cmd, val)
1206 const char *cmd;
1207 const char *val;
1208 {
1209 time_t newval, t;
1210 char *ep;
1211
1212 t = time(NULL);
1213 newval = (time_t)strtoul(val, &ep, 0);
1214 if (val == ep)
1215 errx(EXIT_FAILURE, "invalid %s", cmd);
1216 if (afp->af_af != AF_INET6)
1217 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1218 if (strcmp(cmd, "vltime") == 0) {
1219 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
1220 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
1221 } else if (strcmp(cmd, "pltime") == 0) {
1222 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
1223 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
1224 }
1225 }
1226
1227 void
1228 setia6eui64(cmd, val)
1229 const char *cmd;
1230 int val;
1231 {
1232 struct ifaddrs *ifap, *ifa;
1233 const struct sockaddr_in6 *sin6 = NULL;
1234 const struct in6_addr *lladdr = NULL;
1235 struct in6_addr *in6;
1236
1237 if (afp->af_af != AF_INET6)
1238 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
1239 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
1240 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
1241 errx(EXIT_FAILURE, "interface index is already filled");
1242 if (getifaddrs(&ifap) != 0)
1243 err(EXIT_FAILURE, "getifaddrs");
1244 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1245 if (ifa->ifa_addr->sa_family == AF_INET6) {
1246 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1247 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1248 lladdr = &sin6->sin6_addr;
1249 break;
1250 }
1251 }
1252 }
1253 if (!lladdr)
1254 errx(EXIT_FAILURE, "could not determine link local address");
1255
1256 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
1257
1258 freeifaddrs(ifap);
1259 }
1260 #endif
1261
1262 void
1263 setifmetric(val, d)
1264 const char *val;
1265 int d;
1266 {
1267 char *ep = NULL;
1268
1269 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1270 ifr.ifr_metric = strtoul(val, &ep, 10);
1271 if (!ep || *ep)
1272 errx(EXIT_FAILURE, "%s: invalid metric", val);
1273 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) == -1)
1274 warn("SIOCSIFMETRIC");
1275 }
1276
1277 void
1278 setifmtu(val, d)
1279 const char *val;
1280 int d;
1281 {
1282 char *ep = NULL;
1283
1284 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1285 ifr.ifr_mtu = strtoul(val, &ep, 10);
1286 if (!ep || *ep)
1287 errx(EXIT_FAILURE, "%s: invalid mtu", val);
1288 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) == -1)
1289 warn("SIOCSIFMTU");
1290 }
1291
1292 const char *
1293 get_string(val, sep, buf, lenp)
1294 const char *val, *sep;
1295 u_int8_t *buf;
1296 int *lenp;
1297 {
1298 int len;
1299 int hexstr;
1300 u_int8_t *p;
1301
1302 len = *lenp;
1303 p = buf;
1304 hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
1305 if (hexstr)
1306 val += 2;
1307 for (;;) {
1308 if (*val == '\0')
1309 break;
1310 if (sep != NULL && strchr(sep, *val) != NULL) {
1311 val++;
1312 break;
1313 }
1314 if (hexstr) {
1315 if (!isxdigit((u_char)val[0]) ||
1316 !isxdigit((u_char)val[1])) {
1317 warnx("bad hexadecimal digits");
1318 return NULL;
1319 }
1320 }
1321 if (p > buf + len) {
1322 if (hexstr)
1323 warnx("hexadecimal digits too long");
1324 else
1325 warnx("strings too long");
1326 return NULL;
1327 }
1328 if (hexstr) {
1329 #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
1330 *p++ = (tohex((u_char)val[0]) << 4) |
1331 tohex((u_char)val[1]);
1332 #undef tohex
1333 val += 2;
1334 } else
1335 *p++ = *val++;
1336 }
1337 len = p - buf;
1338 if (len < *lenp)
1339 memset(p, 0, *lenp - len);
1340 *lenp = len;
1341 return val;
1342 }
1343
1344 void
1345 print_string(buf, len)
1346 const u_int8_t *buf;
1347 int len;
1348 {
1349 int i;
1350 int hasspc;
1351
1352 i = 0;
1353 hasspc = 0;
1354 if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
1355 for (; i < len; i++) {
1356 if (!isprint(buf[i]))
1357 break;
1358 if (isspace(buf[i]))
1359 hasspc++;
1360 }
1361 }
1362 if (i == len) {
1363 if (hasspc || len == 0)
1364 printf("\"%.*s\"", len, buf);
1365 else
1366 printf("%.*s", len, buf);
1367 } else {
1368 printf("0x");
1369 for (i = 0; i < len; i++)
1370 printf("%02x", buf[i]);
1371 }
1372 }
1373
1374 void
1375 setifnwid(val, d)
1376 const char *val;
1377 int d;
1378 {
1379 struct ieee80211_nwid nwid;
1380 int len;
1381
1382 len = sizeof(nwid.i_nwid);
1383 if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
1384 return;
1385 nwid.i_len = len;
1386 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1387 ifr.ifr_data = (caddr_t)&nwid;
1388 if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) == -1)
1389 warn("SIOCS80211NWID");
1390 }
1391
1392 void
1393 setifnwkey(val, d)
1394 const char *val;
1395 int d;
1396 {
1397 struct ieee80211_nwkey nwkey;
1398 int i;
1399 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
1400
1401 nwkey.i_wepon = IEEE80211_NWKEY_WEP;
1402 nwkey.i_defkid = 1;
1403 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1404 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
1405 nwkey.i_key[i].i_keydat = keybuf[i];
1406 }
1407 if (d != 0) {
1408 /* disable WEP encryption */
1409 nwkey.i_wepon = 0;
1410 i = 0;
1411 } else if (strcasecmp("persist", val) == 0) {
1412 /* use all values from persistent memory */
1413 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
1414 nwkey.i_defkid = 0;
1415 for (i = 0; i < IEEE80211_WEP_NKID; i++)
1416 nwkey.i_key[i].i_keylen = -1;
1417 } else if (strncasecmp("persist:", val, 8) == 0) {
1418 val += 8;
1419 /* program keys in persistent memory */
1420 nwkey.i_wepon |= IEEE80211_NWKEY_PERSIST;
1421 goto set_nwkey;
1422 } else {
1423 set_nwkey:
1424 if (isdigit(val[0]) && val[1] == ':') {
1425 /* specifying a full set of four keys */
1426 nwkey.i_defkid = val[0] - '0';
1427 val += 2;
1428 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1429 val = get_string(val, ",", keybuf[i],
1430 &nwkey.i_key[i].i_keylen);
1431 if (val == NULL)
1432 return;
1433 }
1434 if (*val != '\0') {
1435 warnx("SIOCS80211NWKEY: too many keys.");
1436 return;
1437 }
1438 } else {
1439 val = get_string(val, NULL, keybuf[0],
1440 &nwkey.i_key[0].i_keylen);
1441 if (val == NULL)
1442 return;
1443 i = 1;
1444 }
1445 }
1446 for (; i < IEEE80211_WEP_NKID; i++)
1447 nwkey.i_key[i].i_keylen = 0;
1448 (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
1449 if (ioctl(s, SIOCS80211NWKEY, (caddr_t)&nwkey) == -1)
1450 warn("SIOCS80211NWKEY");
1451 }
1452
1453 void
1454 setifpowersave(val, d)
1455 const char *val;
1456 int d;
1457 {
1458 struct ieee80211_power power;
1459
1460 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1461 if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) == -1) {
1462 warn("SIOCG80211POWER");
1463 return;
1464 }
1465
1466 power.i_enabled = d;
1467 if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) == -1)
1468 warn("SIOCS80211POWER");
1469 }
1470
1471 void
1472 setifpowersavesleep(val, d)
1473 const char *val;
1474 int d;
1475 {
1476 struct ieee80211_power power;
1477
1478 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1479 if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) == -1) {
1480 warn("SIOCG80211POWER");
1481 return;
1482 }
1483
1484 power.i_maxsleep = atoi(val);
1485 if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) == -1)
1486 warn("SIOCS80211POWER");
1487 }
1488
1489 void
1490 ieee80211_status()
1491 {
1492 int i, nwkey_verbose;
1493 struct ieee80211_nwid nwid;
1494 struct ieee80211_nwkey nwkey;
1495 struct ieee80211_power power;
1496 u_int8_t keybuf[IEEE80211_WEP_NKID][16];
1497
1498 memset(&ifr, 0, sizeof(ifr));
1499 ifr.ifr_data = (caddr_t)&nwid;
1500 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1501 if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == -1)
1502 return;
1503 if (nwid.i_len > IEEE80211_NWID_LEN) {
1504 warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
1505 return;
1506 }
1507 printf("\tnwid ");
1508 print_string(nwid.i_nwid, nwid.i_len);
1509 memset(&nwkey, 0, sizeof(nwkey));
1510 (void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
1511 /* show nwkey only when WEP is enabled */
1512 if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) == -1 ||
1513 nwkey.i_wepon == 0) {
1514 printf("\n");
1515 goto skip_wep;
1516 }
1517
1518 printf(" nwkey ");
1519 /* try to retrieve WEP keys */
1520 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1521 nwkey.i_key[i].i_keydat = keybuf[i];
1522 nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
1523 }
1524 if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) == -1) {
1525 printf("*****");
1526 } else {
1527 nwkey_verbose = 0;
1528 /* check to see non default key or multiple keys defined */
1529 if (nwkey.i_defkid != 1) {
1530 nwkey_verbose = 1;
1531 } else {
1532 for (i = 1; i < IEEE80211_WEP_NKID; i++) {
1533 if (nwkey.i_key[i].i_keylen != 0) {
1534 nwkey_verbose = 1;
1535 break;
1536 }
1537 }
1538 }
1539 /* check extra ambiguity with keywords */
1540 if (!nwkey_verbose) {
1541 if (nwkey.i_key[0].i_keylen >= 2 &&
1542 isdigit(nwkey.i_key[0].i_keydat[0]) &&
1543 nwkey.i_key[0].i_keydat[1] == ':')
1544 nwkey_verbose = 1;
1545 else if (nwkey.i_key[0].i_keylen >= 7 &&
1546 strncasecmp("persist", nwkey.i_key[0].i_keydat, 7)
1547 == 0)
1548 nwkey_verbose = 1;
1549 }
1550 if (nwkey_verbose)
1551 printf("%d:", nwkey.i_defkid);
1552 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1553 if (i > 0)
1554 printf(",");
1555 if (nwkey.i_key[i].i_keylen < 0)
1556 printf("persist");
1557 else
1558 print_string(nwkey.i_key[i].i_keydat,
1559 nwkey.i_key[i].i_keylen);
1560 if (!nwkey_verbose)
1561 break;
1562 }
1563 }
1564 printf("\n");
1565
1566 skip_wep:
1567 (void)strncpy(power.i_name, name, sizeof(power.i_name));
1568 if (ioctl(s, SIOCG80211POWER, &power) == -1)
1569 return;
1570 printf("\tpowersave ");
1571 if (power.i_enabled)
1572 printf("on (%dms sleep)", power.i_maxsleep);
1573 else
1574 printf("off");
1575 printf("\n");
1576 }
1577
1578 void
1579 init_current_media()
1580 {
1581 struct ifmediareq ifmr;
1582
1583 /*
1584 * If we have not yet done so, grab the currently-selected
1585 * media.
1586 */
1587 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
1588 (void) memset(&ifmr, 0, sizeof(ifmr));
1589 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1590
1591 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1592 /*
1593 * If we get E2BIG, the kernel is telling us
1594 * that there are more, so we can ignore it.
1595 */
1596 if (errno != E2BIG)
1597 err(EXIT_FAILURE, "SGIOCGIFMEDIA");
1598 }
1599
1600 media_current = ifmr.ifm_current;
1601 }
1602
1603 /* Sanity. */
1604 if (IFM_TYPE(media_current) == 0)
1605 errx(EXIT_FAILURE, "%s: no link type?", name);
1606 }
1607
1608 void
1609 process_media_commands()
1610 {
1611
1612 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
1613 /* Nothing to do. */
1614 return;
1615 }
1616
1617 /*
1618 * Media already set up, and commands sanity-checked. Set/clear
1619 * any options, and we're ready to go.
1620 */
1621 media_current |= mediaopt_set;
1622 media_current &= ~mediaopt_clear;
1623
1624 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1625 ifr.ifr_media = media_current;
1626
1627 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) == -1)
1628 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1629 }
1630
1631 void
1632 setmedia(val, d)
1633 const char *val;
1634 int d;
1635 {
1636 int type, subtype, inst;
1637
1638 init_current_media();
1639
1640 /* Only one media command may be given. */
1641 if (actions & A_MEDIA)
1642 errx(EXIT_FAILURE, "only one `media' command may be issued");
1643
1644 /* Must not come after mediaopt commands */
1645 if (actions & A_MEDIAOPT)
1646 errx(EXIT_FAILURE,
1647 "may not issue `media' after `mediaopt' commands");
1648
1649 /*
1650 * No need to check if `instance' has been issued; setmediainst()
1651 * craps out if `media' has not been specified.
1652 */
1653
1654 type = IFM_TYPE(media_current);
1655 inst = IFM_INST(media_current);
1656
1657 /* Look up the subtype. */
1658 subtype = get_media_subtype(type, val);
1659
1660 /* Build the new current media word. */
1661 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1662
1663 /* Media will be set after other processing is complete. */
1664 }
1665
1666 void
1667 setmediaopt(val, d)
1668 const char *val;
1669 int d;
1670 {
1671
1672 init_current_media();
1673
1674 /* Can only issue `mediaopt' once. */
1675 if (actions & A_MEDIAOPTSET)
1676 errx(EXIT_FAILURE, "only one `mediaopt' command may be issued");
1677
1678 /* Can't issue `mediaopt' if `instance' has already been issued. */
1679 if (actions & A_MEDIAINST)
1680 errx(EXIT_FAILURE, "may not issue `mediaopt' after `instance'");
1681
1682 mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
1683
1684 /* Media will be set after other processing is complete. */
1685 }
1686
1687 void
1688 unsetmediaopt(val, d)
1689 const char *val;
1690 int d;
1691 {
1692
1693 init_current_media();
1694
1695 /* Can only issue `-mediaopt' once. */
1696 if (actions & A_MEDIAOPTCLR)
1697 errx(EXIT_FAILURE,
1698 "only one `-mediaopt' command may be issued");
1699
1700 /* May not issue `media' and `-mediaopt'. */
1701 if (actions & A_MEDIA)
1702 errx(EXIT_FAILURE,
1703 "may not issue both `media' and `-mediaopt'");
1704
1705 /*
1706 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1707 * implicitly checks for A_MEDIAINST.
1708 */
1709
1710 mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
1711
1712 /* Media will be set after other processing is complete. */
1713 }
1714
1715 void
1716 setmediainst(val, d)
1717 const char *val;
1718 int d;
1719 {
1720 int type, subtype, options, inst;
1721
1722 init_current_media();
1723
1724 /* Can only issue `instance' once. */
1725 if (actions & A_MEDIAINST)
1726 errx(EXIT_FAILURE, "only one `instance' command may be issued");
1727
1728 /* Must have already specified `media' */
1729 if ((actions & A_MEDIA) == 0)
1730 errx(EXIT_FAILURE, "must specify `media' before `instance'");
1731
1732 type = IFM_TYPE(media_current);
1733 subtype = IFM_SUBTYPE(media_current);
1734 options = IFM_OPTIONS(media_current);
1735
1736 inst = atoi(val);
1737 if (inst < 0 || inst > IFM_INST_MAX)
1738 errx(EXIT_FAILURE, "invalid media instance: %s", val);
1739
1740 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1741
1742 /* Media will be set after other processing is complete. */
1743 }
1744
1745 struct ifmedia_description ifm_type_descriptions[] =
1746 IFM_TYPE_DESCRIPTIONS;
1747
1748 struct ifmedia_description ifm_subtype_descriptions[] =
1749 IFM_SUBTYPE_DESCRIPTIONS;
1750
1751 struct ifmedia_description ifm_option_descriptions[] =
1752 IFM_OPTION_DESCRIPTIONS;
1753
1754 const char *
1755 get_media_type_string(mword)
1756 int mword;
1757 {
1758 struct ifmedia_description *desc;
1759
1760 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
1761 desc++) {
1762 if (IFM_TYPE(mword) == desc->ifmt_word)
1763 return (desc->ifmt_string);
1764 }
1765 return ("<unknown type>");
1766 }
1767
1768 const char *
1769 get_media_subtype_string(mword)
1770 int mword;
1771 {
1772 struct ifmedia_description *desc;
1773
1774 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
1775 desc++) {
1776 if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
1777 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
1778 return (desc->ifmt_string);
1779 }
1780 return ("<unknown subtype>");
1781 }
1782
1783 int
1784 get_media_subtype(type, val)
1785 int type;
1786 const char *val;
1787 {
1788 int rval;
1789
1790 rval = lookup_media_word(ifm_subtype_descriptions, type, val);
1791 if (rval == -1)
1792 errx(EXIT_FAILURE, "unknown %s media subtype: %s",
1793 get_media_type_string(type), val);
1794
1795 return (rval);
1796 }
1797
1798 int
1799 get_media_options(type, val)
1800 int type;
1801 const char *val;
1802 {
1803 char *optlist, *str;
1804 int option, rval = 0;
1805
1806 /* We muck with the string, so copy it. */
1807 optlist = strdup(val);
1808 if (optlist == NULL)
1809 err(EXIT_FAILURE, "strdup");
1810 str = optlist;
1811
1812 /*
1813 * Look up the options in the user-provided comma-separated list.
1814 */
1815 for (; (str = strtok(str, ",")) != NULL; str = NULL) {
1816 option = lookup_media_word(ifm_option_descriptions, type, str);
1817 if (option == -1)
1818 errx(EXIT_FAILURE, "unknown %s media option: %s",
1819 get_media_type_string(type), str);
1820 rval |= IFM_OPTIONS(option);
1821 }
1822
1823 free(optlist);
1824 return (rval);
1825 }
1826
1827 int
1828 lookup_media_word(desc, type, val)
1829 struct ifmedia_description *desc;
1830 int type;
1831 const char *val;
1832 {
1833
1834 for (; desc->ifmt_string != NULL; desc++) {
1835 if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
1836 strcasecmp(desc->ifmt_string, val) == 0)
1837 return (desc->ifmt_word);
1838 }
1839 return (-1);
1840 }
1841
1842 void
1843 print_media_word(ifmw, print_type, as_syntax)
1844 int ifmw, print_type, as_syntax;
1845 {
1846 struct ifmedia_description *desc;
1847 int seen_option = 0;
1848
1849 if (print_type)
1850 printf("%s ", get_media_type_string(ifmw));
1851 printf("%s%s", as_syntax ? "media " : "",
1852 get_media_subtype_string(ifmw));
1853
1854 /* Find options. */
1855 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
1856 desc++) {
1857 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
1858 (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 &&
1859 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
1860 if (seen_option == 0)
1861 printf(" %s", as_syntax ? "mediaopt " : "");
1862 printf("%s%s", seen_option ? "," : "",
1863 desc->ifmt_string);
1864 seen_option |= IFM_OPTIONS(desc->ifmt_word);
1865 }
1866 }
1867 if (IFM_INST(ifmw) != 0)
1868 printf(" instance %d", IFM_INST(ifmw));
1869 }
1870
1871 int carrier()
1872 {
1873 struct ifmediareq ifmr;
1874
1875 (void) memset(&ifmr, 0, sizeof(ifmr));
1876 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1877
1878 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1879 /*
1880 * Interface doesn't support SIOC{G,S}IFMEDIA;
1881 * assume ok.
1882 */
1883 return 0;
1884 }
1885 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1886 /*
1887 * Interface doesn't report media-valid status.
1888 * assume ok.
1889 */
1890 return 0;
1891 }
1892 /* otherwise, return ok for active, not-ok if not active. */
1893 return !(ifmr.ifm_status & IFM_ACTIVE);
1894 }
1895
1896
1897 #define IFFBITS \
1898 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
1899 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
1900
1901 #define IFCAPBITS \
1902 "\020\1IP4CSUM\2TCP4CSUM\3UDP4CSUM\4TCP6CSUM\5UDP6CSUM\6TCP4CSUM_Rx\7UDP4CSUM_Rx"
1903
1904 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1905
1906 const struct ifmedia_status_description ifm_status_descriptions[] =
1907 IFM_STATUS_DESCRIPTIONS;
1908
1909 /*
1910 * Print the status of the interface. If an address family was
1911 * specified, show it and it only; otherwise, show them all.
1912 */
1913 void
1914 status(sdl)
1915 const struct sockaddr_dl *sdl;
1916 {
1917 struct afswtch *p = afp;
1918 struct ifmediareq ifmr;
1919 struct ifdatareq ifdr;
1920 int *media_list, i;
1921 char hbuf[NI_MAXHOST];
1922
1923 printf("%s: ", name);
1924 printb("flags", flags, IFFBITS);
1925 if (metric)
1926 printf(" metric %lu", metric);
1927 if (mtu)
1928 printf(" mtu %lu", mtu);
1929 putchar('\n');
1930
1931 if (g_ifcr.ifcr_capabilities) {
1932 putchar('\t');
1933 printb("capabilities", g_ifcr.ifcr_capabilities, IFCAPBITS);
1934 putchar('\n');
1935
1936 putchar('\t');
1937 printb("enabled", g_ifcr.ifcr_capenable, IFCAPBITS);
1938 putchar('\n');
1939 }
1940
1941 ieee80211_status();
1942 vlan_status();
1943 tunnel_status();
1944
1945 if (sdl != NULL &&
1946 getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
1947 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1948 hbuf[0] != '\0')
1949 printf("\taddress: %s\n", hbuf);
1950
1951 (void) memset(&ifmr, 0, sizeof(ifmr));
1952 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1953
1954 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
1955 /*
1956 * Interface doesn't support SIOC{G,S}IFMEDIA.
1957 */
1958 goto iface_stats;
1959 }
1960
1961 if (ifmr.ifm_count == 0) {
1962 warnx("%s: no media types?", name);
1963 goto iface_stats;
1964 }
1965
1966 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1967 if (media_list == NULL)
1968 err(EXIT_FAILURE, "malloc");
1969 ifmr.ifm_ulist = media_list;
1970
1971 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1)
1972 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1973
1974 printf("\tmedia: ");
1975 print_media_word(ifmr.ifm_current, 1, 0);
1976 if (ifmr.ifm_active != ifmr.ifm_current) {
1977 putchar(' ');
1978 putchar('(');
1979 print_media_word(ifmr.ifm_active, 0, 0);
1980 putchar(')');
1981 }
1982 putchar('\n');
1983
1984 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1985 const struct ifmedia_status_description *ifms;
1986 int bitno, found = 0;
1987
1988 printf("\tstatus: ");
1989 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1990 for (ifms = ifm_status_descriptions;
1991 ifms->ifms_valid != 0; ifms++) {
1992 if (ifms->ifms_type !=
1993 IFM_TYPE(ifmr.ifm_current) ||
1994 ifms->ifms_valid !=
1995 ifm_status_valid_list[bitno])
1996 continue;
1997 printf("%s%s", found ? ", " : "",
1998 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1999 found = 1;
2000
2001 /*
2002 * For each valid indicator bit, there's
2003 * only one entry for each media type, so
2004 * terminate the inner loop now.
2005 */
2006 break;
2007 }
2008 }
2009
2010 if (found == 0)
2011 printf("unknown");
2012 putchar('\n');
2013 }
2014
2015 if (mflag) {
2016 int type, printed_type;
2017
2018 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
2019 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
2020 if (IFM_TYPE(media_list[i]) == type) {
2021 if (printed_type == 0) {
2022 printf("\tsupported %s media:\n",
2023 get_media_type_string(type));
2024 printed_type = 1;
2025 }
2026 printf("\t\t");
2027 print_media_word(media_list[i], 0, 1);
2028 printf("\n");
2029 }
2030 }
2031 }
2032 }
2033
2034 free(media_list);
2035
2036 iface_stats:
2037 if (!vflag)
2038 goto proto_status;
2039
2040 (void) strncpy(ifdr.ifdr_name, name, sizeof(ifdr.ifdr_name));
2041
2042 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifdr) != -1) {
2043 struct if_data * const ifi = &ifdr.ifdr_data;
2044 #define PLURAL(n) ((n) == 1 ? "" : "s")
2045 printf("\tinput: %llu packet%s, %llu byte%s",
2046 (unsigned long long) ifi->ifi_ipackets,
2047 PLURAL(ifi->ifi_ipackets),
2048 (unsigned long long) ifi->ifi_ibytes,
2049 PLURAL(ifi->ifi_ibytes));
2050 if (ifi->ifi_imcasts)
2051 printf(", %llu multicast%s",
2052 (unsigned long long) ifi->ifi_imcasts,
2053 PLURAL(ifi->ifi_imcasts));
2054 if (ifi->ifi_ierrors)
2055 printf(", %llu error%s",
2056 (unsigned long long) ifi->ifi_ierrors,
2057 PLURAL(ifi->ifi_ierrors));
2058 if (ifi->ifi_iqdrops)
2059 printf(", %llu queue drop%s",
2060 (unsigned long long) ifi->ifi_iqdrops,
2061 PLURAL(ifi->ifi_iqdrops));
2062 if (ifi->ifi_noproto)
2063 printf(", %llu unknown protocol",
2064 (unsigned long long) ifi->ifi_noproto);
2065 printf("\n\toutput: %llu packet%s, %llu byte%s",
2066 (unsigned long long) ifi->ifi_opackets,
2067 PLURAL(ifi->ifi_opackets),
2068 (unsigned long long) ifi->ifi_obytes,
2069 PLURAL(ifi->ifi_obytes));
2070 if (ifi->ifi_omcasts)
2071 printf(", %llu multicast%s",
2072 (unsigned long long) ifi->ifi_omcasts,
2073 PLURAL(ifi->ifi_omcasts));
2074 if (ifi->ifi_oerrors)
2075 printf(", %llu error%s",
2076 (unsigned long long) ifi->ifi_oerrors,
2077 PLURAL(ifi->ifi_oerrors));
2078 if (ifi->ifi_collisions)
2079 printf(", %llu collision%s",
2080 (unsigned long long) ifi->ifi_collisions,
2081 PLURAL(ifi->ifi_collisions));
2082 printf("\n");
2083 #undef PLURAL
2084 }
2085
2086 proto_status:
2087 if ((p = afp) != NULL) {
2088 (*p->af_status)(1);
2089 } else for (p = afs; p->af_name; p++) {
2090 ifr.ifr_addr.sa_family = p->af_af;
2091 (*p->af_status)(0);
2092 }
2093 }
2094
2095 void
2096 tunnel_status()
2097 {
2098 char psrcaddr[NI_MAXHOST];
2099 char pdstaddr[NI_MAXHOST];
2100 const char *ver = "";
2101 #ifdef NI_WITHSCOPEID
2102 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
2103 #else
2104 const int niflag = NI_NUMERICHOST;
2105 #endif
2106 struct if_laddrreq req;
2107
2108 psrcaddr[0] = pdstaddr[0] = '\0';
2109
2110 memset(&req, 0, sizeof(req));
2111 strncpy(req.iflr_name, name, IFNAMSIZ);
2112 if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&req) == -1)
2113 return;
2114 #ifdef INET6
2115 if (req.addr.ss_family == AF_INET6)
2116 in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
2117 #endif
2118 getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
2119 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
2120 #ifdef INET6
2121 if (req.addr.ss_family == AF_INET6)
2122 ver = "6";
2123 #endif
2124
2125 #ifdef INET6
2126 if (req.dstaddr.ss_family == AF_INET6)
2127 in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
2128 #endif
2129 getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
2130 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
2131
2132 printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
2133 }
2134
2135 void
2136 vlan_status()
2137 {
2138 struct vlanreq vlr;
2139
2140 if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
2141 !isdigit(ifr.ifr_name[4]))
2142 return;
2143
2144 memset(&vlr, 0, sizeof(vlr));
2145 ifr.ifr_data = (caddr_t)&vlr;
2146
2147 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
2148 return;
2149
2150 if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
2151 printf("\tvlan: %d parent: %s\n",
2152 vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
2153 "<none>" : vlr.vlr_parent);
2154 }
2155
2156 void
2157 in_alias(creq)
2158 struct ifreq *creq;
2159 {
2160 struct sockaddr_in *iasin;
2161 int alias;
2162
2163 if (lflag)
2164 return;
2165
2166 alias = 1;
2167
2168 /* Get the non-alias address for this interface. */
2169 getsock(AF_INET);
2170 if (s < 0) {
2171 if (errno == EPROTONOSUPPORT)
2172 return;
2173 err(EXIT_FAILURE, "socket");
2174 }
2175 (void) memset(&ifr, 0, sizeof(ifr));
2176 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2177 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2178 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2179 return;
2180 } else
2181 warn("SIOCGIFADDR");
2182 }
2183 /* If creq and ifr are the same address, this is not an alias. */
2184 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
2185 sizeof(creq->ifr_addr)) == 0)
2186 alias = 0;
2187 (void) memset(&in_addreq, 0, sizeof(in_addreq));
2188 (void) strncpy(in_addreq.ifra_name, name, sizeof(in_addreq.ifra_name));
2189 memcpy(&in_addreq.ifra_addr, &creq->ifr_addr,
2190 sizeof(in_addreq.ifra_addr));
2191 if (ioctl(s, SIOCGIFALIAS, (caddr_t)&in_addreq) == -1) {
2192 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2193 return;
2194 } else
2195 warn("SIOCGIFALIAS");
2196 }
2197
2198 iasin = &in_addreq.ifra_addr;
2199 printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(iasin->sin_addr));
2200
2201 if (flags & IFF_POINTOPOINT) {
2202 iasin = &in_addreq.ifra_dstaddr;
2203 printf(" -> %s", inet_ntoa(iasin->sin_addr));
2204 }
2205
2206 iasin = &in_addreq.ifra_mask;
2207 printf(" netmask 0x%x", ntohl(iasin->sin_addr.s_addr));
2208
2209 if (flags & IFF_BROADCAST) {
2210 iasin = &in_addreq.ifra_broadaddr;
2211 printf(" broadcast %s", inet_ntoa(iasin->sin_addr));
2212 }
2213 printf("\n");
2214 }
2215
2216 void
2217 in_status(force)
2218 int force;
2219 {
2220 struct ifaddrs *ifap, *ifa;
2221 struct ifreq isifr;
2222
2223 if (getifaddrs(&ifap) != 0)
2224 err(EXIT_FAILURE, "getifaddrs");
2225 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2226 if (strcmp(name, ifa->ifa_name) != 0)
2227 continue;
2228 if (ifa->ifa_addr->sa_family != AF_INET)
2229 continue;
2230 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
2231 continue;
2232
2233 memset(&isifr, 0, sizeof(isifr));
2234 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
2235 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
2236 in_alias(&isifr);
2237 }
2238 freeifaddrs(ifap);
2239 }
2240
2241 void
2242 setifprefixlen(addr, d)
2243 const char *addr;
2244 int d;
2245 {
2246 if (*afp->af_getprefix)
2247 (*afp->af_getprefix)(addr, MASK);
2248 explicit_prefix = 1;
2249 }
2250
2251 #ifdef INET6
2252 void
2253 in6_fillscopeid(sin6)
2254 struct sockaddr_in6 *sin6;
2255 {
2256 #if defined(__KAME__) && defined(KAME_SCOPEID)
2257 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2258 sin6->sin6_scope_id =
2259 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
2260 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
2261 }
2262 #endif
2263 }
2264
2265 /* XXX not really an alias */
2266 void
2267 in6_alias(creq)
2268 struct in6_ifreq *creq;
2269 {
2270 struct sockaddr_in6 *sin6;
2271 char hbuf[NI_MAXHOST];
2272 u_int32_t scopeid;
2273 #ifdef NI_WITHSCOPEID
2274 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
2275 #else
2276 const int niflag = NI_NUMERICHOST;
2277 #endif
2278
2279 /* Get the non-alias address for this interface. */
2280 getsock(AF_INET6);
2281 if (s < 0) {
2282 if (errno == EPROTONOSUPPORT)
2283 return;
2284 err(EXIT_FAILURE, "socket");
2285 }
2286
2287 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
2288
2289 in6_fillscopeid(sin6);
2290 scopeid = sin6->sin6_scope_id;
2291 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
2292 hbuf, sizeof(hbuf), NULL, 0, niflag))
2293 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
2294 printf("\tinet6 %s", hbuf);
2295
2296 if (flags & IFF_POINTOPOINT) {
2297 (void) memset(&ifr6, 0, sizeof(ifr6));
2298 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2299 ifr6.ifr_addr = creq->ifr_addr;
2300 if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) == -1) {
2301 if (errno != EADDRNOTAVAIL)
2302 warn("SIOCGIFDSTADDR_IN6");
2303 (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
2304 ifr6.ifr_addr.sin6_family = AF_INET6;
2305 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
2306 }
2307 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
2308 in6_fillscopeid(sin6);
2309 hbuf[0] = '\0';
2310 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
2311 hbuf, sizeof(hbuf), NULL, 0, niflag))
2312 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
2313 printf(" -> %s", hbuf);
2314 }
2315
2316 (void) memset(&ifr6, 0, sizeof(ifr6));
2317 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2318 ifr6.ifr_addr = creq->ifr_addr;
2319 if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) == -1) {
2320 if (errno != EADDRNOTAVAIL)
2321 warn("SIOCGIFNETMASK_IN6");
2322 } else {
2323 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
2324 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
2325 sizeof(struct in6_addr)));
2326 }
2327
2328 (void) memset(&ifr6, 0, sizeof(ifr6));
2329 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2330 ifr6.ifr_addr = creq->ifr_addr;
2331 if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) == -1) {
2332 if (errno != EADDRNOTAVAIL)
2333 warn("SIOCGIFAFLAG_IN6");
2334 } else {
2335 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
2336 printf(" anycast");
2337 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
2338 printf(" tentative");
2339 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
2340 printf(" duplicated");
2341 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
2342 printf(" detached");
2343 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
2344 printf(" deprecated");
2345 }
2346
2347 if (scopeid)
2348 printf(" scopeid 0x%x", scopeid);
2349
2350 if (Lflag) {
2351 struct in6_addrlifetime *lifetime;
2352 (void) memset(&ifr6, 0, sizeof(ifr6));
2353 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
2354 ifr6.ifr_addr = creq->ifr_addr;
2355 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
2356 if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) == -1) {
2357 if (errno != EADDRNOTAVAIL)
2358 warn("SIOCGIFALIFETIME_IN6");
2359 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
2360 time_t t = time(NULL);
2361 printf(" pltime ");
2362 if (lifetime->ia6t_preferred) {
2363 printf("%s", lifetime->ia6t_preferred < t
2364 ? "0"
2365 : sec2str(lifetime->ia6t_preferred - t));
2366 } else
2367 printf("infty");
2368
2369 printf(" vltime ");
2370 if (lifetime->ia6t_expire) {
2371 printf("%s", lifetime->ia6t_expire < t
2372 ? "0"
2373 : sec2str(lifetime->ia6t_expire - t));
2374 } else
2375 printf("infty");
2376 }
2377 }
2378
2379 printf("\n");
2380 }
2381
2382 void
2383 in6_status(force)
2384 int force;
2385 {
2386 struct ifaddrs *ifap, *ifa;
2387 struct in6_ifreq isifr;
2388
2389 if (getifaddrs(&ifap) != 0)
2390 err(EXIT_FAILURE, "getifaddrs");
2391 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
2392 if (strcmp(name, ifa->ifa_name) != 0)
2393 continue;
2394 if (ifa->ifa_addr->sa_family != AF_INET6)
2395 continue;
2396 if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
2397 continue;
2398
2399 memset(&isifr, 0, sizeof(isifr));
2400 strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
2401 memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
2402 in6_alias(&isifr);
2403 }
2404 freeifaddrs(ifap);
2405 }
2406 #endif /*INET6*/
2407
2408 #ifndef INET_ONLY
2409
2410 void
2411 at_status(force)
2412 int force;
2413 {
2414 struct sockaddr_at *sat, null_sat;
2415 struct netrange *nr;
2416
2417 getsock(AF_APPLETALK);
2418 if (s < 0) {
2419 if (errno == EPROTONOSUPPORT)
2420 return;
2421 err(EXIT_FAILURE, "socket");
2422 }
2423 (void) memset(&ifr, 0, sizeof(ifr));
2424 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2425 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2426 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2427 if (!force)
2428 return;
2429 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2430 } else
2431 warn("SIOCGIFADDR");
2432 }
2433 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2434 sat = (struct sockaddr_at *)&ifr.ifr_addr;
2435
2436 (void) memset(&null_sat, 0, sizeof(null_sat));
2437
2438 nr = (struct netrange *) &sat->sat_zero;
2439 printf("\tatalk %d.%d range %d-%d phase %d",
2440 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
2441 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
2442 if (flags & IFF_POINTOPOINT) {
2443 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) == -1) {
2444 if (errno == EADDRNOTAVAIL)
2445 (void) memset(&ifr.ifr_addr, 0,
2446 sizeof(ifr.ifr_addr));
2447 else
2448 warn("SIOCGIFDSTADDR");
2449 }
2450 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
2451 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
2452 if (!sat)
2453 sat = &null_sat;
2454 printf("--> %d.%d",
2455 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
2456 }
2457 if (flags & IFF_BROADCAST) {
2458 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
2459 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
2460 if (sat)
2461 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
2462 sat->sat_addr.s_node);
2463 }
2464 putchar('\n');
2465 }
2466
2467 void
2468 xns_status(force)
2469 int force;
2470 {
2471 struct sockaddr_ns *sns;
2472
2473 getsock(AF_NS);
2474 if (s < 0) {
2475 if (errno == EPROTONOSUPPORT)
2476 return;
2477 err(EXIT_FAILURE, "socket");
2478 }
2479 (void) memset(&ifr, 0, sizeof(ifr));
2480 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2481 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) == -1) {
2482 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2483 if (!force)
2484 return;
2485 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2486 } else
2487 warn("SIOCGIFADDR");
2488 }
2489 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
2490 sns = (struct sockaddr_ns *)&ifr.ifr_addr;
2491 printf("\tns %s ", ns_ntoa(sns->sns_addr));
2492 if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
2493 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) == -1) {
2494 if (errno == EADDRNOTAVAIL)
2495 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2496 else
2497 warn("SIOCGIFDSTADDR");
2498 }
2499 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
2500 sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
2501 printf("--> %s ", ns_ntoa(sns->sns_addr));
2502 }
2503 putchar('\n');
2504 }
2505
2506 void
2507 iso_status(force)
2508 int force;
2509 {
2510 struct sockaddr_iso *siso;
2511 struct iso_ifreq isoifr;
2512
2513 getsock(AF_ISO);
2514 if (s < 0) {
2515 if (errno == EPROTONOSUPPORT)
2516 return;
2517 err(EXIT_FAILURE, "socket");
2518 }
2519 (void) memset(&isoifr, 0, sizeof(isoifr));
2520 (void) strncpy(isoifr.ifr_name, name, sizeof(isoifr.ifr_name));
2521 if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&isoifr) == -1) {
2522 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
2523 if (!force)
2524 return;
2525 (void) memset(&isoifr.ifr_Addr, 0,
2526 sizeof(isoifr.ifr_Addr));
2527 } else
2528 warn("SIOCGIFADDR_ISO");
2529 }
2530 (void) strncpy(isoifr.ifr_name, name, sizeof isoifr.ifr_name);
2531 siso = &isoifr.ifr_Addr;
2532 printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
2533 if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&isoifr) == -1) {
2534 if (errno == EADDRNOTAVAIL)
2535 memset(&isoifr.ifr_Addr, 0, sizeof(isoifr.ifr_Addr));
2536 else
2537 warn("SIOCGIFNETMASK_ISO");
2538 } else {
2539 if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
2540 siso->siso_addr.isoa_len = siso->siso_len
2541 - offsetof(struct sockaddr_iso, siso_addr);
2542 printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
2543 }
2544 if (flags & IFF_POINTOPOINT) {
2545 if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&isoifr) == -1) {
2546 if (errno == EADDRNOTAVAIL)
2547 memset(&isoifr.ifr_Addr, 0,
2548 sizeof(isoifr.ifr_Addr));
2549 else
2550 warn("SIOCGIFDSTADDR_ISO");
2551 }
2552 (void) strncpy(isoifr.ifr_name, name, sizeof (isoifr.ifr_name));
2553 siso = &isoifr.ifr_Addr;
2554 printf("--> %s ", iso_ntoa(&siso->siso_addr));
2555 }
2556 putchar('\n');
2557 }
2558
2559 #endif /* INET_ONLY */
2560
2561 #define SIN(x) ((struct sockaddr_in *) &(x))
2562 struct sockaddr_in *sintab[] = {
2563 SIN(ridreq.ifr_addr), SIN(in_addreq.ifra_addr),
2564 SIN(in_addreq.ifra_mask), SIN(in_addreq.ifra_broadaddr)};
2565
2566 void
2567 in_getaddr(str, which)
2568 const char *str;
2569 int which;
2570 {
2571 struct sockaddr_in *gasin = sintab[which];
2572 struct hostent *hp;
2573 struct netent *np;
2574
2575 gasin->sin_len = sizeof(*gasin);
2576 if (which != MASK)
2577 gasin->sin_family = AF_INET;
2578
2579 if (which == ADDR) {
2580 char *p = NULL;
2581 if ((p = strrchr(str, '/')) != NULL) {
2582 *p = '\0';
2583 in_getprefix(p + 1, MASK);
2584 }
2585 }
2586
2587 if (inet_aton(str, &gasin->sin_addr) == 0) {
2588 if ((hp = gethostbyname(str)) != NULL)
2589 (void) memcpy(&gasin->sin_addr, hp->h_addr, hp->h_length);
2590 else if ((np = getnetbyname(str)) != NULL)
2591 gasin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
2592 else
2593 errx(EXIT_FAILURE, "%s: bad value", str);
2594 }
2595 }
2596
2597 void
2598 in_getprefix(plen, which)
2599 const char *plen;
2600 int which;
2601 {
2602 register struct sockaddr_in *igsin = sintab[which];
2603 register u_char *cp;
2604 int len = strtol(plen, (char **)NULL, 10);
2605
2606 if ((len < 0) || (len > 32))
2607 errx(EXIT_FAILURE, "%s: bad value", plen);
2608 igsin->sin_len = sizeof(*igsin);
2609 if (which != MASK)
2610 igsin->sin_family = AF_INET;
2611 if ((len == 0) || (len == 32)) {
2612 memset(&igsin->sin_addr, 0xff, sizeof(struct in_addr));
2613 return;
2614 }
2615 memset((void *)&igsin->sin_addr, 0x00, sizeof(igsin->sin_addr));
2616 for (cp = (u_char *)&igsin->sin_addr; len > 7; len -= 8)
2617 *cp++ = 0xff;
2618 if (len)
2619 *cp = 0xff << (8 - len);
2620 }
2621
2622 /*
2623 * Print a value a la the %b format of the kernel's printf
2624 */
2625 void
2626 printb(str, v, bits)
2627 const char *str;
2628 unsigned short v;
2629 const char *bits;
2630 {
2631 int i, any = 0;
2632 char c;
2633
2634 if (bits && *bits == 8)
2635 printf("%s=%o", str, v);
2636 else
2637 printf("%s=%x", str, v);
2638 bits++;
2639 if (bits) {
2640 putchar('<');
2641 while ((i = *bits++) != 0) {
2642 if (v & (1 << (i-1))) {
2643 if (any)
2644 putchar(',');
2645 any = 1;
2646 for (; (c = *bits) > 32; bits++)
2647 putchar(c);
2648 } else
2649 for (; *bits > 32; bits++)
2650 ;
2651 }
2652 putchar('>');
2653 }
2654 }
2655
2656 #ifdef INET6
2657 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
2658 struct sockaddr_in6 *sin6tab[] = {
2659 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
2660 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
2661
2662 void
2663 in6_getaddr(str, which)
2664 const char *str;
2665 int which;
2666 {
2667 #if defined(__KAME__) && defined(KAME_SCOPEID)
2668 struct sockaddr_in6 *sin6 = sin6tab[which];
2669 struct addrinfo hints, *res;
2670 int error;
2671
2672 memset(&hints, 0, sizeof(hints));
2673 hints.ai_family = AF_INET6;
2674 hints.ai_socktype = SOCK_DGRAM;
2675 #if 0 /* in_getaddr() allows FQDN */
2676 hints.ai_flags = AI_NUMERICHOST;
2677 #endif
2678 error = getaddrinfo(str, "0", &hints, &res);
2679 if (error)
2680 errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
2681 if (res->ai_next)
2682 errx(EXIT_FAILURE, "%s: resolved to multiple hosts", str);
2683 if (res->ai_addrlen != sizeof(struct sockaddr_in6))
2684 errx(EXIT_FAILURE, "%s: bad value", str);
2685 memcpy(sin6, res->ai_addr, res->ai_addrlen);
2686 freeaddrinfo(res);
2687 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
2688 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
2689 htons(sin6->sin6_scope_id);
2690 sin6->sin6_scope_id = 0;
2691 }
2692 #else
2693 struct sockaddr_in6 *gasin = sin6tab[which];
2694
2695 gasin->sin6_len = sizeof(*gasin);
2696 if (which != MASK)
2697 gasin->sin6_family = AF_INET6;
2698
2699 if (which == ADDR) {
2700 char *p = NULL;
2701 if((p = strrchr(str, '/')) != NULL) {
2702 *p = '\0';
2703 in6_getprefix(p + 1, MASK);
2704 explicit_prefix = 1;
2705 }
2706 }
2707
2708 if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
2709 errx(EXIT_FAILURE, "%s: bad value", str);
2710 #endif
2711 }
2712
2713 void
2714 in6_getprefix(plen, which)
2715 const char *plen;
2716 int which;
2717 {
2718 register struct sockaddr_in6 *gpsin = sin6tab[which];
2719 register u_char *cp;
2720 int len = strtol(plen, (char **)NULL, 10);
2721
2722 if ((len < 0) || (len > 128))
2723 errx(EXIT_FAILURE, "%s: bad value", plen);
2724 gpsin->sin6_len = sizeof(*gpsin);
2725 if (which != MASK)
2726 gpsin->sin6_family = AF_INET6;
2727 if ((len == 0) || (len == 128)) {
2728 memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
2729 return;
2730 }
2731 memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
2732 for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
2733 *cp++ = 0xff;
2734 if (len)
2735 *cp = 0xff << (8 - len);
2736 }
2737
2738 int
2739 prefix(val, size)
2740 void *val;
2741 int size;
2742 {
2743 register u_char *pname = (u_char *)val;
2744 register int byte, bit, plen = 0;
2745
2746 for (byte = 0; byte < size; byte++, plen += 8)
2747 if (pname[byte] != 0xff)
2748 break;
2749 if (byte == size)
2750 return (plen);
2751 for (bit = 7; bit != 0; bit--, plen++)
2752 if (!(pname[byte] & (1 << bit)))
2753 break;
2754 for (; bit != 0; bit--)
2755 if (pname[byte] & (1 << bit))
2756 return(0);
2757 byte++;
2758 for (; byte < size; byte++)
2759 if (pname[byte])
2760 return(0);
2761 return (plen);
2762 }
2763 #endif /*INET6*/
2764
2765 #ifndef INET_ONLY
2766 void
2767 at_getaddr(addr, which)
2768 const char *addr;
2769 int which;
2770 {
2771 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
2772 u_int net, node;
2773
2774 sat->sat_family = AF_APPLETALK;
2775 sat->sat_len = sizeof(*sat);
2776 if (which == MASK)
2777 errx(EXIT_FAILURE, "AppleTalk does not use netmasks\n");
2778 if (sscanf(addr, "%u.%u", &net, &node) != 2
2779 || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
2780 errx(EXIT_FAILURE, "%s: illegal address", addr);
2781 sat->sat_addr.s_net = htons(net);
2782 sat->sat_addr.s_node = node;
2783 }
2784
2785 void
2786 setatrange(range, d)
2787 const char *range;
2788 int d;
2789 {
2790 u_short first = 123, last = 123;
2791
2792 if (sscanf(range, "%hu-%hu", &first, &last) != 2
2793 || first == 0 /* || first > 0xffff */
2794 || last == 0 /* || last > 0xffff */ || first > last)
2795 errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
2796 first, last);
2797 at_nr.nr_firstnet = htons(first);
2798 at_nr.nr_lastnet = htons(last);
2799 }
2800
2801 void
2802 setatphase(phase, d)
2803 const char *phase;
2804 int d;
2805 {
2806 if (!strcmp(phase, "1"))
2807 at_nr.nr_phase = 1;
2808 else if (!strcmp(phase, "2"))
2809 at_nr.nr_phase = 2;
2810 else
2811 errx(EXIT_FAILURE, "%s: illegal phase", phase);
2812 }
2813
2814 void
2815 checkatrange(sat)
2816 struct sockaddr_at *sat;
2817 {
2818 if (at_nr.nr_phase == 0)
2819 at_nr.nr_phase = 2; /* Default phase 2 */
2820 if (at_nr.nr_firstnet == 0)
2821 at_nr.nr_firstnet = /* Default range of one */
2822 at_nr.nr_lastnet = sat->sat_addr.s_net;
2823 printf("\tatalk %d.%d range %d-%d phase %d\n",
2824 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
2825 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
2826 if ((u_short) ntohs(at_nr.nr_firstnet) >
2827 (u_short) ntohs(sat->sat_addr.s_net)
2828 || (u_short) ntohs(at_nr.nr_lastnet) <
2829 (u_short) ntohs(sat->sat_addr.s_net))
2830 errx(EXIT_FAILURE, "AppleTalk address is not in range");
2831 *((struct netrange *) &sat->sat_zero) = at_nr;
2832 }
2833
2834 #define SNS(x) ((struct sockaddr_ns *) &(x))
2835 struct sockaddr_ns *snstab[] = {
2836 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
2837 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
2838
2839 void
2840 xns_getaddr(addr, which)
2841 const char *addr;
2842 int which;
2843 {
2844 struct sockaddr_ns *sns = snstab[which];
2845
2846 sns->sns_family = AF_NS;
2847 sns->sns_len = sizeof(*sns);
2848 sns->sns_addr = ns_addr(addr);
2849 if (which == MASK)
2850 puts("Attempt to set XNS netmask will be ineffectual");
2851 }
2852
2853 #define SISO(x) ((struct sockaddr_iso *) &(x))
2854 struct sockaddr_iso *sisotab[] = {
2855 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
2856 SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
2857
2858 void
2859 iso_getaddr(addr, which)
2860 const char *addr;
2861 int which;
2862 {
2863 struct sockaddr_iso *siso = sisotab[which];
2864 siso->siso_addr = *iso_addr(addr);
2865
2866 if (which == MASK) {
2867 siso->siso_len = TSEL(siso) - (caddr_t)(siso);
2868 siso->siso_nlen = 0;
2869 } else {
2870 siso->siso_len = sizeof(*siso);
2871 siso->siso_family = AF_ISO;
2872 }
2873 }
2874
2875 void
2876 setsnpaoffset(val, d)
2877 const char *val;
2878 int d;
2879 {
2880 iso_addreq.ifra_snpaoffset = atoi(val);
2881 }
2882
2883 void
2884 setnsellength(val, d)
2885 const char *val;
2886 int d;
2887 {
2888 nsellength = atoi(val);
2889 if (nsellength < 0)
2890 errx(EXIT_FAILURE, "Negative NSEL length is absurd");
2891 if (afp == 0 || afp->af_af != AF_ISO)
2892 errx(EXIT_FAILURE, "Setting NSEL length valid only for iso");
2893 }
2894
2895 void
2896 fixnsel(siso)
2897 struct sockaddr_iso *siso;
2898 {
2899 if (siso->siso_family == 0)
2900 return;
2901 siso->siso_tlen = nsellength;
2902 }
2903
2904 void
2905 adjust_nsellength()
2906 {
2907 fixnsel(sisotab[RIDADDR]);
2908 fixnsel(sisotab[ADDR]);
2909 fixnsel(sisotab[DSTADDR]);
2910 }
2911
2912 #endif /* INET_ONLY */
2913
2914 void
2915 usage()
2916 {
2917 const char *progname = getprogname();
2918
2919 fprintf(stderr,
2920 "usage: %s [ -m ] [ -v ]"
2921 #ifdef INET6
2922 "[ -L ] "
2923 #endif
2924 "interface\n"
2925 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
2926 "\t\t[ alias | -alias ] ]\n"
2927 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
2928 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
2929 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
2930 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
2931 "\t[ arp | -arp ]\n"
2932 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
2933 "[ instance minst ]\n"
2934 "\t[ vlan n vlanif i ]\n"
2935 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
2936 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
2937 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
2938 " %s -a [ -m ] [ -d ] [ -u ] [ -v ] [ af ]\n"
2939 " %s -l [ -b ] [ -d ] [ -u ] [ -s ]\n"
2940 " %s -C\n"
2941 " %s interface create\n"
2942 " %s interface destroy\n",
2943 progname, progname, progname, progname, progname, progname);
2944 exit(1);
2945 }
2946
2947 #ifdef INET6
2948 char *
2949 sec2str(total)
2950 time_t total;
2951 {
2952 static char result[256];
2953 int days, hours, mins, secs;
2954 int first = 1;
2955 char *p = result;
2956 char *end = &result[sizeof(result)];
2957 int n;
2958
2959 if (0) { /*XXX*/
2960 days = total / 3600 / 24;
2961 hours = (total / 3600) % 24;
2962 mins = (total / 60) % 60;
2963 secs = total % 60;
2964
2965 if (days) {
2966 first = 0;
2967 n = snprintf(p, end - p, "%dd", days);
2968 if (n < 0 || n >= end - p)
2969 return(result);
2970 p += n;
2971 }
2972 if (!first || hours) {
2973 first = 0;
2974 n = snprintf(p, end - p, "%dh", hours);
2975 if (n < 0 || n >= end - p)
2976 return(result);
2977 p += n;
2978 }
2979 if (!first || mins) {
2980 first = 0;
2981 n = snprintf(p, end - p, "%dm", mins);
2982 if (n < 0 || n >= end - p)
2983 return(result);
2984 p += n;
2985 }
2986 snprintf(p, end - p, "%ds", secs);
2987 } else
2988 snprintf(p, end - p, "%lu", (u_long)total);
2989
2990 return(result);
2991 }
2992 #endif
2993