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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since c333bab was c333bab, checked in by Sebastian Huber <sebastian.huber@…>, on 10/17/13 at 08:46:42

Simplify getopt() to getopt_r() translation

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