← Back to Pull requests

bgp: honor the configured transport local address #633

Closed
nuclearcat closed this pull request without merging on Oct 12, 2026 00:45 UTC
View on GitHub
nuclearcat nuclearcat commented on Oct 05, 2026 07:12 UTC
Author

The transport local-address setting was accepted but ignored, so the kernel chose the source for outgoing connections and incoming sessions could target any local address. Peers requiring a particular source could therefore fail to establish despite a valid configuration.

Bind outgoing sockets to the configured address and check incoming connection destinations, retaining GoBGP's wildcard and bind-interface exceptions. Carry the setting through configuration, peer groups and the gRPC API, report it before connection, and restart sessions when it changes. Keep retrying bind failures without falling back to another source.

Add inline configuration, API and TCP regressions covering IPv4, IPv6, scoped addresses, inheritance, dynamic peers, bind failures and updates. Compare behavior with GoBGP v4.6.0 source; live GoBGP interoperability has not been tested for this change.

Did you use an AI tool?


Assisted-by: OpenAI Codex

Diff: 876 lines (+847 / -29) across 6 files Reviewer: fujita Review duration: 9693m
All Vouch review confidence checks passed
Residual risk 0.11 = Change Risk (0.79) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.79 · 876 lines diff across 6 files
0.79
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.78 · 5 substantive comment(s)
0.78
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
86%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Sensitive Paths sensitive_paths
0.079
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
11 items
fujita fujita commented on code on Oct 08, 2026 11:22 UTC
Code Review
daemon/src/event/grpc.rs:1
@@ -1284,13 +1330,23 @@ impl GoBgpService for GrpcService {
                     ),
                 )));
                 let mut ctx = peer.context.lock().unwrap();
