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