Add L2 PPP and PPPoE

This commit is contained in:
Sergio R. Caprile committed 2026-04-01 15:10:55 -03:00
1 parent d8a5bc5907
commit 1d4ef08e6a
3 files changed
+133 -35

No files matched your search

+41
View File
@@ -88,6 +88,7 @@ struct pppoe { // RFC-2516, "A Method for Transmitting PPP Over Ethernet
#define MG_PPPoE_PADS 0x65
#define MG_PPPoE_PADT 0xa7
<<<<<<< HEAD
#define MG_PPPoE_ST_DISC 0 // Discovery phase, see what servers are out there
#define MG_PPPoE_ST_REQ 1 // Chose a server, request a session and wait
#define MG_PPPoE_ST_SESS 2 // Session established, PPP traffic is exchanged
@@ -96,6 +97,12 @@ struct pppoe { // RFC-2516, "A Method for Transmitting PPP Over Ethernet
static bool s_link = false; // ************ THESE SHOULD MOVE TO A struct mg_l2data *******************************
static uint8_t s_state = MG_PPPoE_ST_DISC;
=======
#define PDIFF(a, b) ((size_t) (((char *) (b)) - ((char *) (a))))
static bool s_link = false; // *******************************************
static uint8_t s_state = 0;
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
static uint16_t s_id;
void mg_l2_ppp_init(struct mg_tcpip_if *ifp) {
@@ -112,7 +119,12 @@ void mg_l2_pppoe_init(struct mg_tcpip_if *ifp) {
}
bool mg_l2_ppp_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
<<<<<<< HEAD
if (expired_1000ms && ifp->state == MG_TCPIP_STATE_DOWN) s_link = false;
=======
(void) ifp;
(void) expired_1000ms;
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
return s_link;
}
@@ -522,17 +534,26 @@ static size_t pppoe_tx_frame(struct mg_tcpip_if *ifp, uint8_t code, uint16_t id,
}
bool mg_l2_pppoe_poll(struct mg_tcpip_if *ifp, bool expired_1000ms) {
<<<<<<< HEAD
if (expired_1000ms && s_state == MG_PPPoE_ST_DISC &&
ifp->state == MG_TCPIP_STATE_LINK_UP) {
=======
if (expired_1000ms && s_state == 0 && ifp->driver_up) {
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
uint16_t tags[2];
tags[0] = mg_htons(0x0101); // Service Request
tags[1] = mg_htons(0x0000); // Any
pppoe_tx_frame(ifp, MG_PPPoE_PADI, 0, (uint8_t *) tags, sizeof(tags),
mg_l2_eth_mapip(MG_TCPIP_L2ADDR_BCAST, NULL));
MG_DEBUG(("Sent PADI"));
<<<<<<< HEAD
} else if (expired_1000ms && (s_state != MG_PPPoE_ST_SESS ||
ifp->state == MG_TCPIP_STATE_DOWN)) {
s_state = MG_PPPoE_ST_DISC;
=======
} else if (expired_1000ms && (s_state != 2 || !ifp->driver_up)) {
s_state = 0;
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
}
return mg_l2_ppp_poll(ifp, expired_1000ms);
}
@@ -550,8 +571,12 @@ bool mg_l2_pppoe_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
if (pay->len < sizeof(*pppoe)) return false; // Truncated
if (eth_proto == MG_TCPIP_L2PROTO_PPPoE_DISC) {
MG_VERBOSE(("PPPoE_DISC"));
<<<<<<< HEAD
if (s_state == MG_PPPoE_ST_DISC && pppoe->code == MG_PPPoE_PADO &&
pppoe->id == 0) {
=======
if (s_state == 0 && pppoe->code == MG_PPPoE_PADO && pppoe->id == 0) {
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
uint16_t tags[2];
bool has_cookie = false;
size_t len = pay->len - sizeof(*pppoe);
@@ -580,6 +605,7 @@ bool mg_l2_pppoe_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
pppoe_tx_frame(ifp, MG_PPPoE_PADR, 0, p, taglen,
mg_l2_eth_getaddr((uint8_t *) raw->buf));
MG_DEBUG(("Sent PADR"));
<<<<<<< HEAD
s_state = MG_PPPoE_ST_REQ;
} else if (s_state == MG_PPPoE_ST_REQ && pppoe->code == MG_PPPoE_PADS) {
s_id = pppoe->id;
@@ -587,14 +613,29 @@ bool mg_l2_pppoe_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
MG_DEBUG(("PPPoE session 0x%04x started", mg_ntohs(s_id)));
s_state = MG_PPPoE_ST_SESS;
} else if (s_state == MG_PPPoE_ST_SESS && pppoe->code == MG_PPPoE_PADT &&
=======
s_state = 1;
} else if (s_state == 1 && pppoe->code == MG_PPPoE_PADS) {
s_id = pppoe->id;
memcpy(&ifp->gwmac, mg_l2_eth_getaddr((uint8_t *) raw->buf)->addr.mac, 6);
MG_DEBUG(("PPPoE session 0x%04x started", mg_ntohs(s_id)));
s_state = 2;
} else if (s_state == 2 && pppoe->code == MG_PPPoE_PADT &&
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
pppoe->id == s_id) {
MG_ERROR(("Got PADT"));
s_id = 0;
s_link = false;
<<<<<<< HEAD
s_state = MG_PPPoE_ST_DISC;
}
} else if (eth_proto == MG_TCPIP_L2PROTO_PPPoE_SESS &&
s_state == MG_PPPoE_ST_SESS) {
=======
s_state = 0;
}
} else if (eth_proto == MG_TCPIP_L2PROTO_PPPoE_SESS && s_state == 2) {
>>>>>>> 3b7ca757 (Add L2 PPP and PPPoE)
pay->buf = (char *) (pppoe + 1);
pay->len = pay->len - sizeof(*pppoe);
return ppp_rx(ifp, proto, pay, raw);