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