Commit Graph
133 Commits
Author SHA1 Message Date
Nayana BidariandgVisor bot 440e31439b Move SO_BINDTODEVICE to socketops.
PiperOrigin-RevId: 348696094
2020-12-22 14:44:02 -08:00
Ayush RanjanandgVisor bot 4bc318854f [netstack] Implement IP(V6)_RECVERR socket option.
PiperOrigin-RevId: 348055514
2020-12-17 11:10:41 -08:00
Ayush RanjanandgVisor bot 130220dd6c [netstack] Implement MSG_ERRQUEUE flag for recvmsg(2).
Introduces the per-socket error queue and the necessary cmsg mechanisms.

PiperOrigin-RevId: 348028508
2020-12-17 08:47:24 -08:00
Nayana BidariandgVisor bot ff7848a5ae Move SO_LINGER option to socketops.
PiperOrigin-RevId: 347437786
2020-12-14 12:03:27 -08:00
Nayana BidariandgVisor bot abf1c4bc0d Move SO_ERROR and SO_OOBINLINE option to socketops.
SO_OOBINLINE option is set/get as boolean value, which is the same as linux.
As we currently do not support disabling this option, we always return it as
true.

PiperOrigin-RevId: 347413905
2020-12-14 10:22:32 -08:00
Ayush RanjanandgVisor bot 8f6c40d2c6 [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
Bhasker HariharanandShentubot 59b6de71b6 Add support for IP_RECVORIGDSTADDR IP option.
Fixes #5004

PiperOrigin-RevId: 346643745
2020-12-09 15:58:53 -08:00
Arthur SfezandgVisor bot c4e3524b5f Export IGMP stats
PiperOrigin-RevId: 346197760
2020-12-07 15:52:25 -08:00
Arthur SfezandgVisor bot e42ab21361 Extract ICMPv4/v6 specific stats to their own types
This change lets us split the v4 stats from the v6 stats, which will be
useful when adding stats for each network endpoint.

PiperOrigin-RevId: 345322615
2020-12-02 15:17:20 -08:00
Ayush RanjanandgVisor bot 86fb20b7cb [netstack] Refactor common utils out of netstack to socket package.
Moved AddressAndFamily() and ConvertAddress() to socket package from netstack.
This helps because these utilities are used by sibling netstack packages.

Such sibling dependencies can later cause circular dependencies. Common utils
shared between siblings should be moved up to the parent.

PiperOrigin-RevId: 345275571
2020-12-02 11:34:42 -08:00
Ayush RanjanandgVisor bot 481841201e [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
Ayush RanjanandgVisor bot a5793b37ab [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
Ayush RanjanandgVisor bot 827beeb3d1 [netstack] Move SO_KEEPALIVE and SO_ACCEPTCONN option to SocketOptions.
PiperOrigin-RevId: 343217712
2020-11-18 21:24:55 -08:00
Ayush RanjanandgVisor bot 6d66aeb734 [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
Ayush RanjanandgVisor bot 7af43c7fff [netstack] Move SO_NO_CHECK option to SocketOptions.
PiperOrigin-RevId: 343146856
2020-11-18 13:42:27 -08:00
Ayush RanjanandgVisor bot e5c8395579 [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
Bhasker HariharanandgVisor bot be3478ec65 Fix SO_ERROR behavior for TCP in gVisor.
Fixes the behaviour of SO_ERROR for tcp sockets where in linux it returns
sk->sk_err and if sk->sk_err is 0 then it returns sk->sk_soft_err. In gVisor TCP
we endpoint.HardError is the equivalent of sk->sk_err and endpoint.LastError
holds soft errors. This change brings this into alignment with Linux such that
both hard/soft errors are cleared when retrieved using getsockopt(.. SO_ERROR)
is called on a socket.

Fixes #3812

PiperOrigin-RevId: 342868552
2020-11-17 08:33:03 -08:00
Nayana BidariandgVisor bot 1d38f400f4 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
Ian LewisandgVisor bot 675637c514 Add basic address deletion to netlink
Updates #3921

PiperOrigin-RevId: 339195417
2020-10-27 00:18:10 -07:00
Jamie LiuandgVisor bot 114d68adb8 Support VFS2 save/restore.
Inode number consistency checks are now skipped in save/restore tests for
reasons described in greatest detail in StatTest.StateDoesntChangeAfterRename.
They pass in VFS1 due to the bug described in new test case
SimpleStatTest.DifferentFilesHaveDifferentDeviceInodeNumberPairs.

Fixes #1663

PiperOrigin-RevId: 338776148
2020-10-23 17:48:33 -07:00
Ayush RanjanandgVisor bot 16279b926e [vfs] kernfs: Implement remaining InodeAttr fields.
Added the following fields in kernfs.InodeAttr:
- blockSize
- atime
- mtime
- ctime

Also resolved all TODOs for #1193.

Fixes #1193

PiperOrigin-RevId: 338714527
2020-10-23 11:43:32 -07:00
Nayana BidariandgVisor bot 11edffc221 Support getsockopt for SO_ACCEPTCONN.
The SO_ACCEPTCONN option is used only on getsockopt(). When this option is
specified, getsockopt() indicates whether socket listening is enabled for
the socket. A value of zero indicates that socket listening is disabled;
non-zero that it is enabled.

PiperOrigin-RevId: 338703206
2020-10-23 10:48:24 -07:00
Andrei VaginandgVisor bot f7a28e92c7 sockets: ignore io.EOF from view.ReadAt
Reported-by: [email protected]
PiperOrigin-RevId: 337451896
2020-10-15 23:15:48 -07:00
Kevin KrakauerandgVisor bot 3273db6579 ip6tables: redirect support
Adds support for the IPv6-compatible redirect target. Redirection is a limited
form of DNAT, where the destination is always the localhost.

Updates #3549.

PiperOrigin-RevId: 334698344
2020-09-30 16:04:26 -07:00
Ghanan GowripalanandgVisor bot 6cdf5e9c7c Don't allow broadcast/multicast source address
As per relevant IP RFCS (see code comments), broadcast (for IPv4) and
multicast addresses are not allowed. Currently checks for these are
done at the transport layer, but since it is explicitly forbidden at
the IP layers, check for them there.

This change also removes the UDP.InvalidSourceAddress stat since there
is no longer a need for it.

Test: ip_test.TestSourceAddressValidation
PiperOrigin-RevId: 334490971
2020-09-29 16:54:23 -07:00
Kevin KrakauerandgVisor bot cdf595466a iptables: refactor to make targets extendable
Like matchers, targets should use a module-like register/lookup system. This
replaces the brittle switch statements we had before.

The only behavior change is supporing IPT_GET_REVISION_TARGET. This makes it
much easier to add IPv6 redirect in the next change.

Updates #3549.

PiperOrigin-RevId: 334469418
2020-09-29 15:02:25 -07:00
Tiwei Bie 3fb942e23f Fix socket record leak in VFS2
VFS2 socket record is not removed from the system-wide
socket table when the socket is released, which will lead
to a memory leak. This patch fixes this issue.

Fixes: #3874

Signed-off-by: Tiwei Bie <[email protected]>
2020-09-24 14:16:12 +08:00
gVisor bot 68f426dd8f Merge pull request #3651 from ianlewis:ip-forwarding
PiperOrigin-RevId: 332760843
2020-09-20 18:17:20 -07:00
Kevin KrakauerandgVisor bot 7e0461c287 Count packets dropped by iptables in IPStats
PiperOrigin-RevId: 332486383
2020-09-18 11:13:19 -07:00
Nayana BidariandgVisor bot 67cfac7dc7 {Set,Get} SO_LINGER on all endpoints.
SO_LINGER is a socket level option and should be stored on all endpoints even
though it is used to linger only for TCP endpoints.

PiperOrigin-RevId: 332369252
2020-09-17 20:00:33 -07:00
Bhasker HariharanandgVisor bot 70679037b2 Return ENOPROTOOPT for all SOL_PACKET options.
This is required to make tcpdump work. tcpdump falls back to not using things
like PACKET_RX_RING if setsockopt returns ENOPROTOOPT. This used to be the case
before https://github.com/google/gvisor/commit/aa8f802bb4c4467a14f62ba76c62b562b05dc9b0.

Fixes #3981

PiperOrigin-RevId: 332326517
2020-09-17 15:16:54 -07:00
Nayana BidariandgVisor bot 4983a9002a Automated rollback of changelist 329526153
PiperOrigin-RevId: 332097286
2020-09-16 15:06:55 -07:00
Rahat MahmoodandgVisor bot 172800eed9 Move the 'marshal' and 'primitive' packages to the 'pkg' directory.
PiperOrigin-RevId: 331256608
2020-09-11 17:42:49 -07:00
Ghanan GowripalanandgVisor bot 88bc846f78 Improve type safety for transport protocol options
The existing implementation for TransportProtocol.{Set}Option take
arguments of an empty interface type which all types (implicitly)
implement; any type may be passed to the functions.

This change introduces marker interfaces for transport protocol options
that may be set or queried which transport protocol option types
implement to ensure that invalid types are caught at compile time.
Different interfaces are used to allow the compiler to enforce read-only
or set-only socket options.

RELNOTES: n/a
PiperOrigin-RevId: 330559811
2020-09-08 12:17:39 -07:00
Bhasker HariharanandgVisor bot 7b1779cbd1 Fix Accept to not return error for sockets in accept queue.
Accept on gVisor will return an error if a socket in the accept queue was closed
before Accept() was called. Linux will return the new fd even if the returned
socket is already closed by the peer say due to a RST being sent by the peer.

This seems to be intentional in linux more details on the github issue.

Fixes #3780

PiperOrigin-RevId: 329828404
2020-09-02 18:21:47 -07:00
Nayana BidariandgVisor bot 338571afdb Automated rollback of changelist 328350576
PiperOrigin-RevId: 329526153
2020-09-01 09:54:55 -07:00
Nicolas LacasseandgVisor bot 4bcdd098d4 Fix kernfs.Dentry reference leak.
PiperOrigin-RevId: 329036994
2020-08-28 17:20:17 -07:00
Ghanan GowripalanandgVisor bot aa8f802bb4 Improve type safety for socket options
The existing implementation for {G,S}etSockOpt take arguments of an
empty interface type which all types (implicitly) implement; any
type may be passed to the functions.

This change introduces marker interfaces for socket options that may be
set or queried which socket option types implement to ensure that invalid
types are caught at compile time. Different interfaces are used to allow
the compiler to enforce read-only or set-only socket options.

Fixes #3714.

RELNOTES: n/a
PiperOrigin-RevId: 328832161
2020-08-27 15:46:44 -07:00
Ghanan GowripalanandgVisor bot a464f8b55b Add function to get error from a tcpip.Endpoint
In an upcoming CL, socket option types are made to implement a marker
interface with pointer receivers. Since this results in calling methods
of an interface with a pointer, we incur an allocation when attempting
to get an Endpoint's last error with the current implementation.

When calling the method of an interface, the compiler is unable to
determine what the interface implementation does with the pointer
(since calling a method on an interface uses virtual dispatch at runtime
so the compiler does not know what the interface method will do) so it
allocates on the heap to be safe incase an implementation continues to
hold the pointer after the functioon returns (the reference escapes the
scope of the object).

In the example below, the compiler does not know what b.foo does with
the reference to a it allocates a on the heap as the reference to a may
escape the scope of a.
```
var a int
var b someInterface
b.foo(&a)
```

This change removes the opportunity for that allocation.

RELNOTES: n/a
PiperOrigin-RevId: 328796559
2020-08-27 12:50:19 -07:00
Kevin KrakauerandgVisor bot e36a47c09f ip6tables: (de)serialize ip6tables structs
More implementation+testing to follow.

#3549.

PiperOrigin-RevId: 328770160
2020-08-27 10:53:49 -07:00
Kevin KrakauerandgVisor bot ca8ed98819 remove iptables sockopt special cases
iptables sockopts were kludged into an unnecessary check, this properly
relegates them to the {get,set}SockOptIP functions.

PiperOrigin-RevId: 328395135
2020-08-25 13:43:26 -07:00
Nayana BidariandgVisor bot 9c66ff57a9 Support SO_LINGER socket option.
When SO_LINGER option is enabled, the close will not return until all the
queued messages are sent and acknowledged for the socket or linger timeout is
reached. If the option is not set, close will return immediately. This option
is mainly supported for connection oriented protocols such as TCP.

PiperOrigin-RevId: 328350576
2020-08-25 10:04:07 -07:00
Bhasker HariharanandgVisor bot d597d9c1e9 Fix TCP_LINGER2 behavior to match linux.
We still deviate a bit from linux in how long we will actually wait in
FIN-WAIT-2. Linux seems to cap it with TIME_WAIT_LEN and it's not completely
obvious as to why it's done that way. For now I think we can ignore that and
fix it if it really is an issue.

PiperOrigin-RevId: 328324922
2020-08-25 07:17:32 -07:00
Bhasker HariharanandgVisor bot 8d07c289ee Skip listening TCP ports when trying to bind a free port.
PiperOrigin-RevId: 327686558
2020-08-20 13:25:23 -07:00
Kevin KrakauerandgVisor bot 4819788737 ip6tables: move ipv4-specific logic into its own file
A later change will introduce the equivalent IPv6 logic.

#3549

PiperOrigin-RevId: 327499064
2020-08-19 13:47:47 -07:00
Ian Lewis 2029379971 Merge branch 'master' into ip-forwarding
- Merges aleksej-paschenko's with HEAD
- Adds vfs2 support for ip_forward
2020-08-17 21:44:31 -04:00
Nayana BidariandgVisor bot 7e262bf6b6 Add loss recovery option for TCP.
/proc/sys/net/ipv4/tcp_recovery is used to enable RACK loss
recovery in TCP.

PiperOrigin-RevId: 325157807
2020-08-05 20:50:06 -07:00
Nayana BidariandgVisor bot 4a8d0d3f20 Plumbing context.Context to DecRef() and Release().
context is passed to DecRef() and Release() which is
needed for SO_LINGER implementation.

PiperOrigin-RevId: 324672584
2020-08-03 13:36:05 -07:00
Kevin Krakauer bad4fe1ade iptables: support SO_ORIGINAL_DST
Envoy (#170) uses this to get the original destination of redirected
packets.
2020-07-31 10:47:26 -07:00
Fabricio VoznikaandgVisor bot 560091c177 Redirect TODO to GitHub issues
PiperOrigin-RevId: 323715260
2020-07-28 21:24:26 -07:00