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