146686Sbrian/*-
246686Sbrian * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
346686Sbrian * All rights reserved.
446686Sbrian *
546686Sbrian * Redistribution and use in source and binary forms, with or without
646686Sbrian * modification, are permitted provided that the following conditions
746686Sbrian * are met:
846686Sbrian * 1. Redistributions of source code must retain the above copyright
946686Sbrian *    notice, this list of conditions and the following disclaimer.
1046686Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1146686Sbrian *    notice, this list of conditions and the following disclaimer in the
1246686Sbrian *    documentation and/or other materials provided with the distribution.
1346686Sbrian *
1446686Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1546686Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1646686Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1746686Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1846686Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1946686Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2046686Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2146686Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2246686Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2346686Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2446686Sbrian * SUCH DAMAGE.
2546686Sbrian *
2650479Speter * $FreeBSD$
2746686Sbrian */
2846686Sbrian
2946686Sbrian#include <sys/types.h>
3046686Sbrian
3146686Sbrian#include <stdio.h>
3246686Sbrian#include <termios.h>
3346686Sbrian
3446686Sbrian#include "layer.h"
3546686Sbrian#include "acf.h"
3646686Sbrian#include "defs.h"
3747061Sbrian#include "log.h"
3846686Sbrian#include "timer.h"
3946686Sbrian#include "fsm.h"
4046686Sbrian#include "mbuf.h"
4146686Sbrian#include "proto.h"
4246686Sbrian#include "throughput.h"
4346686Sbrian#include "lqr.h"
4446686Sbrian#include "hdlc.h"
4563484Sbrian#include "lcp.h"
4646686Sbrian#include "ccp.h"
4746686Sbrian#include "link.h"
4846686Sbrian
4946686Sbrianint
5046686Sbrianproto_WrapperOctets(struct lcp *lcp, u_short proto)
5146686Sbrian{
5246686Sbrian  return (lcp->his_protocomp && !(proto & 0xff00)) ? 1 : 2;
5346686Sbrian}
5446686Sbrian
5546686Sbrianstruct mbuf *
5646686Sbrianproto_Prepend(struct mbuf *bp, u_short proto, unsigned comp, int extra)
5746686Sbrian{
5846686Sbrian  u_char cp[2];
5946686Sbrian
6046686Sbrian  cp[0] = proto >> 8;
6146686Sbrian  cp[1] = proto & 0xff;
6246686Sbrian
6346686Sbrian  if (comp && cp[0] == 0)
6454912Sbrian    bp = m_prepend(bp, cp + 1, 1, extra);
6546686Sbrian  else
6654912Sbrian    bp = m_prepend(bp, cp, 2, extra);
6746686Sbrian
6846686Sbrian  return bp;
6946686Sbrian}
7046686Sbrian
7146686Sbrianstatic struct mbuf *
72134789Sbrianproto_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
73134789Sbrian                int pri __unused, u_short *proto)
7446686Sbrian{
7547061Sbrian  log_Printf(LogDEBUG, "proto_LayerPush: Using 0x%04x\n", *proto);
7646686Sbrian  bp = proto_Prepend(bp, *proto, l->lcp.his_protocomp,
7746686Sbrian                     acf_WrapperOctets(&l->lcp, *proto));
7854912Sbrian  m_settype(bp, MB_PROTOOUT);
7946686Sbrian  link_ProtocolRecord(l, *proto, PROTO_OUT);
8046686Sbrian
8146686Sbrian  return bp;
8246686Sbrian}
8346686Sbrian
8446686Sbrianstatic struct mbuf *
85134789Sbrianproto_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
8646686Sbrian                u_short *proto)
8746686Sbrian{
8846686Sbrian  u_char cp[2];
8946686Sbrian  size_t got;
9046686Sbrian
9146686Sbrian  if ((got = mbuf_View(bp, cp, 2)) == 0) {
9254912Sbrian    m_freem(bp);
9346686Sbrian    return NULL;
9446686Sbrian  }
9546686Sbrian
9646686Sbrian  *proto = cp[0];
9746686Sbrian  if (!(*proto & 1)) {
9846686Sbrian    if (got == 1) {
9954912Sbrian      m_freem(bp);
10046686Sbrian      return NULL;
10146686Sbrian    }
10246686Sbrian    bp = mbuf_Read(bp, cp, 2);
10346686Sbrian    *proto = (*proto << 8) | cp[1];
10446686Sbrian  } else
10546686Sbrian    bp = mbuf_Read(bp, cp, 1);
10646686Sbrian
10747061Sbrian  log_Printf(LogDEBUG, "proto_LayerPull: unknown -> 0x%04x\n", *proto);
10854912Sbrian  m_settype(bp, MB_PROTOIN);
10946686Sbrian  link_ProtocolRecord(l, *proto, PROTO_IN);
11046686Sbrian
11146686Sbrian  return bp;
11246686Sbrian}
11346686Sbrian
11446686Sbrianstruct layer protolayer =
11546686Sbrian  { LAYER_PROTO, "proto", proto_LayerPush, proto_LayerPull };
116