Commit Graph

170 Commits

Author SHA1 Message Date
gVisor bot 796fd943e2 Merge release-20210208.0-101-gda2505df9 (automated) 2021-02-26 19:23:18 +00:00
Tamir Duberstein da2505df94 Use closure to avoid manual unlocking
Also increase refcount of raw.endpoint.route while in use.

Avoid allocating an array of size zero.

PiperOrigin-RevId: 359797788
2021-02-26 11:18:30 -08:00
gVisor bot c35f73b493 Merge release-20210208.0-97-g38c42bbf4 (automated) 2021-02-25 21:40:11 +00:00
Kevin Krakauer 38c42bbf4a Remove deadlock in raw.endpoint caused by recursive read locking
Prevents the following deadlock:
- Raw packet is sent via e.Write(), which read locks e.mu
- Connect() is called, blocking on write locking e.mu
- The packet is routed to loopback and back to e.HandlePacket(), which read
  locks e.mu

Per the atomic.RWMutex documentation, this deadlocks:

"If a goroutine holds a RWMutex for reading and another goroutine might call
Lock, no goroutine should expect to be able to acquire a read lock until the
initial read lock is released. In particular, this prohibits recursive read
locking. This is to ensure that the lock eventually becomes available; a blocked
Lock call excludes new readers from acquiring the lock."

Also, release eps.mu earlier in deliverRawPacket.

PiperOrigin-RevId: 359600926
2021-02-25 13:35:44 -08:00
gVisor bot ed0a3c9243 Merge release-20210125.0-21-g8d1afb418 (automated) 2021-01-29 02:16:39 +00:00
Tamir Duberstein 8d1afb4185 Change tcpip.Error to an interface
This makes it possible to add data to types that implement tcpip.Error.
ErrBadLinkEndpoint is removed as it is unused.

PiperOrigin-RevId: 354437314
2021-01-28 17:59:58 -08:00
gVisor bot 14ee229ad6 Merge release-20210112.0-98-g8e6604474 (automated) 2021-01-27 02:19:18 +00:00
Nayana Bidari 8e66044741 Initialize the send buffer handler in endpoint creation.
- This CL will initialize the function handler used for getting the send
buffer size limits during endpoint creation and does not require the caller of
SetSendBufferSize(..) to know the endpoint type(tcp/udp/..)

PiperOrigin-RevId: 353992634
2021-01-26 18:05:29 -08:00
gVisor bot fcb89d2d29 Merge release-20210112.0-95-ga90661654 (automated) 2021-01-26 20:48:14 +00:00
Bhasker Hariharan a90661654d Fix couple of potential route leaks.
connect() can be invoked multiple times on UDP/RAW sockets and in such
a case we should release the cached route from the previous connect.

Fixes #5359

PiperOrigin-RevId: 353919891
2021-01-26 12:09:10 -08:00
gVisor bot 270d619546 Merge release-20210112.0-92-g9ba24d449 (automated) 2021-01-26 19:04:34 +00:00
Ghanan Gowripalan 9ba24d449f Drop nicID from transport endpoint reg/cleanup fns
...as it is unused.

PiperOrigin-RevId: 353896981
2021-01-26 10:35:14 -08:00
gVisor bot 894327c74e Merge release-20210112.0-90-gdaf0d3f6c (automated) 2021-01-26 16:42:14 +00:00
Nayana Bidari daf0d3f6ca Move SO_SNDBUF to socketops.
This CL moves {S,G}etsockopt of SO_SNDBUF from all endpoints to socketops. For
unix sockets, we do not support setting of this option.

PiperOrigin-RevId: 353871484
2021-01-26 08:25:34 -08:00
gVisor bot 993bee6697 Merge release-20210112.0-74-g6c0e1d9cf (automated) 2021-01-22 20:40:39 +00:00
Tamir Duberstein 6c0e1d9cfe Define tcpip.Payloader in terms of io.Reader
Fixes #1509.

PiperOrigin-RevId: 353295589
2021-01-22 12:26:09 -08:00
gVisor bot 06bcbc23fd Merge release-20210112.0-37-g12d979083 (automated) 2021-01-16 00:07:58 +00:00
Tamir Duberstein 12d9790833 Remove count argument from tcpip.Endpoint.Read
The same intent can be specified via the io.Writer.

