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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3ac9bb1 was 3ac9bb1, checked in by Jennifer Averett <jennifer.averett@…>, on 10/05/12 at 18:41:20

route.c and ifconfig.c: Added rtems style mains.

Fixed route.c to match freebsd version with only rtems additions.

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