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