1 /*****************************************************************************
2 * ppp_oe.c - PPP Over Ethernet implementation for lwIP.
4 * Copyright (c) 2006 by Marc Boucher, Services Informatiques (MBSI) inc.
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.
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.
24 ******************************************************************************
27 * 06-01-01 Marc Boucher <marc@mbsi.ca>
29 *****************************************************************************/
33 /* based on NetBSD: if_pppoe.c,v 1.64 2006/01/31 23:50:15 martin Exp */
36 * Copyright (c) 2002 The NetBSD Foundation, Inc.
37 * All rights reserved.
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Martin Husemann <martin@NetBSD.org>.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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.
73 #if PPPOE_SUPPORT /* don't build if not configured for use in lwipopts.h */
75 #include "netif/ppp_oe.h"
80 #include "lwip/timers.h"
81 #include "lwip/memp.h"
87 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
88 #define PPPOE_ADD_16(PTR, VAL) \
89 *(PTR)++ = (u8_t)((VAL) / 256); \
90 *(PTR)++ = (u8_t)((VAL) % 256)
92 /* Add a complete PPPoE header to the buffer pointed to by PTR */
93 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
94 *(PTR)++ = PPPOE_VERTYPE; \
96 PPPOE_ADD_16(PTR, SESS); \
97 PPPOE_ADD_16(PTR, LEN)
99 #define PPPOE_DISC_TIMEOUT (5*1000) /* base for quick timeout calculation */
100 #define PPPOE_SLOW_RETRY (60*1000) /* persistent retry interval */
101 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
102 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
105 #error "PPPOE_SERVER is not yet supported under lwIP!"
106 /* from if_spppsubr.c */
107 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
110 #ifndef PPPOE_ERRORSTRING_LEN
111 #define PPPOE_ERRORSTRING_LEN 64
113 static char pppoe_error_tmp
[PPPOE_ERRORSTRING_LEN
];
117 static void pppoe_dispatch_disc_pkt(struct netif
*, struct pbuf
*);
119 /* management routines */
120 static int pppoe_do_disconnect(struct pppoe_softc
*);
121 static void pppoe_abort_connect(struct pppoe_softc
*);
122 static void pppoe_clear_softc(struct pppoe_softc
*, const char *);
124 /* internal timeout handling */
125 static void pppoe_timeout(void *);
127 /* sending actual protocol controll packets */
128 static err_t
pppoe_send_padi(struct pppoe_softc
*);
129 static err_t
pppoe_send_padr(struct pppoe_softc
*);
131 static err_t
pppoe_send_pado(struct pppoe_softc
*);
132 static err_t
pppoe_send_pads(struct pppoe_softc
*);
134 static err_t
pppoe_send_padt(struct netif
*, u_int
, const u8_t
*);
136 /* internal helper functions */
137 static struct pppoe_softc
* pppoe_find_softc_by_session(u_int
, struct netif
*);
138 static struct pppoe_softc
* pppoe_find_softc_by_hunique(u8_t
*, size_t, struct netif
*);
140 /** linked list of created pppoe interfaces */
141 static struct pppoe_softc
*pppoe_softc_list
;
144 pppoe_create(struct netif
*ethif
, int pd
, void (*linkStatusCB
)(int pd
, int up
), struct pppoe_softc
**scptr
)
146 struct pppoe_softc
*sc
;
148 sc
= (struct pppoe_softc
*)memp_malloc(MEMP_PPPOE_IF
);
153 memset(sc
, 0, sizeof(struct pppoe_softc
));
155 /* changed to real address later */
156 MEMCPY(&sc
->sc_dest
, ethbroadcast
.addr
, sizeof(sc
->sc_dest
));
159 sc
->sc_linkStatusCB
= linkStatusCB
;
160 sc
->sc_ethif
= ethif
;
162 /* put the new interface at the head of the list */
163 sc
->next
= pppoe_softc_list
;
164 pppoe_softc_list
= sc
;
172 pppoe_destroy(struct netif
*ifp
)
174 struct pppoe_softc
*sc
, *prev
= NULL
;
176 for (sc
= pppoe_softc_list
; sc
!= NULL
; prev
= sc
, sc
= sc
->next
) {
177 if (sc
->sc_ethif
== ifp
) {
182 if(!(sc
&& (sc
->sc_ethif
== ifp
))) {
186 sys_untimeout(pppoe_timeout
, sc
);
188 /* remove sc from the head of the list */
189 pppoe_softc_list
= sc
->next
;
191 /* remove sc from the list */
192 prev
->next
= sc
->next
;
196 if (sc
->sc_concentrator_name
) {
197 mem_free(sc
->sc_concentrator_name
);
199 if (sc
->sc_service_name
) {
200 mem_free(sc
->sc_service_name
);
202 #endif /* PPPOE_TODO */
203 memp_free(MEMP_PPPOE_IF
, sc
);
209 * Find the interface handling the specified session.
210 * Note: O(number of sessions open), this is a client-side only, mean
211 * and lean implementation, so number of open sessions typically should
214 static struct pppoe_softc
*
215 pppoe_find_softc_by_session(u_int session
, struct netif
*rcvif
)
217 struct pppoe_softc
*sc
;
223 for (sc
= pppoe_softc_list
; sc
!= NULL
; sc
= sc
->next
) {
224 if (sc
->sc_state
== PPPOE_STATE_SESSION
225 && sc
->sc_session
== session
) {
226 if (sc
->sc_ethif
== rcvif
) {
236 /* Check host unique token passed and return appropriate softc pointer,
237 * or NULL if token is bogus. */
238 static struct pppoe_softc
*
239 pppoe_find_softc_by_hunique(u8_t
*token
, size_t len
, struct netif
*rcvif
)
241 struct pppoe_softc
*sc
, *t
;
243 if (pppoe_softc_list
== NULL
) {
247 if (len
!= sizeof sc
) {
250 MEMCPY(&t
, token
, len
);
252 for (sc
= pppoe_softc_list
; sc
!= NULL
; sc
= sc
->next
) {
259 PPPDEBUG(LOG_DEBUG
, ("pppoe: alien host unique tag, no session found\n"));
263 /* should be safe to access *sc now */
264 if (sc
->sc_state
< PPPOE_STATE_PADI_SENT
|| sc
->sc_state
>= PPPOE_STATE_SESSION
) {
265 printf("%c%c%"U16_F
": host unique tag found, but it belongs to a connection in state %d\n",
266 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
, sc
->sc_state
);
269 if (sc
->sc_ethif
!= rcvif
) {
270 printf("%c%c%"U16_F
": wrong interface, not accepting host unique\n",
271 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
278 pppoe_linkstatus_up(struct pppoe_softc
*sc
)
280 sc
->sc_linkStatusCB(sc
->sc_pd
, 1);
283 /* analyze and handle a single received packet while not in session state */
285 pppoe_dispatch_disc_pkt(struct netif
*netif
, struct pbuf
*pb
)
289 struct pppoe_softc
*sc
;
300 int off
, err
, errortag
;
301 struct eth_hdr
*ethhdr
;
303 pb
= pppSingleBuf(pb
);
305 strcpy(devname
, "pppoe"); /* as long as we don't know which instance */
308 if (pb
->len
< sizeof(*ethhdr
)) {
311 ethhdr
= (struct eth_hdr
*)pb
->payload
;
312 off
= sizeof(*ethhdr
);
321 if (pb
->len
- off
< PPPOE_HEADERLEN
) {
322 printf("pppoe: packet too short: %d\n", pb
->len
);
326 ph
= (struct pppoehdr
*) (ethhdr
+ 1);
327 if (ph
->vertype
!= PPPOE_VERTYPE
) {
328 printf("pppoe: unknown version/type packet: 0x%x\n", ph
->vertype
);
331 session
= ntohs(ph
->session
);
332 plen
= ntohs(ph
->plen
);
335 if (plen
+ off
> pb
->len
) {
336 printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
337 pb
->len
- off
, plen
);
340 if(pb
->tot_len
== pb
->len
) {
341 pb
->tot_len
= pb
->len
= (u16_t
)off
+ plen
; /* ignore trailing garbage */
346 while (off
+ sizeof(pt
) <= pb
->len
) {
347 MEMCPY(&pt
, (u8_t
*)pb
->payload
+ off
, sizeof(pt
));
350 if (off
+ sizeof(pt
) + len
> pb
->len
) {
351 printf("pppoe: tag 0x%x len 0x%x is too long\n", tag
, len
);
357 case PPPOE_TAG_SNAME
:
359 case PPPOE_TAG_ACNAME
:
361 case PPPOE_TAG_HUNIQUE
:
366 hunique
= (u8_t
*)pb
->payload
+ off
+ sizeof(pt
);
369 sc
= pppoe_find_softc_by_hunique((u8_t
*)pb
->payload
+ off
+ sizeof(pt
), len
, netif
);
371 snprintf(devname
, sizeof(devname
), "%c%c%"U16_F
, sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
374 case PPPOE_TAG_ACCOOKIE
:
375 if (ac_cookie
== NULL
) {
376 ac_cookie
= (u8_t
*)pb
->payload
+ off
+ sizeof(pt
);
380 case PPPOE_TAG_SNAME_ERR
:
381 err_msg
= "SERVICE NAME ERROR";
384 case PPPOE_TAG_ACSYS_ERR
:
385 err_msg
= "AC SYSTEM ERROR";
388 case PPPOE_TAG_GENERIC_ERR
:
389 err_msg
= "GENERIC ERROR";
394 if (errortag
&& len
) {
395 u16_t error_len
= LWIP_MIN(len
, sizeof(pppoe_error_tmp
)-1);
396 strncpy(pppoe_error_tmp
, (char*)pb
->payload
+ off
+ sizeof(pt
), error_len
);
397 pppoe_error_tmp
[error_len
-1] = '\0';
398 printf("%s: %s: %s\n", devname
, err_msg
, pppoe_error_tmp
);
400 printf("%s: %s\n", devname
, err_msg
);
406 off
+= sizeof(pt
) + len
;
411 case PPPOE_CODE_PADI
:
414 * got service name, concentrator name, and/or host unique.
415 * ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
417 if (LIST_EMPTY(&pppoe_softc_list
)) {
420 LIST_FOREACH(sc
, &pppoe_softc_list
, sc_list
) {
421 if (!(sc
->sc_sppp
.pp_if
.if_flags
& IFF_UP
)) {
424 if (!(sc
->sc_sppp
.pp_if
.if_flags
& IFF_PASSIVE
)) {
427 if (sc
->sc_state
== PPPOE_STATE_INITIAL
) {
432 /* printf("pppoe: free passive interface is not found\n"); */
436 if (sc
->sc_hunique
) {
437 mem_free(sc
->sc_hunique
);
439 sc
->sc_hunique
= mem_malloc(hunique_len
);
440 if (sc
->sc_hunique
== NULL
) {
443 sc
->sc_hunique_len
= hunique_len
;
444 MEMCPY(sc
->sc_hunique
, hunique
, hunique_len
);
446 MEMCPY(&sc
->sc_dest
, eh
->ether_shost
, sizeof sc
->sc_dest
);
447 sc
->sc_state
= PPPOE_STATE_PADO_SENT
;
450 #endif /* PPPOE_SERVER */
451 case PPPOE_CODE_PADR
:
454 * get sc from ac_cookie if IFF_PASSIVE
456 if (ac_cookie
== NULL
) {
457 /* be quiet if there is not a single pppoe instance */
458 printf("pppoe: received PADR but not includes ac_cookie\n");
461 sc
= pppoe_find_softc_by_hunique(ac_cookie
, ac_cookie_len
, netif
);
463 /* be quiet if there is not a single pppoe instance */
464 if (!LIST_EMPTY(&pppoe_softc_list
)) {
465 printf("pppoe: received PADR but could not find request for it\n");
469 if (sc
->sc_state
!= PPPOE_STATE_PADO_SENT
) {
470 printf("%c%c%"U16_F
": received unexpected PADR\n", sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
474 if (sc
->sc_hunique
) {
475 mem_free(sc
->sc_hunique
);
477 sc
->sc_hunique
= mem_malloc(hunique_len
);
478 if (sc
->sc_hunique
== NULL
) {
481 sc
->sc_hunique_len
= hunique_len
;
482 MEMCPY(sc
->sc_hunique
, hunique
, hunique_len
);
485 sc
->sc_state
= PPPOE_STATE_SESSION
;
486 pppoe_linkstatus_up(sc
); /* notify upper layers */
489 /* ignore, we are no access concentrator */
491 #endif /* PPPOE_SERVER */
492 case PPPOE_CODE_PADO
:
494 /* be quiet if there is not a single pppoe instance */
495 if (pppoe_softc_list
!= NULL
) {
496 printf("pppoe: received PADO but could not find request for it\n");
500 if (sc
->sc_state
!= PPPOE_STATE_PADI_SENT
) {
501 printf("%c%c%"U16_F
": received unexpected PADO\n", sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
505 sc
->sc_ac_cookie_len
= ac_cookie_len
;
506 MEMCPY(sc
->sc_ac_cookie
, ac_cookie
, ac_cookie_len
);
508 MEMCPY(&sc
->sc_dest
, ethhdr
->src
.addr
, sizeof(sc
->sc_dest
.addr
));
509 sys_untimeout(pppoe_timeout
, sc
);
510 sc
->sc_padr_retried
= 0;
511 sc
->sc_state
= PPPOE_STATE_PADR_SENT
;
512 if ((err
= pppoe_send_padr(sc
)) != 0) {
513 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
));
515 sys_timeout(PPPOE_DISC_TIMEOUT
* (1 + sc
->sc_padr_retried
), pppoe_timeout
, sc
);
517 case PPPOE_CODE_PADS
:
521 sc
->sc_session
= session
;
522 sys_untimeout(pppoe_timeout
, sc
);
523 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
));
524 sc
->sc_state
= PPPOE_STATE_SESSION
;
525 pppoe_linkstatus_up(sc
); /* notify upper layers */
527 case PPPOE_CODE_PADT
:
531 pppoe_clear_softc(sc
, "received PADT");
535 printf("%c%c%"U16_F
": unknown code (0x%"X16_F
") session = 0x%"X16_F
"\n",
536 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
,
537 (u16_t
)ph
->code
, session
);
539 printf("pppoe: unknown code (0x%"X16_F
") session = 0x%"X16_F
"\n", (u16_t
)ph
->code
, session
);
550 pppoe_disc_input(struct netif
*netif
, struct pbuf
*p
)
552 /* avoid error messages if there is not a single pppoe instance */
553 if (pppoe_softc_list
!= NULL
) {
554 pppoe_dispatch_disc_pkt(netif
, p
);
561 pppoe_data_input(struct netif
*netif
, struct pbuf
*pb
)
564 struct pppoe_softc
*sc
;
566 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
567 u8_t shost
[ETHER_ADDR_LEN
];
570 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
571 MEMCPY(shost
, ((struct eth_hdr
*)pb
->payload
)->src
.addr
, sizeof(shost
));
573 if (pbuf_header(pb
, -(int)sizeof(struct eth_hdr
)) != 0) {
575 PPPDEBUG(LOG_ERR
, ("pppoe_data_input: pbuf_header failed\n"));
576 LINK_STATS_INC(link
.lenerr
);
580 pb
= pppSingleBuf (pb
);
582 if (pb
->len
<= PPPOE_HEADERLEN
) {
583 printf("pppoe (data): dropping too short packet: %d bytes\n", pb
->len
);
587 if (pb
->len
< sizeof(*ph
)) {
588 printf("pppoe_data_input: could not get PPPoE header\n");
591 ph
= (struct pppoehdr
*)pb
->payload
;
593 if (ph
->vertype
!= PPPOE_VERTYPE
) {
594 printf("pppoe (data): unknown version/type packet: 0x%x\n", ph
->vertype
);
601 session
= ntohs(ph
->session
);
602 sc
= pppoe_find_softc_by_session(session
, netif
);
604 #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
605 printf("pppoe: input for unknown session 0x%x, sending PADT\n", session
);
606 pppoe_send_padt(netif
, session
, shost
);
611 plen
= ntohs(ph
->plen
);
613 if (pbuf_header(pb
, -(int)(PPPOE_HEADERLEN
)) != 0) {
615 PPPDEBUG(LOG_ERR
, ("pppoe_data_input: pbuf_header PPPOE_HEADERLEN failed\n"));
616 LINK_STATS_INC(link
.lenerr
);
620 PPPDEBUG(LOG_DEBUG
, ("pppoe_data_input: %c%c%"U16_F
": pkthdr.len=%d, pppoe.len=%d\n",
621 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
,
624 if (pb
->len
< plen
) {
628 pppInProcOverEthernet(sc
->sc_pd
, pb
);
637 pppoe_output(struct pppoe_softc
*sc
, struct pbuf
*pb
)
639 struct eth_hdr
*ethhdr
;
648 ethhdr
= (struct eth_hdr
*)pb
->payload
;
649 etype
= sc
->sc_state
== PPPOE_STATE_SESSION
? ETHTYPE_PPPOE
: ETHTYPE_PPPOEDISC
;
650 ethhdr
->type
= htons(etype
);
651 MEMCPY(ethhdr
->dest
.addr
, sc
->sc_dest
.addr
, sizeof(ethhdr
->dest
.addr
));
652 MEMCPY(ethhdr
->src
.addr
, ((struct eth_addr
*)sc
->sc_ethif
->hwaddr
)->addr
, sizeof(ethhdr
->src
.addr
));
654 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",
655 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
, etype
,
656 sc
->sc_state
, sc
->sc_session
,
657 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],
660 res
= sc
->sc_ethif
->linkoutput(sc
->sc_ethif
, pb
);
668 pppoe_send_padi(struct pppoe_softc
*sc
)
674 int l1
= 0, l2
= 0; /* XXX: gcc */
675 #endif /* PPPOE_TODO */
677 if (sc
->sc_state
>PPPOE_STATE_PADI_SENT
) {
678 PPPDEBUG(LOG_ERR
, ("ERROR: pppoe_send_padi in state %d", sc
->sc_state
));
681 /* calculate length of frame (excluding ethernet header + pppoe header) */
682 len
= 2 + 2 + 2 + 2 + sizeof sc
; /* service name tag is required, host unique is send too */
684 if (sc
->sc_service_name
!= NULL
) {
685 l1
= (int)strlen(sc
->sc_service_name
);
688 if (sc
->sc_concentrator_name
!= NULL
) {
689 l2
= (int)strlen(sc
->sc_concentrator_name
);
692 #endif /* PPPOE_TODO */
693 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
694 sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
<= 0xffff);
696 /* allocate a buffer */
697 pb
= pbuf_alloc(PBUF_LINK
, (u16_t
)(sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
), PBUF_RAM
);
701 LWIP_ASSERT("pb->tot_len == pb->len", pb
->tot_len
== pb
->len
);
703 p
= (u8_t
*)pb
->payload
+ sizeof (struct eth_hdr
);
705 PPPOE_ADD_HEADER(p
, PPPOE_CODE_PADI
, 0, (u16_t
)len
);
706 PPPOE_ADD_16(p
, PPPOE_TAG_SNAME
);
708 if (sc
->sc_service_name
!= NULL
) {
710 MEMCPY(p
, sc
->sc_service_name
, l1
);
713 #endif /* PPPOE_TODO */
718 if (sc
->sc_concentrator_name
!= NULL
) {
719 PPPOE_ADD_16(p
, PPPOE_TAG_ACNAME
);
721 MEMCPY(p
, sc
->sc_concentrator_name
, l2
);
724 #endif /* PPPOE_TODO */
725 PPPOE_ADD_16(p
, PPPOE_TAG_HUNIQUE
);
726 PPPOE_ADD_16(p
, sizeof(sc
));
727 MEMCPY(p
, &sc
, sizeof sc
);
730 return pppoe_output(sc
, pb
);
734 pppoe_timeout(void *arg
)
737 struct pppoe_softc
*sc
= (struct pppoe_softc
*)arg
;
739 PPPDEBUG(LOG_DEBUG
, ("pppoe: %c%c%"U16_F
": timeout\n", sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
));
741 switch (sc
->sc_state
) {
742 case PPPOE_STATE_PADI_SENT
:
744 * We have two basic ways of retrying:
745 * - Quick retry mode: try a few times in short sequence
746 * - Slow retry mode: we already had a connection successfully
747 * established and will try infinitely (without user
749 * We only enter slow retry mode if IFF_LINK1 (aka autodial)
753 /* initialize for quick retry mode */
754 retry_wait
= PPPOE_DISC_TIMEOUT
* (1 + sc
->sc_padi_retried
);
756 sc
->sc_padi_retried
++;
757 if (sc
->sc_padi_retried
>= PPPOE_DISC_MAXPADI
) {
759 if ((sc
->sc_sppp
.pp_if
.if_flags
& IFF_LINK1
) == 0) {
760 /* slow retry mode */
761 retry_wait
= PPPOE_SLOW_RETRY
;
765 pppoe_abort_connect(sc
);
769 if ((err
= pppoe_send_padi(sc
)) != 0) {
770 sc
->sc_padi_retried
--;
771 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
));
773 sys_timeout(retry_wait
, pppoe_timeout
, sc
);
776 case PPPOE_STATE_PADR_SENT
:
777 sc
->sc_padr_retried
++;
778 if (sc
->sc_padr_retried
>= PPPOE_DISC_MAXPADR
) {
779 MEMCPY(&sc
->sc_dest
, ethbroadcast
.addr
, sizeof(sc
->sc_dest
));
780 sc
->sc_state
= PPPOE_STATE_PADI_SENT
;
781 sc
->sc_padr_retried
= 0;
782 if ((err
= pppoe_send_padi(sc
)) != 0) {
783 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
));
785 sys_timeout(PPPOE_DISC_TIMEOUT
* (1 + sc
->sc_padi_retried
), pppoe_timeout
, sc
);
788 if ((err
= pppoe_send_padr(sc
)) != 0) {
789 sc
->sc_padr_retried
--;
790 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
));
792 sys_timeout(PPPOE_DISC_TIMEOUT
* (1 + sc
->sc_padr_retried
), pppoe_timeout
, sc
);
794 case PPPOE_STATE_CLOSING
:
795 pppoe_do_disconnect(sc
);
798 return; /* all done, work in peace */
802 /* Start a connection (i.e. initiate discovery phase) */
804 pppoe_connect(struct pppoe_softc
*sc
)
808 if (sc
->sc_state
!= PPPOE_STATE_INITIAL
) {
813 /* wait PADI if IFF_PASSIVE */
814 if ((sc
->sc_sppp
.pp_if
.if_flags
& IFF_PASSIVE
)) {
818 /* save state, in case we fail to send PADI */
819 sc
->sc_state
= PPPOE_STATE_PADI_SENT
;
820 sc
->sc_padr_retried
= 0;
821 err
= pppoe_send_padi(sc
);
822 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
));
823 sys_timeout(PPPOE_DISC_TIMEOUT
, pppoe_timeout
, sc
);
829 pppoe_disconnect(struct pppoe_softc
*sc
)
831 if (sc
->sc_state
< PPPOE_STATE_SESSION
) {
835 * Do not call pppoe_disconnect here, the upper layer state
836 * machine gets confused by this. We must return from this
837 * function and defer disconnecting to the timeout handler.
839 sc
->sc_state
= PPPOE_STATE_CLOSING
;
840 sys_timeout(20, pppoe_timeout
, sc
);
844 pppoe_do_disconnect(struct pppoe_softc
*sc
)
848 if (sc
->sc_state
< PPPOE_STATE_SESSION
) {
851 PPPDEBUG(LOG_DEBUG
, ("pppoe: %c%c%"U16_F
": disconnecting\n", sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
));
852 err
= pppoe_send_padt(sc
->sc_ethif
, sc
->sc_session
, (const u8_t
*)&sc
->sc_dest
);
856 sc
->sc_state
= PPPOE_STATE_INITIAL
;
857 MEMCPY(&sc
->sc_dest
, ethbroadcast
.addr
, sizeof(sc
->sc_dest
));
858 sc
->sc_ac_cookie_len
= 0;
860 if (sc
->sc_hunique
) {
861 mem_free(sc
->sc_hunique
);
862 sc
->sc_hunique
= NULL
;
864 sc
->sc_hunique_len
= 0;
868 sc
->sc_linkStatusCB(sc
->sc_pd
, 0); /* notify upper layers */
873 /* Connection attempt aborted */
875 pppoe_abort_connect(struct pppoe_softc
*sc
)
877 printf("%c%c%"U16_F
": could not establish connection\n", sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
878 sc
->sc_state
= PPPOE_STATE_CLOSING
;
880 sc
->sc_linkStatusCB(sc
->sc_pd
, 0); /* notify upper layers */
882 /* clear connection state */
883 MEMCPY(&sc
->sc_dest
, ethbroadcast
.addr
, sizeof(sc
->sc_dest
));
884 sc
->sc_state
= PPPOE_STATE_INITIAL
;
887 /* Send a PADR packet */
889 pppoe_send_padr(struct pppoe_softc
*sc
)
895 size_t l1
= 0; /* XXX: gcc */
896 #endif /* PPPOE_TODO */
898 if (sc
->sc_state
!= PPPOE_STATE_PADR_SENT
) {
902 len
= 2 + 2 + 2 + 2 + sizeof(sc
); /* service name, host unique */
904 if (sc
->sc_service_name
!= NULL
) { /* service name tag maybe empty */
905 l1
= strlen(sc
->sc_service_name
);
908 #endif /* PPPOE_TODO */
909 if (sc
->sc_ac_cookie_len
> 0) {
910 len
+= 2 + 2 + sc
->sc_ac_cookie_len
; /* AC cookie */
912 LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
913 sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
<= 0xffff);
914 pb
= pbuf_alloc(PBUF_LINK
, (u16_t
)(sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
), PBUF_RAM
);
918 LWIP_ASSERT("pb->tot_len == pb->len", pb
->tot_len
== pb
->len
);
919 p
= (u8_t
*)pb
->payload
+ sizeof (struct eth_hdr
);
920 PPPOE_ADD_HEADER(p
, PPPOE_CODE_PADR
, 0, len
);
921 PPPOE_ADD_16(p
, PPPOE_TAG_SNAME
);
923 if (sc
->sc_service_name
!= NULL
) {
925 MEMCPY(p
, sc
->sc_service_name
, l1
);
928 #endif /* PPPOE_TODO */
932 if (sc
->sc_ac_cookie_len
> 0) {
933 PPPOE_ADD_16(p
, PPPOE_TAG_ACCOOKIE
);
934 PPPOE_ADD_16(p
, sc
->sc_ac_cookie_len
);
935 MEMCPY(p
, sc
->sc_ac_cookie
, sc
->sc_ac_cookie_len
);
936 p
+= sc
->sc_ac_cookie_len
;
938 PPPOE_ADD_16(p
, PPPOE_TAG_HUNIQUE
);
939 PPPOE_ADD_16(p
, sizeof(sc
));
940 MEMCPY(p
, &sc
, sizeof sc
);
942 return pppoe_output(sc
, pb
);
945 /* send a PADT packet */
947 pppoe_send_padt(struct netif
*outgoing_if
, u_int session
, const u8_t
*dest
)
950 struct eth_hdr
*ethhdr
;
954 pb
= pbuf_alloc(PBUF_LINK
, sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
, PBUF_RAM
);
958 LWIP_ASSERT("pb->tot_len == pb->len", pb
->tot_len
== pb
->len
);
960 ethhdr
= (struct eth_hdr
*)pb
->payload
;
961 ethhdr
->type
= PP_HTONS(ETHTYPE_PPPOEDISC
);
962 MEMCPY(ethhdr
->dest
.addr
, dest
, sizeof(ethhdr
->dest
.addr
));
963 MEMCPY(ethhdr
->src
.addr
, ((struct eth_addr
*)outgoing_if
->hwaddr
)->addr
, sizeof(ethhdr
->src
.addr
));
965 p
= (u8_t
*)(ethhdr
+ 1);
966 PPPOE_ADD_HEADER(p
, PPPOE_CODE_PADT
, session
, 0);
968 res
= outgoing_if
->linkoutput(outgoing_if
, pb
);
977 pppoe_send_pado(struct pppoe_softc
*sc
)
983 if (sc
->sc_state
!= PPPOE_STATE_PADO_SENT
) {
989 /* include ac_cookie */
990 len
+= 2 + 2 + sizeof(sc
);
991 /* include hunique */
992 len
+= 2 + 2 + sc
->sc_hunique_len
;
993 pb
= pbuf_alloc(PBUF_LINK
, sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
, PBUF_RAM
);
997 LWIP_ASSERT("pb->tot_len == pb->len", pb
->tot_len
== pb
->len
);
998 p
= (u8_t
*)pb
->payload
+ sizeof (struct eth_hdr
);
999 PPPOE_ADD_HEADER(p
, PPPOE_CODE_PADO
, 0, len
);
1000 PPPOE_ADD_16(p
, PPPOE_TAG_ACCOOKIE
);
1001 PPPOE_ADD_16(p
, sizeof(sc
));
1002 MEMCPY(p
, &sc
, sizeof(sc
));
1004 PPPOE_ADD_16(p
, PPPOE_TAG_HUNIQUE
);
1005 PPPOE_ADD_16(p
, sc
->sc_hunique_len
);
1006 MEMCPY(p
, sc
->sc_hunique
, sc
->sc_hunique_len
);
1007 return pppoe_output(sc
, pb
);
1011 pppoe_send_pads(struct pppoe_softc
*sc
)
1015 size_t len
, l1
= 0; /* XXX: gcc */
1017 if (sc
->sc_state
!= PPPOE_STATE_PADO_SENT
) {
1021 sc
->sc_session
= mono_time
.tv_sec
% 0xff + 1;
1024 /* include hunique */
1025 len
+= 2 + 2 + 2 + 2 + sc
->sc_hunique_len
; /* service name, host unique*/
1026 if (sc
->sc_service_name
!= NULL
) { /* service name tag maybe empty */
1027 l1
= strlen(sc
->sc_service_name
);
1030 pb
= pbuf_alloc(PBUF_LINK
, sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
+ len
, PBUF_RAM
);
1034 LWIP_ASSERT("pb->tot_len == pb->len", pb
->tot_len
== pb
->len
);
1035 p
= (u8_t
*)pb
->payload
+ sizeof (struct eth_hdr
);
1036 PPPOE_ADD_HEADER(p
, PPPOE_CODE_PADS
, sc
->sc_session
, len
);
1037 PPPOE_ADD_16(p
, PPPOE_TAG_SNAME
);
1038 if (sc
->sc_service_name
!= NULL
) {
1039 PPPOE_ADD_16(p
, l1
);
1040 MEMCPY(p
, sc
->sc_service_name
, l1
);
1045 PPPOE_ADD_16(p
, PPPOE_TAG_HUNIQUE
);
1046 PPPOE_ADD_16(p
, sc
->sc_hunique_len
);
1047 MEMCPY(p
, sc
->sc_hunique
, sc
->sc_hunique_len
);
1048 return pppoe_output(sc
, pb
);
1053 pppoe_xmit(struct pppoe_softc
*sc
, struct pbuf
*pb
)
1058 /* are we ready to process data yet? */
1059 if (sc
->sc_state
< PPPOE_STATE_SESSION
) {
1060 /*sppp_flush(&sc->sc_sppp.pp_if);*/
1067 /* make room for Ethernet header - should not fail */
1068 if (pbuf_header(pb
, sizeof(struct eth_hdr
) + PPPOE_HEADERLEN
) != 0) {
1070 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
));
1071 LINK_STATS_INC(link
.lenerr
);
1076 p
= (u8_t
*)pb
->payload
+ sizeof(struct eth_hdr
);
1077 PPPOE_ADD_HEADER(p
, 0, sc
->sc_session
, len
);
1079 return pppoe_output(sc
, pb
);
1082 #if 0 /*def PFIL_HOOKS*/
1084 pppoe_ifattach_hook(void *arg
, struct pbuf
**mp
, struct netif
*ifp
, int dir
)
1086 struct pppoe_softc
*sc
;
1089 if (mp
!= (struct pbuf
**)PFIL_IFNET_DETACH
) {
1093 LIST_FOREACH(sc
, &pppoe_softc_list
, sc_list
) {
1094 if (sc
->sc_ethif
!= ifp
) {
1097 if (sc
->sc_sppp
.pp_if
.if_flags
& IFF_UP
) {
1098 sc
->sc_sppp
.pp_if
.if_flags
&= ~(IFF_UP
|IFF_RUNNING
);
1099 printf("%c%c%"U16_F
": ethernet interface detached, going down\n",
1100 sc
->sc_ethif
->name
[0], sc
->sc_ethif
->name
[1], sc
->sc_ethif
->num
);
1102 sc
->sc_ethif
= NULL
;
1103 pppoe_clear_softc(sc
, "ethernet interface detached");
1111 pppoe_clear_softc(struct pppoe_softc
*sc
, const char *message
)
1113 LWIP_UNUSED_ARG(message
);
1116 sys_untimeout(pppoe_timeout
, sc
);
1117 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
));
1120 sc
->sc_state
= PPPOE_STATE_INITIAL
;
1122 /* notify upper layers */
1123 sc
->sc_linkStatusCB(sc
->sc_pd
, 0);
1125 /* clean up softc */
1126 MEMCPY(&sc
->sc_dest
, ethbroadcast
.addr
, sizeof(sc
->sc_dest
));
1127 sc
->sc_ac_cookie_len
= 0;
1131 #endif /* PPPOE_SUPPORT */