]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vrf.c
ipnetns: parse nsid as a signed integer
[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 nlmsghdr *answer;
123 struct rtattr *tb[IFLA_MAX+1];
124 struct rtattr *li[IFLA_INFO_MAX+1];
125 struct rtattr *vrf_attr[IFLA_VRF_MAX + 1];
126 struct ifinfomsg *ifi;
127 __u32 tb_id = 0;
128 int len;
129
130 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
131
132 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0) {
133 /* special case "default" vrf to be the main table */
134 if (errno == ENODEV && !strcmp(name, "default"))
135 if (rtnl_rttable_a2n(&tb_id, "main"))
136 fprintf(stderr,
137 "BUG: RTTable \"main\" not found.\n");
138
139 return tb_id;
140 }
141
142 ifi = NLMSG_DATA(answer);
143 len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
144 if (len < 0) {
145 fprintf(stderr, "BUG: Invalid response to link query.\n");
146 goto out;
147 }
148
149 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
150
151 if (!tb[IFLA_LINKINFO])
152 goto out;
153
154 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
155
156 if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA])
157 goto out;
158
159 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
160 goto out;
161
162 parse_rtattr_nested(vrf_attr, IFLA_VRF_MAX, li[IFLA_INFO_DATA]);
163 if (vrf_attr[IFLA_VRF_TABLE])
164 tb_id = rta_getattr_u32(vrf_attr[IFLA_VRF_TABLE]);
165
166 if (!tb_id)
167 fprintf(stderr, "BUG: VRF %s is missing table id\n", name);
168
169 out:
170 free(answer);
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 nlmsghdr *answer;
191 struct rtattr *tb[IFLA_MAX+1];
192 struct rtattr *li[IFLA_INFO_MAX+1];
193 struct ifinfomsg *ifi;
194 int ifindex = 0;
195 int len;
196
197 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
198
199 if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0)
200 return 0;
201
202 ifi = NLMSG_DATA(answer);
203 len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
204 if (len < 0) {
205 fprintf(stderr, "BUG: Invalid response to link query.\n");
206 goto out;
207 }
208
209 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
210
211 if (!tb[IFLA_LINKINFO])
212 goto out;
213
214 parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
215
216 if (!li[IFLA_INFO_KIND])
217 goto out;
218
219 if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
220 goto out;
221
222 ifindex = ifi->ifi_index;
223 out:
224 free(answer);
225 return ifindex;
226 }