ip.c revision 10858
1/*
2 *		PPP IP Protocol Interface
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: ip.c,v 1.4 1995/05/30 03:50:37 rgrimes Exp $
21 *
22 *	TODO:
23 *		o Return ICMP message for filterd packet
24 *		  and optionaly record it into log.
25 */
26#include "fsm.h"
27#include "lcpproto.h"
28#include "hdlc.h"
29#include <netinet/in_systm.h>
30#include <netinet/ip.h>
31#include <netinet/ip_icmp.h>
32#include <netinet/udp.h>
33#include <netinet/tcp.h>
34#include "vars.h"
35#include "filter.h"
36
37extern void SendPppFrame();
38extern int PacketCheck();
39extern void LcpClose();
40
41static struct pppTimer IdleTimer;
42
43static void IdleTimeout()
44{
45  LogPrintf(LOG_PHASE, "Idle timer expired.\n");
46  LcpClose();
47}
48
49/*
50 *  Start Idle timer. If timeout is reached, we call LcpClose() to
51 *  close LCP and link.
52 */
53void
54StartIdleTimer()
55{
56  if (!(mode & MODE_DEDICATED)) {
57    StopTimer(&IdleTimer);
58    IdleTimer.func = IdleTimeout;
59    IdleTimer.load = VarIdleTimeout * SECTICKS;
60    IdleTimer.state = TIMER_STOPPED;
61    StartTimer(&IdleTimer);
62  }
63}
64
65void
66StopIdleTimer()
67{
68  StopTimer(&IdleTimer);
69}
70
71/*
72 *  If any IP layer traffic is detected, refresh IdleTimer.
73 */
74static void
75RestartIdleTimer()
76{
77  if (!(mode & MODE_DEDICATED) && ipKeepAlive ) {
78    StartTimer(&IdleTimer);
79    ipIdleSecs = 0;
80  }
81}
82
83static u_short interactive_ports[8] = {
84  0, 513, 0, 0, 0, 21, 0, 23,
85};
86
87#define	INTERACTIVE(p)	(interactive_ports[(p) & 7] == (p))
88
89static char *TcpFlags[] = {
90  "FIN", "SYN", "RST", "PSH", "ACK", "URG",
91};
92
93static char *Direction[] = { "INP", "OUT", "OUT", "IN/OUT" };
94static struct filterent *Filters[] = { ifilters, ofilters, dfilters, afilters };
95
96static int
97PortMatch(op, pport, rport)
98int op;
99u_short pport, rport;
100{
101  switch (op) {
102  case OP_EQ:
103    return(pport == rport);
104  case OP_GT:
105    return(pport > rport);
106  case OP_LT:
107    return(pport < rport);
108  default:
109    return(0);
110  }
111}
112
113/*
114 *  Check a packet against with defined filters
115 */
116static int
117FilterCheck(pip, direction)
118struct ip *pip;
119int direction;
120{
121  struct filterent *fp = Filters[direction];
122  int gotinfo, cproto, estab, n;
123  struct tcphdr *th;
124  struct udphdr *uh;
125  struct icmp *ih;
126  char *ptop;
127  u_short sport, dport;
128
129  if (fp->action) {
130    cproto = gotinfo = estab = 0;
131    sport = dport = 0;
132    for (n = 0; n < MAXFILTERS; n++) {
133      if (fp->action) {
134         /* permit fragments on in and out filter */
135         if ((direction == FL_IN || direction == FL_OUT) &&
136             (pip->ip_off & IP_OFFMASK) != 0) {
137              return(A_PERMIT);
138         }
139#ifdef DEBUG
140logprintf("rule = %d\n", n);
141#endif
142	if ((pip->ip_src.s_addr & fp->smask.s_addr) == fp->saddr.s_addr
143	    && (pip->ip_dst.s_addr & fp->dmask.s_addr) == fp->daddr.s_addr) {
144	  if (fp->proto) {
145	    if (!gotinfo) {
146	      ptop = (char *)pip + (pip->ip_hl << 2);
147
148	      switch (pip->ip_p) {
149	      case IPPROTO_ICMP:
150		cproto = P_ICMP; ih = (struct icmp *)ptop;
151		sport = ih->icmp_type; estab = 1;
152		break;
153	      case IPPROTO_UDP:
154		cproto = P_UDP; uh = (struct udphdr *)ptop;
155		sport = ntohs(uh->uh_sport); dport = ntohs(uh->uh_dport);
156		estab = 1;
157		break;
158	      case IPPROTO_TCP:
159		cproto = P_TCP; th = (struct tcphdr *)ptop;
160		sport = ntohs(th->th_sport); dport = ntohs(th->th_dport);
161		estab = (th->th_flags & TH_ACK);
162#ifdef DEBUG
163if (estab == 0)
164logprintf("flag = %02x, sport = %d, dport = %d\n", th->th_flags, sport, dport);
165#endif
166		break;
167	      default:
168		return(A_DENY);	/* We'll block unknown type of packet */
169	      }
170	      gotinfo = 1;
171#ifdef DEBUG
172logprintf("dir = %d, proto = %d, srcop = %d, dstop = %d, estab = %d\n",
173direction, cproto, fp->opt.srcop, fp->opt.dstop, estab);
174#endif
175	    }
176#ifdef DEBUG
177	    logprintf("check0: rule = %d, proto = %d, sport = %d, dport = %d\n",
178		      n, cproto, sport, dport);
179	    logprintf("check0: action = %d\n", fp->action);
180#endif
181	    if (cproto == fp->proto) {
182	      if ((fp->opt.srcop == OP_NONE ||
183		  PortMatch(fp->opt.srcop, sport, fp->opt.srcport))
184	       &&
185		  (fp->opt.dstop == OP_NONE ||
186		  PortMatch(fp->opt.dstop, dport, fp->opt.dstport))
187	       &&
188		  (fp->opt.estab == 0 || estab)) {
189		return(fp->action);
190	      }
191	    }
192	  } else {
193	    /* Address is mached. Make a decision. */
194#ifdef DEBUG
195	    logprintf("check1: action = %d\n", fp->action);
196#endif
197	    return(fp->action);
198	  }
199	}
200      }
201      fp++;
202    }
203drop:
204    return(A_DENY);	/* No rule is mached. Deny this packet */
205  }
206  return(A_PERMIT);	/* No rule is given. Permit this packet */
207}
208
209static void
210IcmpError(pip, code)
211struct ip *pip;
212int code;
213{
214#ifdef notdef
215  struct mbuf *bp;
216
217  if (pip->ip_p != IPPROTO_ICMP) {
218    bp = mballoc(cnt, MB_IPIN);
219    bcopy(ptr, MBUF_CTOP(bp), cnt);
220    SendPppFrame(PRI_URGENT, bp);
221    RestartIdleTimer();
222    ipOutOctets += cnt;
223  }
224#endif
225}
226
227/*
228 *  For debugging aid.
229 */
230int
231PacketCheck(cp, nb, direction)
232char *cp;
233int nb;
234int direction;
235{
236  struct ip *pip;
237  struct tcphdr *th;
238  struct udphdr *uh;
239  struct icmp *icmph;
240  char *ptop;
241  int mask, len, n;
242  int logit;
243  int pri = PRI_NORMAL;
244
245  logit = (loglevel & (1 << LOG_TCPIP));
246
247  pip = (struct ip *)cp;
248
249  if (logit) logprintf("%s  ", Direction[direction]);
250
251  ptop = (cp + (pip->ip_hl << 2));
252
253  switch (pip->ip_p) {
254  case IPPROTO_ICMP:
255    if (logit) {
256      icmph = (struct icmp *)ptop;
257      logprintf("ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type);
258      logprintf("%s:%d\n", inet_ntoa(pip->ip_dst), icmph->icmp_type);
259    }
260    break;
261  case IPPROTO_UDP:
262    if (logit) {
263      uh = (struct udphdr *)ptop;
264      logprintf("UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
265      logprintf("%s:%d\n", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
266    }
267    break;
268  case IPPROTO_TCP:
269    th = (struct tcphdr *)ptop;
270    if (pip->ip_tos == IPTOS_LOWDELAY)
271      pri = PRI_FAST;
272    else if (pip->ip_off == 0) {
273      if (INTERACTIVE(ntohs(th->th_sport)) || INTERACTIVE(ntohs(th->th_dport)))
274	 pri = PRI_FAST;
275    }
276
277    if (logit) {
278      len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2);
279      logprintf("TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport));
280      logprintf("%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport));
281      n = 0;
282      for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
283	if (th->th_flags & mask)
284	  logprintf(" %s", TcpFlags[n]);
285	n++;
286      }
287      logprintf("  seq:%x  ack:%x (%d/%d)\n",
288	ntohl(th->th_seq), ntohl(th->th_ack), len, nb);
289      if ((th->th_flags & TH_SYN) && nb > 40) {
290        u_short *sp;
291
292	ptop += 20;
293	sp = (u_short *)ptop;
294	if (ntohs(sp[0]) == 0x0204)
295	  logprintf(" MSS = %d\n", ntohs(sp[1]));
296      }
297    }
298    break;
299  }
300  pri = FilterCheck(pip, direction);
301  if (pri & A_DENY) {
302#ifdef DEBUG
303    logprintf("blocked.\n");
304#endif
305    if (direction == 0) IcmpError(pip, pri);
306    return(-1);
307  } else {
308    if ( FilterCheck(pip, FL_KEEP ) & A_DENY ) {  /* Check Keep Alive filter */
309	ipKeepAlive = FALSE;
310    } else {
311	ipKeepAlive = TRUE;
312    }
313    return(pri);
314  }
315}
316
317void
318IpInput(bp)
319struct mbuf *bp;		/* IN: Pointer to IP pakcet */
320{
321  u_char *cp;
322  struct mbuf *wp;
323  int nb, nw;
324  u_char tunbuff[MAX_MRU];
325
326  cp = tunbuff;
327  nb = 0;
328  for (wp = bp; wp; wp = wp->next) {		/* Copy to continuois region */
329    bcopy(MBUF_CTOP(wp), cp, wp->cnt);
330    cp += wp->cnt;
331    nb += wp->cnt;
332  }
333
334  if ( PacketCheck(tunbuff, nb, FL_IN ) < 0) {
335    pfree(bp);
336    return;
337  }
338
339  ipInOctets += nb;
340  /*
341   *  Pass it to tunnel device
342   */
343  nw = write(tun_out, tunbuff, nb);
344  if (nw != nb)
345    fprintf(stderr, "wrote %d, got %d\r\n");
346  pfree(bp);
347
348  RestartIdleTimer();
349}
350
351void
352IpOutput(ptr, cnt)
353u_char *ptr;			/* IN: Pointer to IP packet */
354int cnt;			/* IN: Length of packet */
355{
356  struct mbuf *bp;
357  int pri;
358
359  if (IpcpFsm.state != ST_OPENED)
360    return;
361
362  pri = PacketCheck(ptr, cnt, FL_OUT);
363  if (pri >= 0) {
364    bp = mballoc(cnt, MB_IPIN);
365    bcopy(ptr, MBUF_CTOP(bp), cnt);
366    SendPppFrame(pri, bp);
367    RestartIdleTimer();
368    ipOutOctets += cnt;
369  }
370}
371
372static struct mqueue IpOutputQueues[PRI_URGENT+1];
373
374void
375IpEnqueue(pri, ptr, count)
376int pri;
377char *ptr;
378int count;
379{
380  struct mbuf *bp;
381
382  bp = mballoc(count, MB_IPQ);
383  bcopy(ptr, MBUF_CTOP(bp), count);
384  Enqueue(&IpOutputQueues[pri], bp);
385}
386
387int
388IsIpEnqueued()
389{
390  struct mqueue *queue;
391  int    exist = FALSE;
392  for (queue = &IpOutputQueues[PRI_URGENT]; queue >= IpOutputQueues; queue--) {
393     if ( queue->qlen > 0 ) {
394       exist = TRUE;
395       break;
396     }
397  }
398  return( exist );
399}
400
401void
402IpStartOutput()
403{
404  struct mqueue *queue;
405  struct mbuf *bp;
406  int pri, cnt;
407
408  if (IpcpFsm.state != ST_OPENED)
409    return;
410  pri = PRI_URGENT;
411  for (queue = &IpOutputQueues[PRI_URGENT]; queue >= IpOutputQueues; queue--) {
412    if (queue->top) {
413      bp = Dequeue(queue);
414      if (bp) {
415	cnt = plength(bp);
416	SendPppFrame(pri, bp);
417	RestartIdleTimer();
418	ipOutOctets += cnt;
419       }
420    }
421    pri--;
422  }
423}
424