github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/internal/enforcer/nfqdatapath/afinetrawsocket/afinetrawsocket_windows.go (about)

     1  // +build windows
     2  
     3  package afinetrawsocket
     4  
     5  import (
     6  	"errors"
     7  
     8  	"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/packet"
     9  	"go.aporeto.io/enforcerd/trireme-lib/utils/frontman"
    10  )
    11  
    12  type rawsocket struct {
    13  }
    14  
    15  // WindowPlatformMetadata is platform-specific data about the packet
    16  type WindowPlatformMetadata struct {
    17  	PacketInfo frontman.PacketInfo
    18  	IgnoreFlow bool
    19  	DropFlow   bool
    20  	Drop       bool
    21  	SetMark    uint32
    22  }
    23  
    24  const (
    25  	// RawSocketMark is the mark asserted on all packet sent out of this socket
    26  	RawSocketMark = 0x63
    27  	// NetworkRawSocketMark is the mark on packet egressing
    28  	//the raw socket coming in from network
    29  	NetworkRawSocketMark = 0x40000063
    30  	//ApplicationRawSocketMark is the mark on packet egressing
    31  	//the raw socket coming from application
    32  	ApplicationRawSocketMark = 0x40000062
    33  )
    34  
    35  // SocketWriter interface exposes an interface to write and close sockets
    36  type SocketWriter interface {
    37  	WriteSocket(buf []byte, version packet.IPver, data packet.PlatformMetadata) error
    38  }
    39  
    40  // CreateSocket returns a handle to SocketWriter interface
    41  func CreateSocket(mark int, deviceName string) (SocketWriter, error) {
    42  	return &rawsocket{}, nil
    43  }
    44  
    45  // WriteSocket on Windows calls into the driver to forward the packet
    46  func (sock *rawsocket) WriteSocket(buf []byte, version packet.IPver, data packet.PlatformMetadata) error {
    47  	if data == nil {
    48  		return errors.New("no PlatformMetadata for WriteSocket")
    49  	}
    50  	windata, ok := data.(*WindowPlatformMetadata)
    51  	if !ok {
    52  		return errors.New("no WindowPlatformMetadata for WriteSocket")
    53  	}
    54  	return windata.forwardPacket(buf, version)
    55  }
    56  
    57  // Clone the WindowPlatformMetadata structure
    58  func (w *WindowPlatformMetadata) Clone() packet.PlatformMetadata {
    59  	platformMetadata := &WindowPlatformMetadata{
    60  		PacketInfo: w.PacketInfo,
    61  		IgnoreFlow: w.IgnoreFlow,
    62  		Drop:       w.Drop,
    63  	}
    64  	return platformMetadata
    65  }
    66  
    67  // forwardPacket takes a raw packet and sends it to the driver to be sent on the network
    68  func (w *WindowPlatformMetadata) forwardPacket(buf []byte, version packet.IPver) error {
    69  
    70  	if w.IgnoreFlow && w.DropFlow {
    71  		return errors.New("ignoreFlow and dropFlow cannot both be true")
    72  	}
    73  
    74  	// Could set port/addr in packet info but not required by the driver for forwarding of the packet.
    75  	// Create a copy of the packet info so that these changes don't modifiy the current PacketInfo
    76  	packetInfo := w.PacketInfo
    77  	packetInfo.Outbound = 1
    78  	packetInfo.NewPacket = 1
    79  	packetInfo.Drop = 0
    80  	packetInfo.IgnoreFlow = 0
    81  	if version == packet.V4 {
    82  		packetInfo.Ipv4 = 1
    83  	} else {
    84  		packetInfo.Ipv4 = 0
    85  	}
    86  	if w.Drop {
    87  		packetInfo.Drop = 1
    88  	}
    89  	if w.IgnoreFlow {
    90  		packetInfo.IgnoreFlow = 1
    91  	}
    92  	if w.DropFlow {
    93  		packetInfo.DropFlow = 1
    94  	}
    95  	packetInfo.PacketSize = uint32(len(buf))
    96  	if err := frontman.Wrapper.PacketFilterForward(&packetInfo, buf); err != nil {
    97  		return err
    98  	}
    99  	return nil
   100  }