]>
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 | ||
01c65995 GM |
24 | #define VXLAN_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0) |
25 | ||
561e650e | 26 | static void print_explain(FILE *f) |
27 | { | |
8b471354 PS |
28 | fprintf(f, |
29 | "Usage: ... vxlan id VNI\n" | |
30 | " [ { group | remote } IP_ADDRESS ]\n" | |
31 | " [ local ADDR ]\n" | |
32 | " [ ttl TTL ]\n" | |
33 | " [ tos TOS ]\n" | |
3d98eba4 | 34 | " [ df DF ]\n" |
8b471354 PS |
35 | " [ flowlabel LABEL ]\n" |
36 | " [ dev PHYS_DEV ]\n" | |
37 | " [ dstport PORT ]\n" | |
38 | " [ srcport MIN MAX ]\n" | |
39 | " [ [no]learning ]\n" | |
40 | " [ [no]proxy ]\n" | |
41 | " [ [no]rsc ]\n" | |
42 | " [ [no]l2miss ]\n" | |
43 | " [ [no]l3miss ]\n" | |
44 | " [ ageing SECONDS ]\n" | |
45 | " [ maxaddress NUMBER ]\n" | |
46 | " [ [no]udpcsum ]\n" | |
47 | " [ [no]udp6zerocsumtx ]\n" | |
48 | " [ [no]udp6zerocsumrx ]\n" | |
49 | " [ [no]remcsumtx ] [ [no]remcsumrx ]\n" | |
50 | " [ [no]external ] [ gbp ] [ gpe ]\n" | |
51 | "\n" | |
52 | "Where: VNI := 0-16777215\n" | |
53 | " ADDR := { IP_ADDRESS | any }\n" | |
54 | " TOS := { NUMBER | inherit }\n" | |
8f01001a | 55 | " TTL := { 1..255 | auto | inherit }\n" |
3d98eba4 | 56 | " DF := { unset | set | inherit }\n" |
8b471354 PS |
57 | " LABEL := 0-1048575\n" |
58 | ); | |
561e650e | 59 | } |
60 | ||
a5494df2 SH |
61 | static void explain(void) |
62 | { | |
561e650e | 63 | print_explain(stderr); |
a5494df2 SH |
64 | } |
65 | ||
01c65995 GM |
66 | static void check_duparg(__u64 *attrs, int type, const char *key, |
67 | const char *argv) | |
68 | { | |
69 | if (!VXLAN_ATTRSET(*attrs, type)) { | |
70 | *attrs |= (1L << type); | |
71 | return; | |
72 | } | |
73 | duparg2(key, argv); | |
74 | } | |
75 | ||
a5494df2 SH |
76 | static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv, |
77 | struct nlmsghdr *n) | |
78 | { | |
9cc173d4 | 79 | inet_prefix saddr, daddr; |
a5494df2 | 80 | __u32 vni = 0; |
a5494df2 | 81 | __u8 learning = 1; |
514cdfb4 | 82 | __u16 dstport = 0; |
e79c327e | 83 | __u8 metadata = 0; |
01c65995 GM |
84 | __u64 attrs = 0; |
85 | bool set_op = (n->nlmsg_type == RTM_NEWLINK && | |
86 | !(n->nlmsg_flags & NLM_F_CREATE)); | |
c1360e3b | 87 | bool selected_family = false; |
a5494df2 | 88 | |
1e9b8072 | 89 | saddr.family = daddr.family = AF_UNSPEC; |
9cc173d4 SP |
90 | |
91 | inet_prefix_reset(&saddr); | |
92 | inet_prefix_reset(&daddr); | |
1e9b8072 | 93 | |
a5494df2 SH |
94 | while (argc > 0) { |
95 | if (!matches(*argv, "id") || | |
96 | !matches(*argv, "vni")) { | |
01c65995 GM |
97 | /* We will add ID attribute outside of the loop since we |
98 | * need to consider metadata information as well. | |
99 | */ | |
a5494df2 | 100 | NEXT_ARG(); |
01c65995 | 101 | check_duparg(&attrs, IFLA_VXLAN_ID, "id", *argv); |
a5494df2 SH |
102 | if (get_u32(&vni, *argv, 0) || |
103 | vni >= 1u << 24) | |
104 | invarg("invalid id", *argv); | |
a5494df2 | 105 | } else if (!matches(*argv, "group")) { |
1e9b8072 | 106 | if (is_addrtype_inet_not_multi(&daddr)) { |
01c65995 GM |
107 | fprintf(stderr, "vxlan: both group and remote"); |
108 | fprintf(stderr, " cannot be specified\n"); | |
109 | return -1; | |
110 | } | |
a5494df2 | 111 | NEXT_ARG(); |
01c65995 | 112 | check_duparg(&attrs, IFLA_VXLAN_GROUP, "group", *argv); |
1e9b8072 SP |
113 | get_addr(&daddr, *argv, saddr.family); |
114 | if (!is_addrtype_inet_multi(&daddr)) | |
7cfa3802 AW |
115 | invarg("invalid group address", *argv); |
116 | } else if (!matches(*argv, "remote")) { | |
1e9b8072 | 117 | if (is_addrtype_inet_multi(&daddr)) { |
01c65995 GM |
118 | fprintf(stderr, "vxlan: both group and remote"); |
119 | fprintf(stderr, " cannot be specified\n"); | |
120 | return -1; | |
121 | } | |
7cfa3802 | 122 | NEXT_ARG(); |
01c65995 | 123 | check_duparg(&attrs, IFLA_VXLAN_GROUP, "remote", *argv); |
1e9b8072 SP |
124 | get_addr(&daddr, *argv, saddr.family); |
125 | if (!is_addrtype_inet_not_multi(&daddr)) | |
7cfa3802 | 126 | invarg("invalid remote address", *argv); |
a5494df2 SH |
127 | } else if (!matches(*argv, "local")) { |
128 | NEXT_ARG(); | |
01c65995 | 129 | check_duparg(&attrs, IFLA_VXLAN_LOCAL, "local", *argv); |
1e9b8072 SP |
130 | get_addr(&saddr, *argv, daddr.family); |
131 | if (!is_addrtype_inet_not_multi(&saddr)) | |
a5494df2 SH |
132 | invarg("invalid local address", *argv); |
133 | } else if (!matches(*argv, "dev")) { | |
01c65995 GM |
134 | unsigned int link; |
135 | ||
a5494df2 | 136 | NEXT_ARG(); |
01c65995 | 137 | check_duparg(&attrs, IFLA_VXLAN_LINK, "dev", *argv); |
7a14358b | 138 | link = ll_name_to_index(*argv); |
fe99adbc SP |
139 | if (!link) |
140 | exit(nodev(*argv)); | |
01c65995 | 141 | addattr32(n, 1024, IFLA_VXLAN_LINK, link); |
a5494df2 SH |
142 | } else if (!matches(*argv, "ttl") || |
143 | !matches(*argv, "hoplimit")) { | |
56f5daac | 144 | unsigned int uval; |
01c65995 | 145 | __u8 ttl = 0; |
a5494df2 SH |
146 | |
147 | NEXT_ARG(); | |
01c65995 | 148 | check_duparg(&attrs, IFLA_VXLAN_TTL, "ttl", *argv); |
5b1c363c | 149 | if (strcmp(*argv, "inherit") == 0) { |
952a7a19 | 150 | addattr(n, 1024, IFLA_VXLAN_TTL_INHERIT); |
5b1c363c HL |
151 | } else if (strcmp(*argv, "auto") == 0) { |
152 | addattr8(n, 1024, IFLA_VXLAN_TTL, ttl); | |
153 | } else { | |
a5494df2 | 154 | if (get_unsigned(&uval, *argv, 0)) |
2d596120 | 155 | invarg("invalid TTL", *argv); |
a5494df2 | 156 | if (uval > 255) |
2d596120 | 157 | invarg("TTL must be <= 255", *argv); |
a5494df2 | 158 | ttl = uval; |
5b1c363c | 159 | addattr8(n, 1024, IFLA_VXLAN_TTL, ttl); |
a5494df2 SH |
160 | } |
161 | } else if (!matches(*argv, "tos") || | |
162 | !matches(*argv, "dsfield")) { | |
163 | __u32 uval; | |
01c65995 | 164 | __u8 tos; |
a5494df2 SH |
165 | |
166 | NEXT_ARG(); | |
01c65995 | 167 | check_duparg(&attrs, IFLA_VXLAN_TOS, "tos", *argv); |
a5494df2 SH |
168 | if (strcmp(*argv, "inherit") != 0) { |
169 | if (rtnl_dsfield_a2n(&uval, *argv)) | |
170 | invarg("bad TOS value", *argv); | |
171 | tos = uval; | |
172 | } else | |
173 | tos = 1; | |
01c65995 | 174 | addattr8(n, 1024, IFLA_VXLAN_TOS, tos); |
3d98eba4 SB |
175 | } else if (!matches(*argv, "df")) { |
176 | enum ifla_vxlan_df df; | |
177 | ||
178 | NEXT_ARG(); | |
179 | check_duparg(&attrs, IFLA_VXLAN_DF, "df", *argv); | |
180 | if (strcmp(*argv, "unset") == 0) | |
181 | df = VXLAN_DF_UNSET; | |
182 | else if (strcmp(*argv, "set") == 0) | |
183 | df = VXLAN_DF_SET; | |
184 | else if (strcmp(*argv, "inherit") == 0) | |
185 | df = VXLAN_DF_INHERIT; | |
186 | else | |
187 | invarg("DF must be 'unset', 'set' or 'inherit'", | |
188 | *argv); | |
189 | ||
190 | addattr8(n, 1024, IFLA_VXLAN_DF, df); | |
f8eb79a6 DB |
191 | } else if (!matches(*argv, "label") || |
192 | !matches(*argv, "flowlabel")) { | |
193 | __u32 uval; | |
194 | ||
195 | NEXT_ARG(); | |
01c65995 GM |
196 | check_duparg(&attrs, IFLA_VXLAN_LABEL, "flowlabel", |
197 | *argv); | |
f8eb79a6 DB |
198 | if (get_u32(&uval, *argv, 0) || |
199 | (uval & ~LABEL_MAX_MASK)) | |
200 | invarg("invalid flowlabel", *argv); | |
01c65995 | 201 | addattr32(n, 1024, IFLA_VXLAN_LABEL, htonl(uval)); |
a5494df2 | 202 | } else if (!matches(*argv, "ageing")) { |
01c65995 GM |
203 | __u32 age; |
204 | ||
a5494df2 | 205 | NEXT_ARG(); |
01c65995 GM |
206 | check_duparg(&attrs, IFLA_VXLAN_AGEING, "ageing", |
207 | *argv); | |
a5494df2 | 208 | if (strcmp(*argv, "none") == 0) |
01c65995 | 209 | age = 0; |
a5494df2 | 210 | else if (get_u32(&age, *argv, 0)) |
2d596120 | 211 | invarg("ageing timer", *argv); |
01c65995 | 212 | addattr32(n, 1024, IFLA_VXLAN_AGEING, age); |
a5494df2 | 213 | } else if (!matches(*argv, "maxaddress")) { |
01c65995 GM |
214 | __u32 maxaddr; |
215 | ||
a5494df2 | 216 | NEXT_ARG(); |
01c65995 GM |
217 | check_duparg(&attrs, IFLA_VXLAN_LIMIT, |
218 | "maxaddress", *argv); | |
a5494df2 SH |
219 | if (strcmp(*argv, "unlimited") == 0) |
220 | maxaddr = 0; | |
221 | else if (get_u32(&maxaddr, *argv, 0)) | |
2d596120 | 222 | invarg("max addresses", *argv); |
01c65995 | 223 | addattr32(n, 1024, IFLA_VXLAN_LIMIT, maxaddr); |
514cdfb4 SH |
224 | } else if (!matches(*argv, "port") || |
225 | !matches(*argv, "srcport")) { | |
01c65995 GM |
226 | struct ifla_vxlan_port_range range = { 0, 0 }; |
227 | ||
2d596120 | 228 | NEXT_ARG(); |
01c65995 GM |
229 | check_duparg(&attrs, IFLA_VXLAN_PORT_RANGE, "srcport", |
230 | *argv); | |
9f7401fa | 231 | if (get_be16(&range.low, *argv, 0)) |
2d596120 SH |
232 | invarg("min port", *argv); |
233 | NEXT_ARG(); | |
9f7401fa | 234 | if (get_be16(&range.high, *argv, 0)) |
2d596120 | 235 | invarg("max port", *argv); |
01c65995 GM |
236 | if (range.low || range.high) { |
237 | addattr_l(n, 1024, IFLA_VXLAN_PORT_RANGE, | |
238 | &range, sizeof(range)); | |
239 | } | |
56f5daac | 240 | } else if (!matches(*argv, "dstport")) { |
514cdfb4 | 241 | NEXT_ARG(); |
01c65995 | 242 | check_duparg(&attrs, IFLA_VXLAN_PORT, "dstport", *argv); |
514cdfb4 SH |
243 | if (get_u16(&dstport, *argv, 0)) |
244 | invarg("dst port", *argv); | |
a5494df2 | 245 | } else if (!matches(*argv, "nolearning")) { |
01c65995 | 246 | check_duparg(&attrs, IFLA_VXLAN_LEARNING, *argv, *argv); |
a5494df2 SH |
247 | learning = 0; |
248 | } else if (!matches(*argv, "learning")) { | |
01c65995 | 249 | check_duparg(&attrs, IFLA_VXLAN_LEARNING, *argv, *argv); |
a5494df2 | 250 | learning = 1; |
1556e29d | 251 | } else if (!matches(*argv, "noproxy")) { |
01c65995 GM |
252 | check_duparg(&attrs, IFLA_VXLAN_PROXY, *argv, *argv); |
253 | addattr8(n, 1024, IFLA_VXLAN_PROXY, 0); | |
1556e29d | 254 | } else if (!matches(*argv, "proxy")) { |
01c65995 GM |
255 | check_duparg(&attrs, IFLA_VXLAN_PROXY, *argv, *argv); |
256 | addattr8(n, 1024, IFLA_VXLAN_PROXY, 1); | |
1556e29d | 257 | } else if (!matches(*argv, "norsc")) { |
01c65995 GM |
258 | check_duparg(&attrs, IFLA_VXLAN_RSC, *argv, *argv); |
259 | addattr8(n, 1024, IFLA_VXLAN_RSC, 0); | |
1556e29d | 260 | } else if (!matches(*argv, "rsc")) { |
01c65995 GM |
261 | check_duparg(&attrs, IFLA_VXLAN_RSC, *argv, *argv); |
262 | addattr8(n, 1024, IFLA_VXLAN_RSC, 1); | |
1556e29d | 263 | } else if (!matches(*argv, "nol2miss")) { |
01c65995 GM |
264 | check_duparg(&attrs, IFLA_VXLAN_L2MISS, *argv, *argv); |
265 | addattr8(n, 1024, IFLA_VXLAN_L2MISS, 0); | |
1556e29d | 266 | } else if (!matches(*argv, "l2miss")) { |
01c65995 GM |
267 | check_duparg(&attrs, IFLA_VXLAN_L2MISS, *argv, *argv); |
268 | addattr8(n, 1024, IFLA_VXLAN_L2MISS, 1); | |
1556e29d | 269 | } else if (!matches(*argv, "nol3miss")) { |
01c65995 GM |
270 | check_duparg(&attrs, IFLA_VXLAN_L3MISS, *argv, *argv); |
271 | addattr8(n, 1024, IFLA_VXLAN_L3MISS, 0); | |
1556e29d | 272 | } else if (!matches(*argv, "l3miss")) { |
01c65995 GM |
273 | check_duparg(&attrs, IFLA_VXLAN_L3MISS, *argv, *argv); |
274 | addattr8(n, 1024, IFLA_VXLAN_L3MISS, 1); | |
666cdc50 | 275 | } else if (!matches(*argv, "udpcsum")) { |
01c65995 GM |
276 | check_duparg(&attrs, IFLA_VXLAN_UDP_CSUM, *argv, *argv); |
277 | addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, 1); | |
666cdc50 | 278 | } else if (!matches(*argv, "noudpcsum")) { |
01c65995 GM |
279 | check_duparg(&attrs, IFLA_VXLAN_UDP_CSUM, *argv, *argv); |
280 | addattr8(n, 1024, IFLA_VXLAN_UDP_CSUM, 0); | |
666cdc50 | 281 | } else if (!matches(*argv, "udp6zerocsumtx")) { |
01c65995 GM |
282 | check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
283 | *argv, *argv); | |
284 | addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 1); | |
666cdc50 | 285 | } else if (!matches(*argv, "noudp6zerocsumtx")) { |
01c65995 GM |
286 | check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
287 | *argv, *argv); | |
288 | addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, 0); | |
666cdc50 | 289 | } else if (!matches(*argv, "udp6zerocsumrx")) { |
01c65995 GM |
290 | check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
291 | *argv, *argv); | |
292 | addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1); | |
666cdc50 | 293 | } else if (!matches(*argv, "noudp6zerocsumrx")) { |
01c65995 GM |
294 | check_duparg(&attrs, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
295 | *argv, *argv); | |
296 | addattr8(n, 1024, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 0); | |
35f59d86 | 297 | } else if (!matches(*argv, "remcsumtx")) { |
01c65995 GM |
298 | check_duparg(&attrs, IFLA_VXLAN_REMCSUM_TX, |
299 | *argv, *argv); | |
300 | addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, 1); | |
35f59d86 | 301 | } else if (!matches(*argv, "noremcsumtx")) { |
01c65995 GM |
302 | check_duparg(&attrs, IFLA_VXLAN_REMCSUM_TX, |
303 | *argv, *argv); | |
304 | addattr8(n, 1024, IFLA_VXLAN_REMCSUM_TX, 0); | |
35f59d86 | 305 | } else if (!matches(*argv, "remcsumrx")) { |
01c65995 GM |
306 | check_duparg(&attrs, IFLA_VXLAN_REMCSUM_RX, |
307 | *argv, *argv); | |
308 | addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, 1); | |
35f59d86 | 309 | } else if (!matches(*argv, "noremcsumrx")) { |
01c65995 GM |
310 | check_duparg(&attrs, IFLA_VXLAN_REMCSUM_RX, |
311 | *argv, *argv); | |
312 | addattr8(n, 1024, IFLA_VXLAN_REMCSUM_RX, 0); | |
e79c327e | 313 | } else if (!matches(*argv, "external")) { |
01c65995 GM |
314 | check_duparg(&attrs, IFLA_VXLAN_COLLECT_METADATA, |
315 | *argv, *argv); | |
e79c327e | 316 | metadata = 1; |
44df4597 | 317 | learning = 0; |
01c65995 GM |
318 | /* we will add LEARNING attribute outside of the loop */ |
319 | addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA, | |
320 | metadata); | |
e79c327e | 321 | } else if (!matches(*argv, "noexternal")) { |
01c65995 GM |
322 | check_duparg(&attrs, IFLA_VXLAN_COLLECT_METADATA, |
323 | *argv, *argv); | |
e79c327e | 324 | metadata = 0; |
01c65995 GM |
325 | addattr8(n, 1024, IFLA_VXLAN_COLLECT_METADATA, |
326 | metadata); | |
2eb90dc7 | 327 | } else if (!matches(*argv, "gbp")) { |
01c65995 GM |
328 | check_duparg(&attrs, IFLA_VXLAN_GBP, *argv, *argv); |
329 | addattr_l(n, 1024, IFLA_VXLAN_GBP, NULL, 0); | |
346410bd | 330 | } else if (!matches(*argv, "gpe")) { |
01c65995 GM |
331 | check_duparg(&attrs, IFLA_VXLAN_GPE, *argv, *argv); |
332 | addattr_l(n, 1024, IFLA_VXLAN_GPE, NULL, 0); | |
a5494df2 SH |
333 | } else if (matches(*argv, "help") == 0) { |
334 | explain(); | |
335 | return -1; | |
336 | } else { | |
14645ec2 | 337 | fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv); |
a5494df2 SH |
338 | explain(); |
339 | return -1; | |
340 | } | |
341 | argc--, argv++; | |
342 | } | |
2a126a85 | 343 | |
01c65995 | 344 | if (metadata && VXLAN_ATTRSET(attrs, IFLA_VXLAN_ID)) { |
e79c327e PA |
345 | fprintf(stderr, "vxlan: both 'external' and vni cannot be specified\n"); |
346 | return -1; | |
347 | } | |
348 | ||
b6fae788 | 349 | if (!metadata && !VXLAN_ATTRSET(attrs, IFLA_VXLAN_ID) && !set_op) { |
a5494df2 SH |
350 | fprintf(stderr, "vxlan: missing virtual network identifier\n"); |
351 | return -1; | |
352 | } | |
514cdfb4 | 353 | |
1e9b8072 | 354 | if (is_addrtype_inet_multi(&daddr) && |
01c65995 | 355 | !VXLAN_ATTRSET(attrs, IFLA_VXLAN_LINK)) { |
e2cfe550 JB |
356 | fprintf(stderr, "vxlan: 'group' requires 'dev' to be specified\n"); |
357 | return -1; | |
358 | } | |
359 | ||
01c65995 GM |
360 | if (!VXLAN_ATTRSET(attrs, IFLA_VXLAN_PORT) && |
361 | VXLAN_ATTRSET(attrs, IFLA_VXLAN_GPE)) { | |
346410bd | 362 | dstport = 4790; |
01c65995 | 363 | } else if (!VXLAN_ATTRSET(attrs, IFLA_VXLAN_PORT) && !set_op) { |
514cdfb4 SH |
364 | fprintf(stderr, "vxlan: destination port not specified\n" |
365 | "Will use Linux kernel default (non-standard value)\n"); | |
0612519e | 366 | fprintf(stderr, |
514cdfb4 SH |
367 | "Use 'dstport 4789' to get the IANA assigned value\n" |
368 | "Use 'dstport 0' to get default and quiet this message\n"); | |
369 | } | |
370 | ||
b6fae788 RS |
371 | if (VXLAN_ATTRSET(attrs, IFLA_VXLAN_ID)) |
372 | addattr32(n, 1024, IFLA_VXLAN_ID, vni); | |
1e9b8072 SP |
373 | |
374 | if (is_addrtype_inet(&saddr)) { | |
375 | int type = (saddr.family == AF_INET) ? IFLA_VXLAN_LOCAL | |
376 | : IFLA_VXLAN_LOCAL6; | |
377 | addattr_l(n, 1024, type, saddr.data, saddr.bytelen); | |
c1360e3b | 378 | selected_family = true; |
1e9b8072 SP |
379 | } |
380 | ||
381 | if (is_addrtype_inet(&daddr)) { | |
382 | int type = (daddr.family == AF_INET) ? IFLA_VXLAN_GROUP | |
383 | : IFLA_VXLAN_GROUP6; | |
384 | addattr_l(n, 1024, type, daddr.data, daddr.bytelen); | |
c1360e3b LB |
385 | selected_family = true; |
386 | } | |
387 | ||
388 | if (!selected_family) { | |
389 | if (preferred_family == AF_INET) { | |
390 | get_addr(&daddr, "default", AF_INET); | |
391 | addattr_l(n, 1024, IFLA_VXLAN_GROUP, | |
392 | daddr.data, daddr.bytelen); | |
393 | } else if (preferred_family == AF_INET6) { | |
394 | get_addr(&daddr, "default", AF_INET6); | |
395 | addattr_l(n, 1024, IFLA_VXLAN_GROUP6, | |
396 | daddr.data, daddr.bytelen); | |
397 | } | |
1e9b8072 | 398 | } |
aa574cd6 | 399 | |
01c65995 GM |
400 | if (!set_op || VXLAN_ATTRSET(attrs, IFLA_VXLAN_LEARNING)) |
401 | addattr8(n, 1024, IFLA_VXLAN_LEARNING, learning); | |
514cdfb4 | 402 | |
514cdfb4 SH |
403 | if (dstport) |
404 | addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport)); | |
a5494df2 SH |
405 | |
406 | return 0; | |
407 | } | |
408 | ||
409 | static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
410 | { | |
411 | __u32 vni; | |
375560c4 | 412 | __u8 ttl = 0; |
3caa526c | 413 | __u8 tos = 0; |
2d596120 | 414 | __u32 maxaddr; |
a5494df2 SH |
415 | |
416 | if (!tb) | |
417 | return; | |
418 | ||
00ff4b8e SP |
419 | if (tb[IFLA_VXLAN_COLLECT_METADATA] && |
420 | rta_getattr_u8(tb[IFLA_VXLAN_COLLECT_METADATA])) { | |
421 | print_bool(PRINT_ANY, "external", "external", true); | |
422 | return; | |
423 | } | |
424 | ||
a5494df2 SH |
425 | if (!tb[IFLA_VXLAN_ID] || |
426 | RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32)) | |
427 | return; | |
428 | ||
429 | vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]); | |
3b98d9b8 | 430 | print_uint(PRINT_ANY, "id", "id %u ", vni); |
a5494df2 SH |
431 | |
432 | if (tb[IFLA_VXLAN_GROUP]) { | |
b64da5a5 | 433 | __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]); |
56f5daac | 434 | |
7cfa3802 AW |
435 | if (addr) { |
436 | if (IN_MULTICAST(ntohl(addr))) | |
3b98d9b8 JF |
437 | print_string(PRINT_ANY, |
438 | "group", | |
439 | "group %s ", | |
440 | format_host(AF_INET, 4, &addr)); | |
7cfa3802 | 441 | else |
3b98d9b8 JF |
442 | print_string(PRINT_ANY, |
443 | "remote", | |
444 | "remote %s ", | |
445 | format_host(AF_INET, 4, &addr)); | |
7cfa3802 | 446 | } |
aa574cd6 WC |
447 | } else if (tb[IFLA_VXLAN_GROUP6]) { |
448 | struct in6_addr addr; | |
56f5daac | 449 | |
aa574cd6 | 450 | memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_GROUP6]), sizeof(struct in6_addr)); |
1f4c51c0 | 451 | if (!IN6_IS_ADDR_UNSPECIFIED(&addr)) { |
aa574cd6 | 452 | if (IN6_IS_ADDR_MULTICAST(&addr)) |
3b98d9b8 JF |
453 | print_string(PRINT_ANY, |
454 | "group6", | |
455 | "group %s ", | |
456 | format_host(AF_INET6, | |
457 | sizeof(struct in6_addr), | |
458 | &addr)); | |
aa574cd6 | 459 | else |
3b98d9b8 JF |
460 | print_string(PRINT_ANY, |
461 | "remote6", | |
462 | "remote %s ", | |
463 | format_host(AF_INET6, | |
464 | sizeof(struct in6_addr), | |
465 | &addr)); | |
aa574cd6 | 466 | } |
a5494df2 SH |
467 | } |
468 | ||
469 | if (tb[IFLA_VXLAN_LOCAL]) { | |
b64da5a5 | 470 | __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]); |
56f5daac | 471 | |
a5494df2 | 472 | if (addr) |
3b98d9b8 JF |
473 | print_string(PRINT_ANY, |
474 | "local", | |
475 | "local %s ", | |
476 | format_host(AF_INET, 4, &addr)); | |
aa574cd6 WC |
477 | } else if (tb[IFLA_VXLAN_LOCAL6]) { |
478 | struct in6_addr addr; | |
56f5daac | 479 | |
aa574cd6 | 480 | memcpy(&addr, RTA_DATA(tb[IFLA_VXLAN_LOCAL6]), sizeof(struct in6_addr)); |
1f4c51c0 | 481 | if (!IN6_IS_ADDR_UNSPECIFIED(&addr)) |
3b98d9b8 JF |
482 | print_string(PRINT_ANY, |
483 | "local6", | |
484 | "local %s ", | |
485 | format_host(AF_INET6, | |
486 | sizeof(struct in6_addr), | |
487 | &addr)); | |
a5494df2 SH |
488 | } |
489 | ||
45d3a6ef SP |
490 | if (tb[IFLA_VXLAN_LINK]) { |
491 | unsigned int link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]); | |
a5494df2 | 492 | |
45d3a6ef SP |
493 | if (link) { |
494 | print_string(PRINT_ANY, "link", "dev %s ", | |
495 | ll_index_to_name(link)); | |
496 | } | |
a5494df2 SH |
497 | } |
498 | ||
2d596120 SH |
499 | if (tb[IFLA_VXLAN_PORT_RANGE]) { |
500 | const struct ifla_vxlan_port_range *r | |
501 | = RTA_DATA(tb[IFLA_VXLAN_PORT_RANGE]); | |
3b98d9b8 JF |
502 | if (is_json_context()) { |
503 | open_json_object("port_range"); | |
504 | print_uint(PRINT_JSON, "low", NULL, ntohs(r->low)); | |
505 | print_uint(PRINT_JSON, "high", NULL, ntohs(r->high)); | |
506 | close_json_object(); | |
507 | } else { | |
508 | fprintf(f, "srcport %u %u ", | |
509 | ntohs(r->low), ntohs(r->high)); | |
510 | } | |
1556e29d | 511 | } |
2d596120 | 512 | |
514cdfb4 | 513 | if (tb[IFLA_VXLAN_PORT]) |
3b98d9b8 JF |
514 | print_uint(PRINT_ANY, |
515 | "port", | |
516 | "dstport %u ", | |
517 | rta_getattr_be16(tb[IFLA_VXLAN_PORT])); | |
514cdfb4 | 518 | |
3b98d9b8 JF |
519 | if (tb[IFLA_VXLAN_LEARNING]) { |
520 | __u8 learning = rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]); | |
521 | ||
522 | print_bool(PRINT_JSON, "learning", NULL, learning); | |
523 | if (!learning) | |
524 | print_bool(PRINT_FP, NULL, "nolearning ", true); | |
525 | } | |
1556e29d DS |
526 | |
527 | if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY])) | |
3b98d9b8 | 528 | print_bool(PRINT_ANY, "proxy", "proxy ", true); |
1556e29d DS |
529 | |
530 | if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC])) | |
3b98d9b8 | 531 | print_bool(PRINT_ANY, "rsc", "rsc ", true); |
1556e29d DS |
532 | |
533 | if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS])) | |
3b98d9b8 | 534 | print_bool(PRINT_ANY, "l2miss", "l2miss ", true); |
1556e29d DS |
535 | |
536 | if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS])) | |
3b98d9b8 | 537 | print_bool(PRINT_ANY, "l3miss", "l3miss ", true); |
1556e29d | 538 | |
3caa526c SP |
539 | if (tb[IFLA_VXLAN_TOS]) |
540 | tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]); | |
541 | if (tos) { | |
542 | if (is_json_context() || tos != 1) | |
90c5c969 | 543 | print_0xhex(PRINT_ANY, "tos", "tos %#llx ", tos); |
3caa526c SP |
544 | else |
545 | print_string(PRINT_FP, NULL, "tos %s ", "inherit"); | |
a5494df2 SH |
546 | } |
547 | ||
952a7a19 HL |
548 | if (tb[IFLA_VXLAN_TTL_INHERIT] && |
549 | rta_getattr_u8(tb[IFLA_VXLAN_TTL_INHERIT])) { | |
375560c4 | 550 | print_string(PRINT_FP, NULL, "ttl %s ", "inherit"); |
952a7a19 HL |
551 | } else if (tb[IFLA_VXLAN_TTL]) { |
552 | ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]); | |
553 | if (is_json_context() || ttl) | |
554 | print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl); | |
555 | else | |
556 | print_string(PRINT_FP, NULL, "ttl %s ", "auto"); | |
557 | } | |
a5494df2 | 558 | |
3d98eba4 SB |
559 | if (tb[IFLA_VXLAN_DF]) { |
560 | enum ifla_vxlan_df df = rta_getattr_u8(tb[IFLA_VXLAN_DF]); | |
561 | ||
562 | if (df == VXLAN_DF_UNSET) | |
563 | print_string(PRINT_JSON, "df", "df %s ", "unset"); | |
564 | else if (df == VXLAN_DF_SET) | |
565 | print_string(PRINT_ANY, "df", "df %s ", "set"); | |
566 | else if (df == VXLAN_DF_INHERIT) | |
567 | print_string(PRINT_ANY, "df", "df %s ", "inherit"); | |
568 | } | |
569 | ||
f8eb79a6 DB |
570 | if (tb[IFLA_VXLAN_LABEL]) { |
571 | __u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]); | |
572 | ||
573 | if (label) | |
90c5c969 SH |
574 | print_0xhex(PRINT_ANY, "label", |
575 | "flowlabel %#llx ", ntohl(label)); | |
f8eb79a6 DB |
576 | } |
577 | ||
a5494df2 SH |
578 | if (tb[IFLA_VXLAN_AGEING]) { |
579 | __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]); | |
56f5daac | 580 | |
a5494df2 | 581 | if (age == 0) |
3b98d9b8 | 582 | print_uint(PRINT_ANY, "ageing", "ageing none ", 0); |
a5494df2 | 583 | else |
3b98d9b8 | 584 | print_uint(PRINT_ANY, "ageing", "ageing %u ", age); |
a5494df2 | 585 | } |
2d596120 SH |
586 | |
587 | if (tb[IFLA_VXLAN_LIMIT] && | |
6ad5399c | 588 | ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0)) |
3b98d9b8 | 589 | print_uint(PRINT_ANY, "limit", "maxaddr %u ", maxaddr); |
666cdc50 | 590 | |
af325398 | 591 | if (tb[IFLA_VXLAN_UDP_CSUM]) { |
3b98d9b8 JF |
592 | __u8 udp_csum = rta_getattr_u8(tb[IFLA_VXLAN_UDP_CSUM]); |
593 | ||
594 | if (is_json_context()) { | |
595 | print_bool(PRINT_ANY, "udp_csum", NULL, udp_csum); | |
596 | } else { | |
597 | if (!udp_csum) | |
598 | fputs("no", f); | |
599 | fputs("udpcsum ", f); | |
600 | } | |
af325398 | 601 | } |
666cdc50 | 602 | |
af325398 | 603 | if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) { |
3b98d9b8 JF |
604 | __u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]); |
605 | ||
606 | if (is_json_context()) { | |
607 | print_bool(PRINT_ANY, | |
608 | "udp_zero_csum6_tx", NULL, csum6); | |
609 | } else { | |
610 | if (!csum6) | |
611 | fputs("no", f); | |
612 | fputs("udp6zerocsumtx ", f); | |
613 | } | |
af325398 | 614 | } |
666cdc50 | 615 | |
af325398 | 616 | if (tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) { |
3b98d9b8 JF |
617 | __u8 csum6 = rta_getattr_u8(tb[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]); |
618 | ||
619 | if (is_json_context()) { | |
620 | print_bool(PRINT_ANY, | |
621 | "udp_zero_csum6_rx", | |
622 | NULL, | |
623 | csum6); | |
624 | } else { | |
625 | if (!csum6) | |
626 | fputs("no", f); | |
627 | fputs("udp6zerocsumrx ", f); | |
628 | } | |
af325398 | 629 | } |
2eb90dc7 | 630 | |
35f59d86 TH |
631 | if (tb[IFLA_VXLAN_REMCSUM_TX] && |
632 | rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_TX])) | |
3b98d9b8 | 633 | print_bool(PRINT_ANY, "remcsum_tx", "remcsumtx ", true); |
35f59d86 TH |
634 | |
635 | if (tb[IFLA_VXLAN_REMCSUM_RX] && | |
636 | rta_getattr_u8(tb[IFLA_VXLAN_REMCSUM_RX])) | |
3b98d9b8 | 637 | print_bool(PRINT_ANY, "remcsum_rx", "remcsumrx ", true); |
35f59d86 | 638 | |
2eb90dc7 | 639 | if (tb[IFLA_VXLAN_GBP]) |
3b98d9b8 | 640 | print_bool(PRINT_ANY, "gbp", "gbp ", true); |
346410bd | 641 | if (tb[IFLA_VXLAN_GPE]) |
3b98d9b8 | 642 | print_bool(PRINT_ANY, "gpe", "gpe ", true); |
a5494df2 SH |
643 | } |
644 | ||
561e650e | 645 | static void vxlan_print_help(struct link_util *lu, int argc, char **argv, |
3b98d9b8 | 646 | FILE *f) |
561e650e | 647 | { |
648 | print_explain(f); | |
649 | } | |
650 | ||
a5494df2 SH |
651 | struct link_util vxlan_link_util = { |
652 | .id = "vxlan", | |
653 | .maxattr = IFLA_VXLAN_MAX, | |
654 | .parse_opt = vxlan_parse_opt, | |
655 | .print_opt = vxlan_print_opt, | |
561e650e | 656 | .print_help = vxlan_print_help, |
a5494df2 | 657 | }; |