Adding serialization.

This commit is contained in:
Kevin Krakauer
2020-01-21 16:51:17 -08:00
parent 421b6ff181
commit 538053538d
2 changed files with 35 additions and 8 deletions
+28 -1
View File
@@ -196,7 +196,9 @@ func convertNetstackToBinary(tablename string, table iptables.Table) (linux.Kern
}
func marshalMatcher(matcher iptables.Matcher) []byte {
switch matcher.(type) {
switch m := matcher.(type) {
case *iptables.UDPMatcher:
return marshalUDPMatcher(m)
default:
// TODO(gvisor.dev/issue/170): We don't support any matchers
// yet, so any call to marshalMatcher will panic.
@@ -204,6 +206,31 @@ func marshalMatcher(matcher iptables.Matcher) []byte {
}
}
func marshalUDPMatcher(matcher *iptables.UDPMatcher) []byte {
type udpMatch struct {
linux.XTEntryMatch
linux.XTUDP
}
linuxMatcher := udpMatch{
XTEntryMatch: linux.XTEntryMatch{
MatchSize: linux.SizeOfXTEntryMatch + linux.SizeOfXTUDP,
// Name: "udp",
},
XTUDP: linux.XTUDP{
SourcePortStart: matcher.Data.SourcePortStart,
SourcePortEnd: matcher.Data.SourcePortEnd,
DestinationPortStart: matcher.Data.DestinationPortStart,
DestinationPortEnd: matcher.Data.DestinationPortEnd,
InverseFlags: matcher.Data.InverseFlags,
},
}
copy(linuxMatcher.Name[:], "udp")
var buf [linux.SizeOfXTEntryMatch + linux.SizeOfXTUDP]byte
binary.Marshal(buf[:], usermem.ByteOrder, linuxMatcher)
return buf[:]
}
func marshalTarget(target iptables.Target) []byte {
switch target.(type) {
case iptables.UnconditionalAcceptTarget:
+7 -7
View File
@@ -24,7 +24,7 @@ import (
)
type UDPMatcher struct {
data UDPMatcherData
Data UDPMatcherData
// tablename string
// unsigned int matchsize;
@@ -62,11 +62,11 @@ func NewUDPMatcher(filter IPHeaderFilter, data UDPMatcherData) (Matcher, error)
log.Warningf("UDP matching is only valid for protocol %d.", header.UDPProtocolNumber)
}
return &UDPMatcher{data: data}, nil
return &UDPMatcher{Data: data}, nil
}
// TODO: Check xt_tcpudp.c. Need to check for same things (e.g. fragments).
func (tm *UDPMatcher) Match(hook Hook, pkt tcpip.PacketBuffer, interfaceName string) (bool, bool) {
func (um *UDPMatcher) Match(hook Hook, pkt tcpip.PacketBuffer, interfaceName string) (bool, bool) {
log.Infof("UDPMatcher called from: %s", string(debug.Stack()))
netHeader := header.IPv4(pkt.NetworkHeader)
@@ -114,12 +114,12 @@ func (tm *UDPMatcher) Match(hook Hook, pkt tcpip.PacketBuffer, interfaceName str
destinationPort := udpHeader.DestinationPort()
log.Infof("UDPMatcher: sport and dport are %d and %d. sports and dport start and end are (%d, %d) and (%d, %d)",
udpHeader.SourcePort(), udpHeader.DestinationPort(),
tm.data.SourcePortStart, tm.data.SourcePortEnd,
tm.data.DestinationPortStart, tm.data.DestinationPortEnd)
if sourcePort < tm.data.SourcePortStart || tm.data.SourcePortEnd < sourcePort {
um.Data.SourcePortStart, um.Data.SourcePortEnd,
um.Data.DestinationPortStart, um.Data.DestinationPortEnd)
if sourcePort < um.Data.SourcePortStart || um.Data.SourcePortEnd < sourcePort {
return false, false
}
if destinationPort < tm.data.DestinationPortStart || tm.data.DestinationPortEnd < destinationPort {
if destinationPort < um.Data.DestinationPortStart || um.Data.DestinationPortEnd < destinationPort {
return false, false
}