]> 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 rtnl_rttable_a2n(&tb_id, "main");
140
141 return tb_id;
142 }
143
144 ifi = NLMSG_DATA(&answer.n);
145 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
146 if (len < 0) {
147 fprintf(stderr, "BUG: Invalid response to link query.\n");
148 return 0;
149 }
150
151 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
152
153 if (!tb[IFLA_LINKINFO])
154 return 0;
155
156 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
157
158 if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
159 return 0;
160
161 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
162 return 0;
163
164 parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
165 if (vrf_attr[IFLA_VRF_TABLE])
166 tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
167
168 if (!tb_id)
169 fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
170
171 return tb_id;
172 }
173
174 int name_is_vrf(const char *name)
175 {
176 struct {
177 struct nlmsghdr n;
178 struct ifinfomsg i;
179 char buf[1024];
180 } req = {
181 .n = {
182 .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
183 .nlmsg_flags = NLM_F_REQUEST,
184 .nlmsg_type = RTM_GETLINK,
185 },
186 .i = {
187 .ifi_family = preferred_family,
188 },
189 };
190 struct {
191 struct nlmsghdr n;
192 char buf[8192];
193 } answer;
194 struct rtattr *tb[IFLA_MAX+1];
195 struct rtattr *li[IFLA_INFO_MAX+1];
196 struct ifinfomsg *ifi;
197 int len;
198
199 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
200
201 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n,
202 &answer.n, sizeof(answer)) < 0)
203 return 0;
204
205 ifi = NLMSG_DATA(&answer.n);
206 len = answer.n.nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
207 if (len < 0) {
208 fprintf(stderr, "BUG: Invalid response to link query.\n");
209 return 0;
210 }
211
212 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
213
214 if (!tb[IFLA_LINKINFO])
215 return 0;
216
217 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
218
219 if (!li[IFLA_INFO_KIND])
220 return 0;
221
222 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
223 return 0;
224
225 return ifi->ifi_index;
226 }