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