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: src/usr.sbin/ppp/ip.c,v 1.78.2.11 2002/09/01 02:12:27 brian Exp $ 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 86 struct 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 102 static const char * 103 dns_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 122 static const char * 123 dns_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 149 static __inline int 150 PortMatch(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 char * 165 toprototxt(int cproto) 166 { 167 static char prototxt[16]; 168 struct protoent *pe; 169 170 if ((pe = getprotobynumber(cproto)) == NULL) 171 snprintf(prototxt, sizeof(prototxt), "%d", cproto); 172 else 173 snprintf(prototxt, sizeof(prototxt), "%s", pe->p_name); 174 return prototxt; 175 } 176 177 /* 178 * Check a packet against the given filter 179 * Returns 0 to accept the packet, non-zero to drop the packet. 180 * If psecs is not NULL, populate it with the timeout associated 181 * with the filter rule matched. 182 * 183 * If filtering is enabled, the initial fragment of a datagram must 184 * contain the complete protocol header, and subsequent fragments 185 * must not attempt to over-write it. 186 * 187 * One (and only one) of pip or pip6 must be set. 188 */ 189 int 190 FilterCheck(const unsigned char *packet, u_int32_t family, 191 const struct filter *filter, unsigned *psecs) 192 { 193 int gotinfo; /* true if IP payload decoded */ 194 int cproto; /* IPPROTO_* protocol number if (gotinfo) */ 195 int estab, syn, finrst; /* TCP state flags if (gotinfo) */ 196 u_short sport, dport; /* src, dest port from packet if (gotinfo) */ 197 int n; /* filter rule to process */ 198 int len; /* bytes used in dbuff */ 199 int didname; /* true if filter header printed */ 200 int match; /* true if condition matched */ 201 int mindata; /* minimum data size or zero */ 202 const struct filterent *fp = filter->rule; 203 char dbuff[100], dstip[16]; 204 struct ncpaddr srcaddr, dstaddr; 205 const char *payload; /* IP payload */ 206 int datalen; /* IP datagram length */ 207 208 if (fp->f_action == A_NONE) 209 return 0; /* No rule is given. Permit this packet */ 210 211 #ifndef NOINET6 212 if (family == AF_INET6) { 213 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 214 215 ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 216 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 217 datalen = ntohs(pip6->ip6_plen); 218 payload = packet + sizeof *pip6; 219 cproto = pip6->ip6_nxt; 220 } else 221 #endif 222 { 223 /* 224 * Deny any packet fragment that tries to over-write the header. 225 * Since we no longer have the real header available, punt on the 226 * largest normal header - 20 bytes for TCP without options, rounded 227 * up to the next possible fragment boundary. Since the smallest 228 * `legal' MTU is 576, and the smallest recommended MTU is 296, any 229 * fragmentation within this range is dubious at best 230 */ 231 const struct ip *pip = (const struct ip *)packet; 232 233 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */ 234 if (len > 0) { /* Not first fragment within datagram */ 235 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */ 236 log_Printf(LogFILTER, " error: illegal header\n"); 237 return 1; 238 } 239 /* permit fragments on in and out filter */ 240 if (!filter->fragok) { 241 log_Printf(LogFILTER, " error: illegal fragmentation\n"); 242 return 1; 243 } else 244 return 0; 245 } 246 247 ncpaddr_setip4(&srcaddr, pip->ip_src); 248 ncpaddr_setip4(&dstaddr, pip->ip_dst); 249 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 250 payload = packet + (pip->ip_hl << 2); 251 cproto = pip->ip_p; 252 } 253 254 gotinfo = estab = syn = finrst = didname = 0; 255 sport = dport = 0; 256 257 for (n = 0; n < MAXFILTERS; ) { 258 if (fp->f_action == A_NONE) { 259 n++; 260 fp++; 261 continue; 262 } 263 264 if (!didname) { 265 log_Printf(LogDEBUG, "%s filter:\n", filter->name); 266 didname = 1; 267 } 268 269 match = 0; 270 271 if ((ncprange_family(&fp->f_src) == AF_UNSPEC || 272 ncprange_contains(&fp->f_src, &srcaddr)) && 273 (ncprange_family(&fp->f_dst) == AF_UNSPEC || 274 ncprange_contains(&fp->f_dst, &dstaddr))) { 275 if (fp->f_proto != 0) { 276 if (!gotinfo) { 277 const struct tcphdr *th; 278 const struct udphdr *uh; 279 const struct icmp *ih; 280 #ifndef NOINET6 281 const struct icmp6_hdr *ih6; 282 #endif 283 mindata = 0; 284 sport = dport = 0; 285 estab = syn = finrst = -1; 286 287 switch (cproto) { 288 case IPPROTO_ICMP: 289 mindata = 8; /* ICMP must be at least 8 octets */ 290 ih = (const struct icmp *)payload; 291 sport = ntohs(ih->icmp_type); 292 if (log_IsKept(LogDEBUG)) 293 snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 294 break; 295 296 #ifndef NOINET6 297 case IPPROTO_ICMPV6: 298 mindata = 8; /* ICMP must be at least 8 octets */ 299 ih6 = (const struct icmp6_hdr *)payload; 300 sport = ntohs(ih6->icmp6_type); 301 if (log_IsKept(LogDEBUG)) 302 snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 303 break; 304 #endif 305 306 case IPPROTO_IGMP: 307 mindata = 8; /* IGMP uses 8-octet messages */ 308 break; 309 310 #ifdef IPPROTO_GRE 311 case IPPROTO_GRE: 312 mindata = 2; /* GRE uses 2-octet+ messages */ 313 break; 314 #endif 315 #ifdef IPPROTO_OSPFIGP 316 case IPPROTO_OSPFIGP: 317 mindata = 8; /* IGMP uses 8-octet messages */ 318 break; 319 #endif 320 #ifndef NOINET6 321 case IPPROTO_IPV6: 322 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */ 323 break; 324 #endif 325 326 case IPPROTO_UDP: 327 mindata = 8; /* UDP header is 8 octets */ 328 uh = (const struct udphdr *)payload; 329 sport = ntohs(uh->uh_sport); 330 dport = ntohs(uh->uh_dport); 331 if (log_IsKept(LogDEBUG)) 332 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d", 333 sport, dport); 334 break; 335 336 case IPPROTO_TCP: 337 th = (const struct tcphdr *)payload; 338 /* 339 * TCP headers are variable length. The following code 340 * ensures that the TCP header length isn't de-referenced if 341 * the datagram is too short 342 */ 343 if (datalen < 20 || datalen < (th->th_off << 2)) { 344 log_Printf(LogFILTER, " error: TCP header incorrect\n"); 345 return 1; 346 } 347 sport = ntohs(th->th_sport); 348 dport = ntohs(th->th_dport); 349 estab = (th->th_flags & TH_ACK); 350 syn = (th->th_flags & TH_SYN); 351 finrst = (th->th_flags & (TH_FIN|TH_RST)); 352 if (log_IsKept(LogDEBUG)) { 353 if (!estab) 354 snprintf(dbuff, sizeof dbuff, 355 "flags = %02x, sport = %d, dport = %d", 356 th->th_flags, sport, dport); 357 else 358 *dbuff = '\0'; 359 } 360 break; 361 default: 362 break; 363 } 364 365 if (datalen < mindata) { 366 log_Printf(LogFILTER, " error: proto %s must be at least" 367 " %d octets\n", toprototxt(cproto), mindata); 368 return 1; 369 } 370 371 if (log_IsKept(LogDEBUG)) { 372 if (estab != -1) { 373 len = strlen(dbuff); 374 snprintf(dbuff + len, sizeof dbuff - len, 375 ", estab = %d, syn = %d, finrst = %d", 376 estab, syn, finrst); 377 } 378 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", toprototxt(cproto), dbuff); 379 } 380 gotinfo = 1; 381 } 382 383 if (log_IsKept(LogDEBUG)) { 384 if (fp->f_srcop != OP_NONE) { 385 snprintf(dbuff, sizeof dbuff, ", src %s %d", 386 filter_Op2Nam(fp->f_srcop), fp->f_srcport); 387 len = strlen(dbuff); 388 } else 389 len = 0; 390 if (fp->f_dstop != OP_NONE) { 391 snprintf(dbuff + len, sizeof dbuff - len, 392 ", dst %s %d", filter_Op2Nam(fp->f_dstop), 393 fp->f_dstport); 394 } else if (!len) 395 *dbuff = '\0'; 396 397 log_Printf(LogDEBUG, " rule = %d: Address match, " 398 "check against proto %d%s, action = %s\n", 399 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action)); 400 } 401 402 if (cproto == fp->f_proto) { 403 if ((fp->f_srcop == OP_NONE || 404 PortMatch(fp->f_srcop, sport, fp->f_srcport)) && 405 (fp->f_dstop == OP_NONE || 406 PortMatch(fp->f_dstop, dport, fp->f_dstport)) && 407 (fp->f_estab == 0 || estab) && 408 (fp->f_syn == 0 || syn) && 409 (fp->f_finrst == 0 || finrst)) { 410 match = 1; 411 } 412 } 413 } else { 414 /* Address is matched and no protocol specified. Make a decision. */ 415 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n, 416 filter_Action2Nam(fp->f_action)); 417 match = 1; 418 } 419 } else 420 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n); 421 422 if (match != fp->f_invert) { 423 /* Take specified action */ 424 if (fp->f_action < A_NONE) 425 fp = &filter->rule[n = fp->f_action]; 426 else { 427 if (fp->f_action == A_PERMIT) { 428 if (psecs != NULL) 429 *psecs = fp->timeout; 430 if (strcmp(filter->name, "DIAL") == 0) { 431 /* If dial filter then even print out accept packets */ 432 if (log_IsKept(LogFILTER)) { 433 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 434 log_Printf(LogFILTER, "%sbound rule = %d accept %s " 435 "src = %s:%d dst = %s:%d\n", filter->name, n, 436 toprototxt(cproto), 437 ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 438 } 439 } 440 return 0; 441 } else { 442 if (log_IsKept(LogFILTER)) { 443 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 444 log_Printf(LogFILTER, 445 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n", 446 filter->name, n, toprototxt(cproto), 447 ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 448 } 449 return 1; 450 } /* Explict match. Deny this packet */ 451 } 452 } else { 453 n++; 454 fp++; 455 } 456 } 457 458 if (log_IsKept(LogFILTER)) { 459 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 460 log_Printf(LogFILTER, 461 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n", 462 filter->name, toprototxt(cproto), ncpaddr_ntoa(&srcaddr), sport, 463 dstip, dport); 464 } 465 466 return 1; /* No rule matched, deny this packet */ 467 } 468 469 static void 470 ip_LogDNS(const struct udphdr *uh, const char *direction) 471 { 472 struct dns_header header; 473 const u_short *pktptr; 474 const u_char *ptr; 475 u_short *hptr, tmp; 476 int len; 477 478 ptr = (const char *)uh + sizeof *uh; 479 len = ntohs(uh->uh_ulen) - sizeof *uh; 480 if (len < sizeof header + 5) /* rfc1024 */ 481 return; 482 483 pktptr = (const u_short *)ptr; 484 hptr = (u_short *)&header; 485 ptr += sizeof header; 486 len -= sizeof header; 487 488 while (pktptr < (const u_short *)ptr) { 489 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */ 490 pktptr++; 491 } 492 493 if (header.opcode == OPCODE_QUERY && header.qr == 0) { 494 /* rfc1035 */ 495 char namewithdot[MAXHOSTNAMELEN + 1], *n; 496 const char *qtype, *qclass; 497 const u_char *end; 498 499 n = namewithdot; 500 end = ptr + len - 4; 501 if (end - ptr >= sizeof namewithdot) 502 end = ptr + sizeof namewithdot - 1; 503 while (ptr < end) { 504 len = *ptr++; 505 if (len > end - ptr) 506 len = end - ptr; 507 if (n != namewithdot) 508 *n++ = '.'; 509 memcpy(n, ptr, len); 510 ptr += len; 511 n += len; 512 } 513 *n = '\0'; 514 515 if (log_IsKept(LogDNS)) { 516 memcpy(&tmp, end, sizeof tmp); 517 qtype = dns_Qtype2Txt(ntohs(tmp)); 518 memcpy(&tmp, end + 2, sizeof tmp); 519 qclass = dns_Qclass2Txt(ntohs(tmp)); 520 521 log_Printf(LogDNS, "%sbound query %s %s %s\n", 522 direction, qclass, qtype, namewithdot); 523 } 524 } 525 } 526 527 /* 528 * Check if the given packet matches the given filter. 529 * One of pip or pip6 must be set. 530 */ 531 int 532 PacketCheck(struct bundle *bundle, u_int32_t family, 533 const unsigned char *packet, int nb, struct filter *filter, 534 const char *prefix, unsigned *psecs) 535 { 536 static const char *const TcpFlags[] = { 537 "FIN", "SYN", "RST", "PSH", "ACK", "URG" 538 }; 539 const struct tcphdr *th; 540 const struct udphdr *uh; 541 const struct icmp *icmph; 542 #ifndef NOINET6 543 const struct icmp6_hdr *icmp6h; 544 #endif 545 const unsigned char *payload; 546 struct ncpaddr srcaddr, dstaddr; 547 int cproto, mask, len, n, pri, logit, loglen, result; 548 char logbuf[200]; 549 int datalen, frag; 550 u_char tos; 551 552 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) && 553 (!filter || filter->logok); 554 loglen = 0; 555 pri = 0; 556 557 #ifndef NOINET6 558 if (family == AF_INET6) { 559 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 560 561 ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 562 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 563 datalen = ntohs(pip6->ip6_plen); 564 payload = packet + sizeof *pip6; 565 cproto = pip6->ip6_nxt; 566 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */ 567 frag = 0; /* XXX: ??? */ 568 } else 569 #endif 570 { 571 const struct ip *pip = (const struct ip *)packet; 572 573 ncpaddr_setip4(&srcaddr, pip->ip_src); 574 ncpaddr_setip4(&dstaddr, pip->ip_dst); 575 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 576 payload = packet + (pip->ip_hl << 2); 577 cproto = pip->ip_p; 578 tos = pip->ip_tos; 579 frag = ntohs(pip->ip_off) & IP_OFFMASK; 580 } 581 582 uh = NULL; 583 584 if (logit && loglen < sizeof logbuf) { 585 if (prefix) 586 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix); 587 else if (filter) 588 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name); 589 else 590 snprintf(logbuf + loglen, sizeof logbuf - loglen, " "); 591 loglen += strlen(logbuf + loglen); 592 } 593 594 switch (cproto) { 595 case IPPROTO_ICMP: 596 if (logit && loglen < sizeof logbuf) { 597 len = datalen - sizeof *icmph; 598 icmph = (const struct icmp *)payload; 599 snprintf(logbuf + loglen, sizeof logbuf - loglen, 600 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type); 601 loglen += strlen(logbuf + loglen); 602 snprintf(logbuf + loglen, sizeof logbuf - loglen, 603 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 604 loglen += strlen(logbuf + loglen); 605 } 606 break; 607 608 #ifndef NOINET6 609 case IPPROTO_ICMPV6: 610 if (logit && loglen < sizeof logbuf) { 611 len = datalen - sizeof *icmp6h; 612 icmp6h = (const struct icmp6_hdr *)payload; 613 snprintf(logbuf + loglen, sizeof logbuf - loglen, 614 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type); 615 loglen += strlen(logbuf + loglen); 616 snprintf(logbuf + loglen, sizeof logbuf - loglen, 617 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 618 loglen += strlen(logbuf + loglen); 619 } 620 break; 621 #endif 622 623 case IPPROTO_UDP: 624 uh = (const struct udphdr *)payload; 625 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 626 pri++; 627 628 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport), 629 ntohs(uh->uh_dport))) 630 pri++; 631 632 if (logit && loglen < sizeof logbuf) { 633 len = datalen - sizeof *uh; 634 snprintf(logbuf + loglen, sizeof logbuf - loglen, 635 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport)); 636 loglen += strlen(logbuf + loglen); 637 snprintf(logbuf + loglen, sizeof logbuf - loglen, 638 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport), 639 len, nb); 640 loglen += strlen(logbuf + loglen); 641 } 642 643 if (Enabled(bundle, OPT_FILTERDECAP) && 644 payload[sizeof *uh] == HDLC_ADDR && 645 payload[sizeof *uh + 1] == HDLC_UI) { 646 u_short proto; 647 const char *type; 648 649 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto); 650 type = NULL; 651 652 switch (ntohs(proto)) { 653 case PROTO_IP: 654 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 655 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4, 656 nb - (payload - packet) - sizeof *uh - 4, filter, 657 logbuf, psecs); 658 if (result != -2) 659 return result; 660 type = "IP"; 661 break; 662 663 case PROTO_VJUNCOMP: type = "compressed VJ"; break; 664 case PROTO_VJCOMP: type = "uncompressed VJ"; break; 665 case PROTO_MP: type = "Multi-link"; break; 666 case PROTO_ICOMPD: type = "Individual link CCP"; break; 667 case PROTO_COMPD: type = "CCP"; break; 668 case PROTO_IPCP: type = "IPCP"; break; 669 case PROTO_LCP: type = "LCP"; break; 670 case PROTO_PAP: type = "PAP"; break; 671 case PROTO_CBCP: type = "CBCP"; break; 672 case PROTO_LQR: type = "LQR"; break; 673 case PROTO_CHAP: type = "CHAP"; break; 674 } 675 if (type) { 676 snprintf(logbuf + loglen, sizeof logbuf - loglen, 677 " - %s data", type); 678 loglen += strlen(logbuf + loglen); 679 } 680 } 681 682 break; 683 684 #ifdef IPPROTO_GRE 685 case IPPROTO_GRE: 686 if (logit && loglen < sizeof logbuf) { 687 snprintf(logbuf + loglen, sizeof logbuf - loglen, 688 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr)); 689 loglen += strlen(logbuf + loglen); 690 snprintf(logbuf + loglen, sizeof logbuf - loglen, 691 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 692 loglen += strlen(logbuf + loglen); 693 } 694 break; 695 #endif 696 697 #ifdef IPPROTO_OSPFIGP 698 case IPPROTO_OSPFIGP: 699 if (logit && loglen < sizeof logbuf) { 700 snprintf(logbuf + loglen, sizeof logbuf - loglen, 701 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr)); 702 loglen += strlen(logbuf + loglen); 703 snprintf(logbuf + loglen, sizeof logbuf - loglen, 704 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 705 loglen += strlen(logbuf + loglen); 706 } 707 break; 708 #endif 709 710 #ifndef NOINET6 711 case IPPROTO_IPV6: 712 if (logit && loglen < sizeof logbuf) { 713 snprintf(logbuf + loglen, sizeof logbuf - loglen, 714 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr)); 715 loglen += strlen(logbuf + loglen); 716 snprintf(logbuf + loglen, sizeof logbuf - loglen, 717 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 718 loglen += strlen(logbuf + loglen); 719 } 720 721 if (Enabled(bundle, OPT_FILTERDECAP)) { 722 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 723 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet), 724 filter, logbuf, psecs); 725 if (result != -2) 726 return result; 727 } 728 break; 729 #endif 730 731 case IPPROTO_IPIP: 732 if (logit && loglen < sizeof logbuf) { 733 snprintf(logbuf + loglen, sizeof logbuf - loglen, 734 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 735 loglen += strlen(logbuf + loglen); 736 snprintf(logbuf + loglen, sizeof logbuf - loglen, 737 "%s", ncpaddr_ntoa(&dstaddr)); 738 loglen += strlen(logbuf + loglen); 739 } 740 741 if (Enabled(bundle, OPT_FILTERDECAP) && 742 ((const struct ip *)payload)->ip_v == 4) { 743 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 744 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet), 745 filter, logbuf, psecs); 746 if (result != -2) 747 return result; 748 } 749 break; 750 751 case IPPROTO_ESP: 752 if (logit && loglen < sizeof logbuf) { 753 snprintf(logbuf + loglen, sizeof logbuf - loglen, 754 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 755 loglen += strlen(logbuf + loglen); 756 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 757 ncpaddr_ntoa(&dstaddr), payload); 758 loglen += strlen(logbuf + loglen); 759 } 760 break; 761 762 case IPPROTO_AH: 763 if (logit && loglen < sizeof logbuf) { 764 snprintf(logbuf + loglen, sizeof logbuf - loglen, 765 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr)); 766 loglen += strlen(logbuf + loglen); 767 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 768 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t)); 769 loglen += strlen(logbuf + loglen); 770 } 771 break; 772 773 case IPPROTO_IGMP: 774 if (logit && loglen < sizeof logbuf) { 775 uh = (const struct udphdr *)payload; 776 snprintf(logbuf + loglen, sizeof logbuf - loglen, 777 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), 778 ntohs(uh->uh_sport)); 779 loglen += strlen(logbuf + loglen); 780 snprintf(logbuf + loglen, sizeof logbuf - loglen, 781 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport)); 782 loglen += strlen(logbuf + loglen); 783 } 784 break; 785 786 case IPPROTO_TCP: 787 th = (const struct tcphdr *)payload; 788 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 789 pri++; 790 791 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport), 792 ntohs(th->th_dport))) 793 pri++; 794 795 if (logit && loglen < sizeof logbuf) { 796 len = datalen - (th->th_off << 2); 797 snprintf(logbuf + loglen, sizeof logbuf - loglen, 798 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport)); 799 loglen += strlen(logbuf + loglen); 800 snprintf(logbuf + loglen, sizeof logbuf - loglen, 801 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport)); 802 loglen += strlen(logbuf + loglen); 803 n = 0; 804 for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 805 if (th->th_flags & mask) { 806 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 807 loglen += strlen(logbuf + loglen); 808 } 809 n++; 810 } 811 snprintf(logbuf + loglen, sizeof logbuf - loglen, 812 " seq:%lx ack:%lx (%d/%d)", 813 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb); 814 loglen += strlen(logbuf + loglen); 815 if ((th->th_flags & TH_SYN) && nb > 40) { 816 const u_short *sp; 817 818 sp = (const u_short *)(payload + 20); 819 if (ntohs(sp[0]) == 0x0204) { 820 snprintf(logbuf + loglen, sizeof logbuf - loglen, 821 " MSS = %d", ntohs(sp[1])); 822 loglen += strlen(logbuf + loglen); 823 } 824 } 825 } 826 break; 827 828 default: 829 if (prefix) 830 return -2; 831 832 if (logit && loglen < sizeof logbuf) { 833 snprintf(logbuf + loglen, sizeof logbuf - loglen, 834 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr)); 835 loglen += strlen(logbuf + loglen); 836 snprintf(logbuf + loglen, sizeof logbuf - loglen, 837 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb); 838 loglen += strlen(logbuf + loglen); 839 } 840 break; 841 } 842 843 if (filter && FilterCheck(packet, family, filter, psecs)) { 844 if (logit) 845 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 846 result = -1; 847 } else { 848 /* Check Keep Alive filter */ 849 if (logit && log_IsKept(LogTCPIP)) { 850 unsigned alivesecs; 851 852 alivesecs = 0; 853 if (filter && 854 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs)) 855 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 856 else if (psecs != NULL) { 857 if(*psecs == 0) 858 *psecs = alivesecs; 859 if (*psecs) { 860 if (*psecs != alivesecs) 861 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n", 862 logbuf, *psecs, alivesecs); 863 else 864 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs); 865 } else 866 log_Printf(LogTCPIP, "%s\n", logbuf); 867 } 868 } 869 result = pri; 870 } 871 872 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS)) 873 ip_LogDNS(uh, filter->name); 874 875 return result; 876 } 877 878 static int 879 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af) 880 { 881 int nb, nw; 882 struct tun_data tun; 883 char *data; 884 unsigned secs, alivesecs; 885 886 nb = m_length(bp); 887 if (nb > sizeof tun.data) { 888 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n", 889 l->name, nb, (int)(sizeof tun.data)); 890 m_freem(bp); 891 return 0; 892 } 893 mbuf_Read(bp, tun.data, nb); 894 895 secs = 0; 896 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in, 897 NULL, &secs) < 0) 898 return 0; 899 900 alivesecs = 0; 901 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) { 902 if (secs == 0) 903 secs = alivesecs; 904 bundle_StartIdleTimer(bundle, secs); 905 } 906 907 if (bundle->dev.header) { 908 tun.header.family = htonl(af); 909 nb += sizeof tun - sizeof tun.data; 910 data = (char *)&tun; 911 } else 912 data = tun.data; 913 914 nw = write(bundle->dev.fd, data, nb); 915 if (nw != nb) { 916 if (nw == -1) 917 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n", 918 l->name, nb, strerror(errno)); 919 else 920 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw); 921 } 922 923 return nb; 924 } 925 926 struct mbuf * 927 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 928 { 929 int nb; 930 931 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 932 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n"); 933 m_freem(bp); 934 return NULL; 935 } 936 937 m_settype(bp, MB_IPIN); 938 939 nb = ip_Input(bundle, l, bp, AF_INET); 940 ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 941 942 return NULL; 943 } 944 945 #ifndef NOINET6 946 struct mbuf * 947 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 948 { 949 int nb; 950 951 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) { 952 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n"); 953 m_freem(bp); 954 return NULL; 955 } 956 957 m_settype(bp, MB_IPV6IN); 958 959 nb = ip_Input(bundle, l, bp, AF_INET6); 960 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb); 961 962 return NULL; 963 } 964 #endif 965