|
|
@ -3,9 +3,10 @@ package network |
|
|
|
import ( |
|
|
|
log "github.com/sirupsen/logrus" |
|
|
|
"regexp" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
func getV4Cidr() []string { |
|
|
|
func getV4Cidr() []string { // fetch ipv4 network range
|
|
|
|
var v4Cidr []string |
|
|
|
_, output := run("ip", "-4", "addr") |
|
|
|
for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) { |
|
|
@ -14,7 +15,7 @@ func getV4Cidr() []string { |
|
|
|
return v4Cidr |
|
|
|
} |
|
|
|
|
|
|
|
func getV6Cidr() []string { |
|
|
|
func getV6Cidr() []string { // fetch ipv6 network range
|
|
|
|
var v6Cidr []string |
|
|
|
_, output := run("ip", "-6", "addr") |
|
|
|
for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) { |
|
|
@ -23,31 +24,26 @@ func getV6Cidr() []string { |
|
|
|
return v6Cidr |
|
|
|
} |
|
|
|
|
|
|
|
func flushNetwork(dev string) { |
|
|
|
log.Info("Flush system IP configure") |
|
|
|
run("ip", "link", "set", dev, "down") |
|
|
|
run("ip", "-4", "addr", "flush", "dev", dev) |
|
|
|
run("ip", "-6", "addr", "flush", "dev", dev) |
|
|
|
run("ip", "link", "set", dev, "up") |
|
|
|
} |
|
|
|
|
|
|
|
func flushV4Network(dev string) { |
|
|
|
log.Info("Flush system IPv4 configure") |
|
|
|
run("ip", "link", "set", dev, "down") |
|
|
|
run("ip", "-4", "addr", "flush", "dev", dev) |
|
|
|
run("ip", "link", "set", dev, "up") |
|
|
|
func enableIpForward() { // enable ip forward function
|
|
|
|
log.Info("Enabled IPv4 forward") |
|
|
|
run("sysctl", "-w", "net.ipv4.ip_forward=1") |
|
|
|
log.Info("Enabled IPv6 forward") |
|
|
|
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1") |
|
|
|
} |
|
|
|
|
|
|
|
func flushV6Network(dev string) { |
|
|
|
log.Info("Flush system IPv6 configure") |
|
|
|
func flushNetwork(dev string, isV4 bool, isV6 bool) { // flush ipv4 and ipv6 network
|
|
|
|
log.Info("Flush system IP configure") |
|
|
|
run("ip", "link", "set", dev, "down") |
|
|
|
run("ip", "-6", "addr", "flush", "dev", dev) |
|
|
|
if isV4 { |
|
|
|
run("ip", "-4", "addr", "flush", "dev", dev) |
|
|
|
} |
|
|
|
if isV6 { |
|
|
|
run("ip", "-6", "addr", "flush", "dev", dev) |
|
|
|
} |
|
|
|
run("ip", "link", "set", dev, "up") |
|
|
|
} |
|
|
|
|
|
|
|
func loadV4Network(v4 *Config, dev string) { |
|
|
|
log.Info("Enabled IPv4 forward") |
|
|
|
run("sysctl", "-w", "net.ipv4.ip_forward=1") |
|
|
|
func loadV4Network(v4 *Config, dev string) { // setting up ipv4 network
|
|
|
|
log.Info("Setting up system IPv4 configure") |
|
|
|
if v4.Address != "" { |
|
|
|
run("ip", "-4", "addr", "add", v4.Address, "dev", dev) |
|
|
@ -57,9 +53,7 @@ func loadV4Network(v4 *Config, dev string) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func loadV6Network(v6 *Config, dev string) { |
|
|
|
log.Info("Enabled IPv6 forward") |
|
|
|
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1") |
|
|
|
func loadV6Network(v6 *Config, dev string) { // setting up ipv6 network
|
|
|
|
log.Info("Setting up system IPv6 configure") |
|
|
|
if v6.Address != "" { |
|
|
|
run("ip", "-6", "addr", "add", v6.Address, "dev", dev) |
|
|
@ -68,3 +62,25 @@ func loadV6Network(v6 *Config, dev string) { |
|
|
|
run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", dev) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func loadNetwork(dev string, v4 *Config, v6 *Config) { |
|
|
|
setV4 := v4.Address != "" || v4.Gateway != "" |
|
|
|
setV6 := v6.Address != "" || v6.Gateway != "" |
|
|
|
if setV4 && setV6 { // load both ipv4 and ipv6
|
|
|
|
flushNetwork(dev, true, true) |
|
|
|
loadV4Network(v4, dev) |
|
|
|
loadV6Network(v6, dev) |
|
|
|
} else if setV4 { // only load ipv4 network
|
|
|
|
flushNetwork(dev, true, false) |
|
|
|
loadV4Network(v4, dev) |
|
|
|
} else if setV6 { // only load ipv6 network
|
|
|
|
flushNetwork(dev, false, true) |
|
|
|
loadV6Network(v6, dev) |
|
|
|
} else { // skip network settings
|
|
|
|
log.Infof("Skip system IP configure") |
|
|
|
} |
|
|
|
if setV6 { |
|
|
|
log.Info("Wait 1s for IPv6 setting up") |
|
|
|
time.Sleep(time.Second) // wait for ipv6 setting up (ND protocol) -> RA should reply less than 0.5s
|
|
|
|
} |
|
|
|
} |
|
|
|