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