]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / iplink_vxlan.c
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 static void print_explain(FILE *f)
25 {
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, " [ gbp ]\n");
34 fprintf(f, "\n");
35 fprintf(f, "Where: VNI := 0-16777215\n");
36 fprintf(f, " ADDR := { IP_ADDRESS | any }\n");
37 fprintf(f, " TOS := { NUMBER | inherit }\n");
38 fprintf(f, " TTL := { 1..255 | inherit }\n");
39 }
40
41 static void explain(void)
42 {
43 print_explain(stderr);
44 }
45
46 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
47 struct nlmsghdr *n)
48 {
49 __u32 vni = 0;
50 int vni_set = 0;
51 __u32 saddr = 0;
52 __u32 gaddr = 0;
53 __u32 daddr = 0;
54 struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
55 struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
56 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
57 unsigned link = 0;
58 __u8 tos = 0;
59 __u8 ttl = 0;
60 __u8 learning = 1;
61 __u8 proxy = 0;
62 __u8 rsc = 0;
63 __u8 l2miss = 0;
64 __u8 l3miss = 0;
65 __u8 noage = 0;
66 __u32 age = 0;
67 __u32 maxaddr = 0;
68 __u16 dstport = 0;
69 __u8 udpcsum = 0;
70 __u8 udp6zerocsumtx = 0;
71 __u8 udp6zerocsumrx = 0;
72 __u8 gbp = 0;
73 int dst_port_set = 0;
74 struct ifla_vxlan_port_range range = { 0, 0 };
75
76 while (argc > 0) {
77 if (!matches(*argv, "id") ||
78 !matches(*argv, "vni")) {
79 NEXT_ARG();
80 if (get_u32(&vni, *argv, 0) ||
81 vni >= 1u << 24)
82 invarg("invalid id", *argv);
83 vni_set = 1;
84 } else if (!matches(*argv, "group")) {
85 NEXT_ARG();
86 if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
87 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
88 return -1;
89 }
90 if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
91 invarg("invalid group address", *argv);
92 } else if (!matches(*argv, "remote")) {
93 NEXT_ARG();
94 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
95 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
96 return -1;
97 }
98 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
99 invarg("invalid remote address", *argv);
100 } else if (!matches(*argv, "local")) {
101 NEXT_ARG();
102 if (strcmp(*argv, "any")) {
103 if (!inet_get_addr(*argv, &saddr, &saddr6)) {
104 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
105 return -1;
106 }
107 }
108
109 if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
110 invarg("invalid local address", *argv);
111 } else if (!matches(*argv, "dev")) {
112 NEXT_ARG();
113 link = if_nametoindex(*argv);
114 if (link == 0) {
115 fprintf(stderr, "Cannot find device \"%s\"\n",
116 *argv);
117 exit(-1);
118 }
119 } else if (!matches(*argv, "ttl") ||
120 !matches(*argv, "hoplimit")) {
121 unsigned uval;
122
123 NEXT_ARG();
124 if (strcmp(*argv, "inherit") != 0) {
125 if (get_unsigned(&uval, *argv, 0))
126 invarg("invalid TTL", *argv);
127 if (uval > 255)
128 invarg("TTL must be <= 255", *argv);
129 ttl = uval;
130 }
131 } else if (!matches(*argv, "tos") ||
132 !matches(*argv, "dsfield")) {
133 __u32 uval;
134
135 NEXT_ARG();
136 if (strcmp(*argv, "inherit") != 0) {
137 if (rtnl_dsfield_a2n(&uval, *argv))
138 invarg("bad TOS value", *argv);
139 tos = uval;
140 } else
141 tos = 1;
142 } else if (!matches(*argv, "ageing")) {
143 NEXT_ARG();
144 if (strcmp(*argv, "none") == 0)
145 noage = 1;
146 else if (get_u32(&age, *argv, 0))
147 invarg("ageing timer", *argv);
148 } else if (!matches(*argv, "maxaddress")) {
149 NEXT_ARG();
150 if (strcmp(*argv, "unlimited") == 0)
151 maxaddr = 0;
152 else if (get_u32(&maxaddr, *argv, 0))
153 invarg("max addresses", *argv);
154 } else if (!matches(*argv, "port") ||
155 !matches(*argv, "srcport")) {
156 __u16 minport, maxport;
157 NEXT_ARG();
158 if (get_u16(&minport, *argv, 0))
159 invarg("min port", *argv);
160 NEXT_ARG();
161 if (get_u16(&maxport, *argv, 0))
162 invarg("max port", *argv);
163 range.low = htons(minport);
164 range.high = htons(maxport);
165 } else if (!matches(*argv, "dstport")){
166 NEXT_ARG();
167 if (get_u16(&dstport, *argv, 0))
168 invarg("dst port", *argv);
169 dst_port_set = 1;
170 } else if (!matches(*argv, "nolearning")) {
171 learning = 0;
172 } else if (!matches(*argv, "learning")) {
173 learning = 1;
174 } else if (!matches(*argv, "noproxy")) {
175 proxy = 0;
176 } else if (!matches(*argv, "proxy")) {
177 proxy = 1;
178 } else if (!matches(*argv, "norsc")) {
179 rsc = 0;
180 } else if (!matches(*argv, "rsc")) {
181 rsc = 1;
182 } else if (!matches(*argv, "nol2miss")) {
183 l2miss = 0;
184 } else if (!matches(*argv, "l2miss")) {
185 l2miss = 1;
186 } else if (!matches(*argv, "nol3miss")) {
187 l3miss = 0;
188 } else if (!matches(*argv, "l3miss")) {
189 l3miss = 1;
190 } else if (!matches(*argv, "udpcsum")) {
191 udpcsum = 1;
192 } else if (!matches(*argv, "noudpcsum")) {
193 udpcsum = 0;
194 } else if (!matches(*argv, "udp6zerocsumtx")) {
195 udp6zerocsumtx = 1;
196 } else if (!matches(*argv, "noudp6zerocsumtx")) {
197 udp6zerocsumtx = 0;
198 } else if (!matches(*argv, "udp6zerocsumrx")) {
199 udp6zerocsumrx = 1;
200 } else if (!matches(*argv, "noudp6zerocsumrx")) {
201 udp6zerocsumrx = 0;
202 } else if (!matches(*argv, "gbp")) {
203 gbp = 1;
204 } else if (matches(*argv, "help") == 0) {
205 explain();
206 return -1;
207 } else {
208 fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
209 explain();
210 return -1;
211 }
212 argc--, argv++;
213 }
214
215 if (!vni_set) {
216 fprintf(stderr, "vxlan: missing virtual network identifier\n");
217 return -1;
218 }
219
220 if ((gaddr && daddr) ||
221 (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) &&
222 memcmp(&daddr6, &in6addr_any, sizeof(daddr6)))) {
223 fprintf(stderr, "vxlan: both group and remote cannot be specified\n");
224 return -1;
225 }
226
227 if (!dst_port_set) {
228 fprintf(stderr, "vxlan: destination port not specified\n"
229 "Will use Linux kernel default (non-standard value)\n");
230 fprintf(stderr,
231 "Use 'dstport 4789' to get the IANA assigned value\n"
232 "Use 'dstport 0' to get default and quiet this message\n");
233 }
234
235 addattr32(n, 1024, IFLA_VXLAN_ID, vni);
236 if (gaddr)
237 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
238 else if (daddr)
239 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &daddr, 4);
240 if (memcmp(&gaddr6, &in6addr_any, sizeof(gaddr6)) != 0)
241 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &gaddr6, sizeof(struct in6_addr));
242 else if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
243 addattr_l(n, 1024, IFLA_VXLAN_GROUP6, &daddr6, sizeof(struct in6_addr));
244
245 if (saddr)
246 addattr_l(n, 1024, IFLA_VXLAN_LOCAL, &saddr, 4);
247 else if (memcmp(&saddr6, &in6addr_any, sizeof(saddr6)) != 0)
248 addattr_l(n, 1024, IFLA_VXLAN_LOCAL6, &saddr6, sizeof(struct in6_addr));
249
250 if (link)
251 addattr32(n, 1024, IFLA_VXLAN_LINK, link);
252 addattr8(n, 1024, IFLA_VXLAN_TTL, ttl);
253 addattr8(n, 1024, IFLA_VXLAN_TOS, tos);
254 addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning);
255 addattr8(n, 1024, IFLA_VXLAN_PROXY, proxy);
256 addattr8(n, 1024, IFLA_VXLAN_RSC, rsc);
257 addattr8(n, 1024, IFLA_VXLAN_L2MISS, l2miss);
258 addattr8(n, 1024, IFLA_VXLAN_L3MISS, l3miss);
259 addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, udpcsum);
260 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, udp6zerocsumtx);
261 addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, udp6zerocsumrx);
262
263 if (noage)
264 addattr32(n, 1024, IFLA_VXLAN_AGEING, 0);
265 else if (age)
266 addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
267 if (maxaddr)
268 addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr);
269 if (range.low || range.high)
270 addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE,
271 &range, sizeof(range));
272 if (dstport)
273 addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport));
274
275 if (gbp)
276 addattr_l(n, 1024, IFLA_VXLAN_GBP, NULL, 0);
277
278
279 return 0;
280 }
281
282 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
283 {
284 __u32 vni;
285 unsigned link;
286 __u8 tos;
287 __u32 maxaddr;
288 char s1[1024];
289 char s2[64];
290
291 if (!tb)
292 return;
293
294 if (!tb[IFLA_VXLAN_ID] ||
295 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
296 return;
297
298 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
299 fprintf(f, "id %u ", vni);
300
301 if (tb[IFLA_VXLAN_GROUP]) {
302 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
303 if (addr) {
304 if (IN_MULTICAST(ntohl(addr)))
305 fprintf(f, "group %s ",
306 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
307 else
308 fprintf(f, "remote %s ",
309 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
310 }
311 } else if (tb[IFLA_VXLAN_GROUP6]) {
312 struct in6_addr addr;
313 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr));
314 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
315 if (IN6_IS_ADDR_MULTICAST(&addr))
316 fprintf(f, "group %s ",
317 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
318 else
319 fprintf(f, "remote %s ",
320 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
321 }
322 }
323
324 if (tb[IFLA_VXLAN_LOCAL]) {
325 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
326 if (addr)
327 fprintf(f, "local %s ",
328 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
329 } else if (tb[IFLA_VXLAN_LOCAL6]) {
330 struct in6_addr addr;
331 memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr));
332 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0)
333 fprintf(f, "local %s ",
334 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
335 }
336
337 if (tb[IFLA_VXLAN_LINK] &&
338 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
339 const char *n = if_indextoname(link, s2);
340
341 if (n)
342 fprintf(f, "dev %s ", n);
343 else
344 fprintf(f, "dev %u ", link);
345 }
346
347 if (tb[IFLA_VXLAN_PORT_RANGE]) {
348 const struct ifla_vxlan_port_range *r
349 = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]);
350 fprintf(f, "srcport %u %u ", ntohs(r->low), ntohs(r->high));
351 }
352
353 if (tb[IFLA_VXLAN_PORT])
354 fprintf(f, "dstport %u ",
355 ntohs(rta_getattr_u16(tb[IFLA_VXLAN_PORT])));
356
357 if (tb[IFLA_VXLAN_LEARNING] &&
358 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
359 fputs("nolearning ", f);
360
361 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
362 fputs("proxy ", f);
363
364 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
365 fputs("rsc ", f);
366
367 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
368 fputs("l2miss ", f);
369
370 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
371 fputs("l3miss ", f);
372
373 if (tb[IFLA_VXLAN_TOS] &&
374 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
375 if (tos == 1)
376 fprintf(f, "tos inherit ");
377 else
378 fprintf(f, "tos %#x ", tos);
379 }
380
381 if (tb[IFLA_VXLAN_TTL]) {
382 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
383 if (ttl)
384 fprintf(f, "ttl %d ", ttl);
385 }
386
387 if (tb[IFLA_VXLAN_AGEING]) {
388 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
389 if (age == 0)
390 fprintf(f, "ageing none ");
391 else
392 fprintf(f, "ageing %u ", age);
393 }
394
395 if (tb[IFLA_VXLAN_LIMIT] &&
396 ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
397 fprintf(f, "maxaddr %u ", maxaddr);
398
399 if (tb[IFLA_VXLAN_UDP_CSUM] && rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]))
400 fputs("udpcsum ", f);
401
402 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
403 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
404 fputs("udp6zerocsumtx ", f);
405
406 if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
407 rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
408 fputs("udp6zerocsumrx ", f);
409
410 if (tb[IFLA_VXLAN_GBP])
411 fputs("gbp ", f);
412 }
413
414 static void vxlan_print_help(struct link_util *lu, int argc, char **argv,
415 FILE *f)
416 {
417 print_explain(f);
418 }
419
420 struct link_util vxlan_link_util = {
421 .id = "vxlan",
422 .maxattr = IFLA_VXLAN_MAX,
423 .parse_opt = vxlan_parse_opt,
424 .print_opt = vxlan_print_opt,
425 .print_help = vxlan_print_help,
426 };