ip_input.c revision 1.50
1/* $NetBSD: ip_input.c,v 1.50 1997/06/24 02:26:04 thorpej 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 /* 377 * Look for queue of fragments 378 * of this datagram. 379 */ 380 for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next) 381 if (ip->ip_id == fp->ipq_id && 382 in_hosteq(ip->ip_src, fp->ipq_src) && 383 in_hosteq(ip->ip_dst, fp->ipq_dst) && 384 ip->ip_p == fp->ipq_p) 385 goto found; 386 fp = 0; 387found: 388 389 /* 390 * Adjust ip_len to not reflect header, 391 * set ipqe_mff if more fragments are expected, 392 * convert offset of this to bytes. 393 */ 394 ip->ip_len -= hlen; 395 mff = (ip->ip_off & IP_MF) != 0; 396 if (mff) { 397 /* 398 * Make sure that fragments have a data length 399 * that's a non-zero multiple of 8 bytes. 400 */ 401 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 402 ipstat.ips_badfrags++; 403 goto bad; 404 } 405 } 406 ip->ip_off <<= 3; 407 408 /* 409 * If datagram marked as having more fragments 410 * or if this is not the first fragment, 411 * attempt reassembly; if it succeeds, proceed. 412 */ 413 if (mff || ip->ip_off) { 414 ipstat.ips_fragments++; 415 MALLOC(ipqe, struct ipqent *, sizeof (struct ipqent), 416 M_IPQ, M_NOWAIT); 417 if (ipqe == NULL) { 418 ipstat.ips_rcvmemdrop++; 419 goto bad; 420 } 421 ipqe->ipqe_mff = mff; 422 ipqe->ipqe_m = m; 423 ipqe->ipqe_ip = ip; 424 m = ip_reass(ipqe, fp); 425 if (m == 0) 426 goto next; 427 ipstat.ips_reassembled++; 428 ip = mtod(m, struct ip *); 429 } else 430 if (fp) 431 ip_freef(fp); 432 } else 433 ip->ip_len -= hlen; 434 435 /* 436 * Switch out to protocol's input routine. 437 */ 438 ipstat.ips_delivered++; 439 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 440 goto next; 441bad: 442 m_freem(m); 443 goto next; 444} 445 446/* 447 * Take incoming datagram fragment and try to 448 * reassemble it into whole datagram. If a chain for 449 * reassembly of this datagram already exists, then it 450 * is given as fp; otherwise have to make a chain. 451 */ 452struct mbuf * 453ip_reass(ipqe, fp) 454 register struct ipqent *ipqe; 455 register struct ipq *fp; 456{ 457 register struct mbuf *m = ipqe->ipqe_m; 458 register struct ipqent *nq, *p, *q; 459 struct ip *ip; 460 struct mbuf *t; 461 int hlen = ipqe->ipqe_ip->ip_hl << 2; 462 int i, next; 463 464 /* 465 * Presence of header sizes in mbufs 466 * would confuse code below. 467 */ 468 m->m_data += hlen; 469 m->m_len -= hlen; 470 471 /* 472 * If first fragment to arrive, create a reassembly queue. 473 */ 474 if (fp == 0) { 475 MALLOC(fp, struct ipq *, sizeof (struct ipq), 476 M_FTABLE, M_NOWAIT); 477 if (fp == NULL) 478 goto dropfrag; 479 LIST_INSERT_HEAD(&ipq, fp, ipq_q); 480 fp->ipq_ttl = IPFRAGTTL; 481 fp->ipq_p = ipqe->ipqe_ip->ip_p; 482 fp->ipq_id = ipqe->ipqe_ip->ip_id; 483 LIST_INIT(&fp->ipq_fragq); 484 fp->ipq_src = ipqe->ipqe_ip->ip_src; 485 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 486 p = NULL; 487 goto insert; 488 } 489 490 /* 491 * Find a segment which begins after this one does. 492 */ 493 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL; 494 p = q, q = q->ipqe_q.le_next) 495 if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off) 496 break; 497 498 /* 499 * If there is a preceding segment, it may provide some of 500 * our data already. If so, drop the data from the incoming 501 * segment. If it provides all of our data, drop us. 502 */ 503 if (p != NULL) { 504 i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len - 505 ipqe->ipqe_ip->ip_off; 506 if (i > 0) { 507 if (i >= ipqe->ipqe_ip->ip_len) 508 goto dropfrag; 509 m_adj(ipqe->ipqe_m, i); 510 ipqe->ipqe_ip->ip_off += i; 511 ipqe->ipqe_ip->ip_len -= i; 512 } 513 } 514 515 /* 516 * While we overlap succeeding segments trim them or, 517 * if they are completely covered, dequeue them. 518 */ 519 for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len > 520 q->ipqe_ip->ip_off; q = nq) { 521 i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) - 522 q->ipqe_ip->ip_off; 523 if (i < q->ipqe_ip->ip_len) { 524 q->ipqe_ip->ip_len -= i; 525 q->ipqe_ip->ip_off += i; 526 m_adj(q->ipqe_m, i); 527 break; 528 } 529 nq = q->ipqe_q.le_next; 530 m_freem(q->ipqe_m); 531 LIST_REMOVE(q, ipqe_q); 532 FREE(q, M_IPQ); 533 } 534 535insert: 536 /* 537 * Stick new segment in its place; 538 * check for complete reassembly. 539 */ 540 if (p == NULL) { 541 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 542 } else { 543 LIST_INSERT_AFTER(p, ipqe, ipqe_q); 544 } 545 next = 0; 546 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL; 547 p = q, q = q->ipqe_q.le_next) { 548 if (q->ipqe_ip->ip_off != next) 549 return (0); 550 next += q->ipqe_ip->ip_len; 551 } 552 if (p->ipqe_mff) 553 return (0); 554 555 /* 556 * Reassembly is complete. Check for a bogus message size and 557 * concatenate fragments. 558 */ 559 q = fp->ipq_fragq.lh_first; 560 ip = q->ipqe_ip; 561 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 562 ipstat.ips_toolong++; 563 ip_freef(fp); 564 return (0); 565 } 566 m = q->ipqe_m; 567 t = m->m_next; 568 m->m_next = 0; 569 m_cat(m, t); 570 nq = q->ipqe_q.le_next; 571 FREE(q, M_IPQ); 572 for (q = nq; q != NULL; q = nq) { 573 t = q->ipqe_m; 574 nq = q->ipqe_q.le_next; 575 FREE(q, M_IPQ); 576 m_cat(m, t); 577 } 578 579 /* 580 * Create header for new ip packet by 581 * modifying header of first packet; 582 * dequeue and discard fragment reassembly header. 583 * Make header visible. 584 */ 585 ip->ip_len = next; 586 ip->ip_src = fp->ipq_src; 587 ip->ip_dst = fp->ipq_dst; 588 LIST_REMOVE(fp, ipq_q); 589 FREE(fp, M_FTABLE); 590 m->m_len += (ip->ip_hl << 2); 591 m->m_data -= (ip->ip_hl << 2); 592 /* some debugging cruft by sklower, below, will go away soon */ 593 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 594 register int plen = 0; 595 for (t = m; t; t = t->m_next) 596 plen += t->m_len; 597 m->m_pkthdr.len = plen; 598 } 599 return (m); 600 601dropfrag: 602 ipstat.ips_fragdropped++; 603 m_freem(m); 604 FREE(ipqe, M_IPQ); 605 return (0); 606} 607 608/* 609 * Free a fragment reassembly header and all 610 * associated datagrams. 611 */ 612void 613ip_freef(fp) 614 struct ipq *fp; 615{ 616 register struct ipqent *q, *p; 617 618 for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) { 619 p = q->ipqe_q.le_next; 620 m_freem(q->ipqe_m); 621 LIST_REMOVE(q, ipqe_q); 622 FREE(q, M_IPQ); 623 } 624 LIST_REMOVE(fp, ipq_q); 625 FREE(fp, M_FTABLE); 626} 627 628/* 629 * IP timer processing; 630 * if a timer expires on a reassembly 631 * queue, discard it. 632 */ 633void 634ip_slowtimo() 635{ 636 register struct ipq *fp, *nfp; 637 int s = splsoftnet(); 638 639 for (fp = ipq.lh_first; fp != NULL; fp = nfp) { 640 nfp = fp->ipq_q.le_next; 641 if (--fp->ipq_ttl == 0) { 642 ipstat.ips_fragtimeout++; 643 ip_freef(fp); 644 } 645 } 646 splx(s); 647} 648 649/* 650 * Drain off all datagram fragments. 651 */ 652void 653ip_drain() 654{ 655 656 while (ipq.lh_first != NULL) { 657 ipstat.ips_fragdropped++; 658 ip_freef(ipq.lh_first); 659 } 660} 661 662/* 663 * Do option processing on a datagram, 664 * possibly discarding it if bad options are encountered, 665 * or forwarding it if source-routed. 666 * Returns 1 if packet has been forwarded/freed, 667 * 0 if the packet should be processed further. 668 */ 669int 670ip_dooptions(m) 671 struct mbuf *m; 672{ 673 register struct ip *ip = mtod(m, struct ip *); 674 register u_char *cp; 675 register struct ip_timestamp *ipt; 676 register struct in_ifaddr *ia; 677 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 678 struct in_addr *sin, dst; 679 n_time ntime; 680 681 dst = ip->ip_dst; 682 cp = (u_char *)(ip + 1); 683 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 684 for (; cnt > 0; cnt -= optlen, cp += optlen) { 685 opt = cp[IPOPT_OPTVAL]; 686 if (opt == IPOPT_EOL) 687 break; 688 if (opt == IPOPT_NOP) 689 optlen = 1; 690 else { 691 optlen = cp[IPOPT_OLEN]; 692 if (optlen <= 0 || optlen > cnt) { 693 code = &cp[IPOPT_OLEN] - (u_char *)ip; 694 goto bad; 695 } 696 } 697 switch (opt) { 698 699 default: 700 break; 701 702 /* 703 * Source routing with record. 704 * Find interface with current destination address. 705 * If none on this machine then drop if strictly routed, 706 * or do nothing if loosely routed. 707 * Record interface address and bring up next address 708 * component. If strictly routed make sure next 709 * address is on directly accessible net. 710 */ 711 case IPOPT_LSRR: 712 case IPOPT_SSRR: 713 if (ip_allowsrcrt == 0) { 714 type = ICMP_UNREACH; 715 code = ICMP_UNREACH_NET_PROHIB; 716 goto bad; 717 } 718 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 719 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 720 goto bad; 721 } 722 ipaddr.sin_addr = ip->ip_dst; 723 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 724 if (ia == 0) { 725 if (opt == IPOPT_SSRR) { 726 type = ICMP_UNREACH; 727 code = ICMP_UNREACH_SRCFAIL; 728 goto bad; 729 } 730 /* 731 * Loose routing, and not at next destination 732 * yet; nothing to do except forward. 733 */ 734 break; 735 } 736 off--; /* 0 origin */ 737 if (off > optlen - sizeof(struct in_addr)) { 738 /* 739 * End of source route. Should be for us. 740 */ 741 save_rte(cp, ip->ip_src); 742 break; 743 } 744 /* 745 * locate outgoing interface 746 */ 747 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 748 sizeof(ipaddr.sin_addr)); 749 if (opt == IPOPT_SSRR) { 750#define INA struct in_ifaddr * 751#define SA struct sockaddr * 752 ia = (INA)ifa_ifwithladdr((SA)&ipaddr); 753 } else 754 ia = ip_rtaddr(ipaddr.sin_addr); 755 if (ia == 0) { 756 type = ICMP_UNREACH; 757 code = ICMP_UNREACH_SRCFAIL; 758 goto bad; 759 } 760 ip->ip_dst = ipaddr.sin_addr; 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 /* 765 * Let ip_intr's mcast routing check handle mcast pkts 766 */ 767 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 768 break; 769 770 case IPOPT_RR: 771 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 772 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 773 goto bad; 774 } 775 /* 776 * If no space remains, ignore. 777 */ 778 off--; /* 0 origin */ 779 if (off > optlen - sizeof(struct in_addr)) 780 break; 781 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 782 sizeof(ipaddr.sin_addr)); 783 /* 784 * locate outgoing interface; if we're the destination, 785 * use the incoming interface (should be same). 786 */ 787 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 788 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 789 type = ICMP_UNREACH; 790 code = ICMP_UNREACH_HOST; 791 goto bad; 792 } 793 bcopy((caddr_t)&ia->ia_addr.sin_addr, 794 (caddr_t)(cp + off), sizeof(struct in_addr)); 795 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 796 break; 797 798 case IPOPT_TS: 799 code = cp - (u_char *)ip; 800 ipt = (struct ip_timestamp *)cp; 801 if (ipt->ipt_len < 5) 802 goto bad; 803 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 804 if (++ipt->ipt_oflw == 0) 805 goto bad; 806 break; 807 } 808 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 809 switch (ipt->ipt_flg) { 810 811 case IPOPT_TS_TSONLY: 812 break; 813 814 case IPOPT_TS_TSANDADDR: 815 if (ipt->ipt_ptr + sizeof(n_time) + 816 sizeof(struct in_addr) > ipt->ipt_len) 817 goto bad; 818 ipaddr.sin_addr = dst; 819 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 820 m->m_pkthdr.rcvif); 821 if (ia == 0) 822 continue; 823 bcopy((caddr_t)&ia->ia_addr.sin_addr, 824 (caddr_t)sin, sizeof(struct in_addr)); 825 ipt->ipt_ptr += sizeof(struct in_addr); 826 break; 827 828 case IPOPT_TS_PRESPEC: 829 if (ipt->ipt_ptr + sizeof(n_time) + 830 sizeof(struct in_addr) > ipt->ipt_len) 831 goto bad; 832 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 833 sizeof(struct in_addr)); 834 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 835 continue; 836 ipt->ipt_ptr += sizeof(struct in_addr); 837 break; 838 839 default: 840 goto bad; 841 } 842 ntime = iptime(); 843 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 844 sizeof(n_time)); 845 ipt->ipt_ptr += sizeof(n_time); 846 } 847 } 848 if (forward) { 849 if (ip_forwsrcrt == 0) { 850 type = ICMP_UNREACH; 851 code = ICMP_UNREACH_SRCFAIL; 852 goto bad; 853 } 854 ip_forward(m, 1); 855 return (1); 856 } 857 return (0); 858bad: 859 ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 860 icmp_error(m, type, code, 0, 0); 861 ipstat.ips_badoptions++; 862 return (1); 863} 864 865/* 866 * Given address of next destination (final or next hop), 867 * return internet address info of interface to be used to get there. 868 */ 869struct in_ifaddr * 870ip_rtaddr(dst) 871 struct in_addr dst; 872{ 873 register struct sockaddr_in *sin; 874 875 sin = satosin(&ipforward_rt.ro_dst); 876 877 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) { 878 if (ipforward_rt.ro_rt) { 879 RTFREE(ipforward_rt.ro_rt); 880 ipforward_rt.ro_rt = 0; 881 } 882 sin->sin_family = AF_INET; 883 sin->sin_len = sizeof(*sin); 884 sin->sin_addr = dst; 885 886 rtalloc(&ipforward_rt); 887 } 888 if (ipforward_rt.ro_rt == 0) 889 return ((struct in_ifaddr *)0); 890 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 891} 892 893/* 894 * Save incoming source route for use in replies, 895 * to be picked up later by ip_srcroute if the receiver is interested. 896 */ 897void 898save_rte(option, dst) 899 u_char *option; 900 struct in_addr dst; 901{ 902 unsigned olen; 903 904 olen = option[IPOPT_OLEN]; 905#ifdef DIAGNOSTIC 906 if (ipprintfs) 907 printf("save_rte: olen %d\n", olen); 908#endif 909 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 910 return; 911 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 912 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 913 ip_srcrt.dst = dst; 914} 915 916/* 917 * Retrieve incoming source route for use in replies, 918 * in the same form used by setsockopt. 919 * The first hop is placed before the options, will be removed later. 920 */ 921struct mbuf * 922ip_srcroute() 923{ 924 register struct in_addr *p, *q; 925 register struct mbuf *m; 926 927 if (ip_nhops == 0) 928 return ((struct mbuf *)0); 929 m = m_get(M_DONTWAIT, MT_SOOPTS); 930 if (m == 0) 931 return ((struct mbuf *)0); 932 933#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 934 935 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 936 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 937 OPTSIZ; 938#ifdef DIAGNOSTIC 939 if (ipprintfs) 940 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 941#endif 942 943 /* 944 * First save first hop for return route 945 */ 946 p = &ip_srcrt.route[ip_nhops - 1]; 947 *(mtod(m, struct in_addr *)) = *p--; 948#ifdef DIAGNOSTIC 949 if (ipprintfs) 950 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 951#endif 952 953 /* 954 * Copy option fields and padding (nop) to mbuf. 955 */ 956 ip_srcrt.nop = IPOPT_NOP; 957 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 958 bcopy((caddr_t)&ip_srcrt.nop, 959 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 960 q = (struct in_addr *)(mtod(m, caddr_t) + 961 sizeof(struct in_addr) + OPTSIZ); 962#undef OPTSIZ 963 /* 964 * Record return path as an IP source route, 965 * reversing the path (pointers are now aligned). 966 */ 967 while (p >= ip_srcrt.route) { 968#ifdef DIAGNOSTIC 969 if (ipprintfs) 970 printf(" %x", ntohl(q->s_addr)); 971#endif 972 *q++ = *p--; 973 } 974 /* 975 * Last hop goes to final destination. 976 */ 977 *q = ip_srcrt.dst; 978#ifdef DIAGNOSTIC 979 if (ipprintfs) 980 printf(" %x\n", ntohl(q->s_addr)); 981#endif 982 return (m); 983} 984 985/* 986 * Strip out IP options, at higher 987 * level protocol in the kernel. 988 * Second argument is buffer to which options 989 * will be moved, and return value is their length. 990 * XXX should be deleted; last arg currently ignored. 991 */ 992void 993ip_stripoptions(m, mopt) 994 register struct mbuf *m; 995 struct mbuf *mopt; 996{ 997 register int i; 998 struct ip *ip = mtod(m, struct ip *); 999 register caddr_t opts; 1000 int olen; 1001 1002 olen = (ip->ip_hl<<2) - sizeof (struct ip); 1003 opts = (caddr_t)(ip + 1); 1004 i = m->m_len - (sizeof (struct ip) + olen); 1005 bcopy(opts + olen, opts, (unsigned)i); 1006 m->m_len -= olen; 1007 if (m->m_flags & M_PKTHDR) 1008 m->m_pkthdr.len -= olen; 1009 ip->ip_hl = sizeof(struct ip) >> 2; 1010} 1011 1012int inetctlerrmap[PRC_NCMDS] = { 1013 0, 0, 0, 0, 1014 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1015 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1016 EMSGSIZE, EHOSTUNREACH, 0, 0, 1017 0, 0, 0, 0, 1018 ENOPROTOOPT 1019}; 1020 1021/* 1022 * Forward a packet. If some error occurs return the sender 1023 * an icmp packet. Note we can't always generate a meaningful 1024 * icmp message because icmp doesn't have a large enough repertoire 1025 * of codes and types. 1026 * 1027 * If not forwarding, just drop the packet. This could be confusing 1028 * if ipforwarding was zero but some routing protocol was advancing 1029 * us as a gateway to somewhere. However, we must let the routing 1030 * protocol deal with that. 1031 * 1032 * The srcrt parameter indicates whether the packet is being forwarded 1033 * via a source route. 1034 */ 1035void 1036ip_forward(m, srcrt) 1037 struct mbuf *m; 1038 int srcrt; 1039{ 1040 register struct ip *ip = mtod(m, struct ip *); 1041 register struct sockaddr_in *sin; 1042 register struct rtentry *rt; 1043 int error, type = 0, code = 0; 1044 struct mbuf *mcopy; 1045 n_long dest; 1046 struct ifnet *destifp; 1047 1048 dest = 0; 1049#ifdef DIAGNOSTIC 1050 if (ipprintfs) 1051 printf("forward: src %x dst %x ttl %x\n", 1052 ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl); 1053#endif 1054 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1055 ipstat.ips_cantforward++; 1056 m_freem(m); 1057 return; 1058 } 1059 HTONS(ip->ip_id); 1060 if (ip->ip_ttl <= IPTTLDEC) { 1061 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1062 return; 1063 } 1064 ip->ip_ttl -= IPTTLDEC; 1065 1066 sin = satosin(&ipforward_rt.ro_dst); 1067 if ((rt = ipforward_rt.ro_rt) == 0 || 1068 !in_hosteq(ip->ip_dst, sin->sin_addr)) { 1069 if (ipforward_rt.ro_rt) { 1070 RTFREE(ipforward_rt.ro_rt); 1071 ipforward_rt.ro_rt = 0; 1072 } 1073 sin->sin_family = AF_INET; 1074 sin->sin_len = sizeof(struct sockaddr_in); 1075 sin->sin_addr = ip->ip_dst; 1076 1077 rtalloc(&ipforward_rt); 1078 if (ipforward_rt.ro_rt == 0) { 1079 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1080 return; 1081 } 1082 rt = ipforward_rt.ro_rt; 1083 } 1084 1085 /* 1086 * Save at most 68 bytes of the packet in case 1087 * we need to generate an ICMP message to the src. 1088 */ 1089 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68)); 1090 1091 /* 1092 * If forwarding packet using same interface that it came in on, 1093 * perhaps should send a redirect to sender to shortcut a hop. 1094 * Only send redirect if source is sending directly to us, 1095 * and if packet was not source routed (or has any options). 1096 * Also, don't send redirect if forwarding using a default route 1097 * or a route modified by a redirect. 1098 */ 1099 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1100 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1101 !in_nullhost(satosin(rt_key(rt))->sin_addr) && 1102 ipsendredirects && !srcrt) { 1103 if (rt->rt_ifa && 1104 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1105 ifatoia(rt->rt_ifa)->ia_subnet) { 1106 if (rt->rt_flags & RTF_GATEWAY) 1107 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1108 else 1109 dest = ip->ip_dst.s_addr; 1110 /* Router requirements says to only send host redirects */ 1111 type = ICMP_REDIRECT; 1112 code = ICMP_REDIRECT_HOST; 1113#ifdef DIAGNOSTIC 1114 if (ipprintfs) 1115 printf("redirect (%d) to %x\n", code, (u_int32_t)dest); 1116#endif 1117 } 1118 } 1119 1120 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1121 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0); 1122 if (error) 1123 ipstat.ips_cantforward++; 1124 else { 1125 ipstat.ips_forward++; 1126 if (type) 1127 ipstat.ips_redirectsent++; 1128 else { 1129 if (mcopy) 1130 m_freem(mcopy); 1131 return; 1132 } 1133 } 1134 if (mcopy == NULL) 1135 return; 1136 destifp = NULL; 1137 1138 switch (error) { 1139 1140 case 0: /* forwarded, but need redirect */ 1141 /* type, code set above */ 1142 break; 1143 1144 case ENETUNREACH: /* shouldn't happen, checked above */ 1145 case EHOSTUNREACH: 1146 case ENETDOWN: 1147 case EHOSTDOWN: 1148 default: 1149 type = ICMP_UNREACH; 1150 code = ICMP_UNREACH_HOST; 1151 break; 1152 1153 case EMSGSIZE: 1154 type = ICMP_UNREACH; 1155 code = ICMP_UNREACH_NEEDFRAG; 1156 if (ipforward_rt.ro_rt) 1157 destifp = ipforward_rt.ro_rt->rt_ifp; 1158 ipstat.ips_cantfrag++; 1159 break; 1160 1161 case ENOBUFS: 1162 type = ICMP_SOURCEQUENCH; 1163 code = 0; 1164 break; 1165 } 1166 icmp_error(mcopy, type, code, dest, destifp); 1167} 1168 1169void 1170ip_savecontrol(inp, mp, ip, m) 1171 register struct inpcb *inp; 1172 register struct mbuf **mp; 1173 register struct ip *ip; 1174 register struct mbuf *m; 1175{ 1176 1177 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1178 struct timeval tv; 1179 1180 microtime(&tv); 1181 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1182 SCM_TIMESTAMP, SOL_SOCKET); 1183 if (*mp) 1184 mp = &(*mp)->m_next; 1185 } 1186 if (inp->inp_flags & INP_RECVDSTADDR) { 1187 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1188 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1189 if (*mp) 1190 mp = &(*mp)->m_next; 1191 } 1192#ifdef notyet 1193 /* 1194 * XXX 1195 * Moving these out of udp_input() made them even more broken 1196 * than they already were. 1197 * - fenner@parc.xerox.com 1198 */ 1199 /* options were tossed already */ 1200 if (inp->inp_flags & INP_RECVOPTS) { 1201 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1202 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1203 if (*mp) 1204 mp = &(*mp)->m_next; 1205 } 1206 /* ip_srcroute doesn't do what we want here, need to fix */ 1207 if (inp->inp_flags & INP_RECVRETOPTS) { 1208 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1209 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1210 if (*mp) 1211 mp = &(*mp)->m_next; 1212 } 1213#endif 1214 if (inp->inp_flags & INP_RECVIF) { 1215 struct sockaddr_dl sdl; 1216 1217 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1218 sdl.sdl_family = AF_LINK; 1219 sdl.sdl_index = m->m_pkthdr.rcvif ? 1220 m->m_pkthdr.rcvif->if_index : 0; 1221 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1222 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1223 IP_RECVIF, IPPROTO_IP); 1224 if (*mp) 1225 mp = &(*mp)->m_next; 1226 } 1227} 1228 1229int 1230ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 1231 int *name; 1232 u_int namelen; 1233 void *oldp; 1234 size_t *oldlenp; 1235 void *newp; 1236 size_t newlen; 1237{ 1238 /* All sysctl names at this level are terminal. */ 1239 if (namelen != 1) 1240 return (ENOTDIR); 1241 1242 switch (name[0]) { 1243 case IPCTL_FORWARDING: 1244 return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding)); 1245 case IPCTL_SENDREDIRECTS: 1246 return (sysctl_int(oldp, oldlenp, newp, newlen, 1247 &ipsendredirects)); 1248 case IPCTL_DEFTTL: 1249 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl)); 1250#ifdef notyet 1251 case IPCTL_DEFMTU: 1252 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 1253#endif 1254 case IPCTL_FORWSRCRT: 1255 /* Don't allow this to change in a secure environment. */ 1256 if (securelevel > 0) 1257 return (sysctl_rdint(oldp, oldlenp, newp, 1258 ip_forwsrcrt)); 1259 else 1260 return (sysctl_int(oldp, oldlenp, newp, newlen, 1261 &ip_forwsrcrt)); 1262 case IPCTL_DIRECTEDBCAST: 1263 return (sysctl_int(oldp, oldlenp, newp, newlen, 1264 &ip_directedbcast)); 1265 case IPCTL_ALLOWSRCRT: 1266 return (sysctl_int(oldp, oldlenp, newp, newlen, 1267 &ip_allowsrcrt)); 1268 default: 1269 return (EOPNOTSUPP); 1270 } 1271 /* NOTREACHED */ 1272} 1273