xref: /third_party/lwip/test/fuzz/fuzz.c (revision 195972f6)
1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3195972f6Sopenharmony_ci * All rights reserved.
4195972f6Sopenharmony_ci *
5195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
6195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
7195972f6Sopenharmony_ci *
8195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
9195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
10195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
11195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
12195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
13195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
14195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
15195972f6Sopenharmony_ci *
16195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25195972f6Sopenharmony_ci * OF SUCH DAMAGE.
26195972f6Sopenharmony_ci *
27195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
28195972f6Sopenharmony_ci *
29195972f6Sopenharmony_ci * Author: Erik Ekman <erik@kryo.se>
30195972f6Sopenharmony_ci *
31195972f6Sopenharmony_ci */
32195972f6Sopenharmony_ci
33195972f6Sopenharmony_ci#include "lwip/init.h"
34195972f6Sopenharmony_ci#include "lwip/netif.h"
35195972f6Sopenharmony_ci#include "lwip/dns.h"
36195972f6Sopenharmony_ci#include "netif/etharp.h"
37195972f6Sopenharmony_ci#if LWIP_IPV6
38195972f6Sopenharmony_ci#include "lwip/ethip6.h"
39195972f6Sopenharmony_ci#include "lwip/nd6.h"
40195972f6Sopenharmony_ci#endif
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#include "lwip/apps/httpd.h"
43195972f6Sopenharmony_ci#include "lwip/apps/snmp.h"
44195972f6Sopenharmony_ci#include "lwip/apps/lwiperf.h"
45195972f6Sopenharmony_ci#include "lwip/apps/mdns.h"
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci#include <string.h>
48195972f6Sopenharmony_ci#include <stdio.h>
49195972f6Sopenharmony_ci
50195972f6Sopenharmony_ci/* This define enables multi packet processing.
51195972f6Sopenharmony_ci * For this, the input is interpreted as 2 byte length + data + 2 byte length + data...
52195972f6Sopenharmony_ci * #define LWIP_FUZZ_MULTI_PACKET
53195972f6Sopenharmony_ci*/
54195972f6Sopenharmony_ci#ifdef LWIP_FUZZ_MULTI_PACKET
55195972f6Sopenharmony_ciu8_t pktbuf[20000];
56195972f6Sopenharmony_ci#else
57195972f6Sopenharmony_ciu8_t pktbuf[2000];
58195972f6Sopenharmony_ci#endif
59195972f6Sopenharmony_ci
60195972f6Sopenharmony_ci/* no-op send function */
61195972f6Sopenharmony_cistatic err_t lwip_tx_func(struct netif *netif, struct pbuf *p)
62195972f6Sopenharmony_ci{
63195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(netif);
64195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(p);
65195972f6Sopenharmony_ci  return ERR_OK;
66195972f6Sopenharmony_ci}
67195972f6Sopenharmony_ci
68195972f6Sopenharmony_cistatic err_t testif_init(struct netif *netif)
69195972f6Sopenharmony_ci{
70195972f6Sopenharmony_ci  netif->name[0] = 'f';
71195972f6Sopenharmony_ci  netif->name[1] = 'z';
72195972f6Sopenharmony_ci  netif->output = etharp_output;
73195972f6Sopenharmony_ci  netif->linkoutput = lwip_tx_func;
74195972f6Sopenharmony_ci  netif->mtu = 1500;
75195972f6Sopenharmony_ci  netif->hwaddr_len = 6;
76195972f6Sopenharmony_ci  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
77195972f6Sopenharmony_ci
78195972f6Sopenharmony_ci  netif->hwaddr[0] = 0x00;
79195972f6Sopenharmony_ci  netif->hwaddr[1] = 0x23;
80195972f6Sopenharmony_ci  netif->hwaddr[2] = 0xC1;
81195972f6Sopenharmony_ci  netif->hwaddr[3] = 0xDE;
82195972f6Sopenharmony_ci  netif->hwaddr[4] = 0xD0;
83195972f6Sopenharmony_ci  netif->hwaddr[5] = 0x0D;
84195972f6Sopenharmony_ci
85195972f6Sopenharmony_ci#if LWIP_IPV6
86195972f6Sopenharmony_ci  netif->output_ip6 = ethip6_output;
87195972f6Sopenharmony_ci  netif->ip6_autoconfig_enabled = 1;
88195972f6Sopenharmony_ci  netif_create_ip6_linklocal_address(netif, 1);
89195972f6Sopenharmony_ci  netif->flags |= NETIF_FLAG_MLD6;
90195972f6Sopenharmony_ci#endif
91195972f6Sopenharmony_ci
92195972f6Sopenharmony_ci  return ERR_OK;
93195972f6Sopenharmony_ci}
94195972f6Sopenharmony_ci
95195972f6Sopenharmony_cistatic void input_pkt(struct netif *netif, const u8_t *data, size_t len)
96195972f6Sopenharmony_ci{
97195972f6Sopenharmony_ci  struct pbuf *p, *q;
98195972f6Sopenharmony_ci  err_t err;
99195972f6Sopenharmony_ci
100195972f6Sopenharmony_ci  LWIP_ASSERT("pkt too big", len <= 0xFFFF);
101195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);
102195972f6Sopenharmony_ci  LWIP_ASSERT("alloc failed", p);
103195972f6Sopenharmony_ci  for(q = p; q != NULL; q = q->next) {
104195972f6Sopenharmony_ci    MEMCPY(q->payload, data, q->len);
105195972f6Sopenharmony_ci    data += q->len;
106195972f6Sopenharmony_ci  }
107195972f6Sopenharmony_ci  err = netif->input(p, netif);
108195972f6Sopenharmony_ci  if (err != ERR_OK) {
109195972f6Sopenharmony_ci    pbuf_free(p);
110195972f6Sopenharmony_ci  }
111195972f6Sopenharmony_ci}
112195972f6Sopenharmony_ci
113195972f6Sopenharmony_cistatic void input_pkts(struct netif *netif, const u8_t *data, size_t len)
114195972f6Sopenharmony_ci{
115195972f6Sopenharmony_ci#ifdef LWIP_FUZZ_MULTI_PACKET
116195972f6Sopenharmony_ci  const u16_t max_packet_size = 1514;
117195972f6Sopenharmony_ci  const u8_t *ptr = data;
118195972f6Sopenharmony_ci  size_t rem_len = len;
119195972f6Sopenharmony_ci
120195972f6Sopenharmony_ci  while (rem_len > sizeof(u16_t)) {
121195972f6Sopenharmony_ci    u16_t frame_len;
122195972f6Sopenharmony_ci    memcpy(&frame_len, ptr, sizeof(u16_t));
123195972f6Sopenharmony_ci    ptr += sizeof(u16_t);
124195972f6Sopenharmony_ci    rem_len -= sizeof(u16_t);
125195972f6Sopenharmony_ci    frame_len = htons(frame_len) & 0x7FF;
126195972f6Sopenharmony_ci    frame_len = LWIP_MIN(frame_len, max_packet_size);
127195972f6Sopenharmony_ci    if (frame_len > rem_len) {
128195972f6Sopenharmony_ci      frame_len = (u16_t)rem_len;
129195972f6Sopenharmony_ci    }
130195972f6Sopenharmony_ci    if (frame_len != 0) {
131195972f6Sopenharmony_ci      input_pkt(netif, ptr, frame_len);
132195972f6Sopenharmony_ci    }
133195972f6Sopenharmony_ci    ptr += frame_len;
134195972f6Sopenharmony_ci    rem_len -= frame_len;
135195972f6Sopenharmony_ci  }
136195972f6Sopenharmony_ci#else /* LWIP_FUZZ_MULTI_PACKET */
137195972f6Sopenharmony_ci  input_pkt(netif, data, len);
138195972f6Sopenharmony_ci#endif /* LWIP_FUZZ_MULTI_PACKET */
139195972f6Sopenharmony_ci}
140195972f6Sopenharmony_ci
141195972f6Sopenharmony_ciint main(int argc, char** argv)
142195972f6Sopenharmony_ci{
143195972f6Sopenharmony_ci  struct netif net_test;
144195972f6Sopenharmony_ci  ip4_addr_t addr;
145195972f6Sopenharmony_ci  ip4_addr_t netmask;
146195972f6Sopenharmony_ci  ip4_addr_t gw;
147195972f6Sopenharmony_ci  size_t len;
148195972f6Sopenharmony_ci
149195972f6Sopenharmony_ci  lwip_init();
150195972f6Sopenharmony_ci
151195972f6Sopenharmony_ci  IP4_ADDR(&addr, 172, 30, 115, 84);
152195972f6Sopenharmony_ci  IP4_ADDR(&netmask, 255, 255, 255, 0);
153195972f6Sopenharmony_ci  IP4_ADDR(&gw, 172, 30, 115, 1);
154195972f6Sopenharmony_ci
155195972f6Sopenharmony_ci  netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
156195972f6Sopenharmony_ci  netif_set_up(&net_test);
157195972f6Sopenharmony_ci  netif_set_link_up(&net_test);
158195972f6Sopenharmony_ci
159195972f6Sopenharmony_ci#if LWIP_IPV6
160195972f6Sopenharmony_ci  nd6_tmr(); /* tick nd to join multicast groups */
161195972f6Sopenharmony_ci#endif
162195972f6Sopenharmony_ci  dns_setserver(0, &net_test.gw);
163195972f6Sopenharmony_ci
164195972f6Sopenharmony_ci  /* initialize apps */
165195972f6Sopenharmony_ci  httpd_init();
166195972f6Sopenharmony_ci  lwiperf_start_tcp_server_default(NULL, NULL);
167195972f6Sopenharmony_ci  mdns_resp_init();
168195972f6Sopenharmony_ci  mdns_resp_add_netif(&net_test, "hostname", 255);
169195972f6Sopenharmony_ci  snmp_init();
170195972f6Sopenharmony_ci
171195972f6Sopenharmony_ci  if(argc > 1) {
172195972f6Sopenharmony_ci    FILE* f;
173195972f6Sopenharmony_ci    const char* filename;
174195972f6Sopenharmony_ci    printf("reading input from file... ");
175195972f6Sopenharmony_ci    fflush(stdout);
176195972f6Sopenharmony_ci    filename = argv[1];
177195972f6Sopenharmony_ci    LWIP_ASSERT("invalid filename", filename != NULL);
178195972f6Sopenharmony_ci    f = fopen(filename, "rb");
179195972f6Sopenharmony_ci    LWIP_ASSERT("open failed", f != NULL);
180195972f6Sopenharmony_ci    len = fread(pktbuf, 1, sizeof(pktbuf), f);
181195972f6Sopenharmony_ci    fclose(f);
182195972f6Sopenharmony_ci    printf("testing file: \"%s\"...\r\n", filename);
183195972f6Sopenharmony_ci  } else {
184195972f6Sopenharmony_ci    len = fread(pktbuf, 1, sizeof(pktbuf), stdin);
185195972f6Sopenharmony_ci  }
186195972f6Sopenharmony_ci  input_pkts(&net_test, pktbuf, len);
187195972f6Sopenharmony_ci
188195972f6Sopenharmony_ci  return 0;
189195972f6Sopenharmony_ci}
190