1 /**
2 * @file
3 * Point To Point Protocol Sequential API module
4 *
5 */
6
7 /*
8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28 * OF SUCH DAMAGE.
29 *
30 * This file is part of the lwIP TCP/IP stack.
31 *
32 */
33
34 #include "netif/ppp/ppp_opts.h"
35
36 #if LWIP_PPP_API /* don't build if not configured for use in lwipopts.h */
37
38 #include "netif/ppp/pppapi.h"
39 #include "lwip/priv/tcpip_priv.h"
40 #include "netif/ppp/pppoe.h"
41 #include "netif/ppp/pppol2tp.h"
42 #include "netif/ppp/pppos.h"
43
44 #if LWIP_MPU_COMPATIBLE
45 LWIP_MEMPOOL_DECLARE(PPPAPI_MSG, MEMP_NUM_PPP_API_MSG, sizeof(struct pppapi_msg), "PPPAPI_MSG")
46 #endif
47
48 #define PPPAPI_VAR_REF(name) API_VAR_REF(name)
49 #define PPPAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct pppapi_msg, name)
50 #define PPPAPI_VAR_ALLOC(name) API_VAR_ALLOC_POOL(struct pppapi_msg, PPPAPI_MSG, name, ERR_MEM)
51 #define PPPAPI_VAR_ALLOC_RETURN_NULL(name) API_VAR_ALLOC_POOL(struct pppapi_msg, PPPAPI_MSG, name, NULL)
52 #define PPPAPI_VAR_FREE(name) API_VAR_FREE_POOL(PPPAPI_MSG, name)
53
54 /**
55 * Call ppp_set_default() inside the tcpip_thread context.
56 */
57 static err_t
pppapi_do_ppp_set_default(struct tcpip_api_call_data * m)58 pppapi_do_ppp_set_default(struct tcpip_api_call_data *m)
59 {
60 /* cast through void* to silence alignment warnings.
61 * We know it works because the structs have been instantiated as struct pppapi_msg */
62 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
63
64 ppp_set_default(msg->msg.ppp);
65 return ERR_OK;
66 }
67
68 /**
69 * Call ppp_set_default() in a thread-safe way by running that function inside the
70 * tcpip_thread context.
71 */
72 err_t
pppapi_set_default(ppp_pcb * pcb)73 pppapi_set_default(ppp_pcb *pcb)
74 {
75 err_t err;
76 PPPAPI_VAR_DECLARE(msg);
77 PPPAPI_VAR_ALLOC(msg);
78
79 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
80 err = tcpip_api_call(pppapi_do_ppp_set_default, &PPPAPI_VAR_REF(msg).call);
81 PPPAPI_VAR_FREE(msg);
82 return err;
83 }
84
85
86 #if PPP_NOTIFY_PHASE
87 /**
88 * Call ppp_set_notify_phase_callback() inside the tcpip_thread context.
89 */
90 static err_t
pppapi_do_ppp_set_notify_phase_callback(struct tcpip_api_call_data * m)91 pppapi_do_ppp_set_notify_phase_callback(struct tcpip_api_call_data *m)
92 {
93 /* cast through void* to silence alignment warnings.
94 * We know it works because the structs have been instantiated as struct pppapi_msg */
95 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
96
97 ppp_set_notify_phase_callback(msg->msg.ppp, msg->msg.msg.setnotifyphasecb.notify_phase_cb);
98 return ERR_OK;
99 }
100
101 /**
102 * Call ppp_set_notify_phase_callback() in a thread-safe way by running that function inside the
103 * tcpip_thread context.
104 */
105 err_t
pppapi_set_notify_phase_callback(ppp_pcb * pcb,ppp_notify_phase_cb_fn notify_phase_cb)106 pppapi_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_phase_cb)
107 {
108 err_t err;
109 PPPAPI_VAR_DECLARE(msg);
110 PPPAPI_VAR_ALLOC(msg);
111
112 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
113 PPPAPI_VAR_REF(msg).msg.msg.setnotifyphasecb.notify_phase_cb = notify_phase_cb;
114 err = tcpip_api_call(pppapi_do_ppp_set_notify_phase_callback, &PPPAPI_VAR_REF(msg).call);
115 PPPAPI_VAR_FREE(msg);
116 return err;
117 }
118 #endif /* PPP_NOTIFY_PHASE */
119
120
121 #if PPPOS_SUPPORT
122 /**
123 * Call pppos_create() inside the tcpip_thread context.
124 */
125 static err_t
pppapi_do_pppos_create(struct tcpip_api_call_data * m)126 pppapi_do_pppos_create(struct tcpip_api_call_data *m)
127 {
128 /* cast through void* to silence alignment warnings.
129 * We know it works because the structs have been instantiated as struct pppapi_msg */
130 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
131
132 msg->msg.ppp = pppos_create(msg->msg.msg.serialcreate.pppif, msg->msg.msg.serialcreate.output_cb,
133 msg->msg.msg.serialcreate.link_status_cb, msg->msg.msg.serialcreate.ctx_cb);
134 return ERR_OK;
135 }
136
137 /**
138 * Call pppos_create() in a thread-safe way by running that function inside the
139 * tcpip_thread context.
140 */
141 ppp_pcb*
pppapi_pppos_create(struct netif * pppif,pppos_output_cb_fn output_cb,ppp_link_status_cb_fn link_status_cb,void * ctx_cb)142 pppapi_pppos_create(struct netif *pppif, pppos_output_cb_fn output_cb,
143 ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
144 {
145 ppp_pcb* result;
146 PPPAPI_VAR_DECLARE(msg);
147 PPPAPI_VAR_ALLOC_RETURN_NULL(msg);
148
149 PPPAPI_VAR_REF(msg).msg.ppp = NULL;
150 PPPAPI_VAR_REF(msg).msg.msg.serialcreate.pppif = pppif;
151 PPPAPI_VAR_REF(msg).msg.msg.serialcreate.output_cb = output_cb;
152 PPPAPI_VAR_REF(msg).msg.msg.serialcreate.link_status_cb = link_status_cb;
153 PPPAPI_VAR_REF(msg).msg.msg.serialcreate.ctx_cb = ctx_cb;
154 tcpip_api_call(pppapi_do_pppos_create, &PPPAPI_VAR_REF(msg).call);
155 result = PPPAPI_VAR_REF(msg).msg.ppp;
156 PPPAPI_VAR_FREE(msg);
157 return result;
158 }
159 #endif /* PPPOS_SUPPORT */
160
161
162 #if PPPOE_SUPPORT
163 /**
164 * Call pppoe_create() inside the tcpip_thread context.
165 */
166 static err_t
pppapi_do_pppoe_create(struct tcpip_api_call_data * m)167 pppapi_do_pppoe_create(struct tcpip_api_call_data *m)
168 {
169 /* cast through void* to silence alignment warnings.
170 * We know it works because the structs have been instantiated as struct pppapi_msg */
171 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
172
173 msg->msg.ppp = pppoe_create(msg->msg.msg.ethernetcreate.pppif, msg->msg.msg.ethernetcreate.ethif,
174 msg->msg.msg.ethernetcreate.service_name, msg->msg.msg.ethernetcreate.concentrator_name,
175 msg->msg.msg.ethernetcreate.link_status_cb, msg->msg.msg.ethernetcreate.ctx_cb);
176 return ERR_OK;
177 }
178
179 /**
180 * Call pppoe_create() in a thread-safe way by running that function inside the
181 * tcpip_thread context.
182 */
183 ppp_pcb*
pppapi_pppoe_create(struct netif * pppif,struct netif * ethif,const char * service_name,const char * concentrator_name,ppp_link_status_cb_fn link_status_cb,void * ctx_cb)184 pppapi_pppoe_create(struct netif *pppif, struct netif *ethif, const char *service_name,
185 const char *concentrator_name, ppp_link_status_cb_fn link_status_cb,
186 void *ctx_cb)
187 {
188 ppp_pcb* result;
189 PPPAPI_VAR_DECLARE(msg);
190 PPPAPI_VAR_ALLOC_RETURN_NULL(msg);
191
192 PPPAPI_VAR_REF(msg).msg.ppp = NULL;
193 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.pppif = pppif;
194 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.ethif = ethif;
195 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.service_name = service_name;
196 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.concentrator_name = concentrator_name;
197 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.link_status_cb = link_status_cb;
198 PPPAPI_VAR_REF(msg).msg.msg.ethernetcreate.ctx_cb = ctx_cb;
199 tcpip_api_call(pppapi_do_pppoe_create, &PPPAPI_VAR_REF(msg).call);
200 result = PPPAPI_VAR_REF(msg).msg.ppp;
201 PPPAPI_VAR_FREE(msg);
202 return result;
203 }
204 #endif /* PPPOE_SUPPORT */
205
206
207 #if PPPOL2TP_SUPPORT
208 /**
209 * Call pppol2tp_create() inside the tcpip_thread context.
210 */
211 static err_t
pppapi_do_pppol2tp_create(struct tcpip_api_call_data * m)212 pppapi_do_pppol2tp_create(struct tcpip_api_call_data *m)
213 {
214 /* cast through void* to silence alignment warnings.
215 * We know it works because the structs have been instantiated as struct pppapi_msg */
216 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
217
218 msg->msg.ppp = pppol2tp_create(msg->msg.msg.l2tpcreate.pppif,
219 msg->msg.msg.l2tpcreate.netif, API_EXPR_REF(msg->msg.msg.l2tpcreate.ipaddr), msg->msg.msg.l2tpcreate.port,
220 #if PPPOL2TP_AUTH_SUPPORT
221 msg->msg.msg.l2tpcreate.secret,
222 msg->msg.msg.l2tpcreate.secret_len,
223 #else /* PPPOL2TP_AUTH_SUPPORT */
224 NULL,
225 #endif /* PPPOL2TP_AUTH_SUPPORT */
226 msg->msg.msg.l2tpcreate.link_status_cb, msg->msg.msg.l2tpcreate.ctx_cb);
227 return ERR_OK;
228 }
229
230 /**
231 * Call pppol2tp_create() in a thread-safe way by running that function inside the
232 * tcpip_thread context.
233 */
234 ppp_pcb*
pppapi_pppol2tp_create(struct netif * pppif,struct netif * netif,ip_addr_t * ipaddr,u16_t port,const u8_t * secret,u8_t secret_len,ppp_link_status_cb_fn link_status_cb,void * ctx_cb)235 pppapi_pppol2tp_create(struct netif *pppif, struct netif *netif, ip_addr_t *ipaddr, u16_t port,
236 const u8_t *secret, u8_t secret_len,
237 ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
238 {
239 ppp_pcb* result;
240 PPPAPI_VAR_DECLARE(msg);
241 PPPAPI_VAR_ALLOC_RETURN_NULL(msg);
242
243 PPPAPI_VAR_REF(msg).msg.ppp = NULL;
244 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.pppif = pppif;
245 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.netif = netif;
246 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.ipaddr = PPPAPI_VAR_REF(ipaddr);
247 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.port = port;
248 #if PPPOL2TP_AUTH_SUPPORT
249 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.secret = secret;
250 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.secret_len = secret_len;
251 #endif /* PPPOL2TP_AUTH_SUPPORT */
252 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.link_status_cb = link_status_cb;
253 PPPAPI_VAR_REF(msg).msg.msg.l2tpcreate.ctx_cb = ctx_cb;
254 tcpip_api_call(pppapi_do_pppol2tp_create, &PPPAPI_VAR_REF(msg).call);
255 result = PPPAPI_VAR_REF(msg).msg.ppp;
256 PPPAPI_VAR_FREE(msg);
257 return result;
258 }
259 #endif /* PPPOL2TP_SUPPORT */
260
261
262 /**
263 * Call ppp_connect() inside the tcpip_thread context.
264 */
265 static err_t
pppapi_do_ppp_connect(struct tcpip_api_call_data * m)266 pppapi_do_ppp_connect(struct tcpip_api_call_data *m)
267 {
268 /* cast through void* to silence alignment warnings.
269 * We know it works because the structs have been instantiated as struct pppapi_msg */
270 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
271
272 return ppp_connect(msg->msg.ppp, msg->msg.msg.connect.holdoff);
273 }
274
275 /**
276 * Call ppp_connect() in a thread-safe way by running that function inside the
277 * tcpip_thread context.
278 */
279 err_t
pppapi_connect(ppp_pcb * pcb,u16_t holdoff)280 pppapi_connect(ppp_pcb *pcb, u16_t holdoff)
281 {
282 err_t err;
283 PPPAPI_VAR_DECLARE(msg);
284 PPPAPI_VAR_ALLOC(msg);
285
286 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
287 PPPAPI_VAR_REF(msg).msg.msg.connect.holdoff = holdoff;
288 err = tcpip_api_call(pppapi_do_ppp_connect, &PPPAPI_VAR_REF(msg).call);
289 PPPAPI_VAR_FREE(msg);
290 return err;
291 }
292
293
294 #if PPP_SERVER
295 /**
296 * Call ppp_listen() inside the tcpip_thread context.
297 */
298 static err_t
pppapi_do_ppp_listen(struct tcpip_api_call_data * m)299 pppapi_do_ppp_listen(struct tcpip_api_call_data *m)
300 {
301 /* cast through void* to silence alignment warnings.
302 * We know it works because the structs have been instantiated as struct pppapi_msg */
303 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
304
305 return ppp_listen(msg->msg.ppp);
306 }
307
308 /**
309 * Call ppp_listen() in a thread-safe way by running that function inside the
310 * tcpip_thread context.
311 */
312 err_t
pppapi_listen(ppp_pcb * pcb)313 pppapi_listen(ppp_pcb *pcb)
314 {
315 err_t err;
316 PPPAPI_VAR_DECLARE(msg);
317 PPPAPI_VAR_ALLOC(msg);
318
319 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
320 err = tcpip_api_call(pppapi_do_ppp_listen, &PPPAPI_VAR_REF(msg).call);
321 PPPAPI_VAR_FREE(msg);
322 return err;
323 }
324 #endif /* PPP_SERVER */
325
326
327 /**
328 * Call ppp_close() inside the tcpip_thread context.
329 */
330 static err_t
pppapi_do_ppp_close(struct tcpip_api_call_data * m)331 pppapi_do_ppp_close(struct tcpip_api_call_data *m)
332 {
333 /* cast through void* to silence alignment warnings.
334 * We know it works because the structs have been instantiated as struct pppapi_msg */
335 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
336
337 return ppp_close(msg->msg.ppp, msg->msg.msg.close.nocarrier);
338 }
339
340 /**
341 * Call ppp_close() in a thread-safe way by running that function inside the
342 * tcpip_thread context.
343 */
344 err_t
pppapi_close(ppp_pcb * pcb,u8_t nocarrier)345 pppapi_close(ppp_pcb *pcb, u8_t nocarrier)
346 {
347 err_t err;
348 PPPAPI_VAR_DECLARE(msg);
349 PPPAPI_VAR_ALLOC(msg);
350
351 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
352 PPPAPI_VAR_REF(msg).msg.msg.close.nocarrier = nocarrier;
353 err = tcpip_api_call(pppapi_do_ppp_close, &PPPAPI_VAR_REF(msg).call);
354 PPPAPI_VAR_FREE(msg);
355 return err;
356 }
357
358
359 /**
360 * Call ppp_free() inside the tcpip_thread context.
361 */
362 static err_t
pppapi_do_ppp_free(struct tcpip_api_call_data * m)363 pppapi_do_ppp_free(struct tcpip_api_call_data *m)
364 {
365 /* cast through void* to silence alignment warnings.
366 * We know it works because the structs have been instantiated as struct pppapi_msg */
367 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
368
369 return ppp_free(msg->msg.ppp);
370 }
371
372 /**
373 * Call ppp_free() in a thread-safe way by running that function inside the
374 * tcpip_thread context.
375 */
376 err_t
pppapi_free(ppp_pcb * pcb)377 pppapi_free(ppp_pcb *pcb)
378 {
379 err_t err;
380 PPPAPI_VAR_DECLARE(msg);
381 PPPAPI_VAR_ALLOC(msg);
382
383 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
384 err = tcpip_api_call(pppapi_do_ppp_free, &PPPAPI_VAR_REF(msg).call);
385 PPPAPI_VAR_FREE(msg);
386 return err;
387 }
388
389
390 /**
391 * Call ppp_ioctl() inside the tcpip_thread context.
392 */
393 static err_t
pppapi_do_ppp_ioctl(struct tcpip_api_call_data * m)394 pppapi_do_ppp_ioctl(struct tcpip_api_call_data *m)
395 {
396 /* cast through void* to silence alignment warnings.
397 * We know it works because the structs have been instantiated as struct pppapi_msg */
398 struct pppapi_msg *msg = (struct pppapi_msg *)(void*)m;
399
400 return ppp_ioctl(msg->msg.ppp, msg->msg.msg.ioctl.cmd, msg->msg.msg.ioctl.arg);
401 }
402
403 /**
404 * Call ppp_ioctl() in a thread-safe way by running that function inside the
405 * tcpip_thread context.
406 */
407 err_t
pppapi_ioctl(ppp_pcb * pcb,u8_t cmd,void * arg)408 pppapi_ioctl(ppp_pcb *pcb, u8_t cmd, void *arg)
409 {
410 err_t err;
411 PPPAPI_VAR_DECLARE(msg);
412 PPPAPI_VAR_ALLOC(msg);
413
414 PPPAPI_VAR_REF(msg).msg.ppp = pcb;
415 PPPAPI_VAR_REF(msg).msg.msg.ioctl.cmd = cmd;
416 PPPAPI_VAR_REF(msg).msg.msg.ioctl.arg = arg;
417 err = tcpip_api_call(pppapi_do_ppp_ioctl, &PPPAPI_VAR_REF(msg).call);
418 PPPAPI_VAR_FREE(msg);
419 return err;
420 }
421
422 #endif /* LWIP_PPP_API */
423