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