2015-09-07 13:33:28 -04:00
|
|
|
package ns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-06-15 16:07:52 -04:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2016-05-16 14:51:40 -04:00
|
|
|
"sync"
|
2015-09-07 13:33:28 -04:00
|
|
|
"syscall"
|
2016-11-15 14:42:47 -05:00
|
|
|
"time"
|
2015-09-07 13:33:28 -04:00
|
|
|
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-05-16 14:51:40 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
var (
|
|
|
|
initNs netns.NsHandle
|
|
|
|
initNl *netlink.Handle
|
|
|
|
initOnce sync.Once
|
2016-11-15 14:42:47 -05:00
|
|
|
// NetlinkSocketsTimeout represents the default timeout duration for the sockets
|
|
|
|
NetlinkSocketsTimeout = 3 * time.Second
|
2016-05-16 14:51:40 -04:00
|
|
|
)
|
2015-09-07 13:33:28 -04:00
|
|
|
|
|
|
|
// Init initializes a new network namespace
|
|
|
|
func Init() {
|
|
|
|
var err error
|
|
|
|
initNs, err = netns.Get()
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("could not get initial namespace: %v", err)
|
2015-09-07 13:33:28 -04:00
|
|
|
}
|
2016-06-15 16:07:52 -04:00
|
|
|
initNl, err = netlink.NewHandle(getSupportedNlFamilies()...)
|
2016-05-16 14:51:40 -04:00
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("could not create netlink handle on initial namespace: %v", err)
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
2016-11-15 14:42:47 -05:00
|
|
|
err = initNl.SetSocketTimeout(NetlinkSocketsTimeout)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to set the timeout on the default netlink handle sockets: %v", err)
|
|
|
|
}
|
2015-09-07 13:33:28 -04:00
|
|
|
}
|
|
|
|
|
2016-05-22 22:55:17 -04:00
|
|
|
// ParseHandlerInt transforms the namespace handler into an integer
|
2015-09-07 13:33:28 -04:00
|
|
|
func ParseHandlerInt() int {
|
2016-05-16 14:51:40 -04:00
|
|
|
return int(getHandler())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandler returns the namespace handler
|
|
|
|
func getHandler() netns.NsHandle {
|
|
|
|
initOnce.Do(Init)
|
|
|
|
return initNs
|
2015-09-07 13:33:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLink() (string, error) {
|
|
|
|
return os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
|
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
|
|
|
|
// NlHandle returns the netlink handler
|
|
|
|
func NlHandle() *netlink.Handle {
|
|
|
|
initOnce.Do(Init)
|
|
|
|
return initNl
|
|
|
|
}
|
2016-06-15 16:07:52 -04:00
|
|
|
|
|
|
|
func getSupportedNlFamilies() []int {
|
|
|
|
fams := []int{syscall.NETLINK_ROUTE}
|
2017-04-06 15:26:08 -04:00
|
|
|
// NETLINK_XFRM test
|
2019-03-26 15:55:49 -04:00
|
|
|
if err := checkXfrmSocket(); err != nil {
|
|
|
|
logrus.Warnf("Could not load necessary modules for IPSEC rules: %v", err)
|
2017-04-06 15:26:08 -04:00
|
|
|
} else {
|
|
|
|
fams = append(fams, syscall.NETLINK_XFRM)
|
2016-06-15 16:07:52 -04:00
|
|
|
}
|
2017-04-06 15:26:08 -04:00
|
|
|
// NETLINK_NETFILTER test
|
|
|
|
if err := loadNfConntrackModules(); err != nil {
|
|
|
|
if checkNfSocket() != nil {
|
|
|
|
logrus.Warnf("Could not load necessary modules for Conntrack: %v", err)
|
|
|
|
} else {
|
|
|
|
fams = append(fams, syscall.NETLINK_NETFILTER)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fams = append(fams, syscall.NETLINK_NETFILTER)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fams
|
2016-06-15 16:07:52 -04:00
|
|
|
}
|
|
|
|
|
2016-10-10 14:56:03 -04:00
|
|
|
// API check on required xfrm modules (xfrm_user, xfrm_algo)
|
|
|
|
func checkXfrmSocket() error {
|
|
|
|
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_XFRM)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
syscall.Close(fd)
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-06 15:26:08 -04:00
|
|
|
|
|
|
|
func loadNfConntrackModules() error {
|
|
|
|
if out, err := exec.Command("modprobe", "-va", "nf_conntrack").CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("Running modprobe nf_conntrack failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
|
|
|
|
}
|
|
|
|
if out, err := exec.Command("modprobe", "-va", "nf_conntrack_netlink").CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("Running modprobe nf_conntrack_netlink failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// API check on required nf_conntrack* modules (nf_conntrack, nf_conntrack_netlink)
|
|
|
|
func checkNfSocket() error {
|
|
|
|
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_NETFILTER)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
syscall.Close(fd)
|
|
|
|
return nil
|
|
|
|
}
|