1 /**
2 * @file
3 * Modules initialization
4 *
5 */
6
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 */
37
38 #include "lwip/opt.h"
39
40 #include "lwip/init.h"
41 #include "lwip/stats.h"
42 #include "lwip/sys.h"
43 #include "lwip/mem.h"
44 #include "lwip/memp.h"
45 #include "lwip/pbuf.h"
46 #include "lwip/netif.h"
47 #include "lwip/sockets.h"
48 #include "lwip/ip.h"
49 #include "lwip/raw.h"
50 #include "lwip/udp.h"
51 #include "lwip/priv/tcp_priv.h"
52 #include "lwip/igmp.h"
53 #include "lwip/dns.h"
54 #include "lwip/timeouts.h"
55 #include "lwip/etharp.h"
56 #include "lwip/ip6.h"
57 #include "lwip/nd6.h"
58 #include "lwip/mld6.h"
59 #include "lwip/api.h"
60 #if LWIP_RIPPLE
61 #include "lwip/lwip_rpl.h"
62 #endif
63
64 #ifdef LWIP_EPOLL
65 void EpollEventPtrInit(void);
66 #endif
67
68 #ifndef LWIP_SKIP_PACKING_CHECK
69
70 #ifdef PACK_STRUCT_USE_INCLUDES
71 # include "arch/bpstruct.h"
72 #endif
73 PACK_STRUCT_BEGIN
74 struct packed_struct_test {
75 PACK_STRUCT_FLD_8(u8_t dummy1);
76 PACK_STRUCT_FIELD(u32_t dummy2);
77 } PACK_STRUCT_STRUCT;
78 PACK_STRUCT_END
79 #ifdef PACK_STRUCT_USE_INCLUDES
80 # include "arch/epstruct.h"
81 #endif
82 #define PACKED_STRUCT_TEST_EXPECTED_SIZE 5
83
84 #endif
85
86 /* Compile-time sanity checks for configuration errors.
87 * These can be done independently of LWIP_DEBUG, without penalty.
88 */
89 #ifndef BYTE_ORDER
90 #error "BYTE_ORDER is not defined, you have to define it in your cc.h"
91 #endif
92 #if (!IP_SOF_BROADCAST && IP_SOF_BROADCAST_RECV)
93 #error "If you want to use broadcast filter per pcb on recv operations, you have to define IP_SOF_BROADCAST=1 in your lwipopts.h"
94 #endif
95 #if (!LWIP_UDP && LWIP_UDPLITE)
96 #error "If you want to use UDP Lite, you have to define LWIP_UDP=1 in your lwipopts.h"
97 #endif
98 #if (!LWIP_UDP && LWIP_DHCP)
99 #error "If you want to use DHCP, you have to define LWIP_UDP=1 in your lwipopts.h"
100 #endif
101 #if (!LWIP_UDP && !LWIP_RAW && LWIP_MULTICAST_TX_OPTIONS)
102 #error "If you want to use LWIP_MULTICAST_TX_OPTIONS, you have to define LWIP_UDP=1 and/or LWIP_RAW=1 in your lwipopts.h"
103 #endif
104 #if (!LWIP_UDP && LWIP_DNS)
105 #error "If you want to use DNS, you have to define LWIP_UDP=1 in your lwipopts.h"
106 #endif
107 #ifdef LWIP_SNTP
108 #if (!LWIP_UDP && LWIP_SNTP)
109 #error "If you want to use SNTP, you have to define LWIP_UDP=1 in your lwipopts.h"
110 #endif
111 #endif
112 #if LWIP_WND_SCALE
113 #if (LWIP_TCP && (TCP_WND > 0xffffffff))
114 #error "If you want to use TCP, TCP_WND must fit in an u32_t, so, you have to reduce it in your lwipopts.h"
115 #endif
116 #if (LWIP_TCP && (TCP_RCV_SCALE > 14))
117 #error "The maximum valid window scale value is 14!"
118 #endif
119 #if (LWIP_TCP && (TCP_WND > (0xFFFFU << TCP_RCV_SCALE)))
120 #error "TCP_WND is bigger than the configured LWIP_WND_SCALE allows!"
121 #endif
122 #if (LWIP_TCP && ((TCP_WND >> TCP_RCV_SCALE) == 0))
123 #error "TCP_WND is too small for the configured LWIP_WND_SCALE (results in zero window)!"
124 #endif
125 #else /* LWIP_WND_SCALE */
126 #if (LWIP_TCP && (TCP_WND > 0xffff))
127 #error "If you want to use TCP, TCP_WND must fit in an u16_t, so, you have to reduce it in your lwipopts.h (or enable window scaling)"
128 #endif
129 #endif /* LWIP_WND_SCALE */
130
131 #if (LWIP_TCP && (INITIAL_SSTHRESH < 10 || INITIAL_SSTHRESH > 2000))
132 #pragma message(VAR_NAME_VALUE(INITIAL_SSTHRESH))
133 #error "If you want to use TCP, LWIP_CONGCNTRL_INITIAL_SSTHRESH must greater than 4 and less than 200, you can modify in lwipopts.h"
134 #endif
135
136 #if (LWIP_TCP && ((TCP_MAXRTX < TCP_MAXRTX_MIN_VALUE || TCP_MAXRTX > 255)))
137 #error "If you want to use TCP, TCP_MAXRTX must be greater than 3 and less or equal to 255,\
138 so, you have to reduce them in your lwipopts.h"
139 #endif
140 #if (LWIP_TCP && (TCP_SYNMAXRTX < TCP_MAXRTX_MIN_VALUE || TCP_SYNMAXRTX > 12))
141 #error "If you want to use TCP, TCP_SYNMAXRTX must be greater than 3 and less or equal to 12 ,\
142 so, you have to reduce them in your lwipopts.h"
143 #endif
144 #if (LWIP_TCP && ((TCP_FW1MAXRTX < TCP_MAXRTX_MIN_VALUE || TCP_FW1MAXRTX > 255)))
145 #error "If you want to use TCP, TCP_FW1MAXRTX must be greater than 3 and less or equal to 255,\
146 so, you have to reduce them in your lwipopts.h"
147 #endif
148 #if (LWIP_TCP && (TCP_TTL < TCP_TTL_MIN_VALUE || TCP_TTL > 255))
149 #error "If you want to use TCP, TCP_TTL must be greater than 8 and less or equal to 255, so, you have to reduce them in your lwipopts.h"
150 #endif
151 #if (LWIP_TCP && TCP_LISTEN_BACKLOG && ((TCP_DEFAULT_LISTEN_BACKLOG < 0) || (TCP_DEFAULT_LISTEN_BACKLOG > 0xff)))
152 #error "If you want to use TCP backlog, TCP_DEFAULT_LISTEN_BACKLOG must fit into an u8_t"
153 #endif
154 #if (LWIP_TCP && LWIP_TCP_ER_SUPPORT && (DUPACK_THRESH != 3))
155 #error "If you want to use TCP Early Retransmit, DUPACK_THRESH must be 3"
156 #endif
157 #if (defined(LWIP_TCP_TLP_SUPPORT) && (LWIP_TCP_TLP_SUPPORT == 1) && (!defined(LWIP_SACK) || (LWIP_SACK == 0)))
158 #error "If you want to use TCP support for TLP, LWIP_SACK must be enabled."
159 #endif
160 #if (LWIP_NETIF_API && (NO_SYS==1))
161 #error "If you want to use NETIF API, you have to define NO_SYS=0 in your lwipopts.h"
162 #endif
163 #if ((LWIP_SOCKET || LWIP_NETCONN) && (NO_SYS==1))
164 #error "If you want to use Sequential API, you have to define NO_SYS=0 in your lwipopts.h"
165 #endif
166 #if (!LWIP_NETCONN && LWIP_SOCKET)
167 #error "If you want to use Socket API, you have to define LWIP_NETCONN=1 in your lwipopts.h"
168 #endif
169 #if (((!LWIP_DHCP) || (!LWIP_AUTOIP)) && LWIP_DHCP_AUTOIP_COOP)
170 #error "If you want to use DHCP/AUTOIP cooperation mode, you have to define LWIP_DHCP=1 and LWIP_AUTOIP=1 in your lwipopts.h"
171 #endif
172 #if (((!LWIP_DHCP) || (!LWIP_ARP)) && DHCP_DOES_ARP_CHECK)
173 #error "If you want to use DHCP ARP checking, you have to define LWIP_DHCP=1 and LWIP_ARP=1 in your lwipopts.h"
174 #endif
175 #if (!LWIP_ARP && LWIP_AUTOIP)
176 #error "If you want to use AUTOIP, you have to define LWIP_ARP=1 in your lwipopts.h"
177 #endif
178 #if (LWIP_TCP && ((LWIP_EVENT_API && LWIP_CALLBACK_API) || (!LWIP_EVENT_API && !LWIP_CALLBACK_API)))
179 #error "One and exactly one of LWIP_EVENT_API and LWIP_CALLBACK_API has to be enabled in your lwipopts.h"
180 #endif
181 #if (LWIP_ALTCP && LWIP_EVENT_API)
182 #error "The application layered tcp API does not work with LWIP_EVENT_API"
183 #endif
184 #if (MEM_LIBC_MALLOC && MEM_USE_POOLS)
185 #error "MEM_LIBC_MALLOC and MEM_USE_POOLS may not both be simultaneously enabled in your lwipopts.h"
186 #endif
187 #if (MEM_USE_POOLS && !MEMP_USE_CUSTOM_POOLS)
188 #error "MEM_USE_POOLS requires custom pools (MEMP_USE_CUSTOM_POOLS) to be enabled in your lwipopts.h"
189 #endif
190 #if (PBUF_POOL_BUFSIZE <= MEM_ALIGNMENT)
191 #error "PBUF_POOL_BUFSIZE must be greater than MEM_ALIGNMENT or the offset may take the full first pbuf"
192 #endif
193 #if (DNS_LOCAL_HOSTLIST && !DNS_LOCAL_HOSTLIST_IS_DYNAMIC && !(defined(DNS_LOCAL_HOSTLIST_INIT)))
194 #error "you have to define define DNS_LOCAL_HOSTLIST_INIT {{'host1', 0x123}, {'host2', 0x234}} to initialize DNS_LOCAL_HOSTLIST"
195 #endif
196 #if !LWIP_ETHERNET && (LWIP_ARP || PPPOE_SUPPORT)
197 #error "LWIP_ETHERNET needs to be turned on for LWIP_ARP or PPPOE_SUPPORT"
198 #endif
199 #if LWIP_IGMP && !defined(LWIP_RAND)
200 #error "When using IGMP, LWIP_RAND() needs to be defined to a random-function returning an u32_t random value"
201 #endif
202 #if LWIP_TCPIP_CORE_LOCKING_INPUT && !LWIP_TCPIP_CORE_LOCKING
203 #error "When using LWIP_TCPIP_CORE_LOCKING_INPUT, LWIP_TCPIP_CORE_LOCKING must be enabled, too"
204 #endif
205 #if LWIP_TCP && LWIP_NETIF_TX_SINGLE_PBUF && !TCP_OVERSIZE
206 #error "LWIP_NETIF_TX_SINGLE_PBUF needs TCP_OVERSIZE enabled to create single-pbuf TCP packets"
207 #endif
208 #if LWIP_NETCONN && LWIP_TCP
209 #if NETCONN_COPY != TCP_WRITE_FLAG_COPY
210 #error "NETCONN_COPY != TCP_WRITE_FLAG_COPY"
211 #endif
212 #if NETCONN_MORE != TCP_WRITE_FLAG_MORE
213 #error "NETCONN_MORE != TCP_WRITE_FLAG_MORE"
214 #endif
215 #endif /* LWIP_NETCONN && LWIP_TCP */
216
217 #if LWIP_DHCPS && !ETHARP_SUPPORT_STATIC_ENTRIES
218 #error "ETHARP_SUPPORT_STATIC_ENTRIES needs to be turned on for LWIP_DHCPS"
219 #endif
220 #if LWIP_TFTP && !LWIP_UDP
221 #error "LWIP_UDP needs to be turned on for LWIP_TFTP, as tftp runs over UDP socket"
222 #endif
223 #if LWIP_TFTP && !LWIP_SOCKET
224 #error "LWIP_SOCKET needs to be turned on for LWIP_TFTP, as tftp runs over socket api"
225 #endif
226 #if LWIP_TFTP && !LWIP_SOCKET_SELECT
227 #error "LWIP_SOCKET_SELECT needs to be turned on for LWIP_TFTP, as tftp runs over socket select api"
228 #endif
229
230 #if DRIVER_STATUS_CHECK && !LWIP_TCP
231 #error "LWIP_TCP needs to be turned on for DRIVER_STATUS_CHECK, as DRIVER_STATUS_CHECK is base on tcp apis"
232 #endif
233 #if (DNS_TABLE_SIZE <= 0)
234 #error "DNS_TABLE_SIZE should be configured as greater than zero"
235 #endif
236 #if (DNS_MAX_IPADDR <= 0)
237 #error "DNS_MAX_IPADDR should be configured as greater than zero"
238 #endif
239 #if (DNS_MAX_SERVERS <= 0) || (DNS_MAX_SERVERS > 32)
240 #error "DNS_MAX_SERVERS should be configured as greater than zero and less or equal to 32"
241 #endif
242
243 #if (DNS_MAX_NAME_LENGTH != 256)
244 #error "DNS_MAX_NAME_LENGTH should be 256 as per RFC"
245 #endif
246
247 #if !LWIP_LITEOS_COMPAT && !LWIP_FREERTOS_COMPAT
248 #if LWIP_SOCKET
249 /* Check that the SO_* socket options and SOF_* lwIP-internal flags match */
250 #if SO_REUSEADDR != SOF_REUSEADDR
251 #error "WARNING: SO_REUSEADDR != SOF_REUSEADDR"
252 #endif
253 #if SO_KEEPALIVE != SOF_KEEPALIVE
254 #error "WARNING: SO_KEEPALIVE != SOF_KEEPALIVE"
255 #endif
256 #if SO_BROADCAST != SOF_BROADCAST
257 #error "WARNING: SO_BROADCAST != SOF_BROADCAST"
258 #endif
259 #endif /* LWIP_SOCKET */
260 #endif /* LWIP_LITEOS_COMPAT */
261
262 /* Compile-time checks for deprecated options.
263 */
264 #ifdef MEMP_NUM_TCPIP_MSG
265 #error "MEMP_NUM_TCPIP_MSG option is deprecated. Remove it from your lwipopts.h."
266 #endif
267 #ifdef TCP_REXMIT_DEBUG
268 #error "TCP_REXMIT_DEBUG option is deprecated. Remove it from your lwipopts.h."
269 #endif
270 #ifdef RAW_STATS
271 #error "RAW_STATS option is deprecated. Remove it from your lwipopts.h."
272 #endif
273 #ifdef ETHARP_QUEUE_FIRST
274 #error "ETHARP_QUEUE_FIRST option is deprecated. Remove it from your lwipopts.h."
275 #endif
276 #ifdef ETHARP_ALWAYS_INSERT
277 #error "ETHARP_ALWAYS_INSERT option is deprecated. Remove it from your lwipopts.h."
278 #endif
279 #if !NO_SYS && LWIP_TCPIP_CORE_LOCKING && LWIP_COMPAT_MUTEX && !defined(LWIP_COMPAT_MUTEX_ALLOWED)
280 #error "LWIP_COMPAT_MUTEX cannot prevent priority inversion. It is recommended to implement priority-aware mutexes. (Define LWIP_COMPAT_MUTEX_ALLOWED to disable this error.)"
281 #endif
282
283 #if (LWIP_ND6_NUM_NEIGHBORS < (LWIP_ND6_NUM_ROUTERS * 2))
284 #error "neighbor cache entries must be greater than two times of router entries"
285 #endif
286
287 #ifndef LWIP_DISABLE_TCP_SANITY_CHECKS
288 #define LWIP_DISABLE_TCP_SANITY_CHECKS 0
289 #endif
290
291 /* TCP sanity checks */
292 #if !LWIP_DISABLE_TCP_SANITY_CHECKS
293 #if LWIP_TCP
294
295 #if TCP_MSS >= ((16 * 1024) - 1)
296 #error "lwip_sanity_check: WARNING: TCP_MSS must be <= 16382 to prevent u16_t underflow in TCP_SNDLOWAT calculation!"
297 #endif
298
299 #if TCP_SND_BUF < (2 * TCP_MSS)
300 #error "lwip_sanity_check: WARNING: TCP_SND_BUF must be at least as much as (2 * TCP_MSS) for things to work smoothly. If you know what you are doing, define LWIP_DISABLE_TCP_SANITY_CHECKS to 1 to disable this error."
301 #endif
302 #if !MEMP_MEM_MALLOC && PBUF_POOL_SIZE && (PBUF_POOL_BUFSIZE <= (PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
303 #error "lwip_sanity_check: WARNING: PBUF_POOL_BUFSIZE does not provide enough space for protocol headers. If you know what you are doing, define LWIP_DISABLE_TCP_SANITY_CHECKS to 1 to disable this error."
304 #endif
305 #if !TCP_PBUF_MALLOC && !MEMP_MEM_MALLOC && PBUF_POOL_SIZE && (TCP_WND > (PBUF_POOL_SIZE * (PBUF_POOL_BUFSIZE - (PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))))
306 #error "lwip_sanity_check: WARNING: TCP_WND is larger than space provided by PBUF_POOL_SIZE * (PBUF_POOL_BUFSIZE - protocol headers). If you know what you are doing, define LWIP_DISABLE_TCP_SANITY_CHECKS to 1 to disable this error."
307 #endif
308 #if TCP_WND < TCP_MSS
309 #error "lwip_sanity_check: WARNING: TCP_WND is smaller than MSS. If you know what you are doing, define LWIP_DISABLE_TCP_SANITY_CHECKS to 1 to disable this error."
310 #endif
311 #if TCP_MAX_RTO_TICKS > 0x7FFF
312 #error "MAX RTO TICKS can not be larger than s16"
313 #endif
314
315 #endif /* LWIP_TCP */
316 #endif /* !LWIP_DISABLE_TCP_SANITY_CHECKS */
317
318 /**
319 * @ingroup lwip_nosys
320 * Initialize all modules.
321 * Use this in NO_SYS mode. Use tcpip_init() otherwise.
322 */
323 void
lwip_init(void)324 lwip_init(void)
325 {
326 #ifndef LWIP_SKIP_CONST_CHECK
327 int a = 0;
328 LWIP_UNUSED_ARG(a);
329 LWIP_ASSERT("LWIP_CONST_CAST not implemented correctly. Check your lwIP port.", LWIP_CONST_CAST(void *, &a) == &a);
330 #endif
331 #ifndef LWIP_SKIP_PACKING_CHECK
332 LWIP_ASSERT("Struct packing not implemented correctly. Check your lwIP port.", sizeof(struct packed_struct_test) == PACKED_STRUCT_TEST_EXPECTED_SIZE);
333 #endif
334
335 /* Modules initialization */
336 stats_init();
337 #if !NO_SYS
338 sys_init();
339 #endif /* !NO_SYS */
340 mem_init();
341 memp_init();
342 pbuf_init();
343 netif_init();
344 (void)sock_init();
345 #if LWIP_IPV4
346 ip_init();
347 #if LWIP_ARP
348 etharp_init();
349 #endif /* LWIP_ARP */
350 #endif /* LWIP_IPV4 */
351 #if LWIP_RAW
352 raw_init();
353 #endif /* LWIP_RAW */
354 #if LWIP_UDP
355 udp_init();
356 #endif /* LWIP_UDP */
357 #if LWIP_TCP
358 tcp_init();
359 #endif /* LWIP_TCP */
360 #if LWIP_IGMP
361 igmp_init();
362 #endif /* LWIP_IGMP */
363 #if LWIP_DNS
364 dns_init();
365 #endif /* LWIP_DNS */
366
367 #if LWIP_TIMERS
368 sys_timeouts_init();
369 #endif /* LWIP_TIMERS */
370 #if LWIP_RIPPLE
371 lwip_rpl_init();
372 #endif
373 #ifdef LWIP_EPOLL
374 EpollEventPtrInit();
375 #endif
376 }
377