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
, " [ ageing SECONDS ] [ maxaddress NUMBER ]\n");
32 fprintf(stderr
, "\n");
33 fprintf(stderr
, "Where: VNI := 0-16777215\n");
34 fprintf(stderr
, " ADDR := { IP_ADDRESS | any }\n");
35 fprintf(stderr
, " TOS := { NUMBER | inherit }\n");
36 fprintf(stderr
, " TTL := { 1..255 | inherit }\n");
39 static int vxlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
47 struct in6_addr saddr6
= IN6ADDR_ANY_INIT
;
48 struct in6_addr gaddr6
= IN6ADDR_ANY_INIT
;
49 struct in6_addr daddr6
= IN6ADDR_ANY_INIT
;
63 struct ifla_vxlan_port_range range
= { 0, 0 };
66 if (!matches(*argv
, "id") ||
67 !matches(*argv
, "vni")) {
69 if (get_u32(&vni
, *argv
, 0) ||
71 invarg("invalid id", *argv
);
73 } else if (!matches(*argv
, "group")) {
75 if (!inet_get_addr(*argv
, &gaddr
, &gaddr6
)) {
76 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
79 if (!IN6_IS_ADDR_MULTICAST(&gaddr6
) && !IN_MULTICAST(ntohl(gaddr
)))
80 invarg("invalid group address", *argv
);
81 } else if (!matches(*argv
, "remote")) {
83 if (!inet_get_addr(*argv
, &daddr
, &daddr6
)) {
84 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
87 if (IN6_IS_ADDR_MULTICAST(&daddr6
) || IN_MULTICAST(ntohl(daddr
)))
88 invarg("invalid remote address", *argv
);
89 } else if (!matches(*argv
, "local")) {
91 if (strcmp(*argv
, "any")) {
92 if (!inet_get_addr(*argv
, &saddr
, &saddr6
)) {
93 fprintf(stderr
, "Invalid address \"%s\"\n", *argv
);
98 if (IN_MULTICAST(ntohl(saddr
)) || IN6_IS_ADDR_MULTICAST(&saddr6
))
99 invarg("invalid local address", *argv
);
100 } else if (!matches(*argv
, "dev")) {
102 link
= if_nametoindex(*argv
);
104 fprintf(stderr
, "Cannot find device \"%s\"\n",
108 } else if (!matches(*argv
, "ttl") ||
109 !matches(*argv
, "hoplimit")) {
113 if (strcmp(*argv
, "inherit") != 0) {
114 if (get_unsigned(&uval
, *argv
, 0))
115 invarg("invalid TTL", *argv
);
117 invarg("TTL must be <= 255", *argv
);
120 } else if (!matches(*argv
, "tos") ||
121 !matches(*argv
, "dsfield")) {
125 if (strcmp(*argv
, "inherit") != 0) {
126 if (rtnl_dsfield_a2n(&uval
, *argv
))
127 invarg("bad TOS value", *argv
);
131 } else if (!matches(*argv
, "ageing")) {
133 if (strcmp(*argv
, "none") == 0)
135 else if (get_u32(&age
, *argv
, 0))
136 invarg("ageing timer", *argv
);
137 } else if (!matches(*argv
, "maxaddress")) {
139 if (strcmp(*argv
, "unlimited") == 0)
141 else if (get_u32(&maxaddr
, *argv
, 0))
142 invarg("max addresses", *argv
);
143 } else if (!matches(*argv
, "port") ||
144 !matches(*argv
, "srcport")) {
145 __u16 minport
, maxport
;
147 if (get_u16(&minport
, *argv
, 0))
148 invarg("min port", *argv
);
150 if (get_u16(&maxport
, *argv
, 0))
151 invarg("max port", *argv
);
152 range
.low
= htons(minport
);
153 range
.high
= htons(maxport
);
154 } else if (!matches(*argv
, "dstport")){
156 if (get_u16(&dstport
, *argv
, 0))
157 invarg("dst port", *argv
);
159 } else if (!matches(*argv
, "nolearning")) {
161 } else if (!matches(*argv
, "learning")) {
163 } else if (!matches(*argv
, "noproxy")) {
165 } else if (!matches(*argv
, "proxy")) {
167 } else if (!matches(*argv
, "norsc")) {
169 } else if (!matches(*argv
, "rsc")) {
171 } else if (!matches(*argv
, "nol2miss")) {
173 } else if (!matches(*argv
, "l2miss")) {
175 } else if (!matches(*argv
, "nol3miss")) {
177 } else if (!matches(*argv
, "l3miss")) {
179 } else if (matches(*argv
, "help") == 0) {
183 fprintf(stderr
, "vxlan: unknown command \"%s\"?\n", *argv
);
191 fprintf(stderr
, "vxlan: missing virtual network identifier\n");
195 if ((gaddr
&& daddr
) ||
196 (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) &&
197 memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)))) {
198 fprintf(stderr
, "vxlan: both group and remote cannot be specified\n");
203 fprintf(stderr
, "vxlan: destination port not specified\n"
204 "Will use Linux kernel default (non-standard value)\n");
206 "Use 'dstport 4789' to get the IANA assigned value\n"
207 "Use 'dstport 0' to get default and quiet this message\n");
210 addattr32(n
, 1024, IFLA_VXLAN_ID
, vni
);
212 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &gaddr
, 4);
214 addattr_l(n
, 1024, IFLA_VXLAN_GROUP
, &daddr
, 4);
215 if (memcmp(&gaddr6
, &in6addr_any
, sizeof(gaddr6
)) != 0)
216 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &gaddr6
, sizeof(struct in6_addr
));
217 else if (memcmp(&daddr6
, &in6addr_any
, sizeof(daddr6
)) != 0)
218 addattr_l(n
, 1024, IFLA_VXLAN_GROUP6
, &daddr6
, sizeof(struct in6_addr
));
221 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL
, &saddr
, 4);
222 else if (memcmp(&saddr6
, &in6addr_any
, sizeof(saddr6
)) != 0)
223 addattr_l(n
, 1024, IFLA_VXLAN_LOCAL6
, &saddr6
, sizeof(struct in6_addr
));
226 addattr32(n
, 1024, IFLA_VXLAN_LINK
, link
);
227 addattr8(n
, 1024, IFLA_VXLAN_TTL
, ttl
);
228 addattr8(n
, 1024, IFLA_VXLAN_TOS
, tos
);
229 addattr8(n
, 1024, IFLA_VXLAN_LEARNING
, learning
);
230 addattr8(n
, 1024, IFLA_VXLAN_PROXY
, proxy
);
231 addattr8(n
, 1024, IFLA_VXLAN_RSC
, rsc
);
232 addattr8(n
, 1024, IFLA_VXLAN_L2MISS
, l2miss
);
233 addattr8(n
, 1024, IFLA_VXLAN_L3MISS
, l3miss
);
236 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, 0);
238 addattr32(n
, 1024, IFLA_VXLAN_AGEING
, age
);
240 addattr32(n
, 1024, IFLA_VXLAN_LIMIT
, maxaddr
);
241 if (range
.low
|| range
.high
)
242 addattr_l(n
, 1024, IFLA_VXLAN_PORT_RANGE
,
243 &range
, sizeof(range
));
245 addattr16(n
, 1024, IFLA_VXLAN_PORT
, htons(dstport
));
250 static void vxlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
262 if (!tb
[IFLA_VXLAN_ID
] ||
263 RTA_PAYLOAD(tb
[IFLA_VXLAN_ID
]) < sizeof(__u32
))
266 vni
= rta_getattr_u32(tb
[IFLA_VXLAN_ID
]);
267 fprintf(f
, "id %u ", vni
);
269 if (tb
[IFLA_VXLAN_GROUP
]) {
270 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_GROUP
]);
272 if (IN_MULTICAST(ntohl(addr
)))
273 fprintf(f
, "group %s ",
274 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
276 fprintf(f
, "remote %s ",
277 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
279 } else if (tb
[IFLA_VXLAN_GROUP6
]) {
280 struct in6_addr addr
;
281 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_GROUP6
]), sizeof(struct in6_addr
));
282 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0) {
283 if (IN6_IS_ADDR_MULTICAST(&addr
))
284 fprintf(f
, "group %s ",
285 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
287 fprintf(f
, "remote %s ",
288 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
292 if (tb
[IFLA_VXLAN_LOCAL
]) {
293 __be32 addr
= rta_getattr_u32(tb
[IFLA_VXLAN_LOCAL
]);
295 fprintf(f
, "local %s ",
296 format_host(AF_INET
, 4, &addr
, s1
, sizeof(s1
)));
297 } else if (tb
[IFLA_VXLAN_LOCAL6
]) {
298 struct in6_addr addr
;
299 memcpy(&addr
, RTA_DATA(tb
[IFLA_VXLAN_LOCAL6
]), sizeof(struct in6_addr
));
300 if (memcmp(&addr
, &in6addr_any
, sizeof(addr
)) != 0)
301 fprintf(f
, "local %s ",
302 format_host(AF_INET6
, sizeof(struct in6_addr
), &addr
, s1
, sizeof(s1
)));
305 if (tb
[IFLA_VXLAN_LINK
] &&
306 (link
= rta_getattr_u32(tb
[IFLA_VXLAN_LINK
]))) {
307 const char *n
= if_indextoname(link
, s2
);
310 fprintf(f
, "dev %s ", n
);
312 fprintf(f
, "dev %u ", link
);
315 if (tb
[IFLA_VXLAN_PORT_RANGE
]) {
316 const struct ifla_vxlan_port_range
*r
317 = RTA_DATA(tb
[IFLA_VXLAN_PORT_RANGE
]);
318 fprintf(f
, "srcport %u %u ", ntohs(r
->low
), ntohs(r
->high
));
321 if (tb
[IFLA_VXLAN_PORT
])
322 fprintf(f
, "dstport %u ",
323 ntohs(rta_getattr_u16(tb
[IFLA_VXLAN_PORT
])));
325 if (tb
[IFLA_VXLAN_LEARNING
] &&
326 !rta_getattr_u8(tb
[IFLA_VXLAN_LEARNING
]))
327 fputs("nolearning ", f
);
329 if (tb
[IFLA_VXLAN_PROXY
] && rta_getattr_u8(tb
[IFLA_VXLAN_PROXY
]))
332 if (tb
[IFLA_VXLAN_RSC
] && rta_getattr_u8(tb
[IFLA_VXLAN_RSC
]))
335 if (tb
[IFLA_VXLAN_L2MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L2MISS
]))
338 if (tb
[IFLA_VXLAN_L3MISS
] && rta_getattr_u8(tb
[IFLA_VXLAN_L3MISS
]))
341 if (tb
[IFLA_VXLAN_TOS
] &&
342 (tos
= rta_getattr_u8(tb
[IFLA_VXLAN_TOS
]))) {
344 fprintf(f
, "tos inherit ");
346 fprintf(f
, "tos %#x ", tos
);
349 if (tb
[IFLA_VXLAN_TTL
]) {
350 __u8 ttl
= rta_getattr_u8(tb
[IFLA_VXLAN_TTL
]);
352 fprintf(f
, "ttl %d ", ttl
);
355 if (tb
[IFLA_VXLAN_AGEING
]) {
356 __u32 age
= rta_getattr_u32(tb
[IFLA_VXLAN_AGEING
]);
358 fprintf(f
, "ageing none ");
360 fprintf(f
, "ageing %u ", age
);
363 if (tb
[IFLA_VXLAN_LIMIT
] &&
364 ((maxaddr
= rta_getattr_u32(tb
[IFLA_VXLAN_LIMIT
])) != 0))
365 fprintf(f
, "maxaddr %u ", maxaddr
);
368 struct link_util vxlan_link_util
= {
370 .maxattr
= IFLA_VXLAN_MAX
,
371 .parse_opt
= vxlan_parse_opt
,
372 .print_opt
= vxlan_print_opt
,