Commit graph

194 commits

Author SHA1 Message Date
Paul Sokolovsky ac7b1291da net: tcp: Validate incoming ACK number
Per RFC 793:

  A new acknowledgment (called an "acceptable ack"), is one for which
  the inequality below holds:

    SND.UNA < SEG.ACK =< SND.NXT

If acknowledgement is received for sequence number which wasn't yet
sent, log an error and ignore it.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-01-15 15:07:49 +02:00
Ravi kumar Veeramally a0371ae92f net: tcp: Add initial support for TIMEWAIT timer
Right now in FIN_WAIT1 state, if we receive FIN+ACK message, then
tcp state changed to FIN_WAIT2 on ACK flag and immediately on FIN
flag state changed to TIME_WAIT. Then final ACK is prepared and sent
(in queue at-least) to peer. Again immediately state changed to
TCP_CLOSED, where context is freed. net_context_put frees context
and releases tcp connection. Final ACK packet which is in queue
is dropped.

As a side effect of freed ACK packet, peer device keep on sending
FIN+ACK messages (that's why we see a lot of "TCP spurious
retransimission" messages in wireshark). As a result
of context free (respective connection handler also removed), we see
lot of packets dropped at connection input handler and replying with
ICMP error messages (destination unreachable).

To fix this issue, timewait timer support is required. When tcp
connection state changed to TIMEWAIT state, it should wait until
TIMEWAIT_TIMETOUT before changing state to TCP_CLOSED. It's
appropriate to close the tcp connection after timewait timer expiry.

Note: Right now timeout value is constant (250ms). But it should
be 2 * MSL (Maximum segment lifetime).

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-12-31 11:58:27 -05:00
june li c6407659f3 net: tcp: Add the frag back to caller allocated net_pkt
The original fragment chain of incoming packet will be lost and leaked
in case of early error, add frag back to packet and
let the caller do unref.

Fixes #4323

Signed-off-by: june li <junelizh@foxmail.com>
2017-11-15 12:28:18 +02:00
Paul Sokolovsky b86b079f32 net: tcp: net_tcp_parse_opts: Convert MSS value to host byte order
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-09 17:00:26 +02:00
Paul Sokolovsky cdea2bfab7 net: tcp: Add support for TCP options parsing
Add a generic function for TCP option parsing. So far we're
interested only in MSS option value, so that's what it handles.
Use it to parse MSS value in net_context incoming SYN packet
handler.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-08 16:19:57 +02:00
Paul Sokolovsky bc88ad750b net: tcp: Handle storage of TCP send MSS
MSS is Maximum Segment Size (data payload) of TCP. In SYN packets,
each side of the connection shares an MSS it wants to use (receive)
via the corresponding TCP option. If the option is not available,
the RFC mandates use of the value 536.

This patch handles storage of the send MSS (in the TCP structure,
in TCP backlog), with follow up patch handling actual parsing it
from the SYN TCP options.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-08 16:19:57 +02:00
Jukka Rissanen dd7b4bae28 net: tcp: Do not run expire function in ISR context
The expire function can call net_context_unref() which tries to
get a semaphore with K_FOREVER. This is not allowed in interrupt
context. To overcome this, run the expire functionality from
system work queue instead.

Fixes #4683

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-11-06 17:16:46 +02:00
Andrei Emeltchenko 90721238c7 net: tcp: Print retry count in retransmission attempts
Print also retry count when retransmitting packets.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2017-11-01 10:00:02 +02:00
Andrei Emeltchenko 86272907d8 net: Add initial retransmission timeout config option
Add option to set initial Retransmission Timeout value. The value is
different from NET_TCP_ACK_TIMEOUT since latter affects TCP states
timeout when waiting for ACK for example.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2017-10-31 13:09:30 +02:00
Paul Sokolovsky c469b7fc9f net: tcp: Avoid spurious seqno decrements
There were decrements of TCP sequence numbers, inherited from FNET
stack implementation, as was used as an initial base. RFC793 does
not specify conditions for decrementing sequence numbers, so such
decrements are an artifact of FNET implementation. In Zephyr code,
we had to compensate for these decrements by extra increments
(including an increment-by-2). So, remove decrements and associated
extra increments to simplify the code.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-10-25 14:19:04 +03:00
Jukka Rissanen 059959c83e net: tcp: Do not unref caller allocated net_pkt
If the caller has passed net_pkt to prepare_segment(), then
it is caller responsibility to unref it in a case of error.

Fixes #4292

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-10-13 17:23:25 -07:00
Jukka Rissanen cab505f0c2 net: tcp: Use real MTU size for MSS for IPv6
Instead of hard coded 1280 bytes MSS, use the MTU of the link
for MSS. The minimal MSS is still 1280 which is mandated by
IPv6 RFC.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-10-10 10:53:00 -04:00
Jukka Rissanen 025cf55e55 Revert "net: tcp: Queue FIN instead of sending it immediately"
This reverts commit 817245c564.

In certain cases the peer seems to discard the FIN packet we are
sending, which means that the TCP stream is not closed properly.
This needs more work so revert this for time being.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-09-02 04:52:34 -04:00
Jukka Rissanen 817245c564 net: tcp: Queue FIN instead of sending it immediately
If network context is closed, send FIN by placing it to the end
of send queue instead of sending it immediately. This way all
pending data is sent before the connection is closed.

Jira: ZEP-1853

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-31 11:58:24 -04:00
Jukka Rissanen 75de00225f net: tcp: Print more info if net_pkt was not sent
Useful in debugging if we get information why the TCP packet was
not sent.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-30 08:12:10 -04:00
Jukka Rissanen 5b29631623 net: tcp: Recover a situation if the first net_buf is too short
This is very unlikely to happen but the device will access null
pointer if we do not properly check the return value of header
check function.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-30 08:12:10 -04:00
Jukka Rissanen d2efe6027c net: tcp: Use packet clone function to copy the frags
Using memcpy() to copy net_pkt is not safe because there are
pointers inside. So use the new net_pkt_clone() to do that.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-30 08:12:10 -04:00
Luiz Augusto von Dentz 9147b53d76 net: Remove check for k_delayed_work_cancel
k_delayed_work_cancel now only fail if it hasn't been submitted which
means it is not in use anyway so it safe to reset its data regardless
of its return.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2017-08-25 15:34:40 -04:00
Jukka Rissanen 0a978ea5ef net: tcp: Send data only in established state
If we are not in ESTABLISHED state, then there is no need to
try to resend any pending data packets.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Jukka Rissanen e08716001a net: tcp: Do not re-send already sent packets
If the expire send timer expires, then it sends the packet.
If that happens, then we must not try to send the same packet
again if we receive ACK etc. which can cause re-sends to happen.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Jukka Rissanen 0674e03790 net: tcp: Remove unused variable
There was two pkt variables in net_tcp_ack_received() so removing
the extra one.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Jukka Rissanen cdec73d21e net: tcp: Only do ref when resending if we could send the packet
If the packet sending is slow then we must NOT increment the ref
count when re-sending it. This is unlikely but can happen if there
are lot of debug prints etc. extra activities that prevent the driver
to actually send the packet fast enough.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Jukka Rissanen f8ea1a59ea net: tcp: Add more debugging to TCP code
Useful when investigating issues.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Jukka Rissanen 5325c4b594 net: tcp: Set the TCP sent flag after really sending pkt
Make sure that the sent flag is only set after we have really
sent the packet and the driver has verified that.
If the net_pkt_set_sent() is called while still in tcp.c, then
depending on how fast the device is, it might happen that the
retry timer expires before the packet is actually sent. This was
seen in frdm-k64f with ethernet and various debug prints activated.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Andrew Boie 2e32e815ba net: fix net_tcp_state_str() declaration
The second 'const' is misguided, indicating that the returns pointer
value itself cannot be changed, but since pointers are passed by value
anyway this is not useful and was generating warnings with XCC.

The leading 'const' indicates that the memory pointed to is constant,
which is all we needed.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2017-08-16 10:59:10 -07:00
Jukka Rissanen d8e13c0c45 net: tcp: Make do_ref_if_needed() a macro
This is needed in order to get information which function is
doing the ref. With inline function this was not possible.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-14 11:06:34 +03:00
Jukka Rissanen b6a5d51baf net: Fix indentation issues
Some earlier search/replace broke indentation.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-09 16:01:28 +03:00
Johan Hedberg 2975ca0754 Bluetooth: Kconfig: Rename CONFIG_BLUETOOTH_* to CONFIG_BT_*
The API name space for Bluetooth is bt_* and BT_* so it makes sense to
align the Kconfig name space with this. The additional benefit is that
this also makes the names shorter. It is also in line with what Linux
uses for Bluetooth Kconfig entries.

Some Bluetooth-related Networking Kconfig defines are renamed as well
in order to be consistent, such as NET_L2_BLUETOOTH.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2017-08-09 11:14:19 +03:00
Paul Sokolovsky 19ff963693 net: tcp: Allow to explicitly manage TCP receive window
This fixes the existing situation that "if application buffers data,
it's the problem of application". It's actually the problem of the
stack, as it doesn't allow application to control receive window,
and without this control, any buffer will overflow, peer packets
will be dropped, peer won't receive acks for them, and will employ
exponential backoff, the connection will crawl to a halt.

This patch adds net_context_tcp_recved() function which an
application must explicitly call when it *processes* data, to
advance receive window.

Jira: ZEP-1999

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-05 11:40:50 +03:00
Jukka Rissanen ca7afdc774 net: tcp: Do not try to print TCP error for 6lo packet
For IPv6 header compressed packet, the IP header offsets will
be wrong. In this case there is no need to print error when
trying to print TCP packet information.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-07-31 11:50:25 +03:00
Jukka Rissanen ee989be286 net: tcp: Fix sequence number validator
The sequence number validator was checking the seq numbers
incorrectly. This caused some valid RST packets to be dropped
and the TCP stream to hang.

Added also a TCP test case that tests the seq validator.

Jira: ZEP-2289

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-07-11 10:35:19 +03:00
Jukka Rissanen e663c5df51 net: tcp: Add FIN timer when doing active close
The commit 210c30805b ("net: context: Close connection fast
if TIME_WAIT support is off") was not a proper way of closing
the connection. So if Zephyr closes the connection (active close),
then send FIN and install a timer that makes sure that if the peer
FIN + ACK is lost, we close the connection properly after a timeout.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-07-11 10:30:44 +03:00
Jukka Rissanen ee633595aa net: tcp: Fix passive close ACK timer
The commit 00ac0487b0 ("net: context: Remove tcp struct SYN-ACK
timer handling") removed also the passive close ACK timer.
Adding that ACK timer back so that we can close the connection
properly even if the last ACK from peer is lost.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-07-11 10:30:44 +03:00
june li cdeee63fbf net: tcp: Recalculate the tcp checksum when changing tcp header.
If the TCP data packet needs to be re-sent after the packet is lost,
then the acknowledgment number will be changed. This then means that
the TCP checksum needs to be recalculated too.

Signed-off-by: june li <junelizh@foxmail.com>
2017-07-10 11:08:07 +03:00
Jukka Rissanen 9c907fca43 net: tcp: Remove NET_TCP_HDR() macro and direct access to net_buf
Remove NET_TCP_HDR() macro as we cannot safely access TCP header
via it if the network packet header spans over multiple net_buf
fragments.

Fixed also the TCP unit tests so that they pass correctly.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-07-07 15:24:00 +03:00
Patrik Flykt 9cc7431b9d net: tcp: Make initial sequence number calculation public
The initial TCP sequence number needs to be assigned when a SYN
is received in net_context.c.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2017-07-06 14:33:55 +03:00
Jukka Rissanen 2b6ebc0876 net: tcp: Timeout memory allocations
Instead of waiting forever for a free net_buf, set a timeout to
the allocations (500 ms). This way the application will not be
blocked by memory exhaustion.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-02 12:43:52 +03:00
Leandro Pereira 985d6c3f4b net: tcp: Limit number of segment retransmissions
Defines a new tunable, CONFIG_NET_TCP_RETRY_COUNT, that determines the
number of segment retransmissions that the IP stack will attempt to
perform before resetting the connection.

The default value is 9 retransmissions, which amounts to 1:42 minutes,
as close as possible to the minimum recommended by RFC1122.

Jira: ZEP-1956, ZEP-1957

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-06-02 12:36:05 +03:00
Paul Sokolovsky d6b93b311f net: tcp: When sending FIN, make sure it goes with ACK and proper seq
Without change to add ACK to FIN, invalid TCP packet is generated,
where ack sequence number is non-zero. Without adjusting sequence
number as done, ACK which we send in response to peer's FIN/ACK is
not recognized by peer, and peer keeps retransmitting its FIN/ACK.

Jira: ZEP-2104

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-05-18 10:56:11 +03:00
Jukka Rissanen e253dcbd3b net: tcp: Add TCP statistics support
We did not collect any TCP statistics before but this commit
changes that.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-05-17 13:55:21 +03:00
Jukka Rissanen c6fcda19a3 net: tcp: Always set ACK bit in RST packet
Set the ACK bit in every RST packet as that seems to be expected
by Linux for example.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-05-17 09:36:02 +03:00
Jukka Rissanen 00b4081da3 net: tcp: Handle case when RST is received in any state
We must check if we receive RST in any of the TCP states.
If we do not do this, then the net_context might leak as it
would never be released in some of the states. Receiving RST
in any TCP state is not described in TCP state diagram but is
described in RFC 793 which says in chapter "Reset Processing"
that system "...aborts the connection and advises the user and
goes to the CLOSED state."

We need to also validate the received RST and accept only those
TCP reset packets that contain valid sequence number.

The validate_state_transitions() function is also changed to
accept CLOSED state transition from various other states.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-05-17 09:34:51 +03:00
Paul Sokolovsky 72a30f3843 net: tcp: Add TCP sequence number comparison compliant with RFC793.
RFC793, "Transmission Control Protocol", defines sequence numbers
just as 32-bit numbers without a sign. It doesn't specify any adhoc
rules for comparing them, so standard modular arithmetic should be
used.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-05-16 14:29:17 +03:00
Jukka Rissanen 6a52a30b7d net: tcp: Mark ACK timer as cancelled
This is related to commit "net: tcp: Make sure ACK timer is not
run if cancelled" which did not set the cancel flag when the timer
was cancelled from tcp.c.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-05-03 08:48:24 -04:00
Jukka Rissanen 52d0589c1f net: tcp: Fix TCP trace value debug
The TCP trace values were not printed because of incorrect
config option used. Print also seq and ack values in decimal
in order to make it easier to correlate the values in other
prints in tcp.c.

Change-Id: I44d1535a84dcba8c6c937d348516ba801193ca23
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-04-28 15:01:09 +03:00
Kumar Gala a509441210 net: convert to using newly introduced integer sized types
Convert code to use u{8,16,32,64}_t and s{8,16,32,64}_t instead of C99
integer types.

Jira: ZEP-2051

Change-Id: I4ec03eb2183d59ef86ea2c20d956e5d272656837
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-21 09:30:38 -05:00
Jukka Rissanen 52339cdf5a net: tcp: Avoid unused variable compiler warning
If TCP debugging is enabled but if the loglevel is set to lower
than 4, then compiler prints warning about unused flags variable
in net_tcp_trace().

Change-Id: I2e663644b50fe97b75088202e21b286aa010953e
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-04-21 14:19:51 +03:00
Tomasz Bursztyka e5896906f6 net: Rename all *_BUF() macros to *_HDR()
Most of these macros are not exactly exposing a buffer, but a specific
header pointer (ipv6, ivp4, ethernet and so on), so it relevant to
rename them accordingly.

Change-Id: I66e32f7c3f2bc75994befb28d823e24299a53f5c
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2017-04-21 14:19:51 +03:00
Tomasz Bursztyka db11fcd174 net/net_pkt: Fully separate struct net_pkt from struct net_buf
- net_pkt becomes a stand-alone structure with network packet meta
  information.
- network packet data is still managed through net_buf, mostly named
  'frag'.
- net_pkt memory management is done through k_mem_slab
- function got introduced or relevantly renamed to target eithe net_pkt
  or net_buf fragments.
- net_buf's sent_list ends up in net_pkt now, and thus helps to save
  memory when TCP is enabled.

Change-Id: Ibd5c17df4f75891dec79db723a4c9fc704eb843d
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2017-04-21 14:19:50 +03:00
Tomasz Bursztyka bf964cdd4c net: Renaming net nbuf API to net pkt API
There have been long lasting confusion between net_buf and net_nbuf.
While the first is actually a buffer, the second one is not. It's a
network buffer descriptor. More precisely it provides meta data about a
network packet, and holds the chain of buffer fragments made of net_buf.

Thus renaming net_nbuf to net_pkt and all names around it as well
(function, Kconfig option, ..).

Though net_pkt if the new name, it still inherit its logic from net_buf.
'
This patch is the first of a serie that will separate completely net_pkt
from net_buf.

Change-Id: Iecb32d2a0d8f4647692e5328e54b5c35454194cd
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2017-04-21 14:19:50 +03:00
june li 504b273b85 net: tcp: Do buf ref when resending a segment
We need to check whether buf_sent was true when resending the TCP
segment, and do a buf ref if needed. If this is not done, the buf
will be unref after send, which will cause unpredictable results.

Change-Id: Ibd4490305de88ac6ffd04ec42bba196e57da5c10
Signed-off-by: june li <junelizh@foxmail.com>
2017-04-21 14:19:50 +03:00
june li 961d7fc8f4 net: tcp: Release buf after failing to send
After failing to send the buf we need to release it.
This is not done for Bluetooth or IEEE 802.15.4 links which
create a copy of the sent buf and the failure case is already
checked by net_tcp_send_buf().

Change-Id: Ia556376b58ad74f68accb64eb2221a78d59dc2ec
Signed-off-by: june li <junelizh@foxmail.com>
2017-04-13 15:21:42 +03:00
Jukka Rissanen 7a07f23da7 net: tcp: Store MSS in tcp header correctly
Use UNALIGNED_PUT() to store the MSS value into network packet
because the memory location cannot be guaranteed to be properly
aligned.

Change-Id: I77fd7a70ef45eedb657cac29457b0239b0a1d4c2
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-04-13 15:21:41 +03:00
june li 86cc447f38 net: tcp: Start retry timer when queueing data
When sending TCP data, check if the retry timer needs
to be started.

Change-Id: Iea90716e918dec0b22e60bf32467b11c0d1a296f
Signed-off-by: june li <junelizh@foxmail.com>
2017-04-07 12:35:49 +03:00
Jukka Rissanen 6df4ee9691 net: tcp: Allow tweaking of 2MSL timeout
The default timeout (4 min) is very long. Allow tweaking the
value via Kconfig option.

Change-Id: Iddfd48b96f3612b9bba7caa4d64357505df9644d
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-03-31 10:58:17 +03:00
Jukka Rissanen 4d0c27209b net: tcp: Refactor net_tcp_trace()
No functionality changes, use separate variable so that we do
not need to do big-endian conversion multiple times.

Change-Id: I8874b427bd39dfa2d952034a2623c47544a644fc
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-03-24 17:31:41 +02:00
Jukka Rissanen 9337dd8c22 net: tcp: Print TCP trace in one line
Print TCP header information in one line as there is really no
need to use multiple lines. Also use debug level when printing
the header info so that it is only seen if debugging is activated.

Change-Id: I27f314ca060239545769dec07148897da3426436
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-03-17 10:34:45 +02:00
Ravi kumar Veeramally 8cb97ae47d net: Modify IPv4/6 packet finalize API's
Current finalize api's takes buf as input parameter and returns
the finalized buf. But if there are any issue while finalizing,
it failed to throw an error.

Change-Id: I6db54b7453eec41a8051fab50d5c0dc937debd54
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-03-17 10:34:42 +02:00
Michael Scott 0453629078 net: ip: Fix 6lo buffer leak when sending ACK
Due to recent TCP fixes for 6lo, we are making a copy of buffers
sent to net_tcp_send_buf() so that TCP retransmit can send the original
(unmodified) buffer.  This original buffer is freed via the TCP
sent_list when the related ACK packet is received.

However, there are users of the net_tcp_send_buf() function which
will never get a corresponding ACK (and do not add the buffer to the
TCP sent_list).  An example is send_ack() in net_context.c.  In this
case, we leak the original buffer.

To fix this leak in the 6lo specific block of net_tcp_send_buf(),
let's check to see if the original buffer was added to the TCP sent_list
and if not, then avoid the buffer copy process entirely.

Change-Id: If99e0e5bf266d33dd3466dc5d74443eaa39d10a8
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-03-09 20:33:49 +02:00
june li 07b8e4cb01 net: tcp: Add tcp options length
The tcphdr->offset was not set when tcp options were added.

Change-Id: I19fe97983ce81948a9a84893183e5c9000f12767
Signed-off-by: june li <junelizh@foxmail.com>
2017-03-09 20:33:47 +02:00
Michael Scott eddd598323 net: tcp: remove net_tcp_set_state() function
There are no users of net_tcp_set_state() left outside of
subsys/net/ip/tcp.c.

And the naming of this function is confusing -- it could easily
be mistaken for net_tcp_change_state() which contains additional
logic for certain tcp states.

Let's remove it entirely and fix the remaining uses to set
tcp->state directly.

Change-Id: I92855ad180e8682780fcff11e50af06adcbc177c
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-03-09 20:33:47 +02:00
Michael Scott a96fbc8167 net: tcp: fix NET_TCP_CLOSED state propagatation
In net_tcp_release() when a TCP connection is being closed, we
should call net_tcp_change_state instead of net_tcp_set_state.

net_tcp_change_state() will call into net_tcp_set_state() but
also contains logic specific to NET_TCP_CLOSED which unregisters
the context's conn_handler and sends an accept callback (if
present) with -ENETRESET error for user code to handle.

This fixes an EISCONN error returned by net_context_bind() when
a TCP-based net_context was reused.  Due to the conn_handler
not being cleaned up in the TCP code.

Change-Id: I8439a028a1c7ae5fd2a50d11caa9947a0ac6c7d4
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-03-09 20:33:47 +02:00
Jukka Rissanen 93533b37d8 net: tcp: Fix the issues with 6lo
When sending TCP data using 802.15.4 or Bluetooth technologies,
the 6lo code modified the original IPv6 header. This caused
issue when acknowledgment was waited to the sent packet as
the code could never match the sequence and ack numbers in
TCP header.

This commit changes this and the packet is cloned when sending
it so the 6lo code will modify a copy of the data and will not
touch the original packet.

JIRA: ZEP-1719

Change-Id: Iae51f35d5b5ada0d2543b58a29abbf10f146777e
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-03-09 20:33:43 +02:00
Ravi kumar Veeramally 05d5dcf007 net: tcp: Retransmit buffers are not freed on tcp_release
TCP maintains 'sent_list' for retransmission if it doesn't get ACK for it.
Same list is not freed on net_tcp_release() call. This causes memory leak.

Change-Id: I2b2def1ea19487cc48ea4fbb6343ef0c773f288f
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-02-22 17:28:25 +02:00
Luiz Augusto von Dentz 4ab09bdead net: Convert FOR_EACH macro instances to use CONTAINER
Change-Id: I2fbdc8128bef79eac74441c84fbb80e31f6bc261
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2017-02-10 16:16:15 +00:00
Ravi kumar Veeramally b45c4c7353 net: tcp: Remove multiple times of nbuf_compact() call
finalize_segment() will call net_ipv4_finalize() or
net_ipv6_finalize(). Both the functions perform net_nbuf_compact().
But after finalize_segment(), net_nbuf_compact() called again, which
is unnecessary.

Change-Id: I9fab63bcc44eec87061a4b55edd5053cf6556a75
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-02-09 09:43:06 +02:00
Ravi kumar Veeramally d3ea2021f4 net: tcp: Remove multiple checks on nbuf protocol family
Protocol family is checked in prepare_segment() and in same function
it's again verified by finalize_segment(). So remove the double checking
in finalize_segment().

Change-Id: I17123ab8741d017d7e3ff1ef3fb07371b0d4aa66
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-02-09 09:43:06 +02:00
Ravi kumar Veeramally 369e906e31 net: Ref net_buf using net_nbuf_ref
Using net_buf_ref() technically works but debugging the network buffer
allocations is more difficult if done like that.

Change-Id: Iac81bd3ab95547741d49f32763baaa54e97b4877
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-02-09 09:43:06 +02:00
Ravi kumar Veeramally 58ba1d834e net: nbuf: Fix net_nbuf_compact() API
Current API description of net_nbuf_compact() is not very clear.
The first parameter needs to be the first net_buf in the chain.

The changes to this API are needed in order to clarify following
use cases:
1) User provides fragment that is not first of the chain and compact is
successfully done. In this case there is no free space in fragment list
after the input fragment. But there might be empty space in previous
fragments. So fragment chain is not completely compacted.

2) What if input fragment has been deleted and api returns the same
buf?

