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