modify nuttx bug of lack of files

This commit is contained in:
TangYiwen123
2021-06-11 11:25:07 +08:00
parent 4a7f51a5e9
commit 6d0f2ca39a
6752 changed files with 1528133 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
menu "Network Device Operations"
config NETDEV_IOCTL
bool
default n
config NETDEV_PHY_IOCTL
bool "Enable PHY ioctl()"
default n
select NETDEV_IOCTL
---help---
Enable support for ioctl() commands to access PHY registers
config NETDEV_CAN_BITRATE_IOCTL
bool "Enable CAN bitrate ioctl()"
default n
select NETDEV_IOCTL
depends on NET_CAN
---help---
Enable support for ioctl() commands to change CAN bitrate
config NETDEV_WIRELESS_IOCTL
bool "Enable Wireless ioctl()"
default n
select NETDEV_IOCTL
depends on DRIVERS_WIRELESS
---help---
Enable support for wireless device ioctl() commands
config NETDEV_IFINDEX
bool "Enable IF index support"
default n
---help---
Enable support for references devices by an interface index.
This feature is automatically enabled when raw, PACKET sockets
are enabled.
When enabled, these option also enables the user interfaces:
if_nametoindex() and if_indextoname().
config NETDOWN_NOTIFIER
bool "Support network down notifications"
default n
depends on SCHED_WORKQUEUE
select WQUEUE_NOTIFIER
---help---
Enable building of logic that will execute on the low priority work
thread when the network is taken down. This is is a general purpose
notifier, but was developed specifically to support SIGHUP poll()
logic.
endmenu # Network Device Operations
@@ -0,0 +1,40 @@
############################################################################
# net/netdev/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
# Support for operations on network devices
NETDEV_CSRCS += netdev_register.c netdev_ioctl.c netdev_txnotify.c
NETDEV_CSRCS += netdev_findbyname.c netdev_findbyaddr.c netdev_findbyindex.c
NETDEV_CSRCS += netdev_count.c netdev_ifconf.c netdev_foreach.c
NETDEV_CSRCS += netdev_unregister.c netdev_carrier.c netdev_default.c
NETDEV_CSRCS += netdev_verify.c netdev_lladdrsize.c
ifeq ($(CONFIG_NETDEV_IFINDEX),y)
NETDEV_CSRCS += netdev_indextoname.c netdev_nametoindex.c
endif
ifeq ($(CONFIG_NETDOWN_NOTIFIER),y)
SOCK_CSRCS += netdown_notifier.c
endif
# Include netdev build support
DEPPATH += --dep-path netdev
VPATH += :netdev
+566
View File
@@ -0,0 +1,566 @@
/****************************************************************************
* net/netdev/netdev.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __NET_NETDEV_NETDEV_H
#define __NET_NETDEV_NETDEV_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdbool.h>
#include <nuttx/net/ip.h>
#ifdef CONFIG_NETDOWN_NOTIFIER
# include <nuttx/wqueue.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NETDEV_IFINDEX is enabled then there is limit to the number of
* devices that can be registered due to the nature of some static data.
*/
#define MAX_IFINDEX 32
/****************************************************************************
* Public Data
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/* List of registered Ethernet device drivers. You must have the network
* locked in order to access this list.
*
* NOTE that this duplicates a declaration in net/tcp/tcp.h
*/
EXTERN struct net_driver_s *g_netdevices;
#ifdef CONFIG_NETDEV_IFINDEX
/* The set of network devices that have been registered. This is used to
* assign a unique device index to the newly registered device.
*
* REVISIT: The width of g_nassigned limits the number of registered
* devices to 32 (MAX_IFINDEX).
*/
EXTERN uint32_t g_devset;
/* The set of network devices that have been freed. The purpose of this
* set is to postpone reuse of a interface index for as long as possible,
* i.e., don't reuse an interface index until all of the possible indices
* have been used.
*/
EXTERN uint32_t g_devfreed;
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/* Callback from netdev_foreach() */
struct net_driver_s; /* Forward reference */
typedef int (*netdev_callback_t)(FAR struct net_driver_s *dev,
FAR void *arg);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: netdev_ifup / netdev_ifdown
*
* Description:
* Bring the interface up/down
*
****************************************************************************/
void netdev_ifup(FAR struct net_driver_s *dev);
void netdev_ifdown(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_verify
*
* Description:
* Verify that the specified device still exists
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
bool netdev_verify(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_findbyname
*
* Description:
* Find a previously registered network device using its assigned
* network interface name
*
* Input Parameters:
* ifname The interface name of the device of interest
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyname(FAR const char *ifname);
/****************************************************************************
* Name: netdev_foreach
*
* Description:
* Enumerate each registered network device. This function will terminate
* when either (1) all devices have been enumerated or (2) when a callback
* returns any non-zero value.
*
* NOTE 1: The network must be locked throughout the enumeration.
* NOTE 2: No checks are made on devices. For examples, callbacks will
* will be made on network devices that are in the 'down' state.
* The callback implementations must take into account all
* network device state. Typically, a network in the down state
* would not terminate the traversal.
*
* Input Parameters:
* callback - Will be called for each registered device
* arg - Opaque user argument passed to callback()
*
* Returned Value:
* 0: Enumeration completed
* 1: Enumeration terminated early by callback
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
int netdev_foreach(netdev_callback_t callback, FAR void *arg);
/****************************************************************************
* Name: netdev_findby_lipv4addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv4 address assigned to the network device. Or any
* IPv4 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr);
#endif
/****************************************************************************
* Name: netdev_findby_lipv6addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv6 address assigned to the network device. Or any
* IPv6 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_lipv6addr(
const net_ipv6addr_t lipaddr);
#endif
/****************************************************************************
* Name: netdev_findby_ripv4addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv4 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* the broadcast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_ripv4addr(in_addr_t lipaddr,
in_addr_t ripaddr);
#endif
/****************************************************************************
* Name: netdev_findby_ripv6addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv6 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* a multicast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_ripv6addr(
const net_ipv6addr_t lipaddr,
const net_ipv6addr_t ripaddr);
#endif
/****************************************************************************
* Name: netdev_findbyindex
*
* Description:
* Find a previously registered network device by assigned interface index.
*
* Input Parameters:
* ifindex - The interface index. This is a one-based index and must be
* greater than zero.
*
* Returned Value:
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyindex(int ifindex);
/****************************************************************************
* Name: netdev_nextindex
*
* Description:
* Return the interface index to the next valid device.
*
* Input Parameters:
* ifindex - The first interface index to check. Usually in a traversal
* this would be the previous interface index plus 1.
*
* Returned Value:
* The interface index for the next network driver. -ENODEV is returned if
* there are no further devices with assigned interface indices.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_nextindex(int ifindex);
#endif
/****************************************************************************
* Name: netdev_indextoname
*
* Description:
* The if_indextoname() function maps an interface index to its
* corresponding name.
*
* Input Parameters:
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
* if_indextoname() will place in this buffer the name of the
* interface with index ifindex.
*
* Returned Value:
* If ifindex is an interface index, then the function will return zero
* (OK). Otherwise, the function returns a negated errno value;
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_indextoname(unsigned int ifindex, FAR char *ifname);
#endif
/****************************************************************************
* Name: netdev_nametoindex
*
* Description:
* The if_nametoindex() function returns the interface index corresponding
* to name ifname.
*
* Input Parameters:
* ifname - The interface name
*
* Returned Value:
* The corresponding index if ifname is the name of an interface;
* otherwise, a negated errno value is returned.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
unsigned int netdev_nametoindex(FAR const char *ifname);
#endif
/****************************************************************************
* Name: netdev_default
*
* Description:
* Return the default network device. REVISIT: At present this function
* arbitrarily returns the first UP device at the head of the device
* list. Perhaps the default device should be a device name
* configuration option?
*
* So why is this here: It represents my current though for what to do
* if a socket is connected with INADDY_ANY. In this case, I suppose we
* should use the IP address associated with some default device???
*
* Input Parameters:
* NULL
*
* Returned Value:
* Pointer to default network driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_default(void);
/****************************************************************************
* Name: netdev_ipv4_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
void netdev_ipv4_txnotify(in_addr_t lipaddr, in_addr_t ripaddr);
#endif /* CONFIG_NET_IPv4 */
/****************************************************************************
* Name: netdev_ipv6_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
void netdev_ipv6_txnotify(FAR const net_ipv6addr_t lipaddr,
FAR const net_ipv6addr_t ripaddr);
#endif /* CONFIG_NET_IPv6 */
/****************************************************************************
* Name: netdev_txnotify_dev
*
* Description:
* Notify the device driver that new TX data is available. This variant
* would be called when the upper level logic already understands how the
* packet will be routed.
*
* Input Parameters:
* dev - The network device driver state structure.
*
* Returned Value:
* None
*
****************************************************************************/
void netdev_txnotify_dev(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_count
*
* Description:
* Return the number of network devices
*
* Input Parameters:
* None
*
* Returned Value:
* The number of network devices
*
****************************************************************************/
int netdev_count(void);
/****************************************************************************
* Name: netdev_ipv4_ifconf
*
* Description:
* Return the IPv4 configuration of each network adapter
*
* Input Parameters:
* ifc - A reference to the instance of struct ifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The network is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
struct ifconf; /* Forward reference */
int netdev_ipv4_ifconf(FAR struct ifconf *ifc);
#endif
/****************************************************************************
* Name: netdev_ipv6_ifconf
*
* Description:
* Return the IPv6 configuration of each network adapter
*
* Input Parameters:
* lifc - A reference to the instance of struct lifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The network is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
struct lifconf; /* Forward reference */
int netdev_ipv6_ifconf(FAR struct lifconf *lifc);
#endif
/****************************************************************************
* Name: netdown_notifier_setup
*
* Description:
* Set up to perform a callback to the worker function when the network
* goes down. The worker function will execute on the high priority
* worker thread.
*
* Input Parameters:
* worker - The worker function to execute on the high priority work
* queue when data is available in the UDP readahead buffer.
* dev - The network driver to be monitored
* arg - A user-defined argument that will be available to the worker
* function when it runs.
* Returned Value:
* > 0 - The signal notification is in place. The returned value is a
* key that may be used later in a call to
* netdown_notifier_teardown().
* == 0 - The the device is already down. No signal notification will
* be provided.
* < 0 - An unexpected error occurred and no signal will be sent. The
* returned value is a negated errno value that indicates the
* nature of the failure.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
int netdown_notifier_setup(worker_t worker, FAR struct net_driver_s *dev,
FAR void *arg);
#endif
/****************************************************************************
* Name: netdown_notifier_teardown
*
* Description:
* Eliminate a network down notification previously setup by
* netdown_notifier_setup(). This function should only be called if the
* notification should be aborted prior to the notification. The
* notification will automatically be torn down after the signal is sent.
*
* Input Parameters:
* key - The key value returned from a previous call to
* netdown_notifier_setup().
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
int netdown_notifier_teardown(int key);
#endif
/****************************************************************************
* Name: netdown_notifier_signal
*
* Description:
* A network has gone down has been buffered. Execute worker thread
* functions for all threads monitoring the state of the device.
*
* Input Parameters:
* dev - The TCP connection where read-ahead data was just buffered.
*
* Returned Value:
* None.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
void netdown_notifier_signal(FAR struct net_driver_s *dev);
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __NET_NETDEV_NETDEV_H */
@@ -0,0 +1,102 @@
/****************************************************************************
* net/netdev/netdev_carrier.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <nuttx/net/netdev.h>
#include "netdev/netdev.h"
#include "netlink/netlink.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_carrier_on
*
* Description:
* Notifies the networking layer about an available carrier.
* (e.g. a cable was plugged in)
*
* Input Parameters:
* dev - The device driver structure
*
* Returned Value:
* 0:Success; negated errno on failure
*
****************************************************************************/
int netdev_carrier_on(FAR struct net_driver_s *dev)
{
if (dev)
{
dev->d_flags |= IFF_RUNNING;
netlink_device_notify(dev);
return OK;
}
return -EINVAL;
}
/****************************************************************************
* Name: netdev_carrier_off
*
* Description:
* Notifies the networking layer about an disappeared carrier.
* (e.g. a cable was unplugged)
*
* Input Parameters:
* dev - The device driver structure
*
* Returned Value:
* 0:Success; negated errno on failure
*
****************************************************************************/
int netdev_carrier_off(FAR struct net_driver_s *dev)
{
if (dev)
{
dev->d_flags &= ~IFF_RUNNING;
netlink_device_notify(dev);
/* Notify clients that the network has been taken down */
devif_dev_event(dev, NULL, NETDEV_DOWN);
return OK;
}
return -EINVAL;
}
@@ -0,0 +1,62 @@
/****************************************************************************
* net/netdev/netdev_count.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <errno.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_count
*
* Description:
* Return the number of network devices
*
* Input Parameters:
* None
*
* Returned Value:
* The number of network devices
*
****************************************************************************/
int netdev_count(void)
{
struct net_driver_s *dev;
int ndev;
net_lock();
for (dev = g_netdevices, ndev = 0; dev; dev = dev->flink, ndev++);
net_unlock();
return ndev;
}
@@ -0,0 +1,86 @@
/****************************************************************************
* net/netdev/netdev_default.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_default
*
* Description:
* Return the default network device. REVISIT: At present this function
* arbitrarily returns the first UP device at the head of the device
* list. Perhaps the default device should be a device name
* configuration option?
*
* So why is this here: It represents my current though for what to do
* if a socket is connected with INADDY_ANY. In this case, I suppose we
* should use the IP address associated with some default device???
*
* Input Parameters:
* NULL
*
* Returned Value:
* Pointer to default network driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_default(void)
{
FAR struct net_driver_s *ret = NULL;
FAR struct net_driver_s *dev;
/* Examine each registered network device */
net_lock();
for (dev = g_netdevices; dev; dev = dev->flink)
{
/* Is the interface in the "up" state? */
if ((dev->d_flags & IFF_UP) != 0)
{
/* Return a reference to the first device that we find in the UP
* state (but not the loopback device unless it is the only
* device).
*/
ret = dev;
if (dev->d_lltype != NET_LL_LOOPBACK)
{
break;
}
}
}
net_unlock();
return ret;
}
@@ -0,0 +1,333 @@
/****************************************************************************
* net/netdev/netdev_findbyaddr.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <netinet/in.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/ip.h>
#include "utils/utils.h"
#include "devif/devif.h"
#include "inet/inet.h"
#include "route/route.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_findby_lipv4addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv4 address assigned to the network device. Or any
* IPv4 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr)
{
FAR struct net_driver_s *dev;
/* Examine each registered network device */
net_lock();
for (dev = g_netdevices; dev; dev = dev->flink)
{
/* Is the interface in the "up" state? */
if ((dev->d_flags & IFF_UP) != 0 &&
!net_ipv4addr_cmp(dev->d_ipaddr, INADDR_ANY))
{
/* Yes.. check for an address match (under the netmask) */
if (net_ipv4addr_maskcmp(dev->d_ipaddr, lipaddr,
dev->d_netmask))
{
/* Its a match */
net_unlock();
return dev;
}
}
}
/* No device with the matching address found */
net_unlock();
return NULL;
}
#endif /* CONFIG_NET_IPv4 */
/****************************************************************************
* Name: netdev_findby_lipv6addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv6 address assigned to the network device. Or any
* IPv6 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_lipv6addr(
const net_ipv6addr_t lipaddr)
{
FAR struct net_driver_s *dev;
/* Examine each registered network device */
net_lock();
for (dev = g_netdevices; dev; dev = dev->flink)
{
/* Is the interface in the "up" state? */
if ((dev->d_flags & IFF_UP) != 0 &&
!net_ipv6addr_cmp(dev->d_ipv6addr, g_ipv6_unspecaddr))
{
/* Yes.. check for an address match (under the netmask) */
if (net_ipv6addr_maskcmp(dev->d_ipv6addr, lipaddr,
dev->d_ipv6netmask))
{
/* Its a match */
net_unlock();
return dev;
}
}
}
/* No device with the matching address found */
net_unlock();
return NULL;
}
#endif /* CONFIG_NET_IPv6 */
/****************************************************************************
* Name: netdev_findby_ripv4addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv4 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* the broadcast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_ripv4addr(in_addr_t lipaddr,
in_addr_t ripaddr)
{
struct net_driver_s *dev;
#ifdef CONFIG_NET_ROUTE
in_addr_t router;
int ret;
#endif
/* First, check if this is the broadcast IP address */
if (net_ipv4addr_cmp(ripaddr, INADDR_BROADCAST))
{
/* Yes.. Check the local, bound address. Is it INADDR_ANY? */
if (net_ipv4addr_cmp(lipaddr, INADDR_ANY))
{
/* Yes.. In this case, I think we are supposed to send the
* broadcast packet out ALL locally available networks. I am not
* sure of that and, in any event, there is nothing we can do
* about that here.
*/
return netdev_default();
}
else
{
/* Return the device associated with the local address */
return netdev_findby_lipv4addr(lipaddr);
}
}
/* Check if the address maps to a locally available network */
dev = netdev_findby_lipv4addr(ripaddr);
if (dev)
{
return dev;
}
/* No.. The address lies on an external network */
#ifdef CONFIG_NET_ROUTE
/* If we have a routing table, then perhaps we can find the local
* address of a router that can forward packets to the external network.
*/
ret = net_ipv4_router(ripaddr, &router);
if (ret >= 0)
{
/* Success... try to find the network device associated with the local
* router address
*/
dev = netdev_findby_lipv4addr(router);
if (dev)
{
return dev;
}
}
#endif /* CONFIG_NET_ROUTE */
/* The above lookup will fail if the packet is being sent out of our
* out subnet to a router and there is no routing information. Let's
* try the default network device.
*/
return netdev_default();
}
#endif /* CONFIG_NET_IPv4 */
/****************************************************************************
* Name: netdev_findby_ripv6addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv6 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* a multicast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_ripv6addr(
const net_ipv6addr_t lipaddr,
const net_ipv6addr_t ripaddr)
{
struct net_driver_s *dev;
#ifdef CONFIG_NET_ROUTE
net_ipv6addr_t router;
int ret;
#endif
/* First, check if this is the multicast IP address */
if (net_is_addr_mcast(ripaddr))
{
/* Yes.. Check the local, bound address. Is it the IPv6 unspecified
* address?
*/
if (net_ipv6addr_cmp(lipaddr, g_ipv6_unspecaddr))
{
/* Yes.. In this case, I think we are supposed to send the
* broadcast packet out ALL locally available networks. I am not
* sure of that and, in any event, there is nothing we can do
* about that here.
*/
return netdev_default();
}
else
{
/* Return the device associated with the local address */
return netdev_findby_lipv6addr(lipaddr);
}
}
/* Check if the address maps to a locally available network */
dev = netdev_findby_lipv6addr(ripaddr);
if (dev)
{
return dev;
}
/* No.. The address lies on an external network */
#ifdef CONFIG_NET_ROUTE
/* If we have a routing table, then perhaps we can find the local
* address of a router that can forward packets to the external network.
*/
ret = net_ipv6_router(ripaddr, router);
if (ret >= 0)
{
/* Success... try to find the network device associated with the local
* router address
*/
dev = netdev_findby_lipv6addr(router);
if (dev)
{
return dev;
}
}
#endif /* CONFIG_NET_ROUTE */
/* The above lookup will fail if the packet is being sent out of our
* out subnet to a router and there is no routing information. Let's
* try the default network device.
*/
return netdev_default();
}
#endif /* CONFIG_NET_IPv6 */
@@ -0,0 +1,161 @@
/****************************************************************************
* net/netdev/netdev_findbyindex.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_findbyindex
*
* Description:
* Find a previously registered network device by assigned interface index.
*
* Input Parameters:
* ifindex - The interface index. This is a one-based index and must be
* greater than zero.
*
* Returned Value:
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyindex(int ifindex)
{
FAR struct net_driver_s *dev;
int i;
#ifdef CONFIG_NETDEV_IFINDEX
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
if (ifindex < 1 || ifindex > MAX_IFINDEX)
{
return NULL;
}
#endif
net_lock();
#ifdef CONFIG_NETDEV_IFINDEX
/* Check if this index has been assigned */
if ((g_devset & (1L << (ifindex - 1))) == 0)
{
/* This index has not been assigned */
net_unlock();
return NULL;
}
#endif
for (i = 0, dev = g_netdevices; dev; i++, dev = dev->flink)
{
#ifdef CONFIG_NETDEV_IFINDEX
/* Check if the index matches the index assigned when the device was
* registered.
*/
if (dev->d_ifindex == ifindex)
#else
/* NOTE that this option is not a safe way to enumerate network
* devices: There could be changes to the list of registered device
* causing a given index to be meaningless (unless, of course, the
* caller keeps the network locked).
*/
if (i == (ifindex - 1))
#endif
{
net_unlock();
return dev;
}
}
net_unlock();
return NULL;
}
/****************************************************************************
* Name: netdev_nextindex
*
* Description:
* Return the interface index to the next valid device.
*
* Input Parameters:
* ifindex - The first interface index to check. Usually in a traversal
* this would be the previous interface index plus 1.
*
* Returned Value:
* The interface index for the next network driver. -ENODEV is returned if
* there are no further devices with assigned interface indices.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_nextindex(int ifindex)
{
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
ifindex--;
if (ifindex >= 0 && ifindex < MAX_IFINDEX)
{
net_lock();
for (; ifindex < MAX_IFINDEX; ifindex++)
{
if ((g_devset & (1L << ifindex)) != 0)
{
/* NOTE that the index + 1 is returned. Zero is reserved to
* mean no-index in the POSIX standards.
*/
net_unlock();
return ifindex + 1;
}
}
net_unlock();
}
return -ENODEV;
}
#endif
@@ -0,0 +1,74 @@
/****************************************************************************
* net/netdev/netdev_findbyname.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <errno.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_findbyname
*
* Description:
* Find a previously registered network device using its assigned
* network interface name
*
* Input Parameters:
* ifname The interface name of the device of interest
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyname(FAR const char *ifname)
{
FAR struct net_driver_s *dev;
if (ifname)
{
net_lock();
for (dev = g_netdevices; dev; dev = dev->flink)
{
if (strcmp(ifname, dev->d_ifname) == 0)
{
net_unlock();
return dev;
}
}
net_unlock();
}
return NULL;
}
@@ -0,0 +1,83 @@
/****************************************************************************
* net/netdev/netdev_foreach.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <nuttx/net/netdev.h>
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_foreach
*
* Description:
* Enumerate each registered network device. This function will terminate
* when either (1) all devices have been enumerated or (2) when a callback
* returns any non-zero value.
*
* NOTE 1: The network must be locked throughout the enumeration.
* NOTE 2: No checks are made on devices. For examples, callbacks will
* will be made on network devices that are in the 'down' state.
* The callback implementations must take into account all
* network device state. Typically, a network in the down state
* would not terminate the traversal.
*
* Input Parameters:
* callback - Will be called for each registered device
* arg - Opaque user argument passed to callback()
*
* Returned Value:
* 0: Enumeration completed
* 1: Enumeration terminated early by callback
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
int netdev_foreach(netdev_callback_t callback, FAR void *arg)
{
FAR struct net_driver_s *dev;
int ret = 0;
if (callback != NULL)
{
for (dev = g_netdevices; dev; dev = dev->flink)
{
if (callback(dev, arg) != 0)
{
ret = 1;
break;
}
}
}
return ret;
}
@@ -0,0 +1,287 @@
/****************************************************************************
* net/netdev/netdev_ifconf.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <net/if.h>
#include <nuttx/net/netdev.h>
#include "inet/inet.h"
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Private Types
****************************************************************************/
/* The form of information passed with netdev_foreach() callback */
#ifdef CONFIG_NET_IPv4
struct ifconf_ipv4_info_s
{
size_t bufsize;
FAR struct ifconf *ifc;
};
#endif
#ifdef CONFIG_NET_IPv6
struct ifconf_ipv6_info_s
{
size_t bufsize;
FAR struct lifconf *lifc;
};
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: ifconf_ipv4_callback
*
* Description:
* Callback from netdev_foreach() that does the real implementation of
* netdev_ipv4_ifconf().
*
* Input Parameters:
* dev - The network device for this callback.
* arg - User callback argument
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
static int ifconf_ipv4_callback(FAR struct net_driver_s *dev, FAR void *arg)
{
FAR struct ifconf_ipv4_info_s *info = (FAR struct ifconf_ipv4_info_s *)arg;
FAR struct ifconf *ifc;
DEBUGASSERT(dev != NULL && info != NULL && info->ifc != NULL);
ifc = info->ifc;
/* Check if this adapter has an IPv4 address assigned and is in the UP
* state.
*/
if (!net_ipv4addr_cmp(dev->d_ipaddr, INADDR_ANY) &&
(dev->d_flags & IFF_UP) != 0)
{
/* Check if we would exceed the buffer space provided by the caller.
* NOTE: A common usage model is:
*
* 1) Provide no buffer with the first SIOCGIFCONF command. In this
* case, only the size of the buffer needed for all of IPv4
* capable interface information is returned.
* 2) Allocate a buffer of that size
* 3) Then use the allocated buffer to receive all of the IPv4
* interface information on the second SIOCGIFCONF command.
*
* CAUTION: The number of IPv6 capable interface may change between
* calls. It is the callers responsibility to behave sanely in such
* cases.
*/
if (ifc->ifc_len + sizeof(struct ifreq) <= info->bufsize)
{
FAR struct ifreq *req =
(FAR struct ifreq *)&ifc->ifc_buf[ifc->ifc_len];
FAR struct sockaddr_in *inaddr =
(FAR struct sockaddr_in *)&req->ifr_addr;
/* There is space for information about another adapter. Within
* each ifreq structure, ifr_name will receive the interface name
* and ifr_addr the address. The actual number of bytes
* transferred is returned in ifc_len.
*/
strncpy(req->ifr_name, dev->d_ifname, IFNAMSIZ);
inaddr->sin_family = AF_INET;
inaddr->sin_port = 0;
net_ipv4addr_copy(inaddr->sin_addr.s_addr, dev->d_ipaddr);
memset(inaddr->sin_zero, 0, sizeof(inaddr->sin_zero));
}
/* Increment the size of the buffer in any event */
ifc->ifc_len += sizeof(struct ifreq);
}
return 0;
}
#endif
/****************************************************************************
* Name: ifconf_ipv6_callback
*
* Description:
* Callback from netdev_foreach() that does the real implementation of
* netdev_ipv6_ifconf().
*
* Input Parameters:
* dev - The network device for this callback.
* arg - User callback argument
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
static int ifconf_ipv6_callback(FAR struct net_driver_s *dev, FAR void *arg)
{
FAR struct ifconf_ipv6_info_s *info = (FAR struct ifconf_ipv6_info_s *)arg;
FAR struct lifconf *lifc;
DEBUGASSERT(dev != NULL && info != NULL && info->lifc != NULL);
lifc = info->lifc;
/* Check if this adapter has an IPv6 address assigned and is in the UP
* state.
*/
if (!net_ipv6addr_cmp(dev->d_ipv6addr, g_ipv6_unspecaddr) &&
(dev->d_flags & IFF_UP) != 0)
{
/* Check if we would exceed the buffer space provided by the caller.
* NOTE: A common usage model is:
*
* 1) Provide no buffer with the first SIOCGLIFCONF command. In this
* case, only the size of the buffer needed for all of IPv6
* capable interface information is returned.
* 2) Allocate a buffer of that size
* 3) Then use the allocated buffer to receive all of the IPv6
* interface information on the second SIOCGLIFCONF command.
*
* CAUTION: The number of IPv6 capable interface may change between
* calls. It is the callers responsibility to behave sanely in such
* cases.
*/
if (lifc->lifc_len + sizeof(struct lifreq) <= info->bufsize)
{
FAR struct lifreq *req =
(FAR struct lifreq *)&lifc->lifc_buf[lifc->lifc_len];
FAR struct sockaddr_in6 *inaddr =
(FAR struct sockaddr_in6 *)&req->lifr_addr;
/* There is space for information about another adapter. Within
* each ifreq structure, lifr_name will receive the interface
* name and lifr_addr the address. The actual number of bytes
* transferred is returned in lifc_len.
*/
strncpy(req->lifr_name, dev->d_ifname, IFNAMSIZ);
inaddr->sin6_family = AF_INET6;
inaddr->sin6_port = 0;
net_ipv6addr_copy(inaddr->sin6_addr.s6_addr16, dev->d_ipv6addr);
}
/* Increment the size of the buffer in any event */
lifc->lifc_len += sizeof(struct lifreq);
}
return 0;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_ipv4_ifconf
*
* Description:
* Return the IPv4 configuration of each network adapter that (1) has
* and IPv4 address assigned and (2) is in the UP state
*
* Input Parameters:
* ifc - A reference to the instance of struct ifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The network is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
int netdev_ipv4_ifconf(FAR struct ifconf *ifc)
{
struct ifconf_ipv4_info_s info;
info.bufsize = ifc->ifc_len;
info.ifc = ifc;
ifc->ifc_len = 0;
return netdev_foreach(ifconf_ipv4_callback, (FAR void *)&info);
}
#endif
/****************************************************************************
* Name: netdev_ipv6_ifconf
*
* Description:
* Return the IPv6 configuration of each network adapter that (1) has
* and IPv6 address assigned and (2) is in the UP state.
*
* Input Parameters:
* lifc - A reference to the instance of struct lifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The network is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
int netdev_ipv6_ifconf(FAR struct lifconf *lifc)
{
struct ifconf_ipv6_info_s info;
info.bufsize = lifc->lifc_len;
info.lifc = lifc;
lifc->lifc_len = 0;
return netdev_foreach(ifconf_ipv6_callback, (FAR void *)&info);
}
#endif
@@ -0,0 +1,119 @@
/****************************************************************************
* net/netdev/netdev_indextoname.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <net/if.h>
#include "nuttx/net/net.h"
#include "nuttx/net/netdev.h"
#include "netdev/netdev.h"
#ifdef CONFIG_NETDEV_IFINDEX
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_indextoname
*
* Description:
* The if_indextoname() function maps an interface index to its
* corresponding name.
*
* Input Parameters:
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
* if_indextoname() will place in this buffer the name of the
* interface with index ifindex.
*
* Returned Value:
* If ifindex is an interface index, then the function will return zero
* (OK). Otherwise, the function returns a negated errno value;
*
****************************************************************************/
int netdev_indextoname(unsigned int ifindex, FAR char *ifname)
{
FAR struct net_driver_s *dev;
int ret = -ENODEV;
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
DEBUGASSERT(ifname != NULL);
/* Find the driver with this name */
net_lock();
dev = netdev_findbyindex(ifindex);
if (dev != NULL)
{
memcpy(ifname, dev->d_ifname, IF_NAMESIZE);
ret = OK;
}
net_unlock();
return ret;
}
/****************************************************************************
* Name: if_indextoname
*
* Description:
* The if_indextoname() function maps an interface index to its
* corresponding name.
*
* Input Parameters:
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
* if_indextoname() will place in this buffer the name of the
* interface with index ifindex.
*
* Returned Value:
* If ifindex is an interface index, then the function will return the
* value supplied by ifname. Otherwise, the function returns a NULL pointer
* and sets errno to indicate the error.
*
****************************************************************************/
FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname)
{
int ret;
/* Let netdev_indextoname to the work */
ret = netdev_indextoname(ifindex, ifname);
if (ret < 0)
{
set_errno(-ret);
return NULL;
}
return ifname;
}
#endif /* CONFIG_NETDEV_IFINDEX */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,166 @@
/****************************************************************************
* net/netdev/netdev_lladdrsize.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <net/if.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/radiodev.h>
#include <nuttx/net/bluetooth.h>
#include <nuttx/net/sixlowpan.h>
#include "netdev/netdev.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_pktradio_addrlen
*
* Description:
* Returns the size of the node address associated with a packet radio.
* This is probably CONFIG_PKTRADIO_ADDRLEN but we cannot be sure in the
* case that there are multiple packet radios. In that case, we have to
* query the radio for its address length.
*
* Input Parameters:
* dev - A reference to the device of interest
*
* Returned Value:
* The size of the MAC address associated with this radio
*
****************************************************************************/
#if defined(CONFIG_NET_6LOWPAN) && (defined(CONFIG_WIRELESS_PKTRADIO) || \
defined(CONFIG_NET_BLUETOOTH))
static inline int netdev_pktradio_addrlen(FAR struct net_driver_s *dev)
{
FAR struct radio_driver_s *radio = (FAR struct radio_driver_s *)dev;
struct radiodev_properties_s properties;
int ret;
DEBUGASSERT(radio != NULL && radio->r_properties != NULL);
ret = radio->r_properties(radio, &properties);
if (ret < 0)
{
return ret;
}
return properties.sp_addrlen;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_lladdrsize
*
* Description:
* Returns the size of the MAC address associated with a network device.
*
* Input Parameters:
* dev - A reference to the device of interest
*
* Returned Value:
* The size of the MAC address associated with this device
*
****************************************************************************/
int netdev_lladdrsize(FAR struct net_driver_s *dev)
{
DEBUGASSERT(dev != NULL);
/* Get the length of the address for this link layer type */
switch (dev->d_lltype)
{
#ifdef CONFIG_NET_ETHERNET
case NET_LL_ETHERNET:
case NET_LL_IEEE80211:
{
/* Size of the Ethernet MAC address */
return IFHWADDRLEN;
}
#endif
#ifdef CONFIG_NET_6LOWPAN
#ifdef CONFIG_WIRELESS_BLUETOOTH
case NET_LL_BLUETOOTH:
{
/* 6LoWPAN can be configured to use either extended or short
* addressing.
*/
return BLUETOOTH_HDRLEN;
}
#endif /* CONFIG_WIRELESS_BLUETOOTH */
#ifdef CONFIG_WIRELESS_IEEE802154
case NET_LL_IEEE802154:
{
/* 6LoWPAN can be configured to use either extended or short
* addressing.
*/
#ifdef CONFIG_NET_6LOWPAN_EXTENDEDADDR
return NET_6LOWPAN_EADDRSIZE;
#else
return NET_6LOWPAN_SADDRSIZE;
#endif
}
#endif /* CONFIG_WIRELESS_IEEE802154 */
#if defined(CONFIG_WIRELESS_PKTRADIO) || defined(CONFIG_NET_BLUETOOTH)
#ifdef CONFIG_WIRELESS_PKTRADIO
case NET_LL_PKTRADIO:
#endif
#ifdef CONFIG_NET_BLUETOOTH
case NET_LL_BLUETOOTH:
#endif
{
/* Return the size of the packet radio address */
return netdev_pktradio_addrlen(dev);
}
#endif /* CONFIG_WIRELESS_PKTRADIO || CONFIG_NET_BLUETOOTH */
#endif /* CONFIG_NET_6LOWPAN */
default:
{
/* The link layer type associated has no address */
return 0;
}
}
}
@@ -0,0 +1,109 @@
/****************************************************************************
* net/netdev/netdev_nametoindex.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <net/if.h>
#include "nuttx/net/net.h"
#include "nuttx/net/netdev.h"
#include "netdev/netdev.h"
#ifdef CONFIG_NETDEV_IFINDEX
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_nametoindex
*
* Description:
* The if_nametoindex() function returns the interface index corresponding
* to name ifname.
*
* Input Parameters:
* ifname - The interface name
*
* Returned Value:
* The corresponding index if ifname is the name of an interface;
* otherwise, a negated errno value is returned.
*
****************************************************************************/
unsigned int netdev_nametoindex(FAR const char *ifname)
{
FAR struct net_driver_s *dev;
unsigned int ifindex = -ENODEV;
/* Find the driver with this name */
net_lock();
dev = netdev_findbyname(ifname);
if (dev != NULL)
{
ifindex = dev->d_ifindex;
}
net_unlock();
return ifindex;
}
/****************************************************************************
* Name: if_nametoindex
*
* Description:
* The if_nametoindex() function returns the interface index corresponding
* to name ifname.
*
* Input Parameters:
* ifname - The interface name
*
* Returned Value:
* The corresponding index if ifname is the name of an interface;
* otherwise, zero. Although not specified, the errno value will be set.
*
****************************************************************************/
unsigned int if_nametoindex(FAR const char *ifname)
{
int ret;
/* Let netdev_nametoindex to the work */
ret = netdev_nametoindex(ifname);
if (ret < 0)
{
set_errno(-ret);
return 0;
}
return ret;
}
#endif /* CONFIG_NETDEV_IFINDEX */
@@ -0,0 +1,464 @@
/****************************************************************************
* net/netdev/netdev_register.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <nuttx/net/netconfig.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/ethernet.h>
#include <nuttx/net/bluetooth.h>
#include <nuttx/net/can.h>
#include "utils/utils.h"
#include "igmp/igmp.h"
#include "mld/mld.h"
#include "netdev/netdev.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NETDEV_ETH_FORMAT "eth%d"
#define NETDEV_LO_FORMAT "lo"
#define NETDEV_SLIP_FORMAT "sl%d"
#define NETDEV_TUN_FORMAT "tun%d"
#define NETDEV_BNEP_FORMAT "bnep%d"
#define NETDEV_PAN_FORMAT "pan%d"
#define NETDEV_WLAN_FORMAT "wlan%d"
#define NETDEV_WPAN_FORMAT "wpan%d"
#define NETDEV_WWAN_FORMAT "wwan%d"
#define NETDEV_CAN_FORMAT "can%d"
#if defined(CONFIG_DRIVERS_IEEE80211) /* Usually also has CONFIG_NET_ETHERNET */
# define NETDEV_DEFAULT_FORMAT NETDEV_WLAN_FORMAT
#elif defined(CONFIG_NET_ETHERNET)
# define NETDEV_DEFAULT_FORMAT NETDEV_ETH_FORMAT
#elif defined(CONFIG_NET_6LOWPAN)
# define NETDEV_DEFAULT_FORMAT NETDEV_WPAN_FORMAT
#elif defined(CONFIG_NET_SLIP)
# define NETDEV_DEFAULT_FORMAT NETDEV_SLIP_FORMAT
#elif defined(CONFIG_NET_TUN)
# define NETDEV_DEFAULT_FORMAT NETDEV_TUN_FORMAT
#elif defined(CONFIG_NET_CAN)
# define NETDEV_DEFAULT_FORMAT NETDEV_CAN_FORMAT
#else /* if defined(CONFIG_NET_LOOPBACK) */
# define NETDEV_DEFAULT_FORMAT NETDEV_LO_FORMAT
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* List of registered Ethernet device drivers */
struct net_driver_s *g_netdevices = NULL;
#ifdef CONFIG_NETDEV_IFINDEX
/* The set of network devices that have been registered. This is used to
* assign a unique device index to the newly registered device.
*
* REVISIT: The width of g_nassigned limits the number of registered
* devices to 32 (MAX_IFINDEX).
*/
uint32_t g_devset;
/* The set of network devices that have been freed. The purpose of this
* set is to postpone reuse of a interface index for as long as possible,
* i.e., don't reuse an interface index until all of the possible indices
* have been used.
*/
uint32_t g_devfreed;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: find_devnum
*
* Description:
* Given a device name format string, find the next device number for the
* class of device represented by that format string.
*
* Input Parameters:
* devfmt - The device format string
*
* Returned Value:
* The next device number for that device class
*
****************************************************************************/
static int find_devnum(FAR const char *devfmt)
{
FAR struct net_driver_s *curr;
size_t fmt_size;
int result = 0;
fmt_size = strlen(devfmt);
/* Assumed that devfmt is xxx%d */
DEBUGASSERT(fmt_size > 2);
fmt_size -= 2;
/* Search the list of currently registered network devices */
for (curr = g_netdevices; curr; curr = curr->flink )
{
/* Does this device name match the format we were given? */
if (strncmp(curr->d_ifname, devfmt, fmt_size) == 0)
{
/* Yes.. increment the candidate device number */
result++;
}
}
/* Return this next device number for this format */
return result;
}
/****************************************************************************
* Name: get_ifindex
*
* Description:
* Assign a unique interface index to the device.
*
* Input Parameters:
* None
*
* Returned Value:
* The interface index assigned to the device. -ENOSPC is returned if
* more the MAX_IFINDEX names have been assigned.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
static int get_ifindex(void)
{
uint32_t devset;
int ndx;
/* Try to postpone re-using interface indices as long as possible */
devset = g_devset | g_devfreed;
if (devset == 0xffffffff)
{
/* Time start re-using interface indices */
devset = g_devset;
g_devfreed = 0;
}
/* Search for an unused index */
for (ndx = 0; ndx < MAX_IFINDEX; ndx++)
{
uint32_t bit = 1L << ndx;
if ((devset & bit) == 0)
{
/* Indicate that this index is in use */
g_devset |= bit;
/* NOTE that the index + 1 is returned. Zero is reserved to
* mean no-index in the POSIX standards.
*/
return ndx + 1;
}
}
return -ENOSPC;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_register
*
* Description:
* Register a network device driver and assign a name to it so that it can
* be found in subsequent network ioctl operations on the device.
*
* A custom, device-specific interface name format string may be selected
* by putting that format string into the device structure's d_ifname[]
* array before calling netdev_register(). Otherwise, the d_ifname[] must
* be zeroed on entry.
*
* Input Parameters:
* dev - The device driver structure to be registered.
* lltype - Link level protocol used by the driver (Ethernet, SLIP, TUN,
* ...)
*
* Returned Value:
* 0:Success; negated errno on failure
*
* Assumptions:
* Called during system bring-up, but also when a removable network
* device is installed.
*
****************************************************************************/
int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
{
FAR char devfmt_str[IFNAMSIZ];
FAR const char *devfmt;
uint16_t pktsize = 0;
uint8_t llhdrlen = 0;
int devnum;
#ifdef CONFIG_NETDEV_IFINDEX
int ifindex;
#endif
if (dev != NULL)
{
/* Set the protocol used by the device and the size of the link
* header used by this protocol.
*/
switch (lltype)
{
#ifdef CONFIG_NET_LOOPBACK
case NET_LL_LOOPBACK: /* Local loopback */
llhdrlen = 0;
pktsize = NET_LO_PKTSIZE;
devfmt = NETDEV_LO_FORMAT;
break;
#endif
#ifdef CONFIG_NET_ETHERNET
case NET_LL_ETHERNET: /* Ethernet */
llhdrlen = ETH_HDRLEN;
pktsize = CONFIG_NET_ETH_PKTSIZE;
devfmt = NETDEV_ETH_FORMAT;
break;
#endif
#ifdef CONFIG_DRIVERS_IEEE80211
case NET_LL_IEEE80211: /* IEEE 802.11 */
llhdrlen = ETH_HDRLEN;
pktsize = CONFIG_NET_ETH_PKTSIZE;
devfmt = NETDEV_WLAN_FORMAT;
break;
#endif
#ifdef CONFIG_NET_CAN
case NET_LL_CAN: /* CAN bus */
dev->d_llhdrlen = 0;
dev->d_pktsize = NET_CAN_PKTSIZE;
devfmt = NETDEV_CAN_FORMAT;
break;
#endif
#ifdef CONFIG_NET_BLUETOOTH
case NET_LL_BLUETOOTH: /* Bluetooth */
llhdrlen = BLUETOOTH_MAX_HDRLEN; /* Determined at runtime */
#ifdef CONFIG_NET_6LOWPAN
pktsize = CONFIG_NET_6LOWPAN_PKTSIZE;
#endif
devfmt = NETDEV_BNEP_FORMAT;
break;
#endif
#if defined(CONFIG_NET_6LOWPAN) || defined(CONFIG_NET_IEEE802154)
case NET_LL_IEEE802154: /* IEEE 802.15.4 MAC */
case NET_LL_PKTRADIO: /* Non-IEEE 802.15.4 packet radio */
llhdrlen = 0; /* Determined at runtime */
#ifdef CONFIG_NET_6LOWPAN
pktsize = CONFIG_NET_6LOWPAN_PKTSIZE;
#endif
devfmt = NETDEV_WPAN_FORMAT;
break;
#endif
#ifdef CONFIG_NET_SLIP
case NET_LL_SLIP: /* Serial Line Internet Protocol (SLIP) */
llhdrlen = 0;
pktsize = CONFIG_NET_SLIP_PKTSIZE;
devfmt = NETDEV_SLIP_FORMAT;
break;
#endif
#ifdef CONFIG_NET_TUN
case NET_LL_TUN: /* Virtual Network Device (TUN) */
llhdrlen = 0; /* This will be overwritten by tun_ioctl
* if used as a TAP (layer 2) device */
pktsize = CONFIG_NET_TUN_PKTSIZE;
devfmt = NETDEV_TUN_FORMAT;
break;
#endif
#ifdef CONFIG_NET_MBIM
case NET_LL_MBIM:
llhdrlen = 0;
pktsize = 1200;
devfmt = NETDEV_WWAN_FORMAT;
break;
#endif
default:
nerr("ERROR: Unrecognized link type: %d\n", lltype);
return -EINVAL;
}
/* Update the package length. A network driver may provide custom
* values for MAC header length and for the maximum packet size. Some
* driver implementations (for example, the simulator) will require
* dynamic MTU calculations to support tunneling (as an example).
*/
if (dev->d_llhdrlen == 0)
{
dev->d_llhdrlen = llhdrlen;
}
if (dev->d_pktsize == 0)
{
dev->d_pktsize = pktsize;
}
/* Remember the verified link type */
dev->d_lltype = (uint8_t)lltype;
/* There are no clients of the device yet */
dev->d_conncb = NULL;
dev->d_devcb = NULL;
/* We need exclusive access for the following operations */
net_lock();
#ifdef CONFIG_NETDEV_IFINDEX
ifindex = get_ifindex();
if (ifindex < 0)
{
return ifindex;
}
dev->d_ifindex = (uint8_t)ifindex;
#endif
/* Get the next available device number and assign a device name to
* the interface
*/
/* Check if the caller has provided a user device-specific interface
* name format string (in d_ifname).
*/
if (dev->d_ifname[0] != '\0')
{
/* Copy the string in a temporary buffer. How do we know that the
* string is valid and not just uninitialized memory? We don't.
* Let's at least make certain that the format string is NUL
* terminated.
*/
dev->d_ifname[IFNAMSIZ - 1] = '\0';
strncpy(devfmt_str, dev->d_ifname, IFNAMSIZ);
/* Then use the content of the temporary buffer as the format
* string.
*/
devfmt = (FAR const char *)devfmt_str;
}
#ifdef CONFIG_NET_LOOPBACK
/* The local loopback device is a special case: There can be only one
* local loopback device so it is unnumbered.
*/
if (lltype == NET_LL_LOOPBACK)
{
devnum = 0;
}
else
#endif
{
devnum = find_devnum(devfmt);
}
/* Complete the device name by including the device number (if
* included in the format).
*/
snprintf(dev->d_ifname, IFNAMSIZ, devfmt, devnum);
/* Add the device to the list of known network devices */
dev->flink = g_netdevices;
g_netdevices = dev;
#ifdef CONFIG_NET_IGMP
/* Configure the device for IGMP support */
igmp_devinit(dev);
#endif
#ifdef CONFIG_NET_MLD
/* Configure the device for MLD support */
mld_devinit(dev);
#endif
net_unlock();
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_DRIVERS_IEEE80211)
ninfo("Registered MAC: %02x:%02x:%02x:%02x:%02x:%02x as dev: %s\n",
dev->d_mac.ether.ether_addr_octet[0],
dev->d_mac.ether.ether_addr_octet[1],
dev->d_mac.ether.ether_addr_octet[2],
dev->d_mac.ether.ether_addr_octet[3],
dev->d_mac.ether.ether_addr_octet[4],
dev->d_mac.ether.ether_addr_octet[5],
dev->d_ifname);
#else
ninfo("Registered dev: %s\n", dev->d_ifname);
#endif
return OK;
}
return -EINVAL;
}
@@ -0,0 +1,132 @@
/****************************************************************************
* net/netdev/netdev_txnotify.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/ip.h>
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_ipv4_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
void netdev_ipv4_txnotify(in_addr_t lipaddr, in_addr_t ripaddr)
{
FAR struct net_driver_s *dev;
/* Find the device driver that serves the subnet of the remote address */
dev = netdev_findby_ripv4addr(lipaddr, ripaddr);
if (dev && dev->d_txavail)
{
/* Notify the device driver that new TX data is available. */
dev->d_txavail(dev);
}
}
#endif /* CONFIG_NET_IPv4 */
/****************************************************************************
* Name: netdev_ipv6_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
void netdev_ipv6_txnotify(FAR const net_ipv6addr_t lipaddr,
FAR const net_ipv6addr_t ripaddr)
{
FAR struct net_driver_s *dev;
/* Find the device driver that serves the subnet of the remote address */
dev = netdev_findby_ripv6addr(lipaddr, ripaddr);
if (dev && dev->d_txavail)
{
/* Notify the device driver that new TX data is available. */
dev->d_txavail(dev);
}
}
#endif /* CONFIG_NET_IPv6 */
/****************************************************************************
* Name: netdev_txnotify_dev
*
* Description:
* Notify the device driver that new TX data is available. This variant
* would be called when the upper level logic already understands how the
* packet will be routed.
*
* Input Parameters:
* dev - The network device driver state structure.
*
* Returned Value:
* None
*
****************************************************************************/
void netdev_txnotify_dev(FAR struct net_driver_s *dev)
{
if (dev != NULL && dev->d_txavail != NULL)
{
/* Notify the device driver that new TX data is available. */
dev->d_txavail(dev);
}
}
@@ -0,0 +1,168 @@
/****************************************************************************
* net/netdev/netdev_unregister.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_NET_SLIP
# define NETDEV_FORMAT "sl%d"
#else
# define NETDEV_FORMAT "eth%d"
#endif
/****************************************************************************
* Name: free_ifindex
*
* Description:
* Free a interface index to the assigned to device.
*
* Input Parameters:
* dev - Instance of device structure for the unregistered device.
*
* Returned Value:
* None.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
static inline void free_ifindex(int ndx)
{
uint32_t bit;
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ndx > 0 && ndx <= MAX_IFINDEX);
ndx--;
/* Set the bit in g_devset corresponding to the zero based index */
bit = 1 << ndx;
/* The bit should be in the set state */
DEBUGASSERT((g_devset & bit) != 0 && (g_devfreed & bit) == 0);
g_devset &= ~bit;
g_devfreed |= bit;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_unregister
*
* Description:
* Unregister a network device driver.
*
* Input Parameters:
* dev - The device driver structure to un-register
*
* Returned Value:
* 0:Success; negated errno on failure
*
* Assumptions:
* Currently only called for USB networking devices when the device is
* physically removed from the slot
*
****************************************************************************/
int netdev_unregister(FAR struct net_driver_s *dev)
{
struct net_driver_s *prev;
struct net_driver_s *curr;
if (dev)
{
net_lock();
/* Find the device in the list of known network devices */
for (prev = NULL, curr = g_netdevices;
curr && curr != dev;
prev = curr, curr = curr->flink);
/* Remove the device to the list of known network devices */
if (curr)
{
/* Where was the entry */
if (prev)
{
/* The entry was in the middle or at the end of the list */
prev->flink = curr->flink;
}
else
{
/* The entry was at the beginning of the list */
g_netdevices = curr->flink;
}
curr->flink = NULL;
}
#ifdef CONFIG_NETDEV_IFINDEX
free_ifindex(dev->d_ifindex);
#endif
net_unlock();
#ifdef CONFIG_NET_ETHERNET
ninfo("Unregistered MAC: %02x:%02x:%02x:%02x:%02x:%02x as dev: %s\n",
dev->d_mac.ether.ether_addr_octet[0],
dev->d_mac.ether.ether_addr_octet[1],
dev->d_mac.ether.ether_addr_octet[2],
dev->d_mac.ether.ether_addr_octet[3],
dev->d_mac.ether.ether_addr_octet[4],
dev->d_mac.ether.ether_addr_octet[5], dev->d_ifname);
#else
ninfo("Unregistered dev: %s\n", dev->d_ifname);
#endif
return OK;
}
return -EINVAL;
}
@@ -0,0 +1,72 @@
/****************************************************************************
* net/netdev/netdev_verify.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <nuttx/net/netdev.h>
#include "utils/utils.h"
#include "netdev/netdev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdev_verify
*
* Description:
* Verify that the specified device still exists
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
bool netdev_verify(FAR struct net_driver_s *dev)
{
FAR struct net_driver_s *chkdev;
bool valid = false;
/* Search the list of registered devices */
net_lock();
for (chkdev = g_netdevices; chkdev != NULL; chkdev = chkdev->flink)
{
/* Is the network device that we are looking for? */
if (chkdev == dev)
{
/* Yes.. return true */
valid = true;
break;
}
}
net_unlock();
return valid;
}
@@ -0,0 +1,142 @@
/****************************************************************************
* net/netdev/netdown_notifier.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <assert.h>
#include <nuttx/wqueue.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/netdev.h>
#include "netdev/netdev.h"
#ifdef CONFIG_NETDOWN_NOTIFIER
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netdown_notifier_setup
*
* Description:
* Set up to perform a callback to the worker function when the network
* goes down. The worker function will execute on the high priority
* worker thread.
*
* Input Parameters:
* worker - The worker function to execute on the low priority work
* queue when data is available in the UDP read-ahead buffer.
* dev - The network driver to be monitored
* arg - A user-defined argument that will be available to the worker
* function when it runs.
* Returned Value:
* > 0 - The notification is in place. The returned value is a key that
* may be used later in a call to netdown_notifier_teardown().
* == 0 - The the device is already down. No notification will be
* provided.
* < 0 - An unexpected error occurred and notification will occur. The
* returned value is a negated errno value that indicates the
* nature of the failure.
*
****************************************************************************/
int netdown_notifier_setup(worker_t worker, FAR struct net_driver_s *dev,
FAR void *arg)
{
struct work_notifier_s info;
DEBUGASSERT(worker != NULL);
/* If network driver is already down, then return zero without setting up
* the notification.
*/
if ((dev->d_flags & IFF_UP) == 0)
{
return 0;
}
/* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
info.evtype = WORK_NET_DOWN;
info.qid = LPWORK;
info.qualifier = dev;
info.arg = arg;
info.worker = worker;
return work_notifier_setup(&info);
}
/****************************************************************************
* Name: netdown_notifier_teardown
*
* Description:
* Eliminate a network down notification previously setup by
* netdown_notifier_setup(). This function should only be called if the
* notification should be aborted prior to the notification. The
* notification will automatically be torn down after the notification.
*
* Input Parameters:
* key - The key value returned from a previous call to
* netdown_notifier_setup().
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int netdown_notifier_teardown(int key)
{
/* This is just a simple wrapper around work_notifier_teardown(). */
return work_notifier_teardown(key);
}
/****************************************************************************
* Name: netdown_notifier_signal
*
* Description:
* A network has gone down has been buffered. Execute worker thread
* functions for all threads monitoring the state of the device.
*
* Input Parameters:
* dev - The TCP connection where read-ahead data was just buffered.
*
* Returned Value:
* None.
*
****************************************************************************/
void netdown_notifier_signal(FAR struct net_driver_s *dev)
{
/* This is just a simple wrapper around work_notifier_signal(). */
work_notifier_signal(WORK_NET_DOWN, dev);
}
#endif /* CONFIG_NETDOWN_NOTIFIER */