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