1 /*****************************************************************************
\r
2 * ppp_oe.c - PPP Over Ethernet implementation for lwIP.
\r
4 * Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.
\r
6 * The authors hereby grant permission to use, copy, modify, distribute,
\r
7 * and license this software and its documentation for any purpose, provided
\r
8 * that existing copyright notices are retained in all copies and that this
\r
9 * notice and the following disclaimer are included verbatim in any
\r
10 * distributions. No written agreement, license, or royalty fee is required
\r
11 * for any of the authorized uses.
\r
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
\r
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
16 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
24 ******************************************************************************
\r
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>
\r
29 *****************************************************************************/
\r
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */
\r
36 * Copyright (c) 2002 The NetBSD Foundation, Inc.
\r
37 * All rights reserved.
\r
39 * This code is derived from software contributed to The NetBSD Foundation
\r
40 * by Martin Husemann <martin@NetBSD.org>.
\r
42 * Redistribution and use in source and binary forms, with or without
\r
43 * modification, are permitted provided that the following conditions
\r
45 * 1. Redistributions of source code must retain the above copyright
\r
46 * notice, this list of conditions and the following disclaimer.
\r
47 * 2. Redistributions in binary form must reproduce the above copyright
\r
48 * notice, this list of conditions and the following disclaimer in the
\r
49 * documentation and/or other materials provided with the distribution.
\r
50 * 3. All advertising materials mentioning features or use of this software
\r
51 * must display the following acknowledgement:
\r
52 * This product includes software developed by the NetBSD
\r
53 * Foundation, Inc. and its contributors.
\r
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
\r
55 * contributors may be used to endorse or promote products derived
\r
56 * from this software without specific prior written permission.
\r
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
\r
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
\r
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
\r
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
68 * POSSIBILITY OF SUCH DAMAGE.
\r
71 #include "lwip/opt.h"
\r
73 #if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
\r
76 #include "pppdebug.h"
\r
78 #include "lwip/sys.h"
\r
80 #include "netif/ppp_oe.h"
\r
81 #include "netif/etharp.h"
\r
86 /** @todo Replace this part with a simple list like other lwIP lists */
\r
87 #ifndef _SYS_QUEUE_H_
\r
88 #define _SYS_QUEUE_H_
\r
91 * A list is headed by a single forward pointer (or an array of forward
\r
92 * pointers for a hash table header). The elements are doubly linked
\r
93 * so that an arbitrary element can be removed without a need to
\r
94 * traverse the list. New elements can be added to the list before
\r
95 * or after an existing element or at the head of the list. A list
\r
96 * may only be traversed in the forward direction.
\r
98 * For details on the use of these macros, see the queue(3) manual page.
\r
102 * List declarations.
\r
104 #define LIST_HEAD(name, type) \
\r
106 struct type *lh_first; /* first element */ \
\r
109 #define LIST_HEAD_INITIALIZER(head) \
\r
112 #define LIST_ENTRY(type) \
\r
114 struct type *le_next; /* next element */ \
\r
115 struct type **le_prev; /* address of previous next element */ \
\r
122 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
\r
124 #define LIST_FIRST(head) ((head)->lh_first)
\r
126 #define LIST_FOREACH(var, head, field) \
\r
127 for ((var) = LIST_FIRST((head)); \
\r
129 (var) = LIST_NEXT((var), field))
\r
131 #define LIST_INIT(head) do { \
\r
132 LIST_FIRST((head)) = NULL; \
\r
135 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
\r
136 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL) \
\r
137 LIST_NEXT((listelm), field)->field.le_prev = \
\r
138 &LIST_NEXT((elm), field); \
\r
139 LIST_NEXT((listelm), field) = (elm); \
\r
140 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
\r
143 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
\r
144 (elm)->field.le_prev = (listelm)->field.le_prev; \
\r
145 LIST_NEXT((elm), field) = (listelm); \
\r
146 *(listelm)->field.le_prev = (elm); \
\r
147 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
\r
150 #define LIST_INSERT_HEAD(head, elm, field) do { \
\r
151 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
\r
152 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field); \
\r
153 LIST_FIRST((head)) = (elm); \
\r
154 (elm)->field.le_prev = &LIST_FIRST((head)); \
\r
157 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
\r
159 #define LIST_REMOVE(elm, field) do { \
\r
160 if (LIST_NEXT((elm), field) != NULL) \
\r
161 LIST_NEXT((elm), field)->field.le_prev = \
\r
162 (elm)->field.le_prev; \
\r
163 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
\r
166 #endif /* !_SYS_QUEUE_H_ */
\r
169 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
\r
170 #define PPPOE_ADD_16(PTR, VAL) \
\r
171 *(PTR)++ = (VAL) / 256; \
\r
172 *(PTR)++ = (VAL) % 256
\r
174 /* Add a complete PPPoE header to the buffer pointed to by PTR */
\r
175 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
\r
176 *(PTR)++ = PPPOE_VERTYPE; \
\r
177 *(PTR)++ = (CODE); \
\r
178 PPPOE_ADD_16(PTR, SESS); \
\r
179 PPPOE_ADD_16(PTR, LEN)
\r
181 #define PPPOE_DISC_TIMEOUT (5*1000) /* base for quick timeout calculation */
\r
182 #define PPPOE_SLOW_RETRY (60*1000) /* persistent retry interval */
\r
183 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
\r
184 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
\r
186 #ifdef PPPOE_SERVER
\r
187 /* from if_spppsubr.c */
\r
188 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
\r
191 struct pppoe_softc {
\r
192 LIST_ENTRY(pppoe_softc) sc_list;
\r
193 struct netif *sc_ethif; /* ethernet interface we are using */
\r
194 int sc_pd; /* ppp unit number */
\r
195 void (*sc_linkStatusCB)(int pd, int up);
\r
197 int sc_state; /* discovery phase or session connected */
\r
198 struct eth_addr sc_dest; /* hardware address of concentrator */
\r
199 u16_t sc_session; /* PPPoE session id */
\r
201 char *sc_service_name; /* if != NULL: requested name of service */
\r
202 char *sc_concentrator_name; /* if != NULL: requested concentrator id */
\r
203 u8_t *sc_ac_cookie; /* content of AC cookie we must echo back */
\r
204 size_t sc_ac_cookie_len; /* length of cookie data */
\r
205 #ifdef PPPOE_SERVER
\r
206 u8_t *sc_hunique; /* content of host unique we must echo back */
\r
207 size_t sc_hunique_len; /* length of host unique */
\r
209 int sc_padi_retried; /* number of PADI retries already done */
\r
210 int sc_padr_retried; /* number of PADR retries already done */
\r
213 /* input routines */
\r
214 static void pppoe_dispatch_disc_pkt(struct netif *, struct pbuf *);
\r
216 /* management routines */
\r
217 static int pppoe_do_disconnect(struct pppoe_softc *);
\r
218 static void pppoe_abort_connect(struct pppoe_softc *);
\r
219 static void pppoe_clear_softc(struct pppoe_softc *, const char *);
\r
221 /* internal timeout handling */
\r
222 static void pppoe_timeout(void *);
\r
224 /* sending actual protocol controll packets */
\r
225 static err_t pppoe_send_padi(struct pppoe_softc *);
\r
226 static err_t pppoe_send_padr(struct pppoe_softc *);
\r
227 #ifdef PPPOE_SERVER
\r
228 static err_t pppoe_send_pado(struct pppoe_softc *);
\r
229 static err_t pppoe_send_pads(struct pppoe_softc *);
\r
231 static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);
\r
233 /* internal helper functions */
\r
234 static struct pppoe_softc * pppoe_find_softc_by_session(u_int, struct netif *);
\r
235 static struct pppoe_softc * pppoe_find_softc_by_hunique(u8_t *, size_t, struct netif *);
\r
237 static LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
\r
244 pppoe_hdrlen = sizeof(struct eth_hdr) + PPPOE_HEADERLEN;
\r
245 LIST_INIT(&pppoe_softc_list);
\r
249 pppoe_create(struct netif *ethif, int pd, void (*linkStatusCB)(int pd, int up), struct pppoe_softc **scptr)
\r
251 struct pppoe_softc *sc;
\r
253 sc = mem_malloc(sizeof(struct pppoe_softc));
\r
258 memset(sc, 0, sizeof(struct pppoe_softc));
\r
260 /* changed to real address later */
\r
261 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
\r
264 sc->sc_linkStatusCB = linkStatusCB;
\r
265 sc->sc_ethif = ethif;
\r
267 LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
\r
275 pppoe_destroy(struct netif *ifp)
\r
277 struct pppoe_softc * sc;
\r
279 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
\r
280 if (sc->sc_ethif == ifp) {
\r
285 if(!(sc && (sc->sc_ethif == ifp))) {
\r
289 tcpip_untimeout(pppoe_timeout, sc);
\r
290 LIST_REMOVE(sc, sc_list);
\r
292 if (sc->sc_concentrator_name) {
\r
293 mem_free(sc->sc_concentrator_name);
\r
295 if (sc->sc_service_name) {
\r
296 mem_free(sc->sc_service_name);
\r
298 if (sc->sc_ac_cookie) {
\r
299 mem_free(sc->sc_ac_cookie);
\r
307 * Find the interface handling the specified session.
\r
308 * Note: O(number of sessions open), this is a client-side only, mean
\r
309 * and lean implementation, so number of open sessions typically should
\r
312 static struct pppoe_softc *
\r
313 pppoe_find_softc_by_session(u_int session, struct netif *rcvif)
\r
315 struct pppoe_softc *sc;
\r
317 if (session == 0) {
\r
321 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
\r
322 if (sc->sc_state == PPPOE_STATE_SESSION
\r
323 && sc->sc_session == session) {
\r
324 if (sc->sc_ethif == rcvif) {
\r
334 /* Check host unique token passed and return appropriate softc pointer,
\r
335 * or NULL if token is bogus. */
\r
336 static struct pppoe_softc *
\r
337 pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif)
\r
339 struct pppoe_softc *sc, *t;
\r
341 if (LIST_EMPTY(&pppoe_softc_list)) {
\r
345 if (len != sizeof sc) {
\r
348 MEMCPY(&t, token, len);
\r
350 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
\r
357 PPPDEBUG((LOG_DEBUG, "pppoe: alien host unique tag, no session found\n"));
\r
361 /* should be safe to access *sc now */
\r
362 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
\r
363 printf("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
\r
364 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state);
\r
367 if (sc->sc_ethif != rcvif) {
\r
368 printf("%c%c%"U16_F": wrong interface, not accepting host unique\n",
\r
369 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
376 pppoe_linkstatus_up(void *arg)
\r
378 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
\r
380 sc->sc_linkStatusCB(sc->sc_pd, 1);
\r
383 /* analyze and handle a single received packet while not in session state */
\r
385 pppoe_dispatch_disc_pkt(struct netif *netif, struct pbuf *pb)
\r
388 u16_t session, plen;
\r
389 struct pppoe_softc *sc;
\r
390 const char *err_msg;
\r
394 size_t ac_cookie_len;
\r
395 #ifdef PPPOE_SERVER
\r
397 size_t hunique_len;
\r
399 struct pppoehdr *ph;
\r
400 struct pppoetag pt;
\r
401 int off = 0, err, errortag;
\r
402 struct eth_hdr *ethhdr;
\r
404 pb = pppSingleBuf(pb);
\r
406 strcpy(devname, "pppoe"); /* as long as we don't know which instance */
\r
409 if (pb->len < sizeof(*ethhdr)) {
\r
412 ethhdr = (struct eth_hdr *)pb->payload;
\r
413 off += sizeof(*ethhdr);
\r
417 #ifdef PPPOE_SERVER
\r
422 if (pb->len - off <= PPPOE_HEADERLEN) {
\r
423 printf("pppoe: packet too short: %d\n", pb->len);
\r
427 ph = (struct pppoehdr *) (ethhdr + 1);
\r
428 if (ph->vertype != PPPOE_VERTYPE) {
\r
429 printf("pppoe: unknown version/type packet: 0x%x\n", ph->vertype);
\r
432 session = ntohs(ph->session);
\r
433 plen = ntohs(ph->plen);
\r
434 off += sizeof(*ph);
\r
436 if (plen + off > pb->len) {
\r
437 printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
\r
438 pb->len - off, plen);
\r
441 if(pb->tot_len == pb->len) {
\r
442 pb->tot_len = pb->len = off + plen; /* ignore trailing garbage */
\r
447 while (off + sizeof(pt) <= pb->len) {
\r
448 MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));
\r
449 tag = ntohs(pt.tag);
\r
450 len = ntohs(pt.len);
\r
451 if (off + sizeof(pt) + len > pb->len) {
\r
452 printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);
\r
456 case PPPOE_TAG_EOL:
\r
458 case PPPOE_TAG_SNAME:
\r
459 break; /* ignored */
\r
460 case PPPOE_TAG_ACNAME:
\r
461 break; /* ignored */
\r
462 case PPPOE_TAG_HUNIQUE:
\r
466 #ifdef PPPOE_SERVER
\r
467 hunique = (u8_t*)pb->payload + off + sizeof(pt);
\r
470 sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);
\r
472 snprintf(devname, sizeof(devname), "%c%c%"U16_F, sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
475 case PPPOE_TAG_ACCOOKIE:
\r
476 if (ac_cookie == NULL) {
\r
477 ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);
\r
478 ac_cookie_len = len;
\r
481 case PPPOE_TAG_SNAME_ERR:
\r
482 err_msg = "SERVICE NAME ERROR";
\r
485 case PPPOE_TAG_ACSYS_ERR:
\r
486 err_msg = "AC SYSTEM ERROR";
\r
489 case PPPOE_TAG_GENERIC_ERR:
\r
490 err_msg = "GENERIC ERROR";
\r
496 if (errortag && len) {
\r
497 error = mem_malloc(len+1);
\r
499 strncpy(error, (char*)pb->payload + off + sizeof(pt), len);
\r
500 error[len-1] = '\0';
\r
504 printf("%s: %s: %s\n", devname, err_msg, error);
\r
507 printf("%s: %s\n", devname, err_msg);
\r
513 off += sizeof(pt) + len;
\r
517 switch (ph->code) {
\r
518 case PPPOE_CODE_PADI:
\r
519 #ifdef PPPOE_SERVER
\r
521 * got service name, concentrator name, and/or host unique.
\r
522 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
\r
524 if (LIST_EMPTY(&pppoe_softc_list)) {
\r
527 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
\r
528 if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
\r
531 if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
\r
534 if (sc->sc_state == PPPOE_STATE_INITIAL) {
\r
539 /* printf("pppoe: free passive interface is not found\n"); */
\r
543 if (sc->sc_hunique) {
\r
544 mem_free(sc->sc_hunique);
\r
546 sc->sc_hunique = mem_malloc(hunique_len);
\r
547 if (sc->sc_hunique == NULL) {
\r
550 sc->sc_hunique_len = hunique_len;
\r
551 MEMCPY(sc->sc_hunique, hunique, hunique_len);
\r
553 MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
\r
554 sc->sc_state = PPPOE_STATE_PADO_SENT;
\r
555 pppoe_send_pado(sc);
\r
557 #endif /* PPPOE_SERVER */
\r
558 case PPPOE_CODE_PADR:
\r
559 #ifdef PPPOE_SERVER
\r
561 * get sc from ac_cookie if IFF_PASSIVE
\r
563 if (ac_cookie == NULL) {
\r
564 /* be quiet if there is not a single pppoe instance */
\r
565 printf("pppoe: received PADR but not includes ac_cookie\n");
\r
568 sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
\r
570 /* be quiet if there is not a single pppoe instance */
\r
571 if (!LIST_EMPTY(&pppoe_softc_list)) {
\r
572 printf("pppoe: received PADR but could not find request for it\n");
\r
576 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
\r
577 printf("%c%c%"U16_F": received unexpected PADR\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
581 if (sc->sc_hunique) {
\r
582 mem_free(sc->sc_hunique);
\r
584 sc->sc_hunique = mem_malloc(hunique_len);
\r
585 if (sc->sc_hunique == NULL) {
\r
588 sc->sc_hunique_len = hunique_len;
\r
589 MEMCPY(sc->sc_hunique, hunique, hunique_len);
\r
591 pppoe_send_pads(sc);
\r
592 sc->sc_state = PPPOE_STATE_SESSION;
\r
593 tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */
\r
596 /* ignore, we are no access concentrator */
\r
598 #endif /* PPPOE_SERVER */
\r
599 case PPPOE_CODE_PADO:
\r
601 /* be quiet if there is not a single pppoe instance */
\r
602 if (!LIST_EMPTY(&pppoe_softc_list)) {
\r
603 printf("pppoe: received PADO but could not find request for it\n");
\r
607 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
\r
608 printf("%c%c%"U16_F": received unexpected PADO\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
612 if (sc->sc_ac_cookie) {
\r
613 mem_free(sc->sc_ac_cookie);
\r
615 sc->sc_ac_cookie = mem_malloc(ac_cookie_len);
\r
616 if (sc->sc_ac_cookie == NULL) {
\r
619 sc->sc_ac_cookie_len = ac_cookie_len;
\r
620 MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
\r
622 MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));
\r
623 tcpip_untimeout(pppoe_timeout, sc);
\r
624 sc->sc_padr_retried = 0;
\r
625 sc->sc_state = PPPOE_STATE_PADR_SENT;
\r
626 if ((err = pppoe_send_padr(sc)) != 0) {
\r
627 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));
\r
629 tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
\r
631 case PPPOE_CODE_PADS:
\r
635 sc->sc_session = session;
\r
636 tcpip_untimeout(pppoe_timeout, sc);
\r
637 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));
\r
638 sc->sc_state = PPPOE_STATE_SESSION;
\r
639 tcpip_timeout (100, pppoe_linkstatus_up, sc); /* notify upper layers */
\r
641 case PPPOE_CODE_PADT:
\r
645 pppoe_clear_softc(sc, "received PADT");
\r
649 printf("%c%c%"U16_F": unknown code (0x%04x) session = 0x%04x\n",
\r
650 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
\r
651 ph->code, session);
\r
653 printf("pppoe: unknown code (0x%04x) session = 0x%04x\n", ph->code, session);
\r
664 pppoe_disc_input(struct netif *netif, struct pbuf *p)
\r
666 /* avoid error messages if there is not a single pppoe instance */
\r
667 if (!LIST_EMPTY(&pppoe_softc_list)) {
\r
668 pppoe_dispatch_disc_pkt(netif, p);
\r
675 pppoe_data_input(struct netif *netif, struct pbuf *pb)
\r
677 u16_t session, plen;
\r
678 struct pppoe_softc *sc;
\r
679 struct pppoehdr *ph;
\r
680 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
\r
681 u8_t shost[ETHER_ADDR_LEN];
\r
684 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
\r
685 MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));
\r
687 if (pbuf_header(pb, -(int)sizeof(struct eth_hdr)) != 0) {
\r
689 PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header failed\n"));
\r
690 LINK_STATS_INC(link.lenerr);
\r
694 pb = pppSingleBuf (pb);
\r
696 if (pb->len <= PPPOE_HEADERLEN) {
\r
697 printf("pppoe (data): dropping too short packet: %d bytes\n", pb->len);
\r
701 if (pb->len < sizeof(*ph)) {
\r
702 printf("pppoe_data_input: could not get PPPoE header\n");
\r
705 ph = (struct pppoehdr *)pb->payload;
\r
707 if (ph->vertype != PPPOE_VERTYPE) {
\r
708 printf("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype);
\r
711 if (ph->code != 0) {
\r
715 session = ntohs(ph->session);
\r
716 sc = pppoe_find_softc_by_session(session, netif);
\r
718 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
\r
719 printf("pppoe: input for unknown session 0x%x, sending PADT\n", session);
\r
720 pppoe_send_padt(netif, session, shost);
\r
725 plen = ntohs(ph->plen);
\r
727 if (pbuf_header(pb, -(int)(PPPOE_HEADERLEN)) != 0) {
\r
729 PPPDEBUG((LOG_ERR, "pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));
\r
730 LINK_STATS_INC(link.lenerr);
\r
734 PPPDEBUG((LOG_DEBUG, "pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",
\r
735 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
\r
738 if (pb->len < plen) {
\r
742 pppInProcOverEthernet(sc->sc_pd, pb);
\r
751 pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
\r
753 struct eth_hdr *ethhdr;
\r
757 if (!sc->sc_ethif) {
\r
762 ethhdr = (struct eth_hdr *)pb->payload;
\r
763 etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
\r
764 ethhdr->type = htons(etype);
\r
765 MEMCPY(ethhdr->dest.addr, sc->sc_dest.addr, sizeof(ethhdr->dest.addr));
\r
766 MEMCPY(ethhdr->src.addr, ((struct eth_addr *)sc->sc_ethif->hwaddr)->addr, sizeof(ethhdr->src.addr));
\r
768 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",
\r
769 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,
\r
770 sc->sc_state, sc->sc_session,
\r
771 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],
\r
774 res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);
\r
782 pppoe_send_padi(struct pppoe_softc *sc)
\r
786 int len, l1 = 0, l2 = 0; /* XXX: gcc */
\r
788 if (sc->sc_state >PPPOE_STATE_PADI_SENT) {
\r
789 PPPDEBUG((LOG_ERR, "ERROR: pppoe_send_padi in state %d", sc->sc_state));
\r
792 /* calculate length of frame (excluding ethernet header + pppoe header) */
\r
793 len = 2 + 2 + 2 + 2 + sizeof sc; /* service name tag is required, host unique is send too */
\r
794 if (sc->sc_service_name != NULL) {
\r
795 l1 = strlen(sc->sc_service_name);
\r
798 if (sc->sc_concentrator_name != NULL) {
\r
799 l2 = strlen(sc->sc_concentrator_name);
\r
803 /* allocate a buffer */
\r
804 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
\r
809 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
\r
811 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
\r
812 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
\r
813 if (sc->sc_service_name != NULL) {
\r
814 PPPOE_ADD_16(p, l1);
\r
815 MEMCPY(p, sc->sc_service_name, l1);
\r
818 PPPOE_ADD_16(p, 0);
\r
820 if (sc->sc_concentrator_name != NULL) {
\r
821 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
\r
822 PPPOE_ADD_16(p, l2);
\r
823 MEMCPY(p, sc->sc_concentrator_name, l2);
\r
826 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
\r
827 PPPOE_ADD_16(p, sizeof(sc));
\r
828 MEMCPY(p, &sc, sizeof sc);
\r
831 return pppoe_output(sc, pb);
\r
835 pppoe_timeout(void *arg)
\r
837 int retry_wait, err;
\r
838 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
\r
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));
\r
842 switch (sc->sc_state) {
\r
843 case PPPOE_STATE_PADI_SENT:
\r
845 * We have two basic ways of retrying:
\r
846 * - Quick retry mode: try a few times in short sequence
\r
847 * - Slow retry mode: we already had a connection successfully
\r
848 * established and will try infinitely (without user
\r
850 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
\r
854 /* initialize for quick retry mode */
\r
855 retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
\r
857 sc->sc_padi_retried++;
\r
858 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
\r
860 if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
\r
861 /* slow retry mode */
\r
862 retry_wait = PPPOE_SLOW_RETRY;
\r
866 pppoe_abort_connect(sc);
\r
870 if ((err = pppoe_send_padi(sc)) != 0) {
\r
871 sc->sc_padi_retried--;
\r
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));
\r
874 tcpip_timeout(retry_wait, pppoe_timeout, sc);
\r
877 case PPPOE_STATE_PADR_SENT:
\r
878 sc->sc_padr_retried++;
\r
879 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
\r
880 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
\r
881 sc->sc_state = PPPOE_STATE_PADI_SENT;
\r
882 sc->sc_padr_retried = 0;
\r
883 if ((err = pppoe_send_padi(sc)) != 0) {
\r
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));
\r
886 tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);
\r
889 if ((err = pppoe_send_padr(sc)) != 0) {
\r
890 sc->sc_padr_retried--;
\r
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));
\r
893 tcpip_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
\r
895 case PPPOE_STATE_CLOSING:
\r
896 pppoe_do_disconnect(sc);
\r
899 return; /* all done, work in peace */
\r
903 /* Start a connection (i.e. initiate discovery phase) */
\r
905 pppoe_connect(struct pppoe_softc *sc)
\r
909 if (sc->sc_state != PPPOE_STATE_INITIAL) {
\r
913 #ifdef PPPOE_SERVER
\r
914 /* wait PADI if IFF_PASSIVE */
\r
915 if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
\r
919 /* save state, in case we fail to send PADI */
\r
920 sc->sc_state = PPPOE_STATE_PADI_SENT;
\r
921 sc->sc_padr_retried = 0;
\r
922 err = pppoe_send_padi(sc);
\r
923 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));
\r
924 tcpip_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
\r
930 pppoe_disconnect(struct pppoe_softc *sc)
\r
932 if (sc->sc_state < PPPOE_STATE_SESSION) {
\r
936 * Do not call pppoe_disconnect here, the upper layer state
\r
937 * machine gets confused by this. We must return from this
\r
938 * function and defer disconnecting to the timeout handler.
\r
940 sc->sc_state = PPPOE_STATE_CLOSING;
\r
941 tcpip_timeout(20, pppoe_timeout, sc);
\r
945 pppoe_do_disconnect(struct pppoe_softc *sc)
\r
949 if (sc->sc_state < PPPOE_STATE_SESSION) {
\r
952 PPPDEBUG((LOG_DEBUG, "pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
\r
953 err = pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);
\r
956 /* cleanup softc */
\r
957 sc->sc_state = PPPOE_STATE_INITIAL;
\r
958 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
\r
959 if (sc->sc_ac_cookie) {
\r
960 mem_free(sc->sc_ac_cookie);
\r
961 sc->sc_ac_cookie = NULL;
\r
963 sc->sc_ac_cookie_len = 0;
\r
964 #ifdef PPPOE_SERVER
\r
965 if (sc->sc_hunique) {
\r
966 mem_free(sc->sc_hunique);
\r
967 sc->sc_hunique = NULL;
\r
969 sc->sc_hunique_len = 0;
\r
971 sc->sc_session = 0;
\r
973 sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */
\r
978 /* Connection attempt aborted */
\r
980 pppoe_abort_connect(struct pppoe_softc *sc)
\r
982 printf("%c%c%"U16_F": could not establish connection\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
983 sc->sc_state = PPPOE_STATE_CLOSING;
\r
985 sc->sc_linkStatusCB(sc->sc_pd, 0); /* notify upper layers */
\r
987 /* clear connection state */
\r
988 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
\r
989 sc->sc_state = PPPOE_STATE_INITIAL;
\r
992 /* Send a PADR packet */
\r
994 pppoe_send_padr(struct pppoe_softc *sc)
\r
998 size_t len, l1 = 0; /* XXX: gcc */
\r
1000 if (sc->sc_state != PPPOE_STATE_PADR_SENT) {
\r
1004 len = 2 + 2 + 2 + 2 + sizeof(sc); /* service name, host unique */
\r
1005 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
\r
1006 l1 = strlen(sc->sc_service_name);
\r
1009 if (sc->sc_ac_cookie_len > 0) {
\r
1010 len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
\r
1012 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
\r
1016 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
\r
1017 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
\r
1018 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
\r
1019 if (sc->sc_service_name != NULL) {
\r
1020 PPPOE_ADD_16(p, l1);
\r
1021 MEMCPY(p, sc->sc_service_name, l1);
\r
1024 PPPOE_ADD_16(p, 0);
\r
1026 if (sc->sc_ac_cookie_len > 0) {
\r
1027 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
\r
1028 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
\r
1029 MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
\r
1030 p += sc->sc_ac_cookie_len;
\r
1032 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
\r
1033 PPPOE_ADD_16(p, sizeof(sc));
\r
1034 MEMCPY(p, &sc, sizeof sc);
\r
1036 return pppoe_output(sc, pb);
\r
1039 /* send a PADT packet */
\r
1041 pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
\r
1044 struct eth_hdr *ethhdr;
\r
1048 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN, PBUF_RAM);
\r
1053 ethhdr = (struct eth_hdr *)pb->payload;
\r
1054 ethhdr->type = htons(ETHTYPE_PPPOEDISC);
\r
1055 MEMCPY(ethhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
\r
1056 MEMCPY(ethhdr->src.addr, ((struct eth_addr *)outgoing_if->hwaddr)->addr, sizeof(ethhdr->src.addr));
\r
1058 p = (u8_t*)(ethhdr + 1);
\r
1059 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
\r
1061 res = outgoing_if->linkoutput(outgoing_if, pb);
\r
1068 #ifdef PPPOE_SERVER
\r
1070 pppoe_send_pado(struct pppoe_softc *sc)
\r
1076 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
\r
1082 /* include ac_cookie */
\r
1083 len += 2 + 2 + sizeof(sc);
\r
1084 /* include hunique */
\r
1085 len += 2 + 2 + sc->sc_hunique_len;
\r
1086 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
\r
1090 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
\r
1091 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
\r
1092 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
\r
1093 PPPOE_ADD_16(p, sizeof(sc));
\r
1094 MEMCPY(p, &sc, sizeof(sc));
\r
1096 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
\r
1097 PPPOE_ADD_16(p, sc->sc_hunique_len);
\r
1098 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
\r
1099 return pppoe_output(sc, pb);
\r
1103 pppoe_send_pads(struct pppoe_softc *sc)
\r
1107 size_t len, l1 = 0; /* XXX: gcc */
\r
1109 if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
\r
1113 sc->sc_session = mono_time.tv_sec % 0xff + 1;
\r
1116 /* include hunique */
\r
1117 len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique*/
\r
1118 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
\r
1119 l1 = strlen(sc->sc_service_name);
\r
1122 pb = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len, PBUF_RAM);
\r
1126 p = (u8_t*)pb->payload + sizeof (struct eth_hdr);
\r
1127 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
\r
1128 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
\r
1129 if (sc->sc_service_name != NULL) {
\r
1130 PPPOE_ADD_16(p, l1);
\r
1131 MEMCPY(p, sc->sc_service_name, l1);
\r
1134 PPPOE_ADD_16(p, 0);
\r
1136 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
\r
1137 PPPOE_ADD_16(p, sc->sc_hunique_len);
\r
1138 MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
\r
1139 return pppoe_output(sc, pb);
\r
1144 pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
\r
1149 /* are we ready to process data yet? */
\r
1150 if (sc->sc_state < PPPOE_STATE_SESSION) {
\r
1151 /*sppp_flush(&sc->sc_sppp.pp_if);*/
\r
1156 len = pb->tot_len;
\r
1158 /* make room for Ethernet header - should not fail */
\r
1159 if (pbuf_header(pb, sizeof(struct eth_hdr) + PPPOE_HEADERLEN) != 0) {
\r
1161 PPPDEBUG((LOG_ERR, "pppoe: %c%c%"U16_F": pppoe_xmit: could not allocate room for header\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
\r
1162 LINK_STATS_INC(link.lenerr);
\r
1167 p = (u8_t*)pb->payload + sizeof(struct eth_hdr);
\r
1168 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
\r
1170 return pppoe_output(sc, pb);
\r
1173 #if 0 /*def PFIL_HOOKS*/
\r
1175 pppoe_ifattach_hook(void *arg, struct pbuf **mp, struct netif *ifp, int dir)
\r
1177 struct pppoe_softc *sc;
\r
1180 if (mp != (struct pbuf **)PFIL_IFNET_DETACH) {
\r
1184 LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
\r
1185 if (sc->sc_ethif != ifp) {
\r
1188 if (sc->sc_sppp.pp_if.if_flags & IFF_UP) {
\r
1189 sc->sc_sppp.pp_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
\r
1190 printf("%c%c%"U16_F": ethernet interface detached, going down\n",
\r
1191 sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num);
\r
1193 sc->sc_ethif = NULL;
\r
1194 pppoe_clear_softc(sc, "ethernet interface detached");
\r
1202 pppoe_clear_softc(struct pppoe_softc *sc, const char *message)
\r
1204 LWIP_UNUSED_ARG(message);
\r
1207 tcpip_untimeout(pppoe_timeout, sc);
\r
1208 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));
\r
1210 /* fix our state */
\r
1211 sc->sc_state = PPPOE_STATE_INITIAL;
\r
1213 /* notify upper layers */
\r
1214 sc->sc_linkStatusCB(sc->sc_pd, 0);
\r
1216 /* clean up softc */
\r
1217 MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
\r
1218 if (sc->sc_ac_cookie) {
\r
1219 mem_free(sc->sc_ac_cookie);
\r
1220 sc->sc_ac_cookie = NULL;
\r
1222 sc->sc_ac_cookie_len = 0;
\r
1223 sc->sc_session = 0;
\r
1226 #endif /* PPPOE_SUPPORT */
\r