Commit Graph

150 Commits

Author SHA1 Message Date
Ghanan Gowripalan bdf4e41c86 Always parse Transport headers
..including ICMP headers before delivering them to the
TransportDispatcher.

Updates #3810.

PiperOrigin-RevId: 404404002
2021-10-19 17:25:55 -07:00
Ayush Ranjan 1fe0a6691f Prevent PacketData from being modified.
PacketData should not be modified and should be treated readonly because it
represents packet payload. The old DeleteFront method allowed callers to modify
the underlying buffer which should not be allowed.

Added a way to consume from the PacketData instead of deleting from it.
Updated call points to use that instead.

Reported-by: syzbot+faee5cb350f769a52d1b@syzkaller.appspotmail.com
PiperOrigin-RevId: 399268473
2021-09-27 13:35:22 -07:00
Tony Gong 8b56b6b83b Pass address properties in a single struct
Replaced the current AddAddressWithOptions method with
AddAddressWithProperties which passes all address properties in
a single AddressProperties type. More properties that need to be
configured in the future are expected, so adding a type makes adding
them easier.

PiperOrigin-RevId: 396930729
2021-09-15 15:00:01 -07:00
Zeling Feng 979d6e7d77 Support RTM_DELLINK
This change will allow us to remove the default link in a packetimpact test so
we can reduce indeterministic behaviors as required in https://fxbug.dev/78430.
This will also help with testing #1388.

Updates #578, #1388.

PiperOrigin-RevId: 387896847
2021-07-30 15:41:36 -07:00
Tamir Duberstein 2c1df1f445 Use fake clocks in all tests
...except TCP tests and NDP tests that mutate globals. These will be
undertaken later.

Updates #5940.

PiperOrigin-RevId: 376145608
2021-05-27 05:06:04 -07:00
Tamir Duberstein 4f2439fb0e Use opaque types to represent time
Introduce tcpip.MonotonicTime; replace int64 in tcpip.Clock method
returns with time.Time and MonotonicTime to improve type safety and
ensure that monotonic clock readings are never compared to wall clock
readings.

PiperOrigin-RevId: 375775907
2021-05-25 13:00:29 -07:00
Tamir Duberstein 74b10e31a4 Remove detritus
- Unused constants
- Unused functions
- Unused arguments
- Unkeyed literals
- Unnecessary conversions

PiperOrigin-RevId: 375253464
2021-05-22 05:49:34 -07:00
Arthur Sfez 821cec3f1f Add aggregated NIC stats
This change also includes miscellaneous improvements:
* UnknownProtocolRcvdPackets has been separated into two stats, to
specify at which layer the unknown protocol was found (L3 or L4)
* MalformedRcvdPacket is not aggregated across every endpoint anymore.
Doing it this way did not add useful information, and it was also error-prone
(example: ipv6 forgot to increment this aggregated stat, it only
incremented its own ipv6.MalformedPacketsReceived). It is now only incremented
 the NIC.
* Removed TestStatsString test which was outdated and had no real
utility.

PiperOrigin-RevId: 375057472
2021-05-21 04:27:41 -07:00
Ghanan Gowripalan df2352796d Control forwarding per NetworkEndpoint
...instead of per NetworkProtocol to better conform with linux
(https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt):

```
conf/interface/*

forwarding - BOOLEAN
	Enable IP forwarding on this interface.  This controls whether packets
	received _on_ this interface can be forwarded.
```

Fixes #5932.

PiperOrigin-RevId: 373888000
2021-05-14 16:32:16 -07:00
Ghanan Gowripalan baa0888f11 Rename SetForwarding to SetForwardingDefaultAndAllNICs
...to make it clear to callers that all interfaces are updated with the
forwarding flag and that future NICs will be created with the new
forwarding state.

PiperOrigin-RevId: 373618435
2021-05-13 11:24:20 -07:00
Ghanan Gowripalan 8e40cca4bc Allow handling RAs when forwarding is enabled
...to conform with Linux's `accept_ra` sysctl option.

```
  accept_ra - INTEGER
    Accept Router Advertisements; autoconfigure using them.

    It also determines whether or not to transmit Router
    Solicitations. If and only if the functional setting is to
    accept Router Advertisements, Router Solicitations will be
    transmitted.

    Possible values are:
      0 Do not accept Router Advertisements.
      1 Accept Router Advertisements if forwarding is disabled.
      2 Overrule forwarding behaviour. Accept Router Advertisements
        even if forwarding is enabled.

    Functional default: enabled if local forwarding is disabled.
                        disabled if local forwarding is enabled.
```

