BSD 4_4 release
[unix-history] / usr / src / sbin / ifconfig / ifconfig.c
index ea6056b..36560b9 100644 (file)
@@ -1,53 +1,71 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1983 Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1983, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ifconfig.c 4.24 (Berkeley) %G%";
+static char sccsid[] = "@(#)ifconfig.c 8.1 (Berkeley) 6/5/93";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 
 #define        NSIP
 #include <netns/ns.h>
 #include <netns/ns_if.h>
 
 #define        NSIP
 #include <netns/ns.h>
 #include <netns/ns_if.h>
+#include <netdb.h>
 
 #define EON
 #include <netiso/iso.h>
 #include <netiso/iso_var.h>
 #include <sys/protosw.h>
 
 #define EON
 #include <netiso/iso.h>
 #include <netiso/iso_var.h>
 #include <sys/protosw.h>
-#include <netiso/eonvar.h>
 
 
+#include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
-#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
 
 
-extern int errno;
 struct ifreq           ifr, ridreq;
 struct ifaliasreq      addreq;
 struct iso_ifreq       iso_ridreq;
 struct ifreq           ifr, ridreq;
 struct ifaliasreq      addreq;
 struct iso_ifreq       iso_ridreq;
@@ -57,6 +75,7 @@ struct        sockaddr_in     netmask;
 char   name[30];
 int    flags;
 int    metric;
 char   name[30];
 int    flags;
 int    metric;
+int    nsellength = 1;
 int    setaddr;
 int    setipdst;
 int    doalias;
 int    setaddr;
 int    setipdst;
 int    doalias;
@@ -67,7 +86,7 @@ extern        int errno;
 
 int    setifflags(), setifaddr(), setifdstaddr(), setifnetmask();
 int    setifmetric(), setifbroadaddr(), setifipdst();
 
 int    setifflags(), setifaddr(), setifdstaddr(), setifnetmask();
 int    setifmetric(), setifbroadaddr(), setifipdst();
-int    notealias(), setsnpaoffset();
+int    notealias(), setsnpaoffset(), setnsellength(), notrailers();
 
 #define        NEXTARG         0xffffff
 
 
 #define        NEXTARG         0xffffff
 
@@ -78,8 +97,8 @@ struct        cmd {
 } cmds[] = {
        { "up",         IFF_UP,         setifflags } ,
        { "down",       -IFF_UP,        setifflags },
 } cmds[] = {
        { "up",         IFF_UP,         setifflags } ,
        { "down",       -IFF_UP,        setifflags },
-       { "trailers",   -IFF_NOTRAILERS,setifflags },
-       { "-trailers",  IFF_NOTRAILERS, setifflags },
+       { "trailers",   -1,             notrailers },
+       { "-trailers",  1,              notrailers },
        { "arp",        -IFF_NOARP,     setifflags },
        { "-arp",       IFF_NOARP,      setifflags },
        { "debug",      IFF_DEBUG,      setifflags },
        { "arp",        -IFF_NOARP,     setifflags },
        { "-arp",       IFF_NOARP,      setifflags },
        { "debug",      IFF_DEBUG,      setifflags },
@@ -94,9 +113,16 @@ struct      cmd {
 #endif
        { "netmask",    NEXTARG,        setifnetmask },
        { "metric",     NEXTARG,        setifmetric },
 #endif
        { "netmask",    NEXTARG,        setifnetmask },
        { "metric",     NEXTARG,        setifmetric },
-       { "snpaoffset", NEXTARG,        setsnpaoffset },
        { "broadcast",  NEXTARG,        setifbroadaddr },
        { "ipdst",      NEXTARG,        setifipdst },
        { "broadcast",  NEXTARG,        setifbroadaddr },
        { "ipdst",      NEXTARG,        setifipdst },
+       { "snpaoffset", NEXTARG,        setsnpaoffset },
+       { "nsellength", NEXTARG,        setnsellength },
+       { "link0",      IFF_LINK0,      setifflags } ,
+       { "-link0",     -IFF_LINK0,     setifflags } ,
+       { "link1",      IFF_LINK1,      setifflags } ,
+       { "-link1",     -IFF_LINK1,     setifflags } ,
+       { "link2",      IFF_LINK2,      setifflags } ,
+       { "-link2",     -IFF_LINK2,     setifflags } ,
        { 0,            0,              setifaddr },
        { 0,            0,              setifdstaddr },
 };
        { 0,            0,              setifaddr },
        { 0,            0,              setifdstaddr },
 };
