xref: /btstack/3rd-party/lwip/core/src/netif/ppp/ppp.c (revision 97dc5e692c7d94a280158af58036a0efee5b0e56)
1 /*****************************************************************************
2 * ppp.c - Network Point to Point Protocol program file.
3 *
4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5 * portions Copyright (c) 1997 by Global Election Systems Inc.
6 *
7 * The authors hereby grant permission to use, copy, modify, distribute,
8 * and license this software and its documentation for any purpose, provided
9 * that existing copyright notices are retained in all copies and that this
10 * notice and the following disclaimer are included verbatim in any
11 * distributions. No written agreement, license, or royalty fee is required
12 * for any of the authorized uses.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 ******************************************************************************
26 * REVISION HISTORY
27 *
28 * 03-01-01 Marc Boucher <[email protected]>
29 *   Ported to lwIP.
30 * 97-11-05 Guy Lancaster <[email protected]>, Global Election Systems Inc.
31 *   Original.
32 *****************************************************************************/
33 
34 /*
35  * ppp_defs.h - PPP definitions.
36  *
37  * if_pppvar.h - private structures and declarations for PPP.
38  *
39  * Copyright (c) 1994 The Australian National University.
40  * All rights reserved.
41  *
42  * Permission to use, copy, modify, and distribute this software and its
43  * documentation is hereby granted, provided that the above copyright
44  * notice appears in all copies.  This software is provided without any
45  * warranty, express or implied. The Australian National University
46  * makes no representations about the suitability of this software for
47  * any purpose.
48  *
49  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
50  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
51  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
52  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
53  * OF SUCH DAMAGE.
54  *
55  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
56  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
57  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
58  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
59  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
60  * OR MODIFICATIONS.
61  */
62 
63 /*
64  * if_ppp.h - Point-to-Point Protocol definitions.
65  *
66  * Copyright (c) 1989 Carnegie Mellon University.
67  * All rights reserved.
68  *
69  * Redistribution and use in source and binary forms are permitted
70  * provided that the above copyright notice and this paragraph are
71  * duplicated in all such forms and that any documentation,
72  * advertising materials, and other materials related to such
73  * distribution and use acknowledge that the software was developed
74  * by Carnegie Mellon University.  The name of the
75  * University may not be used to endorse or promote products derived
76  * from this software without specific prior written permission.
77  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
78  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
79  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
80  */
81 
82 /**
83  * @defgroup ppp PPP
84  * @ingroup netifs
85  * @verbinclude "ppp.txt"
86  */
87 
88 #include "netif/ppp/ppp_opts.h"
89 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
90 
91 #include "lwip/pbuf.h"
92 #include "lwip/stats.h"
93 #include "lwip/sys.h"
94 #include "lwip/tcpip.h"
95 #include "lwip/api.h"
96 #include "lwip/snmp.h"
97 #include "lwip/ip4.h" /* for ip4_input() */
98 #if PPP_IPV6_SUPPORT
99 #include "lwip/ip6.h" /* for ip6_input() */
100 #endif /* PPP_IPV6_SUPPORT */
101 #include "lwip/dns.h"
102 
103 #include "netif/ppp/ppp_impl.h"
104 #include "netif/ppp/pppos.h"
105 
106 #include "netif/ppp/fsm.h"
107 #include "netif/ppp/lcp.h"
108 #include "netif/ppp/magic.h"
109 
110 #if PAP_SUPPORT
111 #include "netif/ppp/upap.h"
112 #endif /* PAP_SUPPORT */
113 #if CHAP_SUPPORT
114 #include "netif/ppp/chap-new.h"
115 #endif /* CHAP_SUPPORT */
116 #if EAP_SUPPORT
117 #include "netif/ppp/eap.h"
118 #endif /* EAP_SUPPORT */
119 #if CCP_SUPPORT
120 #include "netif/ppp/ccp.h"
121 #endif /* CCP_SUPPORT */
122 #if MPPE_SUPPORT
123 #include "netif/ppp/mppe.h"
124 #endif /* MPPE_SUPPORT */
125 #if ECP_SUPPORT
126 #include "netif/ppp/ecp.h"
127 #endif /* EAP_SUPPORT */
128 #if VJ_SUPPORT
129 #include "netif/ppp/vj.h"
130 #endif /* VJ_SUPPORT */
131 #if PPP_IPV4_SUPPORT
132 #include "netif/ppp/ipcp.h"
133 #endif /* PPP_IPV4_SUPPORT */
134 #if PPP_IPV6_SUPPORT
135 #include "netif/ppp/ipv6cp.h"
136 #endif /* PPP_IPV6_SUPPORT */
137 
138 /*************************/
139 /*** LOCAL DEFINITIONS ***/
140 /*************************/
141 
142 /* Memory pools */
143 #if PPPOS_SUPPORT
144 LWIP_MEMPOOL_PROTOTYPE(PPPOS_PCB);
145 #endif
146 #if PPPOE_SUPPORT
147 LWIP_MEMPOOL_PROTOTYPE(PPPOE_IF);
148 #endif
149 #if PPPOL2TP_SUPPORT
150 LWIP_MEMPOOL_PROTOTYPE(PPPOL2TP_PCB);
151 #endif
152 #if LWIP_PPP_API && LWIP_MPU_COMPATIBLE
153 LWIP_MEMPOOL_PROTOTYPE(PPPAPI_MSG);
154 #endif
155 LWIP_MEMPOOL_DECLARE(PPP_PCB, MEMP_NUM_PPP_PCB, sizeof(ppp_pcb), "PPP_PCB")
156 
157 /* FIXME: add stats per PPP session */
158 #if PPP_STATS_SUPPORT
159 static struct timeval start_time; /* Time when link was started. */
160 static struct pppd_stats old_link_stats;
161 struct pppd_stats link_stats;
162 unsigned link_connect_time;
163 int link_stats_valid;
164 #endif /* PPP_STATS_SUPPORT */
165 
166 /*
167  * PPP Data Link Layer "protocol" table.
168  * One entry per supported protocol.
169  * The last entry must be NULL.
170  */
171 const struct protent* const protocols[] = {
172     &lcp_protent,
173 #if PAP_SUPPORT
174     &pap_protent,
175 #endif /* PAP_SUPPORT */
176 #if CHAP_SUPPORT
177     &chap_protent,
178 #endif /* CHAP_SUPPORT */
179 #if CBCP_SUPPORT
180     &cbcp_protent,
181 #endif /* CBCP_SUPPORT */
182 #if PPP_IPV4_SUPPORT
183     &ipcp_protent,
184 #endif /* PPP_IPV4_SUPPORT */
185 #if PPP_IPV6_SUPPORT
186     &ipv6cp_protent,
187 #endif /* PPP_IPV6_SUPPORT */
188 #if CCP_SUPPORT
189     &ccp_protent,
190 #endif /* CCP_SUPPORT */
191 #if ECP_SUPPORT
192     &ecp_protent,
193 #endif /* ECP_SUPPORT */
194 #ifdef AT_CHANGE
195     &atcp_protent,
196 #endif /* AT_CHANGE */
197 #if EAP_SUPPORT
198     &eap_protent,
199 #endif /* EAP_SUPPORT */
200     NULL
201 };
202 
203 /* Prototypes for procedures local to this file. */
204 static void ppp_do_connect(void *arg);
205 static err_t ppp_netif_init_cb(struct netif *netif);
206 #if PPP_IPV4_SUPPORT
207 static err_t ppp_netif_output_ip4(struct netif *netif, struct pbuf *pb, const ip4_addr_t *ipaddr);
208 #endif /* PPP_IPV4_SUPPORT */
209 #if PPP_IPV6_SUPPORT
210 static err_t ppp_netif_output_ip6(struct netif *netif, struct pbuf *pb, const ip6_addr_t *ipaddr);
211 #endif /* PPP_IPV6_SUPPORT */
212 static err_t ppp_netif_output(struct netif *netif, struct pbuf *pb, u16_t protocol);
213 
214 /***********************************/
215 /*** PUBLIC FUNCTION DEFINITIONS ***/
216 /***********************************/
217 #if PPP_AUTH_SUPPORT
ppp_set_auth(ppp_pcb * pcb,u8_t authtype,const char * user,const char * passwd)218 void ppp_set_auth(ppp_pcb *pcb, u8_t authtype, const char *user, const char *passwd) {
219   LWIP_ASSERT_CORE_LOCKED();
220 #if PAP_SUPPORT
221   pcb->settings.refuse_pap = !(authtype & PPPAUTHTYPE_PAP);
222 #endif /* PAP_SUPPORT */
223 #if CHAP_SUPPORT
224   pcb->settings.refuse_chap = !(authtype & PPPAUTHTYPE_CHAP);
225 #if MSCHAP_SUPPORT
226   pcb->settings.refuse_mschap = !(authtype & PPPAUTHTYPE_MSCHAP);
227   pcb->settings.refuse_mschap_v2 = !(authtype & PPPAUTHTYPE_MSCHAP_V2);
228 #endif /* MSCHAP_SUPPORT */
229 #endif /* CHAP_SUPPORT */
230 #if EAP_SUPPORT
231   pcb->settings.refuse_eap = !(authtype & PPPAUTHTYPE_EAP);
232 #endif /* EAP_SUPPORT */
233   pcb->settings.user = user;
234   pcb->settings.passwd = passwd;
235 }
236 #endif /* PPP_AUTH_SUPPORT */
237 
238 #if MPPE_SUPPORT
239 /* Set MPPE configuration */
ppp_set_mppe(ppp_pcb * pcb,u8_t flags)240 void ppp_set_mppe(ppp_pcb *pcb, u8_t flags) {
241   if (flags == PPP_MPPE_DISABLE) {
242     pcb->settings.require_mppe = 0;
243     return;
244   }
245 
246   pcb->settings.require_mppe = 1;
247   pcb->settings.refuse_mppe_stateful = !(flags & PPP_MPPE_ALLOW_STATEFUL);
248   pcb->settings.refuse_mppe_40 = !!(flags & PPP_MPPE_REFUSE_40);
249   pcb->settings.refuse_mppe_128 = !!(flags & PPP_MPPE_REFUSE_128);
250 }
251 #endif /* MPPE_SUPPORT */
252 
253 #if PPP_NOTIFY_PHASE
ppp_set_notify_phase_callback(ppp_pcb * pcb,ppp_notify_phase_cb_fn notify_phase_cb)254 void ppp_set_notify_phase_callback(ppp_pcb *pcb, ppp_notify_phase_cb_fn notify_phase_cb) {
255   pcb->notify_phase_cb = notify_phase_cb;
256   notify_phase_cb(pcb, pcb->phase, pcb->ctx_cb);
257 }
258 #endif /* PPP_NOTIFY_PHASE */
259 
260 /*
261  * Initiate a PPP connection.
262  *
263  * This can only be called if PPP is in the dead phase.
264  *
265  * Holdoff is the time to wait (in seconds) before initiating
266  * the connection.
267  *
268  * If this port connects to a modem, the modem connection must be
269  * established before calling this.
270  */
ppp_connect(ppp_pcb * pcb,u16_t holdoff)271 err_t ppp_connect(ppp_pcb *pcb, u16_t holdoff) {
272   LWIP_ASSERT_CORE_LOCKED();
273   if (pcb->phase != PPP_PHASE_DEAD) {
274     return ERR_ALREADY;
275   }
276 
277   PPPDEBUG(LOG_DEBUG, ("ppp_connect[%d]: holdoff=%d\n", pcb->netif->num, holdoff));
278 
279   magic_randomize();
280 
281   if (holdoff == 0) {
282     ppp_do_connect(pcb);
283     return ERR_OK;
284   }
285 
286   new_phase(pcb, PPP_PHASE_HOLDOFF);
287   sys_timeout((u32_t)(holdoff*1000), ppp_do_connect, pcb);
288   return ERR_OK;
289 }
290 
291 #if PPP_SERVER
292 /*
293  * Listen for an incoming PPP connection.
294  *
295  * This can only be called if PPP is in the dead phase.
296  *
297  * If this port connects to a modem, the modem connection must be
298  * established before calling this.
299  */
ppp_listen(ppp_pcb * pcb)300 err_t ppp_listen(ppp_pcb *pcb) {
301   LWIP_ASSERT_CORE_LOCKED();
302   if (pcb->phase != PPP_PHASE_DEAD) {
303     return ERR_ALREADY;
304   }
305 
306   PPPDEBUG(LOG_DEBUG, ("ppp_listen[%d]\n", pcb->netif->num));
307 
308   magic_randomize();
309 
310   if (pcb->link_cb->listen) {
311     new_phase(pcb, PPP_PHASE_INITIALIZE);
312     pcb->link_cb->listen(pcb, pcb->link_ctx_cb);
313     return ERR_OK;
314   }
315   return ERR_IF;
316 }
317 #endif /* PPP_SERVER */
318 
319 /*
320  * Initiate the end of a PPP connection.
321  * Any outstanding packets in the queues are dropped.
322  *
323  * Setting nocarrier to 1 close the PPP connection without initiating the
324  * shutdown procedure. Always using nocarrier = 0 is still recommended,
325  * this is going to take a little longer time if your link is down, but
326  * is a safer choice for the PPP state machine.
327  *
328  * Return 0 on success, an error code on failure.
329  */
330 err_t
ppp_close(ppp_pcb * pcb,u8_t nocarrier)331 ppp_close(ppp_pcb *pcb, u8_t nocarrier)
332 {
333   LWIP_ASSERT_CORE_LOCKED();
334 
335   pcb->err_code = PPPERR_USER;
336 
337   /* holdoff phase, cancel the reconnection */
338   if (pcb->phase == PPP_PHASE_HOLDOFF) {
339     sys_untimeout(ppp_do_connect, pcb);
340     new_phase(pcb, PPP_PHASE_DEAD);
341   }
342 
343   /* dead phase, nothing to do, call the status callback to be consistent */
344   if (pcb->phase == PPP_PHASE_DEAD) {
345     pcb->link_status_cb(pcb, pcb->err_code, pcb->ctx_cb);
346     return ERR_OK;
347   }
348 
349   /* Already terminating, nothing to do */
350   if (pcb->phase >= PPP_PHASE_TERMINATE) {
351     return ERR_INPROGRESS;
352   }
353 
354   /* LCP not open, close link protocol */
355   if (pcb->phase < PPP_PHASE_ESTABLISH) {
356     new_phase(pcb, PPP_PHASE_DISCONNECT);
357     ppp_link_terminated(pcb);
358     return ERR_OK;
359   }
360 
361   /*
362    * Only accept carrier lost signal on the stable running phase in order
363    * to prevent changing the PPP phase FSM in transition phases.
364    *
365    * Always using nocarrier = 0 is still recommended, this is going to
366    * take a little longer time, but is a safer choice from FSM point of view.
367    */
368   if (nocarrier && pcb->phase == PPP_PHASE_RUNNING) {
369     PPPDEBUG(LOG_DEBUG, ("ppp_close[%d]: carrier lost -> lcp_lowerdown\n", pcb->netif->num));
370     lcp_lowerdown(pcb);
371     /* forced link termination, this will force link protocol to disconnect. */
372     link_terminated(pcb);
373     return ERR_OK;
374   }
375 
376   /* Disconnect */
377   PPPDEBUG(LOG_DEBUG, ("ppp_close[%d]: kill_link -> lcp_close\n", pcb->netif->num));
378   /* LCP soft close request. */
379   lcp_close(pcb, "User request");
380   return ERR_OK;
381 }
382 
383 /*
384  * Release the control block.
385  *
386  * This can only be called if PPP is in the dead phase.
387  *
388  * You must use ppp_close() before if you wish to terminate
389  * an established PPP session.
390  *
391  * Return 0 on success, an error code on failure.
392  */
ppp_free(ppp_pcb * pcb)393 err_t ppp_free(ppp_pcb *pcb) {
394   err_t err;
395   LWIP_ASSERT_CORE_LOCKED();
396   if (pcb->phase != PPP_PHASE_DEAD) {
397     return ERR_CONN;
398   }
399 
400   PPPDEBUG(LOG_DEBUG, ("ppp_free[%d]\n", pcb->netif->num));
401 
402   netif_remove(pcb->netif);
403 
404   err = pcb->link_cb->free(pcb, pcb->link_ctx_cb);
405 
406   LWIP_MEMPOOL_FREE(PPP_PCB, pcb);
407   return err;
408 }
409 
410 /* Get and set parameters for the given connection.
411  * Return 0 on success, an error code on failure. */
412 err_t
ppp_ioctl(ppp_pcb * pcb,u8_t cmd,void * arg)413 ppp_ioctl(ppp_pcb *pcb, u8_t cmd, void *arg)
414 {
415   LWIP_ASSERT_CORE_LOCKED();
416   if (pcb == NULL) {
417     return ERR_VAL;
418   }
419 
420   switch(cmd) {
421     case PPPCTLG_UPSTATUS:      /* Get the PPP up status. */
422       if (!arg) {
423         goto fail;
424       }
425       *(int *)arg = (int)(0
426 #if PPP_IPV4_SUPPORT
427            || pcb->if4_up
428 #endif /* PPP_IPV4_SUPPORT */
429 #if PPP_IPV6_SUPPORT
430            || pcb->if6_up
431 #endif /* PPP_IPV6_SUPPORT */
432            );
433       return ERR_OK;
434 
435     case PPPCTLG_ERRCODE:       /* Get the PPP error code. */
436       if (!arg) {
437         goto fail;
438       }
439       *(int *)arg = (int)(pcb->err_code);
440       return ERR_OK;
441 
442     default:
443       goto fail;
444   }
445 
446 fail:
447   return ERR_VAL;
448 }
449 
450 
451 /**********************************/
452 /*** LOCAL FUNCTION DEFINITIONS ***/
453 /**********************************/
454 
ppp_do_connect(void * arg)455 static void ppp_do_connect(void *arg) {
456   ppp_pcb *pcb = (ppp_pcb*)arg;
457 
458   LWIP_ASSERT("pcb->phase == PPP_PHASE_DEAD || pcb->phase == PPP_PHASE_HOLDOFF", pcb->phase == PPP_PHASE_DEAD || pcb->phase == PPP_PHASE_HOLDOFF);
459 
460   new_phase(pcb, PPP_PHASE_INITIALIZE);
461   pcb->link_cb->connect(pcb, pcb->link_ctx_cb);
462 }
463 
464 /*
465  * ppp_netif_init_cb - netif init callback
466  */
ppp_netif_init_cb(struct netif * netif)467 static err_t ppp_netif_init_cb(struct netif *netif) {
468   netif->name[0] = 'p';
469   netif->name[1] = 'p';
470 #if PPP_IPV4_SUPPORT
471   netif->output = ppp_netif_output_ip4;
472 #endif /* PPP_IPV4_SUPPORT */
473 #if PPP_IPV6_SUPPORT
474   netif->output_ip6 = ppp_netif_output_ip6;
475 #endif /* PPP_IPV6_SUPPORT */
476   netif->flags = NETIF_FLAG_UP;
477 #if LWIP_NETIF_HOSTNAME
478   /* @todo: Initialize interface hostname */
479   /* netif_set_hostname(netif, "lwip"); */
480 #endif /* LWIP_NETIF_HOSTNAME */
481   return ERR_OK;
482 }
483 
484 #if PPP_IPV4_SUPPORT
485 /*
486  * Send an IPv4 packet on the given connection.
487  */
ppp_netif_output_ip4(struct netif * netif,struct pbuf * pb,const ip4_addr_t * ipaddr)488 static err_t ppp_netif_output_ip4(struct netif *netif, struct pbuf *pb, const ip4_addr_t *ipaddr) {
489   LWIP_UNUSED_ARG(ipaddr);
490   return ppp_netif_output(netif, pb, PPP_IP);
491 }
492 #endif /* PPP_IPV4_SUPPORT */
493 
494 #if PPP_IPV6_SUPPORT
495 /*
496  * Send an IPv6 packet on the given connection.
497  */
ppp_netif_output_ip6(struct netif * netif,struct pbuf * pb,const ip6_addr_t * ipaddr)498 static err_t ppp_netif_output_ip6(struct netif *netif, struct pbuf *pb, const ip6_addr_t *ipaddr) {
499   LWIP_UNUSED_ARG(ipaddr);
500   return ppp_netif_output(netif, pb, PPP_IPV6);
501 }
502 #endif /* PPP_IPV6_SUPPORT */
503 
ppp_netif_output(struct netif * netif,struct pbuf * pb,u16_t protocol)504 static err_t ppp_netif_output(struct netif *netif, struct pbuf *pb, u16_t protocol) {
505   ppp_pcb *pcb = (ppp_pcb*)netif->state;
506   err_t err;
507   struct pbuf *fpb = NULL;
508 
509   /* Check that the link is up. */
510   if (0
511 #if PPP_IPV4_SUPPORT
512       || (protocol == PPP_IP && !pcb->if4_up)
513 #endif /* PPP_IPV4_SUPPORT */
514 #if PPP_IPV6_SUPPORT
515       || (protocol == PPP_IPV6 && !pcb->if6_up)
516 #endif /* PPP_IPV6_SUPPORT */
517       ) {
518     PPPDEBUG(LOG_ERR, ("ppp_netif_output[%d]: link not up\n", pcb->netif->num));
519     goto err_rte_drop;
520   }
521 
522 #if MPPE_SUPPORT
523   /* If MPPE is required, refuse any IP packet until we are able to crypt them. */
524   if (pcb->settings.require_mppe && pcb->ccp_transmit_method != CI_MPPE) {
525     PPPDEBUG(LOG_ERR, ("ppp_netif_output[%d]: MPPE required, not up\n", pcb->netif->num));
526     goto err_rte_drop;
527   }
528 #endif /* MPPE_SUPPORT */
529 
530 #if VJ_SUPPORT
531   /*
532    * Attempt Van Jacobson header compression if VJ is configured and
533    * this is an IP packet.
534    */
535   if (protocol == PPP_IP && pcb->vj_enabled) {
536     switch (vj_compress_tcp(&pcb->vj_comp, &pb)) {
537       case TYPE_IP:
538         /* No change...
539            protocol = PPP_IP; */
540         break;
541       case TYPE_COMPRESSED_TCP:
542         /* vj_compress_tcp() returns a new allocated pbuf, indicate we should free
543          * our duplicated pbuf later */
544         fpb = pb;
545         protocol = PPP_VJC_COMP;
546         break;
547       case TYPE_UNCOMPRESSED_TCP:
548         /* vj_compress_tcp() returns a new allocated pbuf, indicate we should free
549          * our duplicated pbuf later */
550         fpb = pb;
551         protocol = PPP_VJC_UNCOMP;
552         break;
553       default:
554         PPPDEBUG(LOG_WARNING, ("ppp_netif_output[%d]: bad IP packet\n", pcb->netif->num));
555         LINK_STATS_INC(link.proterr);
556         LINK_STATS_INC(link.drop);
557         MIB2_STATS_NETIF_INC(pcb->netif, ifoutdiscards);
558         return ERR_VAL;
559     }
560   }
561 #endif /* VJ_SUPPORT */
562 
563 #if CCP_SUPPORT
564   switch (pcb->ccp_transmit_method) {
565   case 0:
566     break; /* Don't compress */
567 #if MPPE_SUPPORT
568   case CI_MPPE:
569     if ((err = mppe_compress(pcb, &pcb->mppe_comp, &pb, protocol)) != ERR_OK) {
570       LINK_STATS_INC(link.memerr);
571       LINK_STATS_INC(link.drop);
572       MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
573       goto err;
574     }
575     /* if VJ compressor returned a new allocated pbuf, free it */
576     if (fpb) {
577       pbuf_free(fpb);
578     }
579     /* mppe_compress() returns a new allocated pbuf, indicate we should free
580      * our duplicated pbuf later */
581     fpb = pb;
582     protocol = PPP_COMP;
583     break;
584 #endif /* MPPE_SUPPORT */
585   default:
586     PPPDEBUG(LOG_ERR, ("ppp_netif_output[%d]: bad CCP transmit method\n", pcb->netif->num));
587     goto err_rte_drop; /* Cannot really happen, we only negotiate what we are able to do */
588   }
589 #endif /* CCP_SUPPORT */
590 
591   err = pcb->link_cb->netif_output(pcb, pcb->link_ctx_cb, pb, protocol);
592   goto err;
593 
594 err_rte_drop:
595   err = ERR_RTE;
596   LINK_STATS_INC(link.rterr);
597   LINK_STATS_INC(link.drop);
598   MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
599 err:
600   if (fpb) {
601     pbuf_free(fpb);
602   }
603   return err;
604 }
605 
606 /************************************/
607 /*** PRIVATE FUNCTION DEFINITIONS ***/
608 /************************************/
609 
610 /* Initialize the PPP subsystem. */
ppp_init(void)611 int ppp_init(void)
612 {
613 #if PPPOS_SUPPORT
614   LWIP_MEMPOOL_INIT(PPPOS_PCB);
615 #endif
616 #if PPPOE_SUPPORT
617   LWIP_MEMPOOL_INIT(PPPOE_IF);
618 #endif
619 #if PPPOL2TP_SUPPORT
620   LWIP_MEMPOOL_INIT(PPPOL2TP_PCB);
621 #endif
622 #if LWIP_PPP_API && LWIP_MPU_COMPATIBLE
623   LWIP_MEMPOOL_INIT(PPPAPI_MSG);
624 #endif
625 
626   LWIP_MEMPOOL_INIT(PPP_PCB);
627 
628   /*
629    * Initialize magic number generator now so that protocols may
630    * use magic numbers in initialization.
631    */
632   magic_init();
633 
634   return 0;
635 }
636 
637 /*
638  * Create a new PPP control block.
639  *
640  * This initializes the PPP control block but does not
641  * attempt to negotiate the LCP session.
642  *
643  * Return a new PPP connection control block pointer
644  * on success or a null pointer on failure.
645  */
ppp_new(struct netif * pppif,const struct link_callbacks * callbacks,void * link_ctx_cb,ppp_link_status_cb_fn link_status_cb,void * ctx_cb)646 ppp_pcb *ppp_new(struct netif *pppif, const struct link_callbacks *callbacks, void *link_ctx_cb, ppp_link_status_cb_fn link_status_cb, void *ctx_cb) {
647   ppp_pcb *pcb;
648   const struct protent *protp;
649   int i;
650 
651   /* PPP is single-threaded: without a callback,
652    * there is no way to know when the link is up. */
653   if (link_status_cb == NULL) {
654     return NULL;
655   }
656 
657   pcb = (ppp_pcb*)LWIP_MEMPOOL_ALLOC(PPP_PCB);
658   if (pcb == NULL) {
659     return NULL;
660   }
661 
662   memset(pcb, 0, sizeof(ppp_pcb));
663 
664   /* default configuration */
665 #if PAP_SUPPORT
666   pcb->settings.pap_timeout_time = UPAP_DEFTIMEOUT;
667   pcb->settings.pap_max_transmits = UPAP_DEFTRANSMITS;
668 #if PPP_SERVER
669   pcb->settings.pap_req_timeout = UPAP_DEFREQTIME;
670 #endif /* PPP_SERVER */
671 #endif /* PAP_SUPPORT */
672 
673 #if CHAP_SUPPORT
674   pcb->settings.chap_timeout_time = CHAP_DEFTIMEOUT;
675   pcb->settings.chap_max_transmits = CHAP_DEFTRANSMITS;
676 #if PPP_SERVER
677   pcb->settings.chap_rechallenge_time = CHAP_DEFRECHALLENGETIME;
678 #endif /* PPP_SERVER */
679 #endif /* CHAP_SUPPPORT */
680 
681 #if EAP_SUPPORT
682   pcb->settings.eap_req_time = EAP_DEFREQTIME;
683   pcb->settings.eap_allow_req = EAP_DEFALLOWREQ;
684 #if PPP_SERVER
685   pcb->settings.eap_timeout_time = EAP_DEFTIMEOUT;
686   pcb->settings.eap_max_transmits = EAP_DEFTRANSMITS;
687 #endif /* PPP_SERVER */
688 #endif /* EAP_SUPPORT */
689 
690   pcb->settings.lcp_loopbackfail = LCP_DEFLOOPBACKFAIL;
691   pcb->settings.lcp_echo_interval = LCP_ECHOINTERVAL;
692   pcb->settings.lcp_echo_fails = LCP_MAXECHOFAILS;
693 
694   pcb->settings.fsm_timeout_time = FSM_DEFTIMEOUT;
695   pcb->settings.fsm_max_conf_req_transmits = FSM_DEFMAXCONFREQS;
696   pcb->settings.fsm_max_term_transmits = FSM_DEFMAXTERMREQS;
697   pcb->settings.fsm_max_nak_loops = FSM_DEFMAXNAKLOOPS;
698 
699   pcb->netif = pppif;
700   MIB2_INIT_NETIF(pppif, snmp_ifType_ppp, 0);
701   if (!netif_add(pcb->netif,
702 #if LWIP_IPV4
703                  IP4_ADDR_ANY4, IP4_ADDR_BROADCAST, IP4_ADDR_ANY4,
704 #endif /* LWIP_IPV4 */
705                  (void *)pcb, ppp_netif_init_cb, NULL)) {
706     LWIP_MEMPOOL_FREE(PPP_PCB, pcb);
707     PPPDEBUG(LOG_ERR, ("ppp_new: netif_add failed\n"));
708     return NULL;
709   }
710 
711   pcb->link_cb = callbacks;
712   pcb->link_ctx_cb = link_ctx_cb;
713   pcb->link_status_cb = link_status_cb;
714   pcb->ctx_cb = ctx_cb;
715 
716   /*
717    * Initialize each protocol.
718    */
719   for (i = 0; (protp = protocols[i]) != NULL; ++i) {
720       (*protp->init)(pcb);
721   }
722 
723   new_phase(pcb, PPP_PHASE_DEAD);
724   return pcb;
725 }
726 
727 /** Initiate LCP open request */
ppp_start(ppp_pcb * pcb)728 void ppp_start(ppp_pcb *pcb) {
729   PPPDEBUG(LOG_DEBUG, ("ppp_start[%d]\n", pcb->netif->num));
730 
731   /* Clean data not taken care by anything else, mostly shared data. */
732 #if PPP_STATS_SUPPORT
733   link_stats_valid = 0;
734 #endif /* PPP_STATS_SUPPORT */
735 #if MPPE_SUPPORT
736   pcb->mppe_keys_set = 0;
737   memset(&pcb->mppe_comp, 0, sizeof(pcb->mppe_comp));
738   memset(&pcb->mppe_decomp, 0, sizeof(pcb->mppe_decomp));
739 #endif /* MPPE_SUPPORT */
740 #if VJ_SUPPORT
741   vj_compress_init(&pcb->vj_comp);
742 #endif /* VJ_SUPPORT */
743 
744   /* Start protocol */
745   new_phase(pcb, PPP_PHASE_ESTABLISH);
746   lcp_open(pcb);
747   lcp_lowerup(pcb);
748   PPPDEBUG(LOG_DEBUG, ("ppp_start[%d]: finished\n", pcb->netif->num));
749 }
750 
751 /** Called when link failed to setup */
ppp_link_failed(ppp_pcb * pcb)752 void ppp_link_failed(ppp_pcb *pcb) {
753   PPPDEBUG(LOG_DEBUG, ("ppp_link_failed[%d]\n", pcb->netif->num));
754   new_phase(pcb, PPP_PHASE_DEAD);
755   pcb->err_code = PPPERR_OPEN;
756   pcb->link_status_cb(pcb, pcb->err_code, pcb->ctx_cb);
757 }
758 
759 /** Called when link is normally down (i.e. it was asked to end) */
ppp_link_end(ppp_pcb * pcb)760 void ppp_link_end(ppp_pcb *pcb) {
761   PPPDEBUG(LOG_DEBUG, ("ppp_link_end[%d]\n", pcb->netif->num));
762   new_phase(pcb, PPP_PHASE_DEAD);
763   if (pcb->err_code == PPPERR_NONE) {
764     pcb->err_code = PPPERR_CONNECT;
765   }
766   pcb->link_status_cb(pcb, pcb->err_code, pcb->ctx_cb);
767 }
768 
769 /*
770  * Pass the processed input packet to the appropriate handler.
771  * This function and all handlers run in the context of the tcpip_thread
772  */
ppp_input(ppp_pcb * pcb,struct pbuf * pb)773 void ppp_input(ppp_pcb *pcb, struct pbuf *pb) {
774   u16_t protocol;
775 #if PPP_DEBUG && PPP_PROTOCOLNAME
776     const char *pname;
777 #endif /* PPP_DEBUG && PPP_PROTOCOLNAME */
778 
779   magic_randomize();
780 
781   if (pb->len < 2) {
782     PPPDEBUG(LOG_ERR, ("ppp_input[%d]: packet too short\n", pcb->netif->num));
783     goto drop;
784   }
785   protocol = (((u8_t *)pb->payload)[0] << 8) | ((u8_t*)pb->payload)[1];
786 
787 #if PRINTPKT_SUPPORT
788   ppp_dump_packet(pcb, "rcvd", (unsigned char *)pb->payload, pb->len);
789 #endif /* PRINTPKT_SUPPORT */
790 
791   pbuf_remove_header(pb, sizeof(protocol));
792 
793   LINK_STATS_INC(link.recv);
794   MIB2_STATS_NETIF_INC(pcb->netif, ifinucastpkts);
795   MIB2_STATS_NETIF_ADD(pcb->netif, ifinoctets, pb->tot_len);
796 
797   /*
798    * Toss all non-LCP packets unless LCP is OPEN.
799    */
800   if (protocol != PPP_LCP && pcb->lcp_fsm.state != PPP_FSM_OPENED) {
801     ppp_dbglog("Discarded non-LCP packet when LCP not open");
802     goto drop;
803   }
804 
805   /*
806    * Until we get past the authentication phase, toss all packets
807    * except LCP, LQR and authentication packets.
808    */
809   if (pcb->phase <= PPP_PHASE_AUTHENTICATE
810    && !(protocol == PPP_LCP
811 #if LQR_SUPPORT
812    || protocol == PPP_LQR
813 #endif /* LQR_SUPPORT */
814 #if PAP_SUPPORT
815    || protocol == PPP_PAP
816 #endif /* PAP_SUPPORT */
817 #if CHAP_SUPPORT
818    || protocol == PPP_CHAP
819 #endif /* CHAP_SUPPORT */
820 #if EAP_SUPPORT
821    || protocol == PPP_EAP
822 #endif /* EAP_SUPPORT */
823    )) {
824     ppp_dbglog("discarding proto 0x%x in phase %d", protocol, pcb->phase);
825     goto drop;
826   }
827 
828 #if CCP_SUPPORT
829 #if MPPE_SUPPORT
830   /*
831    * MPPE is required and unencrypted data has arrived (this
832    * should never happen!). We should probably drop the link if
833    * the protocol is in the range of what should be encrypted.
834    * At the least, we drop this packet.
835    */
836   if (pcb->settings.require_mppe && protocol != PPP_COMP && protocol < 0x8000) {
837     PPPDEBUG(LOG_ERR, ("ppp_input[%d]: MPPE required, received unencrypted data!\n", pcb->netif->num));
838     goto drop;
839   }
840 #endif /* MPPE_SUPPORT */
841 
842   if (protocol == PPP_COMP) {
843     u8_t *pl;
844 
845     switch (pcb->ccp_receive_method) {
846 #if MPPE_SUPPORT
847     case CI_MPPE:
848       if (mppe_decompress(pcb, &pcb->mppe_decomp, &pb) != ERR_OK) {
849         goto drop;
850       }
851       break;
852 #endif /* MPPE_SUPPORT */
853     default:
854       PPPDEBUG(LOG_ERR, ("ppp_input[%d]: bad CCP receive method\n", pcb->netif->num));
855       goto drop; /* Cannot really happen, we only negotiate what we are able to do */
856     }
857 
858     /* Assume no PFC */
859     if (pb->len < 2) {
860       goto drop;
861     }
862 
863     /* Extract and hide protocol (do PFC decompression if necessary) */
864     pl = (u8_t*)pb->payload;
865     if (pl[0] & 0x01) {
866       protocol = pl[0];
867       pbuf_remove_header(pb, 1);
868     } else {
869       protocol = (pl[0] << 8) | pl[1];
870       pbuf_remove_header(pb, 2);
871     }
872   }
873 #endif /* CCP_SUPPORT */
874 
875   switch(protocol) {
876 
877 #if PPP_IPV4_SUPPORT
878     case PPP_IP:            /* Internet Protocol */
879       PPPDEBUG(LOG_INFO, ("ppp_input[%d]: ip in pbuf len=%d\n", pcb->netif->num, pb->tot_len));
880       ip4_input(pb, pcb->netif);
881       return;
882 #endif /* PPP_IPV4_SUPPORT */
883 
884 #if PPP_IPV6_SUPPORT
885     case PPP_IPV6:          /* Internet Protocol Version 6 */
886       PPPDEBUG(LOG_INFO, ("ppp_input[%d]: ip6 in pbuf len=%d\n", pcb->netif->num, pb->tot_len));
887       ip6_input(pb, pcb->netif);
888       return;
889 #endif /* PPP_IPV6_SUPPORT */
890 
891 #if VJ_SUPPORT
892     case PPP_VJC_COMP:      /* VJ compressed TCP */
893       /*
894        * Clip off the VJ header and prepend the rebuilt TCP/IP header and
895        * pass the result to IP.
896        */
897       PPPDEBUG(LOG_INFO, ("ppp_input[%d]: vj_comp in pbuf len=%d\n", pcb->netif->num, pb->tot_len));
898       if (pcb->vj_enabled && vj_uncompress_tcp(&pb, &pcb->vj_comp) >= 0) {
899         ip4_input(pb, pcb->netif);
900         return;
901       }
902       /* Something's wrong so drop it. */
903       PPPDEBUG(LOG_WARNING, ("ppp_input[%d]: Dropping VJ compressed\n", pcb->netif->num));
904       break;
905 
906     case PPP_VJC_UNCOMP:    /* VJ uncompressed TCP */
907       /*
908        * Process the TCP/IP header for VJ header compression and then pass
909        * the packet to IP.
910        */
911       PPPDEBUG(LOG_INFO, ("ppp_input[%d]: vj_un in pbuf len=%d\n", pcb->netif->num, pb->tot_len));
912       if (pcb->vj_enabled && vj_uncompress_uncomp(pb, &pcb->vj_comp) >= 0) {
913         ip4_input(pb, pcb->netif);
914         return;
915       }
916       /* Something's wrong so drop it. */
917       PPPDEBUG(LOG_WARNING, ("ppp_input[%d]: Dropping VJ uncompressed\n", pcb->netif->num));
918       break;
919 #endif /* VJ_SUPPORT */
920 
921     default: {
922       int i;
923       const struct protent *protp;
924 
925       /*
926        * Upcall the proper protocol input routine.
927        */
928       for (i = 0; (protp = protocols[i]) != NULL; ++i) {
929         if (protp->protocol == protocol) {
930           pb = pbuf_coalesce(pb, PBUF_RAW);
931           (*protp->input)(pcb, (u8_t*)pb->payload, pb->len);
932           goto out;
933         }
934 #if 0   /* UNUSED
935          *
936          * This is actually a (hacked?) way for the Linux kernel to pass a data
937          * packet to pppd. pppd in normal condition only do signaling
938          * (LCP, PAP, CHAP, IPCP, ...) and does not handle any data packet at all.
939          *
940          * We don't even need this interface, which is only there because of PPP
941          * interface limitation between Linux kernel and pppd. For MPPE, which uses
942          * CCP to negotiate although it is not really a (de)compressor, we added
943          * ccp_resetrequest() in CCP and MPPE input data flow is calling either
944          * ccp_resetrequest() or lcp_close() if the issue is, respectively, non-fatal
945          * or fatal, this is what ccp_datainput() really do.
946          */
947         if (protocol == (protp->protocol & ~0x8000)
948           && protp->datainput != NULL) {
949           (*protp->datainput)(pcb, pb->payload, pb->len);
950           goto out;
951         }
952 #endif /* UNUSED */
953       }
954 
955 #if PPP_DEBUG
956 #if PPP_PROTOCOLNAME
957       pname = protocol_name(protocol);
958       if (pname != NULL) {
959         ppp_warn("Unsupported protocol '%s' (0x%x) received", pname, protocol);
960       } else
961 #endif /* PPP_PROTOCOLNAME */
962         ppp_warn("Unsupported protocol 0x%x received", protocol);
963 #endif /* PPP_DEBUG */
964         if (pbuf_add_header(pb, sizeof(protocol))) {
965           PPPDEBUG(LOG_WARNING, ("ppp_input[%d]: Dropping (pbuf_add_header failed)\n", pcb->netif->num));
966           goto drop;
967         }
968         lcp_sprotrej(pcb, (u8_t*)pb->payload, pb->len);
969       }
970       break;
971   }
972 
973 drop:
974   LINK_STATS_INC(link.drop);
975   MIB2_STATS_NETIF_INC(pcb->netif, ifindiscards);
976 
977 out:
978   pbuf_free(pb);
979 }
980 
981 /*
982  * Write a pbuf to a ppp link, only used from PPP functions
983  * to send PPP packets.
984  *
985  * IPv4 and IPv6 packets from lwIP are sent, respectively,
986  * with ppp_netif_output_ip4() and ppp_netif_output_ip6()
987  * functions (which are callbacks of the netif PPP interface).
988  */
ppp_write(ppp_pcb * pcb,struct pbuf * p)989 err_t ppp_write(ppp_pcb *pcb, struct pbuf *p) {
990 #if PRINTPKT_SUPPORT
991   ppp_dump_packet(pcb, "sent", (unsigned char *)p->payload+2, p->len-2);
992 #endif /* PRINTPKT_SUPPORT */
993   return pcb->link_cb->write(pcb, pcb->link_ctx_cb, p);
994 }
995 
ppp_link_terminated(ppp_pcb * pcb)996 void ppp_link_terminated(ppp_pcb *pcb) {
997   PPPDEBUG(LOG_DEBUG, ("ppp_link_terminated[%d]\n", pcb->netif->num));
998   pcb->link_cb->disconnect(pcb, pcb->link_ctx_cb);
999   PPPDEBUG(LOG_DEBUG, ("ppp_link_terminated[%d]: finished.\n", pcb->netif->num));
1000 }
1001 
1002 
1003 /************************************************************************
1004  * Functions called by various PPP subsystems to configure
1005  * the PPP interface or change the PPP phase.
1006  */
1007 
1008 /*
1009  * new_phase - signal the start of a new phase of pppd's operation.
1010  */
new_phase(ppp_pcb * pcb,int p)1011 void new_phase(ppp_pcb *pcb, int p) {
1012   pcb->phase = p;
1013   PPPDEBUG(LOG_DEBUG, ("ppp phase changed[%d]: phase=%d\n", pcb->netif->num, pcb->phase));
1014 #if PPP_NOTIFY_PHASE
1015   if (pcb->notify_phase_cb != NULL) {
1016     pcb->notify_phase_cb(pcb, p, pcb->ctx_cb);
1017   }
1018 #endif /* PPP_NOTIFY_PHASE */
1019 }
1020 
1021 /*
1022  * ppp_send_config - configure the transmit-side characteristics of
1023  * the ppp interface.
1024  */
ppp_send_config(ppp_pcb * pcb,int mtu,u32_t accm,int pcomp,int accomp)1025 int ppp_send_config(ppp_pcb *pcb, int mtu, u32_t accm, int pcomp, int accomp) {
1026   LWIP_UNUSED_ARG(mtu);
1027   /* pcb->mtu = mtu; -- set correctly with netif_set_mtu */
1028 
1029   if (pcb->link_cb->send_config) {
1030     pcb->link_cb->send_config(pcb, pcb->link_ctx_cb, accm, pcomp, accomp);
1031   }
1032 
1033   PPPDEBUG(LOG_INFO, ("ppp_send_config[%d]\n", pcb->netif->num) );
1034   return 0;
1035 }
1036 
1037 /*
1038  * ppp_recv_config - configure the receive-side characteristics of
1039  * the ppp interface.
1040  */
ppp_recv_config(ppp_pcb * pcb,int mru,u32_t accm,int pcomp,int accomp)1041 int ppp_recv_config(ppp_pcb *pcb, int mru, u32_t accm, int pcomp, int accomp) {
1042   LWIP_UNUSED_ARG(mru);
1043 
1044   if (pcb->link_cb->recv_config) {
1045     pcb->link_cb->recv_config(pcb, pcb->link_ctx_cb, accm, pcomp, accomp);
1046   }
1047 
1048   PPPDEBUG(LOG_INFO, ("ppp_recv_config[%d]\n", pcb->netif->num));
1049   return 0;
1050 }
1051 
1052 #if PPP_IPV4_SUPPORT
1053 /*
1054  * sifaddr - Config the interface IP addresses and netmask.
1055  */
sifaddr(ppp_pcb * pcb,u32_t our_adr,u32_t his_adr,u32_t netmask)1056 int sifaddr(ppp_pcb *pcb, u32_t our_adr, u32_t his_adr, u32_t netmask) {
1057   ip4_addr_t ip, nm, gw;
1058 
1059   ip4_addr_set_u32(&ip, our_adr);
1060   ip4_addr_set_u32(&nm, netmask);
1061   ip4_addr_set_u32(&gw, his_adr);
1062   netif_set_addr(pcb->netif, &ip, &nm, &gw);
1063   return 1;
1064 }
1065 
1066 /********************************************************************
1067  *
1068  * cifaddr - Clear the interface IP addresses, and delete routes
1069  * through the interface if possible.
1070  */
cifaddr(ppp_pcb * pcb,u32_t our_adr,u32_t his_adr)1071 int cifaddr(ppp_pcb *pcb, u32_t our_adr, u32_t his_adr) {
1072   LWIP_UNUSED_ARG(our_adr);
1073   LWIP_UNUSED_ARG(his_adr);
1074 
1075   netif_set_addr(pcb->netif, IP4_ADDR_ANY4, IP4_ADDR_BROADCAST, IP4_ADDR_ANY4);
1076   return 1;
1077 }
1078 
1079 #if 0 /* UNUSED - PROXY ARP */
1080 /********************************************************************
1081  *
1082  * sifproxyarp - Make a proxy ARP entry for the peer.
1083  */
1084 
1085 int sifproxyarp(ppp_pcb *pcb, u32_t his_adr) {
1086   LWIP_UNUSED_ARG(pcb);
1087   LWIP_UNUSED_ARG(his_adr);
1088   return 0;
1089 }
1090 
1091 /********************************************************************
1092  *
1093  * cifproxyarp - Delete the proxy ARP entry for the peer.
1094  */
1095 
1096 int cifproxyarp(ppp_pcb *pcb, u32_t his_adr) {
1097   LWIP_UNUSED_ARG(pcb);
1098   LWIP_UNUSED_ARG(his_adr);
1099   return 0;
1100 }
1101 #endif /* UNUSED - PROXY ARP */
1102 
1103 #if LWIP_DNS
1104 /*
1105  * sdns - Config the DNS servers
1106  */
sdns(ppp_pcb * pcb,u32_t ns1,u32_t ns2)1107 int sdns(ppp_pcb *pcb, u32_t ns1, u32_t ns2) {
1108   ip_addr_t ns;
1109   LWIP_UNUSED_ARG(pcb);
1110 
1111   ip_addr_set_ip4_u32_val(ns, ns1);
1112   dns_setserver(0, &ns);
1113   ip_addr_set_ip4_u32_val(ns, ns2);
1114   dns_setserver(1, &ns);
1115   return 1;
1116 }
1117 
1118 /********************************************************************
1119  *
1120  * cdns - Clear the DNS servers
1121  */
cdns(ppp_pcb * pcb,u32_t ns1,u32_t ns2)1122 int cdns(ppp_pcb *pcb, u32_t ns1, u32_t ns2) {
1123   const ip_addr_t *nsa;
1124   ip_addr_t nsb;
1125   LWIP_UNUSED_ARG(pcb);
1126 
1127   nsa = dns_getserver(0);
1128   ip_addr_set_ip4_u32_val(nsb, ns1);
1129   if (ip_addr_cmp(nsa, &nsb)) {
1130     dns_setserver(0, IP_ADDR_ANY);
1131   }
1132   nsa = dns_getserver(1);
1133   ip_addr_set_ip4_u32_val(nsb, ns2);
1134   if (ip_addr_cmp(nsa, &nsb)) {
1135     dns_setserver(1, IP_ADDR_ANY);
1136   }
1137   return 1;
1138 }
1139 #endif /* LWIP_DNS */
1140 
1141 #if VJ_SUPPORT
1142 /********************************************************************
1143  *
1144  * sifvjcomp - config tcp header compression
1145  */
sifvjcomp(ppp_pcb * pcb,int vjcomp,int cidcomp,int maxcid)1146 int sifvjcomp(ppp_pcb *pcb, int vjcomp, int cidcomp, int maxcid) {
1147   pcb->vj_enabled = vjcomp;
1148   pcb->vj_comp.compressSlot = cidcomp;
1149   pcb->vj_comp.maxSlotIndex = maxcid;
1150   PPPDEBUG(LOG_INFO, ("sifvjcomp[%d]: VJ compress enable=%d slot=%d max slot=%d\n",
1151             pcb->netif->num, vjcomp, cidcomp, maxcid));
1152   return 0;
1153 }
1154 #endif /* VJ_SUPPORT */
1155 
1156 /*
1157  * sifup - Config the interface up and enable IP packets to pass.
1158  */
sifup(ppp_pcb * pcb)1159 int sifup(ppp_pcb *pcb) {
1160   pcb->if4_up = 1;
1161   pcb->err_code = PPPERR_NONE;
1162   netif_set_link_up(pcb->netif);
1163 
1164   PPPDEBUG(LOG_DEBUG, ("sifup[%d]: err_code=%d\n", pcb->netif->num, pcb->err_code));
1165   pcb->link_status_cb(pcb, pcb->err_code, pcb->ctx_cb);
1166   return 1;
1167 }
1168 
1169 /********************************************************************
1170  *
1171  * sifdown - Disable the indicated protocol and config the interface
1172  *           down if there are no remaining protocols.
1173  */
sifdown(ppp_pcb * pcb)1174 int sifdown(ppp_pcb *pcb) {
1175 
1176   pcb->if4_up = 0;
1177 
1178   if (1
1179 #if PPP_IPV6_SUPPORT
1180    /* set the interface down if IPv6 is down as well */
1181    && !pcb->if6_up
1182 #endif /* PPP_IPV6_SUPPORT */
1183   ) {
1184     /* make sure the netif link callback is called */
1185     netif_set_link_down(pcb->netif);
1186   }
1187   PPPDEBUG(LOG_DEBUG, ("sifdown[%d]: err_code=%d\n", pcb->netif->num, pcb->err_code));
1188   return 1;
1189 }
1190 
1191 /********************************************************************
1192  *
1193  * Return user specified netmask, modified by any mask we might determine
1194  * for address `addr' (in network byte order).
1195  * Here we scan through the system's list of interfaces, looking for
1196  * any non-point-to-point interfaces which might appear to be on the same
1197  * network as `addr'.  If we find any, we OR in their netmask to the
1198  * user-specified netmask.
1199  */
get_mask(u32_t addr)1200 u32_t get_mask(u32_t addr) {
1201 #if 0
1202   u32_t mask, nmask;
1203 
1204   addr = lwip_htonl(addr);
1205   if (IP_CLASSA(addr)) { /* determine network mask for address class */
1206     nmask = IP_CLASSA_NET;
1207   } else if (IP_CLASSB(addr)) {
1208     nmask = IP_CLASSB_NET;
1209   } else {
1210     nmask = IP_CLASSC_NET;
1211   }
1212 
1213   /* class D nets are disallowed by bad_ip_adrs */
1214   mask = PP_HTONL(0xffffff00UL) | lwip_htonl(nmask);
1215 
1216   /* XXX
1217    * Scan through the system's network interfaces.
1218    * Get each netmask and OR them into our mask.
1219    */
1220   /* return mask; */
1221   return mask;
1222 #endif /* 0 */
1223   LWIP_UNUSED_ARG(addr);
1224   return IPADDR_BROADCAST;
1225 }
1226 #endif /* PPP_IPV4_SUPPORT */
1227 
1228 #if PPP_IPV6_SUPPORT
1229 #define IN6_LLADDR_FROM_EUI64(ip6, eui64) do {    \
1230   ip6.addr[0] = PP_HTONL(0xfe800000);             \
1231   ip6.addr[1] = 0;                                \
1232   eui64_copy(eui64, ip6.addr[2]);                 \
1233   } while (0)
1234 
1235 /********************************************************************
1236  *
1237  * sif6addr - Config the interface with an IPv6 link-local address
1238  */
sif6addr(ppp_pcb * pcb,eui64_t our_eui64,eui64_t his_eui64)1239 int sif6addr(ppp_pcb *pcb, eui64_t our_eui64, eui64_t his_eui64) {
1240   ip6_addr_t ip6;
1241   LWIP_UNUSED_ARG(his_eui64);
1242 
1243   IN6_LLADDR_FROM_EUI64(ip6, our_eui64);
1244   netif_ip6_addr_set(pcb->netif, 0, &ip6);
1245   netif_ip6_addr_set_state(pcb->netif, 0, IP6_ADDR_PREFERRED);
1246   /* FIXME: should we add an IPv6 static neighbor using his_eui64 ? */
1247   return 1;
1248 }
1249 
1250 /********************************************************************
1251  *
1252  * cif6addr - Remove IPv6 address from interface
1253  */
cif6addr(ppp_pcb * pcb,eui64_t our_eui64,eui64_t his_eui64)1254 int cif6addr(ppp_pcb *pcb, eui64_t our_eui64, eui64_t his_eui64) {
1255   LWIP_UNUSED_ARG(our_eui64);
1256   LWIP_UNUSED_ARG(his_eui64);
1257 
1258   netif_ip6_addr_set_state(pcb->netif, 0, IP6_ADDR_INVALID);
1259   netif_ip6_addr_set(pcb->netif, 0, IP6_ADDR_ANY6);
1260   return 1;
1261 }
1262 
1263 /*
1264  * sif6up - Config the interface up and enable IPv6 packets to pass.
1265  */
sif6up(ppp_pcb * pcb)1266 int sif6up(ppp_pcb *pcb) {
1267 
1268   pcb->if6_up = 1;
1269   pcb->err_code = PPPERR_NONE;
1270   netif_set_link_up(pcb->netif);
1271 
1272   PPPDEBUG(LOG_DEBUG, ("sif6up[%d]: err_code=%d\n", pcb->netif->num, pcb->err_code));
1273   pcb->link_status_cb(pcb, pcb->err_code, pcb->ctx_cb);
1274   return 1;
1275 }
1276 
1277 /********************************************************************
1278  *
1279  * sif6down - Disable the indicated protocol and config the interface
1280  *            down if there are no remaining protocols.
1281  */
sif6down(ppp_pcb * pcb)1282 int sif6down(ppp_pcb *pcb) {
1283 
1284   pcb->if6_up = 0;
1285 
1286   if (1
1287 #if PPP_IPV4_SUPPORT
1288    /* set the interface down if IPv4 is down as well */
1289    && !pcb->if4_up
1290 #endif /* PPP_IPV4_SUPPORT */
1291   ) {
1292     /* make sure the netif link callback is called */
1293     netif_set_link_down(pcb->netif);
1294   }
1295   PPPDEBUG(LOG_DEBUG, ("sif6down[%d]: err_code=%d\n", pcb->netif->num, pcb->err_code));
1296   return 1;
1297 }
1298 #endif /* PPP_IPV6_SUPPORT */
1299 
1300 #if DEMAND_SUPPORT
1301 /*
1302  * sifnpmode - Set the mode for handling packets for a given NP.
1303  */
sifnpmode(ppp_pcb * pcb,int proto,enum NPmode mode)1304 int sifnpmode(ppp_pcb *pcb, int proto, enum NPmode mode) {
1305   LWIP_UNUSED_ARG(pcb);
1306   LWIP_UNUSED_ARG(proto);
1307   LWIP_UNUSED_ARG(mode);
1308   return 0;
1309 }
1310 #endif /* DEMAND_SUPPORT */
1311 
1312 /*
1313  * netif_set_mtu - set the MTU on the PPP network interface.
1314  */
netif_set_mtu(ppp_pcb * pcb,int mtu)1315 void netif_set_mtu(ppp_pcb *pcb, int mtu) {
1316 
1317   pcb->netif->mtu = mtu;
1318   PPPDEBUG(LOG_INFO, ("netif_set_mtu[%d]: mtu=%d\n", pcb->netif->num, mtu));
1319 }
1320 
1321 /*
1322  * netif_get_mtu - get PPP interface MTU
1323  */
netif_get_mtu(ppp_pcb * pcb)1324 int netif_get_mtu(ppp_pcb *pcb) {
1325 
1326   return pcb->netif->mtu;
1327 }
1328 
1329 #if CCP_SUPPORT
1330 #if 0 /* unused */
1331 /*
1332  * ccp_test - whether a given compression method is acceptable for use.
1333  */
1334 int
1335 ccp_test(ppp_pcb *pcb, u_char *opt_ptr, int opt_len, int for_transmit)
1336 {
1337   LWIP_UNUSED_ARG(pcb);
1338   LWIP_UNUSED_ARG(opt_ptr);
1339   LWIP_UNUSED_ARG(opt_len);
1340   LWIP_UNUSED_ARG(for_transmit);
1341   return -1;
1342 }
1343 #endif /* unused */
1344 
1345 /*
1346  * ccp_set - inform about the current state of CCP.
1347  */
1348 void
ccp_set(ppp_pcb * pcb,u8_t isopen,u8_t isup,u8_t receive_method,u8_t transmit_method)1349 ccp_set(ppp_pcb *pcb, u8_t isopen, u8_t isup, u8_t receive_method, u8_t transmit_method)
1350 {
1351   LWIP_UNUSED_ARG(isopen);
1352   LWIP_UNUSED_ARG(isup);
1353   pcb->ccp_receive_method = receive_method;
1354   pcb->ccp_transmit_method = transmit_method;
1355   PPPDEBUG(LOG_DEBUG, ("ccp_set[%d]: is_open=%d, is_up=%d, receive_method=%u, transmit_method=%u\n",
1356            pcb->netif->num, isopen, isup, receive_method, transmit_method));
1357 }
1358 
1359 void
ccp_reset_comp(ppp_pcb * pcb)1360 ccp_reset_comp(ppp_pcb *pcb)
1361 {
1362   switch (pcb->ccp_transmit_method) {
1363 #if MPPE_SUPPORT
1364   case CI_MPPE:
1365     mppe_comp_reset(pcb, &pcb->mppe_comp);
1366     break;
1367 #endif /* MPPE_SUPPORT */
1368   default:
1369     break;
1370   }
1371 }
1372 
1373 void
ccp_reset_decomp(ppp_pcb * pcb)1374 ccp_reset_decomp(ppp_pcb *pcb)
1375 {
1376   switch (pcb->ccp_receive_method) {
1377 #if MPPE_SUPPORT
1378   case CI_MPPE:
1379     mppe_decomp_reset(pcb, &pcb->mppe_decomp);
1380     break;
1381 #endif /* MPPE_SUPPORT */
1382   default:
1383     break;
1384   }
1385 }
1386 
1387 #if 0 /* unused */
1388 /*
1389  * ccp_fatal_error - returns 1 if decompression was disabled as a
1390  * result of an error detected after decompression of a packet,
1391  * 0 otherwise.  This is necessary because of patent nonsense.
1392  */
1393 int
1394 ccp_fatal_error(ppp_pcb *pcb)
1395 {
1396   LWIP_UNUSED_ARG(pcb);
1397   return 1;
1398 }
1399 #endif /* unused */
1400 #endif /* CCP_SUPPORT */
1401 
1402 #if PPP_IDLETIMELIMIT
1403 /********************************************************************
1404  *
1405  * get_idle_time - return how long the link has been idle.
1406  */
get_idle_time(ppp_pcb * pcb,struct ppp_idle * ip)1407 int get_idle_time(ppp_pcb *pcb, struct ppp_idle *ip) {
1408   /* FIXME: add idle time support and make it optional */
1409   LWIP_UNUSED_ARG(pcb);
1410   LWIP_UNUSED_ARG(ip);
1411   return 1;
1412 }
1413 #endif /* PPP_IDLETIMELIMIT */
1414 
1415 #if DEMAND_SUPPORT
1416 /********************************************************************
1417  *
1418  * get_loop_output - get outgoing packets from the ppp device,
1419  * and detect when we want to bring the real link up.
1420  * Return value is 1 if we need to bring up the link, 0 otherwise.
1421  */
get_loop_output(void)1422 int get_loop_output(void) {
1423   return 0;
1424 }
1425 #endif /* DEMAND_SUPPORT */
1426 
1427 #if PPP_PROTOCOLNAME
1428 /* List of protocol names, to make our messages a little more informative. */
1429 struct protocol_list {
1430   u_short proto;
1431   const char *name;
1432 } const protocol_list[] = {
1433   { 0x21, "IP" },
1434   { 0x23, "OSI Network Layer" },
1435   { 0x25, "Xerox NS IDP" },
1436   { 0x27, "DECnet Phase IV" },
1437   { 0x29, "Appletalk" },
1438   { 0x2b, "Novell IPX" },
1439   { 0x2d, "VJ compressed TCP/IP" },
1440   { 0x2f, "VJ uncompressed TCP/IP" },
1441   { 0x31, "Bridging PDU" },
1442   { 0x33, "Stream Protocol ST-II" },
1443   { 0x35, "Banyan Vines" },
1444   { 0x39, "AppleTalk EDDP" },
1445   { 0x3b, "AppleTalk SmartBuffered" },
1446   { 0x3d, "Multi-Link" },
1447   { 0x3f, "NETBIOS Framing" },
1448   { 0x41, "Cisco Systems" },
1449   { 0x43, "Ascom Timeplex" },
1450   { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" },
1451   { 0x47, "DCA Remote Lan" },
1452   { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" },
1453   { 0x4b, "SNA over 802.2" },
1454   { 0x4d, "SNA" },
1455   { 0x4f, "IP6 Header Compression" },
1456   { 0x51, "KNX Bridging Data" },
1457   { 0x53, "Encryption" },
1458   { 0x55, "Individual Link Encryption" },
1459   { 0x57, "IPv6" },
1460   { 0x59, "PPP Muxing" },
1461   { 0x5b, "Vendor-Specific Network Protocol" },
1462   { 0x61, "RTP IPHC Full Header" },
1463   { 0x63, "RTP IPHC Compressed TCP" },
1464   { 0x65, "RTP IPHC Compressed non-TCP" },
1465   { 0x67, "RTP IPHC Compressed UDP 8" },
1466   { 0x69, "RTP IPHC Compressed RTP 8" },
1467   { 0x6f, "Stampede Bridging" },
1468   { 0x73, "MP+" },
1469   { 0xc1, "NTCITS IPI" },
1470   { 0xfb, "single-link compression" },
1471   { 0xfd, "Compressed Datagram" },
1472   { 0x0201, "802.1d Hello Packets" },
1473   { 0x0203, "IBM Source Routing BPDU" },
1474   { 0x0205, "DEC LANBridge100 Spanning Tree" },
1475   { 0x0207, "Cisco Discovery Protocol" },
1476   { 0x0209, "Netcs Twin Routing" },
1477   { 0x020b, "STP - Scheduled Transfer Protocol" },
1478   { 0x020d, "EDP - Extreme Discovery Protocol" },
1479   { 0x0211, "Optical Supervisory Channel Protocol" },
1480   { 0x0213, "Optical Supervisory Channel Protocol" },
1481   { 0x0231, "Luxcom" },
1482   { 0x0233, "Sigma Network Systems" },
1483   { 0x0235, "Apple Client Server Protocol" },
1484   { 0x0281, "MPLS Unicast" },
1485   { 0x0283, "MPLS Multicast" },
1486   { 0x0285, "IEEE p1284.4 standard - data packets" },
1487   { 0x0287, "ETSI TETRA Network Protocol Type 1" },
1488   { 0x0289, "Multichannel Flow Treatment Protocol" },
1489   { 0x2063, "RTP IPHC Compressed TCP No Delta" },
1490   { 0x2065, "RTP IPHC Context State" },
1491   { 0x2067, "RTP IPHC Compressed UDP 16" },
1492   { 0x2069, "RTP IPHC Compressed RTP 16" },
1493   { 0x4001, "Cray Communications Control Protocol" },
1494   { 0x4003, "CDPD Mobile Network Registration Protocol" },
1495   { 0x4005, "Expand accelerator protocol" },
1496   { 0x4007, "ODSICP NCP" },
1497   { 0x4009, "DOCSIS DLL" },
1498   { 0x400B, "Cetacean Network Detection Protocol" },
1499   { 0x4021, "Stacker LZS" },
1500   { 0x4023, "RefTek Protocol" },
1501   { 0x4025, "Fibre Channel" },
1502   { 0x4027, "EMIT Protocols" },
1503   { 0x405b, "Vendor-Specific Protocol (VSP)" },
1504   { 0x8021, "Internet Protocol Control Protocol" },
1505   { 0x8023, "OSI Network Layer Control Protocol" },
1506   { 0x8025, "Xerox NS IDP Control Protocol" },
1507   { 0x8027, "DECnet Phase IV Control Protocol" },
1508   { 0x8029, "Appletalk Control Protocol" },
1509   { 0x802b, "Novell IPX Control Protocol" },
1510   { 0x8031, "Bridging NCP" },
1511   { 0x8033, "Stream Protocol Control Protocol" },
1512   { 0x8035, "Banyan Vines Control Protocol" },
1513   { 0x803d, "Multi-Link Control Protocol" },
1514   { 0x803f, "NETBIOS Framing Control Protocol" },
1515   { 0x8041, "Cisco Systems Control Protocol" },
1516   { 0x8043, "Ascom Timeplex" },
1517   { 0x8045, "Fujitsu LBLB Control Protocol" },
1518   { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
1519   { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
1520   { 0x804b, "SNA over 802.2 Control Protocol" },
1521   { 0x804d, "SNA Control Protocol" },
1522   { 0x804f, "IP6 Header Compression Control Protocol" },
1523   { 0x8051, "KNX Bridging Control Protocol" },
1524   { 0x8053, "Encryption Control Protocol" },
1525   { 0x8055, "Individual Link Encryption Control Protocol" },
1526   { 0x8057, "IPv6 Control Protocol" },
1527   { 0x8059, "PPP Muxing Control Protocol" },
1528   { 0x805b, "Vendor-Specific Network Control Protocol (VSNCP)" },
1529   { 0x806f, "Stampede Bridging Control Protocol" },
1530   { 0x8073, "MP+ Control Protocol" },
1531   { 0x80c1, "NTCITS IPI Control Protocol" },
1532   { 0x80fb, "Single Link Compression Control Protocol" },
1533   { 0x80fd, "Compression Control Protocol" },
1534   { 0x8207, "Cisco Discovery Protocol Control" },
1535   { 0x8209, "Netcs Twin Routing" },
1536   { 0x820b, "STP - Control Protocol" },
1537   { 0x820d, "EDPCP - Extreme Discovery Protocol Ctrl Prtcl" },
1538   { 0x8235, "Apple Client Server Protocol Control" },
1539   { 0x8281, "MPLSCP" },
1540   { 0x8285, "IEEE p1284.4 standard - Protocol Control" },
1541   { 0x8287, "ETSI TETRA TNP1 Control Protocol" },
1542   { 0x8289, "Multichannel Flow Treatment Protocol" },
1543   { 0xc021, "Link Control Protocol" },
1544   { 0xc023, "Password Authentication Protocol" },
1545   { 0xc025, "Link Quality Report" },
1546   { 0xc027, "Shiva Password Authentication Protocol" },
1547   { 0xc029, "CallBack Control Protocol (CBCP)" },
1548   { 0xc02b, "BACP Bandwidth Allocation Control Protocol" },
1549   { 0xc02d, "BAP" },
1550   { 0xc05b, "Vendor-Specific Authentication Protocol (VSAP)" },
1551   { 0xc081, "Container Control Protocol" },
1552   { 0xc223, "Challenge Handshake Authentication Protocol" },
1553   { 0xc225, "RSA Authentication Protocol" },
1554   { 0xc227, "Extensible Authentication Protocol" },
1555   { 0xc229, "Mitsubishi Security Info Exch Ptcl (SIEP)" },
1556   { 0xc26f, "Stampede Bridging Authorization Protocol" },
1557   { 0xc281, "Proprietary Authentication Protocol" },
1558   { 0xc283, "Proprietary Authentication Protocol" },
1559   { 0xc481, "Proprietary Node ID Authentication Protocol" },
1560   { 0, NULL },
1561 };
1562 
1563 /*
1564  * protocol_name - find a name for a PPP protocol.
1565  */
protocol_name(int proto)1566 const char * protocol_name(int proto) {
1567   const struct protocol_list *lp;
1568 
1569   for (lp = protocol_list; lp->proto != 0; ++lp) {
1570     if (proto == lp->proto) {
1571       return lp->name;
1572     }
1573   }
1574   return NULL;
1575 }
1576 #endif /* PPP_PROTOCOLNAME */
1577 
1578 #if PPP_STATS_SUPPORT
1579 
1580 /* ---- Note on PPP Stats support ----
1581  *
1582  * The one willing link stats support should add the get_ppp_stats()
1583  * to fetch statistics from lwIP.
1584  */
1585 
1586 /*
1587  * reset_link_stats - "reset" stats when link goes up.
1588  */
reset_link_stats(int u)1589 void reset_link_stats(int u) {
1590   if (!get_ppp_stats(u, &old_link_stats)) {
1591     return;
1592   }
1593   gettimeofday(&start_time, NULL);
1594 }
1595 
1596 /*
1597  * update_link_stats - get stats at link termination.
1598  */
update_link_stats(int u)1599 void update_link_stats(int u) {
1600   struct timeval now;
1601   char numbuf[32];
1602 
1603   if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) {
1604     return;
1605   }
1606   link_connect_time = now.tv_sec - start_time.tv_sec;
1607   link_stats_valid = 1;
1608 
1609   link_stats.bytes_in  -= old_link_stats.bytes_in;
1610   link_stats.bytes_out -= old_link_stats.bytes_out;
1611   link_stats.pkts_in   -= old_link_stats.pkts_in;
1612   link_stats.pkts_out  -= old_link_stats.pkts_out;
1613 }
1614 
print_link_stats()1615 void print_link_stats() {
1616   /*
1617    * Print connect time and statistics.
1618    */
1619   if (link_stats_valid) {
1620     int t = (link_connect_time + 5) / 6;    /* 1/10ths of minutes */
1621     info("Connect time %d.%d minutes.", t/10, t%10);
1622     info("Sent %u bytes, received %u bytes.", link_stats.bytes_out, link_stats.bytes_in);
1623     link_stats_valid = 0;
1624   }
1625 }
1626 #endif /* PPP_STATS_SUPPORT */
1627 
1628 #endif /* PPP_SUPPORT */
1629