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