Skip to content

Commit e7896ff

Browse files
committed
Support tc flower tcp flags mask
1 parent b4a5e93 commit e7896ff

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/tc/filters/cls_flower.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ pub type MplsTtl = u8;
210210
pub type MplsBos = u8;
211211
pub type MplsTc = u8;
212212
pub type MplsLabel = u32;
213+
pub type TcpFlagsMask = u8;
214+
215+
bitflags! {
216+
// TcpFlags _ARE_ exactly 8 bits.
217+
// Why flower uses a 16-bit field is a mystery, but we deal with it.
218+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
219+
pub struct TcpFlags : u8 {
220+
const Cwr = 1 << 0;
221+
const Ece = 1 << 1;
222+
const Urg = 1 << 2;
223+
const Ack = 1 << 3;
224+
const Psh = 1 << 4;
225+
const Rst = 1 << 5;
226+
const Syn = 1 << 6;
227+
const Fin = 1 << 7;
228+
}
229+
}
213230

214231
// Lists sourced from https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
215232
pub mod icmpv4 {
@@ -1351,6 +1368,8 @@ pub enum TcFilterFlowerOption {
13511368
KeyMplsBos(MplsBos),
13521369
KeyMplsTc(MplsTc),
13531370
KeyMplsLabel(MplsLabel),
1371+
KeyTcpFlags(TcpFlags),
1372+
KeyTcpFlagsMask(TcpFlagsMask),
13541373
}
13551374

13561375
impl Nla for TcFilterFlowerOption {
@@ -1424,6 +1443,8 @@ impl Nla for TcFilterFlowerOption {
14241443
Self::KeyMplsBos(_) => 1,
14251444
Self::KeyMplsTc(_) => 1,
14261445
Self::KeyMplsLabel(_) => 4,
1446+
Self::KeyTcpFlags(_) => 2,
1447+
Self::KeyTcpFlagsMask(_) => 2,
14271448
Self::Other(attr) => attr.value_len(),
14281449
}
14291450
}
@@ -1502,6 +1523,8 @@ impl Nla for TcFilterFlowerOption {
15021523
Self::KeyMplsBos(_) => TCA_FLOWER_KEY_MPLS_BOS,
15031524
Self::KeyMplsTc(_) => TCA_FLOWER_KEY_MPLS_TC,
15041525
Self::KeyMplsLabel(_) => TCA_FLOWER_KEY_MPLS_LABEL,
1526+
Self::KeyTcpFlags(_) => TCA_FLOWER_KEY_TCP_FLAGS,
1527+
Self::KeyTcpFlagsMask(_) => TCA_FLOWER_KEY_TCP_FLAGS_MASK,
15051528
Self::Other(attr) => attr.kind(),
15061529
}
15071530
}
@@ -1519,7 +1542,7 @@ impl Nla for TcFilterFlowerOption {
15191542
buffer.copy_from_slice(eth_type.as_be_bytes().as_slice())
15201543
}
15211544
Self::KeyIpProto(proto) => {
1522-
buffer.copy_from_slice(&[(i32::from(*proto) as u8)])
1545+
buffer.copy_from_slice(&[i32::from(*proto) as u8])
15231546
}
15241547
Self::KeyIpv4Src(ip) => buffer.copy_from_slice(&ip.octets()),
15251548
Self::KeyIpv4SrcMask(ip) => buffer.copy_from_slice(&ip.octets()),
@@ -1647,6 +1670,12 @@ impl Nla for TcFilterFlowerOption {
16471670
// but nothing works unless it's native endian. Bug report?
16481671
buffer.copy_from_slice(label.to_ne_bytes().as_slice())
16491672
}
1673+
Self::KeyTcpFlags(flags) => buffer.copy_from_slice(
1674+
(flags.bits() as u16).to_be_bytes().as_slice(),
1675+
),
1676+
Self::KeyTcpFlagsMask(flags) => {
1677+
buffer.copy_from_slice((*flags as u16).to_be_bytes().as_slice())
1678+
}
16501679
Self::Other(attr) => attr.emit_value(buffer),
16511680
}
16521681
}
@@ -2232,6 +2261,22 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
22322261
let label = BigEndian::read_u32(payload);
22332262
Self::KeyMplsLabel(label)
22342263
}
2264+
TCA_FLOWER_KEY_TCP_FLAGS => {
2265+
if payload.len() != 2 {
2266+
return Err(DecodeError::from("invalid tcp flags length"));
2267+
}
2268+
let flags = BigEndian::read_u16(payload);
2269+
Self::KeyTcpFlags(TcpFlags::from_bits_retain(flags as u8))
2270+
}
2271+
TCA_FLOWER_KEY_TCP_FLAGS_MASK => {
2272+
if payload.len() != 2 {
2273+
return Err(DecodeError::from(
2274+
"invalid tcp flags mask length",
2275+
));
2276+
}
2277+
let flags = BigEndian::read_u16(payload);
2278+
Self::KeyTcpFlagsMask(flags as u8)
2279+
}
22352280
_ => Self::Other(
22362281
DefaultNla::parse(buf).context("failed to parse flower nla")?,
22372282
),

src/tc/filters/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::cls_u32::{
1313
pub use self::matchall::{TcFilterMatchAll, TcFilterMatchAllOption};
1414
pub use cls_flower::{
1515
arp, icmpv4, icmpv6, EncKeyId, EthType, TcFilterFlower,
16-
TcFilterFlowerOption, VlanId, VlanPrio,
16+
TcFilterFlowerOption, TcpFlags, VlanId, VlanPrio,
1717
};
1818
pub use flower_flags::TcFlowerOptionFlags;
1919
pub use u32_flags::{TcU32OptionFlags, TcU32SelectorFlags};

src/tc/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub use self::filters::{
1111
arp, icmpv4, icmpv6, EncKeyId, EthType, TcFilterFlower,
1212
TcFilterFlowerOption, TcFilterMatchAll, TcFilterMatchAllOption,
1313
TcFilterU32, TcFilterU32Option, TcFlowerOptionFlags, TcU32Key,
14-
TcU32OptionFlags, TcU32Selector, TcU32SelectorFlags, VlanId, VlanPrio,
14+
TcU32OptionFlags, TcU32Selector, TcU32SelectorFlags, TcpFlags, VlanId,
15+
VlanPrio,
1516
};
1617
pub use self::header::{TcHandle, TcHeader, TcMessageBuffer};
1718
pub use self::message::TcMessage;

0 commit comments

Comments
 (0)