So this commit simplifies the API behavior. Now net_nbuf_compact()
expects the first parameter to be either TX or RX net_buf and then it
compacts it. It fails only if the input fragment is a data fragment.

Change-Id: I9e02dfcb6f3f2e2998826522a25ec207850a8056
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-02-08 10:12:36 +02:00
Jukka Rissanen bd3908b2a9 net: nbuf: Add timeout to net_buf getters
This commit changes the net_buf getter functions in nbuf.h
by adding a timeout parameter. These function prototypes
are changed to accept a timeout parameter.
	net_nbuf_get_rx()
	net_nbuf_get_tx()
	net_nbuf_get_data()
	net_nbuf_get_reserve_rx()
	net_nbuf_get_reserve_tx()
	net_nbuf_get_reserve_data()
	net_nbuf_copy()
	net_nbuf_copy_all()
	net_nbuf_push()
	net_nbuf_append()
	net_nbuf_write()
	net_nbuf_insert()

Following convinience functions have not been changed
	net_nbuf_append_u8
	net_nbuf_append_be16
	net_nbuf_append_be32
	net_nbuf_insert_u8
	net_nbuf_insert_be16
	net_nbuf_insert_be32
	net_nbuf_write_u8
	net_nbuf_write_be16
	net_nbuf_write_be32
