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