Basic specfile taken from F10
[iproute2.git] / ip / iproute.c
index a43c09e..6a2ea05 100644 (file)
@@ -28,7 +28,6 @@
 #include <netinet/ip.h>
 #include <arpa/inet.h>
 #include <linux/in_route.h>
-#include <linux/ip_mp_alg.h>
 
 #include "rt_names.h"
 #include "utils.h"
 #endif
 
 
+static const char *mx_names[RTAX_MAX+1] = {
+       [RTAX_MTU]      = "mtu",
+       [RTAX_WINDOW]   = "window",
+       [RTAX_RTT]      = "rtt",
+       [RTAX_RTTVAR]   = "rttvar",
+       [RTAX_SSTHRESH] = "ssthresh",
+       [RTAX_CWND]     = "cwnd",
+       [RTAX_ADVMSS]   = "advmss",
+       [RTAX_REORDERING]="reordering",
+       [RTAX_HOPLIMIT] = "hoplimit",
+       [RTAX_INITCWND] = "initcwnd",
+       [RTAX_FEATURES] = "features",
+       [RTAX_RTO_MIN]  = "rto_min",
+};
 static void usage(void) __attribute__((noreturn));
 
 static void usage(void)
@@ -54,13 +67,13 @@ static void usage(void)
        fprintf(stderr, "NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n");
        fprintf(stderr, "             [ table TABLE_ID ] [ proto RTPROTO ]\n");
        fprintf(stderr, "             [ scope SCOPE ] [ metric METRIC ]\n");
-       fprintf(stderr, "             [ mpath MP_ALGO ]\n");
        fprintf(stderr, "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n");
        fprintf(stderr, "NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS\n");
        fprintf(stderr, "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]\n");
-       fprintf(stderr, "           [ rtt NUMBER ] [ rttvar NUMBER ]\n");
-       fprintf(stderr, "           [ window NUMBER] [ cwnd NUMBER ] [ ssthresh NUMBER ]\n");
-       fprintf(stderr, "           [ realms REALM ]\n");
+       fprintf(stderr, "           [ rtt TIME ] [ rttvar TIME ]\n");
+       fprintf(stderr, "           [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n");
+       fprintf(stderr, "           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n");
+       fprintf(stderr, "           [ rto_min TIME ]\n");
        fprintf(stderr, "TYPE := [ unicast | local | broadcast | multicast | throw |\n");
        fprintf(stderr, "          unreachable | prohibit | blackhole | nat ]\n");
        fprintf(stderr, "TABLE_ID := [ local | main | default | all | NUMBER ]\n");
@@ -69,6 +82,7 @@ static void usage(void)
        fprintf(stderr, "MP_ALGO := { rr | drr | random | wrandom }\n");
        fprintf(stderr, "NHFLAGS := [ onlink | pervasive ]\n");
        fprintf(stderr, "RTPROTO := [ kernel | boot | static | NUMBER ]\n");
+       fprintf(stderr, "TIME := NUMBER[s|ms|us|ns|j]\n");
        exit(-1);
 }
 
@@ -76,6 +90,7 @@ static void usage(void)
 static struct
 {
        int tb;
+       int cloned;
        int flushed;
        char *flushb;
        int flushp;
@@ -95,18 +110,10 @@ static struct
        inet_prefix msrc;
 } filter;
 
