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