]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/ifconfig.c
MFV r275548:
[FreeBSD/FreeBSD.git] / sbin / ifconfig / ifconfig.c
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.
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.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)ifconfig.c  8.2 (Berkeley) 2/16/94";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/module.h>
49 #include <sys/linker.h>
50
51 #include <net/ethernet.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57
58 /* IP */
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <arpa/inet.h>
62 #include <netdb.h>
63
64 #include <ifaddrs.h>
65 #include <ctype.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #ifdef JAIL
70 #include <jail.h>
71 #endif
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76
77 #include "ifconfig.h"
78
79 /*
80  * Since "struct ifreq" is composed of various union members, callers
81  * should pay special attention to interpret the value.
82  * (.e.g. little/big endian difference in the structure.)
83  */
84 struct  ifreq ifr;
85
86 char    name[IFNAMSIZ];
87 char    *descr = NULL;
88 size_t  descrlen = 64;
89 int     setaddr;
90 int     setmask;
91 int     doalias;
92 int     clearaddr;
93 int     newaddr = 1;
94 int     verbose;
95 int     noload;
96
97 int     supmedia = 0;
98 int     printkeys = 0;          /* Print keying material for interfaces. */
99
100 static  int ifconfig(int argc, char *const *argv, int iscreate,
101                 const struct afswtch *afp);
102 static  void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
103                 struct ifaddrs *ifa);
104 static  void tunnel_status(int s);
105 static  void usage(void);
106
107 static struct afswtch *af_getbyname(const char *name);
108 static struct afswtch *af_getbyfamily(int af);
109 static void af_other_status(int);
110
111 static struct option *opts = NULL;
112
113 void
114 opt_register(struct option *p)
115 {
116         p->next = opts;
117         opts = p;
118 }
119
120 static void
121 usage(void)
122 {
123         char options[1024];
124         struct option *p;
125
126         /* XXX not right but close enough for now */
127         options[0] = '\0';
128         for (p = opts; p != NULL; p = p->next) {
129                 strlcat(options, p->opt_usage, sizeof(options));
130                 strlcat(options, " ", sizeof(options));
131         }
132
133         fprintf(stderr,
134         "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
135         "                [parameters]\n"
136         "       ifconfig interface create\n"
137         "       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
138         "       ifconfig -l [-d] [-u] [address_family]\n"
139         "       ifconfig %s[-d] [-m] [-u] [-v]\n",
140                 options, options, options);
141         exit(1);
142 }
143
144 int
145 main(int argc, char *argv[])
146 {
147         int c, all, namesonly, downonly, uponly;
148         const struct afswtch *afp = NULL;
149         int ifindex;
150         struct ifaddrs *ifap, *ifa;
151         struct ifreq paifr;
152         const struct sockaddr_dl *sdl;
153         char options[1024], *cp, *namecp = NULL;
154         const char *ifname;
155         struct option *p;
156         size_t iflen;
157
158         all = downonly = uponly = namesonly = noload = verbose = 0;
159
160         /* Parse leading line options */
161         strlcpy(options, "adklmnuv", 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) {
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;
172                 case 'k':
173                         printkeys++;
174                         break;
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 'n':       /* suppress module loading */
182                         noload++;
183                         break;
184                 case 'u':       /* restrict scan to "up" interfaces */
185                         uponly++;
186                         break;
187                 case 'v':
188                         verbose++;
189                         break;
190                 default:
191                         for (p = opts; p != NULL; p = p->next)
192                                 if (p->opt[0] == c) {
193                                         p->cb(optarg);
194                                         break;
195                                 }
196                         if (p == NULL)
197                                 usage();
198                         break;
199                 }
200         }
201         argc -= optind;
202         argv += optind;
203
204         /* -l cannot be used with -a or -m */
205         if (namesonly && (all || supmedia))
206                 usage();
207
208         /* nonsense.. */
209         if (uponly && downonly)
210                 usage();
211
212         /* no arguments is equivalent to '-a' */
213         if (!namesonly && argc < 1)
214                 all = 1;
215
216         /* -a and -l allow an address family arg to limit the output */
217         if (all || namesonly) {
218                 if (argc > 1)
219                         usage();
220
221                 ifname = NULL;
222                 ifindex = 0;
223                 if (argc == 1) {
224                         afp = af_getbyname(*argv);
225                         if (afp == NULL) {
226                                 warnx("Address family '%s' unknown.", *argv);
227                                 usage();
228                         }
229                         if (afp->af_name != NULL)
230                                 argc--, argv++;
231                         /* leave with afp non-zero */
232                 }
233         } else {
234                 /* not listing, need an argument */
235                 if (argc < 1)
236                         usage();
237
238                 ifname = *argv;
239                 argc--, argv++;
240
241                 /* check and maybe load support for this interface */
242                 ifmaybeload(ifname);
243
244                 ifindex = if_nametoindex(ifname);
245                 if (ifindex == 0) {
246                         /*
247                          * NOTE:  We must special-case the `create' command
248                          * right here as we would otherwise fail when trying
249                          * to find the interface.
250                          */
251                         if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
252                             strcmp(argv[0], "plumb") == 0)) {
253                                 iflen = strlcpy(name, ifname, sizeof(name));
254                                 if (iflen >= sizeof(name))
255                                         errx(1, "%s: cloning name too long",
256                                             ifname);
257                                 ifconfig(argc, argv, 1, NULL);
258                                 exit(0);
259                         }
260 #ifdef JAIL
261                         /*
262                          * NOTE:  We have to special-case the `-vnet' command
263                          * right here as we would otherwise fail when trying
264                          * to find the interface as it lives in another vnet.
265                          */
266                         if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
267                                 iflen = strlcpy(name, ifname, sizeof(name));
268                                 if (iflen >= sizeof(name))
269                                         errx(1, "%s: interface name too long",
270                                             ifname);
271                                 ifconfig(argc, argv, 0, NULL);
272                                 exit(0);
273                         }
274 #endif
275                         errx(1, "interface %s does not exist", ifname);
276                 }
277         }
278
279         /* Check for address family */
280         if (argc > 0) {
281                 afp = af_getbyname(*argv);
282                 if (afp != NULL)
283                         argc--, argv++;
284         }
285
286         if (getifaddrs(&ifap) != 0)
287                 err(EXIT_FAILURE, "getifaddrs");
288         cp = NULL;
289         ifindex = 0;
290         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
291                 memset(&paifr, 0, sizeof(paifr));
292                 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
293                 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
294                         memcpy(&paifr.ifr_addr, ifa->ifa_addr,
295                             ifa->ifa_addr->sa_len);
296                 }
297
298                 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
299                         continue;
300                 if (ifa->ifa_addr->sa_family == AF_LINK)
301                         sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
302                 else
303                         sdl = NULL;
304                 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
305                         continue;
306                 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
307                 if (iflen >= sizeof(name)) {
308                         warnx("%s: interface name too long, skipping",
309                             ifa->ifa_name);
310                         continue;
311                 }
312                 cp = ifa->ifa_name;
313
314                 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
315                         continue;
316                 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
317                         continue;
318                 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
319                         continue;
320                 /*
321                  * Are we just listing the interfaces?
322                  */
323                 if (namesonly) {
324                         if (namecp == cp)
325                                 continue;
326                         if (afp != NULL) {
327                                 /* special case for "ether" address family */
328                                 if (!strcmp(afp->af_name, "ether")) {
329                                         if (sdl == NULL ||
330                                             (sdl->sdl_type != IFT_ETHER &&
331                                             sdl->sdl_type != IFT_L2VLAN &&
332                                             sdl->sdl_type != IFT_BRIDGE) ||
333                                             sdl->sdl_alen != ETHER_ADDR_LEN)
334                                                 continue;
335                                 } else {
336                                         if (ifa->ifa_addr->sa_family != afp->af_af)
337                                                 continue;
338                                 }
339                         }
340                         namecp = cp;
341                         ifindex++;
342                         if (ifindex > 1)
343                                 printf(" ");
344                         fputs(name, stdout);
345                         continue;
346                 }
347                 ifindex++;
348
349                 if (argc > 0)
350                         ifconfig(argc, argv, 0, afp);
351                 else
352                         status(afp, sdl, ifa);
353         }
354         if (namesonly)
355                 printf("\n");
356         freeifaddrs(ifap);
357
358         exit(0);
359 }
360
361 static struct afswtch *afs = NULL;
362
363 void
364 af_register(struct afswtch *p)
365 {
366         p->af_next = afs;
367         afs = p;
368 }
369
370 static struct afswtch *
371 af_getbyname(const char *name)
372 {
373         struct afswtch *afp;
374
375         for (afp = afs; afp !=  NULL; afp = afp->af_next)
376                 if (strcmp(afp->af_name, name) == 0)
377                         return afp;
378         return NULL;
379 }
380
381 static struct afswtch *
382 af_getbyfamily(int af)
383 {
384         struct afswtch *afp;
385
386         for (afp = afs; afp != NULL; afp = afp->af_next)
387                 if (afp->af_af == af)
388                         return afp;
389         return NULL;
390 }
391
392 static void
393 af_other_status(int s)
394 {
395         struct afswtch *afp;
396         uint8_t afmask[howmany(AF_MAX, NBBY)];
397
398         memset(afmask, 0, sizeof(afmask));
399         for (afp = afs; afp != NULL; afp = afp->af_next) {
400                 if (afp->af_other_status == NULL)
401                         continue;
402                 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
403                         continue;
404                 afp->af_other_status(s);
405                 setbit(afmask, afp->af_af);
406         }
407 }
408
409 static void
410 af_all_tunnel_status(int s)
411 {
412         struct afswtch *afp;
413         uint8_t afmask[howmany(AF_MAX, NBBY)];
414
415         memset(afmask, 0, sizeof(afmask));
416         for (afp = afs; afp != NULL; afp = afp->af_next) {
417                 if (afp->af_status_tunnel == NULL)
418                         continue;
419                 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
420                         continue;
421                 afp->af_status_tunnel(s);
422                 setbit(afmask, afp->af_af);
423         }
424 }
425
426 static struct cmd *cmds = NULL;
427
428 void
429 cmd_register(struct cmd *p)
430 {
431         p->c_next = cmds;
432         cmds = p;
433 }
434
435 static const struct cmd *
436 cmd_lookup(const char *name, int iscreate)
437 {
438 #define N(a)    (sizeof(a)/sizeof(a[0]))
439         const struct cmd *p;
440
441         for (p = cmds; p != NULL; p = p->c_next)
442                 if (strcmp(name, p->c_name) == 0) {
443                         if (iscreate) {
444                                 if (p->c_iscloneop)
445                                         return p;
446                         } else {
447                                 if (!p->c_iscloneop)
448                                         return p;
449                         }
450                 }
451         return NULL;
452 #undef N
453 }
454
455 struct callback {
456         callback_func *cb_func;
457         void    *cb_arg;
458         struct callback *cb_next;
459 };
460 static struct callback *callbacks = NULL;
461
462 void
463 callback_register(callback_func *func, void *arg)
464 {
465         struct callback *cb;
466
467         cb = malloc(sizeof(struct callback));
468         if (cb == NULL)
469                 errx(1, "unable to allocate memory for callback");
470         cb->cb_func = func;
471         cb->cb_arg = arg;
472         cb->cb_next = callbacks;
473         callbacks = cb;
474 }
475
476 /* specially-handled commands */
477 static void setifaddr(const char *, int, int, const struct afswtch *);
478 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
479
480 static void setifdstaddr(const char *, int, int, const struct afswtch *);
481 static const struct cmd setifdstaddr_cmd =
482         DEF_CMD("ifdstaddr", 0, setifdstaddr);
483
484 static int
485 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
486 {
487         const struct afswtch *afp, *nafp;
488         const struct cmd *p;
489         struct callback *cb;
490         int s;
491
492         strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
493         afp = NULL;
494         if (uafp != NULL)
495                 afp = uafp;
496         /*
497          * This is the historical "accident" allowing users to configure IPv4
498          * addresses without the "inet" keyword which while a nice feature has
499          * proven to complicate other things.  We cannot remove this but only
500          * make sure we will never have a similar implicit default for IPv6 or
501          * any other address familiy.  We need a fallback though for
502          * ifconfig IF up/down etc. to work without INET support as people
503          * never used ifconfig IF link up/down, etc. either.
504          */
505 #ifndef RESCUE
506 #ifdef INET
507         if (afp == NULL && feature_present("inet"))
508                 afp = af_getbyname("inet");
509 #endif
510 #endif
511         if (afp == NULL)
512                 afp = af_getbyname("link");
513         if (afp == NULL) {
514                 warnx("Please specify an address_family.");
515                 usage();
516         }
517 top:
518         ifr.ifr_addr.sa_family =
519                 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
520                 AF_LOCAL : afp->af_af;
521
522         if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
523             (uafp != NULL || errno != EAFNOSUPPORT ||
524              (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
525                 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
526
527         while (argc > 0) {
528                 p = cmd_lookup(*argv, iscreate);
529                 if (iscreate && p == NULL) {
530                         /*
531                          * Push the clone create callback so the new
532                          * device is created and can be used for any
533                          * remaining arguments.
534                          */
535                         cb = callbacks;
536                         if (cb == NULL)
537                                 errx(1, "internal error, no callback");
538                         callbacks = cb->cb_next;
539                         cb->cb_func(s, cb->cb_arg);
540                         iscreate = 0;
541                         /*
542                          * Handle any address family spec that
543                          * immediately follows and potentially
544                          * recreate the socket.
545                          */
546                         nafp = af_getbyname(*argv);
547                         if (nafp != NULL) {
548                                 argc--, argv++;
549                                 if (nafp != afp) {
550                                         close(s);
551                                         afp = nafp;
552                                         goto top;
553                                 }
554                         }
555                         /*
556                          * Look for a normal parameter.
557                          */
558                         continue;
559                 }
560                 if (p == NULL) {
561                         /*
562                          * Not a recognized command, choose between setting
563                          * the interface address and the dst address.
564                          */
565                         p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
566                 }
567                 if (p->c_u.c_func || p->c_u.c_func2) {
568                         if (p->c_parameter == NEXTARG) {
569                                 if (argv[1] == NULL)
570                                         errx(1, "'%s' requires argument",
571                                             p->c_name);
572                                 p->c_u.c_func(argv[1], 0, s, afp);
573                                 argc--, argv++;
574                         } else if (p->c_parameter == OPTARG) {
575                                 p->c_u.c_func(argv[1], 0, s, afp);
576                                 if (argv[1] != NULL)
577                                         argc--, argv++;
578                         } else if (p->c_parameter == NEXTARG2) {
579                                 if (argc < 3)
580                                         errx(1, "'%s' requires 2 arguments",
581                                             p->c_name);
582                                 p->c_u.c_func2(argv[1], argv[2], s, afp);
583                                 argc -= 2, argv += 2;
584                         } else
585                                 p->c_u.c_func(*argv, p->c_parameter, s, afp);
586                 }
587                 argc--, argv++;
588         }
589
590         /*
591          * Do any post argument processing required by the address family.
592          */
593         if (afp->af_postproc != NULL)
594                 afp->af_postproc(s, afp);
595         /*
596          * Do deferred callbacks registered while processing
597          * command-line arguments.
598          */
599         for (cb = callbacks; cb != NULL; cb = cb->cb_next)
600                 cb->cb_func(s, cb->cb_arg);
601         /*
602          * Do deferred operations.
603          */
604         if (clearaddr) {
605                 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
606                         warnx("interface %s cannot change %s addresses!",
607                               name, afp->af_name);
608                         clearaddr = 0;
609                 }
610         }
611         if (clearaddr) {
612                 int ret;
613                 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
614                 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
615                 if (ret < 0) {
616                         if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
617                                 /* means no previous address for interface */
618                         } else
619                                 Perror("ioctl (SIOCDIFADDR)");
620                 }
621         }
622         if (newaddr) {
623                 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
624                         warnx("interface %s cannot change %s addresses!",
625                               name, afp->af_name);
626                         newaddr = 0;
627                 }
628         }
629         if (newaddr && (setaddr || setmask)) {
630                 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
631                 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
632                         Perror("ioctl (SIOCAIFADDR)");
633         }
634
635         close(s);
636         return(0);
637 }
638
639 /*ARGSUSED*/
640 static void
641 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
642 {
643         if (afp->af_getaddr == NULL)
644                 return;
645         /*
646          * Delay the ioctl to set the interface addr until flags are all set.
647          * The address interpretation may depend on the flags,
648          * and the flags may change when the address is set.
649          */
650         setaddr++;
651         if (doalias == 0 && afp->af_af != AF_LINK)
652                 clearaddr = 1;
653         afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
654 }
655
656 static void
657 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
658 {
659         struct addrinfo *srcres, *dstres;
660         int ecode;
661
662         if (afp->af_settunnel == NULL) {
663                 warn("address family %s does not support tunnel setup",
664                         afp->af_name);
665                 return;
666         }
667
668         if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
669                 errx(1, "error in parsing address string: %s",
670                     gai_strerror(ecode));
671
672         if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)  
673                 errx(1, "error in parsing address string: %s",
674                     gai_strerror(ecode));
675
676         if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
677                 errx(1,
678                     "source and destination address families do not match");
679
680         afp->af_settunnel(s, srcres, dstres);
681
682         freeaddrinfo(srcres);
683         freeaddrinfo(dstres);
684 }
685
686 /* ARGSUSED */
687 static void
688 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
689 {
690
691         if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
692                 err(1, "SIOCDIFPHYADDR");
693 }
694
695 #ifdef JAIL
696 static void
697 setifvnet(const char *jname, int dummy __unused, int s,
698     const struct afswtch *afp)
699 {
700         struct ifreq my_ifr;
701
702         memcpy(&my_ifr, &ifr, sizeof(my_ifr));
703         my_ifr.ifr_jid = jail_getid(jname);
704         if (my_ifr.ifr_jid < 0)
705                 errx(1, "%s", jail_errmsg);
706         if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
707                 err(1, "SIOCSIFVNET");
708 }
709
710 static void
711 setifrvnet(const char *jname, int dummy __unused, int s,
712     const struct afswtch *afp)
713 {
714         struct ifreq my_ifr;
715
716         memcpy(&my_ifr, &ifr, sizeof(my_ifr));
717         my_ifr.ifr_jid = jail_getid(jname);
718         if (my_ifr.ifr_jid < 0)
719                 errx(1, "%s", jail_errmsg);
720         if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
721                 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
722 }
723 #endif
724
725 static void
726 setifnetmask(const char *addr, int dummy __unused, int s,
727     const struct afswtch *afp)
728 {
729         if (afp->af_getaddr != NULL) {
730                 setmask++;
731                 afp->af_getaddr(addr, MASK);
732         }
733 }
734
735 static void
736 setifbroadaddr(const char *addr, int dummy __unused, int s,
737     const struct afswtch *afp)
738 {
739         if (afp->af_getaddr != NULL)
740                 afp->af_getaddr(addr, DSTADDR);
741 }
742
743 static void
744 notealias(const char *addr, int param, int s, const struct afswtch *afp)
745 {
746 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
747         if (setaddr && doalias == 0 && param < 0)
748                 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
749                         bcopy((caddr_t)rqtosa(af_addreq),
750                               (caddr_t)rqtosa(af_ridreq),
751                               rqtosa(af_addreq)->sa_len);
752         doalias = param;
753         if (param < 0) {
754                 clearaddr = 1;
755                 newaddr = 0;
756         } else
757                 clearaddr = 0;
758 #undef rqtosa
759 }
760
761 /*ARGSUSED*/
762 static void
763 setifdstaddr(const char *addr, int param __unused, int s, 
764     const struct afswtch *afp)
765 {
766         if (afp->af_getaddr != NULL)
767                 afp->af_getaddr(addr, DSTADDR);
768 }
769
770 /*
771  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
772  * of the ifreq structure, which may confuse other parts of ifconfig.
773  * Make a private copy so we can avoid that.
774  */
775 static void
776 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
777 {
778         struct ifreq            my_ifr;
779         int flags;
780
781         memset(&my_ifr, 0, sizeof(my_ifr));
782         (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
783
784         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
785                 Perror("ioctl (SIOCGIFFLAGS)");
786                 exit(1);
787         }
788         flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
789
790         if (value < 0) {
791                 value = -value;
792                 flags &= ~value;
793         } else
794                 flags |= value;
795         my_ifr.ifr_flags = flags & 0xffff;
796         my_ifr.ifr_flagshigh = flags >> 16;
797         if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
798                 Perror(vname);
799 }
800
801 void
802 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
803 {
804         int flags;
805
806         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
807                 Perror("ioctl (SIOCGIFCAP)");
808                 exit(1);
809         }
810         flags = ifr.ifr_curcap;
811         if (value < 0) {
812                 value = -value;
813                 flags &= ~value;
814         } else
815                 flags |= value;
816         flags &= ifr.ifr_reqcap;
817         ifr.ifr_reqcap = flags;
818         if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
819                 Perror(vname);
820 }
821
822 static void
823 setifmetric(const char *val, int dummy __unused, int s, 
824     const struct afswtch *afp)
825 {
826         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
827         ifr.ifr_metric = atoi(val);
828         if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
829                 warn("ioctl (set metric)");
830 }
831
832 static void
833 setifmtu(const char *val, int dummy __unused, int s, 
834     const struct afswtch *afp)
835 {
836         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
837         ifr.ifr_mtu = atoi(val);
838         if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
839                 warn("ioctl (set mtu)");
840 }
841
842 static void
843 setifname(const char *val, int dummy __unused, int s, 
844     const struct afswtch *afp)
845 {
846         char *newname;
847
848         newname = strdup(val);
849         if (newname == NULL) {
850                 warn("no memory to set ifname");
851                 return;
852         }
853         ifr.ifr_data = newname;
854         if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
855                 warn("ioctl (set name)");
856                 free(newname);
857                 return;
858         }
859         strlcpy(name, newname, sizeof(name));
860         free(newname);
861 }
862
863 /* ARGSUSED */
864 static void
865 setifdescr(const char *val, int dummy __unused, int s, 
866     const struct afswtch *afp)
867 {
868         char *newdescr;
869
870         ifr.ifr_buffer.length = strlen(val) + 1;
871         if (ifr.ifr_buffer.length == 1) {
872                 ifr.ifr_buffer.buffer = newdescr = NULL;
873                 ifr.ifr_buffer.length = 0;
874         } else {
875                 newdescr = strdup(val);
876                 ifr.ifr_buffer.buffer = newdescr;
877                 if (newdescr == NULL) {
878                         warn("no memory to set ifdescr");
879                         return;
880                 }
881         }
882
883         if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
884                 warn("ioctl (set descr)");
885
886         free(newdescr);
887 }
888
889 /* ARGSUSED */
890 static void
891 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
892 {
893
894         setifdescr("", 0, s, 0);
895 }
896
897 #define IFFBITS \
898 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
899 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
900 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
901
902 #define IFCAPBITS \
903 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
904 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
905 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
906 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
907
908 /*
909  * Print the status of the interface.  If an address family was
910  * specified, show only it; otherwise, show them all.
911  */
912 static void
913 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
914         struct ifaddrs *ifa)
915 {
916         struct ifaddrs *ift;
917         int allfamilies, s;
918         struct ifstat ifs;
919
920         if (afp == NULL) {
921                 allfamilies = 1;
922                 ifr.ifr_addr.sa_family = AF_LOCAL;
923         } else {
924                 allfamilies = 0;
925                 ifr.ifr_addr.sa_family =
926                     afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
927         }
928         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
929
930         s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
931         if (s < 0)
932                 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
933
934         printf("%s: ", name);
935         printb("flags", ifa->ifa_flags, IFFBITS);
936         if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
937                 printf(" metric %d", ifr.ifr_metric);
938         if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
939                 printf(" mtu %d", ifr.ifr_mtu);
940         putchar('\n');
941
942         for (;;) {
943                 if ((descr = reallocf(descr, descrlen)) != NULL) {
944                         ifr.ifr_buffer.buffer = descr;
945                         ifr.ifr_buffer.length = descrlen;
946                         if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
947                                 if (ifr.ifr_buffer.buffer == descr) {
948                                         if (strlen(descr) > 0)
949                                                 printf("\tdescription: %s\n",
950                                                     descr);
951                                 } else if (ifr.ifr_buffer.length > descrlen) {
952                                         descrlen = ifr.ifr_buffer.length;
953                                         continue;
954                                 }
955                         }
956                 } else
957                         warn("unable to allocate memory for interface"
958                             "description");
959                 break;
960         }
961
962         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
963                 if (ifr.ifr_curcap != 0) {
964                         printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
965                         putchar('\n');
966                 }
967                 if (supmedia && ifr.ifr_reqcap != 0) {
968                         printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
969                         putchar('\n');
970                 }
971         }
972
973         tunnel_status(s);
974
975         for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
976                 if (ift->ifa_addr == NULL)
977                         continue;
978                 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
979                         continue;
980                 if (allfamilies) {
981                         const struct afswtch *p;
982                         p = af_getbyfamily(ift->ifa_addr->sa_family);
983                         if (p != NULL && p->af_status != NULL)
984                                 p->af_status(s, ift);
985                 } else if (afp->af_af == ift->ifa_addr->sa_family)
986                         afp->af_status(s, ift);
987         }
988 #if 0
989         if (allfamilies || afp->af_af == AF_LINK) {
990                 const struct afswtch *lafp;
991
992                 /*
993                  * Hack; the link level address is received separately
994                  * from the routing information so any address is not
995                  * handled above.  Cobble together an entry and invoke
996                  * the status method specially.
997                  */
998                 lafp = af_getbyname("lladdr");
999                 if (lafp != NULL) {
1000                         info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1001                         lafp->af_status(s, &info);
1002                 }
1003         }
1004 #endif
1005         if (allfamilies)
1006                 af_other_status(s);
1007         else if (afp->af_other_status != NULL)
1008                 afp->af_other_status(s);
1009
1010         strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1011         if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
1012                 printf("%s", ifs.ascii);
1013
1014         if (verbose > 0)
1015                 sfp_status(s, &ifr, verbose);
1016
1017         close(s);
1018         return;
1019 }
1020
1021 static void
1022 tunnel_status(int s)
1023 {
1024         af_all_tunnel_status(s);
1025 }
1026
1027 void
1028 Perror(const char *cmd)
1029 {
1030         switch (errno) {
1031
1032         case ENXIO:
1033                 errx(1, "%s: no such interface", cmd);
1034                 break;
1035
1036         case EPERM:
1037                 errx(1, "%s: permission denied", cmd);
1038                 break;
1039
1040         default:
1041                 err(1, "%s", cmd);
1042         }
1043 }
1044
1045 /*
1046  * Print a value a la the %b format of the kernel's printf
1047  */
1048 void
1049 printb(const char *s, unsigned v, const char *bits)
1050 {
1051         int i, any = 0;
1052         char c;
1053
1054         if (bits && *bits == 8)
1055                 printf("%s=%o", s, v);
1056         else
1057                 printf("%s=%x", s, v);
1058         bits++;
1059         if (bits) {
1060                 putchar('<');
1061                 while ((i = *bits++) != '\0') {
1062                         if (v & (1 << (i-1))) {
1063                                 if (any)
1064                                         putchar(',');
1065                                 any = 1;
1066                                 for (; (c = *bits) > 32; bits++)
1067                                         putchar(c);
1068                         } else
1069                                 for (; *bits > 32; bits++)
1070                                         ;
1071                 }
1072                 putchar('>');
1073         }
1074 }
1075
1076 void
1077 print_vhid(const struct ifaddrs *ifa, const char *s)
1078 {
1079         struct if_data *ifd;
1080
1081         if (ifa->ifa_data == NULL)
1082                 return;
1083
1084         ifd = ifa->ifa_data;
1085         if (ifd->ifi_vhid == 0)
1086                 return;
1087         
1088         printf("vhid %d ", ifd->ifi_vhid);
1089 }
1090
1091 void
1092 ifmaybeload(const char *name)
1093 {
1094 #define MOD_PREFIX_LEN          3       /* "if_" */
1095         struct module_stat mstat;
1096         int fileid, modid;
1097         char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1098         const char *cp;
1099
1100         /* loading suppressed by the user */
1101         if (noload)
1102                 return;
1103
1104         /* trim the interface number off the end */
1105         strlcpy(ifname, name, sizeof(ifname));
1106         for (dp = ifname; *dp != 0; dp++)
1107                 if (isdigit(*dp)) {
1108                         *dp = 0;
1109                         break;
1110                 }
1111
1112         /* turn interface and unit into module name */
1113         strcpy(ifkind, "if_");
1114         strlcpy(ifkind + MOD_PREFIX_LEN, ifname,
1115             sizeof(ifkind) - MOD_PREFIX_LEN);
1116
1117         /* scan files in kernel */
1118         mstat.version = sizeof(struct module_stat);
1119         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1120                 /* scan modules in file */
1121                 for (modid = kldfirstmod(fileid); modid > 0;
1122                      modid = modfnext(modid)) {
1123                         if (modstat(modid, &mstat) < 0)
1124                                 continue;
1125                         /* strip bus name if present */
1126                         if ((cp = strchr(mstat.name, '/')) != NULL) {
1127                                 cp++;
1128                         } else {
1129                                 cp = mstat.name;
1130                         }
1131                         /* already loaded? */
1132                         if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 ||
1133                             strncmp(ifkind, cp, strlen(ifkind) + 1) == 0)
1134                                 return;
1135                 }
1136         }
1137
1138         /* not present, we should try to load it */
1139         kldload(ifkind);
1140 }
1141
1142 static struct cmd basic_cmds[] = {
1143         DEF_CMD("up",           IFF_UP,         setifflags),
1144         DEF_CMD("down",         -IFF_UP,        setifflags),
1145         DEF_CMD("arp",          -IFF_NOARP,     setifflags),
1146         DEF_CMD("-arp",         IFF_NOARP,      setifflags),
1147         DEF_CMD("debug",        IFF_DEBUG,      setifflags),
1148         DEF_CMD("-debug",       -IFF_DEBUG,     setifflags),
1149         DEF_CMD_ARG("description",              setifdescr),
1150         DEF_CMD_ARG("descr",                    setifdescr),
1151         DEF_CMD("-description", 0,              unsetifdescr),
1152         DEF_CMD("-descr",       0,              unsetifdescr),
1153         DEF_CMD("promisc",      IFF_PPROMISC,   setifflags),
1154         DEF_CMD("-promisc",     -IFF_PPROMISC,  setifflags),
1155         DEF_CMD("add",          IFF_UP,         notealias),
1156         DEF_CMD("alias",        IFF_UP,         notealias),
1157         DEF_CMD("-alias",       -IFF_UP,        notealias),
1158         DEF_CMD("delete",       -IFF_UP,        notealias),
1159         DEF_CMD("remove",       -IFF_UP,        notealias),
1160 #ifdef notdef
1161 #define EN_SWABIPS      0x1000
1162         DEF_CMD("swabips",      EN_SWABIPS,     setifflags),
1163         DEF_CMD("-swabips",     -EN_SWABIPS,    setifflags),
1164 #endif
1165         DEF_CMD_ARG("netmask",                  setifnetmask),
1166         DEF_CMD_ARG("metric",                   setifmetric),
1167         DEF_CMD_ARG("broadcast",                setifbroadaddr),
1168         DEF_CMD_ARG2("tunnel",                  settunnel),
1169         DEF_CMD("-tunnel", 0,                   deletetunnel),
1170         DEF_CMD("deletetunnel", 0,              deletetunnel),
1171 #ifdef JAIL
1172         DEF_CMD_ARG("vnet",                     setifvnet),
1173         DEF_CMD_ARG("-vnet",                    setifrvnet),
1174 #endif
1175         DEF_CMD("link0",        IFF_LINK0,      setifflags),
1176         DEF_CMD("-link0",       -IFF_LINK0,     setifflags),
1177         DEF_CMD("link1",        IFF_LINK1,      setifflags),
1178         DEF_CMD("-link1",       -IFF_LINK1,     setifflags),
1179         DEF_CMD("link2",        IFF_LINK2,      setifflags),
1180         DEF_CMD("-link2",       -IFF_LINK2,     setifflags),
1181         DEF_CMD("monitor",      IFF_MONITOR,    setifflags),
1182         DEF_CMD("-monitor",     -IFF_MONITOR,   setifflags),
1183         DEF_CMD("staticarp",    IFF_STATICARP,  setifflags),
1184         DEF_CMD("-staticarp",   -IFF_STATICARP, setifflags),
1185         DEF_CMD("rxcsum6",      IFCAP_RXCSUM_IPV6,      setifcap),
1186         DEF_CMD("-rxcsum6",     -IFCAP_RXCSUM_IPV6,     setifcap),
1187         DEF_CMD("txcsum6",      IFCAP_TXCSUM_IPV6,      setifcap),
1188         DEF_CMD("-txcsum6",     -IFCAP_TXCSUM_IPV6,     setifcap),
1189         DEF_CMD("rxcsum",       IFCAP_RXCSUM,   setifcap),
1190         DEF_CMD("-rxcsum",      -IFCAP_RXCSUM,  setifcap),
1191         DEF_CMD("txcsum",       IFCAP_TXCSUM,   setifcap),
1192         DEF_CMD("-txcsum",      -IFCAP_TXCSUM,  setifcap),
1193         DEF_CMD("netcons",      IFCAP_NETCONS,  setifcap),
1194         DEF_CMD("-netcons",     -IFCAP_NETCONS, setifcap),
1195         DEF_CMD("polling",      IFCAP_POLLING,  setifcap),
1196         DEF_CMD("-polling",     -IFCAP_POLLING, setifcap),
1197         DEF_CMD("tso6",         IFCAP_TSO6,     setifcap),
1198         DEF_CMD("-tso6",        -IFCAP_TSO6,    setifcap),
1199         DEF_CMD("tso4",         IFCAP_TSO4,     setifcap),
1200         DEF_CMD("-tso4",        -IFCAP_TSO4,    setifcap),
1201         DEF_CMD("tso",          IFCAP_TSO,      setifcap),
1202         DEF_CMD("-tso",         -IFCAP_TSO,     setifcap),
1203         DEF_CMD("toe",          IFCAP_TOE,      setifcap),
1204         DEF_CMD("-toe",         -IFCAP_TOE,     setifcap),
1205         DEF_CMD("lro",          IFCAP_LRO,      setifcap),
1206         DEF_CMD("-lro",         -IFCAP_LRO,     setifcap),
1207         DEF_CMD("wol",          IFCAP_WOL,      setifcap),
1208         DEF_CMD("-wol",         -IFCAP_WOL,     setifcap),
1209         DEF_CMD("wol_ucast",    IFCAP_WOL_UCAST,        setifcap),
1210         DEF_CMD("-wol_ucast",   -IFCAP_WOL_UCAST,       setifcap),
1211         DEF_CMD("wol_mcast",    IFCAP_WOL_MCAST,        setifcap),
1212         DEF_CMD("-wol_mcast",   -IFCAP_WOL_MCAST,       setifcap),
1213         DEF_CMD("wol_magic",    IFCAP_WOL_MAGIC,        setifcap),
1214         DEF_CMD("-wol_magic",   -IFCAP_WOL_MAGIC,       setifcap),
1215         DEF_CMD("normal",       -IFF_LINK0,     setifflags),
1216         DEF_CMD("compress",     IFF_LINK0,      setifflags),
1217         DEF_CMD("noicmp",       IFF_LINK1,      setifflags),
1218         DEF_CMD_ARG("mtu",                      setifmtu),
1219         DEF_CMD_ARG("name",                     setifname),
1220 };
1221
1222 static __constructor void
1223 ifconfig_ctor(void)
1224 {
1225 #define N(a)    (sizeof(a) / sizeof(a[0]))
1226         size_t i;
1227
1228         for (i = 0; i < N(basic_cmds);  i++)
1229                 cmd_register(&basic_cmds[i]);
1230 #undef N
1231 }