[7439724] | 1 | /* |
---|
| 2 | * Copyright 2016 Chris Johns <chrisj@rtems.org> |
---|
| 3 | * |
---|
| 4 | * Redistribution and use in source and binary forms, with or without |
---|
| 5 | * modification, are permitted provided that the following conditions |
---|
| 6 | * are met: |
---|
| 7 | * 1. Redistributions of source code must retain the above copyright |
---|
| 8 | * notice, this list of conditions and the following disclaimer. |
---|
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 10 | * notice, this list of conditions and the following disclaimer in the |
---|
| 11 | * documentation and/or other materials provided with the distribution. |
---|
| 12 | * |
---|
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
| 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
| 17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 23 | * SUCH DAMAGE. |
---|
| 24 | */ |
---|
| 25 | |
---|
[573b1982] | 26 | /* |
---|
| 27 | * Tests: |
---|
| 28 | * |
---|
| 29 | * 1. rc.conf processing |
---|
| 30 | * 1.1 syslog_priority |
---|
| 31 | * 1.2 create_args_* |
---|
| 32 | * 1.3 hostname |
---|
| 33 | * 1.4 ifconfig_<iface> |
---|
| 34 | * 1.5 vlans_<iface> |
---|
| 35 | * 1.6 ifconfig_<iface>.<vlan> |
---|
| 36 | * 1.7 defaultrouter |
---|
| 37 | * 1.8 defaultroute_delay |
---|
| 38 | * 1.9 ftp_enable |
---|
| 39 | * 1.10 ftp_options |
---|
| 40 | * 1.11 dhcpcd_priority |
---|
| 41 | * 1.12 dhcpcd_options |
---|
| 42 | * |
---|
| 43 | * 2. dhcpcd (via vlan, should timeout unless VLAN is present) |
---|
| 44 | * |
---|
| 45 | * 3. get route, the defaultrouter sets a default route and the vlan DHCP |
---|
| 46 | * interface requires the default route be probed and found. |
---|
| 47 | * |
---|
| 48 | * 4. ftpd |
---|
| 49 | */ |
---|
| 50 | |
---|
[7439724] | 51 | #include <rtems/bsd/sys/param.h> |
---|
| 52 | |
---|
| 53 | #include <assert.h> |
---|
| 54 | #include <ctype.h> |
---|
| 55 | #include <errno.h> |
---|
[bc9e939] | 56 | #include <fcntl.h> |
---|
[7439724] | 57 | #include <string.h> |
---|
| 58 | #include <stdio.h> |
---|
| 59 | #include <stdlib.h> |
---|
| 60 | #include <sys/stat.h> |
---|
| 61 | #include <sysexits.h> |
---|
| 62 | #include <unistd.h> |
---|
| 63 | |
---|
| 64 | #include <machine/rtems-bsd-commands.h> |
---|
| 65 | #include <machine/rtems-bsd-rc-conf.h> |
---|
| 66 | #include <machine/rtems-bsd-rc-conf-services.h> |
---|
| 67 | |
---|
| 68 | #include <rtems/bsd/test/network-config.h> |
---|
| 69 | |
---|
| 70 | #include <rtems/console.h> |
---|
| 71 | #include <rtems/shell.h> |
---|
| 72 | |
---|
| 73 | #define TEST_NAME "LIBBSD RC.CONF 2" |
---|
| 74 | |
---|
| 75 | #define IFACE_IPV4(iface) \ |
---|
[573b1982] | 76 | "ifconfig_" # iface "=\"inet " NET_CFG_SELF_IP " netmask " NET_CFG_NETMASK "\"\n" |
---|
| 77 | |
---|
[4437573] | 78 | #define IFACE_ALIAS(iface) \ |
---|
| 79 | "ifconfig_" # iface "_alias0=\"ether 10:22:33:44:55:66\"\n" \ |
---|
| 80 | "ifconfig_" # iface "_alias1=\"inet 10.1.1.111 netmask 0xffffffff\"\n" |
---|
[7439724] | 81 | |
---|
[4437573] | 82 | #define RC_CONF_IFACES_IPV4 \ |
---|
[7439724] | 83 | IFACE_IPV4(dmc0) \ |
---|
| 84 | IFACE_IPV4(sm0) \ |
---|
| 85 | IFACE_IPV4(cgem0) \ |
---|
| 86 | IFACE_IPV4(fec0) \ |
---|
| 87 | IFACE_IPV4(em0) \ |
---|
| 88 | IFACE_IPV4(re0) |
---|
| 89 | |
---|
[4437573] | 90 | #define RC_CONF_IFACES_ALIAS \ |
---|
| 91 | IFACE_ALIAS(dmc0) \ |
---|
| 92 | IFACE_ALIAS(sm0) \ |
---|
| 93 | IFACE_ALIAS(cgem0) \ |
---|
| 94 | IFACE_ALIAS(fec0) \ |
---|
| 95 | IFACE_ALIAS(em0) \ |
---|
| 96 | IFACE_ALIAS(re0) |
---|
| 97 | |
---|
| 98 | #define RC_CONF_IFACES \ |
---|
| 99 | RC_CONF_IFACES_IPV4 \ |
---|
| 100 | RC_CONF_IFACES_ALIAS |
---|
| 101 | |
---|
[7439724] | 102 | #define IFACE_VLAN(iface) \ |
---|
| 103 | "vlans_" # iface "=\"101 102\"\n" \ |
---|
[573b1982] | 104 | "ifconfig_" # iface "_101=\"inet 192.0.101.1/24\"\n" \ |
---|
| 105 | "ifconfig_" # iface "_102=\"DHCP\"\n" |
---|
[7439724] | 106 | |
---|
| 107 | #define RC_CONF_VLANS \ |
---|
| 108 | IFACE_VLAN(dmc0) \ |
---|
| 109 | IFACE_VLAN(sm0) \ |
---|
| 110 | IFACE_VLAN(cgem0) \ |
---|
| 111 | IFACE_VLAN(fec0) \ |
---|
| 112 | IFACE_VLAN(em0) \ |
---|
| 113 | IFACE_VLAN(re0) |
---|
| 114 | |
---|
[573b1982] | 115 | static const char* rc_conf_text = \ |
---|
| 116 | "#\n" \ |
---|
| 117 | "# Tests rc.conf. Add every NIC\n" \ |
---|
| 118 | "#\n" \ |
---|
| 119 | "\n" \ |
---|
| 120 | "syslog_priority=\"debug\"\n" \ |
---|
| 121 | "\n" \ |
---|
| 122 | "hostname=\"rctest\"\n" \ |
---|
| 123 | "\n" \ |
---|
| 124 | "create_args_myvlan=\"vlan 102\"\n" \ |
---|
| 125 | "create_args_yourvlan=\"vlan 202\"\n" \ |
---|
| 126 | "\n" \ |
---|
| 127 | RC_CONF_IFACES \ |
---|
| 128 | "\n" \ |
---|
| 129 | RC_CONF_VLANS \ |
---|
| 130 | "\n" \ |
---|
| 131 | "defaultrouter=\"" NET_CFG_GATEWAY_IP "\"\n" \ |
---|
| 132 | "defaultroute_delay=\"5\"\n" \ |
---|
| 133 | "\n" \ |
---|
| 134 | "dhcpcd_options=\"-h foobar\"\n" \ |
---|
| 135 | "\n" \ |
---|
[65c65bb] | 136 | "telnetd_enable=\"YES\"\n" \ |
---|
| 137 | "telnetd_options=\"-v -C 10 -P 50 -L\"\n" \ |
---|
| 138 | "\n" \ |
---|
[573b1982] | 139 | "ftpd_enable=\"YES\"\n" \ |
---|
[90873cc] | 140 | "ftpd_options=\"-v -p 21 -C 10 -P 150 -L -I 10 -R /\"\n" \ |
---|
[bc9e939] | 141 | "\n" \ |
---|
| 142 | "pf_enable=\"YES\"\n" \ |
---|
| 143 | "pf_rules=\"/etc/mypf.conf\"\n" \ |
---|
| 144 | "pf_flags=\"-q -z\"\n" \ |
---|
| 145 | "\n"; |
---|
| 146 | |
---|
| 147 | static const char* pf_conf_text = "pass all\n"; |
---|
| 148 | static const char* pf_os_text = "# empty\n"; |
---|
| 149 | |
---|
| 150 | static void |
---|
| 151 | prepare_files(void) |
---|
| 152 | { |
---|
| 153 | size_t len; |
---|
| 154 | size_t written; |
---|
| 155 | int fd; |
---|
| 156 | int rv; |
---|
| 157 | |
---|
| 158 | len = strlen(pf_conf_text); |
---|
| 159 | fd = open("/etc/mypf.conf", O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
| 160 | assert(fd != -1); |
---|
| 161 | written = write(fd, pf_conf_text, len); |
---|
| 162 | assert(written == len); |
---|
| 163 | rv = close(fd); |
---|
| 164 | assert(rv == 0); |
---|
| 165 | |
---|
| 166 | len = strlen(pf_os_text); |
---|
| 167 | fd = open("/etc/pf.os", O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
| 168 | assert(fd != -1); |
---|
| 169 | written = write(fd, pf_os_text, len); |
---|
| 170 | assert(written == len); |
---|
| 171 | rv = close(fd); |
---|
| 172 | assert(rv == 0); |
---|
| 173 | } |
---|
[7439724] | 174 | |
---|
| 175 | static void |
---|
| 176 | test_rc_conf_script(void) |
---|
| 177 | { |
---|
| 178 | const char* ifconfg_args[] = { |
---|
| 179 | "ifconfig", NULL |
---|
| 180 | }; |
---|
| 181 | const char* netstat_args[] = { |
---|
| 182 | "netstat", "-rn", NULL |
---|
| 183 | }; |
---|
[bc9e939] | 184 | const char* pfctl_args[] = { |
---|
| 185 | "pfctl", "-s", "rules", NULL |
---|
| 186 | }; |
---|
[7439724] | 187 | |
---|
| 188 | printf("--------------- rc.conf -----------------\n"); |
---|
| 189 | printf(rc_conf_text); |
---|
| 190 | printf("-----------------------------------------\n"); |
---|
| 191 | |
---|
| 192 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_text, 15, true) == 0); |
---|
| 193 | |
---|
| 194 | printf("-------------- IFCONFIG -----------------\n"); |
---|
| 195 | rtems_bsd_command_ifconfig(1, (char**) ifconfg_args); |
---|
| 196 | printf("-------------- NETSTAT ------------------\n"); |
---|
| 197 | rtems_bsd_command_netstat(2, (char**) netstat_args); |
---|
[bc9e939] | 198 | printf("-------------- PFCTL --------------------\n"); |
---|
| 199 | rtems_bsd_command_pfctl(RTEMS_BSD_ARGC(pfctl_args), (char **) pfctl_args); |
---|
[7439724] | 200 | printf("-----------------------------------------\n"); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | static void |
---|
[4dec435] | 204 | waiter(int fd, int secs, void *arg) |
---|
[7439724] | 205 | { |
---|
[4dec435] | 206 | int* toggle = (int*) arg; |
---|
[335bb97] | 207 | const char* toggles = "|/-|\\-"; |
---|
[4dec435] | 208 | printf("\b%c", toggles[*toggle]); |
---|
| 209 | fflush(stdout); |
---|
| 210 | ++(*toggle); |
---|
| 211 | if (*toggle >= 6) |
---|
| 212 | *toggle = 0; |
---|
| 213 | } |
---|
[90873cc] | 214 | |
---|
[4dec435] | 215 | static void |
---|
| 216 | shell(void) |
---|
| 217 | { |
---|
| 218 | int toggle = 1; |
---|
| 219 | rtems_status_code sc; |
---|
| 220 | printf("Press any key for the shell .... -"); |
---|
| 221 | fflush(stdout); |
---|
| 222 | sc = rtems_shell_wait_for_input(STDIN_FILENO, 10, waiter, &toggle); |
---|
| 223 | if (sc == RTEMS_SUCCESSFUL) { |
---|
| 224 | rtems_shell_init("SHLL", |
---|
| 225 | 32 * 1024, |
---|
| 226 | 1, |
---|
| 227 | CONSOLE_DEVICE_NAME, |
---|
| 228 | false, |
---|
| 229 | true, |
---|
| 230 | NULL); |
---|
| 231 | } |
---|
| 232 | } |
---|
[90873cc] | 233 | |
---|
[4dec435] | 234 | static void |
---|
| 235 | test_main(void) |
---|
| 236 | { |
---|
[bc9e939] | 237 | prepare_files(); |
---|
[4dec435] | 238 | test_rc_conf_script(); |
---|
| 239 | shell(); |
---|
[7439724] | 240 | exit(0); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | #define CONFIGURE_SHELL_COMMANDS_INIT |
---|
| 244 | |
---|
| 245 | #include <bsp/irq-info.h> |
---|
| 246 | |
---|
| 247 | #include <rtems/netcmds-config.h> |
---|
| 248 | |
---|
| 249 | #define CONFIGURE_SHELL_USER_COMMANDS \ |
---|
| 250 | &bsp_interrupt_shell_command, \ |
---|
| 251 | &rtems_shell_HOSTNAME_Command, \ |
---|
| 252 | &rtems_shell_PING_Command, \ |
---|
| 253 | &rtems_shell_ROUTE_Command, \ |
---|
| 254 | &rtems_shell_NETSTAT_Command, \ |
---|
| 255 | &rtems_shell_IFCONFIG_Command, \ |
---|
| 256 | &rtems_shell_TCPDUMP_Command, \ |
---|
[bc9e939] | 257 | &rtems_shell_PFCTL_Command, \ |
---|
[7439724] | 258 | &rtems_shell_SYSCTL_Command |
---|
| 259 | |
---|
| 260 | #define CONFIGURE_SHELL_COMMAND_CPUUSE |
---|
| 261 | #define CONFIGURE_SHELL_COMMAND_PERIODUSE |
---|
| 262 | #define CONFIGURE_SHELL_COMMAND_STACKUSE |
---|
| 263 | #define CONFIGURE_SHELL_COMMAND_PROFREPORT |
---|
| 264 | |
---|
| 265 | #define CONFIGURE_SHELL_COMMAND_CP |
---|
| 266 | #define CONFIGURE_SHELL_COMMAND_PWD |
---|
| 267 | #define CONFIGURE_SHELL_COMMAND_LS |
---|
| 268 | #define CONFIGURE_SHELL_COMMAND_LN |
---|
| 269 | #define CONFIGURE_SHELL_COMMAND_LSOF |
---|
| 270 | #define CONFIGURE_SHELL_COMMAND_CHDIR |
---|
| 271 | #define CONFIGURE_SHELL_COMMAND_CD |
---|
| 272 | #define CONFIGURE_SHELL_COMMAND_MKDIR |
---|
| 273 | #define CONFIGURE_SHELL_COMMAND_RMDIR |
---|
| 274 | #define CONFIGURE_SHELL_COMMAND_CAT |
---|
| 275 | #define CONFIGURE_SHELL_COMMAND_MV |
---|
| 276 | #define CONFIGURE_SHELL_COMMAND_RM |
---|
| 277 | #define CONFIGURE_SHELL_COMMAND_MALLOC_INFO |
---|
| 278 | #define CONFIGURE_SHELL_COMMAND_SHUTDOWN |
---|
| 279 | |
---|
| 280 | #include <rtems/shellconfig.h> |
---|
[90873cc] | 281 | |
---|
| 282 | #define RTEMS_BSD_CONFIG_BSP_CONFIG |
---|
[65c65bb] | 283 | #define RTEMS_BSD_CONFIG_SERVICE_TELNETD |
---|
| 284 | #define RTEMS_BSD_CONFIG_TELNETD_STACK_SIZE (16 * 1024) |
---|
[90873cc] | 285 | #define RTEMS_BSD_CONFIG_SERVICE_FTPD |
---|
[bc9e939] | 286 | #define RTEMS_BSD_CONFIG_FIREWALL_PF |
---|
[7439724] | 287 | |
---|
[65c65bb] | 288 | #define CONFIGURE_MAXIMUM_DRIVERS 32 |
---|
| 289 | |
---|
[7439724] | 290 | #include <rtems/bsd/test/default-init.h> |
---|