ip.c revision 51048
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 51048 1999-09-07 07:51:11Z 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 */ 12449140Sbrian 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; 17349374Sbrian#ifdef IPPROTO_OSPFIGP 17449372Sbrian case IPPROTO_OSPFIGP: 17549372Sbrian cproto = P_OSPF; 17649372Sbrian if (datalen < 8) /* IGMP uses 8-octet messages */ 17749372Sbrian return (1); 17849372Sbrian estab = syn = finrst = -1; 17949372Sbrian sport = ntohs(0); 18049372Sbrian break; 18149374Sbrian#endif 18249140Sbrian case IPPROTO_UDP: 18349140Sbrian case IPPROTO_IPIP: 18449140Sbrian cproto = P_UDP; 18549140Sbrian if (datalen < 8) /* UDP header is 8 octets */ 18649140Sbrian return (1); 18749140Sbrian uh = (const struct udphdr *) ptop; 18849140Sbrian sport = ntohs(uh->uh_sport); 18949140Sbrian dport = ntohs(uh->uh_dport); 19049140Sbrian estab = syn = finrst = -1; 19149140Sbrian if (log_IsKept(LogDEBUG)) 19249140Sbrian snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d", 19349140Sbrian sport, dport); 19449140Sbrian break; 19549140Sbrian case IPPROTO_TCP: 19649140Sbrian cproto = P_TCP; 19749140Sbrian th = (const struct tcphdr *) ptop; 19849140Sbrian /* TCP headers are variable length. The following code 19949140Sbrian * ensures that the TCP header length isn't de-referenced if 20049140Sbrian * the datagram is too short 20149140Sbrian */ 20249140Sbrian if (datalen < 20 || datalen < (th->th_off << 2)) 20349140Sbrian return (1); 20449140Sbrian sport = ntohs(th->th_sport); 20549140Sbrian dport = ntohs(th->th_dport); 20649140Sbrian estab = (th->th_flags & TH_ACK); 20749140Sbrian syn = (th->th_flags & TH_SYN); 20849140Sbrian finrst = (th->th_flags & (TH_FIN|TH_RST)); 20949140Sbrian if (log_IsKept(LogDEBUG)) { 21049140Sbrian if (!estab) 21149140Sbrian snprintf(dbuff, sizeof dbuff, 21249140Sbrian "flags = %02x, sport = %d, dport = %d", 21349140Sbrian th->th_flags, sport, dport); 21449140Sbrian else 21549140Sbrian *dbuff = '\0'; 2166059Samurai } 21749140Sbrian break; 21849140Sbrian default: 21949140Sbrian return (1); /* We'll block unknown type of packet */ 22049140Sbrian } 22126516Sbrian 22249140Sbrian if (log_IsKept(LogDEBUG)) { 22349140Sbrian if (estab != -1) { 22449140Sbrian len = strlen(dbuff); 22549140Sbrian snprintf(dbuff + len, sizeof dbuff - len, 22649140Sbrian ", estab = %d, syn = %d, finrst = %d", 22749140Sbrian estab, syn, finrst); 2286059Samurai } 22949140Sbrian log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", 23049140Sbrian filter_Proto2Nam(cproto), dbuff); 2316059Samurai } 23249140Sbrian gotinfo = 1; 23349140Sbrian } 23449140Sbrian if (log_IsKept(LogDEBUG)) { 23549140Sbrian if (fp->f_srcop != OP_NONE) { 23649140Sbrian snprintf(dbuff, sizeof dbuff, ", src %s %d", 23749140Sbrian filter_Op2Nam(fp->f_srcop), fp->f_srcport); 23849140Sbrian len = strlen(dbuff); 23949140Sbrian } else 24049140Sbrian len = 0; 24149140Sbrian if (fp->f_dstop != OP_NONE) { 24249140Sbrian snprintf(dbuff + len, sizeof dbuff - len, 24349140Sbrian ", dst %s %d", filter_Op2Nam(fp->f_dstop), 24449140Sbrian fp->f_dstport); 24549140Sbrian } else if (!len) 24649140Sbrian *dbuff = '\0'; 24749140Sbrian 24849140Sbrian log_Printf(LogDEBUG, " rule = %d: Address match, " 24949140Sbrian "check against proto %s%s, action = %s\n", 25049140Sbrian n, filter_Proto2Nam(fp->f_proto), 25149140Sbrian dbuff, filter_Action2Nam(fp->f_action)); 25249140Sbrian } 25349140Sbrian 25449140Sbrian if (cproto == fp->f_proto) { 25549140Sbrian if ((fp->f_srcop == OP_NONE || 25649140Sbrian PortMatch(fp->f_srcop, sport, fp->f_srcport)) && 25749140Sbrian (fp->f_dstop == OP_NONE || 25849140Sbrian PortMatch(fp->f_dstop, dport, fp->f_dstport)) && 25949140Sbrian (fp->f_estab == 0 || estab) && 26049140Sbrian (fp->f_syn == 0 || syn) && 26149140Sbrian (fp->f_finrst == 0 || finrst)) { 26249140Sbrian match = 1; 26349140Sbrian } 26449140Sbrian } 26549140Sbrian } else { 26649140Sbrian /* Address is matched and no protocol specified. Make a decision. */ 26749140Sbrian log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n, 26849140Sbrian filter_Action2Nam(fp->f_action)); 26949140Sbrian match = 1; 2706059Samurai } 27149140Sbrian } else 27249140Sbrian log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n); 27349140Sbrian 27449140Sbrian if (match != fp->f_invert) { 27549140Sbrian /* Take specified action */ 27649140Sbrian if (fp->f_action < A_NONE) 27749140Sbrian fp = &filter->rule[n = fp->f_action]; 27849140Sbrian else 27949140Sbrian return (fp->f_action != A_PERMIT); 28049140Sbrian } else { 28149140Sbrian n++; 2826059Samurai fp++; 2836059Samurai } 2846059Samurai } 28549140Sbrian return (1); /* No rule is mached. Deny this packet */ 2866059Samurai} 2876059Samurai 28836285Sbrian#ifdef notdef 2896059Samuraistatic void 29046828SbrianIcmpError(struct ip *pip, int code) 2916059Samurai{ 2926059Samurai struct mbuf *bp; 2936059Samurai 2946059Samurai if (pip->ip_p != IPPROTO_ICMP) { 29536285Sbrian bp = mbuf_Alloc(cnt, MB_IPIN); 29630715Sbrian memcpy(MBUF_CTOP(bp), ptr, cnt); 29736285Sbrian vj_SendFrame(bp); 29836285Sbrian ipcp_AddOutOctets(cnt); 2996059Samurai } 30036285Sbrian} 3016059Samurai#endif 3026059Samurai 3036059Samurai/* 3046059Samurai * For debugging aid. 3056059Samurai */ 3066059Samuraiint 30736285SbrianPacketCheck(struct bundle *bundle, char *cp, int nb, struct filter *filter) 3086059Samurai{ 3096059Samurai struct ip *pip; 3106059Samurai struct tcphdr *th; 3116059Samurai struct udphdr *uh; 3126059Samurai struct icmp *icmph; 3136059Samurai char *ptop; 3146059Samurai int mask, len, n; 31550867Sbrian int pri = 0; 31626692Sbrian int logit, loglen; 31737010Sbrian char logbuf[200]; 3186059Samurai 31936285Sbrian logit = log_IsKept(LogTCPIP) && filter->logok; 32026692Sbrian loglen = 0; 3216059Samurai 32228679Sbrian pip = (struct ip *) cp; 3238857Srgrimes 32426692Sbrian if (logit && loglen < sizeof logbuf) { 32536285Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name); 32628679Sbrian loglen += strlen(logbuf + loglen); 32726692Sbrian } 3286059Samurai ptop = (cp + (pip->ip_hl << 2)); 3296059Samurai 3306059Samurai switch (pip->ip_p) { 3316059Samurai case IPPROTO_ICMP: 33226692Sbrian if (logit && loglen < sizeof logbuf) { 33328679Sbrian icmph = (struct icmp *) ptop; 33428679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 33528679Sbrian "ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type); 33628679Sbrian loglen += strlen(logbuf + loglen); 33728679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 33828679Sbrian "%s:%d", inet_ntoa(pip->ip_dst), icmph->icmp_type); 33928679Sbrian loglen += strlen(logbuf + loglen); 3406059Samurai } 3416059Samurai break; 34251048Sbrian 3436059Samurai case IPPROTO_UDP: 34451048Sbrian uh = (struct udphdr *) ptop; 34551048Sbrian if (pip->ip_tos == IPTOS_LOWDELAY) 34651048Sbrian pri++; 34751048Sbrian 34851048Sbrian if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 && 34951048Sbrian ipcp_IsUrgentUdpPort(&bundle->ncp.ipcp, ntohs(uh->uh_sport), 35051048Sbrian ntohs(uh->uh_dport))) 35151048Sbrian pri++; 35251048Sbrian 35326692Sbrian if (logit && loglen < sizeof logbuf) { 35428679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 35528679Sbrian "UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 35628679Sbrian loglen += strlen(logbuf + loglen); 35728679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 35828679Sbrian "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 35928679Sbrian loglen += strlen(logbuf + loglen); 3606059Samurai } 3616059Samurai break; 36251048Sbrian 36349374Sbrian#ifdef IPPROTO_OSPFIGP 36449372Sbrian case IPPROTO_OSPFIGP: 36549372Sbrian if (logit && loglen < sizeof logbuf) { 36649372Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 36749372Sbrian "OSPF: %s ---> ", inet_ntoa(pip->ip_src)); 36849372Sbrian loglen += strlen(logbuf + loglen); 36949372Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 37049372Sbrian "%s", inet_ntoa(pip->ip_dst)); 37149372Sbrian loglen += strlen(logbuf + loglen); 37249372Sbrian } 37349372Sbrian break; 37449374Sbrian#endif 37551048Sbrian 37636961Sbrian case IPPROTO_IPIP: 37736961Sbrian if (logit && loglen < sizeof logbuf) { 37836961Sbrian uh = (struct udphdr *) ptop; 37936961Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 38036961Sbrian "IPIP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 38136961Sbrian loglen += strlen(logbuf + loglen); 38236961Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 38336961Sbrian "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 38436961Sbrian loglen += strlen(logbuf + loglen); 38536961Sbrian } 38636961Sbrian break; 38751048Sbrian 38836961Sbrian case IPPROTO_IGMP: 38936961Sbrian if (logit && loglen < sizeof logbuf) { 39036961Sbrian uh = (struct udphdr *) ptop; 39136961Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 39236961Sbrian "IGMP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 39336961Sbrian loglen += strlen(logbuf + loglen); 39436961Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 39536961Sbrian "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 39636961Sbrian loglen += strlen(logbuf + loglen); 39736961Sbrian } 39836961Sbrian break; 39951048Sbrian 4006059Samurai case IPPROTO_TCP: 40128679Sbrian th = (struct tcphdr *) ptop; 4026059Samurai if (pip->ip_tos == IPTOS_LOWDELAY) 40350867Sbrian pri++; 40451048Sbrian 40551048Sbrian if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 && 40651048Sbrian ipcp_IsUrgentTcpPort(&bundle->ncp.ipcp, ntohs(th->th_sport), 40751048Sbrian ntohs(th->th_dport))) 40850867Sbrian pri++; 40950867Sbrian 41026692Sbrian if (logit && loglen < sizeof logbuf) { 4116059Samurai len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2); 41228679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 41328679Sbrian "TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport)); 41428679Sbrian loglen += strlen(logbuf + loglen); 41528679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 41628679Sbrian "%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport)); 41728679Sbrian loglen += strlen(logbuf + loglen); 4186059Samurai n = 0; 4196059Samurai for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 42026692Sbrian if (th->th_flags & mask) { 42128679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 42228679Sbrian loglen += strlen(logbuf + loglen); 42328679Sbrian } 4246059Samurai n++; 4256059Samurai } 42628679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 42737210Sbrian " seq:%lx ack:%lx (%d/%d)", 42837210Sbrian (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb); 42928679Sbrian loglen += strlen(logbuf + loglen); 4306059Samurai if ((th->th_flags & TH_SYN) && nb > 40) { 43128679Sbrian u_short *sp; 4326059Samurai 4336059Samurai ptop += 20; 43428679Sbrian sp = (u_short *) ptop; 43526692Sbrian if (ntohs(sp[0]) == 0x0204) { 43628679Sbrian snprintf(logbuf + loglen, sizeof logbuf - loglen, 43728679Sbrian " MSS = %d", ntohs(sp[1])); 43828679Sbrian loglen += strlen(logbuf + loglen); 43928679Sbrian } 4406059Samurai } 4416059Samurai } 4426059Samurai break; 4436059Samurai } 44426692Sbrian 44549140Sbrian if (FilterCheck(pip, filter)) { 44631142Sbrian if (logit) 44736285Sbrian log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 44836285Sbrian#ifdef notdef 44928679Sbrian if (direction == 0) 45028679Sbrian IcmpError(pip, pri); 45136285Sbrian#endif 45228679Sbrian return (-1); 4536059Samurai } else { 45436285Sbrian /* Check Keep Alive filter */ 45536285Sbrian if (logit) { 45649140Sbrian if (FilterCheck(pip, &bundle->filter.alive)) 45736285Sbrian log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 45836285Sbrian else 45936285Sbrian log_Printf(LogTCPIP, "%s\n", logbuf); 4606735Samurai } 46128679Sbrian return (pri); 4626059Samurai } 4636059Samurai} 4646059Samurai 46546686Sbrianstruct mbuf * 46646686Sbrianip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 46736285Sbrian{ 4686059Samurai int nb, nw; 46931343Sbrian struct tun_data tun; 47046686Sbrian struct ip *pip; 4716059Samurai 47246686Sbrian if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 47346686Sbrian log_Printf(LogWARN, "ip_Input: IPCP not open - packet dropped\n"); 47446686Sbrian mbuf_Free(bp); 47546686Sbrian return NULL; 4766059Samurai } 4776059Samurai 47847695Sbrian mbuf_SetType(bp, MB_IPIN); 47946686Sbrian tun_fill_header(tun, AF_INET); 48046686Sbrian nb = mbuf_Length(bp); 48147168Sbrian if (nb > sizeof tun.data) { 48247168Sbrian log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n", 48347168Sbrian l->name, nb, (int)(sizeof tun.data)); 48447168Sbrian mbuf_Free(bp); 48547168Sbrian return NULL; 48647168Sbrian } 48746686Sbrian mbuf_Read(bp, tun.data, nb); 48826031Sbrian 48946686Sbrian if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0) 49046686Sbrian return NULL; 49126031Sbrian 49246686Sbrian pip = (struct ip *)tun.data; 49349140Sbrian if (!FilterCheck(pip, &bundle->filter.alive)) 49446686Sbrian bundle_StartIdleTimer(bundle); 49526031Sbrian 49646686Sbrian ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 49736285Sbrian 49846686Sbrian nb += sizeof tun - sizeof tun.data; 49946686Sbrian nw = write(bundle->dev.fd, &tun, nb); 50046686Sbrian if (nw != nb) { 50146686Sbrian if (nw == -1) 50247168Sbrian log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n", 50347168Sbrian l->name, nb, strerror(errno)); 50446686Sbrian else 50547168Sbrian log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw); 50646686Sbrian } 50736285Sbrian 50846686Sbrian return NULL; 5096059Samurai} 5106059Samurai 5116059Samuraivoid 51238557Sbrianip_Enqueue(struct ipcp *ipcp, int pri, char *ptr, int count) 5136059Samurai{ 5146059Samurai struct mbuf *bp; 5156059Samurai 51650867Sbrian if (pri < 0 || pri >= IPCP_QUEUES(ipcp)) 51738557Sbrian log_Printf(LogERROR, "Can't store in ip queue %d\n", pri); 51838557Sbrian else { 51946686Sbrian /* 52046686Sbrian * We allocate an extra 6 bytes, four at the front and two at the end. 52146686Sbrian * This is an optimisation so that we need to do less work in 52246686Sbrian * mbuf_Prepend() in acf_LayerPush() and proto_LayerPush() and 52346686Sbrian * appending in hdlc_LayerPush(). 52446686Sbrian */ 52547695Sbrian bp = mbuf_Alloc(count + 6, MB_IPOUT); 52646686Sbrian bp->offset += 4; 52746686Sbrian bp->cnt -= 6; 52838557Sbrian memcpy(MBUF_CTOP(bp), ptr, count); 52950867Sbrian mbuf_Enqueue(ipcp->Queue + pri, bp); 53038557Sbrian } 5316059Samurai} 5326059Samurai 53338544Sbrianvoid 53438557Sbrianip_DeleteQueue(struct ipcp *ipcp) 53538544Sbrian{ 53638544Sbrian struct mqueue *queue; 53738544Sbrian 53850867Sbrian for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++) 53938544Sbrian while (queue->top) 54038544Sbrian mbuf_Free(mbuf_Dequeue(queue)); 54138544Sbrian} 54238544Sbrian 5438857Srgrimesint 54438557Sbrianip_QueueLen(struct ipcp *ipcp) 5457001Samurai{ 5467001Samurai struct mqueue *queue; 54736285Sbrian int result = 0; 54828679Sbrian 54950867Sbrian for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++) 55036285Sbrian result += queue->qlen; 55136285Sbrian 55236285Sbrian return result; 5537001Samurai} 5547001Samurai 55536285Sbrianint 55646686Sbrianip_PushPacket(struct link *l, struct bundle *bundle) 5576059Samurai{ 55838557Sbrian struct ipcp *ipcp = &bundle->ncp.ipcp; 5596059Samurai struct mqueue *queue; 5606059Samurai struct mbuf *bp; 56146686Sbrian struct ip *pip; 56225630Sbrian int cnt; 5636059Samurai 56438557Sbrian if (ipcp->fsm.state != ST_OPENED) 56536285Sbrian return 0; 56636285Sbrian 56750867Sbrian queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1; 56850867Sbrian do { 5696059Samurai if (queue->top) { 57045103Sbrian bp = mbuf_Contiguous(mbuf_Dequeue(queue)); 57146686Sbrian cnt = mbuf_Length(bp); 57246686Sbrian pip = (struct ip *)MBUF_CTOP(bp); 57349140Sbrian if (!FilterCheck(pip, &bundle->filter.alive)) 57446686Sbrian bundle_StartIdleTimer(bundle); 57550867Sbrian link_PushPacket(l, bp, bundle, 0, PROTO_IP); 57646686Sbrian ipcp_AddOutOctets(ipcp, cnt); 57746686Sbrian return 1; 5786059Samurai } 57950867Sbrian } while (queue-- != ipcp->Queue); 58036285Sbrian 58136285Sbrian return 0; 5826059Samurai} 583