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