311 Commits
Author SHA1 Message Date
Tamir DubersteinandHanzo Dev 5d4b282975 checklocks: annotate network lock ownership
Make existing mutex and atomic ownership explicit for networking state.
Annotate self-locking helpers and synchronous forwarding paths, keeping
queue references and callback lifetime requirements in their comments.

Document collection, callback and helper identity limits where the
checker cannot enforce the existing ownership contract.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-26 12:56:52 -04:00
Parth SarthiandgVisor bot c2e9a88ab3 NIC: Added support to return kind with info.
PiperOrigin-RevId: 959288350
2026-08-04 16:25:37 -07:00
Parth SarthiandgVisor bot 5433818834 Nftables: Avoid race condition between update and evaluation.
- There was no lock taken for Nftables rule evaluation.
    So, it could happen that while processing and simultaneous update,
    a packet could pass to half updated ruleset.

- Changed logic to replace atomic pointer, instead of
    replacing fields in the Nftables.

- All the rule evaluation, can now go without needing any reader lock.

- A lock is require for updating Nftables object though,
   to serialize the Netlink messages.

PiperOrigin-RevId: 957516505
2026-07-31 23:46:43 -07:00
Nayana BidariandgVisor bot 3846731234 Refactor network configuration during restore and defer link creation in runsc
This change refactors the network configuration flow during sandbox restore
and defers the creation of network links and routes in runsc to run after
seccomp filters are installed.

- Replaced ReplaceConfig with ResetConfig in inet.Stack. Introduced
inet.NetworkArgs to configure the stack during restore, updating k.LoadFrom
to use it.
- Renamed CreateLinksAndRoutes URPC to SetNetworkArgs, which now configures
host sockets before seccomp is installed, deferring link/route creation to run
after seccomp.
- Fixed S/R support by saving and restoring limit/burst state.
- Minor seccomp filter adjustments and test updates.

PiperOrigin-RevId: 945995430
2026-07-10 18:52:31 -07:00
Nayana BidariandgVisor bot 7e09cf7be6 Automated rollback of changelist 941330876
PiperOrigin-RevId: 944161934
2026-07-07 16:47:40 -07:00
Nayana BidariandgVisor bot 997db57cd5 Refactor setting up network during restore.
Previously, during sandbox restore the network configuration was restored by
creating a new network stack (which has all the config scraped from the host)
and the replacing this new config such as NICs, routes and IP addresses from
the new stack to the loaded/restored stack.

This change refactors the process by introducing a new `inet.NetworkArgs`
interface that abstracts how the network stack is configured. The `Loader` now
implements this interface and configures the loaded stack directly using the
configuration scraped from the host during restore. `Kernel.LoadFrom` accepts
this interface (instead of stack), allowing it to configure the restored stack
using either the Loader's configuration.

PiperOrigin-RevId: 941330876
2026-07-01 15:37:56 -07:00
Nayana BidariandgVisor bot 51723ede44 Support the SO_MARK socket option and Netfilter mark matching.
- This change enables sockets to set and get the `SO_MARK` option, which marks
outgoing packets.
- It also implements Netfilter (iptables) support for matching
packet marks using the `mark` match extension.
- Added unit and E2E tests to verify mark match and capabilities.

PiperOrigin-RevId: 938123591
2026-06-25 12:16:10 -07:00
Nayana BidariandgVisor bot 30136801cf Support live migration of TCP connections during s/r.
When `allow-tcp-live-migration` flag is set, all established TCP connections
are not terminated during save. At the time of restore, the change tries to
find the routes and NICs with the source and destination IP address of the
connection. If it is success, the connection is restored. Enabling this flag
keeps the external connections behavior same as existing loopback connections.
Added unit test to verify the behavior with the flag on.

PiperOrigin-RevId: 929411541
2026-06-09 14:42:46 -07:00
Konstantin BogomolovandgVisor bot 37a2697933 Automated rollback of changelist 928766376
PiperOrigin-RevId: 928932072
2026-06-08 21:18:53 -07:00
Fabricio VoznikaandgVisor bot e8179c1212 Remove references to config stack
The restored stack was still using the stack that was used to hold configuration,
creating a kind of spit brain scenario, where the old network stack was still
being used by parts of it.

Also added a `poisonableClock` to the early-boot stack. This clock is poisoned
immediately after restore, causing any subsequent clock/timer queries on config
stack components to panic instantly, catching missed configuration cleanup bugs.

