Commit graph

554 commits

Author SHA1 Message Date
Gil Pitney c51cf1e463 drivers: wifi: simplelink: Implement setsockopt() for TLS offload
Implements setsockopt() for the socket offload driver
to process the TLS tags sent in via the Zephyr setsockopt() API,
when CONFIG_NET_SOCKETS_SOCKOPT_TLS is chosen.
For each tag, the credential filenames are retrieved and
set via SimpleLink's sl_SetSockOpt() API.

Also, creates a new KConfig option for TLS_CREDENTIAL_FILENAMES.
This new option is used by apps/protocols to add TLS credentials
via filenames referring to the actual content stored on a secure
file system or flash.

Handles the IPPROTO_TLS_* socket protocol families in the
socket() offloaded API.

This was validated on the cc3220sf_launchxl with the http_get sockets
sample, with the globalsign_r2.der file loaded to secure flash via
the TI Uniflash tool, and using the TI Catalog of known good
root CA's.

Signed-off-by: Gil Pitney <gil.pitney@linaro.org>
2018-11-23 09:16:21 -05:00
Paul Sokolovsky 2e21a95fd4 net: sockets: zsock_close: Be sure to free file descriptor
File descriptor I freed automagically when using POSIX subsystem's
close() function, but any subsys-adhoc functions like zsock_close()
should do that explicitly.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-20 11:25:17 +01:00
Robert Lubos 824d0bd854 sockets: tls: Use FD table to reach net_context
With FD table introduction, net_context can no longer be reached by
typecasting socket descriptor. Instead, file descriptor API have to be
used.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-11-15 17:54:40 -05:00
Robert Lubos 72958f2e1c sockets: tls: Fix getsockopt/setsockop return value
ztls_setsockopt and ztls_getsockopt returned error codes instead of
setting errno in particular cases. This commit fixes it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-11-15 17:54:40 -05:00
Paul Sokolovsky 8f690e291b lib: fdtable: FD method tables should be const.
FD method tables contain function pointers, and thus should be
const and reside in ROM. This patch fixes all cases of FD vtable
definitions: for POSIX FS API and for sockets.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-08 09:47:46 -08:00
Paul Sokolovsky f750ce56ce net: lib: sockets: Switch to use fdtable
Previously the "socket file descriptors" were just net_context
pointers cast to int. For full POSIX compatibility and support
of generic operations line read/write/close/fcntl/ioctl, the
real file descriptors should be supported, as implemented by
fdtable mini-subsys.

Socket implementation already has userspace vs flatspace dichotomy,
and adding to that ptr-fds vs real-fds dichotomy (4 possible cases)
is just too cumbersome. So, switch sockets to real fd's regardless
if full POSIX subsystem is enabled or not.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-11-04 22:04:11 +01:00
Robert Lubos 683628ab73 net: tls: Do not assume PSK id is NULL terminated
Current TLS socket implementation assumed that PSK ID stored in
credential manager is NULL terminated. It's actually better to store
only the string content, as the string length is stored as well. This
approach is less confusing, when a user is not operating on C strings
but on a non-NULL terminated byte array.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-10-22 10:32:07 +03:00
Gil Pitney c2cdbbafd7 net: sockets: Get socket offload to build after net logging overhaul.
Also, defines one LOG_MODULE_NAME for the simplelink WiFi driver, and
uses the same name for all files in this driver (module).

Signed-off-by: Gil Pitney <gil.pitney@linaro.org>
2018-10-11 15:51:57 +03:00
Paul Sokolovsky 8e1d78c357 libc: minimal: Make fcntl.h be at top level, not sys/fcntl.h
Placing it at sys/fcntl.h was due to mimicking internal newlib's
layout, but what we need is this file at the standard location,
for reuse.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-10-09 15:44:59 -04:00
Léonard Bise 9975f7b854 net: sockets: Unblock threads waiting on recv on socket close
This commit fixes the issue that if a thread is waiting on recv for
data and the user closes the socket, the waiting thread is not
unblocked.

