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