#include "netif/ppp/ppp_opts.h"
#if PPP_SUPPORT && PPPOE_SUPPORT
#if 0#endif
#include "lwip/timeouts.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "netif/ethernet.h"
#include "netif/ppp/ppp_impl.h"
#include "netif/ppp/lcp.h"
#include "netif/ppp/ipcp.h"
#include "netif/ppp/pppoe.h"
LWIP_MEMPOOL_DECLARE(PPPOE_IF, MEMP_NUM_PPPOE_INTERFACES, sizeof(struct pppoe_softc), "PPPOE_IF")
#define PPPOE_ADD_16(PTR, VAL) \
*(PTR)++ = (u8_t)((VAL) / 256); \
*(PTR)++ = (u8_t)((VAL) % 256)
#define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
*(PTR)++ = PPPOE_VERTYPE; \
*(PTR)++ = (CODE); \
PPPOE_ADD_16(PTR, SESS); \
PPPOE_ADD_16(PTR, LEN)
#define PPPOE_DISC_TIMEOUT (5*1000)
#define PPPOE_SLOW_RETRY (60*1000)
#define PPPOE_DISC_MAXPADI 4
#define PPPOE_DISC_MAXPADR 2
#ifdef PPPOE_SERVER
#error "PPPOE_SERVER is not yet supported under lwIP!"
#define IFF_PASSIVE IFF_LINK0
#endif
#define PPPOE_ERRORSTRING_LEN 64
static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p);
static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol);
static void pppoe_connect(ppp_pcb *ppp, void *ctx);
static void pppoe_disconnect(ppp_pcb *ppp, void *ctx);
static err_t pppoe_destroy(ppp_pcb *ppp, void *ctx);
static void pppoe_abort_connect(struct pppoe_softc *);
#if 0#endif
static void pppoe_timeout(void *);
static err_t pppoe_send_padi(struct pppoe_softc *);
static err_t pppoe_send_padr(struct pppoe_softc *);
#ifdef PPPOE_SERVER
static err_t pppoe_send_pado(struct pppoe_softc *);
static err_t pppoe_send_pads(struct pppoe_softc *);
#endif
static err_t pppoe_send_padt(struct netif *, u_int, const u8_t *);
static err_t pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb);
static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif);
static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif);
static struct pppoe_softc *pppoe_softc_list;
static const struct link_callbacks pppoe_callbacks = {
pppoe_connect,
#if PPP_SERVER
NULL,
#endif
pppoe_disconnect,
pppoe_destroy,
pppoe_write,
pppoe_netif_output,
NULL,
NULL
};
ppp_pcb *pppoe_create(struct netif *pppif,
struct netif *ethif,
const char *service_name, const char *concentrator_name,
ppp_link_status_cb_fn link_status_cb, void *ctx_cb)
{
ppp_pcb *ppp;
struct pppoe_softc *sc;
#if PPPOE_SCNAME_SUPPORT
size_t l;
#else
LWIP_UNUSED_ARG(service_name);
LWIP_UNUSED_ARG(concentrator_name);
#endif
LWIP_ASSERT_CORE_LOCKED();
#if PPPOE_SCNAME_SUPPORT
l = strlen(service_name);
if (l > 1024) {
return NULL;
}
l = strlen(concentrator_name);
if (l > 1024) {
return NULL;
}
#endif
sc = (struct pppoe_softc *)LWIP_MEMPOOL_ALLOC(PPPOE_IF);
if (sc == NULL) {
return NULL;
}
ppp = ppp_new(pppif, &pppoe_callbacks, sc, link_status_cb, ctx_cb);
if (ppp == NULL) {
LWIP_MEMPOOL_FREE(PPPOE_IF, sc);
return NULL;
}
memset(sc, 0, sizeof(struct pppoe_softc));
sc->pcb = ppp;
sc->sc_ethif = ethif;
#if PPPOE_SCNAME_SUPPORT
sc->sc_service_name = service_name;
sc->sc_concentrator_name = concentrator_name;
#endif
sc->next = pppoe_softc_list;
pppoe_softc_list = sc;
return ppp;
}
static err_t pppoe_write(ppp_pcb *ppp, void *ctx, struct pbuf *p) {
struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
struct pbuf *ph;
err_t ret;
#if MIB2_STATS
u16_t tot_len;
#else
LWIP_UNUSED_ARG(ppp);
#endif
pbuf_remove_header(p, 2);
ph = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
if(!ph) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.proterr);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
pbuf_free(p);
return ERR_MEM;
}
pbuf_remove_header(ph, PPPOE_HEADERLEN);
pbuf_cat(ph, p);
#if MIB2_STATS
tot_len = ph->tot_len;
#endif
ret = pppoe_xmit(sc, ph);
if (ret != ERR_OK) {
LINK_STATS_INC(link.err);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
return ret;
}
MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, (u16_t)tot_len);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
LINK_STATS_INC(link.xmit);
return ERR_OK;
}
static err_t pppoe_netif_output(ppp_pcb *ppp, void *ctx, struct pbuf *p, u_short protocol) {
struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
struct pbuf *pb;
u8_t *pl;
err_t err;
#if MIB2_STATS
u16_t tot_len;
#else
LWIP_UNUSED_ARG(ppp);
#endif
pb = pbuf_alloc(PBUF_LINK, PPPOE_HEADERLEN + sizeof(protocol), PBUF_RAM);
if(!pb) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.proterr);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
return ERR_MEM;
}
pbuf_remove_header(pb, PPPOE_HEADERLEN);
pl = (u8_t*)pb->payload;
PUTSHORT(protocol, pl);
pbuf_chain(pb, p);
#if MIB2_STATS
tot_len = pb->tot_len;
#endif
if( (err = pppoe_xmit(sc, pb)) != ERR_OK) {
LINK_STATS_INC(link.err);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutdiscards);
return err;
}
MIB2_STATS_NETIF_ADD(ppp->netif, ifoutoctets, tot_len);
MIB2_STATS_NETIF_INC(ppp->netif, ifoutucastpkts);
LINK_STATS_INC(link.xmit);
return ERR_OK;
}
static err_t
pppoe_destroy(ppp_pcb *ppp, void *ctx)
{
struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
struct pppoe_softc **copp, *freep;
LWIP_UNUSED_ARG(ppp);
sys_untimeout(pppoe_timeout, sc);
for (copp = &pppoe_softc_list; (freep = *copp); copp = &freep->next) {
if (freep == sc) {
*copp = freep->next;
break;
}
}
LWIP_MEMPOOL_FREE(PPPOE_IF, sc);
return ERR_OK;
}
static struct pppoe_softc* pppoe_find_softc_by_session(u_int session, struct netif *rcvif) {
struct pppoe_softc *sc;
for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
if (sc->sc_state == PPPOE_STATE_SESSION
&& sc->sc_session == session
&& sc->sc_ethif == rcvif) {
return sc;
}
}
return NULL;
}
static struct pppoe_softc* pppoe_find_softc_by_hunique(u8_t *token, size_t len, struct netif *rcvif) {
struct pppoe_softc *sc, *t;
if (len != sizeof sc) {
return NULL;
}
MEMCPY(&t, token, len);
for (sc = pppoe_softc_list; sc != NULL; sc = sc->next) {
if (sc == t) {
break;
}
}
if (sc == NULL) {
PPPDEBUG(LOG_DEBUG, ("pppoe: alien host unique tag, no session found\n"));
return NULL;
}
if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": host unique tag found, but it belongs to a connection in state %d\n",
sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, sc->sc_state));
return NULL;
}
if (sc->sc_ethif != rcvif) {
PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": wrong interface, not accepting host unique\n",
sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
return NULL;
}
return sc;
}
void
pppoe_disc_input(struct netif *netif, struct pbuf *pb)
{
u16_t tag, len, off;
u16_t session, plen;
struct pppoe_softc *sc;
#if PPP_DEBUG
const char *err_msg = NULL;
#endif
u8_t *ac_cookie;
u16_t ac_cookie_len;
#ifdef PPPOE_SERVER
u8_t *hunique;
size_t hunique_len;
#endif
struct pppoehdr *ph;
struct pppoetag pt;
int err;
struct eth_hdr *ethhdr;
if (pppoe_softc_list == NULL) {
pbuf_free(pb);
return;
}
pb = pbuf_coalesce(pb, PBUF_RAW);
if (pb->next != NULL) {
PPPDEBUG(LOG_DEBUG, ("pppoe: pbuf_coalesce failed: %d\n", pb->tot_len));
goto done;
}
ethhdr = (struct eth_hdr *)pb->payload;
ac_cookie = NULL;
ac_cookie_len = 0;
#ifdef PPPOE_SERVER
hunique = NULL;
hunique_len = 0;
#endif
session = 0;
off = sizeof(struct eth_hdr) + sizeof(struct pppoehdr);
if (pb->len < off) {
PPPDEBUG(LOG_DEBUG, ("pppoe: packet too short: %d\n", pb->len));
goto done;
}
ph = (struct pppoehdr *) (ethhdr + 1);
if (ph->vertype != PPPOE_VERTYPE) {
PPPDEBUG(LOG_DEBUG, ("pppoe: unknown version/type packet: 0x%x\n", ph->vertype));
goto done;
}
session = lwip_ntohs(ph->session);
plen = lwip_ntohs(ph->plen);
if (plen > (pb->len - off)) {
PPPDEBUG(LOG_DEBUG, ("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
pb->len - off, plen));
goto done;
}
if(pb->tot_len == pb->len) {
u16_t framelen = off + plen;
if (framelen < pb->len) {
pb->tot_len = pb->len = framelen;
}
}
tag = 0;
len = 0;
sc = NULL;
while (off + sizeof(pt) <= pb->len) {
MEMCPY(&pt, (u8_t*)pb->payload + off, sizeof(pt));
tag = lwip_ntohs(pt.tag);
len = lwip_ntohs(pt.len);
if (off + sizeof(pt) + len > pb->len) {
PPPDEBUG(LOG_DEBUG, ("pppoe: tag 0x%x len 0x%x is too long\n", tag, len));
goto done;
}
switch (tag) {
case PPPOE_TAG_EOL:
goto breakbreak;
case PPPOE_TAG_SNAME:
break;
case PPPOE_TAG_ACNAME:
break;
case PPPOE_TAG_HUNIQUE:
if (sc != NULL) {
break;
}
#ifdef PPPOE_SERVER
hunique = (u8_t*)pb->payload + off + sizeof(pt);
hunique_len = len;
#endif
sc = pppoe_find_softc_by_hunique((u8_t*)pb->payload + off + sizeof(pt), len, netif);
break;
case PPPOE_TAG_ACCOOKIE:
if (ac_cookie == NULL) {
if (len > PPPOE_MAX_AC_COOKIE_LEN) {
PPPDEBUG(LOG_DEBUG, ("pppoe: AC cookie is too long: len = %d, max = %d\n", len, PPPOE_MAX_AC_COOKIE_LEN));
goto done;
}
ac_cookie = (u8_t*)pb->payload + off + sizeof(pt);
ac_cookie_len = len;
}
break;
#if PPP_DEBUG
case PPPOE_TAG_SNAME_ERR:
err_msg = "SERVICE NAME ERROR";
break;
case PPPOE_TAG_ACSYS_ERR:
err_msg = "AC SYSTEM ERROR";
break;
case PPPOE_TAG_GENERIC_ERR:
err_msg = "GENERIC ERROR";
break;
#endif
default:
break;
}
#if PPP_DEBUG
if (err_msg != NULL) {
char error_tmp[PPPOE_ERRORSTRING_LEN];
u16_t error_len = LWIP_MIN(len, sizeof(error_tmp)-1);
strncpy(error_tmp, (char*)pb->payload + off + sizeof(pt), error_len);
error_tmp[error_len] = '\0';
if (sc) {
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));
} else {
PPPDEBUG(LOG_DEBUG, ("pppoe: %s: %s\n", err_msg, error_tmp));
}
}
#endif
off += sizeof(pt) + len;
}
breakbreak:;
switch (ph->code) {
case PPPOE_CODE_PADI:
#ifdef PPPOE_SERVER
if (LIST_EMPTY(&pppoe_softc_list)) {
goto done;
}
LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP)) {
continue;
}
if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
continue;
}
if (sc->sc_state == PPPOE_STATE_INITIAL) {
break;
}
}
if (sc == NULL) {
goto done;
}
if (hunique) {
if (sc->sc_hunique) {
mem_free(sc->sc_hunique);
}
sc->sc_hunique = mem_malloc(hunique_len);
if (sc->sc_hunique == NULL) {
goto done;
}
sc->sc_hunique_len = hunique_len;
MEMCPY(sc->sc_hunique, hunique, hunique_len);
}
MEMCPY(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
sc->sc_state = PPPOE_STATE_PADO_SENT;
pppoe_send_pado(sc);
break;
#endif
case PPPOE_CODE_PADR:
#ifdef PPPOE_SERVER
if (ac_cookie == NULL) {
PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but not includes ac_cookie\n"));
goto done;
}
sc = pppoe_find_softc_by_hunique(ac_cookie, ac_cookie_len, netif);
if (sc == NULL) {
if (!LIST_EMPTY(&pppoe_softc_list)) {
PPPDEBUG(LOG_DEBUG, ("pppoe: received PADR but could not find request for it\n"));
}
goto done;
}
if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
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));
goto done;
}
if (hunique) {
if (sc->sc_hunique) {
mem_free(sc->sc_hunique);
}
sc->sc_hunique = mem_malloc(hunique_len);
if (sc->sc_hunique == NULL) {
goto done;
}
sc->sc_hunique_len = hunique_len;
MEMCPY(sc->sc_hunique, hunique, hunique_len);
}
pppoe_send_pads(sc);
sc->sc_state = PPPOE_STATE_SESSION;
ppp_start(sc->pcb);
break;
#else
goto done;
#endif
case PPPOE_CODE_PADO:
if (sc == NULL) {
if (pppoe_softc_list != NULL) {
PPPDEBUG(LOG_DEBUG, ("pppoe: received PADO but could not find request for it\n"));
}
goto done;
}
if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
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));
goto done;
}
if (ac_cookie) {
sc->sc_ac_cookie_len = ac_cookie_len;
MEMCPY(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
}
MEMCPY(&sc->sc_dest, ethhdr->src.addr, sizeof(sc->sc_dest.addr));
sys_untimeout(pppoe_timeout, sc);
sc->sc_padr_retried = 0;
sc->sc_state = PPPOE_STATE_PADR_SENT;
if ((err = pppoe_send_padr(sc)) != 0) {
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));
LWIP_UNUSED_ARG(err);
}
sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
break;
case PPPOE_CODE_PADS:
if (sc == NULL) {
goto done;
}
sc->sc_session = session;
sys_untimeout(pppoe_timeout, sc);
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));
sc->sc_state = PPPOE_STATE_SESSION;
ppp_start(sc->pcb);
break;
case PPPOE_CODE_PADT:
#if 0#endif
break;
default:
if(sc) {
PPPDEBUG(LOG_DEBUG, ("%c%c%"U16_F": unknown code (0x%"X16_F") session = 0x%"X16_F"\n",
sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
(u16_t)ph->code, session));
} else {
PPPDEBUG(LOG_DEBUG, ("pppoe: unknown code (0x%"X16_F") session = 0x%"X16_F"\n", (u16_t)ph->code, session));
}
break;
}
done:
pbuf_free(pb);
return;
}
void
pppoe_data_input(struct netif *netif, struct pbuf *pb)
{
u16_t session, plen;
struct pppoe_softc *sc;
struct pppoehdr *ph;
#ifdef PPPOE_TERM_UNKNOWN_SESSIONS
u8_t shost[ETHER_ADDR_LEN];
#endif
#ifdef PPPOE_TERM_UNKNOWN_SESSIONS
MEMCPY(shost, ((struct eth_hdr *)pb->payload)->src.addr, sizeof(shost));
#endif
if (pbuf_remove_header(pb, sizeof(struct eth_hdr)) != 0) {
PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_remove_header failed\n"));
LINK_STATS_INC(link.lenerr);
goto drop;
}
if (pb->len < sizeof(*ph)) {
PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: could not get PPPoE header\n"));
goto drop;
}
ph = (struct pppoehdr *)pb->payload;
if (ph->vertype != PPPOE_VERTYPE) {
PPPDEBUG(LOG_DEBUG, ("pppoe (data): unknown version/type packet: 0x%x\n", ph->vertype));
goto drop;
}
if (ph->code != 0) {
goto drop;
}
session = lwip_ntohs(ph->session);
sc = pppoe_find_softc_by_session(session, netif);
if (sc == NULL) {
#ifdef PPPOE_TERM_UNKNOWN_SESSIONS
PPPDEBUG(LOG_DEBUG, ("pppoe: input for unknown session 0x%x, sending PADT\n", session));
pppoe_send_padt(netif, session, shost);
#endif
goto drop;
}
plen = lwip_ntohs(ph->plen);
if (pbuf_remove_header(pb, PPPOE_HEADERLEN) != 0) {
PPPDEBUG(LOG_ERR, ("pppoe_data_input: pbuf_remove_header PPPOE_HEADERLEN failed\n"));
LINK_STATS_INC(link.lenerr);
goto drop;
}
PPPDEBUG(LOG_DEBUG, ("pppoe_data_input: %c%c%"U16_F": pkthdr.len=%d, pppoe.len=%d\n",
sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num,
pb->len, plen));
if (pb->tot_len < plen) {
goto drop;
}
ppp_input(sc->pcb, pb);
return;
drop:
pbuf_free(pb);
}
static err_t
pppoe_output(struct pppoe_softc *sc, struct pbuf *pb)
{
struct eth_hdr *ethhdr;
u16_t etype;
err_t res;
if (pbuf_add_header(pb, sizeof(struct eth_hdr)) != 0) {
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));
LINK_STATS_INC(link.lenerr);
pbuf_free(pb);
return ERR_BUF;
}
ethhdr = (struct eth_hdr *)pb->payload;
etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHTYPE_PPPOE : ETHTYPE_PPPOEDISC;
ethhdr->type = lwip_htons(etype);
MEMCPY(ðhdr->dest.addr, &sc->sc_dest.addr, sizeof(ethhdr->dest.addr));
MEMCPY(ðhdr->src.addr, &sc->sc_ethif->hwaddr, sizeof(ethhdr->src.addr));
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",
sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num, etype,
sc->sc_state, sc->sc_session,
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],
pb->tot_len));
res = sc->sc_ethif->linkoutput(sc->sc_ethif, pb);
pbuf_free(pb);
return res;
}
static err_t
pppoe_send_padi(struct pppoe_softc *sc)
{
struct pbuf *pb;
u8_t *p;
size_t len;
#if PPPOE_SCNAME_SUPPORT
size_t l1 = 0, l2 = 0;
#endif
len = 2 + 2 + 2 + 2 + sizeof sc;
#if PPPOE_SCNAME_SUPPORT
if (sc->sc_service_name != NULL) {
l1 = strlen(sc->sc_service_name);
len += l1;
}
if (sc->sc_concentrator_name != NULL) {
l2 = strlen(sc->sc_concentrator_name);
len += 2 + 2 + l2;
}
#endif
LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, (u16_t)len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
#if PPPOE_SCNAME_SUPPORT
if (sc->sc_service_name != NULL) {
PPPOE_ADD_16(p, l1);
MEMCPY(p, sc->sc_service_name, l1);
p += l1;
} else
#endif
{
PPPOE_ADD_16(p, 0);
}
#if PPPOE_SCNAME_SUPPORT
if (sc->sc_concentrator_name != NULL) {
PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
PPPOE_ADD_16(p, l2);
MEMCPY(p, sc->sc_concentrator_name, l2);
p += l2;
}
#endif
PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
PPPOE_ADD_16(p, sizeof(sc));
MEMCPY(p, &sc, sizeof sc);
return pppoe_output(sc, pb);
}
static void
pppoe_timeout(void *arg)
{
u32_t retry_wait;
int err;
struct pppoe_softc *sc = (struct pppoe_softc*)arg;
PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": timeout\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
switch (sc->sc_state) {
case PPPOE_STATE_PADI_SENT:
if (sc->sc_padi_retried < 0xff) {
sc->sc_padi_retried++;
}
if (!sc->pcb->settings.persist && sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
#if 0#endif
{
pppoe_abort_connect(sc);
return;
}
}
retry_wait = LWIP_MIN(PPPOE_DISC_TIMEOUT * sc->sc_padi_retried, PPPOE_SLOW_RETRY);
if ((err = pppoe_send_padi(sc)) != 0) {
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));
LWIP_UNUSED_ARG(err);
}
sys_timeout(retry_wait, pppoe_timeout, sc);
break;
case PPPOE_STATE_PADR_SENT:
sc->sc_padr_retried++;
if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
sc->sc_state = PPPOE_STATE_PADI_SENT;
sc->sc_padr_retried = 0;
if ((err = pppoe_send_padi(sc)) != 0) {
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));
LWIP_UNUSED_ARG(err);
}
sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried), pppoe_timeout, sc);
return;
}
if ((err = pppoe_send_padr(sc)) != 0) {
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));
LWIP_UNUSED_ARG(err);
}
sys_timeout(PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried), pppoe_timeout, sc);
break;
default:
return;
}
}
static void
pppoe_connect(ppp_pcb *ppp, void *ctx)
{
err_t err;
struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
lcp_options *lcp_wo;
lcp_options *lcp_ao;
#if PPP_IPV4_SUPPORT && VJ_SUPPORT
ipcp_options *ipcp_wo;
ipcp_options *ipcp_ao;
#endif
sc->sc_session = 0;
sc->sc_ac_cookie_len = 0;
sc->sc_padi_retried = 0;
sc->sc_padr_retried = 0;
MEMCPY(&sc->sc_dest, ethbroadcast.addr, sizeof(sc->sc_dest));
#ifdef PPPOE_SERVER
if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE)) {
return 0;
}
#endif
lcp_wo = &ppp->lcp_wantoptions;
lcp_wo->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2;
lcp_wo->neg_asyncmap = 0;
lcp_wo->neg_pcompression = 0;
lcp_wo->neg_accompression = 0;
lcp_wo->passive = 0;
lcp_wo->silent = 0;
lcp_ao = &ppp->lcp_allowoptions;
lcp_ao->mru = sc->sc_ethif->mtu-PPPOE_HEADERLEN-2;
lcp_ao->neg_asyncmap = 0;
lcp_ao->neg_pcompression = 0;
lcp_ao->neg_accompression = 0;
#if PPP_IPV4_SUPPORT && VJ_SUPPORT
ipcp_wo = &ppp->ipcp_wantoptions;
ipcp_wo->neg_vj = 0;
ipcp_wo->old_vj = 0;
ipcp_ao = &ppp->ipcp_allowoptions;
ipcp_ao->neg_vj = 0;
ipcp_ao->old_vj = 0;
#endif
sc->sc_state = PPPOE_STATE_PADI_SENT;
if ((err = pppoe_send_padi(sc)) != 0) {
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));
}
sys_timeout(PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
}
static void
pppoe_disconnect(ppp_pcb *ppp, void *ctx)
{
struct pppoe_softc *sc = (struct pppoe_softc *)ctx;
PPPDEBUG(LOG_DEBUG, ("pppoe: %c%c%"U16_F": disconnecting\n", sc->sc_ethif->name[0], sc->sc_ethif->name[1], sc->sc_ethif->num));
if (sc->sc_state == PPPOE_STATE_SESSION) {
pppoe_send_padt(sc->sc_ethif, sc->sc_session, (const u8_t *)&sc->sc_dest);
}
sys_untimeout(pppoe_timeout, sc);
sc->sc_state = PPPOE_STATE_INITIAL;
#ifdef PPPOE_SERVER
if (sc->sc_hunique) {
mem_free(sc->sc_hunique);
sc->sc_hunique = NULL;
}
sc->sc_hunique_len = 0;
#endif
ppp_link_end(ppp);
return;
}
static void
pppoe_abort_connect(struct pppoe_softc *sc)
{
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));
sc->sc_state = PPPOE_STATE_INITIAL;
ppp_link_failed(sc->pcb);
}
static err_t
pppoe_send_padr(struct pppoe_softc *sc)
{
struct pbuf *pb;
u8_t *p;
size_t len;
#if PPPOE_SCNAME_SUPPORT
size_t l1 = 0;
#endif
len = 2 + 2 + 2 + 2 + sizeof(sc);
#if PPPOE_SCNAME_SUPPORT
if (sc->sc_service_name != NULL) {
l1 = strlen(sc->sc_service_name);
len += l1;
}
#endif
if (sc->sc_ac_cookie_len > 0) {
len += 2 + 2 + sc->sc_ac_cookie_len;
}
LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
#if PPPOE_SCNAME_SUPPORT
if (sc->sc_service_name != NULL) {
PPPOE_ADD_16(p, l1);
MEMCPY(p, sc->sc_service_name, l1);
p += l1;
} else
#endif
{
PPPOE_ADD_16(p, 0);
}
if (sc->sc_ac_cookie_len > 0) {
PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
MEMCPY(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
p += sc->sc_ac_cookie_len;
}
PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
PPPOE_ADD_16(p, sizeof(sc));
MEMCPY(p, &sc, sizeof sc);
return pppoe_output(sc, pb);
}
static err_t
pppoe_send_padt(struct netif *outgoing_if, u_int session, const u8_t *dest)
{
struct pbuf *pb;
struct eth_hdr *ethhdr;
err_t res;
u8_t *p;
pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN), PBUF_RAM);
if (!pb) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
if (pbuf_add_header(pb, sizeof(struct eth_hdr))) {
PPPDEBUG(LOG_ERR, ("pppoe: pppoe_send_padt: could not allocate room for PPPoE header\n"));
LINK_STATS_INC(link.lenerr);
pbuf_free(pb);
return ERR_BUF;
}
ethhdr = (struct eth_hdr *)pb->payload;
ethhdr->type = PP_HTONS(ETHTYPE_PPPOEDISC);
MEMCPY(ðhdr->dest.addr, dest, sizeof(ethhdr->dest.addr));
MEMCPY(ðhdr->src.addr, &outgoing_if->hwaddr, sizeof(ethhdr->src.addr));
p = (u8_t*)(ethhdr + 1);
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
res = outgoing_if->linkoutput(outgoing_if, pb);
pbuf_free(pb);
return res;
}
#ifdef PPPOE_SERVER
static err_t
pppoe_send_pado(struct pppoe_softc *sc)
{
struct pbuf *pb;
u8_t *p;
size_t len;
len = 0;
len += 2 + 2 + sizeof(sc);
len += 2 + 2 + sc->sc_hunique_len;
pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
PPPOE_ADD_16(p, sizeof(sc));
MEMCPY(p, &sc, sizeof(sc));
p += sizeof(sc);
PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
PPPOE_ADD_16(p, sc->sc_hunique_len);
MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
return pppoe_output(sc, pb);
}
static err_t
pppoe_send_pads(struct pppoe_softc *sc)
{
struct pbuf *pb;
u8_t *p;
size_t len, l1 = 0;
sc->sc_session = mono_time.tv_sec % 0xff + 1;
len = 0;
len += 2 + 2 + 2 + 2 + sc->sc_hunique_len;
if (sc->sc_service_name != NULL) {
l1 = strlen(sc->sc_service_name);
len += l1;
}
pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
if (sc->sc_service_name != NULL) {
PPPOE_ADD_16(p, l1);
MEMCPY(p, sc->sc_service_name, l1);
p += l1;
} else {
PPPOE_ADD_16(p, 0);
}
PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
PPPOE_ADD_16(p, sc->sc_hunique_len);
MEMCPY(p, sc->sc_hunique, sc->sc_hunique_len);
return pppoe_output(sc, pb);
}
#endif
static err_t
pppoe_xmit(struct pppoe_softc *sc, struct pbuf *pb)
{
u8_t *p;
size_t len;
len = pb->tot_len;
if (pbuf_add_header(pb, PPPOE_HEADERLEN) != 0) {
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));
LINK_STATS_INC(link.lenerr);
pbuf_free(pb);
return ERR_BUF;
}
p = (u8_t*)pb->payload;
PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
return pppoe_output(sc, pb);
}
#if 0#endif
#if 0#endif
#endif