Signed-off-by: Léonard Bise <leonard.bise@gmail.com>
2018-10-09 18:35:22 +02:00
Jukka Rissanen 009e4dafa7 net: Make Kconfig template variables prettier
Adding spaces around "=" when definining Kconfig template so
that is more consistent with overall style of these template
variables.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-10-05 09:01:37 -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 dc29ed0bd9 net: lib: Convert socket library to use log level
Use network log level in BSD socket library.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2018-10-04 14:13:57 +03:00
Robert Lubos 349e14d63c net: tls: Fix mbedtls hostname erros when certificates are not used
When no certificate-based ciphersuites are used, mbedTLS compiles out
hostname field and associated functions from its SSL context. This
resulted in compilation error when only PSK-based ciphersuites were
configured.

This commit resolves the issue by compiling-out hostname-related code
from secure sockets implementation on the same basis as mbedTLS does.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-09-17 09:48:11 +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
Robert Lubos 5286524a5c net: tls: Add missing entropy header
sockets_tls subsystem uses entropy driver, yet it does not include
entropy header. This commit fixes this.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-09-14 15:01:57 +03:00
Gil Pitney c21b0fb357 net: sockets: Add a socket offload module
This patch enables BSD socket offload to a dedicated
TCP/IP offload engine.

This provides a simpler, more direct mechanism than going
through NET_OFFLOAD (zsock -> net_context -> socket conversions)
for those devices which provide complete TCP/IP offload at the
BSD socket level, and whose use cases do not require
IP routing between multiple network interfaces.

To use, configure CONFIG_NET_SOCKETS_OFFLOAD=y, and register
socket_offload_ops with this module.

Fixes #3706

Signed-off-by: Gil Pitney <gil.pitney@linaro.org>
2018-09-11 18:42:34 +03:00
Paul Sokolovsky a2d12527f6 net: sockets: poll: Handle EINTR return from k_poll
This is similar to change which was done in 21f31e90ec, unfortunately
this case was missed.

Fixes: #9032

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-08-30 09:28:29 -04:00
Paul Sokolovsky 2af8dc9630 net: sockets: close: Call net_context_accept only for listening socket
The previous code "optimized" and called both net_context_accept()
and net_context_recv() blindly to reset the corresponding callbacks.
But this leads to "wrong state" logging if debugging is enabled, so
clean that up.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-08-25 07:18:31 -07:00
Robert Lubos d529aef9f2 net: tls: Apply DTLS review fixes
This commit contains several fixes for DTLS implementation, proposed in
a post-merge review of #9338.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-17 15:10:00 +03:00
Andrew Boie 18cec245ba net: introduce system calls for zsock socket APIs
Add system calls for the zsock implementations of socket,
close, bind, connect, listen, accept, sendto, recvfrom,
fcntl, poll, inet_pton, and getaddrinfo.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-08-13 07:19:39 -07:00
Robert Lubos 7c487ba04a net: tls: Use EINVAL for NULL pointer error in option setters
Originally EFAULT was used to indicate NULL pointer error in TLS option
set/get functions. EINVAL was suggested to be more apropriate error code
for this case, hence replace it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 800392e128 net: tls: Enable non-blocking DTLS handshake
Introduce non-blocking DTLS handshake, used during recv function call.
This prevents from blocking while waiting for initial handshake packet
on non-blocking sockets during receive.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos ae41ec1f35 net: tls: Make TLS poll implementation check mbedTLS data
Make TLS poll function verify if decrypted data is available after
socket has notified activity with POLLIN flag. This prevents from giving
false notifications in case data was received on socket but was consumed
by mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 8697cc946f net: tls: Add timeout for mbetTLS read
Specify timeout value for mbedtls_ssl_read function for DTLS servers.
Adding this can prevent TLS context lockup in case blocking recv is used
and peer has shut down DTLS connection without closing it gracefully.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos b1d8271152 net: tls: Support DTLS recv and handshake
Add support for DTLS recv/recvfrom function.

For DTLS client, recv function requires to have an already established
DTLS connection.

For DTLS servers, this function will try to establish DTLS connection
before receiving data. In case that DTLS handshake fails, recv function
will silently retry.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos bdc56cfe89 net: tls: Support DTLS send and handshake
Add support for DTLS send/sendto function.

For DTLS clients, send function will try to establish DTLS connection
before sending data. If DTLS handshake fails, it will return an error.

