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