naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

Blame SOURCES/0025-Consolidated-tunnel-support-fixes-for-ip6-gre-and-ip.patch

a4b897
From 0148deddeee0682884fe95b2ee4f58790498bf0a Mon Sep 17 00:00:00 2001
a4b897
From: Phil Sutter <psutter@redhat.com>
a4b897
Date: Mon, 3 Aug 2015 09:38:43 -0400
a4b897
Subject: [PATCH] Consolidated tunnel support/fixes for ip6/gre and ip6_vti
a4b897
a4b897
The original patches are:
a4b897
- iproute2: GRE over IPv6 tunnel support.
a4b897
- Whitespace and indentation cleanup
a4b897
- iproute2: Add support for IPv6 VTI tunnels to ip6tunnel
a4b897
- ip tunnel: fix 'ip -oneline tunnel show' for some GRE tunnels
a4b897
- iproute2: ip6_tunnel mode bugfixes: any,vti6
a4b897
- iproute2: VTI6 support for ip -6 link command.
a4b897
a4b897
Conflicts:
a4b897
	ip/ipaddress.c
a4b897
- due to previous backport of 07fa9c1529 ("Add VF link state control")
a4b897
- and missing 30b55792 ("iproute2: show counter of carrier on<->off transitions")
a4b897
---
a4b897
 ip/Makefile    |   5 +-
a4b897
 ip/ip6tunnel.c | 138 ++++++++++++++++----
a4b897
 ip/iplink.c    |  19 ++-
a4b897
 ip/iptunnel.c  |   2 +-
a4b897
 ip/link_gre6.c | 398 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
a4b897
 ip/link_vti6.c | 250 ++++++++++++++++++++++++++++++++++++
a4b897
 6 files changed, 776 insertions(+), 36 deletions(-)
a4b897
 create mode 100644 ip/link_gre6.c
a4b897
 create mode 100644 ip/link_vti6.c
a4b897
a4b897
diff --git a/ip/Makefile b/ip/Makefile
a4b897
index 48bd4a1..f377c37 100644
a4b897
--- a/ip/Makefile
a4b897
+++ b/ip/Makefile
a4b897
@@ -3,9 +3,9 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
a4b897
     ipmaddr.o ipmonitor.o ipmroute.o ipprefix.o iptuntap.o iptoken.o \
a4b897
     ipxfrm.o xfrm_state.o xfrm_policy.o xfrm_monitor.o \
a4b897
     iplink_vlan.o link_veth.o link_gre.o iplink_can.o \
a4b897
-    iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o \
a4b897
+    iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o link_vti6.o \
a4b897
     iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \
a4b897
-    link_iptnl.o
a4b897
+    link_iptnl.o link_gre6.o
a4b897
 
a4b897
 RTMONOBJ=rtmon.o
a4b897
 
a4b897
@@ -23,7 +23,6 @@ all: $(TARGETS) $(SCRIPTS)
a4b897
 
a4b897
 ip: $(IPOBJ) $(LIBNETLINK)
a4b897
 
a4b897
-
a4b897
 rtmon: $(RTMONOBJ)
a4b897
 
a4b897
 install: all
a4b897
diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c
a4b897
index 216e982..f37042e 100644
a4b897
--- a/ip/ip6tunnel.c
a4b897
+++ b/ip/ip6tunnel.c
a4b897
@@ -48,11 +48,12 @@ static void usage(void) __attribute__((noreturn));
a4b897
 static void usage(void)
a4b897
 {
a4b897
 	fprintf(stderr, "Usage: ip -f inet6 tunnel { add | change | del | show } [ NAME ]\n");
a4b897
-	fprintf(stderr, "          [ mode { ip6ip6 | ipip6 | any } ]\n");
a4b897
+	fprintf(stderr, "          [ mode { ip6ip6 | ipip6 | ip6gre | vti6 | any } ]\n");
a4b897
 	fprintf(stderr, "          [ remote ADDR local ADDR ] [ dev PHYS_DEV ]\n");
a4b897
 	fprintf(stderr, "          [ encaplimit ELIM ]\n");
a4b897
 	fprintf(stderr ,"          [ hoplimit TTL ] [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
a4b897
 	fprintf(stderr, "          [ dscp inherit ]\n");
a4b897
+	fprintf(stderr, "          [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
a4b897
 	fprintf(stderr, "\n");
a4b897
 	fprintf(stderr, "Where: NAME      := STRING\n");
a4b897
 	fprintf(stderr, "       ADDR      := IPV6_ADDRESS\n");
a4b897
@@ -62,10 +63,11 @@ static void usage(void)
a4b897
 		DEFAULT_TNL_HOP_LIMIT);
a4b897
 	fprintf(stderr, "       TCLASS    := { 0x0..0xff | inherit }\n");
a4b897
 	fprintf(stderr, "       FLOWLABEL := { 0x0..0xfffff | inherit }\n");
a4b897
+	fprintf(stderr, "       KEY       := { DOTTED_QUAD | NUMBER }\n");
a4b897
 	exit(-1);
a4b897
 }
a4b897
 
a4b897
-static void print_tunnel(struct ip6_tnl_parm *p)
a4b897
+static void print_tunnel(struct ip6_tnl_parm2 *p)
a4b897
 {
a4b897
 	char remote[64];
a4b897
 	char local[64];
a4b897
@@ -104,9 +106,29 @@ static void print_tunnel(struct ip6_tnl_parm *p)
a4b897
 
a4b897
 	if (p->flags & IP6_TNL_F_RCV_DSCP_COPY)
a4b897
 		printf(" dscp inherit");
a4b897
+
a4b897
+	if (p->proto == IPPROTO_GRE) {
a4b897
+		if ((p->i_flags&GRE_KEY) && (p->o_flags&GRE_KEY) && p->o_key == p->i_key)
a4b897
+			printf(" key %u", ntohl(p->i_key));
a4b897
+		else if ((p->i_flags|p->o_flags)&GRE_KEY) {
a4b897
+			if (p->i_flags&GRE_KEY)
a4b897
+				printf(" ikey %u ", ntohl(p->i_key));
a4b897
+			if (p->o_flags&GRE_KEY)
a4b897
+				printf(" okey %u ", ntohl(p->o_key));
a4b897
+		}
a4b897
+
a4b897
+		if (p->i_flags&GRE_SEQ)
a4b897
+			printf("%s  Drop packets out of sequence.", _SL_);
a4b897
+		if (p->i_flags&GRE_CSUM)
a4b897
+			printf("%s  Checksum in received packet is required.", _SL_);
a4b897
+		if (p->o_flags&GRE_SEQ)
a4b897
+			printf("%s  Sequence packets on output.", _SL_);
a4b897
+		if (p->o_flags&GRE_CSUM)
a4b897
+			printf("%s  Checksum output packets.", _SL_);
a4b897
+	}
a4b897
 }
a4b897
 
a4b897
-static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm *p)
a4b897
+static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm2 *p)
a4b897
 {
a4b897
 	int count = 0;
a4b897
 	char medium[IFNAMSIZ];
a4b897
@@ -119,19 +141,25 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm *p)
a4b897
 			if (strcmp(*argv, "ipv6/ipv6") == 0 ||
a4b897
 			    strcmp(*argv, "ip6ip6") == 0)
