]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/ifconfig.c
This commit was generated by cvs2svn to compensate for changes in r167817,
[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/sysctl.h>
48 #include <sys/time.h>
49 #include <sys/module.h>
50 #include <sys/linker.h>
51
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58
59 /* IP */
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <arpa/inet.h>
63 #include <netdb.h>
64
65 #include <ifaddrs.h>
66 #include <ctype.h>
67 #include <err.h>
68 #include <errno.h>
69 #include <fcntl.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 int     setaddr;
86 int     setipdst;
87 int     setmask;
88 int     doalias;
89 int     clearaddr;
90 int     newaddr = 1;
91 int     verbose;
92
93 int     supmedia = 0;
94 int     printkeys = 0;          /* Print keying material for interfaces. */
95
96 static  int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
97 static  void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
98                 struct ifaddrs *ifa);
99 static  void tunnel_status(int s);
100 static  void usage(void);
101
102 static struct afswtch *af_getbyname(const char *name);
103 static struct afswtch *af_getbyfamily(int af);
104 static void af_other_status(int);
105
106 static struct option *opts = NULL;
107
108 void
109 opt_register(struct option *p)
110 {
111         p->next = opts;
112         opts = p;
113 }
114
115 static void
116 usage(void)
117 {
118         char options[1024];
119         struct option *p;
120
121         /* XXX not right but close enough for now */
122         options[0] = '\0';
123         for (p = opts; p != NULL; p = p->next) {
124                 strlcat(options, p->opt_usage, sizeof(options));
125                 strlcat(options, " ", sizeof(options));
126         }
127
128         fprintf(stderr,
129         "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
130         "                [parameters]\n"
131         "       ifconfig interface create\n"
132         "       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
133         "       ifconfig -l [-d] [-u] [address_family]\n"
134         "       ifconfig %s[-d] [-m] [-u] [-v]\n",
135                 options, options, options);
136         exit(1);
137 }
138
139 int
140 main(int argc, char *argv[])
141 {
142         int c, all, namesonly, downonly, uponly;
143         const struct afswtch *afp = NULL;
144         int ifindex;
145         struct ifaddrs *ifap, *ifa;
146         struct ifreq paifr;
147         const struct sockaddr_dl *sdl;
148         char options[1024], *cp;
149         const char *ifname;
150         struct option *p;
151         size_t iflen;
152
153         all = downonly = uponly = namesonly = verbose = 0;
154
155         /* Parse leading line options */
156         strlcpy(options, "adklmuv", sizeof(options));
157         for (p = opts; p != NULL; p = p->next)
158                 strlcat(options, p->opt, sizeof(options));
159         while ((c = getopt(argc, argv, options)) != -1) {
160                 switch (c) {
161                 case 'a':       /* scan all interfaces */
162                         all++;
163                         break;
164                 case 'd':       /* restrict scan to "down" interfaces */
165                         downonly++;
166                         break;
167                 case 'k':
168                         printkeys++;
169                         break;
170                 case 'l':       /* scan interface names only */
171                         namesonly++;
172                         break;
173                 case 'm':       /* show media choices in status */
174                         supmedia = 1;
175                         break;
176                 case 'u':       /* restrict scan to "up" interfaces */
177                         uponly++;
178                         break;
179                 case 'v':
180                         verbose++;
181                         break;
182                 default:
183                         for (p = opts; p != NULL; p = p->next)
184                                 if (p->opt[0] == c) {
185                                         p->cb(optarg);
186                                         break;
187                                 }
188                         if (p == NULL)
189                                 usage();
190                         break;
191                 }
192         }
193         argc -= optind;
194         argv += optind;
195
196         /* -l cannot be used with -a or -m */
197         if (namesonly && (all || supmedia))
198                 usage();
199
200         /* nonsense.. */
201         if (uponly && downonly)
202                 usage();
203
204         /* no arguments is equivalent to '-a' */
205         if (!namesonly && argc < 1)
206                 all = 1;
207
208         /* -a and -l allow an address family arg to limit the output */
209         if (all || namesonly) {
210                 if (argc > 1)
211                         usage();
212
213                 ifname = NULL;
214                 ifindex = 0;
215                 if (argc == 1) {
216                         afp = af_getbyname(*argv);
217                         if (afp == NULL)
218                                 usage();
219                         if (afp->af_name != NULL)
220                                 argc--, argv++;
221                         /* leave with afp non-zero */
222                 }
223         } else {
224                 /* not listing, need an argument */
225                 if (argc < 1)
226                         usage();
227
228                 ifname = *argv;
229                 argc--, argv++;
230
231                 ifindex = if_nametoindex(ifname);
232                 if (ifindex == 0) {
233                         /*
234                          * NOTE:  We must special-case the `create' command
235                          * right here as we would otherwise fail when trying
236                          * to find the interface.
237                          */
238                         if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
239                             strcmp(argv[0], "plumb") == 0)) {
240                                 iflen = strlcpy(name, ifname, sizeof(name));
241                                 if (iflen >= sizeof(name))
242                                         errx(1, "%s: cloning name too long",
243                                             ifname);
244                                 ifmaybeload(ifname);
245                                 ifconfig(argc, argv, NULL);
246                                 exit(0);
247                         }
248                         errx(1, "interface %s does not exist", ifname);
249                 }
250         }
251
252         /* Check for address family */
253         if (argc > 0) {
254                 afp = af_getbyname(*argv);
255                 if (afp != NULL)
256                         argc--, argv++;
257         }
258
259         if (getifaddrs(&ifap) != 0)
260                 err(EXIT_FAILURE, "getifaddrs");
261         cp = NULL;
262         ifindex = 0;
263         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
264                 memset(&paifr, 0, sizeof(paifr));
265                 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
266                 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
267                         memcpy(&paifr.ifr_addr, ifa->ifa_addr,
268                             ifa->ifa_addr->sa_len);
269                 }
270
271                 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
272                         continue;
273                 if (ifa->ifa_addr->sa_family == AF_LINK)
274                         sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
275                 else
276                         sdl = NULL;
277                 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
278                         continue;
279                 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
280                 if (iflen >= sizeof(name)) {
281                         warnx("%s: interface name too long, skipping",
282                             ifa->ifa_name);
283                         continue;
284                 }
285                 cp = ifa->ifa_name;
286
287                 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
288                         continue;
289                 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
290                         continue;
291                 ifindex++;
292                 /*
293                  * Are we just listing the interfaces?
294                  */
295                 if (namesonly) {
296                         if (ifindex > 1)
297                                 printf(" ");
298                         fputs(name, stdout);
299                         continue;
300                 }
301
302                 if (argc > 0)
303                         ifconfig(argc, argv, afp);
304                 else
305                         status(afp, sdl, ifa);
306         }
307         if (namesonly)
308                 printf("\n");
309         freeifaddrs(ifap);
310
311         exit(0);
312 }
313
314 static struct afswtch *afs = NULL;
315
316 void
317 af_register(struct afswtch *p)
318 {
319         p->af_next = afs;
320         afs = p;
321 }
322
323 static struct afswtch *
324 af_getbyname(const char *name)
325 {
326         struct afswtch *afp;
327
328         for (afp = afs; afp !=  NULL; afp = afp->af_next)
329                 if (strcmp(afp->af_name, name) == 0)
330                         return afp;
331         return NULL;
332 }
333
334 static struct afswtch *
335 af_getbyfamily(int af)
336 {
337         struct afswtch *afp;
338
339         for (afp = afs; afp != NULL; afp = afp->af_next)
340                 if (afp->af_af == af)
341                         return afp;
342         return NULL;
343 }
344
345 static void
346 af_other_status(int s)
347 {
348         struct afswtch *afp;
349         uint8_t afmask[howmany(AF_MAX, NBBY)];
350
351         memset(afmask, 0, sizeof(afmask));
352         for (afp = afs; afp != NULL; afp = afp->af_next) {
353                 if (afp->af_other_status == NULL)
354                         continue;
355                 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
356                         continue;
357                 afp->af_other_status(s);
358                 setbit(afmask, afp->af_af);
359         }
360 }
361
362 static void
363 af_all_tunnel_status(int s)
364 {
365         struct afswtch *afp;
366         uint8_t afmask[howmany(AF_MAX, NBBY)];
367
368         memset(afmask, 0, sizeof(afmask));
369         for (afp = afs; afp != NULL; afp = afp->af_next) {
370                 if (afp->af_status_tunnel == NULL)
371                         continue;
372                 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
373                         continue;
374                 afp->af_status_tunnel(s);
375                 setbit(afmask, afp->af_af);
376         }
377 }
378
379 static struct cmd *cmds = NULL;
380
381 void
382 cmd_register(struct cmd *p)
383 {
384         p->c_next = cmds;
385         cmds = p;
386 }
387
388 static const struct cmd *
389 cmd_lookup(const char *name)
390 {
391 #define N(a)    (sizeof(a)/sizeof(a[0]))
392         const struct cmd *p;
393
394         for (p = cmds; p != NULL; p = p->c_next)
395                 if (strcmp(name, p->c_name) == 0)
396                         return p;
397         return NULL;
398 #undef N
399 }
400
401 struct callback {
402         callback_func *cb_func;
403         void    *cb_arg;
404         struct callback *cb_next;
405 };
406 static struct callback *callbacks = NULL;
407
408 void
409 callback_register(callback_func *func, void *arg)
410 {
411         struct callback *cb;
412
413         cb = malloc(sizeof(struct callback));
414         if (cb == NULL)
415                 errx(1, "unable to allocate memory for callback");
416         cb->cb_func = func;
417         cb->cb_arg = arg;
418         cb->cb_next = callbacks;
419         callbacks = cb;
420 }
421
422 /* specially-handled commands */
423 static void setifaddr(const char *, int, int, const struct afswtch *);
424 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
425
426 static void setifdstaddr(const char *, int, int, const struct afswtch *);
427 static const struct cmd setifdstaddr_cmd =
428         DEF_CMD("ifdstaddr", 0, setifdstaddr);
429
430 static int
431 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
432 {
433         struct callback *cb;
434         int s;
435
436         if (afp == NULL)
437                 afp = af_getbyname("inet");
438         ifr.ifr_addr.sa_family =
439                 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
440                 AF_INET : afp->af_af;
441         strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
442
443         if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
444                 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
445
446         while (argc > 0) {
447                 const struct cmd *p;
448
449                 p = cmd_lookup(*argv);
450                 if (p == NULL) {
451                         /*
452                          * Not a recognized command, choose between setting
453                          * the interface address and the dst address.
454                          */
455                         p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
456                 }
457                 if (p->c_u.c_func || p->c_u.c_func2) {
458                         if (p->c_parameter == NEXTARG) {
459                                 if (argv[1] == NULL)
460                                         errx(1, "'%s' requires argument",
461                                             p->c_name);
462                                 p->c_u.c_func(argv[1], 0, s, afp);
463                                 argc--, argv++;
464                         } else if (p->c_parameter == OPTARG) {
465                                 p->c_u.c_func(argv[1], 0, s, afp);
466                                 if (argv[1] != NULL)
467                                         argc--, argv++;
468                         } else if (p->c_parameter == NEXTARG2) {
469                                 if (argc < 3)
470                                         errx(1, "'%s' requires 2 arguments",
471                                             p->c_name);
472                                 p->c_u.c_func2(argv[1], argv[2], s, afp);
473                                 argc -= 2, argv += 2;
474                         } else
475                                 p->c_u.c_func(*argv, p->c_parameter, s, afp);
476                 }
477                 argc--, argv++;
478         }
479
480         /*
481          * Do any post argument processing required by the address family.
482          */
483         if (afp->af_postproc != NULL)
484                 afp->af_postproc(s, afp);
485         /*
486          * Do deferred callbacks registered while processing
487          * command-line arguments.
488          */
489         for (cb = callbacks; cb != NULL; cb = cb->cb_next)
490                 cb->cb_func(s, cb->cb_arg);
491         /*
492          * Do deferred operations.
493          */
494         if (clearaddr) {
495                 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
496                         warnx("interface %s cannot change %s addresses!",
497                               name, afp->af_name);
498                         clearaddr = 0;
499                 }
500         }
501         if (clearaddr) {
502                 int ret;
503                 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
504                 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
505                 if (ret < 0) {
506                         if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
507                                 /* means no previous address for interface */
508                         } else
509                                 Perror("ioctl (SIOCDIFADDR)");
510                 }
511         }
512         if (newaddr) {
513                 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
514                         warnx("interface %s cannot change %s addresses!",
515                               name, afp->af_name);
516                         newaddr = 0;
517                 }
518         }
519         if (newaddr && (setaddr || setmask)) {
520                 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
521                 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
522                         Perror("ioctl (SIOCAIFADDR)");
523         }
524
525         close(s);
526         return(0);
527 }
528
529 /*ARGSUSED*/
530 static void
531 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
532 {
533         if (afp->af_getaddr == NULL)
534                 return;
535         /*
536          * Delay the ioctl to set the interface addr until flags are all set.
537          * The address interpretation may depend on the flags,
538          * and the flags may change when the address is set.
539          */
540         setaddr++;
541         if (doalias == 0 && afp->af_af != AF_LINK)
542                 clearaddr = 1;
543         afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
544 }
545
546 static void
547 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
548 {
549         struct addrinfo *srcres, *dstres;
550         int ecode;
551
552         if (afp->af_settunnel == NULL) {
553                 warn("address family %s does not support tunnel setup",
554                         afp->af_name);
555                 return;
556         }
557
558         if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
559                 errx(1, "error in parsing address string: %s",
560                     gai_strerror(ecode));
561
562         if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)  
563                 errx(1, "error in parsing address string: %s",
564                     gai_strerror(ecode));
565
566         if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
567                 errx(1,
568                     "source and destination address families do not match");
569
570         afp->af_settunnel(s, srcres, dstres);
571
572         freeaddrinfo(srcres);
573         freeaddrinfo(dstres);
574 }
575
576 /* ARGSUSED */
577 static void
578 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
579 {
580
581         if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
582                 err(1, "SIOCDIFPHYADDR");
583 }
584
585 static void
586 setifnetmask(const char *addr, int dummy __unused, int s,
587     const struct afswtch *afp)
588 {
589         if (afp->af_getaddr != NULL) {
590                 setmask++;
591                 afp->af_getaddr(addr, MASK);
592         }
593 }
594
595 static void
596 setifbroadaddr(const char *addr, int dummy __unused, int s,
597     const struct afswtch *afp)
598 {
599         if (afp->af_getaddr != NULL)
600                 afp->af_getaddr(addr, DSTADDR);
601 }
602
603 static void
604 setifipdst(const char *addr, int dummy __unused, int s,
605     const struct afswtch *afp)
606 {
607         const struct afswtch *inet;
608
609         inet = af_getbyname("inet");
610         if (inet == NULL)
611                 return;
612         inet->af_getaddr(addr, DSTADDR);
613         setipdst++;
614         clearaddr = 0;
615         newaddr = 0;
616 }
617
618 static void
619 notealias(const char *addr, int param, int s, const struct afswtch *afp)
620 {
621 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
622         if (setaddr && doalias == 0 && param < 0)
623                 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
624                         bcopy((caddr_t)rqtosa(af_addreq),
625                               (caddr_t)rqtosa(af_ridreq),
626                               rqtosa(af_addreq)->sa_len);
627         doalias = param;
628         if (param < 0) {
629                 clearaddr = 1;
630                 newaddr = 0;
631         } else
632                 clearaddr = 0;
633 #undef rqtosa
634 }
635
636 /*ARGSUSED*/
637 static void
638 setifdstaddr(const char *addr, int param __unused, int s, 
639     const struct afswtch *afp)
640 {
641         if (afp->af_getaddr != NULL)
642                 afp->af_getaddr(addr, DSTADDR);
643 }
644
645 /*
646  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
647  * of the ifreq structure, which may confuse other parts of ifconfig.
648  * Make a private copy so we can avoid that.
649  */
650 static void
651 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
652 {
653         struct ifreq            my_ifr;
654         int flags;
655
656         bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
657
658         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
659                 Perror("ioctl (SIOCGIFFLAGS)");
660                 exit(1);
661         }
662         strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
663         flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
664
665         if (value < 0) {
666                 value = -value;
667                 flags &= ~value;
668         } else
669                 flags |= value;
670         my_ifr.ifr_flags = flags & 0xffff;
671         my_ifr.ifr_flagshigh = flags >> 16;
672         if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
673                 Perror(vname);
674 }
675
676 void
677 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
678 {
679         int flags;
680
681         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
682                 Perror("ioctl (SIOCGIFCAP)");
683                 exit(1);
684         }
685         flags = ifr.ifr_curcap;
686         if (value < 0) {
687                 value = -value;
688                 flags &= ~value;
689         } else
690                 flags |= value;
691         flags &= ifr.ifr_reqcap;
692         ifr.ifr_reqcap = flags;
693         if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
694                 Perror(vname);
695 }
696
697 static void
698 setifmetric(const char *val, int dummy __unused, int s, 
699     const struct afswtch *afp)
700 {
701         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
702         ifr.ifr_metric = atoi(val);
703         if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
704                 warn("ioctl (set metric)");
705 }
706
707 static void
708 setifmtu(const char *val, int dummy __unused, int s, 
709     const struct afswtch *afp)
710 {
711         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
712         ifr.ifr_mtu = atoi(val);
713         if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
714                 warn("ioctl (set mtu)");
715 }
716
717 static void
718 setifname(const char *val, int dummy __unused, int s, 
719     const struct afswtch *afp)
720 {
721         char *newname;
722
723         newname = strdup(val);
724         if (newname == NULL) {
725                 warn("no memory to set ifname");
726                 return;
727         }
728         ifr.ifr_data = newname;
729         if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
730                 warn("ioctl (set name)");
731                 free(newname);
732                 return;
733         }
734         strlcpy(name, newname, sizeof(name));
735         free(newname);
736 }
737
738 #define IFFBITS \
739 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
740 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
741 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NEEDSGIANT"
742
743 #define IFCAPBITS \
744 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
745 "\10VLAN_HWCSUM\11TSO4\12TSO6"
746
747 /*
748  * Print the status of the interface.  If an address family was
749  * specified, show only it; otherwise, show them all.
750  */
751 static void
752 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
753         struct ifaddrs *ifa)
754 {
755         struct ifaddrs *ift;
756         int allfamilies, s;
757         struct ifstat ifs;
758
759         if (afp == NULL) {
760                 allfamilies = 1;
761                 afp = af_getbyname("inet");
762         } else
763                 allfamilies = 0;
764
765         ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
766         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
767
768         s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
769         if (s < 0)
770                 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
771
772         printf("%s: ", name);
773         printb("flags", ifa->ifa_flags, IFFBITS);
774         if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
775                 printf(" metric %d", ifr.ifr_metric);
776         if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
777                 printf(" mtu %d", ifr.ifr_mtu);
778         putchar('\n');
779
780         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
781                 if (ifr.ifr_curcap != 0) {
782                         printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
783                         putchar('\n');
784                 }
785                 if (supmedia && ifr.ifr_reqcap != 0) {
786                         printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
787                         putchar('\n');
788                 }
789         }
790
791         tunnel_status(s);
792
793         for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
794                 if (ift->ifa_addr == NULL)
795                         continue;
796                 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
797                         continue;
798                 if (allfamilies) {
799                         const struct afswtch *p;
800                         p = af_getbyfamily(ift->ifa_addr->sa_family);
801                         if (p != NULL && p->af_status != NULL)
802                                 p->af_status(s, ift);
803                 } else if (afp->af_af == ift->ifa_addr->sa_family)
804                         afp->af_status(s, ift);
805         }
806 #if 0
807         if (allfamilies || afp->af_af == AF_LINK) {
808                 const struct afswtch *lafp;
809
810                 /*
811                  * Hack; the link level address is received separately
812                  * from the routing information so any address is not
813                  * handled above.  Cobble together an entry and invoke
814                  * the status method specially.
815                  */
816                 lafp = af_getbyname("lladdr");
817                 if (lafp != NULL) {
818                         info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
819                         lafp->af_status(s, &info);
820                 }
821         }
822 #endif
823         if (allfamilies)
824                 af_other_status(s);
825         else if (afp->af_other_status != NULL)
826                 afp->af_other_status(s);
827
828         strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
829         if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
830                 printf("%s", ifs.ascii);
831
832         close(s);
833         return;
834 }
835
836 static void
837 tunnel_status(int s)
838 {
839         af_all_tunnel_status(s);
840 }
841
842 void
843 Perror(const char *cmd)
844 {
845         switch (errno) {
846
847         case ENXIO:
848                 errx(1, "%s: no such interface", cmd);
849                 break;
850
851         case EPERM:
852                 errx(1, "%s: permission denied", cmd);
853                 break;
854
855         default:
856                 err(1, "%s", cmd);
857         }
858 }
859
860 /*
861  * Print a value a la the %b format of the kernel's printf
862  */
863 void
864 printb(const char *s, unsigned v, const char *bits)
865 {
866         int i, any = 0;
867         char c;
868
869         if (bits && *bits == 8)
870                 printf("%s=%o", s, v);
871         else
872                 printf("%s=%x", s, v);
873         bits++;
874         if (bits) {
875                 putchar('<');
876                 while ((i = *bits++) != '\0') {
877                         if (v & (1 << (i-1))) {
878                                 if (any)
879                                         putchar(',');
880                                 any = 1;
881                                 for (; (c = *bits) > 32; bits++)
882                                         putchar(c);
883                         } else
884                                 for (; *bits > 32; bits++)
885                                         ;
886                 }
887                 putchar('>');
888         }
889 }
890
891 void
892 ifmaybeload(const char *name)
893 {
894         struct module_stat mstat;
895         int fileid, modid;
896         char ifkind[35], *dp;
897         const char *cp;
898
899         /* turn interface and unit into module name */
900         strcpy(ifkind, "if_");
901         for (cp = name, dp = ifkind + 3;
902             (*cp != 0) && !isdigit(*cp); cp++, dp++)
903                 *dp = *cp;
904         *dp = 0;
905
906         /* scan files in kernel */
907         mstat.version = sizeof(struct module_stat);
908         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
909                 /* scan modules in file */
910                 for (modid = kldfirstmod(fileid); modid > 0;
911                      modid = modfnext(modid)) {
912                         if (modstat(modid, &mstat) < 0)
913                                 continue;
914                         /* strip bus name if present */
915                         if ((cp = strchr(mstat.name, '/')) != NULL) {
916                                 cp++;
917                         } else {
918                                 cp = mstat.name;
919                         }
920                         /* already loaded? */
921                         if (strncmp(name, cp, strlen(cp)) == 0 ||
922                             strncmp(ifkind, cp, strlen(cp)) == 0)
923                                 return;
924                 }
925         }
926
927         /* not present, we should try to load it */
928         kldload(ifkind);
929 }
930
931 static struct cmd basic_cmds[] = {
932         DEF_CMD("up",           IFF_UP,         setifflags),
933         DEF_CMD("down",         -IFF_UP,        setifflags),
934         DEF_CMD("arp",          -IFF_NOARP,     setifflags),
935         DEF_CMD("-arp",         IFF_NOARP,      setifflags),
936         DEF_CMD("debug",        IFF_DEBUG,      setifflags),
937         DEF_CMD("-debug",       -IFF_DEBUG,     setifflags),
938         DEF_CMD("promisc",      IFF_PPROMISC,   setifflags),
939         DEF_CMD("-promisc",     -IFF_PPROMISC,  setifflags),
940         DEF_CMD("add",          IFF_UP,         notealias),
941         DEF_CMD("alias",        IFF_UP,         notealias),
942         DEF_CMD("-alias",       -IFF_UP,        notealias),
943         DEF_CMD("delete",       -IFF_UP,        notealias),
944         DEF_CMD("remove",       -IFF_UP,        notealias),
945 #ifdef notdef
946 #define EN_SWABIPS      0x1000
947         DEF_CMD("swabips",      EN_SWABIPS,     setifflags),
948         DEF_CMD("-swabips",     -EN_SWABIPS,    setifflags),
949 #endif
950         DEF_CMD_ARG("netmask",                  setifnetmask),
951         DEF_CMD_ARG("metric",                   setifmetric),
952         DEF_CMD_ARG("broadcast",                setifbroadaddr),
953         DEF_CMD_ARG("ipdst",                    setifipdst),
954         DEF_CMD_ARG2("tunnel",                  settunnel),
955         DEF_CMD("-tunnel", 0,                   deletetunnel),
956         DEF_CMD("deletetunnel", 0,              deletetunnel),
957         DEF_CMD("link0",        IFF_LINK0,      setifflags),
958         DEF_CMD("-link0",       -IFF_LINK0,     setifflags),
959         DEF_CMD("link1",        IFF_LINK1,      setifflags),
960         DEF_CMD("-link1",       -IFF_LINK1,     setifflags),
961         DEF_CMD("link2",        IFF_LINK2,      setifflags),
962         DEF_CMD("-link2",       -IFF_LINK2,     setifflags),
963         DEF_CMD("monitor",      IFF_MONITOR,    setifflags),
964         DEF_CMD("-monitor",     -IFF_MONITOR,   setifflags),
965         DEF_CMD("staticarp",    IFF_STATICARP,  setifflags),
966         DEF_CMD("-staticarp",   -IFF_STATICARP, setifflags),
967         DEF_CMD("rxcsum",       IFCAP_RXCSUM,   setifcap),
968         DEF_CMD("-rxcsum",      -IFCAP_RXCSUM,  setifcap),
969         DEF_CMD("txcsum",       IFCAP_TXCSUM,   setifcap),
970         DEF_CMD("-txcsum",      -IFCAP_TXCSUM,  setifcap),
971         DEF_CMD("netcons",      IFCAP_NETCONS,  setifcap),
972         DEF_CMD("-netcons",     -IFCAP_NETCONS, setifcap),
973         DEF_CMD("polling",      IFCAP_POLLING,  setifcap),
974         DEF_CMD("-polling",     -IFCAP_POLLING, setifcap),
975         DEF_CMD("tso",          IFCAP_TSO,      setifcap),
976         DEF_CMD("-tso",         -IFCAP_TSO,     setifcap),
977         DEF_CMD("normal",       -IFF_LINK0,     setifflags),
978         DEF_CMD("compress",     IFF_LINK0,      setifflags),
979         DEF_CMD("noicmp",       IFF_LINK1,      setifflags),
980         DEF_CMD_ARG("mtu",                      setifmtu),
981         DEF_CMD_ARG("name",                     setifname),
982 };
983
984 static __constructor void
985 ifconfig_ctor(void)
986 {
987 #define N(a)    (sizeof(a) / sizeof(a[0]))
988         int i;
989
990         for (i = 0; i < N(basic_cmds);  i++)
991                 cmd_register(&basic_cmds[i]);
992 #undef N
993 }