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