source: rtems-libbsd/freebsd/sbin/ifconfig/ifconfig.c

6-freebsd-12
Last change on this file was 46b3858, checked in by Sebastian Huber <sebastian.huber@…>, on 02/10/20 at 14:34:55

Update to FreeBSD stable/12 2020-02-10

Git mirror commit 0d1c391321b34b3025cf0e72f2231d836ff76da8.

  • Property mode set to 100644
File size: 40.2 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3#ifdef __rtems__
4#include "rtems-bsd-ifconfig-namespace.h"
5#endif /* __rtems__ */
6
7/*-
8 * SPDX-License-Identifier: BSD-3-Clause
9 *
10 * Copyright (c) 1983, 1993
11 *      The Regents of the University of California.  All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39static const char copyright[] =
40"@(#) Copyright (c) 1983, 1993\n\
41        The Regents of the University of California.  All rights reserved.\n";
42#endif /* not lint */
43
44#ifndef lint
45#if 0
46static char sccsid[] = "@(#)ifconfig.c  8.2 (Berkeley) 2/16/94";
47#endif
48static const char rcsid[] =
49  "$FreeBSD$";
50#endif /* not lint */
51
52#ifdef __rtems__
53#define __need_getopt_newlib
54#define option getopt_option
55#include <getopt.h>
56#undef option
57#include <machine/rtems-bsd-program.h>
58#include <machine/rtems-bsd-commands.h>
59#include <rtems/bsd/modules.h>
60#endif /* __rtems__ */
61#include <sys/param.h>
62#include <sys/ioctl.h>
63#include <sys/module.h>
64#include <sys/linker.h>
65#include <sys/queue.h>
66#include <sys/socket.h>
67#include <sys/time.h>
68
69#include <net/ethernet.h>
70#include <net/if.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
73#include <net/route.h>
74
75/* IP */
76#include <netinet/in.h>
77#include <netinet/in_var.h>
78#include <arpa/inet.h>
79#include <netdb.h>
80
81#include <ifaddrs.h>
82#include <ctype.h>
83#include <err.h>
84#include <errno.h>
85#include <fcntl.h>
86#ifdef JAIL
87#include <jail.h>
88#endif
89#include <stdbool.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <unistd.h>
94
95#include "ifconfig.h"
96#ifdef __rtems__
97#include "rtems-bsd-ifconfig-ifconfig-data.h"
98#endif /* __rtems__ */
99
100/*
101 * Since "struct ifreq" is composed of various union members, callers
102 * should pay special attention to interpret the value.
103 * (.e.g. little/big endian difference in the structure.)
104 */
105struct  ifreq ifr;
106
107char    name[IFNAMSIZ];
108char    *descr = NULL;
109size_t  descrlen = 64;
110int     setaddr;
111int     setmask;
112int     doalias;
113int     clearaddr;
114int     newaddr = 1;
115int     verbose;
116int     noload;
117int     printifname = 0;
118
119int     supmedia = 0;
120int     printkeys = 0;          /* Print keying material for interfaces. */
121int     exit_code = 0;
122
123/* Formatter Strings */
124char    *f_inet, *f_inet6, *f_ether, *f_addr;
125
126static  int ifconfig(int argc, char *const *argv, int iscreate,
127                const struct afswtch *afp);
128static  void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
129                struct ifaddrs *ifa);
130static  void tunnel_status(int s);
131static _Noreturn void usage(void);
132
133static int getifflags(const char *ifname, int us);
134
135static struct afswtch *af_getbyname(const char *name);
136static struct afswtch *af_getbyfamily(int af);
137static void af_other_status(int);
138
139void printifnamemaybe(void);
140
141static struct option *opts = NULL;
142
143struct ifa_order_elt {
144        int if_order;
145        int af_orders[255];
146        struct ifaddrs *ifa;
147        TAILQ_ENTRY(ifa_order_elt) link;
148};
149
150TAILQ_HEAD(ifa_queue, ifa_order_elt);
151
152#ifndef __rtems__
153static struct module_map_entry {
154        const char *ifname;
155        const char *kldname;
156} module_map[] = {
157        {
158                .ifname = "tun",
159                .kldname = "if_tuntap",
160        },
161        {
162                .ifname = "tap",
163                .kldname = "if_tuntap",
164        },
165        {
166                .ifname = "vmnet",
167                .kldname = "if_tuntap",
168        },
169        {
170                .ifname = "ipsec",
171                .kldname = "ipsec",
172        },
173        {
174                /*
175                 * This mapping exists because there is a conflicting enc module
176                 * in CAM.  ifconfig's guessing behavior will attempt to match
177                 * the ifname to a module as well as if_${ifname} and clash with
178                 * CAM enc.  This is an assertion of the correct module to load.
179                 */
180                .ifname = "enc",
181                .kldname = "if_enc",
182        },
183};
184#endif /* __rtems__ */
185
186
187void
188opt_register(struct option *p)
189{
190        p->next = opts;
191        opts = p;
192}
193
194static void
195usage(void)
196{
197        char options[1024];
198        struct option *p;
199
200        /* XXX not right but close enough for now */
201        options[0] = '\0';
202        for (p = opts; p != NULL; p = p->next) {
203                strlcat(options, p->opt_usage, sizeof(options));
204                strlcat(options, " ", sizeof(options));
205        }
206
207        fprintf(stderr,
208        "usage: ifconfig [-f type:format] %sinterface address_family\n"
209        "                [address [dest_address]] [parameters]\n"
210        "       ifconfig interface create\n"
211        "       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
212        "       ifconfig -l [-d] [-u] [address_family]\n"
213        "       ifconfig %s[-d] [-m] [-u] [-v]\n",
214                options, options, options);
215        exit(1);
216}
217
218#define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
219
220static int
221calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
222{
223        struct ifaddrs *prev;
224        struct ifa_order_elt *cur;
225        unsigned int ord, af, ifa_ord;
226
227        prev = NULL;
228        cur = NULL;
229        ord = 0;
230        ifa_ord = 0;
231
232        while (ifa != NULL) {
233                if (prev == NULL ||
234                    strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
235                        cur = calloc(1, sizeof(*cur));
236
237                        if (cur == NULL)
238                                return (-1);
239
240                        TAILQ_INSERT_TAIL(q, cur, link);
241                        cur->if_order = ifa_ord ++;
242                        cur->ifa = ifa;
243                        ord = 0;
244                }
245
246                if (ifa->ifa_addr) {
247                        af = ifa->ifa_addr->sa_family;
248
249                        if (af < ORDERS_SIZE(cur->af_orders) &&
250                            cur->af_orders[af] == 0)
251                                cur->af_orders[af] = ++ord;
252                }
253                prev = ifa;
254                ifa = ifa->ifa_next;
255        }
256
257        return (0);
258}
259
260static int
261cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
262{
263        struct ifa_order_elt *cur, *e1, *e2;
264        unsigned int af1, af2;
265        int ret;
266
267        e1 = e2 = NULL;
268
269        ret = strcmp(a->ifa_name, b->ifa_name);
270        if (ret != 0) {
271                TAILQ_FOREACH(cur, q, link) {
272                        if (e1 && e2)
273                                break;
274
275                        if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
276                                e1 = cur;
277                        else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
278                                e2 = cur;
279                }
280
281                if (!e1 || !e2)
282                        return (0);
283                else
284                        return (e1->if_order - e2->if_order);
285
286        } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
287                TAILQ_FOREACH(cur, q, link) {
288                        if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
289                                e1 = cur;
290                                break;
291                        }
292                }
293
294                if (!e1)
295                        return (0);
296
297                af1 = a->ifa_addr->sa_family;
298                af2 = b->ifa_addr->sa_family;
299
300                if (af1 < ORDERS_SIZE(e1->af_orders) &&
301                    af2 < ORDERS_SIZE(e1->af_orders))
302                        return (e1->af_orders[af1] - e1->af_orders[af2]);
303        }
304
305        return (0);
306}
307
308static void freeformat(void)
309{
310
311        if (f_inet != NULL)
312                free(f_inet);
313        if (f_inet6 != NULL)
314                free(f_inet6);
315        if (f_ether != NULL)
316                free(f_ether);
317        if (f_addr != NULL)
318                free(f_addr);
319}
320
321static void setformat(char *input)
322{
323        char    *formatstr, *category, *modifier;
324
325        formatstr = strdup(input);
326        while ((category = strsep(&formatstr, ",")) != NULL) {
327                modifier = strchr(category, ':');
328                if (modifier == NULL || modifier[1] == '\0') {
329                        warnx("Skipping invalid format specification: %s\n",
330                            category);
331                        continue;
332                }
333
334                /* Split the string on the separator, then seek past it */
335                modifier[0] = '\0';
336                modifier++;
337
338                if (strcmp(category, "addr") == 0)
339                        f_addr = strdup(modifier);
340                else if (strcmp(category, "ether") == 0)
341                        f_ether = strdup(modifier);
342                else if (strcmp(category, "inet") == 0)
343                        f_inet = strdup(modifier);
344                else if (strcmp(category, "inet6") == 0)
345                        f_inet6 = strdup(modifier);
346        }
347        free(formatstr);
348}
349
350#undef ORDERS_SIZE
351
352static struct ifaddrs *
353sortifaddrs(struct ifaddrs *list,
354    int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
355    struct ifa_queue *q)
356{
357        struct ifaddrs *right, *temp, *last, *result, *next, *tail;
358       
359        right = list;
360        temp = list;
361        last = list;
362        result = NULL;
363        next = NULL;
364        tail = NULL;
365
366        if (!list || !list->ifa_next)
367                return (list);
368
369        while (temp && temp->ifa_next) {
370                last = right;
371                right = right->ifa_next;
372                temp = temp->ifa_next->ifa_next;
373        }
374
375        last->ifa_next = NULL;
376
377        list = sortifaddrs(list, compare, q);
378        right = sortifaddrs(right, compare, q);
379
380        while (list || right) {
381
382                if (!right) {
383                        next = list;
384                        list = list->ifa_next;
385                } else if (!list) {
386                        next = right;
387                        right = right->ifa_next;
388                } else if (compare(list, right, q) <= 0) {
389                        next = list;
390                        list = list->ifa_next;
391                } else {
392                        next = right;
393                        right = right->ifa_next;
394                }
395
396                if (!result)
397                        result = next;
398                else
399                        tail->ifa_next = next;
400
401                tail = next;
402        }
403
404        return (result);
405}
406
407void printifnamemaybe()
408{
409        if (printifname)
410                printf("%s\n", name);
411}
412
413#ifdef __rtems__
414static void ifconfig_ctor(void);
415static int main(int argc, char *argv[]);
416
417static int
418mainwrapper(int argc, char *argv[])
419{
420        ifconfig_ctor();
421        bridge_ctor();
422        clone_ctor();
423        gif_ctor();
424        gre_ctor();
425        group_ctor();
426        ifmedia_ctor();
427#ifdef RTEMS_BSD_MODULE_NET80211
428        ieee80211_ctor();
429#endif
430#ifdef RTEMS_BSD_MODULE_NETINET6
431        inet6_ctor();
432#endif
433        inet_ctor();
434        lagg_ctor();
435        link_ctor();
436        mac_ctor();
437        pfsync_ctor();
438        vlan_ctor();
439
440        return main(argc, argv);
441}
442
443RTEMS_LINKER_RWSET(bsd_prog_ifconfig, char);
444
445int
446rtems_bsd_command_ifconfig(int argc, char *argv[])
447{
448        int exit_code;
449        void *data_begin;
450        size_t data_size;
451
452        data_begin = RTEMS_LINKER_SET_BEGIN(bsd_prog_ifconfig);
453        data_size = RTEMS_LINKER_SET_SIZE(bsd_prog_ifconfig);
454
455        rtems_bsd_program_lock();
456        exit_code = rtems_bsd_program_call_main_with_data_restore("ifconfig",
457            mainwrapper, argc, argv, data_begin, data_size);
458        rtems_bsd_program_unlock();
459
460        return exit_code;
461}
462#endif /* __rtems__ */
463int
464main(int argc, char *argv[])
465{
466        int c, all, namesonly, downonly, uponly;
467        const struct afswtch *afp = NULL;
468        int ifindex;
469        struct ifaddrs *ifap, *sifap, *ifa;
470        struct ifreq paifr;
471        const struct sockaddr_dl *sdl;
472        char options[1024], *cp, *envformat, *namecp = NULL;
473        struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
474        struct ifa_order_elt *cur, *tmp;
475        const char *ifname;
476        struct option *p;
477        size_t iflen;
478        int flags;
479#ifdef __rtems__
480        struct getopt_data getopt_data;
481        memset(&getopt_data, 0, sizeof(getopt_data));
482#define optind getopt_data.optind
483#define optarg getopt_data.optarg
484#define opterr getopt_data.opterr
485#define optopt getopt_data.optopt
486#define getopt(argc, argv, opt) getopt_r(argc, argv, opt, &getopt_data)
487#endif /* __rtems__ */
488
489        all = downonly = uponly = namesonly = noload = verbose = 0;
490        f_inet = f_inet6 = f_ether = f_addr = NULL;
491
492        envformat = getenv("IFCONFIG_FORMAT");
493        if (envformat != NULL)
494                setformat(envformat);
495
496        /*
497         * Ensure we print interface name when expected to,
498         * even if we terminate early due to error.
499         */
500        atexit(printifnamemaybe);
501
502        /* Parse leading line options */
503#ifndef __rtems__
504        strlcpy(options, "f:adklmnuv", sizeof(options));
505#else /* __rtems__ */
506        strlcpy(options, "+f:adklmnuv", sizeof(options));
507#endif /* __rtems__ */
508        for (p = opts; p != NULL; p = p->next)
509                strlcat(options, p->opt, sizeof(options));
510        while ((c = getopt(argc, argv, options)) != -1) {
511                switch (c) {
512                case 'a':       /* scan all interfaces */
513                        all++;
514                        break;
515                case 'd':       /* restrict scan to "down" interfaces */
516                        downonly++;
517                        break;
518                case 'f':
519                        if (optarg == NULL)
520                                usage();
521                        setformat(optarg);
522                        break;
523                case 'k':
524                        printkeys++;
525                        break;
526                case 'l':       /* scan interface names only */
527                        namesonly++;
528                        break;
529                case 'm':       /* show media choices in status */
530                        supmedia = 1;
531                        break;
532                case 'n':       /* suppress module loading */
533                        noload++;
534                        break;
535                case 'u':       /* restrict scan to "up" interfaces */
536                        uponly++;
537                        break;
538                case 'v':
539                        verbose++;
540                        break;
541                default:
542                        for (p = opts; p != NULL; p = p->next)
543                                if (p->opt[0] == c) {
544                                        p->cb(optarg);
545                                        break;
546                                }
547                        if (p == NULL)
548                                usage();
549                        break;
550                }
551        }
552        argc -= optind;
553        argv += optind;
554
555        /* -l cannot be used with -a or -m */
556        if (namesonly && (all || supmedia))
557                usage();
558
559        /* nonsense.. */
560        if (uponly && downonly)
561                usage();
562
563        /* no arguments is equivalent to '-a' */
564        if (!namesonly && argc < 1)
565                all = 1;
566
567        /* -a and -l allow an address family arg to limit the output */
568        if (all || namesonly) {
569                if (argc > 1)
570                        usage();
571
572                ifname = NULL;
573                ifindex = 0;
574                if (argc == 1) {
575                        afp = af_getbyname(*argv);
576                        if (afp == NULL) {
577                                warnx("Address family '%s' unknown.", *argv);
578                                usage();
579                        }
580                        if (afp->af_name != NULL)
581                                argc--, argv++;
582                        /* leave with afp non-zero */
583                }
584        } else {
585                /* not listing, need an argument */
586                if (argc < 1)
587                        usage();
588
589                ifname = *argv;
590                argc--, argv++;
591
592                /* check and maybe load support for this interface */
593                ifmaybeload(ifname);
594
595                ifindex = if_nametoindex(ifname);
596                if (ifindex == 0) {
597                        /*
598                         * NOTE:  We must special-case the `create' command
599                         * right here as we would otherwise fail when trying
600                         * to find the interface.
601                         */
602                        if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
603                            strcmp(argv[0], "plumb") == 0)) {
604                                iflen = strlcpy(name, ifname, sizeof(name));
605                                if (iflen >= sizeof(name))
606                                        errx(1, "%s: cloning name too long",
607                                            ifname);
608                                ifconfig(argc, argv, 1, NULL);
609                                exit(exit_code);
610                        }
611#ifdef JAIL
612                        /*
613                         * NOTE:  We have to special-case the `-vnet' command
614                         * right here as we would otherwise fail when trying
615                         * to find the interface as it lives in another vnet.
616                         */
617                        if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
618                                iflen = strlcpy(name, ifname, sizeof(name));
619                                if (iflen >= sizeof(name))
620                                        errx(1, "%s: interface name too long",
621                                            ifname);
622                                ifconfig(argc, argv, 0, NULL);
623                                exit(exit_code);
624                        }
625#endif
626                        errx(1, "interface %s does not exist", ifname);
627                } else {
628                        /*
629                         * Do not allow use `create` command as hostname if
630                         * address family is not specified.
631                         */
632                        if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
633                            strcmp(argv[0], "plumb") == 0)) {
634                                if (argc == 1)
635                                        errx(1, "interface %s already exists",
636                                            ifname);
637                                argc--, argv++;
638                        }
639                }
640        }
641
642        /* Check for address family */
643        if (argc > 0) {
644                afp = af_getbyname(*argv);
645                if (afp != NULL)
646                        argc--, argv++;
647        }
648
649        /*
650         * Check for a requested configuration action on a single interface,
651         * which doesn't require building, sorting, and searching the entire
652         * system address list
653         */
654        if ((argc > 0) && (ifname != NULL)) {
655                iflen = strlcpy(name, ifname, sizeof(name));
656                if (iflen >= sizeof(name)) {
657                        warnx("%s: interface name too long, skipping", ifname);
658                } else {
659                        flags = getifflags(name, -1);
660                        if (!(((flags & IFF_CANTCONFIG) != 0) ||
661                                (downonly && (flags & IFF_UP) != 0) ||
662                                (uponly && (flags & IFF_UP) == 0)))
663                                ifconfig(argc, argv, 0, afp);
664                }
665                goto done;
666        }
667
668        if (getifaddrs(&ifap) != 0)
669                err(EXIT_FAILURE, "getifaddrs");
670
671        cp = NULL;
672       
673        if (calcorders(ifap, &q) != 0)
674                err(EXIT_FAILURE, "calcorders");
675               
676        sifap = sortifaddrs(ifap, cmpifaddrs, &q);
677
678        TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
679                free(cur);
680
681        ifindex = 0;
682        for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
683                memset(&paifr, 0, sizeof(paifr));
684                strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
685                if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
686                        memcpy(&paifr.ifr_addr, ifa->ifa_addr,
687                            ifa->ifa_addr->sa_len);
688                }
689
690                if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
691                        continue;
692                if (ifa->ifa_addr->sa_family == AF_LINK)
693                        sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
694                else
695                        sdl = NULL;
696                if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
697                        continue;
698                iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
699                if (iflen >= sizeof(name)) {
700                        warnx("%s: interface name too long, skipping",
701                            ifa->ifa_name);
702                        continue;
703                }
704                cp = ifa->ifa_name;
705
706                if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
707                        continue;
708                if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
709                        continue;
710                if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
711                        continue;
712                /*
713                 * Are we just listing the interfaces?
714                 */
715                if (namesonly) {
716                        if (namecp == cp)
717                                continue;
718                        if (afp != NULL) {
719                                /* special case for "ether" address family */
720                                if (!strcmp(afp->af_name, "ether")) {
721                                        if (sdl == NULL ||
722                                            (sdl->sdl_type != IFT_ETHER &&
723                                            sdl->sdl_type != IFT_L2VLAN &&
724                                            sdl->sdl_type != IFT_BRIDGE) ||
725                                            sdl->sdl_alen != ETHER_ADDR_LEN)
726                                                continue;
727                                } else {
728                                        if (ifa->ifa_addr->sa_family
729                                            != afp->af_af)
730                                                continue;
731                                }
732                        }
733                        namecp = cp;
734                        ifindex++;
735                        if (ifindex > 1)
736                                printf(" ");
737                        fputs(name, stdout);
738                        continue;
739                }
740                ifindex++;
741
742                if (argc > 0)
743                        ifconfig(argc, argv, 0, afp);
744                else
745                        status(afp, sdl, ifa);
746        }
747        if (namesonly)
748                printf("\n");
749        freeifaddrs(ifap);
750
751done:
752        freeformat();
753        exit(exit_code);
754}
755
756static struct afswtch *afs = NULL;
757
758void
759af_register(struct afswtch *p)
760{
761        p->af_next = afs;
762        afs = p;
763}
764
765static struct afswtch *
766af_getbyname(const char *name)
767{
768        struct afswtch *afp;
769
770        for (afp = afs; afp !=  NULL; afp = afp->af_next)
771                if (strcmp(afp->af_name, name) == 0)
772                        return afp;
773        return NULL;
774}
775
776static struct afswtch *
777af_getbyfamily(int af)
778{
779        struct afswtch *afp;
780
781        for (afp = afs; afp != NULL; afp = afp->af_next)
782                if (afp->af_af == af)
783                        return afp;
784        return NULL;
785}
786
787static void
788af_other_status(int s)
789{
790        struct afswtch *afp;
791        uint8_t afmask[howmany(AF_MAX, NBBY)];
792
793        memset(afmask, 0, sizeof(afmask));
794        for (afp = afs; afp != NULL; afp = afp->af_next) {
795                if (afp->af_other_status == NULL)
796                        continue;
797                if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
798                        continue;
799                afp->af_other_status(s);
800                setbit(afmask, afp->af_af);
801        }
802}
803
804static void
805af_all_tunnel_status(int s)
806{
807        struct afswtch *afp;
808        uint8_t afmask[howmany(AF_MAX, NBBY)];
809
810        memset(afmask, 0, sizeof(afmask));
811        for (afp = afs; afp != NULL; afp = afp->af_next) {
812                if (afp->af_status_tunnel == NULL)
813                        continue;
814                if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
815                        continue;
816                afp->af_status_tunnel(s);
817                setbit(afmask, afp->af_af);
818        }
819}
820
821static struct cmd *cmds = NULL;
822
823void
824cmd_register(struct cmd *p)
825{
826        p->c_next = cmds;
827        cmds = p;
828}
829
830static const struct cmd *
831cmd_lookup(const char *name, int iscreate)
832{
833        const struct cmd *p;
834
835        for (p = cmds; p != NULL; p = p->c_next)
836                if (strcmp(name, p->c_name) == 0) {
837                        if (iscreate) {
838                                if (p->c_iscloneop)
839                                        return p;
840                        } else {
841                                if (!p->c_iscloneop)
842                                        return p;
843                        }
844                }
845        return NULL;
846}
847
848struct callback {
849        callback_func *cb_func;
850        void    *cb_arg;
851        struct callback *cb_next;
852};
853static struct callback *callbacks = NULL;
854
855void
856callback_register(callback_func *func, void *arg)
857{
858        struct callback *cb;
859
860        cb = malloc(sizeof(struct callback));
861        if (cb == NULL)
862                errx(1, "unable to allocate memory for callback");
863        cb->cb_func = func;
864        cb->cb_arg = arg;
865        cb->cb_next = callbacks;
866        callbacks = cb;
867}
868
869/* specially-handled commands */
870static void setifaddr(const char *, int, int, const struct afswtch *);
871static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
872
873static void setifdstaddr(const char *, int, int, const struct afswtch *);
874static const struct cmd setifdstaddr_cmd =
875        DEF_CMD("ifdstaddr", 0, setifdstaddr);
876
877static int
878ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
879{
880        const struct afswtch *afp, *nafp;
881        const struct cmd *p;
882        struct callback *cb;
883        int s;
884
885        strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
886        afp = NULL;
887        if (uafp != NULL)
888                afp = uafp;
889        /*
890         * This is the historical "accident" allowing users to configure IPv4
891         * addresses without the "inet" keyword which while a nice feature has
892         * proven to complicate other things.  We cannot remove this but only
893         * make sure we will never have a similar implicit default for IPv6 or
894         * any other address familiy.  We need a fallback though for
895         * ifconfig IF up/down etc. to work without INET support as people
896         * never used ifconfig IF link up/down, etc. either.
897         */
898#ifndef RESCUE
899#ifdef INET
900        if (afp == NULL && feature_present("inet"))
901                afp = af_getbyname("inet");
902#endif
903#endif
904        if (afp == NULL)
905                afp = af_getbyname("link");
906        if (afp == NULL) {
907                warnx("Please specify an address_family.");
908                usage();
909        }
910top:
911        ifr.ifr_addr.sa_family =
912                afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
913                AF_LOCAL : afp->af_af;
914
915        if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
916            (uafp != NULL || errno != EAFNOSUPPORT ||
917             (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
918                err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
919
920        while (argc > 0) {
921                p = cmd_lookup(*argv, iscreate);
922                if (iscreate && p == NULL) {
923                        /*
924                         * Push the clone create callback so the new
925                         * device is created and can be used for any
926                         * remaining arguments.
927                         */
928                        cb = callbacks;
929                        if (cb == NULL)
930                                errx(1, "internal error, no callback");
931                        callbacks = cb->cb_next;
932                        cb->cb_func(s, cb->cb_arg);
933                        iscreate = 0;
934                        /*
935                         * Handle any address family spec that
936                         * immediately follows and potentially
937                         * recreate the socket.
938                         */
939                        nafp = af_getbyname(*argv);
940                        if (nafp != NULL) {
941                                argc--, argv++;
942                                if (nafp != afp) {
943                                        close(s);
944                                        afp = nafp;
945                                        goto top;
946                                }
947                        }
948                        /*
949                         * Look for a normal parameter.
950                         */
951                        continue;
952                }
953                if (p == NULL) {
954                        /*
955                         * Not a recognized command, choose between setting
956                         * the interface address and the dst address.
957                         */
958                        p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
959                }
960                if (p->c_parameter == NEXTARG && p->c_u.c_func) {
961                        if (argv[1] == NULL)
962                                errx(1, "'%s' requires argument",
963                                    p->c_name);
964                        p->c_u.c_func(argv[1], 0, s, afp);
965                        argc--, argv++;
966                } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
967                        p->c_u.c_func(argv[1], 0, s, afp);
968                        if (argv[1] != NULL)
969                                argc--, argv++;
970                } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
971                        if (argc < 3)
972                                errx(1, "'%s' requires 2 arguments",
973                                    p->c_name);
974                        p->c_u.c_func2(argv[1], argv[2], s, afp);
975                        argc -= 2, argv += 2;
976                } else if (p->c_u.c_func)
977                        p->c_u.c_func(*argv, p->c_parameter, s, afp);
978                argc--, argv++;
979        }
980
981        /*
982         * Do any post argument processing required by the address family.
983         */
984        if (afp->af_postproc != NULL)
985                afp->af_postproc(s, afp);
986        /*
987         * Do deferred callbacks registered while processing
988         * command-line arguments.
989         */
990        for (cb = callbacks; cb != NULL; cb = cb->cb_next)
991                cb->cb_func(s, cb->cb_arg);
992        /*
993         * Do deferred operations.
994         */
995        if (clearaddr) {
996                if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
997                        warnx("interface %s cannot change %s addresses!",
998                              name, afp->af_name);
999                        clearaddr = 0;
1000                }
1001        }
1002        if (clearaddr) {
1003                int ret;
1004                strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
1005                        sizeof ifr.ifr_name);
1006                ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
1007                if (ret < 0) {
1008                        if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
1009                                /* means no previous address for interface */
1010                        } else
1011                                Perror("ioctl (SIOCDIFADDR)");
1012                }
1013        }
1014        if (newaddr) {
1015                if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
1016                        warnx("interface %s cannot change %s addresses!",
1017                              name, afp->af_name);
1018                        newaddr = 0;
1019                }
1020        }
1021        if (newaddr && (setaddr || setmask)) {
1022                strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
1023                        sizeof ifr.ifr_name);
1024                if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
1025                        Perror("ioctl (SIOCAIFADDR)");
1026        }
1027
1028        close(s);
1029        return(0);
1030}
1031
1032/*ARGSUSED*/
1033static void
1034setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
1035{
1036        if (afp->af_getaddr == NULL)
1037                return;
1038        /*
1039         * Delay the ioctl to set the interface addr until flags are all set.
1040         * The address interpretation may depend on the flags,
1041         * and the flags may change when the address is set.
1042         */
1043        setaddr++;
1044        if (doalias == 0 && afp->af_af != AF_LINK)
1045                clearaddr = 1;
1046        afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
1047}
1048
1049static void
1050settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
1051{
1052        struct addrinfo *srcres, *dstres;
1053        int ecode;
1054
1055        if (afp->af_settunnel == NULL) {
1056                warn("address family %s does not support tunnel setup",
1057                        afp->af_name);
1058                return;
1059        }
1060
1061        if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
1062                errx(1, "error in parsing address string: %s",
1063                    gai_strerror(ecode));
1064
1065        if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
1066                errx(1, "error in parsing address string: %s",
1067                    gai_strerror(ecode));
1068
1069        if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
1070                errx(1,
1071                    "source and destination address families do not match");
1072
1073        afp->af_settunnel(s, srcres, dstres);
1074
1075        freeaddrinfo(srcres);
1076        freeaddrinfo(dstres);
1077}
1078
1079/* ARGSUSED */
1080static void
1081deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
1082{
1083
1084        if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1085                err(1, "SIOCDIFPHYADDR");
1086}
1087
1088#ifdef JAIL
1089static void
1090setifvnet(const char *jname, int dummy __unused, int s,
1091    const struct afswtch *afp)
1092{
1093        struct ifreq my_ifr;
1094
1095        memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1096        my_ifr.ifr_jid = jail_getid(jname);
1097        if (my_ifr.ifr_jid < 0)
1098                errx(1, "%s", jail_errmsg);
1099        if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1100                err(1, "SIOCSIFVNET");
1101}
1102
1103static void
1104setifrvnet(const char *jname, int dummy __unused, int s,
1105    const struct afswtch *afp)
1106{
1107        struct ifreq my_ifr;
1108
1109        memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1110        my_ifr.ifr_jid = jail_getid(jname);
1111        if (my_ifr.ifr_jid < 0)
1112                errx(1, "%s", jail_errmsg);
1113        if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1114                err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1115}
1116#endif
1117
1118static void
1119setifnetmask(const char *addr, int dummy __unused, int s,
1120    const struct afswtch *afp)
1121{
1122        if (afp->af_getaddr != NULL) {
1123                setmask++;
1124                afp->af_getaddr(addr, MASK);
1125        }
1126}
1127
1128static void
1129setifbroadaddr(const char *addr, int dummy __unused, int s,
1130    const struct afswtch *afp)
1131{
1132        if (afp->af_getaddr != NULL)
1133                afp->af_getaddr(addr, DSTADDR);
1134}
1135
1136static void
1137notealias(const char *addr, int param, int s, const struct afswtch *afp)
1138{
1139#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1140        if (setaddr && doalias == 0 && param < 0)
1141                if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1142                        bcopy((caddr_t)rqtosa(af_addreq),
1143                              (caddr_t)rqtosa(af_ridreq),
1144                              rqtosa(af_addreq)->sa_len);
1145        doalias = param;
1146        if (param < 0) {
1147                clearaddr = 1;
1148                newaddr = 0;
1149        } else
1150                clearaddr = 0;
1151#undef rqtosa
1152}
1153
1154/*ARGSUSED*/
1155static void
1156setifdstaddr(const char *addr, int param __unused, int s,
1157    const struct afswtch *afp)
1158{
1159        if (afp->af_getaddr != NULL)
1160                afp->af_getaddr(addr, DSTADDR);
1161}
1162
1163static int
1164getifflags(const char *ifname, int us)
1165{
1166        struct ifreq my_ifr;
1167        int s;
1168       
1169        memset(&my_ifr, 0, sizeof(my_ifr));
1170        (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1171        if (us < 0) {
1172                if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1173                        err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1174        } else
1175                s = us;
1176        if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1177                Perror("ioctl (SIOCGIFFLAGS)");
1178                exit(1);
1179        }
1180        if (us < 0)
1181                close(s);
1182        return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1183}
1184
1185/*
1186 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1187 * of the ifreq structure, which may confuse other parts of ifconfig.
1188 * Make a private copy so we can avoid that.
1189 */
1190static void
1191setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1192{
1193        struct ifreq            my_ifr;
1194        int flags;
1195
1196        flags = getifflags(name, s);
1197        if (value < 0) {
1198                value = -value;
1199                flags &= ~value;
1200        } else
1201                flags |= value;
1202        memset(&my_ifr, 0, sizeof(my_ifr));
1203        (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1204        my_ifr.ifr_flags = flags & 0xffff;
1205        my_ifr.ifr_flagshigh = flags >> 16;
1206        if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1207                Perror(vname);
1208}
1209
1210void
1211setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1212{
1213        int flags;
1214
1215        if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1216                Perror("ioctl (SIOCGIFCAP)");
1217                exit(1);
1218        }
1219        flags = ifr.ifr_curcap;
1220        if (value < 0) {
1221                value = -value;
1222                flags &= ~value;
1223        } else
1224                flags |= value;
1225        flags &= ifr.ifr_reqcap;
1226        ifr.ifr_reqcap = flags;
1227        if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1228                Perror(vname);
1229}
1230
1231static void
1232setifmetric(const char *val, int dummy __unused, int s,
1233    const struct afswtch *afp)
1234{
1235        strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1236        ifr.ifr_metric = atoi(val);
1237        if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1238                err(1, "ioctl SIOCSIFMETRIC (set metric)");
1239}
1240
1241static void
1242setifmtu(const char *val, int dummy __unused, int s,
1243    const struct afswtch *afp)
1244{
1245        strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1246        ifr.ifr_mtu = atoi(val);
1247        if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1248                err(1, "ioctl SIOCSIFMTU (set mtu)");
1249}
1250
1251static void
1252setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1253{
1254        u_long ul;
1255        char *endp;
1256
1257        ul = strtoul(val, &endp, 0);
1258        if (*endp != '\0')
1259                errx(1, "invalid value for pcp");
1260        if (ul > 7)
1261                errx(1, "value for pcp out of range");
1262        ifr.ifr_lan_pcp = ul;
1263        if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1264                err(1, "SIOCSLANPCP");
1265}
1266
1267static void
1268disableifpcp(const char *val, int arg __unused, int s,
1269    const struct afswtch *afp)
1270{
1271
1272        ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1273        if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1274                err(1, "SIOCSLANPCP");
1275}
1276
1277static void
1278setifname(const char *val, int dummy __unused, int s,
1279    const struct afswtch *afp)
1280{
1281        char *newname;
1282       
1283        strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1284
1285        newname = strdup(val);
1286        if (newname == NULL)
1287                err(1, "no memory to set ifname");
1288        ifr.ifr_data = newname;
1289        if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1290                free(newname);
1291                err(1, "ioctl SIOCSIFNAME (set name)");
1292        }
1293        printifname = 1;
1294        strlcpy(name, newname, sizeof(name));
1295        free(newname);
1296}
1297
1298/* ARGSUSED */
1299static void
1300setifdescr(const char *val, int dummy __unused, int s,
1301    const struct afswtch *afp)
1302{
1303        char *newdescr;
1304
1305        strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1306       
1307        ifr.ifr_buffer.length = strlen(val) + 1;
1308        if (ifr.ifr_buffer.length == 1) {
1309                ifr.ifr_buffer.buffer = newdescr = NULL;
1310                ifr.ifr_buffer.length = 0;
1311        } else {
1312                newdescr = strdup(val);
1313                ifr.ifr_buffer.buffer = newdescr;
1314                if (newdescr == NULL) {
1315                        warn("no memory to set ifdescr");
1316                        return;
1317                }
1318        }
1319
1320        if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1321                err(1, "ioctl SIOCSIFDESCR (set descr)");
1322
1323        free(newdescr);
1324}
1325
1326/* ARGSUSED */
1327static void
1328unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1329{
1330
1331        setifdescr("", 0, s, 0);
1332}
1333
1334#define IFFBITS \
1335"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1336"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1337"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1338
1339#define IFCAPBITS \
1340"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1341"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1342"\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1343"\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP"
1344
1345/*
1346 * Print the status of the interface.  If an address family was
1347 * specified, show only it; otherwise, show them all.
1348 */
1349static void
1350status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1351        struct ifaddrs *ifa)
1352{
1353        struct ifaddrs *ift;
1354        int allfamilies, s;
1355        struct ifstat ifs;
1356
1357        if (afp == NULL) {
1358                allfamilies = 1;
1359#ifndef __rtems__
1360                ifr.ifr_addr.sa_family = AF_LOCAL;
1361#else /* __rtems__ */
1362                ifr.ifr_addr.sa_family = AF_INET;
1363#endif /* __rtems__ */
1364        } else {
1365                allfamilies = 0;
1366                ifr.ifr_addr.sa_family =
1367                    afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1368        }
1369        strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1370
1371        s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1372        if (s < 0)
1373                err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1374
1375        printf("%s: ", name);
1376        printb("flags", ifa->ifa_flags, IFFBITS);
1377        if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1378                printf(" metric %d", ifr.ifr_metric);
1379        if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1380                printf(" mtu %d", ifr.ifr_mtu);
1381        putchar('\n');
1382
1383        for (;;) {
1384                if ((descr = reallocf(descr, descrlen)) != NULL) {
1385                        ifr.ifr_buffer.buffer = descr;
1386                        ifr.ifr_buffer.length = descrlen;
1387                        if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1388                                if (ifr.ifr_buffer.buffer == descr) {
1389                                        if (strlen(descr) > 0)
1390                                                printf("\tdescription: %s\n",
1391                                                    descr);
1392                                } else if (ifr.ifr_buffer.length > descrlen) {
1393                                        descrlen = ifr.ifr_buffer.length;
1394                                        continue;
1395                                }
1396                        }
1397                } else
1398                        warn("unable to allocate memory for interface"
1399                            "description");
1400                break;
1401        }
1402
1403        if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1404                if (ifr.ifr_curcap != 0) {
1405                        printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1406                        putchar('\n');
1407                }
1408                if (supmedia && ifr.ifr_reqcap != 0) {
1409                        printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1410                        putchar('\n');
1411                }
1412        }
1413
1414        tunnel_status(s);
1415
1416        for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1417                if (ift->ifa_addr == NULL)
1418                        continue;
1419                if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1420                        continue;
1421                if (allfamilies) {
1422                        const struct afswtch *p;
1423                        p = af_getbyfamily(ift->ifa_addr->sa_family);
1424                        if (p != NULL && p->af_status != NULL)
1425                                p->af_status(s, ift);
1426                } else if (afp->af_af == ift->ifa_addr->sa_family)
1427                        afp->af_status(s, ift);
1428        }
1429#if 0
1430        if (allfamilies || afp->af_af == AF_LINK) {
1431                const struct afswtch *lafp;
1432
1433                /*
1434                 * Hack; the link level address is received separately
1435                 * from the routing information so any address is not
1436                 * handled above.  Cobble together an entry and invoke
1437                 * the status method specially.
1438                 */
1439                lafp = af_getbyname("lladdr");
1440                if (lafp != NULL) {
1441                        info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1442                        lafp->af_status(s, &info);
1443                }
1444        }
1445#endif
1446        if (allfamilies)
1447                af_other_status(s);
1448        else if (afp->af_other_status != NULL)
1449                afp->af_other_status(s);
1450
1451        strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1452        if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1453                printf("%s", ifs.ascii);
1454
1455        if (verbose > 0)
1456                sfp_status(s, &ifr, verbose);
1457
1458        close(s);
1459        return;
1460}
1461
1462static void
1463tunnel_status(int s)
1464{
1465        af_all_tunnel_status(s);
1466}
1467
1468void
1469Perror(const char *cmd)
1470{
1471        switch (errno) {
1472
1473        case ENXIO:
1474                errx(1, "%s: no such interface", cmd);
1475                break;
1476
1477        case EPERM:
1478                errx(1, "%s: permission denied", cmd);
1479                break;
1480
1481        default:
1482                err(1, "%s", cmd);
1483        }
1484}
1485
1486/*
1487 * Print a value a la the %b format of the kernel's printf
1488 */
1489void
1490printb(const char *s, unsigned v, const char *bits)
1491{
1492        int i, any = 0;
1493        char c;
1494
1495        if (bits && *bits == 8)
1496                printf("%s=%o", s, v);
1497        else
1498                printf("%s=%x", s, v);
1499        if (bits) {
1500                bits++;
1501                putchar('<');
1502                while ((i = *bits++) != '\0') {
1503                        if (v & (1 << (i-1))) {
1504                                if (any)
1505                                        putchar(',');
1506                                any = 1;
1507                                for (; (c = *bits) > 32; bits++)
1508                                        putchar(c);
1509                        } else
1510                                for (; *bits > 32; bits++)
1511                                        ;
1512                }
1513                putchar('>');
1514        }
1515}
1516
1517void
1518print_vhid(const struct ifaddrs *ifa, const char *s)
1519{
1520        struct if_data *ifd;
1521
1522        if (ifa->ifa_data == NULL)
1523                return;
1524
1525        ifd = ifa->ifa_data;
1526        if (ifd->ifi_vhid == 0)
1527                return;
1528       
1529        printf(" vhid %d", ifd->ifi_vhid);
1530}
1531
1532void
1533ifmaybeload(const char *name)
1534{
1535#ifndef __rtems__
1536#define MOD_PREFIX_LEN          3       /* "if_" */
1537        struct module_stat mstat;
1538        int i, fileid, modid;
1539        char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1540        const char *cp;
1541        struct module_map_entry *mme;
1542        bool found;
1543
1544        /* loading suppressed by the user */
1545        if (noload)
1546                return;
1547
1548        /* trim the interface number off the end */
1549        strlcpy(ifname, name, sizeof(ifname));
1550        for (dp = ifname; *dp != 0; dp++)
1551                if (isdigit(*dp)) {
1552                        *dp = 0;
1553                        break;
1554                }
1555
1556        /* Either derive it from the map or guess otherwise */
1557        *ifkind = '\0';
1558        found = false;
1559        for (i = 0; i < nitems(module_map); ++i) {
1560                mme = &module_map[i];
1561                if (strcmp(mme->ifname, ifname) == 0) {
1562                        strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1563                        found = true;
1564                        break;
1565                }
1566        }
1567
1568        /* We didn't have an alias for it... we'll guess. */
1569        if (!found) {
1570            /* turn interface and unit into module name */
1571            strlcpy(ifkind, "if_", sizeof(ifkind));
1572            strlcat(ifkind, ifname, sizeof(ifkind));
1573        }
1574
1575        /* scan files in kernel */
1576        mstat.version = sizeof(struct module_stat);
1577        for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1578                /* scan modules in file */
1579                for (modid = kldfirstmod(fileid); modid > 0;
1580                     modid = modfnext(modid)) {
1581                        if (modstat(modid, &mstat) < 0)
1582                                continue;
1583                        /* strip bus name if present */
1584                        if ((cp = strchr(mstat.name, '/')) != NULL) {
1585                                cp++;
1586                        } else {
1587                                cp = mstat.name;
1588                        }
1589                        /*
1590                         * Is it already loaded?  Don't compare with ifname if
1591                         * we were specifically told which kld to use.  Doing
1592                         * so could lead to conflicts not trivially solved.
1593                         */
1594                        if ((!found && strcmp(ifname, cp) == 0) ||
1595                            strcmp(ifkind, cp) == 0)
1596                                return;
1597                }
1598        }
1599
1600        /*
1601         * Try to load the module.  But ignore failures, because ifconfig can't
1602         * infer the names of all drivers (eg mlx4en(4)).
1603         */
1604        (void) kldload(ifkind);
1605#endif /* __rtems__ */
1606}
1607
1608static struct cmd basic_cmds[] = {
1609        DEF_CMD("up",           IFF_UP,         setifflags),
1610        DEF_CMD("down",         -IFF_UP,        setifflags),
1611        DEF_CMD("arp",          -IFF_NOARP,     setifflags),
1612        DEF_CMD("-arp",         IFF_NOARP,      setifflags),
1613        DEF_CMD("debug",        IFF_DEBUG,      setifflags),
1614        DEF_CMD("-debug",       -IFF_DEBUG,     setifflags),
1615        DEF_CMD_ARG("description",              setifdescr),
1616        DEF_CMD_ARG("descr",                    setifdescr),
1617        DEF_CMD("-description", 0,              unsetifdescr),
1618        DEF_CMD("-descr",       0,              unsetifdescr),
1619        DEF_CMD("promisc",      IFF_PPROMISC,   setifflags),
1620        DEF_CMD("-promisc",     -IFF_PPROMISC,  setifflags),
1621        DEF_CMD("add",          IFF_UP,         notealias),
1622        DEF_CMD("alias",        IFF_UP,         notealias),
1623        DEF_CMD("-alias",       -IFF_UP,        notealias),
1624        DEF_CMD("delete",       -IFF_UP,        notealias),
1625        DEF_CMD("remove",       -IFF_UP,        notealias),
1626#ifdef notdef
1627#define EN_SWABIPS      0x1000
1628        DEF_CMD("swabips",      EN_SWABIPS,     setifflags),
1629        DEF_CMD("-swabips",     -EN_SWABIPS,    setifflags),
1630#endif
1631        DEF_CMD_ARG("netmask",                  setifnetmask),
1632        DEF_CMD_ARG("metric",                   setifmetric),
1633        DEF_CMD_ARG("broadcast",                setifbroadaddr),
1634        DEF_CMD_ARG2("tunnel",                  settunnel),
1635        DEF_CMD("-tunnel", 0,                   deletetunnel),
1636        DEF_CMD("deletetunnel", 0,              deletetunnel),
1637#ifdef JAIL
1638        DEF_CMD_ARG("vnet",                     setifvnet),
1639        DEF_CMD_ARG("-vnet",                    setifrvnet),
1640#endif
1641        DEF_CMD("link0",        IFF_LINK0,      setifflags),
1642        DEF_CMD("-link0",       -IFF_LINK0,     setifflags),
1643        DEF_CMD("link1",        IFF_LINK1,      setifflags),
1644        DEF_CMD("-link1",       -IFF_LINK1,     setifflags),
1645        DEF_CMD("link2",        IFF_LINK2,      setifflags),
1646        DEF_CMD("-link2",       -IFF_LINK2,     setifflags),
1647        DEF_CMD("monitor",      IFF_MONITOR,    setifflags),
1648        DEF_CMD("-monitor",     -IFF_MONITOR,   setifflags),
1649        DEF_CMD("staticarp",    IFF_STATICARP,  setifflags),
1650        DEF_CMD("-staticarp",   -IFF_STATICARP, setifflags),
1651        DEF_CMD("rxcsum6",      IFCAP_RXCSUM_IPV6,      setifcap),
1652        DEF_CMD("-rxcsum6",     -IFCAP_RXCSUM_IPV6,     setifcap),
1653        DEF_CMD("txcsum6",      IFCAP_TXCSUM_IPV6,      setifcap),
1654        DEF_CMD("-txcsum6",     -IFCAP_TXCSUM_IPV6,     setifcap),
1655        DEF_CMD("rxcsum",       IFCAP_RXCSUM,   setifcap),
1656        DEF_CMD("-rxcsum",      -IFCAP_RXCSUM,  setifcap),
1657        DEF_CMD("txcsum",       IFCAP_TXCSUM,   setifcap),
1658        DEF_CMD("-txcsum",      -IFCAP_TXCSUM,  setifcap),
1659        DEF_CMD("netcons",      IFCAP_NETCONS,  setifcap),
1660        DEF_CMD("-netcons",     -IFCAP_NETCONS, setifcap),
1661        DEF_CMD_ARG("pcp",                      setifpcp),
1662        DEF_CMD("-pcp", 0,                      disableifpcp),
1663        DEF_CMD("polling",      IFCAP_POLLING,  setifcap),
1664        DEF_CMD("-polling",     -IFCAP_POLLING, setifcap),
1665        DEF_CMD("tso6",         IFCAP_TSO6,     setifcap),
1666        DEF_CMD("-tso6",        -IFCAP_TSO6,    setifcap),
1667        DEF_CMD("tso4",         IFCAP_TSO4,     setifcap),
1668        DEF_CMD("-tso4",        -IFCAP_TSO4,    setifcap),
1669        DEF_CMD("tso",          IFCAP_TSO,      setifcap),
1670        DEF_CMD("-tso",         -IFCAP_TSO,     setifcap),
1671        DEF_CMD("toe",          IFCAP_TOE,      setifcap),
1672        DEF_CMD("-toe",         -IFCAP_TOE,     setifcap),
1673        DEF_CMD("lro",          IFCAP_LRO,      setifcap),
1674        DEF_CMD("-lro",         -IFCAP_LRO,     setifcap),
1675        DEF_CMD("wol",          IFCAP_WOL,      setifcap),
1676        DEF_CMD("-wol",         -IFCAP_WOL,     setifcap),
1677        DEF_CMD("wol_ucast",    IFCAP_WOL_UCAST,        setifcap),
1678        DEF_CMD("-wol_ucast",   -IFCAP_WOL_UCAST,       setifcap),
1679        DEF_CMD("wol_mcast",    IFCAP_WOL_MCAST,        setifcap),
1680        DEF_CMD("-wol_mcast",   -IFCAP_WOL_MCAST,       setifcap),
1681        DEF_CMD("wol_magic",    IFCAP_WOL_MAGIC,        setifcap),
1682        DEF_CMD("-wol_magic",   -IFCAP_WOL_MAGIC,       setifcap),
1683        DEF_CMD("txrtlmt",      IFCAP_TXRTLMT,  setifcap),
1684        DEF_CMD("-txrtlmt",     -IFCAP_TXRTLMT, setifcap),
1685        DEF_CMD("hwrxtstmp",    IFCAP_HWRXTSTMP,        setifcap),
1686        DEF_CMD("-hwrxtstmp",   -IFCAP_HWRXTSTMP,       setifcap),
1687        DEF_CMD("normal",       -IFF_LINK0,     setifflags),
1688        DEF_CMD("compress",     IFF_LINK0,      setifflags),
1689        DEF_CMD("noicmp",       IFF_LINK1,      setifflags),
1690        DEF_CMD_ARG("mtu",                      setifmtu),
1691        DEF_CMD_ARG("name",                     setifname),
1692};
1693
1694#ifndef __rtems__
1695static __constructor void
1696#else /* __rtems__ */
1697static void
1698#endif /* __rtems__ */
1699ifconfig_ctor(void)
1700{
1701        size_t i;
1702
1703        for (i = 0; i < nitems(basic_cmds);  i++)
1704                cmd_register(&basic_cmds[i]);
1705}
Note: See TracBrowser for help on using the repository browser.