test_ip6.c 14 KB

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