]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sbin/ifconfig/ifconfig.c
Fix multiple small kernel memory disclosures. [EN-18:04.mem]
[FreeBSD/releng/10.3.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/module.h>
47 #include <sys/linker.h>
48 #include <sys/socket.h>
49 #include <sys/time.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         const struct cmd *p;
439
440         for (p = cmds; p != NULL; p = p->c_next)
441                 if (strcmp(name, p->c_name) == 0) {
442                         if (iscreate) {
443                                 if (p->c_iscloneop)
444                                         return p;
445                         } else {
446                                 if (!p->c_iscloneop)
447                                         return p;
448                         }
449                 }
450         return NULL;
451 }
452
453 struct callback {
454         callback_func *cb_func;
455         void    *cb_arg;
456         struct callback *cb_next;
457 };
458 static struct callback *callbacks = NULL;
459
460 void
461 callback_register(callback_func *func, void *arg)
462 {
463         struct callback *cb;
464
465         cb = malloc(sizeof(struct callback));
466         if (cb == NULL)
467                 errx(1, "unable to allocate memory for callback");
468         cb->cb_func = func;
469         cb->cb_arg = arg;
470         cb->cb_next = callbacks;
471         callbacks = cb;
472 }
473
474 /* specially-handled commands */
475 static void setifaddr(const char *, int, int, const struct afswtch *);
476 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
477
478 static void setifdstaddr(const char *, int, int, const struct afswtch *);
479 static const struct cmd setifdstaddr_cmd =
480         DEF_CMD("ifdstaddr", 0, setifdstaddr);
481
482 static int
483 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
484 {
485         const struct afswtch *afp, *nafp;
486         const struct cmd *p;
487         struct callback *cb;
488         int s;
489
490         strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
491         afp = NULL;
492         if (uafp != NULL)
493                 afp = uafp;
494         /*
495          * This is the historical "accident" allowing users to configure IPv4
496          * addresses without the "inet" keyword which while a nice feature has
497          * proven to complicate other things.  We cannot remove this but only
498          * make sure we will never have a similar implicit default for IPv6 or
499          * any other address familiy.  We need a fallback though for
500          * ifconfig IF up/down etc. to work without INET support as people
501          * never used ifconfig IF link up/down, etc. either.
502          */
503 #ifndef RESCUE
504 #ifdef INET
505         if (afp == NULL && feature_present("inet"))
506                 afp = af_getbyname("inet");
507 #endif
508 #endif
509         if (afp == NULL)
510                 afp = af_getbyname("link");
511         if (afp == NULL) {
512                 warnx("Please specify an address_family.");
513                 usage();
514         }
515 top:
516         ifr.ifr_addr.sa_family =
517                 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
518                 AF_LOCAL : afp->af_af;
519
520         if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
521             (uafp != NULL || errno != EAFNOSUPPORT ||
522              (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
523                 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
524
525         while (argc > 0) {
526                 p = cmd_lookup(*argv, iscreate);
527                 if (iscreate && p == NULL) {
528                         /*
529                          * Push the clone create callback so the new
530                          * device is created and can be used for any
531                          * remaining arguments.
532                          */
533                         cb = callbacks;
534                         if (cb == NULL)
535                                 errx(1, "internal error, no callback");
536                         callbacks = cb->cb_next;
537                         cb->cb_func(s, cb->cb_arg);
538                         iscreate = 0;
539                         /*
540                          * Handle any address family spec that
541                          * immediately follows and potentially
542                          * recreate the socket.
543                          */
544                         nafp = af_getbyname(*argv);
545                         if (nafp != NULL) {
546                                 argc--, argv++;
547                                 if (nafp != afp) {
548                                         close(s);
549                                         afp = nafp;
550                                         goto top;
551                                 }
552                         }
553                         /*
554                          * Look for a normal parameter.
555                          */
556                         continue;
557                 }
558                 if (p == NULL) {
559                         /*
560                          * Not a recognized command, choose between setting
561                          * the interface address and the dst address.
562                          */
563                         p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
564                 }
565                 if (p->c_u.c_func || p->c_u.c_func2) {
566                         if (p->c_parameter == NEXTARG) {
567                                 if (argv[1] == NULL)
568                                         errx(1, "'%s' requires argument",
569                                             p->c_name);
570                                 p->c_u.c_func(argv[1], 0, s, afp);
571                                 argc--, argv++;
572                         } else if (p->c_parameter == OPTARG) {
573                                 p->c_u.c_func(argv[1], 0, s, afp);
574                                 if (argv[1] != NULL)
575                                         argc--, argv++;
576                         } else if (p->c_parameter == NEXTARG2) {
577                                 if (argc < 3)
578                                         errx(1, "'%s' requires 2 arguments",
579                                             p->c_name);
580                                 p->c_u.c_func2(argv[1], argv[2], s, afp);
581                                 argc -= 2, argv += 2;
582                         } else
583                                 p->c_u.c_func(*argv, p->c_parameter, s, afp);
584                 }
585                 argc--, argv++;
586         }
587
588         /*
589          * Do any post argument processing required by the address family.
590          */
591         if (afp->af_postproc != NULL)
592                 afp->af_postproc(s, afp);
593         /*
594          * Do deferred callbacks registered while processing
595          * command-line arguments.
596          */
597         for (cb = callbacks; cb != NULL; cb = cb->cb_next)
598                 cb->cb_func(s, cb->cb_arg);
599         /*
600          * Do deferred operations.
601          */
602         if (clearaddr) {
603                 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
604                         warnx("interface %s cannot change %s addresses!",
605                               name, afp->af_name);
606                         clearaddr = 0;
607                 }
608         }
609         if (clearaddr) {
610                 int ret;
611                 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
612                 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
613                 if (ret < 0) {
614                         if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
615                                 /* means no previous address for interface */
616                         } else
617                                 Perror("ioctl (SIOCDIFADDR)");
618                 }
619         }
620         if (newaddr) {
621                 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
622                         warnx("interface %s cannot change %s addresses!",
623                               name, afp->af_name);
624                         newaddr = 0;
625                 }
626         }
627         if (newaddr && (setaddr || setmask)) {
628                 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
629                 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
630                         Perror("ioctl (SIOCAIFADDR)");
631         }
632
633         close(s);
634         return(0);
635 }
636
637 /*ARGSUSED*/
638 static void
639 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
640 {
641         if (afp->af_getaddr == NULL)
642                 return;
643         /*
644          * Delay the ioctl to set the interface addr until flags are all set.
645          * The address interpretation may depend on the flags,
646          * and the flags may change when the address is set.
647          */
648         setaddr++;
649         if (doalias == 0 && afp->af_af != AF_LINK)
650                 clearaddr = 1;
651         afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
652 }
653
654 static void
655 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
656 {
657         struct addrinfo *srcres, *dstres;
658         int ecode;
659
660         if (afp->af_settunnel == NULL) {
661                 warn("address family %s does not support tunnel setup",
662                         afp->af_name);
663                 return;
664         }
665
666         if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
667                 errx(1, "error in parsing address string: %s",
668                     gai_strerror(ecode));
669
670         if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)  
671                 errx(1, "error in parsing address string: %s",
672                     gai_strerror(ecode));
673
674         if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
675                 errx(1,
676                     "source and destination address families do not match");
677
678         afp->af_settunnel(s, srcres, dstres);
679
680         freeaddrinfo(srcres);
681         freeaddrinfo(dstres);
682 }
683
684 /* ARGSUSED */
685 static void
686 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
687 {
688
689         if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
690                 err(1, "SIOCDIFPHYADDR");
691 }
692
693 #ifdef JAIL
694 static void
695 setifvnet(const char *jname, int dummy __unused, int s,
696     const struct afswtch *afp)
697 {
698         struct ifreq my_ifr;
699
700         memcpy(&my_ifr, &ifr, sizeof(my_ifr));
701         my_ifr.ifr_jid = jail_getid(jname);
702         if (my_ifr.ifr_jid < 0)
703                 errx(1, "%s", jail_errmsg);
704         if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
705                 err(1, "SIOCSIFVNET");
706 }
707
708 static void
709 setifrvnet(const char *jname, int dummy __unused, int s,
710     const struct afswtch *afp)
711 {
712         struct ifreq my_ifr;
713
714         memcpy(&my_ifr, &ifr, sizeof(my_ifr));
715         my_ifr.ifr_jid = jail_getid(jname);
716         if (my_ifr.ifr_jid < 0)
717                 errx(1, "%s", jail_errmsg);
718         if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
719                 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
720 }
721 #endif
722
723 static void
724 setifnetmask(const char *addr, int dummy __unused, int s,
725     const struct afswtch *afp)
726 {
727         if (afp->af_getaddr != NULL) {
728                 setmask++;
729                 afp->af_getaddr(addr, MASK);
730         }
731 }
732
733 static void
734 setifbroadaddr(const char *addr, int dummy __unused, int s,
735     const struct afswtch *afp)
736 {
737         if (afp->af_getaddr != NULL)
738                 afp->af_getaddr(addr, DSTADDR);
739 }
740
741 static void
742 setifipdst(const char *addr, int dummy __unused, int s,
743     const struct afswtch *afp)
744 {
745         const struct afswtch *inet;
746
747         inet = af_getbyname("inet");
748         if (inet == NULL)
749                 return;
750         inet->af_getaddr(addr, DSTADDR);
751         clearaddr = 0;
752         newaddr = 0;
753 }
754
755 static void
756 notealias(const char *addr, int param, int s, const struct afswtch *afp)
757 {
758 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
759         if (setaddr && doalias == 0 && param < 0)
760                 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
761                         bcopy((caddr_t)rqtosa(af_addreq),
762                               (caddr_t)rqtosa(af_ridreq),
763                               rqtosa(af_addreq)->sa_len);
764         doalias = param;
765         if (param < 0) {
766                 clearaddr = 1;
767                 newaddr = 0;
768         } else
769                 clearaddr = 0;
770 #undef rqtosa
771 }
772
773 /*ARGSUSED*/
774 static void
775 setifdstaddr(const char *addr, int param __unused, int s, 
776     const struct afswtch *afp)
777 {
778         if (afp->af_getaddr != NULL)
779                 afp->af_getaddr(addr, DSTADDR);
780 }
781
782 /*
783  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
784  * of the ifreq structure, which may confuse other parts of ifconfig.
785  * Make a private copy so we can avoid that.
786  */
787 static void
788 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
789 {
790         struct ifreq            my_ifr;
791         int flags;
792
793         memset(&my_ifr, 0, sizeof(my_ifr));
794         (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
795
796         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
797                 Perror("ioctl (SIOCGIFFLAGS)");
798                 exit(1);
799         }
800         flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
801
802         if (value < 0) {
803                 value = -value;
804                 flags &= ~value;
805         } else
806                 flags |= value;
807         my_ifr.ifr_flags = flags & 0xffff;
808         my_ifr.ifr_flagshigh = flags >> 16;
809         if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
810                 Perror(vname);
811 }
812
813 void
814 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
815 {
816         int flags;
817
818         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
819                 Perror("ioctl (SIOCGIFCAP)");
820                 exit(1);
821         }
822         flags = ifr.ifr_curcap;
823         if (value < 0) {
824                 value = -value;
825                 flags &= ~value;
826         } else
827                 flags |= value;
828         flags &= ifr.ifr_reqcap;
829         ifr.ifr_reqcap = flags;
830         if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
831                 Perror(vname);
832 }
833
834 static void
835 setifmetric(const char *val, int dummy __unused, int s, 
836     const struct afswtch *afp)
837 {
838         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
839         ifr.ifr_metric = atoi(val);
840         if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
841                 err(1, "ioctl SIOCSIFMETRIC (set metric)");
842 }
843
844 static void
845 setifmtu(const char *val, int dummy __unused, int s, 
846     const struct afswtch *afp)
847 {
848         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
849         ifr.ifr_mtu = atoi(val);
850         if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
851                 err(1, "ioctl SIOCSIFMTU (set mtu)");
852 }
853
854 static void
855 setifname(const char *val, int dummy __unused, int s, 
856     const struct afswtch *afp)
857 {
858         char *newname;
859
860         newname = strdup(val);
861         if (newname == NULL)
862                 err(1, "no memory to set ifname");
863         ifr.ifr_data = newname;
864         if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
865                 free(newname);
866                 err(1, "ioctl SIOCSIFNAME (set name)");
867         }
868         strlcpy(name, newname, sizeof(name));
869         free(newname);
870 }
871
872 /* ARGSUSED */
873 static void
874 setifdescr(const char *val, int dummy __unused, int s, 
875     const struct afswtch *afp)
876 {
877         char *newdescr;
878
879         ifr.ifr_buffer.length = strlen(val) + 1;
880         if (ifr.ifr_buffer.length == 1) {
881                 ifr.ifr_buffer.buffer = newdescr = NULL;
882                 ifr.ifr_buffer.length = 0;
883         } else {
884                 newdescr = strdup(val);
885                 ifr.ifr_buffer.buffer = newdescr;
886                 if (newdescr == NULL) {
887                         warn("no memory to set ifdescr");
888                         return;
889                 }
890         }
891
892         if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
893                 err(1, "ioctl SIOCSIFDESCR (set descr)");
894
895         free(newdescr);
896 }
897
898 /* ARGSUSED */
899 static void
900 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
901 {
902
903         setifdescr("", 0, s, 0);
904 }
905
906 #define IFFBITS \
907 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
908 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
909 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
910
911 #define IFCAPBITS \
912 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
913 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
914 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
915 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
916
917 /*
918  * Print the status of the interface.  If an address family was
919  * specified, show only it; otherwise, show them all.
920  */
921 static void
922 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
923         struct ifaddrs *ifa)
924 {
925         struct ifaddrs *ift;
926         int allfamilies, s;
927         struct ifstat ifs;
928
929         if (afp == NULL) {
930                 allfamilies = 1;
931                 ifr.ifr_addr.sa_family = AF_LOCAL;
932         } else {
933                 allfamilies = 0;
934                 ifr.ifr_addr.sa_family =
935                     afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
936         }
937         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
938
939         s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
940         if (s < 0)
941                 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
942
943         printf("%s: ", name);
944         printb("flags", ifa->ifa_flags, IFFBITS);
945         if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
946                 printf(" metric %d", ifr.ifr_metric);
947         if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
948                 printf(" mtu %d", ifr.ifr_mtu);
949         putchar('\n');
950
951         for (;;) {
952                 if ((descr = reallocf(descr, descrlen)) != NULL) {
953                         ifr.ifr_buffer.buffer = descr;
954                         ifr.ifr_buffer.length = descrlen;
955                         if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
956                                 if (ifr.ifr_buffer.buffer == descr) {
957                                         if (strlen(descr) > 0)
958                                                 printf("\tdescription: %s\n",
959                                                     descr);
960                                 } else if (ifr.ifr_buffer.length > descrlen) {
961                                         descrlen = ifr.ifr_buffer.length;
962                                         continue;
963                                 }
964                         }
965                 } else
966                         warn("unable to allocate memory for interface"
967                             "description");
968                 break;
969         }
970
971         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
972                 if (ifr.ifr_curcap != 0) {
973                         printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
974                         putchar('\n');
975                 }
976                 if (supmedia && ifr.ifr_reqcap != 0) {
977                         printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
978                         putchar('\n');
979                 }
980         }
981
982         tunnel_status(s);
983
984         for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
985                 if (ift->ifa_addr == NULL)
986                         continue;
987                 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
988                         continue;
989                 if (allfamilies) {
990                         const struct afswtch *p;
991                         p = af_getbyfamily(ift->ifa_addr->sa_family);
992                         if (p != NULL && p->af_status != NULL)
993                                 p->af_status(s, ift);
994                 } else if (afp->af_af == ift->ifa_addr->sa_family)
995                         afp->af_status(s, ift);
996         }
997 #if 0
998         if (allfamilies || afp->af_af == AF_LINK) {
999                 const struct afswtch *lafp;
1000
1001                 /*
1002                  * Hack; the link level address is received separately
1003                  * from the routing information so any address is not
1004                  * handled above.  Cobble together an entry and invoke
1005                  * the status method specially.
1006                  */
1007                 lafp = af_getbyname("lladdr");
1008                 if (lafp != NULL) {
1009                         info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1010                         lafp->af_status(s, &info);
1011                 }
1012         }
1013 #endif
1014         if (allfamilies)
1015                 af_other_status(s);
1016         else if (afp->af_other_status != NULL)
1017                 afp->af_other_status(s);
1018
1019         strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1020         if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
1021                 printf("%s", ifs.ascii);
1022
1023         if (verbose > 0)
1024                 sfp_status(s, &ifr, verbose);
1025
1026         close(s);
1027         return;
1028 }
1029
1030 static void
1031 tunnel_status(int s)
1032 {
1033         af_all_tunnel_status(s);
1034 }
1035
1036 void
1037 Perror(const char *cmd)
1038 {
1039         switch (errno) {
1040
1041         case ENXIO:
1042                 errx(1, "%s: no such interface", cmd);
1043                 break;
1044
1045         case EPERM:
1046                 errx(1, "%s: permission denied", cmd);
1047                 break;
1048
1049         default:
1050                 err(1, "%s", cmd);
1051         }
1052 }
1053
1054 /*
1055  * Print a value a la the %b format of the kernel's printf
1056  */
1057 void
1058 printb(const char *s, unsigned v, const char *bits)
1059 {
1060         int i, any = 0;
1061         char c;
1062
1063         if (bits && *bits == 8)
1064                 printf("%s=%o", s, v);
1065         else
1066                 printf("%s=%x", s, v);
1067         bits++;
1068         if (bits) {
1069                 putchar('<');
1070                 while ((i = *bits++) != '\0') {
1071                         if (v & (1 << (i-1))) {
1072                                 if (any)
1073                                         putchar(',');
1074                                 any = 1;
1075                                 for (; (c = *bits) > 32; bits++)
1076                                         putchar(c);
1077                         } else
1078                                 for (; *bits > 32; bits++)
1079                                         ;
1080                 }
1081                 putchar('>');
1082         }
1083 }
1084
1085 void
1086 print_vhid(const struct ifaddrs *ifa, const char *s)
1087 {
1088         struct if_data *ifd;
1089
1090         if (ifa->ifa_data == NULL)
1091                 return;
1092
1093         ifd = ifa->ifa_data;
1094         if (ifd->ifi_vhid == 0)
1095                 return;
1096         
1097         printf("vhid %d ", ifd->ifi_vhid);
1098 }
1099
1100 void
1101 ifmaybeload(const char *name)
1102 {
1103 #define MOD_PREFIX_LEN          3       /* "if_" */
1104         struct module_stat mstat;
1105         int fileid, modid;
1106         char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1107         const char *cp;
1108
1109         /* loading suppressed by the user */
1110         if (noload)
1111                 return;
1112
1113         /* trim the interface number off the end */
1114         strlcpy(ifname, name, sizeof(ifname));
1115         for (dp = ifname; *dp != 0; dp++)
1116                 if (isdigit(*dp)) {
1117                         *dp = 0;
1118                         break;
1119                 }
1120
1121         /* turn interface and unit into module name */
1122         strlcpy(ifkind, "if_", sizeof(ifkind));
1123         strlcat(ifkind, ifname, sizeof(ifkind));
1124
1125         /* scan files in kernel */
1126         mstat.version = sizeof(struct module_stat);
1127         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1128                 /* scan modules in file */
1129                 for (modid = kldfirstmod(fileid); modid > 0;
1130                      modid = modfnext(modid)) {
1131                         if (modstat(modid, &mstat) < 0)
1132                                 continue;
1133                         /* strip bus name if present */
1134                         if ((cp = strchr(mstat.name, '/')) != NULL) {
1135                                 cp++;
1136                         } else {
1137                                 cp = mstat.name;
1138                         }
1139                         /* already loaded? */
1140                         if (strcmp(ifname, cp) == 0 ||
1141                             strcmp(ifkind, cp) == 0)
1142                                 return;
1143                 }
1144         }
1145
1146         /* not present, we should try to load it */
1147         kldload(ifkind);
1148 }
1149
1150 static struct cmd basic_cmds[] = {
1151         DEF_CMD("up",           IFF_UP,         setifflags),
1152         DEF_CMD("down",         -IFF_UP,        setifflags),
1153         DEF_CMD("arp",          -IFF_NOARP,     setifflags),
1154         DEF_CMD("-arp",         IFF_NOARP,      setifflags),
1155         DEF_CMD("debug",        IFF_DEBUG,      setifflags),
1156         DEF_CMD("-debug",       -IFF_DEBUG,     setifflags),
1157         DEF_CMD_ARG("description",              setifdescr),
1158         DEF_CMD_ARG("descr",                    setifdescr),
1159         DEF_CMD("-description", 0,              unsetifdescr),
1160         DEF_CMD("-descr",       0,              unsetifdescr),
1161         DEF_CMD("promisc",      IFF_PPROMISC,   setifflags),
1162         DEF_CMD("-promisc",     -IFF_PPROMISC,  setifflags),
1163         DEF_CMD("add",          IFF_UP,         notealias),
1164         DEF_CMD("alias",        IFF_UP,         notealias),
1165         DEF_CMD("-alias",       -IFF_UP,        notealias),
1166         DEF_CMD("delete",       -IFF_UP,        notealias),
1167         DEF_CMD("remove",       -IFF_UP,        notealias),
1168 #ifdef notdef
1169 #define EN_SWABIPS      0x1000
1170         DEF_CMD("swabips",      EN_SWABIPS,     setifflags),
1171         DEF_CMD("-swabips",     -EN_SWABIPS,    setifflags),
1172 #endif
1173         DEF_CMD_ARG("netmask",                  setifnetmask),
1174         DEF_CMD_ARG("metric",                   setifmetric),
1175         DEF_CMD_ARG("broadcast",                setifbroadaddr),
1176         DEF_CMD_ARG("ipdst",                    setifipdst),
1177         DEF_CMD_ARG2("tunnel",                  settunnel),
1178         DEF_CMD("-tunnel", 0,                   deletetunnel),
1179         DEF_CMD("deletetunnel", 0,              deletetunnel),
1180 #ifdef JAIL
1181         DEF_CMD_ARG("vnet",                     setifvnet),
1182         DEF_CMD_ARG("-vnet",                    setifrvnet),
1183 #endif
1184         DEF_CMD("link0",        IFF_LINK0,      setifflags),
1185         DEF_CMD("-link0",       -IFF_LINK0,     setifflags),
1186         DEF_CMD("link1",        IFF_LINK1,      setifflags),
1187         DEF_CMD("-link1",       -IFF_LINK1,     setifflags),
1188         DEF_CMD("link2",        IFF_LINK2,      setifflags),
1189         DEF_CMD("-link2",       -IFF_LINK2,     setifflags),
1190         DEF_CMD("monitor",      IFF_MONITOR,    setifflags),
1191         DEF_CMD("-monitor",     -IFF_MONITOR,   setifflags),
1192         DEF_CMD("staticarp",    IFF_STATICARP,  setifflags),
1193         DEF_CMD("-staticarp",   -IFF_STATICARP, setifflags),
1194         DEF_CMD("rxcsum6",      IFCAP_RXCSUM_IPV6,      setifcap),
1195         DEF_CMD("-rxcsum6",     -IFCAP_RXCSUM_IPV6,     setifcap),
1196         DEF_CMD("txcsum6",      IFCAP_TXCSUM_IPV6,      setifcap),
1197         DEF_CMD("-txcsum6",     -IFCAP_TXCSUM_IPV6,     setifcap),
1198         DEF_CMD("rxcsum",       IFCAP_RXCSUM,   setifcap),
1199         DEF_CMD("-rxcsum",      -IFCAP_RXCSUM,  setifcap),
1200         DEF_CMD("txcsum",       IFCAP_TXCSUM,   setifcap),
1201         DEF_CMD("-txcsum",      -IFCAP_TXCSUM,  setifcap),
1202         DEF_CMD("netcons",      IFCAP_NETCONS,  setifcap),
1203         DEF_CMD("-netcons",     -IFCAP_NETCONS, setifcap),
1204         DEF_CMD("polling",      IFCAP_POLLING,  setifcap),
1205         DEF_CMD("-polling",     -IFCAP_POLLING, setifcap),
1206         DEF_CMD("tso6",         IFCAP_TSO6,     setifcap),
1207         DEF_CMD("-tso6",        -IFCAP_TSO6,    setifcap),
1208         DEF_CMD("tso4",         IFCAP_TSO4,     setifcap),
1209         DEF_CMD("-tso4",        -IFCAP_TSO4,    setifcap),
1210         DEF_CMD("tso",          IFCAP_TSO,      setifcap),
1211         DEF_CMD("-tso",         -IFCAP_TSO,     setifcap),
1212         DEF_CMD("toe",          IFCAP_TOE,      setifcap),
1213         DEF_CMD("-toe",         -IFCAP_TOE,     setifcap),
1214         DEF_CMD("lro",          IFCAP_LRO,      setifcap),
1215         DEF_CMD("-lro",         -IFCAP_LRO,     setifcap),
1216         DEF_CMD("wol",          IFCAP_WOL,      setifcap),
1217         DEF_CMD("-wol",         -IFCAP_WOL,     setifcap),
1218         DEF_CMD("wol_ucast",    IFCAP_WOL_UCAST,        setifcap),
1219         DEF_CMD("-wol_ucast",   -IFCAP_WOL_UCAST,       setifcap),
1220         DEF_CMD("wol_mcast",    IFCAP_WOL_MCAST,        setifcap),
1221         DEF_CMD("-wol_mcast",   -IFCAP_WOL_MCAST,       setifcap),
1222         DEF_CMD("wol_magic",    IFCAP_WOL_MAGIC,        setifcap),
1223         DEF_CMD("-wol_magic",   -IFCAP_WOL_MAGIC,       setifcap),
1224         DEF_CMD("normal",       -IFF_LINK0,     setifflags),
1225         DEF_CMD("compress",     IFF_LINK0,      setifflags),
1226         DEF_CMD("noicmp",       IFF_LINK1,      setifflags),
1227         DEF_CMD_ARG("mtu",                      setifmtu),
1228         DEF_CMD_ARG("name",                     setifname),
1229 };
1230
1231 static __constructor void
1232 ifconfig_ctor(void)
1233 {
1234         size_t i;
1235
1236         for (i = 0; i < nitems(basic_cmds);  i++)
1237                 cmd_register(&basic_cmds[i]);
1238 }