This commit is contained in:
Lucas Serven
2019-01-18 02:50:10 +01:00
commit e989f0a25f
1789 changed files with 680059 additions and 0 deletions

59
pkg/iproute/ipip.go Normal file
View File

@@ -0,0 +1,59 @@
// Copyright 2019 the Kilo 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 iproute
import (
"bytes"
"fmt"
"os/exec"
"github.com/vishvananda/netlink"
)
const (
ipipHeaderSize = 20
tunnelName = "tunl0"
)
// NewIPIP creates an IPIP interface using the base interface
// to derive the tunnel's MTU.
func NewIPIP(baseIndex int) (int, error) {
link, err := netlink.LinkByName(tunnelName)
if err != nil {
// If we failed to find the tunnel, then it probably simply does not exist.
cmd := exec.Command("ip", "tunnel", "add", tunnelName, "mode", "ipip")
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return 0, fmt.Errorf("failed to create IPIP tunnel: %s", stderr.String())
}
link, err = netlink.LinkByName(tunnelName)
if err != nil {
return 0, fmt.Errorf("failed to get tunnel device: %v", err)
}
}
base, err := netlink.LinkByIndex(baseIndex)
if err != nil {
return 0, fmt.Errorf("failed to get base device: %v", err)
}
mtu := base.Attrs().MTU - ipipHeaderSize
if err = netlink.LinkSetMTU(link, mtu); err != nil {
return 0, fmt.Errorf("failed to set tunnel MTU: %v", err)
}
return link.Attrs().Index, nil
}

70
pkg/iproute/iproute.go Normal file
View File

@@ -0,0 +1,70 @@
// Copyright 2019 the Kilo 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 iproute
import (
"fmt"
"net"
"github.com/vishvananda/netlink"
)
// RemoveInterface removes an interface.
func RemoveInterface(index int) error {
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to get link: %s", err)
}
return netlink.LinkDel(link)
}
// Set sets the interface up or down.
func Set(index int, up bool) error {
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to get link: %s", err)
}
if up {
return netlink.LinkSetUp(link)
}
return netlink.LinkSetDown(link)
}
// SetAddress sets the IP address of an interface.
func SetAddress(index int, cidr *net.IPNet) error {
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to get link: %s", err)
}
addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return err
}
l := len(addrs)
for _, addr := range addrs {
if addr.IP.Equal(cidr.IP) && addr.Mask.String() == cidr.Mask.String() {
continue
}
if err := netlink.AddrDel(link, &addr); err != nil {
return fmt.Errorf("failed to delete address: %s", err)
}
l--
}
// The only address left is the desired address, so quit.
if l == 1 {
return nil
}
return netlink.AddrReplace(link, &netlink.Addr{IPNet: cidr})
}