test_dhcp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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_link_up(&net_test);
  375. netif_set_up(&net_test);
  376. dhcp_start(&net_test);
  377. fail_unless(txpacket == 1); /* DHCP discover sent */
  378. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  379. memcpy(&dhcp_offer[46], &xid, 4);
  380. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  381. /* IP addresses should be zero */
  382. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  383. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  384. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  385. fail_unless(txpacket == 1, "TX %d packets, expected 1", txpacket); /* Nothing more sent */
  386. xid = htonl(netif_dhcp_data(&net_test)->xid);
  387. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  388. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  389. fail_unless(txpacket == 2, "TX %d packets, expected 2", txpacket); /* DHCP request sent */
  390. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  391. memcpy(&dhcp_ack[46], &xid, 4);
  392. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  393. fail_unless(txpacket == 2, "TX %d packets, still expected 2", txpacket); /* No more sent */
  394. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  395. memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */
  396. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  397. for (i = 0; i < 20; i++) {
  398. tick_lwip();
  399. }
  400. fail_unless(txpacket == 5, "TX %d packets, expected 5", txpacket); /* ARP requests sent */
  401. /* Interface up */
  402. fail_unless(netif_is_up(&net_test));
  403. /* Now it should have taken the IP */
  404. IP4_ADDR(&addr, 195, 170, 189, 200);
  405. IP4_ADDR(&netmask, 255, 255, 255, 0);
  406. IP4_ADDR(&gw, 195, 170, 189, 171);
  407. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  408. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  409. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  410. tcase = TEST_NONE;
  411. dhcp_stop(&net_test);
  412. dhcp_cleanup(&net_test);
  413. netif_remove(&net_test);
  414. }
  415. END_TEST
  416. /*
  417. * Test that IP address is not taken and NAK is sent if someone
  418. * replies to ARP requests for the offered address.
  419. */
  420. START_TEST(test_dhcp_nak)
  421. {
  422. ip4_addr_t addr;
  423. ip4_addr_t netmask;
  424. ip4_addr_t gw;
  425. u32_t xid;
  426. LWIP_UNUSED_ARG(_i);
  427. tcase = TEST_LWIP_DHCP;
  428. setdebug(0);
  429. IP4_ADDR(&addr, 0, 0, 0, 0);
  430. IP4_ADDR(&netmask, 0, 0, 0, 0);
  431. IP4_ADDR(&gw, 0, 0, 0, 0);
  432. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  433. netif_set_link_up(&net_test);
  434. netif_set_up(&net_test);
  435. dhcp_start(&net_test);
  436. fail_unless(txpacket == 1); /* DHCP discover sent */
  437. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  438. memcpy(&dhcp_offer[46], &xid, 4);
  439. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  440. /* IP addresses should be zero */
  441. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  442. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  443. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  444. fail_unless(txpacket == 1); /* Nothing more sent */
  445. xid = htonl(netif_dhcp_data(&net_test)->xid);
  446. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  447. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  448. fail_unless(txpacket == 2); /* DHCP request sent */
  449. xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */
  450. memcpy(&dhcp_ack[46], &xid, 4);
  451. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  452. fail_unless(txpacket == 2); /* No more sent */
  453. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  454. memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */
  455. send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack));
  456. fail_unless(txpacket == 3); /* ARP request sent */
  457. tcase = TEST_LWIP_DHCP_NAK; /* Switch testcase */
  458. /* Send arp reply, mark offered IP as taken */
  459. send_pkt(&net_test, arpreply, sizeof(arpreply));
  460. fail_unless(txpacket == 4); /* DHCP nak sent */
  461. tcase = TEST_NONE;
  462. dhcp_stop(&net_test);
  463. dhcp_cleanup(&net_test);
  464. netif_remove(&net_test);
  465. }
  466. END_TEST
  467. /*
  468. * Test case based on captured data where
  469. * replies are sent from a different IP than the
  470. * one the client unicasted to.
  471. */
  472. START_TEST(test_dhcp_relayed)
  473. {
  474. u8_t relay_offer[] = {
  475. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d,
  476. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60,
  477. 0x08, 0x00, 0x45, 0x00,
  478. 0x01, 0x38, 0xfd, 0x53, 0x00, 0x00, 0x40, 0x11,
  479. 0x78, 0x46, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  480. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  481. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35,
  482. 0xb6, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  483. 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  484. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  485. 0xc1, 0xde, 0xd0, 0x0d, 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, 0x00, 0x00,
  509. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  510. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  511. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  512. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  513. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  514. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  515. 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x02, 0x36,
  516. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff
  517. };
  518. u8_t relay_ack1[] = {
  519. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x22,
  520. 0x93, 0x5a, 0xf7, 0x60, 0x08, 0x00, 0x45, 0x00,
  521. 0x01, 0x38, 0xfd, 0x55, 0x00, 0x00, 0x40, 0x11,
  522. 0x78, 0x44, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  523. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  524. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35,
  525. 0xb6, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  526. 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  527. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  528. 0xc1, 0xde, 0xd0, 0x0d, 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, 0x00, 0x00,
  552. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  553. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  554. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  555. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  556. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  557. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  558. 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x05, 0x36,
  559. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff
  560. };
  561. u8_t relay_ack2[] = {
  562. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d,
  563. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60,
  564. 0x08, 0x00, 0x45, 0x00,
  565. 0x01, 0x38, 0xfa, 0x18, 0x00, 0x00, 0x40, 0x11,
  566. 0x7b, 0x81, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a,
  567. 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24,
  568. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x49, 0x8b,
  569. 0x6e, 0xab, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x8a,
  570. 0x33, 0x05, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00,
  571. 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23,
  572. 0xc1, 0xde, 0xd0, 0x0d, 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, 0x00, 0x00,
  596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  597. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  598. 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00,
  599. 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08,
  600. 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1,
  601. 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04,
  602. 0x00, 0x00, 0x54, 0x60, 0x35, 0x01, 0x05, 0x36,
  603. 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff };
  604. const u8_t arp_resp[] = {
  605. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* DEST */
  606. 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, /* SRC */
  607. 0x08, 0x06, /* Type: ARP */
  608. 0x00, 0x01, /* HW: Ethernet */
  609. 0x08, 0x00, /* PROTO: IP */
  610. 0x06, /* HW size */
  611. 0x04, /* PROTO size */
  612. 0x00, 0x02, /* OPCODE: Reply */
  613. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xab, /* Target MAC */
  614. 0x4f, 0x8a, 0x32, 0x01, /* Target IP */
  615. 0x00, 0x23, 0xc1, 0x00, 0x06, 0x50, /* src mac */
  616. 0x4f, 0x8a, 0x33, 0x05, /* src ip */
  617. /* Padding follows.. */
  618. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  619. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  620. 0x00, 0x00, 0x00, 0x00 };
  621. ip4_addr_t addr;
  622. ip4_addr_t netmask;
  623. ip4_addr_t gw;
  624. int i;
  625. u32_t xid;
  626. LWIP_UNUSED_ARG(_i);
  627. tcase = TEST_LWIP_DHCP_RELAY;
  628. setdebug(0);
  629. IP4_ADDR(&addr, 0, 0, 0, 0);
  630. IP4_ADDR(&netmask, 0, 0, 0, 0);
  631. IP4_ADDR(&gw, 0, 0, 0, 0);
  632. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  633. netif_set_link_up(&net_test);
  634. netif_set_up(&net_test);
  635. dhcp_start(&net_test);
  636. fail_unless(txpacket == 1); /* DHCP discover sent */
  637. /* IP addresses should be zero */
  638. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  639. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  640. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  641. fail_unless(txpacket == 1); /* Nothing more sent */
  642. xid = htonl(netif_dhcp_data(&net_test)->xid);
  643. memcpy(&relay_offer[46], &xid, 4); /* insert correct transaction id */
  644. send_pkt(&net_test, relay_offer, sizeof(relay_offer));
  645. /* request sent? */
  646. fail_unless(txpacket == 2, "txpkt = %d, should be 2", txpacket);
  647. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  648. memcpy(&relay_ack1[46], &xid, 4); /* insert transaction id */
  649. send_pkt(&net_test, relay_ack1, sizeof(relay_ack1));
  650. for (i = 0; i < 25; i++) {
  651. tick_lwip();
  652. }
  653. fail_unless(txpacket == 5, "txpkt should be 5, is %d", txpacket); /* ARP requests sent */
  654. /* Interface up */
  655. fail_unless(netif_is_up(&net_test));
  656. /* Now it should have taken the IP */
  657. IP4_ADDR(&addr, 79, 138, 51, 5);
  658. IP4_ADDR(&netmask, 255, 255, 254, 0);
  659. IP4_ADDR(&gw, 79, 138, 50, 1);
  660. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  661. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  662. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  663. fail_unless(txpacket == 5, "txpacket = %d", txpacket);
  664. for (i = 0; i < 108000 - 25; i++) {
  665. tick_lwip();
  666. }
  667. fail_unless(netif_is_up(&net_test));
  668. fail_unless(txpacket == 6, "txpacket = %d", txpacket);
  669. /* We need to send arp response here.. */
  670. send_pkt(&net_test, arp_resp, sizeof(arp_resp));
  671. fail_unless(txpacket == 7, "txpacket = %d", txpacket);
  672. fail_unless(netif_is_up(&net_test));
  673. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  674. memcpy(&relay_ack2[46], &xid, 4); /* insert transaction id */
  675. send_pkt(&net_test, relay_ack2, sizeof(relay_ack2));
  676. for (i = 0; i < 100000; i++) {
  677. tick_lwip();
  678. }
  679. fail_unless(txpacket == 7, "txpacket = %d", txpacket);
  680. tcase = TEST_NONE;
  681. dhcp_stop(&net_test);
  682. dhcp_cleanup(&net_test);
  683. netif_remove(&net_test);
  684. }
  685. END_TEST
  686. START_TEST(test_dhcp_nak_no_endmarker)
  687. {
  688. ip4_addr_t addr;
  689. ip4_addr_t netmask;
  690. ip4_addr_t gw;
  691. u8_t dhcp_nack_no_endmarker[] = {
  692. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x54, 0x75,
  693. 0xd0, 0x26, 0xd0, 0x0d, 0x08, 0x00, 0x45, 0x00,
  694. 0x01, 0x15, 0x38, 0x86, 0x00, 0x00, 0xff, 0x11,
  695. 0xc0, 0xa8, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xff,
  696. 0xff, 0xff, 0x00, 0x43, 0x00, 0x44, 0x01, 0x01,
  697. 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x7a, 0xcb,
  698. 0xba, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  699. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  700. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23,
  701. 0xc1, 0xde, 0xd0, 0x0d, 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, 0x00, 0x00,
  724. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  725. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  726. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
  727. 0x53, 0x63, 0x35, 0x01, 0x06, 0x36, 0x04, 0xc0,
  728. 0xa8, 0x01, 0x01, 0x31, 0xef, 0xad, 0x72, 0x31,
  729. 0x43, 0x4e, 0x44, 0x30, 0x32, 0x35, 0x30, 0x43,
  730. 0x52, 0x47, 0x44, 0x38, 0x35, 0x36, 0x3c, 0x08,
  731. 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30,
  732. 0x37, 0x0d, 0x01, 0x0f, 0x03, 0x06, 0x2c, 0x2e,
  733. 0x2f, 0x1f, 0x21, 0x79, 0xf9, 0x2b, 0xfc, 0xff,
  734. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x71,
  735. 0xf3, 0x5b, 0xe2, 0x71, 0x2e, 0x01, 0x08, 0x03,
  736. 0x04, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xeb, 0x1e,
  737. 0x44, 0xec, 0xeb, 0x1e, 0x30, 0x37, 0x0c, 0x01,
  738. 0x0f, 0x03, 0x06, 0x2c, 0x2e, 0x2f, 0x1f, 0x21,
  739. 0x79, 0xf9, 0x2b, 0xff, 0x25, 0xc0, 0x09, 0xd6,
  740. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  741. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  742. };
  743. u32_t xid;
  744. struct dhcp* dhcp;
  745. u8_t tries;
  746. u16_t request_timeout;
  747. LWIP_UNUSED_ARG(_i);
  748. tcase = TEST_LWIP_DHCP_NAK_NO_ENDMARKER;
  749. setdebug(0);
  750. IP4_ADDR(&addr, 0, 0, 0, 0);
  751. IP4_ADDR(&netmask, 0, 0, 0, 0);
  752. IP4_ADDR(&gw, 0, 0, 0, 0);
  753. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  754. netif_set_link_up(&net_test);
  755. netif_set_up(&net_test);
  756. dhcp_start(&net_test);
  757. dhcp = netif_dhcp_data(&net_test);
  758. fail_unless(txpacket == 1); /* DHCP discover sent */
  759. xid = dhcp->xid; /* Write bad xid, not using htonl! */
  760. memcpy(&dhcp_offer[46], &xid, 4);
  761. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  762. /* IP addresses should be zero */
  763. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  764. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  765. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  766. fail_unless(txpacket == 1); /* Nothing more sent */
  767. xid = htonl(dhcp->xid);
  768. memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */
  769. send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer));
  770. fail_unless(dhcp->state == DHCP_STATE_REQUESTING);
  771. fail_unless(txpacket == 2); /* No more sent */
  772. xid = htonl(dhcp->xid); /* xid updated */
  773. memcpy(&dhcp_nack_no_endmarker[46], &xid, 4); /* insert transaction id */
  774. tries = dhcp->tries;
  775. request_timeout = dhcp->request_timeout;
  776. send_pkt(&net_test, dhcp_nack_no_endmarker, sizeof(dhcp_nack_no_endmarker));
  777. /* NAK should be ignored */
  778. fail_unless(dhcp->state == DHCP_STATE_REQUESTING);
  779. fail_unless(txpacket == 2); /* No more sent */
  780. fail_unless(xid == htonl(dhcp->xid));
  781. fail_unless(tries == dhcp->tries);
  782. fail_unless(request_timeout == dhcp->request_timeout);
  783. tcase = TEST_NONE;
  784. dhcp_stop(&net_test);
  785. dhcp_cleanup(&net_test);
  786. netif_remove(&net_test);
  787. }
  788. END_TEST
  789. START_TEST(test_dhcp_invalid_overload)
  790. {
  791. u8_t dhcp_offer_invalid_overload[] = {
  792. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* To unit */
  793. 0x00, 0x0F, 0xEE, 0x30, 0xAB, 0x22, /* From Remote host */
  794. 0x08, 0x00, /* Protocol: IP */
  795. 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x36, 0xcc, 0xc3, 0xaa, 0xbd, 0xab, 0xc3, 0xaa, 0xbd, 0xc8, /* IP header */
  796. 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, 0x00, 0x00, /* UDP header */
  797. 0x02, /* Type == Boot reply */
  798. 0x01, 0x06, /* Hw Ethernet, 6 bytes addrlen */
  799. 0x00, /* 0 hops */
  800. 0xAA, 0xAA, 0xAA, 0xAA, /* Transaction id, will be overwritten */
  801. 0x00, 0x00, /* 0 seconds elapsed */
  802. 0x00, 0x00, /* Flags (unicast) */
  803. 0x00, 0x00, 0x00, 0x00, /* Client ip */
  804. 0xc3, 0xaa, 0xbd, 0xc8, /* Your IP */
  805. 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server ip */
  806. 0x00, 0x00, 0x00, 0x00, /* relay agent */
  807. 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* MAC addr + padding */
  808. /* Empty server name */
  809. 0x34, 0x01, 0x02, 0xff, /* Overload: SNAME + END */
  810. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  811. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  812. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  813. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  814. /* Empty boot file name */
  815. 0x34, 0x01, 0x01, 0xff, /* Overload FILE + END */
  816. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  817. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  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, 0x00, 0x00, 0x00,
  820. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  821. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  822. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  823. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  824. 0x63, 0x82, 0x53, 0x63, /* Magic cookie */
  825. 0x35, 0x01, 0x02, /* Message type: Offer */
  826. 0x36, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Server identifier (IP) */
  827. 0x33, 0x04, 0x00, 0x00, 0x00, 0x78, /* Lease time 2 minutes */
  828. 0x03, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Router IP */
  829. 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, /* Subnet mask */
  830. 0x34, 0x01, 0x03, /* Overload: FILE + SNAME */
  831. 0xff, /* End option */
  832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Padding */
  834. };
  835. ip4_addr_t addr;
  836. ip4_addr_t netmask;
  837. ip4_addr_t gw;
  838. u32_t xid;
  839. LWIP_UNUSED_ARG(_i);
  840. tcase = TEST_LWIP_DHCP_INVALID_OVERLOAD;
  841. setdebug(0);
  842. IP4_ADDR(&addr, 0, 0, 0, 0);
  843. IP4_ADDR(&netmask, 0, 0, 0, 0);
  844. IP4_ADDR(&gw, 0, 0, 0, 0);
  845. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  846. netif_set_link_up(&net_test);
  847. netif_set_up(&net_test);
  848. dhcp_start(&net_test);
  849. fail_unless(txpacket == 1); /* DHCP discover sent */
  850. xid = htonl(netif_dhcp_data(&net_test)->xid);
  851. memcpy(&dhcp_offer_invalid_overload[46], &xid, 4); /* insert correct transaction id */
  852. dhcp_offer_invalid_overload[311] = 3;
  853. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  854. /* IP addresses should be zero */
  855. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  856. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  857. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  858. fail_unless(txpacket == 1); /* Nothing more sent */
  859. dhcp_offer_invalid_overload[311] = 2;
  860. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  861. /* IP addresses should be zero */
  862. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  863. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  864. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  865. fail_unless(txpacket == 1); /* Nothing more sent */
  866. dhcp_offer_invalid_overload[311] = 1;
  867. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload));
  868. /* IP addresses should be zero */
  869. fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t)));
  870. fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
  871. fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
  872. fail_unless(txpacket == 1); /* Nothing more sent */
  873. dhcp_offer_invalid_overload[311] = 0;
  874. send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer));
  875. fail_unless(netif_dhcp_data(&net_test)->state == DHCP_STATE_REQUESTING);
  876. fail_unless(txpacket == 2); /* No more sent */
  877. xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
  878. tcase = TEST_NONE;
  879. dhcp_stop(&net_test);
  880. dhcp_cleanup(&net_test);
  881. netif_remove(&net_test);
  882. }
  883. END_TEST
  884. /** Create the suite including all tests for this module */
  885. Suite *
  886. dhcp_suite(void)
  887. {
  888. testfunc tests[] = {
  889. TESTFUNC(test_dhcp),
  890. TESTFUNC(test_dhcp_nak),
  891. TESTFUNC(test_dhcp_relayed),
  892. TESTFUNC(test_dhcp_nak_no_endmarker),
  893. TESTFUNC(test_dhcp_invalid_overload)
  894. };
  895. return create_suite("DHCP", tests, sizeof(tests)/sizeof(testfunc), dhcp_setup, dhcp_teardown);
  896. }