]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/link_gre.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / link_gre.c
1 /*
2 * link_gre.c gre driver module
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: Herbert Xu <herbert@gondor.apana.org.au>
10 *
11 */
12
13 #include <string.h>
14 #include <net/if.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
18
19 #include <linux/ip.h>
20 #include <linux/if_tunnel.h>
21 #include "rt_names.h"
22 #include "utils.h"
23 #include "ip_common.h"
24 #include "tunnel.h"
25
26 static void print_usage(FILE *f)
27 {
28 fprintf(f,
29 "Usage: ... { gre | gretap | erspan } [ remote ADDR ]\n"
30 " [ local ADDR ]\n"
31 " [ [i|o]seq ]\n"
32 " [ [i|o]key KEY ]\n"
33 " [ [i|o]csum ]\n"
34 " [ ttl TTL ]\n"
35 " [ tos TOS ]\n"
36 " [ [no]pmtudisc ]\n"
37 " [ [no]ignore-df ]\n"
38 " [ dev PHYS_DEV ]\n"
39 " [ noencap ]\n"
40 " [ encap { fou | gue | none } ]\n"
41 " [ encap-sport PORT ]\n"
42 " [ encap-dport PORT ]\n"
43 " [ [no]encap-csum ]\n"
44 " [ [no]encap-csum6 ]\n"
45 " [ [no]encap-remcsum ]\n"
46 " [ external ]\n"
47 " [ fwmark MARK ]\n"
48 " [ erspan_ver version ]\n"
49 " [ erspan IDX ]\n"
50 " [ erspan_dir { ingress | egress } ]\n"
51 " [ erspan_hwid hwid ]\n"
52 " [ external ]\n"
53 "\n"
54 "Where: ADDR := { IP_ADDRESS | any }\n"
55 " TOS := { NUMBER | inherit }\n"
56 " TTL := { 1..255 | inherit }\n"
57 " KEY := { DOTTED_QUAD | NUMBER }\n"
58 " MARK := { 0x0..0xffffffff }\n"
59 );
60 }
61
62 static void usage(void) __attribute__((noreturn));
63 static void usage(void)
64 {
65 print_usage(stderr);
66 exit(-1);
67 }
68
69 static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
70 struct nlmsghdr *n)
71 {
72 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
73 struct {
74 struct nlmsghdr n;
75 struct ifinfomsg i;
76 } req = {
77 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
78 .n.nlmsg_flags = NLM_F_REQUEST,
79 .n.nlmsg_type = RTM_GETLINK,
80 .i.ifi_family = preferred_family,
81 .i.ifi_index = ifi->ifi_index,
82 };
83 struct nlmsghdr *answer;
84 struct rtattr *tb[IFLA_MAX + 1];
85 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
86 struct rtattr *greinfo[IFLA_GRE_MAX + 1];
87 __u16 iflags = 0;
88 __u16 oflags = 0;
89 __be32 ikey = 0;
90 __be32 okey = 0;
91 unsigned int saddr = 0;
92 unsigned int daddr = 0;
93 unsigned int link = 0;
94 __u8 pmtudisc = 1;
95 __u8 ttl = 0;
96 __u8 tos = 0;
97 int len;
98 __u16 encaptype = 0;
99 __u16 encapflags = 0;
100 __u16 encapsport = 0;
101 __u16 encapdport = 0;
102 __u8 metadata = 0;
103 __u8 ignore_df = 0;
104 __u32 fwmark = 0;
105 __u32 erspan_idx = 0;
106 __u8 erspan_ver = 0;
107 __u8 erspan_dir = 0;
108 __u16 erspan_hwid = 0;
109
110 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
111 if (rtnl_talk(&rth, &req.n, &answer) < 0) {
112 get_failed:
113 fprintf(stderr,
114 "Failed to get existing tunnel info.\n");
115 return -1;
116 }
117
118 len = answer->nlmsg_len;
119 len -= NLMSG_LENGTH(sizeof(*ifi));
120 if (len < 0)
121 goto get_failed;
122
123 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len);
124
125 if (!tb[IFLA_LINKINFO])
126 goto get_failed;
127
128 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
129
130 if (!linkinfo[IFLA_INFO_DATA])
131 goto get_failed;
132
133 parse_rtattr_nested(greinfo, IFLA_GRE_MAX,
134 linkinfo[IFLA_INFO_DATA]);
135
136 if (greinfo[IFLA_GRE_IKEY])
137 ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
138
139 if (greinfo[IFLA_GRE_OKEY])
140 okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
141
142 if (greinfo[IFLA_GRE_IFLAGS])
143 iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
144
145 if (greinfo[IFLA_GRE_OFLAGS])
146 oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
147
148 if (greinfo[IFLA_GRE_LOCAL])
149 saddr = rta_getattr_u32(greinfo[IFLA_GRE_LOCAL]);
150
151 if (greinfo[IFLA_GRE_REMOTE])
152 daddr = rta_getattr_u32(greinfo[IFLA_GRE_REMOTE]);
153
154 if (greinfo[IFLA_GRE_PMTUDISC])
155 pmtudisc = rta_getattr_u8(
156 greinfo[IFLA_GRE_PMTUDISC]);
157
158 if (greinfo[IFLA_GRE_TTL])
159 ttl = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
160
161 if (greinfo[IFLA_GRE_TOS])
162 tos = rta_getattr_u8(greinfo[IFLA_GRE_TOS]);
163
164 if (greinfo[IFLA_GRE_LINK])
165 link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]);
166
167 if (greinfo[IFLA_GRE_ENCAP_TYPE])
168 encaptype = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_TYPE]);
169 if (greinfo[IFLA_GRE_ENCAP_FLAGS])
170 encapflags = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_FLAGS]);
171 if (greinfo[IFLA_GRE_ENCAP_SPORT])
172 encapsport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_SPORT]);
173 if (greinfo[IFLA_GRE_ENCAP_DPORT])
174 encapdport = rta_getattr_u16(greinfo[IFLA_GRE_ENCAP_DPORT]);
175
176 if (greinfo[IFLA_GRE_COLLECT_METADATA])
177 metadata = 1;
178
179 if (greinfo[IFLA_GRE_IGNORE_DF])
180 ignore_df =
181 !!rta_getattr_u8(greinfo[IFLA_GRE_IGNORE_DF]);
182
183 if (greinfo[IFLA_GRE_FWMARK])
184 fwmark = rta_getattr_u32(greinfo[IFLA_GRE_FWMARK]);
185
186 if (greinfo[IFLA_GRE_ERSPAN_INDEX])
187 erspan_idx = rta_getattr_u32(greinfo[IFLA_GRE_ERSPAN_INDEX]);
188
189 if (greinfo[IFLA_GRE_ERSPAN_VER])
190 erspan_ver = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_VER]);
191
192 if (greinfo[IFLA_GRE_ERSPAN_DIR])
193 erspan_dir = rta_getattr_u8(greinfo[IFLA_GRE_ERSPAN_DIR]);
194
195 if (greinfo[IFLA_GRE_ERSPAN_HWID])
196 erspan_hwid = rta_getattr_u16(greinfo[IFLA_GRE_ERSPAN_HWID]);
197
198 free(answer);
199 }
200
201 while (argc > 0) {
202 if (!matches(*argv, "key")) {
203 NEXT_ARG();
204 iflags |= GRE_KEY;
205 oflags |= GRE_KEY;
206 ikey = okey = tnl_parse_key("key", *argv);
207 } else if (!matches(*argv, "ikey")) {
208 NEXT_ARG();
209 iflags |= GRE_KEY;
210 ikey = tnl_parse_key("ikey", *argv);
211 } else if (!matches(*argv, "okey")) {
212 NEXT_ARG();
213 oflags |= GRE_KEY;
214 okey = tnl_parse_key("okey", *argv);
215 } else if (!matches(*argv, "seq")) {
216 iflags |= GRE_SEQ;
217 oflags |= GRE_SEQ;
218 } else if (!matches(*argv, "iseq")) {
219 iflags |= GRE_SEQ;
220 } else if (!matches(*argv, "oseq")) {
221 oflags |= GRE_SEQ;
222 } else if (!matches(*argv, "csum")) {
223 iflags |= GRE_CSUM;
224 oflags |= GRE_CSUM;
225 } else if (!matches(*argv, "icsum")) {
226 iflags |= GRE_CSUM;
227 } else if (!matches(*argv, "ocsum")) {
228 oflags |= GRE_CSUM;
229 } else if (!matches(*argv, "nopmtudisc")) {
230 pmtudisc = 0;
231 } else if (!matches(*argv, "pmtudisc")) {
232 pmtudisc = 1;
233 } else if (!matches(*argv, "remote")) {
234 NEXT_ARG();
235 daddr = get_addr32(*argv);
236 } else if (!matches(*argv, "local")) {
237 NEXT_ARG();
238 saddr = get_addr32(*argv);
239 } else if (!matches(*argv, "dev")) {
240 NEXT_ARG();
241 link = if_nametoindex(*argv);
242 if (link == 0) {
243 fprintf(stderr, "Cannot find device \"%s\"\n",
244 *argv);
245 exit(-1);
246 }
247 } else if (!matches(*argv, "ttl") ||
248 !matches(*argv, "hoplimit")) {
249 unsigned int uval;
250
251 NEXT_ARG();
252 if (strcmp(*argv, "inherit") != 0) {
253 if (get_unsigned(&uval, *argv, 0))
254 invarg("invalid TTL\n", *argv);
255 if (uval > 255)
256 invarg("TTL must be <= 255\n", *argv);
257 ttl = uval;
258 } else
259 ttl = 0;
260 } else if (!matches(*argv, "tos") ||
261 !matches(*argv, "tclass") ||
262 !matches(*argv, "dsfield")) {
263 __u32 uval;
264
265 NEXT_ARG();
266 if (strcmp(*argv, "inherit") != 0) {
267 if (rtnl_dsfield_a2n(&uval, *argv))
268 invarg("bad TOS value", *argv);
269 tos = uval;
270 } else
271 tos = 1;
272 } else if (strcmp(*argv, "noencap") == 0) {
273 encaptype = TUNNEL_ENCAP_NONE;
274 } else if (strcmp(*argv, "encap") == 0) {
275 NEXT_ARG();
276 if (strcmp(*argv, "fou") == 0)
277 encaptype = TUNNEL_ENCAP_FOU;
278 else if (strcmp(*argv, "gue") == 0)
279 encaptype = TUNNEL_ENCAP_GUE;
280 else if (strcmp(*argv, "none") == 0)
281 encaptype = TUNNEL_ENCAP_NONE;
282 else
283 invarg("Invalid encap type.", *argv);
284 } else if (strcmp(*argv, "encap-sport") == 0) {
285 NEXT_ARG();
286 if (strcmp(*argv, "auto") == 0)
287 encapsport = 0;
288 else if (get_u16(&encapsport, *argv, 0))
289 invarg("Invalid source port.", *argv);
290 } else if (strcmp(*argv, "encap-dport") == 0) {
291 NEXT_ARG();
292 if (get_u16(&encapdport, *argv, 0))
293 invarg("Invalid destination port.", *argv);
294 } else if (strcmp(*argv, "encap-csum") == 0) {
295 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
296 } else if (strcmp(*argv, "noencap-csum") == 0) {
297 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
298 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
299 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
300 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
301 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
302 } else if (strcmp(*argv, "encap-remcsum") == 0) {
303 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
304 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
305 encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM;
306 } else if (strcmp(*argv, "external") == 0) {
307 metadata = 1;
308 } else if (strcmp(*argv, "ignore-df") == 0) {
309 ignore_df = 1;
310 } else if (strcmp(*argv, "noignore-df") == 0) {
311 /*
312 *only the lsb is significant, use 2 for presence
313 */
314 ignore_df = 2;
315 } else if (strcmp(*argv, "fwmark") == 0) {
316 NEXT_ARG();
317 if (get_u32(&fwmark, *argv, 0))
318 invarg("invalid fwmark\n", *argv);
319 } else if (strcmp(*argv, "erspan") == 0) {
320 NEXT_ARG();
321 if (get_u32(&erspan_idx, *argv, 0))
322 invarg("invalid erspan index\n", *argv);
323 if (erspan_idx & ~((1<<20) - 1) || erspan_idx == 0)
324 invarg("erspan index must be > 0 and <= 20-bit\n", *argv);
325 } else if (strcmp(*argv, "erspan_ver") == 0) {
326 NEXT_ARG();
327 if (get_u8(&erspan_ver, *argv, 0))
328 invarg("invalid erspan version\n", *argv);
329 if (erspan_ver != 1 && erspan_ver != 2)
330 invarg("erspan version must be 1 or 2\n", *argv);
331 } else if (strcmp(*argv, "erspan_dir") == 0) {
332 NEXT_ARG();
333 if (matches(*argv, "ingress") == 0)
334 erspan_dir = 0;
335 else if (matches(*argv, "egress") == 0)
336 erspan_dir = 1;
337 else
338 invarg("Invalid erspan direction.", *argv);
339 } else if (strcmp(*argv, "erspan_hwid") == 0) {
340 NEXT_ARG();
341 if (get_u16(&erspan_hwid, *argv, 0))
342 invarg("invalid erspan hwid\n", *argv);
343 } else
344 usage();
345 argc--; argv++;
346 }
347
348 if (!ikey && IN_MULTICAST(ntohl(daddr))) {
349 ikey = daddr;
350 iflags |= GRE_KEY;
351 }
352 if (!okey && IN_MULTICAST(ntohl(daddr))) {
353 okey = daddr;
354 oflags |= GRE_KEY;
355 }
356 if (IN_MULTICAST(ntohl(daddr)) && !saddr) {
357 fprintf(stderr, "A broadcast tunnel requires a source address.\n");
358 return -1;
359 }
360
361 if (!metadata) {
362 addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
363 addattr32(n, 1024, IFLA_GRE_OKEY, okey);
364 addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
365 addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
366 addattr_l(n, 1024, IFLA_GRE_LOCAL, &saddr, 4);
367 addattr_l(n, 1024, IFLA_GRE_REMOTE, &daddr, 4);
368 addattr_l(n, 1024, IFLA_GRE_PMTUDISC, &pmtudisc, 1);
369 if (link)
370 addattr32(n, 1024, IFLA_GRE_LINK, link);
371 addattr_l(n, 1024, IFLA_GRE_TTL, &ttl, 1);
372 addattr_l(n, 1024, IFLA_GRE_TOS, &tos, 1);
373 addattr32(n, 1024, IFLA_GRE_FWMARK, fwmark);
374 if (erspan_ver) {
375 addattr8(n, 1024, IFLA_GRE_ERSPAN_VER, erspan_ver);
376 if (erspan_ver == 1 && erspan_idx != 0) {
377 addattr32(n, 1024, IFLA_GRE_ERSPAN_INDEX, erspan_idx);
378 } else if (erspan_ver == 2) {
379 addattr8(n, 1024, IFLA_GRE_ERSPAN_DIR, erspan_dir);
380 addattr16(n, 1024, IFLA_GRE_ERSPAN_HWID, erspan_hwid);
381 }
382 }
383 } else {
384 addattr_l(n, 1024, IFLA_GRE_COLLECT_METADATA, NULL, 0);
385 }
386
387 addattr16(n, 1024, IFLA_GRE_ENCAP_TYPE, encaptype);
388 addattr16(n, 1024, IFLA_GRE_ENCAP_FLAGS, encapflags);
389 addattr16(n, 1024, IFLA_GRE_ENCAP_SPORT, htons(encapsport));
390 addattr16(n, 1024, IFLA_GRE_ENCAP_DPORT, htons(encapdport));
391
392 if (ignore_df)
393 addattr8(n, 1024, IFLA_GRE_IGNORE_DF, ignore_df & 1);
394
395 return 0;
396 }
397
398 static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
399 {
400 char s2[64];
401 const char *local = "any";
402 const char *remote = "any";
403 unsigned int iflags = 0;
404 unsigned int oflags = 0;
405
406 if (tb[IFLA_GRE_REMOTE]) {
407 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
408
409 if (addr)
410 remote = format_host(AF_INET, 4, &addr);
411 }
412
413 print_string(PRINT_ANY, "remote", "remote %s ", remote);
414
415 if (tb[IFLA_GRE_LOCAL]) {
416 unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
417
418 if (addr)
419 local = format_host(AF_INET, 4, &addr);
420 }
421
422 print_string(PRINT_ANY, "local", "local %s ", local);
423
424 if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
425 unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
426 const char *n = if_indextoname(link, s2);
427
428 if (n)
429 print_string(PRINT_ANY, "link", "dev %s ", n);
430 else
431 print_uint(PRINT_ANY, "link_index", "dev %u ", link);
432 }
433
434 if (tb[IFLA_GRE_TTL]) {
435 __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
436
437 if (ttl)
438 print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
439 else
440 print_int(PRINT_JSON, "ttl", NULL, ttl);
441 } else {
442 print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
443 }
444
445 if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) {
446 int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]);
447
448 if (is_json_context()) {
449 SPRINT_BUF(b1);
450
451 snprintf(b1, sizeof(b1), "0x%x", tos);
452 print_string(PRINT_JSON, "tos", NULL, b1);
453 } else {
454 fputs("tos ", f);
455 if (tos == 1)
456 fputs("inherit ", f);
457 else
458 fprintf(f, "0x%x ", tos);
459 }
460 }
461
462 if (tb[IFLA_GRE_PMTUDISC]) {
463 if (!rta_getattr_u8(tb[IFLA_GRE_PMTUDISC]))
464 print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false);
465 else
466 print_bool(PRINT_JSON, "pmtudisc", NULL, true);
467 }
468
469 if (tb[IFLA_GRE_IFLAGS])
470 iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
471
472 if (tb[IFLA_GRE_OFLAGS])
473 oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
474
475 if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
476 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
477 print_string(PRINT_ANY, "ikey", "ikey %s ", s2);
478 }
479
480 if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
481 inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
482 print_string(PRINT_ANY, "okey", "okey %s ", s2);
483 }
484
485 if (iflags & GRE_SEQ)
486 print_bool(PRINT_ANY, "iseq", "iseq ", true);
487 if (oflags & GRE_SEQ)
488 print_bool(PRINT_ANY, "oseq", "oseq ", true);
489 if (iflags & GRE_CSUM)
490 print_bool(PRINT_ANY, "icsum", "icsum ", true);
491 if (oflags & GRE_CSUM)
492 print_bool(PRINT_ANY, "ocsum", "ocsum ", true);
493
494 if (tb[IFLA_GRE_FWMARK]) {
495 __u32 fwmark = rta_getattr_u32(tb[IFLA_GRE_FWMARK]);
496
497 if (fwmark) {
498 snprintf(s2, sizeof(s2), "0x%x", fwmark);
499
500 print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2);
501 }
502 }
503 }
504
505 static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
506 {
507 if (!tb)
508 return;
509
510 if (!tb[IFLA_GRE_COLLECT_METADATA])
511 gre_print_direct_opt(f, tb);
512 else
513 print_bool(PRINT_ANY, "external", "external ", true);
514
515 if (tb[IFLA_GRE_IGNORE_DF] && rta_getattr_u8(tb[IFLA_GRE_IGNORE_DF]))
516 print_bool(PRINT_ANY, "ignore_df", "ignore-df ", true);
517
518 if (tb[IFLA_GRE_ERSPAN_INDEX]) {
519 __u32 erspan_idx = rta_getattr_u32(tb[IFLA_GRE_ERSPAN_INDEX]);
520
521 print_uint(PRINT_ANY, "erspan_index", "erspan_index %u ", erspan_idx);
522 }
523
524 if (tb[IFLA_GRE_ERSPAN_VER]) {
525 __u8 erspan_ver = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_VER]);
526
527 print_uint(PRINT_ANY, "erspan_ver", "erspan_ver %u ", erspan_ver);
528 }
529
530 if (tb[IFLA_GRE_ERSPAN_DIR]) {
531 __u8 erspan_dir = rta_getattr_u8(tb[IFLA_GRE_ERSPAN_DIR]);
532
533 if (erspan_dir == 0)
534 print_string(PRINT_ANY, "erspan_dir",
535 "erspan_dir ingress ", NULL);
536 else
537 print_string(PRINT_ANY, "erspan_dir",
538 "erspan_dir egress ", NULL);
539 }
540
541 if (tb[IFLA_GRE_ERSPAN_HWID]) {
542 __u16 erspan_hwid = rta_getattr_u16(tb[IFLA_GRE_ERSPAN_HWID]);
543
544 print_hex(PRINT_ANY, "erspan_hwid", "erspan_hwid 0x%x ", erspan_hwid);
545 }
546
547 if (tb[IFLA_GRE_ENCAP_TYPE] &&
548 rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
549 __u16 type = rta_getattr_u16(tb[IFLA_GRE_ENCAP_TYPE]);
550 __u16 flags = rta_getattr_u16(tb[IFLA_GRE_ENCAP_FLAGS]);
551 __u16 sport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_SPORT]);
552 __u16 dport = rta_getattr_u16(tb[IFLA_GRE_ENCAP_DPORT]);
553
554
555 open_json_object("encap");
556 print_string(PRINT_FP, NULL, "encap ", NULL);
557
558 switch (type) {
559 case TUNNEL_ENCAP_FOU:
560 print_string(PRINT_ANY, "type", "%s ", "fou");
561 break;
562 case TUNNEL_ENCAP_GUE:
563 print_string(PRINT_ANY, "type", "%s ", "gue");
564 break;
565 default:
566 print_null(PRINT_ANY, "type", "%s ", "unknown");
567 break;
568 }
569
570 if (is_json_context()) {
571 print_uint(PRINT_JSON,
572 "sport",
573 NULL,
574 sport ? ntohs(sport) : 0);
575 print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
576
577 print_bool(PRINT_JSON,
578 "csum",
579 NULL,
580 flags & TUNNEL_ENCAP_FLAG_CSUM);
581
582 print_bool(PRINT_JSON,
583 "csum6",
584 NULL,
585 flags & TUNNEL_ENCAP_FLAG_CSUM6);
586
587 print_bool(PRINT_JSON,
588 "remcsum",
589 NULL,
590 flags & TUNNEL_ENCAP_FLAG_REMCSUM);
591
592 close_json_object();
593 } else {
594 if (sport == 0)
595 fputs("encap-sport auto ", f);
596 else
597 fprintf(f, "encap-sport %u", ntohs(sport));
598
599 fprintf(f, "encap-dport %u ", ntohs(dport));
600
601 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
602 fputs("encap-csum ", f);
603 else
604 fputs("noencap-csum ", f);
605
606 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
607 fputs("encap-csum6 ", f);
608 else
609 fputs("noencap-csum6 ", f);
610
611 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
612 fputs("encap-remcsum ", f);
613 else
614 fputs("noencap-remcsum ", f);
615 }
616 }
617 }
618
619 static void gre_print_help(struct link_util *lu, int argc, char **argv,
620 FILE *f)
621 {
622 print_usage(f);
623 }
624
625 struct link_util gre_link_util = {
626 .id = "gre",
627 .maxattr = IFLA_GRE_MAX,
628 .parse_opt = gre_parse_opt,
629 .print_opt = gre_print_opt,
630 .print_help = gre_print_help,
631 };
632
633 struct link_util gretap_link_util = {
634 .id = "gretap",
635 .maxattr = IFLA_GRE_MAX,
636 .parse_opt = gre_parse_opt,
637 .print_opt = gre_print_opt,
638 .print_help = gre_print_help,
639 };
640
641 struct link_util erspan_link_util = {
642 .id = "erspan",
643 .maxattr = IFLA_GRE_MAX,
644 .parse_opt = gre_parse_opt,
645 .print_opt = gre_print_opt,
646 .print_help = gre_print_help,
647 };