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