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