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