source: S-port/trunk/Middlewares/Third_Party/LwIP/src/netif/ppp/pppoe.c

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