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