mirror of
https://github.com/soypat/lneto.git
synced 2026-08-20 14:39:02 +00:00
add lneto.Validator type for frame validation
This commit is contained in:
+86
-46
@@ -14,77 +14,117 @@ var (
|
||||
errShortARP = errors.New("bad ARP size")
|
||||
errShortTCP = errors.New("TCP offset exceeds frame")
|
||||
errBadTCPOff = errors.New("TCP offset invalid")
|
||||
|
||||
errBadIPVersion = errors.New("bad IP version field")
|
||||
errEvilPacket = errors.New("evil packet")
|
||||
)
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (efrm EthFrame) ValidateSize() error {
|
||||
sz := efrm.EtherTypeOrSize()
|
||||
if sz.IsSize() && len(efrm.buf) < int(sz) {
|
||||
return errShortEth
|
||||
} else if sz == EtherTypeVLAN && len(efrm.buf) < 18 {
|
||||
return errShortVLAN
|
||||
type Validator struct {
|
||||
checkEvil bool
|
||||
accum []error
|
||||
}
|
||||
|
||||
func (v *Validator) ResetErr() {
|
||||
v.accum = v.accum[:0]
|
||||
}
|
||||
|
||||
func (v *Validator) Err() error {
|
||||
if len(v.accum) == 1 {
|
||||
return v.accum[0]
|
||||
} else if len(v.accum) == 0 {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return errors.Join(v.accum...)
|
||||
}
|
||||
|
||||
func (v *Validator) gotErr(err error) {
|
||||
v.accum = append(v.accum, err)
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (afrm ARPFrame) ValidateSize() error {
|
||||
func (efrm EthFrame) ValidateSize(v *Validator) {
|
||||
sz := efrm.EtherTypeOrSize()
|
||||
if sz.IsSize() && len(efrm.buf) < int(sz) {
|
||||
v.gotErr(errShortEth)
|
||||
}
|
||||
if sz == EtherTypeVLAN && len(efrm.buf) < 18 {
|
||||
v.gotErr(errShortVLAN)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (afrm ARPFrame) ValidateSize(v *Validator) {
|
||||
_, hlen := afrm.Hardware()
|
||||
_, ilen := afrm.Protocol()
|
||||
minLen := 8 + 2*(hlen+ilen)
|
||||
if len(afrm.buf) < int(minLen) {
|
||||
return errShortARP
|
||||
v.gotErr(errShortARP)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (ufrm UDPFrame) ValidateSize() error {
|
||||
ul := ufrm.Length()
|
||||
if ul < sizeHeaderUDP {
|
||||
return errBadUDPLen
|
||||
} else if int(ul) > len(ufrm.RawData()) {
|
||||
return errShortUDP
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (ifrm IPv4Frame) ValidateSize() error {
|
||||
func (ifrm IPv4Frame) ValidateSize(v *Validator) {
|
||||
ihl := ifrm.ihl()
|
||||
tl := ifrm.TotalLength()
|
||||
if tl < sizeHeaderIPv4 {
|
||||
return errBadIPv4TL
|
||||
} else if int(tl) > len(ifrm.RawData()) {
|
||||
return errShortIPv4
|
||||
} else if ihl < 5 {
|
||||
return errBadIPv4IHL
|
||||
v.gotErr(errBadIPv4TL)
|
||||
}
|
||||
return nil
|
||||
if int(tl) > len(ifrm.RawData()) {
|
||||
v.gotErr(errShortIPv4)
|
||||
}
|
||||
if ihl < 5 {
|
||||
v.gotErr(errBadIPv4IHL)
|
||||
}
|
||||
}
|
||||
|
||||
func (ifrm IPv4Frame) ValidateFields(v *Validator) {
|
||||
flags := ifrm.Flags()
|
||||
if ifrm.version() != 4 {
|
||||
v.gotErr(errBadIPVersion)
|
||||
}
|
||||
if v.checkEvil && flags.IsEvil() {
|
||||
v.gotErr(errEvilPacket)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate checks for invalid frame values.
|
||||
func (ifrm IPv4Frame) Validate(v *Validator) {
|
||||
ifrm.ValidateSize(v)
|
||||
ifrm.ValidateFields(v)
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (tfrm TCPFrame) ValidateSize() error {
|
||||
off := tfrm.HeaderLength()
|
||||
if off < sizeHeaderTCP {
|
||||
return errBadTCPOff
|
||||
} else if off > len(tfrm.RawData()) {
|
||||
return errShortTCP
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (i6frm IPv6Frame) ValidateSize() error {
|
||||
func (i6frm IPv6Frame) ValidateSize(v *Validator) {
|
||||
tl := i6frm.PayloadLength()
|
||||
if int(tl)+sizeHeaderIPv6 > len(i6frm.RawData()) {
|
||||
return errShortIPv6
|
||||
v.gotErr(errShortIPv6)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (tfrm TCPFrame) ValidateSize(v *Validator) {
|
||||
off := tfrm.HeaderLength()
|
||||
if off < sizeHeaderTCP {
|
||||
v.gotErr(errBadTCPOff)
|
||||
}
|
||||
if off > len(tfrm.RawData()) {
|
||||
v.gotErr(errShortTCP)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateSize checks the frame's size fields and compares with the actual buffer
|
||||
// the frame. It returns a non-nil error on finding an inconsistency.
|
||||
func (ufrm UDPFrame) ValidateSize(v *Validator) {
|
||||
ul := ufrm.Length()
|
||||
if ul < sizeHeaderUDP {
|
||||
v.gotErr(errBadUDPLen)
|
||||
}
|
||||
if int(ul) > len(ufrm.RawData()) {
|
||||
v.gotErr(errShortUDP)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user