test_dhcp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. #include "test_dhcp.h"
  2. #include "lwip/netif.h"
  3. #include "lwip/dhcp.h"
  4. #include "lwip/prot/dhcp.h"
  5. #include "lwip/etharp.h"
  6. #include "netif/ethernet.h"
  7. struct netif net_test;
  8. static const u8_t broadcast[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  9. static const u8_t magic_cookie[] = { 0x63, 0x82, 0x53, 0x63 };
  10. static u8_t dhcp_offer[] = {
  11. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* To unit */
  12. 0x00, 0x0F, 0xEE, 0x30, 0xAB, 0x22, /* From Remote host */
  13. 0x08, 0x00, /* Protocol: IP */
  14. 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x36, 0xcc, 0xc3, 0xaa, 0xbd, 0xab, 0xc3, 0xaa, 0xbd, 0xc8, /* IP header */
  15. 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, 0x00, 0x00, /* UDP header */
  16. 0x02, /* Type == Boot reply */
  17. 0x01, 0x06, /* Hw Ethernet, 6 bytes addrlen */
  18. 0x00, /* 0 hops */
  19. 0xAA, 0xAA, 0xAA, 0xAA, /* Transaction id, will be overwritten */
  20. 0x00, 0x00, /* 0 seconds elapsed */
  21. 0x00, 0x00, /* Flags (unicast) */
  22. 0x00, 0x00, 0x00, 0x00, /* Client ip */
  23. 0xc3, 0xaa, 0xbd, 0xc8, /* Your IP */
  24. 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server ip */
  25. 0x00, 0x00, 0x00, 0x00, /* relay agent */
  26. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* MAC addr + padding */
  27. /* Empty server name and boot file name */
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00,
  40. 0x63, 0x82, 0x53, 0x63, /* Magic cookie */
  41. 0x35, 0x01, 0x02, /* Message type: Offer */
  42. 0x36, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Server identifier (IP) */
  43. 0x33, 0x04, 0x00, 0x00, 0x00, 0x78, /* Lease time 2 minutes */
  44. 0x03, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Router IP */
  45. 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, /* Subnet mask */
  46. 0xff, /* End option */
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Padding */
  49. };
  50. static u8_t dhcp_ack[] = {
  51. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* To unit */
  52. 0x00, 0x0f, 0xEE, 0x30, 0xAB, 0x22, /* From remote host */
  53. 0x08, 0x00, /* Proto IP */
  54. 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x36, 0xcc, 0xc3, 0xaa, 0xbd, 0xab, 0xc3, 0xaa, 0xbd, 0xc8, /* IP header */
  55. 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, 0x00, 0x00, /* UDP header */
  56. 0x02, /* Bootp reply */
  57. 0x01, 0x06, /* Hw type Eth, len 6 */
  58. 0x00, /* 0 hops */
  59. 0xAA, 0xAA, 0xAA, 0xAA,
  60. 0x00, 0x00, /* 0 seconds elapsed */
  61. 0x00, 0x00, /* Flags (unicast) */
  62. 0x00, 0x00, 0x00, 0x00, /* Client IP */
  63. 0xc3, 0xaa, 0xbd, 0xc8, /* Your IP */
  64. 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server IP */
  65. 0x00, 0x00, 0x00, 0x00, /* Relay agent */
  66. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Macaddr + padding */
  67. /* Empty server name and boot file name */
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00,
  80. 0x63, 0x82, 0x53, 0x63, /* Magic cookie */
  81. 0x35, 0x01, 0x05, /* Dhcp message type ack */
  82. 0x36, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server identifier */
  83. 0x33, 0x04, 0x00, 0x00, 0x00, 0x78, /* Lease time 2 minutes */
  84. 0x03, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Router IP */
  85. 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, /* Netmask */
  86. 0xff, /* End marker */
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Padding */
  89. };
  90. static const u8_t arpreply[] = {
  91. 0x00, 0x23, 0xC1, 0xDE, 0xD0, 0x0D, /* dst mac */
  92. 0x00, 0x32, 0x44, 0x20, 0x01, 0x02, /* src mac */
  93. 0x08, 0x06, /* proto arp */
  94. 0x00, 0x01, /* hw eth */
  95. 0x08, 0x00, /* proto ip */
  96. 0x06, /* hw addr len 6 */
  97. 0x04, /* proto addr len 4 */
  98. 0x00, 0x02, /* arp reply */
  99. 0x00, 0x32, 0x44, 0x20, 0x01, 0x02, /* sender mac */
  100. 0xc3, 0xaa, 0xbd, 0xc8, /* sender ip */
  101. 0x00, 0x23, 0xC1, 0xDE, 0xD0, 0x0D, /* target mac */
  102. 0x00, 0x00, 0x00, 0x00, /* target ip */
  103. };
  104. static int txpacket;
  105. static enum tcase {
  106. TEST_LWIP_DHCP,
  107. TEST_LWIP_DHCP_NAK,
  108. TEST_LWIP_DHCP_RELAY,
  109. TEST_LWIP_DHCP_NAK_NO_ENDMARKER,
  110. TEST_LWIP_DHCP_INVALID_OVERLOAD,
  111. TEST_NONE
  112. } tcase;
  113. static int debug = 0;
  114. static void setdebug(int a) {debug = a;}
  115. static int tick = 0;
  116. static void tick_lwip(void)
  117. {
  118. tick++;
  119. if (tick % 5 == 0) {
  120. dhcp_fine_tmr();
  121. }
  122. if (tick % 600 == 0) {
  123. dhcp_coarse_tmr();
  124. }
  125. }
  126. static void send_pkt(struct netif *netif, const u8_t *data, size_t len)
  127. {
  128. struct pbuf *p, *q;
  129. LWIP_ASSERT("pkt too big", len <= 0xFFFF);
  130. p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
  131. if (debug) {
  132. /* Dump data */
  133. u32_t i;
  134. printf("RX data (len %d)", p->tot_len);
  135. for (i = 0; i < len; i++) {
  136. printf(" %02X", data[i]);
  137. }
  138. printf("\n");
  139. }
  140. fail_unless(p != NULL);
  141. for(q = p; q != NULL; q = q->next) {
  142. memcpy(q->payload, data, q->len);
  143. data += q->len;
  144. }
  145. netif->input(p, netif);
  146. }
  147. static err_t lwip_tx_func(struct netif *netif, struct pbuf *p);
  148. static err_t testif_init(struct netif *netif)
  149. {
  150. netif->name[0] = 'c';
  151. netif->name[1] = 'h';
  152. netif->output = etharp_output;
  153. netif->linkoutput = lwip_tx_func;
  154. netif->mtu = 1500;
  155. netif->hwaddr_len = 6;
  156. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  157. netif->hwaddr[0] = 0x00;
  158. netif->hwaddr[1] = 0x23;
  159. netif->hwaddr[2] = 0xC1;
  160. netif->hwaddr[3] = 0xDE;
  161. netif->hwaddr[4] = 0xD0;
  162. netif->hwaddr[5] = 0x0D;
  163. return ERR_OK;
  164. }
  165. static void dhcp_setup(void)
  166. {
  167. txpacket = 0;
  168. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  169. }
  170. static void dhcp_teardown(void)
  171. {
  172. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  173. }
  174. static void check_pkt(struct pbuf *p, u32_t pos, const u8_t *mem, u32_t len)
  175. {
  176. u8_t *data;
  177. fail_if((pos + len) > p->tot_len);
  178. while (pos > p->len && p->next) {
  179. pos -= p->len;
  180. p = p->next;
  181. }
  182. fail_if(p == NULL);
  183. fail_unless(pos + len <= p->len); /* All data we seek within same pbuf */
  184. data = (u8_t*)p->payload;
  185. fail_if(memcmp(&data[pos], mem, len), "data at pos %d, len %d in packet %d did not match", pos, len, txpacket);
  186. }
  187. static void check_pkt_fuzzy(struct pbuf *p, u32_t startpos, const u8_t *mem, u32_t len)
  188. {
  189. int found;
  190. u32_t i;
  191. u8_t *data;
  192. fail_if((startpos + len) > p->tot_len);
  193. while (startpos > p->len && p->next) {
  194. startpos -= p->len;
  195. p = p->next;
  196. }
  197. fail_if(p == NULL);
  198. fail_unless(startpos + len <= p->len); /* All data we seek within same pbuf */
  199. found = 0;
  200. data = (u8_t*)p->payload;
  201. for (i = startpos; i <= (p->len - len); i++) {
  202. if (memcmp(&data[i], mem, len) == 0) {
  203. found = 1;
  204. break;
  205. }
  206. }
  207. fail_unless(found);
  208. }
  209. static err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
  210. {
  211. fail_unless(netif == &net_test);
  212. txpacket++;
  213. if (debug) {
  214. struct pbuf *pp = p;
  215. /* Dump data */
  216. printf("TX data (pkt %d, len %d, tick %d)", txpacket, p->tot_len, tick);
  217. do {
  218. int i;
  219. for (i = 0; i < pp->len; i++) {
  220. printf(" %02X", ((u8_t *) pp->payload)[i]);
  221. }
  222. if (pp->next) {
  223. pp = pp->next;
  224. }
  225. } while (pp->next);
  226. printf("\n");
  227. }
  228. switch (tcase) {
  229. case TEST_LWIP_DHCP:
  230. switch (txpacket) {
  231. case 1:
  232. case 2:
  233. {
  234. const u8_t ipproto[] = { 0x08, 0x00 };
  235. const u8_t bootp_start[] = { 0x01, 0x01, 0x06, 0x00}; /* bootp request, eth, hwaddr len 6, 0 hops */
  236. const u8_t ipaddrs[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  237. check_pkt(p, 0, broadcast, 6); /* eth level dest: broadcast */
  238. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  239. check_pkt(p, 12, ipproto, sizeof(ipproto)); /* eth level proto: ip */
  240. check_pkt(p, 42, bootp_start, sizeof(bootp_start));
  241. check_pkt(p, 53, ipaddrs, sizeof(ipaddrs));
  242. check_pkt(p, 70, netif->hwaddr, 6); /* mac addr inside bootp */
  243. check_pkt(p, 278, magic_cookie, sizeof(magic_cookie));
  244. /* Check dchp message type, can be at different positions */
  245. if (txpacket == 1) {
  246. u8_t dhcp_discover_opt[] = { 0x35, 0x01, 0x01 };
  247. check_pkt_fuzzy(p, 282, dhcp_discover_opt, sizeof(dhcp_discover_opt));
  248. } else if (txpacket == 2) {
  249. u8_t dhcp_request_opt[] = { 0x35, 0x01, 0x03 };
  250. u8_t requested_ipaddr[] = { 0x32, 0x04, 0xc3, 0xaa, 0xbd, 0xc8 }; /* Ask for offered IP */
  251. check_pkt_fuzzy(p, 282, dhcp_request_opt, sizeof(dhcp_request_opt));
  252. check_pkt_fuzzy(p, 282, requested_ipaddr, sizeof(requested_ipaddr));
  253. }
  254. break;
  255. }
  256. case 3:
  257. case 4:
  258. case 5:
  259. {
  260. const u8_t arpproto[] = { 0x08, 0x06 };
  261. check_pkt(p, 0, broadcast, 6); /* eth level dest: broadcast */
  262. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  263. check_pkt(p, 12, arpproto, sizeof(arpproto)); /* eth level proto: ip */
  264. break;
  265. }
  266. default:
  267. fail();
  268. break;
  269. }
  270. break;
  271. case TEST_LWIP_DHCP_NAK:
  272. {
  273. const u8_t ipproto[] = { 0x08, 0x00 };
  274. const u8_t bootp_start[] = { 0x01, 0x01, 0x06, 0x00}; /* bootp request, eth, hwaddr len 6, 0 hops */
  275. const u8_t ipaddrs[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  276. const u8_t dhcp_nak_opt[] = { 0x35, 0x01, 0x04 };
  277. const u8_t requested_ipaddr[] = { 0x32, 0x04, 0xc3, 0xaa, 0xbd, 0xc8 }; /* offered IP */
  278. fail_unless(txpacket == 4);
  279. check_pkt(p, 0, broadcast, 6); /* eth level dest: broadcast */
  280. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  281. check_pkt(p, 12, ipproto, sizeof(ipproto)); /* eth level proto: ip */
  282. check_pkt(p, 42, bootp_start, sizeof(bootp_start));
  283. check_pkt(p, 53, ipaddrs, sizeof(ipaddrs));
  284. check_pkt(p, 70, netif->hwaddr, 6); /* mac addr inside bootp */
  285. check_pkt(p, 278, magic_cookie, sizeof(magic_cookie));
  286. check_pkt_fuzzy(p, 282, dhcp_nak_opt, sizeof(dhcp_nak_opt)); /* NAK the ack */
  287. check_pkt_fuzzy(p, 282, requested_ipaddr, sizeof(requested_ipaddr));
  288. break;
  289. }
  290. case TEST_LWIP_DHCP_RELAY:
  291. switch (txpacket) {
  292. case 1:
  293. case 2:
  294. {
  295. const u8_t ipproto[] = { 0x08, 0x00 };
  296. const u8_t bootp_start[] = { 0x01, 0x01, 0x06, 0x00}; /* bootp request, eth, hwaddr len 6, 0 hops */
  297. const u8_t ipaddrs[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  298. check_pkt(p, 0, broadcast, 6); /* eth level dest: broadcast */
  299. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  300. check_pkt(p, 12, ipproto, sizeof(ipproto)); /* eth level proto: ip */
  301. check_pkt(p, 42, bootp_start, sizeof(bootp_start));
  302. check_pkt(p, 53, ipaddrs, sizeof(ipaddrs));
  303. check_pkt(p, 70, netif->hwaddr, 6); /* mac addr inside bootp */
  304. check_pkt(p, 278, magic_cookie, sizeof(magic_cookie));
  305. /* Check dchp message type, can be at different positions */
  306. if (txpacket == 1) {
  307. u8_t dhcp_discover_opt[] = { 0x35, 0x01, 0x01 };
  308. check_pkt_fuzzy(p, 282, dhcp_discover_opt, sizeof(dhcp_discover_opt));
  309. } else if (txpacket == 2) {
  310. u8_t dhcp_request_opt[] = { 0x35, 0x01, 0x03 };
  311. u8_t requested_ipaddr[] = { 0x32, 0x04, 0x4f, 0x8a, 0x33, 0x05 }; /* Ask for offered IP */
  312. check_pkt_fuzzy(p, 282, dhcp_request_opt, sizeof(dhcp_request_opt));
  313. check_pkt_fuzzy(p, 282, requested_ipaddr, sizeof(requested_ipaddr));
  314. }
  315. break;
  316. }
  317. case 3:
  318. case 4:
  319. case 5:
  320. case 6:
  321. {
  322. const u8_t arpproto[] = { 0x08, 0x06 };
  323. check_pkt(p, 0, broadcast, 6); /* eth level dest: broadcast */
  324. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  325. check_pkt(p, 12, arpproto, sizeof(arpproto)); /* eth level proto: ip */
  326. break;
  327. }
  328. case 7:
  329. {
  330. const u8_t fake_arp[6] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xab };
  331. const u8_t ipproto[] = { 0x08, 0x00 };
  332. const u8_t bootp_start[] = { 0x01, 0x01, 0x06, 0x00}; /* bootp request, eth, hwaddr len 6, 0 hops */
  333. const u8_t ipaddrs[] = { 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  334. const u8_t dhcp_request_opt[] = { 0x35, 0x01, 0x03 };
  335. check_pkt(p, 0, fake_arp, 6); /* eth level dest: broadcast */
  336. check_pkt(p, 6, netif->hwaddr, 6); /* eth level src: unit mac */
  337. check_pkt(p, 12, ipproto, sizeof(ipproto)); /* eth level proto: ip */
  338. check_pkt(p, 42, bootp_start, sizeof(bootp_start));
  339. check_pkt(p, 53, ipaddrs, sizeof(ipaddrs));
  340. check_pkt(p, 70, netif->hwaddr, 6); /* mac addr inside bootp */
  341. check_pkt(p, 278, magic_cookie, sizeof(magic_cookie));
  342. /* Check dchp message type, can be at different positions */
  343. check_pkt_fuzzy(p, 282, dhcp_request_opt, sizeof(dhcp_request_opt));
  344. break;
  345. }
  346. default:
  347. fail();
  348. break;
  349. }
  350. break;
  351. default:
  352. break;
  353. }
  354. return ERR_OK;
  355. }
  356. /*
  357. * Test basic happy flow DHCP session.
  358. * Validate that xid is checked.
  359. */
  360. START_TEST(test_dhcp)
  361. {
  362. ip4_addr_t addr;
  363. ip4_addr_t netmask;
  364. ip4_addr_t gw;
  365. int i;
  366. u32_t xid;
  367. LWIP_UNUSED_ARG(_i);
  368. tcase = TEST_LWIP_DHCP;
  369. setdebug(0);
  370. IP4_ADDR(&addr, 0, 0, 0, 0);
  371. IP4_ADDR(&netmask, 0, 0, 0, 0);
  372. IP4_ADDR(&gw, 0, 0, 0, 0);
  373. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  374. netif_set_up(&net_test);
  375. dhcp_start(&net_test);
  376. fail_unless(txpacket == 1); /* DHCP discover sent */
  377. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  378. memcpy(&dhcp_offer[46], &xid, 4);
  379. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  380. /* IP addresses should be zero */
  381. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  382. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  383. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  384. fail_unless(txpacket == 1, "TX %d packets, expected 1", txpacket); /* Nothing more sent */
  385. xid = htonl(netif_dhcp_data(&net_test)->xid);
  386. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  387. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  388. fail_unless(txpacket == 2, "TX %d packets, expected 2", txpacket); /* DHCP request sent */
  389. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  390. memcpy(&dhcp_ack[46], &xid, 4);
  391. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  392. fail_unless(txpacket == 2, "TX %d packets, still expected 2", txpacket); /* No more sent */
  393. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  394. memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */
  395. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  396. for (i = 0; i < 20; i++) {
  397. tick_lwip();
  398. }
  399. fail_unless(txpacket == 5, "TX %d packets, expected 5", txpacket); /* ARP requests sent */
  400. /* Interface up */
  401. fail_unless(netif_is_up(&net_test));
  402. /* Now it should have taken the IP */
  403. IP4_ADDR(&addr, 195, 170, 189, 200);
  404. IP4_ADDR(&netmask, 255, 255, 255, 0);
  405. IP4_ADDR(&gw, 195, 170, 189, 171);
  406. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  407. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  408. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  409. tcase = TEST_NONE;
  410. dhcp_stop(&net_test);
  411. dhcp_cleanup(&net_test);
  412. netif_remove(&net_test);
  413. }
  414. END_TEST
  415. /*
  416. * Test that IP address is not taken and NAK is sent if someone
  417. * replies to ARP requests for the offered address.
  418. */
  419. START_TEST(test_dhcp_nak)
  420. {
  421. ip4_addr_t addr;
  422. ip4_addr_t netmask;
  423. ip4_addr_t gw;
  424. u32_t xid;
  425. LWIP_UNUSED_ARG(_i);
  426. tcase = TEST_LWIP_DHCP;
  427. setdebug(0);
  428. IP4_ADDR(&addr, 0, 0, 0, 0);
  429. IP4_ADDR(&netmask, 0, 0, 0, 0);
  430. IP4_ADDR(&gw, 0, 0, 0, 0);
  431. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  432. netif_set_up(&net_test);
  433. dhcp_start(&net_test);
  434. fail_unless(txpacket == 1); /* DHCP discover sent */
  435. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  436. memcpy(&dhcp_offer[46], &xid, 4);
  437. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  438. /* IP addresses should be zero */
  439. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  440. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  441. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  442. fail_unless(txpacket == 1); /* Nothing more sent */
  443. xid = htonl(netif_dhcp_data(&net_test)->xid);
  444. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  445. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  446. fail_unless(txpacket == 2); /* DHCP request sent */
  447. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  448. memcpy(&dhcp_ack[46], &xid, 4);
  449. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  450. fail_unless(txpacket == 2); /* No more sent */
  451. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  452. memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */
  453. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  454. fail_unless(txpacket == 3); /* ARP request sent */
  455. tcase = TEST_LWIP_DHCP_NAK; /* Switch testcase */
  456. /* Send arp reply, mark offered IP as taken */
  457. send_pkt(&net_test, arpreply, sizeof(arpreply));
  458. fail_unless(txpacket == 4); /* DHCP nak sent */
  459. tcase = TEST_NONE;
  460. dhcp_stop(&net_test);
  461. dhcp_cleanup(&net_test);
  462. netif_remove(&net_test);
  463. }
  464. END_TEST
  465. /*
  466. * Test case based on captured data where
  467. * replies are sent from a different IP than the
  468. * one the client unicasted to.
  469. */
  470. START_TEST(test_dhcp_relayed)
  471. {
  472. u8_t relay_offer[] = {
  473. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d,
  474. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60,
  475. 0x08, 0x00, 0x45, 0x00,
  476. 0x01, 0x38, 0xfd, 0x53, 0x00, 0x00, 0x40, 0x11,
  477. 0x78, 0x46, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  478. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  479. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35,
  480. 0xb6, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  481. 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  482. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  483. 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00,
  484. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  485. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  486. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  487. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  488. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  490. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  491. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  493. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  494. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  495. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  496. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  497. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  498. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  499. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  500. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  501. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  502. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  503. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  504. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  506. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  509. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  510. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  511. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  512. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  513. 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x02, 0x36,
  514. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff
  515. };
  516. u8_t relay_ack1[] = {
  517. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x22,
  518. 0x93, 0x5a, 0xf7, 0x60, 0x08, 0x00, 0x45, 0x00,
  519. 0x01, 0x38, 0xfd, 0x55, 0x00, 0x00, 0x40, 0x11,
  520. 0x78, 0x44, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  521. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  522. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35,
  523. 0xb6, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  524. 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  525. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  526. 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00,
  527. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  528. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  529. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  530. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  531. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  532. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  533. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  534. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  535. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  536. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  537. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  538. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  539. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  540. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  541. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  542. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  543. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  544. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  545. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  546. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  547. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  548. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  549. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  550. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  551. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  552. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  553. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  554. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  555. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  556. 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x05, 0x36,
  557. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff
  558. };
  559. u8_t relay_ack2[] = {
  560. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d,
  561. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60,
  562. 0x08, 0x00, 0x45, 0x00,
  563. 0x01, 0x38, 0xfa, 0x18, 0x00, 0x00, 0x40, 0x11,
  564. 0x7b, 0x81, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  565. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  566. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x49, 0x8b,
  567. 0x6e, 0xab, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x8a,
  568. 0x33, 0x05, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  569. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  570. 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00,
  571. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  572. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  573. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  574. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  575. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  576. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  577. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  578. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  579. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  580. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  581. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  582. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  583. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  584. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  585. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  586. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  587. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  588. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  589. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  590. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  591. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  592. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  593. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  594. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  596. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  597. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  598. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  599. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  600. 0x00, 0x00, 0x54, 0x60, 0x35, 0x01, 0x05, 0x36,
  601. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff };
  602. const u8_t arp_resp[] = {
  603. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* DEST */
  604. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, /* SRC */
  605. 0x08, 0x06, /* Type: ARP */
  606. 0x00, 0x01, /* HW: Ethernet */
  607. 0x08, 0x00, /* PROTO: IP */
  608. 0x06, /* HW size */
  609. 0x04, /* PROTO size */
  610. 0x00, 0x02, /* OPCODE: Reply */
  611. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xab, /* Target MAC */
  612. 0x4f, 0x8a, 0x32, 0x01, /* Target IP */
  613. 0x00, 0x23, 0xc1, 0x00, 0x06, 0x50, /* src mac */
  614. 0x4f, 0x8a, 0x33, 0x05, /* src ip */
  615. /* Padding follows.. */
  616. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  617. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  618. 0x00, 0x00, 0x00, 0x00 };
  619. ip4_addr_t addr;
  620. ip4_addr_t netmask;
  621. ip4_addr_t gw;
  622. int i;
  623. u32_t xid;
  624. LWIP_UNUSED_ARG(_i);
  625. tcase = TEST_LWIP_DHCP_RELAY;
  626. setdebug(0);
  627. IP4_ADDR(&addr, 0, 0, 0, 0);
  628. IP4_ADDR(&netmask, 0, 0, 0, 0);
  629. IP4_ADDR(&gw, 0, 0, 0, 0);
  630. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  631. netif_set_up(&net_test);
  632. dhcp_start(&net_test);
  633. fail_unless(txpacket == 1); /* DHCP discover sent */
  634. /* IP addresses should be zero */
  635. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  636. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  637. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  638. fail_unless(txpacket == 1); /* Nothing more sent */
  639. xid = htonl(netif_dhcp_data(&net_test)->xid);
  640. memcpy(&relay_offer[46], &xid, 4); /* insert correct transaction id */
  641. send_pkt(&net_test, relay_offer, sizeof(relay_offer));
  642. /* request sent? */
  643. fail_unless(txpacket == 2, "txpkt = %d, should be 2", txpacket);
  644. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  645. memcpy(&relay_ack1[46], &xid, 4); /* insert transaction id */
  646. send_pkt(&net_test, relay_ack1, sizeof(relay_ack1));
  647. for (i = 0; i < 25; i++) {
  648. tick_lwip();
  649. }
  650. fail_unless(txpacket == 5, "txpkt should be 5, is %d", txpacket); /* ARP requests sent */
  651. /* Interface up */
  652. fail_unless(netif_is_up(&net_test));
  653. /* Now it should have taken the IP */
  654. IP4_ADDR(&addr, 79, 138, 51, 5);
  655. IP4_ADDR(&netmask, 255, 255, 254, 0);
  656. IP4_ADDR(&gw, 79, 138, 50, 1);
  657. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  658. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  659. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  660. fail_unless(txpacket == 5, "txpacket = %d", txpacket);
  661. for (i = 0; i < 108000 - 25; i++) {
  662. tick_lwip();
  663. }
  664. fail_unless(netif_is_up(&net_test));
  665. fail_unless(txpacket == 6, "txpacket = %d", txpacket);
  666. /* We need to send arp response here.. */
  667. send_pkt(&net_test, arp_resp, sizeof(arp_resp));
  668. fail_unless(txpacket == 7, "txpacket = %d", txpacket);
  669. fail_unless(netif_is_up(&net_test));
  670. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  671. memcpy(&relay_ack2[46], &xid, 4); /* insert transaction id */
  672. send_pkt(&net_test, relay_ack2, sizeof(relay_ack2));
  673. for (i = 0; i < 100000; i++) {
  674. tick_lwip();
  675. }
  676. fail_unless(txpacket == 7, "txpacket = %d", txpacket);
  677. tcase = TEST_NONE;
  678. dhcp_stop(&net_test);
  679. dhcp_cleanup(&net_test);
  680. netif_remove(&net_test);
  681. }
  682. END_TEST
  683. START_TEST(test_dhcp_nak_no_endmarker)
  684. {
  685. ip4_addr_t addr;
  686. ip4_addr_t netmask;
  687. ip4_addr_t gw;
  688. u8_t dhcp_nack_no_endmarker[] = {
  689. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x54, 0x75,
  690. 0xd0, 0x26, 0xd0, 0x0d, 0x08, 0x00, 0x45, 0x00,
  691. 0x01, 0x15, 0x38, 0x86, 0x00, 0x00, 0xff, 0x11,
  692. 0xc0, 0xa8, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xff,
  693. 0xff, 0xff, 0x00, 0x43, 0x00, 0x44, 0x01, 0x01,
  694. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x7a, 0xcb,
  695. 0xba, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  696. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23,
  698. 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00,
  699. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  700. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  701. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  709. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  713. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  714. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  715. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  716. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  717. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  718. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  719. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  720. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  721. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  722. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  723. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  724. 0x53, 0x63, 0x35, 0x01, 0x06, 0x36, 0x04, 0xc0,
  725. 0xa8, 0x01, 0x01, 0x31, 0xef, 0xad, 0x72, 0x31,
  726. 0x43, 0x4e, 0x44, 0x30, 0x32, 0x35, 0x30, 0x43,
  727. 0x52, 0x47, 0x44, 0x38, 0x35, 0x36, 0x3c, 0x08,
  728. 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30,
  729. 0x37, 0x0d, 0x01, 0x0f, 0x03, 0x06, 0x2c, 0x2e,
  730. 0x2f, 0x1f, 0x21, 0x79, 0xf9, 0x2b, 0xfc, 0xff,
  731. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x71,
  732. 0xf3, 0x5b, 0xe2, 0x71, 0x2e, 0x01, 0x08, 0x03,
  733. 0x04, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xeb, 0x1e,
  734. 0x44, 0xec, 0xeb, 0x1e, 0x30, 0x37, 0x0c, 0x01,
  735. 0x0f, 0x03, 0x06, 0x2c, 0x2e, 0x2f, 0x1f, 0x21,
  736. 0x79, 0xf9, 0x2b, 0xff, 0x25, 0xc0, 0x09, 0xd6,
  737. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  738. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  739. };
  740. u32_t xid;
  741. LWIP_UNUSED_ARG(_i);
  742. tcase = TEST_LWIP_DHCP_NAK_NO_ENDMARKER;
  743. setdebug(0);
  744. IP4_ADDR(&addr, 0, 0, 0, 0);
  745. IP4_ADDR(&netmask, 0, 0, 0, 0);
  746. IP4_ADDR(&gw, 0, 0, 0, 0);
  747. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  748. netif_set_up(&net_test);
  749. dhcp_start(&net_test);
  750. fail_unless(txpacket == 1); /* DHCP discover sent */
  751. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  752. memcpy(&dhcp_offer[46], &xid, 4);
  753. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  754. /* IP addresses should be zero */
  755. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  756. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  757. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  758. fail_unless(txpacket == 1); /* Nothing more sent */
  759. xid = htonl(netif_dhcp_data(&net_test)->xid);
  760. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  761. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  762. fail_unless(netif_dhcp_data(&net_test)->state == DHCP_STATE_REQUESTING);
  763. fail_unless(txpacket == 2); /* No more sent */
  764. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  765. memcpy(&dhcp_nack_no_endmarker[46], &xid, 4); /* insert transaction id */
  766. send_pkt(&net_test, dhcp_nack_no_endmarker, sizeof(dhcp_nack_no_endmarker));
  767. /* NAK should put us in another state for a while, no other way detecting it */
  768. fail_unless(netif_dhcp_data(&net_test)->state != DHCP_STATE_REQUESTING);
  769. tcase = TEST_NONE;
  770. dhcp_stop(&net_test);
  771. dhcp_cleanup(&net_test);
  772. netif_remove(&net_test);
  773. }
  774. END_TEST
  775. START_TEST(test_dhcp_invalid_overload)
  776. {
  777. u8_t dhcp_offer_invalid_overload[] = {
  778. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* To unit */
  779. 0x00, 0x0F, 0xEE, 0x30, 0xAB, 0x22, /* From Remote host */
  780. 0x08, 0x00, /* Protocol: IP */
  781. 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x36, 0xcc, 0xc3, 0xaa, 0xbd, 0xab, 0xc3, 0xaa, 0xbd, 0xc8, /* IP header */
  782. 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, 0x00, 0x00, /* UDP header */
  783. 0x02, /* Type == Boot reply */
  784. 0x01, 0x06, /* Hw Ethernet, 6 bytes addrlen */
  785. 0x00, /* 0 hops */
  786. 0xAA, 0xAA, 0xAA, 0xAA, /* Transaction id, will be overwritten */
  787. 0x00, 0x00, /* 0 seconds elapsed */
  788. 0x00, 0x00, /* Flags (unicast) */
  789. 0x00, 0x00, 0x00, 0x00, /* Client ip */
  790. 0xc3, 0xaa, 0xbd, 0xc8, /* Your IP */
  791. 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server ip */
  792. 0x00, 0x00, 0x00, 0x00, /* relay agent */
  793. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* MAC addr + padding */
  794. /* Empty server name */
  795. 0x34, 0x01, 0x02, 0xff, /* Overload: SNAME + END */
  796. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  797. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  798. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  799. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  800. /* Empty boot file name */
  801. 0x34, 0x01, 0x01, 0xff, /* Overload FILE + END */
  802. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  803. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  804. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  805. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  806. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  807. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  808. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  809. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  810. 0x63, 0x82, 0x53, 0x63, /* Magic cookie */
  811. 0x35, 0x01, 0x02, /* Message type: Offer */
  812. 0x36, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Server identifier (IP) */
  813. 0x33, 0x04, 0x00, 0x00, 0x00, 0x78, /* Lease time 2 minutes */
  814. 0x03, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Router IP */
  815. 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, /* Subnet mask */
  816. 0x34, 0x01, 0x03, /* Overload: FILE + SNAME */
  817. 0xff, /* End option */
  818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  819. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Padding */
  820. };
  821. ip4_addr_t addr;
  822. ip4_addr_t netmask;
  823. ip4_addr_t gw;
  824. u32_t xid;
  825. LWIP_UNUSED_ARG(_i);
  826. tcase = TEST_LWIP_DHCP_INVALID_OVERLOAD;
  827. setdebug(0);
  828. IP4_ADDR(&addr, 0, 0, 0, 0);
  829. IP4_ADDR(&netmask, 0, 0, 0, 0);
  830. IP4_ADDR(&gw, 0, 0, 0, 0);
  831. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  832. netif_set_up(&net_test);
  833. dhcp_start(&net_test);
  834. fail_unless(txpacket == 1); /* DHCP discover sent */
  835. xid = htonl(netif_dhcp_data(&net_test)->xid);
  836. memcpy(&dhcp_offer_invalid_overload[46], &xid, 4); /* insert correct transaction id */
  837. dhcp_offer_invalid_overload[311] = 3;
  838. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  839. /* IP addresses should be zero */
  840. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  841. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  842. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  843. fail_unless(txpacket == 1); /* Nothing more sent */
  844. dhcp_offer_invalid_overload[311] = 2;
  845. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  846. /* IP addresses should be zero */
  847. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  848. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  849. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  850. fail_unless(txpacket == 1); /* Nothing more sent */
  851. dhcp_offer_invalid_overload[311] = 1;
  852. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  853. /* IP addresses should be zero */
  854. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  855. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  856. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  857. fail_unless(txpacket == 1); /* Nothing more sent */
  858. dhcp_offer_invalid_overload[311] = 0;
  859. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer));
  860. fail_unless(netif_dhcp_data(&net_test)->state == DHCP_STATE_REQUESTING);
  861. fail_unless(txpacket == 2); /* No more sent */
  862. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  863. tcase = TEST_NONE;
  864. dhcp_stop(&net_test);
  865. dhcp_cleanup(&net_test);
  866. netif_remove(&net_test);
  867. }
  868. END_TEST
  869. /** Create the suite including all tests for this module */
  870. Suite *
  871. dhcp_suite(void)
  872. {
  873. testfunc tests[] = {
  874. TESTFUNC(test_dhcp),
  875. TESTFUNC(test_dhcp_nak),
  876. TESTFUNC(test_dhcp_relayed),
  877. TESTFUNC(test_dhcp_nak_no_endmarker),
  878. TESTFUNC(test_dhcp_invalid_overload)
  879. };
  880. return create_suite("DHCP", tests, sizeof(tests)/sizeof(testfunc), dhcp_setup, dhcp_teardown);
  881. }