]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_vxlan.c
ip/vxlan: fix display of maxaddress option
[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 explain(void)
25 {
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");
37 }
38
39 static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
40 struct nlmsghdr *n)
41 {
42 __u32 vni = 0;
43 int vni_set = 0;
44 __u32 saddr = 0;
45 __u32 gaddr = 0;
46 __u32 daddr = 0;
47 struct in6_addr saddr6 = IN6ADDR_ANY_INIT;
48 struct in6_addr gaddr6 = IN6ADDR_ANY_INIT;
49 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
50 unsigned link = 0;
51 __u8 tos = 0;
52 __u8 ttl = 0;
53 __u8 learning = 1;
54 __u8 proxy = 0;
55 __u8 rsc = 0;
56 __u8 l2miss = 0;
57 __u8 l3miss = 0;
58 __u8 noage = 0;
59 __u32 age = 0;
60 __u32 maxaddr = 0;
61 __u16 dstport = 0;
62 int dst_port_set = 0;
63 struct ifla_vxlan_port_range range = { 0, 0 };
64
65 while (argc > 0) {
66 if (!matches(*argv, "id") ||
67 !matches(*argv, "vni")) {
68 NEXT_ARG();
69 if (get_u32(&vni, *argv, 0) ||
70 vni >= 1u << 24)
71 invarg("invalid id", *argv);
72 vni_set = 1;
73 } else if (!matches(*argv, "group")) {
74 NEXT_ARG();
75 if (!inet_get_addr(*argv, &gaddr, &gaddr6)) {
76 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
77 return -1;
78 }
79 if (!IN6_IS_ADDR_MULTICAST(&gaddr6) && !IN_MULTICAST(ntohl(gaddr)))
80 invarg("invalid group address", *argv);
81 } else if (!matches(*argv, "remote")) {
82 NEXT_ARG();
83 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
84 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
85 return -1;
86 }
87 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
88 invarg("invalid remote address", *argv);
89 } else if (!matches(*argv, "local")) {
90 NEXT_ARG();
91 if (strcmp(*argv, "any")) {
92 if (!inet_get_addr(*argv, &saddr, &saddr6)) {
93 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
94 return -1;
95 }
96 }
97
98 if (IN_MULTICAST(ntohl(saddr)) || IN6_IS_ADDR_MULTICAST(&saddr6))
99 invarg("invalid local address", *argv);
100 } else if (!matches(*argv, "dev")) {
101 NEXT_ARG();
102 link = if_nametoindex(*argv);
103 if (link == 0) {
104 fprintf(stderr, "Cannot find device \"%s\"\n",
105 *argv);
106 exit(-1);
107 }
108 } else if (!matches(*argv, "ttl") ||
109 !matches(*argv, "hoplimit")) {
110 unsigned uval;
111
112 NEXT_ARG();
113 if (strcmp(*argv, "inherit") != 0) {
114 if (get_unsigned(&uval, *argv, 0))
115 invarg("invalid TTL", *argv);
116 if (uval > 255)
117 invarg("TTL must be <= 255", *argv);
118 ttl = uval;
119 }
120 } else if (!matches(*argv, "tos") ||
121 !matches(*argv, "dsfield")) {
122 __u32 uval;
123
124 NEXT_ARG();
125 if (strcmp(*argv, "inherit") != 0) {
126 if (rtnl_dsfield_a2n(&uval, *argv))
127 invarg("bad TOS value", *argv);
128 tos = uval;
129 } else
130 tos = 1;
131 } else if (!matches(*argv, "ageing")) {
132 NEXT_ARG();
133 if (strcmp(*argv, "none") == 0)
134 noage = 1;
135 else if (get_u32(&age, *argv, 0))
136 invarg("ageing timer", *argv);
137 } else if (!matches(*argv, "maxaddress")) {
138 NEXT_ARG();
139 if (strcmp(*argv, "unlimited") == 0)
140 maxaddr = 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;
146 NEXT_ARG();
147 if (get_u16(&minport, *argv, 0))
148 invarg("min port", *argv);
149 NEXT_ARG();
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")){
155 NEXT_ARG();
156 if (get_u16(&dstport, *argv, 0))
157 invarg("dst port", *argv);
158 dst_port_set = 1;
159 } else if (!matches(*argv, "nolearning")) {
160 learning = 0;
161 } else if (!matches(*argv, "learning")) {
162 learning = 1;
163 } else if (!matches(*argv, "noproxy")) {
164 proxy = 0;
165 } else if (!matches(*argv, "proxy")) {
166 proxy = 1;
167 } else if (!matches(*argv, "norsc")) {
168 rsc = 0;
169 } else if (!matches(*argv, "rsc")) {
170 rsc = 1;
171 } else if (!matches(*argv, "nol2miss")) {
172 l2miss = 0;
173 } else if (!matches(*argv, "l2miss")) {
174 l2miss = 1;
175 } else if (!matches(*argv, "nol3miss")) {
176 l3miss = 0;
177 } else if (!matches(*argv, "l3miss")) {
178 l3miss = 1;
179 } else if (matches(*argv, "help") == 0) {
180 explain();
181 return -1;
182 } else {
183 fprintf(stderr, "vxlan: unknown command \"%s\"?\n", *argv);
184 explain();
185 return -1;
186 }
187 argc--, argv++;
188 }
189
190 if (!vni_set) {
191 fprintf(stderr, "vxlan: missing virtual network identifier\n");
192 return -1;
193 }
194
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");
199 return -1;
200 }
201
202 if (!dst_port_set) {
203 fprintf(stderr, "vxlan: destination port not specified\n"
204 "Will use Linux kernel default (non-standard value)\n");
205 fprintf(stderr,
206 "Use 'dstport 4789' to get the IANA assigned value\n"
207 "Use 'dstport 0' to get default and quiet this message\n");
208 }
209
210 addattr32(n, 1024, IFLA_VXLAN_ID, vni);
211 if (gaddr)
212 addattr_l(n, 1024, IFLA_VXLAN_GROUP, &gaddr, 4);
213 else if (daddr)
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));
219
220 if (saddr)
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));
224
225 if (link)
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);
234
235 if (noage)
236 addattr32(n, 1024, IFLA_VXLAN_AGEING, 0);
237 else if (age)
238 addattr32(n, 1024, IFLA_VXLAN_AGEING, age);
239 if (maxaddr)
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));
244 if (dstport)
245 addattr16(n, 1024, IFLA_VXLAN_PORT, htons(dstport));
246
247 return 0;
248 }
249
250 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
251 {
252 __u32 vni;
253 unsigned link;
254 __u8 tos;
255 __u32 maxaddr;
256 char s1[1024];
257 char s2[64];
258
259 if (!tb)
260 return;
261
262 if (!tb[IFLA_VXLAN_ID] ||
263 RTA_PAYLOAD(tb[IFLA_VXLAN_ID]) < sizeof(__u32))
264 return;
265
266 vni = rta_getattr_u32(tb[IFLA_VXLAN_ID]);
267 fprintf(f, "id %u ", vni);
268
269 if (tb[IFLA_VXLAN_GROUP]) {
270 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_GROUP]);
271 if (addr) {
272 if (IN_MULTICAST(ntohl(addr)))
273 fprintf(f, "group %s ",
274 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
275 else
276 fprintf(f, "remote %s ",
277 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
278 }
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)));
286 else
287 fprintf(f, "remote %s ",
288 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
289 }
290 }
291
292 if (tb[IFLA_VXLAN_LOCAL]) {
293 __be32 addr = rta_getattr_u32(tb[IFLA_VXLAN_LOCAL]);
294 if (addr)
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)));
303 }
304
305 if (tb[IFLA_VXLAN_LINK] &&
306 (link = rta_getattr_u32(tb[IFLA_VXLAN_LINK]))) {
307 const char *n = if_indextoname(link, s2);
308
309 if (n)
310 fprintf(f, "dev %s ", n);
311 else
312 fprintf(f, "dev %u ", link);
313 }
314
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));
319 }
320
321 if (tb[IFLA_VXLAN_PORT])
322 fprintf(f, "dstport %u ",
323 ntohs(rta_getattr_u16(tb[IFLA_VXLAN_PORT])));
324
325 if (tb[IFLA_VXLAN_LEARNING] &&
326 !rta_getattr_u8(tb[IFLA_VXLAN_LEARNING]))
327 fputs("nolearning ", f);
328
329 if (tb[IFLA_VXLAN_PROXY] && rta_getattr_u8(tb[IFLA_VXLAN_PROXY]))
330 fputs("proxy ", f);
331
332 if (tb[IFLA_VXLAN_RSC] && rta_getattr_u8(tb[IFLA_VXLAN_RSC]))
333 fputs("rsc ", f);
334
335 if (tb[IFLA_VXLAN_L2MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L2MISS]))
336 fputs("l2miss ", f);
337
338 if (tb[IFLA_VXLAN_L3MISS] && rta_getattr_u8(tb[IFLA_VXLAN_L3MISS]))
339 fputs("l3miss ", f);
340
341 if (tb[IFLA_VXLAN_TOS] &&
342 (tos = rta_getattr_u8(tb[IFLA_VXLAN_TOS]))) {
343 if (tos == 1)
344 fprintf(f, "tos inherit ");
345 else
346 fprintf(f, "tos %#x ", tos);
347 }
348
349 if (tb[IFLA_VXLAN_TTL]) {
350 __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
351 if (ttl)
352 fprintf(f, "ttl %d ", ttl);
353 }
354
355 if (tb[IFLA_VXLAN_AGEING]) {
356 __u32 age = rta_getattr_u32(tb[IFLA_VXLAN_AGEING]);
357 if (age == 0)
358 fprintf(f, "ageing none ");
359 else
360 fprintf(f, "ageing %u ", age);
361 }
362
363 if (tb[IFLA_VXLAN_LIMIT] &&
364 ((maxaddr = rta_getattr_u32(tb[IFLA_VXLAN_LIMIT])) != 0))
365 fprintf(f, "maxaddr %u ", maxaddr);
366 }
367
368 struct link_util vxlan_link_util = {
369 .id = "vxlan",
370 .maxattr = IFLA_VXLAN_MAX,
371 .parse_opt = vxlan_parse_opt,
372 .print_opt = vxlan_print_opt,
373 };