2022-05-10 06:43:27 +00:00
|
|
|
//
|
|
|
|
// Created by xajhuang on 2022/5/10.
|
|
|
|
//
|
|
|
|
#include <zlog.h>
|
|
|
|
#include "pppoe_session.h"
|
|
|
|
#include "netif/rawif.h"
|
|
|
|
#include "netif/ppp/ppp.h"
|
|
|
|
#include "netif/ppp/pppoe.h"
|
2022-05-30 12:16:50 +00:00
|
|
|
#include "netif/pppoeif.h"
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
static PPPPOE_SESSION g_pSessionList = NULL;
|
|
|
|
|
|
|
|
static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) {
|
2022-05-30 12:16:50 +00:00
|
|
|
struct netif *pppif = ppp_netif(pcb);
|
|
|
|
struct pppoe_softc *sc = (struct pppoe_softc *)pcb->link_ctx_cb;
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
LWIP_UNUSED_ARG(ctx);
|
|
|
|
|
|
|
|
switch (errCode) {
|
|
|
|
case PPPERR_NONE: { /* No error. */
|
2022-05-30 12:16:50 +00:00
|
|
|
dzlog_info("<%p> pppLinkStatusCallback: PPPERR_NONE(%08X), Session: %04X\n",
|
|
|
|
pcb,
|
|
|
|
pcb->lcp_gotoptions.magicnumber,
|
|
|
|
sc->sc_session);
|
2022-05-10 06:43:27 +00:00
|
|
|
#if LWIP_IPV4
|
|
|
|
dzlog_info(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif)));
|
|
|
|
dzlog_info(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif)));
|
|
|
|
dzlog_info(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif)));
|
|
|
|
#endif /* LWIP_IPV4 */
|
|
|
|
#if LWIP_DNS
|
|
|
|
dzlog_info(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0)));
|
|
|
|
dzlog_info(" dns2 = %s\n", ipaddr_ntoa(dns_getserver(1)));
|
|
|
|
#endif /* LWIP_DNS */
|
|
|
|
#if PPP_IPV6_SUPPORT
|
|
|
|
dzlog_info(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
|
|
|
|
#endif /* PPP_IPV6_SUPPORT */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_PARAM: { /* Invalid parameter. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_PARAM\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_OPEN: { /* Unable to open PPP session. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_OPEN\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_DEVICE\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_ALLOC: { /* Unable to allocate resources. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_ALLOC\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_USER: { /* User interrupt. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_USER\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_CONNECT: { /* Connection lost. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_CONNECT\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_AUTHFAIL\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_PROTOCOL: { /* Failed to meet protocol. */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_PROTOCOL\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_PEERDEAD: { /* Connection timeout */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_PEERDEAD\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_IDLETIMEOUT: { /* Idle Timeout */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_IDLETIMEOUT\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_CONNECTTIME: { /* Max connect time reached */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_CONNECTTIME\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PPPERR_LOOPBACK: { /* Loopback detected */
|
|
|
|
printf("<%p> pppLinkStatusCallback: PPPERR_LOOPBACK\n", pcb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
printf("<%p> pppLinkStatusCallback: unknown errCode %d\n", pcb, errCode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 12:16:50 +00:00
|
|
|
static struct netif* g_rawSocketIf = NULL;
|
|
|
|
|
|
|
|
struct netif* get_rawsocket_if(void) {
|
|
|
|
return g_rawSocketIf;
|
|
|
|
}
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
int pppoe_session_init() {
|
|
|
|
PUSER_INFO_LIST pList, pTmp;
|
|
|
|
PUSER_INFO_LIST pUserList = user_info_getall();
|
|
|
|
|
2022-05-30 12:16:50 +00:00
|
|
|
g_rawSocketIf = bind_rawsocket_if("ens160");
|
|
|
|
|
|
|
|
if(g_rawSocketIf) {
|
|
|
|
dzlog_info("Create Raw Socket netif: <%p>\n", (void*)g_rawSocketIf);
|
|
|
|
}
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
HASH_ITER(hh, pUserList, pList, pTmp) {
|
|
|
|
PPPPOE_SESSION pSession = (PPPPOE_SESSION)malloc(sizeof(PPPOE_SESSION));
|
|
|
|
|
|
|
|
if (pSession) {
|
2022-05-30 12:16:50 +00:00
|
|
|
struct netif *ppp_netif;
|
|
|
|
memset(pSession, 0, sizeof(PPPOE_SESSION));
|
|
|
|
|
|
|
|
ppp_netif = (struct netif *)malloc(sizeof(struct netif));
|
2022-05-10 06:43:27 +00:00
|
|
|
|
2022-05-30 12:16:50 +00:00
|
|
|
if (ppp_netif) {
|
|
|
|
struct netif *netif;
|
|
|
|
|
|
|
|
pSession->userid = pList->userid;
|
|
|
|
memcpy(&pSession->user_info, &pList->user_info, sizeof(USER_INFO));
|
|
|
|
|
|
|
|
netif = create_pppoe_if(pSession);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
if (netif == NULL) {
|
|
|
|
free(pSession);
|
|
|
|
free(ppp_netif);
|
2022-05-30 12:16:50 +00:00
|
|
|
dzlog_error("Create PPPoE netif error: %u\n", pList->userid);
|
2022-05-10 06:43:27 +00:00
|
|
|
} else {
|
2022-05-30 12:16:50 +00:00
|
|
|
ppp_pcb *ppp;
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
pSession->nicif = netif;
|
|
|
|
pSession->pppif = ppp_netif;
|
|
|
|
|
|
|
|
ppp = pppoe_create(pSession->pppif, pSession->nicif, NULL, NULL, pppLinkStatusCallback, NULL);
|
|
|
|
|
2022-05-30 12:16:50 +00:00
|
|
|
if (ppp == NULL) {
|
2022-05-10 06:43:27 +00:00
|
|
|
netif_remove(netif);
|
|
|
|
free(pSession);
|
|
|
|
free(ppp_netif);
|
|
|
|
dzlog_error("Create PPPoE netif error: %u\n", pList->userid);
|
|
|
|
} else {
|
2022-05-30 12:16:50 +00:00
|
|
|
|
|
|
|
ppp_set_auth(ppp,
|
|
|
|
PPPAUTHTYPE_ANY,
|
|
|
|
pSession->user_info.pppoe_user,
|
|
|
|
pSession->user_info.pppoe_passwd);
|
2022-05-10 06:43:27 +00:00
|
|
|
ppp_connect(ppp, 0);
|
|
|
|
|
2022-05-30 12:16:50 +00:00
|
|
|
dzlog_info("Create PPPoE netif %p: %u(%c%c:%d, %c%c:%d)\n",
|
|
|
|
ppp,
|
|
|
|
pList->userid,
|
|
|
|
ppp_netif->name[0],
|
|
|
|
ppp_netif->name[1],
|
|
|
|
ppp_netif->num,
|
|
|
|
netif->name[0],
|
|
|
|
netif->name[1],
|
|
|
|
netif->num);
|
2022-05-10 06:43:27 +00:00
|
|
|
HASH_ADD_INT(g_pSessionList, userid, pSession);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
2022-05-27 07:49:45 +00:00
|
|
|
}
|