summaryrefslogtreecommitdiffhomepage
path: root/test/iptables/iptables_util.go
blob: 2a00677be738708e1619771dc9f2ebfdb91faf8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package iptables

import (
	"fmt"
	"net"
	"os/exec"
	"time"

	"gvisor.dev/gvisor/pkg/test/testutil"
)

const iptablesBinary = "iptables"
const localIP = "127.0.0.1"

// filterTable calls `iptables -t filter` with the given args.
func filterTable(args ...string) error {
	return tableCmd("filter", args)
}

// natTable calls `iptables -t nat` with the given args.
func natTable(args ...string) error {
	return tableCmd("nat", args)
}

func tableCmd(table string, args []string) error {
	args = append([]string{"-t", table}, args...)
	cmd := exec.Command(iptablesBinary, args...)
	if out, err := cmd.CombinedOutput(); err != nil {
		return fmt.Errorf("error running iptables with args %v\nerror: %v\noutput: %s", args, err, string(out))
	}
	return nil
}

// filterTableRules is like filterTable, but runs multiple iptables commands.
func filterTableRules(argsList [][]string) error {
	return tableRules("filter", argsList)
}

// natTableRules is like natTable, but runs multiple iptables commands.
func natTableRules(argsList [][]string) error {
	return tableRules("nat", argsList)
}

func tableRules(table string, argsList [][]string) error {
	for _, args := range argsList {
		if err := tableCmd(table, args); err != nil {
			return err
		}
	}
	return nil
}

// listenUDP listens on a UDP port and returns the value of net.Conn.Read() for
// the first read on that port.
func listenUDP(port int, timeout time.Duration) error {
	localAddr := net.UDPAddr{
		Port: port,
	}
	conn, err := net.ListenUDP(network, &localAddr)
	if err != nil {
		return err
	}
	defer conn.Close()
	conn.SetDeadline(time.Now().Add(timeout))
	_, err = conn.Read([]byte{0})
	return err
}

// sendUDPLoop sends 1 byte UDP packets repeatedly to the IP and port specified
// over a duration.
func sendUDPLoop(ip net.IP, port int, duration time.Duration) error {
	// Send packets for a few seconds.
	remote := net.UDPAddr{
		IP:   ip,
		Port: port,
	}
	conn, err := net.DialUDP(network, nil, &remote)
	if err != nil {
		return err
	}
	defer conn.Close()

	to := time.After(duration)
	for timedOut := false; !timedOut; {
		// This may return an error (connection refused) if the remote
		// hasn't started listening yet or they're dropping our
		// packets. So we ignore Write errors and depend on the remote
		// to report a failure if it doesn't get a packet it needs.
		conn.Write([]byte{0})
		select {
		case <-to:
			timedOut = true
		default:
			time.Sleep(200 * time.Millisecond)
		}
	}

	return nil
}

// listenTCP listens for connections on a TCP port.
func listenTCP(port int, timeout time.Duration) error {
	localAddr := net.TCPAddr{
		Port: port,
	}

	// Starts listening on port.
	lConn, err := net.ListenTCP("tcp4", &localAddr)
	if err != nil {
		return err
	}
	defer lConn.Close()

	// Accept connections on port.
	lConn.SetDeadline(time.Now().Add(timeout))
	conn, err := lConn.AcceptTCP()
	if err != nil {
		return err
	}
	conn.Close()
	return nil
}

// connectTCP connects to the given IP and port from an ephemeral local address.
func connectTCP(ip net.IP, port int, timeout time.Duration) error {
	contAddr := net.TCPAddr{
		IP:   ip,
		Port: port,
	}
	// The container may not be listening when we first connect, so retry
	// upon error.
	callback := func() error {
		conn, err := net.DialTimeout("tcp", contAddr.String(), timeout)
		if conn != nil {
			conn.Close()
		}
		return err
	}
	if err := testutil.Poll(callback, timeout); err != nil {
		return fmt.Errorf("timed out waiting to connect IP, most recent error: %v", err)
	}

	return nil
}

// localAddrs returns a list of local network interface addresses.
func localAddrs() ([]string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	addrStrs := make([]string, 0, len(addrs))
	for _, addr := range addrs {
		addrStrs = append(addrStrs, addr.String())
	}
	return addrStrs, nil
}