]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/api/tcpip.c
Fixed bug #28183 (ARP and TCP/IP cannot be disabled on netif used for PPPoE) by addin...
[pes-rpp/rpp-lwip.git] / src / api / tcpip.c
1 /**
2  * @file
3  * Sequential API Main thread module
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
39 #include "lwip/opt.h"
40
41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
42
43 #include "lwip/sys.h"
44 #include "lwip/memp.h"
45 #include "lwip/mem.h"
46 #include "lwip/pbuf.h"
47 #include "lwip/tcpip.h"
48 #include "lwip/init.h"
49 #include "netif/etharp.h"
50 #include "netif/ppp_oe.h"
51
52 /* global variables */
53 static tcpip_init_done_fn tcpip_init_done;
54 static void *tcpip_init_done_arg;
55 static sys_mbox_t mbox;
56
57 #if LWIP_TCPIP_CORE_LOCKING
58 /** The global semaphore to lock the stack. */
59 sys_mutex_t lock_tcpip_core;
60 #endif /* LWIP_TCPIP_CORE_LOCKING */
61
62
63 /**
64  * The main lwIP thread. This thread has exclusive access to lwIP core functions
65  * (unless access to them is not locked). Other threads communicate with this
66  * thread using message boxes.
67  *
68  * It also starts all the timers to make sure they are running in the right
69  * thread context.
70  *
71  * @param arg unused argument
72  */
73 static void
74 tcpip_thread(void *arg)
75 {
76   struct tcpip_msg *msg;
77   LWIP_UNUSED_ARG(arg);
78
79   if (tcpip_init_done != NULL) {
80     tcpip_init_done(tcpip_init_done_arg);
81   }
82
83   LOCK_TCPIP_CORE();
84   while (1) {                          /* MAIN Loop */
85     UNLOCK_TCPIP_CORE();
86     /* wait for a message, timeouts are processed while waiting */
87     sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
88     LOCK_TCPIP_CORE();
89     switch (msg->type) {
90 #if LWIP_NETCONN
91     case TCPIP_MSG_API:
92       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
93       msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
94       break;
95 #endif /* LWIP_NETCONN */
96
97     case TCPIP_MSG_INPKT:
98       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
99 #if LWIP_ETHERNET
100       if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
101         ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
102       } else
103 #endif /* LWIP_ETHERNET */
104       { ip_input(msg->msg.inp.p, msg->msg.inp.netif);
105       }
106       memp_free(MEMP_TCPIP_MSG_INPKT, msg);
107       break;
108
109 #if LWIP_NETIF_API
110     case TCPIP_MSG_NETIFAPI:
111       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
112       msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
113       break;
114 #endif /* LWIP_NETIF_API */
115
116     case TCPIP_MSG_CALLBACK:
117       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
118       msg->msg.cb.function(msg->msg.cb.ctx);
119       memp_free(MEMP_TCPIP_MSG_API, msg);
120       break;
121
122     case TCPIP_MSG_TIMEOUT:
123       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
124       sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
125       memp_free(MEMP_TCPIP_MSG_API, msg);
126       break;
127     case TCPIP_MSG_UNTIMEOUT:
128       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
129       sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
130       memp_free(MEMP_TCPIP_MSG_API, msg);
131       break;
132
133     default:
134       break;
135     }
136   }
137 }
138
139 /**
140  * Pass a received packet to tcpip_thread for input processing
141  *
142  * @param p the received packet, p->payload pointing to the Ethernet header or
143  *          to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
144  *          NETIF_FLAG_ETHERNET flags)
145  * @param inp the network interface on which the packet was received
146  */
147 err_t
148 tcpip_input(struct pbuf *p, struct netif *inp)
149 {
150   struct tcpip_msg *msg;
151
152   if (sys_mbox_valid(&mbox)) {
153     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
154     if (msg == NULL) {
155       return ERR_MEM;
156     }
157
158     msg->type = TCPIP_MSG_INPKT;
159     msg->msg.inp.p = p;
160     msg->msg.inp.netif = inp;
161     if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
162       memp_free(MEMP_TCPIP_MSG_INPKT, msg);
163       return ERR_MEM;
164     }
165     return ERR_OK;
166   }
167   return ERR_VAL;
168 }
169
170 /**
171  * Call a specific function in the thread context of
172  * tcpip_thread for easy access synchronization.
173  * A function called in that way may access lwIP core code
174  * without fearing concurrent access.
175  *
176  * @param f the function to call
177  * @param ctx parameter passed to f
178  * @param block 1 to block until the request is posted, 0 to non-blocking mode
179  * @return ERR_OK if the function was called, another err_t if not
180  */
181 err_t
182 tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
183 {
184   struct tcpip_msg *msg;
185
186   if (sys_mbox_valid(&mbox)) {
187     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
188     if (msg == NULL) {
189       return ERR_MEM;
190     }
191
192     msg->type = TCPIP_MSG_CALLBACK;
193     msg->msg.cb.function = function;
194     msg->msg.cb.ctx = ctx;
195     if (block) {
196       sys_mbox_post(&mbox, msg);
197     } else {
198       if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
199         memp_free(MEMP_TCPIP_MSG_API, msg);
200         return ERR_MEM;
201       }
202     }
203     return ERR_OK;
204   }
205   return ERR_VAL;
206 }
207
208 /**
209  * call sys_timeout in tcpip_thread
210  *
211  * @param msec time in miliseconds for timeout
212  * @param h function to be called on timeout
213  * @param arg argument to pass to timeout function h
214  * @return ERR_MEM on memory error, ERR_OK otherwise
215  */
216 err_t
217 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
218 {
219   struct tcpip_msg *msg;
220
221   if (sys_mbox_valid(&mbox)) {
222     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
223     if (msg == NULL) {
224       return ERR_MEM;
225     }
226
227     msg->type = TCPIP_MSG_TIMEOUT;
228     msg->msg.tmo.msecs = msecs;
229     msg->msg.tmo.h = h;
230     msg->msg.tmo.arg = arg;
231     sys_mbox_post(&mbox, msg);
232     return ERR_OK;
233   }
234   return ERR_VAL;
235 }
236
237 /**
238  * call sys_untimeout in tcpip_thread
239  *
240  * @param msec time in miliseconds for timeout
241  * @param h function to be called on timeout
242  * @param arg argument to pass to timeout function h
243  * @return ERR_MEM on memory error, ERR_OK otherwise
244  */
245 err_t
246 tcpip_untimeout(sys_timeout_handler h, void *arg)
247 {
248   struct tcpip_msg *msg;
249
250   if (sys_mbox_valid(&mbox)) {
251     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
252     if (msg == NULL) {
253       return ERR_MEM;
254     }
255
256     msg->type = TCPIP_MSG_UNTIMEOUT;
257     msg->msg.tmo.h = h;
258     msg->msg.tmo.arg = arg;
259     sys_mbox_post(&mbox, msg);
260     return ERR_OK;
261   }
262   return ERR_VAL;
263 }
264
265 #if LWIP_NETCONN
266 /**
267  * Call the lower part of a netconn_* function
268  * This function is then running in the thread context
269  * of tcpip_thread and has exclusive access to lwIP core code.
270  *
271  * @param apimsg a struct containing the function to call and its parameters
272  * @return ERR_OK if the function was called, another err_t if not
273  */
274 err_t
275 tcpip_apimsg(struct api_msg *apimsg)
276 {
277   struct tcpip_msg msg;
278 #ifdef LWIP_DEBUG
279   /* catch functions that don't set err */
280   apimsg->msg.err = ERR_VAL;
281 #endif
282   
283   if (sys_mbox_valid(&mbox)) {
284     msg.type = TCPIP_MSG_API;
285     msg.msg.apimsg = apimsg;
286     sys_mbox_post(&mbox, &msg);
287     sys_arch_sem_wait(&apimsg->msg.conn->op_completed, 0);
288     return apimsg->msg.err;
289   }
290   return ERR_VAL;
291 }
292
293 #if LWIP_TCPIP_CORE_LOCKING
294 /**
295  * Call the lower part of a netconn_* function
296  * This function has exclusive access to lwIP core code by locking it
297  * before the function is called.
298  *
299  * @param apimsg a struct containing the function to call and its parameters
300  * @return ERR_OK (only for compatibility fo tcpip_apimsg())
301  */
302 err_t
303 tcpip_apimsg_lock(struct api_msg *apimsg)
304 {
305 #ifdef LWIP_DEBUG
306   /* catch functions that don't set err */
307   apimsg->msg.err = ERR_VAL;
308 #endif
309
310   LOCK_TCPIP_CORE();
311   apimsg->function(&(apimsg->msg));
312   UNLOCK_TCPIP_CORE();
313   return apimsg->msg.err;
314
315 }
316 #endif /* LWIP_TCPIP_CORE_LOCKING */
317 #endif /* LWIP_NETCONN */
318
319 #if LWIP_NETIF_API
320 #if !LWIP_TCPIP_CORE_LOCKING
321 /**
322  * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
323  * function.
324  *
325  * @param netifapimsg a struct containing the function to call and its parameters
326  * @return error code given back by the function that was called
327  */
328 err_t
329 tcpip_netifapi(struct netifapi_msg* netifapimsg)
330 {
331   struct tcpip_msg msg;
332   
333   if (sys_mbox_valid(&mbox)) {
334     err_t err = sys_sem_new(&netifapimsg->msg.sem, 0);
335     if (err != ERR_OK) {
336       netifapimsg->msg.err = err;
337       return err;
338     }
339     
340     msg.type = TCPIP_MSG_NETIFAPI;
341     msg.msg.netifapimsg = netifapimsg;
342     sys_mbox_post(&mbox, &msg);
343     sys_sem_wait(&netifapimsg->msg.sem);
344     sys_sem_free(&netifapimsg->msg.sem);
345     return netifapimsg->msg.err;
346   }
347   return ERR_VAL;
348 }
349 #else /* !LWIP_TCPIP_CORE_LOCKING */
350 /**
351  * Call the lower part of a netifapi_* function
352  * This function has exclusive access to lwIP core code by locking it
353  * before the function is called.
354  *
355  * @param netifapimsg a struct containing the function to call and its parameters
356  * @return ERR_OK (only for compatibility fo tcpip_netifapi())
357  */
358 err_t
359 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
360 {
361   LOCK_TCPIP_CORE();  
362   netifapimsg->function(&(netifapimsg->msg));
363   UNLOCK_TCPIP_CORE();
364   return netifapimsg->msg.err;
365 }
366 #endif /* !LWIP_TCPIP_CORE_LOCKING */
367 #endif /* LWIP_NETIF_API */
368
369 /**
370  * Initialize this module:
371  * - initialize all sub modules
372  * - start the tcpip_thread
373  *
374  * @param initfunc a function to call when tcpip_thread is running and finished initializing
375  * @param arg argument to pass to initfunc
376  */
377 void
378 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
379 {
380   lwip_init();
381
382   tcpip_init_done = initfunc;
383   tcpip_init_done_arg = arg;
384   if(sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
385     LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
386   }
387 #if LWIP_TCPIP_CORE_LOCKING
388   if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
389     LWIP_ASSERT("failed to create lock_tcpip_core", 0);
390   }
391 #endif /* LWIP_TCPIP_CORE_LOCKING */
392
393   sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
394 }
395
396 /**
397  * Simple callback function used with tcpip_callback to free a pbuf
398  * (pbuf_free has a wrong signature for tcpip_callback)
399  *
400  * @param p The pbuf (chain) to be dereferenced.
401  */
402 static void
403 pbuf_free_int(void *p)
404 {
405   struct pbuf *q = (struct pbuf *)p;
406   pbuf_free(q);
407 }
408
409 /**
410  * A simple wrapper function that allows you to free a pbuf from interrupt context.
411  *
412  * @param p The pbuf (chain) to be dereferenced.
413  * @return ERR_OK if callback could be enqueued, an err_t if not
414  */
415 err_t
416 pbuf_free_callback(struct pbuf *p)
417 {
418   return tcpip_callback_with_block(pbuf_free_int, p, 0);
419 }
420
421 /**
422  * A simple wrapper function that allows you to free heap memory from
423  * interrupt context.
424  *
425  * @param m the heap memory to free
426  * @return ERR_OK if callback could be enqueued, an err_t if not
427  */
428 err_t
429 mem_free_callback(void *m)
430 {
431   return tcpip_callback_with_block(mem_free, m, 0);
432 }
433
434 #endif /* !NO_SYS */