static void nPut(PPPControl *pc, struct pbuf *nb) { struct pbuf *b; int c; for(b = nb; b != NULL; b = b->next) { if((c = sio_write(pc->fd, b->payload, b->len)) != b->len) { PPPDEBUG((LOG_WARNING, "PPP nPut: incomplete sio_write(%d,, %u) = %d\n", pc->fd, b->len, c)); #if LINK_STATS lwip_stats.link.err++; #endif /* LINK_STATS */ pc->lastXMit = 0; /* prepend PPP_FLAG to next packet */ break; } } pbuf_free(nb); #if LINK_STATS lwip_stats.link.xmit++; #endif /* LINK_STATS */ }
/* * Decompress (decrypt) an MPPE packet. */ err_t mppe_decompress(ppp_pcb *pcb, ppp_mppe_state *state, struct pbuf **pb) { struct pbuf *n0 = *pb, *n; u8_t *pl; u16_t ccount; u8_t flushed; /* MPPE Header */ if (n0->len < MPPE_OVHD) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: short pkt (%d)\n", pcb->netif->num, n0->len)); state->sanity_errors += 100; goto sanity_error; } pl = (u8_t*)n0->payload; flushed = MPPE_BITS(pl) & MPPE_BIT_FLUSHED; ccount = MPPE_CCOUNT(pl); PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: ccount %d\n", pcb->netif->num, ccount)); /* sanity checks -- terminate with extreme prejudice */ if (!(MPPE_BITS(pl) & MPPE_BIT_ENCRYPTED)) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: ENCRYPTED bit not set!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } if (!state->stateful && !flushed) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: FLUSHED bit not set in " "stateless mode!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) { PPPDEBUG(LOG_DEBUG, ("mppe_decompress[%d]: FLUSHED bit not set on " "flag packet!\n", pcb->netif->num)); state->sanity_errors += 100; goto sanity_error; } /* * Check the coherency count. */ if (!state->stateful) { /* Discard late packet */ if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE > MPPE_CCOUNT_SPACE / 2) { state->sanity_errors++; goto sanity_error; } /* RFC 3078, sec 8.1. Rekey for every packet. */ while (state->ccount != ccount) { mppe_rekey(state, 0); state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; } } else { /* RFC 3078, sec 8.2. */ if (!state->discard) { /* normal state */ state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; if (ccount != state->ccount) { /* * (ccount > state->ccount) * Packet loss detected, enter the discard state. * Signal the peer to rekey (by sending a CCP Reset-Request). */ state->discard = 1; ccp_resetrequest(pcb); return ERR_BUF; } } else { /* discard state */ if (!flushed) { /* ccp.c will be silent (no additional CCP Reset-Requests). */ return ERR_BUF; } else { /* Rekey for every missed "flag" packet. */ while ((ccount & ~0xff) != (state->ccount & ~0xff)) { mppe_rekey(state, 0); state->ccount = (state->ccount + 256) % MPPE_CCOUNT_SPACE; } /* reset */ state->discard = 0; state->ccount = ccount; /* * Another problem with RFC 3078 here. It implies that the * peer need not send a Reset-Ack packet. But RFC 1962 * requires it. Hopefully, M$ does send a Reset-Ack; even * though it isn't required for MPPE synchronization, it is * required to reset CCP state. */ } } if (flushed) mppe_rekey(state, 0); } /* Hide MPPE header */ pbuf_header(n0, -(s16_t)(MPPE_OVHD)); /* Decrypt the packet. */ for (n = n0; n != NULL; n = n->next) { arc4_crypt(&state->arc4, (u8_t*)n->payload, n->len); if (n->tot_len == n->len) { break; } } /* good packet credit */ state->sanity_errors >>= 1; return ERR_OK; sanity_error: if (state->sanity_errors >= SANITY_MAX) { /* * Take LCP down if the peer is sending too many bogons. * We don't want to do this for a single or just a few * instances since it could just be due to packet corruption. */ lcp_close(pcb, "Too many MPPE errors"); } return ERR_BUF; }
/* * Compress (encrypt) a packet. * It's strange to call this a compressor, since the output is always * MPPE_OVHD + 2 bytes larger than the input. */ err_t mppe_compress(ppp_pcb *pcb, ppp_mppe_state *state, struct pbuf **pb, u16_t protocol) { struct pbuf *n, *np; u8_t *pl; err_t err; LWIP_UNUSED_ARG(pcb); /* TCP stack requires that we don't change the packet payload, therefore we copy * the whole packet before encryption. */ np = pbuf_alloc(PBUF_RAW, MPPE_OVHD + sizeof(protocol) + (*pb)->tot_len, PBUF_POOL); if (!np) { return ERR_MEM; } /* Hide MPPE header + protocol */ pbuf_header(np, -(s16_t)(MPPE_OVHD + sizeof(protocol))); if ((err = pbuf_copy(np, *pb)) != ERR_OK) { pbuf_free(np); return err; } /* Reveal MPPE header + protocol */ pbuf_header(np, (s16_t)(MPPE_OVHD + sizeof(protocol))); *pb = np; pl = (u8_t*)np->payload; state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE; PPPDEBUG(LOG_DEBUG, ("mppe_compress[%d]: ccount %d\n", pcb->netif->num, state->ccount)); /* FIXME: use PUT* macros */ pl[0] = state->ccount>>8; pl[1] = state->ccount; if (!state->stateful || /* stateless mode */ ((state->ccount & 0xff) == 0xff) || /* "flag" packet */ (state->bits & MPPE_BIT_FLUSHED)) { /* CCP Reset-Request */ /* We must rekey */ if (state->stateful) { PPPDEBUG(LOG_DEBUG, ("mppe_compress[%d]: rekeying\n", pcb->netif->num)); } mppe_rekey(state, 0); state->bits |= MPPE_BIT_FLUSHED; } pl[0] |= state->bits; state->bits &= ~MPPE_BIT_FLUSHED; /* reset for next xmit */ pl += MPPE_OVHD; /* Add protocol */ /* FIXME: add PFC support */ pl[0] = protocol >> 8; pl[1] = protocol; /* Hide MPPE header */ pbuf_header(np, -(s16_t)MPPE_OVHD); /* Encrypt packet */ for (n = np; n != NULL; n = n->next) { arc4_crypt(&state->arc4, (u8_t*)n->payload, n->len); if (n->tot_len == n->len) { break; } } /* Reveal MPPE header */ pbuf_header(np, (s16_t)MPPE_OVHD); return ERR_OK; }
/* * Initialize (de)compressor state. */ void mppe_init(ppp_pcb *pcb, ppp_mppe_state *state, u8_t options) { #if PPP_DEBUG const u8_t *debugstr = (const u8_t*)"mppe_comp_init"; if (&pcb->mppe_decomp == state) { debugstr = (const u8_t*)"mppe_decomp_init"; } #endif /* PPP_DEBUG */ /* Save keys. */ MEMCPY(state->session_key, state->master_key, sizeof(state->master_key)); if (options & MPPE_OPT_128) state->keylen = 16; else if (options & MPPE_OPT_40) state->keylen = 8; else { PPPDEBUG(LOG_DEBUG, ("%s[%d]: unknown key length\n", debugstr, pcb->netif->num)); lcp_close(pcb, "MPPE required but peer negotiation failed"); return; } if (options & MPPE_OPT_STATEFUL) state->stateful = 1; /* Generate the initial session key. */ mppe_rekey(state, 1); #if PPP_DEBUG { int i; char mkey[sizeof(state->master_key) * 2 + 1]; char skey[sizeof(state->session_key) * 2 + 1]; PPPDEBUG(LOG_DEBUG, ("%s[%d]: initialized with %d-bit %s mode\n", debugstr, pcb->netif->num, (state->keylen == 16) ? 128 : 40, (state->stateful) ? "stateful" : "stateless")); for (i = 0; i < (int)sizeof(state->master_key); i++) sprintf(mkey + i * 2, "%02x", state->master_key[i]); for (i = 0; i < (int)sizeof(state->session_key); i++) sprintf(skey + i * 2, "%02x", state->session_key[i]); PPPDEBUG(LOG_DEBUG, ("%s[%d]: keys: master: %s initial session: %s\n", debugstr, pcb->netif->num, mkey, skey)); } #endif /* PPP_DEBUG */ /* * Initialize the coherency count. The initial value is not specified * in RFC 3078, but we can make a reasonable assumption that it will * start at 0. Setting it to the max here makes the comp/decomp code * do the right thing (determined through experiment). */ state->ccount = MPPE_CCOUNT_SPACE - 1; /* * Note that even though we have initialized the key table, we don't * set the FLUSHED bit. This is contrary to RFC 3078, sec. 3.1. */ state->bits = MPPE_BIT_ENCRYPTED; }
/* Send a packet on the given connection. */ static err_t pppifOutput(struct netif *netif, struct pbuf *pb, struct ip_addr *ipaddr) { int pd = (int)netif->state; u_short protocol = PPP_IP; PPPControl *pc = &pppControl[pd]; u_int fcsOut = PPP_INITFCS; struct pbuf *headMB = NULL, *tailMB = NULL, *p; u_char c; (void)ipaddr; /* Validate parameters. */ /* We let any protocol value go through - it can't hurt us * and the peer will just drop it if it's not accepting it. */ if (pd < 0 || pd >= NUM_PPP || !pc->openFlag || !pb) { PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad parms prot=%d pb=%p\n", pd, protocol, pb)); #if LINK_STATS lwip_stats.link.opterr++; lwip_stats.link.drop++; #endif return ERR_ARG; } /* Check that the link is up. */ if (lcp_phase[pd] == PHASE_DEAD) { PPPDEBUG((LOG_ERR, "pppifOutput[%d]: link not up\n", pd)); #if LINK_STATS lwip_stats.link.rterr++; lwip_stats.link.drop++; #endif return ERR_RTE; } /* Grab an output buffer. */ headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL); if (headMB == NULL) { PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: first alloc fail\n", pd)); #if LINK_STATS lwip_stats.link.memerr++; lwip_stats.link.drop++; #endif /* LINK_STATS */ return ERR_MEM; } #if VJ_SUPPORT > 0 /* * Attempt Van Jacobson header compression if VJ is configured and * this is an IP packet. */ if (protocol == PPP_IP && pc->vjEnabled) { switch (vj_compress_tcp(&pc->vjComp, pb)) { case TYPE_IP: /* No change... protocol = PPP_IP_PROTOCOL; */ break; case TYPE_COMPRESSED_TCP: protocol = PPP_VJC_COMP; break; case TYPE_UNCOMPRESSED_TCP: protocol = PPP_VJC_UNCOMP; break; default: PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad IP packet\n", pd)); #if LINK_STATS lwip_stats.link.proterr++; lwip_stats.link.drop++; #endif pbuf_free(headMB); return ERR_VAL; } } #endif tailMB = headMB; /* Build the PPP header. */ if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG) tailMB = pppAppend(PPP_FLAG, tailMB, NULL); pc->lastXMit = sys_jiffies(); if (!pc->accomp) { fcsOut = PPP_FCS(fcsOut, PPP_ALLSTATIONS); tailMB = pppAppend(PPP_ALLSTATIONS, tailMB, &pc->outACCM); fcsOut = PPP_FCS(fcsOut, PPP_UI); tailMB = pppAppend(PPP_UI, tailMB, &pc->outACCM); } if (!pc->pcomp || protocol > 0xFF) { c = (protocol >> 8) & 0xFF; fcsOut = PPP_FCS(fcsOut, c); tailMB = pppAppend(c, tailMB, &pc->outACCM); }