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