11ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel/*
21ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel * link_iptnl.c	ipip and sit driver module
31ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *
41ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *		This program is free software; you can redistribute it and/or
51ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *		modify it under the terms of the GNU General Public License
61ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *		as published by the Free Software Foundation; either version
71ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *		2 of the License, or (at your option) any later version.
81ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *
91ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel * Authors:	Nicolas Dichtel <nicolas.dichtel@6wind.com>
101ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel *
111ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel */
121ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
131ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <string.h>
141ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <net/if.h>
151ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <sys/types.h>
161ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <sys/socket.h>
171ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <arpa/inet.h>
181ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
191ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <linux/ip.h>
201ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include <linux/if_tunnel.h>
211ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include "rt_names.h"
221ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include "utils.h"
231ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include "ip_common.h"
241ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel#include "tunnel.h"
251ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
26561e650eff679296d3f4c12657721ae769cbc187vadimkstatic void print_usage(FILE *f, int sit)
271ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel{
28561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n");
29561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "          type { ipip | sit } [ remote ADDR ] [ local ADDR ]\n");
30561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "          [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]\n");
31561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "          [ 6rd-prefix ADDR ] [ 6rd-relay_prefix ADDR ] [ 6rd-reset ]\n");
32c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	fprintf(f, "          [ noencap ] [ encap { fou | gue | none } ]\n");
33c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	fprintf(f, "          [ encap-sport PORT ] [ encap-dport PORT ]\n");
34858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert	fprintf(f, "          [ [no]encap-csum ] [ [no]encap-csum6 ] [ [no]encap-remcsum ]\n");
3577620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel	if (sit) {
36561e650eff679296d3f4c12657721ae769cbc187vadimk		fprintf(f, "          [ mode { ip6ip | ipip | any } ]\n");
37561e650eff679296d3f4c12657721ae769cbc187vadimk		fprintf(f, "          [ isatap ]\n");
3877620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel	}
39561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "\n");
40561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "Where: NAME := STRING\n");
41561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "       ADDR := { IP_ADDRESS | any }\n");
42561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "       TOS  := { NUMBER | inherit }\n");
43561e650eff679296d3f4c12657721ae769cbc187vadimk	fprintf(f, "       TTL  := { 1..255 | inherit }\n");
44561e650eff679296d3f4c12657721ae769cbc187vadimk}
45561e650eff679296d3f4c12657721ae769cbc187vadimk
46561e650eff679296d3f4c12657721ae769cbc187vadimkstatic void usage(int sit) __attribute__((noreturn));
47561e650eff679296d3f4c12657721ae769cbc187vadimkstatic void usage(int sit)
48561e650eff679296d3f4c12657721ae769cbc187vadimk{
49561e650eff679296d3f4c12657721ae769cbc187vadimk	print_usage(stderr, sit);
501ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	exit(-1);
511ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel}
521ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
531ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtelstatic int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
541ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			      struct nlmsghdr *n)
551ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel{
561ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct {
571ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		struct nlmsghdr n;
581ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		struct ifinfomsg i;
591ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		char buf[2048];
601ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	} req;
611ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
621ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct rtattr *tb[IFLA_MAX + 1];
631ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
641ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
651ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	int len;
661ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u32 link = 0;
671ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u32 laddr = 0;
681ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u32 raddr = 0;
691ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u8 ttl = 0;
701ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u8 tos = 0;
711ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u8 pmtudisc = 1;
721ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u16 iflags = 0;
7377620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel	__u8 proto = 0;
741ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	struct in6_addr ip6rdprefix;
751ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u16 ip6rdprefixlen = 0;
761ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u32 ip6rdrelayprefix = 0;
771ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	__u16 ip6rdrelayprefixlen = 0;
78c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	__u16 encaptype = 0;
79c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	__u16 encapflags = 0;
80c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	__u16 encapsport = 0;
81c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	__u16 encapdport = 0;
821ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
831ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	memset(&ip6rdprefix, 0, sizeof(ip6rdprefix));
841ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
851ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (!(n->nlmsg_flags & NLM_F_CREATE)) {
861ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		memset(&req, 0, sizeof(req));
871ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
881ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
891ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		req.n.nlmsg_flags = NLM_F_REQUEST;
901ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		req.n.nlmsg_type = RTM_GETLINK;
911ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		req.i.ifi_family = preferred_family;
921ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		req.i.ifi_index = ifi->ifi_index;
931ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
94c079e121a73af5eb49e003b13607e8a690331df6Stephen Hemminger		if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) {
951ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtelget_failed:
961ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			fprintf(stderr,
971ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				"Failed to get existing tunnel info.\n");
981ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			return -1;
991ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		}
1001ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1011ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		len = req.n.nlmsg_len;
1021ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		len -= NLMSG_LENGTH(sizeof(*ifi));
1031ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (len < 0)
1041ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			goto get_failed;
1051ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1061ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
1071ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1081ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (!tb[IFLA_LINKINFO])
1091ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			goto get_failed;
1101ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1111ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1121ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1131ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (!linkinfo[IFLA_INFO_DATA])
1141ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			goto get_failed;
1151ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1161ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
1171ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				    linkinfo[IFLA_INFO_DATA]);
1181ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1191ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_LOCAL])
1201ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]);
1211ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1221ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_REMOTE])
1231ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]);
1241ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1251ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_TTL])
1261ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
1271ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1281ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_TOS])
1291ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
1301ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1311ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_PMTUDISC])
1321ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			pmtudisc =
1331ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]);
1341ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1351ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_FLAGS])
1361ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]);
1371ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1381ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_LINK])
1391ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			link = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LINK]);
1401ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
14177620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_PROTO])
14277620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			proto = rta_getattr_u8(iptuninfo[IFLA_IPTUN_PROTO]);
14377620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel
144c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (iptuninfo[IFLA_IPTUN_ENCAP_TYPE])
145c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encaptype = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_TYPE]);
146c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (iptuninfo[IFLA_IPTUN_ENCAP_FLAGS])
147c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_FLAGS]);
148c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (iptuninfo[IFLA_IPTUN_ENCAP_SPORT])
149c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]);
150c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT])
151c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]);
1521ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_6RD_PREFIX])
1531ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			memcpy(&ip6rdprefix,
1541ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			       RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]),
1551ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			       sizeof(laddr));
1561ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1571ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN])
1581ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdprefixlen =
1591ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]);
1601ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1611ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX])
1621ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdrelayprefix =
1631ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]);
1641ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1651ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN])
1661ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdrelayprefixlen =
1671ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
1681ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
1691ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
1701ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	while (argc > 0) {
1711ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (strcmp(*argv, "remote") == 0) {
1721ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
1731ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (strcmp(*argv, "any"))
1741ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				raddr = get_addr32(*argv);
1751ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			else
1761ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				raddr = 0;
1771ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "local") == 0) {
1781ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
1791ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (strcmp(*argv, "any"))
1801ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				laddr = get_addr32(*argv);
1811ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			else
1821ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				laddr = 0;
1831ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (matches(*argv, "dev") == 0) {
1841ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
1851ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			link = if_nametoindex(*argv);
1861ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (link == 0)
1871ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				invarg("\"dev\" is invalid", *argv);
1881ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "ttl") == 0 ||
1891ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			   strcmp(*argv, "hoplimit") == 0) {
1901ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
1911ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (strcmp(*argv, "inherit") != 0) {
1921ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				if (get_u8(&ttl, *argv, 0))
1931ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel					invarg("invalid TTL\n", *argv);
1941ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			} else
1951ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				ttl = 0;
1961ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "tos") == 0 ||
1971ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			   strcmp(*argv, "tclass") == 0 ||
1981ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			   matches(*argv, "dsfield") == 0) {
1991ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			__u32 uval;
2001ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
2011ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (strcmp(*argv, "inherit") != 0) {
2021ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				if (rtnl_dsfield_a2n(&uval, *argv))
2031ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel					invarg("bad TOS value", *argv);
2041ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				tos = uval;
2051ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			} else
2061ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				tos = 1;
2071ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "nopmtudisc") == 0) {
2081ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			pmtudisc = 0;
2091ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "pmtudisc") == 0) {
2101ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			pmtudisc = 1;
2111ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(lu->id, "sit") == 0 &&
2121ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			   strcmp(*argv, "isatap") == 0) {
2131ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			iflags |= SIT_ISATAP;
21477620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel		} else if (strcmp(lu->id, "sit") == 0 &&
21577620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			   strcmp(*argv, "mode") == 0) {
21677620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			NEXT_ARG();
21777620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			if (strcmp(*argv, "ipv6/ipv4") == 0 ||
21877620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			    strcmp(*argv, "ip6ip") == 0)
21977620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				proto = IPPROTO_IPV6;
22077620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
22177620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				 strcmp(*argv, "ipip") == 0 ||
22277620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				 strcmp(*argv, "ip4ip4") == 0)
22377620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				proto = IPPROTO_IPIP;
22477620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			else if (strcmp(*argv, "any/ipv4") == 0 ||
22577620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				 strcmp(*argv, "any") == 0)
22677620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				proto = 0;
22777620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel			else
22877620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel				invarg("Cannot guess tunnel mode.", *argv);
229c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "noencap") == 0) {
230c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encaptype = TUNNEL_ENCAP_NONE;
231c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "encap") == 0) {
232c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			NEXT_ARG();
233c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			if (strcmp(*argv, "fou") == 0)
234c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				encaptype = TUNNEL_ENCAP_FOU;
235c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			else if (strcmp(*argv, "gue") == 0)
236c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				encaptype = TUNNEL_ENCAP_GUE;
237c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			else if (strcmp(*argv, "none") == 0)
238c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				encaptype = TUNNEL_ENCAP_NONE;
239c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			else
240c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				invarg("Invalid encap type.", *argv);
241c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "encap-sport") == 0) {
242c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			NEXT_ARG();
243c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			if (strcmp(*argv, "auto") == 0)
244c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				encapsport = 0;
245c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			else if (get_u16(&encapsport, *argv, 0))
246c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				invarg("Invalid source port.", *argv);
247c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "encap-dport") == 0) {
248c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			NEXT_ARG();
249c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			if (get_u16(&encapdport, *argv, 0))
250c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert				invarg("Invalid destination port.", *argv);
251c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "encap-csum") == 0) {
252c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapflags |= TUNNEL_ENCAP_FLAG_CSUM;
253c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "noencap-csum") == 0) {
254c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM;
255c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "encap-udp6-csum") == 0) {
256c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapflags |= TUNNEL_ENCAP_FLAG_CSUM6;
257c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		} else if (strcmp(*argv, "noencap-udp6-csum") == 0) {
258c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			encapflags &= ~TUNNEL_ENCAP_FLAG_CSUM6;
259858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert		} else if (strcmp(*argv, "encap-remcsum") == 0) {
260858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert			encapflags |= TUNNEL_ENCAP_FLAG_REMCSUM;
261858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert		} else if (strcmp(*argv, "noencap-remcsum") == 0) {
262858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert			encapflags &= ~TUNNEL_ENCAP_FLAG_REMCSUM;
2631ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "6rd-prefix") == 0) {
2641ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			inet_prefix prefix;
2651ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
2661ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (get_prefix(&prefix, *argv, AF_INET6))
2671ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				invarg("invalid 6rd_prefix\n", *argv);
2681ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			memcpy(&ip6rdprefix, prefix.data, 16);
2691ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdprefixlen = prefix.bitlen;
2701ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
2711ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			inet_prefix prefix;
2721ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			NEXT_ARG();
2731ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			if (get_prefix(&prefix, *argv, AF_INET))
2741ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				invarg("invalid 6rd-relay_prefix\n", *argv);
2751ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			memcpy(&ip6rdrelayprefix, prefix.data, 4);
2761ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdrelayprefixlen = prefix.bitlen;
2771ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else if (strcmp(*argv, "6rd-reset") == 0) {
2781ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			inet_prefix prefix;
2791ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			get_prefix(&prefix, "2002::", AF_INET6);
2801ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			memcpy(&ip6rdprefix, prefix.data, 16);
2811ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdprefixlen = 16;
2821ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdrelayprefix = 0;
2831ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			ip6rdrelayprefixlen = 0;
2841ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		} else
2851ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			usage(strcmp(lu->id, "sit") == 0);
2861ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		argc--, argv++;
2871ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
2881ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
2891ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (ttl && pmtudisc == 0) {
29030d07e9e36a4fd1044ed10b36e91dc6a124c6c3cRichard Godbee		fprintf(stderr, "ttl != 0 and nopmtudisc are incompatible\n");
2911ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		exit(-1);
2921ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
2931ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
2941ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr32(n, 1024, IFLA_IPTUN_LINK, link);
2951ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr);
2961ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr);
2971ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
2981ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
2991ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc);
300c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
301c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
302c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	addattr16(n, 1024, IFLA_IPTUN_ENCAP_FLAGS, encapflags);
303c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
304c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
305c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
3061ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (strcmp(lu->id, "sit") == 0) {
3071ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags);
30877620be89af6087c1ff63467f9ccb036db37a033Nicolas Dichtel		addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
3091ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (ip6rdprefixlen) {
3101ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX,
3111ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				  &ip6rdprefix, sizeof(ip6rdprefix));
3121ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN,
3131ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				  ip6rdprefixlen);
3141ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX,
3151ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				  ip6rdrelayprefix);
3161ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
3171ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel				  ip6rdrelayprefixlen);
3181ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		}
3191ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3201ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3211ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	return 0;
3221ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel}
3231ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3241ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtelstatic void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
3251ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel{
3261ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	char s1[1024];
3271ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	char s2[64];
3281ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	const char *local = "any";
3291ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	const char *remote = "any";
3301ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3311ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (!tb)
3321ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		return;
3331ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3341ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_REMOTE]) {
3351ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
3361ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3371ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (addr)
3381ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			remote = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
3391ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3401ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3411ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	fprintf(f, "remote %s ", remote);
3421ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3431ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_LOCAL]) {
3441ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		unsigned addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
3451ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3461ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (addr)
3471ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			local = format_host(AF_INET, 4, &addr, s1, sizeof(s1));
3481ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3491ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3501ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	fprintf(f, "local %s ", local);
3511ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3521ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_LINK] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
3531ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		unsigned link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
3541ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		const char *n = if_indextoname(link, s2);
3551ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3561ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (n)
3571ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			fprintf(f, "dev %s ", n);
3581ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		else
3591ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			fprintf(f, "dev %u ", link);
3601ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3611ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3621ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_TTL] && rta_getattr_u8(tb[IFLA_IPTUN_TTL]))
3631ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		fprintf(f, "ttl %d ", rta_getattr_u8(tb[IFLA_IPTUN_TTL]));
3641ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	else
3651ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		fprintf(f, "ttl inherit ");
3661ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3671ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_TOS] && rta_getattr_u8(tb[IFLA_IPTUN_TOS])) {
3681ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);
3691ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3701ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		fputs("tos ", f);
3711ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (tos == 1)
3721ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			fputs("inherit ", f);
3731ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		else
3741ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			fprintf(f, "0x%x ", tos);
3751ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3761ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3771ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC]))
3781ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		fprintf(f, "pmtudisc ");
3791ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	else
3801ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		fprintf(f, "nopmtudisc ");
3811ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3821ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_FLAGS]) {
383195f0f62d7ae3eb1f98dda46de852660503370ebNicolas Dichtel		__u16 iflags = rta_getattr_u16(tb[IFLA_IPTUN_FLAGS]);
3841ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
385195f0f62d7ae3eb1f98dda46de852660503370ebNicolas Dichtel		if (iflags & SIT_ISATAP)
386195f0f62d7ae3eb1f98dda46de852660503370ebNicolas Dichtel			fprintf(f, "isatap ");
3871ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
3881ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3891ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	if (tb[IFLA_IPTUN_6RD_PREFIXLEN] &&
3901ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	    *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIXLEN])) {
3911ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		__u16 prefixlen = rta_getattr_u16(tb[IFLA_IPTUN_6RD_PREFIXLEN]);
3921ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		__u16 relayprefixlen =
3931ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			rta_getattr_u16(tb[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
3941ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		__u32 relayprefix =
3951ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			rta_getattr_u32(tb[IFLA_IPTUN_6RD_RELAY_PREFIX]);
3961ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
3971ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		printf("6rd-prefix %s/%u ",
3981ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		       inet_ntop(AF_INET6, RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]),
399195f0f62d7ae3eb1f98dda46de852660503370ebNicolas Dichtel				 s1, sizeof(s1)),
4001ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		       prefixlen);
4011ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		if (relayprefix) {
4021ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			printf("6rd-relay_prefix %s/%u ",
4031ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			       format_host(AF_INET, 4, &relayprefix, s1,
404195f0f62d7ae3eb1f98dda46de852660503370ebNicolas Dichtel					   sizeof(s1)),
4051ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel			       relayprefixlen);
4061ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel		}
4071ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	}
408c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
409c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	if (tb[IFLA_IPTUN_ENCAP_TYPE] &&
410c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	    *(__u16 *)RTA_DATA(tb[IFLA_IPTUN_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) {
411c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		__u16 type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE]);
412c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		__u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]);
413c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		__u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]);
414c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		__u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]);
415c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
416c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		fputs("encap ", f);
417c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		switch (type) {
418c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		case TUNNEL_ENCAP_FOU:
419c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("fou ", f);
420c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			break;
421c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		case TUNNEL_ENCAP_GUE:
422c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("gue ", f);
423c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			break;
424c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		default:
425c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("unknown ", f);
426c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			break;
427c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		}
428c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
429c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (sport == 0)
430c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("encap-sport auto ", f);
431c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		else
432c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fprintf(f, "encap-sport %u", ntohs(sport));
433c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
434c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		fprintf(f, "encap-dport %u ", ntohs(dport));
435c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
436c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (flags & TUNNEL_ENCAP_FLAG_CSUM)
437c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("encap-csum ", f);
438c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		else
439c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("noencap-csum ", f);
440c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert
441c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
442c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("encap-csum6 ", f);
443c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert		else
444c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert			fputs("noencap-csum6 ", f);
445858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert
446858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert		if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
447858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert			fputs("encap-remcsum ", f);
448858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert		else
449858dbb208e3934525674252a6b6cf7d36a9de191Tom Herbert			fputs("noencap-remcsum ", f);
450c1159152e1b3abeee74b2d46f67e97fb5fa53ce4Tom Herbert	}
4511ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel}
4521ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
453561e650eff679296d3f4c12657721ae769cbc187vadimkstatic void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
454561e650eff679296d3f4c12657721ae769cbc187vadimk	FILE *f)
455561e650eff679296d3f4c12657721ae769cbc187vadimk{
456561e650eff679296d3f4c12657721ae769cbc187vadimk	print_usage(f, strcmp(lu->id, "sit") == 0);
457561e650eff679296d3f4c12657721ae769cbc187vadimk}
458561e650eff679296d3f4c12657721ae769cbc187vadimk
4591ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtelstruct link_util ipip_link_util = {
4601ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.id = "ipip",
4611ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.maxattr = IFLA_IPTUN_MAX,
4621ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.parse_opt = iptunnel_parse_opt,
4631ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.print_opt = iptunnel_print_opt,
464561e650eff679296d3f4c12657721ae769cbc187vadimk	.print_help = iptunnel_print_help,
4651ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel};
4661ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel
4671ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtelstruct link_util sit_link_util = {
4681ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.id = "sit",
4691ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.maxattr = IFLA_IPTUN_MAX,
4701ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.parse_opt = iptunnel_parse_opt,
4711ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel	.print_opt = iptunnel_print_opt,
472561e650eff679296d3f4c12657721ae769cbc187vadimk	.print_help = iptunnel_print_help,
4731ce2de97386e38c258ee2048a80ee28d0e8bad01Nicolas Dichtel};
474