Port WPA supplicant to RTEMS-libbsd

Last blog post, i write about how to import wpa from FreeBSD.
This blog post, i will introduct how to port WPA to RTEMS-libbsd.



1.RTEMS doesn't support the PID and daemonize, so we need modify the os_unix.c.


a). Add the unistd.h


 #include <mach/mach_time.h>
 #endif /* __MACH__ */

+#ifdef __rtems__
+#include <unistd.h>
+#endif /* __rtems__ */
+
 #include "os.h"
 #include "common.h"


b). Return -1 for os_daemonize function.
We add defined(__rtems__) in os_daemonize funtion


 int os_daemonize(const char *pid_file)
 {
-#if defined(__uClinux__) || defined(__sun__)
+#if defined(__uClinux__) || defined(__sun__) || defined(__rtems__)
  return -1;
 #else /* defined(__uClinux__) || defined(__sun__) */
 #ifdef __FreeBSD__



2. RTEMS doesn't support the urandom, which is widely used in FreeBSD and Linux to generate the random number. So we need replace it with getentropy function, which is supported in RTEMS.


os_unix.c

int os_get_random(unsigned char *buf, size_t len)
{
FILE *f;
size_t rc;

if (TEST_FAIL())
return -1;

#ifdef __rtems__
    return getentropy(buf, len);
#else /* __rtems__ */
f = fopen("/dev/urandom", "rb");
if (f == NULL) {
printf("Could not open /dev/urandom.\n");
return -1;
}

rc = fread(buf, 1, len, f);
fclose(f);

return rc != len ? -1 : 0;
#endif /* __rtems__ */
}



3. Add rtems-bsd-commands.h in WPA config.h, so we can use wpa supplicant command in wpa main.c file.


 #include "wps/wps.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
-
+#ifdef __rtems__
+#include <machine/rtems-bsd-commands.h>
+#endif /* __rtems__ */



4. Add wpa_supplicant command function in wpa_supplicant main.c file. Because this file is the interface between OS and WPA, RTEMS control and use wpa supplicant via this file interface.


 #endif /* __linux__ */
 }

+#ifdef __rtems__
+#include <rtems/libio.h>
+
+static int
+main(int argc, char **argv);
+
+int rtems_bsd_command_wpa_supplicant(int argc, char **argv)
+{
+ int exit_code;
+
+ exit_code = rtems_bsd_program_call_main("wpa_supplicant", main, argc, argv);
+
+ return exit_code;
+}
+#endif /* __rtems__ */

 int main(int argc, char *argv[])
 {



5. Add WPA encryption algorithm module suppport in nexus-devices.h


 SYSINIT_MODULE_REFERENCE(wlan_sta);
 SYSINIT_MODULE_REFERENCE(wlan_amrr);
 SYSINIT_MODULE_REFERENCE(wlan_wep);
+SYSINIT_MODULE_REFERENCE(wlan_tkip);
+SYSINIT_MODULE_REFERENCE(wlan_ccmp);
 SYSINIT_REFERENCE(rtwn_rtl8188eufw);

The tkip and ccmp algorithm module is needed in WPA encryped WiFi. the ccmp is AES.



6. Write the wpa supplicant shell command files: rtems-bsd-shell-wpa_supplicant.c


+/*
+ * Copyright (c) 2017 Sichen Zhao.  All rights reserved.
+ *
+ *  <zsc19940506@gmail.com>
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ */
+
+#include <rtems/netcmds-config.h>
+#include <machine/rtems-bsd-commands.h>
+
+rtems_shell_cmd_t rtems_shell_WPA_SUPPLICANT_Command = {
+  .name = "wpa_supplicant",
+  .usage = "wpa_supplicant [args]",
+  .topic = "net",
+  .command = rtems_bsd_command_wpa_supplicant
+};


We can see from this file, in rtems_shell_WPA_SUPPLICANT_Command struct, i set the Command name: wpa_supplicant, and command function is rtems_bsd_command_wpa_supplicant. So when we use wpa_supplicant command in shell, we are go to the rtems_bsd_command_wpa_supplicant function.
And this rtems_bsd_command_wpa_supplicant function is implemented in wpa supplicant main.c on the above.
In order to let the rtems can find where the rtems_bsd_command_wpa_supplicant funcion are. We need state this function in rtemsbsd/include/machine/rtems-bsd-commands.h



 int rtems_bsd_command_dhcpcd(int argc, char **argv);

+int rtems_bsd_command_wpa_supplicant(int argc, char **argv);
+
 int rtems_bsd_command_tcpdump(int argc, char **argv);

 int rtems_bsd_command_sysctl(int argc, char **argv);

And for the same reason, we need state rtems_shell_WPA_SUPPLICANT_Command struct in rtemsbsd/include/rtems/netcmds-config.h to help the application find where the rtems_shell_WPA_SUPPLICANT_Command struct defined.


 extern rtems_shell_cmd_t rtems_shell_TCPDUMP_Command;

+extern rtems_shell_cmd_t rtems_shell_WPA_SUPPLICANT_Command;
+
 extern rtems_shell_cmd_t rtems_shell_SYSCTL_Command;

 extern rtems_shell_cmd_t rtems_shell_VMSTAT_Command;









评论

此博客中的热门博文

RTEMS-libbsd generates drive device firmware

RTEMS Network Transplantation - rtems system initialization process analysis

Add wpa_supplicant_fork command on RTEMS-libbsd