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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

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