iproute2/ip
Matteo Croce 80a931d41c ip: reset netns after each command in batch mode
When creating a new netns or executing a program into an existing one,
the unshare() or setns() calls will change the current netns.
In batch mode, this can run commands on the wrong interfaces, as the
ifindex value is meaningful only in the current netns. For example, this
command fails because veth-c doesn't exists in the init netns:

    # ip -b - <<-'EOF'
        netns add client
        link add name veth-c type veth peer veth-s netns client
        addr add 192.168.2.1/24 dev veth-c
    EOF
    Cannot find device "veth-c"
    Command failed -:7

But if there are two devices with the same name in the init and new netns,
ip will build a wrong ll_map with indexes belonging to the new netns,
and will execute actions in the init netns using this wrong mapping.
This script will flush all eth0 addresses and bring it down, as it has
the same ifindex of veth0 in the new netns:

    # ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.76/24 brd 192.168.122.255 scope global dynamic eth0
           valid_lft 3598sec preferred_lft 3598sec

    # ip -b - <<-'EOF'
        netns add client
        link add name veth0 type veth peer name veth1
        link add name veth-ns type veth peer name veth0 netns client
        link set veth0 down
        address flush veth0
    EOF

    # ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN group default qlen 1000
        link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    3: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether c2:db:d0:34:13:4a brd ff:ff:ff:ff:ff:ff
    4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether ca:9d:6b:5f:5f:8f brd ff:ff:ff:ff:ff:ff
    5: veth-ns@if2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether 32:ef:22:df:51:0a brd ff:ff:ff:ff:ff:ff link-netns client

The same issue can be triggered by the netns exec subcommand with a
sligthy different script:

    # ip netns add client
    # ip -b - <<-'EOF'
        netns exec client true
        link add name veth0 type veth peer name veth1
        link add name veth-ns type veth peer name veth0 netns client
        link set veth0 down
        address flush veth0
    EOF

Fix this by adding two netns_{save,reset} functions, which are used
to get a file descriptor for the init netns, and restore it after
each batch command.
netns_save() is called before the unshare() or setns(),
while netns_restore() is called after each command.

