Commit graph

226 commits

Author SHA1 Message Date
Michael Scott eb9055c019 net: tcp: move accept_cb from net_context to net_tcp
net_context is used for more than just TCP contexts.  However,
the accept_cb field is only used for TCP.  Let's move it from
the generic net_context structure to the TCP specific net_tcp
structure.

Change-Id: If923c7aba1355cf5f91c07a7e7e469d385c7c365
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Michael Scott 2c3faa0de2 net: tcp: fix buffer leak in tcp_synack_received
When SYNACK is received we dont hand off the netbuf to anything
which will call net_nbuf_unref, so let's not mark it NET_OK.
Instead let the code path fall through to mark it NET_DROP.

Change-Id: I1f883e1a13c53c930bf50c07ff701e3db6f02d8a
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Michael Scott 87914610b0 net: tcp: if buffer is TCP_FIN increment send_ack by 1
Now that the TCP_FIN block is after a potential packet_received()
tcp->send_ack should be appropriately set to the last sequence
processed.

In the case of a TCP_FIN buffer, we should advance it by 1 or else
the destination will continue to retry to send the last block.

Change-Id: I9c97d35a87ad6cc1a50f928b237780bff4cd2877
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Michael Scott c1f822222b net: tcp: don't assume TCP_FIN buffers are NET_DROP
Since we default to a return value of NET_DROP, we
can remove the automatic NET_DROP in the TCP_FIN block.

The return value will be set to NET_OK by packet_received()
if appropriate data is found which needs to be sent to
the callback.

Change-Id: Ib2634ba34440ca7053a4e98bf80f12cf6fbbd361
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Michael Scott 29b7d9c644 net: tcp: handle TCP_FIN after processing any data in the buffer
Buffers marked TCP_FIN may still have data attached to them
which needs to be processed and handed back to the callback.

Let's move the TCP_FIN handling to after the data processing
section now that we have a copy of the TCP flags to do this.

Change-Id: I90f53b10e393024ebffebe1837b8866764b8a7ac
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Michael Scott da7e00e3aa net: tcp: in tcp_establish save TCP flags for post-processing use
buffer TCP flags can be cleared during packet_received so let's
save a copy of them for later.

Change-Id: I401e99c1ed2723dac4e86da58635b548a5645c13
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
Leandro Pereira 46c0997e8b net: tcp: Reduce size of state member from 32 to 4 bits
No more than 4 bits are necessary to store the state of a TCP connection,
so better pack it using bitfields so that it uses only 4 bits instead of
32, by sharing space with `retry_timeout_shift` and `flags` fields.

There are 12 (or 14, if you count the 2 unused bits in the `flags`
field) bits remaining in the same dword, but I don't know what to to
stuff there yet.

This also changes all direct field access for the `state` field to
function calls.  These functions are provided as `static inline`
functions and they perform only casts, so there's no function call
overhead.

Change-Id: I0197462caa0b71b287c0773ec5cd2dd4101a4766
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-20 16:23:17 +02:00
Leandro Pereira 9a558c2aaa net: tcp: Remove unused recv_ack field from TCP context
This value is never set (always zero), so it's safe to remove it from
the net_tcp struct.

Change-Id: Ie4c1d90204a9834f2223b09828af42ee101bd045
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-20 16:23:17 +02:00
Andy Ross d4b9b6c1bf net: tcp: Destroy net_tcp struct at the same time as the context
The net_tcp struct was being cleaned up and destroyed when the
outbound FIN packet is sent on a connection that already received an
inbound FIN.  That's not right, per spec we need to wait for the ACK
(though this would be benign cheating).  And worse: there were code
paths which were themselves spec-compliant where the net_tcp struct
(now a NULL pointer) would be used after this spot leading to
occasional crazy behavior on socket close.

Don't do it this way.  Clean up the TCP struct at the same time we
destroy the net_context.  Much saner that way.

