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