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 explain(void)
26 fprintf(stderr
, "Usage: ... vxlan id VNI [ { group | remote } ADDR ] [ local ADDR ]\n");
27 fprintf(stderr
, " [ ttl TTL ] [ tos TOS ] [ dev PHYS_DEV ]\n");
28 fprintf(stderr
, " [ dstport PORT ] [ srcport MIN MAX ]\n");
29 fprintf(stderr
, " [ [no]learning ] [ [no]proxy ] [ [no]rsc ]\n");
30 fprintf(stderr
, " [ [no]l2miss ] [ [no]l3miss ]\n");
31 fprintf(stderr
, "\n");
32 fprintf(stderr
, "Where: VNI := 0-16777215\n");
33 fprintf(stderr
, " ADDR := { IP_ADDRESS | any }\n");
34 fprintf(stderr
, " TOS := { NUMBER | inherit }\n");
35 fprintf(stderr
, " TTL := { 1..255 | inherit }\n");
38 static int vxlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
46 struct in6_addr saddr6
= IN6ADDR_ANY_INIT
;
47 struct in6_addr gaddr6
= IN6ADDR_ANY_INIT
;
48 struct in6_addr daddr6
= IN6ADDR_ANY_INIT
;
62 struct ifla_vxlan_port_range range
= { 0, 0 };
65 if (!matches(*argv
, "id") ||
66 !matches(*argv
, "vni")) {
68 if (get_u32(&vni
, *argv
, 0) ||
70 invarg("invalid id", *argv
);
72 } else if (!matches(*argv
, "group")) {
74 if (!inet_get_addr(*argv
, &gaddr
, &gaddr6
)) {
75 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
78 if (!IN6_IS_ADDR_MULTICAST(&gaddr6
) && !IN_MULTICAST(ntohl(gaddr
)))
79 invarg("invalid group address", *argv
);
80 } else if (!matches(*argv
, "remote")) {
82 if (!inet_get_addr(*argv
, &daddr
, &daddr6
)) {
83 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
86 if (IN6_IS_ADDR_MULTICAST(&daddr6
) || IN_MULTICAST(ntohl(daddr
)))
87 invarg("invalid remote address", *argv
);
88 } else if (!matches(*argv
, "local")) {
90 if (strcmp(*argv
, "any")) {
91 if (!inet_get_addr(*argv
, &saddr
, &saddr6
)) {
92 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
97 if (IN_MULTICAST(ntohl(saddr
)) || IN6_IS_ADDR_MULTICAST(&saddr6
))
98 invarg("invalid local address", *argv
);
99 } else if (!matches(*argv
, "dev")) {
101 link
= if_nametoindex(*argv
);
104 } else if (!matches(*argv
, "ttl") ||
105 !matches(*argv
, "hoplimit")) {
109 if (strcmp(*argv
, "inherit") != 0) {
110 if (get_unsigned(&uval
, *argv
, 0))
111 invarg("invalid TTL", *argv
);
113 invarg("TTL must be <= 255", *argv
);
116 } else if (!matches(*argv
, "tos") ||
117 !matches(*argv
, "dsfield")) {
121 if (strcmp(*argv
, "inherit") != 0) {
122 if (rtnl_dsfield_a2n(&uval
, *argv
))
123 invarg("bad TOS value", *argv
);
127 } else if (!matches(*argv
, "ageing")) {
129 if (strcmp(*argv
, "none") == 0)
131 else if (get_u32(&age
, *argv
, 0))
132 invarg("ageing timer", *argv
);
133 } else if (!matches(*argv
, "maxaddress")) {
135 if (strcmp(*argv
, "unlimited") == 0)
137 else if (get_u32(&maxaddr
, *argv
, 0))
138 invarg("max addresses", *argv
);
139 } else if (!matches(*argv
, "port") ||
140 !matches(*argv
, "srcport")) {
141 __u16 minport
, maxport
;
143 if (get_u16(&minport
, *argv
, 0))
144 invarg("min port", *argv
);
146 if (get_u16(&maxport
, *argv
, 0))
147 invarg("max port", *argv
);
148 range
.low
= htons(minport
);
149 range
.high
= htons(maxport
);
150 } else if (!matches(*argv
, "dstport")){
152 if (get_u16(&dstport
, *argv
, 0))
153 invarg("dst port", *argv
);
155 } else if (!matches(*argv
, "nolearning")) {
157 } else if (!matches(*argv
, "learning")) {
159 } else if (!matches(*argv
, "noproxy")) {
161 } else if (!matches(*argv
, "proxy")) {
163 } else if (!matches(*argv
, "norsc")) {
165 } else if (!matches(*argv
, "rsc")) {
167 } else if (!matches(*argv
, "nol2miss")) {
169 } else if (!matches(*argv
, "l2miss")) {
171 } else if (!matches(*argv
, "nol3miss")) {
173 } else if (!matches(*argv
, "l3miss")) {
175 } else if (matches(*argv
, "help") == 0) {
179 fprintf(stderr
, "vxlan: unknown command \"%s\"?\n", *argv
);
187 fprintf(stderr
, "vxlan: missing virtual network identifier\n");
191 if ((gaddr
&& daddr
) ||
192 (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) &&
193 memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)))) {
194 fprintf(stderr
, "vxlan: both group and remote cannot be specified\n");
199 fprintf(stderr
, "vxlan: destination port not specified\n"
200 "Will use Linux kernel default (non-standard value)\n");
202 "Use 'dstport 4789' to get the IANA assigned value\n"
203 "Use 'dstport 0' to get default and quiet this message\n");
206 addattr32(n
, 1024, IFLA_VXLAN_ID
, vni
);
208 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &gaddr
, 4);
210 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &daddr
, 4);
211 if (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) != 0)
212 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &gaddr6
, sizeof(struct in6_addr
));
213 else if (memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)) != 0)
214 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &daddr6
, sizeof(struct in6_addr
));
217 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL
, &saddr
, 4);
218 else if (memcmp(&saddr6
, &in6addr_any
, sizeof(saddr6
)) != 0)
219 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL6
, &saddr6
, sizeof(struct in6_addr
));
222 addattr32(n
, 1024, IFLA_VXLAN_LINK
, link
);
223 addattr8(n
, 1024, IFLA_VXLAN_TTL
, ttl
);
224 addattr8(n
, 1024, IFLA_VXLAN_TOS
, tos
);
225 addattr8(n
, 1024, IFLA_VXLAN_LEARNING
, learning
);
226 addattr8(n
, 1024, IFLA_VXLAN_PROXY
, proxy
);
227 addattr8(n
, 1024, IFLA_VXLAN_RSC
, rsc
);
228 addattr8(n
, 1024, IFLA_VXLAN_L2MISS
, l2miss
);
229 addattr8(n
, 1024, IFLA_VXLAN_L3MISS
, l3miss
);
232 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, 0);
234 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, age
);
236 addattr32(n
, 1024, IFLA_VXLAN_LIMIT
, maxaddr
);
237 if (range
.low
|| range
.high
)
238 addattr_l(n
, 1024, IFLA_VXLAN_PORT_RANGE
,
239 &range
, sizeof(range
));
241 addattr16(n
, 1024, IFLA_VXLAN_PORT
, htons(dstport
));
246 static void vxlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
258 if (!tb
[IFLA_VXLAN_ID
] ||
259 RTA_PAYLOAD(tb
[IFLA_VXLAN_ID
]) < sizeof(__u32
))
262 vni
= rta_getattr_u32(tb
[IFLA_VXLAN_ID
]);
263 fprintf(f
, "id %u ", vni
);
265 if (tb
[IFLA_VXLAN_GROUP
]) {
266 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_GROUP
]);
268 if (IN_MULTICAST(ntohl(addr
)))
269 fprintf(f
, "group %s ",
270 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
272 fprintf(f
, "remote %s ",
273 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
275 } else if (tb
[IFLA_VXLAN_GROUP6
]) {
276 struct in6_addr addr
;
277 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_GROUP6
]), sizeof(struct in6_addr
));
278 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0) {
279 if (IN6_IS_ADDR_MULTICAST(&addr
))
280 fprintf(f
, "group %s ",
281 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
283 fprintf(f
, "remote %s ",
284 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
288 if (tb
[IFLA_VXLAN_LOCAL
]) {
289 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_LOCAL
]);
291 fprintf(f
, "local %s ",
292 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
293 } else if (tb
[IFLA_VXLAN_LOCAL6
]) {
294 struct in6_addr addr
;
295 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_LOCAL6
]), sizeof(struct in6_addr
));
296 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0)
297 fprintf(f
, "local %s ",
298 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
301 if (tb
[IFLA_VXLAN_LINK
] &&
302 (link
= rta_getattr_u32(tb
[IFLA_VXLAN_LINK
]))) {
303 const char *n
= if_indextoname(link
, s2
);
306 fprintf(f
, "dev %s ", n
);
308 fprintf(f
, "dev %u ", link
);
311 if (tb
[IFLA_VXLAN_PORT_RANGE
]) {
312 const struct ifla_vxlan_port_range
*r
313 = RTA_DATA(tb
[IFLA_VXLAN_PORT_RANGE
]);
314 fprintf(f
, "srcport %u %u ", ntohs(r
->low
), ntohs(r
->high
));
317 if (tb
[IFLA_VXLAN_PORT
])
318 fprintf(f
, "dstport %u ",
319 ntohs(rta_getattr_u16(tb
[IFLA_VXLAN_PORT
])));
321 if (tb
[IFLA_VXLAN_LEARNING
] &&
322 !rta_getattr_u8(tb
[IFLA_VXLAN_LEARNING
]))
323 fputs("nolearning ", f
);
325 if (tb
[IFLA_VXLAN_PROXY
] && rta_getattr_u8(tb
[IFLA_VXLAN_PROXY
]))
328 if (tb
[IFLA_VXLAN_RSC
] && rta_getattr_u8(tb
[IFLA_VXLAN_RSC
]))
331 if (tb
[IFLA_VXLAN_L2MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L2MISS
]))
334 if (tb
[IFLA_VXLAN_L3MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L3MISS
]))
337 if (tb
[IFLA_VXLAN_TOS
] &&
338 (tos
= rta_getattr_u8(tb
[IFLA_VXLAN_TOS
]))) {
340 fprintf(f
, "tos inherit ");
342 fprintf(f
, "tos %#x ", tos
);
345 if (tb
[IFLA_VXLAN_TTL
]) {
346 __u8 ttl
= rta_getattr_u8(tb
[IFLA_VXLAN_TTL
]);
348 fprintf(f
, "ttl %d ", ttl
);
351 if (tb
[IFLA_VXLAN_AGEING
]) {
352 __u32 age
= rta_getattr_u32(tb
[IFLA_VXLAN_AGEING
]);
354 fprintf(f
, "ageing none ");
356 fprintf(f
, "ageing %u ", age
);
359 if (tb
[IFLA_VXLAN_LIMIT
] &&
360 (maxaddr
= rta_getattr_u32(tb
[IFLA_VXLAN_LIMIT
]) != 0))
361 fprintf(f
, "maxaddr %u ", maxaddr
);
364 struct link_util vxlan_link_util
= {
366 .maxattr
= IFLA_VXLAN_MAX
,
367 .parse_opt
= vxlan_parse_opt
,
368 .print_opt
= vxlan_print_opt
,