ip.c revision 37010
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.42 1998/06/14 01:21:24 brian Exp $ 21 * 22 * TODO: 23 * o Return ICMP message for filterd packet 24 * and optionaly record it into log. 25 */ 26#include <sys/types.h> 27#include <sys/socket.h> 28#include <netinet/in.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 <arpa/inet.h> 35#include <net/if_tun.h> 36#include <sys/un.h> 37 38#ifndef NOALIAS 39#include <alias.h> 40#endif 41#include <errno.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46 47#include "mbuf.h" 48#include "log.h" 49#include "defs.h" 50#include "timer.h" 51#include "fsm.h" 52#include "lqr.h" 53#include "hdlc.h" 54#include "loadalias.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#include "bundle.h" 66#include "vjcomp.h" 67#include "tun.h" 68#include "ip.h" 69 70static const u_short interactive_ports[32] = { 71 544, 513, 514, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72 0, 0, 0, 0, 0, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0, 543, 73}; 74 75#define INTERACTIVE(p) (interactive_ports[(p) & 0x1F] == (p)) 76 77static const char *TcpFlags[] = { "FIN", "SYN", "RST", "PSH", "ACK", "URG" }; 78 79static int 80PortMatch(int op, u_short pport, u_short rport) 81{ 82 switch (op) { 83 case OP_EQ: 84 return (pport == rport); 85 case OP_GT: 86 return (pport > rport); 87 case OP_LT: 88 return (pport < rport); 89 default: 90 return (0); 91 } 92} 93 94/* 95 * Check a packet against with defined filters 96 */ 97static int 98FilterCheck(struct ip *pip, struct filter *filter) 99{ 100 int gotinfo, cproto, estab, syn, finrst, n, len, didname; 101 struct tcphdr *th; 102 struct udphdr *uh; 103 struct icmp *ih; 104 char *ptop; 105 u_short sport, dport; 106 struct filterent *fp = filter->rule; 107 char dbuff[100]; 108 109 if (fp->action) { 110 cproto = gotinfo = estab = syn = finrst = didname = 0; 111 sport = dport = 0; 112 for (n = 0; n < MAXFILTERS; n++) { 113 if (fp->action) { 114 /* permit fragments on in and out filter */ 115 if (filter->fragok && (ntohs(pip->ip_off) & IP_OFFMASK) != 0) 116 return (A_PERMIT); 117 118 if (!didname) 119 log_Printf(LogDEBUG, "%s filter:\n", filter->name); 120 didname = 1; 121 122 if ((pip->ip_src.s_addr & fp->smask.s_addr) == 123 (fp->saddr.s_addr & fp->smask.s_addr) && 124 (pip->ip_dst.s_addr & fp->dmask.s_addr) == 125 (fp->daddr.s_addr & fp->dmask.s_addr)) { 126 if (fp->proto) { 127 if (!gotinfo) { 128 ptop = (char *) pip + (pip->ip_hl << 2); 129 130 switch (pip->ip_p) { 131 case IPPROTO_ICMP: 132 cproto = P_ICMP; 133 ih = (struct icmp *) ptop; 134 sport = ih->icmp_type; 135 estab = syn = finrst = -1; 136 if (log_IsKept(LogDEBUG)) 137 snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 138 break; 139 case IPPROTO_UDP: 140 case IPPROTO_IGMP: 141 case IPPROTO_IPIP: 142 cproto = P_UDP; 143 uh = (struct udphdr *) ptop; 144 sport = ntohs(uh->uh_sport); 145 dport = ntohs(uh->uh_dport); 146 estab = syn = finrst = -1; 147 if (log_IsKept(LogDEBUG)) 148 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d", 149 sport, dport); 150 break; 151 case IPPROTO_TCP: 152 cproto = P_TCP; 153 th = (struct tcphdr *) ptop; 154 sport = ntohs(th->th_sport); 155 dport = ntohs(th->th_dport); 156 estab = (th->th_flags & TH_ACK); 157 syn = (th->th_flags & TH_SYN); 158 finrst = (th->th_flags & (TH_FIN|TH_RST)); 159 if (log_IsKept(LogDEBUG) && !estab) 160 snprintf(dbuff, sizeof dbuff, 161 "flags = %02x, sport = %d, dport = %d", 162 th->th_flags, sport, dport); 163 break; 164 default: 165 return (A_DENY); /* We'll block unknown type of packet */ 166 } 167 if (log_IsKept(LogDEBUG)) { 168 if (estab != -1) { 169 len = strlen(dbuff); 170 snprintf(dbuff + len, sizeof dbuff - len, 171 ", estab = %d, syn = %d, finrst = %d", 172 estab, syn, finrst); 173 } 174 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", 175 filter_Proto2Nam(cproto), dbuff); 176 } 177 gotinfo = 1; 178 } 179 if (log_IsKept(LogDEBUG)) { 180 if (fp->opt.srcop != OP_NONE) { 181 snprintf(dbuff, sizeof dbuff, ", src %s %d", 182 filter_Op2Nam(fp->opt.srcop), fp->opt.srcport); 183 len = strlen(dbuff); 184 } else 185 len = 0; 186 if (fp->opt.dstop != OP_NONE) { 187 snprintf(dbuff + len, sizeof dbuff - len, 188 ", dst %s %d", filter_Op2Nam(fp->opt.dstop), 189 fp->opt.dstport); 190 } else if (!len) 191 *dbuff = '\0'; 192 193 log_Printf(LogDEBUG, " rule = %d: Address match, " 194 "check against proto %s%s, action = %s\n", 195 n, filter_Proto2Nam(fp->proto), 196 dbuff, filter_Action2Nam(fp->action)); 197 } 198 199 if (cproto == fp->proto) { 200 if ((fp->opt.srcop == OP_NONE || 201 PortMatch(fp->opt.srcop, sport, fp->opt.srcport)) && 202 (fp->opt.dstop == OP_NONE || 203 PortMatch(fp->opt.dstop, dport, fp->opt.dstport)) && 204 (fp->opt.estab == 0 || estab) && 205 (fp->opt.syn == 0 || syn) && 206 (fp->opt.finrst == 0 || finrst)) { 207 return (fp->action); 208 } 209 } 210 } else { 211 /* Address is mached. Make a decision. */ 212 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n, 213 filter_Action2Nam(fp->action)); 214 return (fp->action); 215 } 216 } else 217 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n); 218 } 219 fp++; 220 } 221 return (A_DENY); /* No rule is mached. Deny this packet */ 222 } 223 return (A_PERMIT); /* No rule is given. Permit this packet */ 224} 225 226#ifdef notdef 227static void 228IcmpError(struct ip * pip, int code) 229{ 230 struct mbuf *bp; 231 232 if (pip->ip_p != IPPROTO_ICMP) { 233 bp = mbuf_Alloc(cnt, MB_IPIN); 234 memcpy(MBUF_CTOP(bp), ptr, cnt); 235 vj_SendFrame(bp); 236 ipcp_AddOutOctets(cnt); 237 } 238} 239#endif 240 241/* 242 * For debugging aid. 243 */ 244int 245PacketCheck(struct bundle *bundle, char *cp, int nb, struct filter *filter) 246{ 247 struct ip *pip; 248 struct tcphdr *th; 249 struct udphdr *uh; 250 struct icmp *icmph; 251 char *ptop; 252 int mask, len, n; 253 int pri = PRI_NORMAL; 254 int logit, loglen; 255 char logbuf[200]; 256 257 logit = log_IsKept(LogTCPIP) && filter->logok; 258 loglen = 0; 259 260 pip = (struct ip *) cp; 261 262 if (logit && loglen < sizeof logbuf) { 263 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name); 264 loglen += strlen(logbuf + loglen); 265 } 266 ptop = (cp + (pip->ip_hl << 2)); 267 268 switch (pip->ip_p) { 269 case IPPROTO_ICMP: 270 if (logit && loglen < sizeof logbuf) { 271 icmph = (struct icmp *) ptop; 272 snprintf(logbuf + loglen, sizeof logbuf - loglen, 273 "ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type); 274 loglen += strlen(logbuf + loglen); 275 snprintf(logbuf + loglen, sizeof logbuf - loglen, 276 "%s:%d", inet_ntoa(pip->ip_dst), icmph->icmp_type); 277 loglen += strlen(logbuf + loglen); 278 } 279 break; 280 case IPPROTO_UDP: 281 if (logit && loglen < sizeof logbuf) { 282 uh = (struct udphdr *) ptop; 283 snprintf(logbuf + loglen, sizeof logbuf - loglen, 284 "UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 285 loglen += strlen(logbuf + loglen); 286 snprintf(logbuf + loglen, sizeof logbuf - loglen, 287 "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 288 loglen += strlen(logbuf + loglen); 289 } 290 break; 291 case IPPROTO_IPIP: 292 if (logit && loglen < sizeof logbuf) { 293 uh = (struct udphdr *) ptop; 294 snprintf(logbuf + loglen, sizeof logbuf - loglen, 295 "IPIP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 296 loglen += strlen(logbuf + loglen); 297 snprintf(logbuf + loglen, sizeof logbuf - loglen, 298 "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 299 loglen += strlen(logbuf + loglen); 300 } 301 break; 302 case IPPROTO_IGMP: 303 if (logit && loglen < sizeof logbuf) { 304 uh = (struct udphdr *) ptop; 305 snprintf(logbuf + loglen, sizeof logbuf - loglen, 306 "IGMP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport)); 307 loglen += strlen(logbuf + loglen); 308 snprintf(logbuf + loglen, sizeof logbuf - loglen, 309 "%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport)); 310 loglen += strlen(logbuf + loglen); 311 } 312 break; 313 case IPPROTO_TCP: 314 th = (struct tcphdr *) ptop; 315 if (pip->ip_tos == IPTOS_LOWDELAY) 316 pri = PRI_FAST; 317 else if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0) { 318 if (INTERACTIVE(ntohs(th->th_sport)) || INTERACTIVE(ntohs(th->th_dport))) 319 pri = PRI_FAST; 320 } 321 if (logit && loglen < sizeof logbuf) { 322 len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2); 323 snprintf(logbuf + loglen, sizeof logbuf - loglen, 324 "TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport)); 325 loglen += strlen(logbuf + loglen); 326 snprintf(logbuf + loglen, sizeof logbuf - loglen, 327 "%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport)); 328 loglen += strlen(logbuf + loglen); 329 n = 0; 330 for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 331 if (th->th_flags & mask) { 332 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 333 loglen += strlen(logbuf + loglen); 334 } 335 n++; 336 } 337 snprintf(logbuf + loglen, sizeof logbuf - loglen, 338 " seq:%x ack:%x (%d/%d)", 339 ntohl(th->th_seq), ntohl(th->th_ack), len, nb); 340 loglen += strlen(logbuf + loglen); 341 if ((th->th_flags & TH_SYN) && nb > 40) { 342 u_short *sp; 343 344 ptop += 20; 345 sp = (u_short *) ptop; 346 if (ntohs(sp[0]) == 0x0204) { 347 snprintf(logbuf + loglen, sizeof logbuf - loglen, 348 " MSS = %d", ntohs(sp[1])); 349 loglen += strlen(logbuf + loglen); 350 } 351 } 352 } 353 break; 354 } 355 356 if ((FilterCheck(pip, filter) & A_DENY)) { 357 if (logit) 358 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 359#ifdef notdef 360 if (direction == 0) 361 IcmpError(pip, pri); 362#endif 363 return (-1); 364 } else { 365 /* Check Keep Alive filter */ 366 if (logit) { 367 if (FilterCheck(pip, &bundle->filter.alive) & A_DENY) 368 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 369 else 370 log_Printf(LogTCPIP, "%s\n", logbuf); 371 } 372 return (pri); 373 } 374} 375 376void 377ip_Input(struct bundle *bundle, struct mbuf * bp) 378{ 379 u_char *cp; 380 struct mbuf *wp; 381 int nb, nw; 382 struct tun_data tun; 383 struct ip *pip = (struct ip *)tun.data; 384 struct ip *piip = (struct ip *)((char *)pip + (pip->ip_hl << 2)); 385 386 tun_fill_header(tun, AF_INET); 387 cp = tun.data; 388 nb = 0; 389 for (wp = bp; wp; wp = wp->next) { /* Copy to contiguous region */ 390 if (sizeof tun.data - (cp - tun.data) < wp->cnt) { 391 log_Printf(LogERROR, "ip_Input: Packet too large (%d) - dropped\n", 392 mbuf_Length(bp)); 393 mbuf_Free(bp); 394 return; 395 } 396 memcpy(cp, MBUF_CTOP(wp), wp->cnt); 397 cp += wp->cnt; 398 nb += wp->cnt; 399 } 400 401#ifndef NOALIAS 402 if (alias_IsEnabled() && pip->ip_p != IPPROTO_IGMP && 403 (pip->ip_p != IPPROTO_IPIP || !IN_CLASSD(ntohl(piip->ip_dst.s_addr)))) { 404 struct tun_data *frag; 405 int iresult; 406 char *fptr; 407 408 iresult = (*PacketAlias.In)(tun.data, sizeof tun.data); 409 nb = ntohs(((struct ip *) tun.data)->ip_len); 410 411 if (nb > MAX_MRU) { 412 log_Printf(LogERROR, "ip_Input: Problem with IP header length\n"); 413 mbuf_Free(bp); 414 return; 415 } 416 if (iresult == PKT_ALIAS_OK 417 || iresult == PKT_ALIAS_FOUND_HEADER_FRAGMENT) { 418 if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0) { 419 mbuf_Free(bp); 420 return; 421 } 422 423 if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY)) 424 bundle_StartIdleTimer(bundle); 425 426 ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 427 428 nb = ntohs(((struct ip *) tun.data)->ip_len); 429 nb += sizeof tun - sizeof tun.data; 430 nw = write(bundle->dev.fd, &tun, nb); 431 if (nw != nb) { 432 if (nw == -1) 433 log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb, 434 strerror(errno)); 435 else 436 log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw); 437 } 438 439 if (iresult == PKT_ALIAS_FOUND_HEADER_FRAGMENT) { 440 while ((fptr = (*PacketAlias.GetFragment)(tun.data)) != NULL) { 441 (*PacketAlias.FragmentIn)(tun.data, fptr); 442 nb = ntohs(((struct ip *) fptr)->ip_len); 443 frag = (struct tun_data *) 444 ((char *)fptr - sizeof tun + sizeof tun.data); 445 nb += sizeof tun - sizeof tun.data; 446 nw = write(bundle->dev.fd, frag, nb); 447 if (nw != nb) { 448 if (nw == -1) 449 log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb, 450 strerror(errno)); 451 else 452 log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw); 453 } 454 free(frag); 455 } 456 } 457 } else if (iresult == PKT_ALIAS_UNRESOLVED_FRAGMENT) { 458 nb = ntohs(((struct ip *) tun.data)->ip_len); 459 nb += sizeof tun - sizeof tun.data; 460 frag = (struct tun_data *)malloc(nb); 461 if (frag == NULL) 462 log_Printf(LogALERT, "ip_Input: Cannot allocate memory for fragment\n"); 463 else { 464 tun_fill_header(*frag, AF_INET); 465 memcpy(frag->data, tun.data, nb - sizeof tun + sizeof tun.data); 466 (*PacketAlias.SaveFragment)(frag->data); 467 } 468 } 469 } else 470#endif /* #ifndef NOALIAS */ 471 { /* no aliasing */ 472 if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0) { 473 mbuf_Free(bp); 474 return; 475 } 476 477 if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY)) 478 bundle_StartIdleTimer(bundle); 479 480 ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 481 482 nb += sizeof tun - sizeof tun.data; 483 nw = write(bundle->dev.fd, &tun, nb); 484 if (nw != nb) { 485 if (nw == -1) 486 log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb, strerror(errno)); 487 else 488 log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw); 489 } 490 } 491 mbuf_Free(bp); 492} 493 494static struct mqueue IpOutputQueues[PRI_FAST + 1]; 495 496void 497ip_Enqueue(int pri, char *ptr, int count) 498{ 499 struct mbuf *bp; 500 501 bp = mbuf_Alloc(count, MB_IPQ); 502 memcpy(MBUF_CTOP(bp), ptr, count); 503 mbuf_Enqueue(&IpOutputQueues[pri], bp); 504} 505 506int 507ip_QueueLen() 508{ 509 struct mqueue *queue; 510 int result = 0; 511 512 for (queue = &IpOutputQueues[PRI_MAX]; queue >= IpOutputQueues; queue--) 513 result += queue->qlen; 514 515 return result; 516} 517 518int 519ip_FlushPacket(struct link *l, struct bundle *bundle) 520{ 521 struct mqueue *queue; 522 struct mbuf *bp; 523 int cnt; 524 525 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) 526 return 0; 527 528 for (queue = &IpOutputQueues[PRI_FAST]; queue >= IpOutputQueues; queue--) 529 if (queue->top) { 530 bp = mbuf_Dequeue(queue); 531 if (bp) { 532 struct ip *pip = (struct ip *)MBUF_CTOP(bp); 533 534 cnt = mbuf_Length(bp); 535 vj_SendFrame(l, bp, bundle); 536 if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY)) 537 bundle_StartIdleTimer(bundle); 538 ipcp_AddOutOctets(&bundle->ncp.ipcp, cnt); 539 return 1; 540 } 541 } 542 543 return 0; 544} 545