|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +use netlink_packet_core::{ |
| 4 | + parse_u8, DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer, |
| 5 | + Parseable, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::conntrack::attributes::tcp_flags::{TCPFlags, TCPFlagsBuffer}; |
| 9 | + |
| 10 | +const CTA_PROTOINFO_TCP_STATE: u16 = 1; |
| 11 | +const CTA_PROTOINFO_TCP_WSCALE_ORIGINAL: u16 = 2; |
| 12 | +const CTA_PROTOINFO_TCP_WSCALE_REPLY: u16 = 3; |
| 13 | +const CTA_PROTOINFO_TCP_FLAGS_ORIGINAL: u16 = 4; |
| 14 | +const CTA_PROTOINFO_TCP_FLAGS_REPLY: u16 = 5; |
| 15 | + |
| 16 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 17 | +#[non_exhaustive] |
| 18 | +pub enum ProtoInfoTCP { |
| 19 | + State(u8), |
| 20 | + OriginalWindowScale(u8), |
| 21 | + ReplyWindowScale(u8), |
| 22 | + OriginalFlags(TCPFlags), |
| 23 | + ReplyFlags(TCPFlags), |
| 24 | + Other(DefaultNla), |
| 25 | +} |
| 26 | +impl Nla for ProtoInfoTCP { |
| 27 | + fn value_len(&self) -> usize { |
| 28 | + match self { |
| 29 | + ProtoInfoTCP::State(attr) => size_of_val(attr), |
| 30 | + ProtoInfoTCP::OriginalWindowScale(attr) => size_of_val(attr), |
| 31 | + ProtoInfoTCP::ReplyWindowScale(attr) => size_of_val(attr), |
| 32 | + ProtoInfoTCP::OriginalFlags(attr) => attr.buffer_len(), |
| 33 | + ProtoInfoTCP::ReplyFlags(attr) => attr.buffer_len(), |
| 34 | + ProtoInfoTCP::Other(attr) => attr.value_len(), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + fn kind(&self) -> u16 { |
| 39 | + match self { |
| 40 | + ProtoInfoTCP::State(_) => CTA_PROTOINFO_TCP_STATE, |
| 41 | + ProtoInfoTCP::OriginalWindowScale(_) => { |
| 42 | + CTA_PROTOINFO_TCP_WSCALE_ORIGINAL |
| 43 | + } |
| 44 | + ProtoInfoTCP::ReplyWindowScale(_) => CTA_PROTOINFO_TCP_WSCALE_REPLY, |
| 45 | + ProtoInfoTCP::OriginalFlags(_) => CTA_PROTOINFO_TCP_FLAGS_ORIGINAL, |
| 46 | + ProtoInfoTCP::ReplyFlags(_) => CTA_PROTOINFO_TCP_FLAGS_REPLY, |
| 47 | + ProtoInfoTCP::Other(attr) => attr.kind(), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn emit_value(&self, buffer: &mut [u8]) { |
| 52 | + match self { |
| 53 | + ProtoInfoTCP::State(attr) => buffer[0] = *attr, |
| 54 | + ProtoInfoTCP::OriginalWindowScale(attr) => buffer[0] = *attr, |
| 55 | + ProtoInfoTCP::ReplyWindowScale(attr) => buffer[0] = *attr, |
| 56 | + ProtoInfoTCP::OriginalFlags(attr) => attr.emit(buffer), |
| 57 | + ProtoInfoTCP::ReplyFlags(attr) => attr.emit(buffer), |
| 58 | + ProtoInfoTCP::Other(attr) => attr.emit_value(buffer), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> |
| 63 | + for ProtoInfoTCP |
| 64 | +{ |
| 65 | + fn parse(buf: &NlaBuffer<&'buffer T>) -> Result<Self, DecodeError> { |
| 66 | + let kind = buf.kind(); |
| 67 | + let payload = buf.value(); |
| 68 | + let nla = match kind { |
| 69 | + CTA_PROTOINFO_TCP_STATE => ProtoInfoTCP::State( |
| 70 | + parse_u8(payload) |
| 71 | + .context("invalid CTA_PROTOINFO_TCP_STATE value")?, |
| 72 | + ), |
| 73 | + CTA_PROTOINFO_TCP_WSCALE_ORIGINAL => { |
| 74 | + ProtoInfoTCP::OriginalWindowScale(parse_u8(payload).context( |
| 75 | + "invalid CTA_PROTOINFO_TCP_WSCALE_ORIGINAL value", |
| 76 | + )?) |
| 77 | + } |
| 78 | + CTA_PROTOINFO_TCP_WSCALE_REPLY => ProtoInfoTCP::ReplyWindowScale( |
| 79 | + parse_u8(payload) |
| 80 | + .context("invalid CTA_PROTOINFO_TCP_WSCALE_REPLY value")?, |
| 81 | + ), |
| 82 | + CTA_PROTOINFO_TCP_FLAGS_ORIGINAL => ProtoInfoTCP::OriginalFlags( |
| 83 | + TCPFlags::parse(&TCPFlagsBuffer::new(payload)).context( |
| 84 | + "invalid CTA_PROTOINFO_TCP_FLAGS_ORIGINAL value", |
| 85 | + )?, |
| 86 | + ), |
| 87 | + CTA_PROTOINFO_TCP_FLAGS_REPLY => ProtoInfoTCP::ReplyFlags( |
| 88 | + TCPFlags::parse(&TCPFlagsBuffer::new(payload)) |
| 89 | + .context("invalid CTA_PROTOINFO_TCP_FLAGS_REPLY value")?, |
| 90 | + ), |
| 91 | + _ => ProtoInfoTCP::Other(DefaultNla::parse(buf)?), |
| 92 | + }; |
| 93 | + Ok(nla) |
| 94 | + } |
| 95 | +} |
0 commit comments