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.65 1997/09/15 23:07:01 ache 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 42 #include <stddef.h> 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/domain.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/errno.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_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/in_var.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 78 int rsvp_on = 0; 79 static int ip_rsvp_on; 80 struct socket *ip_rsvpd; 81 82 static int ipforwarding = 0; 83 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 84 &ipforwarding, 0, ""); 85 86 static int ipsendredirects = 1; /* XXX */ 87 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 88 &ipsendredirects, 0, ""); 89 90 int ip_defttl = IPDEFTTL; 91 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 92 &ip_defttl, 0, ""); 93 94 static int ip_dosourceroute = 0; 95 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 96 &ip_dosourceroute, 0, ""); 97 #ifdef DIAGNOSTIC 98 static int ipprintfs = 0; 99 #endif 100 101 extern struct domain inetdomain; 102 extern struct protosw inetsw[]; 103 u_char ip_protox[IPPROTO_MAX]; 104 static int ipqmaxlen = IFQ_MAXLEN; 105 struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 106 struct ifqueue ipintrq; 107 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD, 108 &ipintrq.ifq_maxlen, 0, ""); 109 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 110 &ipintrq.ifq_drops, 0, ""); 111 112 struct ipstat ipstat; 113 SYSCTL_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 123 static struct ipq ipq[IPREASS_NHASH]; 124 static int nipq = 0; /* total # of reass queues */ 125 static int maxnipq; 126 127 #ifdef IPCTL_DEFMTU 128 SYSCTL_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 */ 141 ip_fw_chk_t *ip_fw_chk_ptr; 142 ip_fw_ctl_t *ip_fw_ctl_ptr; 143 144 /* IP Network Address Translation (NAT) hooks */ 145 ip_nat_t *ip_nat_ptr; 146 ip_nat_ctl_t *ip_nat_ctl_ptr; 147 #endif 148 149 #if defined(IPFILTER_LKM) || defined(IPFILTER) 150 int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **)); 151 int (*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 */ 162 static int ip_nhops = 0; 163 static 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 */ 176 static u_short frag_divert_port; 177 #endif 178 179 static void save_rte __P((u_char *, struct in_addr)); 180 static void ip_deq __P((struct ipasfrag *)); 181 static int ip_dooptions __P((struct mbuf *)); 182 static void ip_enq __P((struct ipasfrag *, struct ipasfrag *)); 183 static void ip_forward __P((struct mbuf *, int)); 184 static void ip_freef __P((struct ipq *)); 185 static struct ip * 186 ip_reass __P((struct ipasfrag *, struct ipq *, struct ipq *)); 187 static struct in_ifaddr * 188 ip_rtaddr __P((struct in_addr)); 189 static 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 */ 194 void 195 ip_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 228 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 229 static 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 */ 235 void 236 ip_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) { 318 tooshort: 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 476 ours: 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 } 526 found: 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 #endif 582 583 /* 584 * Switch out to protocol's input routine. 585 */ 586 ipstat.ips_delivered++; 587 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 588 return; 589 bad: 590 m_freem(m); 591 } 592 593 /* 594 * IP software interrupt routine - to go away sometime soon 595 */ 596 static void 597 ipintr(void) 598 { 599 int s; 600 struct mbuf *m; 601 602 while(1) { 603 s = splimp(); 604 IF_DEQUEUE(&ipintrq, m); 605 splx(s); 606 if (m == 0) 607 return; 608 ip_input(m); 609 } 610 } 611 612 NETISR_SET(NETISR_IP, ipintr); 613 614 /* 615 * Take incoming datagram fragment and try to 616 * reassemble it into whole datagram. If a chain for 617 * reassembly of this datagram already exists, then it 618 * is given as fp; otherwise have to make a chain. 619 */ 620 static struct ip * 621 ip_reass(ip, fp, where) 622 register struct ipasfrag *ip; 623 register struct ipq *fp; 624 struct ipq *where; 625 { 626 register struct mbuf *m = dtom(ip); 627 register struct ipasfrag *q; 628 struct mbuf *t; 629 int hlen = ip->ip_hl << 2; 630 int i, next; 631 632 /* 633 * Presence of header sizes in mbufs 634 * would confuse code below. 635 */ 636 m->m_data += hlen; 637 m->m_len -= hlen; 638 639 /* 640 * If first fragment to arrive, create a reassembly queue. 641 */ 642 if (fp == 0) { 643 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 644 goto dropfrag; 645 fp = mtod(t, struct ipq *); 646 insque(fp, where); 647 nipq++; 648 fp->ipq_ttl = IPFRAGTTL; 649 fp->ipq_p = ip->ip_p; 650 fp->ipq_id = ip->ip_id; 651 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 652 fp->ipq_src = ((struct ip *)ip)->ip_src; 653 fp->ipq_dst = ((struct ip *)ip)->ip_dst; 654 #ifdef IPDIVERT 655 fp->ipq_divert = 0; 656 #endif 657 q = (struct ipasfrag *)fp; 658 goto insert; 659 } 660 661 /* 662 * Find a segment which begins after this one does. 663 */ 664 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 665 if (q->ip_off > ip->ip_off) 666 break; 667 668 /* 669 * If there is a preceding segment, it may provide some of 670 * our data already. If so, drop the data from the incoming 671 * segment. If it provides all of our data, drop us. 672 */ 673 if (q->ipf_prev != (struct ipasfrag *)fp) { 674 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 675 if (i > 0) { 676 if (i >= ip->ip_len) 677 goto dropfrag; 678 m_adj(dtom(ip), i); 679 ip->ip_off += i; 680 ip->ip_len -= i; 681 } 682 } 683 684 /* 685 * While we overlap succeeding segments trim them or, 686 * if they are completely covered, dequeue them. 687 */ 688 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 689 struct mbuf *m0; 690 691 i = (ip->ip_off + ip->ip_len) - q->ip_off; 692 if (i < q->ip_len) { 693 q->ip_len -= i; 694 q->ip_off += i; 695 m_adj(dtom(q), i); 696 break; 697 } 698 m0 = dtom(q); 699 q = q->ipf_next; 700 ip_deq(q->ipf_prev); 701 m_freem(m0); 702 } 703 704 insert: 705 706 #ifdef IPDIVERT 707 /* 708 * Any fragment diverting causes the whole packet to divert 709 */ 710 if (frag_divert_port != 0) 711 fp->ipq_divert = frag_divert_port; 712 frag_divert_port = 0; 713 #endif 714 715 /* 716 * Stick new segment in its place; 717 * check for complete reassembly. 718 */ 719 ip_enq(ip, q->ipf_prev); 720 next = 0; 721 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 722 if (q->ip_off != next) 723 return (0); 724 next += q->ip_len; 725 } 726 if (q->ipf_prev->ipf_mff & 1) 727 return (0); 728 729 /* 730 * Reassembly is complete. Make sure the packet is a sane size. 731 */ 732 if (next + (IP_VHL_HL(((struct ip *)fp->ipq_next)->ip_vhl) << 2) 733 > IP_MAXPACKET) { 734 ipstat.ips_toolong++; 735 ip_freef(fp); 736 return (0); 737 } 738 739 /* 740 * Concatenate fragments. 741 */ 742 q = fp->ipq_next; 743 m = dtom(q); 744 t = m->m_next; 745 m->m_next = 0; 746 m_cat(m, t); 747 q = q->ipf_next; 748 while (q != (struct ipasfrag *)fp) { 749 t = dtom(q); 750 q = q->ipf_next; 751 m_cat(m, t); 752 } 753 754 #ifdef IPDIVERT 755 /* 756 * Record divert port for packet, if any 757 */ 758 frag_divert_port = fp->ipq_divert; 759 #endif 760 761 /* 762 * Create header for new ip packet by 763 * modifying header of first packet; 764 * dequeue and discard fragment reassembly header. 765 * Make header visible. 766 */ 767 ip = fp->ipq_next; 768 ip->ip_len = next; 769 ip->ipf_mff &= ~1; 770 ((struct ip *)ip)->ip_src = fp->ipq_src; 771 ((struct ip *)ip)->ip_dst = fp->ipq_dst; 772 remque(fp); 773 nipq--; 774 (void) m_free(dtom(fp)); 775 m = dtom(ip); 776 m->m_len += (ip->ip_hl << 2); 777 m->m_data -= (ip->ip_hl << 2); 778 /* some debugging cruft by sklower, below, will go away soon */ 779 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 780 register int plen = 0; 781 for (t = m; m; m = m->m_next) 782 plen += m->m_len; 783 t->m_pkthdr.len = plen; 784 } 785 return ((struct ip *)ip); 786 787 dropfrag: 788 ipstat.ips_fragdropped++; 789 m_freem(m); 790 return (0); 791 } 792 793 /* 794 * Free a fragment reassembly header and all 795 * associated datagrams. 796 */ 797 static void 798 ip_freef(fp) 799 struct ipq *fp; 800 { 801 register struct ipasfrag *q, *p; 802 803 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 804 p = q->ipf_next; 805 ip_deq(q); 806 m_freem(dtom(q)); 807 } 808 remque(fp); 809 (void) m_free(dtom(fp)); 810 nipq--; 811 } 812 813 /* 814 * Put an ip fragment on a reassembly chain. 815 * Like insque, but pointers in middle of structure. 816 */ 817 static void 818 ip_enq(p, prev) 819 register struct ipasfrag *p, *prev; 820 { 821 822 p->ipf_prev = prev; 823 p->ipf_next = prev->ipf_next; 824 prev->ipf_next->ipf_prev = p; 825 prev->ipf_next = p; 826 } 827 828 /* 829 * To ip_enq as remque is to insque. 830 */ 831 static void 832 ip_deq(p) 833 register struct ipasfrag *p; 834 { 835 836 p->ipf_prev->ipf_next = p->ipf_next; 837 p->ipf_next->ipf_prev = p->ipf_prev; 838 } 839 840 /* 841 * IP timer processing; 842 * if a timer expires on a reassembly 843 * queue, discard it. 844 */ 845 void 846 ip_slowtimo() 847 { 848 register struct ipq *fp; 849 int s = splnet(); 850 int i; 851 852 for (i = 0; i < IPREASS_NHASH; i++) { 853 fp = ipq[i].next; 854 if (fp == 0) 855 continue; 856 while (fp != &ipq[i]) { 857 --fp->ipq_ttl; 858 fp = fp->next; 859 if (fp->prev->ipq_ttl == 0) { 860 ipstat.ips_fragtimeout++; 861 ip_freef(fp->prev); 862 } 863 } 864 } 865 splx(s); 866 } 867 868 /* 869 * Drain off all datagram fragments. 870 */ 871 void 872 ip_drain() 873 { 874 int i; 875 876 for (i = 0; i < IPREASS_NHASH; i++) { 877 while (ipq[i].next != &ipq[i]) { 878 ipstat.ips_fragdropped++; 879 ip_freef(ipq[i].next); 880 } 881 } 882 in_rtqdrain(); 883 } 884 885 /* 886 * Do option processing on a datagram, 887 * possibly discarding it if bad options are encountered, 888 * or forwarding it if source-routed. 889 * Returns 1 if packet has been forwarded/freed, 890 * 0 if the packet should be processed further. 891 */ 892 static int 893 ip_dooptions(m) 894 struct mbuf *m; 895 { 896 register struct ip *ip = mtod(m, struct ip *); 897 register u_char *cp; 898 register struct ip_timestamp *ipt; 899 register struct in_ifaddr *ia; 900 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 901 struct in_addr *sin, dst; 902 n_time ntime; 903 904 dst = ip->ip_dst; 905 cp = (u_char *)(ip + 1); 906 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 907 for (; cnt > 0; cnt -= optlen, cp += optlen) { 908 opt = cp[IPOPT_OPTVAL]; 909 if (opt == IPOPT_EOL) 910 break; 911 if (opt == IPOPT_NOP) 912 optlen = 1; 913 else { 914 optlen = cp[IPOPT_OLEN]; 915 if (optlen <= 0 || optlen > cnt) { 916 code = &cp[IPOPT_OLEN] - (u_char *)ip; 917 goto bad; 918 } 919 } 920 switch (opt) { 921 922 default: 923 break; 924 925 /* 926 * Source routing with record. 927 * Find interface with current destination address. 928 * If none on this machine then drop if strictly routed, 929 * or do nothing if loosely routed. 930 * Record interface address and bring up next address 931 * component. If strictly routed make sure next 932 * address is on directly accessible net. 933 */ 934 case IPOPT_LSRR: 935 case IPOPT_SSRR: 936 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 937 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 938 goto bad; 939 } 940 ipaddr.sin_addr = ip->ip_dst; 941 ia = (struct in_ifaddr *) 942 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 943 if (ia == 0) { 944 if (opt == IPOPT_SSRR) { 945 type = ICMP_UNREACH; 946 code = ICMP_UNREACH_SRCFAIL; 947 goto bad; 948 } 949 /* 950 * Loose routing, and not at next destination 951 * yet; nothing to do except forward. 952 */ 953 break; 954 } 955 off--; /* 0 origin */ 956 if (off > optlen - sizeof(struct in_addr)) { 957 /* 958 * End of source route. Should be for us. 959 */ 960 save_rte(cp, ip->ip_src); 961 break; 962 } 963 964 if (!ip_dosourceroute) { 965 char buf[4*sizeof "123"]; 966 strcpy(buf, inet_ntoa(ip->ip_dst)); 967 968 log(LOG_WARNING, 969 "attempted source route from %s to %s\n", 970 inet_ntoa(ip->ip_src), buf); 971 type = ICMP_UNREACH; 972 code = ICMP_UNREACH_SRCFAIL; 973 goto bad; 974 } 975 976 /* 977 * locate outgoing interface 978 */ 979 (void)memcpy(&ipaddr.sin_addr, cp + off, 980 sizeof(ipaddr.sin_addr)); 981 982 if (opt == IPOPT_SSRR) { 983 #define INA struct in_ifaddr * 984 #define SA struct sockaddr * 985 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 986 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 987 } else 988 ia = ip_rtaddr(ipaddr.sin_addr); 989 if (ia == 0) { 990 type = ICMP_UNREACH; 991 code = ICMP_UNREACH_SRCFAIL; 992 goto bad; 993 } 994 ip->ip_dst = ipaddr.sin_addr; 995 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 996 sizeof(struct in_addr)); 997 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 998 /* 999 * Let ip_intr's mcast routing check handle mcast pkts 1000 */ 1001 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1002 break; 1003 1004 case IPOPT_RR: 1005 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1006 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1007 goto bad; 1008 } 1009 /* 1010 * If no space remains, ignore. 1011 */ 1012 off--; /* 0 origin */ 1013 if (off > optlen - sizeof(struct in_addr)) 1014 break; 1015 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1016 sizeof(ipaddr.sin_addr)); 1017 /* 1018 * locate outgoing interface; if we're the destination, 1019 * use the incoming interface (should be same). 1020 */ 1021 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1022 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1023 type = ICMP_UNREACH; 1024 code = ICMP_UNREACH_HOST; 1025 goto bad; 1026 } 1027 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1028 sizeof(struct in_addr)); 1029 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1030 break; 1031 1032 case IPOPT_TS: 1033 code = cp - (u_char *)ip; 1034 ipt = (struct ip_timestamp *)cp; 1035 if (ipt->ipt_len < 5) 1036 goto bad; 1037 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 1038 if (++ipt->ipt_oflw == 0) 1039 goto bad; 1040 break; 1041 } 1042 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1043 switch (ipt->ipt_flg) { 1044 1045 case IPOPT_TS_TSONLY: 1046 break; 1047 1048 case IPOPT_TS_TSANDADDR: 1049 if (ipt->ipt_ptr + sizeof(n_time) + 1050 sizeof(struct in_addr) > ipt->ipt_len) 1051 goto bad; 1052 ipaddr.sin_addr = dst; 1053 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1054 m->m_pkthdr.rcvif); 1055 if (ia == 0) 1056 continue; 1057 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1058 sizeof(struct in_addr)); 1059 ipt->ipt_ptr += sizeof(struct in_addr); 1060 break; 1061 1062 case IPOPT_TS_PRESPEC: 1063 if (ipt->ipt_ptr + sizeof(n_time) + 1064 sizeof(struct in_addr) > ipt->ipt_len) 1065 goto bad; 1066 (void)memcpy(&ipaddr.sin_addr, sin, 1067 sizeof(struct in_addr)); 1068 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1069 continue; 1070 ipt->ipt_ptr += sizeof(struct in_addr); 1071 break; 1072 1073 default: 1074 goto bad; 1075 } 1076 ntime = iptime(); 1077 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1078 sizeof(n_time)); 1079 ipt->ipt_ptr += sizeof(n_time); 1080 } 1081 } 1082 if (forward) { 1083 ip_forward(m, 1); 1084 return (1); 1085 } 1086 return (0); 1087 bad: 1088 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ 1089 icmp_error(m, type, code, 0, 0); 1090 ipstat.ips_badoptions++; 1091 return (1); 1092 } 1093 1094 /* 1095 * Given address of next destination (final or next hop), 1096 * return internet address info of interface to be used to get there. 1097 */ 1098 static struct in_ifaddr * 1099 ip_rtaddr(dst) 1100 struct in_addr dst; 1101 { 1102 register struct sockaddr_in *sin; 1103 1104 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1105 1106 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1107 if (ipforward_rt.ro_rt) { 1108 RTFREE(ipforward_rt.ro_rt); 1109 ipforward_rt.ro_rt = 0; 1110 } 1111 sin->sin_family = AF_INET; 1112 sin->sin_len = sizeof(*sin); 1113 sin->sin_addr = dst; 1114 1115 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1116 } 1117 if (ipforward_rt.ro_rt == 0) 1118 return ((struct in_ifaddr *)0); 1119 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1120 } 1121 1122 /* 1123 * Save incoming source route for use in replies, 1124 * to be picked up later by ip_srcroute if the receiver is interested. 1125 */ 1126 void 1127 save_rte(option, dst) 1128 u_char *option; 1129 struct in_addr dst; 1130 { 1131 unsigned olen; 1132 1133 olen = option[IPOPT_OLEN]; 1134 #ifdef DIAGNOSTIC 1135 if (ipprintfs) 1136 printf("save_rte: olen %d\n", olen); 1137 #endif 1138 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1139 return; 1140 bcopy(option, ip_srcrt.srcopt, olen); 1141 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1142 ip_srcrt.dst = dst; 1143 } 1144 1145 /* 1146 * Retrieve incoming source route for use in replies, 1147 * in the same form used by setsockopt. 1148 * The first hop is placed before the options, will be removed later. 1149 */ 1150 struct mbuf * 1151 ip_srcroute() 1152 { 1153 register struct in_addr *p, *q; 1154 register struct mbuf *m; 1155 1156 if (ip_nhops == 0) 1157 return ((struct mbuf *)0); 1158 m = m_get(M_DONTWAIT, MT_SOOPTS); 1159 if (m == 0) 1160 return ((struct mbuf *)0); 1161 1162 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1163 1164 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1165 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1166 OPTSIZ; 1167 #ifdef DIAGNOSTIC 1168 if (ipprintfs) 1169 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1170 #endif 1171 1172 /* 1173 * First save first hop for return route 1174 */ 1175 p = &ip_srcrt.route[ip_nhops - 1]; 1176 *(mtod(m, struct in_addr *)) = *p--; 1177 #ifdef DIAGNOSTIC 1178 if (ipprintfs) 1179 printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 1180 #endif 1181 1182 /* 1183 * Copy option fields and padding (nop) to mbuf. 1184 */ 1185 ip_srcrt.nop = IPOPT_NOP; 1186 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1187 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1188 &ip_srcrt.nop, OPTSIZ); 1189 q = (struct in_addr *)(mtod(m, caddr_t) + 1190 sizeof(struct in_addr) + OPTSIZ); 1191 #undef OPTSIZ 1192 /* 1193 * Record return path as an IP source route, 1194 * reversing the path (pointers are now aligned). 1195 */ 1196 while (p >= ip_srcrt.route) { 1197 #ifdef DIAGNOSTIC 1198 if (ipprintfs) 1199 printf(" %lx", ntohl(q->s_addr)); 1200 #endif 1201 *q++ = *p--; 1202 } 1203 /* 1204 * Last hop goes to final destination. 1205 */ 1206 *q = ip_srcrt.dst; 1207 #ifdef DIAGNOSTIC 1208 if (ipprintfs) 1209 printf(" %lx\n", ntohl(q->s_addr)); 1210 #endif 1211 return (m); 1212 } 1213 1214 /* 1215 * Strip out IP options, at higher 1216 * level protocol in the kernel. 1217 * Second argument is buffer to which options 1218 * will be moved, and return value is their length. 1219 * XXX should be deleted; last arg currently ignored. 1220 */ 1221 void 1222 ip_stripoptions(m, mopt) 1223 register struct mbuf *m; 1224 struct mbuf *mopt; 1225 { 1226 register int i; 1227 struct ip *ip = mtod(m, struct ip *); 1228 register caddr_t opts; 1229 int olen; 1230 1231 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1232 opts = (caddr_t)(ip + 1); 1233 i = m->m_len - (sizeof (struct ip) + olen); 1234 bcopy(opts + olen, opts, (unsigned)i); 1235 m->m_len -= olen; 1236 if (m->m_flags & M_PKTHDR) 1237 m->m_pkthdr.len -= olen; 1238 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1239 } 1240 1241 u_char inetctlerrmap[PRC_NCMDS] = { 1242 0, 0, 0, 0, 1243 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1244 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1245 EMSGSIZE, EHOSTUNREACH, 0, 0, 1246 0, 0, 0, 0, 1247 ENOPROTOOPT 1248 }; 1249 1250 /* 1251 * Forward a packet. If some error occurs return the sender 1252 * an icmp packet. Note we can't always generate a meaningful 1253 * icmp message because icmp doesn't have a large enough repertoire 1254 * of codes and types. 1255 * 1256 * If not forwarding, just drop the packet. This could be confusing 1257 * if ipforwarding was zero but some routing protocol was advancing 1258 * us as a gateway to somewhere. However, we must let the routing 1259 * protocol deal with that. 1260 * 1261 * The srcrt parameter indicates whether the packet is being forwarded 1262 * via a source route. 1263 */ 1264 static void 1265 ip_forward(m, srcrt) 1266 struct mbuf *m; 1267 int srcrt; 1268 { 1269 register struct ip *ip = mtod(m, struct ip *); 1270 register struct sockaddr_in *sin; 1271 register struct rtentry *rt; 1272 int error, type = 0, code = 0; 1273 struct mbuf *mcopy; 1274 n_long dest; 1275 struct ifnet *destifp; 1276 1277 dest = 0; 1278 #ifdef DIAGNOSTIC 1279 if (ipprintfs) 1280 printf("forward: src %lx dst %lx ttl %x\n", 1281 ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl); 1282 #endif 1283 1284 1285 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1286 ipstat.ips_cantforward++; 1287 m_freem(m); 1288 return; 1289 } 1290 HTONS(ip->ip_id); 1291 if (ip->ip_ttl <= IPTTLDEC) { 1292 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1293 return; 1294 } 1295 ip->ip_ttl -= IPTTLDEC; 1296 1297 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1298 if ((rt = ipforward_rt.ro_rt) == 0 || 1299 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1300 if (ipforward_rt.ro_rt) { 1301 RTFREE(ipforward_rt.ro_rt); 1302 ipforward_rt.ro_rt = 0; 1303 } 1304 sin->sin_family = AF_INET; 1305 sin->sin_len = sizeof(*sin); 1306 sin->sin_addr = ip->ip_dst; 1307 1308 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1309 if (ipforward_rt.ro_rt == 0) { 1310 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1311 return; 1312 } 1313 rt = ipforward_rt.ro_rt; 1314 } 1315 1316 /* 1317 * Save at most 64 bytes of the packet in case 1318 * we need to generate an ICMP message to the src. 1319 */ 1320 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1321 1322 /* 1323 * If forwarding packet using same interface that it came in on, 1324 * perhaps should send a redirect to sender to shortcut a hop. 1325 * Only send redirect if source is sending directly to us, 1326 * and if packet was not source routed (or has any options). 1327 * Also, don't send redirect if forwarding using a default route 1328 * or a route modified by a redirect. 1329 */ 1330 #define satosin(sa) ((struct sockaddr_in *)(sa)) 1331 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1332 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1333 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1334 ipsendredirects && !srcrt) { 1335 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1336 u_long src = ntohl(ip->ip_src.s_addr); 1337 1338 if (RTA(rt) && 1339 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1340 if (rt->rt_flags & RTF_GATEWAY) 1341 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1342 else 1343 dest = ip->ip_dst.s_addr; 1344 /* Router requirements says to only send host redirects */ 1345 type = ICMP_REDIRECT; 1346 code = ICMP_REDIRECT_HOST; 1347 #ifdef DIAGNOSTIC 1348 if (ipprintfs) 1349 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1350 #endif 1351 } 1352 } 1353 1354 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1355 IP_FORWARDING, 0); 1356 if (error) 1357 ipstat.ips_cantforward++; 1358 else { 1359 ipstat.ips_forward++; 1360 if (type) 1361 ipstat.ips_redirectsent++; 1362 else { 1363 if (mcopy) 1364 m_freem(mcopy); 1365 return; 1366 } 1367 } 1368 if (mcopy == NULL) 1369 return; 1370 destifp = NULL; 1371 1372 switch (error) { 1373 1374 case 0: /* forwarded, but need redirect */ 1375 /* type, code set above */ 1376 break; 1377 1378 case ENETUNREACH: /* shouldn't happen, checked above */ 1379 case EHOSTUNREACH: 1380 case ENETDOWN: 1381 case EHOSTDOWN: 1382 default: 1383 type = ICMP_UNREACH; 1384 code = ICMP_UNREACH_HOST; 1385 break; 1386 1387 case EMSGSIZE: 1388 type = ICMP_UNREACH; 1389 code = ICMP_UNREACH_NEEDFRAG; 1390 if (ipforward_rt.ro_rt) 1391 destifp = ipforward_rt.ro_rt->rt_ifp; 1392 ipstat.ips_cantfrag++; 1393 break; 1394 1395 case ENOBUFS: 1396 type = ICMP_SOURCEQUENCH; 1397 code = 0; 1398 break; 1399 } 1400 icmp_error(mcopy, type, code, dest, destifp); 1401 } 1402 1403 void 1404 ip_savecontrol(inp, mp, ip, m) 1405 register struct inpcb *inp; 1406 register struct mbuf **mp; 1407 register struct ip *ip; 1408 register struct mbuf *m; 1409 { 1410 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1411 struct timeval tv; 1412 1413 microtime(&tv); 1414 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1415 SCM_TIMESTAMP, SOL_SOCKET); 1416 if (*mp) 1417 mp = &(*mp)->m_next; 1418 } 1419 if (inp->inp_flags & INP_RECVDSTADDR) { 1420 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1421 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1422 if (*mp) 1423 mp = &(*mp)->m_next; 1424 } 1425 #ifdef notyet 1426 /* XXX 1427 * Moving these out of udp_input() made them even more broken 1428 * than they already were. 1429 */ 1430 /* options were tossed already */ 1431 if (inp->inp_flags & INP_RECVOPTS) { 1432 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1433 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1434 if (*mp) 1435 mp = &(*mp)->m_next; 1436 } 1437 /* ip_srcroute doesn't do what we want here, need to fix */ 1438 if (inp->inp_flags & INP_RECVRETOPTS) { 1439 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1440 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1441 if (*mp) 1442 mp = &(*mp)->m_next; 1443 } 1444 #endif 1445 if (inp->inp_flags & INP_RECVIF) { 1446 struct sockaddr_dl sdl; 1447 1448 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1449 sdl.sdl_family = AF_LINK; 1450 sdl.sdl_index = m->m_pkthdr.rcvif ? 1451 m->m_pkthdr.rcvif->if_index : 0; 1452 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1453 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1454 IP_RECVIF, IPPROTO_IP); 1455 if (*mp) 1456 mp = &(*mp)->m_next; 1457 } 1458 } 1459 1460 int 1461 ip_rsvp_init(struct socket *so) 1462 { 1463 if (so->so_type != SOCK_RAW || 1464 so->so_proto->pr_protocol != IPPROTO_RSVP) 1465 return EOPNOTSUPP; 1466 1467 if (ip_rsvpd != NULL) 1468 return EADDRINUSE; 1469 1470 ip_rsvpd = so; 1471 /* 1472 * This may seem silly, but we need to be sure we don't over-increment 1473 * the RSVP counter, in case something slips up. 1474 */ 1475 if (!ip_rsvp_on) { 1476 ip_rsvp_on = 1; 1477 rsvp_on++; 1478 } 1479 1480 return 0; 1481 } 1482 1483 int 1484 ip_rsvp_done(void) 1485 { 1486 ip_rsvpd = NULL; 1487 /* 1488 * This may seem silly, but we need to be sure we don't over-decrement 1489 * the RSVP counter, in case something slips up. 1490 */ 1491 if (ip_rsvp_on) { 1492 ip_rsvp_on = 0; 1493 rsvp_on--; 1494 } 1495 return 0; 1496 } 1497