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