Change-Id: I4bc6b97eb0b71a7fa8faea02c1eb4c4d3bd3ae6d
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-20 16:23:17 +02:00
Andy Ross 01ae35b58e net: tcp: Pass correct user_data pointers
The TCP stack inherited msot of the user_data management from UDP, but
it doesn't quite work.  It's not possible to have a single pointer in
the general case, as e.g. a net_context_send() call may happen
synchronously underneath a recv callback and clobber the pointer, even
though there will be much more data coming later on the active stream.

Put a recv_user_data field into the TCP struct and use that.  Long
term, it would be good to revisit this and come up with a unified
solution that works for both.  There is yet another "user_data"
pointer in net_connection that seem likely to overlap too.

Change-Id: Id3a8eca64fc680e0e80b74944c4d621d7810a8fe
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-20 16:23:17 +02:00
David B. Kinder ac74d8b652 license: Replace Apache boilerplate with SPDX tag
Replace the existing Apache 2.0 boilerplate header with an SPDX tag
throughout the zephyr code tree. This patch was generated via a
script run over the master branch.

Also updated doc/porting/application.rst that had a dependency on
line numbers in a literal include.

Manually updated subsys/logging/sys_log.c that had a malformed
header in the original file.  Also cleanup several cases that already
had a SPDX tag and we either got a duplicate or missed updating.

Jira: ZEP-1457

Change-Id: I6131a1d4ee0e58f5b938300c2d2fc77d2e69572c
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-01-19 03:50:58 +00:00
Andy Ross fc35e649ff net: tcp: Don't leak net_conn_handles
net_context_put() forgot to release the conn_handler field causing
subsequent failures in net_conn_register() when they ran out.