Fixes: 0dc34c7713 ("iproute2: Add processless network namespace support")
Reviewed-and-tested-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
2019-06-10 10:42:14 -07:00
..
.gitignore Another .gitignore file. 2006-08-08 12:11:23 -07:00
Makefile ip: support for xfrm interfaces 2019-04-05 15:05:00 -07:00
ifcfg ifcfg/rtpr: convert to POSIX shell 2018-01-10 08:26:09 -08:00
ila_common.h SPDX license identifiers 2017-11-24 12:21:35 -08:00
ip.c ip: reset netns after each command in batch mode 2019-06-10 10:42:14 -07:00
ip6tunnel.c utils: Introduce and use nodev() helper routine 2018-03-11 17:58:36 -07:00
ip_common.h ip: bond: add xstats support 2019-03-15 13:58:16 -07:00
ipaddress.c ipaddress: print error message on stderr 2019-03-15 08:30:26 -07:00
ipaddrlabel.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
ipfou.c ip fou: Support binding FOU ports 2019-04-22 11:42:54 -07:00
ipila.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
ipl2tp.c use print_{,h}hu instead of print_uint when format specifier is %{,h}hu 2019-02-10 19:00:59 -08:00
iplink.c iplink: don't try to get ll addr len when creating an iface 2019-05-30 11:03:20 -07:00
iplink_bond.c ip: bond: add xstats support 2019-03-15 13:58:16 -07:00
iplink_bond_slave.c ip: bond: add xstats support 2019-03-15 13:58:16 -07:00
iplink_bridge.c iplink: bridge: add support for vlan_stats_per_port 2019-04-21 06:47:39 -07:00
iplink_bridge_slave.c ip: bridge: add mcast to unicast config flag 2019-03-22 09:44:49 -07:00
iplink_can.c ip: iplink_can.c: fix json formatting 2018-12-07 09:22:29 -08:00
iplink_dummy.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
iplink_geneve.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
iplink_hsr.c ip: iplink_hsr.c: add json output support 2017-08-17 18:02:40 -07:00
iplink_ifb.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
iplink_ipoib.c ip: iplink_ipoib.c: add json output support 2017-08-17 18:02:41 -07:00
iplink_ipvlan.c iplink: add ipvtap support 2018-09-20 17:53:56 -07:00
iplink_macvlan.c ip: link_macvlan.c: add json output support 2017-08-17 18:02:41 -07:00
iplink_netdevsim.c ip: link: add support for netdevsim device type 2018-01-02 20:46:19 -08:00
iplink_nlmon.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
iplink_rmnet.c ip: add rmnet initial support 2018-06-15 11:15:14 -07:00
iplink_team.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
iplink_vcan.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
iplink_vlan.c iplink_vlan: add support for VLAN bridge binding flag 2019-04-26 11:12:58 -07:00
iplink_vrf.c iplink_vrf: Save device index from response for return code 2018-06-01 15:45:09 -04:00
iplink_vxcan.c iplink: Perform most of request buffer setups and checks in iplink_parse() 2018-03-11 17:59:03 -07:00
iplink_vxlan.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
iplink_xdp.c iplink: add support for reporting multiple XDP programs 2018-07-15 13:10:03 -07:00
iplink_xstats.c ip: xstats: add json output support 2019-03-15 13:55:57 -07:00
ipmacsec.c ipmacsec: fix warning on 32bit platform 2018-12-10 13:47:58 -08:00
ipmaddr.c lib: introduce print_nl 2018-09-11 08:29:33 -07:00
ipmonitor.c ipmonitor: make local variable static 2018-11-19 11:42:44 -08:00
ipmroute.c ip: mroute: add fflush to print_mroute 2019-04-29 15:04:18 -07:00
ipneigh.c ipneigh: Print neighbour offload indication 2019-04-21 06:23:23 -07:00
ipnetconf.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
ipnetns.c ip: reset netns after each command in batch mode 2019-06-10 10:42:14 -07:00
ipntable.c fix print_0xhex on 32 bit 2018-12-10 14:20:32 -08:00
ipprefix.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
iproute.c ip route: get: print JSON output when -j is given 2019-03-19 09:50:01 -07:00
iproute_lwtunnel.c iproute: Set ip/ip6 lwtunnel flags 2019-01-25 09:17:27 +13:00
iprule.c iprule: always print realms keyword for rule 2019-04-24 15:06:15 -07:00
ipseg6.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
iptoken.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
iptunnel.c utils: Introduce and use nodev() helper routine 2018-03-11 17:58:36 -07:00
iptuntap.c fix print_0xhex on 32 bit 2018-12-10 14:20:32 -08:00
ipvrf.c ip address: Split ip_linkaddr_list into link and addr functions 2018-12-27 15:35:14 -08:00
ipxfrm.c ip xfrm: support setting/printing XFRMA_IF_ID attribute in states/policies 2019-04-11 15:26:43 -07:00
link_gre.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
link_gre6.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
link_ip6tnl.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
link_iptnl.c ip: add missing space after 'external' in detailed mode 2019-04-17 16:26:31 -07:00
link_veth.c iplink: Perform most of request buffer setups and checks in iplink_parse() 2018-03-11 17:59:03 -07:00
link_vti.c fix print_0xhex on 32 bit 2018-12-10 14:20:32 -08:00
link_vti6.c fix print_0xhex on 32 bit 2018-12-10 14:20:32 -08:00
link_xfrm.c ip: support for xfrm interfaces 2019-04-05 15:05:00 -07:00
routef mark shell scripts +x 2018-01-10 08:23:49 -08:00
routel mark shell scripts +x 2018-01-10 08:23:49 -08:00
rtm_map.c drop unneeded include of syslog.h 2017-11-12 16:22:36 -08:00
rtmon.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
rtpr ifcfg/rtpr: convert to POSIX shell 2018-01-10 08:26:09 -08:00
static-syms.c SPDX license identifiers 2017-11-24 12:21:35 -08:00
tcp_metrics.c Tree wide: Drop sockaddr_nl arg 2018-10-22 09:43:48 -07:00
tunnel.c Include bsd/string.h only in include/utils.h 2018-11-05 08:38:32 -08:00
tunnel.h iptunnel/ip6tunnel: Use netlink to walk through tunnels list 2018-02-07 16:15:42 -08:00
xfrm.h xfrm: add option to hide keys in state output 2019-01-21 08:31:20 -08:00
xfrm_monitor.c xfrm: add option to hide keys in state output 2019-01-21 08:31:20 -08:00
xfrm_policy.c ip-xfrm: Respect family in deleteall and list commands 2019-05-06 13:32:44 -07:00
xfrm_state.c ip-xfrm: Respect family in deleteall and list commands 2019-05-06 13:32:44 -07:00