PiperOrigin-RevId: 372214640
2021-05-05 15:04:13 -07:00
Ting-Yu Wang a41c5fe217 netstack: Rename pkt.Data().TrimFront() to DeleteFront(), and ...
... it may now invalidate backing slice references

This is currently safe because TrimFront() in VectorisedView only shrinks the
view. This may not hold under the a different buffer implementation.
Reordering method calls order to allow this.

PiperOrigin-RevId: 371167610
2021-04-29 11:43:26 -07:00
Ghanan Gowripalan 47bc115158 Only carry GSO options in the packet buffer
With this change, GSO options no longer needs to be passed around as
a function argument in the write path.

This change is done in preparation for a later change that defers
segmentation, and may change GSO options for a packet as it flows
down the stack.

Updates #170.

PiperOrigin-RevId: 369774872
2021-04-21 18:09:27 -07:00
Nayana Bidari 3fff4c4a0f Move SO_RCVBUF to socketops.
Fixes #2926, #674

PiperOrigin-RevId: 369457123
2021-04-20 09:32:54 -07:00
Kevin Krakauer 10de8978f9 Use nicer formatting for IP addresses in tests
This was semi-automated -- there are many addresses that were not replaced.
Future commits should clean those up.

Parse4 and Parse6 were given their own package because //pkg/test can introduce
dependency cycles, as it depends transitively on //pkg/tcpip and some other
netstack packages.

PiperOrigin-RevId: 368726528
2021-04-15 15:11:04 -07:00
Nick Brown ec0aa657ed Unexpose immutable fields in stack.Route
This change sets the inner `routeInfo` struct to be a named private member
and replaces direct access with access through getters. Note that direct
access to the fields of `routeInfo` is still possible through the `RouteInfo`
struct.

Fixes #4902

PiperOrigin-RevId: 364822872
2021-03-24 09:38:27 -07:00
Ghanan Gowripalan a073d76979 Return tcpip.Error from (*Stack).GetMainNICAddress
PiperOrigin-RevId: 364381970
2021-03-22 12:31:46 -07:00
Ghanan Gowripalan ebd7c1b889 Do not call into Stack from LinkAddressRequest
Calling into the stack from LinkAddressRequest is not needed as we
already have a reference to the network endpoint (IPv6) or network
interface (IPv4/ARP).

PiperOrigin-RevId: 363213973
2021-03-16 10:29:49 -07:00
Ghanan Gowripalan 3e8e2cad88 Make stack.DADResult an interface
While I'm here, update NDPDispatcher.OnDuplicateAddressDetectionStatus to
take a DADResult and rename it to OnDuplicateAddressDetectionResult.

Fixes #5606.

PiperOrigin-RevId: 360965416
2021-03-05 11:54:58 -08:00
Ting-Yu Wang 1cd76d958a Make dedicated methods for data operations in PacketBuffer
One of the preparation to decouple underlying buffer implementation.
There are still some methods that tie to VectorisedView, and they will be
changed gradually in later CLs.

This CL also introduce a new ICMPv6ChecksumParams to replace long list of
parameters when calling ICMPv6Checksum, aiming to be more descriptive.

PiperOrigin-RevId: 360778149
2021-03-03 16:05:16 -08:00
Tamir Duberstein 6bc27946a6 Plumb link address request errors up to requester
Prevent the situation where callers to (*stack).GetLinkAddress provide
incorrect arguments and are unable to observe this condition.

Updates #5583.

PiperOrigin-RevId: 360481557
2021-03-02 11:58:12 -08:00
Ghanan Gowripalan 39251f31cb Support performing DAD for any address
...as long as the network protocol supports duplicate address detection.

This CL provides the facilities for a netstack integrator to perform
DAD.

DHCP recommends that clients effectively perform DAD before accepting an
offer. As per RFC 2131 section 4.4.1 pg 38,

  The client SHOULD perform a check on the suggested address to ensure
  that the address is not already in use.  For example, if the client
  is on a network that supports ARP, the client may issue an ARP request
  for the suggested request.

The implementation of ARP-based IPv4 DAD effectively operates the same
as IPv6's NDP DAD - using ARP requests and responses in place of
NDP neighbour solicitations and advertisements, respectively.

