ip.c revision 49372
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.65 1999/07/27 23:43:59 brian Exp $
21 *
22 *	TODO:
23 *		o Return ICMP message for filterd packet
24 *		  and optionaly record it into log.
25 */
26#include <sys/param.h>
27#if defined(__OpenBSD__) || defined(__NetBSD__)
28#include <sys/socket.h>
29#endif
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32#include <netinet/ip.h>
33#include <netinet/ip_icmp.h>
34#include <netinet/udp.h>
35#include <netinet/tcp.h>
36#include <arpa/inet.h>
37#include <sys/un.h>
38
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <termios.h>
44#include <unistd.h>
45
46#include "layer.h"
47#include "proto.h"
48#include "mbuf.h"
49#include "log.h"
50#include "defs.h"
51#include "timer.h"
52#include "fsm.h"
53#include "lqr.h"
54#include "hdlc.h"
55#include "throughput.h"
56#include "iplist.h"
57#include "slcompress.h"
58#include "ipcp.h"
59#include "filter.h"
60#include "descriptor.h"
61#include "lcp.h"
62#include "ccp.h"
63#include "link.h"
64#include "mp.h"
65#ifndef NORADIUS
66#include "radius.h"
67#endif
68#include "bundle.h"
69#include "vjcomp.h"
70#include "tun.h"
71#include "ip.h"
72
73static const u_short interactive_ports[32] = {
74  544, 513, 514, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75  80, 81, 0, 0, 0, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0, 543,
76};
77
78#define	INTERACTIVE(p)	(interactive_ports[(p) & 0x1F] == (p))
79
80static const char *TcpFlags[] = { "FIN", "SYN", "RST", "PSH", "ACK", "URG" };
81
82static __inline int
83PortMatch(int op, u_short pport, u_short rport)
84{
85  switch (op) {
86  case OP_EQ:
87    return (pport == rport);
88  case OP_GT:
89    return (pport > rport);
90  case OP_LT:
91    return (pport < rport);
92  default:
93    return (0);
94  }
95}
96
97/*
98 *  Check a packet against a defined filter
99 *  Returns 0 to accept the packet, non-zero to drop the packet
100 *
101 *  If filtering is enabled, the initial fragment of a datagram must
102 *  contain the complete protocol header, and subsequent fragments
103 *  must not attempt to over-write it.
104 */
105static int
106FilterCheck(const struct ip *pip, const struct filter *filter)
107{
108  int gotinfo;			/* true if IP payload decoded */
109  int cproto;			/* P_* protocol type if (gotinfo) */
110  int estab, syn, finrst;	/* TCP state flags if (gotinfo) */
111  u_short sport, dport;		/* src, dest port from packet if (gotinfo) */
112  int n;			/* filter rule to process */
113  int len;			/* bytes used in dbuff */
114  int didname;			/* true if filter header printed */
115  int match;			/* true if condition matched */
116  const struct filterent *fp = filter->rule;
117  char dbuff[100];
118
119  if (fp->f_action == A_NONE)
120    return (0);		/* No rule is given. Permit this packet */
121
122  /* Deny any packet fragment that tries to over-write the header.
123   * Since we no longer have the real header available, punt on the
124   * largest normal header - 20 bytes for TCP without options, rounded
125   * up to the next possible fragment boundary.  Since the smallest
126   * `legal' MTU is 576, and the smallest recommended MTU is 296, any
127   * fragmentation within this range is dubious at best */
128  len = ntohs(pip->ip_off) & IP_OFFMASK;	/* fragment offset */
129  if (len > 0) {		/* Not first fragment within datagram */
130    if (len < (24 >> 3))	/* don't allow fragment to over-write header */
131      return (1);
132    /* permit fragments on in and out filter */
133    return (filter->fragok);
134  }
135
136  cproto = gotinfo = estab = syn = finrst = didname = 0;
137  sport = dport = 0;
138  for (n = 0; n < MAXFILTERS; ) {
139    if (fp->f_action == A_NONE) {
140      n++;
141      fp++;
142      continue;
143    }
144
145    if (!didname) {
146      log_Printf(LogDEBUG, "%s filter:\n", filter->name);
147      didname = 1;
148    }
149
150    match = 0;
151    if (!((pip->ip_src.s_addr ^ fp->f_src.ipaddr.s_addr) &
152	  fp->f_src.mask.s_addr) &&
153	!((pip->ip_dst.s_addr ^ fp->f_dst.ipaddr.s_addr) &
154	  fp->f_dst.mask.s_addr)) {
155      if (fp->f_proto != P_NONE) {
156	if (!gotinfo) {
157	  const char *ptop = (const char *) pip + (pip->ip_hl << 2);
158	  const struct tcphdr *th;
159	  const struct udphdr *uh;
160	  const struct icmp *ih;
161	  int datalen;	/* IP datagram length */
162
163	  datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
164	  switch (pip->ip_p) {
165	  case IPPROTO_ICMP:
166	    cproto = P_ICMP;
167	    if (datalen < 8)	/* ICMP must be at least 8 octets */
168	      return (1);
169	    ih = (const struct icmp *) ptop;
170	    sport = ih->icmp_type;
171	    estab = syn = finrst = -1;
172	    if (log_IsKept(LogDEBUG))
173	      snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
174	    break;
175	  case IPPROTO_IGMP:
176	    cproto = P_IGMP;
177	    if (datalen < 8)	/* IGMP uses 8-octet messages */
178	      return (1);
179	    estab = syn = finrst = -1;
180	    sport = ntohs(0);
181	    break;
182	  case IPPROTO_OSPFIGP:
183	    cproto = P_OSPF;
184	    if (datalen < 8)	/* IGMP uses 8-octet messages */
185	      return (1);
186	    estab = syn = finrst = -1;
187	    sport = ntohs(0);
188	    break;
189	  case IPPROTO_UDP:
190	  case IPPROTO_IPIP:
191	    cproto = P_UDP;
192	    if (datalen < 8)	/* UDP header is 8 octets */
193	      return (1);
194	    uh = (const struct udphdr *) ptop;
195	    sport = ntohs(uh->uh_sport);
196	    dport = ntohs(uh->uh_dport);
197	    estab = syn = finrst = -1;
198	    if (log_IsKept(LogDEBUG))
199	      snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
200		       sport, dport);
201	    break;
202	  case IPPROTO_TCP:
203	    cproto = P_TCP;
204	    th = (const struct tcphdr *) ptop;
205	    /* TCP headers are variable length.  The following code
206	     * ensures that the TCP header length isn't de-referenced if
207	     * the datagram is too short
208	     */
209	    if (datalen < 20 || datalen < (th->th_off << 2))
210	      return (1);
211	    sport = ntohs(th->th_sport);
212	    dport = ntohs(th->th_dport);
213	    estab = (th->th_flags & TH_ACK);
214	    syn = (th->th_flags & TH_SYN);
215	    finrst = (th->th_flags & (TH_FIN|TH_RST));
216	    if (log_IsKept(LogDEBUG)) {
217	      if (!estab)
218		snprintf(dbuff, sizeof dbuff,
219			 "flags = %02x, sport = %d, dport = %d",
220			 th->th_flags, sport, dport);
221	      else
222		*dbuff = '\0';
223	    }
224	    break;
225	  default:
226	    return (1);	/* We'll block unknown type of packet */
227	  }
228
229	  if (log_IsKept(LogDEBUG)) {
230	    if (estab != -1) {
231	      len = strlen(dbuff);
232	      snprintf(dbuff + len, sizeof dbuff - len,
233		       ", estab = %d, syn = %d, finrst = %d",
234		       estab, syn, finrst);
235	    }
236	    log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
237		       filter_Proto2Nam(cproto), dbuff);
238	  }
239	  gotinfo = 1;
240	}
241	if (log_IsKept(LogDEBUG)) {
242	  if (fp->f_srcop != OP_NONE) {
243	    snprintf(dbuff, sizeof dbuff, ", src %s %d",
244		     filter_Op2Nam(fp->f_srcop), fp->f_srcport);
245	    len = strlen(dbuff);
246	  } else
247	    len = 0;
248	  if (fp->f_dstop != OP_NONE) {
249	    snprintf(dbuff + len, sizeof dbuff - len,
250		     ", dst %s %d", filter_Op2Nam(fp->f_dstop),
251		     fp->f_dstport);
252	  } else if (!len)
253	    *dbuff = '\0';
254
255	  log_Printf(LogDEBUG, "  rule = %d: Address match, "
256		     "check against proto %s%s, action = %s\n",
257		     n, filter_Proto2Nam(fp->f_proto),
258		     dbuff, filter_Action2Nam(fp->f_action));
259	}
260
261	if (cproto == fp->f_proto) {
262	  if ((fp->f_srcop == OP_NONE ||
263	       PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
264	      (fp->f_dstop == OP_NONE ||
265	       PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
266	      (fp->f_estab == 0 || estab) &&
267	      (fp->f_syn == 0 || syn) &&
268	      (fp->f_finrst == 0 || finrst)) {
269	    match = 1;
270	  }
271	}
272      } else {
273	/* Address is matched and no protocol specified. Make a decision. */
274	log_Printf(LogDEBUG, "  rule = %d: Address match, action = %s\n", n,
275		   filter_Action2Nam(fp->f_action));
276	match = 1;
277      }
278    } else
279      log_Printf(LogDEBUG, "  rule = %d: Address mismatch\n", n);
280
281    if (match != fp->f_invert) {
282      /* Take specified action */
283      if (fp->f_action < A_NONE)
284	fp = &filter->rule[n = fp->f_action];
285      else
286	return (fp->f_action != A_PERMIT);
287    } else {
288      n++;
289      fp++;
290    }
291  }
292  return (1);		/* No rule is mached. Deny this packet */
293}
294
295#ifdef notdef
296static void
297IcmpError(struct ip *pip, int code)
298{
299  struct mbuf *bp;
300
301  if (pip->ip_p != IPPROTO_ICMP) {
302    bp = mbuf_Alloc(cnt, MB_IPIN);
303    memcpy(MBUF_CTOP(bp), ptr, cnt);
304    vj_SendFrame(bp);
305    ipcp_AddOutOctets(cnt);
306  }
307}
308#endif
309
310/*
311 *  For debugging aid.
312 */
313int
314PacketCheck(struct bundle *bundle, char *cp, int nb, struct filter *filter)
315{
316  struct ip *pip;
317  struct tcphdr *th;
318  struct udphdr *uh;
319  struct icmp *icmph;
320  char *ptop;
321  int mask, len, n;
322  int pri = PRI_NORMAL;
323  int logit, loglen;
324  char logbuf[200];
325
326  logit = log_IsKept(LogTCPIP) && filter->logok;
327  loglen = 0;
328
329  pip = (struct ip *) cp;
330
331  if (logit && loglen < sizeof logbuf) {
332    snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
333    loglen += strlen(logbuf + loglen);
334  }
335  ptop = (cp + (pip->ip_hl << 2));
336
337  switch (pip->ip_p) {
338  case IPPROTO_ICMP:
339    if (logit && loglen < sizeof logbuf) {
340      icmph = (struct icmp *) ptop;
341      snprintf(logbuf + loglen, sizeof logbuf - loglen,
342	     "ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type);
343      loglen += strlen(logbuf + loglen);
344      snprintf(logbuf + loglen, sizeof logbuf - loglen,
345	       "%s:%d", inet_ntoa(pip->ip_dst), icmph->icmp_type);
346      loglen += strlen(logbuf + loglen);
347    }
348    break;
349  case IPPROTO_UDP:
350    if (logit && loglen < sizeof logbuf) {
351      uh = (struct udphdr *) ptop;
352      snprintf(logbuf + loglen, sizeof logbuf - loglen,
353	   "UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
354      loglen += strlen(logbuf + loglen);
355      snprintf(logbuf + loglen, sizeof logbuf - loglen,
356	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
357      loglen += strlen(logbuf + loglen);
358    }
359    break;
360  case IPPROTO_OSPFIGP:
361    if (logit && loglen < sizeof logbuf) {
362      snprintf(logbuf + loglen, sizeof logbuf - loglen,
363	   "OSPF: %s ---> ", inet_ntoa(pip->ip_src));
364      loglen += strlen(logbuf + loglen);
365      snprintf(logbuf + loglen, sizeof logbuf - loglen,
366	       "%s", inet_ntoa(pip->ip_dst));
367      loglen += strlen(logbuf + loglen);
368    }
369    break;
370  case IPPROTO_IPIP:
371    if (logit && loglen < sizeof logbuf) {
372      uh = (struct udphdr *) ptop;
373      snprintf(logbuf + loglen, sizeof logbuf - loglen,
374	   "IPIP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
375      loglen += strlen(logbuf + loglen);
376      snprintf(logbuf + loglen, sizeof logbuf - loglen,
377	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
378      loglen += strlen(logbuf + loglen);
379    }
380    break;
381  case IPPROTO_IGMP:
382    if (logit && loglen < sizeof logbuf) {
383      uh = (struct udphdr *) ptop;
384      snprintf(logbuf + loglen, sizeof logbuf - loglen,
385	   "IGMP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
386      loglen += strlen(logbuf + loglen);
387      snprintf(logbuf + loglen, sizeof logbuf - loglen,
388	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
389      loglen += strlen(logbuf + loglen);
390    }
391    break;
392  case IPPROTO_TCP:
393    th = (struct tcphdr *) ptop;
394    if (pip->ip_tos == IPTOS_LOWDELAY)
395      pri = PRI_FAST;
396    else if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0) {
397      if (INTERACTIVE(ntohs(th->th_sport)) || INTERACTIVE(ntohs(th->th_dport)))
398	pri = PRI_FAST;
399    }
400    if (logit && loglen < sizeof logbuf) {
401      len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2);
402      snprintf(logbuf + loglen, sizeof logbuf - loglen,
403	   "TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport));
404      loglen += strlen(logbuf + loglen);
405      snprintf(logbuf + loglen, sizeof logbuf - loglen,
406	       "%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport));
407      loglen += strlen(logbuf + loglen);
408      n = 0;
409      for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
410	if (th->th_flags & mask) {
411	  snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
412	  loglen += strlen(logbuf + loglen);
413	}
414	n++;
415      }
416      snprintf(logbuf + loglen, sizeof logbuf - loglen,
417	       "  seq:%lx  ack:%lx (%d/%d)",
418	       (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
419      loglen += strlen(logbuf + loglen);
420      if ((th->th_flags & TH_SYN) && nb > 40) {
421	u_short *sp;
422
423	ptop += 20;
424	sp = (u_short *) ptop;
425	if (ntohs(sp[0]) == 0x0204) {
426	  snprintf(logbuf + loglen, sizeof logbuf - loglen,
427		   " MSS = %d", ntohs(sp[1]));
428	  loglen += strlen(logbuf + loglen);
429	}
430      }
431    }
432    break;
433  }
434
435  if (FilterCheck(pip, filter)) {
436    if (logit)
437      log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
438#ifdef notdef
439    if (direction == 0)
440      IcmpError(pip, pri);
441#endif
442    return (-1);
443  } else {
444    /* Check Keep Alive filter */
445    if (logit) {
446      if (FilterCheck(pip, &bundle->filter.alive))
447        log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
448      else
449        log_Printf(LogTCPIP, "%s\n", logbuf);
450    }
451    return (pri);
452  }
453}
454
455struct mbuf *
456ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
457{
458  int nb, nw;
459  struct tun_data tun;
460  struct ip *pip;
461
462  if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
463    log_Printf(LogWARN, "ip_Input: IPCP not open - packet dropped\n");
464    mbuf_Free(bp);
465    return NULL;
466  }
467
468  mbuf_SetType(bp, MB_IPIN);
469  tun_fill_header(tun, AF_INET);
470  nb = mbuf_Length(bp);
471  if (nb > sizeof tun.data) {
472    log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
473               l->name, nb, (int)(sizeof tun.data));
474    mbuf_Free(bp);
475    return NULL;
476  }
477  mbuf_Read(bp, tun.data, nb);
478
479  if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0)
480    return NULL;
481
482  pip = (struct ip *)tun.data;
483  if (!FilterCheck(pip, &bundle->filter.alive))
484    bundle_StartIdleTimer(bundle);
485
486  ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
487
488  nb += sizeof tun - sizeof tun.data;
489  nw = write(bundle->dev.fd, &tun, nb);
490  if (nw != nb) {
491    if (nw == -1)
492      log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
493                 l->name, nb, strerror(errno));
494    else
495      log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
496  }
497
498  return NULL;
499}
500
501void
502ip_Enqueue(struct ipcp *ipcp, int pri, char *ptr, int count)
503{
504  struct mbuf *bp;
505
506  if (pri < 0 || pri > sizeof ipcp->Queue / sizeof ipcp->Queue[0])
507    log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
508  else {
509    /*
510     * We allocate an extra 6 bytes, four at the front and two at the end.
511     * This is an optimisation so that we need to do less work in
512     * mbuf_Prepend() in acf_LayerPush() and proto_LayerPush() and
513     * appending in hdlc_LayerPush().
514     */
515    bp = mbuf_Alloc(count + 6, MB_IPOUT);
516    bp->offset += 4;
517    bp->cnt -= 6;
518    memcpy(MBUF_CTOP(bp), ptr, count);
519    mbuf_Enqueue(&ipcp->Queue[pri], bp);
520  }
521}
522
523void
524ip_DeleteQueue(struct ipcp *ipcp)
525{
526  struct mqueue *queue;
527
528  for (queue = ipcp->Queue; queue < ipcp->Queue + PRI_MAX; queue++)
529    while (queue->top)
530      mbuf_Free(mbuf_Dequeue(queue));
531}
532
533int
534ip_QueueLen(struct ipcp *ipcp)
535{
536  struct mqueue *queue;
537  int result = 0;
538
539  for (queue = ipcp->Queue; queue < ipcp->Queue + PRI_MAX; queue++)
540    result += queue->qlen;
541
542  return result;
543}
544
545int
546ip_PushPacket(struct link *l, struct bundle *bundle)
547{
548  struct ipcp *ipcp = &bundle->ncp.ipcp;
549  struct mqueue *queue;
550  struct mbuf *bp;
551  struct ip *pip;
552  int cnt;
553
554  if (ipcp->fsm.state != ST_OPENED)
555    return 0;
556
557  for (queue = &ipcp->Queue[PRI_FAST]; queue >= ipcp->Queue; queue--)
558    if (queue->top) {
559      bp = mbuf_Contiguous(mbuf_Dequeue(queue));
560      cnt = mbuf_Length(bp);
561      pip = (struct ip *)MBUF_CTOP(bp);
562      if (!FilterCheck(pip, &bundle->filter.alive))
563        bundle_StartIdleTimer(bundle);
564      link_PushPacket(l, bp, bundle, PRI_NORMAL, PROTO_IP);
565      ipcp_AddOutOctets(ipcp, cnt);
566      return 1;
567    }
568
569  return 0;
570}
571