For DTLS servers, send function requires to have DTLS connection already
established.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos d021b8c60f net: tls: Adapt connect for DTLS
Extend connect function with DTLS functionality.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 6c5aaa070c net: tls: Handle HELLO_VERIFY_REQUIRED during DTLS handshake
DTLS handshake can return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED, which
indicate that TLS session context should be reset.

Also, store information whether TLS connection has beed established.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 4c235dea73 net: tls: Add DTLS bio functions
Add binary IO functions for DTLS connections.

dtls_rx function is more complex than it's TLS counterpart due to fact,
that DTLS does not allow blocking operation for this function. A simple
timeout mechanism was implmented basing on the zsock_poll function.

This function also verifies peer address. As currently only a single
DTLS connection is supported on a socket, if a DTLS connection is
established, and we receive datagram from different peer, it is silently
dropped.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 8719f9a802 net: socket: Extract flag related functions to internal header file
Functions for checking flags set on sockets are needed by TLS sockets as
well, therefore extract them to a separate header file to avoid code
duplication.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 4aaffaff8a net: tls: Add DTLS peer address helpers
Add helper functions to handle stored DTLS peer address.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 3ad2bbceda net: tls: Add cookie for DTLS
Add DTLS cookie to TLS context, required by DTLS servers.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 011b4f4265 net: tls: Add timer handling for DTLS
Add timer functions required by DTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos b03a388d73 net: tls: Add socket option to set DTLS role
Add write-only socket option to set role for DTLS connection. This
option is irrelevant for TLS connections.

This options accepts and integer with a TLS role, compatible with
mbedTLS values:
0 - client,
1 - server.

By default, DTLS will assume client role.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos 85db974ec3 net: tls: Add DTLS protocol types
Define DTLS protocol types and and Kconfig option to enable DTLS
support.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-13 15:24:34 +03:00
Robert Lubos b7e686648b net: sockets: Fix setsockopt coverity issues
Minor refactoring to satisfy Coverity.

Fixes #9291 (CID 187324)
Fixes #9296 (CID 187319)

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-08-06 12:40:40 +03:00
Ulf Magnusson d1684a83a4 Kconfig: Clean up some symbol definitions
- Remove redundant 'n' defaults. 'n' is the default value for bool
  symbols.

  This makes the auto-generated documentation clearer as well: You get
  "implicitly defaults to n" instead of
  "- n if <propagated dependencies>".

- Shorten

      <type>
      prompt "foo"

  to

      <type> "foo"

  This works for all types, not just bool.

- Various formatting nits.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-08-01 12:47:17 -04:00
Robert Lubos 7826228def net: tls: Add socket option to set peer verification level
Add write only TLS secure option to set peer verification level for
TLS connection.

