test_dhcp.c 40 KB

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