a4b897
 				p->proto = IPPROTO_IPV6;
a4b897
-			else if (strcmp(*argv, "ip/ipv6") == 0 ||
a4b897
+			else if (strcmp(*argv, "vti6") == 0) {
a4b897
+				p->proto = IPPROTO_IPV6;
a4b897
+				p->i_flags |= VTI_ISVTI;
a4b897
+			} else if (strcmp(*argv, "ip/ipv6") == 0 ||
a4b897
 				 strcmp(*argv, "ipv4/ipv6") == 0 ||
a4b897
 				 strcmp(*argv, "ipip6") == 0 ||
a4b897
 				 strcmp(*argv, "ip4ip6") == 0)
a4b897
 				p->proto = IPPROTO_IPIP;
a4b897
+			else if (strcmp(*argv, "ip6gre") == 0 ||
a4b897
+				 strcmp(*argv, "gre/ipv6") == 0)
a4b897
+				p->proto = IPPROTO_GRE;
a4b897
 			else if (strcmp(*argv, "any/ipv6") == 0 ||
a4b897
 				 strcmp(*argv, "any") == 0)
a4b897
 				p->proto = 0;
a4b897
 			else {
a4b897
-                                fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv);
a4b897
-                                exit(-1);
a4b897
-                        }
a4b897
-                } else if (strcmp(*argv, "remote") == 0) {
a4b897
+				fprintf(stderr,"Unknown tunnel mode \"%s\"\n", *argv);
a4b897
+				exit(-1);
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "remote") == 0) {
a4b897
 			inet_prefix raddr;
a4b897
 			NEXT_ARG();
a4b897
 			get_prefix(&raddr, *argv, preferred_family);
a4b897
@@ -202,6 +230,60 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm *p)
a4b897
 			if (strcmp(*argv, "inherit") != 0)
a4b897
 				invarg("not inherit", *argv);
a4b897
 			p->flags |= IP6_TNL_F_RCV_DSCP_COPY;
a4b897
+		} else if (strcmp(*argv, "key") == 0) {
a4b897
+			unsigned uval;
a4b897
+			NEXT_ARG();
a4b897
+			p->i_flags |= GRE_KEY;
a4b897
+			p->o_flags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				p->i_key = p->o_key = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0) < 0) {
a4b897
+					fprintf(stderr, "invalid value of \"key\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				p->i_key = p->o_key = htonl(uval);
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "ikey") == 0) {
a4b897
+			unsigned uval;
a4b897
+			NEXT_ARG();
a4b897
+			p->i_flags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				p->i_key = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0)<0) {
a4b897
+					fprintf(stderr, "invalid value of \"ikey\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				p->i_key = htonl(uval);
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "okey") == 0) {
a4b897
+			unsigned uval;
a4b897
+			NEXT_ARG();
a4b897
+			p->o_flags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				p->o_key = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0)<0) {
a4b897
+					fprintf(stderr, "invalid value of \"okey\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				p->o_key = htonl(uval);
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "seq") == 0) {
a4b897
+			p->i_flags |= GRE_SEQ;
a4b897
+			p->o_flags |= GRE_SEQ;
a4b897
+		} else if (strcmp(*argv, "iseq") == 0) {
a4b897
+			p->i_flags |= GRE_SEQ;
a4b897
+		} else if (strcmp(*argv, "oseq") == 0) {
a4b897
+			p->o_flags |= GRE_SEQ;
a4b897
+		} else if (strcmp(*argv, "csum") == 0) {
a4b897
+			p->i_flags |= GRE_CSUM;
a4b897
+			p->o_flags |= GRE_CSUM;
a4b897
+		} else if (strcmp(*argv, "icsum") == 0) {
a4b897
+			p->i_flags |= GRE_CSUM;
a4b897
+		} else if (strcmp(*argv, "ocsum") == 0) {
a4b897
+			p->o_flags |= GRE_CSUM;
a4b897
 		} else {
a4b897
 			if (strcmp(*argv, "name") == 0) {
a4b897
 				NEXT_ARG();
a4b897
@@ -212,7 +294,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm *p)
a4b897
 				duparg2("name", *argv);
a4b897
 			strncpy(p->name, *argv, IFNAMSIZ - 1);
a4b897
 			if (cmd == SIOCCHGTUNNEL && count == 0) {
a4b897
-				struct ip6_tnl_parm old_p;
a4b897
+				struct ip6_tnl_parm2 old_p;
a4b897
 				memset(&old_p, 0, sizeof(old_p));
a4b897
 				if (tnl_get_ioctl(*argv, &old_p))
a4b897
 					return -1;
a4b897
@@ -230,7 +312,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip6_tnl_parm *p)
a4b897
 	return 0;
a4b897
 }
a4b897
 
