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