blob: 917630e8533758a62d8566cd877388c3101d5529 [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>
David Ahern463d9ef2016-12-11 16:53:14 -080016#include <errno.h>
David Ahern15faa0a2015-08-13 14:59:11 -060017
18#include "rt_names.h"
19#include "utils.h"
20#include "ip_common.h"
21
22static void vrf_explain(FILE *f)
23{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070024 fprintf(f, "Usage: ... vrf table TABLEID\n");
David Ahern15faa0a2015-08-13 14:59:11 -060025}
26
27static void explain(void)
28{
29 vrf_explain(stderr);
30}
31
David Ahern15faa0a2015-08-13 14:59:11 -060032static int vrf_parse_opt(struct link_util *lu, int argc, char **argv,
33 struct nlmsghdr *n)
34{
35 while (argc > 0) {
36 if (matches(*argv, "table") == 0) {
37 __u32 table;
38
39 NEXT_ARG();
40
David Ahern8a23f822015-12-08 12:24:44 -080041 if (rtnl_rttable_a2n(&table, *argv))
42 invarg("invalid table ID\n", *argv);
David Ahern15faa0a2015-08-13 14:59:11 -060043 addattr32(n, 1024, IFLA_VRF_TABLE, table);
44 } else if (matches(*argv, "help") == 0) {
45 explain();
46 return -1;
47 } else {
48 fprintf(stderr, "vrf: unknown option \"%s\"?\n",
49 *argv);
50 explain();
51 return -1;
52 }
53 argc--, argv++;
54 }
55
56 return 0;
57}
58
59static void vrf_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
60{
61 if (!tb)
62 return;
63
64 if (tb[IFLA_VRF_TABLE])
65 fprintf(f, "table %u ", rta_getattr_u32(tb[IFLA_VRF_TABLE]));
66}
67
David Ahern33e41672016-02-02 07:43:46 -080068static void vrf_slave_print_opt(struct link_util *lu, FILE *f,
69 struct rtattr *tb[])
70{
71 if (!tb)
72 return;
73
74 if (tb[IFLA_VRF_PORT_TABLE]) {
75 fprintf(f, "table %u ",
76 rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE]));
77 }
78}
79
David Ahern15faa0a2015-08-13 14:59:11 -060080static void vrf_print_help(struct link_util *lu, int argc, char **argv,
81 FILE *f)
82{
83 vrf_explain(f);
84}
85
86struct link_util vrf_link_util = {
87 .id = "vrf",
88 .maxattr = IFLA_VRF_MAX,
89 .parse_opt = vrf_parse_opt,
90 .print_opt = vrf_print_opt,
91 .print_help = vrf_print_help,
92};
David Ahern33e41672016-02-02 07:43:46 -080093
94struct link_util vrf_slave_link_util = {
Hangbin Liu22a84712016-09-20 18:02:12 +080095 .id = "vrf_slave",
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070096 .maxattr = IFLA_VRF_PORT_MAX,
David Ahern33e41672016-02-02 07:43:46 -080097 .print_opt = vrf_slave_print_opt,
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
David Ahern463d9ef2016-12-11 16:53:14 -0800130 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n,
131 &answer.n, sizeof(answer)) < 0) {
132 /* special case "default" vrf to be the main table */
133 if (errno == ENODEV && !strcmp(name, "default"))
134 rtnl_rttable_a2n(&tb_id, "main");
135
136 return tb_id;
137 }
David Ahern9b765772016-06-29 11:27:01 -0700138
139 ifi = NLMSG_DATA(&answer.n);
140 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
141 if (len < 0) {
142 fprintf(stderr, "BUG: Invalid response to link query.\n");
143 return 0;
144 }
145
146 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
147
148 if (!tb[IFLA_LINKINFO])
149 return 0;
150
151 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
152
153 if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
154 return 0;
155
156 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
157 return 0;
158
159 parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
160 if (vrf_attr[IFLA_VRF_TABLE])
161 tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
162
163 if (!tb_id)
164 fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
165
166 return tb_id;
167}
168
David Ahern23304902016-12-11 16:53:13 -0800169int name_is_vrf(const char *name)
David Ahern7dc0e972016-06-29 11:26:57 -0700170{
171 struct {
172 struct nlmsghdr n;
173 struct ifinfomsg i;
174 char buf[1024];
175 } req = {
176 .n = {
177 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
178 .nlmsg_flags = NLM_F_REQUEST,
179 .nlmsg_type = RTM_GETLINK,
180 },
181 .i = {
182 .ifi_family = preferred_family,
183 },
184 };
185 struct {
186 struct nlmsghdr n;
187 char buf[8192];
188 } answer;
189 struct rtattr *tb[IFLA_MAX+1];
190 struct rtattr *li[IFLA_INFO_MAX+1];
191 struct ifinfomsg *ifi;
192 int len;
193
194 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
195
David Ahern463d9ef2016-12-11 16:53:14 -0800196 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n,
197 &answer.n, sizeof(answer)) < 0)
David Ahern23304902016-12-11 16:53:13 -0800198 return 0;
David Ahern7dc0e972016-06-29 11:26:57 -0700199
200 ifi = NLMSG_DATA(&answer.n);
201 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
202 if (len < 0) {
203 fprintf(stderr, "BUG: Invalid response to link query.\n");
David Ahern23304902016-12-11 16:53:13 -0800204 return 0;
David Ahern7dc0e972016-06-29 11:26:57 -0700205 }
206
207 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
208
209 if (!tb[IFLA_LINKINFO])
David Ahern23304902016-12-11 16:53:13 -0800210 return 0;
David Ahern7dc0e972016-06-29 11:26:57 -0700211
212 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
213
214 if (!li[IFLA_INFO_KIND])
David Ahern23304902016-12-11 16:53:13 -0800215 return 0;
David Ahern7dc0e972016-06-29 11:26:57 -0700216
David Ahern23304902016-12-11 16:53:13 -0800217 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
218 return 0;
219
220 return ifi->ifi_index;
David Ahern7dc0e972016-06-29 11:26:57 -0700221}