Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ require (
github.com/go-openapi/spec v0.19.8
github.com/gogo/googleapis v1.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/goombaio/namegenerator v0.0.0-20181006234301-989e774b106e
github.com/gorilla/mux v1.7.4 // indirect
github.com/insomniacslk/dhcp v0.0.0-20221215072855-de60144f33f8
github.com/krolaw/dhcp4 v0.0.0-20190909130307-a50d88189771
github.com/lithammer/dedent v1.1.0
github.com/miekg/dns v1.1.29
github.com/miekg/dns v1.1.48
github.com/mitchellh/go-homedir v1.1.0
github.com/nightlyone/lockfile v1.0.0
github.com/opencontainers/go-digest v1.0.0
Expand All @@ -52,9 +54,11 @@ require (
github.com/weaveworks/libgitops v0.0.0-20200611103311-2c871bbbbf0c
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
golang.org/x/tools v0.1.10 // indirect
golang.org/x/net v0.5.0
golang.org/x/sys v0.4.0
golang.org/x/term v0.4.0
golang.org/x/tools v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/genproto v0.0.0-20210416161957-9910b6c460de // indirect
google.golang.org/grpc v1.37.0 // indirect
gotest.tools v2.2.0+incompatible
Expand Down
84 changes: 66 additions & 18 deletions go.sum

Large diffs are not rendered by default.

64 changes: 48 additions & 16 deletions pkg/container/dhcp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"context"
"fmt"
"net"
"time"
Expand All @@ -11,6 +12,7 @@ import (
log "github.com/sirupsen/logrus"
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/constants"
internalv6 "github.com/weaveworks/ignite/pkg/container/dhcpv6"
)

var leaseDuration, _ = time.ParseDuration(constants.DHCP_INFINITE_LEASE) // Infinite lease time
Expand All @@ -34,28 +36,37 @@ func StartDHCPServers(vm *api.VM, dhcpIfaces []DHCPInterface) error {
dhcpIface.SetDNSServers(clientConfig.Servers)

go func() {
log.Infof("Starting DHCP server for interface %q (%s)\n", dhcpIface.Bridge, dhcpIface.VMIPNet.IP)
if err := dhcpIface.StartBlockingServer(); err != nil {
log.Errorf("%q DHCP server error: %v\n", dhcpIface.Bridge, err)
log.Infof("Starting DHCPv4 server for interface %q (%s)\n", dhcpIface.Bridge, dhcpIface.VMIPNet.IP)
if err := dhcpIface.StartBlockingServerV4(); err != nil {
log.Errorf("%q DHCPv4 server error: %v\n", dhcpIface.Bridge, err)
}
}()

go func() {
log.Infof("Starting DHCPv6 server for interface %q (%s)\n", dhcpIface.Bridge, dhcpIface.VMIPNet.IP)
if err := dhcpIface.StartBlockingServerV6(); err != nil {
log.Errorf("%q DHCPv6 server error: %v\n", dhcpIface.Bridge, err)
}
}()

}

return nil
}

type DHCPInterface struct {
VMIPNet *net.IPNet
GatewayIP *net.IP
VMTAP string
Bridge string
Hostname string
MACFilter string
dnsServers []byte
VMIPNet *net.IPNet
GatewayIP *net.IP
VMTAP string
Bridge string
Hostname string
MACFilter string
dnsv4Servers []byte
dnsv6Servers []byte
}

// StartBlockingServer starts a blocking DHCP server on port 67
func (i *DHCPInterface) StartBlockingServer() error {
// StartBlockingServerV4 starts a blocking DHCPv4 server on port 67
func (i *DHCPInterface) StartBlockingServerV4() error {
packetConn, err := conn.NewUDP4BoundListener(i.Bridge, ":67")
if err != nil {
return err
Expand All @@ -64,6 +75,19 @@ func (i *DHCPInterface) StartBlockingServer() error {
return dhcp.Serve(packetConn, i)
}

// StartBlockingServerV6 starts a blocking DHCPv6 server on port 547
func (i *DHCPInterface) StartBlockingServerV6() error {

interf, err := net.InterfaceByName(i.Bridge)

if err != nil {
return err
}

c := internalv6.Config{Interface: interf}
return internalv6.RunDHCPv6Server(context.TODO(), log.StandardLogger(), c)
}

// ServeDHCP responds to a DHCP request
func (i *DHCPInterface) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) dhcp.Packet {
var respMsg dhcp.MessageType
Expand All @@ -75,19 +99,19 @@ func (i *DHCPInterface) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, optio
respMsg = dhcp.ACK
}

//fmt.Printf("Packet %v, Request: %s, Options: %v, Response: %v\n", p, msgType.String(), options, respMsg.String())
log.Debugf("Packet %v, Request: %s, Options: %v, Response: %v\n", p, msgType.String(), options, respMsg.String())
if respMsg != 0 {
requestingMAC := p.CHAddr().String()
if requestingMAC == i.MACFilter {
opts := dhcp.Options{
dhcp.OptionSubnetMask: []byte(i.VMIPNet.Mask),
dhcp.OptionRouter: []byte(*i.GatewayIP),
dhcp.OptionDomainNameServer: i.dnsServers,
dhcp.OptionDomainNameServer: i.dnsv4Servers,
dhcp.OptionHostName: []byte(i.Hostname),
}

optSlice := opts.SelectOrderOrAll(options[dhcp.OptionParameterRequestList])
//fmt.Printf("Response: %s, Source %s, Client: %s, Options: %v, MAC: %s\n", respMsg.String(), i.GatewayIP.String(), i.VMIPNet.IP.String(), optSlice, requestingMAC)
log.Debugf("Response: %s, Source %s, Client: %s, Options: %v, MAC: %s\n", respMsg.String(), i.GatewayIP.String(), i.VMIPNet.IP.String(), optSlice, requestingMAC)
return dhcp.ReplyPacket(p, respMsg, *i.GatewayIP, i.VMIPNet.IP, leaseDuration, optSlice)
}
}
Expand All @@ -98,6 +122,14 @@ func (i *DHCPInterface) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, optio
// Parse the DNS servers for the DHCP server
func (i *DHCPInterface) SetDNSServers(dns []string) {
for _, server := range dns {
i.dnsServers = append(i.dnsServers, []byte(net.ParseIP(server).To4())...)

if ipv4 := net.ParseIP(server).To4(); ipv4 != nil {
i.dnsv4Servers = append(i.dnsv4Servers, ipv4...)
} else if ipv6 := net.ParseIP(server).To16(); ipv6 != nil {
i.dnsv6Servers = append(i.dnsv4Servers, ipv6...)
} else {
log.Errorf("failed to parse dns %v as either ipv4 or ipv6", server)
}

}
}
Loading