This option accepts an integer with a peer verification
level, compatible with mbedtls values (0 - none, 1 - optional, 2 -
required.

By default, socket mimics mebdTLS behavior - (none for server, required
for client).

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos 3d560e14ac net: tls: Add socket option to read chosen ciphersuite
Add TLS secure socket option to read a ciphersuite chosen during TLS
handshake. Might be useful during development.

This is a read-only option that returns an integer containing an
IANA assigned ciphersuite identifier of chosen ciphersuite.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos 11c24c855d net: tls: Add socket option to select ciphersuites
Add TLS secure socket option that enables to narrow list of ciphersuites
available for TLS connection.

This option accepts an array of integers with IANA assigned ciphersuite
identifiers and returns such.

By default, every statically configured ciphersuite is available for a
socket and getsockopt returns an array of these.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos 915317724c net: tls: Add socket option to set TLS hostname
Add write-only TLS secure socket option to set hostname.

This option accepts a string containing the hostname. May be NULL, to
disable hostname verification.

By default, an empty string is set as a hostname for TLS clients,
to enforce hostname verification in mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos a3edfc2563 net: tls: Set TLS credentials in mbedTLS
Configure selected credentials in mbedTLS before the handshake.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos 48e055577b net: tls: Add socket option to select TLS credentials
Add TLS secure socket option to select TLS credentials to use.

This option accepts and returns an array of sec_tag_t that indicate
which TLS credentials should be used with specific socket.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos f959b5c164 net: tls: Add TLS socket options placeholder
Add TLS secure sockets wrapper for getsockopt/setsockopt functions.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos 11f7abcefd net: socket: Define getsockopt() and setsockopt()
Define socket option functions and make them return ENOPROTOOPT.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Robert Lubos d09cbcaf6f net: tls: Add credential management subsystem
Add TLS credential management subsystem that enables to register TLS
credentials in the system. Once specific credentials are registered in
the system, they will be available for TLS secure sockets to use.

To use a TLS credential with a socket, the following steps have to be
taken:
1. TLS credential has to be registered in a system-wide pool, using the
API provided in "net/tls_credentials.h" header file.
2. TLS credential (and other TLS parameters) should be set on a socket
using setsockopt().

Note, that there is no need to repeat step 1 for different sockets using
the same credentials. Once TLS credential is registered in the system,
it can be used with mulitple sockets, as long as it's not deleted.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-26 12:13:15 -04:00
Paul Sokolovsky 58cc75327b net: getaddrinfo: Make availability depend on CONFIG_DNS_RESOLVER
CONFIG_DNS_RESOLVER is the master switch for DNS resolution support,
for both native and socket APIs. Avoid confusing link errors by
compiling out both dns_resolve_name() and getaddrinfo() if that
option is not enabled.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-07-23 10:04:39 -07:00
Andrew Boie a74137f665 net: getaddrinfo: use memory allocation for res
We no longer use a global array for what is supposed to be
a memory allocation.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-19 08:56:21 -07:00
Andrew Boie 1b37602859 net: getaddrinfo: ai_state no longer global
Move this onto the call stack of the getaddrinfo() function,
there's no need for it to be global.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2018-07-19 08:56:21 -07:00
Paul Sokolovsky eedb8a7bd8 net: sockets: Make poll() call threadsafe by avoiding global array
Initial implementation of poll() was more of a proof of concept, so
was coded with a single-thread application in mind. As we move to
sockets as the main networking API, make it possible to use poll()
from different threads.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-07-17 13:02:16 +03:00
Robert Lubos 47f908872d net: tls: Implement poll with support for mbedTLS pending data
Implement socket poll function for TLS socket. In addition to regular
poll checks, we have to check if there is some decrypted data pending on
mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 07f1a1fe2c net: tls: Handle TLS socket send and recv
Implement socket recv/recvfrom and send/sendto functions.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos d08fd07f60 net: tls: Handle TLS handshake
Implement TLS handshake handling in socket connect/accept functions.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 2d4815dd15 net: tls: Add mbedTLS logging
Add mbedTLS logging function to enable logs from mbedTLS.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos ccdc6a6bdf net: tls: Add mbedTLS entropy source
Add entropy source for mbedTLS. If no entropy driver is available, use
non-secure, software entropy source.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos a7c698d936 net: tls: Add TLS context allocation/deallocation
Add tls_context structure that stored data required by TLS socket
implementation. This structure is allocated from global pool during
socket creation and freed during socket closure.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Robert Lubos 00a69bf9bb net: socket: Add switch to enable TLS socket option support
Add switch to a socket layer that will enable switching socket API to
TLS secure sockets. At this point there is no secure sockets
implementation, so secure socket calls redirect to regular socket calls.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2018-07-13 15:03:45 -04:00
Ulf Magnusson 1073882998 subsys: kconfig: Remove 'default n' properties and clean up a bit
Bool symbols implicitly default to 'n'.

A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.

Remove some 'default ""' properties on string symbols too.

Also make definitions more consistent by converting some

  config FOO
  	<type>
  	prompt "foo"

definitions to a shorter form:

  config FOO
  	<type> "foo"

This shorthand works for int/hex/string symbols too, not just for bool
symbols.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2018-07-12 23:13:22 -04:00
Stig Bjørlykke 97b83ffc2c net: sockets: Check for errors from dns_get_addr_info
When dns_get_addr_info() returns an error it does not call the
resolve callback, and thus the semaphore will not be given.

This fix will avoid a deadlock situation for various errors.

Added some small tests for getaddrinfo().

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-04-03 14:15:43 +03:00
Stig Bjørlykke e652b74860 net: sockets: Do not lookup DNS AAAA entries if IPv6 is disabled
Do not lookup DNS AAAA entries if IPv6 is disabled. The result can not
be used anyway because "struct sockaddr" does not have enough space for
IPv6 address in that case.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-04-03 10:23:06 +03:00
Stig Bjørlykke 5f42a26488 net: sockets: Allow empty service in getaddrinfo()
Make the service parameter optional in getaddrinfo().

The application should be able to use NULL as service when only
interested in the host lookup.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-21 10:00:17 +02:00
Stig Bjørlykke 57e7ea8793 net: sockets: Support MSG_DONTWAIT flag in zsock_sendto
Add support for MSG_DONTWAIT flag in send() and sendto(). This
aligns with the same flag used in recv() and recvfrom().

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-21 09:59:49 +02:00
Stig Bjørlykke 7c9c825138 net: sockets: Support MSG_PEEK flag in zsock_recvfrom
Add support for MSG_PEEK flag in recv and recvfrom.

This flag is needed when using non-zephyr embedded applications with
Zephyr's socket API.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-20 15:55:04 +02:00
Stig Bjørlykke 158adc684f net: sockets: Support MSG_DONTWAIT flag in zsock_recvfrom
Add support for MSG_DONTWAIT flag in recv and recvfrom.

This flag is needed when using non-zephyr embedded applications with
Zephyr's socket API.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-18 17:06:38 -04:00
Stig Bjørlykke b0c8cd8048 net: sockets: Refactoring zsock_recvfrom
Refactor recvfrom code for handling SOCK_DGRAM into zsock_recv_dgram
to align with SOCK_STREAM. Add flags parameter, will be used later.

Signed-off-by: Stig Bjørlykke <stig.bjorlykke@nordicsemi.no>
2018-03-15 15:10:04 +02:00
Paul Sokolovsky 5925112ee6 net: sockets: Preserve user_data for any net_context send() or recv()
We use ctx->user_data to hold socket flags. As each call to
net_context_recv() and net_context_send() overwrites its previous
value, we explicitly must pass the current ctx value there.

Without this, non-blocking socket was turned into blocking after
e.g. switching from receiving to sending.

Fixes: #6309

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Paul Sokolovsky 934195cc43 net: sockets: zsock_accepted_cb: Init child socket on success only
If accept callback is called with error, don't treat the context
passed to the callback as a new accepted context.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Paul Sokolovsky 9e02f7b644 net: sockets: Fix mask application in sock_set_flag()
We pass normal 1-based mask, and mask invert it before logically
AND'ing with the value. This apparently a mix-up between how the
mask was intended to be passed initially and how it was in the end.
This issue actually didn't have an effect, because currently defined
flags have mutually exclusive lifetime (when "eof" flag is set,
"non-blocking" flag value is no longer import). Anyway, that's a
bug and needs fixing.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-03-05 12:54:42 +01:00
Leandro Pereira 816c709ef2 net: sockets: Do not reference NULL pointer with empty frags on start
Assertions should only be used to check invariants.  Things that may
change value in runtime are better left to proper checks.

Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
2018-03-02 07:11:46 +01:00
Paul Sokolovsky ede25c12a3 net: sockets: getaddrinfo() buffer overflow, etc. fixes
The existing implementation assumed DNS resolv callback will be
called just once, but that's not always the case (apparently,
for multi-homes hosts or something). So, apply array bounds
checking (and do pointer arithmetic only after it, as the C
standard otherwise warns of "undefined behavior"). In such a case,
the port number wasn't set in each entry too, so rework how it's
done.

