31 #include "netif/ppp/ppp_opts.h" 32 #if PPP_SUPPORT && VJ_SUPPORT 34 #include "netif/ppp/ppp_impl.h" 35 #include "netif/ppp/pppdebug.h" 37 #include "netif/ppp/vj.h" 42 #define INCR(counter) ++comp->stats.counter 48 vj_compress_init(
struct vjcompress *comp)
51 struct cstate *tstate = comp->tstate;
54 memset((
char *)comp, 0,
sizeof(*comp));
56 comp->maxSlotIndex = MAX_SLOTS - 1;
57 comp->compressSlot = 0;
58 for (i = MAX_SLOTS - 1; i > 0; --i) {
60 tstate[i].cs_next = &tstate[i - 1];
62 tstate[0].cs_next = &tstate[MAX_SLOTS - 1];
64 comp->last_cs = &tstate[0];
65 comp->last_recv = 255;
66 comp->last_xmit = 255;
67 comp->flags = VJF_TOSS;
76 if ((u16_t)(n) >= 256) { \ 79 cp[0] = (u8_t)((n) >> 8); \ 85 #define ENCODEZ(n) { \ 86 if ((u16_t)(n) >= 256 || (u16_t)(n) == 0) { \ 89 cp[0] = (u8_t)((n) >> 8); \ 96 #define DECODEL(f) { \ 98 u32_t tmp_ = lwip_ntohl(f) + ((cp[1] << 8) | cp[2]); \ 99 (f) = lwip_htonl(tmp_); \ 102 u32_t tmp_ = lwip_ntohl(f) + (u32_t)*cp++; \ 103 (f) = lwip_htonl(tmp_); \ 107 #define DECODES(f) { \ 109 u16_t tmp_ = lwip_ntohs(f) + (((u16_t)cp[1] << 8) | cp[2]); \ 110 (f) = lwip_htons(tmp_); \ 113 u16_t tmp_ = lwip_ntohs(f) + (u16_t)*cp++; \ 114 (f) = lwip_htons(tmp_); \ 118 #define DECODEU(f) { \ 120 (f) = lwip_htons(((u16_t)cp[1] << 8) | cp[2]); \ 123 (f) = lwip_htons((u16_t)*cp++); \ 128 #ifdef PACK_STRUCT_USE_INCLUDES 129 # include "arch/bpstruct.h" 134 } PACK_STRUCT_STRUCT;
136 #ifdef PACK_STRUCT_USE_INCLUDES 137 # include "arch/epstruct.h" 140 #ifdef PACK_STRUCT_USE_INCLUDES 141 # include "arch/bpstruct.h" 146 } PACK_STRUCT_STRUCT;
148 #ifdef PACK_STRUCT_USE_INCLUDES 149 # include "arch/epstruct.h" 160 vj_compress_tcp(
struct vjcompress *comp,
struct pbuf **pb)
162 struct pbuf *np = *pb;
164 struct cstate *cs = comp->last_cs->cs_next;
165 u16_t ilen = IPH_HL(ip);
169 u16_t deltaS, deltaA = 0;
178 if (IPH_PROTO(ip) != IP_PROTO_TCP) {
187 if ((IPH_OFFSET(ip) & PP_HTONS(0x3fff)) || np->
tot_len < 40) {
190 th = (
struct tcp_hdr *)&((
struct vj_u32_t*)ip)[ilen];
191 if ((TCPH_FLAGS(th) & (TCP_SYN|TCP_FIN|TCP_RST|TCP_ACK)) != TCP_ACK) {
196 hlen = ilen + TCPH_HDRLEN(th);
198 if (np->
len < hlen) {
199 PPPDEBUG(LOG_INFO, (
"vj_compress_tcp: header len %d spans buffers\n", hlen));
226 if (!ip4_addr_cmp(&ip->src, &cs->cs_ip.src)
227 || !ip4_addr_cmp(&ip->dest, &cs->cs_ip.dest)
228 || (*(
struct vj_u32_t*)th).v != (((
struct vj_u32_t*)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]).v) {
242 struct cstate *lastcs = comp->last_cs;
245 lcs = cs; cs = cs->cs_next;
247 if (ip4_addr_cmp(&ip->src, &cs->cs_ip.src)
248 && ip4_addr_cmp(&ip->dest, &cs->cs_ip.dest)
249 && (*(
struct vj_u32_t*)th).v == (((
struct vj_u32_t*)&cs->cs_ip)[IPH_HL(&cs->cs_ip)]).v) {
252 }
while (cs != lastcs);
273 lcs->cs_next = cs->cs_next;
274 cs->cs_next = lastcs->cs_next;
275 lastcs->cs_next = cs;
279 oth = (
struct tcp_hdr *)&((
struct vj_u32_t*)&cs->cs_ip)[ilen];
293 if ((((
struct vj_u16_t*)ip)[0]).v != (((
struct vj_u16_t*)&cs->cs_ip)[0]).v
294 || (((
struct vj_u16_t*)ip)[3]).v != (((
struct vj_u16_t*)&cs->cs_ip)[3]).v
295 || (((
struct vj_u16_t*)ip)[4]).v != (((
struct vj_u16_t*)&cs->cs_ip)[4]).v
296 || TCPH_HDRLEN(th) != TCPH_HDRLEN(oth)
297 || (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2))
298 || (TCPH_HDRLEN(th) > 5 && BCMP(th + 1, oth + 1, (TCPH_HDRLEN(th) - 5) << 2))) {
308 if (TCPH_FLAGS(th) & TCP_URG) {
309 deltaS = lwip_ntohs(th->urgp);
312 }
else if (th->urgp != oth->urgp) {
320 if ((deltaS = (u16_t)(lwip_ntohs(th->wnd) - lwip_ntohs(oth->wnd))) != 0) {
325 if ((deltaL = lwip_ntohl(th->ackno) - lwip_ntohl(oth->ackno)) != 0) {
326 if (deltaL > 0xffff) {
329 deltaA = (u16_t)deltaL;
334 if ((deltaL = lwip_ntohl(th->seqno) - lwip_ntohl(oth->seqno)) != 0) {
335 if (deltaL > 0xffff) {
338 deltaS = (u16_t)deltaL;
353 if (IPH_LEN(ip) != IPH_LEN(&cs->cs_ip) &&
354 lwip_ntohs(IPH_LEN(&cs->cs_ip)) == hlen) {
369 if (deltaS == deltaA && deltaS == lwip_ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
377 if (deltaS == lwip_ntohs(IPH_LEN(&cs->cs_ip)) - hlen) {
387 deltaS = (u16_t)(lwip_ntohs(IPH_ID(ip)) - lwip_ntohs(IPH_ID(&cs->cs_ip)));
392 if (TCPH_FLAGS(th) & TCP_PSH) {
393 changes |= TCP_PUSH_BIT;
399 deltaA = lwip_ntohs(th->chksum);
400 MEMCPY(&cs->cs_ip, ip, hlen);
411 deltaS = (u16_t)(cp - new_seq);
412 if (!comp->compressSlot || comp->last_xmit != cs->cs_id) {
413 comp->last_xmit = cs->cs_id;
417 LWIP_ASSERT(
"pbuf_header failed\n", 0);
420 *cp++ = (u8_t)(changes | NEW_C);
426 LWIP_ASSERT(
"pbuf_header failed\n", 0);
429 *cp++ = (u8_t)changes;
431 *cp++ = (u8_t)(deltaA >> 8);
432 *cp++ = (u8_t)deltaA;
433 MEMCPY(cp, new_seq, deltaS);
434 INCR(vjs_compressed);
435 return (TYPE_COMPRESSED_TCP);
443 MEMCPY(&cs->cs_ip, ip, hlen);
444 IPH_PROTO_SET(ip, cs->cs_id);
445 comp->last_xmit = cs->cs_id;
446 return (TYPE_UNCOMPRESSED_TCP);
453 vj_uncompress_err(
struct vjcompress *comp)
455 comp->flags |= VJF_TOSS;
464 vj_uncompress_uncomp(
struct pbuf *nb,
struct vjcompress *comp)
471 hlen = IPH_HL(ip) << 2;
472 if (IPH_PROTO(ip) >= MAX_SLOTS
473 || hlen +
sizeof(
struct tcp_hdr) > nb->len
474 || (hlen += TCPH_HDRLEN(((
struct tcp_hdr *)&((
char *)ip)[hlen])) << 2)
477 PPPDEBUG(LOG_INFO, (
"vj_uncompress_uncomp: bad cid=%d, hlen=%d buflen=%d\n",
478 IPH_PROTO(ip), hlen, nb->
len));
479 comp->flags |= VJF_TOSS;
483 cs = &comp->rstate[comp->last_recv = IPH_PROTO(ip)];
484 comp->flags &=~ VJF_TOSS;
485 IPH_PROTO_SET(ip, IP_PROTO_TCP);
486 MEMCPY(&cs->cs_ip, ip, hlen);
487 cs->cs_hlen = (u16_t)hlen;
488 INCR(vjs_uncompressedin);
501 vj_uncompress_tcp(
struct pbuf **nb,
struct vjcompress *comp)
507 struct pbuf *n0 = *nb;
509 u32_t vjlen, hlen, changes;
511 INCR(vjs_compressedin);
514 if (changes & NEW_C) {
519 if (*cp >= MAX_SLOTS) {
520 PPPDEBUG(LOG_INFO, (
"vj_uncompress_tcp: bad cid=%d\n", *cp));
524 comp->flags &=~ VJF_TOSS;
525 comp->last_recv = *cp++;
532 if (comp->flags & VJF_TOSS) {
533 PPPDEBUG(LOG_INFO, (
"vj_uncompress_tcp: tossing\n"));
538 cs = &comp->rstate[comp->last_recv];
539 hlen = IPH_HL(&cs->cs_ip) << 2;
540 th = (
struct tcp_hdr *)&((u8_t*)&cs->cs_ip)[hlen];
541 th->chksum = lwip_htons((*cp << 8) | cp[1]);
543 if (changes & TCP_PUSH_BIT) {
544 TCPH_SET_FLAG(th, TCP_PSH);
546 TCPH_UNSET_FLAG(th, TCP_PSH);
549 switch (changes & SPECIALS_MASK) {
552 u32_t i = lwip_ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
554 tmp = lwip_ntohl(th->ackno) + i;
555 th->ackno = lwip_htonl(tmp);
556 tmp = lwip_ntohl(th->seqno) + i;
557 th->seqno = lwip_htonl(tmp);
563 tmp = lwip_ntohl(th->seqno) + lwip_ntohs(IPH_LEN(&cs->cs_ip)) - cs->cs_hlen;
564 th->seqno = lwip_htonl(tmp);
568 if (changes & NEW_U) {
569 TCPH_SET_FLAG(th, TCP_URG);
572 TCPH_UNSET_FLAG(th, TCP_URG);
574 if (changes & NEW_W) {
577 if (changes & NEW_A) {
580 if (changes & NEW_S) {
585 if (changes & NEW_I) {
586 DECODES(cs->cs_ip._id);
588 IPH_ID_SET(&cs->cs_ip, lwip_ntohs(IPH_ID(&cs->cs_ip)) + 1);
589 IPH_ID_SET(&cs->cs_ip, lwip_htons(IPH_ID(&cs->cs_ip)));
597 vjlen = (u16_t)(cp - (u8_t*)n0->
payload);
598 if (n0->
len < vjlen) {
603 PPPDEBUG(LOG_INFO, (
"vj_uncompress_tcp: head buffer %d too short %d\n",
608 #if BYTE_ORDER == LITTLE_ENDIAN 609 tmp = n0->
tot_len - vjlen + cs->cs_hlen;
610 IPH_LEN_SET(&cs->cs_ip, lwip_htons((u16_t)tmp));
612 IPH_LEN_SET(&cs->cs_ip, lwip_htons(n0->
tot_len - vjlen + cs->cs_hlen));
616 bp = (
struct vj_u16_t*) &cs->cs_ip;
617 IPH_CHKSUM_SET(&cs->cs_ip, 0);
618 for (tmp = 0; hlen > 0; hlen -= 2) {
621 tmp = (tmp & 0xffff) + (tmp >> 16);
622 tmp = (tmp & 0xffff) + (tmp >> 16);
623 IPH_CHKSUM_SET(&cs->cs_ip, (u16_t)(~tmp));
628 LWIP_ASSERT(
"pbuf_header failed\n", 0);
646 PPPDEBUG(LOG_WARNING, (
"vj_uncompress_tcp: realign failed\n"));
652 LWIP_ASSERT(
"pbuf_header failed\n", 0);
657 for(q = np; q != NULL; q = q->
next) {
673 LWIP_ASSERT(
"vj_uncompress_tcp: cs->cs_hlen <= PBUF_POOL_BUFSIZE", cs->cs_hlen <=
PBUF_POOL_BUFSIZE);
676 PPPDEBUG(LOG_WARNING, (
"vj_uncompress_tcp: prepend failed\n"));
682 LWIP_ASSERT(
"n0->len >= cs->cs_hlen", n0->
len >= cs->cs_hlen);
683 MEMCPY(n0->
payload, &cs->cs_ip, cs->cs_hlen);
690 comp->
flags |= VJF_TOSS;
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define PACK_STRUCT_BEGIN
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
void pbuf_chain(struct pbuf *h, struct pbuf *t)
void pbuf_cat(struct pbuf *h, struct pbuf *t)
struct pbuf * pbuf_dechain(struct pbuf *p)
#define PACK_STRUCT_FIELD(x)
#define PBUF_POOL_BUFSIZE
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
#define LWIP_MEM_ALIGN(addr)
u8_t pbuf_free(struct pbuf *p)