ip.c revision 112660
1/*- 2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 4 * Internet Initiative Japan, Inc (IIJ) 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: head/usr.sbin/ppp/ip.c 112660 2003-03-26 02:27:32Z brian $ 29 */ 30 31#include <sys/param.h> 32#include <sys/socket.h> 33#include <netinet/in.h> 34#include <netinet/in_systm.h> 35#include <netinet/ip.h> 36#ifndef NOINET6 37#include <netinet/icmp6.h> 38#include <netinet/ip6.h> 39#endif 40#include <netinet/ip_icmp.h> 41#include <netinet/udp.h> 42#include <netinet/tcp.h> 43#include <sys/un.h> 44 45#include <errno.h> 46#include <netdb.h> 47#include <stdio.h> 48#include <string.h> 49#include <termios.h> 50#include <unistd.h> 51 52#include "layer.h" 53#include "proto.h" 54#include "mbuf.h" 55#include "log.h" 56#include "defs.h" 57#include "timer.h" 58#include "fsm.h" 59#include "lqr.h" 60#include "hdlc.h" 61#include "throughput.h" 62#include "iplist.h" 63#include "slcompress.h" 64#include "ncpaddr.h" 65#include "ip.h" 66#include "ipcp.h" 67#include "filter.h" 68#include "descriptor.h" 69#include "lcp.h" 70#include "ccp.h" 71#include "link.h" 72#include "mp.h" 73#ifndef NORADIUS 74#include "radius.h" 75#endif 76#include "ipv6cp.h" 77#include "ncp.h" 78#include "bundle.h" 79#include "tun.h" 80 81 82#define OPCODE_QUERY 0 83#define OPCODE_IQUERY 1 84#define OPCODE_STATUS 2 85 86struct dns_header { 87 u_short id; 88 unsigned qr : 1; 89 unsigned opcode : 4; 90 unsigned aa : 1; 91 unsigned tc : 1; 92 unsigned rd : 1; 93 unsigned ra : 1; 94 unsigned z : 3; 95 unsigned rcode : 4; 96 u_short qdcount; 97 u_short ancount; 98 u_short nscount; 99 u_short arcount; 100}; 101 102static const char * 103dns_Qclass2Txt(u_short qclass) 104{ 105 static char failure[6]; 106 struct { 107 u_short id; 108 const char *txt; 109 } qtxt[] = { 110 /* rfc1035 */ 111 { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" } 112 }; 113 int f; 114 115 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++) 116 if (qtxt[f].id == qclass) 117 return qtxt[f].txt; 118 119 return HexStr(qclass, failure, sizeof failure); 120} 121 122static const char * 123dns_Qtype2Txt(u_short qtype) 124{ 125 static char failure[6]; 126 struct { 127 u_short id; 128 const char *txt; 129 } qtxt[] = { 130 /* rfc1035/rfc1700 */ 131 { 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" }, 132 { 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" }, 133 { 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" }, 134 { 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" }, 135 { 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" }, 136 { 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" }, 137 { 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" }, 138 { 254, "MAILA" }, { 255, "*" } 139 }; 140 int f; 141 142 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++) 143 if (qtxt[f].id == qtype) 144 return qtxt[f].txt; 145 146 return HexStr(qtype, failure, sizeof failure); 147} 148 149static __inline int 150PortMatch(int op, u_short pport, u_short rport) 151{ 152 switch (op) { 153 case OP_EQ: 154 return pport == rport; 155 case OP_GT: 156 return pport > rport; 157 case OP_LT: 158 return pport < rport; 159 default: 160 return 0; 161 } 162} 163 164/* 165 * Check a packet against the given filter 166 * Returns 0 to accept the packet, non-zero to drop the packet. 167 * If psecs is not NULL, populate it with the timeout associated 168 * with the filter rule matched. 169 * 170 * If filtering is enabled, the initial fragment of a datagram must 171 * contain the complete protocol header, and subsequent fragments 172 * must not attempt to over-write it. 173 * 174 * One (and only one) of pip or pip6 must be set. 175 */ 176int 177FilterCheck(const unsigned char *packet, u_int32_t family, 178 const struct filter *filter, unsigned *psecs) 179{ 180 int gotinfo; /* true if IP payload decoded */ 181 int cproto; /* IPPROTO_* protocol number if (gotinfo) */ 182 int estab, syn, finrst; /* TCP state flags if (gotinfo) */ 183 u_short sport, dport; /* src, dest port from packet if (gotinfo) */ 184 int n; /* filter rule to process */ 185 int len; /* bytes used in dbuff */ 186 int didname; /* true if filter header printed */ 187 int match; /* true if condition matched */ 188 int mindata; /* minimum data size or zero */ 189 const struct filterent *fp = filter->rule; 190 char dbuff[100], dstip[16], prototxt[16]; 191 struct protoent *pe; 192 struct ncpaddr srcaddr, dstaddr; 193 const char *payload; /* IP payload */ 194 int datalen; /* IP datagram length */ 195 196 if (fp->f_action == A_NONE) 197 return 0; /* No rule is given. Permit this packet */ 198 199#ifndef NOINET6 200 if (family == AF_INET6) { 201 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 202 203 ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 204 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 205 datalen = ntohs(pip6->ip6_plen); 206 payload = packet + sizeof *pip6; 207 cproto = pip6->ip6_nxt; 208 } else 209#endif 210 { 211 /* 212 * Deny any packet fragment that tries to over-write the header. 213 * Since we no longer have the real header available, punt on the 214 * largest normal header - 20 bytes for TCP without options, rounded 215 * up to the next possible fragment boundary. Since the smallest 216 * `legal' MTU is 576, and the smallest recommended MTU is 296, any 217 * fragmentation within this range is dubious at best 218 */ 219 const struct ip *pip = (const struct ip *)packet; 220 221 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */ 222 if (len > 0) { /* Not first fragment within datagram */ 223 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */ 224 log_Printf(LogFILTER, " error: illegal header\n"); 225 return 1; 226 } 227 /* permit fragments on in and out filter */ 228 if (!filter->fragok) { 229 log_Printf(LogFILTER, " error: illegal fragmentation\n"); 230 return 1; 231 } else 232 return 0; 233 } 234 235 ncpaddr_setip4(&srcaddr, pip->ip_src); 236 ncpaddr_setip4(&dstaddr, pip->ip_dst); 237 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 238 payload = packet + (pip->ip_hl << 2); 239 cproto = pip->ip_p; 240 } 241 242 if ((pe = getprotobynumber(cproto)) == NULL) 243 snprintf(prototxt, sizeof prototxt, "%d", cproto); 244 else 245 snprintf(prototxt, sizeof prototxt, "%s", pe->p_name); 246 gotinfo = estab = syn = finrst = didname = 0; 247 sport = dport = 0; 248 249 for (n = 0; n < MAXFILTERS; ) { 250 if (fp->f_action == A_NONE) { 251 n++; 252 fp++; 253 continue; 254 } 255 256 if (!didname) { 257 log_Printf(LogDEBUG, "%s filter:\n", filter->name); 258 didname = 1; 259 } 260 261 match = 0; 262 263 if ((ncprange_family(&fp->f_src) == AF_UNSPEC || 264 ncprange_contains(&fp->f_src, &srcaddr)) && 265 (ncprange_family(&fp->f_dst) == AF_UNSPEC || 266 ncprange_contains(&fp->f_dst, &dstaddr))) { 267 if (fp->f_proto != 0) { 268 if (!gotinfo) { 269 const struct tcphdr *th; 270 const struct udphdr *uh; 271 const struct icmp *ih; 272#ifndef NOINET6 273 const struct icmp6_hdr *ih6; 274#endif 275 mindata = 0; 276 sport = dport = 0; 277 estab = syn = finrst = -1; 278 279 switch (cproto) { 280 case IPPROTO_ICMP: 281 mindata = 8; /* ICMP must be at least 8 octets */ 282 ih = (const struct icmp *)payload; 283 sport = ih->icmp_type; 284 if (log_IsKept(LogDEBUG)) 285 snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 286 break; 287 288#ifndef NOINET6 289 case IPPROTO_ICMPV6: 290 mindata = 8; /* ICMP must be at least 8 octets */ 291 ih6 = (const struct icmp6_hdr *)payload; 292 sport = ih6->icmp6_type; 293 if (log_IsKept(LogDEBUG)) 294 snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 295 break; 296#endif 297 298 case IPPROTO_IGMP: 299 mindata = 8; /* IGMP uses 8-octet messages */ 300 break; 301 302#ifdef IPPROTO_GRE 303 case IPPROTO_GRE: 304 mindata = 2; /* GRE uses 2-octet+ messages */ 305 break; 306#endif 307#ifdef IPPROTO_OSPFIGP 308 case IPPROTO_OSPFIGP: 309 mindata = 8; /* IGMP uses 8-octet messages */ 310 break; 311#endif 312#ifndef NOINET6 313 case IPPROTO_IPV6: 314 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */ 315 break; 316#endif 317 318 case IPPROTO_UDP: 319 mindata = 8; /* UDP header is 8 octets */ 320 uh = (const struct udphdr *)payload; 321 sport = ntohs(uh->uh_sport); 322 dport = ntohs(uh->uh_dport); 323 if (log_IsKept(LogDEBUG)) 324 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d", 325 sport, dport); 326 break; 327 328 case IPPROTO_TCP: 329 th = (const struct tcphdr *)payload; 330 /* 331 * TCP headers are variable length. The following code 332 * ensures that the TCP header length isn't de-referenced if 333 * the datagram is too short 334 */ 335 if (datalen < 20 || datalen < (th->th_off << 2)) { 336 log_Printf(LogFILTER, " error: TCP header incorrect\n"); 337 return 1; 338 } 339 sport = ntohs(th->th_sport); 340 dport = ntohs(th->th_dport); 341 estab = (th->th_flags & TH_ACK); 342 syn = (th->th_flags & TH_SYN); 343 finrst = (th->th_flags & (TH_FIN|TH_RST)); 344 if (log_IsKept(LogDEBUG)) { 345 if (!estab) 346 snprintf(dbuff, sizeof dbuff, 347 "flags = %02x, sport = %d, dport = %d", 348 th->th_flags, sport, dport); 349 else 350 *dbuff = '\0'; 351 } 352 break; 353 default: 354 break; 355 } 356 357 if (datalen < mindata) { 358 log_Printf(LogFILTER, " error: proto %s must be at least" 359 " %d octets\n", prototxt, mindata); 360 return 1; 361 } 362 363 if (log_IsKept(LogDEBUG)) { 364 if (estab != -1) { 365 len = strlen(dbuff); 366 snprintf(dbuff + len, sizeof dbuff - len, 367 ", estab = %d, syn = %d, finrst = %d", 368 estab, syn, finrst); 369 } 370 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff); 371 } 372 gotinfo = 1; 373 } 374 375 if (log_IsKept(LogDEBUG)) { 376 if (fp->f_srcop != OP_NONE) { 377 snprintf(dbuff, sizeof dbuff, ", src %s %d", 378 filter_Op2Nam(fp->f_srcop), fp->f_srcport); 379 len = strlen(dbuff); 380 } else 381 len = 0; 382 if (fp->f_dstop != OP_NONE) { 383 snprintf(dbuff + len, sizeof dbuff - len, 384 ", dst %s %d", filter_Op2Nam(fp->f_dstop), 385 fp->f_dstport); 386 } else if (!len) 387 *dbuff = '\0'; 388 389 log_Printf(LogDEBUG, " rule = %d: Address match, " 390 "check against proto %d%s, action = %s\n", 391 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action)); 392 } 393 394 if (cproto == fp->f_proto) { 395 if ((fp->f_srcop == OP_NONE || 396 PortMatch(fp->f_srcop, sport, fp->f_srcport)) && 397 (fp->f_dstop == OP_NONE || 398 PortMatch(fp->f_dstop, dport, fp->f_dstport)) && 399 (fp->f_estab == 0 || estab) && 400 (fp->f_syn == 0 || syn) && 401 (fp->f_finrst == 0 || finrst)) { 402 match = 1; 403 } 404 } 405 } else { 406 /* Address is matched and no protocol specified. Make a decision. */ 407 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n, 408 filter_Action2Nam(fp->f_action)); 409 match = 1; 410 } 411 } else 412 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n); 413 414 if (match != fp->f_invert) { 415 /* Take specified action */ 416 if (fp->f_action < A_NONE) 417 fp = &filter->rule[n = fp->f_action]; 418 else { 419 if (fp->f_action == A_PERMIT) { 420 if (psecs != NULL) 421 *psecs = fp->timeout; 422 if (strcmp(filter->name, "DIAL") == 0) { 423 /* If dial filter then even print out accept packets */ 424 if (log_IsKept(LogFILTER)) { 425 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 426 log_Printf(LogFILTER, "%sbound rule = %d accept %s " 427 "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt, 428 ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 429 } 430 } 431 return 0; 432 } else { 433 if (log_IsKept(LogFILTER)) { 434 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 435 log_Printf(LogFILTER, 436 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n", 437 filter->name, n, prototxt, 438 ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 439 } 440 return 1; 441 } /* Explict match. Deny this packet */ 442 } 443 } else { 444 n++; 445 fp++; 446 } 447 } 448 449 if (log_IsKept(LogFILTER)) { 450 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 451 log_Printf(LogFILTER, 452 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n", 453 filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport, 454 dstip, dport); 455 } 456 457 return 1; /* No rule matched, deny this packet */ 458} 459 460static void 461ip_LogDNS(const struct udphdr *uh, const char *direction) 462{ 463 struct dns_header header; 464 const u_short *pktptr; 465 const u_char *ptr; 466 u_short *hptr, tmp; 467 int len; 468 469 ptr = (const char *)uh + sizeof *uh; 470 len = ntohs(uh->uh_ulen) - sizeof *uh; 471 if (len < sizeof header + 5) /* rfc1024 */ 472 return; 473 474 pktptr = (const u_short *)ptr; 475 hptr = (u_short *)&header; 476 ptr += sizeof header; 477 len -= sizeof header; 478 479 while (pktptr < (const u_short *)ptr) { 480 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */ 481 pktptr++; 482 } 483 484 if (header.opcode == OPCODE_QUERY && header.qr == 0) { 485 /* rfc1035 */ 486 char namewithdot[MAXHOSTNAMELEN + 1], *n; 487 const char *qtype, *qclass; 488 const u_char *end; 489 490 n = namewithdot; 491 end = ptr + len - 4; 492 if (end - ptr >= sizeof namewithdot) 493 end = ptr + sizeof namewithdot - 1; 494 while (ptr < end) { 495 len = *ptr++; 496 if (len > end - ptr) 497 len = end - ptr; 498 if (n != namewithdot) 499 *n++ = '.'; 500 memcpy(n, ptr, len); 501 ptr += len; 502 n += len; 503 } 504 *n = '\0'; 505 506 if (log_IsKept(LogDNS)) { 507 memcpy(&tmp, end, sizeof tmp); 508 qtype = dns_Qtype2Txt(ntohs(tmp)); 509 memcpy(&tmp, end + 2, sizeof tmp); 510 qclass = dns_Qclass2Txt(ntohs(tmp)); 511 512 log_Printf(LogDNS, "%sbound query %s %s %s\n", 513 direction, qclass, qtype, namewithdot); 514 } 515 } 516} 517 518/* 519 * Check if the given packet matches the given filter. 520 * One of pip or pip6 must be set. 521 */ 522int 523PacketCheck(struct bundle *bundle, u_int32_t family, 524 const unsigned char *packet, int nb, struct filter *filter, 525 const char *prefix, unsigned *psecs) 526{ 527 static const char *const TcpFlags[] = { 528 "FIN", "SYN", "RST", "PSH", "ACK", "URG" 529 }; 530 const struct tcphdr *th; 531 const struct udphdr *uh; 532 const struct icmp *icmph; 533#ifndef NOINET6 534 const struct icmp6_hdr *icmp6h; 535#endif 536 const unsigned char *payload; 537 struct ncpaddr srcaddr, dstaddr; 538 int cproto, mask, len, n, pri, logit, loglen, result; 539 char logbuf[200]; 540 int datalen, frag; 541 u_char tos; 542 543 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) && 544 (!filter || filter->logok); 545 loglen = 0; 546 pri = 0; 547 548#ifndef NOINET6 549 if (family == AF_INET6) { 550 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 551 552 ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 553 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 554 datalen = ntohs(pip6->ip6_plen); 555 payload = packet + sizeof *pip6; 556 cproto = pip6->ip6_nxt; 557 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */ 558 frag = 0; /* XXX: ??? */ 559 } else 560#endif 561 { 562 const struct ip *pip = (const struct ip *)packet; 563 564 ncpaddr_setip4(&srcaddr, pip->ip_src); 565 ncpaddr_setip4(&dstaddr, pip->ip_dst); 566 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 567 payload = packet + (pip->ip_hl << 2); 568 cproto = pip->ip_p; 569 tos = pip->ip_tos; 570 frag = ntohs(pip->ip_off) & IP_OFFMASK; 571 } 572 573 uh = NULL; 574 575 if (logit && loglen < sizeof logbuf) { 576 if (prefix) 577 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix); 578 else if (filter) 579 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name); 580 else 581 snprintf(logbuf + loglen, sizeof logbuf - loglen, " "); 582 loglen += strlen(logbuf + loglen); 583 } 584 585 switch (cproto) { 586 case IPPROTO_ICMP: 587 if (logit && loglen < sizeof logbuf) { 588 len = datalen - sizeof *icmph; 589 icmph = (const struct icmp *)payload; 590 snprintf(logbuf + loglen, sizeof logbuf - loglen, 591 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type); 592 loglen += strlen(logbuf + loglen); 593 snprintf(logbuf + loglen, sizeof logbuf - loglen, 594 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 595 loglen += strlen(logbuf + loglen); 596 } 597 break; 598 599#ifndef NOINET6 600 case IPPROTO_ICMPV6: 601 if (logit && loglen < sizeof logbuf) { 602 len = datalen - sizeof *icmp6h; 603 icmp6h = (const struct icmp6_hdr *)payload; 604 snprintf(logbuf + loglen, sizeof logbuf - loglen, 605 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type); 606 loglen += strlen(logbuf + loglen); 607 snprintf(logbuf + loglen, sizeof logbuf - loglen, 608 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 609 loglen += strlen(logbuf + loglen); 610 } 611 break; 612#endif 613 614 case IPPROTO_UDP: 615 uh = (const struct udphdr *)payload; 616 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 617 pri++; 618 619 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport), 620 ntohs(uh->uh_dport))) 621 pri++; 622 623 if (logit && loglen < sizeof logbuf) { 624 len = datalen - sizeof *uh; 625 snprintf(logbuf + loglen, sizeof logbuf - loglen, 626 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport)); 627 loglen += strlen(logbuf + loglen); 628 snprintf(logbuf + loglen, sizeof logbuf - loglen, 629 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport), 630 len, nb); 631 loglen += strlen(logbuf + loglen); 632 } 633 634 if (Enabled(bundle, OPT_FILTERDECAP) && 635 payload[sizeof *uh] == HDLC_ADDR && 636 payload[sizeof *uh + 1] == HDLC_UI) { 637 u_short proto; 638 const char *type; 639 640 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto); 641 type = NULL; 642 643 switch (ntohs(proto)) { 644 case PROTO_IP: 645 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 646 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4, 647 nb - (payload - packet) - sizeof *uh - 4, filter, 648 logbuf, psecs); 649 if (result != -2) 650 return result; 651 type = "IP"; 652 break; 653 654 case PROTO_VJUNCOMP: type = "compressed VJ"; break; 655 case PROTO_VJCOMP: type = "uncompressed VJ"; break; 656 case PROTO_MP: type = "Multi-link"; break; 657 case PROTO_ICOMPD: type = "Individual link CCP"; break; 658 case PROTO_COMPD: type = "CCP"; break; 659 case PROTO_IPCP: type = "IPCP"; break; 660 case PROTO_LCP: type = "LCP"; break; 661 case PROTO_PAP: type = "PAP"; break; 662 case PROTO_CBCP: type = "CBCP"; break; 663 case PROTO_LQR: type = "LQR"; break; 664 case PROTO_CHAP: type = "CHAP"; break; 665 } 666 if (type) { 667 snprintf(logbuf + loglen, sizeof logbuf - loglen, 668 " - %s data", type); 669 loglen += strlen(logbuf + loglen); 670 } 671 } 672 673 break; 674 675#ifdef IPPROTO_GRE 676 case IPPROTO_GRE: 677 if (logit && loglen < sizeof logbuf) { 678 snprintf(logbuf + loglen, sizeof logbuf - loglen, 679 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr)); 680 loglen += strlen(logbuf + loglen); 681 snprintf(logbuf + loglen, sizeof logbuf - loglen, 682 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 683 loglen += strlen(logbuf + loglen); 684 } 685 break; 686#endif 687 688#ifdef IPPROTO_OSPFIGP 689 case IPPROTO_OSPFIGP: 690 if (logit && loglen < sizeof logbuf) { 691 snprintf(logbuf + loglen, sizeof logbuf - loglen, 692 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr)); 693 loglen += strlen(logbuf + loglen); 694 snprintf(logbuf + loglen, sizeof logbuf - loglen, 695 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 696 loglen += strlen(logbuf + loglen); 697 } 698 break; 699#endif 700 701#ifndef NOINET6 702 case IPPROTO_IPV6: 703 if (logit && loglen < sizeof logbuf) { 704 snprintf(logbuf + loglen, sizeof logbuf - loglen, 705 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr)); 706 loglen += strlen(logbuf + loglen); 707 snprintf(logbuf + loglen, sizeof logbuf - loglen, 708 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 709 loglen += strlen(logbuf + loglen); 710 } 711 712 if (Enabled(bundle, OPT_FILTERDECAP)) { 713 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 714 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet), 715 filter, logbuf, psecs); 716 if (result != -2) 717 return result; 718 } 719 break; 720#endif 721 722 case IPPROTO_IPIP: 723 if (logit && loglen < sizeof logbuf) { 724 snprintf(logbuf + loglen, sizeof logbuf - loglen, 725 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 726 loglen += strlen(logbuf + loglen); 727 snprintf(logbuf + loglen, sizeof logbuf - loglen, 728 "%s", ncpaddr_ntoa(&dstaddr)); 729 loglen += strlen(logbuf + loglen); 730 } 731 732 if (Enabled(bundle, OPT_FILTERDECAP) && 733 ((const struct ip *)payload)->ip_v == 4) { 734 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 735 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet), 736 filter, logbuf, psecs); 737 if (result != -2) 738 return result; 739 } 740 break; 741 742 case IPPROTO_ESP: 743 if (logit && loglen < sizeof logbuf) { 744 snprintf(logbuf + loglen, sizeof logbuf - loglen, 745 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 746 loglen += strlen(logbuf + loglen); 747 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 748 ncpaddr_ntoa(&dstaddr), payload); 749 loglen += strlen(logbuf + loglen); 750 } 751 break; 752 753 case IPPROTO_AH: 754 if (logit && loglen < sizeof logbuf) { 755 snprintf(logbuf + loglen, sizeof logbuf - loglen, 756 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr)); 757 loglen += strlen(logbuf + loglen); 758 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 759 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t)); 760 loglen += strlen(logbuf + loglen); 761 } 762 break; 763 764 case IPPROTO_IGMP: 765 if (logit && loglen < sizeof logbuf) { 766 uh = (const struct udphdr *)payload; 767 snprintf(logbuf + loglen, sizeof logbuf - loglen, 768 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), 769 ntohs(uh->uh_sport)); 770 loglen += strlen(logbuf + loglen); 771 snprintf(logbuf + loglen, sizeof logbuf - loglen, 772 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport)); 773 loglen += strlen(logbuf + loglen); 774 } 775 break; 776 777 case IPPROTO_TCP: 778 th = (const struct tcphdr *)payload; 779 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 780 pri++; 781 782 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport), 783 ntohs(th->th_dport))) 784 pri++; 785 786 if (logit && loglen < sizeof logbuf) { 787 len = datalen - (th->th_off << 2); 788 snprintf(logbuf + loglen, sizeof logbuf - loglen, 789 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport)); 790 loglen += strlen(logbuf + loglen); 791 snprintf(logbuf + loglen, sizeof logbuf - loglen, 792 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport)); 793 loglen += strlen(logbuf + loglen); 794 n = 0; 795 for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 796 if (th->th_flags & mask) { 797 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 798 loglen += strlen(logbuf + loglen); 799 } 800 n++; 801 } 802 snprintf(logbuf + loglen, sizeof logbuf - loglen, 803 " seq:%lx ack:%lx (%d/%d)", 804 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb); 805 loglen += strlen(logbuf + loglen); 806 if ((th->th_flags & TH_SYN) && nb > 40) { 807 const u_short *sp; 808 809 sp = (const u_short *)(payload + 20); 810 if (ntohs(sp[0]) == 0x0204) { 811 snprintf(logbuf + loglen, sizeof logbuf - loglen, 812 " MSS = %d", ntohs(sp[1])); 813 loglen += strlen(logbuf + loglen); 814 } 815 } 816 } 817 break; 818 819 default: 820 if (prefix) 821 return -2; 822 823 if (logit && loglen < sizeof logbuf) { 824 snprintf(logbuf + loglen, sizeof logbuf - loglen, 825 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr)); 826 loglen += strlen(logbuf + loglen); 827 snprintf(logbuf + loglen, sizeof logbuf - loglen, 828 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb); 829 loglen += strlen(logbuf + loglen); 830 } 831 break; 832 } 833 834 if (filter && FilterCheck(packet, family, filter, psecs)) { 835 if (logit) 836 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 837 result = -1; 838 } else { 839 /* Check Keep Alive filter */ 840 if (logit && log_IsKept(LogTCPIP)) { 841 unsigned alivesecs; 842 843 alivesecs = 0; 844 if (filter && 845 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs)) 846 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 847 else if (psecs != NULL) { 848 if(*psecs == 0) 849 *psecs = alivesecs; 850 if (*psecs) { 851 if (*psecs != alivesecs) 852 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n", 853 logbuf, *psecs, alivesecs); 854 else 855 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs); 856 } else 857 log_Printf(LogTCPIP, "%s\n", logbuf); 858 } 859 } 860 result = pri; 861 } 862 863 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS)) 864 ip_LogDNS(uh, filter->name); 865 866 return result; 867} 868 869static int 870ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af) 871{ 872 int nb, nw; 873 struct tun_data tun; 874 char *data; 875 unsigned secs, alivesecs; 876 877 nb = m_length(bp); 878 if (nb > sizeof tun.data) { 879 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n", 880 l->name, nb, (int)(sizeof tun.data)); 881 m_freem(bp); 882 return 0; 883 } 884 mbuf_Read(bp, tun.data, nb); 885 886 secs = 0; 887 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in, 888 NULL, &secs) < 0) 889 return 0; 890 891 alivesecs = 0; 892 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) { 893 if (secs == 0) 894 secs = alivesecs; 895 bundle_StartIdleTimer(bundle, secs); 896 } 897 898 if (bundle->dev.header) { 899 tun.header.family = htonl(af); 900 nb += sizeof tun - sizeof tun.data; 901 data = (char *)&tun; 902 } else 903 data = tun.data; 904 905 nw = write(bundle->dev.fd, data, nb); 906 if (nw != nb) { 907 if (nw == -1) 908 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n", 909 l->name, nb, strerror(errno)); 910 else 911 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw); 912 } 913 914 return nb; 915} 916 917struct mbuf * 918ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 919{ 920 int nb; 921 922 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 923 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n"); 924 m_freem(bp); 925 return NULL; 926 } 927 928 m_settype(bp, MB_IPIN); 929 930 nb = ip_Input(bundle, l, bp, AF_INET); 931 ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 932 933 return NULL; 934} 935 936#ifndef NOINET6 937struct mbuf * 938ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 939{ 940 int nb; 941 942 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) { 943 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n"); 944 m_freem(bp); 945 return NULL; 946 } 947 948 m_settype(bp, MB_IPV6IN); 949 950 nb = ip_Input(bundle, l, bp, AF_INET6); 951 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb); 952 953 return NULL; 954} 955#endif 956