The issues discovered while resolving archive.ubuntu.com.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2018-02-12 20:17:26 -05:00
Anas Nashif 429c2a4d9d kconfig: fix help syntax and add spaces
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2017-12-13 17:43:28 -06:00
Paul Sokolovsky 21f31e90ec net: sockets: Handle EINTR return from k_poll()
In 90b471fe4, there was a change to make k_poll() return EINTR error
if it was cancelled with k_fifo_cancel_wait(). Handle this change, or
otherwise sockets EOF handling was broken.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-21 07:01:49 -05:00
Aska Wu 7e58c5609a net: sockets: Fix net pkt leak
If net_context_recv() returns a error, net pkt will not be released. For
example, net_context_recv() returns -EBADF because the TCP connection is
closed by the peer.

Handle the return value instead of using SET_ERRNO().

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-17 12:58:32 +02:00
Paul Sokolovsky 91e810f87a net: sockets: sendto: Remove adhoc handling of max pkt payload size
Now the check happens on the level of the core IP stack, in
net_pkt_append().

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-10 16:30:42 +02:00
Sebastian Bøe 0829ddfe9a kbuild: Removed KBuild
Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Sebastian Bøe 12f8f76165 Introduce cmake-based rewrite of KBuild
Introducing CMake is an important step in a larger effort to make
Zephyr easy to use for application developers working on different
platforms with different development environment needs.