PiperOrigin-RevId: 352098747
2021-01-15 15:49:15 -08:00
gVisor bot fc9aec0925 Merge release-20201216.0-105-g25b5ec713 (automated) 2021-01-14 00:18:26 +00:00
Ghanan Gowripalan 25b5ec7135 Do not resolve remote link address at transport layer
Link address resolution is performed at the link layer (if required) so
we can defer it from the transport layer. When link resolution is
required, packets will be queued and sent once link resolution
completes. If link resolution fails, the transport layer will receive a
control message indicating that the stack failed to route the packet.

tcpip.Endpoint.Write no longer returns a channel now that writes do not
wait for link resolution at the transport layer.

tcpip.ErrNoLinkAddress is no longer used so it is removed.

Removed calls to stack.Route.ResolveWith from the transport layer so
that link resolution is performed when a route is created in response
to an incoming packet (e.g. to complete TCP handshakes or send a RST).

Tests:
- integration_test.TestForwarding
- integration_test.TestTCPLinkResolutionFailure

Fixes #4458

RELNOTES: n/a
PiperOrigin-RevId: 351684158
2021-01-13 16:04:33 -08:00
gVisor bot e524c21569 Merge release-20201216.0-82-g4c4de6644 (automated) 2021-01-11 21:27:53 +00:00
gVisor bot f1f3952a22 Merge release-20201208.0-127-gb1de1da31 (automated) 2021-01-07 22:25:30 +00:00
Ting-Yu Wang b1de1da318 netstack: Refactor tcpip.Endpoint.Read
Read now takes a destination io.Writer, count, options. Keeping the method name
Read, in contrast to the Write method.

This enables:
* direct transfer of views under VV
* zero copy

It also eliminates the need for sentry to keep a slice of view because
userspace had requested a read that is smaller than the view returned, removing
the complexity there.

Read/Peek/ReadPacket are now consolidated together and some duplicate code is
removed.

PiperOrigin-RevId: 350636322
2021-01-07 14:17:18 -08:00
gVisor bot 5c21c7c3bd Merge release-20201208.0-89-g3ff7324df (automated) 2020-12-28 22:05:49 +00:00
Peter Johnston fee2cd640f Invoke address resolution upon subsequent traffic to Failed neighbor
Removes the period of time in which subseqeuent traffic to a Failed neighbor
immediately fails with ErrNoLinkAddress. A Failed neighbor is one in which
address resolution fails; or in other words, the neighbor's IP address cannot
be translated to a MAC address.

This means removing the Failed state for linkAddrCache and allowing transitiong
out of Failed into Incomplete for neighborCache. Previously, both caches would
transition entries to Failed after address resolution fails. In this state, any
subsequent traffic requested within an unreachable time would immediately fail
with ErrNoLinkAddress. This does not follow RFC 4861 section 7.3.3:

  If address resolution fails, the entry SHOULD be deleted, so that subsequent
  traffic to that neighbor invokes the next-hop determination procedure again.
  Invoking next-hop determination at this point ensures that alternate default
  routers are tried.

The API for getting a link address for a given address, whether through the link
address cache or the neighbor table, is updated to optionally take a callback
which will be called when address resolution completes. This allows `Route` to
handle completing link resolution internally, so callers of (*Route).Resolve
(e.g. endpoints) don’t have to keep track of when it completes and update the
Route accordingly.

This change also removes the wakers from LinkAddressCache, NeighborCache, and
Route in favor of the callbacks, and callers that previously used a waker can
now just pass a callback to (*Route).Resolve that will notify the waker on
resolution completion.

Fixes #4796

Startblock:
  has LGTM from sbalana
  and then
  add reviewer ghanan
PiperOrigin-RevId: 348597478
2020-12-22 01:37:05 -08:00
Kevin Krakauer 981faa2c12 RLock Endpoint in raw.Endpoint.HandlePacket
PiperOrigin-RevId: 348530530
2020-12-21 14:44:39 -08:00
Nayana Bidari b15acae9a6 Fix error code for connect in raw sockets.
PiperOrigin-RevId: 347650354
2020-12-15 11:07:48 -08:00
gVisor bot 22ae84e87a Merge release-20201208.0-43-g2e191cb3f (automated) 2020-12-14 20:11:32 +00:00
Nayana Bidari 2e191cb3f7 Move SO_LINGER option to socketops.
PiperOrigin-RevId: 347437786
2020-12-14 12:03:27 -08:00
gVisor bot 3c8ccf6f45 Merge release-20201208.0-34-gd45420b15 (automated) 2020-12-11 21:38:36 +00:00
Nayana Bidari d45420b152 Fix panic when IPv4 address is used in sendmsg for IPv6 sockets
We do not rely on error for getsockopt options(which have boolean values)
anymore. This will cause issue in sendmsg where we used to return error
for IPV6_V6Only option. Fix the panic by returning error (for sockets other
than TCP and UDP) if the address does not match the type(AF_INET/AF_INET6) of
the socket.

