Skip to content

Commit 5eb26b1

Browse files
Jarno Rajahalmejessegross
authored andcommitted
openvswitch: TCP flags matching support.
tcp_flags=flags/mask Bitwise match on TCP flags. The flags and mask are 16-bit num‐ bers written in decimal or in hexadecimal prefixed by 0x. Each 1-bit in mask requires that the corresponding bit in port must match. Each 0-bit in mask causes the corresponding bit to be ignored. TCP protocol currently defines 9 flag bits, and additional 3 bits are reserved (must be transmitted as zero), see RFCs 793, 3168, and 3540. The flag bits are, numbering from the least significant bit: 0: FIN No more data from sender. 1: SYN Synchronize sequence numbers. 2: RST Reset the connection. 3: PSH Push function. 4: ACK Acknowledgement field significant. 5: URG Urgent pointer field significant. 6: ECE ECN Echo. 7: CWR Congestion Windows Reduced. 8: NS Nonce Sum. 9-11: Reserved. 12-15: Not matchable, must be zero. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Signed-off-by: Jesse Gross <jesse@nicira.com>
1 parent df23e9f commit 5eb26b1

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎include/uapi/linux/openvswitch.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ enum ovs_key_attr {
271271
OVS_KEY_ATTR_SKB_MARK, /* u32 skb mark */
272272
OVS_KEY_ATTR_TUNNEL, /* Nested set of ovs_tunnel attributes */
273273
OVS_KEY_ATTR_SCTP, /* struct ovs_key_sctp */
274+
OVS_KEY_ATTR_TCP_FLAGS, /* be16 TCP flags. */
274275

275276
#ifdef __KERNEL__
276277
OVS_KEY_ATTR_IPV4_TUNNEL, /* struct ovs_key_ipv4_tunnel */

‎net/openvswitch/flow.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
428428
struct tcphdr *tcp = tcp_hdr(skb);
429429
key->ipv4.tp.src = tcp->source;
430430
key->ipv4.tp.dst = tcp->dest;
431+
key->ipv4.tp.flags = TCP_FLAGS_BE16(tcp);
431432
}
432433
} else if (key->ip.proto == IPPROTO_UDP) {
433434
if (udphdr_ok(skb)) {
@@ -496,6 +497,7 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
496497
struct tcphdr *tcp = tcp_hdr(skb);
497498
key->ipv6.tp.src = tcp->source;
498499
key->ipv6.tp.dst = tcp->dest;
500+
key->ipv6.tp.flags = TCP_FLAGS_BE16(tcp);
499501
}
500502
} else if (key->ip.proto == NEXTHDR_UDP) {
501503
if (udphdr_ok(skb)) {

‎net/openvswitch/flow.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct sw_flow_key {
9393
struct {
9494
__be16 src; /* TCP/UDP/SCTP source port. */
9595
__be16 dst; /* TCP/UDP/SCTP destination port. */
96+
__be16 flags; /* TCP flags. */
9697
} tp;
9798
struct {
9899
u8 sha[ETH_ALEN]; /* ARP source hardware address. */
@@ -109,6 +110,7 @@ struct sw_flow_key {
109110
struct {
110111
__be16 src; /* TCP/UDP/SCTP source port. */
111112
__be16 dst; /* TCP/UDP/SCTP destination port. */
113+
__be16 flags; /* TCP flags. */
112114
} tp;
113115
struct {
114116
struct in6_addr target; /* ND target address. */

‎net/openvswitch/flow_netlink.c‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static bool match_validate(const struct sw_flow_match *match,
114114
mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
115115
| (1 << OVS_KEY_ATTR_IPV6)
116116
| (1 << OVS_KEY_ATTR_TCP)
117+
| (1 << OVS_KEY_ATTR_TCP_FLAGS)
117118
| (1 << OVS_KEY_ATTR_UDP)
118119
| (1 << OVS_KEY_ATTR_SCTP)
119120
| (1 << OVS_KEY_ATTR_ICMP)
@@ -154,8 +155,11 @@ static bool match_validate(const struct sw_flow_match *match,
154155

155156
if (match->key->ip.proto == IPPROTO_TCP) {
156157
key_expected |= 1 << OVS_KEY_ATTR_TCP;
157-
if (match->mask && (match->mask->key.ip.proto == 0xff))
158+
key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
159+
if (match->mask && (match->mask->key.ip.proto == 0xff)) {
158160
mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
161+
mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
162+
}
159163
}
160164

161165
if (match->key->ip.proto == IPPROTO_ICMP) {
@@ -186,8 +190,11 @@ static bool match_validate(const struct sw_flow_match *match,
186190

187191
if (match->key->ip.proto == IPPROTO_TCP) {
188192
key_expected |= 1 << OVS_KEY_ATTR_TCP;
189-
if (match->mask && (match->mask->key.ip.proto == 0xff))
193+
key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
194+
if (match->mask && (match->mask->key.ip.proto == 0xff)) {
190195
mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
196+
mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
197+
}
191198
}
192199

193200
if (match->key->ip.proto == IPPROTO_ICMPV6) {
@@ -235,6 +242,7 @@ static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
235242
[OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
236243
[OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
237244
[OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
245+
[OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
238246
[OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
239247
[OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
240248
[OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
@@ -634,6 +642,19 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
634642
attrs &= ~(1 << OVS_KEY_ATTR_TCP);
635643
}
636644

645+
if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
646+
if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
647+
SW_FLOW_KEY_PUT(match, ipv4.tp.flags,
648+
nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
649+
is_mask);
650+
} else {
651+
SW_FLOW_KEY_PUT(match, ipv6.tp.flags,
652+
nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
653+
is_mask);
654+
}
655+
attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
656+
}
657+
637658
if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
638659
const struct ovs_key_udp *udp_key;
639660

@@ -1004,9 +1025,15 @@ int ovs_nla_put_flow(const struct sw_flow_key *swkey,
10041025
if (swkey->eth.type == htons(ETH_P_IP)) {
10051026
tcp_key->tcp_src = output->ipv4.tp.src;
10061027
tcp_key->tcp_dst = output->ipv4.tp.dst;
1028+
if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1029+
output->ipv4.tp.flags))
1030+
goto nla_put_failure;
10071031
} else if (swkey->eth.type == htons(ETH_P_IPV6)) {
10081032
tcp_key->tcp_src = output->ipv6.tp.src;
10091033
tcp_key->tcp_dst = output->ipv6.tp.dst;
1034+
if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1035+
output->ipv6.tp.flags))
1036+
goto nla_put_failure;
10101037
}
10111038
} else if (swkey->ip.proto == IPPROTO_UDP) {
10121039
struct ovs_key_udp *udp_key;

0 commit comments

Comments
 (0)