Commit graph

226 commits

Author SHA1 Message Date
Tomasz Bursztyka 1a54cabe5e net/ipv4: Rename net_ipv4_finalize since legacy version disappeared
s/net_ipv4_finalize_new/net_ipv4_finalize

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-12 20:24:02 -05:00
Tomasz Bursztyka 0c80a84bc7 net/ipv6: Rename net_ipv6_finalize since legacy version disappeared
s/net_ipv6_finalize_new/net_ipv6_finalize

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-12 20:24:02 -05:00
Tomasz Bursztyka b93df74fa2 net/context: Let's use the new net_ipv4_finalize
And removing the legacy and unused one.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-12 20:24:02 -05:00
Tomasz Bursztyka 31a079023f net/context: Let's use the new net_ipv6_finalize
And removing the legacy and unused one.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-12 20:24:02 -05:00
Tomasz Bursztyka 16edbbad63 net/context: Update length according to available buffer for payload
UDP was the only one doing the right thing. Let's generalize it
relevantly.

Fixes #13211

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-12 09:46:01 -05:00
Ravi kumar Veeramally 36aa7d1878 net: Allow disabling IPv4, IPv6, UDP and TCP simultaneously
This commit fixes compilation warnings if user disables
CONFIG_NET_IPV4, CONFIG_NET_IPV6, CONFIG_NET_TCP and
CONFIG_NET_UDP.

E.g Samples like packet-socket doesn't need above configuration.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2019-02-08 12:35:03 +02:00
Jukka Rissanen a046a86966 net: context: Use signed value for interface index
As the value 0 is a valid network interface index, we cannot use
unsigned value for interface index as that would not allow to
distinguish an invalid value. So make interface index a signed
8-bit value which is ok as we do not expect to have more than 127
network interfaces in the system.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-02-07 18:08:27 +02:00
Jukka Rissanen 66621791d1 net: can: Add CAN handling to net_context
Make sure that we can work with CANBUS based sockets
in net_context API.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-02-07 18:08:27 +02:00
Jukka Rissanen bdf6f0cc20 net: utils: Fix net_proto2str() function
The function did not take the address family into account
when printing protocol name. The protocol value depends on
address family.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-02-07 18:08:27 +02:00
Ravi kumar Veeramally 337b6f9a73 net: context: Add packet socket support
This commit adds basic packet socket support to net_context and
allows application to receive or send network packets in raw
format.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2019-02-07 14:43:30 +02:00
Tomasz Bursztyka 522a4e86f3 net/context: Fix mutex unlock issues
2 unlock were missing, one in tcp and one in net_context.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-04 17:13:45 +02:00
Tomasz Bursztyka 212bcfb202 net/context: Lock/unlock mutex on new send/sendto functions
This was forgotten modification as commit 93e5181fbd came in master
though commit 0d519f7bcf was already written.

Thus fixing the missing lock/unlock.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-04 17:13:45 +02:00
Tomasz Bursztyka 4b78a251d7 net/context: Make recv_cb providing the ip and protocol headers
If status is 0, both ip_hdr and proto_hdr will own a pointer to the
relevant IP and Protocol headers. In order to know which of ipv4/ipv6
and udp/tcp one will need to use respectively net_pkt_family(pkt) and
net_context_get_ip_proto(context).

Having access to those headers directly, many callbacks will not need
to parse the packet again no get the src/dst addresses or the src/dst
ports. This will be change after this commit.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka e4ebe486b8 net/ip: Let's make public the 2 utility unions for ip/proto headers
Though these are currently used by the core only, it will be then used
by net_context as well. This one of the steps to get rid of net_pkt's
appdata/appdatalen attributes.

Also normalizing all ip/proto parameters name to ip_hdr and proto_hdr.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka 1ca48beed8 net/context: Enable support for TCP on new sendto function
Let's enable usage of TCP through net_context on its new sendto
function.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka 118a0f0d22 net/conn: Change callback signature to pass TCP/IP headers
As these were parsed already by IPv4/6 input functions let's use them.

