diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index 3a3681e19..9e5608c88 100644 --- a/include/wireless/wapi.h +++ b/include/wireless/wapi.h @@ -71,14 +71,26 @@ #define WAPI_PROC_LINE_SIZE 1024 /* Select options to successfully open a socket in this nework configuration. */ +/* The address family that we used to create the socket really does not + * matter. It should, however, be valid in the current configuration. + */ -#ifdef CONFIG_NET_UDP -# define NETLIB_SOCK_IOCTL SOCK_DGRAM -#else -# define NETLIB_SOCK_IOCTL SOCK_STREAM +#if defined(CONFIG_NET_IPv4) +# define PF_INETX PF_INET +#elif defined(CONFIG_NET_IPv6) +# define PF_INETX PF_INET6 #endif -#define PF_INETX PF_INET +/* SOCK_DGRAM is the preferred socket type to use when we just want a + * socket for performing driver ioctls. However, we can't use SOCK_DRAM + * if UDP is disabled. + */ + +#ifdef CONFIG_NET_UDP +# define SOCK_WAPI SOCK_DGRAM +#else +# define SOCK_WAPI SOCK_STREAM +#endif /**************************************************************************** * Public Types @@ -122,26 +134,6 @@ enum wapi_mode_e WAPI_MODE_MESH = IW_MODE_MESH /* Mesh (IEEE 802.11s) network */ }; -/* WPA **********************************************************************/ - -enum wpa_alg_e -{ - WPA_ALG_NONE, - WPA_ALG_WEP, - WPA_ALG_TKIP, - WPA_ALG_CCMP, - WPA_ALG_IGTK, - WPA_ALG_PMK, - WPA_ALG_GCMP, - WPA_ALG_SMS4, - WPA_ALG_KRK, - WPA_ALG_GCMP_256, - WPA_ALG_CCMP_256, - WPA_ALG_BIP_GMAC_128, - WPA_ALG_BIP_GMAC_256, - WPA_ALG_BIP_CMAC_256 -}; - /* Bitrate flags. * * At the moment, unicast (IW_BITRATE_UNICAST) and broadcast @@ -221,6 +213,47 @@ struct wapi_list_s } head; }; +/* WPA **********************************************************************/ + +enum wpa_alg_e +{ + WPA_ALG_NONE = 0, + WPA_ALG_WEP, + WPA_ALG_TKIP, + WPA_ALG_CCMP, + WPA_ALG_IGTK, + WPA_ALG_PMK, + WPA_ALG_GCMP, + WPA_ALG_SMS4, + WPA_ALG_KRK, + WPA_ALG_GCMP_256, + WPA_ALG_CCMP_256, + WPA_ALG_BIP_GMAC_128, + WPA_ALG_BIP_GMAC_256, + WPA_ALG_BIP_CMAC_256 +}; + +/* This structure provides the wireless configuration to + * wpa_driver_wext_associate(). + */ + +struct wpa_wconfig_s +{ + uint8_t sta_mode; /* Mode of operation, e.g. IW_MODE_INFRA */ + uint8_t auth_wpa; /* IW_AUTH_WPA_VERSION values, e.g. + * IW_AUTH_WPA_VERSION_WPA2 */ + uint8_t cipher_mode; /* IW_AUTH_PAIRWISE_CIPHER and + * IW_AUTH_GROUP_CIPHER values, e.g., + * IW_AUTH_CIPHER_CCMP */ + uint8_t alg; /* See enum wpa_alg_e above, e.g. + * WPA_ALG_CCMP */ + uint8_t ssidlen; /* Length of the SSID */ + uint8_t phraselen; /* Length of the passphrase */ + FAR const char *ifname; /* E.g., "wlan0" */ + FAR const uint8_t *ssid; /* E.g., "myApSSID" */ + FAR const uint8_t *passphrase; /* E.g., "mySSIDpassphrase" */ +}; + /**************************************************************************** * Public Data ****************************************************************************/ @@ -268,7 +301,7 @@ EXTERN FAR const char *g_wapi_txpower_flags[]; * ****************************************************************************/ -int wapi_get_ifup(int sock, const char *ifname, int *is_up); +int wapi_get_ifup(int sock, FAR const char *ifname, FAR int *is_up); /**************************************************************************** * Name: wapi_set_ifup @@ -278,7 +311,7 @@ int wapi_get_ifup(int sock, const char *ifname, int *is_up); * ****************************************************************************/ -int wapi_set_ifup(int sock, const char *ifname); +int wapi_set_ifup(int sock, FAR const char *ifname); /**************************************************************************** * Name: wapi_set_ifdown @@ -288,7 +321,7 @@ int wapi_set_ifup(int sock, const char *ifname); * ****************************************************************************/ -int wapi_set_ifdown(int sock, const char *ifname); +int wapi_set_ifdown(int sock, FAR const char *ifname); /**************************************************************************** * Name: wapi_get_ip @@ -298,7 +331,7 @@ int wapi_set_ifdown(int sock, const char *ifname); * ****************************************************************************/ -int wapi_get_ip(int sock, const char *ifname, struct in_addr *addr); +int wapi_get_ip(int sock, FAR const char *ifname, struct in_addr *addr); /**************************************************************************** * Name: wapi_set_ip @@ -308,7 +341,8 @@ int wapi_get_ip(int sock, const char *ifname, struct in_addr *addr); * ****************************************************************************/ -int wapi_set_ip(int sock, const char *ifname, const struct in_addr *addr); +int wapi_set_ip(int sock, FAR const char *ifname, + FAR const struct in_addr *addr); /**************************************************************************** * Name: wapi_get_netmask @@ -318,7 +352,8 @@ int wapi_set_ip(int sock, const char *ifname, const struct in_addr *addr); * ****************************************************************************/ -int wapi_get_netmask(int sock, const char *ifname, struct in_addr *addr); +int wapi_get_netmask(int sock, FAR const char *ifname, + FAR struct in_addr *addr); /**************************************************************************** * Name: wapi_set_netmask @@ -328,7 +363,8 @@ int wapi_get_netmask(int sock, const char *ifname, struct in_addr *addr); * ****************************************************************************/ -int wapi_set_netmask(int sock, const char *ifname, const struct in_addr *addr); +int wapi_set_netmask(int sock, FAR const char *ifname, + FAR const struct in_addr *addr); /**************************************************************************** * Name: wapi_add_route_gw @@ -589,7 +625,7 @@ int wapi_make_socket(void); * ****************************************************************************/ -int wapi_scan_init(int sock, const char *ifname); +int wapi_scan_init(int sock, FAR const char *ifname); /**************************************************************************** * Name: wapi_scan_stat @@ -634,7 +670,7 @@ int wapi_scan_coll(int sock, FAR const char *ifname, FAR struct wapi_list_s *aps * ************************************************************************************/ -int wpa_driver_wext_set_ssid(int sockfd, FAR char *ifname, +int wpa_driver_wext_set_ssid(int sockfd, FAR const char *ifname, FAR const uint8_t *ssid, size_t ssid_len); /************************************************************************************ @@ -653,7 +689,7 @@ int wpa_driver_wext_set_ssid(int sockfd, FAR char *ifname, * ************************************************************************************/ -int wpa_driver_wext_set_mode(int sockfd, FAR char *ifname, int mode); +int wpa_driver_wext_set_mode(int sockfd, FAR const char *ifname, int mode); /************************************************************************************ * Name: wpa_driver_wext_set_key_ext @@ -668,7 +704,7 @@ int wpa_driver_wext_set_mode(int sockfd, FAR char *ifname, int mode); * ************************************************************************************/ -int wpa_driver_wext_set_key_ext(int sockfd, FAR char *ifname, enum wpa_alg_e alg, +int wpa_driver_wext_set_key_ext(int sockfd, FAR const char *ifname, enum wpa_alg_e alg, FAR const uint8_t *key, size_t key_len); /************************************************************************************ @@ -677,12 +713,13 @@ int wpa_driver_wext_set_key_ext(int sockfd, FAR char *ifname, enum wpa_alg_e alg * Description: * * Input Parameters: + * wconfig - Describes the wireless configuration. * * Returned Value: * ************************************************************************************/ -int wpa_driver_wext_associate(void); +int wpa_driver_wext_associate(FAR struct wpa_wconfig_s *wconfig); /************************************************************************************ * Name: wpa_driver_wext_set_auth_param @@ -695,7 +732,7 @@ int wpa_driver_wext_associate(void); * ************************************************************************************/ -int wpa_driver_wext_set_auth_param(int sockfd, FAR char *ifname, +int wpa_driver_wext_set_auth_param(int sockfd, FAR const char *ifname, int idx, uint32_t value); #undef EXTERN diff --git a/nshlib/Kconfig b/nshlib/Kconfig index b402b5ac0..955d601c4 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -1465,6 +1465,56 @@ config NSH_PANID ---help--- Select the PAN ID to join upon initialization. +menu "WAPI Configuration" + depends on NET && WIRELESS_WAPI + +config NSH_WAPI_STAMODE + int "Wireless mode of operation" + default 2 + range 0 8 + ---help--- + Mode of operation. See the IW_MODE_* definitions in + include/nuttx/wireless/wireless. The default value corresponds to + IW_MODE_INFRA + +config NSH_WAPI_AUTHWPA + hex "IW_AUTH_WPA_VERSION value" + default 0x00000004 + range 0x00000001 0x00000004 + ---help--- + IW_AUTH_WPA_VERSION values. See the IW_AUTH_WPA_VERSION_* definitions + in include/nuttx/wireless/wireless. The default value corresponds to + IW_AUTH_WPA_VERSION_WPA2. NOTE that this is a bit-encoded field. The + only valid values are 0x00000001, 0x00000002, and 0x00000004 + +config NSH_WAPI_CIPHERMODE + hex " IW_AUTH_PAIRWISE_CIPHER and IW_AUTH_GROUP_CIPHER values" + default 0x00000008 + range 0x00000001 0x00000010 + ---help--- + IW_AUTH_PAIRWISE_CIPHER and IW_AUTH_GROUP_CIPHER values. See the + IW_AUTH_CIPHER_* definitions in include/nuttx/wireless/wireless. + The default value corresponds to IW_AUTH_CIPHER_CCMP. NOTE that + this is a bit-encoded field. The only valid values are 0x00000001, + 0x00000002,0x00000004, ... 0x00000010 + +config NSH_WAPI_ALG; + int "Algorithm" + default 3 + range 0 13 + ---help--- + Algorithm. See enum wpa_alg_e in apps/include/wireless/wapi.h. The + default corresponds to WPA_ALG_CCMP. + +config NSH_WAPI_SSID + string "SSID" + default "myApSSID" + +config NSH_WAPI_PASSPHRASE + string "Passprhase" + default "mySSIDpassphrase" + +endmenu # WAPI Configuration endif # NSH_NETINIT config NSH_MAX_ROUNDTRIP diff --git a/nshlib/Makefile b/nshlib/Makefile index feeb28298..6490d74ee 100644 --- a/nshlib/Makefile +++ b/nshlib/Makefile @@ -74,6 +74,9 @@ endif ifeq ($(CONFIG_NET),y) CSRCS += nsh_netinit.c nsh_netcmds.c +ifeq ($(CONFIG_WIRELESS_WAPI),y) +CSRCS += nsh_associate.c +endif ifeq ($(CONFIG_NET_ROUTE),y) CSRCS += nsh_routecmds.c endif diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 9ca6b009f..73cfe9ec5 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -943,9 +943,9 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR char **argv, FAR const char *redirfile, int oflags); #endif +#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON) /* Working directory support */ -#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON) FAR const char *nsh_getcwd(void); FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl, FAR const char *relpath); @@ -957,9 +957,15 @@ void nsh_freefullpath(FAR char *fullpath); void nsh_dumpbuffer(FAR struct nsh_vtbl_s *vtbl, const char *msg, const uint8_t *buffer, ssize_t nbytes); -/* USB debug support */ +#ifdef CONFIG_WIRELESS_WAPI +/* Wireless */ + +int nsh_associate(FAR const char *ifname); +#endif #ifdef CONFIG_NSH_USBDEV_TRACE +/* USB debug support */ + void nsh_usbtrace(void); #endif diff --git a/nshlib/nsh_netinit.c b/nshlib/nsh_netinit.c index 71b504eb6..f9a1da2cc 100644 --- a/nshlib/nsh_netinit.c +++ b/nshlib/nsh_netinit.c @@ -80,10 +80,6 @@ # include "netutils/ntpclient.h" #endif -#ifdef CONFIG_WIRELESS_WEXT -# include "wireless/wext.h" -#endif - #include "nsh.h" #ifdef CONFIG_NSH_NETINIT @@ -367,12 +363,10 @@ static void nsh_netinit_configure(void) netlib_getmacaddr(NET_DEVNAME, mac); -#ifdef CONFIG_WIRELESS_WEXT +#ifdef CONFIG_WIRELESS_WAPI /* Associate the wlan */ - sleep(2); - wpa_driver_wext_associate(); - sleep(2); + nsh_associate(NET_DEVNAME); #endif /* Set up the DHCPC modules */ diff --git a/wireless/wapi/src/driver_wext.c b/wireless/wapi/src/driver_wext.c index f12c3751f..34ccc66c7 100644 --- a/wireless/wapi/src/driver_wext.c +++ b/wireless/wapi/src/driver_wext.c @@ -59,6 +59,7 @@ #include #include #include +#include #include @@ -85,13 +86,15 @@ * ************************************************************************************/ -int wpa_driver_wext_set_ssid(int sockfd, FAR char *ifname, FAR const uint8_t *ssid, - size_t ssid_len) +int wpa_driver_wext_set_ssid(int sockfd, FAR const char *ifname, + FAR const uint8_t *ssid, size_t ssid_len) { struct iwreq iwr; int ret = 0; char buf[33]; + DEBUGASSERT(ifname != NULL && ssid != NULL && ssid_len > 0); + if (ssid_len > WAPI_ESSID_MAX_SIZE) { return -1; @@ -134,11 +137,13 @@ int wpa_driver_wext_set_ssid(int sockfd, FAR char *ifname, FAR const uint8_t *s * ************************************************************************************/ -int wpa_driver_wext_set_mode(int sockfd, FAR char *ifname, int mode) +int wpa_driver_wext_set_mode(int sockfd, FAR const char *ifname, int mode) { struct iwreq iwr; int ret = -1; + DEBUGASSERT(ifname != NULL); + memset(&iwr, 0, sizeof(iwr)); strncpy(iwr.ifr_name, ifname, IFNAMSIZ); iwr.u.mode = mode; @@ -168,13 +173,15 @@ done: * ************************************************************************************/ -int wpa_driver_wext_set_key_ext(int sockfd, FAR char *ifname, enum wpa_alg_e alg, +int wpa_driver_wext_set_key_ext(int sockfd, FAR const char *ifname, enum wpa_alg_e alg, FAR const uint8_t *key, size_t key_len) { struct iwreq iwr; int ret = 0; struct iw_encode_ext *ext; + DEBUGASSERT(ifname != NULL && key != NULL && key_len > 0); + ext = malloc(sizeof(*ext) + key_len); if (ext == NULL) { @@ -233,42 +240,29 @@ int wpa_driver_wext_set_key_ext(int sockfd, FAR char *ifname, enum wpa_alg_e al * Description: * * Input Parameters: + * wconfig - Describes the wireless configuration. * * Returned Value: * ************************************************************************************/ -int wpa_driver_wext_associate(void) +int wpa_driver_wext_associate(FAR struct wpa_wconfig_s *wconfig) { - int ret; - int sockfd; struct iwreq req; + int sockfd; + int ret; - int sta_mode = IW_MODE_INFRA; + DEBUGASSERT(wconfig != NULL); - // IW_AUTH_WPA_VERSION_DISABLED - // IW_AUTH_WPA_VERSION_WPA - - int auth_wpa = IW_AUTH_WPA_VERSION_WPA2; - - // IW_AUTH_CIPHER_NONE - // IW_AUTH_CIPHER_WEP40 - // IW_AUTH_CIPHER_TKIP - // IW_AUTH_CIPHER_WEP104 - - int cipher_mode = IW_AUTH_CIPHER_CCMP; - - enum wpa_alg_e alg = WPA_ALG_CCMP; - - char ifname[] = "wlan0"; - const uint8_t ssid[] = "myApSSID"; - const uint8_t passphrase[] = "mySSIDpassphrase"; - - ninfo("Entry\n"); + ninfo("sta_mode=%u auth_wpa=%08x cipher_mode=%08x\n", + wconfig->sta_mode, wconfig->auth_wpa, wconfig->cipher_mode); + ninfo("ifname=%s ssid[%u]=%s passphrase[%u]=%s\n", + wconfig->ifname, wconfig->ssidlen, wconfig->ssid, wconfig->phraselen, + wconfig->passphrase); /* Get a socket (only so that we get access to the INET subsystem) */ - sockfd = socket(PF_INETX, NETLIB_SOCK_IOCTL, 0); + sockfd = socket(PF_INETX, SOCK_WAPI, 0); if (sockfd < 0) { return sockfd; @@ -276,44 +270,47 @@ int wpa_driver_wext_associate(void) /* Put the driver name into the request */ - strncpy(req.ifr_name, ifname, IFNAMSIZ); + strncpy(req.ifr_name, wconfig->ifname, IFNAMSIZ); - if (wpa_driver_wext_set_mode(sockfd, ifname, sta_mode) < 0) + ret = wpa_driver_wext_set_mode(sockfd, wconfig->ifname, wconfig->sta_mode); + if (ret < 0) { - nerr("Fail set sta mode\n"); + nerr("ERROR: Fail set sta mode: %d\n", ret); + goto close_socket; + } + + ret = wpa_driver_wext_set_auth_param(sockfd, wconfig->ifname, + IW_AUTH_WPA_VERSION, + wconfig->auth_wpa); + if (ret < 0) + { + nerr("ERROR: Fail set wpa version: %d\n", ret); + goto close_socket; + } + + ret = wpa_driver_wext_set_auth_param(sockfd, wconfig->ifname, + IW_AUTH_CIPHER_PAIRWISE, + wconfig->cipher_mode); + if (ret < 0) + { + nerr("ERROR: Fail set cipher mode: %d\n", ret); + goto close_socket; + } + + ret = wpa_driver_wext_set_key_ext(sockfd, wconfig->ifname, wconfig->alg, + wconfig->passphrase, wconfig->phraselen); + if (ret < 0) + { + nerr("ERROR: Fail set key: %d\n", ret); ret = -1; goto close_socket; } - if (wpa_driver_wext_set_auth_param(sockfd, ifname, IW_AUTH_WPA_VERSION, - auth_wpa) < 0) + ret = wpa_driver_wext_set_ssid(sockfd, wconfig->ifname, wconfig->ssid, + wconfig->ssidlen); + if (ret < 0) { - nerr("Fail set wpa version\n"); - ret = -1; - goto close_socket; - } - - if (wpa_driver_wext_set_auth_param(sockfd, ifname, - IW_AUTH_CIPHER_PAIRWISE, cipher_mode) < 0) - { - nerr("Fail set cipher mode\n"); - ret = -1; - goto close_socket; - } - - if (wpa_driver_wext_set_key_ext(sockfd, ifname, alg, passphrase, - sizeof(passphrase)-1)) - { - nerr("Fail set key\n"); - ret = -1; - goto close_socket; - } - - if (wpa_driver_wext_set_ssid(sockfd, ifname, ssid, sizeof(ssid)-1) < 0) - { - nerr("Fail set ssid\n"); - ret = -1; - goto close_socket; + nerr("ERROR: Fail set ssid: %d\n", ret); } close_socket: @@ -332,13 +329,15 @@ close_socket: * ************************************************************************************/ -int wpa_driver_wext_set_auth_param(int sockfd, FAR char *ifname, int idx, +int wpa_driver_wext_set_auth_param(int sockfd, FAR const char *ifname, int idx, uint32_t value) { struct iwreq iwr; int errcode; int ret = 0; + DEBUGASSERT(ifname != NULL); + memset(&iwr, 0, sizeof(iwr)); strncpy(iwr.ifr_name, ifname, IFNAMSIZ); iwr.u.param.flags = idx & IW_AUTH_INDEX; diff --git a/wireless/wapi/src/util.c b/wireless/wapi/src/util.c index 28dd7b600..d7b340863 100644 --- a/wireless/wapi/src/util.c +++ b/wireless/wapi/src/util.c @@ -51,27 +51,6 @@ * Pre-processor Definitions ****************************************************************************/ -/* The address family that we used to create the socket really does not - * matter. It should, however, be valid in the current configuration. - */ - -#if defined(CONFIG_NET_IPv4) -# define PF_INETX PF_INET -#elif defined(CONFIG_NET_IPv6) -# define PF_INETX PF_INET6 -#endif - -/* SOCK_DGRAM is the preferred socket type to use when we just want a - * socket for performing driver ioctls. However, we can't use SOCK_DRAM - * if UDP is disabled. - */ - -#ifdef CONFIG_NET_UDP -# define SOCK_WAPI SOCK_DGRAM -#else -# define SOCK_WAPI SOCK_STREAM -#endif - /* Size of the command buffer */ #define WAPI_IOCTL_COMMAND_NAMEBUFSIZ 24