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