BSD 4_3_Net_2 release
[unix-history] / usr / src / sbin / route / route.c
index eb4e410..ec040c8 100644 (file)
@@ -2,7 +2,33 @@
  * Copyright (c) 1983, 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983, 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * 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
@@ -12,13 +38,13 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)route.c    5.32 (Berkeley) %G%";
+static char sccsid[] = "@(#)route.c    5.35 (Berkeley) 6/27/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
-#include <sys/file.h>
 #include <sys/mbuf.h>
 #include <sys/kinfo.h>
 
 #include <sys/mbuf.h>
 #include <sys/kinfo.h>
 
@@ -27,11 +53,14 @@ static char sccsid[] = "@(#)route.c 5.32 (Berkeley) %G%";
 #include <netinet/in.h>
 #include <netns/ns.h>
 #include <netiso/iso.h>
 #include <netinet/in.h>
 #include <netns/ns.h>
 #include <netiso/iso.h>
-
+#include <netccitt/x25.h>
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <netdb.h>
+
 #include <errno.h>
 #include <errno.h>
-#include <ctype.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <paths.h>
 #include <stdlib.h>
 #include <string.h>
 #include <paths.h>
@@ -51,12 +80,16 @@ union       sockunion {
        struct  sockaddr_ns sns;
        struct  sockaddr_iso siso;
        struct  sockaddr_dl sdl;
        struct  sockaddr_ns sns;
        struct  sockaddr_iso siso;
        struct  sockaddr_dl sdl;
-} so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp, *so_addrs[] =
-{ &so_dst, &so_gate, &so_mask, &so_genmask, &so_ifp, &so_ifa, 0}; 
+       struct  sockaddr_x25 sx25;
+} so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
+
+union sockunion *so_addrs[] =
+       { &so_dst, &so_gate, &so_mask, &so_genmask, &so_ifp, &so_ifa, 0}; 
+
 typedef union sockunion *sup;
 int    pid, rtm_addrs, uid;
 int    s;
 typedef union sockunion *sup;
 int    pid, rtm_addrs, uid;
 int    s;
-int    forcehost, forcenet, doflush, nflag, af, qflag, Cflag, keyword();
+int    forcehost, forcenet, doflush, nflag, af, qflag, tflag, Cflag, keyword();
 int    iflag, verbose, aflen = sizeof (struct sockaddr_in);
 int    locking, lockrest, debugonly;
 struct sockaddr_in sin = { sizeof(sin), AF_INET };
 int    iflag, verbose, aflen = sizeof (struct sockaddr_in);
 int    locking, lockrest, debugonly;
 struct sockaddr_in sin = { sizeof(sin), AF_INET };
@@ -64,7 +97,7 @@ struct        rt_metrics rt_metrics;
 u_long  rtm_inits;
 struct in_addr inet_makeaddr();
 char   *routename(), *netname();
 u_long  rtm_inits;
 struct in_addr inet_makeaddr();
 char   *routename(), *netname();
-void   flushroutes(), newroute(), monitor();
+void   flushroutes(), newroute(), monitor(), sockaddr();
 void   print_getmsg(), print_rtmsg(), pmsg_common(), sodump(), bprintf();
 int    getaddr(), rtmsg();
 extern char *inet_ntoa(), *iso_ntoa(), *link_ntoa();
 void   print_getmsg(), print_rtmsg(), pmsg_common(), sodump(), bprintf();
 int    getaddr(), rtmsg();
 extern char *inet_ntoa(), *iso_ntoa(), *link_ntoa();
@@ -110,7 +143,7 @@ main(argc, argv)
        if (argc < 2)
                usage((char *)NULL);
 
        if (argc < 2)
                usage((char *)NULL);
 