PiperOrigin-RevId: 347063838
2020-12-11 13:31:28 -08:00
gVisor bot e643a3498e Merge release-20201208.0-28-gaf4afdc0e (automated) 2020-12-11 18:40:22 +00:00
Ayush Ranjan af4afdc0e0 [netstack] Decouple tcpip.ControlMessages from the IP control messges.
tcpip.ControlMessages can not contain Linux specific structures which makes it
painful to convert back and forth from Linux to tcpip back to Linux when passing
around control messages in hostinet and raw sockets.

Now we convert to the Linux version of the control message as soon as we are
out of tcpip.

PiperOrigin-RevId: 347027065
2020-12-11 10:33:58 -08:00
gVisor bot bf476d6586 Merge release-20201109.0-120-gad8311242 (automated) 2020-11-26 08:46:41 +00:00
Ayush Ranjan ad83112423 [netstack] Add SOL_TCP options to SocketOptions.
Ports the following options:
- TCP_NODELAY
- TCP_CORK
- TCP_QUICKACK

Also deletes the {Get/Set}SockOptBool interface methods from all implementations

PiperOrigin-RevId: 344378824
2020-11-26 00:43:13 -08:00
gVisor bot 55c38174d6 Merge release-20201109.0-119-gbebadb518 (automated) 2020-11-26 04:04:29 +00:00
Ayush Ranjan bebadb5182 [netstack] Add SOL_IP and SOL_IPV6 options to SocketOptions.
We will use SocketOptions for all kinds of options, not just SOL_SOCKET options
because (1) it is consistent with Linux which defines all option variables on
the top level socket struct, (2) avoid code complexity. Appropriate checks
have been added for matching option level to the endpoint type.

Ported the following options to this new utility:
- IP_MULTICAST_LOOP
- IP_RECVTOS
- IPV6_RECVTCLASS
- IP_PKTINFO
- IP_HDRINCL
- IPV6_V6ONLY

Changes in behavior (these are consistent with what Linux does AFAICT):
- Now IP_MULTICAST_LOOP can be set for TCP (earlier it was a noop) but does not
  affect the endpoint itself.
- We can now getsockopt IP_HDRINCL (earlier we would get an error).
- Now we return ErrUnknownProtocolOption if SOL_IP or SOL_IPV6 options are used
  on unix sockets.
- Now we return ErrUnknownProtocolOption if SOL_IPV6 options are used on non
  AF_INET6 endpoints.

This change additionally makes the following modifications:
- Add State() uint32 to commonEndpoint because both tcpip.Endpoint and
  transport.Endpoint interfaces have it. It proves to be quite useful.
- Gets rid of SocketOptionsHandler.IsListening(). It was an anomaly as it was
  not a handler. It is now implemented on netstack itself.
- Gets rid of tcp.endpoint.EndpointInfo and directly embeds
  stack.TransportEndpointInfo. There was an unnecessary level of embedding
  which served no purpose.
- Removes some checks dual_stack_test.go that used the errors from
  GetSockOptBool(tcpip.V6OnlyOption) to confirm some state. This is not
  consistent with the new design and also seemed to be testing the
  implementation instead of behavior.

PiperOrigin-RevId: 344354051
2020-11-25 20:01:10 -08:00
gVisor bot 2442e44c4b Merge release-20201109.0-117-g2485a4e2c (automated) 2020-11-25 22:56:14 +00:00
Ghanan Gowripalan 2485a4e2cb Make stack.Route safe to access concurrently
Multiple goroutines may use the same stack.Route concurrently so
the stack.Route should make sure that any functions called on it
are thread-safe.

Fixes #4073

