]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
ip: code cleanup
[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 print_explain(FILE *f)
25 {
26 fprintf(f, "Usage: ... vxlan id VNI [ { group | remote } IP_ADDRESS ] [ local ADDR ]\n");
27 fprintf(f, " [ ttl TTL ] [ tos TOS ] [ dev PHYS_DEV ]\n");
28 fprintf(f, " [ dstport PORT ] [ srcport MIN MAX ]\n");
29 fprintf(f, " [ [no]learning ] [ [no]proxy ] [ [no]rsc ]\n");
30 fprintf(f, " [ [no]l2miss ] [ [no]l3miss ]\n");
31 fprintf(f, " [ ageing SECONDS ] [ maxaddress NUMBER ]\n");
32 fprintf(f, " [ [no]udpcsum ] [ [no]udp6zerocsumtx ] [ [no]udp6zerocsumrx ]\n");
33 fprintf(f, " [ [no]remcsumtx ] [ [no]remcsumrx ]\n");
34 fprintf(f, " [ [no]external ] [ gbp ]\n");
35 fprintf(f, "\n");
36 fprintf(f, "Where: VNI := 0-16777215\n");
37 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
38 fprintf(f, " TOS := { NUMBER | inherit }\n");
39 fprintf(f, " TTL := { 1..255 | inherit }\n");
40 }
41
42 static void explain(void)
43 {
44 print_explain(stderr);
45 }
46
47 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
48 struct nlmsghdr *n)
49 {
50 __u32 vni = 0;
51 int vni_set = 0;
52 __u32 saddr = 0;
53 __u32 gaddr = 0;
54 __u32 daddr = 0;
55 struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
56 struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
57 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
58 unsigned int link = 0;
59 __u8 tos = 0;
60 __u8 ttl = 0;
61 __u8 learning = 1;
62 __u8 proxy = 0;
63 __u8 rsc = 0;
64 __u8 l2miss = 0;
65 __u8 l3miss = 0;
66 __u8 noage = 0;
67 __u32 age = 0;
68 __u32 maxaddr = 0;
69 __u16 dstport = 0;
70 __u8 udpcsum = 0;
71 __u8 udp6zerocsumtx = 0;
72 __u8 udp6zerocsumrx = 0;
73 __u8 remcsumtx = 0;
74 __u8 remcsumrx = 0;
75 __u8 metadata = 0;
76 __u8 gbp = 0;
77 int dst_port_set = 0;
78 struct ifla_vxlan_port_range range = { 0, 0 };
79
80 while (argc > 0) {
81 if (!matches(*argv, "id") ||
82 !matches(*argv, "vni")) {
83 NEXT_ARG();
84 if (get_u32(&vni, *argv, 0) ||
85 vni >= 1u << 24)
86 invarg("invalid id", *argv);
87 vni_set = 1;
88 } else if (!matches(*argv, "group")) {
89 NEXT_ARG();
90 if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
91 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
92 return -1;
93 }
94 if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
95 invarg("invalid group address", *argv);
96 } else if (!matches(*argv, "remote")) {
97 NEXT_ARG();
98 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
99 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
100 return -1;
101 }
102 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
103 invarg("invalid remote address", *argv);
104 } else if (!matches(*argv, "local")) {
105 NEXT_ARG();
106 if (strcmp(*argv, "any")) {
107 if (!inet_get_addr(*argv, &saddr, &saddr6)) {
108 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
109 return -1;
110 }
111 }
112
113 if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
114 invarg("invalid local address", *argv);
115 } else if (!matches(*argv, "dev")) {
116 NEXT_ARG();
117 link = if_nametoindex(*argv);
118 if (link == 0) {
119 fprintf(stderr, "Cannot find device \"%s\"\n",
120 *argv);
121 exit(-1);
122 }
123 } else if (!matches(*argv, "ttl") ||
124 !matches(*argv, "hoplimit")) {
125 unsigned int uval;
126
127 NEXT_ARG();
128 if (strcmp(*argv, "inherit") != 0) {
129 if (get_unsigned(&uval, *argv, 0))
130 invarg("invalid TTL", *argv);
131 if (uval > 255)
132 invarg("TTL must be <= 255", *argv);
133 ttl = uval;
134 }
135 } else if (!matches(*argv, "tos") ||
136 !matches(*argv, "dsfield")) {
137 __u32 uval;
138
139 NEXT_ARG();
140 if (strcmp(*argv, "inherit") != 0) {
141 if (rtnl_dsfield_a2n(&uval, *argv))
142 invarg("bad TOS value", *argv);
143 tos = uval;
144 } else
145 tos = 1;
146 } else if (!matches(*argv, "ageing")) {
147 NEXT_ARG();
148 if (strcmp(*argv, "none") == 0)
149 noage = 1;
150 else if (get_u32(&age, *argv, 0))
151 invarg("ageing timer", *argv);
152 } else if (!matches(*argv, "maxaddress")) {
153 NEXT_ARG();
154 if (strcmp(*argv, "unlimited") == 0)
155 maxaddr = 0;
156 else if (get_u32(&maxaddr, *argv, 0))
157 invarg("max addresses", *argv);
158 } else if (!matches(*argv, "port") ||
159 !matches(*argv, "srcport")) {
160 __u16 minport, maxport;
161
162 NEXT_ARG();
163 if (get_u16(&minport, *argv, 0))
164 invarg("min port", *argv);
165 NEXT_ARG();
166 if (get_u16(&maxport, *argv, 0))
167 invarg("max port", *argv);
168 range.low = htons(minport);
169 range.high = htons(maxport);
170 } else if (!matches(*argv, "dstport")) {
171 NEXT_ARG();
172 if (get_u16(&dstport, *argv, 0))
173 invarg("dst port", *argv);
174 dst_port_set = 1;
175 } else if (!matches(*argv, "nolearning")) {
176 learning = 0;
177 } else if (!matches(*argv, "learning")) {
178 learning = 1;
179 } else if (!matches(*argv, "noproxy")) {
180 proxy = 0;
181 } else if (!matches(*argv, "proxy")) {
182 proxy = 1;
183 } else if (!matches(*argv, "norsc")) {
184 rsc = 0;
185 } else if (!matches(*argv, "rsc")) {
186 rsc = 1;
187 } else if (!matches(*argv, "nol2miss")) {
188 l2miss = 0;
189 } else if (!matches(*argv, "l2miss")) {
190 l2miss = 1;
191 } else if (!matches(*argv, "nol3miss")) {
192 l3miss = 0;
193 } else if (!matches(*argv, "l3miss")) {
194 l3miss = 1;
195 } else if (!matches(*argv, "udpcsum")) {
196 udpcsum = 1;
197 } else if (!matches(*argv, "noudpcsum")) {
198 udpcsum = 0;
199 } else if (!matches(*argv, "udp6zerocsumtx")) {
200 udp6zerocsumtx = 1;
201 } else if (!matches(*argv, "noudp6zerocsumtx")) {
202 udp6zerocsumtx = 0;
203 } else if (!matches(*argv, "udp6zerocsumrx")) {
204 udp6zerocsumrx = 1;
205 } else if (!matches(*argv, "noudp6zerocsumrx")) {
206 udp6zerocsumrx = 0;
207 } else if (!matches(*argv, "remcsumtx")) {
208 remcsumtx = 1;
209 } else if (!matches(*argv, "noremcsumtx")) {
210 remcsumtx = 0;
211 } else if (!matches(*argv, "remcsumrx")) {
212 remcsumrx = 1;
213 } else if (!matches(*argv, "noremcsumrx")) {
214 remcsumrx = 0;
215 } else if (!matches(*argv, "external")) {
216 metadata = 1;
217 } else if (!matches(*argv, "noexternal")) {
218 metadata = 0;
219 } else if (!matches(*argv, "gbp")) {
220 gbp = 1;
221 } else if (matches(*argv, "help") == 0) {
222 explain();
223 return -1;
224 } else {
225 fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
226 explain();
227 return -1;
228 }
229 argc--, argv++;
230 }
231
232 if (metadata && vni_set) {
233 fprintf(stderr, "vxlan: both 'external' and vni cannot be specified\n");
234 return -1;
235 }
236
237 if (!metadata && !vni_set) {
238 fprintf(stderr, "vxlan: missing virtual network identifier\n");
239 return -1;
240 }
241
242 if ((gaddr && daddr) ||
243 (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) &&
244 memcmp(&daddr6, &in6addr_any, sizeof(daddr6)))) {
245 fprintf(stderr, "vxlan: both group and remote cannot be specified\n");
246 return -1;
247 }
248
249 if (!dst_port_set) {
250 fprintf(stderr, "vxlan: destination port not specified\n"
251 "Will use Linux kernel default (non-standard value)\n");
252 fprintf(stderr,
253 "Use 'dstport 4789' to get the IANA assigned value\n"
254 "Use 'dstport 0' to get default and quiet this message\n");
255 }
256
257 addattr32(n, 1024, IFLA_VXLAN_ID, vni);
258 if (gaddr)
259 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
260 else if (daddr)
261 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
262 if (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) != 0)
263 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &gaddr6, sizeof(struct in6_addr));
264 else if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
265 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
266
267 if (saddr)
268 addattr_l(n, 1024, IFLA_VXLAN_LOCAL, &saddr, 4);
269 else if (memcmp(&saddr6, &in6addr_any, sizeof(saddr6)) != 0)
270 addattr_l(n, 1024, IFLA_VXLAN_LOCAL6, &saddr6, sizeof(struct in6_addr));
271
272 if (link)
273 addattr32(n, 1024, IFLA_VXLAN_LINK, link);
274 addattr8(n, 1024, IFLA_VXLAN_TTL, ttl);
275 addattr8(n, 1024, IFLA_VXLAN_TOS, tos);
276 addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning);
277 addattr8(n, 1024, IFLA_VXLAN_PROXY, proxy);
278 addattr8(n, 1024, IFLA_VXLAN_RSC, rsc);
279 addattr8(n, 1024, IFLA_VXLAN_L2MISS, l2miss);
280 addattr8(n, 1024, IFLA_VXLAN_L3MISS, l3miss);
281 addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, udpcsum);
282 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, udp6zerocsumtx);
283 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, udp6zerocsumrx);
284 addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, remcsumtx);
285 addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, remcsumrx);
286 addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA, metadata);
287
288 if (noage)
289 addattr32(n, 1024, IFLA_VXLAN_AGEING, 0);
290 else if (age)
291 addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
292 if (maxaddr)
293 addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr);
294 if (range.low || range.high)
295 addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE,
296 &range, sizeof(range));
297 if (dstport)
298 addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport));
299
300 if (gbp)
301 addattr_l(n, 1024, IFLA_VXLAN_GBP, NULL, 0);
302
303
304 return 0;
305 }
306
307 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
308 {
309 __u32 vni;
310 unsigned int link;
311 __u8 tos;
312 __u32 maxaddr;
313 char s1[1024];
314 char s2[64];
315
316 if (!tb)
317 return;
318
319 if (!tb[IFLA_VXLAN_ID] ||
320 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
321 return;
322
323 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
324 fprintf(f, "id %u ", vni);
325
326 if (tb[IFLA_VXLAN_GROUP]) {
327 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
328
329 if (addr) {
330 if (IN_MULTICAST(ntohl(addr)))
331 fprintf(f, "group %s ",
332 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
333 else
334 fprintf(f, "remote %s ",
335 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
336 }
337 } else if (tb[IFLA_VXLAN_GROUP6]) {
338 struct in6_addr addr;
339
340 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
341 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
342 if (IN6_IS_ADDR_MULTICAST(&addr))
343 fprintf(f, "group %s ",
344 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
345 else
346 fprintf(f, "remote %s ",
347 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
348 }
349 }
350
351 if (tb[IFLA_VXLAN_LOCAL]) {
352 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
353
354 if (addr)
355 fprintf(f, "local %s ",
356 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
357 } else if (tb[IFLA_VXLAN_LOCAL6]) {
358 struct in6_addr addr;
359
360 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
361 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
362 fprintf(f, "local %s ",
363 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
364 }
365
366 if (tb[IFLA_VXLAN_LINK] &&
367 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
368 const char *n = if_indextoname(link, s2);
369
370 if (n)
371 fprintf(f, "dev %s ", n);
372 else
373 fprintf(f, "dev %u ", link);
374 }
375
376 if (tb[IFLA_VXLAN_PORT_RANGE]) {
377 const struct ifla_vxlan_port_range *r
378 = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
379 fprintf(f, "srcport %u %u ", ntohs(r->low), ntohs(r->high));
380 }
381
382 if (tb[IFLA_VXLAN_PORT])
383 fprintf(f, "dstport %u ",
384 ntohs(rta_getattr_u16(tb[IFLA_VXLAN_PORT])));
385
386 if (tb[IFLA_VXLAN_LEARNING] &&
387 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
388 fputs("nolearning ", f);
389
390 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
391 fputs("proxy ", f);
392
393 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
394 fputs("rsc ", f);
395
396 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
397 fputs("l2miss ", f);
398
399 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
400 fputs("l3miss ", f);
401
402 if (tb[IFLA_VXLAN_TOS] &&
403 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
404 if (tos == 1)
405 fprintf(f, "tos inherit ");
406 else
407 fprintf(f, "tos %#x ", tos);
408 }
409
410 if (tb[IFLA_VXLAN_TTL]) {
411 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
412
413 if (ttl)
414 fprintf(f, "ttl %d ", ttl);
415 }
416
417 if (tb[IFLA_VXLAN_AGEING]) {
418 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
419
420 if (age == 0)
421 fprintf(f, "ageing none ");
422 else
423 fprintf(f, "ageing %u ", age);
424 }
425
426 if (tb[IFLA_VXLAN_LIMIT] &&
427 ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
428 fprintf(f, "maxaddr %u ", maxaddr);
429
430 if (tb[IFLA_VXLAN_UDP_CSUM] && rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]))
431 fputs("udpcsum ", f);
432
433 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
434 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
435 fputs("udp6zerocsumtx ", f);
436
437 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
438 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
439 fputs("udp6zerocsumrx ", f);
440
441 if (tb[IFLA_VXLAN_REMCSUM_TX] &&
442 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]))
443 fputs("remcsumtx ", f);
444
445 if (tb[IFLA_VXLAN_REMCSUM_RX] &&
446 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]))
447 fputs("remcsumrx ", f);
448
449 if (tb[IFLA_VXLAN_COLLECT_METADATA] &&
450 rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA]))
451 fputs("external ", f);
452
453 if (tb[IFLA_VXLAN_GBP])
454 fputs("gbp ", f);
455 }
456
457 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
458 FILE *f)
459 {
460 print_explain(f);
461 }
462
463 struct link_util vxlan_link_util = {
464 .id = "vxlan",
465 .maxattr = IFLA_VXLAN_MAX,
466 .parse_opt = vxlan_parse_opt,
467 .print_opt = vxlan_print_opt,
468 .print_help = vxlan_print_help,
469 };