-       while ((ch = getopt(argc, argv, "Cnqv")) != EOF)
+       while ((ch = getopt(argc, argv, "Cnqtv")) != EOF)
                switch(ch) {
                case 'C':
                        Cflag = 1;      /* Use old ioctls. */
                switch(ch) {
                case 'C':
                        Cflag = 1;      /* Use old ioctls. */
@@ -124,6 +157,9 @@ main(argc, argv)
                case 'v':
                        verbose = 1;
                        break;
                case 'v':
                        verbose = 1;
                        break;
+               case 't':
+                       tflag = 1;
+                       break;
                case '?':
                default:
                        usage();
                case '?':
                default:
                        usage();
@@ -133,28 +169,39 @@ main(argc, argv)
 
        pid = getpid();
        uid = getuid();
 
        pid = getpid();
        uid = getuid();
-       if (Cflag)
+       if (tflag)
+               s = open("/dev/null", O_WRONLY, 0);
+       else if (Cflag)
                s = socket(AF_INET, SOCK_RAW, 0);
        else
                s = socket(PF_ROUTE, SOCK_RAW, 0);
        if (s < 0)
                quit("socket");
        if (*argv)
                s = socket(AF_INET, SOCK_RAW, 0);
        else
                s = socket(PF_ROUTE, SOCK_RAW, 0);
        if (s < 0)
                quit("socket");
        if (*argv)
-               switch(keyword(*argv)) {
+               switch (keyword(*argv)) {
                case K_GET:
                        uid = 0;
                        /* FALLTHROUGH */
                case K_GET:
                        uid = 0;
                        /* FALLTHROUGH */
+
                case K_CHANGE:
                        if (Cflag)
                                usage("change or get with -C");
                        /* FALLTHROUGH */
                case K_CHANGE:
                        if (Cflag)
                                usage("change or get with -C");
                        /* FALLTHROUGH */
+
                case K_ADD:
                case K_DELETE:
                        newroute(argc, argv);
                case K_ADD:
                case K_DELETE:
                        newroute(argc, argv);
+                       exit(0);
+                       /* NOTREACHED */
+
                case K_MONITOR:
                        monitor();
                case K_MONITOR:
                        monitor();
+                       /* NOTREACHED */
+
                case K_FLUSH:
                        flushroutes(argc, argv);
                case K_FLUSH:
                        flushroutes(argc, argv);
+                       exit(0);
+                       /* NOTREACHED */
                }
        usage(*argv);
        /* NOTREACHED */
                }
        usage(*argv);
        /* NOTREACHED */
@@ -174,18 +221,31 @@ flushroutes(argc, argv)
        register struct rt_msghdr *rtm;
 
        if (uid)
        register struct rt_msghdr *rtm;
 
        if (uid)
-               usage("must be root to alter routing table");
+               quit("must be root to alter routing table");
        shutdown(s, 0); /* Don't want to read back our messages */
        if (argc > 1) {
                argv++;
        shutdown(s, 0); /* Don't want to read back our messages */
        if (argc > 1) {
                argv++;
-               if (argc == 2 && **argv == '-') switch (keyword(1 + *argv)) {
-                       case K_INET:    af = AF_INET;   break;
-                       case K_XNS:     af = AF_NS;     break;
-                       case K_LINK:    af = AF_LINK;   break;
-                       case K_ISO: case K_OSI: af = AF_ISO; break;
-                       default: goto bad;
+               if (argc == 2 && **argv == '-')
+                   switch (keyword(*argv + 1)) {
+                       case K_INET:
+                               af = AF_INET;
+                               break;
+                       case K_XNS:
+                               af = AF_NS;
+                               break;
+                       case K_LINK:
+                               af = AF_LINK;
+                               break;
+                       case K_ISO:
+                       case K_OSI:
+                               af = AF_ISO;
+                               break;
+                       case K_X25:
+                               af = AF_CCITT;
+                       default:
+                               goto bad;
                } else
                } else
-                       bad: usage(*argv);
+bad:                   usage(*argv);
        }
        if ((needed = getkerninfo(KINFO_RT_DUMP, 0, 0, 0)) < 0)
                quit("route-getkerninfo-estimate");
        }
        if ((needed = getkerninfo(KINFO_RT_DUMP, 0, 0, 0)) < 0)
                quit("route-getkerninfo-estimate");
@@ -201,6 +261,7 @@ flushroutes(argc, argv)
                        continue;
                if (af) {
                        struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
                        continue;
                if (af) {
                        struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
+
                        if (sa->sa_family != af)
                                continue;
                }
                        if (sa->sa_family != af)
                                continue;
                }
@@ -228,8 +289,6 @@ flushroutes(argc, argv)
                        (void) printf("done\n");
                }
        }
                        (void) printf("done\n");
                }
        }
-       exit(0);
-       /* NOTREACHED */
 }
        
 char *
 }
        
 char *