PiperOrigin-RevId: 344320491
2020-11-25 14:52:59 -08:00
gVisor bot 317cc02865 Merge release-20201109.0-84-ge5650d124 (automated) 2020-11-19 05:28:06 +00:00
Ayush Ranjan e5650d1240 [netstack] Move SO_KEEPALIVE and SO_ACCEPTCONN option to SocketOptions.
PiperOrigin-RevId: 343217712
2020-11-18 21:24:55 -08:00
gVisor bot 0e43c0c8fb Merge release-20201109.0-79-gdf37babd5 (automated) 2020-11-18 22:39:51 +00:00
Ayush Ranjan df37babd57 [netstack] Move SO_REUSEPORT and SO_REUSEADDR option to SocketOptions.
This changes also introduces:
- `SocketOptionsHandler` interface which can be implemented by endpoints to
  handle endpoint specific behavior on SetSockOpt. This is analogous to what
  Linux does.
- `DefaultSocketOptionsHandler` which is a default implementation of the above.
  This is embedded in all endpoints so that we don't have to uselessly
  implement empty functions. Endpoints with specific behavior can override the
  embedded method by manually defining its own implementation.

PiperOrigin-RevId: 343158301
2020-11-18 14:36:41 -08:00
gVisor bot 38e95b7a09 Merge release-20201109.0-71-gfc342fb43 (automated) 2020-11-18 18:22:53 +00:00
Ayush Ranjan fc342fb439 [netstack] Move SO_PASSCRED option to SocketOptions.
This change also makes the following fixes:
- Make SocketOptions use atomic operations instead of having to acquire/drop
  locks upon each get/set option.
- Make documentation more consistent.
- Remove tcpip.SocketOptions from socketOpsCommon because it already exists
  in transport.Endpoint.
- Refactors get/set socket options tests to be easily extendable.

PiperOrigin-RevId: 343103780
2020-11-18 10:19:33 -08:00
gVisor bot f560fd07ef Merge release-20201030.0-83-g5bb64ce1b (automated) 2020-11-13 07:00:09 +00:00
Nayana Bidari 5bb64ce1b8 Refactor SOL_SOCKET options
Store all the socket level options in a struct and call {Get/Set}SockOpt on
this struct. This will avoid implementing socket level options on all
endpoints. This CL contains implementing one socket level option for tcp and
udp endpoints.

PiperOrigin-RevId: 342203981
2020-11-12 22:57:00 -08:00
gVisor bot 1e0cc55833 Merge release-20201030.0-34-g8c0701462 (automated) 2020-11-06 00:01:49 +00:00
Ghanan Gowripalan 8c0701462a Use stack.Route exclusively for writing packets
* Remove stack.Route from incoming packet path.
There is no need to pass around a stack.Route during the incoming path
of a packet. Instead, pass around the packet's link/network layer
information in the packet buffer since all layers may need this
information.

* Support address bound and outgoing packet NIC in routes.
When forwarding is enabled, the source address of a packet may be bound
to a different interface than the outgoing interface. This change
updates stack.Route to hold both NICs so that one can be used to write
packets while the other is used to check if the route's bound address
is valid. Note, we need to hold the address's interface so we can check
if the address is a spoofed address.

* Introduce the concept of a local route.
Local routes are routes where the packet never needs to leave the stack;
the destination is stack-local. We can now route between interfaces
within a stack if the packet never needs to leave the stack, even when
forwarding is disabled.

* Always obtain a route from the stack before sending a packet.
If a packet needs to be sent in response to an incoming packet, a route
must be obtained from the stack to ensure the stack is configured to
send packets to the packet's source from the packet's destination.

* Enable spoofing if a stack may send packets from unowned addresses.
This change required changes to some netgophers since previously,
promiscuous mode was enough to let the netstack respond to all
incoming packets regardless of the packet's destination address. Now
that a stack.Route is not held for each incoming packet, finding a route
may fail with local addresses we don't own but accepted packets for
while in promiscuous mode. Since we also want to be able to send from
any address (in response the received promiscuous mode packets), we need
to enable spoofing.

* Skip transport layer checksum checks for locally generated packets.
If a packet is locally generated, the stack can safely assume that no
errors were introduced while being locally routed since the packet is
never sent out the wire.

Some bugs fixed:
- transport layer checksum was never calculated after NAT.
- handleLocal didn't handle routing across interfaces.
- stack didn't support forwarding across interfaces.
- always consult the routing table before creating an endpoint.

Updates #4688
Fixes #3906

PiperOrigin-RevId: 340943442
2020-11-05 15:52:16 -08:00
gVisor bot ce44e4f40f Merge release-20201019.0-37-g39e9b3bb8 (automated) 2020-10-23 17:51:50 +00:00