1 /* Test the lifetime of RA. */
2
3 #include "nx_api.h"
4
5 extern void test_control_return(UINT status);
6
7 #if defined(FEATURE_NX_IPV6) && !defined(NX_DISABLE_ICMPV6_ROUTER_ADVERTISEMENT_PROCESS)
8
9 #define DEMO_STACK_SIZE 2048
10
11 /* Define the ThreadX and NetX object control blocks... */
12
13 static TX_THREAD thread_0;
14 static NX_PACKET_POOL pool_0;
15 static NX_IP ip_0;
16
17 /* Define the counters used in the demo application... */
18
19 static ULONG error_counter;
20
21 /* Define thread prototypes. */
22 static VOID thread_0_entry(ULONG thread_input);
23 extern VOID test_control_return(UINT status);
24 extern VOID _nx_ram_network_driver_1500(struct NX_IP_DRIVER_STRUCT *driver_req);
25
26 /* RA packet.
27 * router lifetime: 5s.
28 * prefix: 0x2001::/64. */
29 static char ra_pkt1[110] = {
30 0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* 33...... */
31 0x00, 0x00, 0x01, 0x00, 0x86, 0xdd, 0x60, 0x00, /* ......`. */
32 0x00, 0x00, 0x00, 0x38, 0x3a, 0xff, 0xfe, 0x80, /* ...8:... */
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, /* ........ */
34 0x00, 0xff, 0xfe, 0x00, 0x01, 0x00, 0xff, 0x02, /* ........ */
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, /* ........ */
37 0xd4, 0x71, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, /* .q...... */
38 0x75, 0x30, 0x00, 0x00, 0x03, 0xe8, 0x01, 0x01, /* u0...... */
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x04, /* ........ */
40 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09, /* @..'.... */
41 0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x20, 0x01, /* :..... . */
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* ...... */
44 };
45
46 /* RA packet.
47 * router lifetime: 6s.
48 * prefix: 0x2002::/64. */
49 static char ra_pkt2[110] = {
50 0x33, 0x33, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, /* 33...... */
51 0x00, 0x00, 0x01, 0x00, 0x86, 0xdd, 0x60, 0x00, /* ......`. */
52 0x00, 0x00, 0x00, 0x38, 0x3a, 0xff, 0xfe, 0x80, /* ...8:... */
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, /* ........ */
54 0x00, 0xff, 0xfe, 0x00, 0x02, 0x00, 0xff, 0x02, /* ........ */
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x86, 0x00, /* ........ */
57 0xd3, 0x6f, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, /* .o...... */
58 0x75, 0x30, 0x00, 0x00, 0x03, 0xe8, 0x01, 0x01, /* u0...... */
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, /* ........ */
60 0x40, 0xc0, 0x00, 0x27, 0x8d, 0x00, 0x00, 0x09, /* @..'.... */
61 0x3a, 0x80, 0x00, 0x00, 0x00, 0x00, 0x20, 0x02, /* :..... . */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* ...... */
64 };
65
66 /* Define what the initial system looks like. */
67
68 #ifdef CTEST
test_application_define(void * first_unused_memory)69 VOID test_application_define(void *first_unused_memory)
70 #else
71 void netx_icmpv6_ra_lifetime_test_application_define(void *first_unused_memory)
72 #endif
73 {
74 CHAR *pointer;
75 UINT status;
76
77 /* Setup the working pointer. */
78 pointer = (CHAR *) first_unused_memory;
79
80 /* Initialize the value. */
81 error_counter = 0;
82
83 /* Create the main thread. */
84 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
85 pointer, DEMO_STACK_SIZE,
86 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
87
88 pointer = pointer + DEMO_STACK_SIZE;
89
90 /* Initialize the NetX system. */
91 nx_system_initialize();
92
93 /* Create a packet pool. */
94 status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1536, pointer, 1536*16);
95 pointer = pointer + 1536*16;
96
97 if(status)
98 error_counter++;
99
100 /* Create an IP instance. */
101 status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1,2,3,4), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500,
102 pointer, 2048, 1);
103 pointer = pointer + 2048;
104
105 /* Enable IPv6 */
106 status += nxd_ipv6_enable(&ip_0);
107
108 /* Check IPv6 enable status. */
109 if(status)
110 error_counter++;
111
112 /* Enable IPv6 ICMP */
113 status += nxd_icmp_enable(&ip_0);
114
115 /* Check IPv6 ICMP enable status. */
116 if(status)
117 error_counter++;
118 }
119
120 /* Define the test threads. */
121
thread_0_entry(ULONG thread_input)122 static void thread_0_entry(ULONG thread_input)
123 {
124 UINT status;
125 NX_PACKET *packet_ptr;
126 UINT num_entries;
127
128 /* Print out test information banner. */
129 printf("NetX Test: ICMPv6 RA Lifetime Test...................................");
130
131 /* Check for earlier error. */
132 if(error_counter)
133 {
134 printf("ERROR!\n");
135 test_control_return(1);
136 }
137
138 /* Set the linklocal address. */
139 status = nxd_ipv6_address_set(&ip_0, 0, NX_NULL, 10, NX_NULL);
140
141 /* Check the status. */
142 if(status)
143 error_counter++;
144
145 /* Sleep 5 seconds for linklocal address DAD. */
146 tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
147
148 /* Inject RA packet. */
149 status = nx_packet_allocate(&pool_0, &packet_ptr, NX_PHYSICAL_HEADER, NX_WAIT_FOREVER);
150
151 /* Check status */
152 if(status)
153 error_counter++;
154
155 /* Fill in the packet with data. Skip the MAC header. */
156 memcpy(packet_ptr -> nx_packet_prepend_ptr, &ra_pkt1[14], sizeof(ra_pkt1) - 14);
157 packet_ptr -> nx_packet_length = sizeof(ra_pkt1) - 14;
158 packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + packet_ptr -> nx_packet_length;
159
160 /* Directly receive the RA packet. */
161 _nx_ip_packet_deferred_receive(&ip_0, packet_ptr);
162
163 /* Inject RA packet. */
164 status = nx_packet_allocate(&pool_0, &packet_ptr, NX_PHYSICAL_HEADER, NX_WAIT_FOREVER);
165
166 /* Check status */
167 if(status)
168 error_counter++;
169
170 /* Fill in the packet with data. Skip the MAC header. */
171 memcpy(packet_ptr -> nx_packet_prepend_ptr, &ra_pkt2[14], sizeof(ra_pkt2) - 14);
172 packet_ptr -> nx_packet_length = sizeof(ra_pkt2) - 14;
173 packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + packet_ptr -> nx_packet_length;
174
175 /* Directly receive the RA packet. */
176 _nx_ip_packet_deferred_receive(&ip_0, packet_ptr);
177
178
179 /* Check whether two routers are added. */
180 status = nxd_ipv6_default_router_number_of_entries_get(&ip_0, 0, &num_entries);
181
182 /* Check status */
183 if(status)
184 error_counter++;
185
186 if (num_entries != 2)
187 error_counter++;
188
189
190 /* Wait until router timeouts. */
191 tx_thread_sleep(10 * NX_IP_PERIODIC_RATE);
192
193
194 /* Check whether no routers are available. */
195 status = nxd_ipv6_default_router_number_of_entries_get(&ip_0, 0, &num_entries);
196
197 /* Check status */
198 if(status)
199 error_counter++;
200
201 if (num_entries != 0)
202 error_counter++;
203
204
205 /* Check the error. */
206 if(error_counter)
207 {
208 printf("ERROR!\n");
209 test_control_return(1);
210 }
211 else
212 {
213 printf("SUCCESS!\n");
214 test_control_return(0);
215 }
216 }
217 #else
218
219 #ifdef CTEST
test_application_define(void * first_unused_memory)220 VOID test_application_define(void *first_unused_memory)
221 #else
222 void netx_icmpv6_ra_lifetime_test_application_define(void *first_unused_memory)
223 #endif
224 {
225
226 /* Print out test information banner. */
227 printf("NetX Test: ICMPv6 RA Lifetime Test...................................N/A\n");
228 test_control_return(3);
229
230 }
231 #endif /* FEATURE_NX_IPV6 */
232