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