-static char *mp_alg_names[IP_MP_ALG_MAX+1] = {
-       [IP_MP_ALG_NONE] = "none",
-       [IP_MP_ALG_RR] = "rr",
-       [IP_MP_ALG_DRR] = "drr",
-       [IP_MP_ALG_RANDOM] = "random",
-       [IP_MP_ALG_WRANDOM] = "wrandom"
-};
-
 static int flush_update(void)
 {
-       if (rtnl_send(&rth, filter.flushb, filter.flushp) < 0) {
-               perror("Failed to send flush request\n");
+       if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
+               perror("Failed to send flush request");
                return -1;
        }
        filter.flushp = 0;
@@ -125,8 +132,10 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        inet_prefix prefsrc;
        inet_prefix via;
        int host_len = -1;
+       static int ip6_multiple_tables;
+       __u32 table;
        SPRINT_BUF(b1);
-       
+       static int hz;
 
        if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
                fprintf(stderr, "Not a route: %08x %08x %08x\n",
@@ -150,27 +159,36 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        else if (r->rtm_family == AF_IPX)
                host_len = 80;
 
-       if (r->rtm_family == AF_INET6) {
+       parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
+       table = rtm_get_table(r, tb);
+
+       if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN)
+               ip6_multiple_tables = 1;
+
+       if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
+               if (filter.cloned) {
+                       if (!(r->rtm_flags&RTM_F_CLONED))
+                               return 0;
+               }
                if (filter.tb) {
-                       if (filter.tb < 0) {
-                               if (!(r->rtm_flags&RTM_F_CLONED))
-                                       return 0;
-                       } else {
-                               if (r->rtm_flags&RTM_F_CLONED)
+                       if (!filter.cloned && r->rtm_flags&RTM_F_CLONED)
+                               return 0;
+                       if (filter.tb == RT_TABLE_LOCAL) {
+                               if (r->rtm_type != RTN_LOCAL)
                                        return 0;
-                               if (filter.tb == RT_TABLE_LOCAL) {
-                                       if (r->rtm_type != RTN_LOCAL)
-                                               return 0;
-                               } else if (filter.tb == RT_TABLE_MAIN) {
-                                       if (r->rtm_type == RTN_LOCAL)
-                                               return 0;
-                               } else {
+                       } else if (filter.tb == RT_TABLE_MAIN) {
+                               if (r->rtm_type == RTN_LOCAL)
                                        return 0;
-                               }
+                       } else {
+                               return 0;
                        }
                }
        } else {
-               if (filter.tb > 0 && filter.tb != r->rtm_table)
+               if (filter.cloned) {
+                       if (!(r->rtm_flags&RTM_F_CLONED))
+                               return 0;
+               }
+               if (filter.tb > 0 && filter.tb != table)
                        return 0;
        }
        if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
@@ -200,8 +218,6 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
                return 0;
 
-       parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
-
        memset(&dst, 0, sizeof(dst));
        dst.family = r->rtm_family;
        if (tb[RTA_DST])
@@ -262,7 +278,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                if ((oif^filter.oif)&filter.oifmask)
                        return 0;
        }
-       if (filter.flushb && 
+       if (filter.flushb &&
            r->rtm_family == AF_INET6 &&
            r->rtm_dst_len == 0 &&
            r->rtm_type == RTN_UNREACHABLE &&
@@ -335,16 +351,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
        }
 
-       if (tb[RTA_MP_ALGO]) {
-               __u32 mp_alg = *(__u32*) RTA_DATA(tb[RTA_MP_ALGO]);
-               if (mp_alg > IP_MP_ALG_NONE) {
-                       fprintf(fp, "mpath %s ",
-                           mp_alg < IP_MP_ALG_MAX ? mp_alg_names[mp_alg] : "unknown");
-               }
-       }
-
        if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
-               fprintf(fp, "via %s ", 
+               fprintf(fp, "via %s ",
                        format_host(r->rtm_family,
                                    RTA_PAYLOAD(tb[RTA_GATEWAY]),
                                    RTA_DATA(tb[RTA_GATEWAY]),
@@ -354,8 +362,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
 
        if (!(r->rtm_flags&RTM_F_CLONED)) {
-               if (r->rtm_table != RT_TABLE_MAIN && !filter.tb)
-                       fprintf(fp, " table %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
+               if (table != RT_TABLE_MAIN && !filter.tb)
+                       fprintf(fp, " table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
                if (r->rtm_protocol != RTPROT_BOOT && filter.protocolmask != -1)
                        fprintf(fp, " proto %s ", rtnl_rtprot_n2a(r->rtm_protocol, b1, sizeof(b1)));
                if (r->rtm_scope != RT_SCOPE_UNIVERSE && filter.scopemask != -1)
@@ -365,7 +373,7 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                /* Do not use format_host(). It is our local addr
                   and symbolic name will not be useful.
                 */
-               fprintf(fp, " src %s ", 
+               fprintf(fp, " src %s ",
                        rt_addr_n2a(r->rtm_family,
                                    RTA_PAYLOAD(tb[RTA_PREFSRC]),
                                    RTA_DATA(tb[RTA_PREFSRC]),
@@ -427,7 +435,6 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                        fprintf(fp, "%s%x> ", first ? "<" : "", flags);
                if (tb[RTA_CACHEINFO]) {
                        struct rta_cacheinfo *ci = RTA_DATA(tb[RTA_CACHEINFO]);
-                       static int hz;
                        if (!hz)
                                hz = get_user_hz();
                        if (ci->rta_expires != 0)
@@ -454,7 +461,6 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                if (tb[RTA_CACHEINFO])
                        ci = RTA_DATA(tb[RTA_CACHEINFO]);
                if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
-                       static int hz;
                        if (!hz)
                                hz = get_user_hz();
                        if (r->rtm_flags & RTM_F_CLONED)
@@ -486,42 +492,32 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                if (mxrta[RTAX_LOCK])
                        mxlock = *(unsigned*)RTA_DATA(mxrta[RTAX_LOCK]);
 
-               for (i=2; i<=RTAX_MAX; i++) {
-                       static char *mx_names[] = 
-                       {
-                               "mtu",
-                               "window",
-                               "rtt",
-                               "rttvar",
-                               "ssthresh",
-                               "cwnd",
-                               "advmss",
-                               "reordering",
-                       };
-                       static int hz;
+               for (i=2; i<= RTAX_MAX; i++) {
                        if (mxrta[i] == NULL)
                                continue;
                        if (!hz)
                                hz = get_hz();
-                       if (i-2 < sizeof(mx_names)/sizeof(char*))
-                               fprintf(fp, " %s", mx_names[i-2]);
+
+                       if (i < sizeof(mx_names)/sizeof(char*) && mx_names[i])
+                               fprintf(fp, " %s", mx_names[i]);
                        else
                                fprintf(fp, " metric %d", i);
                        if (mxlock & (1<<i))
                                fprintf(fp, " lock");
 
-                       if (i != RTAX_RTT && i != RTAX_RTTVAR)
+                       if (i != RTAX_RTT && i != RTAX_RTTVAR &&
+                           i != RTAX_RTO_MIN)
                                fprintf(fp, " %u", *(unsigned*)RTA_DATA(mxrta[i]));
                        else {
-                               unsigned val = *(unsigned*)RTA_DATA(mxrta[i]);
+                               unsigned long long val = *(unsigned*)RTA_DATA(mxrta[i]);
 
                                val *= 1000;
                                if (i == RTAX_RTT)
                                        val /= 8;
-                               else
+                               else if (i == RTAX_RTTVAR)
                                        val /= 4;
                                if (val >= hz)
-                                       fprintf(fp, " %ums", val/hz);
+                                       fprintf(fp, " %llums", val/hz);
                                else
                                        fprintf(fp, " %.2fms", (float)val/hz);
                        }
@@ -551,12 +547,24 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                        if (nh->rtnh_len > sizeof(*nh)) {
                                parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh), nh->rtnh_len - sizeof(*nh));
                                if (tb[RTA_GATEWAY]) {
-                                       fprintf(fp, " via %s ", 
+                                       fprintf(fp, " via %s ",
                                                format_host(r->rtm_family,
                                                            RTA_PAYLOAD(tb[RTA_GATEWAY]),
                                                            RTA_DATA(tb[RTA_GATEWAY]),
                                                            abuf, sizeof(abuf)));
                                }
+                               if (tb[RTA_FLOW]) {
+                                       __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
+                                       __u32 from = to>>16;
+                                       to &= 0xFFFF;
+                                       fprintf(fp, " realm%s ", from ? "s" : "");
+                                       if (from) {
+                                               fprintf(fp, "%s/",
+                                                       rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
+                                       }
+                                       fprintf(fp, "%s",
+                                               rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
+                               }
                        }
                        if (r->rtm_flags&RTM_F_CLONED && r->rtm_type == RTN_MULTICAST) {
                                fprintf(fp, " %s", ll_index_to_name(nh->rtnh_ifindex));
@@ -606,6 +614,13 @@ int parse_one_nh(struct rtattr *rta, struct rtnexthop *rtnh, int *argcp, char **
                        rtnh->rtnh_hops = w - 1;
                } else if (strcmp(*argv, "onlink") == 0) {
                        rtnh->rtnh_flags |= RTNH_F_ONLINK;
+               } else if (matches(*argv, "realms") == 0) {
+                       __u32 realm;
+                       NEXT_ARG();
+                       if (get_rt_realms(&realm, *argv))
+                               invarg("\"realm\" value is invalid\n", *argv);
+                       rta_addattr32(rta, 4096, RTA_FLOW, realm);
+                       rtnh->rtnh_len += sizeof(struct rtattr) + 4;
                } else
                        break;
        }
@@ -664,6 +679,7 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
        int table_ok = 0;
        int proto_ok = 0;
        int type_ok = 0;
+       int raw = 0;
 
        memset(&req, 0, sizeof(req));
 
@@ -771,9 +787,19 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                                mxlock |= (1<<RTAX_RTT);
                                NEXT_ARG();
                        }
-                       if (get_unsigned(&rtt, *argv, 0))
+                       if (get_jiffies(&rtt, *argv, 0, &raw))
                                invarg("\"rtt\" value is invalid\n", *argv);
-                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, rtt);
+                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT, 
+                               (raw) ? rtt : rtt * 8);
+               } else if (strcmp(*argv, "rto_min") == 0) {
+                       unsigned rto_min;
+                       NEXT_ARG();
+                       mxlock |= (1<<RTAX_RTO_MIN);
+                       if (get_jiffies(&rto_min, *argv, 0, &raw))
+                               invarg("\"rto_min\" value is invalid\n",
+                                      *argv);
+                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
+                                     rto_min);
                } else if (matches(*argv, "window") == 0) {
                        unsigned win;
                        NEXT_ARG();
@@ -794,6 +820,16 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                        if (get_unsigned(&win, *argv, 0))
                                invarg("\"cwnd\" value is invalid\n", *argv);
                        rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
+               } else if (matches(*argv, "initcwnd") == 0) {
+                       unsigned win;
+                       NEXT_ARG();
+                       if (strcmp(*argv, "lock") == 0) {
+                               mxlock |= (1<<RTAX_INITCWND);
+                               NEXT_ARG();
+                       }
+                       if (get_unsigned(&win, *argv, 0))
+                               invarg("\"initcwnd\" value is invalid\n", *argv);
+                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
                } else if (matches(*argv, "rttvar") == 0) {
                        unsigned win;
                        NEXT_ARG();
@@ -801,9 +837,10 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                                mxlock |= (1<<RTAX_RTTVAR);
                                NEXT_ARG();
                        }
-                       if (get_unsigned(&win, *argv, 0))
+                       if (get_jiffies(&win, *argv, 0, &raw))
                                invarg("\"rttvar\" value is invalid\n", *argv);
-                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR, win);
+                       rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
+                               (raw) ? win : win * 4);
                } else if (matches(*argv, "ssthresh") == 0) {
                        unsigned win;
                        NEXT_ARG();
@@ -840,24 +877,17 @@ int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
                        NEXT_ARG();
                        if (rtnl_rttable_a2n(&tid, *argv))
                                invarg("\"table\" value is invalid\n", *argv);
-                       req.r.rtm_table = tid;
+                       if (tid < 256)
+                               req.r.rtm_table = tid;
+                       else {
+                               req.r.rtm_table = RT_TABLE_UNSPEC;
+                               addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
+                       }
                        table_ok = 1;
                } else if (strcmp(*argv, "dev") == 0 ||
                           strcmp(*argv, "oif") == 0) {
                        NEXT_ARG();
                        d = *argv;
-               } else if (strcmp(*argv, "mpath") == 0 ||
-                          strcmp(*argv, "mp") == 0) {
-                       int i;
-                       __u32 mp_alg = IP_MP_ALG_NONE;
-
-                       NEXT_ARG();
-                       for (i = 1; i < ARRAY_SIZE(mp_alg_names); i++)
-                               if (strcmp(*argv, mp_alg_names[i]) == 0)
-                                       mp_alg = i;
-                       if (mp_alg == IP_MP_ALG_NONE)
-                               invarg("\"mpath\" value is invalid\n", *argv);
-                       addattr_l(&req.n, sizeof(req), RTA_MP_ALGO, &mp_alg, sizeof(mp_alg));
                } else {
                        int type;
                        inet_prefix dst;
@@ -980,7 +1010,7 @@ static int iproute_flush_cache(void)
        }
 
        len = strlen (buffer);
-               
+
        if ((write (flush_fd, (void *)buffer, len)) < len) {
                fprintf (stderr, "Cannot flush routing cache\n");
                return -1;
@@ -1010,19 +1040,19 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
                        NEXT_ARG();
                        if (rtnl_rttable_a2n(&tid, *argv)) {
                                if (strcmp(*argv, "all") == 0) {
-                                       tid = 0;
+                                       filter.tb = 0;
                                } else if (strcmp(*argv, "cache") == 0) {
-                                       tid = -1;
+                                       filter.cloned = 1;
                                } else if (strcmp(*argv, "help") == 0) {
                                        usage();
                                } else {
                                        invarg("table id value is invalid\n", *argv);
                                }
-                       }
-                       filter.tb = tid;
+                       } else
+                               filter.tb = tid;
                } else if (matches(*argv, "cached") == 0 ||
                           matches(*argv, "cloned") == 0) {
-                       filter.tb = -1;
+                       filter.cloned = 1;
                } else if (strcmp(*argv, "tos") == 0 ||
                           matches(*argv, "dsfield") == 0) {
                        __u32 tos;
@@ -1154,7 +1184,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
                char flushb[4096-512];
                time_t start = time(0);
 
-               if (filter.tb == -1) {
+               if (filter.cloned) {
                        if (do_ipv6 != AF_INET6) {
                                iproute_flush_cache();
                                if (show_stats)
@@ -1179,11 +1209,12 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
                                exit(1);
                        }
                        if (filter.flushed == 0) {
-                               if (round == 0) {
-                                       if (filter.tb != -1 || do_ipv6 == AF_INET6)
-                                               fprintf(stderr, "Nothing to flush.\n");
-                               } else if (show_stats)
-                                       printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
+                               if (show_stats) {
+                                       if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
+                                               printf("Nothing to flush.\n");
+                                       else
+                                               printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
+                               }
                                fflush(stdout);
                                return 0;
                        }
@@ -1204,7 +1235,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
                }
        }
 
-       if (filter.tb != -1) {
+       if (!filter.cloned) {
                if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
                        perror("Cannot send dump request");
                        exit(1);
@@ -1252,7 +1283,7 @@ int iproute_get(int argc, char **argv)
        req.r.rtm_src_len = 0;
        req.r.rtm_dst_len = 0;
        req.r.rtm_tos = 0;
-       
+
        while (argc > 0) {
                if (strcmp(*argv, "tos") == 0 ||
                    matches(*argv, "dsfield") == 0) {
@@ -1394,7 +1425,7 @@ int do_iproute(int argc, char **argv)
 {
        if (argc < 1)
                return iproute_list_or_flush(0, NULL, 0);
-       
+
        if (matches(*argv, "add") == 0)
                return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
                                      argc-1, argv+1);