From ce5c5eb5921844367b6d7394fb992f109d866222 Mon Sep 17 00:00:00 2001 From: Ben Draut Date: Fri, 5 Mar 2021 09:41:09 -0700 Subject: [PATCH 1/2] Add start/stop callbacks to packet proxy This adds support for two additional functions in go plugins in the `packet_proxy` module: * `func OnStart() int` * `func OnStop()` These will be called when the packet proxy module is turned on and off, respectively. --- .../packet_proxy/packet_proxy_linux_amd64.go | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/packet_proxy/packet_proxy_linux_amd64.go b/modules/packet_proxy/packet_proxy_linux_amd64.go index 0f4f7243..4d92dbb1 100644 --- a/modules/packet_proxy/packet_proxy_linux_amd64.go +++ b/modules/packet_proxy/packet_proxy_linux_amd64.go @@ -161,6 +161,18 @@ func (mod *PacketProxy) Configure() (err error) { return fmt.Errorf("Symbol OnPacket is not a valid callback function.") } + if sym, err = mod.plugin.Lookup("OnStart"); err == nil { + var onStartCb func() int + if onStartCb, ok = sym.(func() int); !ok { + return fmt.Errorf("OnStart signature does not match expected signature: 'func() int'") + } else { + var result int + if result = onStartCb(); result != 0 { + return fmt.Errorf("OnStart returned non-zero result. result=%d", result) + } + } + } + mod.queue = new(nfqueue.Queue) if err = mod.queue.SetCallback(dummyCallback); err != nil { return @@ -206,10 +218,22 @@ func (mod *PacketProxy) Start() error { }) } -func (mod *PacketProxy) Stop() error { +func (mod *PacketProxy) Stop() (err error) { return mod.SetRunning(false, func() { mod.queue.StopLoop() mod.runRule(false) + + var sym plugin.Symbol + if sym, err = mod.plugin.Lookup("OnStop"); err == nil { + var onStopCb func() + var ok bool + if onStopCb, ok = sym.(func()); !ok { + mod.Error("OnStop signature does not match expected signature: 'func()', unable to call OnStop.") + } else { + onStopCb() + } + } + <-mod.done }) } From 32eee7d94b00b1cc2a4fb267cc00a6e39fbd6d46 Mon Sep 17 00:00:00 2001 From: Ben Draut Date: Tue, 9 Mar 2021 20:30:48 -0700 Subject: [PATCH 2/2] Fix bug in target parsing When a MAC address with uppercase letters was provided, parsing would return an error because the parsing logic would only attempt to remove normalized versions (all lowercase) from the target list. This would leave the address with uppercase letters in the target list, which it would then try to interpet as an Alias. This fixes the bug by using the original address form when removing it from the target list. --- network/net.go | 6 +++--- network/net_test.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/network/net.go b/network/net.go index 2971d831..e117bcb9 100644 --- a/network/net.go +++ b/network/net.go @@ -101,10 +101,10 @@ func ParseTargets(targets string, aliasMap *data.UnsortedKV) (ips []net.IP, macs // first isolate MACs and parse them for _, mac := range macParser.FindAllString(targets, -1) { - mac = NormalizeMac(mac) - hw, err := net.ParseMAC(mac) + normalizedMac := NormalizeMac(mac) + hw, err := net.ParseMAC(normalizedMac) if err != nil { - return nil, nil, fmt.Errorf("error while parsing MAC '%s': %s", mac, err) + return nil, nil, fmt.Errorf("error while parsing MAC '%s': %s", normalizedMac, err) } macs = append(macs, hw) diff --git a/network/net_test.go b/network/net_test.go index 827d14d1..dcf08d8e 100644 --- a/network/net_test.go +++ b/network/net_test.go @@ -37,6 +37,14 @@ func TestNormalizeMac(t *testing.T) { // TODO: refactor to parse targets with an actual alias map func TestParseTargets(t *testing.T) { + aliasMap, err := data.NewMemUnsortedKV() + if err != nil { + panic(err) + } + + aliasMap.Set("5c:00:0b:90:a9:f0", "test_alias") + aliasMap.Set("5c:00:0b:90:a9:f1", "Home_Laptop") + cases := []struct { Name string InputTargets string @@ -57,9 +65,17 @@ func TestParseTargets(t *testing.T) { }, { "MACs are parsed", - "192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0", + "192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0, 6C:00:0B:90:A9:F0", &data.UnsortedKV{}, 2, + 3, + false, + }, + { + "Aliases are parsed", + "test_alias, Home_Laptop", + aliasMap, + 0, 2, false, },