@@ -133,17 +159,6 @@ struct afswtch {
 
 struct afswtch *afp;   /*the address family being set or asked about*/
 
 
 struct afswtch *afp;   /*the address family being set or asked about*/
 
-int testing = 0;
-Ioctl(a,b,c) {
-       int error = 0;
-       if (testing)
-               printf("would call ioctl with %x, %x, %x\n", a, b, c);
-       else
-               error = ioctl(a, b, c);
-       return error;
-}
-#define ioctl(a, b, c) Ioctl(a,b,c)
-
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -156,8 +171,8 @@ main(argc, argv)
                    "\t[ af [ address [ dest_addr ] ] [ up ] [ down ]",
                            "[ netmask mask ] ]\n",
                    "\t[ metric n ]\n",
                    "\t[ af [ address [ dest_addr ] ] [ up ] [ down ]",
                            "[ netmask mask ] ]\n",
                    "\t[ metric n ]\n",
-                   "\t[ trailers | -trailers ]\n",
-                   "\t[ arp | -arp ]\n");
+                   "\t[ arp | -arp ]\n",
+                   "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ] \n");
                exit(1);
        }
        argc--, argv++;
                exit(1);
        }
        argc--, argv++;
@@ -209,6 +224,8 @@ main(argc, argv)
                }
                argc--, argv++;
        }
                }
                argc--, argv++;
        }
+       if (af == AF_ISO)
+               adjust_nsellength();
        if (setipdst && af==AF_NS) {
                struct nsip_req rq;
                int size = sizeof(rq);
        if (setipdst && af==AF_NS) {
                struct nsip_req rq;
                int size = sizeof(rq);
@@ -219,15 +236,6 @@ main(argc, argv)
                if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
                        Perror("Encapsulation Routing");
        }
                if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
                        Perror("Encapsulation Routing");
        }
-       if (setipdst && af == AF_ISO) {
-               iso_ridreq.ifr_Addr = iso_addreq.ifra_addr;
-               iso_ridreq.ifr_Addr.siso_ssuffixlen =
-                       ((struct sockaddr_in *)&addreq.ifra_dstaddr)->
-                               sin_addr.s_addr;
-               strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
-               if (ioctl(s, SIOCSEONCORE, &iso_ridreq) < 0)
-                       Perror("Iso Encapsulation Routing");
-       }
        if (clearaddr) {
                int ret;
                strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
        if (clearaddr) {
                int ret;
                strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
@@ -261,7 +269,7 @@ setifaddr(addr, param)
         * and the flags may change when the address is set.
         */
        setaddr++;
         * and the flags may change when the address is set.
         */
        setaddr++;
-       if (doalias = 0)
+       if (doalias == 0)
                clearaddr = 1;
        (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
 }
                clearaddr = 1;
        (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
 }
@@ -303,6 +311,14 @@ notealias(addr, param)
                clearaddr = 0;
 }
 
                clearaddr = 0;
 }
 
+/*ARGSUSED*/
+notrailers(vname, value)
+       char *vname;
+       int value;
+{
+       printf("Note: trailers are no longer sent, but always received\n");
+}
+
 /*ARGSUSED*/
 setifdstaddr(addr, param)
        char *addr;
 /*ARGSUSED*/
 setifdstaddr(addr, param)
        char *addr;
@@ -349,7 +365,7 @@ setsnpaoffset(val)
 
 #define        IFFBITS \
 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
 
 #define        IFFBITS \
 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
