Deleted Added
full compact
ng_pred1.c (175706) ng_pred1.c (187405)
1/*-
2 * Copyright (c) 2006 Alexander Motin <mav@alkar.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
1/*-
2 * Copyright (c) 2006 Alexander Motin <mav@alkar.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 10 unchanged lines hidden (view full) ---

19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/netgraph/ng_pred1.c 175706 2008-01-27 02:04:12Z mav $
27 * $FreeBSD: head/sys/netgraph/ng_pred1.c 187405 2009-01-18 19:25:36Z mav $
28 */
29
30/*
31 * Predictor-1 PPP compression netgraph node type.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>

--- 359 unchanged lines hidden (view full) ---

395
396 /* Reserve space for expansion. */
397 if (inlen > (PRED1_BUF_SIZE*8/9 + 1 + 4)) {
398 priv->stats.Errors++;
399 NG_FREE_M(m);
400 return (ENOMEM);
401 }
402
28 */
29
30/*
31 * Predictor-1 PPP compression netgraph node type.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>

--- 359 unchanged lines hidden (view full) ---

395
396 /* Reserve space for expansion. */
397 if (inlen > (PRED1_BUF_SIZE*8/9 + 1 + 4)) {
398 priv->stats.Errors++;
399 NG_FREE_M(m);
400 return (ENOMEM);
401 }
402
403 /* We must own the mbuf chain exclusively to modify it. */
404 m = m_unshare(m, M_DONTWAIT);
405 if (m == NULL) {
406 priv->stats.Errors++;
407 return (ENOMEM);
408 }
409
403 /* Work with contiguous regions of memory. */
404 m_copydata(m, 0, inlen, (caddr_t)(priv->inbuf + 2));
405
410 /* Work with contiguous regions of memory. */
411 m_copydata(m, 0, inlen, (caddr_t)(priv->inbuf + 2));
412
406 NG_FREE_M(m);
407
408 lenn = htons(inlen & 0x7FFF);
409
410 /* Compute FCS. */
411 fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
412 fcs = Crc16(fcs, priv->inbuf + 2, inlen);
413 fcs = ~fcs;
414
415 /* Compress data. */

--- 16 unchanged lines hidden (view full) ---

432 /* Add FCS. */
433 (out + outlen)[0] = fcs & 0xFF;
434 (out + outlen)[1] = fcs >> 8;
435
436 /* Calculate resulting size. */
437 outlen += 2;
438
439 /* Return packet in an mbuf. */
413 lenn = htons(inlen & 0x7FFF);
414
415 /* Compute FCS. */
416 fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
417 fcs = Crc16(fcs, priv->inbuf + 2, inlen);
418 fcs = ~fcs;
419
420 /* Compress data. */

--- 16 unchanged lines hidden (view full) ---

