REM:
1. 增加PPPoE netif 接口
2. 重构RawSocket netif接口收发数据功能
This commit is contained in:
huangxin 2022-05-30 20:16:50 +08:00
parent 92b9277abc
commit da396530d6
10 changed files with 382 additions and 37 deletions

View File

@ -23,6 +23,7 @@ typedef struct {
} PPPOE_SESSION, *PPPPOE_SESSION;
int pppoe_session_init();
struct netif* get_rawsocket_if(void);
#ifdef __cplusplus
}
#endif

View File

@ -25,6 +25,8 @@ message("INCLUDE: " ${LWIP_INCLUDE_DIRS})
include(${LWIP_LINUX_DIR}/Filelists.cmake)
include_directories(${LWIP_DIR}/../libs/include ${LWIP_DIR}/../include)
add_library(${LWIP_LIB_NAME} ${lwipnoapps_SRCS} ${lwipcontribportunix_SRCS} ${lwipcontribportunixnetifs_SRCS})
target_compile_options(${LWIP_LIB_NAME} PRIVATE ${LWIP_COMPILER_FLAGS})
target_compile_definitions(${LWIP_LIB_NAME} PRIVATE ${LWIP_DEFINITIONS} ${LWIP_MBEDTLS_DEFINITIONS})

View File

