test_dhcp.c 38 KB

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