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