PiperOrigin-RevId: 928766376
2026-06-08 14:20:59 -07:00
Nayana BidariandgVisor bot c280c7a90e Add a new flag to allow connected connections on save.
PiperOrigin-RevId: 917036041
2026-05-17 23:21:56 -07:00
Fabricio VoznikaandgVisor bot 0633e2bfb4 Remove unused RestoreStackFromContext()
PiperOrigin-RevId: 912687439
2026-05-08 14:10:03 -07:00
Nayana BidariandgVisor bot 1e6759d3d9 Remove usages of save-restore-netstack flag.
This flag has been true by default for a while, remove its usages and
deprecate the flag.

PiperOrigin-RevId: 903909652
2026-04-22 10:15:08 -07:00
Lucas ManningandgVisor bot 588a60de85 Add a method for stopping external network access while a sandbox is running.
PiperOrigin-RevId: 871368103
2026-02-17 10:00:50 -08:00
Nayana BidariandgVisor bot a41a6099f9 Terminate connections only for restore (not for resume).
Previously, external connections were terminated during both restore and
resume operations, causing a reconnection after every resume. By limiting
termination to the restore only, connections now remain active during resumes
as intended.

Added tests to verify that resume does not terminate the connection.

PiperOrigin-RevId: 868830577
2026-02-12 14:35:26 -08:00
Eric Pauley 6470a54824 tcpip: use RLock for GetNIC functions to reduce lock contention 2026-01-27 14:19:09 -05:00
Shailend ChandandgVisor bot 7920d34651 netlink: Supply IFLA_MASTER in RTM_NEWLINK messages
This allows `ip link show master` to work.

PiperOrigin-RevId: 845470317
2025-12-16 16:07:24 -08:00
Shailend ChandandgVisor bot 775941c0a5 netstack: Remove NIC from bridge when moving between stacks.
PiperOrigin-RevId: 845418560
2025-12-16 13:47:58 -08:00
Shailend ChandandgVisor bot b30c659b42 Add RTNLGRP_LINK netlink multicast support for recv
Only netstack supports sending link events, requests to join multicast
groups for netlink sockets continue to be denied when hostinet is in use.

connect() and sendmsg() to RTMGRP_LINK still fails.

PiperOrigin-RevId: 831688843
2025-11-12 22:04:10 -08:00
Nayana BidariandgVisor bot 54f6111b83 Do not close processors during save, but instead close during restore.
Before this change, the processors were closed in beforeSave which caused the
packet processing for established connections to stop even in the case of
save/resume. Do not close in beforeSave, instead close these processors in
afterLoad for restore.

PiperOrigin-RevId: 821714849
2025-10-20 10:55:13 -07:00
Lucas ManningandgVisor bot a1cbc6f606 Check if CtxResumeStack exists in ResumeStackFromContext.
This change also removes the check for "resume" from processor's
afterLoad. This method is only invoked on restore.

PiperOrigin-RevId: 807408179
2025-09-15 15:34:56 -07:00
Nayana BidariandgVisor bot c52821b3e0 Remove old NICs and routes during save/restore.
During restore there will be new NICs and routes created based on the network
config. Close these old NICs and routes in beforeSave() only for save/restore.
For save/resume, do not close them.

As new link endpoints are created during restore, there is no need to start
the associated old processor goroutines in afterLoad. Start them only for
save/resume.

PiperOrigin-RevId: 804994281
2025-09-09 11:33:49 -07:00
Nayana BidariandgVisor bot 4153537377 Do not save loopback NIC, it is already being set in ReplaceConfig.
We are not saving other NICs, loopback NIC should also not be saved as it is
being updated during restore.

PiperOrigin-RevId: 803543860
2025-09-05 11:47:35 -07:00
Nayana BidariandgVisor bot f49fe582d5 Update iptables and nftables before restoring nics in netstack.
PiperOrigin-RevId: 802725954
2025-09-03 15:19:13 -07:00
Alexander CuevaandgVisor bot 3cf41b9ab6 Hookup nftables filtering into IPv4 and IPv6 paths in netstack.
This change adds functionality to traverse the nftables ruleset
in the IPv4 and IPv6 network endpoints within netstack.

Updates #11778

PiperOrigin-RevId: 799463692
2025-08-26 02:03:26 -07:00
Andrei VaginandgVisor bot 33fa1117f8 netstack: set loopback as outgoing NIC for local traffic
This CL adjusts the network stack's behavior to route traffic addressed to any
of the local IP addresses through the loopback interface. This change is
required to match the Linux behavior.