Simplified, this change retains Kconfig as-is, and replaces all
Makefiles with CMakeLists.txt. The DSL-like Make language that KBuild
offers is replaced by a set of CMake extentions. These extentions have
either provided simple one-to-one translations of KBuild features or
introduced new concepts that replace KBuild concepts.

This is a breaking change for existing test infrastructure and build
scripts that are maintained out-of-tree. But for FW itself, no porting
should be necessary.

For users that just want to continue their work with minimal
disruption the following should suffice:

Install CMake 3.8.2+

Port any out-of-tree Makefiles to CMake.

Learn the absolute minimum about the new command line interface:

$ cd samples/hello_world
$ mkdir build && cd build
$ cmake -DBOARD=nrf52_pca10040 ..

$ cd build
$ make

PR: zephyrproject-rtos#4692
docs: http://docs.zephyrproject.org/getting_started/getting_started.html

Signed-off-by: Sebastian Boe <sebastian.boe@nordicsemi.no>
2017-11-08 20:00:22 -05:00
Aska Wu 62e5f8d7a1 net: socket: Handle the value-result argument
The addrlen of accept() and recvfrom() is a value-result argument. It
should be updated to the actual size of the source address after
calling accept() and recvfrom().

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-11-06 20:59:05 +02:00
Paul Sokolovsky 6f76a19f77 net: sockets: Fix Coverity false positive
Due to parameters used, net_context_recv() call cannot fail (it just
installs a callback, no I/O performed).

Coverity-CID: 178247
Fixes: #4581

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-11-01 09:47:40 +02:00
Aska Wu 632904117c net: sockets: Fix the error zsock_send() failed on the datagram socket
net_context_sendto() returns an error if dest address is NULL.

If dest address is available, net_conext_sendto() should be used.
Otherwise, net_context_send() should be used.

Fixes #4347

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-17 08:10:14 -04:00
Paul Sokolovsky 82a03936a4 net: sockets: Simplify if logic in sendto(), fix warning
send()/sendto() aren't "front facing" functions, so when user calls
them, context type hopefully should be already validated by other
functions. They are also on critical path of app/network performance,
so getting rid of extra check helps a little bit too. This also
fixes a warning of "err" possibly being used non-initialized.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-10-03 16:18:11 -04:00
Aska Wu 9ec60774f8 net: sockets: Implement sendto() and recvfrom()
sendto() and recvfrom() are often used with datagram socket.

sendto() is based on net_context_sendto() and recvfrom() is based on
zsock_recv() with parsing source address from the packet header.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-10-03 14:40:05 +03:00
Paul Sokolovsky d3c7152528 net: sockets: Implement getaddrinfo()
POSIX-compatible getaddrinfo() call, implemented on top of native
Zephyr DNS resolver.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-19 17:39:44 +03:00
Paul Sokolovsky c7da45f9e2 net: Enable logging in more modules if CONFIG_NET_LOG_GLOBAL defined
This is useful to enable error/warning logging across the net
codebase (less useful for debug level logging, but that's true
for CONFIG_NET_LOG_GLOBAL already).

Implementation-wise, instead of keeping adding to long list of
"select"'s in CONFIG_NET_LOG_GLOBAL and thus introduce component
inter-dependencies, add "default y if NET_LOG_GLOBAL" to
individual components' logging options.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-18 08:19:27 -04:00
Paul Sokolovsky 958ff51da8 net: sock: zsock_close: Explicitly cast few calls to (void).
To make Coverity happy.

Coverity-CID: 173646
Jira: ZEP-2465

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-09-01 09:31:19 -04:00
Paul Sokolovsky eb5d6fcef5 net: sockets: Be sure to initialize socket's queue on accept
When new socket context is created on accepting connection to a
listening socket, its recv_q FIFO should be initialized. Without
initialization, this worked by a chance when FIFO structure was
simple, but recent change to add dlist to it (which now needs
proper initialization) exposed this issue.

