Skip to content

Commit

Permalink
drop traffic not to default gw
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Dec 24, 2024
1 parent 5b56f07 commit 342443f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmds/modules/netlightd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func action(cli *cli.Context) error {
}

if err := nft.DropTrafficToLAN(); err != nil {
return errors.New("failed to drop traffic to lan")
return fmt.Errorf("failed to drop traffic to lan: %w", err)
}

mod, err := netlight.NewNetworker()
Expand Down
17 changes: 14 additions & 3 deletions pkg/netlight/nft/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nft
import (
"fmt"
"io"
"log/slog"
"net"
"os/exec"

Expand Down Expand Up @@ -41,7 +40,19 @@ func Apply(r io.Reader, ns string) error {
// the same lan network
func DropTrafficToLAN() error {
mac, err := getDefaultGwMac()
slog.Info("returned", "mac", mac.String(), "err", err)
log.Debug().Str("mac", mac.String()).Err(err).Msg("default gw return")

cmd := exec.Command("nft", "add", "rule", "inet", "filter", "forward",
"ether", "daddr", "!=", mac.String(), "drop")

out, err := cmd.CombinedOutput()
if err != nil {
log.Error().Err(err).Str("output", string(out)).Msg("error during nft")
if eerr, ok := err.(*exec.ExitError); ok {
return errors.Wrapf(err, "failed to execute nft: %v", string(eerr.Stderr))
}
return errors.Wrap(err, "failed to execute nft")
}
return nil
}

Expand Down Expand Up @@ -78,5 +89,5 @@ func getDefaultGwMac() (net.HardwareAddr, error) {
}
}

return nil, nil
return nil, errors.New("failed to get default gw")
}

0 comments on commit 342443f

Please sign in to comment.