The CL introduces a loopbackNIC field in the Stack struct to cache the loopback
interface. This cached interface is then used in findLocalRouteRLocked to create
a local route for traffic destined to any local address.

PiperOrigin-RevId: 795893254
2025-08-16 12:55:34 -07:00
Nayana BidariandgVisor bot 3fde3348b0 Fix typo
PiperOrigin-RevId: 791936863
2025-08-06 19:07:29 -07:00
Nayana BidariandgVisor bot 6c7d444961 Return NetworkUnreachable instead of HostUnreachable when routes are not found
Updates #8105

PiperOrigin-RevId: 791875726
2025-08-06 15:52:32 -07:00
Alexander CuevaandgVisor bot 5b9c25cf71 Netlink_Netfilter process message implementation for adding and getting tables.
Added an Nftables field to the stack and functionality and error-checking for
creating a new NFTable (NFT_MSG_NEWTABLE) and retrieving that table
(NFT_MSG_GETTABLE). Removed hostinet tests, as nftables is only supported
for netstack.

PiperOrigin-RevId: 775398315
2025-06-24 15:09:48 -07:00
Nayana BidariandgVisor bot 2276dd10f0 Start TCP dispatchers after endpoints are completely loaded during restore.
- During restore if endpoints' states are not loaded before starting
dispatchers, then the incoming packets will be queued and the dispatcher will
try to send the packets, which will result in a panic as the endpoint might
still be in an invalid state (eg: initial).
- To avoid this, wait for all the endpoints to be loaded completely before
starting the dispatchers by calling AsycnLoading.Wait() in stack.Restore() and
remove this from kernel.LoadFrom()
- Added a test to check save restore of multiple(100) listening sockets.

PiperOrigin-RevId: 762010964
2025-05-22 10:07:11 -07:00
Nayana BidariandgVisor bot 40296833dd Enable netstack save/restore in cloud/gvisor by default.
PiperOrigin-RevId: 712978128
2025-01-07 11:19:26 -08:00
Nayana BidariandgVisor bot cbd03df7b0 Changes to support netstack save restore.
- Added a new Stats() method in inet.Stack to get the saved stats
during restore.
- Mark stack.nic, tcpip.Route and stack.addressState structs as "nosave".
These fields should not be saved because the IP addresses and routes can
change during restore and new configuration of routes and IP addresses will be
extracted from the restore spec and initialized in the saved stack.
- Changes in Restore() method in icmp, udp, tcp, packet and raw endpoint files
to support save restore of these endpoints. These changes are flag guarded by
the TESTONLY-save-restore-netstack flag.

PiperOrigin-RevId: 707639274
2024-12-18 12:52:22 -08:00
Nayana BidariandgVisor bot 832b9b1dd4 Remove TODOs for fields which are restored in netstack.
insecureRNG and secureRNG stack fields are restored in stack.afterLoad()
tcp processor fields are restored in tcp endpoint.Restore()

PiperOrigin-RevId: 704477536
2024-12-09 17:37:04 -08:00
Lucas ManningandgVisor bot 7249eb02dd Require special NIC settings for enabling experiment IP option passing.
PiperOrigin-RevId: 702465832
2024-12-03 14:18:05 -08:00
Kevin KrakauerandgVisor bot a45bc3bfb1 netstack: move TCP state to tcp package and cleanup probe
TCP probe support is implemented, unnecessarily, across the stack and and tcp
packages. It can live entirely in tcp. Additionally, it is only ever set at
initialization time, so support for dynamically adding/removing the probe isn't
necesary.

The probe is getting in the way of adding debugging for b/339664055.

PiperOrigin-RevId: 699330364
2024-11-22 17:11:04 -08:00
Nayana BidariandgVisor bot 1bc1bb3b81 Restore listening connections when netstack s/r is enabled.
This CL restores the listening connections when netstack s/r is enabled.
The changes include:
- New method as a workaround to replace the new routes and nics to the loaded
stack after restore.
- New Restore() for transport layer protocols to restore the protocol level
background workers.
- Adds afterLoad() method for fdbased processors.
- Adds a test to verify listening connection is restored after checkpointing
with netstack s/r enabled.
- Few other changes to save restore fields to enable netstack s/r.

PiperOrigin-RevId: 698453124
2024-11-20 11:13:57 -08:00
Jing ChenandgVisor bot 3f71f6d73a Disallow setting a coordinator for a coordinator device.
The error code is based on the runc's behavior:

```shell
# ip link set dev br0 master br0
Error: Can not enslave a bridge to a bridge.
# echo $?
2
```

Reported-by: [email protected]
PiperOrigin-RevId: 679761515
2024-09-27 16:13:45 -07:00
Jing ChenandgVisor bot 8045b3337a Implement RTM_DELROUTE in netstack.
PiperOrigin-RevId: 675711612
2024-09-17 14:33:41 -07:00
Andrei VaginandgVisor bot 9db706661f Automated rollback of changelist 651095633
PiperOrigin-RevId: 651133212
2024-07-10 13:33:08 -07:00
Lucas ManningandgVisor bot 9ebd7806dc Automated rollback of changelist 650779522
PiperOrigin-RevId: 651095633
2024-07-10 11:37:05 -07:00
Jing ChenandgVisor bot ee22eb3a99 Implement RTM_NEWROUTE in netstack to create/replace a route.
PiperOrigin-RevId: 650814137
2024-07-09 18:04:19 -07:00
Andrei VaginandgVisor bot 533a908afa netstack: call NetworkLink.Close synchronously
PiperOrigin-RevId: 650779522
2024-07-09 15:40:25 -07:00
Jeff MartinandgVisor bot 04b4eac2c0 netstack: remove stale TODOs for multicast forwarding
Support for multicast forwarding was finished by [email protected] in
commit b63c4b3348. These TODOs are no
longer applicable and can be removed

Updates #7338

PiperOrigin-RevId: 650748952
2024-07-09 13:59:59 -07:00
Andrei VaginandgVisor bot 1e132c94ce netstack: implement RTM_SETLINK/IFLA_NET_NS_FD
IFLA_NET_NS_FD specifies a file descriptor that refers to a network namespace.

PiperOrigin-RevId: 648882826
2024-07-02 16:49:27 -07:00
Jing ChenandgVisor bot 144d08a7f7 Implement IFLA_MTU which changes a link's MTU.
PiperOrigin-RevId: 648618069
2024-07-02 00:36:40 -07:00
Jing ChenandgVisor bot 750ff31f5e Implement IFLA_IFNAME which specifies an interface's name.
IFLA_IFNAME is used to set an interface's name when being used
with RTM_NEWLINK and RTM_SETLINK. RTM_NEWLINK is the preferred way, it
is backwards compatible with RTM_SETLINK. For gVisor, when RTM_SETLINK is
introduced, it shall just share the implementation.

PiperOrigin-RevId: 647833657
2024-06-28 17:02:31 -07:00
Andrei VaginandgVisor bot 57a86d3caa netstack: choose route by longest prefix
It has to return the most specific route.

PiperOrigin-RevId: 647421535
2024-06-27 13:12:45 -07:00
Ayush RanjanandgVisor bot efe2391a05 Delete pkg/tcpip/stack.UniqueID.
There are 2 interfaces in gVisor which are exactly the same:
- pkg/tcpip/stack.UniqueID
- pkg/sentry/uniqueid.Provider

Before this change, both were using the Kernel as the unique number generator.
However, we want to decouple the netstack from the kernel. This coupling is
causing bugs in restore because netstack needs to be created before the Kernel
is restored. So right now, netstack ends up using a "temporary" Kernel which is
later destroyed in the restore sequence, but netstack keeps referencing it.

Before this change, the unique ID generator in pkg/tcpip/stack.Stack was used
for 2 things:
1. Provide NIC IDs which are unique across all network namespaces.
2. Implement stack.TransportEndpoint.UniqueID.

(2) is not used anywhere, so deleted it. (1) is overly unique. NIC IDs do not
need to be unique across network namespace. So instead of holding a pointer to
the kernel, added a NIC ID generator in stack.Stack itself and added
NextNICID() function with proper typing.

PiperOrigin-RevId: 646997006
2024-06-26 10:34:48 -07:00
Jing ChenandgVisor bot 338586fe06 Support IFLA_ADDRESS which changes a device's hardware address.
PiperOrigin-RevId: 645611089
2024-06-21 23:56:13 -07:00
Andrei VaginandgVisor bot 064490393c netstack: implement bridge devices
Implement the core part. All packets are broadcast-ed to all ports. The next
step will be to implement forwarding and multicast group databases.

PiperOrigin-RevId: 644471256
2024-06-18 12:15:19 -07:00