-"
+\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
 
 /*
  * Print the status of the interface.  If an address family was
 
 /*
  * Print the status of the interface.  If an address family was
@@ -478,33 +494,37 @@ iso_status(force)
                perror("ifconfig: socket");
                exit(1);
        }
                perror("ifconfig: socket");
                exit(1);
        }
-       if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
+       bzero((caddr_t)&ifr, sizeof(ifr));
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+       if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
                if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
                        if (!force)
                                return;
                        bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
                        if (!force)
                                return;
                        bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
-               } else
-                       perror("ioctl (SIOCGIFADDR)");
+               } else {
+                       perror("ioctl (SIOCGIFADDR_ISO)");
+                       exit(1);
+               }
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        siso = &ifr.ifr_Addr;
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        siso = &ifr.ifr_Addr;
-       printf("\tiso %s ", iso_ntoa(siso->siso_addr));
-       if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
+       printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
+       if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
                if (errno != EADDRNOTAVAIL)
                if (errno != EADDRNOTAVAIL)
-                       perror("ioctl (SIOCGIFNETMASK)");
+                       perror("ioctl (SIOCGIFNETMASK_ISO)");
        } else {
        } else {
-               printf("\n netmask %s ", iso_ntoa(siso->siso_addr));
+               printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
        }
        if (flags & IFF_POINTOPOINT) {
        }
        if (flags & IFF_POINTOPOINT) {
-               if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
+               if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                        else
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                        else
-                           Perror("ioctl (SIOCGIFDSTADDR)");
+                           Perror("ioctl (SIOCGIFDSTADDR_ISO)");
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                siso = &ifr.ifr_Addr;
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                siso = &ifr.ifr_Addr;
-               printf("--> %s ", iso_ntoa(siso->siso_addr));
+               printf("--> %s ", iso_ntoa(&siso->siso_addr));
        }
        putchar('\n');
 }
        }
        putchar('\n');
 }
@@ -546,31 +566,20 @@ in_getaddr(s, which)
        struct netent *np;
        int val;
 
        struct netent *np;
        int val;
 
-       if (which == MASK)
-               sin->sin_len = 8;
-       else {
+       sin->sin_len = sizeof(*sin);
+       if (which != MASK)
                sin->sin_family = AF_INET;
                sin->sin_family = AF_INET;
-               sin->sin_len = sizeof(*sin);
-       }
-       val = inet_addr(s);
-       if (val != -1) {
+
+       if ((val = inet_addr(s)) != -1)
                sin->sin_addr.s_addr = val;
                sin->sin_addr.s_addr = val;
-               return;
-       }
-       hp = gethostbyname(s);
-       if (hp) {
-               sin->sin_family = hp->h_addrtype;
+       else if (hp = gethostbyname(s))
                bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
                bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
-               return;
-       }
-       np = getnetbyname(s);
-       if (np) {
-               sin->sin_family = np->n_addrtype;
+       else if (np = getnetbyname(s))
                sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
                sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
-               return;
+       else {
+               fprintf(stderr, "%s: bad value\n", s);
+               exit(1);
        }
        }
-       fprintf(stderr, "%s: bad value\n", s);
-       exit(1);
 }
 
 /*
 }
 
 /*
@@ -632,14 +641,44 @@ SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
 iso_getaddr(addr, which)
 char *addr;
 {
 iso_getaddr(addr, which)
 char *addr;
 {
-       struct sockaddr_iso *siso = sisotab[which];
+       register struct sockaddr_iso *siso = sisotab[which];
        struct iso_addr *iso_addr();
        siso->siso_addr = *iso_addr(addr);
        struct iso_addr *iso_addr();
        siso->siso_addr = *iso_addr(addr);
+
        if (which == MASK) {
        if (which == MASK) {
-               siso->siso_len = siso->siso_nlen + 5;
+               siso->siso_len = TSEL(siso) - (caddr_t)(siso);
                siso->siso_nlen = 0;
        } else {
                siso->siso_nlen = 0;
        } else {
-           siso->siso_family = AF_ISO;
-           siso->siso_len =  sizeof(*siso);
+               siso->siso_len = sizeof(*siso);
+               siso->siso_family = AF_ISO;
        }
 }
        }
 }
+
+setnsellength(val)
+       char *val;
+{
+       nsellength = atoi(val);
+       if (nsellength < 0) {
+               fprintf(stderr, "Negative NSEL length is absurd\n");
+               exit (1);
+       }
+       if (afp == 0 || afp->af_af != AF_ISO) {
+               fprintf(stderr, "Setting NSEL length valid only for iso\n");
+               exit (1);
+       }
+}
+
+fixnsel(s)
+register struct sockaddr_iso *s;
+{
+       if (s->siso_family == 0)
+               return;
+       s->siso_tlen = nsellength;
+}
+
+adjust_nsellength()
+{
+       fixnsel(sisotab[RIDADDR]);
+       fixnsel(sisotab[ADDR]);
+       fixnsel(sisotab[DSTADDR]);
+}