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