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