test_dhcp.c 33 KB

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