ip_input.c revision 73399
1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 34 * $FreeBSD: head/sys/netinet/ip_input.c 73399 2001-03-04 01:39:19Z truckman $ 35 */ 36 37#define _IP_VHL 38 39#include "opt_bootp.h" 40#include "opt_ipfw.h" 41#include "opt_ipdn.h" 42#include "opt_ipdivert.h" 43#include "opt_ipfilter.h" 44#include "opt_ipstealth.h" 45#include "opt_ipsec.h" 46#include "opt_pfil_hooks.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/mbuf.h> 51#include <sys/malloc.h> 52#include <sys/domain.h> 53#include <sys/protosw.h> 54#include <sys/socket.h> 55#include <sys/time.h> 56#include <sys/kernel.h> 57#include <sys/syslog.h> 58#include <sys/sysctl.h> 59 60#include <net/pfil.h> 61#include <net/if.h> 62#include <net/if_var.h> 63#include <net/if_dl.h> 64#include <net/route.h> 65#include <net/netisr.h> 66#include <net/intrq.h> 67 68#include <netinet/in.h> 69#include <netinet/in_systm.h> 70#include <netinet/in_var.h> 71#include <netinet/ip.h> 72#include <netinet/in_pcb.h> 73#include <netinet/ip_var.h> 74#include <netinet/ip_icmp.h> 75#include <machine/in_cksum.h> 76 77#include <netinet/ipprotosw.h> 78 79#include <sys/socketvar.h> 80 81#include <netinet/ip_fw.h> 82 83#ifdef IPSEC 84#include <netinet6/ipsec.h> 85#include <netkey/key.h> 86#endif 87 88#include "faith.h" 89#if defined(NFAITH) && NFAITH > 0 90#include <net/if_types.h> 91#endif 92 93#ifdef DUMMYNET 94#include <netinet/ip_dummynet.h> 95#endif 96 97int rsvp_on = 0; 98static int ip_rsvp_on; 99struct socket *ip_rsvpd; 100 101int ipforwarding = 0; 102SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 103 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 104 105static int ipsendredirects = 1; /* XXX */ 106SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 107 &ipsendredirects, 0, "Enable sending IP redirects"); 108 109int ip_defttl = IPDEFTTL; 110SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 111 &ip_defttl, 0, "Maximum TTL on IP packets"); 112 113static int ip_dosourceroute = 0; 114SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 115 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 116 117static int ip_acceptsourceroute = 0; 118SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 119 CTLFLAG_RW, &ip_acceptsourceroute, 0, 120 "Enable accepting source routed IP packets"); 121 122static int ip_keepfaith = 0; 123SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 124 &ip_keepfaith, 0, 125 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 126 127/* 128 * XXX - Setting ip_checkinterface mostly implements the receive side of 129 * the Strong ES model described in RFC 1122, but since the routing table 130 * and transmit implementation do not implement the Strong ES model, so 131 * setting this to 1 results in an odd hybrid. 132 */ 133static int ip_checkinterface = 1; 134SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 135 &ip_checkinterface, 0, "Verify packet arrives on correct interface"); 136 137#ifdef DIAGNOSTIC 138static int ipprintfs = 0; 139#endif 140 141extern struct domain inetdomain; 142extern struct ipprotosw inetsw[]; 143u_char ip_protox[IPPROTO_MAX]; 144static int ipqmaxlen = IFQ_MAXLEN; 145struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 146SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 147 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 148SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 149 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); 150 151struct ipstat ipstat; 152SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD, 153 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 154 155/* Packet reassembly stuff */ 156#define IPREASS_NHASH_LOG2 6 157#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 158#define IPREASS_HMASK (IPREASS_NHASH - 1) 159#define IPREASS_HASH(x,y) \ 160 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 161 162static struct ipq ipq[IPREASS_NHASH]; 163static int nipq = 0; /* total # of reass queues */ 164static int maxnipq; 165const int ipintrq_present = 1; 166 167#ifdef IPCTL_DEFMTU 168SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 169 &ip_mtu, 0, "Default MTU"); 170#endif 171 172#ifdef IPSTEALTH 173static int ipstealth = 0; 174SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 175 &ipstealth, 0, ""); 176#endif 177 178 179/* Firewall hooks */ 180ip_fw_chk_t *ip_fw_chk_ptr; 181ip_fw_ctl_t *ip_fw_ctl_ptr; 182int fw_enable = 1 ; 183 184#ifdef DUMMYNET 185ip_dn_ctl_t *ip_dn_ctl_ptr; 186#endif 187 188 189/* 190 * We need to save the IP options in case a protocol wants to respond 191 * to an incoming packet over the same route if the packet got here 192 * using IP source routing. This allows connection establishment and 193 * maintenance when the remote end is on a network that is not known 194 * to us. 195 */ 196static int ip_nhops = 0; 197static struct ip_srcrt { 198 struct in_addr dst; /* final destination */ 199 char nop; /* one NOP to align */ 200 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 201 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 202} ip_srcrt; 203 204struct sockaddr_in *ip_fw_fwd_addr; 205 206static void save_rte __P((u_char *, struct in_addr)); 207static int ip_dooptions __P((struct mbuf *)); 208static void ip_forward __P((struct mbuf *, int)); 209static void ip_freef __P((struct ipq *)); 210#ifdef IPDIVERT 211static struct mbuf *ip_reass __P((struct mbuf *, 212 struct ipq *, struct ipq *, u_int32_t *, u_int16_t *)); 213#else 214static struct mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *)); 215#endif 216static struct in_ifaddr *ip_rtaddr __P((struct in_addr)); 217static void ipintr __P((void)); 218 219/* 220 * IP initialization: fill in IP protocol switch table. 221 * All protocols not implemented in kernel go to raw IP protocol handler. 222 */ 223void 224ip_init() 225{ 226 register struct ipprotosw *pr; 227 register int i; 228 229 TAILQ_INIT(&in_ifaddrhead); 230 pr = (struct ipprotosw *)pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 231 if (pr == 0) 232 panic("ip_init"); 233 for (i = 0; i < IPPROTO_MAX; i++) 234 ip_protox[i] = pr - inetsw; 235 for (pr = (struct ipprotosw *)inetdomain.dom_protosw; 236 pr < (struct ipprotosw *)inetdomain.dom_protoswNPROTOSW; pr++) 237 if (pr->pr_domain->dom_family == PF_INET && 238 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 239 ip_protox[pr->pr_protocol] = pr - inetsw; 240 241 for (i = 0; i < IPREASS_NHASH; i++) 242 ipq[i].next = ipq[i].prev = &ipq[i]; 243 244 maxnipq = nmbclusters/4; 245 246 ip_id = time_second & 0xffff; 247 ipintrq.ifq_maxlen = ipqmaxlen; 248 mtx_init(&ipintrq.ifq_mtx, "ip_inq", MTX_DEF); 249 250 register_netisr(NETISR_IP, ipintr); 251} 252 253static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 254static struct route ipforward_rt; 255 256/* 257 * Ip input routine. Checksum and byte swap header. If fragmented 258 * try to reassemble. Process options. Pass to next level. 259 */ 260void 261ip_input(struct mbuf *m) 262{ 263 struct ip *ip; 264 struct ipq *fp; 265 struct in_ifaddr *ia = NULL; 266 int i, hlen, checkif; 267 u_short sum; 268 u_int16_t divert_cookie; /* firewall cookie */ 269 struct in_addr pkt_dst; 270#ifdef IPDIVERT 271 u_int32_t divert_info = 0; /* packet divert/tee info */ 272#endif 273 struct ip_fw_chain *rule = NULL; 274#ifdef PFIL_HOOKS 275 struct packet_filter_hook *pfh; 276 struct mbuf *m0; 277 int rv; 278#endif /* PFIL_HOOKS */ 279 280#ifdef IPDIVERT 281 /* Get and reset firewall cookie */ 282 divert_cookie = ip_divert_cookie; 283 ip_divert_cookie = 0; 284#else 285 divert_cookie = 0; 286#endif 287 288#if defined(IPFIREWALL) && defined(DUMMYNET) 289 /* 290 * dummynet packet are prepended a vestigial mbuf with 291 * m_type = MT_DUMMYNET and m_data pointing to the matching 292 * rule. 293 */ 294 if (m->m_type == MT_DUMMYNET) { 295 rule = (struct ip_fw_chain *)(m->m_data) ; 296 m = m->m_next ; 297 ip = mtod(m, struct ip *); 298 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 299 goto iphack ; 300 } else 301 rule = NULL ; 302#endif 303 304#ifdef DIAGNOSTIC 305 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 306 panic("ip_input no HDR"); 307#endif 308 ipstat.ips_total++; 309 310 if (m->m_pkthdr.len < sizeof(struct ip)) 311 goto tooshort; 312 313 if (m->m_len < sizeof (struct ip) && 314 (m = m_pullup(m, sizeof (struct ip))) == 0) { 315 ipstat.ips_toosmall++; 316 return; 317 } 318 ip = mtod(m, struct ip *); 319 320 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 321 ipstat.ips_badvers++; 322 goto bad; 323 } 324 325 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 326 if (hlen < sizeof(struct ip)) { /* minimum header length */ 327 ipstat.ips_badhlen++; 328 goto bad; 329 } 330 if (hlen > m->m_len) { 331 if ((m = m_pullup(m, hlen)) == 0) { 332 ipstat.ips_badhlen++; 333 return; 334 } 335 ip = mtod(m, struct ip *); 336 } 337 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 338 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 339 } else { 340 if (hlen == sizeof(struct ip)) { 341 sum = in_cksum_hdr(ip); 342 } else { 343 sum = in_cksum(m, hlen); 344 } 345 } 346 if (sum) { 347 ipstat.ips_badsum++; 348 goto bad; 349 } 350 351 /* 352 * Convert fields to host representation. 353 */ 354 NTOHS(ip->ip_len); 355 if (ip->ip_len < hlen) { 356 ipstat.ips_badlen++; 357 goto bad; 358 } 359 NTOHS(ip->ip_off); 360 361 /* 362 * Check that the amount of data in the buffers 363 * is as at least much as the IP header would have us expect. 364 * Trim mbufs if longer than we expect. 365 * Drop packet if shorter than we expect. 366 */ 367 if (m->m_pkthdr.len < ip->ip_len) { 368tooshort: 369 ipstat.ips_tooshort++; 370 goto bad; 371 } 372 if (m->m_pkthdr.len > ip->ip_len) { 373 if (m->m_len == m->m_pkthdr.len) { 374 m->m_len = ip->ip_len; 375 m->m_pkthdr.len = ip->ip_len; 376 } else 377 m_adj(m, ip->ip_len - m->m_pkthdr.len); 378 } 379 /* 380 * IpHack's section. 381 * Right now when no processing on packet has done 382 * and it is still fresh out of network we do our black 383 * deals with it. 384 * - Firewall: deny/allow/divert 385 * - Xlate: translate packet's addr/port (NAT). 386 * - Pipe: pass pkt through dummynet. 387 * - Wrap: fake packet's addr/port <unimpl.> 388 * - Encapsulate: put it in another IP and send out. <unimp.> 389 */ 390 391#if defined(IPFIREWALL) && defined(DUMMYNET) 392iphack: 393#endif 394 395#ifdef PFIL_HOOKS 396 /* 397 * Run through list of hooks for input packets. If there are any 398 * filters which require that additional packets in the flow are 399 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 400 * Note that filters must _never_ set this flag, as another filter 401 * in the list may have previously cleared it. 402 */ 403 m0 = m; 404 pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 405 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 406 if (pfh->pfil_func) { 407 rv = pfh->pfil_func(ip, hlen, 408 m->m_pkthdr.rcvif, 0, &m0); 409 if (rv) 410 return; 411 m = m0; 412 if (m == NULL) 413 return; 414 ip = mtod(m, struct ip *); 415 } 416#endif /* PFIL_HOOKS */ 417 418 if (fw_enable && ip_fw_chk_ptr) { 419#ifdef IPFIREWALL_FORWARD 420 /* 421 * If we've been forwarded from the output side, then 422 * skip the firewall a second time 423 */ 424 if (ip_fw_fwd_addr) 425 goto ours; 426#endif /* IPFIREWALL_FORWARD */ 427 /* 428 * See the comment in ip_output for the return values 429 * produced by the firewall. 430 */ 431 i = (*ip_fw_chk_ptr)(&ip, 432 hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr); 433 if (i & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */ 434 if (m) 435 m_freem(m); 436 return ; 437 } 438 if (m == NULL) { /* Packet discarded by firewall */ 439 static int __debug=10; 440 if (__debug >0) { 441 printf("firewall returns NULL, please update!\n"); 442 __debug-- ; 443 } 444 return; 445 } 446 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ 447 goto pass; 448#ifdef DUMMYNET 449 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) { 450 /* Send packet to the appropriate pipe */ 451 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule, 452 0); 453 return; 454 } 455#endif 456#ifdef IPDIVERT 457 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { 458 /* Divert or tee packet */ 459 divert_info = i; 460 goto ours; 461 } 462#endif 463#ifdef IPFIREWALL_FORWARD 464 if (i == 0 && ip_fw_fwd_addr != NULL) 465 goto pass; 466#endif 467 /* 468 * if we get here, the packet must be dropped 469 */ 470 m_freem(m); 471 return; 472 } 473pass: 474 475 /* 476 * Process options and, if not destined for us, 477 * ship it on. ip_dooptions returns 1 when an 478 * error was detected (causing an icmp message 479 * to be sent and the original packet to be freed). 480 */ 481 ip_nhops = 0; /* for source routed packets */ 482 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 483#ifdef IPFIREWALL_FORWARD 484 ip_fw_fwd_addr = NULL; 485#endif 486 return; 487 } 488 489 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 490 * matter if it is destined to another node, or whether it is 491 * a multicast one, RSVP wants it! and prevents it from being forwarded 492 * anywhere else. Also checks if the rsvp daemon is running before 493 * grabbing the packet. 494 */ 495 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 496 goto ours; 497 498 /* 499 * Check our list of addresses, to see if the packet is for us. 500 * If we don't have any addresses, assume any unicast packet 501 * we receive might be for us (and let the upper layers deal 502 * with it). 503 */ 504 if (TAILQ_EMPTY(&in_ifaddrhead) && 505 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 506 goto ours; 507 508 /* 509 * Cache the destination address of the packet; this may be 510 * changed by use of 'ipfw fwd'. 511 */ 512 pkt_dst = ip_fw_fwd_addr == NULL ? 513 ip->ip_dst : ip_fw_fwd_addr->sin_addr; 514 515 /* 516 * Don't accept packets with a loopback destination address 517 * unless they arrived via the loopback interface. 518 * XXX - should ip->ip_dst.s_addr be pkt_dst.s_addr? 519 */ 520 if ((ntohl(ip->ip_dst.s_addr) & IN_CLASSA_NET) == 521 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT) && 522 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 523 m_freem(m); 524#ifdef IPFIREWALL_FORWARD 525 ip_fw_fwd_addr = NULL; 526#endif 527 return; 528 } 529 530 /* 531 * Enable a consistency check between the destination address 532 * and the arrival interface for a unicast packet (the RFC 1122 533 * strong ES model) if IP forwarding is disabled and the packet 534 * is not locally generated. 535 */ 536 checkif = ip_checkinterface && (ipforwarding == 0) && 537 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0); 538 539 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 540#define satosin(sa) ((struct sockaddr_in *)(sa)) 541 542#ifdef BOOTP_COMPAT 543 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 544 goto ours; 545#endif 546 /* 547 * If the address matches, verify that the packet 548 * arrived via the correct interface if checking is 549 * enabled. 550 */ 551 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && 552 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 553 goto ours; 554 /* 555 * Only accept broadcast packets that arrive via the 556 * matching interface. Reception of forwarded directed 557 * broadcasts would be handled via ip_forward() and 558 * ether_output() with the loopback into the stack for 559 * SIMPLEX interfaces handled by ether_output(). 560 */ 561 if (ia->ia_ifp == m->m_pkthdr.rcvif && 562 ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 563 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 564 pkt_dst.s_addr) 565 goto ours; 566 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr) 567 goto ours; 568 } 569 } 570 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 571 struct in_multi *inm; 572 if (ip_mrouter) { 573 /* 574 * If we are acting as a multicast router, all 575 * incoming multicast packets are passed to the 576 * kernel-level multicast forwarding function. 577 * The packet is returned (relatively) intact; if 578 * ip_mforward() returns a non-zero value, the packet 579 * must be discarded, else it may be accepted below. 580 */ 581 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 582 ipstat.ips_cantforward++; 583 m_freem(m); 584 return; 585 } 586 587 /* 588 * The process-level routing demon needs to receive 589 * all multicast IGMP packets, whether or not this 590 * host belongs to their destination groups. 591 */ 592 if (ip->ip_p == IPPROTO_IGMP) 593 goto ours; 594 ipstat.ips_forward++; 595 } 596 /* 597 * See if we belong to the destination multicast group on the 598 * arrival interface. 599 */ 600 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 601 if (inm == NULL) { 602 ipstat.ips_notmember++; 603 m_freem(m); 604 return; 605 } 606 goto ours; 607 } 608 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 609 goto ours; 610 if (ip->ip_dst.s_addr == INADDR_ANY) 611 goto ours; 612 613#if defined(NFAITH) && 0 < NFAITH 614 /* 615 * FAITH(Firewall Aided Internet Translator) 616 */ 617 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 618 if (ip_keepfaith) { 619 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 620 goto ours; 621 } 622 m_freem(m); 623 return; 624 } 625#endif 626 /* 627 * Not for us; forward if possible and desirable. 628 */ 629 if (ipforwarding == 0) { 630 ipstat.ips_cantforward++; 631 m_freem(m); 632 } else 633 ip_forward(m, 0); 634#ifdef IPFIREWALL_FORWARD 635 ip_fw_fwd_addr = NULL; 636#endif 637 return; 638 639ours: 640 /* Count the packet in the ip address stats */ 641 if (ia != NULL) { 642 ia->ia_ifa.if_ipackets++; 643 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 644 } 645 646 /* 647 * If offset or IP_MF are set, must reassemble. 648 * Otherwise, nothing need be done. 649 * (We could look in the reassembly queue to see 650 * if the packet was previously fragmented, 651 * but it's not worth the time; just let them time out.) 652 */ 653 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 654 655 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 656 /* 657 * Look for queue of fragments 658 * of this datagram. 659 */ 660 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) 661 if (ip->ip_id == fp->ipq_id && 662 ip->ip_src.s_addr == fp->ipq_src.s_addr && 663 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 664 ip->ip_p == fp->ipq_p) 665 goto found; 666 667 fp = 0; 668 669 /* check if there's a place for the new queue */ 670 if (nipq > maxnipq) { 671 /* 672 * drop something from the tail of the current queue 673 * before proceeding further 674 */ 675 if (ipq[sum].prev == &ipq[sum]) { /* gak */ 676 for (i = 0; i < IPREASS_NHASH; i++) { 677 if (ipq[i].prev != &ipq[i]) { 678 ip_freef(ipq[i].prev); 679 break; 680 } 681 } 682 } else 683 ip_freef(ipq[sum].prev); 684 } 685found: 686 /* 687 * Adjust ip_len to not reflect header, 688 * convert offset of this to bytes. 689 */ 690 ip->ip_len -= hlen; 691 if (ip->ip_off & IP_MF) { 692 /* 693 * Make sure that fragments have a data length 694 * that's a non-zero multiple of 8 bytes. 695 */ 696 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 697 ipstat.ips_toosmall++; /* XXX */ 698 goto bad; 699 } 700 m->m_flags |= M_FRAG; 701 } 702 ip->ip_off <<= 3; 703 704 /* 705 * Attempt reassembly; if it succeeds, proceed. 706 */ 707 ipstat.ips_fragments++; 708 m->m_pkthdr.header = ip; 709#ifdef IPDIVERT 710 m = ip_reass(m, 711 fp, &ipq[sum], &divert_info, &divert_cookie); 712#else 713 m = ip_reass(m, fp, &ipq[sum]); 714#endif 715 if (m == 0) { 716#ifdef IPFIREWALL_FORWARD 717 ip_fw_fwd_addr = NULL; 718#endif 719 return; 720 } 721 ipstat.ips_reassembled++; 722 ip = mtod(m, struct ip *); 723 /* Get the header length of the reassembled packet */ 724 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 725#ifdef IPDIVERT 726 /* Restore original checksum before diverting packet */ 727 if (divert_info != 0) { 728 ip->ip_len += hlen; 729 HTONS(ip->ip_len); 730 HTONS(ip->ip_off); 731 ip->ip_sum = 0; 732 if (hlen == sizeof(struct ip)) 733 ip->ip_sum = in_cksum_hdr(ip); 734 else 735 ip->ip_sum = in_cksum(m, hlen); 736 NTOHS(ip->ip_off); 737 NTOHS(ip->ip_len); 738 ip->ip_len -= hlen; 739 } 740#endif 741 } else 742 ip->ip_len -= hlen; 743 744#ifdef IPDIVERT 745 /* 746 * Divert or tee packet to the divert protocol if required. 747 * 748 * If divert_info is zero then cookie should be too, so we shouldn't 749 * need to clear them here. Assume divert_packet() does so also. 750 */ 751 if (divert_info != 0) { 752 struct mbuf *clone = NULL; 753 754 /* Clone packet if we're doing a 'tee' */ 755 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0) 756 clone = m_dup(m, M_DONTWAIT); 757 758 /* Restore packet header fields to original values */ 759 ip->ip_len += hlen; 760 HTONS(ip->ip_len); 761 HTONS(ip->ip_off); 762 763 /* Deliver packet to divert input routine */ 764 ip_divert_cookie = divert_cookie; 765 divert_packet(m, 1, divert_info & 0xffff); 766 ipstat.ips_delivered++; 767 768 /* If 'tee', continue with original packet */ 769 if (clone == NULL) 770 return; 771 m = clone; 772 ip = mtod(m, struct ip *); 773 } 774#endif 775 776 /* 777 * Switch out to protocol's input routine. 778 */ 779 ipstat.ips_delivered++; 780 { 781 int off = hlen, nh = ip->ip_p; 782 783 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off, nh); 784#ifdef IPFIREWALL_FORWARD 785 ip_fw_fwd_addr = NULL; /* tcp needed it */ 786#endif 787 return; 788 } 789bad: 790#ifdef IPFIREWALL_FORWARD 791 ip_fw_fwd_addr = NULL; 792#endif 793 m_freem(m); 794} 795 796/* 797 * IP software interrupt routine - to go away sometime soon 798 */ 799static void 800ipintr(void) 801{ 802 struct mbuf *m; 803 804 while (1) { 805 IF_DEQUEUE(&ipintrq, m); 806 if (m == 0) 807 return; 808 ip_input(m); 809 } 810} 811 812/* 813 * Take incoming datagram fragment and try to reassemble it into 814 * whole datagram. If a chain for reassembly of this datagram already 815 * exists, then it is given as fp; otherwise have to make a chain. 816 * 817 * When IPDIVERT enabled, keep additional state with each packet that 818 * tells us if we need to divert or tee the packet we're building. 819 */ 820 821static struct mbuf * 822#ifdef IPDIVERT 823ip_reass(m, fp, where, divinfo, divcookie) 824#else 825ip_reass(m, fp, where) 826#endif 827 register struct mbuf *m; 828 register struct ipq *fp; 829 struct ipq *where; 830#ifdef IPDIVERT 831 u_int32_t *divinfo; 832 u_int16_t *divcookie; 833#endif 834{ 835 struct ip *ip = mtod(m, struct ip *); 836 register struct mbuf *p, *q, *nq; 837 struct mbuf *t; 838 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 839 int i, next; 840 841 /* 842 * Presence of header sizes in mbufs 843 * would confuse code below. 844 */ 845 m->m_data += hlen; 846 m->m_len -= hlen; 847 848 /* 849 * If first fragment to arrive, create a reassembly queue. 850 */ 851 if (fp == 0) { 852 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 853 goto dropfrag; 854 fp = mtod(t, struct ipq *); 855 insque(fp, where); 856 nipq++; 857 fp->ipq_ttl = IPFRAGTTL; 858 fp->ipq_p = ip->ip_p; 859 fp->ipq_id = ip->ip_id; 860 fp->ipq_src = ip->ip_src; 861 fp->ipq_dst = ip->ip_dst; 862 fp->ipq_frags = m; 863 m->m_nextpkt = NULL; 864#ifdef IPDIVERT 865 fp->ipq_div_info = 0; 866 fp->ipq_div_cookie = 0; 867#endif 868 goto inserted; 869 } 870 871#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 872 873 /* 874 * Find a segment which begins after this one does. 875 */ 876 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 877 if (GETIP(q)->ip_off > ip->ip_off) 878 break; 879 880 /* 881 * If there is a preceding segment, it may provide some of 882 * our data already. If so, drop the data from the incoming 883 * segment. If it provides all of our data, drop us, otherwise 884 * stick new segment in the proper place. 885 * 886 * If some of the data is dropped from the the preceding 887 * segment, then it's checksum is invalidated. 888 */ 889 if (p) { 890 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 891 if (i > 0) { 892 if (i >= ip->ip_len) 893 goto dropfrag; 894 m_adj(m, i); 895 m->m_pkthdr.csum_flags = 0; 896 ip->ip_off += i; 897 ip->ip_len -= i; 898 } 899 m->m_nextpkt = p->m_nextpkt; 900 p->m_nextpkt = m; 901 } else { 902 m->m_nextpkt = fp->ipq_frags; 903 fp->ipq_frags = m; 904 } 905 906 /* 907 * While we overlap succeeding segments trim them or, 908 * if they are completely covered, dequeue them. 909 */ 910 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 911 q = nq) { 912 i = (ip->ip_off + ip->ip_len) - 913 GETIP(q)->ip_off; 914 if (i < GETIP(q)->ip_len) { 915 GETIP(q)->ip_len -= i; 916 GETIP(q)->ip_off += i; 917 m_adj(q, i); 918 q->m_pkthdr.csum_flags = 0; 919 break; 920 } 921 nq = q->m_nextpkt; 922 m->m_nextpkt = nq; 923 m_freem(q); 924 } 925 926inserted: 927 928#ifdef IPDIVERT 929 /* 930 * Transfer firewall instructions to the fragment structure. 931 * Any fragment diverting causes the whole packet to divert. 932 */ 933 fp->ipq_div_info = *divinfo; 934 fp->ipq_div_cookie = *divcookie; 935 *divinfo = 0; 936 *divcookie = 0; 937#endif 938 939 /* 940 * Check for complete reassembly. 941 */ 942 next = 0; 943 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 944 if (GETIP(q)->ip_off != next) 945 return (0); 946 next += GETIP(q)->ip_len; 947 } 948 /* Make sure the last packet didn't have the IP_MF flag */ 949 if (p->m_flags & M_FRAG) 950 return (0); 951 952 /* 953 * Reassembly is complete. Make sure the packet is a sane size. 954 */ 955 q = fp->ipq_frags; 956 ip = GETIP(q); 957 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 958 ipstat.ips_toolong++; 959 ip_freef(fp); 960 return (0); 961 } 962 963 /* 964 * Concatenate fragments. 965 */ 966 m = q; 967 t = m->m_next; 968 m->m_next = 0; 969 m_cat(m, t); 970 nq = q->m_nextpkt; 971 q->m_nextpkt = 0; 972 for (q = nq; q != NULL; q = nq) { 973 nq = q->m_nextpkt; 974 q->m_nextpkt = NULL; 975 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 976 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 977 m_cat(m, q); 978 } 979 980#ifdef IPDIVERT 981 /* 982 * Extract firewall instructions from the fragment structure. 983 */ 984 *divinfo = fp->ipq_div_info; 985 *divcookie = fp->ipq_div_cookie; 986#endif 987 988 /* 989 * Create header for new ip packet by 990 * modifying header of first packet; 991 * dequeue and discard fragment reassembly header. 992 * Make header visible. 993 */ 994 ip->ip_len = next; 995 ip->ip_src = fp->ipq_src; 996 ip->ip_dst = fp->ipq_dst; 997 remque(fp); 998 nipq--; 999 (void) m_free(dtom(fp)); 1000 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 1001 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 1002 /* some debugging cruft by sklower, below, will go away soon */ 1003 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1004 register int plen = 0; 1005 for (t = m; t; t = t->m_next) 1006 plen += t->m_len; 1007 m->m_pkthdr.len = plen; 1008 } 1009 return (m); 1010 1011dropfrag: 1012#ifdef IPDIVERT 1013 *divinfo = 0; 1014 *divcookie = 0; 1015#endif 1016 ipstat.ips_fragdropped++; 1017 m_freem(m); 1018 return (0); 1019 1020#undef GETIP 1021} 1022 1023/* 1024 * Free a fragment reassembly header and all 1025 * associated datagrams. 1026 */ 1027static void 1028ip_freef(fp) 1029 struct ipq *fp; 1030{ 1031 register struct mbuf *q; 1032 1033 while (fp->ipq_frags) { 1034 q = fp->ipq_frags; 1035 fp->ipq_frags = q->m_nextpkt; 1036 m_freem(q); 1037 } 1038 remque(fp); 1039 (void) m_free(dtom(fp)); 1040 nipq--; 1041} 1042 1043/* 1044 * IP timer processing; 1045 * if a timer expires on a reassembly 1046 * queue, discard it. 1047 */ 1048void 1049ip_slowtimo() 1050{ 1051 register struct ipq *fp; 1052 int s = splnet(); 1053 int i; 1054 1055 for (i = 0; i < IPREASS_NHASH; i++) { 1056 fp = ipq[i].next; 1057 if (fp == 0) 1058 continue; 1059 while (fp != &ipq[i]) { 1060 --fp->ipq_ttl; 1061 fp = fp->next; 1062 if (fp->prev->ipq_ttl == 0) { 1063 ipstat.ips_fragtimeout++; 1064 ip_freef(fp->prev); 1065 } 1066 } 1067 } 1068 ipflow_slowtimo(); 1069 splx(s); 1070} 1071 1072/* 1073 * Drain off all datagram fragments. 1074 */ 1075void 1076ip_drain() 1077{ 1078 int i; 1079 1080 for (i = 0; i < IPREASS_NHASH; i++) { 1081 while (ipq[i].next != &ipq[i]) { 1082 ipstat.ips_fragdropped++; 1083 ip_freef(ipq[i].next); 1084 } 1085 } 1086 in_rtqdrain(); 1087} 1088 1089/* 1090 * Do option processing on a datagram, 1091 * possibly discarding it if bad options are encountered, 1092 * or forwarding it if source-routed. 1093 * Returns 1 if packet has been forwarded/freed, 1094 * 0 if the packet should be processed further. 1095 */ 1096static int 1097ip_dooptions(m) 1098 struct mbuf *m; 1099{ 1100 register struct ip *ip = mtod(m, struct ip *); 1101 register u_char *cp; 1102 register struct ip_timestamp *ipt; 1103 register struct in_ifaddr *ia; 1104 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1105 struct in_addr *sin, dst; 1106 n_time ntime; 1107 1108 dst = ip->ip_dst; 1109 cp = (u_char *)(ip + 1); 1110 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1111 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1112 opt = cp[IPOPT_OPTVAL]; 1113 if (opt == IPOPT_EOL) 1114 break; 1115 if (opt == IPOPT_NOP) 1116 optlen = 1; 1117 else { 1118 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1119 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1120 goto bad; 1121 } 1122 optlen = cp[IPOPT_OLEN]; 1123 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1124 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1125 goto bad; 1126 } 1127 } 1128 switch (opt) { 1129 1130 default: 1131 break; 1132 1133 /* 1134 * Source routing with record. 1135 * Find interface with current destination address. 1136 * If none on this machine then drop if strictly routed, 1137 * or do nothing if loosely routed. 1138 * Record interface address and bring up next address 1139 * component. If strictly routed make sure next 1140 * address is on directly accessible net. 1141 */ 1142 case IPOPT_LSRR: 1143 case IPOPT_SSRR: 1144 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1145 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1146 goto bad; 1147 } 1148 ipaddr.sin_addr = ip->ip_dst; 1149 ia = (struct in_ifaddr *) 1150 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1151 if (ia == 0) { 1152 if (opt == IPOPT_SSRR) { 1153 type = ICMP_UNREACH; 1154 code = ICMP_UNREACH_SRCFAIL; 1155 goto bad; 1156 } 1157 if (!ip_dosourceroute) 1158 goto nosourcerouting; 1159 /* 1160 * Loose routing, and not at next destination 1161 * yet; nothing to do except forward. 1162 */ 1163 break; 1164 } 1165 off--; /* 0 origin */ 1166 if (off > optlen - (int)sizeof(struct in_addr)) { 1167 /* 1168 * End of source route. Should be for us. 1169 */ 1170 if (!ip_acceptsourceroute) 1171 goto nosourcerouting; 1172 save_rte(cp, ip->ip_src); 1173 break; 1174 } 1175 1176 if (!ip_dosourceroute) { 1177 if (ipforwarding) { 1178 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1179 /* 1180 * Acting as a router, so generate ICMP 1181 */ 1182nosourcerouting: 1183 strcpy(buf, inet_ntoa(ip->ip_dst)); 1184 log(LOG_WARNING, 1185 "attempted source route from %s to %s\n", 1186 inet_ntoa(ip->ip_src), buf); 1187 type = ICMP_UNREACH; 1188 code = ICMP_UNREACH_SRCFAIL; 1189 goto bad; 1190 } else { 1191 /* 1192 * Not acting as a router, so silently drop. 1193 */ 1194 ipstat.ips_cantforward++; 1195 m_freem(m); 1196 return (1); 1197 } 1198 } 1199 1200 /* 1201 * locate outgoing interface 1202 */ 1203 (void)memcpy(&ipaddr.sin_addr, cp + off, 1204 sizeof(ipaddr.sin_addr)); 1205 1206 if (opt == IPOPT_SSRR) { 1207#define INA struct in_ifaddr * 1208#define SA struct sockaddr * 1209 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1210 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1211 } else 1212 ia = ip_rtaddr(ipaddr.sin_addr); 1213 if (ia == 0) { 1214 type = ICMP_UNREACH; 1215 code = ICMP_UNREACH_SRCFAIL; 1216 goto bad; 1217 } 1218 ip->ip_dst = ipaddr.sin_addr; 1219 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1220 sizeof(struct in_addr)); 1221 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1222 /* 1223 * Let ip_intr's mcast routing check handle mcast pkts 1224 */ 1225 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1226 break; 1227 1228 case IPOPT_RR: 1229 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1230 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1231 goto bad; 1232 } 1233 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1234 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1235 goto bad; 1236 } 1237 /* 1238 * If no space remains, ignore. 1239 */ 1240 off--; /* 0 origin */ 1241 if (off > optlen - (int)sizeof(struct in_addr)) 1242 break; 1243 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1244 sizeof(ipaddr.sin_addr)); 1245 /* 1246 * locate outgoing interface; if we're the destination, 1247 * use the incoming interface (should be same). 1248 */ 1249 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1250 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1251 type = ICMP_UNREACH; 1252 code = ICMP_UNREACH_HOST; 1253 goto bad; 1254 } 1255 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1256 sizeof(struct in_addr)); 1257 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1258 break; 1259 1260 case IPOPT_TS: 1261 code = cp - (u_char *)ip; 1262 ipt = (struct ip_timestamp *)cp; 1263 if (ipt->ipt_len < 5) 1264 goto bad; 1265 if (ipt->ipt_ptr > 1266 ipt->ipt_len - (int)sizeof(int32_t)) { 1267 if (++ipt->ipt_oflw == 0) 1268 goto bad; 1269 break; 1270 } 1271 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1272 switch (ipt->ipt_flg) { 1273 1274 case IPOPT_TS_TSONLY: 1275 break; 1276 1277 case IPOPT_TS_TSANDADDR: 1278 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1279 sizeof(struct in_addr) > ipt->ipt_len) 1280 goto bad; 1281 ipaddr.sin_addr = dst; 1282 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1283 m->m_pkthdr.rcvif); 1284 if (ia == 0) 1285 continue; 1286 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1287 sizeof(struct in_addr)); 1288 ipt->ipt_ptr += sizeof(struct in_addr); 1289 break; 1290 1291 case IPOPT_TS_PRESPEC: 1292 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1293 sizeof(struct in_addr) > ipt->ipt_len) 1294 goto bad; 1295 (void)memcpy(&ipaddr.sin_addr, sin, 1296 sizeof(struct in_addr)); 1297 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1298 continue; 1299 ipt->ipt_ptr += sizeof(struct in_addr); 1300 break; 1301 1302 default: 1303 goto bad; 1304 } 1305 ntime = iptime(); 1306 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1307 sizeof(n_time)); 1308 ipt->ipt_ptr += sizeof(n_time); 1309 } 1310 } 1311 if (forward && ipforwarding) { 1312 ip_forward(m, 1); 1313 return (1); 1314 } 1315 return (0); 1316bad: 1317 icmp_error(m, type, code, 0, 0); 1318 ipstat.ips_badoptions++; 1319 return (1); 1320} 1321 1322/* 1323 * Given address of next destination (final or next hop), 1324 * return internet address info of interface to be used to get there. 1325 */ 1326static struct in_ifaddr * 1327ip_rtaddr(dst) 1328 struct in_addr dst; 1329{ 1330 register struct sockaddr_in *sin; 1331 1332 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1333 1334 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1335 if (ipforward_rt.ro_rt) { 1336 RTFREE(ipforward_rt.ro_rt); 1337 ipforward_rt.ro_rt = 0; 1338 } 1339 sin->sin_family = AF_INET; 1340 sin->sin_len = sizeof(*sin); 1341 sin->sin_addr = dst; 1342 1343 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1344 } 1345 if (ipforward_rt.ro_rt == 0) 1346 return ((struct in_ifaddr *)0); 1347 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1348} 1349 1350/* 1351 * Save incoming source route for use in replies, 1352 * to be picked up later by ip_srcroute if the receiver is interested. 1353 */ 1354void 1355save_rte(option, dst) 1356 u_char *option; 1357 struct in_addr dst; 1358{ 1359 unsigned olen; 1360 1361 olen = option[IPOPT_OLEN]; 1362#ifdef DIAGNOSTIC 1363 if (ipprintfs) 1364 printf("save_rte: olen %d\n", olen); 1365#endif 1366 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1367 return; 1368 bcopy(option, ip_srcrt.srcopt, olen); 1369 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1370 ip_srcrt.dst = dst; 1371} 1372 1373/* 1374 * Retrieve incoming source route for use in replies, 1375 * in the same form used by setsockopt. 1376 * The first hop is placed before the options, will be removed later. 1377 */ 1378struct mbuf * 1379ip_srcroute() 1380{ 1381 register struct in_addr *p, *q; 1382 register struct mbuf *m; 1383 1384 if (ip_nhops == 0) 1385 return ((struct mbuf *)0); 1386 m = m_get(M_DONTWAIT, MT_HEADER); 1387 if (m == 0) 1388 return ((struct mbuf *)0); 1389 1390#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1391 1392 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1393 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1394 OPTSIZ; 1395#ifdef DIAGNOSTIC 1396 if (ipprintfs) 1397 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1398#endif 1399 1400 /* 1401 * First save first hop for return route 1402 */ 1403 p = &ip_srcrt.route[ip_nhops - 1]; 1404 *(mtod(m, struct in_addr *)) = *p--; 1405#ifdef DIAGNOSTIC 1406 if (ipprintfs) 1407 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1408#endif 1409 1410 /* 1411 * Copy option fields and padding (nop) to mbuf. 1412 */ 1413 ip_srcrt.nop = IPOPT_NOP; 1414 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1415 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1416 &ip_srcrt.nop, OPTSIZ); 1417 q = (struct in_addr *)(mtod(m, caddr_t) + 1418 sizeof(struct in_addr) + OPTSIZ); 1419#undef OPTSIZ 1420 /* 1421 * Record return path as an IP source route, 1422 * reversing the path (pointers are now aligned). 1423 */ 1424 while (p >= ip_srcrt.route) { 1425#ifdef DIAGNOSTIC 1426 if (ipprintfs) 1427 printf(" %lx", (u_long)ntohl(q->s_addr)); 1428#endif 1429 *q++ = *p--; 1430 } 1431 /* 1432 * Last hop goes to final destination. 1433 */ 1434 *q = ip_srcrt.dst; 1435#ifdef DIAGNOSTIC 1436 if (ipprintfs) 1437 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1438#endif 1439 return (m); 1440} 1441 1442/* 1443 * Strip out IP options, at higher 1444 * level protocol in the kernel. 1445 * Second argument is buffer to which options 1446 * will be moved, and return value is their length. 1447 * XXX should be deleted; last arg currently ignored. 1448 */ 1449void 1450ip_stripoptions(m, mopt) 1451 register struct mbuf *m; 1452 struct mbuf *mopt; 1453{ 1454 register int i; 1455 struct ip *ip = mtod(m, struct ip *); 1456 register caddr_t opts; 1457 int olen; 1458 1459 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1460 opts = (caddr_t)(ip + 1); 1461 i = m->m_len - (sizeof (struct ip) + olen); 1462 bcopy(opts + olen, opts, (unsigned)i); 1463 m->m_len -= olen; 1464 if (m->m_flags & M_PKTHDR) 1465 m->m_pkthdr.len -= olen; 1466 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1467} 1468 1469u_char inetctlerrmap[PRC_NCMDS] = { 1470 0, 0, 0, 0, 1471 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1472 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1473 EMSGSIZE, EHOSTUNREACH, 0, 0, 1474 0, 0, 0, 0, 1475 ENOPROTOOPT, ENETRESET 1476}; 1477 1478/* 1479 * Forward a packet. If some error occurs return the sender 1480 * an icmp packet. Note we can't always generate a meaningful 1481 * icmp message because icmp doesn't have a large enough repertoire 1482 * of codes and types. 1483 * 1484 * If not forwarding, just drop the packet. This could be confusing 1485 * if ipforwarding was zero but some routing protocol was advancing 1486 * us as a gateway to somewhere. However, we must let the routing 1487 * protocol deal with that. 1488 * 1489 * The srcrt parameter indicates whether the packet is being forwarded 1490 * via a source route. 1491 */ 1492static void 1493ip_forward(m, srcrt) 1494 struct mbuf *m; 1495 int srcrt; 1496{ 1497 register struct ip *ip = mtod(m, struct ip *); 1498 register struct sockaddr_in *sin; 1499 register struct rtentry *rt; 1500 int error, type = 0, code = 0; 1501 struct mbuf *mcopy; 1502 n_long dest; 1503 struct ifnet *destifp; 1504#ifdef IPSEC 1505 struct ifnet dummyifp; 1506#endif 1507 1508 dest = 0; 1509#ifdef DIAGNOSTIC 1510 if (ipprintfs) 1511 printf("forward: src %lx dst %lx ttl %x\n", 1512 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1513 ip->ip_ttl); 1514#endif 1515 1516 1517 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1518 ipstat.ips_cantforward++; 1519 m_freem(m); 1520 return; 1521 } 1522#ifdef IPSTEALTH 1523 if (!ipstealth) { 1524#endif 1525 if (ip->ip_ttl <= IPTTLDEC) { 1526 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1527 dest, 0); 1528 return; 1529 } 1530#ifdef IPSTEALTH 1531 } 1532#endif 1533 1534 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1535 if ((rt = ipforward_rt.ro_rt) == 0 || 1536 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1537 if (ipforward_rt.ro_rt) { 1538 RTFREE(ipforward_rt.ro_rt); 1539 ipforward_rt.ro_rt = 0; 1540 } 1541 sin->sin_family = AF_INET; 1542 sin->sin_len = sizeof(*sin); 1543 sin->sin_addr = ip->ip_dst; 1544 1545 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1546 if (ipforward_rt.ro_rt == 0) { 1547 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1548 return; 1549 } 1550 rt = ipforward_rt.ro_rt; 1551 } 1552 1553 /* 1554 * Save at most 64 bytes of the packet in case 1555 * we need to generate an ICMP message to the src. 1556 */ 1557 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1558 if (mcopy && (mcopy->m_flags & M_EXT)) 1559 m_copydata(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t)); 1560 1561#ifdef IPSTEALTH 1562 if (!ipstealth) { 1563#endif 1564 ip->ip_ttl -= IPTTLDEC; 1565#ifdef IPSTEALTH 1566 } 1567#endif 1568 1569 /* 1570 * If forwarding packet using same interface that it came in on, 1571 * perhaps should send a redirect to sender to shortcut a hop. 1572 * Only send redirect if source is sending directly to us, 1573 * and if packet was not source routed (or has any options). 1574 * Also, don't send redirect if forwarding using a default route 1575 * or a route modified by a redirect. 1576 */ 1577#define satosin(sa) ((struct sockaddr_in *)(sa)) 1578 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1579 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1580 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1581 ipsendredirects && !srcrt) { 1582#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1583 u_long src = ntohl(ip->ip_src.s_addr); 1584 1585 if (RTA(rt) && 1586 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1587 if (rt->rt_flags & RTF_GATEWAY) 1588 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1589 else 1590 dest = ip->ip_dst.s_addr; 1591 /* Router requirements says to only send host redirects */ 1592 type = ICMP_REDIRECT; 1593 code = ICMP_REDIRECT_HOST; 1594#ifdef DIAGNOSTIC 1595 if (ipprintfs) 1596 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1597#endif 1598 } 1599 } 1600 1601 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1602 IP_FORWARDING, 0); 1603 if (error) 1604 ipstat.ips_cantforward++; 1605 else { 1606 ipstat.ips_forward++; 1607 if (type) 1608 ipstat.ips_redirectsent++; 1609 else { 1610 if (mcopy) { 1611 ipflow_create(&ipforward_rt, mcopy); 1612 m_freem(mcopy); 1613 } 1614 return; 1615 } 1616 } 1617 if (mcopy == NULL) 1618 return; 1619 destifp = NULL; 1620 1621 switch (error) { 1622 1623 case 0: /* forwarded, but need redirect */ 1624 /* type, code set above */ 1625 break; 1626 1627 case ENETUNREACH: /* shouldn't happen, checked above */ 1628 case EHOSTUNREACH: 1629 case ENETDOWN: 1630 case EHOSTDOWN: 1631 default: 1632 type = ICMP_UNREACH; 1633 code = ICMP_UNREACH_HOST; 1634 break; 1635 1636 case EMSGSIZE: 1637 type = ICMP_UNREACH; 1638 code = ICMP_UNREACH_NEEDFRAG; 1639#ifndef IPSEC 1640 if (ipforward_rt.ro_rt) 1641 destifp = ipforward_rt.ro_rt->rt_ifp; 1642#else 1643 /* 1644 * If the packet is routed over IPsec tunnel, tell the 1645 * originator the tunnel MTU. 1646 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1647 * XXX quickhack!!! 1648 */ 1649 if (ipforward_rt.ro_rt) { 1650 struct secpolicy *sp = NULL; 1651 int ipsecerror; 1652 int ipsechdr; 1653 struct route *ro; 1654 1655 sp = ipsec4_getpolicybyaddr(mcopy, 1656 IPSEC_DIR_OUTBOUND, 1657 IP_FORWARDING, 1658 &ipsecerror); 1659 1660 if (sp == NULL) 1661 destifp = ipforward_rt.ro_rt->rt_ifp; 1662 else { 1663 /* count IPsec header size */ 1664 ipsechdr = ipsec4_hdrsiz(mcopy, 1665 IPSEC_DIR_OUTBOUND, 1666 NULL); 1667 1668 /* 1669 * find the correct route for outer IPv4 1670 * header, compute tunnel MTU. 1671 * 1672 * XXX BUG ALERT 1673 * The "dummyifp" code relies upon the fact 1674 * that icmp_error() touches only ifp->if_mtu. 1675 */ 1676 /*XXX*/ 1677 destifp = NULL; 1678 if (sp->req != NULL 1679 && sp->req->sav != NULL 1680 && sp->req->sav->sah != NULL) { 1681 ro = &sp->req->sav->sah->sa_route; 1682 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1683 dummyifp.if_mtu = 1684 ro->ro_rt->rt_ifp->if_mtu; 1685 dummyifp.if_mtu -= ipsechdr; 1686 destifp = &dummyifp; 1687 } 1688 } 1689 1690 key_freesp(sp); 1691 } 1692 } 1693#endif /*IPSEC*/ 1694 ipstat.ips_cantfrag++; 1695 break; 1696 1697 case ENOBUFS: 1698 type = ICMP_SOURCEQUENCH; 1699 code = 0; 1700 break; 1701 1702 case EACCES: /* ipfw denied packet */ 1703 m_freem(mcopy); 1704 return; 1705 } 1706 if (mcopy->m_flags & M_EXT) 1707 m_copyback(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t)); 1708 icmp_error(mcopy, type, code, dest, destifp); 1709} 1710 1711void 1712ip_savecontrol(inp, mp, ip, m) 1713 register struct inpcb *inp; 1714 register struct mbuf **mp; 1715 register struct ip *ip; 1716 register struct mbuf *m; 1717{ 1718 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1719 struct timeval tv; 1720 1721 microtime(&tv); 1722 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1723 SCM_TIMESTAMP, SOL_SOCKET); 1724 if (*mp) 1725 mp = &(*mp)->m_next; 1726 } 1727 if (inp->inp_flags & INP_RECVDSTADDR) { 1728 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1729 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1730 if (*mp) 1731 mp = &(*mp)->m_next; 1732 } 1733#ifdef notyet 1734 /* XXX 1735 * Moving these out of udp_input() made them even more broken 1736 * than they already were. 1737 */ 1738 /* options were tossed already */ 1739 if (inp->inp_flags & INP_RECVOPTS) { 1740 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1741 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1742 if (*mp) 1743 mp = &(*mp)->m_next; 1744 } 1745 /* ip_srcroute doesn't do what we want here, need to fix */ 1746 if (inp->inp_flags & INP_RECVRETOPTS) { 1747 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1748 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1749 if (*mp) 1750 mp = &(*mp)->m_next; 1751 } 1752#endif 1753 if (inp->inp_flags & INP_RECVIF) { 1754 struct ifnet *ifp; 1755 struct sdlbuf { 1756 struct sockaddr_dl sdl; 1757 u_char pad[32]; 1758 } sdlbuf; 1759 struct sockaddr_dl *sdp; 1760 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1761 1762 if (((ifp = m->m_pkthdr.rcvif)) 1763 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1764 sdp = (struct sockaddr_dl *)(ifnet_addrs 1765 [ifp->if_index - 1]->ifa_addr); 1766 /* 1767 * Change our mind and don't try copy. 1768 */ 1769 if ((sdp->sdl_family != AF_LINK) 1770 || (sdp->sdl_len > sizeof(sdlbuf))) { 1771 goto makedummy; 1772 } 1773 bcopy(sdp, sdl2, sdp->sdl_len); 1774 } else { 1775makedummy: 1776 sdl2->sdl_len 1777 = offsetof(struct sockaddr_dl, sdl_data[0]); 1778 sdl2->sdl_family = AF_LINK; 1779 sdl2->sdl_index = 0; 1780 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1781 } 1782 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1783 IP_RECVIF, IPPROTO_IP); 1784 if (*mp) 1785 mp = &(*mp)->m_next; 1786 } 1787} 1788 1789int 1790ip_rsvp_init(struct socket *so) 1791{ 1792 if (so->so_type != SOCK_RAW || 1793 so->so_proto->pr_protocol != IPPROTO_RSVP) 1794 return EOPNOTSUPP; 1795 1796 if (ip_rsvpd != NULL) 1797 return EADDRINUSE; 1798 1799 ip_rsvpd = so; 1800 /* 1801 * This may seem silly, but we need to be sure we don't over-increment 1802 * the RSVP counter, in case something slips up. 1803 */ 1804 if (!ip_rsvp_on) { 1805 ip_rsvp_on = 1; 1806 rsvp_on++; 1807 } 1808 1809 return 0; 1810} 1811 1812int 1813ip_rsvp_done(void) 1814{ 1815 ip_rsvpd = NULL; 1816 /* 1817 * This may seem silly, but we need to be sure we don't over-decrement 1818 * the RSVP counter, in case something slips up. 1819 */ 1820 if (ip_rsvp_on) { 1821 ip_rsvp_on = 0; 1822 rsvp_on--; 1823 } 1824 return 0; 1825} 1826