This file is indexed.

/usr/share/gocode/src/github.com/docker/libnetwork/drivers/solaris/bridge/port_mapping.go is in golang-github-docker-libnetwork-dev 0.8.0-dev.2+git20170202.599.45b4086-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// +build solaris

package bridge

import (
	"bytes"
	"errors"
	"fmt"
	"net"
	"os"
	"os/exec"

	"github.com/Sirupsen/logrus"
	"github.com/docker/libnetwork/types"
)

var (
	defaultBindingIP = net.IPv4(0, 0, 0, 0)
)

const (
	maxAllocatePortAttempts = 10
)

func addPFRules(epid, bindIntf string, bs []types.PortBinding) {
	var id string

	if len(epid) > 12 {
		id = epid[:12]
	} else {
		id = epid
	}

	fname := "/var/lib/docker/network/files/pf." + id

	f, err := os.OpenFile(fname,
		os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
	if err != nil {
		logrus.Warn("cannot open temp pf file")
		return
	}
	for _, b := range bs {
		r := fmt.Sprintf(
			"pass in on %s proto %s from any to (%s) "+
				"port %d rdr-to %s port %d\n", bindIntf,
			b.Proto.String(), bindIntf, b.HostPort,
			b.IP.String(), b.Port)
		_, err = f.WriteString(r)
		if err != nil {
			logrus.Warnf("cannot write firewall rules to %s: %v", fname, err)
		}
	}
	f.Close()

	anchor := fmt.Sprintf("_auto/docker/ep%s", id)
	err = exec.Command("/usr/sbin/pfctl", "-a", anchor, "-f", fname).Run()
	if err != nil {
		logrus.Warnf("failed to add firewall rules: %v", err)
	}
	os.Remove(fname)
}

func removePFRules(epid string) {
	var id string

	if len(epid) > 12 {
		id = epid[:12]
	} else {
		id = epid
	}

	anchor := fmt.Sprintf("_auto/docker/ep%s", id)
	err := exec.Command("/usr/sbin/pfctl", "-a", anchor, "-F", "all").Run()
	if err != nil {
		logrus.Warnf("failed to remove firewall rules: %v", err)
	}
}

func (n *bridgeNetwork) allocatePorts(ep *bridgeEndpoint, bindIntf string, reqDefBindIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
	if ep.extConnConfig == nil || ep.extConnConfig.PortBindings == nil {
		return nil, nil
	}

	defHostIP := defaultBindingIP
	if reqDefBindIP != nil {
		defHostIP = reqDefBindIP
	}

	bs, err := n.allocatePortsInternal(ep.extConnConfig.PortBindings, bindIntf, ep.addr.IP, defHostIP, ulPxyEnabled)
	if err != nil {
		return nil, err
	}

	// Add PF rules for port bindings, if any
	if len(bs) > 0 {
		addPFRules(ep.id, bindIntf, bs)
	}

	return bs, err
}

func (n *bridgeNetwork) allocatePortsInternal(bindings []types.PortBinding, bindIntf string, containerIP, defHostIP net.IP, ulPxyEnabled bool) ([]types.PortBinding, error) {
	bs := make([]types.PortBinding, 0, len(bindings))
	for _, c := range bindings {
		b := c.GetCopy()
		if err := n.allocatePort(&b, containerIP, defHostIP); err != nil {
			// On allocation failure,release previously
			// allocated ports. On cleanup error, just log
			// a warning message
			if cuErr := n.releasePortsInternal(bs); cuErr != nil {
				logrus.Warnf("Upon allocation failure "+
					"for %v, failed to clear previously "+
					"allocated port bindings: %v", b, cuErr)
			}
			return nil, err
		}
		bs = append(bs, b)
	}
	return bs, nil
}

func (n *bridgeNetwork) allocatePort(bnd *types.PortBinding, containerIP, defHostIP net.IP) error {
	var (
		host net.Addr
		err  error
	)

	// Store the container interface address in the operational binding
	bnd.IP = containerIP

	// Adjust the host address in the operational binding
	if len(bnd.HostIP) == 0 {
		bnd.HostIP = defHostIP
	}

	// Adjust HostPortEnd if this is not a range.
	if bnd.HostPortEnd == 0 {
		bnd.HostPortEnd = bnd.HostPort
	}

	// Construct the container side transport address
	container, err := bnd.ContainerAddr()
	if err != nil {
		return err
	}

	// Try up to maxAllocatePortAttempts times to get a port that's
	// not already allocated.
	for i := 0; i < maxAllocatePortAttempts; i++ {
		if host, err = n.portMapper.MapRange(container, bnd.HostIP,
			int(bnd.HostPort), int(bnd.HostPortEnd), false); err == nil {
			break
		}
		// There is no point in immediately retrying to map an
		// explicitly chosen port.
		if bnd.HostPort != 0 {
			logrus.Warnf(
				"Failed to allocate and map port %d-%d: %s",
				bnd.HostPort, bnd.HostPortEnd, err)
			break
		}
		logrus.Warnf("Failed to allocate and map port: %s, retry: %d",
			err, i+1)
	}
	if err != nil {
		return err
	}

	// Save the host port (regardless it was or not specified in the
	// binding)
	switch netAddr := host.(type) {
	case *net.TCPAddr:
		bnd.HostPort = uint16(host.(*net.TCPAddr).Port)
		return nil
	case *net.UDPAddr:
		bnd.HostPort = uint16(host.(*net.UDPAddr).Port)
		return nil
	default:
		// For completeness
		return ErrUnsupportedAddressType(fmt.Sprintf("%T", netAddr))
	}
}

func (n *bridgeNetwork) releasePorts(ep *bridgeEndpoint) error {
	err := n.releasePortsInternal(ep.portMapping)
	if err != nil {
		return nil
	}

	// remove rules if there are any port mappings
	if len(ep.portMapping) > 0 {
		removePFRules(ep.id)
	}

	return nil

}

func (n *bridgeNetwork) releasePortsInternal(bindings []types.PortBinding) error {
	var errorBuf bytes.Buffer

	// Attempt to release all port bindings, do not stop on failure
	for _, m := range bindings {
		if err := n.releasePort(m); err != nil {
			errorBuf.WriteString(
				fmt.Sprintf(
					"\ncould not release %v because of %v",
					m, err))
		}
	}

	if errorBuf.Len() != 0 {
		return errors.New(errorBuf.String())
	}
	return nil
}

func (n *bridgeNetwork) releasePort(bnd types.PortBinding) error {
	// Construct the host side transport address
	host, err := bnd.HostAddr()
	if err != nil {
		return err
	}
	return n.portMapper.Unmap(host)
}