iproute2/ip
Konstantin Shemyak cc9c1dfaee ip_tunnel: determine tunnel address family from the tunnel type
On 24.11.2015 02:26, Stephen Hemminger wrote:
> On Thu, 12 Nov 2015 21:10:08 +0000
> Konstantin Shemyak <konstantin@shemyak.com> wrote:
>
>> When creating an IP tunnel over IPv6, the address family must be passed in
>> the option, e.g.
>>
>> ip -6 tunnel add mode ip6gre local 1::1 remote 2::2
>>
>> This makes it impossible to create both IPv4 and IPv6 tunnels in one batch.
>>
>> In fact the address family option is redundant here, as each tunnel mode is
>> relevant for only one address family.
>> The patch determines whether the applicable address family is AF_INET6
>> instead of the default AF_INET and makes the "-6" option unnecessary for
>> "ip tunnel add".
>>
>> Signed-off-by: Konstantin Shemyak <konstantin@shemyak.com>
>> ---
>>   ip/iptunnel.c                          | 26 ++++++++++++++++++++++++++
>>   testsuite/tests/ip/tunnel/add_tunnel.t | 14 ++++++++++++++
>>   2 files changed, 40 insertions(+)
>>   create mode 100755 testsuite/tests/ip/tunnel/add_tunnel.t
>>
>> diff --git a/ip/iptunnel.c b/ip/iptunnel.c
>> index 78fa988..7826a37 100644
>> --- a/ip/iptunnel.c
>> +++ b/ip/iptunnel.c
>> @@ -629,8 +629,34 @@ static int do_6rd(int argc, char **argv)
>>          return tnl_6rd_ioctl(cmd, medium, &ip6rd);
>>   }
>>
>> +static int tunnel_mode_is_ipv6(char *tunnel_mode) {
>> +       char *ipv6_modes[] = {
>> +               "ipv6/ipv6", "ip6ip6",
>> +               "vti6",
>> +               "ip/ipv6", "ipv4/ipv6", "ipip6", "ip4ip6",
>> +               "ip6gre", "gre/ipv6",
>> +               "any/ipv6", "any"
>> +       };
>> +       int i;
>> +
>> +       for (i = 0; i < sizeof(ipv6_modes) / sizeof(char *); i++) {
>> +               if (strcmp(ipv6_modes[i], tunnel_mode) == 0)
>> +                       return 1;
>> +       }
>> +       return 0;
>> +}
>> +
>
> The ipv6_modes table should be static const.

Thank you for the note! attached the corrected patch.

> Also is it possible to use strstr for ipv6 and ip6 or even strchr(tunnel_mode, '6')
> to simplify this?

There is IPv6 tunnel mode 'any', and IPv4 tunnel mode 'ipv6/ip' (aka
'sit'). It looks to me that attempts to find some substring match
would not make the code much shorter, but definitely less readable.

Konstantin Shemyak.

>From 42d27db0055c3a114fe6eb86d680bef9ec098ad4 Mon Sep 17 00:00:00 2001
From: Konstantin Shemyak <konstantin@shemyak.com>
Date: Thu, 12 Nov 2015 20:52:02 +0200
Subject: [PATCH] Tunnel address family is determined from the tunnel mode

When the tunnel mode already tells the IP address family, "ip tunnel"
command determines it and does not require option "-4"/"-6" to be passed.

This makes possible creating both IPv4 and IPv6 tunnels in one batch.

