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