Applying the change on trivial UDP usage. TCP usage will have its own
commit.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka 40ad4b7e3e net/context: Expose new functions to create ipv4/6 packet from context
These will be specifically needed in TCP, as well as being used in
context internally.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka 282962bb85 net/context: Add support for IPv6 on the new send/sendto functions
It can now use the new IPv6 function to create IPv6 header.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Tomasz Bursztyka 0d519f7bcf net/context: Create new send sendto functions using new net_pkt API
It's not anymore up to user to provide the pkt. Context will build the
packet according to its metadata and provided buffer and length.

It currently supports only IPv4 and UDP.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2019-02-01 14:34:38 +02:00
Jukka Rissanen 93e5181fbd net: context: Add locking for concurrent access
If the net_context functions are accessed from preemptive priority,
then we need to protect various internal resources.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-01-31 11:20:26 +02:00
Jukka Rissanen 86689030e8 net: Clarify logging in networking code
Remove network specific default and max log level setting
and start to use the zephyr logging values for those.

Remove LOG_MODULE_REGISTER() from net_core.h and place the
calls into .c files. This is done in order to avoid weird
compiler errors in some cases and to make the code look similar
as other subsystems.

Fixes #11343
Fixes #11659

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-12-07 12:00:04 +02:00
Patrik Flykt b97db52de7 misra-c: Add 'U' to unsigned variable assignments in subsys/
Add 'U' to a value when assigning it to an unsigned variable.
MISRA-C rule 7.2

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-12-04 22:51:56 -05:00
Ravi kumar Veeramally f2e0754718 net: ipv4: Drop the replies if src address in unspecified
Networking stack sometimes try to find source address based
on destination address. If interface could not find best match
then it returns unspecified address (0.0.0.0). Host should not
send these packets.

IPv4 reply related issues fixed.

Fixes #11329

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2018-11-28 10:13:41 +01:00
Jukka Rissanen cf063fe85b net: Rename net_is_xxx...() functions to net_xxx_is...()
Unify the function naming for various network checking functions.

For example:
     net_is_ipv6_addr_loopback() -> net_ipv6_is_addr_loopback()
     net_is_my_ipv6_maddr() -> net_ipv6_is_my_maddr()
etc.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-11-02 14:52:33 -04:00
Jukka Rissanen 57a8db7789 net: Use log_strdup() when printing debug strings
As the debugging print calls are async, all the strings that might
be overwritten must use log_strdup() which will create a copy
of the printable string.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-10-04 14:13:57 +03:00
Jukka Rissanen a76814bfb6 net: Convert core IP stack to use log levels
Instead of one global log level option and one on/off boolean
config option / module, this commit creates one log level option
for each module. This simplifies the logging as it is now possible
to enable different level of debugging output for each network
module individually.

The commit also converts the code to use the new logger
instead of the old sys_log.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-10-04 14:13:57 +03:00
Jukka Rissanen fc27a81ed2 net: context: Select proper network interface when binding
Use the destination address to select the proper network interface
when binding. The default network interface cannot be used here
as then the packet might be sent to wrong network interface.

Fixes #9935

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-09-20 11:21:22 +03:00
Flavio Ceolin da49f2e440 coccicnelle: Ignore return of memset
The return of memset is never checked. This patch explicitly ignore
the return to avoid MISRA-C violations.