+                let has_session = {

This fixes a separate bug: on master, if a peer has no session, UpdatePeer with a change in needs_teardown cancels the dial loop and nothing starts it again. For example, changing remote_port or passive (true to false) on an idle peer. Please move this fix to a separate commit with a test.

fujita fujita commented on code on Oct 08, 2026 11:28 UTC
Code Review
daemon/src/event/mod.rs:1
@@ -642,28 +642,52 @@ pub(super) fn enable_active_connect(peer: &mut Peer, ch: mpsc::UnboundedSender<T
     let retry_time = peer.config.connect_retry_time;
     let password = peer.config.password.as_ref().map(|x| x.to_string());
     let bind_interface = peer.config.bind_interface.clone();
+    let local_address = peer.config.local_address.clone();
     let peer_ifindex = bind_interface.as_deref().map(auth::ifindex_of).unwrap_or(0);
     let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
     let join_handle = tokio::spawn(async move {
         loop {
-            let socket = match peer_addr {
-                IpAddr::V4(_) => tokio::net::TcpSocket::new_v4().unwrap(),
-                IpAddr::V6(_) => tokio::net::TcpSocket::new_v6().unwrap(),
+            let connect = async {
+                let socket = match peer_addr {
+                    IpAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
+                    IpAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
+                };
+                if let Some(dev) = bind_interface.as_deref() {
+                    socket.bind_device(Some(dev.as_bytes()))?;

This is a behavior change that is not related to local-address. Before this patch, a bind_device() error was ignored. Now the connection fails. This is the same as GoBGP, so the change itself is fine. Can you split it out as a preparatory commit? It can change the socket setup to return errors, and then the local-address commit only adds the bind() call.

fujita fujita commented on code on Oct 08, 2026 11:54 UTC
Code Review
daemon/src/event/mod.rs:1
@@ -642,28 +642,52 @@ pub(super) fn enable_active_connect(peer: &mut Peer, ch: mpsc::UnboundedSender<T
     let retry_time = peer.config.connect_retry_time;
     let password = peer.config.password.as_ref().map(|x| x.to_string());
     let bind_interface = peer.config.bind_interface.clone();
+    let local_address = peer.config.local_address.clone();
     let peer_ifindex = bind_interface.as_deref().map(auth::ifindex_of).unwrap_or(0);
     let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
     let join_handle = tokio::spawn(async move {
         loop {
-            let socket = match peer_addr {
-                IpAddr::V4(_) => tokio::net::TcpSocket::new_v4().unwrap(),
-                IpAddr::V6(_) => tokio::net::TcpSocket::new_v6().unwrap(),
+            let connect = async {
+                let socket = match peer_addr {
+                    IpAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
+                    IpAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
+                };
+                if let Some(dev) = bind_interface.as_deref() {
+                    socket.bind_device(Some(dev.as_bytes()))?;
+                }
+                if let Some(value) = local_address.as_deref() {
+                    let mut local = peer::local_socket_address(value)?;
+                    // Go's TCP dialer treats either unspecified address as a
+                    // wildcard, even when it differs from the remote family.
+                    if local.ip().is_unspecified() {
+                        local = SocketAddr::new(
+                            if peer_addr.is_ipv4() {
+                                IpAddr::V4(Ipv4Addr::UNSPECIFIED)
+                            } else {
+                                IpAddr::V6(Ipv6Addr::UNSPECIFIED)
+                            },
+                            0,
+                        );
+                    }
+                    socket.bind(local)?;
+                }
+                if let Some(key) = password.as_ref() {
+                    auth::set_md5sig(socket.as_raw_fd(), &peer_addr, key, peer_ifindex);
+                }
+                socket.connect(sockaddr).await
             };
-            if let Some(dev) = bind_interface.as_deref() {
-                let _ = socket.bind_device(Some(dev.as_bytes()));
-            }
-            if let Some(key) = password.as_ref() {
-                auth::set_md5sig(socket.as_raw_fd(), &peer_addr, key, peer_ifindex);
-            }
             tokio::select! {
                 result = tokio::time::timeout(
                     tokio::time::Duration::from_secs(5),
-                    socket.connect(sockaddr),
+                    connect,
                 ) => {
-                    if let Ok(Ok(stream)) = result {
-                        let _ = ch.send(stream);
-                        return;
+                    match result {
+                        Ok(Ok(stream)) => {
+                            let _ = ch.send(stream);
+                            return;
+                        }
+                        Ok(Err(e)) => log::warn!("connect to {peer_addr} from {local_address:?} failed: {e}"),

Before this patch, connect errors were not logged. connect_retry_time is 3 seconds by default, so this prints a warning every 3 seconds while the peer is down. Can we log only the socket setup errors (bind_device, bind) at warn level, and the connect error at debug level?

fujita fujita commented on code on Oct 08, 2026 12:02 UTC
Code Review
README.md:1
@@ -43,6 +43,28 @@ Peer            AS Up/Down State       |#Received  Accepted
 - Peer group
 - Address families: IPv4/IPv6 unicast/multicast, L3VPN (RFC 4364), EVPN Types 1–5 (RFC 7432), BGP-LS (RFC 7752), Flowspec (RFC 8955), SR Policy (RFC 9256), MUP ([draft-ietf-bess-mup-safi](https://datatracker.ietf.org/doc/draft-ietf-bess-mup-safi/))
 
+## TCP source address

I want to keep the README to the essential information only. If you want to add this kind of detail, please add a new file instead, for example docs/configuration.md.

fujita fujita requested changes on Oct 08, 2026 12:02 UTC
Changes Requested Reviewer

Requested changes.

nuclearcat nuclearcat commented on code on Oct 11, 2026 08:49 UTC
Code Review
daemon/src/event/grpc.rs:1
@@ -1284,13 +1330,23 @@ impl GoBgpService for GrpcService {
                     ),
                 )));
                 let mut ctx = peer.context.lock().unwrap();
+                let has_session = {

moved this into its own commit, 499904e. Added tests, both fail without the fix.

nuclearcat nuclearcat commented on code on Oct 11, 2026 08:50 UTC
Code Review
daemon/src/event/mod.rs:1
@@ -642,28 +642,52 @@ pub(super) fn enable_active_connect(peer: &mut Peer, ch: mpsc::UnboundedSender<T
     let retry_time = peer.config.connect_retry_time;
     let password = peer.config.password.as_ref().map(|x| x.to_string());
     let bind_interface = peer.config.bind_interface.clone();
+    let local_address = peer.config.local_address.clone();
     let peer_ifindex = bind_interface.as_deref().map(auth::ifindex_of).unwrap_or(0);
     let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
     let join_handle = tokio::spawn(async move {
         loop {
-            let socket = match peer_addr {
-                IpAddr::V4(_) => tokio::net::TcpSocket::new_v4().unwrap(),
-                IpAddr::V6(_) => tokio::net::TcpSocket::new_v6().unwrap(),
+            let connect = async {
+                let socket = match peer_addr {
+                    IpAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
+                    IpAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
+                };
+                if let Some(dev) = bind_interface.as_deref() {
+                    socket.bind_device(Some(dev.as_bytes()))?;

As suggested - split this into preparatory commit 61e4ff3, including a regression test for failed device binding. The local-address commit now adds the address binding on top.

nuclearcat nuclearcat commented on code on Oct 11, 2026 08:50 UTC
Code Review
daemon/src/event/mod.rs:1
@@ -642,28 +642,52 @@ pub(super) fn enable_active_connect(peer: &mut Peer, ch: mpsc::UnboundedSender<T
     let retry_time = peer.config.connect_retry_time;
     let password = peer.config.password.as_ref().map(|x| x.to_string());
     let bind_interface = peer.config.bind_interface.clone();
+    let local_address = peer.config.local_address.clone();
     let peer_ifindex = bind_interface.as_deref().map(auth::ifindex_of).unwrap_or(0);
     let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
     let join_handle = tokio::spawn(async move {
         loop {
-            let socket = match peer_addr {
-                IpAddr::V4(_) => tokio::net::TcpSocket::new_v4().unwrap(),
-                IpAddr::V6(_) => tokio::net::TcpSocket::new_v6().unwrap(),
+            let connect = async {
+                let socket = match peer_addr {
+                    IpAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
+                    IpAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
+                };
+                if let Some(dev) = bind_interface.as_deref() {
+                    socket.bind_device(Some(dev.as_bytes()))?;
+                }
+                if let Some(value) = local_address.as_deref() {
+                    let mut local = peer::local_socket_address(value)?;
+                    // Go's TCP dialer treats either unspecified address as a
+                    // wildcard, even when it differs from the remote family.
+                    if local.ip().is_unspecified() {
+                        local = SocketAddr::new(
+                            if peer_addr.is_ipv4() {
+                                IpAddr::V4(Ipv4Addr::UNSPECIFIED)
+                            } else {
+                                IpAddr::V6(Ipv6Addr::UNSPECIFIED)
+                            },
+                            0,
+                        );
+                    }
+                    socket.bind(local)?;
+                }
+                if let Some(key) = password.as_ref() {
+                    auth::set_md5sig(socket.as_raw_fd(), &peer_addr, key, peer_ifindex);
+                }
+                socket.connect(sockaddr).await
             };
-            if let Some(dev) = bind_interface.as_deref() {
-                let _ = socket.bind_device(Some(dev.as_bytes()));
-            }
-            if let Some(key) = password.as_ref() {
-                auth::set_md5sig(socket.as_raw_fd(), &peer_addr, key, peer_ifindex);
-            }
             tokio::select! {
                 result = tokio::time::timeout(
                     tokio::time::Duration::from_secs(5),
-                    socket.connect(sockaddr),
+                    connect,
                 ) => {
-                    if let Ok(Ok(stream)) = result {
-                        let _ = ch.send(stream);
-                        return;
+                    match result {
+                        Ok(Ok(stream)) => {
+                            let _ = ch.send(stream);
+                            return;
+                        }
+                        Ok(Err(e)) => log::warn!("connect to {peer_addr} from {local_address:?} failed: {e}"),

Changed this so socket setup failures log at warn, while connection failures and timeouts log at debug.

nuclearcat nuclearcat commented on code on Oct 11, 2026 08:50 UTC
Code Review
README.md:1
@@ -43,6 +43,28 @@ Peer            AS Up/Down State       |#Received  Accepted
 - Peer group
 - Address families: IPv4/IPv6 unicast/multicast, L3VPN (RFC 4364), EVPN Types 1–5 (RFC 7432), BGP-LS (RFC 7752), Flowspec (RFC 8955), SR Policy (RFC 9256), MUP ([draft-ietf-bess-mup-safi](https://datatracker.ietf.org/doc/draft-ietf-bess-mup-safi/))
 
+## TCP source address

Moved the details to docs/configuration.md and left a short link in the README, is it ok like this?

fujita fujita commented on Oct 12, 2026 00:45 UTC
Contributor

Merged, thanks.

Reviewers
Assignees
nuclearcat nuclearcat
Labels
risk: low confidence: 86% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 78.5% (HIGH) · Higher = larger blast radius
78.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 86% · Lower = rushed or shallow review
86%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 11.0% · Safe to merge
11.0%
Repository