• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test_ip6.h"
2 
3 #include "lwip/ethip6.h"
4 #include "lwip/ip6.h"
5 #include "lwip/inet_chksum.h"
6 #include "lwip/nd6.h"
7 #include "lwip/stats.h"
8 #include "lwip/prot/ethernet.h"
9 #include "lwip/prot/ip.h"
10 #include "lwip/prot/ip6.h"
11 
12 #include "lwip/tcpip.h"
13 
14 #if LWIP_IPV6 /* allow to build the unit tests without IPv6 support */
15 
16 static struct netif test_netif6;
17 static int linkoutput_ctr;
18 
19 static err_t
default_netif_linkoutput(struct netif * netif,struct pbuf * p)20 default_netif_linkoutput(struct netif *netif, struct pbuf *p)
21 {
22   fail_unless(netif == &test_netif6);
23   fail_unless(p != NULL);
24   linkoutput_ctr++;
25   return ERR_OK;
26 }
27 
28 static err_t
default_netif_init(struct netif * netif)29 default_netif_init(struct netif *netif)
30 {
31   fail_unless(netif != NULL);
32   netif->linkoutput = default_netif_linkoutput;
33   netif->output_ip6 = ethip6_output;
34   netif->mtu = 1500;
35   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHERNET | NETIF_FLAG_MLD6;
36   netif->hwaddr_len = ETH_HWADDR_LEN;
37   return ERR_OK;
38 }
39 
40 static void
default_netif_add(void)41 default_netif_add(void)
42 {
43   struct netif *n;
44   fail_unless(netif_default == NULL);
45   n = netif_add_noaddr(&test_netif6, NULL, default_netif_init, NULL);
46   fail_unless(n == &test_netif6);
47   netif_set_default(&test_netif6);
48 }
49 
50 static void
default_netif_remove(void)51 default_netif_remove(void)
52 {
53   fail_unless(netif_default == &test_netif6);
54   netif_remove(&test_netif6);
55 }
56 
57 static void
ip6_test_handle_timers(int count)58 ip6_test_handle_timers(int count)
59 {
60   int i;
61   for (i = 0; i < count; i++) {
62     nd6_tmr();
63   }
64 }
65 
66 /* Setups/teardown functions */
67 
68 static void
ip6_setup(void)69 ip6_setup(void)
70 {
71   default_netif_add();
72   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
73 }
74 
75 static void
ip6_teardown(void)76 ip6_teardown(void)
77 {
78   if (netif_list->loop_first != NULL) {
79     pbuf_free(netif_list->loop_first);
80     netif_list->loop_first = NULL;
81   }
82   netif_list->loop_last = NULL;
83   /* poll until all memory is released... */
84   tcpip_thread_poll_one();
85   default_netif_remove();
86   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
87 }
88 
89 
90 /* Test functions */
91 
92 static void
test_ip6_ll_addr_iter(int expected_ctr1,int expected_ctr2)93 test_ip6_ll_addr_iter(int expected_ctr1, int expected_ctr2)
94 {
95   fail_unless(linkoutput_ctr == 0);
96 
97   /* test that nothing is sent with link uo but netif down */
98   netif_set_link_up(&test_netif6);
99   ip6_test_handle_timers(500);
100   fail_unless(linkoutput_ctr == 0);
101   netif_set_link_down(&test_netif6);
102   fail_unless(linkoutput_ctr == 0);
103 
104   /* test that nothing is sent with link down but netif up */
105   netif_set_up(&test_netif6);
106   ip6_test_handle_timers(500);
107   fail_unless(linkoutput_ctr == 0);
108   netif_set_down(&test_netif6);
109   fail_unless(linkoutput_ctr == 0);
110 
111   /* test what is sent with link up + netif up */
112   netif_set_link_up(&test_netif6);
113   netif_set_up(&test_netif6);
114   ip6_test_handle_timers(500);
115   fail_unless(linkoutput_ctr == expected_ctr1);
116   netif_set_down(&test_netif6);
117   netif_set_link_down(&test_netif6);
118   fail_unless(linkoutput_ctr == expected_ctr1);
119   linkoutput_ctr = 0;
120 
121   netif_set_up(&test_netif6);
122   netif_set_link_up(&test_netif6);
123   ip6_test_handle_timers(500);
124   fail_unless(linkoutput_ctr == expected_ctr2);
125   netif_set_link_down(&test_netif6);
126   netif_set_down(&test_netif6);
127   fail_unless(linkoutput_ctr == expected_ctr2);
128   linkoutput_ctr = 0;
129 }
130 
START_TEST(test_ip6_ll_addr)131 START_TEST(test_ip6_ll_addr)
132 {
133   LWIP_UNUSED_ARG(_i);
134 
135   /* test without link-local address */
136   test_ip6_ll_addr_iter(0, 0);
137 
138   /* test with link-local address */
139   netif_create_ip6_linklocal_address(&test_netif6, 1);
140   test_ip6_ll_addr_iter(3 + LWIP_IPV6_DUP_DETECT_ATTEMPTS + LWIP_IPV6_MLD, 3);
141 }
142 END_TEST
143 
START_TEST(test_ip6_aton_ipv4mapped)144 START_TEST(test_ip6_aton_ipv4mapped)
145 {
146   int ret;
147   ip_addr_t addr;
148   ip6_addr_t addr6;
149   const ip_addr_t addr_expected = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
150   const char *full_ipv6_addr = "0:0:0:0:0:FFFF:D4CC:65D2";
151   const char *shortened_ipv6_addr = "::FFFF:D4CC:65D2";
152   const char *full_ipv4_mapped_addr = "0:0:0:0:0:FFFF:212.204.101.210";
153   const char *shortened_ipv4_mapped_addr = "::FFFF:212.204.101.210";
154   const char *bogus_ipv4_mapped_addr = "::FFFF:212.204.101.2101";
155   LWIP_UNUSED_ARG(_i);
156 
157   /* check IPv6 representation */
158   memset(&addr6, 0, sizeof(addr6));
159   ret = ip6addr_aton(full_ipv6_addr, &addr6);
160   fail_unless(ret == 1);
161   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
162   memset(&addr, 0, sizeof(addr));
163   ret = ipaddr_aton(full_ipv6_addr, &addr);
164   fail_unless(ret == 1);
165   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
166 
167   /* check shortened IPv6 representation */
168   memset(&addr6, 0, sizeof(addr6));
169   ret = ip6addr_aton(shortened_ipv6_addr, &addr6);
170   fail_unless(ret == 1);
171   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
172   memset(&addr, 0, sizeof(addr));
173   ret = ipaddr_aton(shortened_ipv6_addr, &addr);
174   fail_unless(ret == 1);
175   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
176 
177   /* checked shortened mixed representation */
178   memset(&addr6, 0, sizeof(addr6));
179   ret = ip6addr_aton(shortened_ipv4_mapped_addr, &addr6);
180   fail_unless(ret == 1);
181   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
182   memset(&addr, 0, sizeof(addr));
183   ret = ipaddr_aton(shortened_ipv4_mapped_addr, &addr);
184   fail_unless(ret == 1);
185   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
186 
187   /* checked mixed representation */
188   memset(&addr6, 0, sizeof(addr6));
189   ret = ip6addr_aton(full_ipv4_mapped_addr, &addr6);
190   fail_unless(ret == 1);
191   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
192   memset(&addr, 0, sizeof(addr));
193   ret = ipaddr_aton(full_ipv4_mapped_addr, &addr);
194   fail_unless(ret == 1);
195   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
196 
197   /* checked bogus mixed representation */
198   memset(&addr6, 0, sizeof(addr6));
199   ret = ip6addr_aton(bogus_ipv4_mapped_addr, &addr6);
200   fail_unless(ret == 0);
201   memset(&addr, 0, sizeof(addr));
202   ret = ipaddr_aton(bogus_ipv4_mapped_addr, &addr);
203   fail_unless(ret == 0);
204 }
205 END_TEST
206 
START_TEST(test_ip6_ntoa_ipv4mapped)207 START_TEST(test_ip6_ntoa_ipv4mapped)
208 {
209   const ip_addr_t addr = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
210   char buf[128];
211   char *str;
212   LWIP_UNUSED_ARG(_i);
213 
214   str = ip6addr_ntoa_r(ip_2_ip6(&addr), buf, sizeof(buf));
215   fail_unless(str == buf);
216   fail_unless(!strcmp(str, "::FFFF:212.204.101.210"));
217 }
218 END_TEST
219 
220 struct test_addr_and_str {
221   ip_addr_t addr;
222   const char *str;
223 };
224 
START_TEST(test_ip6_ntoa)225 START_TEST(test_ip6_ntoa)
226 {
227   struct test_addr_and_str tests[] = {
228     {IPADDR6_INIT_HOST(0xfe800000, 0x00000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80::B2A1:A2FF:FEA3:A4A5"}, /* test shortened zeros */
229     {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80:0:FF00:0:B2A1:A2FF:FEA3:A4A5"}, /* don't omit single zero blocks */
230     {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2000000, 0x0000a4a5), "FE80:0:FF00:0:B200::A4A5"}, /* omit longest zero block */
231   };
232   char buf[128];
233   char *str;
234   size_t i;
235   LWIP_UNUSED_ARG(_i);
236 
237   for (i = 0; i < LWIP_ARRAYSIZE(tests); i++) {
238     str = ip6addr_ntoa_r(ip_2_ip6(&tests[i].addr), buf, sizeof(buf));
239     fail_unless(str == buf);
240     fail_unless(!strcmp(str, tests[i].str));
241   }
242 }
243 END_TEST
244 
START_TEST(test_ip6_lladdr)245 START_TEST(test_ip6_lladdr)
246 {
247   u8_t zeros[128];
248   const u8_t test_mac_addr[6] = {0xb0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5};
249   const u32_t expected_ip6_addr_1[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0xb2a1a2ff), PP_HTONL(0xfea3a4a5)};
250   const u32_t expected_ip6_addr_2[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0x0000b0a1), PP_HTONL(0xa2a3a4a5)};
251   LWIP_UNUSED_ARG(_i);
252   memset(zeros, 0, sizeof(zeros));
253 
254   fail_unless(test_netif6.hwaddr_len == 6);
255   fail_unless(!memcmp(test_netif6.hwaddr, zeros, 6));
256 
257   fail_unless(test_netif6.ip6_addr_state[0] == 0);
258   fail_unless(!memcmp(netif_ip6_addr(&test_netif6, 0), zeros, sizeof(ip6_addr_t)));
259 
260   /* set specific mac addr */
261   memcpy(test_netif6.hwaddr, test_mac_addr, 6);
262 
263   /* create link-local addr based on mac (EUI-48) */
264   netif_create_ip6_linklocal_address(&test_netif6, 1);
265   fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
266   fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_1, 16));
267 #if LWIP_IPV6_SCOPES
268   fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
269 #endif
270   /* reset address */
271   memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
272   test_netif6.ip6_addr_state[0] = 0;
273 
274   /* create link-local addr based interface ID */
275   netif_create_ip6_linklocal_address(&test_netif6, 0);
276   fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
277   fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_2, 16));
278 #if LWIP_IPV6_SCOPES
279   fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
280 #endif
281   /* reset address */
282   memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
283   test_netif6.ip6_addr_state[0] = 0;
284 
285   /* reset mac address */
286   memset(&test_netif6.hwaddr, 0, sizeof(test_netif6.hwaddr));
287 }
288 END_TEST
289 
290 /* Reproduces bug #57374 */
START_TEST(test_ip6_frag_pbuf_len_assert)291 START_TEST(test_ip6_frag_pbuf_len_assert)
292 {
293   ip_addr_t my_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x1);
294   ip_addr_t peer_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x4);
295   struct pbuf *payload, *hdr;
296   err_t err;
297   int i;
298 
299   /* Configure and enable local address */
300   test_netif6.mtu = 1500;
301   netif_set_up(&test_netif6);
302   netif_ip6_addr_set(&test_netif6, 0, ip_2_ip6(&my_addr));
303   netif_ip6_addr_set_state(&test_netif6, 0, IP6_ADDR_VALID);
304 
305   /* Create packet with lots of small pbufs around mtu limit */
306   payload = pbuf_alloc(PBUF_RAW, 1400, PBUF_POOL);
307   fail_unless(payload != NULL);
308   for (i = 0; i < 16; i++) {
309     struct pbuf *p = pbuf_alloc(PBUF_RAW, 32, PBUF_RAM);
310     fail_unless(p != NULL);
311     pbuf_cat(payload, p);
312   }
313   /* Prefix with header like UDP would */
314   hdr = pbuf_alloc(PBUF_IP, 8, PBUF_RAM);
315   fail_unless(hdr != NULL);
316   pbuf_chain(hdr, payload);
317 
318   /* Send it and don't crash while fragmenting */
319   err = ip6_output_if_src(hdr, ip_2_ip6(&my_addr), ip_2_ip6(&peer_addr), 15, 0, IP_PROTO_UDP, &test_netif6);
320   fail_unless(err == ERR_OK);
321 
322   pbuf_free(hdr);
323   pbuf_free(payload);
324 }
325 END_TEST
326 
327 /** Create the suite including all tests for this module */
328 Suite *
ip6_suite(void)329 ip6_suite(void)
330 {
331   testfunc tests[] = {
332     TESTFUNC(test_ip6_ll_addr),
333     TESTFUNC(test_ip6_aton_ipv4mapped),
334     TESTFUNC(test_ip6_ntoa_ipv4mapped),
335     TESTFUNC(test_ip6_ntoa),
336     TESTFUNC(test_ip6_lladdr),
337     TESTFUNC(test_ip6_frag_pbuf_len_assert)
338   };
339   return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), ip6_setup, ip6_teardown);
340 }
341 
342 #else /* LWIP_IPV6 */
343 
344 /* allow to build the unit tests without IPv6 support */
START_TEST(test_ip6_dummy)345 START_TEST(test_ip6_dummy)
346 {
347   LWIP_UNUSED_ARG(_i);
348 }
349 END_TEST
350 
351 Suite *
ip6_suite(void)352 ip6_suite(void)
353 {
354   testfunc tests[] = {
355     TESTFUNC(test_ip6_dummy),
356   };
357   return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
358 }
359 #endif /* LWIP_IPV6 */
360