David Ahern | 15faa0a | 2015-08-13 14:59:11 -0600 | [diff] [blame] | 1 | /* iplink_vrf.c VRF device support |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version |
| 6 | * 2 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * Authors: Shrijeet Mukherjee <shm@cumulusnetworks.com> |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <linux/if_link.h> |
| 16 | |
| 17 | #include "rt_names.h" |
| 18 | #include "utils.h" |
| 19 | #include "ip_common.h" |
| 20 | |
| 21 | static void vrf_explain(FILE *f) |
| 22 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 23 | fprintf(f, "Usage: ... vrf table TABLEID\n"); |
David Ahern | 15faa0a | 2015-08-13 14:59:11 -0600 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static void explain(void) |
| 27 | { |
| 28 | vrf_explain(stderr); |
| 29 | } |
| 30 | |
David Ahern | 15faa0a | 2015-08-13 14:59:11 -0600 | [diff] [blame] | 31 | static int vrf_parse_opt(struct link_util *lu, int argc, char **argv, |
| 32 | struct nlmsghdr *n) |
| 33 | { |
| 34 | while (argc > 0) { |
| 35 | if (matches(*argv, "table") == 0) { |
| 36 | __u32 table; |
| 37 | |
| 38 | NEXT_ARG(); |
| 39 | |
David Ahern | 8a23f82 | 2015-12-08 12:24:44 -0800 | [diff] [blame] | 40 | if (rtnl_rttable_a2n(&table, *argv)) |
| 41 | invarg("invalid table ID\n", *argv); |
David Ahern | 15faa0a | 2015-08-13 14:59:11 -0600 | [diff] [blame] | 42 | addattr32(n, 1024, IFLA_VRF_TABLE, table); |
| 43 | } else if (matches(*argv, "help") == 0) { |
| 44 | explain(); |
| 45 | return -1; |
| 46 | } else { |
| 47 | fprintf(stderr, "vrf: unknown option \"%s\"?\n", |
| 48 | *argv); |
| 49 | explain(); |
| 50 | return -1; |
| 51 | } |
| 52 | argc--, argv++; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static void vrf_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 59 | { |
| 60 | if (!tb) |
| 61 | return; |
| 62 | |
| 63 | if (tb[IFLA_VRF_TABLE]) |
| 64 | fprintf(f, "table %u ", rta_getattr_u32(tb[IFLA_VRF_TABLE])); |
| 65 | } |
| 66 | |
David Ahern | 33e4167 | 2016-02-02 07:43:46 -0800 | [diff] [blame] | 67 | static void vrf_slave_print_opt(struct link_util *lu, FILE *f, |
| 68 | struct rtattr *tb[]) |
| 69 | { |
| 70 | if (!tb) |
| 71 | return; |
| 72 | |
| 73 | if (tb[IFLA_VRF_PORT_TABLE]) { |
| 74 | fprintf(f, "table %u ", |
| 75 | rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE])); |
| 76 | } |
| 77 | } |
| 78 | |
David Ahern | 15faa0a | 2015-08-13 14:59:11 -0600 | [diff] [blame] | 79 | static void vrf_print_help(struct link_util *lu, int argc, char **argv, |
| 80 | FILE *f) |
| 81 | { |
| 82 | vrf_explain(f); |
| 83 | } |
| 84 | |
| 85 | struct link_util vrf_link_util = { |
| 86 | .id = "vrf", |
| 87 | .maxattr = IFLA_VRF_MAX, |
| 88 | .parse_opt = vrf_parse_opt, |
| 89 | .print_opt = vrf_print_opt, |
| 90 | .print_help = vrf_print_help, |
| 91 | }; |
David Ahern | 33e4167 | 2016-02-02 07:43:46 -0800 | [diff] [blame] | 92 | |
| 93 | struct link_util vrf_slave_link_util = { |
Hangbin Liu | 22a8471 | 2016-09-20 18:02:12 +0800 | [diff] [blame] | 94 | .id = "vrf_slave", |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 95 | .maxattr = IFLA_VRF_PORT_MAX, |
David Ahern | 33e4167 | 2016-02-02 07:43:46 -0800 | [diff] [blame] | 96 | .print_opt = vrf_slave_print_opt, |
David Ahern | 33e4167 | 2016-02-02 07:43:46 -0800 | [diff] [blame] | 97 | }; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 98 | |
David Ahern | 9b76577 | 2016-06-29 11:27:01 -0700 | [diff] [blame] | 99 | /* returns table id if name is a VRF device */ |
| 100 | __u32 ipvrf_get_table(const char *name) |
| 101 | { |
| 102 | struct { |
| 103 | struct nlmsghdr n; |
| 104 | struct ifinfomsg i; |
| 105 | char buf[1024]; |
| 106 | } req = { |
| 107 | .n = { |
| 108 | .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 109 | .nlmsg_flags = NLM_F_REQUEST, |
| 110 | .nlmsg_type = RTM_GETLINK, |
| 111 | }, |
| 112 | .i = { |
| 113 | .ifi_family = preferred_family, |
| 114 | }, |
| 115 | }; |
| 116 | struct { |
| 117 | struct nlmsghdr n; |
| 118 | char buf[8192]; |
| 119 | } answer; |
| 120 | struct rtattr *tb[IFLA_MAX+1]; |
| 121 | struct rtattr *li[IFLA_INFO_MAX+1]; |
| 122 | struct rtattr *vrf_attr[IFLA_VRF_MAX + 1]; |
| 123 | struct ifinfomsg *ifi; |
| 124 | __u32 tb_id = 0; |
| 125 | int len; |
| 126 | |
| 127 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1); |
| 128 | |
| 129 | if (rtnl_talk(&rth, &req.n, &answer.n, sizeof(answer)) < 0) |
| 130 | return 0; |
| 131 | |
| 132 | ifi = NLMSG_DATA(&answer.n); |
| 133 | len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); |
| 134 | if (len < 0) { |
| 135 | fprintf(stderr, "BUG: Invalid response to link query.\n"); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 140 | |
| 141 | if (!tb[IFLA_LINKINFO]) |
| 142 | return 0; |
| 143 | |
| 144 | parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 145 | |
| 146 | if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA]) |
| 147 | return 0; |
| 148 | |
| 149 | if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf")) |
| 150 | return 0; |
| 151 | |
| 152 | parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]); |
| 153 | if (vrf_attr[IFLA_VRF_TABLE]) |
| 154 | tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]); |
| 155 | |
| 156 | if (!tb_id) |
| 157 | fprintf(stderr, "BUG: VRF %s is missing table id\n", name); |
| 158 | |
| 159 | return tb_id; |
| 160 | } |
| 161 | |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 162 | int name_is_vrf(const char *name) |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 163 | { |
| 164 | struct { |
| 165 | struct nlmsghdr n; |
| 166 | struct ifinfomsg i; |
| 167 | char buf[1024]; |
| 168 | } req = { |
| 169 | .n = { |
| 170 | .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 171 | .nlmsg_flags = NLM_F_REQUEST, |
| 172 | .nlmsg_type = RTM_GETLINK, |
| 173 | }, |
| 174 | .i = { |
| 175 | .ifi_family = preferred_family, |
| 176 | }, |
| 177 | }; |
| 178 | struct { |
| 179 | struct nlmsghdr n; |
| 180 | char buf[8192]; |
| 181 | } answer; |
| 182 | struct rtattr *tb[IFLA_MAX+1]; |
| 183 | struct rtattr *li[IFLA_INFO_MAX+1]; |
| 184 | struct ifinfomsg *ifi; |
| 185 | int len; |
| 186 | |
| 187 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1); |
| 188 | |
| 189 | if (rtnl_talk(&rth, &req.n, &answer.n, sizeof(answer)) < 0) |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 190 | return 0; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 191 | |
| 192 | ifi = NLMSG_DATA(&answer.n); |
| 193 | len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); |
| 194 | if (len < 0) { |
| 195 | fprintf(stderr, "BUG: Invalid response to link query.\n"); |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 196 | return 0; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 200 | |
| 201 | if (!tb[IFLA_LINKINFO]) |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 202 | return 0; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 203 | |
| 204 | parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 205 | |
| 206 | if (!li[IFLA_INFO_KIND]) |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 207 | return 0; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 208 | |
David Ahern | 2330490 | 2016-12-11 16:53:13 -0800 | [diff] [blame^] | 209 | if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf")) |
| 210 | return 0; |
| 211 | |
| 212 | return ifi->ifi_index; |
David Ahern | 7dc0e97 | 2016-06-29 11:26:57 -0700 | [diff] [blame] | 213 | } |