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