Bring in new ifconfig(8) from FreeBSD6. It is more modular and flexible with
[dragonfly.git] / sbin / ifconfig / ifconfig.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
984263bc
MD
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
1de703da 28 *
ca74a0a2
SZ
29 * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.113.2.4 2006/02/09 10:48:43 yar Exp $
30 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.26 2006/04/02 03:33:59 sephe Exp $
984263bc
MD
31 */
32
984263bc
MD
33#include <sys/param.h>
34#include <sys/ioctl.h>
35#include <sys/socket.h>
36#include <sys/sysctl.h>
37#include <sys/time.h>
38#include <sys/module.h>
39#include <sys/linker.h>
40
41#include <net/ethernet.h>
42#include <net/if.h>
43#include <net/if_var.h>
44#include <net/if_dl.h>
45#include <net/if_types.h>
46#include <net/route.h>
47
48/* IP */
49#include <netinet/in.h>
50#include <netinet/in_var.h>
51#include <arpa/inet.h>
52#include <netdb.h>
53
984263bc
MD
54#include <ctype.h>
55#include <err.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
984263bc
MD
62
63#include "ifconfig.h"
64
ca74a0a2
SZ
65/*
66 * This macro returns the size of a struct sockaddr when passed
67 * through a routing socket. Basically we round up sa_len to
68 * a multiple of sizeof(long), with a minimum of sizeof(long).
69 * The check for a NULL pointer is just a convenience, probably never used.
70 * The case sa_len == 0 should only apply to empty structures.
71 */
72#define SA_SIZE(sa) \
73 ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \
74 sizeof(long) : \
75 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
984263bc 76
b50e4759
MD
77/*
78 * Since "struct ifreq" is composed of various union members, callers
79 * should pay special attention to interprete the value.
80 * (.e.g. little/big endian difference in the structure.)
81 */
ca74a0a2 82struct ifreq ifr;
984263bc 83
c021b82e 84char name[IFNAMSIZ];
984263bc
MD
85int flags;
86int setaddr;
87int setipdst;
88int setmask;
89int doalias;
90int clearaddr;
91int newaddr = 1;
ca74a0a2 92int verbose;
984263bc 93
ca74a0a2
SZ
94int supmedia = 0;
95int printkeys = 0; /* Print keying material for interfaces. */
96int printname = 0; /* Print the name of the created interface. */
984263bc 97
ca74a0a2
SZ
98static int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
99static void status(const struct afswtch *afp, int addrcount,
984263bc 100 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
cd5332df 101 struct ifa_msghdr *ifam);
ca74a0a2
SZ
102static void tunnel_status(int s);
103static void usage(void);
984263bc 104
ca74a0a2
SZ
105static struct afswtch *af_getbyname(const char *name);
106static struct afswtch *af_getbyfamily(int af);
107static void af_other_status(int);
b50e4759 108
ca74a0a2 109static struct option *opts = NULL;
984263bc
MD
110
111void
ca74a0a2 112opt_register(struct option *p)
984263bc 113{
ca74a0a2
SZ
114 p->next = opts;
115 opts = p;
984263bc
MD
116}
117
ca74a0a2 118static void
b5744197 119usage(void)
984263bc 120{
ca74a0a2
SZ
121 char options[1024];
122 struct option *p;
123
124 /* XXX not right but close enough for now */
125 options[0] = '\0';
126 for (p = opts; p != NULL; p = p->next) {
127 strlcat(options, p->opt_usage, sizeof(options));
128 strlcat(options, " ", sizeof(options));
129 }
130
131 fprintf(stderr,
132 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
133 " [parameters]\n"
134 " ifconfig interface create\n"
135 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
136 " ifconfig -l [-d] [-u] [address_family]\n"
137 " ifconfig %s[-d] [-m] [-u] [-v]\n",
138 options, options, options);
984263bc
MD
139 exit(1);
140}
141
142int
ca74a0a2 143main(int argc, char *argv[])
984263bc 144{
ca74a0a2 145 int c, all, namesonly, downonly, uponly;
b50e4759 146 int need_nl = 0, count = 0;
ca74a0a2 147 const struct afswtch *afp = NULL;
e0417840 148 int addrcount, ifindex;
ca74a0a2
SZ
149 struct if_msghdr *ifm, *nextifm;
150 struct ifa_msghdr *ifam;
151 struct sockaddr_dl *sdl;
152 char *buf, *lim, *next;
984263bc
MD
153 size_t needed;
154 int mib[6];
ca74a0a2
SZ
155 char options[1024];
156 struct option *p;
157
158 all = downonly = uponly = namesonly = verbose = 0;
984263bc
MD
159
160 /* Parse leading line options */
ca74a0a2
SZ
161 strlcpy(options, "adklmuv", sizeof(options));
162 for (p = opts; p != NULL; p = p->next)
163 strlcat(options, p->opt, sizeof(options));
164 while ((c = getopt(argc, argv, options)) != -1) {
984263bc
MD
165 switch (c) {
166 case 'a': /* scan all interfaces */
167 all++;
168 break;
169 case 'd': /* restrict scan to "down" interfaces */
170 downonly++;
171 break;
ca74a0a2
SZ
172 case 'k':
173 printkeys++;
174 break;
984263bc
MD
175 case 'l': /* scan interface names only */
176 namesonly++;
177 break;
178 case 'm': /* show media choices in status */
179 supmedia = 1;
180 break;
181 case 'u': /* restrict scan to "up" interfaces */
182 uponly++;
183 break;
ca74a0a2
SZ
184 case 'v':
185 verbose++;
984263bc 186 break;
984263bc 187 default:
ca74a0a2
SZ
188 for (p = opts; p != NULL; p = p->next)
189 if (p->opt[0] == c) {
190 p->cb(optarg);
191 break;
192 }
193 if (p == NULL)
194 usage();
984263bc
MD
195 break;
196 }
197 }
198 argc -= optind;
199 argv += optind;
200
984263bc
MD
201 /* -l cannot be used with -a or -m */
202 if (namesonly && (all || supmedia))
203 usage();
204
205 /* nonsense.. */
206 if (uponly && downonly)
207 usage();
208
209 /* no arguments is equivalent to '-a' */
210 if (!namesonly && argc < 1)
211 all = 1;
212
213 /* -a and -l allow an address family arg to limit the output */
214 if (all || namesonly) {
215 if (argc > 1)
216 usage();
217
e0417840 218 ifindex = 0;
984263bc 219 if (argc == 1) {
ca74a0a2
SZ
220 afp = af_getbyname(*argv);
221 if (afp == NULL)
984263bc 222 usage();
ca74a0a2
SZ
223 if (afp->af_name != NULL)
224 argc--, argv++;
984263bc
MD
225 /* leave with afp non-zero */
226 }
227 } else {
228 /* not listing, need an argument */
229 if (argc < 1)
230 usage();
231
232 strncpy(name, *argv, sizeof(name));
233 argc--, argv++;
234
235 /* check and maybe load support for this interface */
236 ifmaybeload(name);
237
238 /*
239 * NOTE: We must special-case the `create' command right
240 * here as we would otherwise fail when trying to find
241 * the interface.
242 */
243 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
244 strcmp(argv[0], "plumb") == 0)) {
245 clone_create();
246 argc--, argv++;
247 if (argc == 0)
c021b82e 248 goto end;
984263bc 249 }
e0417840
JS
250 ifindex = if_nametoindex(name);
251 if (ifindex == 0)
252 errx(1, "interface %s does not exist", name);
984263bc
MD
253 }
254
255 /* Check for address family */
256 if (argc > 0) {
ca74a0a2
SZ
257 afp = af_getbyname(*argv);
258 if (afp != NULL)
259 argc--, argv++;
984263bc
MD
260 }
261
b50e4759 262retry:
984263bc
MD
263 mib[0] = CTL_NET;
264 mib[1] = PF_ROUTE;
265 mib[2] = 0;
b50e4759 266 mib[3] = 0; /* address family */
984263bc 267 mib[4] = NET_RT_IFLIST;
b50e4759 268 mib[5] = ifindex; /* interface index */
984263bc
MD
269
270 /* if particular family specified, only ask about it */
ca74a0a2 271 if (afp != NULL)
984263bc
MD
272 mib[3] = afp->af_af;
273
274 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
275 errx(1, "iflist-sysctl-estimate");
276 if ((buf = malloc(needed)) == NULL)
277 errx(1, "malloc");
b50e4759
MD
278 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
279 if (errno == ENOMEM && count++ < 10) {
280 warnx("Routing table grew, retrying");
281 free(buf);
282 sleep(1);
283 goto retry;
284 }
984263bc 285 errx(1, "actual retrieval of interface table");
b50e4759 286 }
984263bc
MD
287 lim = buf + needed;
288
289 next = buf;
290 while (next < lim) {
291
292 ifm = (struct if_msghdr *)next;
293
294 if (ifm->ifm_type == RTM_IFINFO) {
ca74a0a2 295#ifdef notyet
b50e4759
MD
296 if (ifm->ifm_data.ifi_datalen == 0)
297 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
298 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
ca74a0a2 299 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
b50e4759 300#else
ca74a0a2 301 sdl = (struct sockaddr_dl *)(ifm + 1);
b50e4759 302#endif
984263bc
MD
303 flags = ifm->ifm_flags;
304 } else {
305 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
306 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
307 ifm->ifm_type);
308 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
309 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
310 lim);
311 exit (1);
312 }
313
314 next += ifm->ifm_msglen;
315 ifam = NULL;
316 addrcount = 0;
317 while (next < lim) {
318
319 nextifm = (struct if_msghdr *)next;
320
321 if (nextifm->ifm_type != RTM_NEWADDR)
322 break;
323
324 if (ifam == NULL)
325 ifam = (struct ifa_msghdr *)nextifm;
326
327 addrcount++;
328 next += nextifm->ifm_msglen;
329 }
b50e4759
MD
330 memcpy(name, sdl->sdl_data,
331 sizeof(name) < sdl->sdl_nlen ?
332 sizeof(name)-1 : sdl->sdl_nlen);
333 name[sizeof(name) < sdl->sdl_nlen ?
334 sizeof(name)-1 : sdl->sdl_nlen] = '\0';
e0417840 335
984263bc
MD
336 if (all || namesonly) {
337 if (uponly)
338 if ((flags & IFF_UP) == 0)
339 continue; /* not up */
340 if (downonly)
341 if (flags & IFF_UP)
342 continue; /* not down */
984263bc 343 if (namesonly) {
ca74a0a2
SZ
344 if (afp == NULL || afp->af_af != AF_LINK ||
345 sdl->sdl_type == IFT_ETHER) {
984263bc
MD
346 if (need_nl)
347 putchar(' ');
348 fputs(name, stdout);
349 need_nl++;
350 }
351 continue;
352 }
984263bc
MD
353 }
354
355 if (argc > 0)
356 ifconfig(argc, argv, afp);
357 else
358 status(afp, addrcount, sdl, ifm, ifam);
984263bc
MD
359 }
360 free(buf);
361
362 if (namesonly && need_nl > 0)
363 putchar('\n');
c021b82e
HP
364end:
365 if (printname)
366 printf("%s\n", name);
984263bc 367
984263bc
MD
368 exit (0);
369}
370
ca74a0a2 371static struct afswtch *afs = NULL;
984263bc 372
ca74a0a2
SZ
373void
374af_register(struct afswtch *p)
375{
376 p->af_next = afs;
377 afs = p;
378}
379
380static struct afswtch *
381af_getbyname(const char *name)
382{
383 struct afswtch *afp;
384
385 for (afp = afs; afp != NULL; afp = afp->af_next)
386 if (strcmp(afp->af_name, name) == 0)
387 return afp;
388 return NULL;
389}
390
391static struct afswtch *
392af_getbyfamily(int af)
393{
394 struct afswtch *afp;
395
396 for (afp = afs; afp != NULL; afp = afp->af_next)
397 if (afp->af_af == af)
398 return afp;
399 return NULL;
400}
401
402static void
403af_other_status(int s)
404{
405 struct afswtch *afp;
406 uint8_t afmask[howmany(AF_MAX, NBBY)];
407
408 memset(afmask, 0, sizeof(afmask));
409 for (afp = afs; afp != NULL; afp = afp->af_next) {
410 if (afp->af_other_status == NULL)
411 continue;
412 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
413 continue;
414 afp->af_other_status(s);
415 setbit(afmask, afp->af_af);
416 }
417}
418
419static void
420af_all_tunnel_status(int s)
421{
422 struct afswtch *afp;
423 uint8_t afmask[howmany(AF_MAX, NBBY)];
424
425 memset(afmask, 0, sizeof(afmask));
426 for (afp = afs; afp != NULL; afp = afp->af_next) {
427 if (afp->af_status_tunnel == NULL)
428 continue;
429 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
430 continue;
431 afp->af_status_tunnel(s);
432 setbit(afmask, afp->af_af);
433 }
434}
435
436static struct cmd *cmds = NULL;
437
438void
439cmd_register(struct cmd *p)
440{
441 p->c_next = cmds;
442 cmds = p;
443}
444
445static const struct cmd *
446cmd_lookup(const char *name)
447{
448#define N(a) (sizeof(a)/sizeof(a[0]))
449 const struct cmd *p;
450
451 for (p = cmds; p != NULL; p = p->c_next)
452 if (strcmp(name, p->c_name) == 0)
453 return p;
454 return NULL;
455#undef N
456}
457
458struct callback {
459 callback_func *cb_func;
460 void *cb_arg;
461 struct callback *cb_next;
462};
463static struct callback *callbacks = NULL;
464
465void
466callback_register(callback_func *func, void *arg)
467{
468 struct callback *cb;
469
470 cb = malloc(sizeof(struct callback));
471 if (cb == NULL)
472 errx(1, "unable to allocate memory for callback");
473 cb->cb_func = func;
474 cb->cb_arg = arg;
475 cb->cb_next = callbacks;
476 callbacks = cb;
477}
478
479/* specially-handled commands */
480static void setifaddr(const char *, int, int, const struct afswtch *);
481static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
482
483static void setifdstaddr(const char *, int, int, const struct afswtch *);
484static const struct cmd setifdstaddr_cmd =
485 DEF_CMD("ifdstaddr", 0, setifdstaddr);
486
487static int
b5744197 488ifconfig(int argc, char *const *argv, const struct afswtch *afp)
984263bc 489{
ca74a0a2 490 struct callback *cb;
984263bc
MD
491 int s;
492
493 if (afp == NULL)
ca74a0a2
SZ
494 afp = af_getbyname("inet");
495 ifr.ifr_addr.sa_family =
496 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
497 AF_INET : afp->af_af;
984263bc
MD
498 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
499
500 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
ca74a0a2 501 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
984263bc
MD
502
503 while (argc > 0) {
8d5bb0c5 504 const struct cmd *p;
984263bc 505
ca74a0a2
SZ
506 p = cmd_lookup(*argv);
507 if (p == NULL) {
508 /*
509 * Not a recognized command, choose between setting
510 * the interface address and the dst address.
511 */
512 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
513 }
514 if (p->c_u.c_func || p->c_u.c_func2) {
984263bc
MD
515 if (p->c_parameter == NEXTARG) {
516 if (argv[1] == NULL)
517 errx(1, "'%s' requires argument",
518 p->c_name);
ca74a0a2 519 p->c_u.c_func(argv[1], 0, s, afp);
984263bc 520 argc--, argv++;
ca74a0a2
SZ
521 } else if (p->c_parameter == OPTARG) {
522 p->c_u.c_func(argv[1], 0, s, afp);
523 if (argv[1] != NULL)
524 argc--, argv++;
984263bc
MD
525 } else if (p->c_parameter == NEXTARG2) {
526 if (argc < 3)
527 errx(1, "'%s' requires 2 arguments",
528 p->c_name);
ca74a0a2 529 p->c_u.c_func2(argv[1], argv[2], s, afp);
984263bc
MD
530 argc -= 2, argv += 2;
531 } else
ca74a0a2 532 p->c_u.c_func(*argv, p->c_parameter, s, afp);
984263bc
MD
533 }
534 argc--, argv++;
535 }
984263bc 536
ca74a0a2
SZ
537 /*
538 * Do any post argument processing required by the address family.
539 */
540 if (afp->af_postproc != NULL)
541 afp->af_postproc(s, afp);
542 /*
543 * Do deferred callbacks registered while processing
544 * command-line arguments.
545 */
546 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
547 cb->cb_func(s, cb->cb_arg);
548 /*
549 * Do deferred operations.
550 */
984263bc
MD
551 if (clearaddr) {
552 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
553 warnx("interface %s cannot change %s addresses!",
554 name, afp->af_name);
ca74a0a2 555 clearaddr = 0;
984263bc
MD
556 }
557 }
558 if (clearaddr) {
559 int ret;
560 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
ca74a0a2
SZ
561 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
562 if (ret < 0) {
984263bc
MD
563 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
564 /* means no previous address for interface */
565 } else
566 Perror("ioctl (SIOCDIFADDR)");
567 }
568 }
569 if (newaddr) {
570 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
571 warnx("interface %s cannot change %s addresses!",
572 name, afp->af_name);
573 newaddr = 0;
574 }
575 }
576 if (newaddr && (setaddr || setmask)) {
577 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
578 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
579 Perror("ioctl (SIOCAIFADDR)");
580 }
ca74a0a2 581
984263bc
MD
582 close(s);
583 return(0);
584}
984263bc
MD
585
586/*ARGSUSED*/
ca74a0a2
SZ
587static void
588setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
984263bc 589{
ca74a0a2 590 if (afp->af_getaddr == NULL)
984263bc
MD
591 return;
592 /*
593 * Delay the ioctl to set the interface addr until flags are all set.
594 * The address interpretation may depend on the flags,
595 * and the flags may change when the address is set.
596 */
597 setaddr++;
598 if (doalias == 0 && afp->af_af != AF_LINK)
599 clearaddr = 1;
ca74a0a2 600 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
984263bc
MD
601}
602
ca74a0a2 603static void
b5744197 604settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
984263bc 605{
ca74a0a2 606 struct addrinfo *srcres, *dstres;
984263bc 607 int ecode;
984263bc 608
ca74a0a2
SZ
609 if (afp->af_settunnel == NULL) {
610 warn("address family %s does not support tunnel setup",
611 afp->af_name);
612 return;
613 }
984263bc
MD
614
615 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
616 errx(1, "error in parsing address string: %s",
617 gai_strerror(ecode));
618
619 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
620 errx(1, "error in parsing address string: %s",
621 gai_strerror(ecode));
622
623 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
624 errx(1,
625 "source and destination address families do not match");
626
ca74a0a2 627 afp->af_settunnel(s, srcres, dstres);
984263bc
MD
628
629 freeaddrinfo(srcres);
630 freeaddrinfo(dstres);
631}
632
633/* ARGSUSED */
ca74a0a2
SZ
634static void
635deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
984263bc 636{
ca74a0a2 637
984263bc
MD
638 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
639 err(1, "SIOCDIFPHYADDR");
640}
641
ca74a0a2
SZ
642static void
643setifnetmask(const char *addr, int dummy __unused, int s,
644 const struct afswtch *afp)
984263bc 645{
ca74a0a2
SZ
646 if (afp->af_getaddr != NULL) {
647 setmask++;
648 afp->af_getaddr(addr, MASK);
984263bc
MD
649 }
650}
651
ca74a0a2
SZ
652static void
653setifbroadaddr(const char *addr, int dummy __unused, int s,
654 const struct afswtch *afp)
984263bc 655{
ca74a0a2
SZ
656 if (afp->af_getaddr != NULL)
657 afp->af_getaddr(addr, DSTADDR);
984263bc 658}
984263bc 659
ca74a0a2
SZ
660static void
661setifipdst(const char *addr, int dummy __unused, int s,
662 const struct afswtch *afp)
984263bc 663{
ca74a0a2 664 const struct afswtch *inet;
984263bc 665
ca74a0a2
SZ
666 inet = af_getbyname("inet");
667 if (inet == NULL)
668 return;
669 inet->af_getaddr(addr, DSTADDR);
984263bc
MD
670 setipdst++;
671 clearaddr = 0;
672 newaddr = 0;
673}
984263bc 674
ca74a0a2
SZ
675static void
676notealias(const char *addr, int param, int s, const struct afswtch *afp)
984263bc 677{
ca74a0a2 678#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
984263bc 679 if (setaddr && doalias == 0 && param < 0)
b50e4759
MD
680 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
681 bcopy((caddr_t)rqtosa(af_addreq),
682 (caddr_t)rqtosa(af_ridreq),
683 rqtosa(af_addreq)->sa_len);
984263bc
MD
684 doalias = param;
685 if (param < 0) {
686 clearaddr = 1;
687 newaddr = 0;
688 } else
689 clearaddr = 0;
ca74a0a2 690#undef rqtosa
984263bc
MD
691}
692
693/*ARGSUSED*/
ca74a0a2
SZ
694static void
695setifdstaddr(const char *addr, int param __unused, int s,
696 const struct afswtch *afp)
984263bc 697{
ca74a0a2
SZ
698 if (afp->af_getaddr != NULL)
699 afp->af_getaddr(addr, DSTADDR);
984263bc
MD
700}
701
702/*
703 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
704 * of the ifreq structure, which may confuse other parts of ifconfig.
705 * Make a private copy so we can avoid that.
706 */
ca74a0a2
SZ
707static void
708setifflags(const char *vname, int value, int s, const struct afswtch *afp)
984263bc
MD
709{
710 struct ifreq my_ifr;
711
712 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
713
714 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
715 Perror("ioctl (SIOCGIFFLAGS)");
716 exit(1);
717 }
718 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
719 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
720
721 if (value < 0) {
722 value = -value;
723 flags &= ~value;
724 } else
725 flags |= value;
726 my_ifr.ifr_flags = flags & 0xffff;
727 my_ifr.ifr_flagshigh = flags >> 16;
728 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
729 Perror(vname);
730}
731
732void
ca74a0a2 733setifcap(const char *vname, int value, int s, const struct afswtch *afp)
984263bc
MD
734{
735
736 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
737 Perror("ioctl (SIOCGIFCAP)");
738 exit(1);
739 }
740 flags = ifr.ifr_curcap;
741 if (value < 0) {
742 value = -value;
743 flags &= ~value;
744 } else
745 flags |= value;
746 ifr.ifr_reqcap = flags;
747 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
748 Perror(vname);
749}
750
ca74a0a2
SZ
751static void
752setifmetric(const char *val, int dummy __unused, int s,
753 const struct afswtch *afp)
984263bc
MD
754{
755 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
756 ifr.ifr_metric = atoi(val);
757 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
758 warn("ioctl (set metric)");
759}
760
ca74a0a2
SZ
761static void
762setifmtu(const char *val, int dummy __unused, int s,
763 const struct afswtch *afp)
984263bc
MD
764{
765 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
766 ifr.ifr_mtu = atoi(val);
767 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
768 warn("ioctl (set mtu)");
769}
770
ca74a0a2 771static void
c021b82e 772setifname(const char *val, int dummy __unused, int s,
ca74a0a2 773 const struct afswtch *afp)
c021b82e 774{
ca74a0a2 775 char *newname;
c021b82e
HP
776
777 newname = strdup(val);
ca74a0a2
SZ
778 if (newname == NULL) {
779 warn("no memory to set ifname");
780 return;
781 }
c021b82e
HP
782 ifr.ifr_data = newname;
783 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
784 warn("ioctl (set name)");
785 free(newname);
786 return;
787 }
788 strlcpy(name, newname, sizeof(name));
c021b82e
HP
789 free(newname);
790
791 /*
792 * Even if we just created the interface, we don't need to print
793 * its name because we just nailed it down separately.
794 */
795 printname = 0;
796}
797
ca74a0a2
SZ
798/*
799 * Expand the compacted form of addresses as returned via the
800 * configuration read via sysctl().
801 */
802static void
803rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
804{
805 struct sockaddr *sa;
806 int i;
807
808 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
809 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
810 if ((rtinfo->rti_addrs & (1 << i)) == 0)
811 continue;
812 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
813 cp += SA_SIZE(sa);
814 }
815}
816
984263bc
MD
817#define IFFBITS \
818"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
819"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
ca74a0a2 820"\20MULTICAST\21POLLING\22PPROMISC\23MONITOR\24STATICARP\25NEEDSGIANT"
984263bc
MD
821
822#define IFCAPBITS \
b50e4759 823"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING"
984263bc
MD
824
825/*
826 * Print the status of the interface. If an address family was
ca74a0a2 827 * specified, show only it; otherwise, show them all.
984263bc 828 */
ca74a0a2
SZ
829static void
830status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
831 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
984263bc 832{
984263bc
MD
833 struct rt_addrinfo info;
834 int allfamilies, s;
835 struct ifstat ifs;
836
837 if (afp == NULL) {
838 allfamilies = 1;
ca74a0a2 839 afp = af_getbyname("inet");
984263bc
MD
840 } else
841 allfamilies = 0;
842
843 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
ca74a0a2 844 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
984263bc 845
ca74a0a2
SZ
846 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
847 if (s < 0)
848 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
984263bc
MD
849
850 printf("%s: ", name);
851 printb("flags", flags, IFFBITS);
852 if (ifm->ifm_data.ifi_metric)
853 printf(" metric %ld", ifm->ifm_data.ifi_metric);
854 if (ifm->ifm_data.ifi_mtu)
855 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
856 putchar('\n');
857
858 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
859 if (ifr.ifr_curcap != 0) {
860 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
861 putchar('\n');
862 }
863 if (supmedia && ifr.ifr_reqcap != 0) {
ca74a0a2 864 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
984263bc
MD
865 putchar('\n');
866 }
867 }
868
869 tunnel_status(s);
870
871 while (addrcount > 0) {
984263bc 872 info.rti_addrs = ifam->ifam_addrs;
984263bc
MD
873 /* Expand the compacted addresses */
874 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
875 &info);
876
ca74a0a2
SZ
877 if (allfamilies) {
878 const struct afswtch *p;
879 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
880 if (p != NULL && p->af_status != NULL)
881 p->af_status(s, &info);
882 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
883 afp->af_status(s, &info);
984263bc
MD
884 addrcount--;
885 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
886 }
ca74a0a2
SZ
887 if (allfamilies || afp->af_af == AF_LINK) {
888 const struct afswtch *lafp;
984263bc 889
984263bc 890 /*
ca74a0a2
SZ
891 * Hack; the link level address is received separately
892 * from the routing information so any address is not
893 * handled above. Cobble together an entry and invoke
894 * the status method specially.
984263bc 895 */
ca74a0a2
SZ
896 lafp = af_getbyname("lladdr");
897 if (lafp != NULL) {
898 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
899 lafp->af_status(s, &info);
984263bc
MD
900 }
901 }
ca74a0a2
SZ
902 if (allfamilies)
903 af_other_status(s);
904 else if (afp->af_other_status != NULL)
905 afp->af_other_status(s);
984263bc 906
ca74a0a2
SZ
907 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
908 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
909 printf("%s", ifs.ascii);
984263bc 910
984263bc 911 close(s);
ca74a0a2 912 return;
984263bc 913}
984263bc 914
ca74a0a2
SZ
915static void
916tunnel_status(int s)
984263bc 917{
ca74a0a2 918 af_all_tunnel_status(s);
984263bc
MD
919}
920
921void
b5744197 922Perror(const char *cmd)
984263bc
MD
923{
924 switch (errno) {
925
926 case ENXIO:
927 errx(1, "%s: no such interface", cmd);
928 break;
929
930 case EPERM:
931 errx(1, "%s: permission denied", cmd);
932 break;
933
934 default:
935 err(1, "%s", cmd);
936 }
937}
938
984263bc
MD
939/*
940 * Print a value a la the %b format of the kernel's printf
941 */
942void
8d5bb0c5 943printb(const char *s, unsigned v, const char *bits)
984263bc 944{
8d5bb0c5
CP
945 int i, any = 0;
946 char c;
984263bc
MD
947
948 if (bits && *bits == 8)
949 printf("%s=%o", s, v);
950 else
951 printf("%s=%x", s, v);
952 bits++;
953 if (bits) {
954 putchar('<');
955 while ((i = *bits++) != '\0') {
956 if (v & (1 << (i-1))) {
957 if (any)
958 putchar(',');
959 any = 1;
960 for (; (c = *bits) > 32; bits++)
961 putchar(c);
962 } else
963 for (; *bits > 32; bits++)
964 ;
965 }
966 putchar('>');
967 }
968}
969
984263bc 970void
ca74a0a2 971ifmaybeload(char *name)
984263bc
MD
972{
973 struct module_stat mstat;
974 int fileid, modid;
975 char ifkind[35], *cp, *dp;
976
984263bc
MD
977 /* turn interface and unit into module name */
978 strcpy(ifkind, "if_");
ca74a0a2 979 for (cp = name, dp = ifkind + 3;
984263bc
MD
980 (*cp != 0) && !isdigit(*cp); cp++, dp++)
981 *dp = *cp;
982 *dp = 0;
983
984 /* scan files in kernel */
985 mstat.version = sizeof(struct module_stat);
986 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
987 /* scan modules in file */
988 for (modid = kldfirstmod(fileid); modid > 0;
989 modid = modfnext(modid)) {
990 if (modstat(modid, &mstat) < 0)
991 continue;
992 /* strip bus name if present */
993 if ((cp = strchr(mstat.name, '/')) != NULL) {
994 cp++;
995 } else {
996 cp = mstat.name;
997 }
998 /* already loaded? */
ca74a0a2
SZ
999 if (strncmp(name, cp, strlen(cp)) == 0 ||
1000 strncmp(ifkind, cp, strlen(cp)) == 0)
984263bc
MD
1001 return;
1002 }
1003 }
1004
1005 /* not present, we should try to load it */
1006 kldload(ifkind);
1007}
1008
ca74a0a2
SZ
1009static struct cmd basic_cmds[] = {
1010 DEF_CMD("up", IFF_UP, setifflags),
1011 DEF_CMD("down", -IFF_UP, setifflags),
1012 DEF_CMD("arp", -IFF_NOARP, setifflags),
1013 DEF_CMD("-arp", IFF_NOARP, setifflags),
1014 DEF_CMD("debug", IFF_DEBUG, setifflags),
1015 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1016 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1017 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1018 DEF_CMD("add", IFF_UP, notealias),
1019 DEF_CMD("alias", IFF_UP, notealias),
1020 DEF_CMD("-alias", -IFF_UP, notealias),
1021 DEF_CMD("delete", -IFF_UP, notealias),
1022 DEF_CMD("remove", -IFF_UP, notealias),
1023#ifdef notdef
1024#define EN_SWABIPS 0x1000
1025 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1026 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1027#endif
1028 DEF_CMD_ARG("netmask", setifnetmask),
1029 DEF_CMD_ARG("metric", setifmetric),
1030 DEF_CMD_ARG("broadcast", setifbroadaddr),
1031#ifndef NO_IPX
1032 DEF_CMD_ARG("ipdst", setifipdst),
1033#endif
1034 DEF_CMD_ARG2("tunnel", settunnel),
1035 DEF_CMD("-tunnel", 0, deletetunnel),
1036 DEF_CMD("deletetunnel", 0, deletetunnel),
1037 DEF_CMD("link0", IFF_LINK0, setifflags),
1038 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1039 DEF_CMD("link1", IFF_LINK1, setifflags),
1040 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1041 DEF_CMD("link2", IFF_LINK2, setifflags),
1042 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1043#ifdef notyet
1044 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1045 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1046 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1047 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1048#endif
1049 DEF_CMD("polling", IFF_POLLING, setifcap),
1050 DEF_CMD("-polling", -IFF_POLLING, setifcap),
1051 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1052 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1053 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1054 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1055 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1056 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1057 DEF_CMD("normal", -IFF_LINK0, setifflags),
1058 DEF_CMD("compress", IFF_LINK0, setifflags),
1059 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1060 DEF_CMD_ARG("mtu", setifmtu),
1061 DEF_CMD_ARG("name", setifname),
1062};
984263bc 1063
ca74a0a2
SZ
1064static __constructor void
1065ifconfig_ctor(void)
984263bc 1066{
ca74a0a2
SZ
1067#define N(a) (sizeof(a) / sizeof(a[0]))
1068 int i;
984263bc 1069
ca74a0a2
SZ
1070 for (i = 0; i < N(basic_cmds); i++)
1071 cmd_register(&basic_cmds[i]);
1072#undef N
984263bc 1073}