437 /* Add FCS. */
438 (out + outlen)[0] = fcs & 0xFF;
439 (out + outlen)[1] = fcs >> 8;
440
441 /* Calculate resulting size. */
442 outlen += 2;
443
444 /* Return packet in an mbuf. */
440 *resultp = m_devget((caddr_t)out, outlen, 0, NULL, NULL);
441 if (*resultp == NULL) {
442 priv->stats.Errors++;
443 return (ENOMEM);
444 };
445
445 m_copyback(m, 0, outlen, (caddr_t)out);
446 if (m->m_pkthdr.len < outlen) {
447 m_freem(m);
448 priv->stats.Errors++;
449 return (ENOMEM);
450 } else if (outlen < m->m_pkthdr.len)
451 m_adj(m, outlen - m->m_pkthdr.len);
452 *resultp = m;
446 priv->stats.OutOctets += outlen;
447
448 return (0);
449}
450
451/*
452 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
453 * The original mbuf is not free'd.

--- 12 unchanged lines hidden (view full) ---

466 inlen = m->m_pkthdr.len;
467
468 if (inlen > PRED1_BUF_SIZE) {
469 priv->stats.Errors++;
470 NG_FREE_M(m);
471 return (ENOMEM);
472 }
473
453 priv->stats.OutOctets += outlen;
454
455 return (0);
456}
457
458/*
459 * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
460 * The original mbuf is not free'd.

--- 12 unchanged lines hidden (view full) ---

473 inlen = m->m_pkthdr.len;
474
475 if (inlen > PRED1_BUF_SIZE) {
476 priv->stats.Errors++;
477 NG_FREE_M(m);
478 return (ENOMEM);
479 }
480
481 /* We must own the mbuf chain exclusively to modify it. */
482 m = m_unshare(m, M_DONTWAIT);
483 if (m == NULL) {
484 priv->stats.Errors++;
485 return (ENOMEM);
486 }
487
474 /* Work with contiguous regions of memory. */
475 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
476
477 priv->stats.InOctets += inlen;
478
479 /* Get initial length value. */
480 len = priv->inbuf[0] << 8;
481 len += priv->inbuf[1];
482
483 cf = (len & 0x8000);
484 len &= 0x7fff;
485
486 /* Is data compressed or not really? */
487 if (cf) {
488 /* Work with contiguous regions of memory. */
489 m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
490
491 priv->stats.InOctets += inlen;
492
493 /* Get initial length value. */
494 len = priv->inbuf[0] << 8;
495 len += priv->inbuf[1];
496
497 cf = (len & 0x8000);
498 len &= 0x7fff;
499
500 /* Is data compressed or not really? */
501 if (cf) {
488 NG_FREE_M(m);
489
490 priv->stats.FramesComp++;
491 len1 = Pred1Decompress(node, priv->inbuf + 2, priv->outbuf,
492 inlen - 4, PRED1_BUF_SIZE);
493 if (len != len1) {
494 /* Error is detected. Send reset request */
502 priv->stats.FramesComp++;
503 len1 = Pred1Decompress(node, priv->inbuf + 2, priv->outbuf,
504 inlen - 4, PRED1_BUF_SIZE);
505 if (len != len1) {
506 /* Error is detected. Send reset request */
507 m_freem(m);
495 priv->stats.Errors++;
496 log(LOG_NOTICE, "ng_pred1: Comp length error (%d) "
497 "--> len (%d)\n", len, len1);
498 return (EIO);
499 }
500
501 /*
502 * CRC check on receive is defined in RFC. It is surely required
503 * for compressed frames to signal dictionary corruption,
504 * but it is actually useless for uncompressed frames because
505 * the same check has already done by HDLC and/or other layer.
506 */
507 lenn = htons(len);
508 fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
509 fcs = Crc16(fcs, priv->outbuf, len);
510 fcs = Crc16(fcs, priv->inbuf + inlen - 2, 2);
511
512 if (fcs != PPP_GOODFCS) {
508 priv->stats.Errors++;
509 log(LOG_NOTICE, "ng_pred1: Comp length error (%d) "
510 "--> len (%d)\n", len, len1);
511 return (EIO);
512 }
513
514 /*
515 * CRC check on receive is defined in RFC. It is surely required
516 * for compressed frames to signal dictionary corruption,
517 * but it is actually useless for uncompressed frames because
518 * the same check has already done by HDLC and/or other layer.
519 */
520 lenn = htons(len);
521 fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
522 fcs = Crc16(fcs, priv->outbuf, len);
523 fcs = Crc16(fcs, priv->inbuf + inlen - 2, 2);
524
525 if (fcs != PPP_GOODFCS) {
526 m_freem(m);
513 priv->stats.Errors++;
514 log(LOG_NOTICE, "ng_pred1: Pred1: Bad CRC-16\n");
515 return (EIO);
516 }
517
518 /* Return packet in an mbuf. */
527 priv->stats.Errors++;
528 log(LOG_NOTICE, "ng_pred1: Pred1: Bad CRC-16\n");
529 return (EIO);
530 }
531
532 /* Return packet in an mbuf. */
519 *resultp = m_devget((caddr_t)priv->outbuf, len, 0, NULL, NULL);
520 if (*resultp == NULL) {
533 m_copyback(m, 0, len, (caddr_t)priv->outbuf);
534 if (m->m_pkthdr.len < len) {
535 m_freem(m);
521 priv->stats.Errors++;
522 return (ENOMEM);
536 priv->stats.Errors++;
537 return (ENOMEM);
523 };
538 } else if (len < m->m_pkthdr.len)
539 m_adj(m, len - m->m_pkthdr.len);
540 *resultp = m;
524
525 } else {
526 priv->stats.FramesUncomp++;
527 if (len != (inlen - 4)) {
528 /* Wrong length. Send reset request */
529 priv->stats.Errors++;
530 log(LOG_NOTICE, "ng_pred1: Uncomp length error (%d) "
531 "--> len (%d)\n", len, inlen - 4);

--- 167 unchanged lines hidden ---
541
542 } else {
543 priv->stats.FramesUncomp++;
544 if (len != (inlen - 4)) {
545 /* Wrong length. Send reset request */
546 priv->stats.Errors++;
547 log(LOG_NOTICE, "ng_pred1: Uncomp length error (%d) "
548 "--> len (%d)\n", len, inlen - 4);

--- 167 unchanged lines hidden ---