blob: 015b41e1e7b0ea98066161a1d4d9039858e80db6 [file] [log] [blame]
David Ahern15faa0a2015-08-13 14:59:11 -06001/* 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
21static void vrf_explain(FILE *f)
22{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070023 fprintf(f, "Usage: ... vrf table TABLEID\n");
David Ahern15faa0a2015-08-13 14:59:11 -060024}
25
26static void explain(void)
27{
28 vrf_explain(stderr);
29}
30
David Ahern15faa0a2015-08-13 14:59:11 -060031static 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 Ahern8a23f822015-12-08 12:24:44 -080040 if (rtnl_rttable_a2n(&table, *argv))
41 invarg("invalid table ID\n", *argv);
David Ahern15faa0a2015-08-13 14:59:11 -060042 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
58static 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 Ahern33e41672016-02-02 07:43:46 -080067static 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 Ahern15faa0a2015-08-13 14:59:11 -060079static void vrf_print_help(struct link_util *lu, int argc, char **argv,
80 FILE *f)
81{
82 vrf_explain(f);
83}
84
85struct 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 Ahern33e41672016-02-02 07:43:46 -080092
93struct link_util vrf_slave_link_util = {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070094 .id = "vrf",
95 .maxattr = IFLA_VRF_PORT_MAX,
David Ahern33e41672016-02-02 07:43:46 -080096 .print_opt = vrf_slave_print_opt,
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070097 .slave = true,
David Ahern33e41672016-02-02 07:43:46 -080098};
David Ahern7dc0e972016-06-29 11:26:57 -070099
David Ahern9b765772016-06-29 11:27:01 -0700100/* returns table id if name is a VRF device */
101__u32 ipvrf_get_table(const char *name)
102{
103 struct {
104 struct nlmsghdr n;
105 struct ifinfomsg i;
106 char buf[1024];
107 } req = {
108 .n = {
109 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
110 .nlmsg_flags = NLM_F_REQUEST,
111 .nlmsg_type = RTM_GETLINK,
112 },
113 .i = {
114 .ifi_family = preferred_family,
115 },
116 };
117 struct {
118 struct nlmsghdr n;
119 char buf[8192];
120 } answer;
121 struct rtattr *tb[IFLA_MAX+1];
122 struct rtattr *li[IFLA_INFO_MAX+1];
123 struct rtattr *vrf_attr[IFLA_VRF_MAX + 1];
124 struct ifinfomsg *ifi;
125 __u32 tb_id = 0;
126 int len;
127
128 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
129
130 if (rtnl_talk(&rth, &req.n, &answer.n, sizeof(answer)) < 0)
131 return 0;
132
133 ifi = NLMSG_DATA(&answer.n);
134 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
135 if (len < 0) {
136 fprintf(stderr, "BUG: Invalid response to link query.\n");
137 return 0;
138 }
139
140 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
141
142 if (!tb[IFLA_LINKINFO])
143 return 0;
144
145 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
146
147 if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
148 return 0;
149
150 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
151 return 0;
152
153 parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
154 if (vrf_attr[IFLA_VRF_TABLE])
155 tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
156
157 if (!tb_id)
158 fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
159
160 return tb_id;
161}
162
David Ahern7dc0e972016-06-29 11:26:57 -0700163bool name_is_vrf(const char *name)
164{
165 struct {
166 struct nlmsghdr n;
167 struct ifinfomsg i;
168 char buf[1024];
169 } req = {
170 .n = {
171 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
172 .nlmsg_flags = NLM_F_REQUEST,
173 .nlmsg_type = RTM_GETLINK,
174 },
175 .i = {
176 .ifi_family = preferred_family,
177 },
178 };
179 struct {
180 struct nlmsghdr n;
181 char buf[8192];
182 } answer;
183 struct rtattr *tb[IFLA_MAX+1];
184 struct rtattr *li[IFLA_INFO_MAX+1];
185 struct ifinfomsg *ifi;
186 int len;
187
188 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
189
190 if (rtnl_talk(&rth, &req.n, &answer.n, sizeof(answer)) < 0)
191 return false;
192
193 ifi = NLMSG_DATA(&answer.n);
194 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
195 if (len < 0) {
196 fprintf(stderr, "BUG: Invalid response to link query.\n");
197 return false;
198 }
199
200 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
201
202 if (!tb[IFLA_LINKINFO])
203 return false;
204
205 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
206
207 if (!li[IFLA_INFO_KIND])
208 return false;
209
210 return strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf") == 0;
211}