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