]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
make format_host non-reentrant by default
[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 s2[64];
314
315 if (!tb)
316 return;
317
318 if (!tb[IFLA_VXLAN_ID] ||
319 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
320 return;
321
322 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
323 fprintf(f, "id %u ", vni);
324
325 if (tb[IFLA_VXLAN_GROUP]) {
326 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
327
328 if (addr) {
329 if (IN_MULTICAST(ntohl(addr)))
330 fprintf(f, "group %s ",
331 format_host(AF_INET, 4, &addr));
332 else
333 fprintf(f, "remote %s ",
334 format_host(AF_INET, 4, &addr));
335 }
336 } else if (tb[IFLA_VXLAN_GROUP6]) {
337 struct in6_addr addr;
338
339 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
340 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
341 if (IN6_IS_ADDR_MULTICAST(&addr))
342 fprintf(f, "group %s ",
343 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
344 else
345 fprintf(f, "remote %s ",
346 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
347 }
348 }
349
350 if (tb[IFLA_VXLAN_LOCAL]) {
351 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
352
353 if (addr)
354 fprintf(f, "local %s ",
355 format_host(AF_INET, 4, &addr));
356 } else if (tb[IFLA_VXLAN_LOCAL6]) {
357 struct in6_addr addr;
358
359 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
360 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
361 fprintf(f, "local %s ",
362 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
363 }
364
365 if (tb[IFLA_VXLAN_LINK] &&
366 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
367 const char *n = if_indextoname(link, s2);
368
369 if (n)
370 fprintf(f, "dev %s ", n);
371 else
372 fprintf(f, "dev %u ", link);
373 }
374
375 if (tb[IFLA_VXLAN_PORT_RANGE]) {
376 const struct ifla_vxlan_port_range *r
377 = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
378 fprintf(f, "srcport %u %u ", ntohs(r->low), ntohs(r->high));
379 }
380
381 if (tb[IFLA_VXLAN_PORT])
382 fprintf(f, "dstport %u ",
383 ntohs(rta_getattr_u16(tb[IFLA_VXLAN_PORT])));
384
385 if (tb[IFLA_VXLAN_LEARNING] &&
386 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
387 fputs("nolearning ", f);
388
389 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
390 fputs("proxy ", f);
391
392 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
393 fputs("rsc ", f);
394
395 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
396 fputs("l2miss ", f);
397
398 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
399 fputs("l3miss ", f);
400
401 if (tb[IFLA_VXLAN_TOS] &&
402 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
403 if (tos == 1)
404 fprintf(f, "tos inherit ");
405 else
406 fprintf(f, "tos %#x ", tos);
407 }
408
409 if (tb[IFLA_VXLAN_TTL]) {
410 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
411
412 if (ttl)
413 fprintf(f, "ttl %d ", ttl);
414 }
415
416 if (tb[IFLA_VXLAN_AGEING]) {
417 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
418
419 if (age == 0)
420 fprintf(f, "ageing none ");
421 else
422 fprintf(f, "ageing %u ", age);
423 }
424
425 if (tb[IFLA_VXLAN_LIMIT] &&
426 ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
427 fprintf(f, "maxaddr %u ", maxaddr);
428
429 if (tb[IFLA_VXLAN_UDP_CSUM] && rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]))
430 fputs("udpcsum ", f);
431
432 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
433 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
434 fputs("udp6zerocsumtx ", f);
435
436 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
437 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
438 fputs("udp6zerocsumrx ", f);
439
440 if (tb[IFLA_VXLAN_REMCSUM_TX] &&
441 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]))
442 fputs("remcsumtx ", f);
443
444 if (tb[IFLA_VXLAN_REMCSUM_RX] &&
445 rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]))
446 fputs("remcsumrx ", f);
447
448 if (tb[IFLA_VXLAN_COLLECT_METADATA] &&
449 rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA]))
450 fputs("external ", f);
451
452 if (tb[IFLA_VXLAN_GBP])
453 fputs("gbp ", f);
454 }
455
456 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
457 FILE *f)
458 {
459 print_explain(f);
460 }
461
462 struct link_util vxlan_link_util = {
463 .id = "vxlan",
464 .maxattr = IFLA_VXLAN_MAX,
465 .parse_opt = vxlan_parse_opt,
466 .print_opt = vxlan_print_opt,
467 .print_help = vxlan_print_help,
468 };