mirror of https://github.com/jetkvm/kvm.git
fix(net): stop dhcp client and release all v4 addr on linkdown (#16)
This commit fixes jetkvm/kvm#12 by disabling the udhcpc client when the link goes down, it then removes all the active IPv4 addresses from the deivce. Once the link comes back up, it re-activates the udhcpc client so it can fetch a new IPv4 address for the device. This doesn't make any changes to the IPv6 side of things yet.
This commit is contained in:
parent
0b5033f798
commit
2a99c2db9d
35
network.go
35
network.go
|
@ -6,6 +6,7 @@ import (
|
|||
"golang.org/x/net/ipv4"
|
||||
"golang.org/x/net/ipv6"
|
||||
"net"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
|
@ -25,6 +26,23 @@ type LocalIpInfo struct {
|
|||
MAC string
|
||||
}
|
||||
|
||||
// setDhcpClientState sends signals to udhcpc to change it's current mode
|
||||
// of operation. Setting active to true will force udhcpc to renew the DHCP lease.
|
||||
// Setting active to false will put udhcpc into idle mode.
|
||||
func setDhcpClientState(active bool) {
|
||||
var signal string;
|
||||
if active {
|
||||
signal = "-SIGUSR1"
|
||||
} else {
|
||||
signal = "-SIGUSR2"
|
||||
}
|
||||
|
||||
cmd := exec.Command("/usr/bin/killall", signal, "udhcpc");
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Printf("network: setDhcpClientState: failed to change udhcpc state: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkNetworkState() {
|
||||
iface, err := netlink.LinkByName("eth0")
|
||||
if err != nil {
|
||||
|
@ -47,9 +65,26 @@ func checkNetworkState() {
|
|||
fmt.Printf("failed to get addresses for eth0: %v\n", err)
|
||||
}
|
||||
|
||||
// If the link is going down, put udhcpc into idle mode.
|
||||
// If the link is coming back up, activate udhcpc and force it to renew the lease.
|
||||
if newState.Up != networkState.Up {
|
||||
setDhcpClientState(newState.Up)
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if addr.IP.To4() != nil {
|
||||
if !newState.Up && networkState.Up {
|
||||
// If the network is going down, remove all IPv4 addresses from the interface.
|
||||
fmt.Printf("network: state transitioned to down, removing IPv4 address %s\n", addr.IP.String())
|
||||
err := netlink.AddrDel(iface, &addr)
|
||||
if err != nil {
|
||||
fmt.Printf("network: failed to delete %s", addr.IP.String())
|
||||
}
|
||||
|
||||
newState.IPv4 = "..."
|
||||
} else {
|
||||
newState.IPv4 = addr.IP.String()
|
||||
}
|
||||
} else if addr.IP.To16() != nil && newState.IPv6 == "" {
|
||||
newState.IPv6 = addr.IP.String()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue