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