-
Notifications
You must be signed in to change notification settings - Fork 11
Conntrack get netlink netfilter message types #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shivkr6
wants to merge
2
commits into
rust-netlink:main
Choose a base branch
from
shivkr6:conntrack-get
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{ | ||
| DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer, | ||
| NlasIterator, Parseable, | ||
| }; | ||
|
|
||
| use crate::conntrack::attributes::{protoinfo::ProtoInfo, tuple::Tuple}; | ||
|
|
||
| const CTA_TUPLE_ORIG: u16 = 1; | ||
| const CTA_PROTOINFO: u16 = 4; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
| pub enum ConntrackNla { | ||
| CtaTupleOrig(Vec<Tuple>), | ||
| CtaProtoInfo(Vec<ProtoInfo>), | ||
| Other(DefaultNla), | ||
| } | ||
|
|
||
| impl Nla for ConntrackNla { | ||
| fn value_len(&self) -> usize { | ||
| match self { | ||
| ConntrackNla::CtaTupleOrig(attr) => { | ||
| attr.iter().map(|op| op.buffer_len()).sum() | ||
| } | ||
| ConntrackNla::CtaProtoInfo(attr) => { | ||
| attr.iter().map(|op| op.buffer_len()).sum() | ||
| } | ||
| ConntrackNla::Other(attr) => attr.value_len(), | ||
| } | ||
| } | ||
|
|
||
| fn kind(&self) -> u16 { | ||
| match self { | ||
| ConntrackNla::CtaTupleOrig(_) => CTA_TUPLE_ORIG, | ||
| ConntrackNla::CtaProtoInfo(_) => CTA_PROTOINFO, | ||
| ConntrackNla::Other(attr) => attr.kind(), | ||
| } | ||
| } | ||
|
|
||
| fn emit_value(&self, buffer: &mut [u8]) { | ||
| match self { | ||
| ConntrackNla::CtaTupleOrig(attr) => { | ||
| let mut len = 0; | ||
| for op in attr { | ||
| op.emit(&mut buffer[len..]); | ||
| len += op.buffer_len(); | ||
| } | ||
| } | ||
| ConntrackNla::CtaProtoInfo(attr) => { | ||
| let mut len = 0; | ||
| for op in attr { | ||
| op.emit(&mut buffer[len..]); | ||
| len += op.buffer_len(); | ||
| } | ||
| } | ||
| ConntrackNla::Other(attr) => attr.emit_value(buffer), | ||
| } | ||
| } | ||
| fn is_nested(&self) -> bool { | ||
| matches!( | ||
| self, | ||
| ConntrackNla::CtaTupleOrig(_) | ConntrackNla::CtaProtoInfo(_) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> | ||
| for ConntrackNla | ||
| { | ||
| fn parse(buf: &NlaBuffer<&'buffer T>) -> Result<Self, DecodeError> { | ||
| let kind = buf.kind(); | ||
| let payload = buf.value(); | ||
| let nla = match kind { | ||
| CTA_TUPLE_ORIG => { | ||
| let mut tuples = Vec::new(); | ||
| for nlas in NlasIterator::new(payload) { | ||
| let nlas = &nlas.context("invalid CTA_TUPLE_ORIG value")?; | ||
| tuples.push(Tuple::parse(nlas)?); | ||
| } | ||
| ConntrackNla::CtaTupleOrig(tuples) | ||
| } | ||
| CTA_PROTOINFO => { | ||
| let mut proto_infos = Vec::new(); | ||
| for nlas in NlasIterator::new(payload) { | ||
| let nlas = &nlas.context("invalid CTA_PROTOINFO value")?; | ||
| proto_infos.push(ProtoInfo::parse(nlas)?); | ||
| } | ||
| ConntrackNla::CtaProtoInfo(proto_infos) | ||
| } | ||
| _ => ConntrackNla::Other(DefaultNla::parse(buf)?), | ||
| }; | ||
| Ok(nla) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{ | ||
| parse_ip, DecodeError, DefaultNla, ErrorContext, Nla, NlaBuffer, Parseable, | ||
| }; | ||
| use std::net::IpAddr; | ||
|
|
||
| const CTA_IP_V4_SRC: u16 = 1; | ||
| const CTA_IP_V6_SRC: u16 = 3; | ||
| const CTA_IP_V4_DST: u16 = 2; | ||
| const CTA_IP_V6_DST: u16 = 4; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
| pub enum IPTuple { | ||
| SourceAddress(IpAddr), | ||
| DestinationAddress(IpAddr), | ||
| Other(DefaultNla), | ||
| } | ||
|
|
||
| const IPV4_LEN: usize = 4; | ||
| const IPV6_LEN: usize = 16; | ||
|
|
||
| // Helper function needed for implementing the Nla trait | ||
| pub fn emit_ip(addr: &IpAddr, buf: &mut [u8]) { | ||
| match addr { | ||
| IpAddr::V4(ip) => { | ||
| buf[..IPV4_LEN].copy_from_slice(ip.octets().as_slice()); | ||
| } | ||
| IpAddr::V6(ip) => { | ||
| buf[..IPV6_LEN].copy_from_slice(ip.octets().as_slice()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Nla for IPTuple { | ||
| fn value_len(&self) -> usize { | ||
| match self { | ||
| IPTuple::SourceAddress(attr) => match *attr { | ||
| IpAddr::V4(_) => IPV4_LEN, | ||
| IpAddr::V6(_) => IPV6_LEN, | ||
| }, | ||
| IPTuple::DestinationAddress(attr) => match *attr { | ||
| IpAddr::V4(_) => IPV4_LEN, | ||
| IpAddr::V6(_) => IPV6_LEN, | ||
| }, | ||
| IPTuple::Other(attr) => attr.value_len(), | ||
| } | ||
| } | ||
|
|
||
| fn kind(&self) -> u16 { | ||
| match self { | ||
| IPTuple::SourceAddress(attr) => match *attr { | ||
| IpAddr::V4(_) => CTA_IP_V4_SRC, | ||
| IpAddr::V6(_) => CTA_IP_V6_SRC, | ||
| }, | ||
| IPTuple::DestinationAddress(attr) => match *attr { | ||
| IpAddr::V4(_) => CTA_IP_V4_DST, | ||
| IpAddr::V6(_) => CTA_IP_V6_DST, | ||
| }, | ||
| IPTuple::Other(attr) => attr.kind(), | ||
| } | ||
| } | ||
|
|
||
| fn emit_value(&self, buffer: &mut [u8]) { | ||
| match self { | ||
| IPTuple::SourceAddress(attr) => emit_ip(attr, buffer), | ||
| IPTuple::DestinationAddress(attr) => emit_ip(attr, buffer), | ||
| IPTuple::Other(attr) => attr.emit_value(buffer), | ||
| } | ||
| } | ||
| } | ||
| impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> | ||
| for IPTuple | ||
| { | ||
| fn parse(buf: &NlaBuffer<&'buffer T>) -> Result<Self, DecodeError> { | ||
| let kind = buf.kind(); | ||
| let payload = buf.value(); | ||
| let nla = match kind { | ||
| CTA_IP_V4_SRC | CTA_IP_V6_SRC => Self::SourceAddress( | ||
| parse_ip(payload).context("invalid SourceAddress value")?, | ||
| ), | ||
| CTA_IP_V4_DST | CTA_IP_V6_DST => Self::DestinationAddress( | ||
| parse_ip(payload) | ||
| .context("invalid DestinationAddress value")?, | ||
| ), | ||
| _ => IPTuple::Other(DefaultNla::parse(buf)?), | ||
| }; | ||
| Ok(nla) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| mod attribute; | ||
| mod iptuple; | ||
| mod protoinfo; | ||
| mod protoinfotcp; | ||
| mod prototuple; | ||
| mod tcp_flags; | ||
| mod tuple; | ||
|
|
||
| pub use attribute::ConntrackNla; | ||
| pub use iptuple::IPTuple; | ||
| pub use protoinfo::ProtoInfo; | ||
| pub use protoinfotcp::ProtoInfoTCP; | ||
| pub use prototuple::{ProtoTuple, Protocol}; | ||
| pub use tcp_flags::TCPFlags; | ||
| pub use tuple::Tuple; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use netlink_packet_core::{ | ||
| DecodeError, DefaultNla, Emitable, ErrorContext, Nla, NlaBuffer, | ||
| NlasIterator, Parseable, | ||
| }; | ||
|
|
||
| use crate::conntrack::attributes::protoinfotcp::ProtoInfoTCP; | ||
|
|
||
| const CTA_PROTOINFO_TCP: u16 = 1; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
| pub enum ProtoInfo { | ||
| TCP(Vec<ProtoInfoTCP>), | ||
| Other(DefaultNla), | ||
| } | ||
| impl Nla for ProtoInfo { | ||
| fn value_len(&self) -> usize { | ||
| match self { | ||
| ProtoInfo::TCP(nlas) => nlas.iter().map(|op| op.buffer_len()).sum(), | ||
| ProtoInfo::Other(attr) => attr.value_len(), | ||
| } | ||
| } | ||
|
|
||
| fn kind(&self) -> u16 { | ||
| match self { | ||
| ProtoInfo::TCP(_) => CTA_PROTOINFO_TCP, | ||
| ProtoInfo::Other(attr) => attr.kind(), | ||
| } | ||
| } | ||
| fn emit_value(&self, buffer: &mut [u8]) { | ||
| match self { | ||
| ProtoInfo::TCP(nlas) => { | ||
| let mut len = 0; | ||
| for op in nlas { | ||
| op.emit(&mut buffer[len..]); | ||
| len += op.buffer_len(); | ||
| } | ||
| } | ||
| ProtoInfo::Other(attr) => attr.emit_value(buffer), | ||
| } | ||
| } | ||
| fn is_nested(&self) -> bool { | ||
| matches!(self, ProtoInfo::TCP(_)) | ||
| } | ||
| } | ||
|
|
||
| impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> | ||
| for ProtoInfo | ||
| { | ||
| fn parse(buf: &NlaBuffer<&'buffer T>) -> Result<Self, DecodeError> { | ||
| let kind = buf.kind(); | ||
| let payload = buf.value(); | ||
| let nla = match kind { | ||
| CTA_PROTOINFO_TCP => { | ||
| let mut proto_info_tcps = Vec::new(); | ||
| for nlas in NlasIterator::new(payload) { | ||
| let nlas = | ||
| &nlas.context("invailid CTA_PROTOINFO_TCP value")?; | ||
| proto_info_tcps.push(ProtoInfoTCP::parse(nlas)?); | ||
| } | ||
| ProtoInfo::TCP(proto_info_tcps) | ||
| } | ||
| _ => ProtoInfo::Other(DefaultNla::parse(buf)?), | ||
| }; | ||
| Ok(nla) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
ContrackAttributelikeLinkAttributeinnetlink-packet-route.