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