|
|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
|
dhcp "github.com/krolaw/dhcp4"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
@ -109,10 +110,17 @@ func pipeForward(prefix string) {
|
|
|
|
|
log.Debug("Start packet")
|
|
|
|
|
filterMAC(prefix, &frame.DstMAC, &frame.SrcMAC, frame.LayerType())
|
|
|
|
|
|
|
|
|
|
// Handle IPv6 packet
|
|
|
|
|
// Handle IPv4 packet
|
|
|
|
|
if ipv4layer := packet.Layer(layers.LayerTypeIPv4); ipv4layer != nil {
|
|
|
|
|
ipv4Packet, _ := ipv4layer.(*layers.IPv4)
|
|
|
|
|
log.Debug("IP Protocol ", ipv4Packet.Protocol)
|
|
|
|
|
|
|
|
|
|
// Handle DHCPv4 packet (based on IPv4)
|
|
|
|
|
if dhcpLayer := packet.Layer(layers.LayerTypeDHCPv4); dhcpLayer != nil && !Passthrough {
|
|
|
|
|
handleDHCP(dhcpLayer.LayerContents(), frame.DstMAC, prefix)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filterIP(prefix, &ipv4Packet.DstIP, &ipv4Packet.SrcIP, ipv4Packet.LayerType())
|
|
|
|
|
|
|
|
|
|
// Handle ICMP packet (based on IPv4)
|
|
|
|
@ -121,13 +129,6 @@ func pipeForward(prefix string) {
|
|
|
|
|
log.Debug(prefix, "ICMP Type ", icmpPacket.TypeCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle DHCP packet (based on IPv4) - drop for now
|
|
|
|
|
if dhcpLayer := packet.Layer(layers.LayerTypeDHCPv4); dhcpLayer != nil && !Passthrough {
|
|
|
|
|
//dhcpPacket, _ := dhcpLayer.(*layers.DHCPv4)
|
|
|
|
|
log.Info(prefix, "DHCP packet dropped")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle TCP packet
|
|
|
|
|
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
|
|
|
|
|
tcpPacket, _ := tcpLayer.(*layers.TCP)
|
|
|
|
@ -175,7 +176,7 @@ func pipeForward(prefix string) {
|
|
|
|
|
if isInteresting {
|
|
|
|
|
util.WriteBinary(fmt.Sprintf("/tmp/pck_%di.dat", time.Now().Unix()), frameBytes)
|
|
|
|
|
util.WriteBinary(fmt.Sprintf("/tmp/pck_%do.dat", time.Now().Unix()), newFrameBytes)
|
|
|
|
|
//util.WritePcap("xyz.pcap", packet.Data(), packet.Metadata().CaptureInfo)
|
|
|
|
|
//util.WritePcapNg("xyz.pcap", packet.Data(), packet.Metadata().CaptureInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Forward modified frame to other plug
|
|
|
|
@ -210,6 +211,9 @@ func filterIP(prefix string, dst interface{}, src interface{}, context gopacket.
|
|
|
|
|
return
|
|
|
|
|
} else if prefix == cmd.Out {
|
|
|
|
|
if isIp {
|
|
|
|
|
if!ip.IsGlobalUnicast() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
OldIP = *ip
|
|
|
|
|
} else if isBs {
|
|
|
|
|
OldIP = *bs
|
|
|
|
@ -268,3 +272,106 @@ func filterMAC(prefix string, dst interface{}, src interface{}, context gopacket
|
|
|
|
|
log.Debugf("%s%s %s MAC %s changed to %s", prefix, context, which, condVal, newVal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleDHCP(content []byte, srcMAC net.HardwareAddr, prefix string) {
|
|
|
|
|
req := dhcp.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 {
|
|
|
|
|
log.Error(prefix, "Invalid DHCP message type")
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
reqType = dhcp.MessageType(t[0])
|
|
|
|
|
if reqType < dhcp.Discover || reqType > dhcp.Inform {
|
|
|
|
|
log.Error(prefix, "Invalid DHCP message type: ", reqType)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Debug(prefix, "DHCP message registered: ", reqType)
|
|
|
|
|
if prefix == cmd.Out {
|
|
|
|
|
switch reqType {
|
|
|
|
|
case dhcp.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])
|
|
|
|
|
if reqIP == nil {
|
|
|
|
|
reqIP = req.CIAddr()
|
|
|
|
|
}
|
|
|
|
|
if len(reqIP) != 4 || reqIP.Equal(net.IPv4zero) {
|
|
|
|
|
log.Error(prefix, "Invalid IP requested in DHCP: ", reqIP)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sendDHCPReply(req, dhcp.ACK, reqIP, options, srcMAC)
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 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) {
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
ipv4 := layers.IPv4{
|
|
|
|
|
Version: 4,
|
|
|
|
|
TTL: 64,
|
|
|
|
|
Protocol: layers.IPProtocolUDP,
|
|
|
|
|
SrcIP: serverIP,
|
|
|
|
|
DstIP: lease,
|
|
|
|
|
}
|
|
|
|
|
udp := layers.UDP{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
packetData := buf.Bytes()
|
|
|
|
|
|
|
|
|
|
// Sending layer through VM's pipe
|
|
|
|
|
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)
|
|
|
|
|
}
|