2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* apps/netutils/netinit/netinit.c
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2019-04-29 18:22:56 +02:00
|
|
|
* Copyright (C) 2010-2012, 2014-2016, 2019 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-02-02 17:04:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* Is network initialization debug forced on? */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_DEBUG
|
2016-06-11 19:50:38 +02:00
|
|
|
# undef CONFIG_DEBUG_INFO
|
|
|
|
# define CONFIG_DEBUG_INFO 1
|
2014-08-17 17:51:26 +02:00
|
|
|
# undef CONFIG_DEBUG_NET
|
|
|
|
# define CONFIG_DEBUG_NET 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2014-08-06 19:59:41 +02:00
|
|
|
#include <pthread.h>
|
2014-08-17 22:48:27 +02:00
|
|
|
#include <semaphore.h>
|
2014-08-17 17:51:26 +02:00
|
|
|
#include <signal.h>
|
2014-08-17 22:48:27 +02:00
|
|
|
#include <assert.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2011-03-19 18:29:36 +01:00
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
#include <nuttx/net/mii.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/dhcpc.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
# include <nuttx/net/sixlowpan.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-20 17:09:47 +02:00
|
|
|
#ifdef CONFIG_NET_IEEE802154
|
|
|
|
# include <nuttx/net/ieee802154.h>
|
|
|
|
#endif
|
|
|
|
|
2016-06-09 16:18:49 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_NTPCLIENT
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/ntpclient.h"
|
2016-06-09 16:18:49 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#include "netutils/netinit.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_NETINIT
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-08-17 22:48:27 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-17 19:31:04 +02:00
|
|
|
/* Pick one and at most one supported link layer so that all decisions are
|
|
|
|
* made consistently.
|
2017-06-18 19:28:36 +02:00
|
|
|
*
|
|
|
|
* NOTE: Ethernet should always be selected with IEEE 802.11
|
2017-06-17 19:31:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
# undef CONFIG_NET_6LOWPAN
|
|
|
|
# undef CONFIG_NET_SLIP
|
|
|
|
# undef CONFIG_NET_TUN
|
|
|
|
# undef CONFIG_NET_LOCAL
|
|
|
|
# undef CONFIG_NET_USRSOCK
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_6LOWPAN)
|
|
|
|
# undef CONFIG_NET_SLIP
|
|
|
|
# undef CONFIG_NET_TUN
|
|
|
|
# undef CONFIG_NET_LOCAL
|
|
|
|
# undef CONFIG_NET_USRSOCK
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_SLIP)
|
|
|
|
# undef CONFIG_NET_TUN
|
|
|
|
# undef CONFIG_NET_LOCAL
|
|
|
|
# undef CONFIG_NET_USRSOCK
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_TUN)
|
|
|
|
# undef CONFIG_NET_LOCAL
|
|
|
|
# undef CONFIG_NET_USRSOCK
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_LOCAL)
|
|
|
|
# undef CONFIG_NET_USRSOCK
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_USRSOCK)
|
2017-08-20 17:09:47 +02:00
|
|
|
# undef CONFIG_NET_IEEE802154
|
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#elif defined(CONFIG_NET_IEEE802154)
|
2017-06-17 19:31:04 +02:00
|
|
|
# undef CONFIG_NET_LOOPBACK
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 00:02:11 +02:00
|
|
|
/* Only Ethernet and 6LoWPAN have MAC layer addresses */
|
2017-04-07 00:00:41 +02:00
|
|
|
|
|
|
|
#undef HAVE_MAC
|
|
|
|
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_NET_6LOWPAN)
|
|
|
|
# define HAVE_MAC 1
|
|
|
|
#endif
|
|
|
|
|
2017-07-31 00:03:56 +02:00
|
|
|
/* Currently there is only logic in 6LoWPAN configurations to
|
|
|
|
* set the IEEE 802.15.4 addresses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef HAVE_EADDR
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_6LOWPAN)
|
|
|
|
# if defined(CONFIG_WIRELESS_IEEE802154)
|
|
|
|
# define HAVE_EADDR 1
|
|
|
|
# elif defined(CONFIG_WIRELESS_PKTRADIO)
|
|
|
|
# warning Missing logic
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-06-17 19:31:04 +02:00
|
|
|
/* Provide a default DNS address */
|
2017-06-17 18:37:31 +02:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DRIPADDR) && !defined(CONFIG_NETINIT_DNSIPADDR)
|
|
|
|
# define CONFIG_NETINIT_DNSIPADDR CONFIG_NETINIT_DRIPADDR
|
2014-11-15 20:52:39 +01:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 20:26:20 +02:00
|
|
|
/* Select the single network device name supported this this network
|
2017-09-01 15:39:39 +02:00
|
|
|
* initialization logic. If multiple interfaces are present with different
|
2017-05-11 21:40:03 +02:00
|
|
|
* link types, the order of definition in the following conditional
|
2017-04-03 23:33:39 +02:00
|
|
|
* logic will select the one interface that will be used (which might
|
|
|
|
* not be the one that you want).
|
2014-11-15 20:52:39 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-19 16:49:36 +01:00
|
|
|
#undef NETINIT_HAVE_NETDEV
|
2017-05-02 16:26:21 +02:00
|
|
|
#if defined(CONFIG_DRIVERS_IEEE80211) /* Usually also has CONFIG_NET_ETHERNET */
|
|
|
|
# define NET_DEVNAME "wlan0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2017-05-02 16:26:21 +02:00
|
|
|
#elif defined(CONFIG_NET_ETHERNET)
|
2014-11-15 20:52:39 +01:00
|
|
|
# define NET_DEVNAME "eth0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2017-08-20 17:09:47 +02:00
|
|
|
#elif defined(CONFIG_NET_6LOWPAN) || defined(CONFIG_NET_IEEE802154)
|
2017-04-03 23:33:39 +02:00
|
|
|
# define NET_DEVNAME "wpan0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2018-04-03 18:01:55 +02:00
|
|
|
#elif defined(CONFIG_NET_BLUETOOTH)
|
|
|
|
# define NET_DEVNAME "bnep0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2014-11-15 20:52:39 +01:00
|
|
|
#elif defined(CONFIG_NET_SLIP)
|
2014-06-27 20:46:54 +02:00
|
|
|
# define NET_DEVNAME "sl0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# ifndef CONFIG_NETINIT_NOMAC
|
|
|
|
# error "CONFIG_NETINIT_NOMAC must be defined for SLIP"
|
2014-06-27 20:46:54 +02:00
|
|
|
# endif
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2015-05-19 20:26:20 +02:00
|
|
|
#elif defined(CONFIG_NET_TUN)
|
|
|
|
# define NET_DEVNAME "tun0"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2015-05-12 15:41:12 +02:00
|
|
|
#elif defined(CONFIG_NET_LOCAL)
|
|
|
|
# define NET_DEVNAME "lo"
|
2019-04-29 18:22:56 +02:00
|
|
|
# define NETINIT_HAVE_NETDEV
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
|
|
|
|
2019-11-19 16:49:36 +01:00
|
|
|
/* If we have no network device (perhaps only USRSOCK, local loopback, or
|
|
|
|
* Netlink sockets), then we cannot support the network monitor.
|
2015-08-24 21:59:52 +02:00
|
|
|
*/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifndef NETINIT_HAVE_NETDEV
|
|
|
|
# undef CONFIG_NETINIT_MONITOR
|
2015-08-24 21:59:52 +02:00
|
|
|
#endif
|
|
|
|
|
2015-02-03 23:19:08 +01:00
|
|
|
/* We need a valid IP domain (any domain) to create a socket that we can use
|
2020-02-23 05:51:44 +01:00
|
|
|
* to communicate with the network device.
|
2015-02-03 23:19:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4)
|
|
|
|
# define AF_INETX AF_INET
|
|
|
|
#elif defined(CONFIG_NET_IPv6)
|
|
|
|
# define AF_INETX AF_INET6
|
|
|
|
#endif
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* While the network is up, the network monitor really does nothing. It
|
|
|
|
* will wait for a very long time while waiting, it can be awakened by a
|
|
|
|
* signal indicating a change in network status.
|
|
|
|
*/
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
#define LONG_TIME_SEC (60*60) /* One hour in seconds */
|
|
|
|
#define SHORT_TIME_SEC (2) /* 2 seconds */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_MONITOR
|
2014-08-17 22:48:27 +02:00
|
|
|
static sem_t g_notify_sem;
|
|
|
|
#endif
|
|
|
|
|
2017-06-17 19:31:04 +02:00
|
|
|
#if defined(CONFIG_NET_IPv6) && !defined(CONFIG_NET_ICMPv6_AUTOCONF) && \
|
|
|
|
!defined(CONFIG_NET_6LOWPAN)
|
2015-02-03 15:01:55 +01:00
|
|
|
/* Host IPv6 address */
|
2015-01-19 16:03:10 +01:00
|
|
|
|
|
|
|
static const uint16_t g_ipv6_hostaddr[8] =
|
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_1),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_2),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_3),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_4),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_5),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_6),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_7),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6ADDR_8),
|
2015-01-19 16:03:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Default routine IPv6 address */
|
|
|
|
|
|
|
|
static const uint16_t g_ipv6_draddr[8] =
|
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_1),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_2),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_3),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_4),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_5),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_6),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_7),
|
|
|
|
HTONS(CONFIG_NETINIT_DRIPv6ADDR_8),
|
2015-01-19 16:03:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* IPv6 netmask */
|
|
|
|
|
|
|
|
static const uint16_t g_ipv6_netmask[8] =
|
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_1),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_2),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_3),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_4),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_5),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_6),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_7),
|
|
|
|
HTONS(CONFIG_NETINIT_IPv6NETMASK_8),
|
2015-01-19 16:03:10 +01:00
|
|
|
};
|
2017-06-17 19:31:04 +02:00
|
|
|
#endif /* CONFIG_NET_IPv6 && !CONFIG_NET_ICMPv6_AUTOCONF && !CONFIG_NET_6LOWPAN */
|
2015-01-19 16:03:10 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_set_macaddr
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-06-17 18:37:31 +02:00
|
|
|
* Set the hardware MAC address if the hardware is not capable of doing
|
|
|
|
* that for itself.
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(NETINIT_HAVE_NETDEV) && defined(CONFIG_NETINIT_NOMAC) && defined(HAVE_MAC)
|
|
|
|
static void netinit_set_macaddr(void)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2017-04-07 00:00:41 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
2014-08-06 18:12:10 +02:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
2017-07-31 00:03:56 +02:00
|
|
|
#elif defined(HAVE_EADDR)
|
2017-06-19 17:47:17 +02:00
|
|
|
uint8_t eaddr[8];
|
2017-04-07 00:00:41 +02:00
|
|
|
#endif
|
2014-08-06 19:59:41 +02:00
|
|
|
|
2014-08-06 18:12:10 +02:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2017-04-07 00:00:41 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
2014-08-06 18:12:10 +02:00
|
|
|
/* Use the configured, fixed MAC address */
|
|
|
|
|
2019-06-03 18:04:44 +02:00
|
|
|
mac[0] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 1)) & 0xff;
|
|
|
|
mac[1] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 0)) & 0xff;
|
|
|
|
|
|
|
|
mac[2] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 3)) & 0xff;
|
|
|
|
mac[3] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 2)) & 0xff;
|
|
|
|
mac[4] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 1)) & 0xff;
|
|
|
|
mac[5] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 0)) & 0xff;
|
2014-08-06 18:12:10 +02:00
|
|
|
|
|
|
|
/* Set the MAC address */
|
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr(NET_DEVNAME, mac);
|
2015-01-27 23:39:30 +01:00
|
|
|
|
2017-07-31 00:03:56 +02:00
|
|
|
#elif defined(HAVE_EADDR)
|
2017-06-17 18:04:18 +02:00
|
|
|
/* Use the configured, fixed extended address */
|
2017-04-07 00:00:41 +02:00
|
|
|
|
2019-06-03 18:04:44 +02:00
|
|
|
eaddr[0] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 3)) & 0xff;
|
|
|
|
eaddr[1] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 2)) & 0xff;
|
|
|
|
eaddr[2] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 1)) & 0xff;
|
|
|
|
eaddr[3] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 0)) & 0xff;
|
|
|
|
|
|
|
|
eaddr[4] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 3)) & 0xff;
|
|
|
|
eaddr[5] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 2)) & 0xff;
|
|
|
|
eaddr[6] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 1)) & 0xff;
|
|
|
|
eaddr[7] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 0)) & 0xff;
|
2017-04-07 00:00:41 +02:00
|
|
|
|
2017-06-19 00:02:11 +02:00
|
|
|
/* Set the 6LoWPAN extended address */
|
2017-04-07 00:00:41 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
netlib_seteaddr(NET_DEVNAME, eaddr);
|
2017-07-31 00:03:56 +02:00
|
|
|
#endif /* CONFIG_NET_ETHERNET or HAVE_EADDR */
|
2017-06-17 18:37:31 +02:00
|
|
|
}
|
|
|
|
#else
|
2019-04-29 18:22:56 +02:00
|
|
|
# define netinit_set_macaddr()
|
2017-06-17 18:37:31 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2017-06-17 18:37:31 +02:00
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_set_ipaddrs
|
2017-06-17 18:37:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup IP addresses.
|
|
|
|
*
|
2017-06-19 00:02:11 +02:00
|
|
|
* For 6LoWPAN, the IP address derives from the MAC address. Setting it
|
2017-06-17 19:31:04 +02:00
|
|
|
* to any user provided value is asking for trouble.
|
|
|
|
*
|
2017-06-17 18:37:31 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(NETINIT_HAVE_NETDEV) && !defined(CONFIG_NET_6LOWPAN) && ! \
|
2017-08-20 17:09:47 +02:00
|
|
|
defined(CONFIG_NET_IEEE802154)
|
2019-04-29 18:22:56 +02:00
|
|
|
static void netinit_set_ipaddrs(void)
|
2017-06-17 18:37:31 +02:00
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2017-06-17 18:37:31 +02:00
|
|
|
struct in_addr addr;
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Set up our host address */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifndef CONFIG_NETINIT_DHCPC
|
|
|
|
addr.s_addr = HTONL(CONFIG_NETINIT_IPADDR);
|
2011-03-18 20:46:25 +01:00
|
|
|
#else
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#endif
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(NET_DEVNAME, &addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_NETINIT_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(NET_DEVNAME, &addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_NETINIT_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(NET_DEVNAME, &addr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2018-11-07 18:25:39 +01:00
|
|
|
#ifndef CONFIG_NET_ICMPv6_AUTOCONF
|
2015-02-03 15:01:55 +01:00
|
|
|
/* Set up our fixed host address */
|
2015-01-19 16:03:10 +01:00
|
|
|
|
|
|
|
netlib_set_ipv6addr(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_hostaddr);
|
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
netlib_set_dripv6addr(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_draddr);
|
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
netlib_set_ipv6netmask(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_netmask);
|
2015-02-03 22:40:56 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_AUTOCONF */
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_DNS
|
|
|
|
addr.s_addr = HTONL(CONFIG_NETINIT_DNSIPADDR);
|
2015-07-12 19:53:23 +02:00
|
|
|
netlib_set_ipv4dnsaddr(&addr);
|
2012-10-28 14:20:36 +01:00
|
|
|
#endif
|
2017-06-17 18:37:31 +02:00
|
|
|
}
|
|
|
|
#else
|
2019-04-29 18:22:56 +02:00
|
|
|
# define netinit_set_ipaddrs()
|
2017-06-17 18:37:31 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2017-06-17 18:37:31 +02:00
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_net_bringup()
|
2017-06-17 18:37:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bring up the configured network
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(NETINIT_HAVE_NETDEV) && !defined(CONFIG_NETINIT_NETLOCAL)
|
|
|
|
static void netinit_net_bringup(void)
|
2017-06-17 18:37:31 +02:00
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_DHCPC
|
2017-06-17 18:37:31 +02:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
FAR void *handle;
|
|
|
|
#endif
|
2017-05-21 20:00:55 +02:00
|
|
|
|
|
|
|
/* Bring the network up. */
|
2017-05-19 17:37:17 +02:00
|
|
|
|
2017-06-17 18:37:31 +02:00
|
|
|
netlib_ifup(NET_DEVNAME);
|
2017-05-19 17:37:17 +02:00
|
|
|
|
2017-05-06 19:28:11 +02:00
|
|
|
#ifdef CONFIG_WIRELESS_WAPI
|
2017-05-09 14:39:08 +02:00
|
|
|
/* Associate the wlan with an access point. */
|
2017-05-02 17:43:48 +02:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_associate(NET_DEVNAME);
|
2017-05-02 17:43:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-11-07 18:25:39 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
|
|
|
|
/* Perform ICMPv6 auto-configuration */
|
|
|
|
|
|
|
|
netlib_icmpv6_autoconfiguration(NET_DEVNAME);
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_DHCPC
|
2017-05-09 14:39:08 +02:00
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
netlib_getmacaddr(NET_DEVNAME, mac);
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
2017-05-19 22:34:00 +02:00
|
|
|
handle = dhcpc_open(NET_DEVNAME, &mac, IFHWADDRLEN);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
/* Get an IP address. Note that there is no logic for renewing the IP
|
|
|
|
* address in this example. The address should be renewed in
|
|
|
|
* ds.lease_time/2 seconds.
|
2011-03-18 20:46:25 +01:00
|
|
|
*/
|
|
|
|
|
2017-05-02 17:43:48 +02:00
|
|
|
if (handle != NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
struct dhcpc_state ds =
|
|
|
|
{
|
|
|
|
};
|
2017-05-02 17:43:48 +02:00
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
if (dhcpc_request(handle, &ds) == OK)
|
2017-05-02 17:43:48 +02:00
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
netlib_set_ipv4addr(NET_DEVNAME, &ds.ipaddr);
|
|
|
|
|
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
|
|
|
netlib_set_ipv4netmask(NET_DEVNAME, &ds.netmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
|
|
|
netlib_set_dripv4addr(NET_DEVNAME, &ds.default_router);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
|
|
|
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
|
|
|
}
|
2017-05-02 17:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dhcpc_close(handle);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-06-09 16:18:49 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_NTPCLIENT
|
|
|
|
/* Start the NTP client */
|
|
|
|
|
|
|
|
ntpc_start();
|
|
|
|
#endif
|
2017-06-17 18:37:31 +02:00
|
|
|
}
|
|
|
|
#else
|
2019-04-29 18:22:56 +02:00
|
|
|
# define netinit_net_bringup()
|
2017-06-17 18:37:31 +02:00
|
|
|
#endif
|
2012-09-17 20:18:44 +02:00
|
|
|
|
2017-06-17 18:37:31 +02:00
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_configure
|
2017-06-17 18:37:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
static void netinit_configure(void)
|
2017-06-17 18:37:31 +02:00
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef NETINIT_HAVE_NETDEV
|
2017-06-17 18:37:31 +02:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_set_macaddr();
|
2017-06-17 18:37:31 +02:00
|
|
|
|
|
|
|
/* Set up IP addresses */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_set_ipaddrs();
|
2017-06-17 18:37:31 +02:00
|
|
|
|
|
|
|
/* That completes the 'local' initialization of the network device. */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifndef CONFIG_NETINIT_NETLOCAL
|
2017-06-17 18:37:31 +02:00
|
|
|
/* Bring the network up. */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_net_bringup();
|
2017-06-17 18:37:31 +02:00
|
|
|
#endif
|
2019-04-29 18:22:56 +02:00
|
|
|
#endif /* NETINIT_HAVE_NETDEV */
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_signal
|
2014-08-17 17:51:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This signal handler responds to changes in PHY status.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_MONITOR
|
|
|
|
static void netinit_signal(int signo, FAR siginfo_t *siginfo,
|
2014-08-17 17:51:26 +02:00
|
|
|
FAR void * context)
|
|
|
|
{
|
2014-08-17 22:48:27 +02:00
|
|
|
int semcount;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* What is the count on the semaphore? Don't over-post */
|
|
|
|
|
|
|
|
ret = sem_getvalue(&g_notify_sem, &semcount);
|
2016-06-20 19:48:15 +02:00
|
|
|
ninfo("Entry: semcount=%d\n", semcount);
|
2014-08-17 22:48:27 +02:00
|
|
|
|
|
|
|
if (ret == OK && semcount <= 0)
|
|
|
|
{
|
|
|
|
sem_post(&g_notify_sem);
|
|
|
|
}
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2016-06-20 19:48:15 +02:00
|
|
|
ninfo("Exit\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_monitor
|
2014-08-17 17:51:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Monitor link status, gracefully taking the link up and down as the
|
|
|
|
* link becomes available or as the link is lost.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_MONITOR
|
|
|
|
static int netinit_monitor(void)
|
2014-08-17 17:51:26 +02:00
|
|
|
{
|
2014-08-17 22:48:27 +02:00
|
|
|
struct timespec abstime;
|
|
|
|
struct timespec reltime;
|
2014-08-17 17:51:26 +02:00
|
|
|
struct ifreq ifr;
|
|
|
|
struct sigaction act;
|
2019-07-26 18:58:14 +02:00
|
|
|
struct sigaction oact;
|
2014-08-17 17:51:26 +02:00
|
|
|
bool devup;
|
|
|
|
int ret;
|
|
|
|
int sd;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Entry\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/* Initialize the notification semaphore */
|
|
|
|
|
|
|
|
DEBUGVERIFY(sem_init(&g_notify_sem, 0, 0));
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* Get a socket descriptor that we can use to communicate with the network
|
|
|
|
* interface driver.
|
|
|
|
*/
|
|
|
|
|
2015-02-03 23:19:08 +01:00
|
|
|
sd = socket(AF_INETX, SOCK_DGRAM, 0);
|
2014-08-17 17:51:26 +02:00
|
|
|
if (sd < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: Failed to create a socket: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach a signal handler so that we do not lose PHY events */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
act.sa_sigaction = netinit_signal;
|
2014-08-17 17:51:26 +02:00
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
2019-07-26 18:58:14 +02:00
|
|
|
ret = sigaction(CONFIG_NETINIT_SIGNO, &act, &oact);
|
2014-08-17 17:51:26 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: sigaction() failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop, waiting for changes in link status */
|
|
|
|
|
2019-07-11 20:15:08 +02:00
|
|
|
for (; ; )
|
2014-08-17 17:51:26 +02:00
|
|
|
{
|
2014-08-18 00:51:56 +02:00
|
|
|
/* Configure to receive a signal on changes in link status */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2019-01-27 23:45:56 +01:00
|
|
|
memset(&ifr, 0, sizeof(struct ifreq));
|
2014-08-17 17:51:26 +02:00
|
|
|
strncpy(ifr.ifr_name, NET_DEVNAME, IFNAMSIZ);
|
|
|
|
|
2019-01-27 23:45:56 +01:00
|
|
|
ifr.ifr_mii_notify_event.sigev_notify = SIGEV_SIGNAL;
|
2019-04-29 18:22:56 +02:00
|
|
|
ifr.ifr_mii_notify_event.sigev_signo = CONFIG_NETINIT_SIGNO;
|
2014-08-18 00:51:56 +02:00
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCMIINOTIFY, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCMIINOTIFY) failed: %d\n", ret);
|
2014-08-18 00:51:56 +02:00
|
|
|
goto errout_with_sigaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the driver think that the link is up or down? */
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
ret = ioctl(sd, SIOCGIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCGIFFLAGS) failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
devup = ((ifr.ifr_flags & IFF_UP) != 0);
|
|
|
|
|
|
|
|
/* Get the current PHY address in use. This probably does not change,
|
|
|
|
* but just in case...
|
|
|
|
*
|
|
|
|
* NOTE: We are assuming that the network device name is preserved in
|
|
|
|
* the ifr structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCGMIIPHY, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCGMIIPHY) failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the PHY status register */
|
|
|
|
|
|
|
|
ifr.ifr_mii_reg_num = MII_MSR;
|
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCGMIIREG, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCGMIIREG) failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("%s: devup=%d PHY address=%02x MSR=%04x\n",
|
2014-08-17 19:26:02 +02:00
|
|
|
ifr.ifr_name, devup, ifr.ifr_mii_phy_id, ifr.ifr_mii_val_out);
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
/* Check for link up or down */
|
|
|
|
|
|
|
|
if ((ifr.ifr_mii_val_out & MII_MSR_LINKSTATUS) != 0)
|
|
|
|
{
|
|
|
|
/* Link up... does the drive think that the link is up? */
|
|
|
|
|
|
|
|
if (!devup)
|
|
|
|
{
|
|
|
|
/* No... We just transitioned from link down to link up.
|
|
|
|
* Bring the link up.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Bringing the link up\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
ifr.ifr_flags = IFF_UP;
|
|
|
|
ret = ioctl(sd, SIOCSIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCSIFFLAGS) failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And wait for a short delay. We will want to recheck the
|
|
|
|
* link status again soon.
|
|
|
|
*/
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
reltime.tv_sec = SHORT_TIME_SEC;
|
|
|
|
reltime.tv_nsec = 0;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The link is still up. Take a long, well-deserved rest */
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
reltime.tv_sec = LONG_TIME_SEC;
|
|
|
|
reltime.tv_nsec = 0;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Link down... Was the driver link state already down? */
|
|
|
|
|
|
|
|
if (devup)
|
|
|
|
{
|
|
|
|
/* No... we just transitioned from link up to link down. Take
|
|
|
|
* the link down.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Taking the link down\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
ifr.ifr_flags = IFF_DOWN;
|
|
|
|
ret = ioctl(sd, SIOCSIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: ioctl(SIOCSIFFLAGS) failed: %d\n", ret);
|
2014-08-17 17:51:26 +02:00
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In either case, wait for the short, configurable delay */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
reltime.tv_sec = CONFIG_NETINIT_RETRYMSEC / 1000;
|
|
|
|
reltime.tv_nsec = (CONFIG_NETINIT_RETRYMSEC % 1000) * 1000000;
|
2014-08-17 22:48:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for either the semaphore to be posted for a timed-out to
|
|
|
|
* occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
DEBUGVERIFY(clock_gettime(CLOCK_REALTIME, &abstime));
|
|
|
|
|
|
|
|
abstime.tv_sec += reltime.tv_sec;
|
|
|
|
abstime.tv_nsec += reltime.tv_nsec;
|
2015-02-20 14:07:36 +01:00
|
|
|
if (abstime.tv_nsec >= 1000000000)
|
2014-08-17 22:48:27 +02:00
|
|
|
{
|
|
|
|
abstime.tv_sec++;
|
|
|
|
abstime.tv_nsec -= 1000000000;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
2014-08-17 22:48:27 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sem_timedwait(&g_notify_sem, &abstime);
|
2014-08-17 22:48:27 +02:00
|
|
|
sched_unlock();
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/* TODO: Stop the PHY notifications and remove the signal handler. */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
errout_with_notification:
|
2020-03-07 18:16:43 +01:00
|
|
|
ifr.ifr_mii_notify_event.sigev_notify = SIGEV_NONE;
|
|
|
|
ioctl(sd, SIOCMIINOTIFY, (unsigned long)&ifr);
|
2014-08-17 17:51:26 +02:00
|
|
|
errout_with_sigaction:
|
2020-01-02 13:09:50 +01:00
|
|
|
sigaction(CONFIG_NETINIT_SIGNO, &oact, NULL);
|
2014-08-17 17:51:26 +02:00
|
|
|
errout_with_socket:
|
|
|
|
close(sd);
|
|
|
|
errout:
|
2016-06-14 22:18:07 +02:00
|
|
|
nerr("ERROR: Aborting\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_thread
|
2014-08-17 17:51:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_THREAD
|
|
|
|
static pthread_addr_t netinit_thread(pthread_addr_t arg)
|
2014-08-17 17:51:26 +02:00
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Entry\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
/* Configure the network */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_configure();
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_MONITOR
|
2014-08-17 17:51:26 +02:00
|
|
|
/* Monitor the network status */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_monitor();
|
2014-08-17 17:51:26 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Exit\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
return NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-09-17 00:08:06 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-08-06 19:59:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-04-29 18:22:56 +02:00
|
|
|
* Name: netinit_bringup
|
2014-08-06 19:59:41 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
int netinit_bringup(void)
|
2014-08-06 19:59:41 +02:00
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
#ifdef CONFIG_NETINIT_THREAD
|
2014-08-06 19:59:41 +02:00
|
|
|
struct sched_param sparam;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t tid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Start the network initialization thread to perform the network bring-up
|
|
|
|
* asynchronously.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
2019-04-29 18:22:56 +02:00
|
|
|
sparam.sched_priority = CONFIG_NETINIT_THREAD_PRIORITY;
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
pthread_attr_setstacksize(&attr, CONFIG_NETINIT_THREAD_STACKSIZE);
|
2014-08-06 19:59:41 +02:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Starting netinit thread\n");
|
2019-04-29 18:22:56 +02:00
|
|
|
ret = pthread_create(&tid, &attr, netinit_thread, NULL);
|
2014-08-06 19:59:41 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: Failed to create netinit thread: %d\n", ret);
|
2020-01-02 13:09:50 +01:00
|
|
|
netinit_thread(NULL);
|
2014-08-06 19:59:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Detach the thread because we will not be joining to it */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_detach(tid);
|
2014-08-06 19:59:41 +02:00
|
|
|
|
|
|
|
/* Name the thread */
|
|
|
|
|
|
|
|
pthread_setname_np(tid, "netinit");
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Perform network initialization sequentially */
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
netinit_configure();
|
2014-09-17 00:08:06 +02:00
|
|
|
return OK;
|
2014-08-06 19:59:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#endif /* CONFIG_NETUTILS_NETINIT */
|