ip.c revision 56413
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 56413 2000-01-23 01:48:19Z 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> 2731195Sbrian#include <sys/socket.h> 2830715Sbrian#include <netinet/in.h> 296059Samurai#include <netinet/in_systm.h> 306059Samurai#include <netinet/ip.h> 316059Samurai#include <netinet/ip_icmp.h> 326059Samurai#include <netinet/udp.h> 336059Samurai#include <netinet/tcp.h> 3413389Sphk#include <arpa/inet.h> 3536285Sbrian#include <sys/un.h> 3630715Sbrian 3730092Sbrian#include <errno.h> 3830715Sbrian#include <stdio.h> 3930715Sbrian#include <string.h> 4046686Sbrian#include <termios.h> 4130715Sbrian#include <unistd.h> 4230715Sbrian 4346686Sbrian#include "layer.h" 4446686Sbrian#include "proto.h" 4530715Sbrian#include "mbuf.h" 4630715Sbrian#include "log.h" 4730715Sbrian#include "defs.h" 4830715Sbrian#include "timer.h" 4930715Sbrian#include "fsm.h" 5036285Sbrian#include "lqr.h" 5130715Sbrian#include "hdlc.h" 5236285Sbrian#include "throughput.h" 5336285Sbrian#include "iplist.h" 5436285Sbrian#include "slcompress.h" 5536285Sbrian#include "ipcp.h" 566059Samurai#include "filter.h" 5736285Sbrian#include "descriptor.h" 5836285Sbrian#include "lcp.h" 5936285Sbrian#include "ccp.h" 6036285Sbrian#include "link.h" 6136285Sbrian#include "mp.h" 6243313Sbrian#ifndef NORADIUS 6343313Sbrian#include "radius.h" 6443313Sbrian#endif 6536285Sbrian#include "bundle.h" 6631195Sbrian#include "tun.h" 6730715Sbrian#include "ip.h" 686059Samurai 6955146Sbrianstatic const char * const TcpFlags[] = { 7055146Sbrian "FIN", "SYN", "RST", "PSH", "ACK", "URG" 7155146Sbrian}; 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) { 30454912Sbrian bp = m_get(m_len, MB_IPIN); 30554912Sbrian memcpy(MBUF_CTOP(bp), ptr, m_len); 30636285Sbrian vj_SendFrame(bp); 30754912Sbrian ipcp_AddOutOctets(m_len); 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; 49356413Sbrian char *data; 4946059Samurai 49546686Sbrian if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 49646686Sbrian log_Printf(LogWARN, "ip_Input: IPCP not open - packet dropped\n"); 49754912Sbrian m_freem(bp); 49846686Sbrian return NULL; 4996059Samurai } 5006059Samurai 50154912Sbrian m_settype(bp, MB_IPIN); 50254912Sbrian nb = m_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)); 50654912Sbrian m_freem(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 52056413Sbrian if (bundle->dev.header) { 52156413Sbrian tun.family = htonl(AF_INET); 52256413Sbrian nb += sizeof tun - sizeof tun.data; 52356413Sbrian data = (char *)&tun; 52456413Sbrian } else 52556413Sbrian data = tun.data; 52656413Sbrian 52756413Sbrian nw = write(bundle->dev.fd, data, nb); 52846686Sbrian if (nw != nb) { 52946686Sbrian if (nw == -1) 53047168Sbrian log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n", 53147168Sbrian l->name, nb, strerror(errno)); 53246686Sbrian else 53347168Sbrian log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw); 53446686Sbrian } 53536285Sbrian 53646686Sbrian return NULL; 5376059Samurai} 5386059Samurai 5396059Samuraivoid 54038557Sbrianip_Enqueue(struct ipcp *ipcp, int pri, char *ptr, int count) 5416059Samurai{ 5426059Samurai struct mbuf *bp; 5436059Samurai 54450867Sbrian if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) 54538557Sbrian log_Printf(LogERROR, "Can't store in ip queue %d\n", pri); 54638557Sbrian else { 54746686Sbrian /* 54846686Sbrian * We allocate an extra 6 bytes, four at the front and two at the end. 54946686Sbrian * This is an optimisation so that we need to do less work in 55054912Sbrian * m_prepend() in acf_LayerPush() and proto_LayerPush() and 55146686Sbrian * appending in hdlc_LayerPush(). 55246686Sbrian */ 55354912Sbrian bp = m_get(count + 6, MB_IPOUT); 55454912Sbrian bp->m_offset += 4; 55554912Sbrian bp->m_len -= 6; 55638557Sbrian memcpy(MBUF_CTOP(bp), ptr, count); 55754912Sbrian m_enqueue(ipcp->Queue + pri, bp); 55838557Sbrian } 5596059Samurai} 5606059Samurai 56138544Sbrianvoid 56238557Sbrianip_DeleteQueue(struct ipcp *ipcp) 56338544Sbrian{ 56438544Sbrian struct mqueue *queue; 56538544Sbrian 56650867Sbrian for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++) 56738544Sbrian while (queue->top) 56854912Sbrian m_freem(m_dequeue(queue)); 56938544Sbrian} 57038544Sbrian 57154912Sbriansize_t 57238557Sbrianip_QueueLen(struct ipcp *ipcp) 5737001Samurai{ 5747001Samurai struct mqueue *queue; 57554912Sbrian size_t result; 57628679Sbrian 57754912Sbrian result = 0; 57850867Sbrian for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++) 57954912Sbrian result += queue->len; 58036285Sbrian 58136285Sbrian return result; 5827001Samurai} 5837001Samurai 58436285Sbrianint 58546686Sbrianip_PushPacket(struct link *l, struct bundle *bundle) 5866059Samurai{ 58738557Sbrian struct ipcp *ipcp = &bundle->ncp.ipcp; 5886059Samurai struct mqueue *queue; 5896059Samurai struct mbuf *bp; 59046686Sbrian struct ip *pip; 59154912Sbrian int m_len; 5926059Samurai 59338557Sbrian if (ipcp->fsm.state != ST_OPENED) 59436285Sbrian return 0; 59536285Sbrian 59650867Sbrian queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1; 59750867Sbrian do { 5986059Samurai if (queue->top) { 59954912Sbrian bp = m_pullup(m_dequeue(queue)); 60054912Sbrian m_len = m_length(bp); 60146686Sbrian pip = (struct ip *)MBUF_CTOP(bp); 60249140Sbrian if (!FilterCheck(pip, &bundle->filter.alive)) 60346686Sbrian bundle_StartIdleTimer(bundle); 60450867Sbrian link_PushPacket(l, bp, bundle, 0, PROTO_IP); 60554912Sbrian ipcp_AddOutOctets(ipcp, m_len); 60646686Sbrian return 1; 6076059Samurai } 60850867Sbrian } while (queue-- != ipcp->Queue); 60936285Sbrian 61036285Sbrian return 0; 6116059Samurai} 612