Skip to content
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

Limited number of connections per IP #2046

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

xgreenx
Copy link
Collaborator

@xgreenx xgreenx commented Jul 23, 2024

It is possible to create many connections from the same subnetwork and occupy connection slots. This change should limit the number of connections allowed from the same remote IP.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests

Before requesting review

  • I have reviewed the code myself

@xgreenx xgreenx requested a review from a team July 23, 2024 23:24
@xgreenx xgreenx self-assigned this Jul 23, 2024
@@ -90,6 +90,10 @@ pub struct P2PArgs {
#[clap(long = "max-peers-connected", default_value = "50", env)]
pub max_peers_connected: u32,

/// Max number of unique peers connected with the same remote IP address.
#[clap(long = "max-peers-per-remote-ip", default_value = "5", env)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[clap(long = "max-peers-per-remote-ip", default_value = "5", env)]
#[clap(long = "max-peers-per-remote-ip", default_value = "50", env)]

right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, 5=)

@@ -203,6 +206,7 @@ impl Config<NotInitialized> {
bootstrap_nodes: vec![],
enable_mdns: false,
max_peers_connected: 50,
max_peers_per_remote_ip: 50,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
max_peers_per_remote_ip: 50,
max_peers_per_remote_ip: 5,

The config default here doesn't match the default on clap

) -> Result<THandler<Self>, ConnectionDenied> {
if let Some(ip) = get_ip_addr(remote_addr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that get_ip_addr could return two different IP for an IPv4 and his IPv6 mapped or libp2p prevents IPv4-mapped in IPv6 ?

If it's the first case then they will fall in two separate entries and in this case we might want get_ip_addr to canonicalise the IP (https://doc.rust-lang.org/std/net/enum.IpAddr.html#method.to_canonical)

@@ -71,15 +84,17 @@ pub struct Behaviour {
connected_reserved_nodes: HashSet<PeerId>,
pending_connections: HashSet<ConnectionId>,
pending_events: VecDeque<ToSwarm<PeerReportEvent, Void>>,
ips_to_peers: HashMap<IpAddr, HashSet<PeerId>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seem that this is currently used only to track a number of connection per ip. If we don't want this to be usable for others use cases in the future we could use
HashMap<IpAddr, usize>

It will simplify the current usage that we have by avoiding having to do .len() to measure it.

Comment on lines +141 to +158
let entry = self.ips_to_peers.entry(ip);

match entry {
Entry::Occupied(mut occupied) => {
let set = occupied.get_mut();

if set.len() >= self.max_connections_per_ip {
return Err(ConnectionDenied::new(
"Max connections per IP reached",
))
}

set.insert(peer);
}
Entry::Vacant(_) => {
// Do nothing
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let entry = self.ips_to_peers.entry(ip);
match entry {
Entry::Occupied(mut occupied) => {
let set = occupied.get_mut();
if set.len() >= self.max_connections_per_ip {
return Err(ConnectionDenied::new(
"Max connections per IP reached",
))
}
set.insert(peer);
}
Entry::Vacant(_) => {
// Do nothing
}
}
if let Some(peers) = self.ips_to_peers.get_mut(&ip) {
if peers.len() >= self.max_connections_per_ip {
return Err(ConnectionDenied::new(
"Max connections per IP reached",
))
}
peers.insert(peer);
}

Comment on lines +233 to +255
let ip = get_ip_addr(send_back_addr);

if let Some(ip) = ip {
let entry = self.ips_to_peers.entry(ip);

match entry {
Entry::Occupied(mut occupied) => {
let set = occupied.get_mut();
set.remove(&peer_id);

if set.is_empty() {
occupied.remove();
}
}
Entry::Vacant(_) => {
tracing::warn!(
"IP address not found in the \
map during disconnection of the peer {:?}",
peer_id
)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let ip = get_ip_addr(send_back_addr);
if let Some(ip) = ip {
let entry = self.ips_to_peers.entry(ip);
match entry {
Entry::Occupied(mut occupied) => {
let set = occupied.get_mut();
set.remove(&peer_id);
if set.is_empty() {
occupied.remove();
}
}
Entry::Vacant(_) => {
tracing::warn!(
"IP address not found in the \
map during disconnection of the peer {:?}",
peer_id
)
}
}
}
if let Some(ip) = get_ip_addr(send_back_addr) {
if let Some(peers) = self.ips_to_peers.get_mut(&ip) {
peers.remove(&peer_id);
if peers.is_empty() {
self.ips_to_peers.remove(&ip);
}
} else {
tracing::warn!(
"IP address not found in the \
map during disconnection of the peer {:?}",
peer_id
)
}
}

@xgreenx xgreenx marked this pull request as draft August 27, 2024 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants