📄 ppp.c
字号:
/*
* Pass the processed input packet to the appropriate handler.
* This function and all handlers run in the context of the tcpip_thread
*/
static err_t pppInput(struct pbuf *nb, struct netif *inp)
{
u16_t protocol;
int pd = inp->num;
memcpy(&protocol, nb->payload, sizeof(protocol));
pbuf_header(nb, -(int)sizeof(protocol));
#ifdef LINK_STATS
lwip_stats.link.recv++;
#endif /* LINK_STATS */
/*
* Toss all non-LCP packets unless LCP is OPEN.
* Until we get past the authentication phase, toss all packets
* except LCP, LQR and authentication packets.
*/
if((lcp_phase[pd] <= PHASE_AUTHENTICATE) && (protocol != PPP_LCP)) {
if(!((protocol == PPP_LQR) || (protocol == PPP_PAP) || (protocol == PPP_CHAP)) ||
(lcp_phase[pd] != PHASE_AUTHENTICATE)) {
PPPDEBUG((LOG_INFO, "pppInput: discarding proto 0x%04X in phase %d\n", protocol, lcp_phase[pd]));
goto drop;
}
}
switch(protocol) {
case PPP_VJC_COMP: /* VJ compressed TCP */
#if VJ_SUPPORT > 0
PPPDEBUG((LOG_INFO, "pppInput[%d]: vj_comp in nBuf len=%d\n", pd, nb->len));
/*
* Clip off the VJ header and prepend the rebuilt TCP/IP header and
* pass the result to IP.
*/
if (vj_uncompress_tcp(&nb, &pppControl[pd].vjComp) >= 0) {
return pppControl[pd].netif->input(nb, pppControl[pd].netif);
}
/* Something's wrong so drop it. */
PPPDEBUG((LOG_WARNING, "pppInput[%d]: Dropping VJ compressed\n", pd));
#else
/* No handler for this protocol so drop the packet. */
PPPDEBUG((LOG_INFO, "pppInput[%d]: drop VJ Comp in %d:%s\n", pd, nb->len, nb->payload));
#endif /* VJ_SUPPORT > 0 */
break;
case PPP_VJC_UNCOMP: /* VJ uncompressed TCP */
#if VJ_SUPPORT > 0
PPPDEBUG((LOG_INFO, "pppInput[%d]: vj_un in nBuf len=%d\n", pd, nb->len));
/*
* Process the TCP/IP header for VJ header compression and then pass
* the packet to IP.
*/
if (vj_uncompress_uncomp(nb, &pppControl[pd].vjComp) >= 0) {
return pppControl[pd].netif->input(nb, pppControl[pd].netif);
}
/* Something's wrong so drop it. */
PPPDEBUG((LOG_WARNING, "pppInput[%d]: Dropping VJ uncompressed\n", pd));
#else
/* No handler for this protocol so drop the packet. */
PPPDEBUG((LOG_INFO,
"pppInput[%d]: drop VJ UnComp in %d:.*H\n",
pd, nb->len, MIN(nb->len * 2, 40), nb->payload));
#endif /* VJ_SUPPORT > 0 */
break;
case PPP_IP: /* Internet Protocol */
PPPDEBUG((LOG_INFO, "pppInput[%d]: ip in nBuf len=%d", pd, nb->len));
return pppControl[pd].netif->input(nb, pppControl[pd].netif);
default:
{
struct protent *protp;
int i;
/*
* Upcall the proper protocol input routine.
*/
for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
if (protp->protocol == protocol && protp->enabled_flag) {
PPPDEBUG((LOG_INFO, "pppInput[%d]: %s len=%d\n", pd, protp->name, nb->len));
nb = pppSingleBuf(nb);
(*protp->input)(pd, nb->payload, nb->len);
goto out;
}
}
/* No handler for this protocol so reject the packet. */
PPPDEBUG((LOG_INFO, "pppInput[%d]: rejecting unsupported proto 0x%04X len=%d\n", pd, protocol, nb->len));
pbuf_header(nb, sizeof(protocol));
#if BYTE_ORDER == LITTLE_ENDIAN
protocol = htons(protocol);
memcpy(nb->payload, &protocol, sizeof(protocol));
#endif
lcp_sprotrej(pd, nb->payload, nb->len);
}
break;
}
drop:
#ifdef LINK_STATS
lwip_stats.link.drop++;
#endif
out:
pbuf_free(nb);
return ERR_OK;
}
/*
* Drop the input packet.
*/
static void pppDrop(PPPControl *pc)
{
if (pc->inHead != NULL) {
// PPPDEBUG((LOG_INFO, "pppDrop: %d:%.*H\n", pc->inHead->len, min(60, pc->inHead->len * 2), pc->inHead->payload));
PPPDEBUG((LOG_INFO, "pppDrop: nBuf len=%d\n", pc->inHead->len));
pbuf_free(pc->inHead);
pc->inHead = NULL;
pc->inTail = NULL;
}
#if VJ_SUPPORT > 0
vj_uncompress_err(&pc->vjComp);
#endif
#ifdef LINK_STATS
lwip_stats.link.drop++;
#endif /* LINK_STATS */
}
/*
* Process a received octet string.
*/
static void pppInProc(int pd, u_char *s, int l)
{
PPPControl *pc = &pppControl[pd];
struct pbuf *nextNBuf;
u_char curChar;
PPPDEBUG((LOG_DEBUG, "pppInProc[%d]: got %d bytes\n", pd, l));
while (l-- > 0) {
curChar = *s++;
/* Handle special characters. */
if (ESCAPE_P(pc->inACCM, curChar)) {
/* Check for escape sequences. */
/* XXX Note that this does not handle an escaped 0x5d character which
* would appear as an escape character. Since this is an ASCII ']'
* and there is no reason that I know of to escape it, I won't complicate
* the code to handle this case. GLL */
if (curChar == PPP_ESCAPE)
pc->inEscaped = 1;
/* Check for the flag character. */
else if (curChar == PPP_FLAG) {
/* If this is just an extra flag character, ignore it. */
if (pc->inState <= PDADDRESS)
;
/* If we haven't received the packet header, drop what has come in. */
else if (pc->inState < PDDATA) {
PPPDEBUG((LOG_WARNING,
"pppInProc[%d]: Dropping incomplete packet %d\n",
pd, pc->inState));
#ifdef LINK_STATS
lwip_stats.link.lenerr++;
#endif
pppDrop(pc);
}
/* If the fcs is invalid, drop the packet. */
else if (pc->inFCS != PPP_GOODFCS) {
PPPDEBUG((LOG_INFO,
"pppInProc[%d]: Dropping bad fcs 0x%04X proto=0x%04X\n",
pd, pc->inFCS, pc->inProtocol));
#ifdef LINK_STATS
lwip_stats.link.chkerr++;
#endif
pppDrop(pc);
}
/* Otherwise it's a good packet so pass it on. */
else {
/* Trim off the checksum. */
if(pc->inTail->len >= 2) {
pc->inTail->len -= 2;
pc->inLen -= 2;
/* Update the packet header. */
pc->inHead->tot_len = pc->inLen;
} else {
pc->inHead->tot_len = pc->inLen;
pbuf_realloc(pc->inHead, pc->inLen - 2);
}
/* Dispatch the packet thereby consuming it. */
tcpip_link_input(pc->inHead, &pc->linkif);
pc->inHead = NULL;
pc->inTail = NULL;
}
/* Prepare for a new packet. */
pc->inFCS = PPP_INITFCS;
pc->inState = PDADDRESS;
pc->inEscaped = 0;
}
/* Other characters are usually control characters that may have
* been inserted by the physical layer so here we just drop them. */
else {
PPPDEBUG((LOG_WARNING,
"pppInProc[%d]: Dropping ACCM char <%d>\n", pd, curChar));
}
}
/* Process other characters. */
else {
/* Unencode escaped characters. */
if (pc->inEscaped) {
pc->inEscaped = 0;
curChar ^= PPP_TRANS;
}
/* Process character relative to current state. */
switch(pc->inState) {
case PDIDLE: /* Idle state - waiting. */
/* Drop the character if it's not 0xff
* we would have processed a flag character above. */
if (curChar != PPP_ALLSTATIONS) {
break;
}
/* Fall through */
case PDSTART: /* Process start flag. */
/* Prepare for a new packet. */
pc->inFCS = PPP_INITFCS;
/* Fall through */
case PDADDRESS: /* Process address field. */
if (curChar == PPP_ALLSTATIONS) {
pc->inState = PDCONTROL;
break;
}
/* Else assume compressed address and control fields so
* fall through to get the protocol... */
case PDCONTROL: /* Process control field. */
/* If we don't get a valid control code, restart. */
if (curChar == PPP_UI) {
pc->inState = PDPROTOCOL1;
break;
}
#if 0
else {
PPPDEBUG((LOG_WARNING,
"pppInProc[%d]: Invalid control <%d>\n", pd, curChar));
pc->inState = PDSTART;
}
#endif
case PDPROTOCOL1: /* Process protocol field 1. */
/* If the lower bit is set, this is the end of the protocol
* field. */
if (curChar & 1) {
pc->inProtocol = curChar;
pc->inState = PDDATA;
}
else {
pc->inProtocol = (u_int)curChar << 8;
pc->inState = PDPROTOCOL2;
}
break;
case PDPROTOCOL2: /* Process protocol field 2. */
pc->inProtocol |= curChar;
pc->inState = PDDATA;
break;
case PDDATA: /* Process data byte. */
/* Make space to receive processed data. */
if (pc->inTail == NULL || pc->inTail->len == PBUF_POOL_BUFSIZE) {
/* If we haven't started a packet, we need a packet header. */
nextNBuf = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
if (nextNBuf == NULL) {
/* No free buffers. Drop the input packet and let the
* higher layers deal with it. Continue processing
* the received nBuf chain in case a new packet starts. */
PPPDEBUG((LOG_ERR, "pppInProc[%d]: NO FREE MBUFS!\n", pd));
#ifdef LINK_STATS
lwip_stats.link.memerr++;
#endif /* LINK_STATS */
pppDrop(pc);
pc->inState = PDSTART; /* Wait for flag sequence. */
break;
} else {
if (pc->inHead == NULL) {
memcpy(nextNBuf->payload, &pc->inProtocol, sizeof(pc->inProtocol));
nextNBuf->len += sizeof(pc->inProtocol);
pc->inLen = nextNBuf->len;
pc->inHead = nextNBuf;
}
else { /* Since if inHead is not NULL, then neither is inTail! */
pc->inTail->next = nextNBuf;
}
pc->inTail = nextNBuf;
}
}
/* Load character into buffer. */
((u_char*)pc->inTail->payload)[pc->inTail->len++] = curChar;
pc->inLen++;
break;
}
/* update the frame check sequence number. */
pc->inFCS = PPP_FCS(pc->inFCS, curChar);
}
}
avRandomize();
}
/*
* pppMPutC - append given character to end of given nBuf. If the character
* needs to be escaped, do so. If nBuf is full, append another.
* Return the current nBuf.
*/
static struct pbuf *pppMPutC(u_char c, ext_accm *outACCM, struct pbuf *nb)
{
struct pbuf *tb = nb;
/* Make sure there is room for the character and an escape code.
* Sure we don't quite fill the buffer if the character doesn't
* get escaped but is one character worth complicating this? */
/* Note: We assume no packet header. */
if (nb && (PBUF_POOL_BUFSIZE - nb->len) < 2) {
tb = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
if (tb) {
nb->next = tb;
}
#ifdef LINK_STATS
else {
lwip_stats.link.memerr++;
}
#endif /* LINK_STATS */
nb = tb;
}
if (nb) {
if (ESCAPE_P(*outACCM, c)) {
*((u_char*)nb->payload + nb->len++) = PPP_ESCAPE;
*((u_char*)nb->payload + nb->len++) = c ^ PPP_TRANS;
}
else
*((u_char*)nb->payload + nb->len++) = c;
}
return tb;
}
/*
* pppMPutRaw - append given character to end of given nBuf without escaping
* it. If nBuf is full, append another.
* This is normally used to add the flag character to a packet.
* Return the current nBuf.
*/
static struct pbuf *pppMPutRaw(u_char c, struct pbuf *nb)
{
struct pbuf *tb = nb;
/* Make sure there is room for the character and an escape code.
* Sure we don't quite fill the buffer if the character doesn't
* get escaped but is one character worth complicating this? */
/* Note: We assume no packet header. */
if (nb && (PBUF_POOL_BUFSIZE - nb->len) < 2) {
tb = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
if (tb) {
nb->next = tb;
}
#ifdef LINK_STATS
else {
lwip_stats.link.memerr++;
}
#endif /* LINK_STATS */
nb = tb;
}
if (nb) {
*((u_char*)nb->payload + nb->len++) = c;
}
return tb;
}
#endif /* PPP_SUPPORT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -