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