Change-Id: I0d306b5035199422fa8788338ac9da8d1900d5f9
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-13 10:29:03 +01:00
Andy Ross 96294b79a7 net: tcp: Signal EOF with a NULL buffer in the callback
TCP didn't actually have a way to signal synchronous receipt of a FIN
packet.  Extend the recv_cb API to allow a NULL buf argument with
status==0 (by analogy to Unix's zero-length read) to signal EOF.

Update docs too, and also echo_server which wasn't prepared to handle
this situation.

Change-Id: I7dc08f9e262a81dcad9c670c6471898889f0b05d
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-13 10:29:03 +01:00
Michael Scott a972d03ce7 net: ip: save TCP seq/ack values in tcp_synack_received
The tcp_synack_received() function ends with a call to send_ack().
However, if we don't update the sequence and ACK values, we'll send back
headers with 0 values and the destination will try resending over and
over.

Fix this by saving the seq and ack values when a TCP_SYN is flagged
in the header (which should be the case almost any time this function
is used as a callback).

Change-Id: I57f07ce719f2b6e2fb34c96c867d2e1c37f342ba
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:03 +01:00
Michael Scott f23ca0e5e1 net: ip: reword appdata debug message in packet_received
Trivial text tweak for clarity.

Change-Id: I470d54d86de7ddc4e83e3ad871448c244134c117
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:03 +01:00
Michael Scott 742b5e22ea net: ip: set context state to NET_CONTEXT_CONNECTED on synack success
Once SYNACK has been received for a TCP connection, we need to set the
net context state to NET_CONTEXT_CONNECTED or else calls to sendto()
will fail with -ENOTCONN.

Change-Id: Idd78e1dcdd5ac0bca5d3fba40b59ab8fde6b8729
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:02 +01:00
Michael Scott c306844583 net: ip: set local address family during TCP connect
When calling net_context_connect, the local address family
is never set prior to calling net_tcp_register.  This generates
an error:
"Local address family not set." (-EINVAL)

Let's set the local address family prior to this call.

Change-Id: Ic5f2edf684d14f9bb77019c49c95e5524a406417
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:02 +01:00
Michael Scott a2464d049f net: ip: on synack copy local/remote data prior to net_tcp_register
When TCP SYNACK is received we register the connection via the
net_tcp_register function.  During this call several errors are
generated concerning local and remote address information not
being set.

Let's copy the local and remote address data prior to this call.

Change-Id: I17cd83f7b4b7e65e45fec1810fb38f745653bdc7
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:02 +01:00
Leandro Pereira 84b68b8b21 net: tcp: Be more consistent with namespaces for private funcs
Change-Id: Ie2699e01c5cc19cd04eed8383d6c73769366a55e
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-01-06 14:27:13 +00:00
Andy Ross 3a8b73067d net: tcp: Precompute appdata properly
TCP packets need the computed packet length early so they can fill in
a correct sequence number in their generated ACKs.  Waiting for
packet_received() is too late.  Precompute it before needed, and skip
the evaluation later.

Change-Id: I25547009f88277e0042c74f2005a141819797886
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-06 14:25:58 +00:00
Andy Ross 02174b8e0f net: tcp: Swap tcp->context backpointers
On accepting a new connection, the stack does an odd "swap" trick
where it updates the struct net_tcp record on the *listening* context
with the values from the new connection, and then swaps it with the
empty one that got allocated for the *new* context.

Unfortunately this swap forgot to swap the net_tcp "context" field
backpointers, so the net_context retrieved at runtime was for the
wrong connection.  Surprisingly, this actually almost worked for a
long time, except that the destination address would be wrong in the
newer setup.

Change-Id: I0c1812ddb9f9ff3e7deb60d1fd67cafd9ba96997
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-06 13:56:02 +00:00
Andy Ross a5b694fbbc net: tcp: Select correct source address for SYNACK packets
The source address for a TCP SYNACK must (obviously) be the same as
the destination address of the SYN that produced it.  But the existing
IP packet creation routines would simply fill in a default address
from the net_context struct, which is correct for *established*
connections, but for the listening socket is generally INADDR_ANY
(i.e. all zeroes) and will result in an arbitrary choice for source
address (e.g. a link-local address on the same interface) which can
easily be wrong.

So we need to pass the correct address all the way down from the SYN
packet handler code through the net_ipv*_create() packet creation
functions.  This requires lots of API plumbing, but relatively little
logic change.

Change-Id: Ic368f8cef6689f8a27cbafd5933a4964d5cc457e
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-01-06 08:21:15 -05:00
Tomasz Bursztyka a1aa08c288 net: ip: Improve logging by adding a dedicated sys_log level
Let's make net stack having its own level of debugging through sys_log.
It replaces NET_DEBUG by NET_LOG_ENABLED, which is then semantically
better: someone wanting to log the errors might want that not only for
debugging.

Along with it, CONFIG_NET_LOG_GLOBAL option is added, in order to enable
all available logging in network stack. It is disabled by default but
might be found useful when warning/errors need to be logged, so it is
then unnecessary to selectively enable by hand all CONFIG_NET_DEBUG_*
options.

It is possible, locally, to override CONFIG_SYS_LOG_NET_LEVEL by setting
the level one want to NET_SYS_LOG_LEVEL. This can be useful on samples
or tests.

Change-Id: I56a8f052340bc3a932229963cc69b39912093b88
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2017-01-02 10:03:20 +01:00
Flavio Santes 9216e6c47d net: Add ARG_UNUSED
This patch adds the ARG_UNUSED macros to some function arguments
to avoid compiler warnings.

Change-Id: Iae2cd3018c9442ffa9268fdfd33eb9a21f55087c
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-21 12:54:49 +00:00
Johan Hedberg fc21a76db6 net: Fix incorrect logging format specifiers
Fix compilation issues that show up if SYS_LOG is mapped to printk
instead of printf. Unlike printf, printk is annotated so that the
compiler catches incorrect format specifiers passed to it.

Change-Id: Iab7cc6da110e9c98720211a6f773dcf055a3a411
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2016-12-07 13:15:58 +00:00
Jukka Rissanen 3eaec41ab8 net: Moved net/ to subsys/net
* Moved networking code into subsys/net.
* Renamed net/yaip to net/ip at the same time.
* Fixed the tests/net to compile
* Fixed the Makefiles and Kconfig files in subsys/net
  to use the new location of the IP stack

Change-Id: Ie45d9e8cb45a93fefdf969b20a81e3b1d3c16355
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2016-12-02 12:41:20 +02:00
Renamed from net/yaip/net_context.c (Browse further)