@@ -261,7 +320,7 @@ routename(sa)
                if (in.s_addr == INADDR_ANY)
                        cp = "default";
                if (cp == 0 && !nflag) {
                if (in.s_addr == INADDR_ANY)
                        cp = "default";
                if (cp == 0 && !nflag) {
-                       hp = gethostbyaddr(&in, sizeof (struct in_addr),
+                       hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
                                AF_INET);
                        if (hp) {
                                if ((cp = index(hp->h_name, '.')) &&
                                AF_INET);
                        if (hp) {
                                if ((cp = index(hp->h_name, '.')) &&
@@ -436,7 +495,7 @@ newroute(argc, argv)
        struct hostent *hp = 0;
 
        if (uid)
        struct hostent *hp = 0;
 
        if (uid)
-               usage("must be root to alter routing table");
+               quit("must be root to alter routing table");
        cmd = argv[0];
        if (*cmd != 'g')
                shutdown(s, 0); /* Don't want to read back our messages */
        cmd = argv[0];
        if (*cmd != 'g')
                shutdown(s, 0); /* Don't want to read back our messages */
@@ -456,6 +515,14 @@ newroute(argc, argv)
                                af = AF_INET;
                                aflen = sizeof(struct sockaddr_in);
                                break;
                                af = AF_INET;
                                aflen = sizeof(struct sockaddr_in);
                                break;
+                       case K_X25:
+                               af = AF_CCITT;
+                               aflen = sizeof(struct sockaddr_x25);
+                               break;
+                       case K_SA:
+                               af = 0;
+                               aflen = sizeof(union sockunion);
+                               break;
                        case K_XNS:
                                af = AF_NS;
                                aflen = sizeof(struct sockaddr_ns);
                        case K_XNS:
                                af = AF_NS;
                                aflen = sizeof(struct sockaddr_ns);
@@ -476,6 +543,12 @@ newroute(argc, argv)
                        case K_REJECT:
                                flags |= RTF_REJECT;
                                break;
                        case K_REJECT:
                                flags |= RTF_REJECT;
                                break;
+                       case K_PROTO1:
+                               flags |= RTF_PROTO1;
+                               break;
+                       case K_PROTO2:
+                               flags |= RTF_PROTO2;
+                               break;
                        case K_CLONING:
                                flags |= RTF_CLONING;
                                break;
                        case K_CLONING:
                                flags |= RTF_CLONING;
                                break;
@@ -606,8 +679,6 @@ newroute(argc, argv)
                }
                (void) printf(": %s\n", err);
        }
                }
                (void) printf(": %s\n", err);
        }
-       exit(0);
-       /* NOTREACHED */
 }
 
 void
 }
 
 void
@@ -702,6 +773,10 @@ getaddr(which, s, hpp)
                goto do_osi;
        if (af == AF_LINK)
                goto do_link;
                goto do_osi;
        if (af == AF_LINK)
                goto do_link;
+       if (af == AF_CCITT)
+               goto do_ccitt;
+       if (af == 0)
+               goto do_sa;
        if (hpp == NULL)
                hpp = &hp;
        *hpp = NULL;
        if (hpp == NULL)
                hpp = &hp;
        *hpp = NULL;
@@ -756,9 +831,16 @@ do_osi:
                su->siso.siso_len = 1 + cp - (char *)su;
        }
        return (1);
                su->siso.siso_len = 1 + cp - (char *)su;
        }
        return (1);
+do_ccitt:
+       ccitt_addr(s, &su->sx25);
+       return (1);
 do_link:
        link_addr(s, &su->sdl);
        return (1);
 do_link:
        link_addr(s, &su->sdl);
        return (1);
+do_sa:
+       su->sa.sa_len = sizeof(*su);
+       sockaddr(s, &su->sa);
+       return (1);
 }
 
 short ns_nullh[] = {0,0,0};
 }
 
 short ns_nullh[] = {0,0,0};
@@ -861,6 +943,8 @@ rtmsg(cmd, flags)
        rtm.rtm_rmx = rt_metrics;
        rtm.rtm_inits = rtm_inits;
 
        rtm.rtm_rmx = rt_metrics;
        rtm.rtm_inits = rtm_inits;
 
+       if (rtm_addrs & RTA_NETMASK)
+               mask_addr();
        NEXTADDR(RTA_DST, so_dst);
        NEXTADDR(RTA_GATEWAY, so_gate);
        NEXTADDR(RTA_NETMASK, so_mask);
        NEXTADDR(RTA_DST, so_dst);
        NEXTADDR(RTA_GATEWAY, so_gate);
        NEXTADDR(RTA_NETMASK, so_mask);
@@ -891,6 +975,31 @@ rtmsg(cmd, flags)
        return (0);
 }
 
        return (0);
 }
 
