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 | |
---|
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 | |
---|
51 | #include <rtems/bsd/sys/param.h> |
---|
52 | |
---|
53 | #include <assert.h> |
---|
54 | #include <ctype.h> |
---|
55 | #include <errno.h> |
---|
56 | #include <fcntl.h> |
---|
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) \ |
---|
76 | "ifconfig_" # iface "=\"inet " NET_CFG_SELF_IP " netmask " NET_CFG_NETMASK "\"\n" |
---|
77 | |
---|
78 | |
---|
79 | #define RC_CONF_IFACES \ |
---|
80 | IFACE_IPV4(dmc0) \ |
---|
81 | IFACE_IPV4(sm0) \ |
---|
82 | IFACE_IPV4(cgem0) \ |
---|
83 | IFACE_IPV4(fec0) \ |
---|
84 | IFACE_IPV4(em0) \ |
---|
85 | IFACE_IPV4(re0) |
---|
86 | |
---|
87 | #define IFACE_VLAN(iface) \ |
---|
88 | "vlans_" # iface "=\"101 102\"\n" \ |
---|
89 | "ifconfig_" # iface "_101=\"inet 192.0.101.1/24\"\n" \ |
---|
90 | "ifconfig_" # iface "_102=\"DHCP\"\n" |
---|
91 | |
---|
92 | #define RC_CONF_VLANS \ |
---|
93 | IFACE_VLAN(dmc0) \ |
---|
94 | IFACE_VLAN(sm0) \ |
---|
95 | IFACE_VLAN(cgem0) \ |
---|
96 | IFACE_VLAN(fec0) \ |
---|
97 | IFACE_VLAN(em0) \ |
---|
98 | IFACE_VLAN(re0) |
---|
99 | |
---|
100 | static const char* rc_conf_text = \ |
---|
101 | "#\n" \ |
---|
102 | "# Tests rc.conf. Add every NIC\n" \ |
---|
103 | "#\n" \ |
---|
104 | "\n" \ |
---|
105 | "syslog_priority=\"debug\"\n" \ |
---|
106 | "\n" \ |
---|
107 | "hostname=\"rctest\"\n" \ |
---|
108 | "\n" \ |
---|
109 | "create_args_myvlan=\"vlan 102\"\n" \ |
---|
110 | "create_args_yourvlan=\"vlan 202\"\n" \ |
---|
111 | "\n" \ |
---|
112 | RC_CONF_IFACES \ |
---|
113 | "\n" \ |
---|
114 | RC_CONF_VLANS \ |
---|
115 | "\n" \ |
---|
116 | "defaultrouter=\"" NET_CFG_GATEWAY_IP "\"\n" \ |
---|
117 | "defaultroute_delay=\"5\"\n" \ |
---|
118 | "\n" \ |
---|
119 | "dhcpcd_options=\"-h foobar\"\n" \ |
---|
120 | "\n" \ |
---|
121 | "telnetd_enable=\"YES\"\n" \ |
---|
122 | "telnetd_options=\"-v -C 10 -P 50 -L\"\n" \ |
---|
123 | "\n" \ |
---|
124 | "ftpd_enable=\"YES\"\n" \ |
---|
125 | "ftpd_options=\"-v -p 21 -C 10 -P 150 -L -I 10 -R /\"\n" \ |
---|
126 | "\n" \ |
---|
127 | "pf_enable=\"YES\"\n" \ |
---|
128 | "pf_rules=\"/etc/mypf.conf\"\n" \ |
---|
129 | "pf_flags=\"-q -z\"\n" \ |
---|
130 | "\n"; |
---|
131 | |
---|
132 | static const char* pf_conf_text = "pass all\n"; |
---|
133 | static const char* pf_os_text = "# empty\n"; |
---|
134 | |
---|
135 | static void |
---|
136 | prepare_files(void) |
---|
137 | { |
---|
138 | size_t len; |
---|
139 | size_t written; |
---|
140 | int fd; |
---|
141 | int rv; |
---|
142 | |
---|
143 | len = strlen(pf_conf_text); |
---|
144 | fd = open("/etc/mypf.conf", O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
145 | assert(fd != -1); |
---|
146 | written = write(fd, pf_conf_text, len); |
---|
147 | assert(written == len); |
---|
148 | rv = close(fd); |
---|
149 | assert(rv == 0); |
---|
150 | |
---|
151 | len = strlen(pf_os_text); |
---|
152 | fd = open("/etc/pf.os", O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
153 | assert(fd != -1); |
---|
154 | written = write(fd, pf_os_text, len); |
---|
155 | assert(written == len); |
---|
156 | rv = close(fd); |
---|
157 | assert(rv == 0); |
---|
158 | } |
---|
159 | |
---|
160 | static void |
---|
161 | test_rc_conf_script(void) |
---|
162 | { |
---|
163 | const char* ifconfg_args[] = { |
---|
164 | "ifconfig", NULL |
---|
165 | }; |
---|
166 | const char* netstat_args[] = { |
---|
167 | "netstat", "-rn", NULL |
---|
168 | }; |
---|
169 | const char* pfctl_args[] = { |
---|
170 | "pfctl", "-s", "rules", NULL |
---|
171 | }; |
---|
172 | |
---|
173 | printf("--------------- rc.conf -----------------\n"); |
---|
174 | printf(rc_conf_text); |
---|
175 | printf("-----------------------------------------\n"); |
---|
176 | |
---|
177 | assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_text, 15, true) == 0); |
---|
178 | |
---|
179 | printf("-------------- IFCONFIG -----------------\n"); |
---|
180 | rtems_bsd_command_ifconfig(1, (char**) ifconfg_args); |
---|
181 | printf("-------------- NETSTAT ------------------\n"); |
---|
182 | rtems_bsd_command_netstat(2, (char**) netstat_args); |
---|
183 | printf("-------------- PFCTL --------------------\n"); |
---|
184 | rtems_bsd_command_pfctl(RTEMS_BSD_ARGC(pfctl_args), (char **) pfctl_args); |
---|
185 | printf("-----------------------------------------\n"); |
---|
186 | } |
---|
187 | |
---|
188 | static void |
---|
189 | waiter(int fd, int secs, void *arg) |
---|
190 | { |
---|
191 | int* toggle = (int*) arg; |
---|
192 | const char* toggles = "|/-|\-"; |
---|
193 | printf("\b%c", toggles[*toggle]); |
---|
194 | fflush(stdout); |
---|
195 | ++(*toggle); |
---|
196 | if (*toggle >= 6) |
---|
197 | *toggle = 0; |
---|
198 | } |
---|
199 | |
---|
200 | static void |
---|
201 | shell(void) |
---|
202 | { |
---|
203 | int toggle = 1; |
---|
204 | rtems_status_code sc; |
---|
205 | printf("Press any key for the shell .... -"); |
---|
206 | fflush(stdout); |
---|
207 | sc = rtems_shell_wait_for_input(STDIN_FILENO, 10, waiter, &toggle); |
---|
208 | if (sc == RTEMS_SUCCESSFUL) { |
---|
209 | rtems_shell_init("SHLL", |
---|
210 | 32 * 1024, |
---|
211 | 1, |
---|
212 | CONSOLE_DEVICE_NAME, |
---|
213 | false, |
---|
214 | true, |
---|
215 | NULL); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | static void |
---|
220 | test_main(void) |
---|
221 | { |
---|
222 | prepare_files(); |
---|
223 | test_rc_conf_script(); |
---|
224 | shell(); |
---|
225 | exit(0); |
---|
226 | } |
---|
227 | |
---|
228 | #define CONFIGURE_SHELL_COMMANDS_INIT |
---|
229 | |
---|
230 | #include <bsp/irq-info.h> |
---|
231 | |
---|
232 | #include <rtems/netcmds-config.h> |
---|
233 | |
---|
234 | #define CONFIGURE_SHELL_USER_COMMANDS \ |
---|
235 | &bsp_interrupt_shell_command, \ |
---|
236 | &rtems_shell_BSD_Command, \ |
---|
237 | &rtems_shell_HOSTNAME_Command, \ |
---|
238 | &rtems_shell_PING_Command, \ |
---|
239 | &rtems_shell_ROUTE_Command, \ |
---|
240 | &rtems_shell_NETSTAT_Command, \ |
---|
241 | &rtems_shell_IFCONFIG_Command, \ |
---|
242 | &rtems_shell_TCPDUMP_Command, \ |
---|
243 | &rtems_shell_PFCTL_Command, \ |
---|
244 | &rtems_shell_SYSCTL_Command |
---|
245 | |
---|
246 | #define CONFIGURE_SHELL_COMMAND_CPUUSE |
---|
247 | #define CONFIGURE_SHELL_COMMAND_PERIODUSE |
---|
248 | #define CONFIGURE_SHELL_COMMAND_STACKUSE |
---|
249 | #define CONFIGURE_SHELL_COMMAND_PROFREPORT |
---|
250 | |
---|
251 | #define CONFIGURE_SHELL_COMMAND_CP |
---|
252 | #define CONFIGURE_SHELL_COMMAND_PWD |
---|
253 | #define CONFIGURE_SHELL_COMMAND_LS |
---|
254 | #define CONFIGURE_SHELL_COMMAND_LN |
---|
255 | #define CONFIGURE_SHELL_COMMAND_LSOF |
---|
256 | #define CONFIGURE_SHELL_COMMAND_CHDIR |
---|
257 | #define CONFIGURE_SHELL_COMMAND_CD |
---|
258 | #define CONFIGURE_SHELL_COMMAND_MKDIR |
---|
259 | #define CONFIGURE_SHELL_COMMAND_RMDIR |
---|
260 | #define CONFIGURE_SHELL_COMMAND_CAT |
---|
261 | #define CONFIGURE_SHELL_COMMAND_MV |
---|
262 | #define CONFIGURE_SHELL_COMMAND_RM |
---|
263 | #define CONFIGURE_SHELL_COMMAND_MALLOC_INFO |
---|
264 | #define CONFIGURE_SHELL_COMMAND_SHUTDOWN |
---|
265 | |
---|
266 | #include <rtems/shellconfig.h> |
---|
267 | |
---|
268 | #define RTEMS_BSD_CONFIG_BSP_CONFIG |
---|
269 | #define RTEMS_BSD_CONFIG_SERVICE_TELNETD |
---|
270 | #define RTEMS_BSD_CONFIG_TELNETD_STACK_SIZE (16 * 1024) |
---|
271 | #define RTEMS_BSD_CONFIG_SERVICE_FTPD |
---|
272 | #define RTEMS_BSD_CONFIG_FIREWALL_PF |
---|
273 | |
---|
274 | #define CONFIGURE_MAXIMUM_DRIVERS 32 |
---|
275 | |
---|
276 | #include <rtems/bsd/test/default-init.h> |
---|