Move wireless/wext into wireless/wapi/src. Update some naming for better follow the NuttX coding style.

This commit is contained in:
Gregory Nutt 2017-05-06 09:40:09 -06:00
parent 33027bd66e
commit f12d857f0a
11 changed files with 288 additions and 436 deletions

View file

@ -35,7 +35,7 @@
ifeq ($(CONFIG_WIRELESS_WAPI),y)
CSRCS = network.c util.c wireless.c
CSRCS = network.c util.c wireless.c driver_wext.c
ifeq ($(CONFIG_WIRELESS_WAPI_CMDTOOL),y)
MAINSRC = wapi.c

View file

@ -0,0 +1,361 @@
/************************************************************************************
* apps/wireless/wapi/src/driver_wext.c
* Driver interaction with generic Wireless Extensions
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Simon Piriou <spiriou31@gmail.com>
* Gregory Nutt <gnutt@nuttx.org>
*
* Adapted for NuttX from the driver_ext.c of WPA suplicant written originally
* by Jouni Malinen
*
* Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
*
* 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.
*
************************************************************************************/
/* This file implements a driver interface for the Linux Wireless Extensions.
* When used with WE-18 or newer, this interface can be used as-is with number
* of drivers. In addition to this, some of the common functions in this file
* can be used by other driver interface implementations that use generic WE
* ioctls, but require private ioctls for some of the functionality.
*/
/************************************************************************************
* Included Files
************************************************************************************/
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <debug.h>
#include "wireless/wapi.h"
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: wpa_driver_wext_set_ssid
*
* Description:
* Set SSID, SIOCSIWESSID
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* ssid - SSID
* ssid_len - Length of SSID (0..32)
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_ssid(int sockfd, FAR char *ifname, FAR const uint8_t *ssid,
size_t ssid_len)
{
struct iwreq iwr;
int ret = 0;
char buf[33];
if (ssid_len > WAPI_ESSID_MAX_SIZE)
{
return -1;
}
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
/* flags: 1 = ESSID is active, 0 = not (promiscuous) */
iwr.u.essid.flags = (ssid_len != 0);
memset(buf, 0, sizeof(buf));
memcpy(buf, ssid, ssid_len);
iwr.u.essid.pointer = (caddr_t) buf;
iwr.u.essid.length = ssid_len;
if (ioctl(sockfd, SIOCSIWESSID, (unsigned long)&iwr) < 0)
{
nerr("ERROR: ioctl[SIOCSIWESSID]: %d", errno);
ret = -1;
}
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_set_mode
*
* Description:
* Set wireless mode (infra/adhoc), SIOCSIWMODE
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* mode - 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_mode(int sockfd, FAR char *ifname, int mode)
{
struct iwreq iwr;
int ret = -1;
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.mode = mode;
if (ioctl(sockfd, SIOCSIWMODE, (unsigned long)&iwr) == 0)
{
ret = 0;
goto done;
}
nerr("ERROR: ioctl[SIOCSIWMODE]: %d", errno);
done:
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_set_key_ext
*
* Description:
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
*
* Returned Value:
*
************************************************************************************/
int wpa_driver_wext_set_key_ext(int sockfd, FAR 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;
ext = malloc(sizeof(*ext) + key_len);
if (ext == NULL)
{
return -1;
}
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.encoding.pointer = (caddr_t) ext;
iwr.u.encoding.length = sizeof(*ext) + key_len;
if (key && key_len)
{
memcpy(ext + 1, key, key_len);
ext->key_len = key_len;
}
switch (alg)
{
case WPA_ALG_NONE:
ext->alg = IW_ENCODE_ALG_NONE;
break;
case WPA_ALG_WEP:
ext->alg = IW_ENCODE_ALG_WEP;
break;
case WPA_ALG_TKIP:
ext->alg = IW_ENCODE_ALG_TKIP;
break;
case WPA_ALG_CCMP:
ext->alg = IW_ENCODE_ALG_CCMP;
break;
default:
nerr("ERROR: Unknown algorithm %d", alg);
free(ext);
return -1;
}
if (ioctl(sockfd, SIOCSIWENCODEEXT, (unsigned long)&iwr) < 0)
{
ret = errno == EOPNOTSUPP ? -2 : -1;
nerr("ERROR: ioctl[SIOCSIWENCODEEXT]: %d", errno);
}
free(ext);
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_associate
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
************************************************************************************/
int wpa_driver_wext_associate(void)
{
int ret;
int sockfd;
struct iwreq req;
int sta_mode = IW_MODE_INFRA;
// 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");
/* Get a socket (only so that we get access to the INET subsystem) */
sockfd = socket(PF_INETX, NETLIB_SOCK_IOCTL, 0);
if (sockfd < 0)
{
return sockfd;
}
/* Put the driver name into the request */
strncpy(req.ifr_name, ifname, IFNAMSIZ);
if (wpa_driver_wext_set_mode(sockfd, ifname, sta_mode) < 0)
{
nerr("Fail set sta mode\n");
ret = -1;
goto close_socket;
}
if (wpa_driver_wext_set_auth_param(sockfd, ifname, IW_AUTH_WPA_VERSION,
auth_wpa) < 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;
}
close_socket:
close(sockfd);
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_set_auth_param
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
************************************************************************************/
int wpa_driver_wext_set_auth_param(int sockfd, FAR char *ifname, int idx,
uint32_t value)
{
struct iwreq iwr;
int errcode;
int ret = 0;
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.param.flags = idx & IW_AUTH_INDEX;
iwr.u.param.value = value;
if (ioctl(sockfd, SIOCSIWAUTH, (unsigned long)&iwr) < 0)
{
errcode = errno;
if (errcode != EOPNOTSUPP)
{
nerr("ERROR: SIOCSIWAUTH(param %d value 0x%x) failed: %d)",
idx, value, errcode);
}
ret = errcode == EOPNOTSUPP ? -2 : -1;
}
return ret;
}

View file

@ -104,7 +104,7 @@ static int wapi_set_addr(int sock, FAR const char *ifname, int cmd,
#ifdef CONFIG_NET_ROUTE
static int wapi_act_route_gw(int sock, int act,
wapi_route_target_t targettype,
enum wapi_route_target_e targettype,
FAR const struct in_addr *target,
FAR const struct in_addr *netmask,
FAR const struct in_addr *gw)
@ -325,7 +325,7 @@ int wapi_set_netmask(int sock, FAR const char *ifname,
****************************************************************************/
#ifdef CONFIG_NET_ROUTE
int wapi_add_route_gw(int sock, wapi_route_target_t targettype,
int wapi_add_route_gw(int sock, enum wapi_route_target_e targettype,
FAR const struct in_addr *target,
FAR const struct in_addr *netmask,
FAR const struct in_addr *gw)
@ -343,7 +343,7 @@ int wapi_add_route_gw(int sock, wapi_route_target_t targettype,
****************************************************************************/
#ifdef CONFIG_NET_ROUTE
int wapi_del_route_gw(int sock, wapi_route_target_t targettype,
int wapi_del_route_gw(int sock, enum wapi_route_target_e targettype,
FAR const struct in_addr *target,
FAR const struct in_addr *netmask,
FAR const struct in_addr *gw)

View file

@ -227,20 +227,20 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
struct in_addr addr;
double freq;
wapi_freq_flag_t freq_flag;
enum wapi_freq_flag_e freq_flag;
char essid[WAPI_ESSID_MAX_SIZE + 1];
wapi_essid_flag_t essid_flag;
enum wapi_essid_flag_e essid_flag;
wapi_mode_t mode;
enum wapi_mode_e mode;
struct ether_addr ap;
int bitrate;
wapi_bitrate_flag_t bitrate_flag;
enum wapi_bitrate_flag_e bitrate_flag;
int txpower;
wapi_txpower_flag_t txpower_flag;
enum wapi_txpower_flag_e txpower_flag;
int ret;
@ -450,13 +450,13 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname,
FAR const char *freqstr, FAR const char *flagstr)
{
double frequency;
wapi_freq_flag_t freq_flag;
enum wapi_freq_flag_e freq_flag;
int ret;
/* Convert input strings to values */
frequency = wapi_str2double(freqstr);
freq_flag = (wapi_freq_flag_t)wapi_str2ndx(flagstr, g_wapi_freq_flags,
freq_flag = (enum wapi_freq_flag_e)wapi_str2ndx(flagstr, g_wapi_freq_flags,
IW_FREQ_NFLAGS);
/* Set the frequency */
@ -482,12 +482,12 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname,
static void wapi_essid_cmd(int sock, FAR const char *ifname,
FAR const char *essid, FAR const char *flagstr)
{
wapi_essid_flag_t essid_flag;
enum wapi_essid_flag_e essid_flag;
int ret;
/* Convert input strings to values */
essid_flag = (wapi_essid_flag_t)wapi_str2ndx(flagstr, g_wapi_essid_flags, 2);
essid_flag = (enum wapi_essid_flag_e)wapi_str2ndx(flagstr, g_wapi_essid_flags, 2);
/* Set the ESSID */
@ -512,12 +512,12 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname,
static void wapi_mode_cmd(int sock, FAR const char *ifname,
FAR const char *modestr)
{
wapi_mode_t mode;
enum wapi_mode_e mode;
int ret;
/* Convert input strings to values */
mode = (wapi_mode_t)wapi_str2ndx(modestr, g_wapi_modes, IW_MODE_NFLAGS);
mode = (enum wapi_mode_e)wapi_str2ndx(modestr, g_wapi_modes, IW_MODE_NFLAGS);
/* Set operating mode */
@ -576,14 +576,14 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
FAR const char *ratestr, FAR const char *flagstr)
{
wapi_bitrate_flag_t bitrate_flag;
enum wapi_bitrate_flag_e bitrate_flag;
int bitrate;
int ret;
/* Convert input strings to values */
bitrate = wapi_str2int(ratestr);
bitrate_flag = (wapi_bitrate_flag_t)
bitrate_flag = (enum wapi_bitrate_flag_e)
wapi_str2ndx(flagstr, g_wapi_bitrate_flags, 2);
/* Set bitrate */
@ -609,14 +609,14 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
static void wapi_txpower_cmd(int sock, FAR const char *ifname,
FAR const char *pwrstr, FAR const char *flagstr)
{
wapi_txpower_flag_t txpower_flag;
enum wapi_txpower_flag_e txpower_flag;
int txpower;
int ret;
/* Convert input strings to values */
txpower = wapi_str2int(pwrstr);
txpower_flag = (wapi_txpower_flag_t)
txpower_flag = (enum wapi_txpower_flag_e)
wapi_str2ndx(flagstr, g_wapi_txpower_flags, IW_TXPOW_NFLAGS);
/* Set txpower */
@ -643,8 +643,8 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
{
int sleepdur = 1;
int sleeptries = 5;
wapi_list_t list;
FAR wapi_scan_info_t *info;
struct wapi_list_s list;
FAR struct wapi_scan_info_s *info;
int ret;
/* Start scan */
@ -670,7 +670,7 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
/* Collect results */
bzero(&list, sizeof(wapi_list_t));
bzero(&list, sizeof(struct wapi_list_s));
ret = wapi_scan_coll(sock, ifname, &list);
if (ret < 0)
{
@ -693,7 +693,7 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
info = list.head.scan;
while (info)
{
FAR wapi_scan_info_t *temp;
FAR struct wapi_scan_info_s *temp;
temp = info->next;
free(info);

View file

@ -161,7 +161,7 @@ static inline void wapi_float2freq(double floatfreq, struct iw_freq *freq)
*
****************************************************************************/
static int wapi_parse_mode(int iw_mode, FAR wapi_mode_t *wapi_mode)
static int wapi_parse_mode(int iw_mode, FAR enum wapi_mode_e *wapi_mode)
{
switch (iw_mode)
{
@ -234,9 +234,9 @@ return -ENOSYS;
*
****************************************************************************/
static int wapi_scan_event(FAR struct iw_event *event, FAR wapi_list_t *list)
static int wapi_scan_event(FAR struct iw_event *event, FAR struct wapi_list_s *list)
{
FAR wapi_scan_info_t *info;
FAR struct wapi_scan_info_s *info;
/* Get current "wapi_info_t". */
@ -248,11 +248,11 @@ static int wapi_scan_event(FAR struct iw_event *event, FAR wapi_list_t *list)
{
case SIOCGIWAP:
{
wapi_scan_info_t *temp;
struct wapi_scan_info_s *temp;
/* Allocate a new cell. */
temp = malloc(sizeof(wapi_scan_info_t));
temp = malloc(sizeof(struct wapi_scan_info_s));
if (!temp)
{
WAPI_STRERROR("malloc()");
@ -261,7 +261,7 @@ static int wapi_scan_event(FAR struct iw_event *event, FAR wapi_list_t *list)
/* Reset it. */
bzero(temp, sizeof(wapi_scan_info_t));
bzero(temp, sizeof(struct wapi_scan_info_s));
/* Save cell identifier. */
@ -333,7 +333,7 @@ static int wapi_scan_event(FAR struct iw_event *event, FAR wapi_list_t *list)
****************************************************************************/
int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq,
FAR wapi_freq_flag_t *flag)
FAR enum wapi_freq_flag_e *flag)
{
struct iwreq wrq;
int ret;
@ -384,7 +384,7 @@ int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq,
****************************************************************************/
int wapi_set_freq(int sock, FAR const char *ifname, double freq,
wapi_freq_flag_t flag)
enum wapi_freq_flag_e flag)
{
struct iwreq wrq;
int ret;
@ -551,7 +551,7 @@ int wapi_chan2freq(int sock, FAR const char *ifname, int chan,
****************************************************************************/
int wapi_get_essid(int sock, FAR const char *ifname, FAR char *essid,
FAR wapi_essid_flag_t *flag)
FAR enum wapi_essid_flag_e *flag)
{
struct iwreq wrq;
int ret;
@ -590,7 +590,7 @@ int wapi_get_essid(int sock, FAR const char *ifname, FAR char *essid,
****************************************************************************/
int wapi_set_essid(int sock, FAR const char *ifname, FAR const char *essid,
wapi_essid_flag_t flag)
enum wapi_essid_flag_e flag)
{
char buf[WAPI_ESSID_MAX_SIZE + 1];
struct iwreq wrq;
@ -623,7 +623,7 @@ int wapi_set_essid(int sock, FAR const char *ifname, FAR const char *essid,
*
****************************************************************************/
int wapi_get_mode(int sock, FAR const char *ifname, FAR wapi_mode_t *mode)
int wapi_get_mode(int sock, FAR const char *ifname, FAR enum wapi_mode_e *mode)
{
struct iwreq wrq;
int ret;
@ -653,7 +653,7 @@ int wapi_get_mode(int sock, FAR const char *ifname, FAR wapi_mode_t *mode)
*
****************************************************************************/
int wapi_set_mode(int sock, FAR const char *ifname, wapi_mode_t mode)
int wapi_set_mode(int sock, FAR const char *ifname, enum wapi_mode_e mode)
{
struct iwreq wrq;
int ret;
@ -772,7 +772,7 @@ int wapi_set_ap(int sock, FAR const char *ifname,
****************************************************************************/
int wapi_get_bitrate(int sock, FAR const char *ifname,
FAR int *bitrate, FAR wapi_bitrate_flag_t *flag)
FAR int *bitrate, FAR enum wapi_bitrate_flag_e *flag)
{
struct iwreq wrq;
int ret;
@ -815,7 +815,7 @@ int wapi_get_bitrate(int sock, FAR const char *ifname,
****************************************************************************/
int wapi_set_bitrate(int sock, FAR const char *ifname, int bitrate,
wapi_bitrate_flag_t flag)
enum wapi_bitrate_flag_e flag)
{
struct iwreq wrq;
int ret;
@ -870,7 +870,7 @@ int wapi_mwatt2dbm(int mwatt)
****************************************************************************/
int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power,
FAR wapi_txpower_flag_t *flag)
FAR enum wapi_txpower_flag_e *flag)
{
struct iwreq wrq;
int ret;
@ -931,7 +931,7 @@ int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power,
****************************************************************************/
int wapi_set_txpower(int sock, FAR const char *ifname, int power,
wapi_txpower_flag_t flag)
enum wapi_txpower_flag_e flag)
{
struct iwreq wrq;
int ret;
@ -1054,11 +1054,11 @@ int wapi_scan_stat(int sock, FAR const char *ifname)
* Collects the results of a scan process.
*
* Input Parameters:
* aps - Pushes collected wapi_scan_info_t into this list.
* aps - Pushes collected struct wapi_scan_info_s into this list.
*
****************************************************************************/
int wapi_scan_coll(int sock, FAR const char *ifname, FAR wapi_list_t *aps)
int wapi_scan_coll(int sock, FAR const char *ifname, FAR struct wapi_list_s *aps)
{
FAR char *buf;
int buflen;