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 } 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");
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
;
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
;
162 if (get_u16(&minport
, *argv
, 0))
163 invarg("min port", *argv
);
165 if (get_u16(&maxport
, *argv
, 0))
166 invarg("max port", *argv
);
167 range
.low
= htons(minport
);
168 range
.high
= htons(maxport
);
169 } else if (!matches(*argv
, "dstport")){
171 if (get_u16(&dstport
, *argv
, 0))
172 invarg("dst port", *argv
);
174 } else if (!matches(*argv
, "nolearning")) {
176 } else if (!matches(*argv
, "learning")) {
178 } else if (!matches(*argv
, "noproxy")) {
180 } else if (!matches(*argv
, "proxy")) {
182 } else if (!matches(*argv
, "norsc")) {
184 } else if (!matches(*argv
, "rsc")) {
186 } else if (!matches(*argv
, "nol2miss")) {
188 } else if (!matches(*argv
, "l2miss")) {
190 } else if (!matches(*argv
, "nol3miss")) {
192 } else if (!matches(*argv
, "l3miss")) {
194 } else if (!matches(*argv
, "udpcsum")) {
196 } else if (!matches(*argv
, "noudpcsum")) {
198 } else if (!matches(*argv
, "udp6zerocsumtx")) {
200 } else if (!matches(*argv
, "noudp6zerocsumtx")) {
202 } else if (!matches(*argv
, "udp6zerocsumrx")) {
204 } else if (!matches(*argv
, "noudp6zerocsumrx")) {
206 } else if (!matches(*argv
, "remcsumtx")) {
208 } else if (!matches(*argv
, "noremcsumtx")) {
210 } else if (!matches(*argv
, "remcsumrx")) {
212 } else if (!matches(*argv
, "noremcsumrx")) {
214 } else if (!matches(*argv
, "external")) {
216 } else if (!matches(*argv
, "noexternal")) {
218 } else if (!matches(*argv
, "gbp")) {
220 } else if (matches(*argv
, "help") == 0) {
224 fprintf(stderr
, "vxlan: unknown command \"%s\"?\n", *argv
);
231 if (metadata
&& vni_set
) {
232 fprintf(stderr
, "vxlan: both 'external' and vni cannot be specified\n");
236 if (!metadata
&& !vni_set
) {
237 fprintf(stderr
, "vxlan: missing virtual network identifier\n");
241 if ((gaddr
&& daddr
) ||
242 (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) &&
243 memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)))) {
244 fprintf(stderr
, "vxlan: both group and remote cannot be specified\n");
249 fprintf(stderr
, "vxlan: destination port not specified\n"
250 "Will use Linux kernel default (non-standard value)\n");
252 "Use 'dstport 4789' to get the IANA assigned value\n"
253 "Use 'dstport 0' to get default and quiet this message\n");
256 addattr32(n
, 1024, IFLA_VXLAN_ID
, vni
);
258 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &gaddr
, 4);
260 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &daddr
, 4);
261 if (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) != 0)
262 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &gaddr6
, sizeof(struct in6_addr
));
263 else if (memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)) != 0)
264 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &daddr6
, sizeof(struct in6_addr
));
267 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL
, &saddr
, 4);
268 else if (memcmp(&saddr6
, &in6addr_any
, sizeof(saddr6
)) != 0)
269 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL6
, &saddr6
, sizeof(struct in6_addr
));
272 addattr32(n
, 1024, IFLA_VXLAN_LINK
, link
);
273 addattr8(n
, 1024, IFLA_VXLAN_TTL
, ttl
);
274 addattr8(n
, 1024, IFLA_VXLAN_TOS
, tos
);
275 addattr8(n
, 1024, IFLA_VXLAN_LEARNING
, learning
);
276 addattr8(n
, 1024, IFLA_VXLAN_PROXY
, proxy
);
277 addattr8(n
, 1024, IFLA_VXLAN_RSC
, rsc
);
278 addattr8(n
, 1024, IFLA_VXLAN_L2MISS
, l2miss
);
279 addattr8(n
, 1024, IFLA_VXLAN_L3MISS
, l3miss
);
280 addattr8(n
, 1024, IFLA_VXLAN_UDP_CSUM
, udpcsum
);
281 addattr8(n
, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX
, udp6zerocsumtx
);
282 addattr8(n
, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX
, udp6zerocsumrx
);
283 addattr8(n
, 1024, IFLA_VXLAN_REMCSUM_TX
, remcsumtx
);
284 addattr8(n
, 1024, IFLA_VXLAN_REMCSUM_RX
, remcsumrx
);
285 addattr8(n
, 1024, IFLA_VXLAN_COLLECT_METADATA
, metadata
);
288 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, 0);
290 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, age
);
292 addattr32(n
, 1024, IFLA_VXLAN_LIMIT
, maxaddr
);
293 if (range
.low
|| range
.high
)
294 addattr_l(n
, 1024, IFLA_VXLAN_PORT_RANGE
,
295 &range
, sizeof(range
));
297 addattr16(n
, 1024, IFLA_VXLAN_PORT
, htons(dstport
));
300 addattr_l(n
, 1024, IFLA_VXLAN_GBP
, NULL
, 0);
306 static void vxlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
318 if (!tb
[IFLA_VXLAN_ID
] ||
319 RTA_PAYLOAD(tb
[IFLA_VXLAN_ID
]) < sizeof(__u32
))
322 vni
= rta_getattr_u32(tb
[IFLA_VXLAN_ID
]);
323 fprintf(f
, "id %u ", vni
);
325 if (tb
[IFLA_VXLAN_GROUP
]) {
326 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_GROUP
]);
328 if (IN_MULTICAST(ntohl(addr
)))
329 fprintf(f
, "group %s ",
330 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
332 fprintf(f
, "remote %s ",
333 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
335 } else if (tb
[IFLA_VXLAN_GROUP6
]) {
336 struct in6_addr addr
;
337 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_GROUP6
]), sizeof(struct in6_addr
));
338 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0) {
339 if (IN6_IS_ADDR_MULTICAST(&addr
))
340 fprintf(f
, "group %s ",
341 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
343 fprintf(f
, "remote %s ",
344 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
348 if (tb
[IFLA_VXLAN_LOCAL
]) {
349 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_LOCAL
]);
351 fprintf(f
, "local %s ",
352 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
353 } else if (tb
[IFLA_VXLAN_LOCAL6
]) {
354 struct in6_addr addr
;
355 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_LOCAL6
]), sizeof(struct in6_addr
));
356 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0)
357 fprintf(f
, "local %s ",
358 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
361 if (tb
[IFLA_VXLAN_LINK
] &&
362 (link
= rta_getattr_u32(tb
[IFLA_VXLAN_LINK
]))) {
363 const char *n
= if_indextoname(link
, s2
);
366 fprintf(f
, "dev %s ", n
);
368 fprintf(f
, "dev %u ", link
);
371 if (tb
[IFLA_VXLAN_PORT_RANGE
]) {
372 const struct ifla_vxlan_port_range
*r
373 = RTA_DATA(tb
[IFLA_VXLAN_PORT_RANGE
]);
374 fprintf(f
, "srcport %u %u ", ntohs(r
->low
), ntohs(r
->high
));
377 if (tb
[IFLA_VXLAN_PORT
])
378 fprintf(f
, "dstport %u ",
379 ntohs(rta_getattr_u16(tb
[IFLA_VXLAN_PORT
])));
381 if (tb
[IFLA_VXLAN_LEARNING
] &&
382 !rta_getattr_u8(tb
[IFLA_VXLAN_LEARNING
]))
383 fputs("nolearning ", f
);
385 if (tb
[IFLA_VXLAN_PROXY
] && rta_getattr_u8(tb
[IFLA_VXLAN_PROXY
]))
388 if (tb
[IFLA_VXLAN_RSC
] && rta_getattr_u8(tb
[IFLA_VXLAN_RSC
]))
391 if (tb
[IFLA_VXLAN_L2MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L2MISS
]))
394 if (tb
[IFLA_VXLAN_L3MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L3MISS
]))
397 if (tb
[IFLA_VXLAN_TOS
] &&
398 (tos
= rta_getattr_u8(tb
[IFLA_VXLAN_TOS
]))) {
400 fprintf(f
, "tos inherit ");
402 fprintf(f
, "tos %#x ", tos
);
405 if (tb
[IFLA_VXLAN_TTL
]) {
406 __u8 ttl
= rta_getattr_u8(tb
[IFLA_VXLAN_TTL
]);
408 fprintf(f
, "ttl %d ", ttl
);
411 if (tb
[IFLA_VXLAN_AGEING
]) {
412 __u32 age
= rta_getattr_u32(tb
[IFLA_VXLAN_AGEING
]);
414 fprintf(f
, "ageing none ");
416 fprintf(f
, "ageing %u ", age
);
419 if (tb
[IFLA_VXLAN_LIMIT
] &&
420 ((maxaddr
= rta_getattr_u32(tb
[IFLA_VXLAN_LIMIT
])) != 0))
421 fprintf(f
, "maxaddr %u ", maxaddr
);
423 if (tb
[IFLA_VXLAN_UDP_CSUM
] && rta_getattr_u8(tb
[IFLA_VXLAN_UDP_CSUM
]))
424 fputs("udpcsum ", f
);
426 if (tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
] &&
427 rta_getattr_u8(tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_TX
]))
428 fputs("udp6zerocsumtx ", f
);
430 if (tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
] &&
431 rta_getattr_u8(tb
[IFLA_VXLAN_UDP_ZERO_CSUM6_RX
]))
432 fputs("udp6zerocsumrx ", f
);
434 if (tb
[IFLA_VXLAN_REMCSUM_TX
] &&
435 rta_getattr_u8(tb
[IFLA_VXLAN_REMCSUM_TX
]))
436 fputs("remcsumtx ", f
);
438 if (tb
[IFLA_VXLAN_REMCSUM_RX
] &&
439 rta_getattr_u8(tb
[IFLA_VXLAN_REMCSUM_RX
]))
440 fputs("remcsumrx ", f
);
442 if (tb
[IFLA_VXLAN_COLLECT_METADATA
] &&
443 rta_getattr_u8(tb
[IFLA_VXLAN_COLLECT_METADATA
]))
444 fputs("external ", f
);
446 if (tb
[IFLA_VXLAN_GBP
])
450 static void vxlan_print_help(struct link_util
*lu
, int argc
, char **argv
,
456 struct link_util vxlan_link_util
= {
458 .maxattr
= IFLA_VXLAN_MAX
,
459 .parse_opt
= vxlan_parse_opt
,
460 .print_opt
= vxlan_print_opt
,
461 .print_help
= vxlan_print_help
,