]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/tunnel.c
iproute: Set ip/ip6 lwtunnel flags
[mirror_iproute2.git] / ip / tunnel.c
index 06533cfa1fa0d2f864e4d5fad2bf1e5c1469a1d5..d0d55f37169e9f522e39453b894b61f9bbf0200e 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/if.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
+#include <linux/if_arp.h>
 
 #include "utils.h"
 #include "tunnel.h"
@@ -64,7 +65,7 @@ int tnl_get_ioctl(const char *basedev, void *p)
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
        ifr.ifr_ifru.ifru_data = (void *)p;
 
        fd = socket(preferred_family, SOCK_DGRAM, 0);
@@ -89,9 +90,9 @@ int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p)
        int err;
 
        if (cmd == SIOCCHGTUNNEL && name[0])
-               strncpy(ifr.ifr_name, name, IFNAMSIZ);
+               strlcpy(ifr.ifr_name, name, IFNAMSIZ);
        else
-               strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+               strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
        ifr.ifr_ifru.ifru_data = p;
 
        fd = socket(preferred_family, SOCK_DGRAM, 0);
@@ -115,9 +116,9 @@ int tnl_del_ioctl(const char *basedev, const char *name, void *p)
        int err;
 
        if (name[0])
-               strncpy(ifr.ifr_name, name, IFNAMSIZ);
+               strlcpy(ifr.ifr_name, name, IFNAMSIZ);
        else
-               strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
+               strlcpy(ifr.ifr_name, basedev, IFNAMSIZ);
 
        ifr.ifr_ifru.ifru_data = p;
 
@@ -142,7 +143,7 @@ static int tnl_gen_ioctl(int cmd, const char *name,
        int fd;
        int err;
 
-       strncpy(ifr.ifr_name, name, IFNAMSIZ);
+       strlcpy(ifr.ifr_name, name, IFNAMSIZ);
        ifr.ifr_ifru.ifru_data = p;
 
        fd = socket(preferred_family, SOCK_DGRAM, 0);
@@ -307,37 +308,7 @@ void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
        }
 }
 
-int tnl_get_stats(const char *buf, struct rtnl_link_stats64 *s)
-{
-       /* rx */
-       __u64 *rx_bytes   = &s->rx_bytes;
-       __u64 *rx_packets = &s->rx_packets;
-       __u64 *rx_errs    = &s->rx_errors;
-       __u64 *rx_drops   = &s->rx_dropped;
-       __u64 *rx_fifo    = &s->rx_fifo_errors;
-       __u64 *rx_frame   = &s->rx_frame_errors;
-       __u64 *rx_multi   = &s->multicast;
-       /* tx */
-       __u64 *tx_bytes   = &s->tx_bytes;
-       __u64 *tx_packets = &s->tx_packets;
-       __u64 *tx_errs    = &s->tx_errors;
-       __u64 *tx_drops   = &s->tx_dropped;
-       __u64 *tx_fifo    = &s->tx_fifo_errors;
-       __u64 *tx_carrier = &s->tx_carrier_errors;
-       __u64 *tx_colls   = &s->collisions;
-
-       if (sscanf(buf,
-                  "%llu%llu%llu%llu%llu%llu%llu%*d%llu%llu%llu%llu%llu%llu%llu",
-                  rx_bytes, rx_packets, rx_errs, rx_drops,
-                  rx_fifo, rx_frame, rx_multi,
-                  tx_bytes, tx_packets, tx_errs, tx_drops,
-                  tx_fifo, tx_colls, tx_carrier) != 14)
-               return -1;
-
-       return 0;
-}
-
-void tnl_print_stats(const struct rtnl_link_stats64 *s)
+static void tnl_print_stats(const struct rtnl_link_stats64 *s)
 {
        printf("%s", _SL_);
        printf("RX: Packets    Bytes        Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
@@ -349,3 +320,86 @@ void tnl_print_stats(const struct rtnl_link_stats64 *s)
               s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions,
               s->tx_carrier_errors, s->tx_dropped);
 }
+
+static int print_nlmsg_tunnel(struct nlmsghdr *n, void *arg)
+{
+       struct tnl_print_nlmsg_info *info = arg;
+       struct ifinfomsg *ifi = NLMSG_DATA(n);
+       struct rtattr *tb[IFLA_MAX+1];
+       const char *name, *n1;
+
+       if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
+               return 0;
+
+       if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
+               return -1;
+
+       if (preferred_family == AF_INET) {
+               switch (ifi->ifi_type) {
+               case ARPHRD_TUNNEL:
+               case ARPHRD_IPGRE:
+               case ARPHRD_SIT:
+                       break;
+               default:
+                       return 0;
+               }
+       } else {
+               switch (ifi->ifi_type) {
+               case ARPHRD_TUNNEL6:
+               case ARPHRD_IP6GRE:
+                       break;
+               default:
+                       return 0;
+               }
+       }
+
+       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
+
+       if (!tb[IFLA_IFNAME])
+               return 0;
+
+       name = rta_getattr_str(tb[IFLA_IFNAME]);
+
+       /* Assume p1->name[IFNAMSIZ] is first field of structure */
+       n1 = info->p1;
+       if (n1[0] && strcmp(n1, name))
+               return 0;
+
+       info->ifi = ifi;
+       info->init(info);
+
+       /* TODO: parse netlink attributes */
+       if (tnl_get_ioctl(name, info->p2))
+               return 0;
+
+       if (!info->match(info))
+               return 0;
+
+       info->print(info->p2);
+       if (show_stats) {
+               struct rtnl_link_stats64 s;
+
+               if (get_rtnl_link_stats_rta(&s, tb) <= 0)
+                       return -1;
+
+               tnl_print_stats(&s);
+       }
+       fputc('\n', stdout);
+
+       return 0;
+}
+
+int do_tunnels_list(struct tnl_print_nlmsg_info *info)
+{
+       if (rtnl_linkdump_req(&rth, preferred_family) < 0) {
+               perror("Cannot send dump request\n");
+               return -1;
+       }
+
+       if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) {
+               fprintf(stderr, "Dump terminated\n");
+               return -1;
+       }
+
+       return 0;
+}