Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/sbin/ifconfig - Add `tunnel' and `deletetunnel' command...



details:   https://anonhg.NetBSD.org/src/rev/67f69bef661a
branches:  netbsd-1-5
changeset: 488342:67f69bef661a
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Fri Jun 30 18:12:46 2000 +0000

description:
- Add `tunnel' and `deletetunnel' commands, which issue the ioctls
  that gifconfig(8) would issue to configure tunnel endpoints.  This
  allows IP tunnel interfaces (`gif' right now, and `gre' later) to
  be configured with ifconfig(8), and via /etc/ifconfig.<interface>.
  Partially taken from similar changes in OpenBSD.
- Const poison the command functions a bit.  We really need to clean
  up the command function interface.

Rationale for pulling into netbsd-1-5 branch: While this looks like
a feature enhancement, it actually fixes a bug -- without this change
or a change in rc.d's network bring-up, it is not possible to configure
IP tunnels as a normal part of bringing up the network.

diffstat:

 sbin/ifconfig/ifconfig.8 |   15 +-
 sbin/ifconfig/ifconfig.c |  341 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 264 insertions(+), 92 deletions(-)

diffs (truncated from 701 to 300 lines):

diff -r 7fff1af25953 -r 67f69bef661a sbin/ifconfig/ifconfig.8
--- a/sbin/ifconfig/ifconfig.8  Fri Jun 30 16:27:37 2000 +0000
+++ b/sbin/ifconfig/ifconfig.8  Fri Jun 30 18:12:46 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ifconfig.8,v 1.32 2000/03/01 03:32:55 enami Exp $
+.\"    $NetBSD: ifconfig.8,v 1.32.4.1 2000/06/30 18:12:46 thorpej Exp $
 .\"
 .\" Copyright (c) 1983, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -298,6 +298,19 @@
 .It Cm nwid Ar id
 (IEEE 802.11 devices only)
 Configure network ID for IEEE 802.11-based wireless network interfaces.
+.It Cm tunnel Ar src_addr Ar dest_addr
+(IP tunnel devices only)
+Configure the physical source and destination address for IP tunnel
+interfaces (gif).  The arguments
+.Ar src_addr
+and
+.Ar dest_addr
+are interpreted as the outer source/destination for the encapsulating
+IPv4/IPv6 header.
+.It Cm deletetunnel
+Unconfigure the physical source and destination address for IP tunnel
+interfaces previously configured with
+.Cm tunnel .
 .It Cm pltime Ar n
 (inet6 only)
 Set preferred lifetime for the address.
diff -r 7fff1af25953 -r 67f69bef661a sbin/ifconfig/ifconfig.c
--- a/sbin/ifconfig/ifconfig.c  Fri Jun 30 16:27:37 2000 +0000
+++ b/sbin/ifconfig/ifconfig.c  Fri Jun 30 18:12:46 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ifconfig.c,v 1.79 2000/04/20 02:08:55 enami Exp $      */
+/*     $NetBSD: ifconfig.c,v 1.79.4.1 2000/06/30 18:12:46 thorpej Exp $        */
 
 /*-
  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
@@ -80,7 +80,7 @@
 #if 0
 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
 #else
-__RCSID("$NetBSD: ifconfig.c,v 1.79 2000/04/20 02:08:55 enami Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.79.4.1 2000/06/30 18:12:46 thorpej Exp $");
 #endif
 #endif /* not lint */
 
@@ -149,33 +149,35 @@
 int    reset_if_flags;
 int    explicit_prefix = 0;
 
-void   notealias __P((char *, int));
-void   notrailers __P((char *, int));
-void   setifaddr __P((char *, int));
-void   setifdstaddr __P((char *, int));
-void   setifflags __P((char *, int));
-void   setifbroadaddr __P((char *, int));
-void   setifipdst __P((char *, int));
-void   setifmetric __P((char *, int));
-void   setifmtu __P((char *, int));
-void   setifnwid __P((char *, int));
-void   setifnetmask __P((char *, int));
-void   setifprefixlen __P((char *, int));
-void   setnsellength __P((char *, int));
-void   setsnpaoffset __P((char *, int));
-void   setatrange __P((char *, int));
-void   setatphase __P((char *, int));
+void   notealias __P((const char *, int));
+void   notrailers __P((const char *, int));
+void   setifaddr __P((const char *, int));
+void   setifdstaddr __P((const char *, int));
+void   setifflags __P((const char *, int));
+void   setifbroadaddr __P((const char *, int));
+void   setifipdst __P((const char *, int));
+void   setifmetric __P((const char *, int));
+void   setifmtu __P((const char *, int));
+void   setifnwid __P((const char *, int));
+void   setifnetmask __P((const char *, int));
+void   setifprefixlen __P((const char *, int));
+void   setnsellength __P((const char *, int));
+void   setsnpaoffset __P((const char *, int));
+void   setatrange __P((const char *, int));
+void   setatphase __P((const char *, int));
+void   settunnel __P((const char *, const char *));
+void   deletetunnel __P((const char *, int));
 #ifdef INET6
-void   setia6flags __P((char *, int));
-void   setia6pltime __P((char *, int));
-void   setia6vltime __P((char *, int));
-void   setia6lifetime __P((char *, char *));
+void   setia6flags __P((const char *, int));
+void   setia6pltime __P((const char *, int));
+void   setia6vltime __P((const char *, int));
+void   setia6lifetime __P((const char *, const char *));
 #endif
 void   checkatrange __P ((struct sockaddr_at *));
-void   setmedia __P((char *, int));
-void   setmediaopt __P((char *, int));
-void   unsetmediaopt __P((char *, int));
-void   setmediainst __P((char *, int));
+void   setmedia __P((const char *, int));
+void   setmediaopt __P((const char *, int));
+void   unsetmediaopt __P((const char *, int));
+void   setmediainst __P((const char *, int));
 void   fixnsel __P((struct sockaddr_iso *));
 int    main __P((int, char *[]));
 
@@ -199,12 +201,14 @@
 #define        A_MEDIAINST     0x0008          /* instance or inst command */
 
 #define        NEXTARG         0xffffff
+#define        NEXTARG2        0xfffffe
 
-struct cmd {
-       char    *c_name;
-       int     c_parameter;            /* NEXTARG means next argv */
+const struct cmd {
+       const char *c_name;
+       int     c_parameter;    /* NEXTARG means next argv */
        int     c_action;       /* defered action */
-       void    (*c_func) __P((char *, int));
+       void    (*c_func) __P((const char *, int));
+       void    (*c_func2) __P((const char *, const char *));
 } cmds[] = {
        { "up",         IFF_UP,         0,              setifflags } ,
        { "down",       -IFF_UP,        0,              setifflags },
@@ -243,6 +247,9 @@
        { "snpaoffset", NEXTARG,        0,              setsnpaoffset },
        { "nsellength", NEXTARG,        0,              setnsellength },
 #endif /* INET_ONLY */
+       { "tunnel",     NEXTARG2,       0,              NULL,
+                                                       settunnel } ,
+       { "deletetunnel", 0,            0,              deletetunnel },
        { "link0",      IFF_LINK0,      0,              setifflags } ,
        { "-link0",     -IFF_LINK0,     0,              setifflags } ,
        { "link1",      IFF_LINK1,      0,              setifflags } ,
@@ -263,7 +270,7 @@
 int    carrier __P((void));
 void   getsock __P((int));
 void   printall __P((void));
-void   printb __P((char *, unsigned short, char *));
+void   printb __P((const char *, unsigned short, const char *));
 int    prefix __P((void *, int));
 void   status __P((const u_int8_t *, int));
 void   usage __P((void));
@@ -285,29 +292,31 @@
  */
 void   in_alias __P((struct ifreq *));
 void   in_status __P((int));
-void   in_getaddr __P((char *, int));
+void   in_getaddr __P((const char *, int));
 #ifdef INET6
 void   in6_fillscopeid __P((struct sockaddr_in6 *sin6));
 void   in6_alias __P((struct in6_ifreq *));
 void   in6_status __P((int));
-void   in6_getaddr __P((char *, int));
-void   in6_getprefix __P((char *, int));
+void   in6_getaddr __P((const char *, int));
+void   in6_getprefix __P((const char *, int));
 #endif
 void   at_status __P((int));
-void   at_getaddr __P((char *, int));
+void   at_getaddr __P((const char *, int));
 void   xns_status __P((int));
-void   xns_getaddr __P((char *, int));
+void   xns_getaddr __P((const char *, int));
 void   iso_status __P((int));
-void   iso_getaddr __P((char *, int));
+void   iso_getaddr __P((const char *, int));
+
 void   ieee80211_status __P((void));
+void   tunnel_status __P((void));
 
 /* Known address families */
 struct afswtch {
-       char *af_name;
+       const char *af_name;
        short af_af;
        void (*af_status) __P((int));
-       void (*af_getaddr) __P((char *, int));
-       void (*af_getprefix) __P((char *, int));
+       void (*af_getaddr) __P((const char *, int));
+       void (*af_getprefix) __P((const char *, int));
        u_long af_difaddr;
        u_long af_aifaddr;
        u_long af_gifaddr;
@@ -487,7 +496,7 @@
 
        /* Process commands. */
        while (argc > 0) {
-               struct cmd *p;
+               const struct cmd *p;
 
                for (p = cmds; p->c_name; p++)
                        if (strcmp(argv[0], p->c_name) == 0)
@@ -499,13 +508,19 @@
                        }
                        p++;    /* got src, do dst */
                }
-               if (p->c_func) {
+               if (p->c_func != NULL || p->c_func2 != NULL) {
                        if (p->c_parameter == NEXTARG) {
                                if (argc < 2)
                                        errx(1, "'%s' requires argument",
                                            p->c_name);
                                (*p->c_func)(argv[1], 0);
                                argc--, argv++;
+                       } else if (p->c_parameter == NEXTARG2) {
+                               if (argc < 3)
+                                       errx(1, "'%s' requires 2 arguments",
+                                           p->c_name);
+                               (*p->c_func2)(argv[1], argv[2]);
+                               argc -= 2, argv += 2;
                        } else
                                (*p->c_func)(argv[0], p->c_parameter);
                        actions |= p->c_action;
@@ -766,7 +781,7 @@
 /*ARGSUSED*/
 void
 setifaddr(addr, param)
-       char *addr;
+       const char *addr;
        int param;
 {
        struct ifreq *ifr;              /* XXX */
@@ -796,8 +811,80 @@
 }
 
 void
+settunnel(src, dst)
+       const char *src, *dst;
+{
+       struct addrinfo hints, *srcres, *dstres;
+       struct ifaliasreq addreq;
+       int ecode;
+#ifdef INET6
+       struct in6_aliasreq in6_addreq; 
+#endif
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = afp->af_af;
+
+       if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
+               errx(1, "error in parsing address string: %s",
+                   gai_strerror(ecode));
+
+       if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)  
+               errx(1, "error in parsing address string: %s",
+                   gai_strerror(ecode));
+
+       if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
+               errx(1,
+                   "source and destination address families do not match");
+
+       switch (srcres->ai_addr->sa_family) {
+       case AF_INET:
+               memset(&addreq, 0, sizeof(addreq));
+               strncpy(addreq.ifra_name, name, IFNAMSIZ);
+               memcpy(&addreq.ifra_addr, srcres->ai_addr,
+                   srcres->ai_addr->sa_len);
+               memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
+                   dstres->ai_addr->sa_len);
+
+               if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
+                       warn("SIOCSIFPHYADDR");
+               break;
+
+#ifdef INET6
+       case AF_INET6:
+               memset(&in6_addreq, 0, sizeof(in6_addreq));
+               strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
+               memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
+                   srcres->ai_addr->sa_len);
+               memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
+                   dstres->ai_addr->sa_len);
+
+               if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
+                       warn("SIOCSIFPHYADDR_IN6");
+               break;
+#endif /* INET6 */
+
+       default:
+               warn("address family not supported");
+       }
+
+       freeaddrinfo(srcres);
+       freeaddrinfo(dstres);
+}
+
+/* ARGSUSED */
+void
+deletetunnel(vname, param)
+       const char *vname;
+       int param;
+{
+
+       if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
+               err(1, "SIOCDIFPHYADDR");
+}
+



Home | Main Index | Thread Index | Old Index