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 unsigned f; 114 115 for (f = 0; f < NELEM(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 unsigned f; 141 142 for (f = 0; f < NELEM(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 static 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 unsigned 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 >= (int)sizeof namewithdot) 502 end = ptr + sizeof namewithdot - 1; 503 while (ptr < end) { 504 len = *ptr++; 505 if ((int)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 char logbuf[200]; 537 static const char *const TcpFlags[] = { 538 "FIN", "SYN", "RST", "PSH", "ACK", "URG" 539 }; 540 const struct tcphdr *th; 541 const struct udphdr *uh; 542 const struct icmp *icmph; 543 #ifndef NOINET6 544 const struct icmp6_hdr *icmp6h; 545 #endif 546 const unsigned char *payload; 547 struct ncpaddr srcaddr, dstaddr; 548 int cproto, mask, len, n, pri, logit, result, datalen, frag; 549 unsigned loglen; 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 loglen += strlen(logbuf + loglen); 747 if (result != -2) 748 return result; 749 } 750 break; 751 752 case IPPROTO_ESP: 753 if (logit && loglen < sizeof logbuf) { 754 snprintf(logbuf + loglen, sizeof logbuf - loglen, 755 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 756 loglen += strlen(logbuf + loglen); 757 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 758 ncpaddr_ntoa(&dstaddr), payload); 759 loglen += strlen(logbuf + loglen); 760 } 761 break; 762 763 case IPPROTO_AH: 764 if (logit && loglen < sizeof logbuf) { 765 snprintf(logbuf + loglen, sizeof logbuf - loglen, 766 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr)); 767 loglen += strlen(logbuf + loglen); 768 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 769 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t)); 770 loglen += strlen(logbuf + loglen); 771 } 772 break; 773 774 case IPPROTO_IGMP: 775 if (logit && loglen < sizeof logbuf) { 776 uh = (const struct udphdr *)payload; 777 snprintf(logbuf + loglen, sizeof logbuf - loglen, 778 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), 779 ntohs(uh->uh_sport)); 780 loglen += strlen(logbuf + loglen); 781 snprintf(logbuf + loglen, sizeof logbuf - loglen, 782 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport)); 783 loglen += strlen(logbuf + loglen); 784 } 785 break; 786 787 case IPPROTO_TCP: 788 th = (const struct tcphdr *)payload; 789 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 790 pri++; 791 792 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport), 793 ntohs(th->th_dport))) 794 pri++; 795 796 if (logit && loglen < sizeof logbuf) { 797 len = datalen - (th->th_off << 2); 798 snprintf(logbuf + loglen, sizeof logbuf - loglen, 799 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport)); 800 loglen += strlen(logbuf + loglen); 801 snprintf(logbuf + loglen, sizeof logbuf - loglen, 802 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport)); 803 loglen += strlen(logbuf + loglen); 804 n = 0; 805 for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 806 if (th->th_flags & mask) { 807 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 808 loglen += strlen(logbuf + loglen); 809 } 810 n++; 811 } 812 snprintf(logbuf + loglen, sizeof logbuf - loglen, 813 " seq:%lx ack:%lx (%d/%d)", 814 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb); 815 loglen += strlen(logbuf + loglen); 816 if ((th->th_flags & TH_SYN) && nb > 40) { 817 const u_short *sp; 818 819 sp = (const u_short *)(payload + 20); 820 if (ntohs(sp[0]) == 0x0204) { 821 snprintf(logbuf + loglen, sizeof logbuf - loglen, 822 " MSS = %d", ntohs(sp[1])); 823 loglen += strlen(logbuf + loglen); 824 } 825 } 826 } 827 break; 828 829 default: 830 if (prefix) 831 return -2; 832 833 if (logit && loglen < sizeof logbuf) { 834 snprintf(logbuf + loglen, sizeof logbuf - loglen, 835 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr)); 836 loglen += strlen(logbuf + loglen); 837 snprintf(logbuf + loglen, sizeof logbuf - loglen, 838 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb); 839 loglen += strlen(logbuf + loglen); 840 } 841 break; 842 } 843 844 if (filter && FilterCheck(packet, family, filter, psecs)) { 845 if (logit) 846 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 847 result = -1; 848 } else { 849 /* Check Keep Alive filter */ 850 if (logit && log_IsKept(LogTCPIP)) { 851 unsigned alivesecs; 852 853 alivesecs = 0; 854 if (filter && 855 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs)) 856 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 857 else if (psecs != NULL) { 858 if(*psecs == 0) 859 *psecs = alivesecs; 860 if (*psecs) { 861 if (*psecs != alivesecs) 862 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n", 863 logbuf, *psecs, alivesecs); 864 else 865 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs); 866 } else 867 log_Printf(LogTCPIP, "%s\n", logbuf); 868 } 869 } 870 result = pri; 871 } 872 873 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS)) 874 ip_LogDNS(uh, filter->name); 875 876 return result; 877 } 878 879 static size_t 880 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af) 881 { 882 ssize_t nw; 883 size_t nb; 884 struct tun_data tun; 885 char *data; 886 unsigned secs, alivesecs; 887 888 nb = m_length(bp); 889 if (nb > sizeof tun.data) { 890 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %zu, max %d)\n", 891 l->name, nb, (int)(sizeof tun.data)); 892 m_freem(bp); 893 return 0; 894 } 895 mbuf_Read(bp, tun.data, nb); 896 897 secs = 0; 898 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in, 899 NULL, &secs) < 0) 900 return 0; 901 902 alivesecs = 0; 903 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) { 904 if (secs == 0) 905 secs = alivesecs; 906 bundle_StartIdleTimer(bundle, secs); 907 } 908 909 if (bundle->dev.header) { 910 tun.header.family = htonl(af); 911 nb += sizeof tun - sizeof tun.data; 912 data = (char *)&tun; 913 } else 914 data = tun.data; 915 916 nw = write(bundle->dev.fd, data, nb); 917 if (nw != (ssize_t)nb) { 918 if (nw == -1) 919 log_Printf(LogERROR, "ip_Input: %s: wrote %zu, got %s\n", 920 l->name, nb, strerror(errno)); 921 else 922 log_Printf(LogERROR, "ip_Input: %s: wrote %zu, got %zu\n", l->name, nb, nw); 923 } 924 925 return nb; 926 } 927 928 struct mbuf * 929 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 930 { 931 int nb; 932 933 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 934 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n"); 935 m_freem(bp); 936 return NULL; 937 } 938 939 m_settype(bp, MB_IPIN); 940 941 nb = ip_Input(bundle, l, bp, AF_INET); 942 ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 943 944 return NULL; 945 } 946 947 #ifndef NOINET6 948 struct mbuf * 949 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 950 { 951 int nb; 952 953 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) { 954 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n"); 955 m_freem(bp); 956 return NULL; 957 } 958 959 m_settype(bp, MB_IPV6IN); 960 961 nb = ip_Input(bundle, l, bp, AF_INET6); 962 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb); 963 964 return NULL; 965 } 966 #endif 967