+mask_addr() {
+       register char *cp1, *cp2;
+       int olen;
+
+       if ((rtm_addrs & RTA_DST) == 0)
+               return;
+       switch(so_dst.sa.sa_family) {
+       case AF_NS: case AF_INET: case 0:
+               return;
+       case AF_ISO:
+               olen = MIN(so_dst.siso.siso_nlen, so_mask.sa.sa_len - 6);
+       }
+       cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
+       cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
+       while (cp2 > cp1)
+               *--cp2 = 0;
+       cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
+       while (cp1 > so_dst.sa.sa_data)
+               *--cp1 &= *--cp2;
+       switch(so_dst.sa.sa_family) {
+       case AF_ISO:
+               so_dst.siso.siso_nlen = olen;
+       }
+}
+
 char *msgtypes[] = {
        "",
        "RTM_ADD: Add Route",
 char *msgtypes[] = {
        "",
        "RTM_ADD: Add Route",
@@ -909,7 +1018,7 @@ char *msgtypes[] = {
 char metricnames[] =
 "\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount\1mtu";
 char routeflags[] = 
 char metricnames[] =
 "\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount\1mtu";
 char routeflags[] = 
-"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT\011CLONING\012XRESOLVE";
+"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT\011CLONING\012XRESOLVE\013LLINFO\017PROTO2\020PROTO1";
 
 
 void
 
 
 void
@@ -947,16 +1056,17 @@ print_getmsg(rtm, msglen)
        }
        (void) printf("RTM_GET: errno %d, flags:", rtm->rtm_errno); 
        bprintf(stdout, rtm->rtm_flags, routeflags);
        }
        (void) printf("RTM_GET: errno %d, flags:", rtm->rtm_errno); 
        bprintf(stdout, rtm->rtm_flags, routeflags);
-       (void) printf("\nmetric values:\n\n");
-#define metric(f, e) printf("\t%s:\t%d\n", "f", rtm->rtm_rmx.e)
-       metric(RTV_RPIPE, rmx_recvpipe);
-       metric(RTV_SPIPE, rmx_sendpipe);
-       metric(RTV_SSTHRESH, rmx_ssthresh);
-       metric(RTV_RTT, rmx_rtt);
-       metric(RTV_RTTVAR, rmx_rttvar);
-       metric(RTV_HOPCOUNT, rmx_hopcount);
-       metric(RTV_MTU, rmx_mtu);
-       metric(RTV_EXPIRE, rmx_expire);
+       (void) printf("\nmetric values:\n  ");
+#define metric(f, e)\
+    printf("%s: %d%s", __STRING(f), rtm->rtm_rmx.__CONCAT(rmx_,f), e)
+       metric(recvpipe, ", ");
+       metric(sendpipe, ", ");
+       metric(ssthresh, ", ");
+       metric(rtt, "\n  ");
+       metric(rttvar, ", ");
+       metric(hopcount, ", ");
+       metric(mtu, ", ");
+       metric(expire, "\n");
 #undef metric
        pmsg_common(rtm);
 }
 #undef metric
        pmsg_common(rtm);
 }
@@ -1054,3 +1164,54 @@ sodump(su, which)
        }
        (void) fflush(stdout);
 }
        }
        (void) fflush(stdout);
 }
+/* States*/
+#define VIRGIN 0
+#define GOTONE 1
+#define GOTTWO 2
+/* Inputs */
+#define        DIGIT   (4*0)
+#define        END     (4*1)
+#define DELIM  (4*2)
+
+void
+sockaddr(addr, sa)
+register char *addr;
+register struct sockaddr *sa;
+{
+       register char *cp = (char *)sa;
+       int size = sa->sa_len;
+       char *cplim = cp + size;
+       register int byte = 0, state = VIRGIN, new;
+
+       bzero(cp, size);
+       do {
+               if ((*addr >= '0') && (*addr <= '9')) {
+                       new = *addr - '0';
+               } else if ((*addr >= 'a') && (*addr <= 'f')) {
+                       new = *addr - 'a' + 10;
+               } else if ((*addr >= 'A') && (*addr <= 'F')) {
+                       new = *addr - 'A' + 10;
+               } else if (*addr == 0) 
+                       state |= END;
+               else
+                       state |= DELIM;
+               addr++;
+               switch (state /* | INPUT */) {
+               case GOTTWO | DIGIT:
+                       *cp++ = byte; /*FALLTHROUGH*/
+               case VIRGIN | DIGIT:
+                       state = GOTONE; byte = new; continue;
+               case GOTONE | DIGIT:
+                       state = GOTTWO; byte = new + (byte << 4); continue;
+               default: /* | DELIM */
+                       state = VIRGIN; *cp++ = byte; byte = 0; continue;
+               case GOTONE | END:
+               case GOTTWO | END:
+                       *cp++ = byte; /* FALLTHROUGH */
+               case VIRGIN | END:
+                       break;
+               }
+               break;
+       } while (cp < cplim); 
+       sa->sa_len = cp - (char *)sa;
+}