a4b897
-static void ip6_tnl_parm_init(struct ip6_tnl_parm *p, int apply_default)
a4b897
+static void ip6_tnl_parm_init(struct ip6_tnl_parm2 *p, int apply_default)
a4b897
 {
a4b897
 	memset(p, 0, sizeof(*p));
a4b897
 	p->proto = IPPROTO_IPV6;
a4b897
@@ -244,8 +326,8 @@ static void ip6_tnl_parm_init(struct ip6_tnl_parm *p, int apply_default)
a4b897
  * @p1: user specified parameter
a4b897
  * @p2: database entry
a4b897
  */
a4b897
-static int ip6_tnl_parm_match(const struct ip6_tnl_parm *p1,
a4b897
-			      const struct ip6_tnl_parm *p2)
a4b897
+static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 *p1,
a4b897
+			      const struct ip6_tnl_parm2 *p2)
a4b897
 {
a4b897
 	return ((!p1->link || p1->link == p2->link) &&
a4b897
 		(!p1->name[0] || strcmp(p1->name, p2->name) == 0) &&
a4b897
@@ -263,7 +345,7 @@ static int ip6_tnl_parm_match(const struct ip6_tnl_parm *p1,
a4b897
 		(!p1->flags || (p1->flags & p2->flags)));
a4b897
 }
a4b897
 
a4b897
-static int do_tunnels_list(struct ip6_tnl_parm *p)
a4b897
+static int do_tunnels_list(struct ip6_tnl_parm2 *p)
a4b897
 {
a4b897
 	char buf[512];
a4b897
 	int err = -1;
a4b897
@@ -287,7 +369,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
a4b897
 			rx_fifo, rx_frame,
a4b897
 			tx_bytes, tx_packets, tx_errs, tx_drops,
a4b897
 			tx_fifo, tx_colls, tx_carrier, rx_multi;
a4b897
-		struct ip6_tnl_parm p1;
a4b897
+		struct ip6_tnl_parm2 p1;
a4b897
 		char *ptr;
a4b897
 
a4b897
 		buf[sizeof(buf) - 1] = '\0';
a4b897
@@ -312,10 +394,12 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
a4b897
 			fprintf(stderr, "Failed to get type of \"%s\"\n", name);
a4b897
 			continue;
a4b897
 		}
a4b897
-		if (type != ARPHRD_TUNNEL6)
a4b897
+		if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE)
a4b897
 			continue;
a4b897
 		memset(&p1, 0, sizeof(p1));
a4b897
 		ip6_tnl_parm_init(&p1, 0);
a4b897
+		if (type == ARPHRD_IP6GRE)
a4b897
+			p1.proto = IPPROTO_GRE;
a4b897
 		strcpy(p1.name, name);
a4b897
 		p1.link = ll_name_to_index(p1.name);
a4b897
 		if (p1.link == 0)
a4b897
@@ -346,7 +430,7 @@ static int do_tunnels_list(struct ip6_tnl_parm *p)
a4b897
 
a4b897
 static int do_show(int argc, char **argv)
a4b897
 {
a4b897
-        struct ip6_tnl_parm p;
a4b897
+        struct ip6_tnl_parm2 p;
a4b897
 
a4b897
 	ll_init_map(&rth);
a4b897
 	ip6_tnl_parm_init(&p, 0);
a4b897
@@ -369,28 +453,38 @@ static int do_show(int argc, char **argv)
a4b897
 
a4b897
 static int do_add(int cmd, int argc, char **argv)
a4b897
 {
a4b897
-	struct ip6_tnl_parm p;
a4b897
+	struct ip6_tnl_parm2 p;
a4b897
+	const char *basedev = "ip6tnl0";
a4b897
 
a4b897
 	ip6_tnl_parm_init(&p, 1);
a4b897
 
a4b897
 	if (parse_args(argc, argv, cmd, &p) < 0)
a4b897
 		return -1;
a4b897
 
a4b897
-	return tnl_add_ioctl(cmd,
a4b897
-			     cmd == SIOCCHGTUNNEL && p.name[0] ?
a4b897
-			     p.name : "ip6tnl0", p.name, &p);
a4b897
+	if (p.proto == IPPROTO_GRE)
a4b897
+		basedev = "ip6gre0";
a4b897
+	else if (p.i_flags & VTI_ISVTI)
a4b897
+		basedev = "ip6_vti0";
a4b897
+
a4b897
+	return tnl_add_ioctl(cmd, basedev, p.name, &p);
a4b897
 }
a4b897
 
a4b897
 static int do_del(int argc, char **argv)
a4b897
 {
a4b897
-	struct ip6_tnl_parm p;
a4b897
+	struct ip6_tnl_parm2 p;
a4b897
+	const char *basedev = "ip6tnl0";
a4b897
 
a4b897
 	ip6_tnl_parm_init(&p, 1);
a4b897
 
a4b897
 	if (parse_args(argc, argv, SIOCDELTUNNEL, &p) < 0)
a4b897
 		return -1;
a4b897
 
a4b897
-	return tnl_del_ioctl(p.name[0] ? p.name : "ip6tnl0", p.name, &p);
a4b897
+	if (p.proto == IPPROTO_GRE)
a4b897
+		basedev = "ip6gre0";
a4b897
+	else if (p.i_flags & VTI_ISVTI)
a4b897
+		basedev = "ip6_vti0";
a4b897
+
a4b897
+	return tnl_del_ioctl(basedev, p.name, &p);
a4b897
 }
a4b897
 
a4b897
 int do_ip6tunnel(int argc, char **argv)
a4b897
diff --git a/ip/iplink.c b/ip/iplink.c
a4b897
index dc98019..deef14b 100644
a4b897
--- a/ip/iplink.c
a4b897
+++ b/ip/iplink.c
a4b897
@@ -84,7 +84,8 @@ void iplink_usage(void)
a4b897
 	if (iplink_have_newlink()) {
a4b897
 		fprintf(stderr, "\n");
a4b897
 		fprintf(stderr, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can |\n");
a4b897
-		fprintf(stderr, "          bridge | ipoib | ip6tnl | ipip | sit | vxlan }\n");
a4b897
+		fprintf(stderr, "          bridge | ipoib | ip6tnl | ipip | sit | vxlan |\n");
a4b897
+		fprintf(stderr, "          gre | gretap | ip6gre | ip6gretap }\n");
a4b897
 	}
a4b897
 	exit(-1);
a4b897
 }
a4b897
@@ -242,7 +243,7 @@ static int iplink_parse_vf(int vf, int *argcp, char ***argvp,
a4b897
 			}
a4b897
 			ivt.vf = vf;
a4b897
 			addattr_l(&req->n, sizeof(*req), IFLA_VF_TX_RATE, &ivt, sizeof(ivt));
a4b897
-		
a4b897
+
a4b897
 		} else if (matches(*argv, "spoofchk") == 0) {
a4b897
 			struct ifla_vf_spoofchk ivs;
a4b897
 			NEXT_ARG();
a4b897
@@ -272,7 +273,6 @@ static int iplink_parse_vf(int vf, int *argcp, char ***argvp,
a4b897
 	return 0;
a4b897
 }
a4b897
 
a4b897
-
a4b897
 int iplink_parse(int argc, char **argv, struct iplink_req *req,
a4b897
 		char **name, char **type, char **link, char **dev, int *group)
a4b897
 {
a4b897
@@ -330,16 +330,16 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
a4b897
 			if (get_integer(&mtu, *argv, 0))
a4b897
 				invarg("Invalid \"mtu\" value\n", *argv);
a4b897
 			addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4);
a4b897
-                } else if (strcmp(*argv, "netns") == 0) {
a4b897
-                        NEXT_ARG();
a4b897
-                        if (netns != -1)
a4b897
-                                duparg("netns", *argv);
a4b897
+		} else if (strcmp(*argv, "netns") == 0) {
a4b897
+			NEXT_ARG();
a4b897
+			if (netns != -1)
a4b897
+				duparg("netns", *argv);
a4b897
 			if ((netns = get_netns_fd(*argv)) >= 0)
a4b897
 				addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
a4b897
 			else if (get_integer(&netns, *argv, 0) == 0)
a4b897
 				addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
a4b897
 			else
a4b897
-                                invarg("Invalid \"netns\" value\n", *argv);
a4b897
+				invarg("Invalid \"netns\" value\n", *argv);
a4b897
 		} else if (strcmp(*argv, "multicast") == 0) {
a4b897
 			NEXT_ARG();
a4b897
 			req->i.ifi_change |= IFF_MULTICAST;
a4b897
@@ -797,7 +797,6 @@ static int set_address(struct ifreq *ifr, int brd)
a4b897
 	return 0;
a4b897
 }
