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