Skip to content

Commit 5d97951

Browse files
committed
Add functions/types for matching UDP src/dst ports
1 parent 4a6a687 commit 5d97951

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

ovs/match.go

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const (
7575
tunTTL = "tun_ttl"
7676
tunv6DST = "tun_ipv6_dst"
7777
tunv6SRC = "tun_ipv6_src"
78+
udpDST = "udp_dst"
79+
udpSRC = "udp_src"
7880
vlanTCI1 = "vlan_tci1"
7981
vlanTCI = "vlan_tci"
8082
)
@@ -879,8 +881,7 @@ func (m *arpProtocolAddressMatch) GoString() string {
879881
return fmt.Sprintf("ovs.ARPTargetProtocolAddress(%q)", m.ip)
880882
}
881883

882-
// TransportSourcePort matches packets with a transport layer (TCP/UDP) source
883-
// port matching port.
884+
// TransportSourcePort matches packets with a TCP source port matching port.
884885
func TransportSourcePort(port uint16) Match {
885886
return &transportPortMatch{
886887
srcdst: source,
@@ -889,8 +890,7 @@ func TransportSourcePort(port uint16) Match {
889890
}
890891
}
891892

892-
// TransportDestinationPort matches packets with a transport layer (TCP/UDP)
893-
// destination port matching port.
893+
// TransportDestinationPort matches packets with a TCP destination port matching port.
894894
func TransportDestinationPort(port uint16) Match {
895895
return &transportPortMatch{
896896
srcdst: destination,
@@ -899,8 +899,7 @@ func TransportDestinationPort(port uint16) Match {
899899
}
900900
}
901901

902-
// TransportSourceMaskedPort matches packets with a transport layer (TCP/UDP)
903-
// source port matching a masked port range.
902+
// TransportSourceMaskedPort matches packets with TCP source port matching a masked port range.
904903
func TransportSourceMaskedPort(port uint16, mask uint16) Match {
905904
return &transportPortMatch{
906905
srcdst: source,
@@ -909,8 +908,7 @@ func TransportSourceMaskedPort(port uint16, mask uint16) Match {
909908
}
910909
}
911910

912-
// TransportDestinationMaskedPort matches packets with a transport layer (TCP/UDP)
913-
// destination port matching a masked port range.
911+
// TransportDestinationMaskedPort matches packets with a TCP destination port matching a masked port range.
914912
func TransportDestinationMaskedPort(port uint16, mask uint16) Match {
915913
return &transportPortMatch{
916914
srcdst: destination,
@@ -919,6 +917,73 @@ func TransportDestinationMaskedPort(port uint16, mask uint16) Match {
919917
}
920918
}
921919

920+
// UdpSourcePort matches packets with a UDP source port matching port.
921+
func UdpSourcePort(port uint16) Match {
922+
return &udpPortMatch{
923+
srcdst: source,
924+
port: port,
925+
mask: 0,
926+
}
927+
}
928+
929+
// UdpDestinationPort matches packets with a UDP destination port matching port.
930+
func UdpDestinationPort(port uint16) Match {
931+
return &udpPortMatch{
932+
srcdst: destination,
933+
port: port,
934+
mask: 0,
935+
}
936+
}
937+
938+
// UdpSourceMaskedPort matches packets with UDP source port matching a masked port range.
939+
func UdpSourceMaskedPort(port uint16, mask uint16) Match {
940+
return &udpPortMatch{
941+
srcdst: source,
942+
port: port,
943+
mask: mask,
944+
}
945+
}
946+
947+
// UdpDestinationMaskedPort matches packets with a UDP destination port matching a masked port range.
948+
func UdpDestinationMaskedPort(port uint16, mask uint16) Match {
949+
return &udpPortMatch{
950+
srcdst: destination,
951+
port: port,
952+
mask: mask,
953+
}
954+
}
955+
956+
// A udpPortMatch is a Match returned by Udp{Source,Destination}Port.
957+
type udpPortMatch struct {
958+
srcdst string
959+
port uint16
960+
mask uint16
961+
}
962+
963+
var _ Match = &udpPortMatch{}
964+
965+
// MarshalText implements Match.
966+
func (m *udpPortMatch) MarshalText() ([]byte, error) {
967+
return matchUdpPort(m.srcdst, m.port, m.mask)
968+
}
969+
970+
// GoString implements Match.
971+
func (m *udpPortMatch) GoString() string {
972+
if m.mask > 0 {
973+
if m.srcdst == source {
974+
return fmt.Sprintf("ovs.UdpSourceMaskedPort(%#x, %#x)", m.port, m.mask)
975+
}
976+
977+
return fmt.Sprintf("ovs.UdpDestinationMaskedPort(%#x, %#x)", m.port, m.mask)
978+
}
979+
980+
if m.srcdst == source {
981+
return fmt.Sprintf("ovs.UdpSourcePort(%d)", m.port)
982+
}
983+
984+
return fmt.Sprintf("ovs.UdpDestinationPort(%d)", m.port)
985+
}
986+
922987
// A transportPortMatch is a Match returned by Transport{Source,Destination}Port.
923988
type transportPortMatch struct {
924989
srcdst string
@@ -1491,6 +1556,17 @@ func matchTransportPort(srcdst string, port uint16, mask uint16) ([]byte, error)
14911556
return bprintf("tp_%s=0x%04x/0x%04x", srcdst, port, mask), nil
14921557
}
14931558

1559+
// matchUdpPort is the common implementation for
1560+
// Udp{Source,Destination}Port.
1561+
func matchUdpPort(srcdst string, port uint16, mask uint16) ([]byte, error) {
1562+
// No mask specified
1563+
if mask == 0 {
1564+
return bprintf("udp_%s=%d", srcdst, port), nil
1565+
}
1566+
1567+
return bprintf("udp_%s=0x%04x/0x%04x", srcdst, port, mask), nil
1568+
}
1569+
14941570
// IPFragFlag is a string type which can be used with the IPFragMatch.
14951571
type IPFragFlag string
14961572

ovs/matchparser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func parseMatch(key string, value string) (Match, error) {
3535
return parseIntMatch(key, value, math.MaxUint8)
3636
case ctZone:
3737
return parseIntMatch(key, value, math.MaxUint16)
38-
case tpSRC, tpDST:
38+
case tpSRC, tpDST, udpSRC, udpDST:
3939
return parsePort(key, value, math.MaxUint16)
4040
case conjID:
4141
return parseIntMatch(key, value, math.MaxUint32)
@@ -215,6 +215,10 @@ func parsePort(key string, value string, max int) (Match, error) {
215215
return TransportSourceMaskedPort(uint16(values[0]), uint16(values[1])), nil
216216
case tpDST:
217217
return TransportDestinationMaskedPort(uint16(values[0]), uint16(values[1])), nil
218+
case udpSRC:
219+
return UdpSourceMaskedPort(uint16(values[0]), uint16(values[1])), nil
220+
case udpDST:
221+
return UdpDestinationMaskedPort(uint16(values[0]), uint16(values[1])), nil
218222
}
219223
// Return error if input is invalid
220224
return nil, fmt.Errorf("no action matched for %s=%s", key, value)

0 commit comments

Comments
 (0)