]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/link_iptnl.c
ip/tunnel: No need to free answer after rtnl_talk() on error
[mirror_iproute2.git] / ip / link_iptnl.c
CommitLineData
1ce2de97
ND
1/*
2 * link_iptnl.c ipip and sit 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: Nicolas Dichtel <nicolas.dichtel@6wind.com>
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
f005b700 19#include <linux/in.h>
1ce2de97
ND
20#include <linux/ip.h>
21#include <linux/if_tunnel.h>
22#include "rt_names.h"
23#include "utils.h"
24#include "ip_common.h"
25#include "tunnel.h"
26
561e650e 27static void print_usage(FILE *f, int sit)
1ce2de97 28{
8b471354
PS
29 const char *type = sit ? "sit " : "ipip";
30
31 fprintf(f,
32 "Usage: ... %s [ remote ADDR ]\n"
33 " [ local ADDR ]\n"
34 " [ ttl TTL ]\n"
35 " [ tos TOS ]\n"
36 " [ [no]pmtudisc ]\n"
37 " [ dev PHYS_DEV ]\n"
38 " [ 6rd-prefix ADDR ]\n"
39 " [ 6rd-relay_prefix ADDR ]\n"
40 " [ 6rd-reset ]\n"
41 " [ noencap ]\n"
42 " [ encap { fou | gue | none } ]\n"
43 " [ encap-sport PORT ]\n"
44 " [ encap-dport PORT ]\n"
45 " [ [no]encap-csum ]\n"
46 " [ [no]encap-csum6 ]\n"
47 " [ [no]encap-remcsum ]\n",
48 type
49 );
77620be8 50 if (sit) {
f005b700
KJ
51 fprintf(f, " [ mode { ip6ip | ipip | mplsip | any } ]\n");
52 fprintf(f, " [ isatap ]\n");
288c28bc
KJ
53 } else {
54 fprintf(f, " [ mode { ipip | mplsip | any } ]\n");
77620be8 55 }
8b471354 56 fprintf(f, " [ external ]\n");
ad4b1425 57 fprintf(f, " [ fwmark MARK ]\n");
561e650e 58 fprintf(f, "\n");
8b471354 59 fprintf(f, "Where: ADDR := { IP_ADDRESS | any }\n");
561e650e 60 fprintf(f, " TOS := { NUMBER | inherit }\n");
61 fprintf(f, " TTL := { 1..255 | inherit }\n");
ad4b1425 62 fprintf(f, " MARK := { 0x0..0xffffffff }\n");
561e650e 63}
64
65static void usage(int sit) __attribute__((noreturn));
66static void usage(int sit)
67{
68 print_usage(stderr, sit);
1ce2de97
ND
69 exit(-1);
70}
71
72static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
73 struct nlmsghdr *n)
74{
d17b136f 75 struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
1ce2de97
ND
76 struct {
77 struct nlmsghdr n;
78 struct ifinfomsg i;
d17b136f
PS
79 } req = {
80 .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)),
81 .n.nlmsg_flags = NLM_F_REQUEST,
82 .n.nlmsg_type = RTM_GETLINK,
83 .i.ifi_family = preferred_family,
84 .i.ifi_index = ifi->ifi_index,
85 };
08ede25f 86 struct nlmsghdr *answer;
1ce2de97
ND
87 struct rtattr *tb[IFLA_MAX + 1];
88 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
89 struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
90 int len;
91 __u32 link = 0;
92 __u32 laddr = 0;
93 __u32 raddr = 0;
94 __u8 ttl = 0;
95 __u8 tos = 0;
96 __u8 pmtudisc = 1;
97 __u16 iflags = 0;
77620be8 98 __u8 proto = 0;
d17b136f 99 struct in6_addr ip6rdprefix = {};
1ce2de97
ND
100 __u16 ip6rdprefixlen = 0;
101 __u32 ip6rdrelayprefix = 0;
102 __u16 ip6rdrelayprefixlen = 0;
c1159152
TH
103 __u16 encaptype = 0;
104 __u16 encapflags = 0;
105 __u16 encapsport = 0;
106 __u16 encapdport = 0;
4bfe6825 107 __u8 metadata = 0;
ad4b1425 108 __u32 fwmark = 0;
1ce2de97 109
1ce2de97 110 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
86bf43c7 111 if (rtnl_talk(&rth, &req.n, &answer) < 0) {
1ce2de97
ND
112get_failed:
113 fprintf(stderr,
114 "Failed to get existing tunnel info.\n");
115 return -1;
116 }
117
86bf43c7 118 len = answer->nlmsg_len;
1ce2de97
ND
119 len -= NLMSG_LENGTH(sizeof(*ifi));
120 if (len < 0)
121 goto get_failed;
122
86bf43c7 123 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), len);
1ce2de97
ND
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(iptuninfo, IFLA_IPTUN_MAX,
134 linkinfo[IFLA_INFO_DATA]);
135
136 if (iptuninfo[IFLA_IPTUN_LOCAL])
137 laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]);
138
139 if (iptuninfo[IFLA_IPTUN_REMOTE])
140 raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]);
141
142 if (iptuninfo[IFLA_IPTUN_TTL])
143 ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
144
145 if (iptuninfo[IFLA_IPTUN_TOS])
146 tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
147
148 if (iptuninfo[IFLA_IPTUN_PMTUDISC])
149 pmtudisc =
150 rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]);
151
152 if (iptuninfo[IFLA_IPTUN_FLAGS])
153 iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]);
154
155 if (iptuninfo[IFLA_IPTUN_LINK])
156 link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
157
77620be8
ND
158 if (iptuninfo[IFLA_IPTUN_PROTO])
159 proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
160
c1159152
TH
161 if (iptuninfo[IFLA_IPTUN_ENCAP_TYPE])
162 encaptype = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_TYPE]);
163 if (iptuninfo[IFLA_IPTUN_ENCAP_FLAGS])
164 encapflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]);
165 if (iptuninfo[IFLA_IPTUN_ENCAP_SPORT])
166 encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]);
167 if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT])
168 encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]);
1ce2de97
ND
169 if (iptuninfo[IFLA_IPTUN_6RD_PREFIX])
170 memcpy(&ip6rdprefix,
171 RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]),
172 sizeof(laddr));
173
174 if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN])
175 ip6rdprefixlen =
176 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]);
177
178 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX])
179 ip6rdrelayprefix =
180 rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]);
181
182 if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN])
183 ip6rdrelayprefixlen =
184 rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
4bfe6825
AS
185 if (iptuninfo[IFLA_IPTUN_COLLECT_METADATA])
186 metadata = 1;
ad4b1425
CG
187
188 if (iptuninfo[IFLA_IPTUN_FWMARK])
189 fwmark = rta_getattr_u32(iptuninfo[IFLA_IPTUN_FWMARK]);
190
86bf43c7 191 free(answer);
1ce2de97
ND
192 }
193
194 while (argc > 0) {
195 if (strcmp(*argv, "remote") == 0) {
196 NEXT_ARG();
57daab1e 197 raddr = get_addr32(*argv);
1ce2de97
ND
198 } else if (strcmp(*argv, "local") == 0) {
199 NEXT_ARG();
57daab1e 200 laddr = get_addr32(*argv);
1ce2de97
ND
201 } else if (matches(*argv, "dev") == 0) {
202 NEXT_ARG();
203 link = if_nametoindex(*argv);
204 if (link == 0)
205 invarg("\"dev\" is invalid", *argv);
206 } else if (strcmp(*argv, "ttl") == 0 ||
207 strcmp(*argv, "hoplimit") == 0) {
208 NEXT_ARG();
209 if (strcmp(*argv, "inherit") != 0) {
210 if (get_u8(&ttl, *argv, 0))
211 invarg("invalid TTL\n", *argv);
212 } else
213 ttl = 0;
214 } else if (strcmp(*argv, "tos") == 0 ||
215 strcmp(*argv, "tclass") == 0 ||
216 matches(*argv, "dsfield") == 0) {
217 __u32 uval;
56f5daac 218
1ce2de97
ND
219 NEXT_ARG();
220 if (strcmp(*argv, "inherit") != 0) {
221 if (rtnl_dsfield_a2n(&uval, *argv))
222 invarg("bad TOS value", *argv);
223 tos = uval;
224 } else
225 tos = 1;
226 } else if (strcmp(*argv, "nopmtudisc") == 0) {
227 pmtudisc = 0;
228 } else if (strcmp(*argv, "pmtudisc") == 0) {
229 pmtudisc = 1;
230 } else if (strcmp(lu->id, "sit") == 0 &&
231 strcmp(*argv, "isatap") == 0) {
232 iflags |= SIT_ISATAP;
77620be8
ND
233 } else if (strcmp(lu->id, "sit") == 0 &&
234 strcmp(*argv, "mode") == 0) {
235 NEXT_ARG();
236 if (strcmp(*argv, "ipv6/ipv4") == 0 ||
237 strcmp(*argv, "ip6ip") == 0)
238 proto = IPPROTO_IPV6;
239 else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
240 strcmp(*argv, "ipip") == 0 ||
241 strcmp(*argv, "ip4ip4") == 0)
242 proto = IPPROTO_IPIP;
f005b700
KJ
243 else if (strcmp(*argv, "mpls/ipv4") == 0 ||
244 strcmp(*argv, "mplsip") == 0)
245 proto = IPPROTO_MPLS;
77620be8
ND
246 else if (strcmp(*argv, "any/ipv4") == 0 ||
247 strcmp(*argv, "any") == 0)
248 proto = 0;
249 else
250 invarg("Cannot guess tunnel mode.", *argv);
288c28bc
KJ
251 } else if (strcmp(lu->id, "ipip") == 0 &&
252 strcmp(*argv, "mode") == 0) {
253 NEXT_ARG();
254 if (strcmp(*argv, "ipv4/ipv4") == 0 ||
255 strcmp(*argv, "ipip") == 0 ||
256 strcmp(*argv, "ip4ip4") == 0)
257 proto = IPPROTO_IPIP;
258 else if (strcmp(*argv, "mpls/ipv4") == 0 ||
259 strcmp(*argv, "mplsip") == 0)
260 proto = IPPROTO_MPLS;
261 else if (strcmp(*argv, "any/ipv4") == 0 ||
262 strcmp(*argv, "any") == 0)
263 proto = 0;
264 else
265 invarg("Cannot guess tunnel mode.", *argv);
c1159152
TH
266 } else if (strcmp(*argv, "noencap") == 0) {
267 encaptype = TUNNEL_ENCAP_NONE;
268 } else if (strcmp(*argv, "encap") == 0) {
269 NEXT_ARG();
270 if (strcmp(*argv, "fou") == 0)
271 encaptype = TUNNEL_ENCAP_FOU;
272 else if (strcmp(*argv, "gue") == 0)
273 encaptype = TUNNEL_ENCAP_GUE;
274 else if (strcmp(*argv, "none") == 0)
275 encaptype = TUNNEL_ENCAP_NONE;
276 else
277 invarg("Invalid encap type.", *argv);
278 } else if (strcmp(*argv, "encap-sport") == 0) {
279 NEXT_ARG();
280 if (strcmp(*argv, "auto") == 0)
281 encapsport = 0;
282 else if (get_u16(&encapsport, *argv, 0))
283 invarg("Invalid source port.", *argv);
284 } else if (strcmp(*argv, "encap-dport") == 0) {
285 NEXT_ARG();
286 if (get_u16(&encapdport, *argv, 0))
287 invarg("Invalid destination port.", *argv);
288 } else if (strcmp(*argv, "encap-csum") == 0) {
289 encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
290 } else if (strcmp(*argv, "noencap-csum") == 0) {
291 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
292 } else if (strcmp(*argv, "encap-udp6-csum") == 0) {
293 encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
294 } else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
295 encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
858dbb20
TH
296 } else if (strcmp(*argv, "encap-remcsum") == 0) {
297 encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
298 } else if (strcmp(*argv, "noencap-remcsum") == 0) {
299 encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM;
4bfe6825
AS
300 } else if (strcmp(*argv, "external") == 0) {
301 metadata = 1;
1ce2de97
ND
302 } else if (strcmp(*argv, "6rd-prefix") == 0) {
303 inet_prefix prefix;
56f5daac 304
1ce2de97
ND
305 NEXT_ARG();
306 if (get_prefix(&prefix, *argv, AF_INET6))
307 invarg("invalid 6rd_prefix\n", *argv);
308 memcpy(&ip6rdprefix, prefix.data, 16);
309 ip6rdprefixlen = prefix.bitlen;
310 } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
311 inet_prefix prefix;
56f5daac 312
1ce2de97
ND
313 NEXT_ARG();
314 if (get_prefix(&prefix, *argv, AF_INET))
315 invarg("invalid 6rd-relay_prefix\n", *argv);
316 memcpy(&ip6rdrelayprefix, prefix.data, 4);
317 ip6rdrelayprefixlen = prefix.bitlen;
318 } else if (strcmp(*argv, "6rd-reset") == 0) {
319 inet_prefix prefix;
56f5daac 320
1ce2de97
ND
321 get_prefix(&prefix, "2002::", AF_INET6);
322 memcpy(&ip6rdprefix, prefix.data, 16);
323 ip6rdprefixlen = 16;
324 ip6rdrelayprefix = 0;
325 ip6rdrelayprefixlen = 0;
ad4b1425
CG
326 } else if (strcmp(*argv, "fwmark") == 0) {
327 NEXT_ARG();
328 if (get_u32(&fwmark, *argv, 0))
329 invarg("invalid fwmark\n", *argv);
1ce2de97
ND
330 } else
331 usage(strcmp(lu->id, "sit") == 0);
332 argc--, argv++;
333 }
334
335 if (ttl && pmtudisc == 0) {
30d07e9e 336 fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
1ce2de97
ND
337 exit(-1);
338 }
339
4bfe6825
AS
340 if (metadata) {
341 addattr_l(n, 1024, IFLA_IPTUN_COLLECT_METADATA, NULL, 0);
342 return 0;
343 }
344
1ce2de97
ND
345 addattr32(n, 1024, IFLA_IPTUN_LINK, link);
346 addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr);
347 addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr);
348 addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
349 addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
350 addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc);
ad4b1425 351 addattr32(n, 1024, IFLA_IPTUN_FWMARK, fwmark);
c1159152
TH
352
353 addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
354 addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags);
355 addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
356 addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
357
288c28bc
KJ
358 if (strcmp(lu->id, "ipip") == 0 || strcmp(lu->id, "sit") == 0)
359 addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
360
1ce2de97
ND
361 if (strcmp(lu->id, "sit") == 0) {
362 addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags);
363 if (ip6rdprefixlen) {
364 addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX,
365 &ip6rdprefix, sizeof(ip6rdprefix));
366 addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN,
367 ip6rdprefixlen);
368 addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX,
369 ip6rdrelayprefix);
370 addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
371 ip6rdrelayprefixlen);
372 }
373 }
374
375 return 0;
376}
377
378static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
379{
380 char s1[1024];
381 char s2[64];
382 const char *local = "any";
383 const char *remote = "any";
9f1370c0 384 __u16 prefixlen, type;
1ce2de97
ND
385
386 if (!tb)
387 return;
388
7b178324
PD
389 if (tb[IFLA_IPTUN_COLLECT_METADATA])
390 print_bool(PRINT_ANY, "external", "external ", true);
391
1ce2de97 392 if (tb[IFLA_IPTUN_REMOTE]) {
56f5daac 393 unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
1ce2de97
ND
394
395 if (addr)
a418e451 396 remote = format_host(AF_INET, 4, &addr);
1ce2de97
ND
397 }
398
2539a407 399 print_string(PRINT_ANY, "remote", "remote %s ", remote);
1ce2de97
ND
400
401 if (tb[IFLA_IPTUN_LOCAL]) {
56f5daac 402 unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
1ce2de97
ND
403
404 if (addr)
a418e451 405 local = format_host(AF_INET, 4, &addr);
1ce2de97
ND
406 }
407
2539a407 408 print_string(PRINT_ANY, "local", "local %s ", local);
1ce2de97
ND
409
410 if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
56f5daac 411 unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
1ce2de97
ND
412 const char *n = if_indextoname(link, s2);
413
414 if (n)
2539a407 415 print_string(PRINT_ANY, "link", "dev %s ", n);
1ce2de97 416 else
2539a407 417 print_int(PRINT_ANY, "link_index", "dev %u ", link);
1ce2de97
ND
418 }
419
2539a407
JF
420 if (tb[IFLA_IPTUN_TTL]) {
421 __u8 ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]);
422
423 if (ttl)
424 print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
425 else
426 print_int(PRINT_JSON, "ttl", NULL, ttl);
427 } else {
428 print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
429 }
1ce2de97 430
2539a407 431 if (tb[IFLA_IPTUN_TOS]) {
1ce2de97
ND
432 int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);
433
2539a407
JF
434 if (tos) {
435 if (is_json_context()) {
436 print_0xhex(PRINT_JSON, "tos", "%#x", tos);
437 } else {
438 fputs("tos ", f);
439 if (tos == 1)
440 fputs("inherit ", f);
441 else
442 fprintf(f, "0x%x ", tos);
443 }
444 }
1ce2de97
ND
445 }
446
447 if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC]))
2539a407 448 print_bool(PRINT_ANY, "pmtudisc", "pmtudisc ", true);
1ce2de97 449 else
2539a407 450 print_bool(PRINT_ANY, "pmtudisc", "nopmtudisc ", false);
1ce2de97
ND
451
452 if (tb[IFLA_IPTUN_FLAGS]) {
195f0f62 453 __u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]);
1ce2de97 454
195f0f62 455 if (iflags & SIT_ISATAP)
2539a407 456 print_bool(PRINT_ANY, "isatap", "isatap ", true);
1ce2de97
ND
457 }
458
459 if (tb[IFLA_IPTUN_6RD_PREFIXLEN] &&
9f1370c0 460 (prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]))) {
1ce2de97
ND
461 __u16 relayprefixlen =
462 rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
463 __u32 relayprefix =
464 rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
465
2539a407
JF
466 const char *prefix = inet_ntop(AF_INET6,
467 RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]),
468 s1, sizeof(s1));
469
470 if (is_json_context()) {
471 print_string(PRINT_JSON, "prefix", NULL, prefix);
472 print_int(PRINT_JSON, "prefixlen", NULL, prefixlen);
473 if (relayprefix) {
474 print_string(PRINT_JSON,
475 "relay_prefix",
476 NULL,
477 format_host(AF_INET,
478 4,
479 &relayprefix));
480 print_int(PRINT_JSON,
481 "relay_prefixlen",
482 NULL,
483 relayprefixlen);
484 }
485 } else {
486 printf("6rd-prefix %s/%u ", prefix, prefixlen);
487 if (relayprefix) {
488 printf("6rd-relay_prefix %s/%u ",
489 format_host(AF_INET, 4, &relayprefix),
490 relayprefixlen);
491 }
1ce2de97
ND
492 }
493 }
c1159152
TH
494
495 if (tb[IFLA_IPTUN_ENCAP_TYPE] &&
9f1370c0 496 (type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) {
c1159152
TH
497 __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]);
498 __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]);
499 __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]);
500
2539a407 501 print_string(PRINT_FP, NULL, "encap ", NULL);
c1159152
TH
502 switch (type) {
503 case TUNNEL_ENCAP_FOU:
2539a407 504 print_string(PRINT_ANY, "type", "%s ", "fou");
c1159152
TH
505 break;
506 case TUNNEL_ENCAP_GUE:
2539a407 507 print_string(PRINT_ANY, "type", "%s ", "gue");
c1159152
TH
508 break;
509 default:
2539a407 510 print_null(PRINT_ANY, "type", "unknown ", NULL);
c1159152
TH
511 break;
512 }
513
2539a407
JF
514 if (is_json_context()) {
515 print_uint(PRINT_JSON,
516 "sport",
517 NULL,
518 sport ? ntohs(sport) : 0);
519 print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
520 print_bool(PRINT_JSON,
521 "csum",
522 NULL,
523 flags & TUNNEL_ENCAP_FLAG_CSUM);
524 print_bool(PRINT_JSON,
525 "csum6",
526 NULL,
527 flags & TUNNEL_ENCAP_FLAG_CSUM6);
528 print_bool(PRINT_JSON,
529 "remcsum",
530 NULL,
531 flags & TUNNEL_ENCAP_FLAG_REMCSUM);
532 close_json_object();
533 } else {
534 if (sport == 0)
535 fputs("encap-sport auto ", f);
536 else
537 fprintf(f, "encap-sport %u", ntohs(sport));
c1159152 538
2539a407 539 fprintf(f, "encap-dport %u ", ntohs(dport));
c1159152 540
2539a407
JF
541 if (flags & TUNNEL_ENCAP_FLAG_CSUM)
542 fputs("encap-csum ", f);
543 else
544 fputs("noencap-csum ", f);
c1159152 545
2539a407
JF
546 if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
547 fputs("encap-csum6 ", f);
548 else
549 fputs("noencap-csum6 ", f);
858dbb20 550
2539a407
JF
551 if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
552 fputs("encap-remcsum ", f);
553 else
554 fputs("noencap-remcsum ", f);
555 }
c1159152 556 }
ad4b1425 557
2539a407
JF
558 if (tb[IFLA_IPTUN_FWMARK]) {
559 __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]);
560
561 if (fwmark) {
562 snprintf(s2, sizeof(s2), "0x%x", fwmark);
563
564 print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2);
565 }
566 }
1ce2de97
ND
567}
568
561e650e 569static void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
570 FILE *f)
571{
572 print_usage(f, strcmp(lu->id, "sit") == 0);
573}
574
1ce2de97
ND
575struct link_util ipip_link_util = {
576 .id = "ipip",
577 .maxattr = IFLA_IPTUN_MAX,
578 .parse_opt = iptunnel_parse_opt,
579 .print_opt = iptunnel_print_opt,
561e650e 580 .print_help = iptunnel_print_help,
1ce2de97
ND
581};
582
583struct link_util sit_link_util = {
584 .id = "sit",
585 .maxattr = IFLA_IPTUN_MAX,
586 .parse_opt = iptunnel_parse_opt,
587 .print_opt = iptunnel_print_opt,
561e650e 588 .print_help = iptunnel_print_help,
1ce2de97 589};