2 * iplink_vxlan.c VXLAN device support
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.
9 * Authors: Stephen Hemminger <shemminger@vyatta.com
17 #include <linux/if_link.h>
18 #include <arpa/inet.h>
22 #include "ip_common.h"
24 static void print_explain(FILE *f
)
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");
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");
42 static void explain(void)
44 print_explain(stderr
);
47 static int vxlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
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;
71 __u8 udp6zerocsumtx
= 0;
72 __u8 udp6zerocsumrx
= 0;
78 struct ifla_vxlan_port_range range
= { 0, 0 };
81 if (!matches(*argv
, "id") ||
82 !matches(*argv
, "vni")) {
84 if (get_u32(&vni
, *argv
, 0) ||
86 invarg("invalid id", *argv
);
88 } else if (!matches(*argv
, "group")) {
90 if (!inet_get_addr(*argv
, &gaddr
, &gaddr6
)) {
91 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
94 if (!IN6_IS_ADDR_MULTICAST(&gaddr6
) && !IN_MULTICAST(ntohl(gaddr
)))
95 invarg("invalid group address", *argv
);
96 } else if (!matches(*argv
, "remote")) {
98 if (!inet_get_addr(*argv
, &daddr
, &daddr6
)) {
99 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
102 if (IN6_IS_ADDR_MULTICAST(&daddr6
) || IN_MULTICAST(ntohl(daddr
)))
103 invarg("invalid remote address", *argv
);
104 } else if (!matches(*argv
, "local")) {
106 if (strcmp(*argv
, "any")) {
107 if (!inet_get_addr(*argv
, &saddr
, &saddr6
)) {
108 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
113 if (IN_MULTICAST(ntohl(saddr
)) || IN6_IS_ADDR_MULTICAST(&saddr6
))
114 invarg("invalid local address", *argv
);
115 } else if (!matches(*argv
, "dev")) {
117 link
= if_nametoindex(*argv
);
119 fprintf(stderr
, "Cannot find device \"%s\"\n",
123 } else if (!matches(*argv
, "ttl") ||
124 !matches(*argv
, "hoplimit")) {
128 if (strcmp(*argv
, "inherit") != 0) {
129 if (get_unsigned(&uval
, *argv
, 0))
130 invarg("invalid TTL", *argv
);
132 invarg("TTL must be <= 255", *argv
);
135 } else if (!matches(*argv
, "tos") ||
136 !matches(*argv
, "dsfield")) {
140 if (strcmp(*argv
, "inherit") != 0) {
141 if (rtnl_dsfield_a2n(&uval
, *argv
))
142 invarg("bad TOS value", *argv
);
146 } else if (!matches(*argv
, "ageing")) {
148 if (strcmp(*argv
, "none") == 0)
150 else if (get_u32(&age
, *argv
, 0))
151 invarg("ageing timer", *argv
);
152 } else if (!matches(*argv
, "maxaddress")) {
154 if (strcmp(*argv
, "unlimited") == 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
;
163 if (get_u16(&minport
, *argv
, 0))
164 invarg("min port", *argv
);
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")) {
172 if (get_u16(&dstport
, *argv
, 0))
173 invarg("dst port", *argv
);
175 } else if (!matches(*argv
, "nolearning")) {
177 } else if (!matches(*argv
, "learning")) {
179 } else if (!matches(*argv
, "noproxy")) {
181 } else if (!matches(*argv
, "proxy")) {
183 } else if (!matches(*argv
, "norsc")) {
185 } else if (!matches(*argv
, "rsc")) {
187 } else if (!matches(*argv
, "nol2miss")) {
189 } else if (!matches(*argv
, "l2miss")) {
191 } else if (!matches(*argv
, "nol3miss")) {
193 } else if (!matches(*argv
, "l3miss")) {
195 } else if (!matches(*argv
, "udpcsum")) {
197 } else if (!matches(*argv
, "noudpcsum")) {
199 } else if (!matches(*argv
, "udp6zerocsumtx")) {
201 } else if (!matches(*argv
, "noudp6zerocsumtx")) {
203 } else if (!matches(*argv
, "udp6zerocsumrx")) {
205 } else if (!matches(*argv
, "noudp6zerocsumrx")) {
207 } else if (!matches(*argv
, "remcsumtx")) {
209 } else if (!matches(*argv
, "noremcsumtx")) {
211 } else if (!matches(*argv
, "remcsumrx")) {
213 } else if (!matches(*argv
, "noremcsumrx")) {
215 } else if (!matches(*argv
, "external")) {
217 } else if (!matches(*argv
, "noexternal")) {
219 } else if (!matches(*argv
, "gbp")) {
221 } else if (matches(*argv
, "help") == 0) {
225 fprintf(stderr
, "vxlan: unknown command \"%s\"?\n", *argv
);
232 if (metadata
&& vni_set
) {
233 fprintf(stderr
, "vxlan: both 'external' and vni cannot be specified\n");
237 if (!metadata
&& !vni_set
) {
238 fprintf(stderr
, "vxlan: missing virtual network identifier\n");
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");
250 fprintf(stderr
, "vxlan: destination port not specified\n"
251 "Will use Linux kernel default (non-standard value)\n");
253 "Use 'dstport 4789' to get the IANA assigned value\n"
254 "Use 'dstport 0' to get default and quiet this message\n");
257 addattr32(n
, 1024, IFLA_VXLAN_ID
, vni
);
259 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &gaddr
, 4);
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
));
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
));
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
);
289 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, 0);
291 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, age
);
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
));
298 addattr16(n
, 1024, IFLA_VXLAN_PORT
, htons(dstport
));
301 addattr_l(n
, 1024, IFLA_VXLAN_GBP
, NULL
, 0);
307 static void vxlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
319 if (!tb
[IFLA_VXLAN_ID
] ||
320 RTA_PAYLOAD(tb
[IFLA_VXLAN_ID
]) < sizeof(__u32
))
323 vni
= rta_getattr_u32(tb
[IFLA_VXLAN_ID
]);
324 fprintf(f
, "id %u ", vni
);
326 if (tb
[IFLA_VXLAN_GROUP
]) {
327 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_GROUP
]);
330 if (IN_MULTICAST(ntohl(addr
)))
331 fprintf(f
, "group %s ",
332 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
334 fprintf(f
, "remote %s ",
335 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
337 } else if (tb
[IFLA_VXLAN_GROUP6
]) {
338 struct in6_addr addr
;
340 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_GROUP6
]), sizeof(struct in6_addr
));
341 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0) {
342 if (IN6_IS_ADDR_MULTICAST(&addr
))
343 fprintf(f
, "group %s ",
344 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
346 fprintf(f
, "remote %s ",
347 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
351 if (tb
[IFLA_VXLAN_LOCAL
]) {
352 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_LOCAL
]);
355 fprintf(f
, "local %s ",
356 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
357 } else if (tb
[IFLA_VXLAN_LOCAL6
]) {
358 struct in6_addr addr
;
360 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_LOCAL6
]), sizeof(struct in6_addr
));
361 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0)
362 fprintf(f
, "local %s ",
363 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
366 if (tb
[IFLA_VXLAN_LINK
] &&
367 (link
= rta_getattr_u32(tb
[IFLA_VXLAN_LINK
]))) {
368 const char *n
= if_indextoname(link
, s2
);
371 fprintf(f
, "dev %s ", n
);
373 fprintf(f
, "dev %u ", link
);
376 if (tb
[IFLA_VXLAN_PORT_RANGE
]) {
377 const struct ifla_vxlan_port_range
*r
378 = RTA_DATA(tb
[IFLA_VXLAN_PORT_RANGE
]);
379 fprintf(f
, "srcport %u %u ", ntohs(r
->low
), ntohs(r
->high
));
382 if (tb
[IFLA_VXLAN_PORT
])
383 fprintf(f
, "dstport %u ",
384 ntohs(rta_getattr_u16(tb
[IFLA_VXLAN_PORT
])));
386 if (tb
[IFLA_VXLAN_LEARNING
] &&
387 !rta_getattr_u8(tb
[IFLA_VXLAN_LEARNING
]))
388 fputs("nolearning ", f
);
390 if (tb
[IFLA_VXLAN_PROXY
] && rta_getattr_u8(tb
[IFLA_VXLAN_PROXY
]))
393 if (tb
[IFLA_VXLAN_RSC
] && rta_getattr_u8(tb
[IFLA_VXLAN_RSC
]))
396 if (tb
[IFLA_VXLAN_L2MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L2MISS
]))
399 if (tb
[IFLA_VXLAN_L3MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L3MISS
]))
402 if (tb
[IFLA_VXLAN_TOS
] &&
403 (tos
= rta_getattr_u8(tb
[IFLA_VXLAN_TOS
]))) {
405 fprintf(f
, "tos inherit ");
407 fprintf(f
, "tos %#x ", tos
);
410 if (tb
[IFLA_VXLAN_TTL
]) {
411 __u8 ttl
= rta_getattr_u8(tb
[IFLA_VXLAN_TTL
]);
414 fprintf(f
, "ttl %d ", ttl
);
417 if (tb
[IFLA_VXLAN_AGEING
]) {
418 __u32 age
= rta_getattr_u32(tb
[IFLA_VXLAN_AGEING
]);
421 fprintf(f
, "ageing none ");
423 fprintf(f
, "ageing %u ", age
);
426 if (tb
[IFLA_VXLAN_LIMIT
] &&
427 ((maxaddr
= rta_getattr_u32(tb
[IFLA_VXLAN_LIMIT
])) != 0))
428 fprintf(f
, "maxaddr %u ", maxaddr
);
430 if (tb
[IFLA_VXLAN_UDP_CSUM
] && rta_getattr_u8(tb
[IFLA_VXLAN_UDP_CSUM
]))
431 fputs("udpcsum ", f
);
433 if (tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
434 rta_getattr_u8(tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
435 fputs("udp6zerocsumtx ", f
);
437 if (tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
438 rta_getattr_u8(tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
439 fputs("udp6zerocsumrx ", f
);
441 if (tb
[IFLA_VXLAN_REMCSUM_TX
] &&
442 rta_getattr_u8(tb
[IFLA_VXLAN_REMCSUM_TX
]))
443 fputs("remcsumtx ", f
);
445 if (tb
[IFLA_VXLAN_REMCSUM_RX
] &&
446 rta_getattr_u8(tb
[IFLA_VXLAN_REMCSUM_RX
]))
447 fputs("remcsumrx ", f
);
449 if (tb
[IFLA_VXLAN_COLLECT_METADATA
] &&
450 rta_getattr_u8(tb
[IFLA_VXLAN_COLLECT_METADATA
]))
451 fputs("external ", f
);
453 if (tb
[IFLA_VXLAN_GBP
])
457 static void vxlan_print_help(struct link_util
*lu
, int argc
, char **argv
,
463 struct link_util vxlan_link_util
= {
465 .maxattr
= IFLA_VXLAN_MAX
,
466 .parse_opt
= vxlan_parse_opt
,
467 .print_opt
= vxlan_print_opt
,
468 .print_help
= vxlan_print_help
,