Add packet payload for kext2

This commit is contained in:
Vladimir Stoilov 2024-04-17 11:15:29 +03:00
parent ead271f51c
commit c425007be1
No known key found for this signature in database
GPG key ID: 2F190B67A43A81AF
5 changed files with 157 additions and 129 deletions

View file

@ -105,26 +105,25 @@ func startInterception(packets chan packet.Packet) error {
} }
}) })
// Start kext logging. The worker will periodically send request to the kext to print memory stats. module.StartServiceWorker("kext clean ended connection worker", 0, func(ctx context.Context) error {
// module.StartServiceWorker("kext memory stats request worker", 0, func(ctx context.Context) error { timer := time.NewTicker(30 * time.Second)
// timer := time.NewTicker(20 * time.Second) for {
// for { select {
// select { case <-timer.C:
// case <-timer.C: {
// { err := kext2.SendCleanEndedConnection()
// err := kext2.SendPrintMemoryStatsCommand() if err != nil {
// if err != nil { return err
// return err }
// } }
// } case <-ctx.Done():
// case <-ctx.Done(): {
// { return nil
// return nil }
// } }
// }
// } }
// }) })
} }
return nil return nil

View file

@ -24,6 +24,7 @@ func createKextService(driverName string, driverPath string) (*KextService, erro
} }
defer windows.CloseServiceHandle(manager) defer windows.CloseServiceHandle(manager)
// Convert the driver name to a UTF16 string
driverNameU16, err := syscall.UTF16FromString(driverName) driverNameU16, err := syscall.UTF16FromString(driverName)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to convert driver name to UTF16 string: %w", err) return nil, fmt.Errorf("failed to convert driver name to UTF16 string: %w", err)

View file

@ -45,8 +45,10 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch
// New Packet // New Packet
new := &Packet{ new := &Packet{
verdictRequest: conn.Id, verdictRequest: conn.Id,
payload: conn.Payload,
verdictSet: abool.NewBool(false), verdictSet: abool.NewBool(false),
} }
new.Base.Payload()
info := new.Info() info := new.Info()
info.Inbound = conn.Direction > 0 info.Inbound = conn.Direction > 0
info.InTunnel = false info.InTunnel = false
@ -95,6 +97,7 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch
// New Packet // New Packet
new := &Packet{ new := &Packet{
verdictRequest: conn.Id, verdictRequest: conn.Id,
payload: conn.Payload,
verdictSet: abool.NewBool(false), verdictSet: abool.NewBool(false),
} }
info := new.Info() info := new.Info()

View file

@ -102,13 +102,17 @@ func SendPrintMemoryStatsCommand() error {
return kext_interface.SendPrintMemoryStatsCommand(kextFile) return kext_interface.SendPrintMemoryStatsCommand(kextFile)
} }
func SendCleanEndedConnection() error {
return kext_interface.SendCleanEndedConnectionsCommand(kextFile)
}
// RecvVerdictRequest waits for the next verdict request from the kext. If a timeout is reached, both *VerdictRequest and error will be nil. // RecvVerdictRequest waits for the next verdict request from the kext. If a timeout is reached, both *VerdictRequest and error will be nil.
func RecvVerdictRequest() (*kext_interface.Info, error) { func RecvVerdictRequest() (*kext_interface.Info, error) {
return kext_interface.RecvInfo(kextFile) return kext_interface.RecvInfo(kextFile)
} }
// SetVerdict sets the verdict for a packet and/or connection. // SetVerdict sets the verdict for a packet and/or connection.
func SetVerdict(pkt *Packet, verdict network.Verdict) error { func SetVerdict(pkt *Packet, verdict kext_interface.KextVerdict) error {
verdictCommand := kext_interface.Verdict{Id: pkt.verdictRequest, Verdict: uint8(verdict)} verdictCommand := kext_interface.Verdict{Id: pkt.verdictRequest, Verdict: uint8(verdict)}
return kext_interface.SendVerdictCommand(kextFile, verdictCommand) return kext_interface.SendVerdictCommand(kextFile, verdictCommand)
} }

View file

