Code cleanup

This commit is contained in:
Kioubit 2021-12-22 06:09:52 -05:00
parent 338139ad0f
commit 7d876fb290

View File

@ -70,45 +70,32 @@ func listen(iface string, responder chan *NDRequest, requestType NDPType) {
panic(err.Error()) panic(err.Error())
} }
var f Filter var protocolNo uint32
if requestType == NDP_SOL { if requestType == NDP_SOL {
f = []bpf.Instruction{ //Neighbor Solicitation
// Load "EtherType" field from the ethernet header. protocolNo = 0x87
bpf.LoadAbsolute{Off: 12, Size: 2},
// Jump to the drop packet instruction if EtherType is not IPv6.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x86dd, SkipTrue: 4},
// Load "Next Header" field from IPV6 header.
bpf.LoadAbsolute{Off: 20, Size: 1},
// Jump to the drop packet instruction if Next Header is not ICMPv6.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x3a, SkipTrue: 2},
// Load "Type" field from ICMPv6 header.
bpf.LoadAbsolute{Off: 54, Size: 1},
// Jump to the drop packet instruction if Type is not Neighbor Solicitation.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x87, SkipTrue: 1},
// Verdict is "send up to 4k of the packet to userspace."buf
bpf.RetConstant{Val: 4096},
// Verdict is "ignore packet."
bpf.RetConstant{Val: 0},
}
} else { } else {
f = []bpf.Instruction{ //Neighbor Advertisement
// Load "EtherType" field from the ethernet header. protocolNo = 0x88
bpf.LoadAbsolute{Off: 12, Size: 2}, }
// Jump to the drop packet instruction if EtherType is not IPv6.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x86dd, SkipTrue: 4}, var f Filter = []bpf.Instruction{
// Load "Next Header" field from IPV6 header. // Load "EtherType" field from the ethernet header.
bpf.LoadAbsolute{Off: 20, Size: 1}, bpf.LoadAbsolute{Off: 12, Size: 2},
// Jump to the drop packet instruction if Next Header is not ICMPv6. // Jump to the drop packet instruction if EtherType is not IPv6.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x3a, SkipTrue: 2}, bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x86dd, SkipTrue: 4},
// Load "Type" field from ICMPv6 header. // Load "Next Header" field from IPV6 header.
bpf.LoadAbsolute{Off: 54, Size: 1}, bpf.LoadAbsolute{Off: 20, Size: 1},
// Jump to the drop packet instruction if Type is not Neighbor Advertisement. // Jump to the drop packet instruction if Next Header is not ICMPv6.
bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x88, SkipTrue: 1}, bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x3a, SkipTrue: 2},
// Verdict is "send up to 4k of the packet to userspace." // Load "Type" field from ICMPv6 header.
bpf.RetConstant{Val: 4096}, bpf.LoadAbsolute{Off: 54, Size: 1},
// Verdict is "ignore packet." // Jump to the drop packet instruction if Type is not Neighbor Solicitation / Advertisement.
bpf.RetConstant{Val: 0}, bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: protocolNo, SkipTrue: 1},
} // Verdict is "send up to 4k of the packet to userspace."buf
bpf.RetConstant{Val: 4096},
// Verdict is "ignore packet."
bpf.RetConstant{Val: 0},
} }
err = f.ApplyTo(fd) err = f.ApplyTo(fd)