source: rtems-libbsd/freebsd-userspace/commands/sbin/ifconfig/ifconfig.c @ ebbe3cc

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since ebbe3cc was ebbe3cc, checked in by Jennifer Averett <jennifer.averett@…>, on 09/07/12 at 18:16:22

Added the ifconfig command.

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