fuzz_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Erik Ekman <erik@kryo.se>
  30. * Simon Goldschmidt <goldsimon@gmx.de>
  31. *
  32. */
  33. #include "fuzz_common.h"
  34. #include "lwip/altcp_tcp.h"
  35. #include "lwip/dns.h"
  36. #include "lwip/init.h"
  37. #include "lwip/netif.h"
  38. #include "lwip/sys.h"
  39. #include "lwip/timeouts.h"
  40. #include "lwip/udp.h"
  41. #include "netif/etharp.h"
  42. #if LWIP_IPV6
  43. #include "lwip/ethip6.h"
  44. #include "lwip/nd6.h"
  45. #endif
  46. #include "lwip/apps/httpd.h"
  47. #include "lwip/apps/snmp.h"
  48. #include "lwip/apps/lwiperf.h"
  49. #include "lwip/apps/mdns.h"
  50. #include <string.h>
  51. #include <stdio.h>
  52. static u8_t pktbuf[200000];
  53. static const u8_t *remfuzz_ptr; /* remaining fuzz pointer */
  54. static size_t remfuzz_len; /* remaining fuzz length */
  55. #ifndef FUZZ_DEBUG
  56. #define FUZZ_DEBUG LWIP_DBG_OFF
  57. #endif
  58. #ifdef LWIP_FUZZ_SYS_NOW
  59. /* This offset should be added to the time 'sys_now()' returns */
  60. u32_t sys_now_offset;
  61. #endif
  62. /** Set this to 1 and define FUZZ_DUMP_PCAP_FILE to dump tx and rx packets into
  63. * a pcap file. At the same time, packet info is written via LWIP_DEBUGF so
  64. * packets can be matched to other events for debugging them.
  65. */
  66. #ifndef FUZZ_DUMP_PCAP
  67. #define FUZZ_DUMP_PCAP 0
  68. #endif
  69. #if FUZZ_DUMP_PCAP
  70. const u8_t pcap_file_header[24] = {
  71. 0xd4, 0xc3, 0xb2, 0xa1, 0x02, 0x00, 0x04, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00
  74. };
  75. static FILE* fpcap;
  76. static u32_t pcap_packet;
  77. static void pcap_dump_init(void)
  78. {
  79. fpcap = fopen(FUZZ_DUMP_PCAP_FILE, "wb");
  80. if (fpcap != NULL) {
  81. /* write header */
  82. fwrite(pcap_file_header, 1, sizeof(pcap_file_header), fpcap);
  83. }
  84. }
  85. /* This function might have to be called from LWIP_PLATFORM_ASSERT()
  86. * in order to produce correct pcap results on crash.
  87. * Define this global so that for a test, we can call this from anywhere...
  88. */
  89. void pcap_dump_stop(void);
  90. void pcap_dump_stop(void)
  91. {
  92. if (fpcap != NULL) {
  93. fclose(fpcap);
  94. fpcap = NULL;
  95. }
  96. }
  97. static void pcap_dump_packet(struct pbuf *p, int is_tx)
  98. {
  99. if (fpcap != NULL) {
  100. struct pbuf *q;
  101. u32_t data;
  102. pcap_packet++;
  103. if (is_tx) {
  104. LWIP_DEBUGF(FUZZ_DEBUG, ("> %d fuzz: netif: send %u bytes\n", pcap_packet, p->tot_len));
  105. } else {
  106. LWIP_DEBUGF(FUZZ_DEBUG, ("< %d fuzz: RX packet of %u bytes\n", pcap_packet, p->tot_len));
  107. if (pcap_packet == 50 || pcap_packet == 33 || pcap_packet == 29) {
  108. pcap_packet++;
  109. pcap_packet--;
  110. }
  111. }
  112. /* write packet header */
  113. fwrite(&pcap_packet, 1, sizeof(pcap_packet), fpcap);
  114. data = 0;
  115. fwrite(&data, 1, sizeof(data), fpcap);
  116. data = p->tot_len;
  117. fwrite(&data, 1, sizeof(data), fpcap);
  118. fwrite(&data, 1, sizeof(data), fpcap);
  119. /* write packet data */
  120. for(q = p; q != NULL; q = q->next) {
  121. fwrite(q->payload, 1, q->len, fpcap);
  122. }
  123. }
  124. }
  125. static void pcap_dump_rx_packet(struct pbuf *p)
  126. {
  127. pcap_dump_packet(p, 0);
  128. }
  129. static void pcap_dump_tx_packet(struct pbuf *p)
  130. {
  131. pcap_dump_packet(p, 1);
  132. }
  133. #else /* FUZZ_DUMP_PCAP */
  134. #define pcap_dump_rx_packet(p)
  135. #define pcap_dump_tx_packet(p)
  136. #define pcap_dump_init()
  137. #define pcap_dump_stop()
  138. #endif /* FUZZ_DUMP_PCAP */
  139. /* no-op send function */
  140. static err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
  141. {
  142. pcap_dump_tx_packet(p);
  143. LWIP_UNUSED_ARG(netif);
  144. LWIP_UNUSED_ARG(p);
  145. return ERR_OK;
  146. }
  147. static err_t testif_init(struct netif *netif)
  148. {
  149. netif->name[0] = 'f';
  150. netif->name[1] = 'z';
  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 | NETIF_FLAG_IGMP;
  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. #if LWIP_IPV6
  163. netif->output_ip6 = ethip6_output;
  164. netif_create_ip6_linklocal_address(netif, 1);
  165. netif->flags |= NETIF_FLAG_MLD6;
  166. #endif
  167. return ERR_OK;
  168. }
  169. static void input_pkt(struct netif *netif, const u8_t *data, size_t len)
  170. {
  171. struct pbuf *p, *q;
  172. err_t err;
  173. if (len > 0xFFFF) {
  174. printf("pkt too big (%#zX bytes)\n", len);
  175. return;
  176. }
  177. p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
  178. LWIP_ASSERT("alloc failed", p);
  179. for(q = p; q != NULL; q = q->next) {
  180. MEMCPY(q->payload, data, q->len);
  181. data += q->len;
  182. }
  183. remfuzz_ptr += len;
  184. remfuzz_len -= len;
  185. pcap_dump_rx_packet(p);
  186. err = netif->input(p, netif);
  187. if (err != ERR_OK) {
  188. pbuf_free(p);
  189. }
  190. }
  191. static void input_pkts(enum lwip_fuzz_type type, struct netif *netif, const u8_t *data, size_t len)
  192. {
  193. size_t packet_nr = 0;
  194. remfuzz_ptr = data;
  195. remfuzz_len = len;
  196. if (type == LWIP_FUZZ_SINGLE) {
  197. input_pkt(netif, data, len);
  198. } else {
  199. const u16_t max_packet_size = 1514;
  200. const size_t minlen = sizeof(u16_t) + (type == LWIP_FUZZ_MULTIPACKET_TIME ? sizeof(u32_t) : 0);
  201. while (remfuzz_len > minlen) {
  202. u16_t frame_len;
  203. #ifdef LWIP_FUZZ_SYS_NOW
  204. u32_t external_delay = 0;
  205. #endif
  206. packet_nr++;
  207. if (type == LWIP_FUZZ_MULTIPACKET_TIME) {
  208. #ifdef LWIP_FUZZ_SYS_NOW
  209. /* Extract external delay time from fuzz pool */
  210. memcpy(&external_delay, remfuzz_ptr, sizeof(u32_t));
  211. external_delay = ntohl(external_delay);
  212. #endif
  213. remfuzz_ptr += sizeof(u32_t);
  214. remfuzz_len -= sizeof(u32_t);
  215. }
  216. memcpy(&frame_len, remfuzz_ptr, sizeof(u16_t));
  217. remfuzz_ptr += sizeof(u16_t);
  218. remfuzz_len -= sizeof(u16_t);
  219. frame_len = ntohs(frame_len) & 0x7FF;
  220. frame_len = LWIP_MIN(frame_len, max_packet_size);
  221. if (frame_len > remfuzz_len) {
  222. frame_len = (u16_t)remfuzz_len;
  223. }
  224. if (frame_len != 0) {
  225. if (type == LWIP_FUZZ_MULTIPACKET_TIME) {
  226. #ifdef LWIP_FUZZ_SYS_NOW
  227. /* Update total external delay time, and check timeouts */
  228. sys_now_offset += external_delay;
  229. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: sys_now_offset += %u -> %u\n", external_delay, sys_now_offset));
  230. #endif
  231. sys_check_timeouts();
  232. }
  233. input_pkt(netif, remfuzz_ptr, frame_len);
  234. /* Check timeouts again */
  235. sys_check_timeouts();
  236. }
  237. }
  238. }
  239. }
  240. #if LWIP_TCP
  241. static struct altcp_pcb *tcp_client_pcb; /* a pcb for the TCP client */
  242. static struct altcp_pcb *tcp_server_pcb; /* a pcb for the TCP server */
  243. static u16_t tcp_remote_port; /* a TCP port number of the destination */
  244. static u16_t tcp_local_port; /* a TCP port number of the local server */
  245. /**
  246. * tcp_app_fuzz_input
  247. * Input fuzz with a write function for TCP.
  248. */
  249. static void
  250. tcp_app_fuzz_input(struct altcp_pcb *pcb)
  251. {
  252. if (remfuzz_len > sizeof(u16_t)) {
  253. /*
  254. * (max IP packet size) - ((minimum IP header size) + (minimum TCP header size))
  255. * = 65535 - (20 + 20)
  256. * = 65495
  257. */
  258. const u16_t max_data_size = 65495;
  259. u16_t data_len;
  260. memcpy(&data_len, remfuzz_ptr, sizeof(u16_t));
  261. remfuzz_ptr += sizeof(u16_t);
  262. remfuzz_len -= sizeof(u16_t);
  263. data_len = ntohs(data_len);
  264. data_len = LWIP_MIN(data_len, max_data_size);
  265. if (data_len > remfuzz_len) {
  266. data_len = (u16_t)remfuzz_len;
  267. }
  268. if (data_len != 0) {
  269. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: tcp: write %u bytes\n", data_len));
  270. altcp_write(pcb, remfuzz_ptr, data_len, TCP_WRITE_FLAG_COPY);
  271. altcp_output(pcb);
  272. } else {
  273. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: tcp: close\n"));
  274. altcp_close(pcb);
  275. }
  276. remfuzz_ptr += data_len;
  277. remfuzz_len -= data_len;
  278. }
  279. }
  280. /**
  281. * tcp_client_connected
  282. * A connected callback function (for the TCP client)
  283. */
  284. static err_t
  285. tcp_client_connected(void *arg, struct altcp_pcb *pcb, err_t err)
  286. {
  287. LWIP_UNUSED_ARG(arg);
  288. LWIP_UNUSED_ARG(err);
  289. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: tcp: tcp_client_connected\n"));
  290. tcp_app_fuzz_input(pcb);
  291. return ERR_OK;
  292. }
  293. /**
  294. * tcp_client_recv
  295. * A recv callback function (for the TCP client)
  296. */
  297. static err_t
  298. tcp_client_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
  299. {
  300. LWIP_UNUSED_ARG(arg);
  301. LWIP_UNUSED_ARG(err);
  302. if (p == NULL) {
  303. altcp_close(pcb);
  304. } else {
  305. altcp_recved(pcb, p->tot_len);
  306. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: tcp: tcp_client_recv: %d\n", p->tot_len));
  307. tcp_app_fuzz_input(pcb);
  308. pbuf_free(p);
  309. }
  310. return ERR_OK;
  311. }
  312. /**
  313. * tcp_client_sent
  314. * A sent callback function (for the TCP client)
  315. */
  316. static err_t
  317. tcp_client_sent(void *arg, struct altcp_pcb *pcb, u16_t len)
  318. {
  319. LWIP_UNUSED_ARG(arg);
  320. LWIP_UNUSED_ARG(pcb);
  321. LWIP_UNUSED_ARG(len);
  322. return ERR_OK;
  323. }
  324. /**
  325. * tcp_client_poll
  326. * A poll callback function (for the TCP client)
  327. */
  328. static err_t
  329. tcp_client_poll(void *arg, struct altcp_pcb *pcb)
  330. {
  331. LWIP_UNUSED_ARG(arg);
  332. LWIP_UNUSED_ARG(pcb);
  333. return ERR_OK;
  334. }
  335. /**
  336. * tcp_client_err
  337. * An err callback function (for the TCP client)
  338. */
  339. static void
  340. tcp_client_err(void *arg, err_t err)
  341. {
  342. LWIP_UNUSED_ARG(arg);
  343. LWIP_UNUSED_ARG(err);
  344. }
  345. /**
  346. * tcp_server_recv
  347. * A recv callback function (for the TCP server)
  348. */
  349. static err_t
  350. tcp_server_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
  351. {
  352. LWIP_UNUSED_ARG(arg);
  353. LWIP_UNUSED_ARG(err);
  354. if (p == NULL) {
  355. altcp_close(pcb);
  356. } else {
  357. altcp_recved(pcb, p->tot_len);
  358. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: tcp: tcp_server_recv: %d\n", p->tot_len));
  359. tcp_app_fuzz_input(pcb);
  360. pbuf_free(p);
  361. }
  362. return ERR_OK;
  363. }
  364. /**
  365. * tcp_server_sent
  366. * A sent callback function (for the TCP server)
  367. */
  368. static err_t
  369. tcp_server_sent(void *arg, struct altcp_pcb *pcb, u16_t len)
  370. {
  371. LWIP_UNUSED_ARG(arg);
  372. LWIP_UNUSED_ARG(pcb);
  373. LWIP_UNUSED_ARG(len);
  374. return ERR_OK;
  375. }
  376. /**
  377. * tcp_server_poll
  378. * A poll callback function (for the TCP server)
  379. */
  380. static err_t
  381. tcp_server_poll(void *arg, struct altcp_pcb *pcb)
  382. {
  383. LWIP_UNUSED_ARG(arg);
  384. LWIP_UNUSED_ARG(pcb);
  385. return ERR_OK;
  386. }
  387. /**
  388. * tcp_server_err
  389. * An err callbuck function (for the TCP server)
  390. */
  391. static void
  392. tcp_server_err(void *arg, err_t err)
  393. {
  394. LWIP_UNUSED_ARG(arg);
  395. LWIP_UNUSED_ARG(err);
  396. }
  397. /**
  398. * tcp_server_accept
  399. * An accept callbuck function (for the TCP server)
  400. */
  401. static err_t
  402. tcp_server_accept(void *arg, struct altcp_pcb *pcb, err_t err)
  403. {
  404. LWIP_UNUSED_ARG(arg);
  405. LWIP_UNUSED_ARG(err);
  406. if ((err != ERR_OK) || (pcb == NULL)) {
  407. return ERR_VAL;
  408. }
  409. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: accept from remote\n"));
  410. altcp_setprio(pcb, TCP_PRIO_MIN);
  411. altcp_recv(pcb, tcp_server_recv);
  412. altcp_err(pcb, tcp_server_err);
  413. altcp_poll(pcb, tcp_server_poll, 10);
  414. altcp_sent(pcb, tcp_server_sent);
  415. return ERR_OK;
  416. }
  417. #endif /* LWIP_TCP */
  418. #if LWIP_UDP
  419. static struct udp_pcb *udp_client_pcb; /* a pcb for the UDP client */
  420. static struct udp_pcb *udp_server_pcb; /* a pcb for the UDP server */
  421. static u16_t udp_remote_port; /* a UDP port number of the destination */
  422. static u16_t udp_local_port; /* a UDP port number of the local server*/
  423. /**
  424. * udp_app_fuzz_input
  425. * Input fuzz with write functions for UDP.
  426. */
  427. static void
  428. udp_app_fuzz_input(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port)
  429. {
  430. if (remfuzz_len > sizeof(u16_t)) {
  431. /*
  432. * (max IP packet size) - ((minimum IP header size) - (minimum UDP header size))
  433. * = 65535 - (20 + 8)
  434. * = 65507
  435. */
  436. const u16_t max_data_size = 65507;
  437. u16_t data_len;
  438. memcpy(&data_len, remfuzz_ptr, sizeof(u16_t));
  439. remfuzz_ptr += sizeof(u16_t);
  440. remfuzz_len -= sizeof(u16_t);
  441. data_len = ntohs(data_len);
  442. data_len = LWIP_MIN(data_len, max_data_size);
  443. if (data_len > remfuzz_len) {
  444. data_len = (u16_t)remfuzz_len;
  445. }
  446. LWIP_DEBUGF(FUZZ_DEBUG, ("fuzz: udp: send %u bytes\n", data_len));
  447. if (data_len != 0) {
  448. struct pbuf *p, *q;
  449. p = pbuf_alloc(PBUF_RAW, (u16_t)data_len, PBUF_POOL);
  450. LWIP_ASSERT("alloc failed", p);
  451. for (q = p; q != NULL; q = q->next) {
  452. MEMCPY(q->payload, remfuzz_ptr, q->len);
  453. remfuzz_ptr += q->len;
  454. }
  455. remfuzz_len -= data_len;
  456. /*
  457. * Trying input from ...
  458. *
  459. * client:
  460. * The pcb has information about the destination.
  461. * We use udp_send().
  462. *
  463. * server:
  464. * The pcb does NOT have information about the destination.
  465. * We use udp_sendto().
  466. */
  467. if (addr == NULL) {
  468. udp_send(pcb, p);
  469. } else {
  470. udp_sendto(pcb, p, addr, port);
  471. }
  472. pbuf_free(p);
  473. }
  474. }
  475. }
  476. /**
  477. * udp_client_recv
  478. * A recv callback function (for the UDP client)
  479. */
  480. static void
  481. udp_client_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  482. {
  483. LWIP_UNUSED_ARG(arg);
  484. LWIP_UNUSED_ARG(p);
  485. LWIP_UNUSED_ARG(addr);
  486. LWIP_UNUSED_ARG(port);
  487. if (p == NULL) {
  488. udp_disconnect(pcb);
  489. } else {
  490. /*
  491. * We call the function with 2nd argument set to NULL
  492. * to input fuzz from udp_send.
  493. */
  494. udp_app_fuzz_input(pcb, NULL, port);
  495. pbuf_free(p);
  496. }
  497. }
  498. /**
  499. * udp_server_recv
  500. * A recv callback functyion (for the UDP server)
  501. */
  502. static void
  503. udp_server_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  504. {
  505. LWIP_UNUSED_ARG(arg);
  506. LWIP_UNUSED_ARG(p);
  507. LWIP_UNUSED_ARG(addr);
  508. LWIP_UNUSED_ARG(port);
  509. if (p != NULL) {
  510. udp_app_fuzz_input(pcb, addr, port);
  511. pbuf_free(p);
  512. }
  513. }
  514. #endif /* LWIP_UDP */
  515. int lwip_fuzztest(int argc, char** argv, enum lwip_fuzz_type type, u32_t test_apps)
  516. {
  517. struct netif net_test;
  518. ip4_addr_t addr;
  519. ip4_addr_t netmask;
  520. ip4_addr_t gw;
  521. size_t len;
  522. err_t err;
  523. ip_addr_t remote_addr; /* a IPv4 addr of the destination */
  524. struct eth_addr remote_mac = ETH_ADDR(0x28, 0x00, 0x00, 0x22, 0x2b, 0x38); /* a MAC addr of the destination */
  525. pcap_dump_init();
  526. lwip_init();
  527. IP4_ADDR(&addr, 172, 30, 115, 84);
  528. IP4_ADDR(&netmask, 255, 255, 255, 0);
  529. IP4_ADDR(&gw, 172, 30, 115, 1);
  530. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  531. netif_set_up(&net_test);
  532. netif_set_link_up(&net_test);
  533. if (test_apps & LWIP_FUZZ_STATICARP) {
  534. /* Add the ARP entry */
  535. IP_ADDR4(&remote_addr, 172, 30, 115, 37);
  536. etharp_add_static_entry(&(remote_addr.u_addr.ip4), &remote_mac);
  537. }
  538. #if LWIP_IPV6
  539. nd6_tmr(); /* tick nd to join multicast groups */
  540. #endif
  541. dns_setserver(0, &net_test.gw);
  542. if (test_apps & LWIP_FUZZ_DEFAULT) {
  543. /* initialize apps */
  544. httpd_init();
  545. lwiperf_start_tcp_server_default(NULL, NULL);
  546. mdns_resp_init();
  547. mdns_resp_add_netif(&net_test, "hostname");
  548. snmp_init();
  549. }
  550. if (test_apps & LWIP_FUZZ_TCP_CLIENT) {
  551. tcp_client_pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY);
  552. LWIP_ASSERT("Error: altcp_new() failed", tcp_client_pcb != NULL);
  553. tcp_remote_port = 80;
  554. err = altcp_connect(tcp_client_pcb, &remote_addr, tcp_remote_port, tcp_client_connected);
  555. LWIP_ASSERT("Error: altcp_connect() failed", err == ERR_OK);
  556. altcp_recv(tcp_client_pcb, tcp_client_recv);
  557. altcp_err(tcp_client_pcb, tcp_client_err);
  558. altcp_poll(tcp_client_pcb, tcp_client_poll, 10);
  559. altcp_sent(tcp_client_pcb, tcp_client_sent);
  560. }
  561. if (test_apps & LWIP_FUZZ_TCP_SERVER) {
  562. tcp_server_pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY);
  563. LWIP_ASSERT("Error: altcp_new() failed", tcp_server_pcb != NULL);
  564. altcp_setprio(tcp_server_pcb, TCP_PRIO_MIN);
  565. tcp_local_port = 80;
  566. err = altcp_bind(tcp_server_pcb, IP_ANY_TYPE, tcp_local_port);
  567. LWIP_ASSERT("Error: altcp_bind() failed", err == ERR_OK);
  568. tcp_server_pcb = altcp_listen(tcp_server_pcb);
  569. LWIP_ASSERT("Error: altcp_listen() failed", err == ERR_OK);
  570. altcp_accept(tcp_server_pcb, tcp_server_accept);
  571. }
  572. if (test_apps & LWIP_FUZZ_UDP_CLIENT) {
  573. udp_client_pcb = udp_new();
  574. udp_new_ip_type(IPADDR_TYPE_ANY);
  575. udp_recv(udp_client_pcb, udp_client_recv, NULL);
  576. udp_remote_port = 161;
  577. udp_connect(udp_client_pcb, &remote_addr, udp_remote_port);
  578. }
  579. if (test_apps & LWIP_FUZZ_UDP_SERVER) {
  580. udp_server_pcb = udp_new();
  581. udp_new_ip_type(IPADDR_TYPE_ANY);
  582. udp_local_port = 161;
  583. udp_bind(udp_server_pcb, IP_ANY_TYPE, udp_local_port);
  584. udp_recv(udp_server_pcb, udp_server_recv, NULL);
  585. }
  586. if(argc > 1) {
  587. FILE* f;
  588. const char* filename;
  589. printf("reading input from file... ");
  590. fflush(stdout);
  591. filename = argv[1];
  592. LWIP_ASSERT("invalid filename", filename != NULL);
  593. f = fopen(filename, "rb");
  594. LWIP_ASSERT("open failed", f != NULL);
  595. len = fread(pktbuf, 1, sizeof(pktbuf), f);
  596. fclose(f);
  597. printf("testing file: \"%s\"...\r\n", filename);
  598. } else {
  599. len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
  600. }
  601. input_pkts(type, &net_test, pktbuf, len);
  602. pcap_dump_stop();
  603. return 0;
  604. }
  605. #ifdef LWIP_RAND_FOR_FUZZ
  606. u32_t lwip_fuzz_rand(void)
  607. {
  608. #ifdef LWIP_RAND_FOR_FUZZ_SIMULATE_GLIBC
  609. /* this is what glibc rand() returns (first 20 numbers) */
  610. static u32_t rand_nrs[] = {0x6b8b4567, 0x327b23c6, 0x643c9869, 0x66334873, 0x74b0dc51,
  611. 0x19495cff, 0x2ae8944a, 0x625558ec, 0x238e1f29, 0x46e87ccd,
  612. 0x3d1b58ba, 0x507ed7ab, 0x2eb141f2, 0x41b71efb, 0x79e2a9e3,
  613. 0x7545e146, 0x515f007c, 0x5bd062c2, 0x12200854, 0x4db127f8};
  614. static unsigned idx = 0;
  615. u32_t ret = rand_nrs[idx];
  616. idx++;
  617. if (idx >= sizeof(rand_nrs)/sizeof((rand_nrs)[0])) {
  618. idx = 0;
  619. }
  620. return ret;
  621. #else
  622. /* a simple LCG, unsafe but should give the same result for every execution (best for fuzzing) */
  623. u32_t result;
  624. static s32_t state[1] = {0xdeadbeef};
  625. uint64_t val = state[0] & 0xffffffff;
  626. val = ((val * 1103515245) + 12345) & 0x7fffffff;
  627. state[0] = (s32_t)val;
  628. result = (u32_t)val;
  629. return result;
  630. #endif
  631. }
  632. #endif