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