STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
pppoe.c
Go to the documentation of this file.
1 /*****************************************************************************
2 * pppoe.c - PPP Over Ethernet implementation for lwIP.
3 *
4 * Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.
5 *
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice and the following disclaimer are included verbatim in any
10 * distributions. No written agreement, license, or royalty fee is required
11 * for any of the authorized uses.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 ******************************************************************************
25 * REVISION HISTORY
26 *
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>
28 * Ported to lwIP.
29 *****************************************************************************/
30 
31 
32 
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */
34 
35 /*-
36  * Copyright (c) 2002 The NetBSD Foundation, Inc.
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to The NetBSD Foundation
40  * by Martin Husemann <martin@NetBSD.org>.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  * notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  * notice, this list of conditions and the following disclaimer in the
49  * documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  * must display the following acknowledgement:
52  * This product includes software developed by the NetBSD
53  * Foundation, Inc. and its contributors.
54  * 4. Neither the name of The NetBSD Foundation nor the names of its
55  * contributors may be used to endorse or promote products derived
56  * from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  */
70 
71 #include "lwip/opt.h"
72 #if PPP_SUPPORT && PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
73 
74 #if 0 /* UNUSED */
75 #include <string.h>
76 #include <stdio.h>
77 #endif /* UNUSED */
78 
79 #include "lwip/lwip_timers.h"
80 #include "lwip/memp.h"
81 #include "lwip/stats.h"
82 #include "lwip/snmp.h"
83 
84 #include "netif/ppp/ppp_impl.h"
85 #include "netif/ppp/lcp.h"
86 #include "netif/ppp/ipcp.h"
87 #include "netif/ppp/pppoe.h"
88 
89 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
90 #define PPPOE_ADD_16(PTR, VAL) \
91  *(PTR)++ = (u8_t)((VAL) / 256); \
92  *(PTR)++ = (u8_t)((VAL) % 256)
93 
94 /* Add a complete PPPoE header to the buffer pointed to by PTR */
95 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
96  *(PTR)++ = PPPOE_VERTYPE; \
97  *(PTR)++ = (CODE); \
98  PPPOE_ADD_16(PTR, SESS); \
99  PPPOE_ADD_16(PTR, LEN)
100 
101 #define PPPOE_DISC_TIMEOUT (5*1000) /* base for quick timeout calculation */
102 #define PPPOE_SLOW_RETRY (60*1000) /* persistent retry interval */
103 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
104 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
105 
106 #ifdef PPPOE_SERVER
107 #error "PPPOE_SERVER is not yet supported under lwIP!"
108 /* from if_spppsubr.c */
109 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
110 #endif
111 
112 #define PPPOE_ERRORSTRING_LEN 64
113 
114 
115 /* callbacks called from PPP core */
116 static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p);
117 static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol);
118 static err_t pppoe_connect(ppp_pcb *ppp, void *ctx);
119 static void pppoe_disconnect(ppp_pcb *ppp, void *ctx);
120 static err_t pppoe_destroy(ppp_pcb *ppp, void *ctx);
121 
122 /* management routines */
123 static void pppoe_abort_connect(struct pppoe_softc *);
124 #if 0 /* UNUSED */
125 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
126 #endif /* UNUSED */
127 
128 /* internal timeout handling */
129 static void pppoe_timeout(void *);
130 
131 /* sending actual protocol controll packets */
132 static err_t pppoe_send_padi(struct pppoe_softc *);
133 static err_t pppoe_send_padr(struct pppoe_softc *);
134 #ifdef PPPOE_SERVER
135 static err_t pppoe_send_pado(struct pppoe_softc *);
136 static err_t pppoe_send_pads(struct pppoe_softc *);
137 #endif
138 static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);
139 
140 /* internal helper functions */
141 static err_t pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb);
142 static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif);
143 static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif);
144 
146 static struct pppoe_softc *pppoe_softc_list;
147 
148 /* Callbacks structure for PPP core */
149 static const struct link_callbacks pppoe_callbacks = {
150  pppoe_connect,
151 #if PPP_SERVER
152  NULL,
153 #endif /* PPP_SERVER */
154  pppoe_disconnect,
155  pppoe_destroy,
156  pppoe_write,
157  pppoe_netif_output,
158  NULL,
159  NULL
160 };
161 
162 /*
163  * Create a new PPP Over Ethernet (PPPoE) connection.
164  *
165  * Return 0 on success, an error code on failure.
166  */
167 ppp_pcb *pppoe_create(struct netif *pppif,
168  struct netif *ethif,
169  const char *service_name, const char *concentrator_name,
170  ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
171 {
172  ppp_pcb *ppp;
173  struct pppoe_softc *sc;
174  LWIP_UNUSED_ARG(service_name);
175  LWIP_UNUSED_ARG(concentrator_name);
176 
177  sc = (struct pppoe_softc *)memp_malloc(MEMP_PPPOE_IF);
178  if (sc == NULL) {
179  return NULL;
180  }
181 
182  ppp = ppp_new(pppif, &pppoe_callbacks, sc, link_status_cb, ctx_cb);
183  if (ppp == NULL) {
184  memp_free(MEMP_PPPOE_IF, sc);
185  return NULL;
186  }
187 
188  memset(sc, 0, sizeof(struct pppoe_softc));
189  /* changed to real address later */
190  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
191  sc->pcb = ppp;
192  sc->sc_ethif = ethif;
193  /* put the new interface at the head of the list */
194  sc->next = pppoe_softc_list;
195  pppoe_softc_list = sc;
196  return ppp;
197 }
198 
199 /* Called by PPP core */
200 static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) {
201  struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
202  struct pbuf *ph; /* Ethernet + PPPoE header */
203  err_t ret;
204 #if MIB2_STATS
205  u16_t tot_len;
206 #else /* MIB2_STATS */
207  LWIP_UNUSED_ARG(ppp);
208 #endif /* MIB2_STATS */
209 
210  /* skip address & flags */
211  pbuf_header(p, -(s16_t)2);
212 
213  ph = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
214  if(!ph) {
215  LINK_STATS_INC(link.memerr);
216  LINK_STATS_INC(link.proterr);
217  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
218  pbuf_free(p);
219  return ERR_MEM;
220  }
221 
222  pbuf_header(ph, -(s16_t)PPPOE_HEADERLEN); /* hide PPPoE header */
223  pbuf_cat(ph, p);
224 #if MIB2_STATS
225  tot_len = ph->tot_len;
226 #endif /* MIB2_STATS */
227 
228  ret = pppoe_xmit(sc, ph);
229  if (ret != ERR_OK) {
230  LINK_STATS_INC(link.err);
231  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
232  return ret;
233  }
234 
235  MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len);
236  MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
237  LINK_STATS_INC(link.xmit);
238  return ERR_OK;
239 }
240 
241 /* Called by PPP core */
242 static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol) {
243  struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
244  struct pbuf *pb;
245  u8_t *pl;
246  err_t err;
247 #if MIB2_STATS
248  u16_t tot_len;
249 #else /* MIB2_STATS */
250  LWIP_UNUSED_ARG(ppp);
251 #endif /* MIB2_STATS */
252 
253  /* @todo: try to use pbuf_header() here! */
254  pb = pbuf_alloc(PBUF_LINK, PPPOE_HEADERLEN + sizeof(protocol), PBUF_RAM);
255  if(!pb) {
256  LINK_STATS_INC(link.memerr);
257  LINK_STATS_INC(link.proterr);
258  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
259  return ERR_MEM;
260  }
261 
262  pbuf_header(pb, -(s16_t)PPPOE_HEADERLEN);
263 
264  pl = (u8_t*)pb->payload;
265  PUTSHORT(protocol, pl);
266 
267  pbuf_chain(pb, p);
268 #if MIB2_STATS
269  tot_len = pb->tot_len;
270 #endif /* MIB2_STATS */
271 
272  if( (err = pppoe_xmit(sc, pb)) != ERR_OK) {
273  LINK_STATS_INC(link.err);
274  MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
275  return err;
276  }
277 
278  MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
279  MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
280  LINK_STATS_INC(link.xmit);
281  return ERR_OK;
282 }
283 
284 static err_t
285 pppoe_destroy(ppp_pcb *ppp, void *ctx)
286 {
287  struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
288  struct pppoe_softc **copp, *freep;
289  LWIP_UNUSED_ARG(ppp);
290 
291  sys_untimeout(pppoe_timeout, sc);
292 
293  /* remove interface from list */
294  for (copp = &pppoe_softc_list; (freep = *copp); copp = &freep->next) {
295  if (freep == sc) {
296  *copp = freep->next;
297  break;
298  }
299  }
300 
301 #ifdef PPPOE_TODO
302  if (sc->sc_concentrator_name) {
303  mem_free(sc->sc_concentrator_name);
304  }
305  if (sc->sc_service_name) {
306  mem_free(sc->sc_service_name);
307  }
308 #endif /* PPPOE_TODO */
309  memp_free(MEMP_PPPOE_IF, sc);
310 
311  return ERR_OK;
312 }
313 
314 /*
315  * Find the interface handling the specified session.
316  * Note: O(number of sessions open), this is a client-side only, mean
317  * and lean implementation, so number of open sessions typically should
318  * be 1.
319  */
320 static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif) {
321  struct pppoe_softc *sc;
322 
323  if (session == 0) {
324  return NULL;
325  }
326 
327  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
328  if (sc->sc_state == PPPOE_STATE_SESSION
329  && sc->sc_session == session
330  && sc->sc_ethif == rcvif) {
331  return sc;
332  }
333  }
334  return NULL;
335 }
336 
337 /* Check host unique token passed and return appropriate softc pointer,
338  * or NULL if token is bogus. */
339 static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif) {
340  struct pppoe_softc *sc, *t;
341 
342  if (pppoe_softc_list == NULL) {
343  return NULL;
344  }
345 
346  if (len != sizeof sc) {
347  return NULL;
348  }
349  MEMCPY(&t, token, len);
350 
351  for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
352  if (sc == t) {
353  break;
354  }
355  }
356 
357  if (sc == NULL) {
358  PPPDEBUG(LOG_DEBUG, ("pppoe: alien host unique tag, no session found\n"));
359  return NULL;
360  }
361 
362  /* should be safe to access *sc now */
363  if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
364  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
365  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state));
366  return NULL;
367  }
368  if (sc->sc_ethif != rcvif) {
369  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": wrong interface, not accepting host unique\n",
370  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
371  return NULL;
372  }
373  return sc;
374 }
375 
376 /* analyze and handle a single received packet while not in session state */
377 void
378 pppoe_disc_input(struct netif *netif, struct pbuf *pb)
379 {
380  u16_t tag, len;
381  u16_t session, plen;
382  struct pppoe_softc *sc;
383 #if PPP_DEBUG
384  const char *err_msg = NULL;
385 #endif /* PPP_DEBUG */
386  u8_t *ac_cookie;
387  u16_t ac_cookie_len;
388 #ifdef PPPOE_SERVER
389  u8_t *hunique;
390  size_t hunique_len;
391 #endif
392  struct pppoehdr *ph;
393  struct pppoetag pt;
394  int off, err;
395  struct eth_hdr *ethhdr;
396 
397  /* don't do anything if there is not a single PPPoE instance */
398  if (pppoe_softc_list == NULL) {
399  pbuf_free(pb);
400  return;
401  }
402 
403  pb = ppp_singlebuf(pb);
404 
405  if (pb->len < sizeof(*ethhdr)) {
406  goto done;
407  }
408  ethhdr = (struct eth_hdr *)pb->payload;
409  off = sizeof(*ethhdr);
410 
411  ac_cookie = NULL;
412  ac_cookie_len = 0;
413 #ifdef PPPOE_SERVER
414  hunique = NULL;
415  hunique_len = 0;
416 #endif
417  session = 0;
418  if (pb->len - off < (u16_t)PPPOE_HEADERLEN) {
419  PPPDEBUG(LOG_DEBUG, ("pppoe: packet too short: %d\n", pb->len));
420  goto done;
421  }
422 
423  ph = (struct pppoehdr *) (ethhdr + 1);
424  if (ph->vertype != PPPOE_VERTYPE) {
425  PPPDEBUG(LOG_DEBUG, ("pppoe: unknown version/type packet: 0x%x\n", ph->vertype));
426  goto done;
427  }
428  session = ntohs(ph->session);
429  plen = ntohs(ph->plen);
430  off += sizeof(*ph);
431 
432  if (plen + off > pb->len) {
433  PPPDEBUG(LOG_DEBUG, ("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
434  pb->len - off, plen));
435  goto done;
436  }
437  if(pb->tot_len == pb->len) {
438  pb->tot_len = pb->len = (u16_t)off + plen; /* ignore trailing garbage */
439  }
440  tag = 0;
441  len = 0;
442  sc = NULL;
443  while (off + sizeof(pt) <= pb->len) {
444  MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));
445  tag = ntohs(pt.tag);
446  len = ntohs(pt.len);
447  if (off + sizeof(pt) + len > pb->len) {
448  PPPDEBUG(LOG_DEBUG, ("pppoe: tag 0x%x len 0x%x is too long\n", tag, len));
449  goto done;
450  }
451  switch (tag) {
452  case PPPOE_TAG_EOL:
453  goto breakbreak;
454  case PPPOE_TAG_SNAME:
455  break; /* ignored */
456  case PPPOE_TAG_ACNAME:
457  break; /* ignored */
458  case PPPOE_TAG_HUNIQUE:
459  if (sc != NULL) {
460  break;
461  }
462 #ifdef PPPOE_SERVER
463  hunique = (u8_t*)pb->payload + off + sizeof(pt);
464  hunique_len = len;
465 #endif
466  sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);
467  break;
468  case PPPOE_TAG_ACCOOKIE:
469  if (ac_cookie == NULL) {
470  ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);
471  ac_cookie_len = len;
472  }
473  break;
474 #if PPP_DEBUG
475  case PPPOE_TAG_SNAME_ERR:
476  err_msg = "SERVICE NAME ERROR";
477  break;
478  case PPPOE_TAG_ACSYS_ERR:
479  err_msg = "AC SYSTEM ERROR";
480  break;
481  case PPPOE_TAG_GENERIC_ERR:
482  err_msg = "GENERIC ERROR";
483  break;
484 #endif /* PPP_DEBUG */
485  default:
486  break;
487  }
488 #if PPP_DEBUG
489  if (err_msg != NULL) {
490  char error_tmp[PPPOE_ERRORSTRING_LEN];
491  u16_t error_len = LWIP_MIN(len, sizeof(error_tmp)-1);
492  strncpy(error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
493  error_tmp[error_len] = '\0';
494  if (sc) {
495  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": %s: %s\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err_msg, error_tmp));
496  } else {
497  PPPDEBUG(LOG_DEBUG, ("pppoe: %s: %s\n", err_msg, error_tmp));
498  }
499  }
500 #endif /* PPP_DEBUG */
501  off += sizeof(pt) + len;
502  }
503 
504 breakbreak:;
505  switch (ph->code) {
506  case PPPOE_CODE_PADI:
507 #ifdef PPPOE_SERVER
508  /*
509  * got service name, concentrator name, and/or host unique.
510  * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
511  */
512  if (LIST_EMPTY(&pppoe_softc_list)) {
513  goto done;
514  }
515  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
516  if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
517  continue;
518  }
519  if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
520  continue;
521  }
522  if (sc->sc_state == PPPOE_STATE_INITIAL) {
523  break;
524  }
525  }
526  if (sc == NULL) {
527  /* PPPDEBUG(LOG_DEBUG, ("pppoe: free passive interface is not found\n")); */
528  goto done;
529  }
530  if (hunique) {
531  if (sc->sc_hunique) {
532  mem_free(sc->sc_hunique);
533  }
534  sc->sc_hunique = mem_malloc(hunique_len);
535  if (sc->sc_hunique == NULL) {
536  goto done;
537  }
538  sc->sc_hunique_len = hunique_len;
539  MEMCPY(sc->sc_hunique, hunique, hunique_len);
540  }
541  MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
542  sc->sc_state = PPPOE_STATE_PADO_SENT;
543  pppoe_send_pado(sc);
544  break;
545 #endif /* PPPOE_SERVER */
546  case PPPOE_CODE_PADR:
547 #ifdef PPPOE_SERVER
548  /*
549  * get sc from ac_cookie if IFF_PASSIVE
550  */
551  if (ac_cookie == NULL) {
552  /* be quiet if there is not a single pppoe instance */
553  PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but not includes ac_cookie\n"));
554  goto done;
555  }
556  sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
557  if (sc == NULL) {
558  /* be quiet if there is not a single pppoe instance */
559  if (!LIST_EMPTY(&pppoe_softc_list)) {
560  PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but could not find request for it\n"));
561  }
562  goto done;
563  }
564  if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
565  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
566  goto done;
567  }
568  if (hunique) {
569  if (sc->sc_hunique) {
570  mem_free(sc->sc_hunique);
571  }
572  sc->sc_hunique = mem_malloc(hunique_len);
573  if (sc->sc_hunique == NULL) {
574  goto done;
575  }
576  sc->sc_hunique_len = hunique_len;
577  MEMCPY(sc->sc_hunique, hunique, hunique_len);
578  }
579  pppoe_send_pads(sc);
580  sc->sc_state = PPPOE_STATE_SESSION;
581  ppp_start(sc->pcb); /* notify upper layers */
582  break;
583 #else
584  /* ignore, we are no access concentrator */
585  goto done;
586 #endif /* PPPOE_SERVER */
587  case PPPOE_CODE_PADO:
588  if (sc == NULL) {
589  /* be quiet if there is not a single pppoe instance */
590  if (pppoe_softc_list != NULL) {
591  PPPDEBUG(LOG_DEBUG, ("pppoe: received PADO but could not find request for it\n"));
592  }
593  goto done;
594  }
595  if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
596  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
597  goto done;
598  }
599  if (ac_cookie) {
600  sc->sc_ac_cookie_len = ac_cookie_len;
601  MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
602  }
603  MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));
604  sys_untimeout(pppoe_timeout, sc);
605  sc->sc_padr_retried = 0;
606  sc->sc_state = PPPOE_STATE_PADR_SENT;
607  if ((err = pppoe_send_padr(sc)) != 0) {
608  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
609  }
610  sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
611  break;
612  case PPPOE_CODE_PADS:
613  if (sc == NULL) {
614  goto done;
615  }
616  sc->sc_session = session;
617  sys_untimeout(pppoe_timeout, sc);
618  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": session 0x%x connected\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, session));
619  sc->sc_state = PPPOE_STATE_SESSION;
620  ppp_start(sc->pcb); /* notify upper layers */
621  break;
622  case PPPOE_CODE_PADT:
623  /* Don't disconnect here, we let the LCP Echo/Reply find the fact
624  * that PPP session is down. Asking the PPP stack to end the session
625  * require strict checking about the PPP phase to prevent endless
626  * disconnection loops.
627  */
628 #if 0 /* UNUSED */
629  if (sc == NULL) { /* PADT frames are rarely sent with a hunique tag, this is actually almost always true */
630  goto done;
631  }
632  pppoe_clear_softc(sc, "received PADT");
633 #endif /* UNUSED */
634  break;
635  default:
636  if(sc) {
637  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
638  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
639  (u16_t)ph->code, session));
640  } else {
641  PPPDEBUG(LOG_DEBUG, ("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session));
642  }
643  break;
644  }
645 
646 done:
647  pbuf_free(pb);
648  return;
649 }
650 
651 void
652 pppoe_data_input(struct netif *netif, struct pbuf *pb)
653 {
654  u16_t session, plen;
655  struct pppoe_softc *sc;
656  struct pppoehdr *ph;
657 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
658  u8_t shost[ETHER_ADDR_LEN];
659 #endif
660 
661 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
662  MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));
663 #endif
664  if (pbuf_header(pb, -(s16_t)sizeof(struct eth_hdr)) != 0) {
665  /* bail out */
666  PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header failed\n"));
667  LINK_STATS_INC(link.lenerr);
668  goto drop;
669  }
670 
671  if (pb->len < sizeof(*ph)) {
672  PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: could not get PPPoE header\n"));
673  goto drop;
674  }
675  ph = (struct pppoehdr *)pb->payload;
676 
677  if (ph->vertype != PPPOE_VERTYPE) {
678  PPPDEBUG(LOG_DEBUG, ("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype));
679  goto drop;
680  }
681  if (ph->code != 0) {
682  goto drop;
683  }
684 
685  session = ntohs(ph->session);
686  sc = pppoe_find_softc_by_session(session, netif);
687  if (sc == NULL) {
688 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
689  PPPDEBUG(LOG_DEBUG, ("pppoe: input for unknown session 0x%x, sending PADT\n", session));
690  pppoe_send_padt(netif, session, shost);
691 #endif
692  goto drop;
693  }
694 
695  plen = ntohs(ph->plen);
696 
697  if (pbuf_header(pb, -(s16_t)(PPPOE_HEADERLEN)) != 0) {
698  /* bail out */
699  PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));
700  LINK_STATS_INC(link.lenerr);
701  goto drop;
702  }
703 
704  PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",
705  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
706  pb->len, plen));
707 
708  if (pb->tot_len < plen) {
709  goto drop;
710  }
711 
712  /* Dispatch the packet thereby consuming it. */
713  ppp_input(sc->pcb, pb);
714  return;
715 
716 drop:
717  pbuf_free(pb);
718 }
719 
720 static err_t
721 pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
722 {
723  struct eth_hdr *ethhdr;
724  u16_t etype;
725  err_t res;
726 
727  if (!sc->sc_ethif) {
728  pbuf_free(pb);
729  return ERR_IF;
730  }
731 
732  /* make room for Ethernet header - should not fail */
733  if (pbuf_header(pb, (s16_t)(sizeof(struct eth_hdr))) != 0) {
734  /* bail out */
735  PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_output: could not allocate room for Ethernet header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
736  LINK_STATS_INC(link.lenerr);
737  pbuf_free(pb);
738  return ERR_BUF;
739  }
740  ethhdr = (struct eth_hdr *)pb->payload;
741  etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
742  ethhdr->type = htons(etype);
743  MEMCPY(&ethhdr->dest.addr, &sc->sc_dest.addr, sizeof(ethhdr->dest.addr));
744  MEMCPY(&ethhdr->src.addr, &sc->sc_ethif->hwaddr, sizeof(ethhdr->src.addr));
745 
746  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F" (%x) state=%d, session=0x%x output -> %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F", len=%d\n",
747  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,
748  sc->sc_state, sc->sc_session,
749  sc->sc_dest.addr[0], sc->sc_dest.addr[1], sc->sc_dest.addr[2], sc->sc_dest.addr[3], sc->sc_dest.addr[4], sc->sc_dest.addr[5],
750  pb->tot_len));
751 
752  res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);
753 
754  pbuf_free(pb);
755 
756  return res;
757 }
758 
759 static err_t
760 pppoe_send_padi(struct pppoe_softc *sc)
761 {
762  struct pbuf *pb;
763  u8_t *p;
764  int len;
765 #ifdef PPPOE_TODO
766  int l1 = 0, l2 = 0; /* XXX: gcc */
767 #endif /* PPPOE_TODO */
768 
769  if (sc->sc_state >PPPOE_STATE_PADI_SENT) {
770  PPPDEBUG(LOG_ERR, ("ERROR: pppoe_send_padi in state %d", sc->sc_state));
771  }
772 
773  /* calculate length of frame (excluding ethernet header + pppoe header) */
774  len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
775 #ifdef PPPOE_TODO
776  if (sc->sc_service_name != NULL) {
777  l1 = (int)strlen(sc->sc_service_name);
778  len += l1;
779  }
780  if (sc->sc_concentrator_name != NULL) {
781  l2 = (int)strlen(sc->sc_concentrator_name);
782  len += 2 + 2 + l2;
783  }
784 #endif /* PPPOE_TODO */
785  LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
786  sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
787 
788  /* allocate a buffer */
789  pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
790  if (!pb) {
791  return ERR_MEM;
792  }
793  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
794 
795  p = (u8_t*)pb->payload;
796  /* fill in pkt */
797  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
798  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
799 #ifdef PPPOE_TODO
800  if (sc->sc_service_name != NULL) {
801  PPPOE_ADD_16(p, l1);
802  MEMCPY(p, sc->sc_service_name, l1);
803  p += l1;
804  } else
805 #endif /* PPPOE_TODO */
806  {
807  PPPOE_ADD_16(p, 0);
808  }
809 #ifdef PPPOE_TODO
810  if (sc->sc_concentrator_name != NULL) {
811  PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
812  PPPOE_ADD_16(p, l2);
813  MEMCPY(p, sc->sc_concentrator_name, l2);
814  p += l2;
815  }
816 #endif /* PPPOE_TODO */
817  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
818  PPPOE_ADD_16(p, sizeof(sc));
819  MEMCPY(p, &sc, sizeof sc);
820 
821  /* send pkt */
822  return pppoe_output(sc, pb);
823 }
824 
825 static void
826 pppoe_timeout(void *arg)
827 {
828  u32_t retry_wait;
829  int err;
830  struct pppoe_softc *sc = (struct pppoe_softc*)arg;
831 
832  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
833 
834  switch (sc->sc_state) {
835  case PPPOE_STATE_PADI_SENT:
836  /*
837  * We have two basic ways of retrying:
838  * - Quick retry mode: try a few times in short sequence
839  * - Slow retry mode: we already had a connection successfully
840  * established and will try infinitely (without user
841  * intervention)
842  * We only enter slow retry mode if IFF_LINK1 (aka autodial)
843  * is not set.
844  */
845  if (sc->sc_padi_retried < 0xff) {
846  sc->sc_padi_retried++;
847  }
848  if (!sc->pcb->settings.persist && sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
849 #if 0
850  if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
851  /* slow retry mode */
852  retry_wait = PPPOE_SLOW_RETRY;
853  } else
854 #endif
855  {
856  pppoe_abort_connect(sc);
857  return;
858  }
859  }
860  /* initialize for quick retry mode */
861  retry_wait = LWIP_MIN(PPPOE_DISC_TIMEOUT * sc->sc_padi_retried, PPPOE_SLOW_RETRY);
862  if ((err = pppoe_send_padi(sc)) != 0) {
863  sc->sc_padi_retried--;
864  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to transmit PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
865  }
866  sys_timeout(retry_wait, pppoe_timeout, sc);
867  break;
868 
869  case PPPOE_STATE_PADR_SENT:
870  sc->sc_padr_retried++;
871  if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
872  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
873  sc->sc_state = PPPOE_STATE_PADI_SENT;
874  sc->sc_padr_retried = 0;
875  if ((err = pppoe_send_padi(sc)) != 0) {
876  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
877  }
878  sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);
879  return;
880  }
881  if ((err = pppoe_send_padr(sc)) != 0) {
882  sc->sc_padr_retried--;
883  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADR, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
884  }
885  sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
886  break;
887  default:
888  return; /* all done, work in peace */
889  }
890 }
891 
892 /* Start a connection (i.e. initiate discovery phase) */
893 static err_t
894 pppoe_connect(ppp_pcb *ppp, void *ctx)
895 {
896  int err;
897  struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
898  lcp_options *lcp_wo;
899  lcp_options *lcp_ao;
900 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
901  ipcp_options *ipcp_wo;
902  ipcp_options *ipcp_ao;
903 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
904 
905  if (sc->sc_state != PPPOE_STATE_INITIAL) {
906 #ifdef LWIP_PROVIDE_ERRNO
907  return EBUSY;
908 #else
909  return 16;
910 #endif
911  }
912 
913  /* stop any timer */
914  sys_untimeout(pppoe_timeout, sc);
915  sc->sc_session = 0;
916  sc->sc_padi_retried = 0;
917  sc->sc_padr_retried = 0;
918 #ifdef PPPOE_SERVER
919  /* wait PADI if IFF_PASSIVE */
920  if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
921  return 0;
922  }
923 #endif
924 
925  ppp_clear(ppp);
926 
927  lcp_wo = &ppp->lcp_wantoptions;
928  lcp_wo->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2; /* two byte PPP protocol discriminator, then IP data */
929  lcp_wo->neg_asyncmap = 0;
930  lcp_wo->neg_pcompression = 0;
931  lcp_wo->neg_accompression = 0;
932 
933  lcp_ao = &ppp->lcp_allowoptions;
934  lcp_ao->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2; /* two byte PPP protocol discriminator, then IP data */
935  lcp_ao->neg_asyncmap = 0;
936  lcp_ao->neg_pcompression = 0;
937  lcp_ao->neg_accompression = 0;
938 
939 #if PPP_IPV4_SUPPORT && VJ_SUPPORT
940  ipcp_wo = &ppp->ipcp_wantoptions;
941  ipcp_wo->neg_vj = 0;
942  ipcp_wo->old_vj = 0;
943 
944  ipcp_ao = &ppp->ipcp_allowoptions;
945  ipcp_ao->neg_vj = 0;
946  ipcp_ao->old_vj = 0;
947 #endif /* PPP_IPV4_SUPPORT && VJ_SUPPORT */
948 
949  /* save state, in case we fail to send PADI */
950  sc->sc_state = PPPOE_STATE_PADI_SENT;
951  if ((err = pppoe_send_padi(sc)) != 0) {
952  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": failed to send PADI, error=%d\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, err));
953  }
954  sys_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
955  return err;
956 }
957 
958 /* disconnect */
959 static void
960 pppoe_disconnect(ppp_pcb *ppp, void *ctx)
961 {
962  struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
963 
964  if (sc->sc_state < PPPOE_STATE_SESSION) {
965  return;
966  }
967 
968  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
969  pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);
970 
971  /* cleanup softc */
972  sc->sc_state = PPPOE_STATE_INITIAL;
973  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
974  sc->sc_ac_cookie_len = 0;
975 #ifdef PPPOE_SERVER
976  if (sc->sc_hunique) {
977  mem_free(sc->sc_hunique);
978  sc->sc_hunique = NULL;
979  }
980  sc->sc_hunique_len = 0;
981 #endif
982  sc->sc_session = 0;
983  sc->sc_padi_retried = 0;
984  sc->sc_padr_retried = 0;
985 
986  ppp_link_end(ppp); /* notify upper layers */
987  return;
988 }
989 
990 /* Connection attempt aborted */
991 static void
992 pppoe_abort_connect(struct pppoe_softc *sc)
993 {
994  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
995 
996  /* clear connection state */
997  sc->sc_state = PPPOE_STATE_INITIAL;
998  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
999  sc->sc_ac_cookie_len = 0;
1000  sc->sc_session = 0;
1001  sc->sc_padi_retried = 0;
1002  sc->sc_padr_retried = 0;
1003 
1004  ppp_link_failed(sc->pcb); /* notify upper layers */
1005 }
1006 
1007 /* Send a PADR packet */
1008 static err_t
1009 pppoe_send_padr(struct pppoe_softc *sc)
1010 {
1011  struct pbuf *pb;
1012  u8_t *p;
1013  size_t len;
1014 #ifdef PPPOE_TODO
1015  size_t l1 = 0; /* XXX: gcc */
1016 #endif /* PPPOE_TODO */
1017 
1018  if (sc->sc_state != PPPOE_STATE_PADR_SENT) {
1019  return ERR_CONN;
1020  }
1021 
1022  len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
1023 #ifdef PPPOE_TODO
1024  if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1025  l1 = strlen(sc->sc_service_name);
1026  len += l1;
1027  }
1028 #endif /* PPPOE_TODO */
1029  if (sc->sc_ac_cookie_len > 0) {
1030  len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
1031  }
1032  LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
1033  sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
1034  pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1035  if (!pb) {
1036  return ERR_MEM;
1037  }
1038  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1039  p = (u8_t*)pb->payload;
1040  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
1041  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1042 #ifdef PPPOE_TODO
1043  if (sc->sc_service_name != NULL) {
1044  PPPOE_ADD_16(p, l1);
1045  MEMCPY(p, sc->sc_service_name, l1);
1046  p += l1;
1047  } else
1048 #endif /* PPPOE_TODO */
1049  {
1050  PPPOE_ADD_16(p, 0);
1051  }
1052  if (sc->sc_ac_cookie_len > 0) {
1053  PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1054  PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
1055  MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
1056  p += sc->sc_ac_cookie_len;
1057  }
1058  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1059  PPPOE_ADD_16(p, sizeof(sc));
1060  MEMCPY(p, &sc, sizeof sc);
1061 
1062  return pppoe_output(sc, pb);
1063 }
1064 
1065 /* send a PADT packet */
1066 static err_t
1067 pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
1068 {
1069  struct pbuf *pb;
1070  struct eth_hdr *ethhdr;
1071  err_t res;
1072  u8_t *p;
1073 
1074  pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
1075  if (!pb) {
1076  return ERR_MEM;
1077  }
1078  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1079 
1080  pbuf_header(pb, (s16_t)sizeof(struct eth_hdr));
1081  ethhdr = (struct eth_hdr *)pb->payload;
1082  ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
1083  MEMCPY(&ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
1084  MEMCPY(&ethhdr->src.addr, &outgoing_if->hwaddr, sizeof(ethhdr->src.addr));
1085 
1086  p = (u8_t*)(ethhdr + 1);
1087  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
1088 
1089  res = outgoing_if->linkoutput(outgoing_if, pb);
1090 
1091  pbuf_free(pb);
1092 
1093  return res;
1094 }
1095 
1096 #ifdef PPPOE_SERVER
1097 static err_t
1098 pppoe_send_pado(struct pppoe_softc *sc)
1099 {
1100  struct pbuf *pb;
1101  u8_t *p;
1102  size_t len;
1103 
1104  if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
1105  return ERR_CONN;
1106  }
1107 
1108  /* calc length */
1109  len = 0;
1110  /* include ac_cookie */
1111  len += 2 + 2 + sizeof(sc);
1112  /* include hunique */
1113  len += 2 + 2 + sc->sc_hunique_len;
1114  pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1115  if (!pb) {
1116  return ERR_MEM;
1117  }
1118  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1119  p = (u8_t*)pb->payload;
1120  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
1121  PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
1122  PPPOE_ADD_16(p, sizeof(sc));
1123  MEMCPY(p, &sc, sizeof(sc));
1124  p += sizeof(sc);
1125  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1126  PPPOE_ADD_16(p, sc->sc_hunique_len);
1127  MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1128  return pppoe_output(sc, pb);
1129 }
1130 
1131 static err_t
1132 pppoe_send_pads(struct pppoe_softc *sc)
1133 {
1134  struct pbuf *pb;
1135  u8_t *p;
1136  size_t len, l1 = 0; /* XXX: gcc */
1137 
1138  if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
1139  return ERR_CONN;
1140  }
1141 
1142  sc->sc_session = mono_time.tv_sec % 0xff + 1;
1143  /* calc length */
1144  len = 0;
1145  /* include hunique */
1146  len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
1147  if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
1148  l1 = strlen(sc->sc_service_name);
1149  len += l1;
1150  }
1151  pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
1152  if (!pb) {
1153  return ERR_MEM;
1154  }
1155  LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
1156  p = (u8_t*)pb->payload;
1157  PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
1158  PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
1159  if (sc->sc_service_name != NULL) {
1160  PPPOE_ADD_16(p, l1);
1161  MEMCPY(p, sc->sc_service_name, l1);
1162  p += l1;
1163  } else {
1164  PPPOE_ADD_16(p, 0);
1165  }
1166  PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
1167  PPPOE_ADD_16(p, sc->sc_hunique_len);
1168  MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
1169  return pppoe_output(sc, pb);
1170 }
1171 #endif
1172 
1173 static err_t
1174 pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
1175 {
1176  u8_t *p;
1177  size_t len;
1178 
1179  /* are we ready to process data yet? */
1180  if (sc->sc_state < PPPOE_STATE_SESSION) {
1181  /*sppp_flush(&sc->sc_sppp.pp_if);*/
1182  pbuf_free(pb);
1183  return ERR_CONN;
1184  }
1185 
1186  len = pb->tot_len;
1187 
1188  /* make room for PPPoE header - should not fail */
1189  if (pbuf_header(pb, (s16_t)(PPPOE_HEADERLEN)) != 0) {
1190  /* bail out */
1191  PPPDEBUG(LOG_ERR, ("pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for PPPoE header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
1192  LINK_STATS_INC(link.lenerr);
1193  pbuf_free(pb);
1194  return ERR_BUF;
1195  }
1196 
1197  p = (u8_t*)pb->payload;
1198  PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
1199 
1200  return pppoe_output(sc, pb);
1201 }
1202 
1203 #if 0 /*def PFIL_HOOKS*/
1204 static int
1205 pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)
1206 {
1207  struct pppoe_softc *sc;
1208  int s;
1209 
1210  if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {
1211  return 0;
1212  }
1213 
1214  LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
1215  if (sc->sc_ethif != ifp) {
1216  continue;
1217  }
1218  if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
1219  sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1220  PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": ethernet interface detached, going down\n",
1221  sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
1222  }
1223  sc->sc_ethif = NULL;
1224  pppoe_clear_softc(sc, "ethernet interface detached");
1225  }
1226 
1227  return 0;
1228 }
1229 #endif
1230 
1231 #if 0 /* UNUSED */
1232 static void
1233 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
1234 {
1235  LWIP_UNUSED_ARG(message);
1236 
1237  /* stop timer */
1238  sys_untimeout(pppoe_timeout, sc);
1239  PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": session 0x%x terminated, %s\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_session, message));
1240  /* fix our state */
1241  sc->sc_state = PPPOE_STATE_INITIAL;
1242 
1243  /* notify upper layers */
1244  ppp_link_end(sc->pcb); /* /!\ dangerous /!\ */
1245 
1246  /* clean up softc */
1247  MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
1248  sc->sc_ac_cookie_len = 0;
1249  sc->sc_session = 0;
1250  sc->sc_padi_retried = 0;
1251  sc->sc_padr_retried = 0;
1252 }
1253 #endif /* UNUSED */
1254 #endif /* PPP_SUPPORT && PPPOE_SUPPORT */
1255 
#define ERR_CONN
Definition: err.h:64
void mem_free(void *rmem)
Definition: mem.c:334
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:267
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
struct netif * next
Definition: netif.h:184
#define htons(x)
Definition: def.h:86
#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
Definition: pbuf.h:66
netif_linkoutput_fn linkoutput
Definition: netif.h:211
#define LWIP_MIN(x, y)
Definition: def.h:50
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define PP_HTONS(x)
Definition: def.h:97
unsigned long u32_t
Definition: cc.h:42
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:820
#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 LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
unsigned char u8_t
Definition: cc.h:38
void * mem_malloc(mem_size_t size)
Definition: mem.c:517
#define MIB2_STATS_NETIF_INC(n, x)
Definition: snmp.h:121
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
void sys_untimeout(sys_timeout_handler handler, void *arg)
#define ntohs(x)
Definition: def.h:87
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define X16_F
Definition: cc.h:50
#define ERR_BUF
Definition: err.h:54