MCPcopy Create free account
hub / github.com/encodeous/nylon / InstallTC

Method InstallTC

core/nylon_tc.go:20–131  ·  view source on GitHub ↗

polyamide traffic control for nylon

()

Source from the content-addressed store, hash-verified

18// polyamide traffic control for nylon
19
20func (n *Nylon) InstallTC() {
21 t := n.Trace
22
23 if n.DBG_trace_tc {
24 n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
25 if packet.Validate() { // make sure it's an IP packet
26 peer := packet.FromPeer
27 if peer == nil {
28 peer = packet.ToPeer
29 }
30 src := packet.GetSrc()
31 dst := packet.GetDst()
32 if src.IsValid() &&
33 dst.IsValid() &&
34 peer != nil &&
35 src != netip.IPv4Unspecified() && src != netip.IPv6Unspecified() &&
36 dst != netip.IPv4Unspecified() && dst != netip.IPv6Unspecified() {
37 t.Submit(fmt.Sprintf("Unhandled TC packet: %v -> %v, peer %s\n", packet.GetSrc(), packet.GetDst(), peer))
38 }
39 }
40 return device.TcPass, nil
41 })
42 }
43
44 // bounce back packets if using system routing
45 if n.UseSystemRouting {
46 n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
47 if packet.Incoming() {
48 // bounce incoming packets
49 //dev.Log.Verbosef("BounceFwd packet: %v -> %v", packet.GetSrc(), packet.GetDst())
50 return device.TcBounce, nil
51 }
52 return device.TcPass, nil
53 })
54 // forward only outgoing packets based on the routing table
55 n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
56 entry, ok := n.router.ForwardTable.Load().Lookup(packet.GetDst())
57 if ok && !packet.Incoming() {
58 if entry.Blackhole {
59 return device.TcDrop, nil
60 }
61 packet.ToPeer = entry.Peer
62 if n.DBG_trace_tc {
63 t.Submit(fmt.Sprintf("Fwd packet: %v -> %v, via %s\n", packet.GetSrc(), packet.GetDst(), entry.Nh))
64 }
65 return device.TcForward, nil
66 }
67 return device.TcPass, nil
68 })
69 } else {
70 // forward packets based on the routing table
71 n.Device.InstallFilter(func(dev *device.Device, packet *device.TCElement) (device.TCAction, error) {
72 entry, ok := n.router.ForwardTable.Load().Lookup(packet.GetDst())
73 if ok {
74 if entry.Blackhole {
75 return device.TcDrop, nil
76 }
77 packet.ToPeer = entry.Peer

Callers 1

initWireGuardMethod · 0.95

Calls 11

handleNylonPacketMethod · 0.95
InstallFilterMethod · 0.80
ValidateMethod · 0.80
GetSrcMethod · 0.80
GetDstMethod · 0.80
IncomingMethod · 0.80
GetIPVersionMethod · 0.80
GetTTLMethod · 0.80
DecrementTTLMethod · 0.80
PayloadMethod · 0.80
LookupMethod · 0.45

Tested by

no test coverage detected