]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
Merge branch 'master' into net-next-3.11
[mirror_iproute2.git] / ip / iplink_vxlan.c
1 /*
2 * iplink_vxlan.c VXLAN device support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Stephen Hemminger <shemminger@vyatta.com
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <net/if.h>
16 #include <linux/ip.h>
17 #include <linux/if_link.h>
18 #include <arpa/inet.h>
19
20 #include "rt_names.h"
21 #include "utils.h"
22 #include "ip_common.h"
23
24 static void explain(void)
25 {
26 fprintf(stderr, "Usage: ... vxlan id VNI [ { group | remote } ADDR ] [ local ADDR ]\n");
27 fprintf(stderr, " [ ttl TTL ] [ tos TOS ] [ dev PHYS_DEV ]\n");
28 fprintf(stderr, " [ port MIN MAX ] [ [no]learning ]\n");
29 fprintf(stderr, " [ [no]proxy ] [ [no]rsc ]\n");
30 fprintf(stderr, " [ [no]l2miss ] [ [no]l3miss ]\n");
31 fprintf(stderr, "\n");
32 fprintf(stderr, "Where: VNI := 0-16777215\n");
33 fprintf(stderr, " ADDR := { IP_ADDRESS | any }\n");
34 fprintf(stderr, " TOS := { NUMBER | inherit }\n");
35 fprintf(stderr, " TTL := { 1..255 | inherit }\n");
36 }
37
38 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
39 struct nlmsghdr *n)
40 {
41 __u32 vni = 0;
42 int vni_set = 0;
43 __u32 saddr = 0;
44 __u32 gaddr = 0;
45 __u32 daddr = 0;
46 struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
47 struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
48 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
49 unsigned link = 0;
50 __u8 tos = 0;
51 __u8 ttl = 0;
52 __u8 learning = 1;
53 __u8 proxy = 0;
54 __u8 rsc = 0;
55 __u8 l2miss = 0;
56 __u8 l3miss = 0;
57 __u8 noage = 0;
58 __u32 age = 0;
59 __u32 maxaddr = 0;
60 struct ifla_vxlan_port_range range = { 0, 0 };
61
62 while (argc > 0) {
63 if (!matches(*argv, "id") ||
64 !matches(*argv, "vni")) {
65 NEXT_ARG();
66 if (get_u32(&vni, *argv, 0) ||
67 vni >= 1u << 24)
68 invarg("invalid id", *argv);
69 vni_set = 1;
70 } else if (!matches(*argv, "group")) {
71 NEXT_ARG();
72 if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
73 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
74 return -1;
75 }
76 if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
77 invarg("invalid group address", *argv);
78 } else if (!matches(*argv, "remote")) {
79 NEXT_ARG();
80 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
81 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
82 return -1;
83 }
84 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
85 invarg("invalid remote address", *argv);
86 } else if (!matches(*argv, "local")) {
87 NEXT_ARG();
88 if (strcmp(*argv, "any")) {
89 if (!inet_get_addr(*argv, &saddr, &saddr6)) {
90 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
91 return -1;
92 }
93 }
94
95 if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
96 invarg("invalid local address", *argv);
97 } else if (!matches(*argv, "dev")) {
98 NEXT_ARG();
99 link = if_nametoindex(*argv);
100 if (link == 0)
101 exit(-1);
102 } else if (!matches(*argv, "ttl") ||
103 !matches(*argv, "hoplimit")) {
104 unsigned uval;
105
106 NEXT_ARG();
107 if (strcmp(*argv, "inherit") != 0) {
108 if (get_unsigned(&uval, *argv, 0))
109 invarg("invalid TTL", *argv);
110 if (uval > 255)
111 invarg("TTL must be <= 255", *argv);
112 ttl = uval;
113 }
114 } else if (!matches(*argv, "tos") ||
115 !matches(*argv, "dsfield")) {
116 __u32 uval;
117
118 NEXT_ARG();
119 if (strcmp(*argv, "inherit") != 0) {
120 if (rtnl_dsfield_a2n(&uval, *argv))
121 invarg("bad TOS value", *argv);
122 tos = uval;
123 } else
124 tos = 1;
125 } else if (!matches(*argv, "ageing")) {
126 NEXT_ARG();
127 if (strcmp(*argv, "none") == 0)
128 noage = 1;
129 else if (get_u32(&age, *argv, 0))
130 invarg("ageing timer", *argv);
131 } else if (!matches(*argv, "maxaddress")) {
132 NEXT_ARG();
133 if (strcmp(*argv, "unlimited") == 0)
134 maxaddr = 0;
135 else if (get_u32(&maxaddr, *argv, 0))
136 invarg("max addresses", *argv);
137 } else if (!matches(*argv, "port")) {
138 __u16 minport, maxport;
139 NEXT_ARG();
140 if (get_u16(&minport, *argv, 0))
141 invarg("min port", *argv);
142 NEXT_ARG();
143 if (get_u16(&maxport, *argv, 0))
144 invarg("max port", *argv);
145 range.low = htons(minport);
146 range.high = htons(maxport);
147 } else if (!matches(*argv, "nolearning")) {
148 learning = 0;
149 } else if (!matches(*argv, "learning")) {
150 learning = 1;
151 } else if (!matches(*argv, "noproxy")) {
152 proxy = 0;
153 } else if (!matches(*argv, "proxy")) {
154 proxy = 1;
155 } else if (!matches(*argv, "norsc")) {
156 rsc = 0;
157 } else if (!matches(*argv, "rsc")) {
158 rsc = 1;
159 } else if (!matches(*argv, "nol2miss")) {
160 l2miss = 0;
161 } else if (!matches(*argv, "l2miss")) {
162 l2miss = 1;
163 } else if (!matches(*argv, "nol3miss")) {
164 l3miss = 0;
165 } else if (!matches(*argv, "l3miss")) {
166 l3miss = 1;
167 } else if (matches(*argv, "help") == 0) {
168 explain();
169 return -1;
170 } else {
171 fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
172 explain();
173 return -1;
174 }
175 argc--, argv++;
176 }
177
178 if (!vni_set) {
179 fprintf(stderr, "vxlan: missing virtual network identifier\n");
180 return -1;
181 }
182 if ((gaddr && daddr) ||
183 (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) &&
184 memcmp(&daddr6, &in6addr_any, sizeof(daddr6)))) {
185 fprintf(stderr, "vxlan: both group and remote cannot be specified\n");
186 return -1;
187 }
188 addattr32(n, 1024, IFLA_VXLAN_ID, vni);
189 if (gaddr)
190 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
191 else if (daddr)
192 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
193 if (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) != 0)
194 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &gaddr6, sizeof(struct in6_addr));
195 else if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
196 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
197
198 if (saddr)
199 addattr_l(n, 1024, IFLA_VXLAN_LOCAL, &saddr, 4);
200 else if (memcmp(&saddr6, &in6addr_any, sizeof(saddr6)) != 0)
201 addattr_l(n, 1024, IFLA_VXLAN_LOCAL6, &saddr6, sizeof(struct in6_addr));
202
203 if (link)
204 addattr32(n, 1024, IFLA_VXLAN_LINK, link);
205 addattr8(n, 1024, IFLA_VXLAN_TTL, ttl);
206 addattr8(n, 1024, IFLA_VXLAN_TOS, tos);
207 addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning);
208 addattr8(n, 1024, IFLA_VXLAN_PROXY, proxy);
209 addattr8(n, 1024, IFLA_VXLAN_RSC, rsc);
210 addattr8(n, 1024, IFLA_VXLAN_L2MISS, l2miss);
211 addattr8(n, 1024, IFLA_VXLAN_L3MISS, l3miss);
212 if (noage)
213 addattr32(n, 1024, IFLA_VXLAN_AGEING, 0);
214 else if (age)
215 addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
216 if (maxaddr)
217 addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr);
218 if (range.low || range.high)
219 addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE,
220 &range, sizeof(range));
221
222 return 0;
223 }
224
225 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
226 {
227 __u32 vni;
228 unsigned link;
229 __u8 tos;
230 __u32 maxaddr;
231 char s1[1024];
232 char s2[64];
233
234 if (!tb)
235 return;
236
237 if (!tb[IFLA_VXLAN_ID] ||
238 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
239 return;
240
241 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
242 fprintf(f, "id %u ", vni);
243
244 if (tb[IFLA_VXLAN_GROUP]) {
245 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
246 if (addr) {
247 if (IN_MULTICAST(ntohl(addr)))
248 fprintf(f, "group %s ",
249 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
250 else
251 fprintf(f, "remote %s ",
252 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
253 }
254 } else if (tb[IFLA_VXLAN_GROUP6]) {
255 struct in6_addr addr;
256 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
257 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
258 if (IN6_IS_ADDR_MULTICAST(&addr))
259 fprintf(f, "group %s ",
260 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
261 else
262 fprintf(f, "remote %s ",
263 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
264 }
265 }
266
267 if (tb[IFLA_VXLAN_LOCAL]) {
268 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
269 if (addr)
270 fprintf(f, "local %s ",
271 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
272 } else if (tb[IFLA_VXLAN_LOCAL6]) {
273 struct in6_addr addr;
274 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
275 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
276 fprintf(f, "local %s ",
277 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
278 }
279
280 if (tb[IFLA_VXLAN_LINK] &&
281 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
282 const char *n = if_indextoname(link, s2);
283
284 if (n)
285 fprintf(f, "dev %s ", n);
286 else
287 fprintf(f, "dev %u ", link);
288 }
289
290 if (tb[IFLA_VXLAN_PORT_RANGE]) {
291 const struct ifla_vxlan_port_range *r
292 = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
293 fprintf(f, "port %u %u ", ntohs(r->low), ntohs(r->high));
294 }
295
296 if (tb[IFLA_VXLAN_LEARNING] &&
297 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
298 fputs("nolearning ", f);
299
300 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
301 fputs("proxy ", f);
302
303 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
304 fputs("rsc ", f);
305
306 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
307 fputs("l2miss ", f);
308
309 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
310 fputs("l3miss ", f);
311
312 if (tb[IFLA_VXLAN_TOS] &&
313 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
314 if (tos == 1)
315 fprintf(f, "tos inherit ");
316 else
317 fprintf(f, "tos %#x ", tos);
318 }
319
320 if (tb[IFLA_VXLAN_TTL]) {
321 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
322 if (ttl)
323 fprintf(f, "ttl %d ", ttl);
324 }
325
326 if (tb[IFLA_VXLAN_AGEING]) {
327 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
328 if (age == 0)
329 fprintf(f, "ageing none ");
330 else
331 fprintf(f, "ageing %u ", age);
332 }
333
334 if (tb[IFLA_VXLAN_LIMIT] &&
335 (maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT]) != 0))
336 fprintf(f, "maxaddr %u ", maxaddr);
337 }
338
339 struct link_util vxlan_link_util = {
340 .id = "vxlan",
341 .maxattr = IFLA_VXLAN_MAX,
342 .parse_opt = vxlan_parse_opt,
343 .print_opt = vxlan_print_opt,
344 };