so they call the base function using K_FOREVER. Use the
base function if you want to have a timeout when net_buf
is allocated.

Change-Id: I20bb602ffb73069e5a02668fce60575141586c0f
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-02-08 10:12:35 +02:00
Andy Ross 3efe6b7ede net: tcp: Add optional TIME_WAIT support
The RFC requires we honor the 2MSL TIME_WAIT timeout, support for
which was just removed with the FIN cleanup.  Add it back, but make it
optional (proper sequence number and ephemeral port randomization
makes true collisions a birthday problem in a ~80 bit space!).

Change-Id: I176c6250f43bba0c914da1ee7f0136dcb1008046
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-02-03 15:59:15 +02:00
Andy Ross fece856959 net: tcp: Clean up FIN handling
The connection close paths were a little tangle. The use of separate
callbacks for "active" and "passive" close obscured the fundamentally
symmetric operation of those modes and made it hard to check sequence
numbers for validation (they didn't).  Similarly the use of the
official TCP states missed some details we need, like the distinction
between having "queued" a FIN packet for transmission and the state
reached when it's actually transmitted.

Remove the state-specific callbacks (which actually had very little to
do) and just rely on the existing packet queuing and generic sequence
number handling in tcp_established().  A few new state bits in the
net_tcp struct help us track current state in a way that doesn't fall
over the asymmetry of the TCP state diagram.  We can also junk the
FIN-specific timer and just use the same retransmit timer we do for
data packets (though long term we should investigate choosing
different timeouts by state).

Change-Id: I09951b848c63fefefce33962ee6cff6a09b4ca50
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-02-03 15:59:15 +02:00
Andy Ross 5d6e0d425b net: tcp: Clean up net_context lifecycle
The death of a network context was sort of a mess.  There was one
function, net_context_put(), which was used both by the user as a way
to "close" the connection and by the internals to delete it and to
"clean up" a TCP connection at the end of its life.

This has led to repeated gotchas where contexts die before you are
ready for them (one example: when a user callback decides the
transation is complete and calls net_context_put() underneath the
receive callback for the EOF, which then returns and tries to inspect
the now-freed memory inside the TCP internals).  I've now stepped into
this mess four times now, and it's time to fix the architecture:

Swap the solitary put() call for a more conventional reference
counting implementation.  The put() call now is a pure user API (and
maybe should be renamed "close" or "shutdown").  For compatibility,
it still calls unref() where appropriate (i.e. when the context can be
synchronously deleted) and the FIN processing will still do an unref()
when the FIN packets have been both transmitted and acked.  The
context will start with a refcount of 1, and all TCP callbacks made on
it will increment the refcount around the callback to prevent
premature deletion.

Note that this gives the user a "destroy" mechanism for an in-progress
connection that doesn't require a network round trip.  That might be
useful in some circumstances.

Change-Id: I44cb355e42941605913b2f84eb14d4eb3c134570
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2017-02-03 15:59:14 +02:00
Michael Scott 72e013a7ba net: tcp: add timeout wait in net_context_connect
If the parameter "timeout" is set in net_context_connect(), the
assumption by the user is that the function would wait for SYNACK
to be received before returning to the caller.

Currently this is not the case.  The timeout parameter is handed
off to net_l2_offload_ip_connect() if CONFIG_NET_L2_OFFLOAD_IP is
defined but never handled in a normal call.

To implement the timeout, let's use a semaphore to wait for
tcp_synack_received() to get a SYNACK before returning from
net_context_connect().

Change-Id: I7565550ed5545e6410b2d99c429367c1fb539970
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:20 +02:00
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 dbd7481274 net: tcp: remove unused semaphore tcp_lock
Change-Id: Ibcd89b063a8bd5f6f7400e0ed73eb6b6d8b86b26
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-20 16:23:18 +02:00
Jukka Rissanen 66f5dfa04f net: tcp: Allocate space for TCP header
We need to allocate separate fragment to store the IP
protocol headers.

Coverity-CID: 157582

Change-Id: Ib0dd5d28cd6876a0cf2de3b063c030ef64da998c
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-01-20 16:23:18 +02:00
Leandro Pereira 06d3ccce96 net: tcp: When retransmitting, hold an extra, temporary reference
The link layer dereferences a buffer right after it is transmitted.

If this extra reference is not held, the second time a buffer is
retransmitted, the reference that TCP holds when keeping the buffer in
the `sent_list` will be taken, and retransmission won't happen reliably
anymore.

As soon as the TCP fragment is acknowledged by the peer, the
`sent_list` reference is taken, and the buffer is freed.

Change-Id: Ie50f9acf02c1dff74248a5dfbec3785a91ff90f7
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-20 16:23:17 +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 525b51594f net: tcp: Get rid of recv_mss field from TCP struct
This frees up some more memory as well, by computing the maximum segment
size whenever needed.  A flag is set in the TCP context to signal if
the value has been already computed.

Change-Id: Idb228d4682540f92b269e3878fcee45cbc28038a
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
Leandro Pereira 5de6d084a9 net: tcp: Use an uint8_t for retry timeout instead of an uin32_t
Rename the variable to `retry_timeout_shift`, and shift-right the value
each time there's a timeout.  This saves some memory in that structure
by using the holes left due to alignment.

Change-Id: I18f45d00ecc434a588758a8d331921db902f4419
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-20 16:23:17 +02:00
Leandro Pereira 1aee85be4c net: tcp: Ensure all timers are disposed of when releasing context
Cancel all delayed work timers: FIN, ACK, and retry timers.  Also, do
that unconditionally regardless of which state the machine is in, as
that's a no-op if the timer has not been started yet.

Change-Id: Ia36b97c6823943976447fbd6389ae04862c19ff9
Signed-off-by: Leandro Pereira <leandro.pereira@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
Michael Scott ff1345dbaf net: tcp: replace seq/ack/wnd value shifts with system calls
In other portions of the code we use the sys_put_be* function
to shift the values from the current system endian to big
endian array of bytes.  Let's be consistent and do that in
the prepare_segment function as well.

Change-Id: I5a1a4c30ddf313c9e978be98fd969899f5de6190
Signed-off-by: Michael Scott <michael.scott@linaro.org>
2017-01-13 10:29:02 +01:00
Leandro Pereira fb02efb1c9 net: tcp: Use appdatalen when acknowledging packets
If the length of all fragments is used instead, the TCP header is also
considered; sequence numbers do not consider the header.

Change-Id: I19e77ac0fe62ca619b0903dd0265f8ea4878cdf7
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-06 14:27:48 +00:00
Leandro Pereira 162843f90a net: tcp: Store return value of net_buf_frags_len() on a size_t
The net_buf_frags_len() function returns a size_t, which is an unsigned
integer.  Store on an appropriate size_t variable instead.

Change-Id: I98aa4c0ddd7c464737436aa9ce13bdc86c11da2b
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-01-06 14:27:35 +00: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 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
Flavio Santes a8b73aa287 tcp: Validate net_context_put return code
Validate the return code of net_context_put and add debug information
in case any error is detected.

This patch fixes the error reported by Coverity:

Error handling issues  (CHECKED_RETURN)
Calling "net_context_put" without checking return value
(as is done elsewhere 4 out of 5 times).

Coverity-CID: 157833

Change-Id: Ia4a87cc08094bee465c8828411d7c1311481695a
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
2016-12-16 19:14:52 +01:00
Jukka Rissanen 5ece8c26b5 net: tcp: Fixing the constness of TCP state debug string
Change-Id: I42a8271cf10efcd9ce67ef860a45fa54c0690387
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2016-12-02 12:41:20 +02: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/tcp.c (Browse further)