ip.c revision 51809
16059Samurai/*
26059Samurai *		PPP IP Protocol Interface
36059Samurai *
46059Samurai *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
56059Samurai *
66059Samurai *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
76059Samurai *
86059Samurai * Redistribution and use in source and binary forms are permitted
96059Samurai * provided that the above copyright notice and this paragraph are
106059Samurai * duplicated in all such forms and that any documentation,
116059Samurai * advertising materials, and other materials related to such
126059Samurai * distribution and use acknowledge that the software was developed
136059Samurai * by the Internet Initiative Japan.  The name of the
146059Samurai * IIJ may not be used to endorse or promote products derived
156059Samurai * from this software without specific prior written permission.
166059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
176059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
186059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
198857Srgrimes *
2050479Speter * $FreeBSD: head/usr.sbin/ppp/ip.c 51809 1999-09-30 07:23:13Z brian $
218857Srgrimes *
226059Samurai *	TODO:
236059Samurai *		o Return ICMP message for filterd packet
246059Samurai *		  and optionaly record it into log.
256059Samurai */
2643313Sbrian#include <sys/param.h>
2746086Sbrian#if defined(__OpenBSD__) || defined(__NetBSD__)
2831195Sbrian#include <sys/socket.h>
2938174Sbrian#endif
3030715Sbrian#include <netinet/in.h>
316059Samurai#include <netinet/in_systm.h>
326059Samurai#include <netinet/ip.h>
336059Samurai#include <netinet/ip_icmp.h>
346059Samurai#include <netinet/udp.h>
356059Samurai#include <netinet/tcp.h>
3613389Sphk#include <arpa/inet.h>
3736285Sbrian#include <sys/un.h>
3830715Sbrian
3930092Sbrian#include <errno.h>
4030715Sbrian#include <stdio.h>
4130715Sbrian#include <string.h>
4246686Sbrian#include <termios.h>
4330715Sbrian#include <unistd.h>
4430715Sbrian
4546686Sbrian#include "layer.h"
4646686Sbrian#include "proto.h"
4730715Sbrian#include "mbuf.h"
4830715Sbrian#include "log.h"
4930715Sbrian#include "defs.h"
5030715Sbrian#include "timer.h"
5130715Sbrian#include "fsm.h"
5236285Sbrian#include "lqr.h"
5330715Sbrian#include "hdlc.h"
5436285Sbrian#include "throughput.h"
5536285Sbrian#include "iplist.h"
5636285Sbrian#include "slcompress.h"
5736285Sbrian#include "ipcp.h"
586059Samurai#include "filter.h"
5936285Sbrian#include "descriptor.h"
6036285Sbrian#include "lcp.h"
6136285Sbrian#include "ccp.h"
6236285Sbrian#include "link.h"
6336285Sbrian#include "mp.h"
6443313Sbrian#ifndef NORADIUS
6543313Sbrian#include "radius.h"
6643313Sbrian#endif
6736285Sbrian#include "bundle.h"
6831195Sbrian#include "tun.h"
6930715Sbrian#include "ip.h"
706059Samurai
7131343Sbrianstatic const char *TcpFlags[] = { "FIN", "SYN", "RST", "PSH", "ACK", "URG" };
726059Samurai
7349140Sbrianstatic __inline int
7428679SbrianPortMatch(int op, u_short pport, u_short rport)
756059Samurai{
766059Samurai  switch (op) {
7749140Sbrian  case OP_EQ:
7828679Sbrian    return (pport == rport);
796059Samurai  case OP_GT:
8028679Sbrian    return (pport > rport);
816059Samurai  case OP_LT:
8228679Sbrian    return (pport < rport);
836059Samurai  default:
8428679Sbrian    return (0);
856059Samurai  }
866059Samurai}
876059Samurai
886059Samurai/*
8946686Sbrian *  Check a packet against a defined filter
9049140Sbrian *  Returns 0 to accept the packet, non-zero to drop the packet
9149140Sbrian *
9249140Sbrian *  If filtering is enabled, the initial fragment of a datagram must
9349140Sbrian *  contain the complete protocol header, and subsequent fragments
9449140Sbrian *  must not attempt to over-write it.
956059Samurai */
966059Samuraistatic int
9749140SbrianFilterCheck(const struct ip *pip, const struct filter *filter)
986059Samurai{
9949140Sbrian  int gotinfo;			/* true if IP payload decoded */
10049140Sbrian  int cproto;			/* P_* protocol type if (gotinfo) */
10149140Sbrian  int estab, syn, finrst;	/* TCP state flags if (gotinfo) */
10249140Sbrian  u_short sport, dport;		/* src, dest port from packet if (gotinfo) */
10349140Sbrian  int n;			/* filter rule to process */
10449140Sbrian  int len;			/* bytes used in dbuff */
10549140Sbrian  int didname;			/* true if filter header printed */
10649140Sbrian  int match;			/* true if condition matched */
10749140Sbrian  const struct filterent *fp = filter->rule;
10836285Sbrian  char dbuff[100];
1096059Samurai
11049140Sbrian  if (fp->f_action == A_NONE)
11149140Sbrian    return (0);		/* No rule is given. Permit this packet */
11236285Sbrian
11349140Sbrian  /* Deny any packet fragment that tries to over-write the header.
11449140Sbrian   * Since we no longer have the real header available, punt on the
11549140Sbrian   * largest normal header - 20 bytes for TCP without options, rounded
11649140Sbrian   * up to the next possible fragment boundary.  Since the smallest
11749140Sbrian   * `legal' MTU is 576, and the smallest recommended MTU is 296, any
11849140Sbrian   * fragmentation within this range is dubious at best */
11949140Sbrian  len = ntohs(pip->ip_off) & IP_OFFMASK;	/* fragment offset */
12049140Sbrian  if (len > 0) {		/* Not first fragment within datagram */
12149140Sbrian    if (len < (24 >> 3))	/* don't allow fragment to over-write header */
12249140Sbrian      return (1);
12349140Sbrian    /* permit fragments on in and out filter */
12451333Sbrian    return (!filter->fragok);
12549140Sbrian  }
12649140Sbrian
12749140Sbrian  cproto = gotinfo = estab = syn = finrst = didname = 0;
12849140Sbrian  sport = dport = 0;
12949140Sbrian  for (n = 0; n < MAXFILTERS; ) {
13049140Sbrian    if (fp->f_action == A_NONE) {
13149140Sbrian      n++;
13249140Sbrian      fp++;
13349140Sbrian      continue;
13449140Sbrian    }
13536285Sbrian
13649140Sbrian    if (!didname) {
13749140Sbrian      log_Printf(LogDEBUG, "%s filter:\n", filter->name);
13849140Sbrian      didname = 1;
13949140Sbrian    }
1406059Samurai
14149140Sbrian    match = 0;
14249140Sbrian    if (!((pip->ip_src.s_addr ^ fp->f_src.ipaddr.s_addr) &
14349140Sbrian	  fp->f_src.mask.s_addr) &&
14449140Sbrian	!((pip->ip_dst.s_addr ^ fp->f_dst.ipaddr.s_addr) &
14549140Sbrian	  fp->f_dst.mask.s_addr)) {
14649140Sbrian      if (fp->f_proto != P_NONE) {
14749140Sbrian	if (!gotinfo) {
14849140Sbrian	  const char *ptop = (const char *) pip + (pip->ip_hl << 2);
14949140Sbrian	  const struct tcphdr *th;
15049140Sbrian	  const struct udphdr *uh;
15149140Sbrian	  const struct icmp *ih;
15249140Sbrian	  int datalen;	/* IP datagram length */
15349140Sbrian
15449140Sbrian	  datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
15549140Sbrian	  switch (pip->ip_p) {
15649140Sbrian	  case IPPROTO_ICMP:
15749140Sbrian	    cproto = P_ICMP;
15849140Sbrian	    if (datalen < 8)	/* ICMP must be at least 8 octets */
15949140Sbrian	      return (1);
16049140Sbrian	    ih = (const struct icmp *) ptop;
16149140Sbrian	    sport = ih->icmp_type;
16249140Sbrian	    estab = syn = finrst = -1;
16349140Sbrian	    if (log_IsKept(LogDEBUG))
16449140Sbrian	      snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
16549140Sbrian	    break;
16649140Sbrian	  case IPPROTO_IGMP:
16749140Sbrian	    cproto = P_IGMP;
16849140Sbrian	    if (datalen < 8)	/* IGMP uses 8-octet messages */
16949140Sbrian	      return (1);
17049140Sbrian	    estab = syn = finrst = -1;
17149140Sbrian	    sport = ntohs(0);
17249140Sbrian	    break;
17351809Sbrian#ifdef IPPROTO_GRE
17451809Sbrian          case IPPROTO_GRE:
17551809Sbrian            cproto = P_GRE;
17651809Sbrian            if (datalen < 2)    /* GRE uses 2-octet+ messages */
17751809Sbrian              return (1);
17851809Sbrian            estab = syn = finrst = -1;
17951809Sbrian            sport = ntohs(0);
18051809Sbrian            break;
18151809Sbrian#endif
18249374Sbrian#ifdef IPPROTO_OSPFIGP
18349372Sbrian	  case IPPROTO_OSPFIGP:
18449372Sbrian	    cproto = P_OSPF;
18549372Sbrian	    if (datalen < 8)	/* IGMP uses 8-octet messages */
18649372Sbrian	      return (1);
18749372Sbrian	    estab = syn = finrst = -1;
18849372Sbrian	    sport = ntohs(0);
18949372Sbrian	    break;
19049374Sbrian#endif
19149140Sbrian	  case IPPROTO_UDP:
19249140Sbrian	  case IPPROTO_IPIP:
19349140Sbrian	    cproto = P_UDP;
19449140Sbrian	    if (datalen < 8)	/* UDP header is 8 octets */
19549140Sbrian	      return (1);
19649140Sbrian	    uh = (const struct udphdr *) ptop;
19749140Sbrian	    sport = ntohs(uh->uh_sport);
19849140Sbrian	    dport = ntohs(uh->uh_dport);
19949140Sbrian	    estab = syn = finrst = -1;
20049140Sbrian	    if (log_IsKept(LogDEBUG))
20149140Sbrian	      snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
20249140Sbrian		       sport, dport);
20349140Sbrian	    break;
20449140Sbrian	  case IPPROTO_TCP:
20549140Sbrian	    cproto = P_TCP;
20649140Sbrian	    th = (const struct tcphdr *) ptop;
20749140Sbrian	    /* TCP headers are variable length.  The following code
20849140Sbrian	     * ensures that the TCP header length isn't de-referenced if
20949140Sbrian	     * the datagram is too short
21049140Sbrian	     */
21149140Sbrian	    if (datalen < 20 || datalen < (th->th_off << 2))
21249140Sbrian	      return (1);
21349140Sbrian	    sport = ntohs(th->th_sport);
21449140Sbrian	    dport = ntohs(th->th_dport);
21549140Sbrian	    estab = (th->th_flags & TH_ACK);
21649140Sbrian	    syn = (th->th_flags & TH_SYN);
21749140Sbrian	    finrst = (th->th_flags & (TH_FIN|TH_RST));
21849140Sbrian	    if (log_IsKept(LogDEBUG)) {
21949140Sbrian	      if (!estab)
22049140Sbrian		snprintf(dbuff, sizeof dbuff,
22149140Sbrian			 "flags = %02x, sport = %d, dport = %d",
22249140Sbrian			 th->th_flags, sport, dport);
22349140Sbrian	      else
22449140Sbrian		*dbuff = '\0';
2256059Samurai	    }
22649140Sbrian	    break;
22749140Sbrian	  default:
22849140Sbrian	    return (1);	/* We'll block unknown type of packet */
22949140Sbrian	  }
23026516Sbrian
23149140Sbrian	  if (log_IsKept(LogDEBUG)) {
23249140Sbrian	    if (estab != -1) {
23349140Sbrian	      len = strlen(dbuff);
23449140Sbrian	      snprintf(dbuff + len, sizeof dbuff - len,
23549140Sbrian		       ", estab = %d, syn = %d, finrst = %d",
23649140Sbrian		       estab, syn, finrst);
2376059Samurai	    }
23849140Sbrian	    log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
23949140Sbrian		       filter_Proto2Nam(cproto), dbuff);
2406059Samurai	  }
24149140Sbrian	  gotinfo = 1;
24249140Sbrian	}
24349140Sbrian	if (log_IsKept(LogDEBUG)) {
24449140Sbrian	  if (fp->f_srcop != OP_NONE) {
24549140Sbrian	    snprintf(dbuff, sizeof dbuff, ", src %s %d",
24649140Sbrian		     filter_Op2Nam(fp->f_srcop), fp->f_srcport);
24749140Sbrian	    len = strlen(dbuff);
24849140Sbrian	  } else
24949140Sbrian	    len = 0;
25049140Sbrian	  if (fp->f_dstop != OP_NONE) {
25149140Sbrian	    snprintf(dbuff + len, sizeof dbuff - len,
25249140Sbrian		     ", dst %s %d", filter_Op2Nam(fp->f_dstop),
25349140Sbrian		     fp->f_dstport);
25449140Sbrian	  } else if (!len)
25549140Sbrian	    *dbuff = '\0';
25649140Sbrian
25749140Sbrian	  log_Printf(LogDEBUG, "  rule = %d: Address match, "
25849140Sbrian		     "check against proto %s%s, action = %s\n",
25949140Sbrian		     n, filter_Proto2Nam(fp->f_proto),
26049140Sbrian		     dbuff, filter_Action2Nam(fp->f_action));
26149140Sbrian	}
26249140Sbrian
26349140Sbrian	if (cproto == fp->f_proto) {
26449140Sbrian	  if ((fp->f_srcop == OP_NONE ||
26549140Sbrian	       PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
26649140Sbrian	      (fp->f_dstop == OP_NONE ||
26749140Sbrian	       PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
26849140Sbrian	      (fp->f_estab == 0 || estab) &&
26949140Sbrian	      (fp->f_syn == 0 || syn) &&
27049140Sbrian	      (fp->f_finrst == 0 || finrst)) {
27149140Sbrian	    match = 1;
27249140Sbrian	  }
27349140Sbrian	}
27449140Sbrian      } else {
27549140Sbrian	/* Address is matched and no protocol specified. Make a decision. */
27649140Sbrian	log_Printf(LogDEBUG, "  rule = %d: Address match, action = %s\n", n,
27749140Sbrian		   filter_Action2Nam(fp->f_action));
27849140Sbrian	match = 1;
2796059Samurai      }
28049140Sbrian    } else
28149140Sbrian      log_Printf(LogDEBUG, "  rule = %d: Address mismatch\n", n);
28249140Sbrian
28349140Sbrian    if (match != fp->f_invert) {
28449140Sbrian      /* Take specified action */
28549140Sbrian      if (fp->f_action < A_NONE)
28649140Sbrian	fp = &filter->rule[n = fp->f_action];
28749140Sbrian      else
28849140Sbrian	return (fp->f_action != A_PERMIT);
28949140Sbrian    } else {
29049140Sbrian      n++;
2916059Samurai      fp++;
2926059Samurai    }
2936059Samurai  }
29449140Sbrian  return (1);		/* No rule is mached. Deny this packet */
2956059Samurai}
2966059Samurai
29736285Sbrian#ifdef notdef
2986059Samuraistatic void
29946828SbrianIcmpError(struct ip *pip, int code)
3006059Samurai{
3016059Samurai  struct mbuf *bp;
3026059Samurai
3036059Samurai  if (pip->ip_p != IPPROTO_ICMP) {
30436285Sbrian    bp = mbuf_Alloc(cnt, MB_IPIN);
30530715Sbrian    memcpy(MBUF_CTOP(bp), ptr, cnt);
30636285Sbrian    vj_SendFrame(bp);
30736285Sbrian    ipcp_AddOutOctets(cnt);
3086059Samurai  }
30936285Sbrian}
3106059Samurai#endif
3116059Samurai
3126059Samurai/*
3136059Samurai *  For debugging aid.
3146059Samurai */
3156059Samuraiint
31636285SbrianPacketCheck(struct bundle *bundle, char *cp, int nb, struct filter *filter)
3176059Samurai{
3186059Samurai  struct ip *pip;
3196059Samurai  struct tcphdr *th;
3206059Samurai  struct udphdr *uh;
3216059Samurai  struct icmp *icmph;
3226059Samurai  char *ptop;
3236059Samurai  int mask, len, n;
32450867Sbrian  int pri = 0;
32526692Sbrian  int logit, loglen;
32637010Sbrian  char logbuf[200];
3276059Samurai
32836285Sbrian  logit = log_IsKept(LogTCPIP) && filter->logok;
32926692Sbrian  loglen = 0;
3306059Samurai
33128679Sbrian  pip = (struct ip *) cp;
3328857Srgrimes
33326692Sbrian  if (logit && loglen < sizeof logbuf) {
33436285Sbrian    snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
33528679Sbrian    loglen += strlen(logbuf + loglen);
33626692Sbrian  }
3376059Samurai  ptop = (cp + (pip->ip_hl << 2));
3386059Samurai
3396059Samurai  switch (pip->ip_p) {
3406059Samurai  case IPPROTO_ICMP:
34126692Sbrian    if (logit && loglen < sizeof logbuf) {
34228679Sbrian      icmph = (struct icmp *) ptop;
34328679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
34428679Sbrian	     "ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type);
34528679Sbrian      loglen += strlen(logbuf + loglen);
34628679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
34728679Sbrian	       "%s:%d", inet_ntoa(pip->ip_dst), icmph->icmp_type);
34828679Sbrian      loglen += strlen(logbuf + loglen);
3496059Samurai    }
3506059Samurai    break;
35151048Sbrian
3526059Samurai  case IPPROTO_UDP:
35351048Sbrian    uh = (struct udphdr *) ptop;
35451048Sbrian    if (pip->ip_tos == IPTOS_LOWDELAY)
35551048Sbrian      pri++;
35651048Sbrian
35751048Sbrian    if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
35851048Sbrian        ipcp_IsUrgentUdpPort(&bundle->ncp.ipcp, ntohs(uh->uh_sport),
35951048Sbrian                          ntohs(uh->uh_dport)))
36051048Sbrian      pri++;
36151048Sbrian
36226692Sbrian    if (logit && loglen < sizeof logbuf) {
36328679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
36428679Sbrian	   "UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
36528679Sbrian      loglen += strlen(logbuf + loglen);
36628679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
36728679Sbrian	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
36828679Sbrian      loglen += strlen(logbuf + loglen);
3696059Samurai    }
3706059Samurai    break;
37151048Sbrian
37251809Sbrian#ifdef IPPROTO_GRE
37351809Sbrian  case IPPROTO_GRE:
37451809Sbrian    if (logit && loglen < sizeof logbuf) {
37551809Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
37651809Sbrian          "GRE: %s ---> ", inet_ntoa(pip->ip_src));
37751809Sbrian      loglen += strlen(logbuf + loglen);
37851809Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
37951809Sbrian              "%s", inet_ntoa(pip->ip_dst));
38051809Sbrian      loglen += strlen(logbuf + loglen);
38151809Sbrian    }
38251809Sbrian    break;
38351809Sbrian#endif
38451809Sbrian
38549374Sbrian#ifdef IPPROTO_OSPFIGP
38649372Sbrian  case IPPROTO_OSPFIGP:
38749372Sbrian    if (logit && loglen < sizeof logbuf) {
38849372Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
38949372Sbrian	   "OSPF: %s ---> ", inet_ntoa(pip->ip_src));
39049372Sbrian      loglen += strlen(logbuf + loglen);
39149372Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
39249372Sbrian	       "%s", inet_ntoa(pip->ip_dst));
39349372Sbrian      loglen += strlen(logbuf + loglen);
39449372Sbrian    }
39549372Sbrian    break;
39649374Sbrian#endif
39751048Sbrian
39836961Sbrian  case IPPROTO_IPIP:
39936961Sbrian    if (logit && loglen < sizeof logbuf) {
40036961Sbrian      uh = (struct udphdr *) ptop;
40136961Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
40236961Sbrian	   "IPIP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
40336961Sbrian      loglen += strlen(logbuf + loglen);
40436961Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
40536961Sbrian	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
40636961Sbrian      loglen += strlen(logbuf + loglen);
40736961Sbrian    }
40836961Sbrian    break;
40951048Sbrian
41036961Sbrian  case IPPROTO_IGMP:
41136961Sbrian    if (logit && loglen < sizeof logbuf) {
41236961Sbrian      uh = (struct udphdr *) ptop;
41336961Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
41436961Sbrian	   "IGMP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
41536961Sbrian      loglen += strlen(logbuf + loglen);
41636961Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
41736961Sbrian	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
41836961Sbrian      loglen += strlen(logbuf + loglen);
41936961Sbrian    }
42036961Sbrian    break;
42151048Sbrian
4226059Samurai  case IPPROTO_TCP:
42328679Sbrian    th = (struct tcphdr *) ptop;
4246059Samurai    if (pip->ip_tos == IPTOS_LOWDELAY)
42550867Sbrian      pri++;
42651048Sbrian
42751048Sbrian    if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
42851048Sbrian        ipcp_IsUrgentTcpPort(&bundle->ncp.ipcp, ntohs(th->th_sport),
42951048Sbrian                          ntohs(th->th_dport)))
43050867Sbrian      pri++;
43150867Sbrian
43226692Sbrian    if (logit && loglen < sizeof logbuf) {
4336059Samurai      len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2);
43428679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
43528679Sbrian	   "TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport));
43628679Sbrian      loglen += strlen(logbuf + loglen);
43728679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
43828679Sbrian	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport));
43928679Sbrian      loglen += strlen(logbuf + loglen);
4406059Samurai      n = 0;
4416059Samurai      for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
44226692Sbrian	if (th->th_flags & mask) {
44328679Sbrian	  snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
44428679Sbrian	  loglen += strlen(logbuf + loglen);
44528679Sbrian	}
4466059Samurai	n++;
4476059Samurai      }
44828679Sbrian      snprintf(logbuf + loglen, sizeof logbuf - loglen,
44937210Sbrian	       "  seq:%lx  ack:%lx (%d/%d)",
45037210Sbrian	       (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
45128679Sbrian      loglen += strlen(logbuf + loglen);
4526059Samurai      if ((th->th_flags & TH_SYN) && nb > 40) {
45328679Sbrian	u_short *sp;
4546059Samurai
4556059Samurai	ptop += 20;
45628679Sbrian	sp = (u_short *) ptop;
45726692Sbrian	if (ntohs(sp[0]) == 0x0204) {
45828679Sbrian	  snprintf(logbuf + loglen, sizeof logbuf - loglen,
45928679Sbrian		   " MSS = %d", ntohs(sp[1]));
46028679Sbrian	  loglen += strlen(logbuf + loglen);
46128679Sbrian	}
4626059Samurai      }
4636059Samurai    }
4646059Samurai    break;
4656059Samurai  }
46626692Sbrian
46749140Sbrian  if (FilterCheck(pip, filter)) {
46831142Sbrian    if (logit)
46936285Sbrian      log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
47036285Sbrian#ifdef notdef
47128679Sbrian    if (direction == 0)
47228679Sbrian      IcmpError(pip, pri);
47336285Sbrian#endif
47428679Sbrian    return (-1);
4756059Samurai  } else {
47636285Sbrian    /* Check Keep Alive filter */
47736285Sbrian    if (logit) {
47849140Sbrian      if (FilterCheck(pip, &bundle->filter.alive))
47936285Sbrian        log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
48036285Sbrian      else
48136285Sbrian        log_Printf(LogTCPIP, "%s\n", logbuf);
4826735Samurai    }
48328679Sbrian    return (pri);
4846059Samurai  }
4856059Samurai}
4866059Samurai
48746686Sbrianstruct mbuf *
48846686Sbrianip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
48936285Sbrian{
4906059Samurai  int nb, nw;
49131343Sbrian  struct tun_data tun;
49246686Sbrian  struct ip *pip;
4936059Samurai
49446686Sbrian  if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
49546686Sbrian    log_Printf(LogWARN, "ip_Input: IPCP not open - packet dropped\n");
49646686Sbrian    mbuf_Free(bp);
49746686Sbrian    return NULL;
4986059Samurai  }
4996059Samurai
50047695Sbrian  mbuf_SetType(bp, MB_IPIN);
50146686Sbrian  tun_fill_header(tun, AF_INET);
50246686Sbrian  nb = mbuf_Length(bp);
50347168Sbrian  if (nb > sizeof tun.data) {
50447168Sbrian    log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
50547168Sbrian               l->name, nb, (int)(sizeof tun.data));
50647168Sbrian    mbuf_Free(bp);
50747168Sbrian    return NULL;
50847168Sbrian  }
50946686Sbrian  mbuf_Read(bp, tun.data, nb);
51026031Sbrian
51146686Sbrian  if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0)
51246686Sbrian    return NULL;
51326031Sbrian
51446686Sbrian  pip = (struct ip *)tun.data;
51549140Sbrian  if (!FilterCheck(pip, &bundle->filter.alive))
51646686Sbrian    bundle_StartIdleTimer(bundle);
51726031Sbrian
51846686Sbrian  ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
51936285Sbrian
52046686Sbrian  nb += sizeof tun - sizeof tun.data;
52146686Sbrian  nw = write(bundle->dev.fd, &tun, nb);
52246686Sbrian  if (nw != nb) {
52346686Sbrian    if (nw == -1)
52447168Sbrian      log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
52547168Sbrian                 l->name, nb, strerror(errno));
52646686Sbrian    else
52747168Sbrian      log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
52846686Sbrian  }
52936285Sbrian
53046686Sbrian  return NULL;
5316059Samurai}
5326059Samurai
5336059Samuraivoid
53438557Sbrianip_Enqueue(struct ipcp *ipcp, int pri, char *ptr, int count)
5356059Samurai{
5366059Samurai  struct mbuf *bp;
5376059Samurai
53850867Sbrian  if (pri < 0 || pri >= IPCP_QUEUES(ipcp))
53938557Sbrian    log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
54038557Sbrian  else {
54146686Sbrian    /*
54246686Sbrian     * We allocate an extra 6 bytes, four at the front and two at the end.
54346686Sbrian     * This is an optimisation so that we need to do less work in
54446686Sbrian     * mbuf_Prepend() in acf_LayerPush() and proto_LayerPush() and
54546686Sbrian     * appending in hdlc_LayerPush().
54646686Sbrian     */
54747695Sbrian    bp = mbuf_Alloc(count + 6, MB_IPOUT);
54846686Sbrian    bp->offset += 4;
54946686Sbrian    bp->cnt -= 6;
55038557Sbrian    memcpy(MBUF_CTOP(bp), ptr, count);
55150867Sbrian    mbuf_Enqueue(ipcp->Queue + pri, bp);
55238557Sbrian  }
5536059Samurai}
5546059Samurai
55538544Sbrianvoid
55638557Sbrianip_DeleteQueue(struct ipcp *ipcp)
55738544Sbrian{
55838544Sbrian  struct mqueue *queue;
55938544Sbrian
56050867Sbrian  for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++)
56138544Sbrian    while (queue->top)
56238544Sbrian      mbuf_Free(mbuf_Dequeue(queue));
56338544Sbrian}
56438544Sbrian
5658857Srgrimesint
56638557Sbrianip_QueueLen(struct ipcp *ipcp)
5677001Samurai{
5687001Samurai  struct mqueue *queue;
56936285Sbrian  int result = 0;
57028679Sbrian
57150867Sbrian  for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++)
57236285Sbrian    result += queue->qlen;
57336285Sbrian
57436285Sbrian  return result;
5757001Samurai}
5767001Samurai
57736285Sbrianint
57846686Sbrianip_PushPacket(struct link *l, struct bundle *bundle)
5796059Samurai{
58038557Sbrian  struct ipcp *ipcp = &bundle->ncp.ipcp;
5816059Samurai  struct mqueue *queue;
5826059Samurai  struct mbuf *bp;
58346686Sbrian  struct ip *pip;
58425630Sbrian  int cnt;
5856059Samurai
58638557Sbrian  if (ipcp->fsm.state != ST_OPENED)
58736285Sbrian    return 0;
58836285Sbrian
58950867Sbrian  queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1;
59050867Sbrian  do {
5916059Samurai    if (queue->top) {
59245103Sbrian      bp = mbuf_Contiguous(mbuf_Dequeue(queue));
59346686Sbrian      cnt = mbuf_Length(bp);
59446686Sbrian      pip = (struct ip *)MBUF_CTOP(bp);
59549140Sbrian      if (!FilterCheck(pip, &bundle->filter.alive))
59646686Sbrian        bundle_StartIdleTimer(bundle);
59750867Sbrian      link_PushPacket(l, bp, bundle, 0, PROTO_IP);
59846686Sbrian      ipcp_AddOutOctets(ipcp, cnt);
59946686Sbrian      return 1;
6006059Samurai    }
60150867Sbrian  } while (queue-- != ipcp->Queue);
60236285Sbrian
60336285Sbrian  return 0;
6046059Samurai}
605