|
|
|
@ -7,7 +7,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
|
dhcp "github.com/krolaw/dhcp4"
|
|
|
|
|
"github.com/krolaw/dhcp4"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
@ -51,7 +51,7 @@ func main() {
|
|
|
|
|
DisableTimestamp: true,
|
|
|
|
|
})
|
|
|
|
|
if *logFile != "" {
|
|
|
|
|
if f, err := os.OpenFile(*logFile, os.O_WRONLY | os.O_CREATE, 0755); err != nil {
|
|
|
|
|
if f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE, 0755); err != nil {
|
|
|
|
|
log.Error("Error opening logFile ", *logFile)
|
|
|
|
|
} else {
|
|
|
|
|
log.SetOutput(f)
|
|
|
|
@ -156,8 +156,12 @@ func pipeForward(prefix string) {
|
|
|
|
|
|
|
|
|
|
// Forward original frame to other plug
|
|
|
|
|
if Passthrough {
|
|
|
|
|
writer.Write(frameLength)
|
|
|
|
|
writer.Write(frameBytes)
|
|
|
|
|
if _, err := writer.Write(frameLength); err != nil {
|
|
|
|
|
log.Error("Error forwarding original packet length", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := writer.Write(frameBytes); err != nil {
|
|
|
|
|
log.Error("Error forwarding original packet data", err)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -180,8 +184,12 @@ func pipeForward(prefix string) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Forward modified frame to other plug
|
|
|
|
|
writer.Write(newFrameLength)
|
|
|
|
|
writer.Write(newFrameBytes)
|
|
|
|
|
if _, err := writer.Write(newFrameLength); err != nil {
|
|
|
|
|
log.Error("Error forwarding packet length", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := writer.Write(newFrameBytes); err != nil {
|
|
|
|
|
log.Error("Error forwarding packet data", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -211,7 +219,7 @@ func filterIP(prefix string, dst interface{}, src interface{}, context gopacket.
|
|
|
|
|
return
|
|
|
|
|
} else if prefix == cmd.Out {
|
|
|
|
|
if isIp {
|
|
|
|
|
if!ip.IsGlobalUnicast() {
|
|
|
|
|
if !ip.IsGlobalUnicast() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
OldIP = *ip
|
|
|
|
@ -274,19 +282,19 @@ func filterMAC(prefix string, dst interface{}, src interface{}, context gopacket
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleDHCP(content []byte, srcMAC net.HardwareAddr, prefix string) {
|
|
|
|
|
req := dhcp.Packet(content)
|
|
|
|
|
req := dhcp4.Packet(content)
|
|
|
|
|
if req.HLen() > 16 { // Invalid size
|
|
|
|
|
log.Error(prefix, "Invalid DHCP size")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
options := req.ParseOptions()
|
|
|
|
|
var reqType dhcp.MessageType
|
|
|
|
|
if t := options[dhcp.OptionDHCPMessageType]; len(t) != 1 {
|
|
|
|
|
var reqType dhcp4.MessageType
|
|
|
|
|
if t := options[dhcp4.OptionDHCPMessageType]; len(t) != 1 {
|
|
|
|
|
log.Error(prefix, "Invalid DHCP message type")
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
reqType = dhcp.MessageType(t[0])
|
|
|
|
|
if reqType < dhcp.Discover || reqType > dhcp.Inform {
|
|
|
|
|
reqType = dhcp4.MessageType(t[0])
|
|
|
|
|
if reqType < dhcp4.Discover || reqType > dhcp4.Inform {
|
|
|
|
|
log.Error(prefix, "Invalid DHCP message type: ", reqType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@ -294,13 +302,13 @@ func handleDHCP(content []byte, srcMAC net.HardwareAddr, prefix string) {
|
|
|
|
|
log.Debug(prefix, "DHCP message registered: ", reqType)
|
|
|
|
|
if prefix == cmd.Out {
|
|
|
|
|
switch reqType {
|
|
|
|
|
case dhcp.Discover:
|
|
|
|
|
case dhcp4.Discover:
|
|
|
|
|
if OldIP == nil {
|
|
|
|
|
log.Fatal(prefix, "DHCPDISCOVER but not previous address is known")
|
|
|
|
|
}
|
|
|
|
|
sendDHCPReply(req, dhcp.Offer, OldIP, options, srcMAC)
|
|
|
|
|
case dhcp.Request:
|
|
|
|
|
reqIP := net.IP(options[dhcp.OptionRequestedIPAddress])
|
|
|
|
|
sendDHCPReply(req, dhcp4.Offer, OldIP, options, srcMAC)
|
|
|
|
|
case dhcp4.Request:
|
|
|
|
|
reqIP := net.IP(options[dhcp4.OptionRequestedIPAddress])
|
|
|
|
|
if reqIP == nil {
|
|
|
|
|
reqIP = req.CIAddr()
|
|
|
|
|
}
|
|
|
|
@ -308,34 +316,37 @@ func handleDHCP(content []byte, srcMAC net.HardwareAddr, prefix string) {
|
|
|
|
|
log.Error(prefix, "Invalid IP requested in DHCP: ", reqIP)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sendDHCPReply(req, dhcp.ACK, reqIP, options, srcMAC)
|
|
|
|
|
sendDHCPReply(req, dhcp4.ACK, reqIP, options, srcMAC)
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: talk to dhcp server
|
|
|
|
|
// TODO: talk to DHCP server
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sendDHCPReply creates a response dhcp packet and sends it
|
|
|
|
|
func sendDHCPReply(req dhcp.Packet, mt dhcp.MessageType, lease net.IP, reqOpt dhcp.Options, dstMAC net.HardwareAddr) {
|
|
|
|
|
// sendDHCPReply creates a response DHCP packet and sends it
|
|
|
|
|
func sendDHCPReply(req dhcp4.Packet, mt dhcp4.MessageType, lease net.IP, reqOpt dhcp4.Options, dstMAC net.HardwareAddr) {
|
|
|
|
|
log.Info("Sending DHCP response: ", mt, lease)
|
|
|
|
|
// Getting the options
|
|
|
|
|
serverIP := []byte{10, 0, 0, 1} // TODO: serverIP
|
|
|
|
|
serverMAC := util.GenerateMac("") // TODO: server MAC
|
|
|
|
|
opt := dhcp.Options{
|
|
|
|
|
dhcp.OptionSubnetMask: []byte{255, 255, 255, 0}, // TODO: subnet mask
|
|
|
|
|
dhcp.OptionRouter: serverIP, // TODO: Presuming Server is also your router
|
|
|
|
|
dhcp.OptionDomainNameServer: serverIP, // TODO: Presuming Server is also your DNS server
|
|
|
|
|
}.SelectOrderOrAll(reqOpt[dhcp.OptionParameterRequestList])
|
|
|
|
|
serverIP := []byte{10, 0, 0, 1} // TODO: serverIP
|
|
|
|
|
serverMAC := util.GenerateMac("") // TODO: server MAC
|
|
|
|
|
opt := dhcp4.Options{
|
|
|
|
|
dhcp4.OptionSubnetMask: []byte{255, 255, 255, 0}, // TODO: subnet mask
|
|
|
|
|
dhcp4.OptionRouter: serverIP, // TODO: Presuming Server is also your router
|
|
|
|
|
dhcp4.OptionDomainNameServer: serverIP, // TODO: Presuming Server is also your DNS server
|
|
|
|
|
}.SelectOrderOrAll(reqOpt[dhcp4.OptionParameterRequestList])
|
|
|
|
|
|
|
|
|
|
// Creating the full packet layer by layer
|
|
|
|
|
dhcplayer := dhcp.ReplyPacket(req, mt, serverIP, lease, 24 * time.Hour, opt)
|
|
|
|
|
buf := gopacket.NewSerializeBuffer()
|
|
|
|
|
opts := gopacket.SerializeOptions{
|
|
|
|
|
ComputeChecksums: true,
|
|
|
|
|
FixLengths: true,
|
|
|
|
|
|
|
|
|
|
FixLengths: true,
|
|
|
|
|
}
|
|
|
|
|
eth := layers.Ethernet{
|
|
|
|
|
SrcMAC: serverMAC,
|
|
|
|
|
DstMAC: dstMAC,
|
|
|
|
|
EthernetType: layers.EthernetTypeIPv4,
|
|
|
|
|
}
|
|
|
|
|
ipv4 := layers.IPv4{
|
|
|
|
|
Version: 4,
|
|
|
|
@ -348,18 +359,12 @@ func sendDHCPReply(req dhcp.Packet, mt dhcp.MessageType, lease net.IP, reqOpt dh
|
|
|
|
|
SrcPort: 67,
|
|
|
|
|
DstPort: 68,
|
|
|
|
|
}
|
|
|
|
|
udp.SetNetworkLayerForChecksum(&ipv4)
|
|
|
|
|
err := gopacket.SerializeLayers(buf, opts,
|
|
|
|
|
&layers.Ethernet{
|
|
|
|
|
SrcMAC: serverMAC,
|
|
|
|
|
DstMAC: dstMAC,
|
|
|
|
|
EthernetType: layers.EthernetTypeIPv4,
|
|
|
|
|
},
|
|
|
|
|
&ipv4,
|
|
|
|
|
&udp,
|
|
|
|
|
gopacket.Payload(dhcplayer))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
dhcp := dhcp4.ReplyPacket(req, mt, serverIP, lease, 24*time.Hour, opt)
|
|
|
|
|
if err := udp.SetNetworkLayerForChecksum(&ipv4); err != nil {
|
|
|
|
|
log.Error("Error building DHCP response", err)
|
|
|
|
|
}
|
|
|
|
|
if err := gopacket.SerializeLayers(buf, opts, ð, &ipv4, &udp, gopacket.Payload(dhcp)); err != nil {
|
|
|
|
|
log.Error("Error serializing DHCP response", err)
|
|
|
|
|
}
|
|
|
|
|
packetData := buf.Bytes()
|
|
|
|
|
|
|
|
|
@ -367,11 +372,10 @@ func sendDHCPReply(req dhcp.Packet, mt dhcp.MessageType, lease net.IP, reqOpt dh
|
|
|
|
|
packetLength := make([]byte, 2)
|
|
|
|
|
binary.BigEndian.PutUint16(packetLength, uint16(len(packetData)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
util.WriteBinary(fmt.Sprintf("/tmp/dhcp_%d.dat", time.Now().Unix()), packetData)
|
|
|
|
|
util.WritePcap(fmt.Sprintf("/tmp/dhcp_%d.pcap", time.Now().Unix()), packetData)
|
|
|
|
|
util.WritePcap(fmt.Sprintf("/tmp/dhcp_%d2.pcap", time.Now().Unix()), packetData[36:])
|
|
|
|
|
|
|
|
|
|
VmWriter.Write(packetLength)
|
|
|
|
|
VmWriter.Write(packetData)
|
|
|
|
|
}
|
|
|
|
|
if _, err := VmWriter.Write(packetLength); err != nil {
|
|
|
|
|
log.Error("Error writing DHCP response length", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := VmWriter.Write(packetData); err != nil {
|
|
|
|
|
log.Error("Error writing DHCP response data", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|