DAD performed by calls to (*Stack).CheckDuplicateAddress don't interfere
with DAD performed when a new IPv6 address is added. This is so that
integrator requests to check for duplicate addresses aren't unexpectedly
aborted when addresses are removed.

A network package internal package provides protocol agnostic DAD state
management that specific protocols that provide DAD can use.

Fixes #4550.

Tests:
  - internal/ip_test.*
  - integration_test.TestDAD
  - arp_test.TestDADARPRequestPacket
  - ipv6.TestCheckDuplicateAddress
PiperOrigin-RevId: 356405593
2021-02-08 19:05:45 -08:00
Ghanan Gowripalan 3853a94f10 Remove linkAddrCache
It was replaced by NUD/neighborCache.

Fixes #4658.

PiperOrigin-RevId: 356085221
2021-02-06 21:37:15 -08:00
Ghanan Gowripalan 24416032ab Refactor locally delivered packets
Make it clear that failing to parse a looped back is not a packet
sending error but a malformed received packet error.

FindNetworkEndpoint returns nil when no network endpoint is found
instead of an error.

PiperOrigin-RevId: 355954946
2021-02-05 16:47:11 -08:00
Ghanan Gowripalan ebd3912c0f Refactor HandleControlPacket/SockError
...to remove the need for the transport layer to deduce the type of
error it received.

Rename HandleControlPacket to HandleError as HandleControlPacket only
handles errors.

tcpip.SockError now holds a tcpip.SockErrorCause interface that
different errors can implement.

PiperOrigin-RevId: 354994306
2021-02-01 12:04:03 -08:00
Ghanan Gowripalan 4ee8cf8734 Use different neighbor tables per network endpoint
This stores each protocol's neighbor state separately.

This change also removes the need for each neighbor entry to keep
track of their own link address resolver now that all the entries
in a cache will use the same resolver.

PiperOrigin-RevId: 354818155
2021-01-31 11:33:46 -08:00
Ghanan Gowripalan daeb06d2cb Hide neighbor table kind from NetworkEndpoint
The network endpoint should not need to have logic to handle different
kinds of neighbor tables. Network endpoints can let the NIC know about
differnt neighbor discovery messages and let the NIC decide which table
to update.

This allows us to remove the LinkAddressCache interface.

PiperOrigin-RevId: 354812584
2021-01-31 10:03:46 -08: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
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
Arthur Sfez 18ebec0ec9 Refactor GetMainNICAddress
It previously returned an error but it could only be UnknownNICID. It now
returns a boolean to indicate whether the nic exists or not.

PiperOrigin-RevId: 353337489
2021-01-22 16:12:12 -08:00
Ghanan Gowripalan e0f4e46e34 Resolve static link addresses in GetLinkAddress
If a network address has a static mapping to a link address, calculate
it in GetLinkAddress.

Test: stack_test.TestStaticGetLinkAddress
PiperOrigin-RevId: 353179616
2021-01-21 23:26:40 -08:00
Ghanan Gowripalan 9f46328e11 Only use callback for GetLinkAddress
GetLinkAddress's callback will be called immediately with a
stack.LinkResolutionResult which will hold the link address
so no need to also return the link address from the function.

Fixes #5151.

PiperOrigin-RevId: 353157857
2021-01-21 19:55:37 -08:00
Arthur Sfez be17b94446 Per NIC NetworkEndpoint statistics
To facilitate the debugging of multi-homed setup, track Network
protocols statistics for each endpoint. Note that the original
stack-wide stats still exist.

A new type of statistic counter is introduced, which track two
versions of a stat at the same time. This lets a network endpoint
increment both the local stat and the stack-wide stat at the same
time.

Fixes #4605

PiperOrigin-RevId: 352663276
2021-01-19 15:07:39 -08:00
Ghanan Gowripalan cd75bb163f Resolve known link address on route creation
If a Route is being created through a link that requires link address
resolution and a remote address that has a known mapping to a link
address, populate the link address when the route is created.

This removes the need for neighbor/link address caches to perform this
check.

Fixes #5149

PiperOrigin-RevId: 352122401
2021-01-15 18:49:22 -08:00
Ghanan Gowripalan 2814a032be Support GetLinkAddress with neighborCache
Test: integration_test.TestGetLinkAddress
PiperOrigin-RevId: 352119404
2021-01-15 18:15:26 -08:00
Ghanan Gowripalan c49ce8ca8a Clear neighbor table on NIC down
Note, this includes static entries to match linux's behaviour.

