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