]>
Commit | Line | Data |
---|---|---|
15faa0a3 DA |
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> | |
463d9efa | 16 | #include <errno.h> |
15faa0a3 DA |
17 | |
18 | #include "rt_names.h" | |
19 | #include "utils.h" | |
20 | #include "ip_common.h" | |
21 | ||
22 | static void vrf_explain(FILE *f) | |
23 | { | |
56f5daac | 24 | fprintf(f, "Usage: ... vrf table TABLEID\n"); |
15faa0a3 DA |
25 | } |
26 | ||
27 | static void explain(void) | |
28 | { | |
29 | vrf_explain(stderr); | |
30 | } | |
31 | ||
15faa0a3 DA |
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 | ||
8a23f820 DA |
41 | if (rtnl_rttable_a2n(&table, *argv)) |
42 | invarg("invalid table ID\n", *argv); | |
15faa0a3 DA |
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]) | |
d9e84ec2 JF |
65 | print_uint(PRINT_ANY, |
66 | "table", | |
67 | "table %u ", | |
68 | rta_getattr_u32(tb[IFLA_VRF_TABLE])); | |
15faa0a3 DA |
69 | } |
70 | ||
33e41670 DA |
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]) { | |
d9e84ec2 JF |
78 | print_uint(PRINT_ANY, |
79 | "table", | |
80 | "table %u ", | |
81 | rta_getattr_u32(tb[IFLA_VRF_PORT_TABLE])); | |
33e41670 DA |
82 | } |
83 | } | |
84 | ||
15faa0a3 | 85 | static void vrf_print_help(struct link_util *lu, int argc, char **argv, |
d9e84ec2 | 86 | FILE *f) |
15faa0a3 DA |
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 | }; | |
33e41670 DA |
98 | |
99 | struct link_util vrf_slave_link_util = { | |
22a84711 | 100 | .id = "vrf_slave", |
56f5daac | 101 | .maxattr = IFLA_VRF_PORT_MAX, |
33e41670 | 102 | .print_opt = vrf_slave_print_opt, |
33e41670 | 103 | }; |
7dc0e974 | 104 | |
9b765770 DA |
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 | }; | |
86bf43c7 | 122 | struct nlmsghdr *answer; |
9b765770 DA |
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 | ||
86bf43c7 | 132 | if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0) { |
463d9efa DA |
133 | /* special case "default" vrf to be the main table */ |
134 | if (errno == ENODEV && !strcmp(name, "default")) | |
84b6a3f4 PS |
135 | if (rtnl_rttable_a2n(&tb_id, "main")) |
136 | fprintf(stderr, | |
137 | "BUG: RTTable \"main\" not found.\n"); | |
463d9efa DA |
138 | |
139 | return tb_id; | |
140 | } | |
9b765770 | 141 | |
86bf43c7 HL |
142 | ifi = NLMSG_DATA(answer); |
143 | len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); | |
9b765770 DA |
144 | if (len < 0) { |
145 | fprintf(stderr, "BUG: Invalid response to link query.\n"); | |
86bf43c7 | 146 | goto out; |
9b765770 DA |
147 | } |
148 | ||
149 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
150 | ||
151 | if (!tb[IFLA_LINKINFO]) | |
86bf43c7 | 152 | goto out; |
9b765770 DA |
153 | |
154 | parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
155 | ||
156 | if (!li[IFLA_INFO_KIND] || !li[IFLA_INFO_DATA]) | |
86bf43c7 | 157 | goto out; |
9b765770 DA |
158 | |
159 | if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf")) | |
86bf43c7 | 160 | goto out; |
9b765770 DA |
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 | ||
86bf43c7 HL |
169 | out: |
170 | free(answer); | |
9b765770 DA |
171 | return tb_id; |
172 | } | |
173 | ||
2330490f | 174 | int name_is_vrf(const char *name) |
7dc0e974 DA |
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 | }; | |
86bf43c7 | 190 | struct nlmsghdr *answer; |
7dc0e974 DA |
191 | struct rtattr *tb[IFLA_MAX+1]; |
192 | struct rtattr *li[IFLA_INFO_MAX+1]; | |
193 | struct ifinfomsg *ifi; | |
4c349368 | 194 | int ifindex = 0; |
7dc0e974 DA |
195 | int len; |
196 | ||
197 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1); | |
198 | ||
86bf43c7 | 199 | if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0) |
2330490f | 200 | return 0; |
7dc0e974 | 201 | |
86bf43c7 HL |
202 | ifi = NLMSG_DATA(answer); |
203 | len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); | |
7dc0e974 DA |
204 | if (len < 0) { |
205 | fprintf(stderr, "BUG: Invalid response to link query.\n"); | |
86bf43c7 | 206 | goto out; |
7dc0e974 DA |
207 | } |
208 | ||
209 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
210 | ||
211 | if (!tb[IFLA_LINKINFO]) | |
86bf43c7 | 212 | goto out; |
7dc0e974 DA |
213 | |
214 | parse_rtattr_nested(li, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
215 | ||
216 | if (!li[IFLA_INFO_KIND]) | |
86bf43c7 | 217 | goto out; |
2330490f DA |
218 | |
219 | if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf")) | |
86bf43c7 | 220 | goto out; |
7dc0e974 | 221 | |
4c349368 | 222 | ifindex = ifi->ifi_index; |
86bf43c7 HL |
223 | out: |
224 | free(answer); | |
4c349368 | 225 | return ifindex; |
7dc0e974 | 226 | } |