From f6de06f9f9959027ee598c0177f676aa4f2ca627 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 4 Nov 2012 18:54:04 +0000 Subject: [PATCH] Add interfaces flags, extend ifconfig, add ifup and ifdown commands (Darcy Gong git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5308 42af7a65-404d-4744-a932-0658087f49c3 --- apps/ChangeLog.txt | 6 + apps/include/netutils/uiplib.h | 5 + apps/netutils/uiplib/Makefile | 3 +- apps/netutils/uiplib/uip_gethostaddr.c | 2 + apps/netutils/uiplib/uip_getifflag.c | 118 +++++++++++++ apps/netutils/uiplib/uip_getmacaddr.c | 2 + apps/netutils/uiplib/uip_setifflag.c | 141 ++++++++++++++++ apps/netutils/uiplib/uiplib.c | 57 +++++++ apps/netutils/webclient/webclient.c | 1 + apps/nshlib/README.txt | 43 +++-- apps/nshlib/nsh.h | 34 +++- apps/nshlib/nsh_netcmds.c | 222 +++++++++++++++++++++++-- apps/nshlib/nsh_parse.c | 32 ++-- nuttx/ChangeLog | 2 + nuttx/Documentation/NuttShell.html | 199 +++++++++++++++------- nuttx/include/net/if.h | 9 + nuttx/include/nuttx/net/ioctl.h | 6 + nuttx/include/nuttx/net/uip/uip-arch.h | 4 + nuttx/net/netdev_ioctl.c | 151 ++++++++++++++--- 19 files changed, 905 insertions(+), 132 deletions(-) create mode 100644 apps/netutils/uiplib/uip_getifflag.c create mode 100644 apps/netutils/uiplib/uip_setifflag.c diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index 0c4e94fd93..cd1b4eb53a 100644 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -400,3 +400,9 @@ * apps/netutils/webclient, apps/netutils.codes, and apps/examples/wgetjson: Add support for wget POST interface. Contributed by Darcy Gong. * apps/examples/relays: A relay example contributed by Darcy Gong. + * apps/nshlib/nsh_netcmds: Add ifup and ifdown commands (from Darcy + Gong). + * apps/nshlib/nsh_netcmds: Extend the ifconfig command so that it + supports setting IP addresses, network masks, name server addresses, + and hardware address (from Darcy Gong). + diff --git a/apps/include/netutils/uiplib.h b/apps/include/netutils/uiplib.h index b994173621..8e66fb9709 100644 --- a/apps/include/netutils/uiplib.h +++ b/apps/include/netutils/uiplib.h @@ -102,6 +102,7 @@ extern "C" { */ EXTERN bool uiplib_ipaddrconv(const char *addrstr, uint8_t *addr); +EXTERN bool uiplib_hwmacconv(const char *hwstr, uint8_t *hw); /* Get and set IP/MAC addresses (Ethernet L2 only) */ @@ -135,6 +136,10 @@ EXTERN int uip_parsehttpurl(const char *url, uint16_t *port, EXTERN int uip_listenon(uint16_t portno); EXTERN void uip_server(uint16_t portno, pthread_startroutine_t handler, int stacksize); +EXTERN int uip_getifstatus(const char *ifname, bool *status); +EXTERN int uip_ifup(const char *ifname); +EXTERN int uip_ifdown(const char *ifname); + #undef EXTERN #ifdef __cplusplus } diff --git a/apps/netutils/uiplib/Makefile b/apps/netutils/uiplib/Makefile index 189b32acc2..c1b128c26d 100644 --- a/apps/netutils/uiplib/Makefile +++ b/apps/netutils/uiplib/Makefile @@ -41,7 +41,8 @@ include $(APPDIR)/Make.defs ASRCS = CSRCS = uiplib.c uip_sethostaddr.c uip_gethostaddr.c uip_setdraddr.c \ - uip_setnetmask.c uip_parsehttpurl.c + uip_setnetmask.c uip_parsehttpurl.c uip_setifflag.c \ + uip_getifflag.c # These require TCP support diff --git a/apps/netutils/uiplib/uip_gethostaddr.c b/apps/netutils/uiplib/uip_gethostaddr.c index c5c70b1be5..0b2b5dd28c 100644 --- a/apps/netutils/uiplib/uip_gethostaddr.c +++ b/apps/netutils/uiplib/uip_gethostaddr.c @@ -45,6 +45,7 @@ #include #include +#include #include #include @@ -97,6 +98,7 @@ int uip_gethostaddr(const char *ifname, struct in_addr *addr) memcpy(addr, &req.ifr_addr, sizeof(struct in_addr)); #endif } + close(sockfd); } } return ret; diff --git a/apps/netutils/uiplib/uip_getifflag.c b/apps/netutils/uiplib/uip_getifflag.c new file mode 100644 index 0000000000..8574d331ae --- /dev/null +++ b/apps/netutils/uiplib/uip_getifflag.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * netutils/uiplib/uip_getifflag.c + * + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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 +#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_getifstatus + * + * Description: + * Get the network driver ifup/ifdown status + * + * Parameters: + * ifname The name of the interface to use + * status interface flag ifup or ifdown status + * + * Return: + * 0 on sucess; -1 on failure + * + ****************************************************************************/ + +int uip_getifstatus(const char *ifname, bool *status) +{ + int ret = ERROR; + if (ifname) + { + /* Get a socket (only so that we get access to the INET subsystem) */ + + int sockfd = socket(PF_INET, UIPLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + struct ifreq req; + memset (&req, 0, sizeof(struct ifreq)); + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Perform the ioctl to ifup or ifdown status */ + + ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req); + if (!ret) + { + /* Return the ifup or ifdown status */ + + if ((req.ifr_flags & IF_FLAG_IFUP) == (req.ifr_flags & IF_FLAG_IFDOWN)) + { + ret = ERROR; + } + else if(req.ifr_flags & IF_FLAG_IFUP) + { + *status = true; + } + else + { + *status = false; + } + } + close(sockfd); + } + } + return ret; +} + +#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/apps/netutils/uiplib/uip_getmacaddr.c b/apps/netutils/uiplib/uip_getmacaddr.c index f4f01a7f30..e0f72f4a78 100644 --- a/apps/netutils/uiplib/uip_getmacaddr.c +++ b/apps/netutils/uiplib/uip_getmacaddr.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -96,6 +97,7 @@ int uip_getmacaddr(const char *ifname, uint8_t *macaddr) memcpy(macaddr, &req.ifr_hwaddr.sa_data, IFHWADDRLEN); } + close(sockfd); } } return ret; diff --git a/apps/netutils/uiplib/uip_setifflag.c b/apps/netutils/uiplib/uip_setifflag.c new file mode 100644 index 0000000000..317ddcce15 --- /dev/null +++ b/apps/netutils/uiplib/uip_setifflag.c @@ -0,0 +1,141 @@ +/**************************************************************************** + * netutils/uiplib/uip_setifflag.c + * + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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 +#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * Global Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: uip_ifup + * + * Description: + * Set the network interface UP + * + * Parameters: + * ifname The name of the interface to use + * + * Return: + * 0 on sucess; -1 on failure + * + ****************************************************************************/ + +int uip_ifup(const char *ifname) +{ + int ret = ERROR; + if (ifname) + { + /* Get a socket (only so that we get access to the INET subsystem) */ + + int sockfd = socket(PF_INET, UIPLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + struct ifreq req; + memset (&req, 0, sizeof(struct ifreq)); + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Perform the ioctl to ifup flag */ + req.ifr_flags |= IF_FLAG_IFUP; + + ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req); + close(sockfd); + } + } + return ret; +} + +/**************************************************************************** + * Name: uip_ifdown + * + * Description: + * Set the network interface DOWN + * + * Parameters: + * ifname The name of the interface to use + * + * Return: + * 0 on sucess; -1 on failure + * + ****************************************************************************/ + +int uip_ifdown(const char *ifname) +{ + int ret = ERROR; + if (ifname) + { + /* Get a socket (only so that we get access to the INET subsystem) */ + + int sockfd = socket(PF_INET, UIPLIB_SOCK_IOCTL, 0); + if (sockfd >= 0) + { + struct ifreq req; + memset (&req, 0, sizeof(struct ifreq)); + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Perform the ioctl to ifup flag */ + req.ifr_flags |= IF_FLAG_IFDOWN; + + ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req); + close(sockfd); + } + } + return ret; +} + +#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/apps/netutils/uiplib/uiplib.c b/apps/netutils/uiplib/uiplib.c index f863b7343f..32ffaeb06d 100644 --- a/apps/netutils/uiplib/uiplib.c +++ b/apps/netutils/uiplib/uiplib.c @@ -45,10 +45,12 @@ #include #include +#include #include #include + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -93,3 +95,58 @@ bool uiplib_ipaddrconv(const char *addrstr, uint8_t *ipaddr) } return true; } + +bool uiplib_hwmacconv(const char *hwstr, uint8_t *hw) +{ + unsigned char tmp; + char c; + unsigned char i; + unsigned char j; + + if (strlen(hwstr)!=17) + { + return false; + } + + tmp = 0; + + for (i = 0; i < 6; ++i) + { + j = 0; + do + { + c = *hwstr; + ++j; + if (j > 3) + { + return false; + } + if (c == ':' || c == 0) + { + *hw = tmp; + nvdbg("HWMAC[%d]%0.2X\n",i,tmp); + ++hw; + tmp = 0; + } + else if(c >= '0' && c <= '9') + { + tmp = (tmp << 4) + (c - '0'); + } + else if(c >= 'a' && c <= 'f') + { + tmp = (tmp << 4) + (c - 'a' + 10); + } + else if(c >= 'A' && c <= 'F') + { + tmp = (tmp << 4) + (c - 'A' + 10); + } + else + { + return false; + } + ++hwstr; + } + while(c != ':' && c != 0); + } + return true; +} diff --git a/apps/netutils/webclient/webclient.c b/apps/netutils/webclient/webclient.c index 4dbd130ec5..2604ce020c 100644 --- a/apps/netutils/webclient/webclient.c +++ b/apps/netutils/webclient/webclient.c @@ -56,6 +56,7 @@ #include #include +#include #include #include #include diff --git a/apps/nshlib/README.txt b/apps/nshlib/README.txt index 9f371678ee..227f01c0a0 100644 --- a/apps/nshlib/README.txt +++ b/apps/nshlib/README.txt @@ -385,7 +385,7 @@ o help [-v] [] Show full command usage only for this command -o ifconfig +o ifconfig [nic_name [ip]] [dr|gw|gateway ] [netmask ] [dns ] [hw ] Show the current configuration of the network, for example: @@ -396,6 +396,22 @@ o ifconfig if uIP statistics are enabled (CONFIG_NET_STATISTICS), then this command will also show the detailed state of uIP. +o ifdown + + Take down the interface identified by the name . + + Example: + + ifdown eth0 + +o ifup + + Bring up down the interface identified by the name . + + Example: + + ifup eth0 + o kill - Send the to the task identified by . @@ -850,6 +866,8 @@ Command Dependencies on Configuration Settings get CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NET_BUFSIZE >= 558 (see note 1) help -- ifconfig CONFIG_NET + ifdown CONFIG_NET + ifup CONFIG_NET kill !CONFIG_DISABLE_SIGNALS losetup !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 ls CONFIG_NFILE_DESCRIPTORS > 0 @@ -899,17 +917,18 @@ also allow it to squeeze into very small memory footprints. CONFIG_NSH_DISABLE_CD, CONFIG_NSH_DISABLE_CP, CONFIG_NSH_DISABLE_DD, CONFIG_NSH_DISABLE_DF, CONFIG_NSH_DISABLE_ECHO, CONFIG_NSH_DISABLE_EXEC, CONFIG_NSH_DISABLE_EXIT, CONFIG_NSH_DISABLE_FREE, CONFIG_NSH_DISABLE_GET, - CONFIG_NSH_DISABLE_HELP, CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_KILL, - CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS, CONFIG_NSH_DISABLE_MD5 - CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, CONFIG_NSH_DISABLE_MKFATFS, - CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, CONFIG_NSH_DISABLE_MH, - CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, CONFIG_NSH_DISABLE_MV, - CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING, - CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_RM, - CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, - CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT, - CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, - CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD + CONFIG_NSH_DISABLE_HELP, CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_IFUPDOWN, + CONFIG_NSH_DISABLE_KILL, CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS, + CONFIG_NSH_DISABLE_MD5 CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, + CONFIG_NSH_DISABLE_MKFATFS, CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, + CONFIG_NSH_DISABLE_MH, CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, + CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_PS, + CONFIG_NSH_DISABLE_PING, CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, + CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, + CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, + CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, + CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, + CONFIG_NSH_DISABLE_XD Verbose help output can be suppressed by defining CONFIG_NSH_HELP_TERSE. In that case, the help command is still available but will be slightly smaller. diff --git a/apps/nshlib/nsh.h b/apps/nshlib/nsh.h index 9f36b1d1f0..ac75cf2e1d 100644 --- a/apps/nshlib/nsh.h +++ b/apps/nshlib/nsh.h @@ -267,9 +267,35 @@ # undef CONFIG_NSH_ROMFSSECTSIZE #endif -/* This is the maximum number of arguments that will be accepted for a command */ +/* This is the maximum number of arguments that will be accepted for a + * command. Here we attempt to select the smallest number possible depending + * upon the of commands that are available. Most commands use six or fewer + * arguments, but there are a few that require more. + * + * This value is also configurable with CONFIG_NSH_MAXARGUMENTS. This + * configurability is necessary since there may also be external, "built-in" + * commands that require more commands than NSH is aware of. + */ -#define NSH_MAX_ARGUMENTS 6 +#ifndef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 6 +#endif + +#if CONFIG_NSH_MAXARGUMENTS < 11 +# if defined(CONFIG_NET) && !defined(CONFIG_NSH_DISABLE_IFCONFIG) +# undef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 11 +# endif +#endif + +#if CONFIG_NSH_MAXARGUMENTS < 7 +# if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0 +# if !defined(CONFIG_NSH_DISABLE_GET) || !defined(CONFIG_NSH_DISABLE_PUT) +# undef CONFIG_NSH_MAXARGUMENTS +# define CONFIG_NSH_MAXARGUMENTS 7 +# endif +# endif +#endif /* strerror() produces much nicer output but is, however, quite large and * will only be used if CONFIG_NSH_STRERROR is defined. Note that the strerror @@ -602,6 +628,10 @@ void nsh_usbtrace(void); # ifndef CONFIG_NSH_DISABLE_IFCONFIG int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif +# ifndef CONFIG_NSH_DISABLE_IFUPDOWN + int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +# endif #if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_GET int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); diff --git a/apps/nshlib/nsh_netcmds.c b/apps/nshlib/nsh_netcmds.c index f3457a809d..d28fe873d0 100644 --- a/apps/nshlib/nsh_netcmds.c +++ b/apps/nshlib/nsh_netcmds.c @@ -277,14 +277,34 @@ int ifconfig_callback(FAR struct uip_driver_s *dev, void *arg) { struct nsh_vtbl_s *vtbl = (struct nsh_vtbl_s*)arg; struct in_addr addr; + bool is_running = false; + int ret; + + ret = uip_getifstatus(dev->d_ifname,&is_running); + if (ret != OK) + { + nsh_output(vtbl, "\tGet %s interface flags error: %d\n", + dev->d_ifname, ret); + } + + nsh_output(vtbl, "%s\tHWaddr %s at %s\n", + dev->d_ifname, ether_ntoa(&dev->d_mac), (is_running)?"UP":"DOWN"); - nsh_output(vtbl, "%s\tHWaddr %s\n", dev->d_ifname, ether_ntoa(&dev->d_mac)); addr.s_addr = dev->d_ipaddr; nsh_output(vtbl, "\tIPaddr:%s ", inet_ntoa(addr)); + addr.s_addr = dev->d_draddr; nsh_output(vtbl, "DRaddr:%s ", inet_ntoa(addr)); + addr.s_addr = dev->d_netmask; - nsh_output(vtbl, "Mask:%s\n\n", inet_ntoa(addr)); + nsh_output(vtbl, "Mask:%s\n", inet_ntoa(addr)); + +#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS) + resolv_getserver(&addr); + nsh_output(vtbl, "\tDNSaddr:%s\n", inet_ntoa(addr)); +#endif + + nsh_output(vtbl, "\n"); return OK; } @@ -483,6 +503,54 @@ int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #endif #endif +/**************************************************************************** + * Name: cmd_ifup + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_IFUPDOWN +int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR char *intf = NULL; + int ret; + + if (argc != 2) + { + nsh_output(vtbl, "Please select nic_name:\n"); + netdev_foreach(ifconfig_callback, vtbl); + return OK; + } + + intf = argv[1]; + ret = uip_ifup(intf); + nsh_output(vtbl, "ifup %s...%s\n", intf, (ret == OK) ? "OK" : "Failed"); + return ret; +} +#endif + +/**************************************************************************** + * Name: cmd_ifdown + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_IFUPDOWN +int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + FAR char *intf = NULL; + int ret; + + if (argc != 2) + { + nsh_output(vtbl, "Please select nic_name:\n"); + netdev_foreach(ifconfig_callback, vtbl); + return OK; + } + + intf = argv[1]; + ret = uip_ifdown(intf); + nsh_output(vtbl, "ifdown %s...%s\n", intf, (ret == OK) ? "OK" : "Failed"); + return ret; +} +#endif + /**************************************************************************** * Name: cmd_ifconfig ****************************************************************************/ @@ -491,7 +559,17 @@ int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { struct in_addr addr; - in_addr_t ip; + in_addr_t gip; + int i; + FAR char *intf = NULL; + FAR char *hostip = NULL; + FAR char *gwip = NULL; + FAR char *mask = NULL; + FAR char *tmp = NULL; + FAR char *hw = NULL; + FAR char *dns = NULL; + bool badarg=false; + uint8_t mac[6]; /* With one or no arguments, ifconfig simply shows the status of ethernet * device: @@ -513,24 +591,142 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) * ifconfig nic_name ip_address */ + if (argc > 2) + { + for(i = 0; i < argc; i++) + { + if (i == 1) + { + intf = argv[i]; + } + else if (i == 2) + { + hostip = argv[i]; + } + else + { + tmp = argv[i]; + if (!strcmp(tmp, "dr") || !strcmp(tmp, "gw") || !strcmp(tmp, "gateway")) + { + if (argc-1 >= i+1) + { + gwip = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "netmask")) + { + if (argc-1 >= i+1) + { + mask = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "hw")) + { + if (argc-1>=i+1) + { + hw = argv[i+1]; + i++; + badarg = !uiplib_hwmacconv(hw, mac); + } + else + { + badarg = true; + } + } + else if(!strcmp(tmp, "dns")) + { + if (argc-1 >= i+1) + { + dns = argv[i+1]; + i++; + } + else + { + badarg = true; + } + } + } + } + } + + if (badarg) + { + nsh_output(vtbl, g_fmtargrequired, argv[0]); + return ERROR; + } + + /* Set Hardware ethernet MAC addr */ + + if (hw) + { + ndbg("HW MAC: %s\n", hw); + uip_setmacaddr(intf, mac); + } + /* Set host ip address */ - ip = addr.s_addr = inet_addr(argv[2]); - uip_sethostaddr(argv[1], &addr); + ndbg("Host IP: %s\n", hostip); + gip = addr.s_addr = inet_addr(hostip); + uip_sethostaddr(intf, &addr); /* Set gateway */ - ip = NTOHL(ip); - ip &= ~0x000000ff; - ip |= 0x00000001; + if (gwip) + { + ndbg("Gateway: %s\n", gwip); + gip = addr.s_addr = inet_addr(gwip); + } + else + { + ndbg("Gateway: default\n"); + gip = NTOHL(gip); + gip &= ~0x000000ff; + gip |= 0x00000001; + gip = HTONL(gip); + addr.s_addr = gip; + } - addr.s_addr = HTONL(ip); - uip_setdraddr(argv[1], &addr); + uip_setdraddr(intf, &addr); - /* Set netmask */ + /* Set network mask */ - addr.s_addr = inet_addr("255.255.255.0"); - uip_setnetmask(argv[1], &addr); + if (mask) + { + ndbg("Netmask: %s\n",mask); + addr.s_addr = inet_addr(mask); + } + else + { + ndbg("Netmask: Default\n"); + addr.s_addr = inet_addr("255.255.255.0"); + } + + uip_setnetmask(intf, &addr); + +#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS) + if (dns) + { + ndbg("DNS: %s\n", dns); + addr.s_addr = inet_addr(dns); + } + else + { + ndbg("DNS: Default\n"); + addr.s_addr = gip; + } + + resolv_conf(&addr); +#endif return OK; } diff --git a/apps/nshlib/nsh_parse.c b/apps/nshlib/nsh_parse.c index f9642809fc..bf2b8a4a47 100644 --- a/apps/nshlib/nsh_parse.c +++ b/apps/nshlib/nsh_parse.c @@ -73,19 +73,19 @@ /* Argument list size * * argv[0]: The command name. - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc-3]: Possibly '>' or '>>' * argv[argc-2]: Possibly * argv[argc-1]: Possibly '&' (if pthreads are enabled) * argv[argc]: NULL terminating pointer * - * Maximum size is NSH_MAX_ARGUMENTS+5 + * Maximum size is CONFIG_NSH_MAXARGUMENTS+5 */ #ifndef CONFIG_NSH_DISABLEBG -# define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+5) +# define MAX_ARGV_ENTRIES (CONFIG_NSH_MAXARGUMENTS+5) #else -# define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+4) +# define MAX_ARGV_ENTRIES (CONFIG_NSH_MAXARGUMENTS+4) #endif /* Help command summary layout */ @@ -146,7 +146,7 @@ static const char g_failure[] = "1"; static const struct cmdmap_s g_cmdmap[] = { #if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_TEST) - { "[", cmd_lbracket, 4, NSH_MAX_ARGUMENTS, " ]" }, + { "[", cmd_lbracket, 4, CONFIG_NSH_MAXARGUMENTS, " ]" }, #endif #ifndef CONFIG_NSH_DISABLE_HELP @@ -164,7 +164,7 @@ static const struct cmdmap_s g_cmdmap[] = #if CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_CAT - { "cat", cmd_cat, 2, NSH_MAX_ARGUMENTS, " [ [ ...]]" }, + { "cat", cmd_cat, 2, CONFIG_NSH_MAXARGUMENTS, " [ [ ...]]" }, # endif #ifndef CONFIG_DISABLE_ENVIRON # ifndef CONFIG_NSH_DISABLE_CD @@ -196,9 +196,9 @@ static const struct cmdmap_s g_cmdmap[] = #ifndef CONFIG_NSH_DISABLE_ECHO # ifndef CONFIG_DISABLE_ENVIRON - { "echo", cmd_echo, 0, NSH_MAX_ARGUMENTS, "[ [...]]" }, + { "echo", cmd_echo, 0, CONFIG_NSH_MAXARGUMENTS, "[ [...]]" }, # else - { "echo", cmd_echo, 0, NSH_MAX_ARGUMENTS, "[ [...]]" }, + { "echo", cmd_echo, 0, CONFIG_NSH_MAXARGUMENTS, "[ [...]]" }, # endif #endif @@ -229,7 +229,11 @@ static const struct cmdmap_s g_cmdmap[] = #ifdef CONFIG_NET # ifndef CONFIG_NSH_DISABLE_IFCONFIG - { "ifconfig", cmd_ifconfig, 1, 3, "[nic_name [ip]]" }, + { "ifconfig", cmd_ifconfig, 1, 11, "[nic_name [ip]] [dr|gw|gateway ] [netmask ] [dns ] [hw ]" }, +# endif +# ifndef CONFIG_NSH_DISABLE_IFUPDOWN + { "ifdown", cmd_ifdown, 2, 2, "" }, + { "ifup", cmd_ifup, 2, 2, "" }, # endif #endif @@ -363,7 +367,7 @@ static const struct cmdmap_s g_cmdmap[] = #endif #if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_TEST) - { "test", cmd_test, 3, NSH_MAX_ARGUMENTS, "" }, + { "test", cmd_test, 3, CONFIG_NSH_MAXARGUMENTS, "" }, #endif #if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_READABLE) @@ -736,7 +740,7 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, int argc, char *argv[]) * * argv[0]: The command name. This is argv[0] when the arguments * are, finally, received by the command vtblr - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc]: NULL terminating pointer */ @@ -1343,13 +1347,13 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline) * of argv is: * * argv[0]: The command name. - * argv[1]: The beginning of argument (up to NSH_MAX_ARGUMENTS) + * argv[1]: The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS) * argv[argc-3]: Possibly '>' or '>>' * argv[argc-2]: Possibly * argv[argc-1]: Possibly '&' * argv[argc]: NULL terminating pointer * - * Maximum size is NSH_MAX_ARGUMENTS+5 + * Maximum size is CONFIG_NSH_MAXARGUMENTS+5 */ argv[0] = cmd; @@ -1423,7 +1427,7 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline) /* Check if the maximum number of arguments was exceeded */ - if (argc > NSH_MAX_ARGUMENTS) + if (argc > CONFIG_NSH_MAXARGUMENTS) { nsh_output(vtbl, g_fmttoomanyargs, cmd); } diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 3b60fc8128..f4bdc7ed01 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -3564,4 +3564,6 @@ * RGMP 4.0 updated from Qiany Yu. * configs/*/Make.defs and configs/*/ld.script: Massive clean-up and standardization of linker scripts from Freddie Chopin. + * net/netdev_ioctl.c: Add interface state flags and ioctl calls + to bring network interfaces up and down (from Darcy Gong). diff --git a/nuttx/Documentation/NuttShell.html b/nuttx/Documentation/NuttShell.html index f8ef41fb96..31546a100c 100644 --- a/nuttx/Documentation/NuttShell.html +++ b/nuttx/Documentation/NuttShell.html @@ -8,7 +8,7 @@

NuttShell (NSH)

-

Last Updated: October 31, 2012

+

Last Updated: November 4, 2012

@@ -173,175 +173,187 @@
- 2.16 Show Network Configuration (ifconfig) + 2.16 Manage Network Configuration (ifconfig)
- 2.17 Send a signal to a task (kill) + 2.17 Take a network down (ifdown)
- 2.18 Setup/teardown the Loop Device (losetup) + 2.18 Bring a network up (ifup)
- 2.19 List Directory Contents (ls) + 2.19 Send a signal to a task (kill)
- 2.20 Calculate MD5 (md5) + 2.20 Setup/teardown the Loop Device (losetup)
- 2.21 Access Memory (mb, mh, and mw) + 2.21 List Directory Contents (ls)
- 2.22 Show Current Tasks and Threads (ps) + 2.22 Calculate MD5 (md5)
- 2.23 Create a Directory (mkdir) + 2.23 Access Memory (mb, mh, and mw)
- 2.24 Create a FAT Filesystem (mkfatfs) + 2.24 Show Current Tasks and Threads (ps)
- 2.25 Create a FIFO (mkfifo) + 2.25 Create a Directory (mkdir)
- 2.26 Create a RAMDISK (mkrd) + 2.26 Create a FAT Filesystem (mkfatfs)
- 2.27 Mount a File System (mount) + 2.27 Create a FIFO (mkfifo)
- 2.28 Rename a File (mv) + 2.28 Create a RAMDISK (mkrd)
- 2.29 Mount an NFS file system (nfsmount) + 2.29 Mount a File System (mount)
- 2.30 Check Network Peer (ping) + 2.30 Rename a File (mv)
- 2.31 Send File Via TFTP (put) + 2.31 Mount an NFS file system (nfsmount)
- 2.32 Show Current Working Directory (pwd) + 2.32 Check Network Peer (ping)
- 2.33 Remove a File (rm) + 2.33 Send File Via TFTP (put)
- 2.34 Remove a Directory (rmdir) + 2.34 Show Current Working Directory (pwd)
- 2.35 Set an Environment Variable (set) + 2.35 Remove a File (rm)
- 2.36 Execute an NSH Script (sh) + 2.36 Remove a Directory (rmdir)
- 2.37 Wait for Seconds (sleep) + 2.37 Set an Environment Variable (set)
- 2.38 Unmount a File System (umount) + 2.38 Execute an NSH Script (sh)
- 2.39 Unset an Environment Variable (unset) + 2.39 Wait for Seconds (sleep)
- 2.40 URL Decode (urldecode) + 2.40 Unmount a File System (umount)
- 2.41 URL Encode (urlencode) + 2.41 Unset an Environment Variable (unset)
- 2.42 Wait for Microseconds (usleep) + 2.42 URL Decode (urldecode)
- 2.43 Get File Via HTTP (wget) + 2.43 URL Encode (urlencode)
- 2.44 Hexadecimal Dump (xd) + 2.44 Wait for Microseconds (usleep) + + + +
+ + 2.45 Get File Via HTTP (wget) + + + +
+ + 2.46 Hexadecimal Dump (xd) @@ -1170,18 +1182,18 @@ help [-v] [<cmd>]
-

2.16 Show Network Configuration (ifconfig)

+

2.16 Manage Network Configuration (ifconfig)

Command Syntax:

    -ifconfig [nic_name [ip_address]]
    +ifconfig [nic_name [ip]] [dr|gw|gateway <dr-address>] [netmask <net-mask>] [dns <dns-address>] [hw <hw-mac>]]
     

Synopsis. - Two forms of the ifconfigcommand are supported: + Multiple forms of the ifconfigcommand are supported:

  1. @@ -1214,14 +1226,63 @@ eth0 HWaddr 00:18:11:80:10:06

       ifconfig nic_name ip_address
      -
        +
      + +
    • + Other forms to be provided
+ +
-

2.17 Send a signal to a task (kill)

+

2.17 Take a network down (ifdown)

+
+ +

Command Syntax:

+
    +ifdown <nic-name>
    +
+

+ Synopsis. + Take down the interface identified by the name <nic-name>. +

+

+ Example: +

+
    +ifdown eth0
    +
+ + + + + +
+

2.18 Bring a network up (ifup)

+
+ +

Command Syntax:

+
    +ifup <nic-name>
    +
+

+ Synopsis. + Bring up down the interface identified by the name <nic-name>. +

+

+ Example: +

+
    +ifup eth0
    +
+ + + +
+

2.19 Send a signal to a task (kill)

@@ -1262,7 +1323,7 @@ nsh>
-

2.18 Setup/teardown the Loop Device (losetup)

+

2.20 Setup/teardown the Loop Device (losetup)

@@ -1315,7 +1376,7 @@ losetup d <dev-path>
-

2.19 List Directory Contents (ls)

+

2.21 List Directory Contents (ls)

@@ -1352,7 +1413,7 @@ ls [-lRs] <dir-path>
-

2.20 Calculate MD5 (md5)

+

2.22 Calculate MD5 (md5)

@@ -1369,7 +1430,7 @@ md5 [-f] <string or filepath>
-

2.21 Access Memory (mb, mh, and mw)

+

2.23 Access Memory (mb, mh, and mw)

@@ -1423,7 +1484,7 @@ nsh>
-

2.22 Show Current Tasks and Threads (ps)

+

2.24 Show Current Tasks and Threads (ps)

@@ -1449,7 +1510,7 @@ nsh>
-

2.23 Create a Directory (mkdir)

+

2.25 Create a Directory (mkdir)

@@ -1484,7 +1545,7 @@ nsh>
-

2.24 Create a FAT Filesystem (mkfatfs)

+

2.26 Create a FAT Filesystem (mkfatfs)

@@ -1504,7 +1565,7 @@ mkfatfs <path>
-

2.25 Create a FIFO (mkfifo)

+

2.27 Create a FIFO (mkfifo)

@@ -1542,7 +1603,7 @@ nsh>
-

2.26 Create a RAMDISK (mkrd)

+

2.28 Create a RAMDISK (mkrd)

@@ -1593,7 +1654,7 @@ nsh>
-

2.27 Mount a File System (mount)

+

2.29 Mount a File System (mount)

@@ -1672,7 +1733,7 @@ nsh> mount
-

2.28 Rename a File (mv)

+

2.30 Rename a File (mv)

@@ -1690,7 +1751,7 @@ mv <old-path> <new-path>
-

2.29 Mount an NFS file system (nfsmount)

+

2.31 Mount an NFS file system (nfsmount)

@@ -1709,7 +1770,7 @@ nfsmount <server-address> <mount-point> <remote-path>
-

2.30 Check Network Peer (ping)

+

2.32 Check Network Peer (ping)

@@ -1742,7 +1803,7 @@ nsh>
-

2.31 Send File Via TFTP (put)

+

2.33 Send File Via TFTP (put)

@@ -1777,7 +1838,7 @@ put [-b|-n] [-f <remote-path>] -h <ip-address> <local-path>
-

2.32 Show Current Working Directory (pwd)

+

2.34 Show Current Working Directory (pwd)

@@ -1807,7 +1868,7 @@ nsh>
-

2.33 Remove a File (rm)

+

2.35 Remove a File (rm)

@@ -1841,7 +1902,7 @@ nsh>
-

2.34 Remove a Directory (rmdir)

+

2.36 Remove a Directory (rmdir)

@@ -1876,7 +1937,7 @@ nsh>
-

2.35 Set an Environment Variable (set)

+

2.37 Set an Environment Variable (set)

@@ -1902,7 +1963,7 @@ nsh>
-

2.36 Execute an NSH Script (sh)

+

2.38 Execute an NSH Script (sh)

@@ -1920,7 +1981,7 @@ sh <script-path>
-

2.37 Wait for Seconds (sleep)

+

2.39 Wait for Seconds (sleep)

@@ -1937,7 +1998,7 @@ sleep <sec>
-

2.38 Unmount a File System (umount)

+

2.40 Unmount a File System (umount)

@@ -1967,7 +2028,7 @@ nsh>
-

2.39 Unset an Environment Variable (unset)

+

2.41 Unset an Environment Variable (unset)

@@ -1993,7 +2054,7 @@ nsh>
-

2.40 URL Decode (urldecode)

+

2.42 URL Decode (urldecode)

@@ -2010,7 +2071,7 @@ urldecode [-f] <string or filepath>
-

2.41 URL Encode (urlencode)

+

2.43 URL Encode (urlencode)

@@ -2027,7 +2088,7 @@ urlencode [-f] <string or filepath>
-

2.42 Wait for Microseconds (usleep)

+

2.44 Wait for Microseconds (usleep)

@@ -2044,7 +2105,7 @@ usleep <usec>
- 2.43 Get File Via HTTP (wget) + 2.45 Get File Via HTTP (wget)
@@ -2071,7 +2132,7 @@ wget [-o <local-path>] <url>
-

2.44 Hexadecimal dump (xd)

+

2.46 Hexadecimal dump (xd)

@@ -2215,6 +2276,16 @@ nsh> CONFIG_NET CONFIG_NSH_DISABLE_IFCONFIG + + ifdown + CONFIG_NET + CONFIG_NSH_DISABLE_IFUPDOWN + + + ifup + CONFIG_NET + CONFIG_NSH_DISABLE_IFUPDOWN + kill !CONFIG_DISABLE_SIGNALS @@ -3723,9 +3794,9 @@ mount -t vfat /dev/ram1 /tmp
  • /etc/init.d/rcS
  • exec
  • exec_namedapp()
  • +
  • exit
  • -
  • exit
  • free
  • g_cmdmap
  • genromfs
  • @@ -3734,6 +3805,8 @@ mount -t vfat /dev/ram1 /tmp
  • help
  • if-then[-else]-fi
  • ifconfig
  • +
  • ifdown
  • +
  • ifup
  • Initialization sequence
  • kill
  • losetup
  • diff --git a/nuttx/include/net/if.h b/nuttx/include/net/if.h index e64b58563f..1ff8ebc383 100644 --- a/nuttx/include/net/if.h +++ b/nuttx/include/net/if.h @@ -52,6 +52,10 @@ #define IF_NAMESIZE 6 /* Newer naming standard */ #define IFHWADDRLEN 6 +#define IFF_RUNNING (1 << 0) +#define IF_FLAG_IFUP (1 << 0) +#define IF_FLAG_IFDOWN (2 << 0) + /******************************************************************************************* * Public Type Definitions *******************************************************************************************/ @@ -72,6 +76,7 @@ struct lifreq struct sockaddr lifru_hwaddr; /* MAC address */ int lifru_count; /* Number of devices */ int lifru_mtu; /* MTU size */ + uint8_t lifru_flags; /* Interface flags */ } lifr_ifru; }; @@ -82,6 +87,7 @@ struct lifreq #define lifr_hwaddr lifr_ifru.lifru_hwaddr /* MAC address */ #define lifr_mtu lifr_ifru.lifru_mtu /* MTU */ #define lifr_count lifr_ifru.lifru_count /* Number of devices */ +#define lifr_flags lifr_ifru.lifru_flags /* interface flags */ /* This is the older I/F request that should only be used with IPv4. However, since * NuttX only supports IPv4 or 6 (not both), we can force the older structure to @@ -101,6 +107,7 @@ struct ifreq struct sockaddr ifru_hwaddr; /* MAC address */ int ifru_count; /* Number of devices */ int ifru_mtu; /* MTU size */ + uint8_t ifru_flags; /* Interface flags */ } ifr_ifru; }; @@ -111,6 +118,7 @@ struct ifreq #define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */ #define ifr_mtu ifr_ifru.ifru_mtu /* MTU */ #define ifr_count ifr_ifru.ifru_count /* Number of devices */ +#define ifr_flags ifr_ifru.ifru_flags /* interface flags */ #else /* CONFIG_NET_IPv6 */ @@ -123,6 +131,7 @@ struct ifreq #define ifr_hwaddr lifr_ifru.lifru_hwaddr /* MAC address */ #define ifr_mtu lifr_ifru.lifru_mtu /* MTU */ #define ifr_count lifr_ifru.lifru_count /* Number of devices */ +#define ifr_flags lifr_ifru.lifru_flags /* interface flags */ #endif /* CONFIG_NET_IPv6 */ diff --git a/nuttx/include/nuttx/net/ioctl.h b/nuttx/include/nuttx/net/ioctl.h index be3f597f47..d5d1a001c8 100644 --- a/nuttx/include/nuttx/net/ioctl.h +++ b/nuttx/include/nuttx/net/ioctl.h @@ -146,9 +146,15 @@ #define SIOCSIWPMKSA _SIOC(0x0036) /* PMKSA cache operation */ +/* Interface flags */ + +#define SIOCSIFFLAGS _SIOC(0x0037) /* Sets the interface flags */ +#define SIOCGIFFLAGS _SIOC(0x0038) /* Gets the interface flags */ + /**************************************************************************** * Type Definitions ****************************************************************************/ + /* See include/net/if.h */ /**************************************************************************** diff --git a/nuttx/include/nuttx/net/uip/uip-arch.h b/nuttx/include/nuttx/net/uip/uip-arch.h index 9546de04eb..73805c6fb1 100644 --- a/nuttx/include/nuttx/net/uip/uip-arch.h +++ b/nuttx/include/nuttx/net/uip/uip-arch.h @@ -90,6 +90,10 @@ struct uip_driver_s char d_ifname[IFNAMSIZ]; #endif + /* Drivers interface flags. See IFF_* definitions in include/net/if.h */ + + uint8_t d_flags; + /* Ethernet device identity */ #ifdef CONFIG_NET_ETHERNET diff --git a/nuttx/net/netdev_ioctl.c b/nuttx/net/netdev_ioctl.c index 4b5876efa0..ea5c0e4362 100644 --- a/nuttx/net/netdev_ioctl.c +++ b/nuttx/net/netdev_ioctl.c @@ -138,19 +138,47 @@ static void ioctl_setipaddr(FAR uip_ipaddr_t *outaddr, FAR const void *inaddr) * ****************************************************************************/ -static inline void ioctl_ifup(FAR struct uip_driver_s *dev) +static void ioctl_ifup(FAR struct uip_driver_s *dev) { + /* Make sure that the device supports the d_ifup() method */ + if (dev->d_ifup) { - dev->d_ifup(dev); + /* Is the interface already up? */ + + if ((dev->d_flags & IFF_RUNNING) == 0) + { + /* No, bring the interface up now */ + + if (dev->d_ifup(dev) == OK) + { + /* Mark the interface as up */ + + dev->d_flags |= IFF_RUNNING; + } + } } } -static inline void ioctl_ifdown(FAR struct uip_driver_s *dev) +static void ioctl_ifdown(FAR struct uip_driver_s *dev) { + /* Make sure that the device supports the d_ifdown() method */ + if (dev->d_ifdown) { - dev->d_ifdown(dev); + /* Is the interface already down? */ + + if ((dev->d_flags & IFF_RUNNING) != 0) + { + /* No, take the interface down now */ + + if (dev->d_ifdown(dev) == OK) + { + /* Mark the interface as down */ + + dev->d_flags &= ~IFF_RUNNING; + } + } } } @@ -194,63 +222,130 @@ static int netdev_ifrioctl(FAR struct socket *psock, int cmd, struct ifreq *req) switch (cmd) { - case SIOCGIFADDR: /* Get IP address */ - ioctl_getipaddr(&req->ifr_addr, &dev->d_ipaddr); + case SIOCGIFADDR: /* Get IP address */ + { + ioctl_getipaddr(&req->ifr_addr, &dev->d_ipaddr); + } break; - case SIOCSIFADDR: /* Set IP address */ - ioctl_ifdown(dev); - ioctl_setipaddr(&dev->d_ipaddr, &req->ifr_addr); - ioctl_ifup(dev); + case SIOCSIFADDR: /* Set IP address */ + { + ioctl_ifdown(dev); + ioctl_setipaddr(&dev->d_ipaddr, &req->ifr_addr); + ioctl_ifup(dev); + } break; case SIOCGIFDSTADDR: /* Get P-to-P address */ - ioctl_getipaddr(&req->ifr_dstaddr, &dev->d_draddr); + { + ioctl_getipaddr(&req->ifr_dstaddr, &dev->d_draddr); + } break; case SIOCSIFDSTADDR: /* Set P-to-P address */ - ioctl_setipaddr(&dev->d_draddr, &req->ifr_dstaddr); + { + ioctl_setipaddr(&dev->d_draddr, &req->ifr_dstaddr); + } break; case SIOCGIFNETMASK: /* Get network mask */ - ioctl_getipaddr(&req->ifr_addr, &dev->d_netmask); + { + ioctl_getipaddr(&req->ifr_addr, &dev->d_netmask); + } break; case SIOCSIFNETMASK: /* Set network mask */ - ioctl_setipaddr(&dev->d_netmask, &req->ifr_addr); + { + ioctl_setipaddr(&dev->d_netmask, &req->ifr_addr); + } break; case SIOCGIFMTU: /* Get MTU size */ - req->ifr_mtu = CONFIG_NET_BUFSIZE; + { + req->ifr_mtu = CONFIG_NET_BUFSIZE; + } + break; + + case SIOCSIFFLAGS: /* Sets the interface flags */ + { + /* Is this a request to bring the interface up? */ + + if (req->ifr_flags & IF_FLAG_IFUP) + { + /* Yes.. bring the interface up */ + + ioctl_ifup(dev); + } + + /* Is this a request to take the interface down? */ + + else if (req->ifr_flags & IF_FLAG_IFDOWN) + { + /* Yes.. take the interface down */ + + ioctl_ifdown(dev); + } + } + break; + + case SIOCGIFFLAGS: /* Gets the interface flags */ + { + req->ifr_flags = 0; + + /* Is the interface running? */ + + if (dev->d_flags & IFF_RUNNING) + { + /* Yes.. report interface up */ + + req->ifr_flags |= IF_FLAG_IFUP; + } + else + { + /* No.. report interface down */ + + req->ifr_flags |= IF_FLAG_IFDOWN; + } + } break; /* MAC address operations only make sense if Ethernet is supported */ #ifdef CONFIG_NET_ETHERNET case SIOCGIFHWADDR: /* Get hardware address */ - req->ifr_hwaddr.sa_family = AF_INETX; - memcpy(req->ifr_hwaddr.sa_data, dev->d_mac.ether_addr_octet, IFHWADDRLEN); + { + req->ifr_hwaddr.sa_family = AF_INETX; + memcpy(req->ifr_hwaddr.sa_data, dev->d_mac.ether_addr_octet, IFHWADDRLEN); + } break; case SIOCSIFHWADDR: /* Set hardware address -- will not take effect until ifup */ - req->ifr_hwaddr.sa_family = AF_INETX; - memcpy(dev->d_mac.ether_addr_octet, req->ifr_hwaddr.sa_data, IFHWADDRLEN); + { + req->ifr_hwaddr.sa_family = AF_INETX; + memcpy(dev->d_mac.ether_addr_octet, req->ifr_hwaddr.sa_data, IFHWADDRLEN); + } break; #endif case SIOCDIFADDR: /* Delete IP address */ - ioctl_ifdown(dev); - memset(&dev->d_ipaddr, 0, sizeof(uip_ipaddr_t)); + { + ioctl_ifdown(dev); + memset(&dev->d_ipaddr, 0, sizeof(uip_ipaddr_t)); + } break; case SIOCGIFCOUNT: /* Get number of devices */ - req->ifr_count = netdev_count(); - ret = -ENOSYS; + { + req->ifr_count = netdev_count(); + ret = -ENOSYS; + } break; - case SIOCGIFBRDADDR: /* Get broadcast IP address */ - case SIOCSIFBRDADDR: /* Set broadcast IP address */ - ret = -ENOSYS; + case SIOCGIFBRDADDR: /* Get broadcast IP address */ + case SIOCSIFBRDADDR: /* Set broadcast IP address */ + { + ret = -ENOSYS; + } break; #ifdef CONFIG_NET_ARPIOCTLS @@ -261,7 +356,9 @@ static int netdev_ifrioctl(FAR struct socket *psock, int cmd, struct ifreq *req) #endif default: - ret = -EINVAL; + { + ret = -EINVAL; + } break;; }