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