ip_input.c revision 73402
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 73402 2001-03-04 03:22:36Z 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 */ 519 if ((ntohl(ip->ip_dst.s_addr) & IN_CLASSA_NET) == 520 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT) && 521 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 522 m_freem(m); 523#ifdef IPFIREWALL_FORWARD 524 ip_fw_fwd_addr = NULL; 525#endif 526 return; 527 } 528 529 /* 530 * Enable a consistency check between the destination address 531 * and the arrival interface for a unicast packet (the RFC 1122 532 * strong ES model) if IP forwarding is disabled and the packet 533 * is not locally generated and the packet is not subject to 534 * 'ipfw fwd'. 535 */ 536 checkif = ip_checkinterface && (ipforwarding == 0) && 537 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 538 (ip_fw_fwd_addr == NULL); 539 540 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 541#define satosin(sa) ((struct sockaddr_in *)(sa)) 542 543#ifdef BOOTP_COMPAT 544 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 545 goto ours; 546#endif 547 /* 548 * If the address matches, verify that the packet 549 * arrived via the correct interface if checking is 550 * enabled. 551 */ 552 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && 553 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 554 goto ours; 555 /* 556 * Only accept broadcast packets that arrive via the 557 * matching interface. Reception of forwarded directed 558 * broadcasts would be handled via ip_forward() and 559 * ether_output() with the loopback into the stack for 560 * SIMPLEX interfaces handled by ether_output(). 561 */ 562 if (ia->ia_ifp == m->m_pkthdr.rcvif && 563 ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 564 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 565 pkt_dst.s_addr) 566 goto ours; 567 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr) 568 goto ours; 569 } 570 } 571 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 572 struct in_multi *inm; 573 if (ip_mrouter) { 574 /* 575 * If we are acting as a multicast router, all 576 * incoming multicast packets are passed to the 577 * kernel-level multicast forwarding function. 578 * The packet is returned (relatively) intact; if 579 * ip_mforward() returns a non-zero value, the packet 580 * must be discarded, else it may be accepted below. 581 */ 582 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 583 ipstat.ips_cantforward++; 584 m_freem(m); 585 return; 586 } 587 588 /* 589 * The process-level routing demon needs to receive 590 * all multicast IGMP packets, whether or not this 591 * host belongs to their destination groups. 592 */ 593 if (ip->ip_p == IPPROTO_IGMP) 594 goto ours; 595 ipstat.ips_forward++; 596 } 597 /* 598 * See if we belong to the destination multicast group on the 599 * arrival interface. 600 */ 601 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 602 if (inm == NULL) { 603 ipstat.ips_notmember++; 604 m_freem(m); 605 return; 606 } 607 goto ours; 608 } 609 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 610 goto ours; 611 if (ip->ip_dst.s_addr == INADDR_ANY) 612 goto ours; 613 614#if defined(NFAITH) && 0 < NFAITH 615 /* 616 * FAITH(Firewall Aided Internet Translator) 617 */ 618 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 619 if (ip_keepfaith) { 620 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 621 goto ours; 622 } 623 m_freem(m); 624 return; 625 } 626#endif 627 /* 628 * Not for us; forward if possible and desirable. 629 */ 630 if (ipforwarding == 0) { 631 ipstat.ips_cantforward++; 632 m_freem(m); 633 } else 634 ip_forward(m, 0); 635#ifdef IPFIREWALL_FORWARD 636 ip_fw_fwd_addr = NULL; 637#endif 638 return; 639 640ours: 641 /* Count the packet in the ip address stats */ 642 if (ia != NULL) { 643 ia->ia_ifa.if_ipackets++; 644 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 645 } 646 647 /* 648 * If offset or IP_MF are set, must reassemble. 649 * Otherwise, nothing need be done. 650 * (We could look in the reassembly queue to see 651 * if the packet was previously fragmented, 652 * but it's not worth the time; just let them time out.) 653 */ 654 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 655 656 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 657 /* 658 * Look for queue of fragments 659 * of this datagram. 660 */ 661 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) 662 if (ip->ip_id == fp->ipq_id && 663 ip->ip_src.s_addr == fp->ipq_src.s_addr && 664 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 665 ip->ip_p == fp->ipq_p) 666 goto found; 667 668 fp = 0; 669 670 /* check if there's a place for the new queue */ 671 if (nipq > maxnipq) { 672 /* 673 * drop something from the tail of the current queue 674 * before proceeding further 675 */ 676 if (ipq[sum].prev == &ipq[sum]) { /* gak */ 677 for (i = 0; i < IPREASS_NHASH; i++) { 678 if (ipq[i].prev != &ipq[i]) { 679 ip_freef(ipq[i].prev); 680 break; 681 } 682 } 683 } else 684 ip_freef(ipq[sum].prev); 685 } 686found: 687 /* 688 * Adjust ip_len to not reflect header, 689 * convert offset of this to bytes. 690 */ 691 ip->ip_len -= hlen; 692 if (ip->ip_off & IP_MF) { 693 /* 694 * Make sure that fragments have a data length 695 * that's a non-zero multiple of 8 bytes. 696 */ 697 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 698 ipstat.ips_toosmall++; /* XXX */ 699 goto bad; 700 } 701 m->m_flags |= M_FRAG; 702 } 703 ip->ip_off <<= 3; 704 705 /* 706 * Attempt reassembly; if it succeeds, proceed. 707 */ 708 ipstat.ips_fragments++; 709 m->m_pkthdr.header = ip; 710#ifdef IPDIVERT 711 m = ip_reass(m, 712 fp, &ipq[sum], &divert_info, &divert_cookie); 713#else 714 m = ip_reass(m, fp, &ipq[sum]); 715#endif 716 if (m == 0) { 717#ifdef IPFIREWALL_FORWARD 718 ip_fw_fwd_addr = NULL; 719#endif 720 return; 721 } 722 ipstat.ips_reassembled++; 723 ip = mtod(m, struct ip *); 724 /* Get the header length of the reassembled packet */ 725 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 726#ifdef IPDIVERT 727 /* Restore original checksum before diverting packet */ 728 if (divert_info != 0) { 729 ip->ip_len += hlen; 730 HTONS(ip->ip_len); 731 HTONS(ip->ip_off); 732 ip->ip_sum = 0; 733 if (hlen == sizeof(struct ip)) 734 ip->ip_sum = in_cksum_hdr(ip); 735 else 736 ip->ip_sum = in_cksum(m, hlen); 737 NTOHS(ip->ip_off); 738 NTOHS(ip->ip_len); 739 ip->ip_len -= hlen; 740 } 741#endif 742 } else 743 ip->ip_len -= hlen; 744 745#ifdef IPDIVERT 746 /* 747 * Divert or tee packet to the divert protocol if required. 748 * 749 * If divert_info is zero then cookie should be too, so we shouldn't 750 * need to clear them here. Assume divert_packet() does so also. 751 */ 752 if (divert_info != 0) { 753 struct mbuf *clone = NULL; 754 755 /* Clone packet if we're doing a 'tee' */ 756 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0) 757 clone = m_dup(m, M_DONTWAIT); 758 759 /* Restore packet header fields to original values */ 760 ip->ip_len += hlen; 761 HTONS(ip->ip_len); 762 HTONS(ip->ip_off); 763 764 /* Deliver packet to divert input routine */ 765 ip_divert_cookie = divert_cookie; 766 divert_packet(m, 1, divert_info & 0xffff); 767 ipstat.ips_delivered++; 768 769 /* If 'tee', continue with original packet */ 770 if (clone == NULL) 771 return; 772 m = clone; 773 ip = mtod(m, struct ip *); 774 } 775#endif 776 777 /* 778 * Switch out to protocol's input routine. 779 */ 780 ipstat.ips_delivered++; 781 { 782 int off = hlen, nh = ip->ip_p; 783 784 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off, nh); 785#ifdef IPFIREWALL_FORWARD 786 ip_fw_fwd_addr = NULL; /* tcp needed it */ 787#endif 788 return; 789 } 790bad: 791#ifdef IPFIREWALL_FORWARD 792 ip_fw_fwd_addr = NULL; 793#endif 794 m_freem(m); 795} 796 797/* 798 * IP software interrupt routine - to go away sometime soon 799 */ 800static void 801ipintr(void) 802{ 803 struct mbuf *m; 804 805 while (1) { 806 IF_DEQUEUE(&ipintrq, m); 807 if (m == 0) 808 return; 809 ip_input(m); 810 } 811} 812 813/* 814 * Take incoming datagram fragment and try to reassemble it into 815 * whole datagram. If a chain for reassembly of this datagram already 816 * exists, then it is given as fp; otherwise have to make a chain. 817 * 818 * When IPDIVERT enabled, keep additional state with each packet that 819 * tells us if we need to divert or tee the packet we're building. 820 */ 821 822static struct mbuf * 823#ifdef IPDIVERT 824ip_reass(m, fp, where, divinfo, divcookie) 825#else 826ip_reass(m, fp, where) 827#endif 828 register struct mbuf *m; 829 register struct ipq *fp; 830 struct ipq *where; 831#ifdef IPDIVERT 832 u_int32_t *divinfo; 833 u_int16_t *divcookie; 834#endif 835{ 836 struct ip *ip = mtod(m, struct ip *); 837 register struct mbuf *p, *q, *nq; 838 struct mbuf *t; 839 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 840 int i, next; 841 842 /* 843 * Presence of header sizes in mbufs 844 * would confuse code below. 845 */ 846 m->m_data += hlen; 847 m->m_len -= hlen; 848 849 /* 850 * If first fragment to arrive, create a reassembly queue. 851 */ 852 if (fp == 0) { 853 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 854 goto dropfrag; 855 fp = mtod(t, struct ipq *); 856 insque(fp, where); 857 nipq++; 858 fp->ipq_ttl = IPFRAGTTL; 859 fp->ipq_p = ip->ip_p; 860 fp->ipq_id = ip->ip_id; 861 fp->ipq_src = ip->ip_src; 862 fp->ipq_dst = ip->ip_dst; 863 fp->ipq_frags = m; 864 m->m_nextpkt = NULL; 865#ifdef IPDIVERT 866 fp->ipq_div_info = 0; 867 fp->ipq_div_cookie = 0; 868#endif 869 goto inserted; 870 } 871 872#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 873 874 /* 875 * Find a segment which begins after this one does. 876 */ 877 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 878 if (GETIP(q)->ip_off > ip->ip_off) 879 break; 880 881 /* 882 * If there is a preceding segment, it may provide some of 883 * our data already. If so, drop the data from the incoming 884 * segment. If it provides all of our data, drop us, otherwise 885 * stick new segment in the proper place. 886 * 887 * If some of the data is dropped from the the preceding 888 * segment, then it's checksum is invalidated. 889 */ 890 if (p) { 891 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 892 if (i > 0) { 893 if (i >= ip->ip_len) 894 goto dropfrag; 895 m_adj(m, i); 896 m->m_pkthdr.csum_flags = 0; 897 ip->ip_off += i; 898 ip->ip_len -= i; 899 } 900 m->m_nextpkt = p->m_nextpkt; 901 p->m_nextpkt = m; 902 } else { 903 m->m_nextpkt = fp->ipq_frags; 904 fp->ipq_frags = m; 905 } 906 907 /* 908 * While we overlap succeeding segments trim them or, 909 * if they are completely covered, dequeue them. 910 */ 911 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 912 q = nq) { 913 i = (ip->ip_off + ip->ip_len) - 914 GETIP(q)->ip_off; 915 if (i < GETIP(q)->ip_len) { 916 GETIP(q)->ip_len -= i; 917 GETIP(q)->ip_off += i; 918 m_adj(q, i); 919 q->m_pkthdr.csum_flags = 0; 920 break; 921 } 922 nq = q->m_nextpkt; 923 m->m_nextpkt = nq; 924 m_freem(q); 925 } 926 927inserted: 928 929#ifdef IPDIVERT 930 /* 931 * Transfer firewall instructions to the fragment structure. 932 * Any fragment diverting causes the whole packet to divert. 933 */ 934 fp->ipq_div_info = *divinfo; 935 fp->ipq_div_cookie = *divcookie; 936 *divinfo = 0; 937 *divcookie = 0; 938#endif 939 940 /* 941 * Check for complete reassembly. 942 */ 943 next = 0; 944 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 945 if (GETIP(q)->ip_off != next) 946 return (0); 947 next += GETIP(q)->ip_len; 948 } 949 /* Make sure the last packet didn't have the IP_MF flag */ 950 if (p->m_flags & M_FRAG) 951 return (0); 952 953 /* 954 * Reassembly is complete. Make sure the packet is a sane size. 955 */ 956 q = fp->ipq_frags; 957 ip = GETIP(q); 958 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 959 ipstat.ips_toolong++; 960 ip_freef(fp); 961 return (0); 962 } 963 964 /* 965 * Concatenate fragments. 966 */ 967 m = q; 968 t = m->m_next; 969 m->m_next = 0; 970 m_cat(m, t); 971 nq = q->m_nextpkt; 972 q->m_nextpkt = 0; 973 for (q = nq; q != NULL; q = nq) { 974 nq = q->m_nextpkt; 975 q->m_nextpkt = NULL; 976 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 977 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 978 m_cat(m, q); 979 } 980 981#ifdef IPDIVERT 982 /* 983 * Extract firewall instructions from the fragment structure. 984 */ 985 *divinfo = fp->ipq_div_info; 986 *divcookie = fp->ipq_div_cookie; 987#endif 988 989 /* 990 * Create header for new ip packet by 991 * modifying header of first packet; 992 * dequeue and discard fragment reassembly header. 993 * Make header visible. 994 */ 995 ip->ip_len = next; 996 ip->ip_src = fp->ipq_src; 997 ip->ip_dst = fp->ipq_dst; 998 remque(fp); 999 nipq--; 1000 (void) m_free(dtom(fp)); 1001 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 1002 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 1003 /* some debugging cruft by sklower, below, will go away soon */ 1004 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1005 register int plen = 0; 1006 for (t = m; t; t = t->m_next) 1007 plen += t->m_len; 1008 m->m_pkthdr.len = plen; 1009 } 1010 return (m); 1011 1012dropfrag: 1013#ifdef IPDIVERT 1014 *divinfo = 0; 1015 *divcookie = 0; 1016#endif 1017 ipstat.ips_fragdropped++; 1018 m_freem(m); 1019 return (0); 1020 1021#undef GETIP 1022} 1023 1024/* 1025 * Free a fragment reassembly header and all 1026 * associated datagrams. 1027 */ 1028static void 1029ip_freef(fp) 1030 struct ipq *fp; 1031{ 1032 register struct mbuf *q; 1033 1034 while (fp->ipq_frags) { 1035 q = fp->ipq_frags; 1036 fp->ipq_frags = q->m_nextpkt; 1037 m_freem(q); 1038 } 1039 remque(fp); 1040 (void) m_free(dtom(fp)); 1041 nipq--; 1042} 1043 1044/* 1045 * IP timer processing; 1046 * if a timer expires on a reassembly 1047 * queue, discard it. 1048 */ 1049void 1050ip_slowtimo() 1051{ 1052 register struct ipq *fp; 1053 int s = splnet(); 1054 int i; 1055 1056 for (i = 0; i < IPREASS_NHASH; i++) { 1057 fp = ipq[i].next; 1058 if (fp == 0) 1059 continue; 1060 while (fp != &ipq[i]) { 1061 --fp->ipq_ttl; 1062 fp = fp->next; 1063 if (fp->prev->ipq_ttl == 0) { 1064 ipstat.ips_fragtimeout++; 1065 ip_freef(fp->prev); 1066 } 1067 } 1068 } 1069 ipflow_slowtimo(); 1070 splx(s); 1071} 1072 1073/* 1074 * Drain off all datagram fragments. 1075 */ 1076void 1077ip_drain() 1078{ 1079 int i; 1080 1081 for (i = 0; i < IPREASS_NHASH; i++) { 1082 while (ipq[i].next != &ipq[i]) { 1083 ipstat.ips_fragdropped++; 1084 ip_freef(ipq[i].next); 1085 } 1086 } 1087 in_rtqdrain(); 1088} 1089 1090/* 1091 * Do option processing on a datagram, 1092 * possibly discarding it if bad options are encountered, 1093 * or forwarding it if source-routed. 1094 * Returns 1 if packet has been forwarded/freed, 1095 * 0 if the packet should be processed further. 1096 */ 1097static int 1098ip_dooptions(m) 1099 struct mbuf *m; 1100{ 1101 register struct ip *ip = mtod(m, struct ip *); 1102 register u_char *cp; 1103 register struct ip_timestamp *ipt; 1104 register struct in_ifaddr *ia; 1105 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1106 struct in_addr *sin, dst; 1107 n_time ntime; 1108 1109 dst = ip->ip_dst; 1110 cp = (u_char *)(ip + 1); 1111 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1112 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1113 opt = cp[IPOPT_OPTVAL]; 1114 if (opt == IPOPT_EOL) 1115 break; 1116 if (opt == IPOPT_NOP) 1117 optlen = 1; 1118 else { 1119 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1120 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1121 goto bad; 1122 } 1123 optlen = cp[IPOPT_OLEN]; 1124 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1125 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1126 goto bad; 1127 } 1128 } 1129 switch (opt) { 1130 1131 default: 1132 break; 1133 1134 /* 1135 * Source routing with record. 1136 * Find interface with current destination address. 1137 * If none on this machine then drop if strictly routed, 1138 * or do nothing if loosely routed. 1139 * Record interface address and bring up next address 1140 * component. If strictly routed make sure next 1141 * address is on directly accessible net. 1142 */ 1143 case IPOPT_LSRR: 1144 case IPOPT_SSRR: 1145 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1146 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1147 goto bad; 1148 } 1149 ipaddr.sin_addr = ip->ip_dst; 1150 ia = (struct in_ifaddr *) 1151 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1152 if (ia == 0) { 1153 if (opt == IPOPT_SSRR) { 1154 type = ICMP_UNREACH; 1155 code = ICMP_UNREACH_SRCFAIL; 1156 goto bad; 1157 } 1158 if (!ip_dosourceroute) 1159 goto nosourcerouting; 1160 /* 1161 * Loose routing, and not at next destination 1162 * yet; nothing to do except forward. 1163 */ 1164 break; 1165 } 1166 off--; /* 0 origin */ 1167 if (off > optlen - (int)sizeof(struct in_addr)) { 1168 /* 1169 * End of source route. Should be for us. 1170 */ 1171 if (!ip_acceptsourceroute) 1172 goto nosourcerouting; 1173 save_rte(cp, ip->ip_src); 1174 break; 1175 } 1176 1177 if (!ip_dosourceroute) { 1178 if (ipforwarding) { 1179 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1180 /* 1181 * Acting as a router, so generate ICMP 1182 */ 1183nosourcerouting: 1184 strcpy(buf, inet_ntoa(ip->ip_dst)); 1185 log(LOG_WARNING, 1186 "attempted source route from %s to %s\n", 1187 inet_ntoa(ip->ip_src), buf); 1188 type = ICMP_UNREACH; 1189 code = ICMP_UNREACH_SRCFAIL; 1190 goto bad; 1191 } else { 1192 /* 1193 * Not acting as a router, so silently drop. 1194 */ 1195 ipstat.ips_cantforward++; 1196 m_freem(m); 1197 return (1); 1198 } 1199 } 1200 1201 /* 1202 * locate outgoing interface 1203 */ 1204 (void)memcpy(&ipaddr.sin_addr, cp + off, 1205 sizeof(ipaddr.sin_addr)); 1206 1207 if (opt == IPOPT_SSRR) { 1208#define INA struct in_ifaddr * 1209#define SA struct sockaddr * 1210 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1211 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1212 } else 1213 ia = ip_rtaddr(ipaddr.sin_addr); 1214 if (ia == 0) { 1215 type = ICMP_UNREACH; 1216 code = ICMP_UNREACH_SRCFAIL; 1217 goto bad; 1218 } 1219 ip->ip_dst = ipaddr.sin_addr; 1220 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1221 sizeof(struct in_addr)); 1222 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1223 /* 1224 * Let ip_intr's mcast routing check handle mcast pkts 1225 */ 1226 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1227 break; 1228 1229 case IPOPT_RR: 1230 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1231 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1232 goto bad; 1233 } 1234 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1235 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1236 goto bad; 1237 } 1238 /* 1239 * If no space remains, ignore. 1240 */ 1241 off--; /* 0 origin */ 1242 if (off > optlen - (int)sizeof(struct in_addr)) 1243 break; 1244 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1245 sizeof(ipaddr.sin_addr)); 1246 /* 1247 * locate outgoing interface; if we're the destination, 1248 * use the incoming interface (should be same). 1249 */ 1250 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1251 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1252 type = ICMP_UNREACH; 1253 code = ICMP_UNREACH_HOST; 1254 goto bad; 1255 } 1256 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1257 sizeof(struct in_addr)); 1258 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1259 break; 1260 1261 case IPOPT_TS: 1262 code = cp - (u_char *)ip; 1263 ipt = (struct ip_timestamp *)cp; 1264 if (ipt->ipt_len < 5) 1265 goto bad; 1266 if (ipt->ipt_ptr > 1267 ipt->ipt_len - (int)sizeof(int32_t)) { 1268 if (++ipt->ipt_oflw == 0) 1269 goto bad; 1270 break; 1271 } 1272 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1273 switch (ipt->ipt_flg) { 1274 1275 case IPOPT_TS_TSONLY: 1276 break; 1277 1278 case IPOPT_TS_TSANDADDR: 1279 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1280 sizeof(struct in_addr) > ipt->ipt_len) 1281 goto bad; 1282 ipaddr.sin_addr = dst; 1283 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1284 m->m_pkthdr.rcvif); 1285 if (ia == 0) 1286 continue; 1287 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1288 sizeof(struct in_addr)); 1289 ipt->ipt_ptr += sizeof(struct in_addr); 1290 break; 1291 1292 case IPOPT_TS_PRESPEC: 1293 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1294 sizeof(struct in_addr) > ipt->ipt_len) 1295 goto bad; 1296 (void)memcpy(&ipaddr.sin_addr, sin, 1297 sizeof(struct in_addr)); 1298 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1299 continue; 1300 ipt->ipt_ptr += sizeof(struct in_addr); 1301 break; 1302 1303 default: 1304 goto bad; 1305 } 1306 ntime = iptime(); 1307 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1308 sizeof(n_time)); 1309 ipt->ipt_ptr += sizeof(n_time); 1310 } 1311 } 1312 if (forward && ipforwarding) { 1313 ip_forward(m, 1); 1314 return (1); 1315 } 1316 return (0); 1317bad: 1318 icmp_error(m, type, code, 0, 0); 1319 ipstat.ips_badoptions++; 1320 return (1); 1321} 1322 1323/* 1324 * Given address of next destination (final or next hop), 1325 * return internet address info of interface to be used to get there. 1326 */ 1327static struct in_ifaddr * 1328ip_rtaddr(dst) 1329 struct in_addr dst; 1330{ 1331 register struct sockaddr_in *sin; 1332 1333 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1334 1335 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1336 if (ipforward_rt.ro_rt) { 1337 RTFREE(ipforward_rt.ro_rt); 1338 ipforward_rt.ro_rt = 0; 1339 } 1340 sin->sin_family = AF_INET; 1341 sin->sin_len = sizeof(*sin); 1342 sin->sin_addr = dst; 1343 1344 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1345 } 1346 if (ipforward_rt.ro_rt == 0) 1347 return ((struct in_ifaddr *)0); 1348 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1349} 1350 1351/* 1352 * Save incoming source route for use in replies, 1353 * to be picked up later by ip_srcroute if the receiver is interested. 1354 */ 1355void 1356save_rte(option, dst) 1357 u_char *option; 1358 struct in_addr dst; 1359{ 1360 unsigned olen; 1361 1362 olen = option[IPOPT_OLEN]; 1363#ifdef DIAGNOSTIC 1364 if (ipprintfs) 1365 printf("save_rte: olen %d\n", olen); 1366#endif 1367 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1368 return; 1369 bcopy(option, ip_srcrt.srcopt, olen); 1370 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1371 ip_srcrt.dst = dst; 1372} 1373 1374/* 1375 * Retrieve incoming source route for use in replies, 1376 * in the same form used by setsockopt. 1377 * The first hop is placed before the options, will be removed later. 1378 */ 1379struct mbuf * 1380ip_srcroute() 1381{ 1382 register struct in_addr *p, *q; 1383 register struct mbuf *m; 1384 1385 if (ip_nhops == 0) 1386 return ((struct mbuf *)0); 1387 m = m_get(M_DONTWAIT, MT_HEADER); 1388 if (m == 0) 1389 return ((struct mbuf *)0); 1390 1391#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1392 1393 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1394 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1395 OPTSIZ; 1396#ifdef DIAGNOSTIC 1397 if (ipprintfs) 1398 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1399#endif 1400 1401 /* 1402 * First save first hop for return route 1403 */ 1404 p = &ip_srcrt.route[ip_nhops - 1]; 1405 *(mtod(m, struct in_addr *)) = *p--; 1406#ifdef DIAGNOSTIC 1407 if (ipprintfs) 1408 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1409#endif 1410 1411 /* 1412 * Copy option fields and padding (nop) to mbuf. 1413 */ 1414 ip_srcrt.nop = IPOPT_NOP; 1415 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1416 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1417 &ip_srcrt.nop, OPTSIZ); 1418 q = (struct in_addr *)(mtod(m, caddr_t) + 1419 sizeof(struct in_addr) + OPTSIZ); 1420#undef OPTSIZ 1421 /* 1422 * Record return path as an IP source route, 1423 * reversing the path (pointers are now aligned). 1424 */ 1425 while (p >= ip_srcrt.route) { 1426#ifdef DIAGNOSTIC 1427 if (ipprintfs) 1428 printf(" %lx", (u_long)ntohl(q->s_addr)); 1429#endif 1430 *q++ = *p--; 1431 } 1432 /* 1433 * Last hop goes to final destination. 1434 */ 1435 *q = ip_srcrt.dst; 1436#ifdef DIAGNOSTIC 1437 if (ipprintfs) 1438 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1439#endif 1440 return (m); 1441} 1442 1443/* 1444 * Strip out IP options, at higher 1445 * level protocol in the kernel. 1446 * Second argument is buffer to which options 1447 * will be moved, and return value is their length. 1448 * XXX should be deleted; last arg currently ignored. 1449 */ 1450void 1451ip_stripoptions(m, mopt) 1452 register struct mbuf *m; 1453 struct mbuf *mopt; 1454{ 1455 register int i; 1456 struct ip *ip = mtod(m, struct ip *); 1457 register caddr_t opts; 1458 int olen; 1459 1460 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1461 opts = (caddr_t)(ip + 1); 1462 i = m->m_len - (sizeof (struct ip) + olen); 1463 bcopy(opts + olen, opts, (unsigned)i); 1464 m->m_len -= olen; 1465 if (m->m_flags & M_PKTHDR) 1466 m->m_pkthdr.len -= olen; 1467 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1468} 1469 1470u_char inetctlerrmap[PRC_NCMDS] = { 1471 0, 0, 0, 0, 1472 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1473 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1474 EMSGSIZE, EHOSTUNREACH, 0, 0, 1475 0, 0, 0, 0, 1476 ENOPROTOOPT, ENETRESET 1477}; 1478 1479/* 1480 * Forward a packet. If some error occurs return the sender 1481 * an icmp packet. Note we can't always generate a meaningful 1482 * icmp message because icmp doesn't have a large enough repertoire 1483 * of codes and types. 1484 * 1485 * If not forwarding, just drop the packet. This could be confusing 1486 * if ipforwarding was zero but some routing protocol was advancing 1487 * us as a gateway to somewhere. However, we must let the routing 1488 * protocol deal with that. 1489 * 1490 * The srcrt parameter indicates whether the packet is being forwarded 1491 * via a source route. 1492 */ 1493static void 1494ip_forward(m, srcrt) 1495 struct mbuf *m; 1496 int srcrt; 1497{ 1498 register struct ip *ip = mtod(m, struct ip *); 1499 register struct sockaddr_in *sin; 1500 register struct rtentry *rt; 1501 int error, type = 0, code = 0; 1502 struct mbuf *mcopy; 1503 n_long dest; 1504 struct ifnet *destifp; 1505#ifdef IPSEC 1506 struct ifnet dummyifp; 1507#endif 1508 1509 dest = 0; 1510#ifdef DIAGNOSTIC 1511 if (ipprintfs) 1512 printf("forward: src %lx dst %lx ttl %x\n", 1513 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1514 ip->ip_ttl); 1515#endif 1516 1517 1518 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1519 ipstat.ips_cantforward++; 1520 m_freem(m); 1521 return; 1522 } 1523#ifdef IPSTEALTH 1524 if (!ipstealth) { 1525#endif 1526 if (ip->ip_ttl <= IPTTLDEC) { 1527 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1528 dest, 0); 1529 return; 1530 } 1531#ifdef IPSTEALTH 1532 } 1533#endif 1534 1535 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1536 if ((rt = ipforward_rt.ro_rt) == 0 || 1537 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1538 if (ipforward_rt.ro_rt) { 1539 RTFREE(ipforward_rt.ro_rt); 1540 ipforward_rt.ro_rt = 0; 1541 } 1542 sin->sin_family = AF_INET; 1543 sin->sin_len = sizeof(*sin); 1544 sin->sin_addr = ip->ip_dst; 1545 1546 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1547 if (ipforward_rt.ro_rt == 0) { 1548 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1549 return; 1550 } 1551 rt = ipforward_rt.ro_rt; 1552 } 1553 1554 /* 1555 * Save at most 64 bytes of the packet in case 1556 * we need to generate an ICMP message to the src. 1557 */ 1558 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1559 if (mcopy && (mcopy->m_flags & M_EXT)) 1560 m_copydata(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t)); 1561 1562#ifdef IPSTEALTH 1563 if (!ipstealth) { 1564#endif 1565 ip->ip_ttl -= IPTTLDEC; 1566#ifdef IPSTEALTH 1567 } 1568#endif 1569 1570 /* 1571 * If forwarding packet using same interface that it came in on, 1572 * perhaps should send a redirect to sender to shortcut a hop. 1573 * Only send redirect if source is sending directly to us, 1574 * and if packet was not source routed (or has any options). 1575 * Also, don't send redirect if forwarding using a default route 1576 * or a route modified by a redirect. 1577 */ 1578#define satosin(sa) ((struct sockaddr_in *)(sa)) 1579 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1580 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1581 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1582 ipsendredirects && !srcrt) { 1583#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1584 u_long src = ntohl(ip->ip_src.s_addr); 1585 1586 if (RTA(rt) && 1587 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1588 if (rt->rt_flags & RTF_GATEWAY) 1589 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1590 else 1591 dest = ip->ip_dst.s_addr; 1592 /* Router requirements says to only send host redirects */ 1593 type = ICMP_REDIRECT; 1594 code = ICMP_REDIRECT_HOST; 1595#ifdef DIAGNOSTIC 1596 if (ipprintfs) 1597 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1598#endif 1599 } 1600 } 1601 1602 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1603 IP_FORWARDING, 0); 1604 if (error) 1605 ipstat.ips_cantforward++; 1606 else { 1607 ipstat.ips_forward++; 1608 if (type) 1609 ipstat.ips_redirectsent++; 1610 else { 1611 if (mcopy) { 1612 ipflow_create(&ipforward_rt, mcopy); 1613 m_freem(mcopy); 1614 } 1615 return; 1616 } 1617 } 1618 if (mcopy == NULL) 1619 return; 1620 destifp = NULL; 1621 1622 switch (error) { 1623 1624 case 0: /* forwarded, but need redirect */ 1625 /* type, code set above */ 1626 break; 1627 1628 case ENETUNREACH: /* shouldn't happen, checked above */ 1629 case EHOSTUNREACH: 1630 case ENETDOWN: 1631 case EHOSTDOWN: 1632 default: 1633 type = ICMP_UNREACH; 1634 code = ICMP_UNREACH_HOST; 1635 break; 1636 1637 case EMSGSIZE: 1638 type = ICMP_UNREACH; 1639 code = ICMP_UNREACH_NEEDFRAG; 1640#ifndef IPSEC 1641 if (ipforward_rt.ro_rt) 1642 destifp = ipforward_rt.ro_rt->rt_ifp; 1643#else 1644 /* 1645 * If the packet is routed over IPsec tunnel, tell the 1646 * originator the tunnel MTU. 1647 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1648 * XXX quickhack!!! 1649 */ 1650 if (ipforward_rt.ro_rt) { 1651 struct secpolicy *sp = NULL; 1652 int ipsecerror; 1653 int ipsechdr; 1654 struct route *ro; 1655 1656 sp = ipsec4_getpolicybyaddr(mcopy, 1657 IPSEC_DIR_OUTBOUND, 1658 IP_FORWARDING, 1659 &ipsecerror); 1660 1661 if (sp == NULL) 1662 destifp = ipforward_rt.ro_rt->rt_ifp; 1663 else { 1664 /* count IPsec header size */ 1665 ipsechdr = ipsec4_hdrsiz(mcopy, 1666 IPSEC_DIR_OUTBOUND, 1667 NULL); 1668 1669 /* 1670 * find the correct route for outer IPv4 1671 * header, compute tunnel MTU. 1672 * 1673 * XXX BUG ALERT 1674 * The "dummyifp" code relies upon the fact 1675 * that icmp_error() touches only ifp->if_mtu. 1676 */ 1677 /*XXX*/ 1678 destifp = NULL; 1679 if (sp->req != NULL 1680 && sp->req->sav != NULL 1681 && sp->req->sav->sah != NULL) { 1682 ro = &sp->req->sav->sah->sa_route; 1683 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1684 dummyifp.if_mtu = 1685 ro->ro_rt->rt_ifp->if_mtu; 1686 dummyifp.if_mtu -= ipsechdr; 1687 destifp = &dummyifp; 1688 } 1689 } 1690 1691 key_freesp(sp); 1692 } 1693 } 1694#endif /*IPSEC*/ 1695 ipstat.ips_cantfrag++; 1696 break; 1697 1698 case ENOBUFS: 1699 type = ICMP_SOURCEQUENCH; 1700 code = 0; 1701 break; 1702 1703 case EACCES: /* ipfw denied packet */ 1704 m_freem(mcopy); 1705 return; 1706 } 1707 if (mcopy->m_flags & M_EXT) 1708 m_copyback(mcopy, 0, sizeof(struct ip), mtod(mcopy, caddr_t)); 1709 icmp_error(mcopy, type, code, dest, destifp); 1710} 1711 1712void 1713ip_savecontrol(inp, mp, ip, m) 1714 register struct inpcb *inp; 1715 register struct mbuf **mp; 1716 register struct ip *ip; 1717 register struct mbuf *m; 1718{ 1719 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1720 struct timeval tv; 1721 1722 microtime(&tv); 1723 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1724 SCM_TIMESTAMP, SOL_SOCKET); 1725 if (*mp) 1726 mp = &(*mp)->m_next; 1727 } 1728 if (inp->inp_flags & INP_RECVDSTADDR) { 1729 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1730 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1731 if (*mp) 1732 mp = &(*mp)->m_next; 1733 } 1734#ifdef notyet 1735 /* XXX 1736 * Moving these out of udp_input() made them even more broken 1737 * than they already were. 1738 */ 1739 /* options were tossed already */ 1740 if (inp->inp_flags & INP_RECVOPTS) { 1741 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1742 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1743 if (*mp) 1744 mp = &(*mp)->m_next; 1745 } 1746 /* ip_srcroute doesn't do what we want here, need to fix */ 1747 if (inp->inp_flags & INP_RECVRETOPTS) { 1748 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1749 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1750 if (*mp) 1751 mp = &(*mp)->m_next; 1752 } 1753#endif 1754 if (inp->inp_flags & INP_RECVIF) { 1755 struct ifnet *ifp; 1756 struct sdlbuf { 1757 struct sockaddr_dl sdl; 1758 u_char pad[32]; 1759 } sdlbuf; 1760 struct sockaddr_dl *sdp; 1761 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1762 1763 if (((ifp = m->m_pkthdr.rcvif)) 1764 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1765 sdp = (struct sockaddr_dl *)(ifnet_addrs 1766 [ifp->if_index - 1]->ifa_addr); 1767 /* 1768 * Change our mind and don't try copy. 1769 */ 1770 if ((sdp->sdl_family != AF_LINK) 1771 || (sdp->sdl_len > sizeof(sdlbuf))) { 1772 goto makedummy; 1773 } 1774 bcopy(sdp, sdl2, sdp->sdl_len); 1775 } else { 1776makedummy: 1777 sdl2->sdl_len 1778 = offsetof(struct sockaddr_dl, sdl_data[0]); 1779 sdl2->sdl_family = AF_LINK; 1780 sdl2->sdl_index = 0; 1781 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1782 } 1783 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1784 IP_RECVIF, IPPROTO_IP); 1785 if (*mp) 1786 mp = &(*mp)->m_next; 1787 } 1788} 1789 1790int 1791ip_rsvp_init(struct socket *so) 1792{ 1793 if (so->so_type != SOCK_RAW || 1794 so->so_proto->pr_protocol != IPPROTO_RSVP) 1795 return EOPNOTSUPP; 1796 1797 if (ip_rsvpd != NULL) 1798 return EADDRINUSE; 1799 1800 ip_rsvpd = so; 1801 /* 1802 * This may seem silly, but we need to be sure we don't over-increment 1803 * the RSVP counter, in case something slips up. 1804 */ 1805 if (!ip_rsvp_on) { 1806 ip_rsvp_on = 1; 1807 rsvp_on++; 1808 } 1809 1810 return 0; 1811} 1812 1813int 1814ip_rsvp_done(void) 1815{ 1816 ip_rsvpd = NULL; 1817 /* 1818 * This may seem silly, but we need to be sure we don't over-decrement 1819 * the RSVP counter, in case something slips up. 1820 */ 1821 if (ip_rsvp_on) { 1822 ip_rsvp_on = 0; 1823 rsvp_on--; 1824 } 1825 return 0; 1826} 1827