The only directory excluded directory was ext/* since it contains
only imported code.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2018-09-14 16:55:37 -04:00
Tomasz Bursztyka e8dee89079 net/udp: Separating net_context from udp and other cleanup
- Up to net_context to give the source port.
- net_udp_append is unused anywhere: let's remove it.
- left over macros on _raw versions removed as well.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2018-09-13 14:34:25 +03:00
Paul Sokolovsky 8ccac9f74c net: context: Move/rename net_context_set_appdata_values() to net_pkt.c
This function has absolutely nothing to do with net_context.

Addresses: #8723

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-08-25 07:16:15 -07:00
Jukka Rissanen 7c7cfdda50 net: core: Always have a timeout when allocating a net_buf
Instead of waiting forever for a network buffer, have a timeout
when allocating net_buf. This way we cannot left hanging for a
long time waiting for a buffer and possibly deadlock the system.
This commit only adds checks to core IP stack in subsys/net/ip

Fixes #7571

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-08-14 12:17:00 +03:00
Tomasz Bursztyka abf68bc5ea net/ipv4: Remove useless return value
net_ipv4_finalize is always successful.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2018-07-24 17:22:58 +03:00
Tomasz Bursztyka d309c870c7 net/ipv6: Properly separate what belongs to ipv6 from the rest
Context part should be in context. Then, rename ipv6 function for
clarity.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2018-07-02 12:34:12 +03:00
Tomasz Bursztyka 68f7e96916 net/ipv4: Properly separate what belongs to ipv4 from the rest
Context part should be in context. Then, rename ipv4 function for
clarity.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2018-07-02 12:34:12 +03:00
Michael Scott 2c987298f2 net: tcp: expose some TCP helper functions
Similar to UDP, some drivers can make use of the following functions:
net_tcp_get_hdr()
net_tcp_set_hdr()

Let's expose them as <net/tcp.h> and change all internal references
to "tcp_internal.h".

Signed-off-by: Michael Scott <michael@opensourcefoundries.com>
2018-05-15 18:07:47 +03:00
Paul Sokolovsky 8c2362a62d net: ip: context: Merge send_data() into the only caller
sendto() is one and only caller of send_data(), a function of handful
of lines, and yet send_data() is located a hundred lines away from
it. Such "spaghetti functions" complicate review, debugging, and
refactoring of the IP stack.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-05-10 16:23:53 -07:00
Jukka Rissanen 1443ff0f5e net: stats: Make statistics collection per network interface
Instead of one global statistics, collect statistics information
separately for each network interface. This per interface statistics
collection is optional but turned on by default. It can be turned
off if needed, in which case only global statistics are collected.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-04-10 13:29:39 +03:00
Patrik Flykt e9b00eae75 net: tcp: Move TCP stack to tcp.c
Move core TCP functionality from net_context.c to tcp.c. Create empty
functions that the compiler can remove if TCP is not configured. As a
result remove TCP ifdefs from net_context.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 2c748ae6b5 net: context: Fix connect for UDP contexts
Similar to the socket connect() call, calling connect for a UDP
context needs to set both local and remote addresses and port
number. Fix this not to be exclusive for TCP.

Similarly, the remote destination can be a multicast address when
UDP is used.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt cfc6b56203 net: context: Factor out TCP receive window handling
Receive window is needed only for TCP.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 08110d8295 net: context: Factor out TCP context listen state
Factor out TCP context listening state change.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt b5c74b9706 net: context: Refactor queueing of a TCP FIN when closing socket
Queue a TCP FIN packet when needed if the socket was connected or
listening and where FIN wasn't already received.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt aed6a227a6 net: context: Remove unnecessary callback clearing
A new context cannot have callbacks set, as it was freshly created
in the beginning of the function. Thus the extra callback clearing
can be removed. This is a left-over from commit ce41d5f432.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 081e0eb8af net: context: Refactor net_tcp_send_data()
Refactor net_tcp_send_data() by adding callback information in the
function.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 34d82eb949 net: context: Factor out code for TCP receive
Create a function for TCP receive.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt d8522657bd net: context: Remove unnecessary ifdefs
Ifdefs around TCP code can be removed since the TCP code will compile
to empty functions when not enabled.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 8236e1e5cd net: context: Factor out TCP header length checking
TCP header length checking is needed by net_context.c.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 3b579ffb90 net: context: Refactor sendto()
Refactor sendto() code so that destination address and its validity
is checked first, followed by offloading verification. Move context
and shutdown checks into TCP queueing function.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Patrik Flykt 09b67a4825 net: context: Update net_context_sendto()
Only #ifdefs are removed in net_context_sendto().

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2018-03-29 12:39:43 +03:00
Jukka Rissanen 81d8d5219c net: context: Add PRIORITY option support
Add context option support and implement PRIORITY option that
can be used to classify the network traffic to different trafic
classes according to said priority value.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-03-27 10:06:54 -04:00
Michael Scott ddbcef4623 net: context: fix semaphore for offload in in net_context_put()
We're missing a k_sem_give for contexts_lock in the
CONFIG_NET_OFFLOAD path of net_context_put().

This fixes a network hang which occurs after any http_close()
call when CONFIG_NET_OFFLOAD is enabled.

Signed-off-by: Michael Scott <michael@opensourcefoundries.com>
2018-03-03 20:40:25 +01:00
Ravi kumar Veeramally 18422de8da net: tcp: Provide local address in TCP reset message preparation
If context is bound to IPv6 unspecified addresss and some port
number, then unspecified address is passed in TCP reset packet
message preparation. Eventually packet dropped at the peer.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2018-02-13 13:37:20 +02:00
Ravi kumar Veeramally e059e8b379 net: context: Handle failure case of tcp backlog entry
In case of failed to get source address from the net packet,
release the assgined tcp backlog entry. Otherwise it will
never be freed.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2018-02-13 13:37:20 +02:00
Ravi kumar Veeramally 7dc72c3ca6 net: context: Remove duplicate call
k_delayed_work_cancel(&context->tcp->fin_timer) called twice
immediately one after other.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2018-02-13 13:37:20 +02:00
Paul Sokolovsky cb140154b0 net: tcp Remove recv_max_ack field from struct net_tcp
This field is set and maintained, but not actually used for anything.
The only purpose for it would be to validate ACK numbers from peer,
but such a validation is now implemented by using send_seq field
directly.

Fixes: #4653

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-01-15 15:08:22 +02:00
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
Ravi kumar Veeramally 124c067027 net: tcp: Cancel the fin_timer on FIN message in FIN_WAIT1 state
fin_timer will be started after sending FIN to peer. After successful
reception of FIN+ACK message in FIN_WAIT1 state, fin_timer should be
cancelled.

Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
2017-12-31 11:58:27 -05:00
Aska Wu 8a20c4add7 net: context: Make sure contexts lock is released when error
Also, set the in-use flag after it is allocated successfully.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-24 08:30:00 -05:00
Aska Wu 3689107b59 net: tcp: First check sequence number
Previously, the connection will be reset easily due to a forged TCP
reset with a random sequence number.

As described in RFC793 p.69, we should check if the sequence number
falls into the receiver window at first.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-21 07:00:20 -05:00
Aska Wu b6c8c2d749 net: tcp: Fix tcp passive close
Previously, if passive close is peformed, the net context is released
after FIN is received and FIN,ACK is sent. The following last ack from
the peer will be treated as an improper packet, RST is sent to the peer.

This patch refines tcp_established() by centralizing the tcp state
transition and releases the net context only if NET_TCP_CLOSED is
reached.

Besides, the logic that releases the net pkt without appdata (i.e. ACK
or FIN) is moved from packet_received() to tcp_established(). This makes
packet_received() less dependent on the protocol and make the usage of
net pkt more clear in tcp_established().

Fixes: #4901

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-20 13:42:10 +02:00
Jukka Rissanen 9663287bf0 net: context: Check if TCP header is found
If the packet is too short, the TCP header pointer might be
NULL. In this case we just need to bail out.

Coverity-CID: 178787
Fixes #4787

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-11-14 23:03:16 +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 3c652996ff net: tcp: Add NET_TCP_HDR_LEN(hdr) macro for reuse
Calculates full TCP header length (with options). Macro introduced
for reuse, to avoid "magic formula". (E.g., it would be needed to
parse TCP options).

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
Aska Wu 1bdd80f8db net: context: Replace net pkt parsing by helper functions
The code parsing received net pkt to get source or destination
sockaddr repeats multiple times in net_context.c.

Eliminate the duplication by net_pkt_get_src_addr() and
net_pkt_get_dst_addr() which can handle different internet protocol
(i.e. ipv4 or ipv6) and transport protocol (i.e. tcp or udp)

Fixes: #4421

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-06 14:09:46 +02:00
Jukka Rissanen 0e7c8edc92 net: context: Make sure we honor timeout when waiting data
The call to net_context_recv() with timeout returned -ETIMEDOUT
even when data was returned properly and there was no timeout.

Fixes #4565

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-10-30 17:18:11 +02:00
Aska Wu 60a8e7843e net: context: Fix find_available_port()
In bind_default(), a local variable is passed to find_available_port().
However, the port number is unpredictable as it's not initialized and
will be used directly if not zero. This will lead to problems if the
port number is already used.

This patch makes find_available_port() always returns an available port
regardless of the port number in the sockaddr parameter.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-30 12:01:43 +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
Aska Wu 379771e2eb net: context: Bind default address for UDP
This patch makes net_context_sendto() work independently without calling
net_context_connect() first. It will bind default address and port if
necessary.

Also, since receive callback should be provided before sending data in
order to receive the response, bind default address and port to prevent
providing an unbound address and port to net_conn_register().

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-03 14:40:05 +03:00
Jukka Rissanen 2c6e473c93 net: context: Bind did not check IPv4 multicast address
If the user tried to bind to IPv4 multicast address, then the
operation failed and returned error.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-09-29 11:34:06 +03:00
Jukka Rissanen ce41d5f432 net: tcp: Set the TCP context properly when accepting
When new connection is accepted, the TCP context variables like
sequence number etc. were not properly set.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-09-10 10:03:58 -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
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
Paul Sokolovsky dcb80f7ab8 net: struct sockaddr should have field "sa_family"
POSIX requires struct sockaddr's field to be named "sa_family"
(not just "family"):
http://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/socket.h.html

This change allows to port POSIX apps easier (including writing
portable apps using BSD Sockets compatible API).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-18 16:34:51 -04:00
Jukka Rissanen 3fe4877123 net: tcp: Cleanup TCP backlog when releasing context
If there is a backlog entry when TCP context is released, then
cancel the ACK timer if one exists.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-16 14:50:57 -04:00
Andrew Boie 951ce4d00e net_context: work around missing errno codes
Some older C libraries do not have errno for EPFNOSUPPORT.
Fixes issues with newlib in some versions of Xtensa XCC compiler.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2017-08-16 10:59:10 -07: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
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
Leandro Pereira 732424f065 drivers, net: Clean up semaphore initialization
Change the common "init with 0" + "give" idiom to "init with 1".  This
won't change the behavior or performance, but should decrease the size
ever so slightly.

This change has been performed mechanically with the following
Coccinelle script:

    @@
    expression SEM;
    expression LIMIT;
    expression TIMEOUT;
    @@

    - k_sem_init(SEM, 0, LIMIT);
    - k_sem_give(SEM);
    + k_sem_init(SEM, 1, LIMIT);

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-07-27 15:23:07 -04:00
Paul Sokolovsky 9ed716b9b2 net: context: connect: Make sure local end is bound before connecting
Introduce net_context_bind_default() to ensure that local address is
set for context if not yet (via explict bind() call). This fixes
dereferences of NULL pointer to local address which was exposed when
MMU was enabled for qemu_x86.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-17 21:12:03 +03:00
Paul Sokolovsky 4334144caa net: context: Fix use of k_delayed_work_cancel with SYN backlog
This patch fixes a regression which the original patch introducing
this code (improving concurrent connection handling) had on
*sequential* connection handling. Without this patch, with the
default CONFIG_NET_TCP_BACKLOG_SIZE of 1, after each connection
request, there was 1s (ACK timeout) "dead time" during which new
connection wasn't ptocessed.

This is because k_delayed_work_remaining_get() was checked the
wrong way. But there's no need to use k_delayed_work_remaining_get()
at all, instead just call k_delayed_work_cancel() and dispatch on
its return code.

Note that there's still a problem of synchronizing access to
the global array tcp_backlog, as worker (which modifies it) may
preempt packet handling code (which also modifies it).

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-11 11:21:42 +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
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
Jukka Rissanen 3604c391e6 net: udp: Remove NET_UDP_HDR() macro and direct access to net_buf
Remove NET_UDP_HDR() macro as we cannot safely access UDP header
via it if the network packet header spans over multiple net_buf
fragments.

Fixed also the UDP 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
june li 054e5453b9 net: tcp: Reset context->tcp to NULL after net_tcp_release.
net_context_unref() is protocol agnostic, after being un-referenced,
the same context might end up being used then for UDP,
if context->tcp is not reset to NULL, calling net_context_unref()
after this UDP usage will again try to release this TCP pointer
which might lead to random error.

Signed-off-by: june li <junelizh@foxmail.com>
2017-07-06 22:35:51 +03:00
Patrik Flykt 00ac0487b0 net: context: Remove tcp struct SYN-ACK timer handling
The SYN-ACK timer is now handled by the TCP backlog functionality,
while the remaining ACKs for established connections use the tcp
struct ack timer. With this, code setting tcp struct SYN-ACK state
timers can now be removed.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2017-07-06 14:33:55 +03:00
Patrik Flykt 117771e633 net: context: Add TCP SYN-ACK timer handling
Add timer for sent TCP SYN-ACKs. If the timer is already
scheduled to run before canceling it is attempted, set
the cancelled flag and let the callback remove the
delayed work.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2017-07-06 14:33:55 +03:00
Patrik Flykt 864df870f8 net: tcp: Implement TCP backlog for incoming connections
Add an array of configurable size that holds TCP backlog entries.
The array is shared between all incoming TCP connections in order
to make it possible to get away with less memory consumed than
with a connection based approach.

The backlog entries are created when a SYN is received for a
listening TCP socket and removed once the corresponding ACK is
seen. With an incoming RST the corresponding backlog entry is
cleared, if any.

The size of the global backlog array is defined with the
CONFIG_NET_TCP_BACKLOG_SIZE Kconfig variable.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2017-07-06 14:33:55 +03:00
Patrik Flykt ac07550566 net: context: Go back to LISTEN state when receiving RST
In LISTEN state ignore a TCP RST. In SYN RCVD state, reset TCP
connection state to LISTEN when a valid RST segment is received. In all
other states close the connection - except that these other states will
not be handled in tcp_syn_rcvd() function.

Jira: ZEP-2279

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
2017-06-22 13:17:27 +03:00
Jukka Rissanen 210c30805b net: context: Close connection fast if TIME_WAIT support is off
If the CONFIG_NET_TCP_TIME_WAIT support is disabled, then do not
start to wait for reply to sent FIN in active close, but unref
the corresponding net_context in order to close the connection
as soon as possible.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-21 10:51:52 +03:00
Leandro Pereira 9e329c7997 net: tcp: Remove fin_queued flag from struct net_tcp
This flag was set but never used.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2017-06-13 10:33:41 +03:00
Paul Sokolovsky 515788648a net: ip: Make struct in_addr::s_addr compatible with POSIX definition
From
http://pubs.opengroup.org/onlinepubs/7908799/xns/netinetin.h.html:

in_addr_t
    An unsigned integral type of exactly 32 bits.

[] the in_addr structure [] includes at least the following member:

in_addr_t      s_addr

In other words, POSIX requires s_addr to be a single integer value,
whereas Zephyr defines it as an array, and then access as s_addr[0]
everywhere. Fix that by following POSIX definition, which helps to
port existing apps to Zephyr.

Jira: ZEP-2264

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-13 10:32:03 +03:00
Jukka Rissanen c0682a9dca net: context: Init context state after unref
When the context is freed, we must set the state to UNCONNECTED.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-12 10:23:19 +03:00
Jukka Rissanen 286139e13b net: tcp: Check pkt before sending RESET
In certain TCP states we should not try to send RESET segment
to peer. So check this and do not try to use NULL pkt to send
a message.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-09 13:09:37 +03:00
Jukka Rissanen 447193c959 net: context: Print error code if sending fails
If we cannot send network data, then print the error code when
printing debug information about the issue. This is needed when
debugging the issue.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-09 09:56:21 +03:00
Jukka Rissanen 7812569e0f net: context: Add debug print when context is freed
This is useful info to have when debugging.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-06-09 09:56:21 +03:00
Paul Sokolovsky 08a2c6019c net: context: Operations on unused context should lead to EBADF.
Semantics of ENOENT error as used previously is "named entity not
found", whereas for "I/O handle is not valid", there's EBADF. For
example, POSIX/SUSV2 doesn't even list ENOENT as a possible error
for accept(), connect(), recv(), etc. whereas it lists EBADF, e.g.:
http://pubs.opengroup.org/onlinepubs/7908799/xns/connect.html

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-05 10:32:28 +03:00