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 #define VXLAN_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
25 
print_explain(FILE * f)26 static void print_explain(FILE *f)
27 {
28 	fprintf(f,
29 		"Usage: ... vxlan id VNI\n"
30 		"                 [ { group | remote } IP_ADDRESS ]\n"
31 		"                 [ local ADDR ]\n"
32 		"                 [ ttl TTL ]\n"
33 		"                 [ tos TOS ]\n"
34 		"                 [ flowlabel LABEL ]\n"
35 		"                 [ dev PHYS_DEV ]\n"
36 		"                 [ dstport PORT ]\n"
37 		"                 [ srcport MIN MAX ]\n"
38 		"                 [ [no]learning ]\n"
39 		"                 [ [no]proxy ]\n"
40 		"                 [ [no]rsc ]\n"
41 		"                 [ [no]l2miss ]\n"
42 		"                 [ [no]l3miss ]\n"
43 		"                 [ ageing SECONDS ]\n"
44 		"                 [ maxaddress NUMBER ]\n"
45 		"                 [ [no]udpcsum ]\n"
46 		"                 [ [no]udp6zerocsumtx ]\n"
47 		"                 [ [no]udp6zerocsumrx ]\n"
48 		"                 [ [no]remcsumtx ] [ [no]remcsumrx ]\n"
49 		"                 [ [no]external ] [ gbp ] [ gpe ]\n"
50 		"\n"
51 		"Where: VNI   := 0-16777215\n"
52 		"       ADDR  := { IP_ADDRESS | any }\n"
53 		"       TOS   := { NUMBER | inherit }\n"
54 		"       TTL   := { 1..255 | inherit }\n"
55 		"       LABEL := 0-1048575\n"
56 	);
57 }
58 
explain(void)59 static void explain(void)
60 {
61 	print_explain(stderr);
62 }
63 
check_duparg(__u64 * attrs,int type,const char * key,const char * argv)64 static void check_duparg(__u64 *attrs, int type, const char *key,
65 			 const char *argv)
66 {
67 	if (!VXLAN_ATTRSET(*attrs, type)) {
68 		*attrs |= (1L << type);
69 		return;
70 	}
71 	duparg2(key, argv);
72 }
73 
vxlan_parse_opt(struct link_util * lu,int argc,char ** argv,struct nlmsghdr * n)74 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
75 			  struct nlmsghdr *n)
76 {
77 	__u32 vni = 0;
78 	__u32 gaddr = 0;
79 	__u32 daddr = 0;
80 	struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
81 	struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
82 	__u8 learning = 1;
83 	__u16 dstport = 0;
84 	__u8 metadata = 0;
85 	__u64 attrs = 0;
86 	bool set_op = (n->nlmsg_type == RTM_NEWLINK &&
87 		       !(n->nlmsg_flags & NLM_F_CREATE));
88 
89 	while (argc > 0) {
90 		if (!matches(*argv, "id") ||
91 		    !matches(*argv, "vni")) {
92 			/* We will add ID attribute outside of the loop since we
93 			 * need to consider metadata information as well.
94 			 */
95 			NEXT_ARG();
96 			check_duparg(&attrs, IFLA_VXLAN_ID, "id", *argv);
97 			if (get_u32(&vni, *argv, 0) ||
98 			    vni >= 1u << 24)
99 				invarg("invalid id", *argv);
100 		} else if (!matches(*argv, "group")) {
101 			if (daddr || !IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
102 				fprintf(stderr, "vxlan: both group and remote");
103 				fprintf(stderr, " cannot be specified\n");
104 				return -1;
105 			}
106 			NEXT_ARG();
107 			check_duparg(&attrs, IFLA_VXLAN_GROUP, "group", *argv);
108 			if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
109 				fprintf(stderr, "Invalid address \"%s\"\n", *argv);
110 				return -1;
111 			}
112 			if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
113 				invarg("invalid group address", *argv);
114 		} else if (!matches(*argv, "remote")) {
115 			if (gaddr || !IN6_IS_ADDR_UNSPECIFIED(&gaddr6)) {
116 				fprintf(stderr, "vxlan: both group and remote");
117 				fprintf(stderr, " cannot be specified\n");
118 				return -1;
119 			}
120 			NEXT_ARG();
121 			check_duparg(&attrs, IFLA_VXLAN_GROUP, "remote", *argv);
122 			if (!inet_get_addr(*argv, &daddr, &daddr6)) {
123 				fprintf(stderr, "Invalid address \"%s\"\n", *argv);
124 				return -1;
125 			}
126 			if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
127 				invarg("invalid remote address", *argv);
128 		} else if (!matches(*argv, "local")) {
129 			__u32 saddr = 0;
130 			struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
131 
132 			NEXT_ARG();
133 			check_duparg(&attrs, IFLA_VXLAN_LOCAL, "local", *argv);
134 			if (strcmp(*argv, "any")) {
135 				if (!inet_get_addr(*argv, &saddr, &saddr6)) {
136 					fprintf(stderr, "Invalid address \"%s\"\n", *argv);
137 					return -1;
138 				}
139 			}
140 
141 			if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
142 				invarg("invalid local address", *argv);
143 
144 			if (saddr)
145 				addattr_l(n, 1024, IFLA_VXLAN_LOCAL, &saddr, 4);
146 			else if (!IN6_IS_ADDR_UNSPECIFIED(&saddr6))
147 				addattr_l(n, 1024, IFLA_VXLAN_LOCAL6, &saddr6,
148 					  sizeof(struct in6_addr));
149 		} else if (!matches(*argv, "dev")) {
150 			unsigned int link;
151 
152 			NEXT_ARG();
153 			check_duparg(&attrs, IFLA_VXLAN_LINK, "dev", *argv);
154 			link = if_nametoindex(*argv);
155 			if (link == 0) {
156 				fprintf(stderr, "Cannot find device \"%s\"\n",
157 					*argv);
158 				exit(-1);
159 			}
160 			addattr32(n, 1024, IFLA_VXLAN_LINK, link);
161 		} else if (!matches(*argv, "ttl") ||
162 			   !matches(*argv, "hoplimit")) {
163 			unsigned int uval;
164 			__u8 ttl = 0;
165 
166 			NEXT_ARG();
167 			check_duparg(&attrs, IFLA_VXLAN_TTL, "ttl", *argv);
168 			if (strcmp(*argv, "inherit") != 0) {
169 				if (get_unsigned(&uval, *argv, 0))
170 					invarg("invalid TTL", *argv);
171 				if (uval > 255)
172 					invarg("TTL must be <= 255", *argv);
173 				ttl = uval;
174 			}
175 			addattr8(n, 1024, IFLA_VXLAN_TTL, ttl);
176 		} else if (!matches(*argv, "tos") ||
177 			   !matches(*argv, "dsfield")) {
178 			__u32 uval;
179 			__u8 tos;
180 
181 			NEXT_ARG();
182 			check_duparg(&attrs, IFLA_VXLAN_TOS, "tos", *argv);
183 			if (strcmp(*argv, "inherit") != 0) {
184 				if (rtnl_dsfield_a2n(&uval, *argv))
185 					invarg("bad TOS value", *argv);
186 				tos = uval;
187 			} else
188 				tos = 1;
189 			addattr8(n, 1024, IFLA_VXLAN_TOS, tos);
190 		} else if (!matches(*argv, "label") ||
191 			   !matches(*argv, "flowlabel")) {
192 			__u32 uval;
193 
194 			NEXT_ARG();
195 			check_duparg(&attrs, IFLA_VXLAN_LABEL, "flowlabel",
196 				     *argv);
197 			if (get_u32(&uval, *argv, 0) ||
198 			    (uval & ~LABEL_MAX_MASK))
199 				invarg("invalid flowlabel", *argv);
200 			addattr32(n, 1024, IFLA_VXLAN_LABEL, htonl(uval));
201 		} else if (!matches(*argv, "ageing")) {
202 			__u32 age;
203 
204 			NEXT_ARG();
205 			check_duparg(&attrs, IFLA_VXLAN_AGEING, "ageing",
206 				     *argv);
207 			if (strcmp(*argv, "none") == 0)
208 				age = 0;
209 			else if (get_u32(&age, *argv, 0))
210 				invarg("ageing timer", *argv);
211 			addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
212 		} else if (!matches(*argv, "maxaddress")) {
213 			__u32 maxaddr;
214 
215 			NEXT_ARG();
216 			check_duparg(&attrs, IFLA_VXLAN_LIMIT,
217 				     "maxaddress", *argv);
218 			if (strcmp(*argv, "unlimited") == 0)
219 				maxaddr = 0;
220 			else if (get_u32(&maxaddr, *argv, 0))
221 				invarg("max addresses", *argv);
222 			addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr);
223 		} else if (!matches(*argv, "port") ||
224 			   !matches(*argv, "srcport")) {
225 			struct ifla_vxlan_port_range range = { 0, 0 };
226 
227 			NEXT_ARG();
228 			check_duparg(&attrs, IFLA_VXLAN_PORT_RANGE, "srcport",
229 				     *argv);
230 			if (get_be16(&range.low, *argv, 0))
231 				invarg("min port", *argv);
232 			NEXT_ARG();
233 			if (get_be16(&range.high, *argv, 0))
234 				invarg("max port", *argv);
235 			if (range.low || range.high) {
236 				addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE,
237 					  &range, sizeof(range));
238 			}
239 		} else if (!matches(*argv, "dstport")) {
240 			NEXT_ARG();
241 			check_duparg(&attrs, IFLA_VXLAN_PORT, "dstport", *argv);
242 			if (get_u16(&dstport, *argv, 0))
243 				invarg("dst port", *argv);
244 		} else if (!matches(*argv, "nolearning")) {
245 			check_duparg(&attrs, IFLA_VXLAN_LEARNING, *argv, *argv);
246 			learning = 0;
247 		} else if (!matches(*argv, "learning")) {
248 			check_duparg(&attrs, IFLA_VXLAN_LEARNING, *argv, *argv);
249 			learning = 1;
250 		} else if (!matches(*argv, "noproxy")) {
251 			check_duparg(&attrs, IFLA_VXLAN_PROXY, *argv, *argv);
252 			addattr8(n, 1024, IFLA_VXLAN_PROXY, 0);
253 		} else if (!matches(*argv, "proxy")) {
254 			check_duparg(&attrs, IFLA_VXLAN_PROXY, *argv, *argv);
255 			addattr8(n, 1024, IFLA_VXLAN_PROXY, 1);
256 		} else if (!matches(*argv, "norsc")) {
257 			check_duparg(&attrs, IFLA_VXLAN_RSC, *argv, *argv);
258 			addattr8(n, 1024, IFLA_VXLAN_RSC, 0);
259 		} else if (!matches(*argv, "rsc")) {
260 			check_duparg(&attrs, IFLA_VXLAN_RSC, *argv, *argv);
261 			addattr8(n, 1024, IFLA_VXLAN_RSC, 1);
262 		} else if (!matches(*argv, "nol2miss")) {
263 			check_duparg(&attrs, IFLA_VXLAN_L2MISS, *argv, *argv);
264 			addattr8(n, 1024, IFLA_VXLAN_L2MISS, 0);
265 		} else if (!matches(*argv, "l2miss")) {
266 			check_duparg(&attrs, IFLA_VXLAN_L2MISS, *argv, *argv);
267 			addattr8(n, 1024, IFLA_VXLAN_L2MISS, 1);
268 		} else if (!matches(*argv, "nol3miss")) {
269 			check_duparg(&attrs, IFLA_VXLAN_L3MISS, *argv, *argv);
270 			addattr8(n, 1024, IFLA_VXLAN_L3MISS, 0);
271 		} else if (!matches(*argv, "l3miss")) {
272 			check_duparg(&attrs, IFLA_VXLAN_L3MISS, *argv, *argv);
273 			addattr8(n, 1024, IFLA_VXLAN_L3MISS, 1);
274 		} else if (!matches(*argv, "udpcsum")) {
275 			check_duparg(&attrs, IFLA_VXLAN_UDP_CSUM, *argv, *argv);
276 			addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, 1);
277 		} else if (!matches(*argv, "noudpcsum")) {
278 			check_duparg(&attrs, IFLA_VXLAN_UDP_CSUM, *argv, *argv);
279 			addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, 0);
280 		} else if (!matches(*argv, "udp6zerocsumtx")) {
281 			check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
282 				     *argv, *argv);
283 			addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 1);
284 		} else if (!matches(*argv, "noudp6zerocsumtx")) {
285 			check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
286 				     *argv, *argv);
287 			addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 0);
288 		} else if (!matches(*argv, "udp6zerocsumrx")) {
289 			check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
290 				     *argv, *argv);
291 			addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1);
292 		} else if (!matches(*argv, "noudp6zerocsumrx")) {
293 			check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
294 				     *argv, *argv);
295 			addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 0);
296 		} else if (!matches(*argv, "remcsumtx")) {
297 			check_duparg(&attrs, IFLA_VXLAN_REMCSUM_TX,
298 				     *argv, *argv);
299 			addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, 1);
300 		} else if (!matches(*argv, "noremcsumtx")) {
301 			check_duparg(&attrs, IFLA_VXLAN_REMCSUM_TX,
302 				     *argv, *argv);
303 			addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, 0);
304 		} else if (!matches(*argv, "remcsumrx")) {
305 			check_duparg(&attrs, IFLA_VXLAN_REMCSUM_RX,
306 				     *argv, *argv);
307 			addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, 1);
308 		} else if (!matches(*argv, "noremcsumrx")) {
309 			check_duparg(&attrs, IFLA_VXLAN_REMCSUM_RX,
310 				     *argv, *argv);
311 			addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, 0);
312 		} else if (!matches(*argv, "external")) {
313 			check_duparg(&attrs, IFLA_VXLAN_COLLECT_METADATA,
314 				     *argv, *argv);
315 			metadata = 1;
316 			learning = 0;
317 			/* we will add LEARNING attribute outside of the loop */
318 			addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA,
319 				 metadata);
320 		} else if (!matches(*argv, "noexternal")) {
321 			check_duparg(&attrs, IFLA_VXLAN_COLLECT_METADATA,
322 				     *argv, *argv);
323 			metadata = 0;
324 			addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA,
325 				 metadata);
326 		} else if (!matches(*argv, "gbp")) {
327 			check_duparg(&attrs, IFLA_VXLAN_GBP, *argv, *argv);
328 			addattr_l(n, 1024, IFLA_VXLAN_GBP, NULL, 0);
329 		} else if (!matches(*argv, "gpe")) {
330 			check_duparg(&attrs, IFLA_VXLAN_GPE, *argv, *argv);
331 			addattr_l(n, 1024, IFLA_VXLAN_GPE, NULL, 0);
332 		} else if (matches(*argv, "help") == 0) {
333 			explain();
334 			return -1;
335 		} else {
336 			fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
337 			explain();
338 			return -1;
339 		}
340 		argc--, argv++;
341 	}
342 
343 	if (metadata && VXLAN_ATTRSET(attrs, IFLA_VXLAN_ID)) {
344 		fprintf(stderr, "vxlan: both 'external' and vni cannot be specified\n");
345 		return -1;
346 	}
347 
348 	if (!metadata && !VXLAN_ATTRSET(attrs, IFLA_VXLAN_ID)) {
349 		fprintf(stderr, "vxlan: missing virtual network identifier\n");
350 		return -1;
351 	}
352 
353 	if ((gaddr || !IN6_IS_ADDR_UNSPECIFIED(&gaddr6)) &&
354 	    !VXLAN_ATTRSET(attrs, IFLA_VXLAN_LINK)) {
355 		fprintf(stderr, "vxlan: 'group' requires 'dev' to be specified\n");
356 		return -1;
357 	}
358 
359 	if (!VXLAN_ATTRSET(attrs, IFLA_VXLAN_PORT) &&
360 	    VXLAN_ATTRSET(attrs, IFLA_VXLAN_GPE)) {
361 		dstport = 4790;
362 	} else if (!VXLAN_ATTRSET(attrs, IFLA_VXLAN_PORT) && !set_op) {
363 		fprintf(stderr, "vxlan: destination port not specified\n"
364 			"Will use Linux kernel default (non-standard value)\n");
365 		fprintf(stderr,
366 			"Use 'dstport 4789' to get the IANA assigned value\n"
367 			"Use 'dstport 0' to get default and quiet this message\n");
368 	}
369 
370 	addattr32(n, 1024, IFLA_VXLAN_ID, vni);
371 	if (gaddr)
372 		addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
373 	else if (daddr)
374 		addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
375 	else if (!IN6_IS_ADDR_UNSPECIFIED(&gaddr6))
376 		addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &gaddr6, sizeof(struct in6_addr));
377 	else if (!IN6_IS_ADDR_UNSPECIFIED(&daddr6))
378 		addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
379 	else if (preferred_family == AF_INET)
380 		addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
381 	else if (preferred_family == AF_INET6)
382 		addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
383 
384 	if (!set_op || VXLAN_ATTRSET(attrs, IFLA_VXLAN_LEARNING))
385 		addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning);
386 
387 	if (dstport)
388 		addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport));
389 
390 	return 0;
391 }
392 
vxlan_print_opt(struct link_util * lu,FILE * f,struct rtattr * tb[])393 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
394 {
395 	__u32 vni;
396 	unsigned int link;
397 	__u8 tos;
398 	__u32 maxaddr;
399 	char s2[64];
400 
401 	if (!tb)
402 		return;
403 
404 	if (!tb[IFLA_VXLAN_ID] ||
405 	    RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
406 		return;
407 
408 	vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
409 	print_uint(PRINT_ANY, "id", "id %u ", vni);
410 
411 	if (tb[IFLA_VXLAN_GROUP]) {
412 		__be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
413 
414 		if (addr) {
415 			if (IN_MULTICAST(ntohl(addr)))
416 				print_string(PRINT_ANY,
417 					     "group",
418 					     "group %s ",
419 					     format_host(AF_INET, 4, &addr));
420 			else
421 				print_string(PRINT_ANY,
422 					     "remote",
423 					     "remote %s ",
424 					     format_host(AF_INET, 4, &addr));
425 		}
426 	} else if (tb[IFLA_VXLAN_GROUP6]) {
427 		struct in6_addr addr;
428 
429 		memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
430 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr)) {
431 			if (IN6_IS_ADDR_MULTICAST(&addr))
432 				print_string(PRINT_ANY,
433 					     "group6",
434 					     "group %s ",
435 					     format_host(AF_INET6,
436 							 sizeof(struct in6_addr),
437 							 &addr));
438 			else
439 				print_string(PRINT_ANY,
440 					     "remote6",
441 					     "remote %s ",
442 					     format_host(AF_INET6,
443 							 sizeof(struct in6_addr),
444 							 &addr));
445 		}
446 	}
447 
448 	if (tb[IFLA_VXLAN_LOCAL]) {
449 		__be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
450 
451 		if (addr)
452 			print_string(PRINT_ANY,
453 				     "local",
454 				     "local %s ",
455 				     format_host(AF_INET, 4, &addr));
456 	} else if (tb[IFLA_VXLAN_LOCAL6]) {
457 		struct in6_addr addr;
458 
459 		memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
460 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr))
461 			print_string(PRINT_ANY,
462 				     "local6",
463 				     "local %s ",
464 				     format_host(AF_INET6,
465 						 sizeof(struct in6_addr),
466 						 &addr));
467 	}
468 
469 	if (tb[IFLA_VXLAN_LINK] &&
470 	    (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
471 		const char *n = if_indextoname(link, s2);
472 
473 		if (n)
474 			print_string(PRINT_ANY, "link", "dev %s ", n);
475 		else
476 			print_uint(PRINT_ANY, "link_index", "dev %u ", link);
477 	}
478 
479 	if (tb[IFLA_VXLAN_PORT_RANGE]) {
480 		const struct ifla_vxlan_port_range *r
481 			= RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
482 		if (is_json_context()) {
483 			open_json_object("port_range");
484 			print_uint(PRINT_JSON, "low", NULL, ntohs(r->low));
485 			print_uint(PRINT_JSON, "high", NULL, ntohs(r->high));
486 			close_json_object();
487 		} else {
488 			fprintf(f, "srcport %u %u ",
489 				ntohs(r->low), ntohs(r->high));
490 		}
491 	}
492 
493 	if (tb[IFLA_VXLAN_PORT])
494 		print_uint(PRINT_ANY,
495 			   "port",
496 			   "dstport %u ",
497 			   rta_getattr_be16(tb[IFLA_VXLAN_PORT]));
498 
499 	if (tb[IFLA_VXLAN_LEARNING]) {
500 		__u8 learning = rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]);
501 
502 		print_bool(PRINT_JSON, "learning", NULL, learning);
503 		if (!learning)
504 			print_bool(PRINT_FP, NULL, "nolearning ", true);
505 	}
506 
507 	if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
508 		print_bool(PRINT_ANY, "proxy", "proxy ", true);
509 
510 	if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
511 		print_bool(PRINT_ANY, "rsc", "rsc ", true);
512 
513 	if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
514 		print_bool(PRINT_ANY, "l2miss", "l2miss ", true);
515 
516 	if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
517 		print_bool(PRINT_ANY, "l3miss", "l3miss ", true);
518 
519 	if (tb[IFLA_VXLAN_TOS] &&
520 	    (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
521 		if (is_json_context()) {
522 			print_0xhex(PRINT_JSON, "tos", "%#x", tos);
523 		} else {
524 			if (tos == 1)
525 				fprintf(f, "tos %s ", "inherit");
526 			else
527 				fprintf(f, "tos %#x ", tos);
528 		}
529 	}
530 
531 	if (tb[IFLA_VXLAN_TTL]) {
532 		__u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
533 
534 		if (ttl)
535 			print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
536 		else
537 			print_int(PRINT_JSON, "ttl", NULL, ttl);
538 	}
539 
540 	if (tb[IFLA_VXLAN_LABEL]) {
541 		__u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]);
542 
543 		if (label)
544 			print_0xhex(PRINT_ANY,
545 				    "label",
546 				    "flowlabel %#x ",
547 				    ntohl(label));
548 	}
549 
550 	if (tb[IFLA_VXLAN_AGEING]) {
551 		__u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
552 
553 		if (age == 0)
554 			print_uint(PRINT_ANY, "ageing", "ageing none ", 0);
555 		else
556 			print_uint(PRINT_ANY, "ageing", "ageing %u ", age);
557 	}
558 
559 	if (tb[IFLA_VXLAN_LIMIT] &&
560 	    ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
561 		print_uint(PRINT_ANY, "limit", "maxaddr %u ", maxaddr);
562 
563 	if (tb[IFLA_VXLAN_UDP_CSUM]) {
564 		__u8 udp_csum = rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]);
565 
566 		if (is_json_context()) {
567 			print_bool(PRINT_ANY, "udp_csum", NULL, udp_csum);
568 		} else {
569 			if (!udp_csum)
570 				fputs("no", f);
571 			fputs("udpcsum ", f);
572 		}
573 	}
574 
575 	if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
576 		__u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]);
577 
578 		if (is_json_context()) {
579 			print_bool(PRINT_ANY,
580 				   "udp_zero_csum6_tx", NULL, csum6);
581 		} else {
582 			if (!csum6)
583 				fputs("no", f);
584 			fputs("udp6zerocsumtx ", f);
585 		}
586 	}
587 
588 	if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
589 		__u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]);
590 
591 		if (is_json_context()) {
592 			print_bool(PRINT_ANY,
593 				   "udp_zero_csum6_rx",
594 				   NULL,
595 				   csum6);
596 		} else {
597 			if (!csum6)
598 				fputs("no", f);
599 			fputs("udp6zerocsumrx ", f);
600 		}
601 	}
602 
603 	if (tb[IFLA_VXLAN_REMCSUM_TX] &&
604 	    rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX]))
605 		print_bool(PRINT_ANY, "remcsum_tx", "remcsumtx ", true);
606 
607 	if (tb[IFLA_VXLAN_REMCSUM_RX] &&
608 	    rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX]))
609 		print_bool(PRINT_ANY, "remcsum_rx", "remcsumrx ", true);
610 
611 	if (tb[IFLA_VXLAN_COLLECT_METADATA] &&
612 	    rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA]))
613 		print_bool(PRINT_ANY, "collect_metadata", "external ", true);
614 
615 	if (tb[IFLA_VXLAN_GBP])
616 		print_bool(PRINT_ANY, "gbp", "gbp ", true);
617 	if (tb[IFLA_VXLAN_GPE])
618 		print_bool(PRINT_ANY, "gpe", "gpe ", true);
619 }
620 
vxlan_print_help(struct link_util * lu,int argc,char ** argv,FILE * f)621 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
622 			     FILE *f)
623 {
624 	print_explain(f);
625 }
626 
627 struct link_util vxlan_link_util = {
628 	.id		= "vxlan",
629 	.maxattr	= IFLA_VXLAN_MAX,
630 	.parse_opt	= vxlan_parse_opt,
631 	.print_opt	= vxlan_print_opt,
632 	.print_help	= vxlan_print_help,
633 };
634