a4b897
 
a4b897
-
a4b897
 static int do_set(int argc, char **argv)
a4b897
 {
a4b897
 	char *dev = NULL;
a4b897
@@ -897,7 +896,7 @@ static int do_set(int argc, char **argv)
a4b897
 			} else
a4b897
 				return on_off("dynamic", *argv);
a4b897
 		} else {
a4b897
-                        if (strcmp(*argv, "dev") == 0) {
a4b897
+			if (strcmp(*argv, "dev") == 0) {
a4b897
 				NEXT_ARG();
a4b897
 			}
a4b897
 			if (matches(*argv, "help") == 0)
a4b897
diff --git a/ip/iptunnel.c b/ip/iptunnel.c
a4b897
index 43f8585..9099503 100644
a4b897
--- a/ip/iptunnel.c
a4b897
+++ b/ip/iptunnel.c
a4b897
@@ -405,7 +405,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
a4b897
 	}
a4b897
 
a4b897
 	if (p->i_flags&GRE_SEQ)
a4b897
-		printf("%s  Drop packets out of sequence.\n", _SL_);
a4b897
+		printf("%s  Drop packets out of sequence.", _SL_);
a4b897
 	if (p->i_flags&GRE_CSUM)
a4b897
 		printf("%s  Checksum in received packet is required.", _SL_);
a4b897
 	if (p->o_flags&GRE_SEQ)
a4b897
diff --git a/ip/link_gre6.c b/ip/link_gre6.c
a4b897
new file mode 100644
a4b897
index 0000000..4c9c536
a4b897
--- /dev/null
a4b897
+++ b/ip/link_gre6.c
a4b897
@@ -0,0 +1,398 @@
a4b897
+/*
a4b897
+ * link_gre6.c	gre driver module
a4b897
+ *
a4b897
+ *		This program is free software; you can redistribute it and/or
a4b897
+ *		modify it under the terms of the GNU General Public License
a4b897
+ *		as published by the Free Software Foundation; either version
a4b897
+ *		2 of the License, or (at your option) any later version.
a4b897
+ *
a4b897
+ * Authors:	Dmitry Kozlov <xeb@mail.ru>
a4b897
+ *
a4b897
+ */
a4b897
+
a4b897
+#include <string.h>
a4b897
+#include <net/if.h>
a4b897
+#include <sys/types.h>
a4b897
+#include <sys/socket.h>
a4b897
+#include <arpa/inet.h>
a4b897
+
a4b897
+#include <linux/ip.h>
a4b897
+#include <linux/if_tunnel.h>
a4b897
+#include <linux/ip6_tunnel.h>
a4b897
+
a4b897
+#include "rt_names.h"
a4b897
+#include "utils.h"
a4b897
+#include "ip_common.h"
a4b897
+#include "tunnel.h"
a4b897
+
a4b897
+#define IP6_FLOWINFO_TCLASS	htonl(0x0FF00000)
a4b897
+#define IP6_FLOWINFO_FLOWLABEL	htonl(0x000FFFFF)
a4b897
+
a4b897
+#define DEFAULT_TNL_HOP_LIMIT	(64)
a4b897
+
a4b897
+static void usage(void) __attribute__((noreturn));
a4b897
+static void usage(void)
a4b897
+{
a4b897
+	fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n");
a4b897
+	fprintf(stderr, "          type { ip6gre | ip6gretap } [ remote ADDR ] [ local ADDR ]\n");
a4b897
+	fprintf(stderr, "          [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]\n");
a4b897
+	fprintf(stderr, "          [ hoplimit TTL ] [ encaplimit ELIM ]\n");
a4b897
+	fprintf(stderr, "          [ tclass TCLASS ] [ flowlabel FLOWLABEL ]\n");
a4b897
+	fprintf(stderr, "          [ dscp inherit ] [ dev PHYS_DEV ]\n");
a4b897
+	fprintf(stderr, "\n");
a4b897
+	fprintf(stderr, "Where: NAME      := STRING\n");
a4b897
+	fprintf(stderr, "       ADDR      := IPV6_ADDRESS\n");
a4b897
+	fprintf(stderr, "       TTL       := { 0..255 } (default=%d)\n",
a4b897
+		DEFAULT_TNL_HOP_LIMIT);
a4b897
+	fprintf(stderr, "       KEY       := { DOTTED_QUAD | NUMBER }\n");
a4b897
+	fprintf(stderr, "       ELIM      := { none | 0..255 }(default=%d)\n",
a4b897
+		IPV6_DEFAULT_TNL_ENCAP_LIMIT);
a4b897
+	fprintf(stderr, "       TCLASS    := { 0x0..0xff | inherit }\n");
a4b897
+	fprintf(stderr, "       FLOWLABEL := { 0x0..0xfffff | inherit }\n");
a4b897
+	exit(-1);
a4b897
+}
a4b897
+
a4b897
+static int gre_parse_opt(struct link_util *lu, int argc, char **argv,
a4b897
+			 struct nlmsghdr *n)
a4b897
+{
a4b897
+	struct {
a4b897
+		struct nlmsghdr n;
a4b897
+		struct ifinfomsg i;
a4b897
+		char buf[1024];
a4b897
+	} req;
a4b897
+	struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
a4b897
+	struct rtattr *tb[IFLA_MAX + 1];
a4b897
+	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
a4b897
+	struct rtattr *greinfo[IFLA_GRE_MAX + 1];
a4b897
+	__u16 iflags = 0;
a4b897
+	__u16 oflags = 0;
a4b897
+	unsigned ikey = 0;
a4b897
+	unsigned okey = 0;
a4b897
+	struct in6_addr raddr = IN6ADDR_ANY_INIT;
a4b897
+	struct in6_addr laddr = IN6ADDR_ANY_INIT;
a4b897
+	unsigned link = 0;
a4b897
+	unsigned flowinfo = 0;
a4b897
+	unsigned flags = 0;
a4b897
+	__u8 hop_limit = DEFAULT_TNL_HOP_LIMIT;
a4b897
+	__u8 encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
a4b897
+	int len;
a4b897
+
a4b897
+	if (!(n->nlmsg_flags & NLM_F_CREATE)) {
a4b897
+		memset(&req, 0, sizeof(req));
a4b897
+
a4b897
+		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
a4b897
+		req.n.nlmsg_flags = NLM_F_REQUEST;
a4b897
+		req.n.nlmsg_type = RTM_GETLINK;
a4b897
+		req.i.ifi_family = preferred_family;
a4b897
+		req.i.ifi_index = ifi->ifi_index;
a4b897
+
a4b897
+		if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) {
a4b897
+get_failed:
a4b897
+			fprintf(stderr,
a4b897
+				"Failed to get existing tunnel info.\n");
a4b897
+			return -1;
a4b897
+		}
a4b897
+
a4b897
+		len = req.n.nlmsg_len;
a4b897
+		len -= NLMSG_LENGTH(sizeof(*ifi));
a4b897
+		if (len < 0)
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
a4b897
+
a4b897
+		if (!tb[IFLA_LINKINFO])
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
a4b897
+
a4b897
+		if (!linkinfo[IFLA_INFO_DATA])
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr_nested(greinfo, IFLA_GRE_MAX,
a4b897
+				    linkinfo[IFLA_INFO_DATA]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_IKEY])
a4b897
+			ikey = rta_getattr_u32(greinfo[IFLA_GRE_IKEY]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_OKEY])
a4b897
+			okey = rta_getattr_u32(greinfo[IFLA_GRE_OKEY]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_IFLAGS])
a4b897
+			iflags = rta_getattr_u16(greinfo[IFLA_GRE_IFLAGS]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_OFLAGS])
a4b897
+			oflags = rta_getattr_u16(greinfo[IFLA_GRE_OFLAGS]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_LOCAL])
a4b897
+			memcpy(&laddr, RTA_DATA(greinfo[IFLA_GRE_LOCAL]), sizeof(laddr));
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_REMOTE])
a4b897
+			memcpy(&raddr, RTA_DATA(greinfo[IFLA_GRE_REMOTE]), sizeof(raddr));
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_TTL])
a4b897
+			hop_limit = rta_getattr_u8(greinfo[IFLA_GRE_TTL]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_LINK])
a4b897
+			link = rta_getattr_u32(greinfo[IFLA_GRE_LINK]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_ENCAP_LIMIT])
a4b897
+			encap_limit = rta_getattr_u8(greinfo[IFLA_GRE_ENCAP_LIMIT]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_FLOWINFO])
a4b897
+			flowinfo = rta_getattr_u32(greinfo[IFLA_GRE_FLOWINFO]);
a4b897
+
a4b897
+		if (greinfo[IFLA_GRE_FLAGS])
a4b897
+			flags = rta_getattr_u32(greinfo[IFLA_GRE_FLAGS]);
a4b897
+	}
a4b897
+
a4b897
+	while (argc > 0) {
a4b897
+		if (!matches(*argv, "key")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			iflags |= GRE_KEY;
a4b897
+			oflags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0) < 0) {
a4b897
+					fprintf(stderr,
a4b897
+						"Invalid value for \"key\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+
a4b897
+			ikey = okey = uval;
a4b897
+		} else if (!matches(*argv, "ikey")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			iflags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0)<0) {
a4b897
+					fprintf(stderr, "invalid value of \"ikey\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+			ikey = uval;
a4b897
+		} else if (!matches(*argv, "okey")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			oflags |= GRE_KEY;
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0)<0) {
a4b897
+					fprintf(stderr, "invalid value of \"okey\"\n");
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+			okey = uval;
a4b897
+		} else if (!matches(*argv, "seq")) {
a4b897
+			iflags |= GRE_SEQ;
a4b897
+			oflags |= GRE_SEQ;
a4b897
+		} else if (!matches(*argv, "iseq")) {
a4b897
+			iflags |= GRE_SEQ;
a4b897
+		} else if (!matches(*argv, "oseq")) {
a4b897
+			oflags |= GRE_SEQ;
a4b897
+		} else if (!matches(*argv, "csum")) {
a4b897
+			iflags |= GRE_CSUM;
a4b897
+			oflags |= GRE_CSUM;
a4b897
+		} else if (!matches(*argv, "icsum")) {
a4b897
+			iflags |= GRE_CSUM;
a4b897
+		} else if (!matches(*argv, "ocsum")) {
a4b897
+			oflags |= GRE_CSUM;
a4b897
+		} else if (!matches(*argv, "remote")) {
a4b897
+			inet_prefix addr;
a4b897
+			NEXT_ARG();
a4b897
+			get_prefix(&addr, *argv, preferred_family);
a4b897
+			if (addr.family == AF_UNSPEC)
a4b897
+				invarg("\"remote\" address family is AF_UNSPEC", *argv);
a4b897
+			memcpy(&raddr, &addr.data, sizeof(raddr));
a4b897
+		} else if (!matches(*argv, "local")) {
a4b897
+			inet_prefix addr;
a4b897
+			NEXT_ARG();
a4b897
+			get_prefix(&addr, *argv, preferred_family);
a4b897
+			if (addr.family == AF_UNSPEC)
a4b897
+				invarg("\"local\" address family is AF_UNSPEC", *argv);
a4b897
+			memcpy(&laddr, &addr.data, sizeof(laddr));
a4b897
+		} else if (!matches(*argv, "dev")) {
a4b897
+			NEXT_ARG();
a4b897
+			link = if_nametoindex(*argv);
a4b897
+			if (link == 0)
a4b897
+				exit(-1);
a4b897
+		} else if (!matches(*argv, "ttl") ||
a4b897
+			   !matches(*argv, "hoplimit")) {
a4b897
+			__u8 uval;
a4b897
+			NEXT_ARG();
a4b897
+			if (get_u8(&uval, *argv, 0))
a4b897
+				invarg("invalid TTL", *argv);
a4b897
+			hop_limit = uval;
a4b897
+		} else if (!matches(*argv, "tos") ||
a4b897
+			   !matches(*argv, "tclass") ||
a4b897
+			   !matches(*argv, "dsfield")) {
a4b897
+			__u8 uval;
a4b897
+			NEXT_ARG();
a4b897
+			if (strcmp(*argv, "inherit") == 0)
a4b897
+				flags |= IP6_TNL_F_USE_ORIG_TCLASS;
a4b897
+			else {
a4b897
+				if (get_u8(&uval, *argv, 16))
a4b897
+					invarg("invalid TClass", *argv);
a4b897
+				flowinfo |= htonl((__u32)uval << 20) & IP6_FLOWINFO_TCLASS;
a4b897
+				flags &= ~IP6_TNL_F_USE_ORIG_TCLASS;
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "flowlabel") == 0 ||
a4b897
+			   strcmp(*argv, "fl") == 0) {
a4b897
+			__u32 uval;
a4b897
+			NEXT_ARG();
a4b897
+			if (strcmp(*argv, "inherit") == 0)
a4b897
+				flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL;
a4b897
+			else {
a4b897
+				if (get_u32(&uval, *argv, 16))
a4b897
+					invarg("invalid Flowlabel", *argv);
a4b897
+				if (uval > 0xFFFFF)
a4b897
+					invarg("invalid Flowlabel", *argv);
a4b897
+				flowinfo |= htonl(uval) & IP6_FLOWINFO_FLOWLABEL;
a4b897
+				flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL;
a4b897
+			}
a4b897
+		} else if (strcmp(*argv, "dscp") == 0) {
a4b897
+			NEXT_ARG();
a4b897
+			if (strcmp(*argv, "inherit") != 0)
a4b897
+				invarg("not inherit", *argv);
a4b897
+			flags |= IP6_TNL_F_RCV_DSCP_COPY;
a4b897
+		} else
a4b897
+			usage();
a4b897
+		argc--; argv++;
a4b897
+	}
a4b897
+
a4b897
+	addattr32(n, 1024, IFLA_GRE_IKEY, ikey);
a4b897
+	addattr32(n, 1024, IFLA_GRE_OKEY, okey);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_IFLAGS, &iflags, 2);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_OFLAGS, &oflags, 2);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_LOCAL, &laddr, sizeof(laddr));
a4b897
+	addattr_l(n, 1024, IFLA_GRE_REMOTE, &raddr, sizeof(raddr));
a4b897
+	if (link)
a4b897
+		addattr32(n, 1024, IFLA_GRE_LINK, link);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_TTL, &hop_limit, 1);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_ENCAP_LIMIT, &encap_limit, 1);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_FLOWINFO, &flowinfo, 4);
a4b897
+	addattr_l(n, 1024, IFLA_GRE_FLAGS, &flowinfo, 4);
a4b897
+
a4b897
+	return 0;
a4b897
+}
a4b897
+
a4b897
+static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
a4b897
+{
a4b897
+	char s1[1024];
a4b897
+	char s2[64];
a4b897
+	const char *local = "any";
a4b897
+	const char *remote = "any";
a4b897
+	unsigned iflags = 0;
a4b897
+	unsigned oflags = 0;
a4b897
+	unsigned flags = 0;
a4b897
+	unsigned flowinfo = 0;
a4b897
+	struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT;
a4b897
+
a4b897
+	if (!tb)
a4b897
+		return;
a4b897
+
a4b897
+	if (tb[IFLA_GRE_FLAGS])
a4b897
+		flags = rta_getattr_u32(tb[IFLA_GRE_FLAGS]);
a4b897
+
a4b897
+	if (tb[IFLA_GRE_FLOWINFO])
a4b897
+		flags = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]);
a4b897
+
a4b897
+	if (tb[IFLA_GRE_REMOTE]) {
a4b897
+		struct in6_addr addr;
a4b897
+		memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr));
a4b897
+
a4b897
+		if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
a4b897
+			remote = format_host(AF_INET6, sizeof(addr), &addr, s1, sizeof(s1));
a4b897
+	}
a4b897
+
a4b897
+	fprintf(f, "remote %s ", remote);
a4b897
+
a4b897
+	if (tb[IFLA_GRE_LOCAL]) {
a4b897
+		struct in6_addr addr;
a4b897
+		memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr));
a4b897
+
a4b897
+		if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
a4b897
+			local = format_host(AF_INET6, sizeof(addr), &addr, s1, sizeof(s1));
a4b897
+	}
a4b897
+
a4b897
+	fprintf(f, "local %s ", local);
a4b897
+
a4b897
+	if (tb[IFLA_GRE_LINK] && rta_getattr_u32(tb[IFLA_GRE_LINK])) {
a4b897
+		unsigned link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
a4b897
+		const char *n = if_indextoname(link, s2);
a4b897
+
a4b897
+		if (n)
a4b897
+			fprintf(f, "dev %s ", n);
a4b897
+		else
a4b897
+			fprintf(f, "dev %u ", link);
a4b897
+	}
a4b897
+
a4b897
+	if (tb[IFLA_GRE_TTL] && rta_getattr_u8(tb[IFLA_GRE_TTL]))
a4b897
+		fprintf(f, "hoplimit %d ", rta_getattr_u8(tb[IFLA_GRE_TTL]));
a4b897
+
a4b897
+	if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT)
a4b897
+		fprintf(f, "encaplimit none ");
a4b897
+	else if (tb[IFLA_GRE_ENCAP_LIMIT]) {
a4b897
+		int encap_limit = rta_getattr_u8(tb[IFLA_GRE_ENCAP_LIMIT]);
a4b897
+
a4b897
+		fprintf(f, "encaplimit %d ", encap_limit);
a4b897
+	}
a4b897
+
a4b897
+	if (flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
a4b897
+		fprintf(f, "flowlabel inherit ");
a4b897
+	else
a4b897
+		fprintf(f, "flowlabel 0x%05x ", ntohl(flowinfo & IP6_FLOWINFO_FLOWLABEL));
a4b897
+
a4b897
+	if (flags & IP6_TNL_F_RCV_DSCP_COPY)
a4b897
+		fprintf(f, "dscp inherit ");
a4b897
+
a4b897
+	if (tb[IFLA_GRE_IFLAGS])
a4b897
+		iflags = rta_getattr_u16(tb[IFLA_GRE_IFLAGS]);
a4b897
+
a4b897
+	if (tb[IFLA_GRE_OFLAGS])
a4b897
+		oflags = rta_getattr_u16(tb[IFLA_GRE_OFLAGS]);
a4b897
+
a4b897
+	if ((iflags & GRE_KEY) && tb[IFLA_GRE_IKEY]) {
a4b897
+		inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_IKEY]), s2, sizeof(s2));
a4b897
+		fprintf(f, "ikey %s ", s2);
a4b897
+	}
a4b897
+
a4b897
+	if ((oflags & GRE_KEY) && tb[IFLA_GRE_OKEY]) {
a4b897
+		inet_ntop(AF_INET, RTA_DATA(tb[IFLA_GRE_OKEY]), s2, sizeof(s2));
a4b897
+		fprintf(f, "okey %s ", s2);
a4b897
+	}
a4b897
+
a4b897
+	if (iflags & GRE_SEQ)
a4b897
+		fputs("iseq ", f);
a4b897
+	if (oflags & GRE_SEQ)
a4b897
+		fputs("oseq ", f);
a4b897
+	if (iflags & GRE_CSUM)
a4b897
+		fputs("icsum ", f);
a4b897
+	if (oflags & GRE_CSUM)
a4b897
+		fputs("ocsum ", f);
a4b897
+}
a4b897
+
a4b897
+struct link_util ip6gre_link_util = {
a4b897
+	.id = "ip6gre",
a4b897
+	.maxattr = IFLA_GRE_MAX,
a4b897
+	.parse_opt = gre_parse_opt,
a4b897
+	.print_opt = gre_print_opt,
a4b897
+};
a4b897
+
a4b897
+struct link_util ip6gretap_link_util = {
a4b897
+	.id = "ip6gretap",
a4b897
+	.maxattr = IFLA_GRE_MAX,
a4b897
+	.parse_opt = gre_parse_opt,
a4b897
+	.print_opt = gre_print_opt,
a4b897
+};
a4b897
diff --git a/ip/link_vti6.c b/ip/link_vti6.c
a4b897
new file mode 100644
a4b897
index 0000000..282896d
a4b897
--- /dev/null
a4b897
+++ b/ip/link_vti6.c
a4b897
@@ -0,0 +1,250 @@
a4b897
+/*
a4b897
+ * link_vti6.c	VTI driver module
a4b897
+ *
a4b897
+ *		This program is free software; you can redistribute it and/or
a4b897
+ *		modify it under the terms of the GNU General Public License
a4b897
+ *		as published by the Free Software Foundation; either version
a4b897
+ *		2 of the License, or (at your option) any later version.
a4b897
+ *
a4b897
+ * Authors:	Herbert Xu <herbert@gondor.apana.org.au>
a4b897
+ *		Saurabh Mohan <saurabh.mohan@vyatta.com> Modified link_gre.c for VTI
a4b897
+ *		Steffen Klassert <steffen.klassert@secunet.com> Modified link_vti.c for IPv6
a4b897
+ */
a4b897
+
a4b897
+#include <string.h>
a4b897
+#include <net/if.h>
a4b897
+#include <sys/types.h>
a4b897
+#include <sys/socket.h>
a4b897
+#include <arpa/inet.h>
a4b897
+
a4b897
+#include <linux/ip.h>
a4b897
+#include <linux/if_tunnel.h>
a4b897
+#include "rt_names.h"
a4b897
+#include "utils.h"
a4b897
+#include "ip_common.h"
a4b897
+#include "tunnel.h"
a4b897
+
a4b897
+
a4b897
+static void usage(void) __attribute__((noreturn));
a4b897
+static void usage(void)
a4b897
+{
a4b897
+	fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n");
a4b897
+	fprintf(stderr, "          type { vti6 } [ remote ADDR ] [ local ADDR ]\n");
a4b897
+	fprintf(stderr, "          [ [i|o]key KEY ]\n");
a4b897
+	fprintf(stderr, "          [ dev PHYS_DEV ]\n");
a4b897
+	fprintf(stderr, "\n");
a4b897
+	fprintf(stderr, "Where: NAME := STRING\n");
a4b897
+	fprintf(stderr, "       ADDR := { IPV6_ADDRESS }\n");
a4b897
+	fprintf(stderr, "       KEY  := { DOTTED_QUAD | NUMBER }\n");
a4b897
+	exit(-1);
a4b897
+}
a4b897
+
a4b897
+static int vti6_parse_opt(struct link_util *lu, int argc, char **argv,
a4b897
+			  struct nlmsghdr *n)
a4b897
+{
a4b897
+	struct {
a4b897
+		struct nlmsghdr n;
a4b897
+		struct ifinfomsg i;
a4b897
+		char buf[1024];
a4b897
+	} req;
a4b897
+	struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
a4b897
+	struct rtattr *tb[IFLA_MAX + 1];
a4b897
+	struct rtattr *linkinfo[IFLA_INFO_MAX+1];
a4b897
+	struct rtattr *vtiinfo[IFLA_VTI_MAX + 1];
a4b897
+	struct in6_addr saddr;
a4b897
+	struct in6_addr daddr;
a4b897
+	unsigned ikey = 0;
a4b897
+	unsigned okey = 0;
a4b897
+	unsigned link = 0;
a4b897
+	int len;
a4b897
+
a4b897
+	if (!(n->nlmsg_flags & NLM_F_CREATE)) {
a4b897
+		memset(&req, 0, sizeof(req));
a4b897
+
a4b897
+		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
a4b897
+		req.n.nlmsg_flags = NLM_F_REQUEST;
a4b897
+		req.n.nlmsg_type = RTM_GETLINK;
a4b897
+		req.i.ifi_family = preferred_family;
a4b897
+		req.i.ifi_index = ifi->ifi_index;
a4b897
+
a4b897
+		if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0) {
a4b897
+get_failed:
a4b897
+			fprintf(stderr,
a4b897
+				"Failed to get existing tunnel info.\n");
a4b897
+			return -1;
a4b897
+		}
a4b897
+
a4b897
+		len = req.n.nlmsg_len;
a4b897
+		len -= NLMSG_LENGTH(sizeof(*ifi));
a4b897
+		if (len < 0)
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len);
a4b897
+
a4b897
+		if (!tb[IFLA_LINKINFO])
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
a4b897
+
a4b897
+		if (!linkinfo[IFLA_INFO_DATA])
a4b897
+			goto get_failed;
a4b897
+
a4b897
+		parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX,
a4b897
+				    linkinfo[IFLA_INFO_DATA]);
a4b897
+
a4b897
+		if (vtiinfo[IFLA_VTI_IKEY])
a4b897
+			ikey = rta_getattr_u32(vtiinfo[IFLA_VTI_IKEY]);
a4b897
+
a4b897
+		if (vtiinfo[IFLA_VTI_OKEY])
a4b897
+			okey = rta_getattr_u32(vtiinfo[IFLA_VTI_OKEY]);
a4b897
+
a4b897
+		if (vtiinfo[IFLA_VTI_LOCAL])
a4b897
+			memcpy(&saddr, RTA_DATA(vtiinfo[IFLA_VTI_LOCAL]), sizeof(saddr));
a4b897
+
a4b897
+		if (vtiinfo[IFLA_VTI_REMOTE])
a4b897
+			memcpy(&daddr, RTA_DATA(vtiinfo[IFLA_VTI_REMOTE]), sizeof(daddr));
a4b897
+
a4b897
+		if (vtiinfo[IFLA_VTI_LINK])
a4b897
+			link = rta_getattr_u8(vtiinfo[IFLA_VTI_LINK]);
a4b897
+	}
a4b897
+
a4b897
+	while (argc > 0) {
a4b897
+		if (!matches(*argv, "key")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0) < 0) {
a4b897
+					fprintf(stderr,
a4b897
+						"Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv);
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+
a4b897
+			ikey = okey = uval;
a4b897
+		} else if (!matches(*argv, "ikey")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0) < 0) {
a4b897
+					fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv);
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+			ikey = uval;
a4b897
+		} else if (!matches(*argv, "okey")) {
a4b897
+			unsigned uval;
a4b897
+
a4b897
+			NEXT_ARG();
a4b897
+			if (strchr(*argv, '.'))
a4b897
+				uval = get_addr32(*argv);
a4b897
+			else {
a4b897
+				if (get_unsigned(&uval, *argv, 0) < 0) {
a4b897
+					fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv);
a4b897
+					exit(-1);
a4b897
+				}
a4b897
+				uval = htonl(uval);
a4b897
+			}
a4b897
+			okey = uval;
a4b897
+		} else if (!matches(*argv, "remote")) {
a4b897
+			NEXT_ARG();
a4b897
+			if (!strcmp(*argv, "any")) {
a4b897
+				fprintf(stderr, "invalid value for \"remote\": \"%s\"\n", *argv);
a4b897
+				exit(-1);
a4b897
+			} else {
a4b897
+				inet_prefix addr;
a4b897
+				get_prefix(&addr, *argv, AF_INET6);
a4b897
+				memcpy(&daddr, addr.data, addr.bytelen);
a4b897
+			}
a4b897
+		} else if (!matches(*argv, "local")) {
a4b897
+			NEXT_ARG();
a4b897
+			if (!strcmp(*argv, "any")) {
a4b897
+				fprintf(stderr, "invalid value for \"local\": \"%s\"\n", *argv);
a4b897
+				exit(-1);
a4b897
+			} else {
a4b897
+				inet_prefix addr;
a4b897
+				get_prefix(&addr, *argv, AF_INET6);
a4b897
+				memcpy(&saddr, addr.data, addr.bytelen);
a4b897
+			}
a4b897
+		} else if (!matches(*argv, "dev")) {
a4b897
+			NEXT_ARG();
a4b897
+			link = if_nametoindex(*argv);
a4b897
+			if (link == 0)
a4b897
+				exit(-1);
a4b897
+		} else
a4b897
+			usage();
a4b897
+		argc--; argv++;
a4b897
+	}
a4b897
+
a4b897
+	addattr32(n, 1024, IFLA_VTI_IKEY, ikey);
a4b897
+	addattr32(n, 1024, IFLA_VTI_OKEY, okey);
a4b897
+	addattr_l(n, 1024, IFLA_VTI_LOCAL, &saddr, sizeof(saddr));
a4b897
+	addattr_l(n, 1024, IFLA_VTI_REMOTE, &daddr, sizeof(daddr));
a4b897
+	if (link)
a4b897
+		addattr32(n, 1024, IFLA_VTI_LINK, link);
a4b897
+
a4b897
+	return 0;
a4b897
+}
a4b897
+
a4b897
+static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
a4b897
+{
a4b897
+	char s1[1024];
a4b897
+	char s2[64];
a4b897
+	const char *local = "any";
a4b897
+	const char *remote = "any";
a4b897
+	struct in6_addr saddr;
a4b897
+	struct in6_addr daddr;
a4b897
+
a4b897
+	if (!tb)
a4b897
+		return;
a4b897
+
a4b897
+	if (tb[IFLA_VTI_REMOTE]) {
a4b897
+		memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr));
a4b897
+
a4b897
+		remote = format_host(AF_INET6, 16, &daddr, s1, sizeof(s1));
a4b897
+	}
a4b897
+
a4b897
+	fprintf(f, "remote %s ", remote);
a4b897
+
a4b897
+	if (tb[IFLA_VTI_LOCAL]) {
a4b897
+		memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr));
a4b897
+
a4b897
+		local = format_host(AF_INET6, 16, &saddr, s1, sizeof(s1));
a4b897
+	}
a4b897
+
a4b897
+	fprintf(f, "local %s ", local);
a4b897
+
a4b897
+	if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) {
a4b897
+		unsigned link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]);
a4b897
+		const char *n = if_indextoname(link, s2);
a4b897
+
a4b897
+		if (n)
a4b897
+			fprintf(f, "dev %s ", n);
a4b897
+		else
a4b897
+			fprintf(f, "dev %u ", link);
a4b897
+	}
a4b897
+
a4b897
+	if (tb[IFLA_VTI_IKEY]) {
a4b897
+		inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2));
a4b897
+		fprintf(f, "ikey %s ", s2);
a4b897
+	}
a4b897
+
a4b897
+	if (tb[IFLA_VTI_OKEY]) {
a4b897
+		inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2));
a4b897
+		fprintf(f, "okey %s ", s2);
a4b897
+	}
a4b897
+}
a4b897
+
a4b897
+struct link_util vti6_link_util = {
a4b897
+	.id = "vti6",
a4b897
+	.maxattr = IFLA_VTI_MAX,
a4b897
+	.parse_opt = vti6_parse_opt,
a4b897
+	.print_opt = vti6_print_opt,
a4b897
+};
a4b897
-- 
a4b897
1.8.3.1
a4b897