@ -16,6 +16,7 @@ set(lwipcontribportunix_SRCS
set(lwipcontribportunixnetifs_SRCS
${LWIP_DIR}/src/arch_linux/netif/rawif.c
${LWIP_DIR}/src/arch_linux/netif/sio.c
${LWIP_DIR}/src/arch_linux/netif/pppoe_if.c
)
if (FALSE)

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#ifndef LWIP_PPPOEIF_H
#define LWIP_PPPOEIF_H
#include "lwip/netif.h"
#include "../../../../../include/pppoe_session.h"
#ifdef __cplusplus
extern "C" {
#endif
err_t pppoeif_init(struct netif *netif);
struct netif *create_pppoe_if(PPPPOE_SESSION pSession);
#ifdef __cplusplus
}
#endif
#endif /* LWIP_TAPIF_H */

View File

@ -39,7 +39,8 @@ extern "C" {
#endif
err_t rawif_init(struct netif *netif);
void rawif_poll(struct netif *netif);
struct netif *bind_rawsocket_if(const char *eth_name, unsigned char haddr[6]);
err_t rawif_output(struct netif *netif, struct pbuf *p);
struct netif *bind_rawsocket_if(const char *eth_name);
#if NO_SYS
int rawif_select(struct netif *netif);
#endif /* NO_SYS */

View File

@ -0,0 +1,225 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#include <stdio.h>
#include <string.h>
#include <zlog.h>
#include "lwip/opt.h"
#include "lwip/tcpip.h"
#include "lwip/debug.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/snmp.h"
#include "lwip/pbuf.h"
#include "netif/etharp.h"
#include "netif/pppoeif.h"
#include "netif/rawif.h"
#include "pppoe_session.h"
#if defined(LWIP_UNIX_LINUX)
/* Define those to better describe your network interface. */
#define IFNAME0 'p'
#define IFNAME1 'e'
#ifndef PPPOEIF_DEBUG
#define PPPOEIF_DEBUG LWIP_DBG_OFF
#endif
#define DEFAULT_GW_IPADDR (0xC0A80001)
static unsigned int g_localIpAddrBegin = DEFAULT_GW_IPADDR + 1;
/*-----------------------------------------------------------------------------------*/
#if NO_SYS
int pppoeif_select(struct netif *netif) {
fd_set fdset;
int ret;
struct timeval tv;
struct pppoeif *pppoeif;
u32_t msecs = sys_timeouts_sleeptime();
pppoeif = (struct pppoeif *)netif->state;
tv.tv_sec = msecs / 1000;
tv.tv_usec = (msecs % 1000) * 1000;
FD_ZERO(&fdset);
FD_SET(pppoeif->fd, &fdset);
ret = select(pppoeif->fd + 1, &fdset, NULL, NULL, &tv);
if (ret > 0) {
pppoeif_input(netif);
}
return ret;
}
#else /* NO_SYS */
#endif /* NO_SYS */
/*-----------------------------------------------------------------------------------*/
static void low_level_init(struct netif *netif) {
PPPPOE_SESSION pSession = (PPPPOE_SESSION)netif->state;
LWIP_DEBUGF(PPPOEIF_DEBUG, ("<%p>pppoeif_init\n", (void *)pSession));
/* Obtain MAC address from network interface. */
netif->hwaddr[0] = pSession->user_info.mac_addr[0];
netif->hwaddr[1] = pSession->user_info.mac_addr[1];
netif->hwaddr[2] = pSession->user_info.mac_addr[2];
netif->hwaddr[3] = pSession->user_info.mac_addr[3];
netif->hwaddr[4] = pSession->user_info.mac_addr[4];
netif->hwaddr[5] = pSession->user_info.mac_addr[5];
netif->hwaddr_len = 6;
/* device capabilities */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
netif_set_link_up(netif);
}
/*-----------------------------------------------------------------------------------*/
/*
* low_level_output():
*
* Should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
*/
/*-----------------------------------------------------------------------------------*/
static err_t low_level_output(struct netif *netif, struct pbuf *p) {
LWIP_UNUSED_ARG(netif);
struct netif *rs_if = get_rawsocket_if();
return rawif_output(rs_if, p);
}
/*-----------------------------------------------------------------------------------*/
/*
* pppoeif_init():
*
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
*/
/*-----------------------------------------------------------------------------------*/
err_t pppoeif_init(struct netif *netif) {
netif->name[0] = 'p';
netif->name[1] = 'e';
#if LWIP_IPV4
netif->output = etharp_output;
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output;
netif->mtu = 1500;
low_level_init(netif);
return ERR_OK;
}
#if LWIP_NETIF_STATUS_CALLBACK
static void status_callback(struct netif *state_netif) {
if (netif_is_up(state_netif)) {
#if LWIP_IPV4
dzlog_info("status_callback==UP, local interface IP is %s\n", ip4addr_ntoa(netif_ip4_addr(state_netif)));
#else
printf("status_callback==UP\n");
#endif
} else {
dzlog_error("status_callback==DOWN\n");
}
}
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
static void link_callback(struct netif *state_netif) {
if (netif_is_link_up(state_netif)) {
dzlog_info("link_callback==UP\n");
} else {
dzlog_error("link_callback==DOWN\n");
}
}
#endif /* LWIP_NETIF_LINK_CALLBACK */
struct netif *create_pppoe_if(PPPPOE_SESSION pSession) {
struct netif *netif;
ip4_addr_t ipaddr, netmask, gw;
ip4_addr_set_zero(&gw);
ip4_addr_set_zero(&ipaddr);
ip4_addr_set_zero(&netmask);
IP4_ADDR(&netmask, 255, 255, 0, 0);
IP4_ADDR(&gw,
(DEFAULT_GW_IPADDR & 0xFF000000),
(DEFAULT_GW_IPADDR & 0xFF0000),
(DEFAULT_GW_IPADDR & 0xFF00),
(DEFAULT_GW_IPADDR & 0xFF));
IP4_ADDR(&ipaddr,
(g_localIpAddrBegin & 0xFF000000),
(g_localIpAddrBegin & 0xFF0000),
(g_localIpAddrBegin & 0xFF00),
(g_localIpAddrBegin & 0xFF));
g_localIpAddrBegin++;
netif = (struct netif *)mem_malloc(sizeof(struct netif));
if (!netif) {
dzlog_error("Create PPPoE netif error\n");
return NULL;
}
netif_add(netif, &ipaddr, &netmask, &gw, pSession, pppoeif_init, tcpip_input);
#if LWIP_NETIF_STATUS_CALLBACK
netif_set_status_callback(netif, status_callback);
#endif /* LWIP_NETIF_STATUS_CALLBACK */
#if LWIP_NETIF_LINK_CALLBACK
netif_set_link_callback(netif, link_callback);
#endif /* LWIP_NETIF_LINK_CALLBACK */
return netif;
}
#endif

View File

@ -55,7 +55,6 @@
#include "netif/rawif.h"
#define IFCONFIG_BIN "/sbin/ifconfig "
#if defined(LWIP_UNIX_LINUX)
#include <linux/if.h>
@ -154,7 +153,6 @@ static void low_level_init(struct netif *netif) {
rawif->if_index = ifr.ifr_ifindex;
#if 0
ret = ioctl(rawif->fd, SIOCGIFHWADDR, &ifr);
if (ret != 0) {
@ -165,14 +163,15 @@ static void low_level_init(struct netif *netif) {
memcpy(rawif->mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
#endif
/* Obtain MAC address from network interface. */
netif->hwaddr[0] = rawif->mac_addr[0];
netif->hwaddr[1] = rawif->mac_addr[1];
netif->hwaddr[2] = rawif->mac_addr[2];
netif->hwaddr[3] = rawif->mac_addr[3];
netif->hwaddr[4] = rawif->mac_addr[4];
netif->hwaddr[5] = rawif->mac_addr[5];
netif->hwaddr[0] = 0x00;
netif->hwaddr[1] = 0x0C;
netif->hwaddr[2] = 0x01;
netif->hwaddr[3] = 0x02;
netif->hwaddr[4] = 0x00;
netif->hwaddr[5] = 0x01;
netif->hwaddr_len = 6;
/* device capabilities */
@ -246,7 +245,8 @@ static struct pbuf *low_level_input(struct netif *netif) {
u16_t len;
int addrSize;
ssize_t readlen;
char buf[1518]; /* max packet size including VLAN excluding CRC */
unsigned char bmac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char buf[1518]; /* max packet size including VLAN excluding CRC */
struct sockaddr from;
struct rawif *rawif = (struct rawif *)netif->state;
@ -260,10 +260,17 @@ static struct pbuf *low_level_input(struct netif *netif) {
}
len = (u16_t)readlen;
//{0x00, 0x0C, 0x01, 0x02, 0x00, 0x01}
if((buf[0] != 0x00 && buf[1] != 0x0C && buf[2] != 0x01 && buf[3] != 0x02) &&
memcmp(buf, bmac, 6) != 0) {
return NULL;
}
MIB2_STATS_NETIF_ADD(netif, ifinoctets, len);
/* We allocate a pbuf chain of pbufs from the pool. */
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
if (p != NULL) {
pbuf_take(p, buf, len);
/* acknowledge that packet has been read(); */
@ -276,6 +283,11 @@ static struct pbuf *low_level_input(struct netif *netif) {
return p;
}
err_t rawif_output(struct netif *netif, struct pbuf *p) {
return low_level_output(netif, p);
}
/*-----------------------------------------------------------------------------------*/
/*
* rawif_input():
@ -294,7 +306,7 @@ static void rawif_input(struct netif *netif) {
#if LINK_STATS
LINK_STATS_INC(link.recv);
#endif /* LINK_STATS */
LWIP_DEBUGF(RAWIF_DEBUG, ("rawif_input: low_level_input returned NULL\n"));
//LWIP_DEBUGF(RAWIF_DEBUG, ("rawif_input: low_level_input returned NULL\n"));
return;
}
@ -427,7 +439,31 @@ static void link_callback(struct netif *state_netif) {
}
#endif /* LWIP_NETIF_LINK_CALLBACK */
struct netif *bind_rawsocket_if(const char *eth_name, unsigned char haddr[6]) {
static err_t netif_input_data(struct pbuf *p, struct netif *inp) {
struct netif *netif;
LWIP_ASSERT_CORE_LOCKED();
NETIF_FOREACH(netif) {
err_t err;
unsigned char* pMac = (unsigned char*)p->payload;
if(memcmp(pMac, netif->hwaddr, netif->hwaddr_len) == 0) {
// printf("++++++receive %02X:%02X:%02X:%02X:%02X:%02X\n",
// pMac[0], pMac[1], pMac[2], pMac[3], pMac[4], pMac[5]);
if ((err = netif->input(p, netif)) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("pppoeif_input: netif input error\n"));
pbuf_free(p);
}
return err;
}
}
return tcpip_input(p, inp);
}
struct netif *bind_rawsocket_if(const char *eth_name) {
struct netif *netif;
ip4_addr_t ipaddr, netmask, gw;
@ -435,8 +471,6 @@ struct netif *bind_rawsocket_if(const char *eth_name, unsigned char haddr[6]) {
g_ethName = strdup(eth_name);
}
memcpy(g_macAddr, haddr, 6);
ip4_addr_set_zero(&gw);
ip4_addr_set_zero(&ipaddr);
ip4_addr_set_zero(&netmask);
@ -462,7 +496,7 @@ struct netif *bind_rawsocket_if(const char *eth_name, unsigned char haddr[6]) {
return NULL;
}
netif_add(netif, &ipaddr, &netmask, &gw, NULL, rawif_init, tcpip_input);
netif_add(netif, &ipaddr, &netmask, &gw, NULL, rawif_init, netif_input_data);
#if LWIP_NETIF_STATUS_CALLBACK
netif_set_status_callback(netif, status_callback);
@ -471,6 +505,7 @@ struct netif *bind_rawsocket_if(const char *eth_name, unsigned char haddr[6]) {
netif_set_link_callback(netif, link_callback);
#endif /* LWIP_NETIF_LINK_CALLBACK */
netif_set_default(netif);
return netif;
}
#endif

View File

@ -98,7 +98,7 @@ struct sys_mbox_msg {
void *msg;
};
#define SYS_MBOX_SIZE 12800
#define SYS_MBOX_SIZE 128
struct sys_mbox {
int first, last;
@ -221,6 +221,7 @@ void sys_check_core_locking(void) {
#endif
}
/*-----------------------------------------------------------------------------------*/
/* Mailbox */
err_t sys_mbox_new(struct sys_mbox **mb, int size) {
@ -239,6 +240,8 @@ err_t sys_mbox_new(struct sys_mbox **mb, int size) {
SYS_STATS_INC_USED(mbox);
*mb = mbox;
return ERR_OK;
}

View File

@ -6,19 +6,22 @@
#include "netif/rawif.h"
#include "netif/ppp/ppp.h"
#include "netif/ppp/pppoe.h"
#include "netif/pppoeif.h"
static PPPPOE_SESSION g_pSessionList = NULL;
static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) {
struct netif *pppif = ppp_netif(pcb);
struct pppoe_softc* sc = (struct pppoe_softc*)pcb->link_ctx_cb;
struct netif *pppif = ppp_netif(pcb);
struct pppoe_softc *sc = (struct pppoe_softc *)pcb->link_ctx_cb;
LWIP_UNUSED_ARG(ctx);
switch (errCode) {
case PPPERR_NONE: { /* No error. */
dzlog_info("<%p> pppLinkStatusCallback: PPPERR_NONE(%08X), Session: %04X\n", pcb,
pcb->lcp_gotoptions.magicnumber, sc->sc_session);
dzlog_info("<%p> pppLinkStatusCallback: PPPERR_NONE(%08X), Session: %04X\n",
pcb,
pcb->lcp_gotoptions.magicnumber,
sc->sc_session);
#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)));
@ -88,49 +91,77 @@ static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) {
}
}
static struct netif* g_rawSocketIf = NULL;
struct netif* get_rawsocket_if(void) {
return g_rawSocketIf;
}
int pppoe_session_init() {
PUSER_INFO_LIST pList, pTmp;
PUSER_INFO_LIST pUserList = user_info_getall();
g_rawSocketIf = bind_rawsocket_if("ens160");
if(g_rawSocketIf) {
dzlog_info("Create Raw Socket netif: <%p>\n", (void*)g_rawSocketIf);
}
HASH_ITER(hh, pUserList, pList, pTmp) {
PPPPOE_SESSION pSession = (PPPPOE_SESSION)malloc(sizeof(PPPOE_SESSION));
if (pSession) {
struct netif* ppp_netif = (struct netif*)malloc(sizeof(struct netif));
struct netif *ppp_netif;
memset(pSession, 0, sizeof(PPPOE_SESSION));
if(ppp_netif) {
struct netif *netif = bind_rawsocket_if("ens160", pList->user_info.mac_addr);
ppp_netif = (struct netif *)malloc(sizeof(struct netif));
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);
if (netif == NULL) {
free(pSession);
free(ppp_netif);
dzlog_error("Create RAW netif error: %u\n", pList->userid);
dzlog_error("Create PPPoE netif error: %u\n", pList->userid);
} else {
ppp_pcb* ppp;
memset(pSession, 0, sizeof(PPPOE_SESSION));
ppp_pcb *ppp;
pSession->nicif = netif;
pSession->pppif = ppp_netif;
pSession->userid = pList->userid;
memcpy(&pSession->user_info, &pList->user_info, sizeof(USER_INFO));
ppp = pppoe_create(pSession->pppif, pSession->nicif, NULL, NULL, pppLinkStatusCallback, NULL);
if(ppp == NULL) {
if (ppp == NULL) {
netif_remove(netif);
free(pSession);
free(ppp_netif);
dzlog_error("Create PPPoE netif error: %u\n", pList->userid);
} else {
ppp_set_auth(ppp, PPPAUTHTYPE_ANY, pSession->user_info.pppoe_user, pSession->user_info.pppoe_passwd);
ppp_set_auth(ppp,
PPPAUTHTYPE_ANY,
pSession->user_info.pppoe_user,
pSession->user_info.pppoe_passwd);
ppp_connect(ppp, 0);
dzlog_info("Create PPPoE netif %p: %u\n", ppp, pList->userid);
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);
HASH_ADD_INT(g_pSessionList, userid, pSession);
}
}
}
}
}
return ERR_OK;

View File

@ -7,15 +7,15 @@
static PUSER_INFO_LIST g_pUserList = NULL;
static USER_INFO g_userInfo[] = {
{0, 1, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x01}, "xajhuang", "aaaHuang1"},
{1, 2, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "xajhuang1", "aaaHuang1"},
{3, 4, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "xajhuang2", "aaaHuang1"},
{0, 1, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "xajhuang", "aaaHuang1"},
{1, 2, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "xajhuang1", "aaaHuang1"},
{3, 4, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang2", "aaaHuang1"},
};
void user_info_init() {
user_info_add(0, &g_userInfo[0]);
user_info_add(1, &g_userInfo[1]);
//user_info_add(2, &g_userInfo[2]);
user_info_add(2, &g_userInfo[2]);
}
int user_info_add(unsigned int userid, PUSER_INFO pInfo) {