@ -4,12 +4,12 @@
package windowskext package windowskext
import ( import (
"fmt"
"sync" "sync"
"github.com/tevino/abool" "github.com/tevino/abool"
"github.com/vlabo/portmaster_windows_rust_kext/kext_interface"
"github.com/safing/portmaster/network" "github.com/safing/portbase/log"
"github.com/safing/portmaster/network/packet" "github.com/safing/portmaster/network/packet"
) )
@ -18,6 +18,7 @@ type Packet struct {
packet.Base packet.Base
verdictRequest uint64 verdictRequest uint64
payload []byte
verdictSet *abool.AtomicBool verdictSet *abool.AtomicBool
payloadLoaded bool payloadLoaded bool
@ -33,7 +34,7 @@ func (pkt *Packet) FastTrackedByIntegration() bool {
// InfoOnly returns whether the packet is informational only and does not // InfoOnly returns whether the packet is informational only and does not
// represent an actual packet. // represent an actual packet.
func (pkt *Packet) InfoOnly() bool { func (pkt *Packet) InfoOnly() bool {
return pkt.verdictRequest == 0 return false
} }
// ExpectInfo returns whether the next packet is expected to be informational only. // ExpectInfo returns whether the next packet is expected to be informational only.
@ -43,13 +44,33 @@ func (pkt *Packet) ExpectInfo() bool {
// GetPayload returns the full raw packet. // GetPayload returns the full raw packet.
func (pkt *Packet) LoadPacketData() error { func (pkt *Packet) LoadPacketData() error {
return fmt.Errorf("Not implemented") pkt.lock.Lock()
defer pkt.lock.Unlock()
if !pkt.payloadLoaded {
pkt.payloadLoaded = true
if len(pkt.payload) > 0 {
err := packet.Parse(pkt.payload, &pkt.Base)
if err != nil {
log.Tracef("payload: %#v", pkt.payload)
log.Tracer(pkt.Ctx()).Warningf("windowskext: failed to parse payload: %s", err)
return packet.ErrFailedToLoadPayload
}
}
}
if len(pkt.Raw()) == 0 {
return packet.ErrFailedToLoadPayload
}
return nil
} }
// Accept accepts the packet. // Accept accepts the packet.
func (pkt *Packet) Accept() error { func (pkt *Packet) Accept() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, -network.VerdictAccept) return SetVerdict(pkt, kext_interface.VerdictAccept)
} }
return nil return nil
} }
@ -57,7 +78,7 @@ func (pkt *Packet) Accept() error {
// Block blocks the packet. // Block blocks the packet.
func (pkt *Packet) Block() error { func (pkt *Packet) Block() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, -network.VerdictBlock) return SetVerdict(pkt, kext_interface.VerdictBlock)
} }
return nil return nil
} }
@ -65,7 +86,7 @@ func (pkt *Packet) Block() error {
// Drop drops the packet. // Drop drops the packet.
func (pkt *Packet) Drop() error { func (pkt *Packet) Drop() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, -network.VerdictDrop) return SetVerdict(pkt, kext_interface.VerdictDrop)
} }
return nil return nil
} }
@ -73,7 +94,7 @@ func (pkt *Packet) Drop() error {
// PermanentAccept permanently accepts connection (and the current packet). // PermanentAccept permanently accepts connection (and the current packet).
func (pkt *Packet) PermanentAccept() error { func (pkt *Packet) PermanentAccept() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, network.VerdictAccept) return SetVerdict(pkt, kext_interface.VerdictAccept)
} }
return nil return nil
} }
@ -81,7 +102,7 @@ func (pkt *Packet) PermanentAccept() error {
// PermanentBlock permanently blocks connection (and the current packet). // PermanentBlock permanently blocks connection (and the current packet).
func (pkt *Packet) PermanentBlock() error { func (pkt *Packet) PermanentBlock() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, network.VerdictBlock) return SetVerdict(pkt, kext_interface.VerdictBlock)
} }
return nil return nil
} }
@ -89,7 +110,7 @@ func (pkt *Packet) PermanentBlock() error {
// PermanentDrop permanently drops connection (and the current packet). // PermanentDrop permanently drops connection (and the current packet).
func (pkt *Packet) PermanentDrop() error { func (pkt *Packet) PermanentDrop() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, network.VerdictDrop) return SetVerdict(pkt, kext_interface.VerdictDrop)
} }
return nil return nil
} }
@ -97,7 +118,7 @@ func (pkt *Packet) PermanentDrop() error {
// RerouteToNameserver permanently reroutes the connection to the local nameserver (and the current packet). // RerouteToNameserver permanently reroutes the connection to the local nameserver (and the current packet).
func (pkt *Packet) RerouteToNameserver() error { func (pkt *Packet) RerouteToNameserver() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, network.VerdictRerouteToNameserver) return SetVerdict(pkt, kext_interface.VerdictRerouteToNameserver)
} }
return nil return nil
} }
@ -105,7 +126,7 @@ func (pkt *Packet) RerouteToNameserver() error {
// RerouteToTunnel permanently reroutes the connection to the local tunnel entrypoint (and the current packet). // RerouteToTunnel permanently reroutes the connection to the local tunnel entrypoint (and the current packet).
func (pkt *Packet) RerouteToTunnel() error { func (pkt *Packet) RerouteToTunnel() error {
if pkt.verdictSet.SetToIf(false, true) { if pkt.verdictSet.SetToIf(false, true) {
return SetVerdict(pkt, network.VerdictRerouteToTunnel) return SetVerdict(pkt, kext_interface.VerdictRerouteToTunnel)
} }
return nil return nil
} }