xiaoxiang781216 commented on code in PR #3876: URL: https://github.com/apache/nuttx-apps/pull/3876#discussion_r4309614753
########## netutils/netlib/netlib_bridge.c: ########## @@ -0,0 +1,177 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_bridge.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> +#ifdef CONFIG_NET_BRIDGE + +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <string.h> +#include <unistd.h> +#include <net/if.h> + +#include <nuttx/net/ioctl.h> +#include <nuttx/net/netconfig.h> + +#include "netutils/netlib.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_bridge_cmd + * + * Description: + * Issue a bridge ioctl that takes a bridge name only (add/delete bridge) + * or a bridge name and an interface (add/delete port). + * + ****************************************************************************/ + +static int netlib_bridge_cmd(int cmd, FAR const char *bridge, + FAR const char *ifname) +{ + struct ifreq req; + int sockfd; + int ret; + + if (bridge == NULL) + { + return ERROR; + } + + sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL); + if (sockfd < 0) + { + return ERROR; + } + + if (ifname == NULL) + { + ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)bridge); + } + else + { + memset(&req, 0, sizeof(req)); + strlcpy(req.ifr_name, bridge, IFNAMSIZ); + req.ifr_ifindex = if_nametoindex(ifname); + if (req.ifr_ifindex == 0) + { + ret = ERROR; + } + else + { + ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)&req); + } + } + + close(sockfd); + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_add_bridge + * + * Description: + * Create a bridge. + * + * Parameters: + * bridge - The name of the bridge to create + * + * Return: + * 0 on success; -1 on failure with errno set + * + ****************************************************************************/ + +int netlib_add_bridge(FAR const char *bridge) +{ + return netlib_bridge_cmd(SIOCBRADDBR, bridge, NULL); +} + +/**************************************************************************** + * Name: netlib_del_bridge + * + * Description: + * Delete a bridge. + * + * Parameters: + * bridge - The name of the bridge to delete + * + * Return: + * 0 on success; -1 on failure with errno set + * + ****************************************************************************/ + +int netlib_del_bridge(FAR const char *bridge) +{ + return netlib_bridge_cmd(SIOCBRDELBR, bridge, NULL); +} + +/**************************************************************************** + * Name: netlib_bridge_addif + * + * Description: + * Add a network device to a bridge. + * + * Parameters: + * bridge - The name of the bridge + * ifname - The name of the network device to add + * + * Return: + * 0 on success; -1 on failure with errno set (the device must exist) + * + ****************************************************************************/ + +int netlib_bridge_addif(FAR const char *bridge, FAR const char *ifname) +{ + return ifname == NULL ? ERROR : + netlib_bridge_cmd(SIOCBRADDIF, bridge, ifname); +} + +/**************************************************************************** + * Name: netlib_bridge_delif + * + * Description: + * Remove a network device from a bridge. + * + * Parameters: + * bridge - The name of the bridge + * ifname - The name of the network device to remove + * + * Return: + * 0 on success; -1 on failure with errno set (the device must exist) + * + ****************************************************************************/ + +int netlib_bridge_delif(FAR const char *bridge, FAR const char *ifname) +{ + return ifname == NULL ? ERROR : Review Comment: ditto ########## netutils/netlib/netlib_bridge.c: ########## @@ -0,0 +1,177 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_bridge.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> +#ifdef CONFIG_NET_BRIDGE Review Comment: remove the above two lines ########## netutils/netlib/netlib_bridge.c: ########## @@ -0,0 +1,177 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_bridge.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> +#ifdef CONFIG_NET_BRIDGE + +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <string.h> +#include <unistd.h> +#include <net/if.h> + +#include <nuttx/net/ioctl.h> +#include <nuttx/net/netconfig.h> + +#include "netutils/netlib.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_bridge_cmd + * + * Description: + * Issue a bridge ioctl that takes a bridge name only (add/delete bridge) + * or a bridge name and an interface (add/delete port). + * + ****************************************************************************/ + +static int netlib_bridge_cmd(int cmd, FAR const char *bridge, + FAR const char *ifname) +{ + struct ifreq req; + int sockfd; + int ret; + + if (bridge == NULL) + { + return ERROR; + } + + sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL); + if (sockfd < 0) + { + return ERROR; + } + + if (ifname == NULL) + { + ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)bridge); + } + else + { + memset(&req, 0, sizeof(req)); + strlcpy(req.ifr_name, bridge, IFNAMSIZ); + req.ifr_ifindex = if_nametoindex(ifname); + if (req.ifr_ifindex == 0) + { + ret = ERROR; + } + else + { + ret = ioctl(sockfd, cmd, (unsigned long)(uintptr_t)&req); + } + } + + close(sockfd); + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_add_bridge + * + * Description: + * Create a bridge. + * + * Parameters: + * bridge - The name of the bridge to create + * + * Return: + * 0 on success; -1 on failure with errno set + * + ****************************************************************************/ + +int netlib_add_bridge(FAR const char *bridge) +{ + return netlib_bridge_cmd(SIOCBRADDBR, bridge, NULL); +} + +/**************************************************************************** + * Name: netlib_del_bridge + * + * Description: + * Delete a bridge. + * + * Parameters: + * bridge - The name of the bridge to delete + * + * Return: + * 0 on success; -1 on failure with errno set + * + ****************************************************************************/ + +int netlib_del_bridge(FAR const char *bridge) +{ + return netlib_bridge_cmd(SIOCBRDELBR, bridge, NULL); +} + +/**************************************************************************** + * Name: netlib_bridge_addif + * + * Description: + * Add a network device to a bridge. + * + * Parameters: + * bridge - The name of the bridge + * ifname - The name of the network device to add + * + * Return: + * 0 on success; -1 on failure with errno set (the device must exist) + * + ****************************************************************************/ + +int netlib_bridge_addif(FAR const char *bridge, FAR const char *ifname) +{ + return ifname == NULL ? ERROR : Review Comment: need set errno ########## netutils/wifibridge/wifibridge.c: ########## @@ -0,0 +1,351 @@ +/**************************************************************************** + * apps/netutils/wifibridge/wifibridge.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <errno.h> +#include <string.h> +#include <unistd.h> + +#include <net/if.h> +#include <arpa/inet.h> +#include <netinet/in.h> + +#include "netutils/netlib.h" +#include "netutils/wifibridge.h" +#include "wireless/wapi.h" + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD +# include "netutils/dhcpd.h" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_setup_softap + * + * Description: + * Put the Wi-Fi interface in master mode with the requested security, + * channel and SSID. + * + ****************************************************************************/ + +static int wifibridge_setup_softap(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + size_t passlen = strnlen(cfg->passphrase, 64); + int version; + int ret; + + if (passlen < 8 || passlen > 63 || cfg->channel < 1 || cfg->channel > 13) + { + return -EINVAL; + } + + version = cfg->security == WIFIBRIDGE_SEC_WPA3 ? + IW_AUTH_WPA_VERSION_WPA3 : IW_AUTH_WPA_VERSION_WPA2; + + ret = wapi_set_mode(sock, cfg->wlanif, WAPI_MODE_MASTER); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_WPA_VERSION, version); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_CIPHER_PAIRWISE, + IW_AUTH_CIPHER_CCMP); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_key_ext(sock, cfg->wlanif, WPA_ALG_CCMP, + cfg->passphrase, passlen); + if (ret < 0) + { + return ret; + } + + ret = wapi_set_freq(sock, cfg->wlanif, cfg->channel, WAPI_FREQ_FIXED); + if (ret < 0) + { + return ret; + } + + return wapi_set_essid(sock, cfg->wlanif, cfg->ssid, WAPI_ESSID_ON); +} + +/**************************************************************************** + * Name: wifibridge_teardown + * + * Description: + * Undo the steps of wifibridge_start() in reverse order. Each step is + * tried even if an earlier one failed, since this also cleans up a + * partially started bridge. + * + ****************************************************************************/ + +static int wifibridge_teardown(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + int ret = OK; + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD + dhcpd_stop(); +#endif + + netlib_ifdown(cfg->bridge); + + netlib_bridge_delif(cfg->bridge, cfg->wlanif); + netlib_bridge_delif(cfg->bridge, cfg->ethif); + + if (netlib_del_bridge(cfg->bridge) < 0) + { + ret = -errno; + } + + /* Bringing the interface down does not stop the AP, an ESSID set with the + * off flag does. + */ + + wapi_set_essid(sock, cfg->wlanif, "", WAPI_ESSID_OFF); + wapi_set_ifdown(sock, cfg->wlanif); + + return ret; +} + +/**************************************************************************** + * Name: wifibridge_isup + ****************************************************************************/ + +static bool wifibridge_isup(int sock, FAR const char *ifname) +{ + int is_up; + + return wapi_get_ifup(sock, ifname, &is_up) >= 0 && is_up != 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_start + ****************************************************************************/ + +int wifibridge_start(FAR const struct wifibridge_config_s *config) +{ + struct in_addr none; + struct in_addr addr; + int sock; + int ret; + + if (config == NULL || config->bridge == NULL || config->ethif == NULL || + config->wlanif == NULL || config->ssid == NULL || + config->passphrase == NULL) + { + return -EINVAL; + } + + if (if_nametoindex(config->bridge) != 0) + { + return -EEXIST; Review Comment: -errno ########## netutils/netlib/netlib_bridge.c: ########## @@ -0,0 +1,177 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_bridge.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> +#ifdef CONFIG_NET_BRIDGE + +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <string.h> +#include <unistd.h> +#include <net/if.h> + +#include <nuttx/net/ioctl.h> +#include <nuttx/net/netconfig.h> + +#include "netutils/netlib.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_bridge_cmd + * + * Description: + * Issue a bridge ioctl that takes a bridge name only (add/delete bridge) + * or a bridge name and an interface (add/delete port). + * + ****************************************************************************/ + +static int netlib_bridge_cmd(int cmd, FAR const char *bridge, + FAR const char *ifname) +{ + struct ifreq req; + int sockfd; + int ret; + + if (bridge == NULL) + { + return ERROR; Review Comment: errno = EINVAL ########## netutils/wifibridge/wifibridge.c: ########## @@ -0,0 +1,351 @@ +/**************************************************************************** + * apps/netutils/wifibridge/wifibridge.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <errno.h> +#include <string.h> +#include <unistd.h> + +#include <net/if.h> +#include <arpa/inet.h> +#include <netinet/in.h> + +#include "netutils/netlib.h" +#include "netutils/wifibridge.h" +#include "wireless/wapi.h" + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD +# include "netutils/dhcpd.h" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_setup_softap + * + * Description: + * Put the Wi-Fi interface in master mode with the requested security, + * channel and SSID. + * + ****************************************************************************/ + +static int wifibridge_setup_softap(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + size_t passlen = strnlen(cfg->passphrase, 64); + int version; + int ret; + + if (passlen < 8 || passlen > 63 || cfg->channel < 1 || cfg->channel > 13) + { + return -EINVAL; + } + + version = cfg->security == WIFIBRIDGE_SEC_WPA3 ? + IW_AUTH_WPA_VERSION_WPA3 : IW_AUTH_WPA_VERSION_WPA2; + + ret = wapi_set_mode(sock, cfg->wlanif, WAPI_MODE_MASTER); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_WPA_VERSION, version); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_CIPHER_PAIRWISE, + IW_AUTH_CIPHER_CCMP); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_key_ext(sock, cfg->wlanif, WPA_ALG_CCMP, + cfg->passphrase, passlen); + if (ret < 0) + { + return ret; + } + + ret = wapi_set_freq(sock, cfg->wlanif, cfg->channel, WAPI_FREQ_FIXED); + if (ret < 0) + { + return ret; + } + + return wapi_set_essid(sock, cfg->wlanif, cfg->ssid, WAPI_ESSID_ON); +} + +/**************************************************************************** + * Name: wifibridge_teardown + * + * Description: + * Undo the steps of wifibridge_start() in reverse order. Each step is + * tried even if an earlier one failed, since this also cleans up a + * partially started bridge. + * + ****************************************************************************/ + +static int wifibridge_teardown(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + int ret = OK; + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD + dhcpd_stop(); +#endif + + netlib_ifdown(cfg->bridge); + + netlib_bridge_delif(cfg->bridge, cfg->wlanif); + netlib_bridge_delif(cfg->bridge, cfg->ethif); + + if (netlib_del_bridge(cfg->bridge) < 0) + { + ret = -errno; + } + + /* Bringing the interface down does not stop the AP, an ESSID set with the + * off flag does. + */ + + wapi_set_essid(sock, cfg->wlanif, "", WAPI_ESSID_OFF); + wapi_set_ifdown(sock, cfg->wlanif); + + return ret; +} + +/**************************************************************************** + * Name: wifibridge_isup + ****************************************************************************/ + +static bool wifibridge_isup(int sock, FAR const char *ifname) +{ + int is_up; + + return wapi_get_ifup(sock, ifname, &is_up) >= 0 && is_up != 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_start + ****************************************************************************/ + +int wifibridge_start(FAR const struct wifibridge_config_s *config) +{ + struct in_addr none; + struct in_addr addr; + int sock; + int ret; + + if (config == NULL || config->bridge == NULL || config->ethif == NULL || + config->wlanif == NULL || config->ssid == NULL || + config->passphrase == NULL) + { + return -EINVAL; + } + + if (if_nametoindex(config->bridge) != 0) + { + return -EEXIST; + } + + sock = wapi_make_socket(); + if (sock < 0) + { + return sock; + } + + /* The ports carry no address, only the bridge does */ + + none.s_addr = INADDR_ANY; + + ret = wifibridge_setup_softap(sock, config); + if (ret < 0) + { + goto errout; + } + + ret = netlib_set_ipv4addr(config->wlanif, &none); + if (ret < 0) + { + goto errout; + } + + ret = netlib_set_ipv4addr(config->ethif, &none); + if (ret < 0) + { + goto errout; + } + + /* A bridge port must be up to carry traffic */ + + ret = netlib_ifup(config->ethif); + if (ret < 0) + { + goto errout; + } + + ret = netlib_ifup(config->wlanif); + if (ret < 0) + { + goto errout; + } + + if (netlib_add_bridge(config->bridge) < 0) + { + ret = -errno; Review Comment: let's return -errno for the new bridge netutils functions like netlib_ifup ########## netutils/wifibridge/wifibridge.c: ########## @@ -0,0 +1,351 @@ +/**************************************************************************** + * apps/netutils/wifibridge/wifibridge.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <errno.h> +#include <string.h> +#include <unistd.h> + +#include <net/if.h> +#include <arpa/inet.h> +#include <netinet/in.h> + +#include "netutils/netlib.h" +#include "netutils/wifibridge.h" +#include "wireless/wapi.h" + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD +# include "netutils/dhcpd.h" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_setup_softap + * + * Description: + * Put the Wi-Fi interface in master mode with the requested security, + * channel and SSID. + * + ****************************************************************************/ + +static int wifibridge_setup_softap(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + size_t passlen = strnlen(cfg->passphrase, 64); + int version; + int ret; + + if (passlen < 8 || passlen > 63 || cfg->channel < 1 || cfg->channel > 13) + { + return -EINVAL; + } + + version = cfg->security == WIFIBRIDGE_SEC_WPA3 ? + IW_AUTH_WPA_VERSION_WPA3 : IW_AUTH_WPA_VERSION_WPA2; + + ret = wapi_set_mode(sock, cfg->wlanif, WAPI_MODE_MASTER); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_WPA_VERSION, version); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_auth_param(sock, cfg->wlanif, + IW_AUTH_CIPHER_PAIRWISE, + IW_AUTH_CIPHER_CCMP); + if (ret < 0) + { + return ret; + } + + ret = wpa_driver_wext_set_key_ext(sock, cfg->wlanif, WPA_ALG_CCMP, + cfg->passphrase, passlen); + if (ret < 0) + { + return ret; + } + + ret = wapi_set_freq(sock, cfg->wlanif, cfg->channel, WAPI_FREQ_FIXED); + if (ret < 0) + { + return ret; + } + + return wapi_set_essid(sock, cfg->wlanif, cfg->ssid, WAPI_ESSID_ON); +} + +/**************************************************************************** + * Name: wifibridge_teardown + * + * Description: + * Undo the steps of wifibridge_start() in reverse order. Each step is + * tried even if an earlier one failed, since this also cleans up a + * partially started bridge. + * + ****************************************************************************/ + +static int wifibridge_teardown(int sock, + FAR const struct wifibridge_config_s *cfg) +{ + int ret = OK; + +#ifdef CONFIG_NETUTILS_WIFIBRIDGE_DHCPD + dhcpd_stop(); +#endif + + netlib_ifdown(cfg->bridge); + + netlib_bridge_delif(cfg->bridge, cfg->wlanif); + netlib_bridge_delif(cfg->bridge, cfg->ethif); + + if (netlib_del_bridge(cfg->bridge) < 0) + { + ret = -errno; + } + + /* Bringing the interface down does not stop the AP, an ESSID set with the + * off flag does. + */ + + wapi_set_essid(sock, cfg->wlanif, "", WAPI_ESSID_OFF); + wapi_set_ifdown(sock, cfg->wlanif); + + return ret; +} + +/**************************************************************************** + * Name: wifibridge_isup + ****************************************************************************/ + +static bool wifibridge_isup(int sock, FAR const char *ifname) +{ + int is_up; + + return wapi_get_ifup(sock, ifname, &is_up) >= 0 && is_up != 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wifibridge_start + ****************************************************************************/ + +int wifibridge_start(FAR const struct wifibridge_config_s *config) +{ + struct in_addr none; + struct in_addr addr; + int sock; + int ret; + + if (config == NULL || config->bridge == NULL || config->ethif == NULL || + config->wlanif == NULL || config->ssid == NULL || + config->passphrase == NULL) + { + return -EINVAL; + } + + if (if_nametoindex(config->bridge) != 0) + { + return -EEXIST; + } + + sock = wapi_make_socket(); + if (sock < 0) + { + return sock; + } + + /* The ports carry no address, only the bridge does */ + + none.s_addr = INADDR_ANY; Review Comment: move after line 204 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
