fuzz_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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, (0x1000977e, "> %d fuzz: netif: send %u bytes\n", pcap_packet, p->tot_len));
  105. } else {
  106. LWIP_DEBUGF(FUZZ_DEBUG, (0x1000977f, "< %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. LWIP_ASSERT("pkt too big", len <= 0xFFFF);
  174. p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
  175. LWIP_ASSERT("alloc failed", p);
  176. for(q = p; q != NULL; q = q->next) {
  177. MEMCPY(q->payload, data, q->len);
  178. data += q->len;
  179. }
  180. remfuzz_ptr += len;
  181. remfuzz_len -= len;
  182. pcap_dump_rx_packet(p);
  183. err = netif->input(p, netif);
  184. if (err != ERR_OK) {
  185. pbuf_free(p);
  186. }
  187. }
  188. static void input_pkts(enum lwip_fuzz_type type, struct netif *netif, const u8_t *data, size_t len)
  189. {
  190. remfuzz_ptr = data;
  191. remfuzz_len = len;
  192. if (type == LWIP_FUZZ_SINGLE) {
  193. input_pkt(netif, data, len);
  194. } else {
  195. const u16_t max_packet_size = 1514;
  196. const size_t minlen = sizeof(u16_t) + (type == LWIP_FUZZ_MULTIPACKET_TIME ? sizeof(u32_t) : 0);
  197. while (remfuzz_len > minlen) {
  198. u16_t frame_len;
  199. #ifdef LWIP_FUZZ_SYS_NOW
  200. u32_t external_delay = 0;
  201. #endif
  202. if (type == LWIP_FUZZ_MULTIPACKET_TIME) {
  203. #ifdef LWIP_FUZZ_SYS_NOW
  204. /* Extract external delay time from fuzz pool */
  205. memcpy(&external_delay, remfuzz_ptr, sizeof(u32_t));
  206. external_delay = htonl(external_delay);
  207. #endif
  208. remfuzz_ptr += sizeof(u32_t);
  209. remfuzz_len -= sizeof(u32_t);
  210. }
  211. memcpy(&frame_len, remfuzz_ptr, sizeof(u16_t));
  212. remfuzz_ptr += sizeof(u16_t);
  213. remfuzz_len -= sizeof(u16_t);
  214. frame_len = htons(frame_len) & 0x7FF;
  215. frame_len = LWIP_MIN(frame_len, max_packet_size);
  216. if (frame_len > remfuzz_len) {
  217. frame_len = (u16_t)remfuzz_len;
  218. }
  219. if (frame_len != 0) {
  220. if (type == LWIP_FUZZ_MULTIPACKET_TIME) {
  221. #ifdef LWIP_FUZZ_SYS_NOW
  222. /* Update total external delay time, and check timeouts */
  223. sys_now_offset += external_delay;
  224. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009780, "fuzz: sys_now_offset += %u -> %u\n", external_delay, sys_now_offset));
  225. #endif
  226. sys_check_timeouts();
  227. }
  228. input_pkt(netif, remfuzz_ptr, frame_len);
  229. /* Check timeouts again */
  230. sys_check_timeouts();
  231. }
  232. }
  233. }
  234. }
  235. #if LWIP_TCP
  236. static struct altcp_pcb *tcp_client_pcb; /* a pcb for the TCP client */
  237. static struct altcp_pcb *tcp_server_pcb; /* a pcb for the TCP server */
  238. static u16_t tcp_remote_port; /* a TCP port number of the destionation */
  239. static u16_t tcp_local_port; /* a TCP port number of the local server */
  240. /**
  241. * tcp_app_fuzz_input
  242. * Input fuzz with a write function for TCP.
  243. */
  244. static void
  245. tcp_app_fuzz_input(struct altcp_pcb *pcb)
  246. {
  247. if (remfuzz_len > sizeof(u16_t)) {
  248. /*
  249. * (max IP packet size) - ((minimum IP header size) + (minimum TCP header size))
  250. * = 65535 - (20 + 20)
  251. * = 65495
  252. */
  253. const u16_t max_data_size = 65495;
  254. u16_t data_len;
  255. memcpy(&data_len, remfuzz_ptr, sizeof(u16_t));
  256. remfuzz_ptr += sizeof(u16_t);
  257. remfuzz_len -= sizeof(u16_t);
  258. data_len = htons(data_len);
  259. data_len = LWIP_MIN(data_len, max_data_size);
  260. if (data_len > remfuzz_len) {
  261. data_len = (u16_t)remfuzz_len;
  262. }
  263. if (data_len != 0) {
  264. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009781, "fuzz: tcp: write %u bytes\n", data_len));
  265. altcp_write(pcb, remfuzz_ptr, data_len, TCP_WRITE_FLAG_COPY);
  266. altcp_output(pcb);
  267. } else {
  268. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009782, "fuzz: tcp: close\n"));
  269. altcp_close(pcb);
  270. }
  271. remfuzz_ptr += data_len;
  272. remfuzz_len -= data_len;
  273. }
  274. }
  275. /**
  276. * tcp_client_connected
  277. * A connected callback function (for the TCP client)
  278. */
  279. static err_t
  280. tcp_client_connected(void *arg, struct altcp_pcb *pcb, err_t err)
  281. {
  282. LWIP_UNUSED_ARG(arg);
  283. LWIP_UNUSED_ARG(err);
  284. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009783, "fuzz: tcp: tcp_client_connected\n"));
  285. tcp_app_fuzz_input(pcb);
  286. return ERR_OK;
  287. }
  288. /**
  289. * tcp_client_recv
  290. * A recv callback function (for the TCP client)
  291. */
  292. static err_t
  293. tcp_client_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
  294. {
  295. LWIP_UNUSED_ARG(arg);
  296. LWIP_UNUSED_ARG(err);
  297. if (p == NULL) {
  298. altcp_close(pcb);
  299. } else {
  300. altcp_recved(pcb, p->tot_len);
  301. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009784, "fuzz: tcp: tcp_client_recv: %d\n", p->tot_len));
  302. tcp_app_fuzz_input(pcb);
  303. pbuf_free(p);
  304. }
  305. return ERR_OK;
  306. }
  307. /**
  308. * tcp_client_sent
  309. * A sent callback function (for the TCP client)
  310. */
  311. static err_t
  312. tcp_client_sent(void *arg, struct altcp_pcb *pcb, u16_t len)
  313. {
  314. LWIP_UNUSED_ARG(arg);
  315. LWIP_UNUSED_ARG(pcb);
  316. LWIP_UNUSED_ARG(len);
  317. return ERR_OK;
  318. }
  319. /**
  320. * tcp_client_poll
  321. * A poll callback function (for the TCP client)
  322. */
  323. static err_t
  324. tcp_client_poll(void *arg, struct altcp_pcb *pcb)
  325. {
  326. LWIP_UNUSED_ARG(arg);
  327. LWIP_UNUSED_ARG(pcb);
  328. return ERR_OK;
  329. }
  330. /**
  331. * tcp_client_err
  332. * An err callback function (for the TCP client)
  333. */
  334. static void
  335. tcp_client_err(void *arg, err_t err)
  336. {
  337. LWIP_UNUSED_ARG(arg);
  338. LWIP_UNUSED_ARG(err);
  339. }
  340. /**
  341. * tcp_server_recv
  342. * A recv callback function (for the TCP server)
  343. */
  344. static err_t
  345. tcp_server_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
  346. {
  347. LWIP_UNUSED_ARG(arg);
  348. LWIP_UNUSED_ARG(err);
  349. if (p == NULL) {
  350. altcp_close(pcb);
  351. } else {
  352. altcp_recved(pcb, p->tot_len);
  353. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009785, "fuzz: tcp: tcp_server_recv: %d\n", p->tot_len));
  354. tcp_app_fuzz_input(pcb);
  355. pbuf_free(p);
  356. }
  357. return ERR_OK;
  358. }
  359. /**
  360. * tcp_server_sent
  361. * A sent callback function (for the TCP server)
  362. */
  363. static err_t
  364. tcp_server_sent(void *arg, struct altcp_pcb *pcb, u16_t len)
  365. {
  366. LWIP_UNUSED_ARG(arg);
  367. LWIP_UNUSED_ARG(pcb);
  368. LWIP_UNUSED_ARG(len);
  369. return ERR_OK;
  370. }
  371. /**
  372. * tcp_server_poll
  373. * A poll callback function (for the TCP server)
  374. */
  375. static err_t
  376. tcp_server_poll(void *arg, struct altcp_pcb *pcb)
  377. {
  378. LWIP_UNUSED_ARG(arg);
  379. LWIP_UNUSED_ARG(pcb);
  380. return ERR_OK;
  381. }
  382. /**
  383. * tcp_server_err
  384. * An err callbuck function (for the TCP server)
  385. */
  386. static void
  387. tcp_server_err(void *arg, err_t err)
  388. {
  389. LWIP_UNUSED_ARG(arg);
  390. LWIP_UNUSED_ARG(err);
  391. }
  392. /**
  393. * tcp_server_accept
  394. * An accept callbuck function (for the TCP server)
  395. */
  396. static err_t
  397. tcp_server_accept(void *arg, struct altcp_pcb *pcb, err_t err)
  398. {
  399. LWIP_UNUSED_ARG(arg);
  400. LWIP_UNUSED_ARG(err);
  401. if ((err != ERR_OK) || (pcb == NULL)) {
  402. return ERR_VAL;
  403. }
  404. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009786, "fuzz: accept from remote\n"));
  405. altcp_setprio(pcb, TCP_PRIO_MIN);
  406. altcp_recv(pcb, tcp_server_recv);
  407. altcp_err(pcb, tcp_server_err);
  408. altcp_poll(pcb, tcp_server_poll, 10);
  409. altcp_sent(pcb, tcp_server_sent);
  410. return ERR_OK;
  411. }
  412. #endif /* LWIP_TCP */
  413. #if LWIP_UDP
  414. static struct udp_pcb *udp_client_pcb; /* a pcb for the UDP client */
  415. static struct udp_pcb *udp_server_pcb; /* a pcb for the UDP server */
  416. static u16_t udp_remote_port; /* a UDP port number of the destination */
  417. static u16_t udp_local_port; /* a UDP port number of the local server*/
  418. /**
  419. * udp_app_fuzz_input
  420. * Input fuzz with write functions for UDP.
  421. */
  422. static void
  423. udp_app_fuzz_input(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port)
  424. {
  425. if (remfuzz_len > sizeof(u16_t)) {
  426. /*
  427. * (max IP packet size) - ((minimum IP header size) - (minimum UDP header size))
  428. * = 65535 - (20 + 8)
  429. * = 65507
  430. */
  431. const u16_t max_data_size = 65507;
  432. u16_t data_len;
  433. memcpy(&data_len, remfuzz_ptr, sizeof(u16_t));
  434. remfuzz_ptr += sizeof(u16_t);
  435. remfuzz_len -= sizeof(u16_t);
  436. data_len = htons(data_len);
  437. data_len = LWIP_MIN(data_len, max_data_size);
  438. if (data_len > remfuzz_len) {
  439. data_len = (u16_t)remfuzz_len;
  440. }
  441. LWIP_DEBUGF(FUZZ_DEBUG, (0x10009787, "fuzz: udp: send %u bytes\n", data_len));
  442. if (data_len != 0) {
  443. struct pbuf *p, *q;
  444. p = pbuf_alloc(PBUF_RAW, (u16_t)data_len, PBUF_POOL);
  445. LWIP_ASSERT("alloc failed", p);
  446. for (q = p; q != NULL; q = q->next) {
  447. MEMCPY(q->payload, remfuzz_ptr, q->len);
  448. remfuzz_ptr += q->len;
  449. }
  450. remfuzz_len -= data_len;
  451. /*
  452. * Trying input from ...
  453. *
  454. * client:
  455. * The pcb has information about the destination.
  456. * We use udp_send().
  457. *
  458. * server:
  459. * The pcb does NOT have infomation about the destionation.
  460. * We use udp_sendto().
  461. */
  462. if (addr == NULL) {
  463. udp_send(pcb, p);
  464. } else {
  465. udp_sendto(pcb, p, addr, port);
  466. }
  467. pbuf_free(p);
  468. }
  469. }
  470. }
  471. /**
  472. * udp_client_recv
  473. * A recv callback function (for the UDP client)
  474. */
  475. static void
  476. udp_client_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  477. {
  478. LWIP_UNUSED_ARG(arg);
  479. LWIP_UNUSED_ARG(p);
  480. LWIP_UNUSED_ARG(addr);
  481. LWIP_UNUSED_ARG(port);
  482. if (p == NULL) {
  483. udp_disconnect(pcb);
  484. } else {
  485. /*
  486. * We call the function with 2nd argument set to NULL
  487. * to input fuzz from udp_send.
  488. */
  489. udp_app_fuzz_input(pcb, NULL, port);
  490. pbuf_free(p);
  491. }
  492. }
  493. /**
  494. * udp_server_recv
  495. * A recv callback functyion (for the UDP server)
  496. */
  497. static void
  498. udp_server_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  499. {
  500. LWIP_UNUSED_ARG(arg);
  501. LWIP_UNUSED_ARG(p);
  502. LWIP_UNUSED_ARG(addr);
  503. LWIP_UNUSED_ARG(port);
  504. if (p != NULL) {
  505. udp_app_fuzz_input(pcb, addr, port);
  506. pbuf_free(p);
  507. }
  508. }
  509. #endif /* LWIP_UDP */
  510. int lwip_fuzztest(int argc, char** argv, enum lwip_fuzz_type type, u32_t test_apps)
  511. {
  512. struct netif net_test;
  513. ip4_addr_t addr;
  514. ip4_addr_t netmask;
  515. ip4_addr_t gw;
  516. size_t len;
  517. err_t err;
  518. ip_addr_t remote_addr; /* a IPv4 addr of the destination */
  519. struct eth_addr remote_mac = ETH_ADDR(0x28, 0x00, 0x00, 0x22, 0x2b, 0x38); /* a MAC addr of the destination */
  520. pcap_dump_init();
  521. lwip_init();
  522. IP4_ADDR(&addr, 172, 30, 115, 84);
  523. IP4_ADDR(&netmask, 255, 255, 255, 0);
  524. IP4_ADDR(&gw, 172, 30, 115, 1);
  525. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  526. netif_set_up(&net_test);
  527. netif_set_link_up(&net_test);
  528. if (test_apps & LWIP_FUZZ_STATICARP) {
  529. /* Add the ARP entry */
  530. IP_ADDR4(&remote_addr, 172, 30, 115, 37);
  531. etharp_add_static_entry(&(remote_addr.u_addr.ip4), &remote_mac);
  532. }
  533. #if LWIP_IPV6
  534. nd6_tmr(); /* tick nd to join multicast groups */
  535. #endif
  536. dns_setserver(0, &net_test.gw);
  537. if (test_apps & LWIP_FUZZ_DEFAULT) {
  538. /* initialize apps */
  539. httpd_init();
  540. lwiperf_start_tcp_server_default(NULL, NULL);
  541. mdns_resp_init();
  542. mdns_resp_add_netif(&net_test, "hostname");
  543. snmp_init();
  544. }
  545. if (test_apps & LWIP_FUZZ_TCP_CLIENT) {
  546. tcp_client_pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY);
  547. LWIP_ASSERT("Error: altcp_new() failed", tcp_client_pcb != NULL);
  548. tcp_remote_port = 80;
  549. err = altcp_connect(tcp_client_pcb, &remote_addr, tcp_remote_port, tcp_client_connected);
  550. LWIP_ASSERT("Error: altcp_connect() failed", err == ERR_OK);
  551. altcp_recv(tcp_client_pcb, tcp_client_recv);
  552. altcp_err(tcp_client_pcb, tcp_client_err);
  553. altcp_poll(tcp_client_pcb, tcp_client_poll, 10);
  554. altcp_sent(tcp_client_pcb, tcp_client_sent);
  555. }
  556. if (test_apps & LWIP_FUZZ_TCP_SERVER) {
  557. tcp_server_pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY);
  558. LWIP_ASSERT("Error: altcp_new() failed", tcp_server_pcb != NULL);
  559. altcp_setprio(tcp_server_pcb, TCP_PRIO_MIN);
  560. tcp_local_port = 80;
  561. err = altcp_bind(tcp_server_pcb, IP_ANY_TYPE, tcp_local_port);
  562. LWIP_ASSERT("Error: altcp_bind() failed", err == ERR_OK);
  563. tcp_server_pcb = altcp_listen(tcp_server_pcb);
  564. LWIP_ASSERT("Error: altcp_listen() failed", err == ERR_OK);
  565. altcp_accept(tcp_server_pcb, tcp_server_accept);
  566. }
  567. if (test_apps & LWIP_FUZZ_UDP_CLIENT) {
  568. udp_client_pcb = udp_new();
  569. udp_new_ip_type(IPADDR_TYPE_ANY);
  570. udp_recv(udp_client_pcb, udp_client_recv, NULL);
  571. udp_remote_port = 161;
  572. udp_connect(udp_client_pcb, &remote_addr, udp_remote_port);
  573. }
  574. if (test_apps & LWIP_FUZZ_UDP_SERVER) {
  575. udp_server_pcb = udp_new();
  576. udp_new_ip_type(IPADDR_TYPE_ANY);
  577. udp_local_port = 161;
  578. udp_bind(udp_server_pcb, IP_ANY_TYPE, udp_local_port);
  579. udp_recv(udp_server_pcb, udp_server_recv, NULL);
  580. }
  581. if(argc > 1) {
  582. FILE* f;
  583. const char* filename;
  584. printf("reading input from file... ");
  585. fflush(stdout);
  586. filename = argv[1];
  587. LWIP_ASSERT("invalid filename", filename != NULL);
  588. f = fopen(filename, "rb");
  589. LWIP_ASSERT("open failed", f != NULL);
  590. len = fread(pktbuf, 1, sizeof(pktbuf), f);
  591. fclose(f);
  592. printf("testing file: \"%s\"...\r\n", filename);
  593. } else {
  594. len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
  595. }
  596. input_pkts(type, &net_test, pktbuf, len);
  597. pcap_dump_stop();
  598. return 0;
  599. }
  600. #ifdef LWIP_RAND_FOR_FUZZ
  601. u32_t lwip_fuzz_rand(void)
  602. {
  603. #ifdef LWIP_RAND_FOR_FUZZ_SIMULATE_GLIBC
  604. /* this is what glibc rand() returns (first 20 numbers) */
  605. static u32_t rand_nrs[] = {0x6b8b4567, 0x327b23c6, 0x643c9869, 0x66334873, 0x74b0dc51,
  606. 0x19495cff, 0x2ae8944a, 0x625558ec, 0x238e1f29, 0x46e87ccd,
  607. 0x3d1b58ba, 0x507ed7ab, 0x2eb141f2, 0x41b71efb, 0x79e2a9e3,
  608. 0x7545e146, 0x515f007c, 0x5bd062c2, 0x12200854, 0x4db127f8};
  609. static unsigned idx = 0;
  610. u32_t ret = rand_nrs[idx];
  611. idx++;
  612. if (idx >= sizeof(rand_nrs)/sizeof((rand_nrs)[0])) {
  613. idx = 0;
  614. }
  615. return ret;
  616. #else
  617. /* a simple LCG, unsafe but should give the same result for every execution (best for fuzzing) */
  618. u32_t result;
  619. static s32_t state[1] = {0xdeadbeef};
  620. s32_t val = state[0];
  621. val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
  622. state[0] = val;
  623. result = val;
  624. return result;
  625. #endif
  626. }
  627. #endif