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