Jira: ZEP-2576

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-31 15:07:33 -04:00
Paul Sokolovsky d9af77bfd6 net: sockets: Consistently use "BSD Sockets compatible API" moniker
This is how it's called in the main docs, so use this same phrase in
Kconfig and samples too.

Also, added some articles to docs.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-17 12:02:55 -05:00
David B. Kinder 81f7c82625 doc: fix misspellings and missing EOF newlines
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-16 17:38:41 -05:00
Paul Sokolovsky c1a5573c85 net: sockets: Check return value of k_poll()
Mostly to make Coverity happy.

Coverity-CID: 173641

Jira: ZEP-2465

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-14 23:46:44 +03:00
Aska Wu eb8fd4aacf net: sockets: inet_pton() should return 1 on success
Add zsock_inet_pton() to convert the return value of net_addr_pton()
which returns 0 on success.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
2017-08-10 17:41:47 +03:00
Paul Sokolovsky f80b2ac865 net: sockets: Fix race condition with accept() call
For an accepted socket, we should set our receive callback and start
to queue packets ASAP (in the accept callback itself). Otherwise,
(if done in accept() call like before) we may miss to queue some
packets.

This issue wasn't exposed with slow SLIP and with emulated QEMU, but
easily exposed with Ethernet on a real hardware.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-08 12:44:28 +03:00
Jukka Rissanen 238c51f6d0 net: sockets: Comment Coverity warning about unchecked values
Coverity complains about non-checked return values here. This is false
positive as the return values do not need checking in this special
case because we are closing the socket.

Coverity-CID: 173646

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2017-08-07 21:32:03 -04:00
Paul Sokolovsky b7e3739043 net: sockets: Manage TCP receive window
As we buffer incoming packets in receive callbacks, we must decrease
receive window to avoid situation that incoming stream for one socket
uses up all buffers in the system and causes deadlock. Once user app
consumes queued data using recv() call, we increase window again.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-05 11:40:50 +03:00
Paul Sokolovsky 8088e14c74 net: sockets: poll: Use prefixed POLL* constants.
Default examples build with native POSIX aliases enabled, so
unprefixed refs got thru.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-08-04 17:24:22 +03:00
David B. Kinder 62a0cd246d doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.

This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).

This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".

We may not change this default role behavior, but it becomes an option
after the fixes in this patch.  In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).

Jira: ZEP-2414

Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-03 11:08:23 -04:00
Paul Sokolovsky 2c2e8b26b4 net: sockets: Implement poll() operation.
poll() allows to (efficiently) wait for available data on sockets,
and is essential operation for working with non-blocking sockets.

This is initial, very basic implementation, effectively supporting
just POLLIN operation. (POLLOUT implementation is dummy - it's
assumed that socket is always writable, as there's currently no
reasonable way to test that.)

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-18 21:46:06 +03:00
Paul Sokolovsky 1abb89f640 net: sockets: Implement non-blocking mode.
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-07-11 14:00:27 +03:00
Paul Sokolovsky 49eaa2b722 net: sockets: Explicitly flush conn/pkt queue on close()
If a socket is closed without reading all data from peer or accepting
all pending connection, they will be leaked. So, flush queues
explicitly.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 6055b6cd6b net: sockets: Implement recv() for DGRAM sockets
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 2b4a240859 net: sockets: Implement recv() for STREAM sockets
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 41e88ea40c net: sockets: Implement send()
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 7567962f3a net: sockets: Implement bind(), connect(), listen(), accept()
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 386c5bacd1 net: sockets: Add configurable option to provide raw POSIX API names
With CONFIG_NET_SOCKETS_POSIX_NAMES=y, "raw" POSIX names like
socket(), recv(), close() will be exposed (using macro defines).
The close() is the biggest culprit here, because in POSIX it
applies to any file descriptor, but in this implementation -
only to sockets.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00
Paul Sokolovsky 3432ff4fca net: sockets: Bootstrap Sockets API implementation
This adds Kconfig and build infrastructure and implements
zsock_socket() and zsock_close() functions.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-06-28 16:07:55 +03:00