Signed-off-by: Konstantin Shemyak <konstantin@shemyak.com>
2015-11-29 11:57:21 -08:00
..
.gitignore Another .gitignore file. 2006-08-08 12:11:23 -07:00
Makefile lwtunnel: Add encapsulation support to ip route 2015-10-16 16:13:22 -07:00
ifcfg Remove trailing whitespace 2006-12-05 10:10:22 -08:00
ip.c add support for brief output for link and addresses 2015-08-31 16:24:10 -07:00
ip6tunnel.c ip{,6}tunnel: put spaces around non-unary operators 2015-11-23 15:26:37 -08:00
ip_common.h ip_common.h header cleanup 2015-11-23 15:44:03 -08:00
ipaddress.c ipaddress: drop unnecessary check in ipaddr_list_flush_or_save() 2015-11-29 11:47:29 -08:00
ipaddrlabel.c ip: remove extra newlines at end-of-file 2015-10-23 15:41:58 -07:00
ipfou.c ip: remove extra newlines at end-of-file 2015-10-23 15:41:58 -07:00
ipl2tp.c l2tp: add missing newline on show output 2015-09-11 15:26:58 -07:00
iplink.c ip: allow using a device "help" (or a prefix thereof) 2015-10-07 10:35:17 +01:00
iplink_bond.c ip link: missing options in bond usage 2015-09-21 14:36:02 -07:00
iplink_bond_slave.c ip: remove extra newlines at end-of-file 2015-10-23 15:41:58 -07:00
iplink_bridge.c iplink: Add support for IFLA_BR_VLAN_PROTOCOL attribute 2015-08-31 16:35:25 -07:00
iplink_bridge_slave.c iplink: shortify printing the usage of link type 2015-08-12 09:05:57 -07:00
iplink_can.c can: Add support for CAN FD non-ISO feature 2015-02-05 10:35:24 -08:00
iplink_geneve.c geneve: add support for IPv6 link partners 2015-11-23 16:23:11 -08:00
iplink_hsr.c ip link: Shortify printing the usage of link type 2014-10-09 08:29:47 -07:00
iplink_ipoib.c ip link: Shortify printing the usage of link type 2014-10-09 08:29:47 -07:00
iplink_ipvlan.c ip link: Add ipvlan support to the iproute2/ip util 2014-12-03 09:37:37 -08:00
iplink_macvlan.c ip: macvlan: support MACVLAN_FLAG_NOPROMISC flag 2015-10-12 09:46:55 -07:00
iplink_vlan.c ip link: Shortify printing the usage of link type 2014-10-09 08:29:47 -07:00
iplink_vrf.c add support for VRF device 2015-08-23 10:14:16 -07:00
iplink_vxlan.c vxlan: Add support for remote checksum offload 2015-11-29 11:53:02 -08:00
ipmaddr.c ip: allow using a device "help" (or a prefix thereof) 2015-10-07 10:35:17 +01:00
ipmonitor.c ipmonitor: allows to monitor in several netns 2015-05-21 15:28:56 -07:00
ipmroute.c mroute: "ip mroute show" not working when "to" and/or "from" is given 2015-06-25 08:47:07 -04:00
ipneigh.c Merge branch 'master' into net-next 2015-10-16 16:03:32 -07:00
ipnetconf.c libnetlink: introduce rtnl_listen_filter_t 2015-05-21 15:28:56 -07:00
ipnetns.c ipnetns: make net namespace cache variable size 2015-08-12 08:53:31 -07:00
ipntable.c ip_common.h header cleanup 2015-11-23 15:44:03 -08:00
ipprefix.c ip: remove extra newlines at end-of-file 2015-10-23 15:41:58 -07:00
iproute.c iproute: restrict hoplimit values to be in range [0; 255] 2015-11-29 11:47:29 -08:00
iproute_lwtunnel.c lwtunnel: Add encapsulation support to ip route 2015-10-16 16:13:22 -07:00
iproute_lwtunnel.h lwtunnel: Add encapsulation support to ip route 2015-10-16 16:13:22 -07:00
iprule.c ip, realms: also allow to pass in raw realms value 2015-10-22 23:40:51 -07:00
iptoken.c iptoken: simplify iptoken_list a bit 2015-11-29 11:47:29 -08:00
iptunnel.c ip_tunnel: determine tunnel address family from the tunnel type 2015-11-29 11:57:21 -08:00
iptuntap.c ip tuntap: Added missing commands in usage 2014-09-28 16:19:31 -07:00
ipxfrm.c xfrm: remove duplicated include 2015-07-27 14:36:53 -07:00
link_gre.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
link_gre6.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
link_ip6tnl.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
link_iptnl.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
link_veth.c ip link: Shortify printing the usage of link type 2014-10-09 08:29:47 -07:00
link_vti.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
link_vti6.c libnetlink: add size argument to rtnl_talk 2015-05-27 13:00:21 -07:00
routef ip/routef lifesaver 2007-07-10 18:29:20 -07:00
routel (Logical change 1.3) 2004-04-15 20:56:59 +00:00
rtm_map.c ip, realms: also allow to pass in raw realms value 2015-10-22 23:40:51 -07:00
rtmon.c libnetlink: introduce rtnl_listen_filter_t 2015-05-21 15:28:56 -07:00
rtpr (Logical change 1.3) 2004-04-15 20:56:59 +00:00
static-syms.c Fix build when shared libraries are disabled 2013-03-13 08:29:59 -07:00
tcp_metrics.c ip: remove extra newlines at end-of-file 2015-10-23 15:41:58 -07:00
tunnel.c ip/tunnel: introduce tnl_parse_key() 2015-11-23 15:26:37 -08:00
tunnel.h ip/tunnel: introduce tnl_parse_key() 2015-11-23 15:26:37 -08:00
xfrm.h Fix FSF address in file headers 2013-12-06 15:05:07 -08:00
xfrm_monitor.c xfrm: remove duplicated include 2015-07-27 14:36:53 -07:00
xfrm_policy.c xfrm: remove duplicated include 2015-07-27 14:36:53 -07:00
xfrm_state.c xfrm: remove duplicated include 2015-07-27 14:36:53 -07:00