```
  $ ip neigh show dev eth0
  192.168.42.1 lladdr fc:ec:da:70:6e:f9 STALE
  $ sudo ip neigh add 192.168.42.172 lladdr 22:33:44:55:66:77 dev eth0
  $ ip neigh show dev eth0
  192.168.42.1 lladdr fc:ec:da:70:6e:f9 STALE
  192.168.42.172 lladdr 22:33:44:55:66:77 PERMANENT
  $ sudo ifconfig eth0 down
  $ ip neigh show dev eth0
  $ sudo ifconfig eth0 up
  $ ip neigh show dev eth0
```

Test: stack_test.TestClearNeighborCacheOnNICDisable
PiperOrigin-RevId: 351696306
2021-01-13 17:12:29 -08: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
Ghanan Gowripalan 620de250a4 Prefer matching labels and longest matching prefix
...when performing source address selection for IPv6.

These are defined in RFC 6724 section 5 rule 6 (prefer matching label)
and rule 8 (use longest matching prefix).

This change also considers ULA of global scope instead of its own scope,
as per RFC 6724 section 3.1:

   Also, note that ULAs are considered as global, not
   site-local, scope but are handled via the prefix policy table as
   discussed in Section 10.6.

Test: stack_test.TestIPv6SourceAddressSelectionScope

Startblock:
  has LGTM from peterjohnston
  and then
  add reviewer brunodalbo
PiperOrigin-RevId: 348580996
2020-12-21 22:26:10 -08:00
Peter Johnston 3ff1aef544 Make `stack.Route` thread safe
Currently we rely on the user to take the lock on the endpoint that owns the
route, in order to modify it safely. We can instead move
`Route.RemoteLinkAddress` under `Route`'s mutex, and allow non-locking and
thread-safe access to other fields of `Route`.

PiperOrigin-RevId: 345461586
2020-12-03 08:54:24 -08: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
Ghanan Gowripalan 1de08889df Deduplicate code in ipv6.protocol
PiperOrigin-RevId: 344009602
2020-11-24 01:19:42 -08:00
Bruno Dal Bo 9d148627f8 Introduce stack.WritePacketToRemote, remove LinkEndpoint.WriteRawPacket
Redefine stack.WritePacket into stack.WritePacketToRemote which lets the NIC
decide whether to append link headers.

PiperOrigin-RevId: 343071742
2020-11-18 07:05:59 -08:00
Ghanan Gowripalan 1a972411b3 Move packet handling to NetworkEndpoint
The NIC should not hold network-layer state or logic - network packet
handling/forwarding should be performed at the network layer instead
of the NIC.

Fixes #4688

PiperOrigin-RevId: 342166985
2020-11-12 17:33:21 -08: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
Ian Lewis 59e2c9f16a Add basic address deletion to netlink
Updates #3921

PiperOrigin-RevId: 339195417
2020-10-27 00:18:10 -07:00
Ghanan Gowripalan 6e6a9d3f3d Find route before sending NA response
This change also brings back the stack.Route.ResolveWith method so that
we can immediately resolve a route when sending an NA in response to a
a NS with a source link layer address option.

Test: ipv6_test.TestNeighorSolicitationResponse
PiperOrigin-RevId: 337185461
2020-10-14 15:29:47 -07:00
Ghanan Gowripalan 257703c050 Automated rollback of changelist 336304024
PiperOrigin-RevId: 336339194
2020-10-09 12:09:12 -07:00
Bhasker Hariharan 8566decab0 Automated rollback of changelist 336185457
PiperOrigin-RevId: 336304024
2020-10-09 09:11:18 -07:00
Ghanan Gowripalan 6768e6c59e Do not resolve routes immediately
When a response needs to be sent to an incoming packet, the stack should
consult its neighbour table to determine the remote address's link
address.

When an entry does not exist in the stack's neighbor table, the stack
should queue the packet while link resolution completes. See comments.

PiperOrigin-RevId: 336185457
2020-10-08 16:15:59 -07:00
Ghanan Gowripalan e5ece9aea7 Return permanent addresses when NIC is down
Test: stack_test.TestGetMainNICAddressWhenNICDisabled
PiperOrigin-RevId: 334513286
2020-09-29 19:46:50 -07:00