1
0

fuzz.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. *
  31. */
  32. #include "lwip/init.h"
  33. #include "lwip/netif.h"
  34. #include "lwip/dns.h"
  35. #include "netif/etharp.h"
  36. #if LWIP_IPV6
  37. #include "lwip/ethip6.h"
  38. #include "lwip/nd6.h"
  39. #endif
  40. #include "lwip/apps/httpd.h"
  41. #include "lwip/apps/snmp.h"
  42. #include "lwip/apps/lwiperf.h"
  43. #include "lwip/apps/mdns.h"
  44. #include <string.h>
  45. #include <stdio.h>
  46. /* This define enables multi packet processing.
  47. * For this, the input is interpreted as 2 byte length + data + 2 byte length + data...
  48. * #define LWIP_FUZZ_MULTI_PACKET
  49. */
  50. #ifdef LWIP_FUZZ_MULTI_PACKET
  51. u8_t pktbuf[20000];
  52. #else
  53. u8_t pktbuf[2000];
  54. #endif
  55. /* no-op send function */
  56. static err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
  57. {
  58. LWIP_UNUSED_ARG(netif);
  59. LWIP_UNUSED_ARG(p);
  60. return ERR_OK;
  61. }
  62. static err_t testif_init(struct netif *netif)
  63. {
  64. netif->name[0] = 'f';
  65. netif->name[1] = 'z';
  66. netif->output = etharp_output;
  67. netif->linkoutput = lwip_tx_func;
  68. netif->mtu = 1500;
  69. netif->hwaddr_len = 6;
  70. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
  71. netif->hwaddr[0] = 0x00;
  72. netif->hwaddr[1] = 0x23;
  73. netif->hwaddr[2] = 0xC1;
  74. netif->hwaddr[3] = 0xDE;
  75. netif->hwaddr[4] = 0xD0;
  76. netif->hwaddr[5] = 0x0D;
  77. #if LWIP_IPV6
  78. netif->output_ip6 = ethip6_output;
  79. netif->ip6_autoconfig_enabled = 1;
  80. netif_create_ip6_linklocal_address(netif, 1);
  81. netif->flags |= NETIF_FLAG_MLD6;
  82. #endif
  83. return ERR_OK;
  84. }
  85. static void input_pkt(struct netif *netif, const u8_t *data, size_t len)
  86. {
  87. struct pbuf *p, *q;
  88. err_t err;
  89. LWIP_ASSERT("pkt too big", len <= 0xFFFF);
  90. p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
  91. LWIP_ASSERT("alloc failed", p);
  92. for(q = p; q != NULL; q = q->next) {
  93. MEMCPY(q->payload, data, q->len);
  94. data += q->len;
  95. }
  96. err = netif->input(p, netif);
  97. if (err != ERR_OK) {
  98. pbuf_free(p);
  99. }
  100. }
  101. static void input_pkts(struct netif *netif, const u8_t *data, size_t len)
  102. {
  103. #ifdef LWIP_FUZZ_MULTI_PACKET
  104. const u16_t max_packet_size = 1514;
  105. const u8_t *ptr = data;
  106. size_t rem_len = len;
  107. while (rem_len > sizeof(u16_t)) {
  108. u16_t frame_len;
  109. memcpy(&frame_len, ptr, sizeof(u16_t));
  110. ptr += sizeof(u16_t);
  111. rem_len -= sizeof(u16_t);
  112. frame_len = htons(frame_len) & 0x7FF;
  113. frame_len = LWIP_MIN(frame_len, max_packet_size);
  114. if (frame_len > rem_len) {
  115. frame_len = (u16_t)rem_len;
  116. }
  117. if (frame_len != 0) {
  118. input_pkt(netif, ptr, frame_len);
  119. }
  120. ptr += frame_len;
  121. rem_len -= frame_len;
  122. }
  123. #else /* LWIP_FUZZ_MULTI_PACKET */
  124. input_pkt(netif, data, len);
  125. #endif /* LWIP_FUZZ_MULTI_PACKET */
  126. }
  127. int main(int argc, char** argv)
  128. {
  129. struct netif net_test;
  130. ip4_addr_t addr;
  131. ip4_addr_t netmask;
  132. ip4_addr_t gw;
  133. size_t len;
  134. lwip_init();
  135. IP4_ADDR(&addr, 172, 30, 115, 84);
  136. IP4_ADDR(&netmask, 255, 255, 255, 0);
  137. IP4_ADDR(&gw, 172, 30, 115, 1);
  138. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  139. netif_set_up(&net_test);
  140. netif_set_link_up(&net_test);
  141. #if LWIP_IPV6
  142. nd6_tmr(); /* tick nd to join multicast groups */
  143. #endif
  144. dns_setserver(0, &net_test.gw);
  145. /* initialize apps */
  146. httpd_init();
  147. lwiperf_start_tcp_server_default(NULL, NULL);
  148. mdns_resp_init();
  149. mdns_resp_add_netif(&net_test, "hostname", 255);
  150. snmp_init();
  151. if(argc > 1) {
  152. FILE* f;
  153. const char* filename;
  154. printf("reading input from file... ");
  155. fflush(stdout);
  156. filename = argv[1];
  157. LWIP_ASSERT("invalid filename", filename != NULL);
  158. f = fopen(filename, "rb");
  159. LWIP_ASSERT("open failed", f != NULL);
  160. len = fread(pktbuf, 1, sizeof(pktbuf), f);
  161. fclose(f);
  162. printf("testing file: \"%s\"...\r\n", filename);
  163. } else {
  164. len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
  165. }
  166. input_pkts(&net_test, pktbuf, len);
  167. return 0;
  168. }