]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iplink_vrf.c
bridge: fdb: add support for src_vni option
[mirror_iproute2.git] / ip / iplink_vrf.c
index abc796886a5f7607260c32debb102b185193a358..5d20f29d3526f845344e929ecf14922c9c056925 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <linux/if_link.h>
+#include <errno.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -20,7 +21,7 @@
 
 static void vrf_explain(FILE *f)
 {
-       fprintf(f, "Usage: ... vrf table TABLEID \n");
+       fprintf(f, "Usage: ... vrf table TABLEID\n");
 }
 
 static void explain(void)
@@ -61,7 +62,10 @@ static void vrf_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                return;
 
        if (tb[IFLA_VRF_TABLE])
-               fprintf(f, "table %u ", rta_getattr_u32(tb[IFLA_VRF_TABLE]));
+               print_uint(PRINT_ANY,
+                          "table",
+                          "table %u ",
+                          rta_getattr_u32(tb[IFLA_VRF_TABLE]));
 }
 
 static void vrf_slave_print_opt(struct link_util *lu, FILE *f,
@@ -71,13 +75,15 @@ static void vrf_slave_print_opt(struct link_util *lu, FILE *f,
                return;
 
        if (tb[IFLA_VRF_PORT_TABLE]) {
-               fprintf(f, "table %u ",
-                       rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE]));
+               print_uint(PRINT_ANY,
+                          "table",
+                          "table %u ",
+                          rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE]));
        }
 }
 
 static void vrf_print_help(struct link_util *lu, int argc, char **argv,
-                             FILE *f)
+                          FILE *f)
 {
        vrf_explain(f);
 }
@@ -91,8 +97,130 @@ struct link_util vrf_link_util = {
 };
 
 struct link_util vrf_slave_link_util = {
-        .id             = "vrf",
-        .maxattr        = IFLA_VRF_PORT_MAX,
+       .id             = "vrf_slave",
+       .maxattr        = IFLA_VRF_PORT_MAX,
        .print_opt      = vrf_slave_print_opt,
-        .slave          = true,
 };
+
+/* returns table id if name is a VRF device */
+__u32 ipvrf_get_table(const char *name)
+{
+       struct {
+               struct nlmsghdr         n;
+               struct ifinfomsg        i;
+               char                    buf[1024];
+       } req = {
+               .n = {
+                       .nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+                       .nlmsg_flags = NLM_F_REQUEST,
+                       .nlmsg_type  = RTM_GETLINK,
+               },
+               .i = {
+                       .ifi_family  = preferred_family,
+               },
+       };
+       struct nlmsghdr *answer;
+       struct rtattr *tb[IFLA_MAX+1];
+       struct rtattr *li[IFLA_INFO_MAX+1];
+       struct rtattr *vrf_attr[IFLA_VRF_MAX + 1];
+       struct ifinfomsg *ifi;
+       __u32 tb_id = 0;
+       int len;
+
+       addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
+
+       if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0) {
+               /* special case "default" vrf to be the main table */
+               if (errno == ENODEV && !strcmp(name, "default"))
+                       if (rtnl_rttable_a2n(&tb_id, "main"))
+                               fprintf(stderr,
+                                       "BUG: RTTable \"main\" not found.\n");
+
+               return tb_id;
+       }
+
+       ifi = NLMSG_DATA(answer);
+       len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
+       if (len < 0) {
+               fprintf(stderr, "BUG: Invalid response to link query.\n");
+               goto out;
+       }
+
+       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
+
+       if (!tb[IFLA_LINKINFO])
+               goto out;
+
+       parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
+
+       if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
+               goto out;
+
+       if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
+               goto out;
+
+       parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
+       if (vrf_attr[IFLA_VRF_TABLE])
+               tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
+
+       if (!tb_id)
+               fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
+
+out:
+       free(answer);
+       return tb_id;
+}
+
+int name_is_vrf(const char *name)
+{
+       struct {
+               struct nlmsghdr         n;
+               struct ifinfomsg        i;
+               char                    buf[1024];
+       } req = {
+               .n = {
+                       .nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+                       .nlmsg_flags = NLM_F_REQUEST,
+                       .nlmsg_type  = RTM_GETLINK,
+               },
+               .i = {
+                       .ifi_family  = preferred_family,
+               },
+       };
+       struct nlmsghdr *answer;
+       struct rtattr *tb[IFLA_MAX+1];
+       struct rtattr *li[IFLA_INFO_MAX+1];
+       struct ifinfomsg *ifi;
+       int ifindex = 0;
+       int len;
+
+       addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
+
+       if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0)
+               return 0;
+
+       ifi = NLMSG_DATA(answer);
+       len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
+       if (len < 0) {
+               fprintf(stderr, "BUG: Invalid response to link query.\n");
+               goto out;
+       }
+
+       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
+
+       if (!tb[IFLA_LINKINFO])
+               goto out;
+
+       parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
+
+       if (!li[IFLA_INFO_KIND])
+               goto out;
+
+       if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
+               goto out;
+
+       ifindex = ifi->ifi_index;
+out:
+       free(answer);
+       return ifindex;
+}