2016-06-06 21:17:10 -04:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-14 19:32:39 -04:00
|
|
|
"encoding/binary"
|
2016-06-06 21:17:10 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-07-19 14:51:29 -04:00
|
|
|
"hash/fnv"
|
2016-06-06 21:17:10 -04:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2016-07-19 21:17:30 -04:00
|
|
|
"strconv"
|
|
|
|
|
2018-10-26 22:51:34 -04:00
|
|
|
"github.com/docker/libnetwork/drivers/overlay/overlayutils"
|
2016-06-06 21:17:10 -04:00
|
|
|
"github.com/docker/libnetwork/iptables"
|
2016-06-11 19:34:54 -04:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-06-06 21:17:10 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-06 21:17:10 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-04 02:34:19 -05:00
|
|
|
r = 0xD0C4E3
|
2016-07-02 03:02:41 -04:00
|
|
|
pktExpansion = 26 // SPI(4) + SeqN(4) + IV(8) + PadLength(1) + NextHeader(1) + ICV(8)
|
2016-06-06 21:17:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
forward = iota + 1
|
|
|
|
reverse
|
|
|
|
bidir
|
|
|
|
)
|
|
|
|
|
2017-02-04 02:34:19 -05:00
|
|
|
var spMark = netlink.XfrmMark{Value: uint32(r), Mask: 0xffffffff}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
type key struct {
|
|
|
|
value []byte
|
|
|
|
tag uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *key) String() string {
|
2016-06-11 07:50:25 -04:00
|
|
|
if k != nil {
|
|
|
|
return fmt.Sprintf("(key: %s, tag: 0x%x)", hex.EncodeToString(k.value)[0:5], k.tag)
|
|
|
|
}
|
|
|
|
return ""
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type spi struct {
|
|
|
|
forward int
|
|
|
|
reverse int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *spi) String() string {
|
|
|
|
return fmt.Sprintf("SPI(FWD: 0x%x, REV: 0x%x)", uint32(s.forward), uint32(s.reverse))
|
|
|
|
}
|
|
|
|
|
|
|
|
type encrMap struct {
|
|
|
|
nodes map[string][]*spi
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encrMap) String() string {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
for k, v := range e.nodes {
|
|
|
|
b.WriteString("\n")
|
|
|
|
b.WriteString(k)
|
|
|
|
b.WriteString(":")
|
|
|
|
b.WriteString("[")
|
|
|
|
for _, s := range v {
|
|
|
|
b.WriteString(s.String())
|
|
|
|
b.WriteString(",")
|
|
|
|
}
|
|
|
|
b.WriteString("]")
|
|
|
|
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) checkEncryption(nid string, rIP net.IP, vxlanID uint32, isLocal, add bool) error {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Debugf("checkEncryption(%.7s, %v, %d, %t)", nid, rIP, vxlanID, isLocal)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil || !n.secure {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(d.keys) == 0 {
|
|
|
|
return types.ForbiddenErrorf("encryption key is not present")
|
|
|
|
}
|
|
|
|
|
2016-07-21 18:44:12 -04:00
|
|
|
lIP := net.ParseIP(d.bindAddress)
|
|
|
|
aIP := net.ParseIP(d.advertiseAddress)
|
2016-06-06 21:17:10 -04:00
|
|
|
nodes := map[string]net.IP{}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isLocal:
|
|
|
|
if err := d.peerDbNetworkWalk(nid, func(pKey *peerKey, pEntry *peerEntry) bool {
|
2016-07-25 15:02:30 -04:00
|
|
|
if !aIP.Equal(pEntry.vtep) {
|
2016-07-21 18:44:12 -04:00
|
|
|
nodes[pEntry.vtep.String()] = pEntry.vtep
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}); err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Warnf("Failed to retrieve list of participating nodes in overlay network %.5s: %v", nid, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if len(d.network(nid).endpoints) > 0 {
|
2016-07-21 18:44:12 -04:00
|
|
|
nodes[rIP.String()] = rIP
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("List of nodes: %s", nodes)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
if add {
|
|
|
|
for _, rIP := range nodes {
|
2016-07-19 21:17:30 -04:00
|
|
|
if err := setupEncryption(lIP, aIP, rIP, vxlanID, d.secMap, d.keys); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to program network encryption between %s and %s: %v", lIP, rIP, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
if err := removeEncryption(lIP, rIP, d.secMap); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to remove network encryption between %s and %s: %v", lIP, rIP, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:17:30 -04:00
|
|
|
func setupEncryption(localIP, advIP, remoteIP net.IP, vni uint32, em *encrMap, keys []*key) error {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Programming encryption for vxlan %d between %s and %s", vni, localIP, remoteIP)
|
2016-06-06 21:17:10 -04:00
|
|
|
rIPs := remoteIP.String()
|
|
|
|
|
|
|
|
indices := make([]*spi, 0, len(keys))
|
|
|
|
|
|
|
|
err := programMangle(vni, true)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:28:36 -05:00
|
|
|
err = programInput(vni, true)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
for i, k := range keys {
|
2016-07-19 21:17:30 -04:00
|
|
|
spis := &spi{buildSPI(advIP, remoteIP, k.tag), buildSPI(remoteIP, advIP, k.tag)}
|
2016-06-06 21:17:10 -04:00
|
|
|
dir := reverse
|
|
|
|
if i == 0 {
|
|
|
|
dir = bidir
|
|
|
|
}
|
|
|
|
fSA, rSA, err := programSA(localIP, remoteIP, spis, k, dir, true)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
indices = append(indices, spis)
|
|
|
|
if i != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = programSP(fSA, rSA, true)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
em.Lock()
|
|
|
|
em.nodes[rIPs] = indices
|
|
|
|
em.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeEncryption(localIP, remoteIP net.IP, em *encrMap) error {
|
|
|
|
em.Lock()
|
|
|
|
indices, ok := em.nodes[remoteIP.String()]
|
|
|
|
em.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for i, idxs := range indices {
|
|
|
|
dir := reverse
|
|
|
|
if i == 0 {
|
|
|
|
dir = bidir
|
|
|
|
}
|
|
|
|
fSA, rSA, err := programSA(localIP, remoteIP, idxs, nil, dir, false)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
if i != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = programSP(fSA, rSA, false)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func programMangle(vni uint32, add bool) (err error) {
|
|
|
|
var (
|
2018-11-13 18:44:42 -05:00
|
|
|
p = strconv.FormatUint(uint64(overlayutils.VXLANUDPPort()), 10)
|
2016-06-06 21:17:10 -04:00
|
|
|
c = fmt.Sprintf("0>>22&0x3C@12&0xFFFFFF00=%d", int(vni)<<8)
|
2017-02-04 02:34:19 -05:00
|
|
|
m = strconv.FormatUint(uint64(r), 10)
|
2016-06-06 21:17:10 -04:00
|
|
|
chain = "OUTPUT"
|
|
|
|
rule = []string{"-p", "udp", "--dport", p, "-m", "u32", "--u32", c, "-j", "MARK", "--set-mark", m}
|
|
|
|
a = "-A"
|
|
|
|
action = "install"
|
|
|
|
)
|
|
|
|
|
|
|
|
if add == iptables.Exists(iptables.Mangle, chain, rule...) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !add {
|
|
|
|
a = "-D"
|
|
|
|
action = "remove"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = iptables.RawCombinedOutput(append([]string{"-t", string(iptables.Mangle), a, chain}, rule...)...); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("could not %s mangle rule: %v", action, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-17 14:28:36 -05:00
|
|
|
func programInput(vni uint32, add bool) (err error) {
|
|
|
|
var (
|
2018-11-13 18:44:42 -05:00
|
|
|
port = strconv.FormatUint(uint64(overlayutils.VXLANUDPPort()), 10)
|
2017-02-17 14:28:36 -05:00
|
|
|
vniMatch = fmt.Sprintf("0>>22&0x3C@12&0xFFFFFF00=%d", int(vni)<<8)
|
|
|
|
plainVxlan = []string{"-p", "udp", "--dport", port, "-m", "u32", "--u32", vniMatch, "-j"}
|
|
|
|
ipsecVxlan = append([]string{"-m", "policy", "--dir", "in", "--pol", "ipsec"}, plainVxlan...)
|
|
|
|
block = append(plainVxlan, "DROP")
|
|
|
|
accept = append(ipsecVxlan, "ACCEPT")
|
|
|
|
chain = "INPUT"
|
|
|
|
action = iptables.Append
|
|
|
|
msg = "add"
|
|
|
|
)
|
|
|
|
|
|
|
|
if !add {
|
|
|
|
action = iptables.Delete
|
|
|
|
msg = "remove"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iptables.ProgramRule(iptables.Filter, chain, action, accept); err != nil {
|
|
|
|
logrus.Errorf("could not %s input rule: %v. Please do it manually.", msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iptables.ProgramRule(iptables.Filter, chain, action, block); err != nil {
|
|
|
|
logrus.Errorf("could not %s input rule: %v. Please do it manually.", msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
func programSA(localIP, remoteIP net.IP, spi *spi, k *key, dir int, add bool) (fSA *netlink.XfrmState, rSA *netlink.XfrmState, err error) {
|
|
|
|
var (
|
|
|
|
action = "Removing"
|
2016-06-11 19:34:54 -04:00
|
|
|
xfrmProgram = ns.NlHandle().XfrmStateDel
|
2016-06-06 21:17:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if add {
|
|
|
|
action = "Adding"
|
2016-06-11 19:34:54 -04:00
|
|
|
xfrmProgram = ns.NlHandle().XfrmStateAdd
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if dir&reverse > 0 {
|
|
|
|
rSA = &netlink.XfrmState{
|
|
|
|
Src: remoteIP,
|
|
|
|
Dst: localIP,
|
|
|
|
Proto: netlink.XFRM_PROTO_ESP,
|
|
|
|
Spi: spi.reverse,
|
|
|
|
Mode: netlink.XFRM_MODE_TRANSPORT,
|
2017-02-04 02:34:19 -05:00
|
|
|
Reqid: r,
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
if add {
|
2016-07-14 19:32:39 -04:00
|
|
|
rSA.Aead = buildAeadAlgo(k, spi.reverse)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := saExists(rSA)
|
|
|
|
if err != nil {
|
|
|
|
exists = !add
|
|
|
|
}
|
|
|
|
|
|
|
|
if add != exists {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("%s: rSA{%s}", action, rSA)
|
2016-06-06 21:17:10 -04:00
|
|
|
if err := xfrmProgram(rSA); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed %s rSA{%s}: %v", action, rSA, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dir&forward > 0 {
|
|
|
|
fSA = &netlink.XfrmState{
|
|
|
|
Src: localIP,
|
|
|
|
Dst: remoteIP,
|
|
|
|
Proto: netlink.XFRM_PROTO_ESP,
|
|
|
|
Spi: spi.forward,
|
|
|
|
Mode: netlink.XFRM_MODE_TRANSPORT,
|
2017-02-04 02:34:19 -05:00
|
|
|
Reqid: r,
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
if add {
|
2016-07-14 19:32:39 -04:00
|
|
|
fSA.Aead = buildAeadAlgo(k, spi.forward)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := saExists(fSA)
|
|
|
|
if err != nil {
|
|
|
|
exists = !add
|
|
|
|
}
|
|
|
|
|
|
|
|
if add != exists {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("%s fSA{%s}", action, fSA)
|
2016-06-06 21:17:10 -04:00
|
|
|
if err := xfrmProgram(fSA); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed %s fSA{%s}: %v.", action, fSA, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func programSP(fSA *netlink.XfrmState, rSA *netlink.XfrmState, add bool) error {
|
|
|
|
action := "Removing"
|
2016-06-11 19:34:54 -04:00
|
|
|
xfrmProgram := ns.NlHandle().XfrmPolicyDel
|
2016-06-06 21:17:10 -04:00
|
|
|
if add {
|
|
|
|
action = "Adding"
|
2016-06-11 19:34:54 -04:00
|
|
|
xfrmProgram = ns.NlHandle().XfrmPolicyAdd
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2016-07-25 04:10:53 -04:00
|
|
|
// Create a congruent cidr
|
|
|
|
s := types.GetMinimalIP(fSA.Src)
|
|
|
|
d := types.GetMinimalIP(fSA.Dst)
|
|
|
|
fullMask := net.CIDRMask(8*len(s), 8*len(s))
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
fPol := &netlink.XfrmPolicy{
|
2016-07-25 04:10:53 -04:00
|
|
|
Src: &net.IPNet{IP: s, Mask: fullMask},
|
|
|
|
Dst: &net.IPNet{IP: d, Mask: fullMask},
|
2016-06-06 21:17:10 -04:00
|
|
|
Dir: netlink.XFRM_DIR_OUT,
|
|
|
|
Proto: 17,
|
|
|
|
DstPort: 4789,
|
2017-02-04 02:34:19 -05:00
|
|
|
Mark: &spMark,
|
2016-06-06 21:17:10 -04:00
|
|
|
Tmpls: []netlink.XfrmPolicyTmpl{
|
|
|
|
{
|
|
|
|
Src: fSA.Src,
|
|
|
|
Dst: fSA.Dst,
|
|
|
|
Proto: netlink.XFRM_PROTO_ESP,
|
|
|
|
Mode: netlink.XFRM_MODE_TRANSPORT,
|
|
|
|
Spi: fSA.Spi,
|
2017-02-04 02:34:19 -05:00
|
|
|
Reqid: r,
|
2016-06-06 21:17:10 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := spExists(fPol)
|
|
|
|
if err != nil {
|
|
|
|
exists = !add
|
|
|
|
}
|
|
|
|
|
|
|
|
if add != exists {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("%s fSP{%s}", action, fPol)
|
2016-06-06 21:17:10 -04:00
|
|
|
if err := xfrmProgram(fPol); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("%s fSP{%s}: %v", action, fPol, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saExists(sa *netlink.XfrmState) (bool, error) {
|
2016-06-11 19:34:54 -04:00
|
|
|
_, err := ns.NlHandle().XfrmStateGet(sa)
|
2016-06-06 21:17:10 -04:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
case syscall.ESRCH:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Error while checking for SA existence: %v", err)
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func spExists(sp *netlink.XfrmPolicy) (bool, error) {
|
2016-06-11 19:34:54 -04:00
|
|
|
_, err := ns.NlHandle().XfrmPolicyGet(sp)
|
2016-06-06 21:17:10 -04:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
case syscall.ENOENT:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Error while checking for SP existence: %v", err)
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildSPI(src, dst net.IP, st uint32) int {
|
2016-07-19 14:51:29 -04:00
|
|
|
b := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(b, st)
|
|
|
|
h := fnv.New32a()
|
|
|
|
h.Write(src)
|
|
|
|
h.Write(b)
|
|
|
|
h.Write(dst)
|
|
|
|
return int(binary.BigEndian.Uint32(h.Sum(nil)))
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2016-07-14 19:32:39 -04:00
|
|
|
func buildAeadAlgo(k *key, s int) *netlink.XfrmStateAlgo {
|
|
|
|
salt := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(salt, uint32(s))
|
|
|
|
return &netlink.XfrmStateAlgo{
|
|
|
|
Name: "rfc4106(gcm(aes))",
|
|
|
|
Key: append(k.value, salt...),
|
|
|
|
ICVLen: 64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
func (d *driver) secMapWalk(f func(string, []*spi) ([]*spi, bool)) error {
|
|
|
|
d.secMap.Lock()
|
|
|
|
for node, indices := range d.secMap.nodes {
|
|
|
|
idxs, stop := f(node, indices)
|
|
|
|
if idxs != nil {
|
|
|
|
d.secMap.nodes[node] = idxs
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.secMap.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) setKeys(keys []*key) error {
|
2017-02-04 02:34:19 -05:00
|
|
|
// Remove any stale policy, state
|
|
|
|
clearEncryptionStates()
|
2016-09-19 14:37:30 -04:00
|
|
|
// Accept the encryption keys and clear any stale encryption map
|
|
|
|
d.Lock()
|
2016-06-06 21:17:10 -04:00
|
|
|
d.keys = keys
|
2016-09-19 14:37:30 -04:00
|
|
|
d.secMap = &encrMap{nodes: map[string][]*spi{}}
|
|
|
|
d.Unlock()
|
2018-04-27 11:24:36 -04:00
|
|
|
logrus.Debugf("Initial encryption keys: %v", keys)
|
2016-06-06 21:17:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateKeys allows to add a new key and/or change the primary key and/or prune an existing key
|
|
|
|
// The primary key is the key used in transmission and will go in first position in the list.
|
|
|
|
func (d *driver) updateKeys(newKey, primary, pruneKey *key) error {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Updating Keys. New: %v, Primary: %v, Pruned: %v", newKey, primary, pruneKey)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Current: %v", d.keys)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
newIdx = -1
|
|
|
|
priIdx = -1
|
|
|
|
delIdx = -1
|
2016-07-21 18:44:12 -04:00
|
|
|
lIP = net.ParseIP(d.bindAddress)
|
2017-02-10 21:02:28 -05:00
|
|
|
aIP = net.ParseIP(d.advertiseAddress)
|
2016-06-06 21:17:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
d.Lock()
|
2018-04-27 11:24:36 -04:00
|
|
|
defer d.Unlock()
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
// add new
|
|
|
|
if newKey != nil {
|
|
|
|
d.keys = append(d.keys, newKey)
|
|
|
|
newIdx += len(d.keys)
|
|
|
|
}
|
|
|
|
for i, k := range d.keys {
|
|
|
|
if primary != nil && k.tag == primary.tag {
|
|
|
|
priIdx = i
|
|
|
|
}
|
|
|
|
if pruneKey != nil && k.tag == pruneKey.tag {
|
|
|
|
delIdx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newKey != nil && newIdx == -1) ||
|
|
|
|
(primary != nil && priIdx == -1) ||
|
|
|
|
(pruneKey != nil && delIdx == -1) {
|
2016-09-19 14:37:30 -04:00
|
|
|
return types.BadRequestErrorf("cannot find proper key indices while processing key update:"+
|
2016-06-06 21:17:10 -04:00
|
|
|
"(newIdx,priIdx,delIdx):(%d, %d, %d)", newIdx, priIdx, delIdx)
|
|
|
|
}
|
|
|
|
|
2018-04-27 11:24:36 -04:00
|
|
|
if priIdx != -1 && priIdx == delIdx {
|
|
|
|
return types.BadRequestErrorf("attempting to both make a key (index %d) primary and delete it", priIdx)
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
d.secMapWalk(func(rIPs string, spis []*spi) ([]*spi, bool) {
|
2016-07-21 18:44:12 -04:00
|
|
|
rIP := net.ParseIP(rIPs)
|
2017-02-10 21:02:28 -05:00
|
|
|
return updateNodeKey(lIP, aIP, rIP, spis, d.keys, newIdx, priIdx, delIdx), false
|
2016-06-06 21:17:10 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// swap primary
|
|
|
|
if priIdx != -1 {
|
2018-04-27 11:24:36 -04:00
|
|
|
d.keys[0], d.keys[priIdx] = d.keys[priIdx], d.keys[0]
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
// prune
|
|
|
|
if delIdx != -1 {
|
|
|
|
if delIdx == 0 {
|
|
|
|
delIdx = priIdx
|
|
|
|
}
|
|
|
|
d.keys = append(d.keys[:delIdx], d.keys[delIdx+1:]...)
|
|
|
|
}
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Updated: %v", d.keys)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************
|
2016-07-21 18:44:12 -04:00
|
|
|
* Steady state: rSA0, rSA1, rSA2, fSA1, fSP1
|
|
|
|
* Rotation --> -rSA0, +rSA3, +fSA2, +fSP2/-fSP1, -fSA1
|
|
|
|
* Steady state: rSA1, rSA2, rSA3, fSA2, fSP2
|
2016-06-06 21:17:10 -04:00
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
// Spis and keys are sorted in such away the one in position 0 is the primary
|
2017-02-10 21:02:28 -05:00
|
|
|
func updateNodeKey(lIP, aIP, rIP net.IP, idxs []*spi, curKeys []*key, newIdx, priIdx, delIdx int) []*spi {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Updating keys for node: %s (%d,%d,%d)", rIP, newIdx, priIdx, delIdx)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
spis := idxs
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Current: %v", spis)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
// add new
|
|
|
|
if newIdx != -1 {
|
|
|
|
spis = append(spis, &spi{
|
2017-02-10 21:02:28 -05:00
|
|
|
forward: buildSPI(aIP, rIP, curKeys[newIdx].tag),
|
|
|
|
reverse: buildSPI(rIP, aIP, curKeys[newIdx].tag),
|
2016-06-06 21:17:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if delIdx != -1 {
|
2016-07-21 18:44:12 -04:00
|
|
|
// -rSA0
|
2016-07-25 19:23:53 -04:00
|
|
|
programSA(lIP, rIP, spis[delIdx], nil, reverse, false)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if newIdx > -1 {
|
2016-07-25 04:10:53 -04:00
|
|
|
// +rSA2
|
2016-06-06 21:17:10 -04:00
|
|
|
programSA(lIP, rIP, spis[newIdx], curKeys[newIdx], reverse, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if priIdx > 0 {
|
2016-07-21 18:44:12 -04:00
|
|
|
// +fSA2
|
|
|
|
fSA2, _, _ := programSA(lIP, rIP, spis[priIdx], curKeys[priIdx], forward, true)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
2016-07-21 18:44:12 -04:00
|
|
|
// +fSP2, -fSP1
|
2016-07-25 04:10:53 -04:00
|
|
|
s := types.GetMinimalIP(fSA2.Src)
|
|
|
|
d := types.GetMinimalIP(fSA2.Dst)
|
|
|
|
fullMask := net.CIDRMask(8*len(s), 8*len(s))
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
fSP1 := &netlink.XfrmPolicy{
|
2016-07-25 04:10:53 -04:00
|
|
|
Src: &net.IPNet{IP: s, Mask: fullMask},
|
|
|
|
Dst: &net.IPNet{IP: d, Mask: fullMask},
|
2016-06-06 21:17:10 -04:00
|
|
|
Dir: netlink.XFRM_DIR_OUT,
|
|
|
|
Proto: 17,
|
|
|
|
DstPort: 4789,
|
2017-02-04 02:34:19 -05:00
|
|
|
Mark: &spMark,
|
2016-06-06 21:17:10 -04:00
|
|
|
Tmpls: []netlink.XfrmPolicyTmpl{
|
|
|
|
{
|
2016-07-21 18:44:12 -04:00
|
|
|
Src: fSA2.Src,
|
|
|
|
Dst: fSA2.Dst,
|
2016-06-06 21:17:10 -04:00
|
|
|
Proto: netlink.XFRM_PROTO_ESP,
|
|
|
|
Mode: netlink.XFRM_MODE_TRANSPORT,
|
2016-07-21 18:44:12 -04:00
|
|
|
Spi: fSA2.Spi,
|
2017-02-04 02:34:19 -05:00
|
|
|
Reqid: r,
|
2016-06-06 21:17:10 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Updating fSP{%s}", fSP1)
|
2016-06-11 19:34:54 -04:00
|
|
|
if err := ns.NlHandle().XfrmPolicyUpdate(fSP1); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to update fSP{%s}: %v", fSP1, err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2016-07-21 18:44:12 -04:00
|
|
|
// -fSA1
|
|
|
|
programSA(lIP, rIP, spis[0], nil, forward, false)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// swap
|
|
|
|
if priIdx > 0 {
|
|
|
|
swp := spis[0]
|
|
|
|
spis[0] = spis[priIdx]
|
|
|
|
spis[priIdx] = swp
|
|
|
|
}
|
|
|
|
// prune
|
|
|
|
if delIdx != -1 {
|
|
|
|
if delIdx == 0 {
|
|
|
|
delIdx = priIdx
|
|
|
|
}
|
|
|
|
spis = append(spis[:delIdx], spis[delIdx+1:]...)
|
|
|
|
}
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Updated: %v", spis)
|
2016-06-06 21:17:10 -04:00
|
|
|
|
|
|
|
return spis
|
|
|
|
}
|
2016-07-02 03:02:41 -04:00
|
|
|
|
|
|
|
func (n *network) maxMTU() int {
|
2016-07-23 15:29:49 -04:00
|
|
|
mtu := 1500
|
|
|
|
if n.mtu != 0 {
|
|
|
|
mtu = n.mtu
|
|
|
|
}
|
|
|
|
mtu -= vxlanEncap
|
2016-07-02 03:02:41 -04:00
|
|
|
if n.secure {
|
|
|
|
// In case of encryption account for the
|
2017-05-21 22:25:52 -04:00
|
|
|
// esp packet expansion and padding
|
2016-07-02 03:02:41 -04:00
|
|
|
mtu -= pktExpansion
|
|
|
|
mtu -= (mtu % 4)
|
|
|
|
}
|
|
|
|
return mtu
|
|
|
|
}
|
2017-02-04 02:34:19 -05:00
|
|
|
|
|
|
|
func clearEncryptionStates() {
|
|
|
|
nlh := ns.NlHandle()
|
|
|
|
spList, err := nlh.XfrmPolicyList(netlink.FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to retrieve SP list for cleanup: %v", err)
|
|
|
|
}
|
|
|
|
saList, err := nlh.XfrmStateList(netlink.FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to retrieve SA list for cleanup: %v", err)
|
|
|
|
}
|
|
|
|
for _, sp := range spList {
|
|
|
|
if sp.Mark != nil && sp.Mark.Value == spMark.Value {
|
|
|
|
if err := nlh.XfrmPolicyDel(&sp); err != nil {
|
|
|
|
logrus.Warnf("Failed to delete stale SP %s: %v", sp, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Debugf("Removed stale SP: %s", sp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, sa := range saList {
|
|
|
|
if sa.Reqid == r {
|
|
|
|
if err := nlh.XfrmStateDel(&sa); err != nil {
|
|
|
|
logrus.Warnf("Failed to delete stale SA %s: %v", sa, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Debugf("Removed stale SA: %s", sa)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|