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