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