]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vrf.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / iplink_vrf.c
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 #include <errno.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "ip_common.h"
21
22 static void vrf_explain(FILE *f)
23 {
24 fprintf(f, "Usage: ... vrf table TABLEID\n");
25 }
26
27 static void explain(void)
28 {
29 vrf_explain(stderr);
30 }
31
32 static 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
41 if (rtnl_rttable_a2n(&table, *argv))
42 invarg("invalid table ID\n", *argv);
43 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
59 static 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 print_uint(PRINT_ANY,
66 "table",
67 "table %u ",
68 rta_getattr_u32(tb[IFLA_VRF_TABLE]));
69 }
70
71 static void vrf_slave_print_opt(struct link_util *lu, FILE *f,
72 struct rtattr *tb[])
73 {
74 if (!tb)
75 return;
76
77 if (tb[IFLA_VRF_PORT_TABLE]) {
78 print_uint(PRINT_ANY,
79 "table",
80 "table %u ",
81 rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE]));
82 }
83 }
84
85 static void vrf_print_help(struct link_util *lu, int argc, char **argv,
86 FILE *f)
87 {
88 vrf_explain(f);
89 }
90
91 struct link_util vrf_link_util = {
92 .id = "vrf",
93 .maxattr = IFLA_VRF_MAX,
94 .parse_opt = vrf_parse_opt,
95 .print_opt = vrf_print_opt,
96 .print_help = vrf_print_help,
97 };
98
99 struct link_util vrf_slave_link_util = {
100 .id = "vrf_slave",
101 .maxattr = IFLA_VRF_PORT_MAX,
102 .print_opt = vrf_slave_print_opt,
103 };
104
105 /* returns table id if name is a VRF device */
106 __u32 ipvrf_get_table(const char *name)
107 {
108 struct {
109 struct nlmsghdr n;
110 struct ifinfomsg i;
111 char buf[1024];
112 } req = {
113 .n = {
114 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
115 .nlmsg_flags = NLM_F_REQUEST,
116 .nlmsg_type = RTM_GETLINK,
117 },
118 .i = {
119 .ifi_family = preferred_family,
120 },
121 };
122 struct {
123 struct nlmsghdr n;
124 char buf[8192];
125 } answer;
126 struct rtattr *tb[IFLA_MAX+1];
127 struct rtattr *li[IFLA_INFO_MAX+1];
128 struct rtattr *vrf_attr[IFLA_VRF_MAX + 1];
129 struct ifinfomsg *ifi;
130 __u32 tb_id = 0;
131 int len;
132
133 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
134
135 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n,
136 &answer.n, sizeof(answer)) < 0) {
137 /* special case "default" vrf to be the main table */
138 if (errno == ENODEV && !strcmp(name, "default"))
139 if (rtnl_rttable_a2n(&tb_id, "main"))
140 fprintf(stderr,
141 "BUG: RTTable \"main\" not found.\n");
142
143 return tb_id;
144 }
145
146 ifi = NLMSG_DATA(&answer.n);
147 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
148 if (len < 0) {
149 fprintf(stderr, "BUG: Invalid response to link query.\n");
150 return 0;
151 }
152
153 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
154
155 if (!tb[IFLA_LINKINFO])
156 return 0;
157
158 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
159
160 if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
161 return 0;
162
163 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
164 return 0;
165
166 parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
167 if (vrf_attr[IFLA_VRF_TABLE])
168 tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
169
170 if (!tb_id)
171 fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
172
173 return tb_id;
174 }
175
176 int name_is_vrf(const char *name)
177 {
178 struct {
179 struct nlmsghdr n;
180 struct ifinfomsg i;
181 char buf[1024];
182 } req = {
183 .n = {
184 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
185 .nlmsg_flags = NLM_F_REQUEST,
186 .nlmsg_type = RTM_GETLINK,
187 },
188 .i = {
189 .ifi_family = preferred_family,
190 },
191 };
192 struct {
193 struct nlmsghdr n;
194 char buf[8192];
195 } answer;
196 struct rtattr *tb[IFLA_MAX+1];
197 struct rtattr *li[IFLA_INFO_MAX+1];
198 struct ifinfomsg *ifi;
199 int len;
200
201 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
202
203 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n,
204 &answer.n, sizeof(answer)) < 0)
205 return 0;
206
207 ifi = NLMSG_DATA(&answer.n);
208 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
209 if (len < 0) {
210 fprintf(stderr, "BUG: Invalid response to link query.\n");
211 return 0;
212 }
213
214 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
215
216 if (!tb[IFLA_LINKINFO])
217 return 0;
218
219 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
220
221 if (!li[IFLA_INFO_KIND])
222 return 0;
223
224 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
225 return 0;
226
227 return ifi->ifi_index;
228 }