ip_input.c revision 1.140
1/* $NetBSD: ip_input.c,v 1.140 2001/11/04 20:55:27 matt Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * 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. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69/* 70 * Copyright (c) 1982, 1986, 1988, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. All advertising materials mentioning features or use of this software 82 * must display the following acknowledgement: 83 * This product includes software developed by the University of 84 * California, Berkeley and its contributors. 85 * 4. Neither the name of the University nor the names of its contributors 86 * may be used to endorse or promote products derived from this software 87 * without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 * 101 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 102 */ 103 104#include "opt_gateway.h" 105#include "opt_pfil_hooks.h" 106#include "opt_ipsec.h" 107#include "opt_mrouting.h" 108#include "opt_inet_csum.h" 109 110#include <sys/param.h> 111#include <sys/systm.h> 112#include <sys/malloc.h> 113#include <sys/mbuf.h> 114#include <sys/domain.h> 115#include <sys/protosw.h> 116#include <sys/socket.h> 117#include <sys/socketvar.h> 118#include <sys/errno.h> 119#include <sys/time.h> 120#include <sys/kernel.h> 121#include <sys/pool.h> 122#include <sys/sysctl.h> 123 124#include <net/if.h> 125#include <net/if_dl.h> 126#include <net/route.h> 127#include <net/pfil.h> 128 129#include <netinet/in.h> 130#include <netinet/in_systm.h> 131#include <netinet/ip.h> 132#include <netinet/in_pcb.h> 133#include <netinet/in_var.h> 134#include <netinet/ip_var.h> 135#include <netinet/ip_icmp.h> 136/* just for gif_ttl */ 137#include <netinet/in_gif.h> 138#include "gif.h" 139 140#ifdef MROUTING 141#include <netinet/ip_mroute.h> 142#endif 143 144#ifdef IPSEC 145#include <netinet6/ipsec.h> 146#include <netkey/key.h> 147#endif 148 149#ifndef IPFORWARDING 150#ifdef GATEWAY 151#define IPFORWARDING 1 /* forward IP packets not for us */ 152#else /* GATEWAY */ 153#define IPFORWARDING 0 /* don't forward IP packets not for us */ 154#endif /* GATEWAY */ 155#endif /* IPFORWARDING */ 156#ifndef IPSENDREDIRECTS 157#define IPSENDREDIRECTS 1 158#endif 159#ifndef IPFORWSRCRT 160#define IPFORWSRCRT 1 /* forward source-routed packets */ 161#endif 162#ifndef IPALLOWSRCRT 163#define IPALLOWSRCRT 1 /* allow source-routed packets */ 164#endif 165#ifndef IPMTUDISC 166#define IPMTUDISC 0 167#endif 168#ifndef IPMTUDISCTIMEOUT 169#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 170#endif 171 172/* 173 * Note: DIRECTED_BROADCAST is handled this way so that previous 174 * configuration using this option will Just Work. 175 */ 176#ifndef IPDIRECTEDBCAST 177#ifdef DIRECTED_BROADCAST 178#define IPDIRECTEDBCAST 1 179#else 180#define IPDIRECTEDBCAST 0 181#endif /* DIRECTED_BROADCAST */ 182#endif /* IPDIRECTEDBCAST */ 183int ipforwarding = IPFORWARDING; 184int ipsendredirects = IPSENDREDIRECTS; 185int ip_defttl = IPDEFTTL; 186int ip_forwsrcrt = IPFORWSRCRT; 187int ip_directedbcast = IPDIRECTEDBCAST; 188int ip_allowsrcrt = IPALLOWSRCRT; 189int ip_mtudisc = IPMTUDISC; 190u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 191#ifdef DIAGNOSTIC 192int ipprintfs = 0; 193#endif 194 195struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 196 197extern struct domain inetdomain; 198int ipqmaxlen = IFQ_MAXLEN; 199struct in_ifaddrhead in_ifaddr; 200struct in_ifaddrhashhead *in_ifaddrhashtbl; 201struct ifqueue ipintrq; 202struct ipstat ipstat; 203u_int16_t ip_id; 204 205#ifdef PFIL_HOOKS 206struct pfil_head inet_pfil_hook; 207#endif 208 209struct ipqhead ipq; 210int ipq_locked; 211int ip_nfragpackets = 0; 212int ip_maxfragpackets = 200; 213 214static __inline int ipq_lock_try __P((void)); 215static __inline void ipq_unlock __P((void)); 216 217static __inline int 218ipq_lock_try() 219{ 220 int s; 221 222 /* 223 * Use splvm() -- we're bloking things that would cause 224 * mbuf allocation. 225 */ 226 s = splvm(); 227 if (ipq_locked) { 228 splx(s); 229 return (0); 230 } 231 ipq_locked = 1; 232 splx(s); 233 return (1); 234} 235 236static __inline void 237ipq_unlock() 238{ 239 int s; 240 241 s = splvm(); 242 ipq_locked = 0; 243 splx(s); 244} 245 246#ifdef DIAGNOSTIC 247#define IPQ_LOCK() \ 248do { \ 249 if (ipq_lock_try() == 0) { \ 250 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \ 251 panic("ipq_lock"); \ 252 } \ 253} while (0) 254#define IPQ_LOCK_CHECK() \ 255do { \ 256 if (ipq_locked == 0) { \ 257 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \ 258 panic("ipq lock check"); \ 259 } \ 260} while (0) 261#else 262#define IPQ_LOCK() (void) ipq_lock_try() 263#define IPQ_LOCK_CHECK() /* nothing */ 264#endif 265 266#define IPQ_UNLOCK() ipq_unlock() 267 268struct pool ipqent_pool; 269 270#ifdef INET_CSUM_COUNTERS 271#include <sys/device.h> 272 273struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 274 NULL, "inet", "hwcsum bad"); 275struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 276 NULL, "inet", "hwcsum ok"); 277struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 278 NULL, "inet", "swcsum"); 279 280#define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 281 282#else 283 284#define INET_CSUM_COUNTER_INCR(ev) /* nothing */ 285 286#endif /* INET_CSUM_COUNTERS */ 287 288/* 289 * We need to save the IP options in case a protocol wants to respond 290 * to an incoming packet over the same route if the packet got here 291 * using IP source routing. This allows connection establishment and 292 * maintenance when the remote end is on a network that is not known 293 * to us. 294 */ 295int ip_nhops = 0; 296static struct ip_srcrt { 297 struct in_addr dst; /* final destination */ 298 char nop; /* one NOP to align */ 299 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 300 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 301} ip_srcrt; 302 303static void save_rte __P((u_char *, struct in_addr)); 304 305/* 306 * IP initialization: fill in IP protocol switch table. 307 * All protocols not implemented in kernel go to raw IP protocol handler. 308 */ 309void 310ip_init() 311{ 312 struct protosw *pr; 313 int i; 314 315 pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", 316 0, NULL, NULL, M_IPQ); 317 318 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 319 if (pr == 0) 320 panic("ip_init"); 321 for (i = 0; i < IPPROTO_MAX; i++) 322 ip_protox[i] = pr - inetsw; 323 for (pr = inetdomain.dom_protosw; 324 pr < inetdomain.dom_protoswNPROTOSW; pr++) 325 if (pr->pr_domain->dom_family == PF_INET && 326 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 327 ip_protox[pr->pr_protocol] = pr - inetsw; 328 LIST_INIT(&ipq); 329 ip_id = time.tv_sec & 0xffff; 330 ipintrq.ifq_maxlen = ipqmaxlen; 331 TAILQ_INIT(&in_ifaddr); 332 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR, 333 M_WAITOK, &in_ifaddrhash); 334 if (ip_mtudisc != 0) 335 ip_mtudisc_timeout_q = 336 rt_timer_queue_create(ip_mtudisc_timeout); 337#ifdef GATEWAY 338 ipflow_init(); 339#endif 340 341#ifdef PFIL_HOOKS 342 /* Register our Packet Filter hook. */ 343 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 344 inet_pfil_hook.ph_af = AF_INET; 345 i = pfil_head_register(&inet_pfil_hook); 346 if (i != 0) 347 printf("ip_init: WARNING: unable to register pfil hook, " 348 "error %d\n", i); 349#endif /* PFIL_HOOKS */ 350 351#ifdef INET_CSUM_COUNTERS 352 evcnt_attach_static(&ip_hwcsum_bad); 353 evcnt_attach_static(&ip_hwcsum_ok); 354 evcnt_attach_static(&ip_swcsum); 355#endif /* INET_CSUM_COUNTERS */ 356} 357 358struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 359struct route ipforward_rt; 360 361/* 362 * IP software interrupt routine 363 */ 364void 365ipintr() 366{ 367 int s; 368 struct mbuf *m; 369 370 while (1) { 371 s = splnet(); 372 IF_DEQUEUE(&ipintrq, m); 373 splx(s); 374 if (m == 0) 375 return; 376 ip_input(m); 377 } 378} 379 380/* 381 * Ip input routine. Checksum and byte swap header. If fragmented 382 * try to reassemble. Process options. Pass to next level. 383 */ 384void 385ip_input(struct mbuf *m) 386{ 387 struct ip *ip = NULL; 388 struct ipq *fp; 389 struct in_ifaddr *ia; 390 struct ifaddr *ifa; 391 struct ipqent *ipqe; 392 int hlen = 0, mff, len; 393 int downmatch; 394 395#ifdef DIAGNOSTIC 396 if ((m->m_flags & M_PKTHDR) == 0) 397 panic("ipintr no HDR"); 398#endif 399#ifdef IPSEC 400 /* 401 * should the inner packet be considered authentic? 402 * see comment in ah4_input(). 403 */ 404 if (m) { 405 m->m_flags &= ~M_AUTHIPHDR; 406 m->m_flags &= ~M_AUTHIPDGM; 407 } 408#endif 409 /* 410 * If no IP addresses have been set yet but the interfaces 411 * are receiving, can't do anything with incoming packets yet. 412 */ 413 if (TAILQ_FIRST(&in_ifaddr) == 0) 414 goto bad; 415 ipstat.ips_total++; 416 if (m->m_len < sizeof (struct ip) && 417 (m = m_pullup(m, sizeof (struct ip))) == 0) { 418 ipstat.ips_toosmall++; 419 return; 420 } 421 ip = mtod(m, struct ip *); 422 if (ip->ip_v != IPVERSION) { 423 ipstat.ips_badvers++; 424 goto bad; 425 } 426 hlen = ip->ip_hl << 2; 427 if (hlen < sizeof(struct ip)) { /* minimum header length */ 428 ipstat.ips_badhlen++; 429 goto bad; 430 } 431 if (hlen > m->m_len) { 432 if ((m = m_pullup(m, hlen)) == 0) { 433 ipstat.ips_badhlen++; 434 return; 435 } 436 ip = mtod(m, struct ip *); 437 } 438 439 /* 440 * RFC1122: packets with a multicast source address are 441 * not allowed. 442 */ 443 if (IN_MULTICAST(ip->ip_src.s_addr)) { 444 ipstat.ips_badaddr++; 445 goto bad; 446 } 447 448 /* 127/8 must not appear on wire - RFC1122 */ 449 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 450 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 451 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 452 ipstat.ips_badaddr++; 453 goto bad; 454 } 455 } 456 457 switch (m->m_pkthdr.csum_flags & 458 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) | 459 M_CSUM_IPv4_BAD)) { 460 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 461 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); 462 goto badcsum; 463 464 case M_CSUM_IPv4: 465 /* Checksum was okay. */ 466 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); 467 break; 468 469 default: 470 /* Must compute it ourselves. */ 471 INET_CSUM_COUNTER_INCR(&ip_swcsum); 472 if (in_cksum(m, hlen) != 0) 473 goto bad; 474 break; 475 } 476 477 /* Retrieve the packet length. */ 478 len = ntohs(ip->ip_len); 479 480 /* 481 * Check for additional length bogosity 482 */ 483 if (len < hlen) { 484 ipstat.ips_badlen++; 485 goto bad; 486 } 487 488 /* 489 * Check that the amount of data in the buffers 490 * is as at least much as the IP header would have us expect. 491 * Trim mbufs if longer than we expect. 492 * Drop packet if shorter than we expect. 493 */ 494 if (m->m_pkthdr.len < len) { 495 ipstat.ips_tooshort++; 496 goto bad; 497 } 498 if (m->m_pkthdr.len > len) { 499 if (m->m_len == m->m_pkthdr.len) { 500 m->m_len = len; 501 m->m_pkthdr.len = len; 502 } else 503 m_adj(m, len - m->m_pkthdr.len); 504 } 505 506#ifdef IPSEC 507 /* ipflow (IP fast fowarding) is not compatible with IPsec. */ 508 m->m_flags &= ~M_CANFASTFWD; 509#else 510 /* 511 * Assume that we can create a fast-forward IP flow entry 512 * based on this packet. 513 */ 514 m->m_flags |= M_CANFASTFWD; 515#endif 516 517#ifdef PFIL_HOOKS 518 /* 519 * Run through list of hooks for input packets. If there are any 520 * filters which require that additional packets in the flow are 521 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 522 * Note that filters must _never_ set this flag, as another filter 523 * in the list may have previously cleared it. 524 */ 525 /* 526 * let ipfilter look at packet on the wire, 527 * not the decapsulated packet. 528 */ 529#ifdef IPSEC 530 if (!ipsec_getnhist(m)) 531#else 532 if (1) 533#endif 534 { 535 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 536 PFIL_IN) != 0) 537 return; 538 if (m == NULL) 539 return; 540 ip = mtod(m, struct ip *); 541 } 542#endif /* PFIL_HOOKS */ 543 544#ifdef ALTQ 545 /* XXX Temporary until ALTQ is changed to use a pfil hook */ 546 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) { 547 /* packet dropped by traffic conditioner */ 548 return; 549 } 550#endif 551 552 /* 553 * Convert fields to host representation. 554 */ 555 NTOHS(ip->ip_len); 556 NTOHS(ip->ip_off); 557 558 /* 559 * Process options and, if not destined for us, 560 * ship it on. ip_dooptions returns 1 when an 561 * error was detected (causing an icmp message 562 * to be sent and the original packet to be freed). 563 */ 564 ip_nhops = 0; /* for source routed packets */ 565 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 566 return; 567 568 /* 569 * Check our list of addresses, to see if the packet is for us. 570 * 571 * Traditional 4.4BSD did not consult IFF_UP at all. 572 * The behavior here is to treat addresses on !IFF_UP interface 573 * as not mine. 574 */ 575 downmatch = 0; 576 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 577 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) { 578 if ((ia->ia_ifp->if_flags & IFF_UP) != 0) 579 break; 580 else 581 downmatch++; 582 } 583 } 584 if (ia != NULL) 585 goto ours; 586 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 587 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) { 588 if (ifa->ifa_addr->sa_family != AF_INET) 589 continue; 590 ia = ifatoia(ifa); 591 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || 592 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || 593 /* 594 * Look for all-0's host part (old broadcast addr), 595 * either for subnet or net. 596 */ 597 ip->ip_dst.s_addr == ia->ia_subnet || 598 ip->ip_dst.s_addr == ia->ia_net) 599 goto ours; 600 /* 601 * An interface with IP address zero accepts 602 * all packets that arrive on that interface. 603 */ 604 if (in_nullhost(ia->ia_addr.sin_addr)) 605 goto ours; 606 } 607 } 608 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 609 struct in_multi *inm; 610#ifdef MROUTING 611 extern struct socket *ip_mrouter; 612 613 if (m->m_flags & M_EXT) { 614 if ((m = m_pullup(m, hlen)) == 0) { 615 ipstat.ips_toosmall++; 616 return; 617 } 618 ip = mtod(m, struct ip *); 619 } 620 621 if (ip_mrouter) { 622 /* 623 * If we are acting as a multicast router, all 624 * incoming multicast packets are passed to the 625 * kernel-level multicast forwarding function. 626 * The packet is returned (relatively) intact; if 627 * ip_mforward() returns a non-zero value, the packet 628 * must be discarded, else it may be accepted below. 629 * 630 * (The IP ident field is put in the same byte order 631 * as expected when ip_mforward() is called from 632 * ip_output().) 633 */ 634 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 635 ipstat.ips_cantforward++; 636 m_freem(m); 637 return; 638 } 639 640 /* 641 * The process-level routing demon needs to receive 642 * all multicast IGMP packets, whether or not this 643 * host belongs to their destination groups. 644 */ 645 if (ip->ip_p == IPPROTO_IGMP) 646 goto ours; 647 ipstat.ips_forward++; 648 } 649#endif 650 /* 651 * See if we belong to the destination multicast group on the 652 * arrival interface. 653 */ 654 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 655 if (inm == NULL) { 656 ipstat.ips_cantforward++; 657 m_freem(m); 658 return; 659 } 660 goto ours; 661 } 662 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 663 in_nullhost(ip->ip_dst)) 664 goto ours; 665 666 /* 667 * Not for us; forward if possible and desirable. 668 */ 669 if (ipforwarding == 0) { 670 ipstat.ips_cantforward++; 671 m_freem(m); 672 } else { 673 /* 674 * If ip_dst matched any of my address on !IFF_UP interface, 675 * and there's no IFF_UP interface that matches ip_dst, 676 * send icmp unreach. Forwarding it will result in in-kernel 677 * forwarding loop till TTL goes to 0. 678 */ 679 if (downmatch) { 680 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 681 ipstat.ips_cantforward++; 682 return; 683 } 684 ip_forward(m, 0); 685 } 686 return; 687 688ours: 689 /* 690 * If offset or IP_MF are set, must reassemble. 691 * Otherwise, nothing need be done. 692 * (We could look in the reassembly queue to see 693 * if the packet was previously fragmented, 694 * but it's not worth the time; just let them time out.) 695 */ 696 if (ip->ip_off & ~(IP_DF|IP_RF)) { 697 /* 698 * Look for queue of fragments 699 * of this datagram. 700 */ 701 IPQ_LOCK(); 702 LIST_FOREACH(fp, &ipq, ipq_q) 703 if (ip->ip_id == fp->ipq_id && 704 in_hosteq(ip->ip_src, fp->ipq_src) && 705 in_hosteq(ip->ip_dst, fp->ipq_dst) && 706 ip->ip_p == fp->ipq_p) 707 goto found; 708 fp = 0; 709found: 710 711 /* 712 * Adjust ip_len to not reflect header, 713 * set ipqe_mff if more fragments are expected, 714 * convert offset of this to bytes. 715 */ 716 ip->ip_len -= hlen; 717 mff = (ip->ip_off & IP_MF) != 0; 718 if (mff) { 719 /* 720 * Make sure that fragments have a data length 721 * that's a non-zero multiple of 8 bytes. 722 */ 723 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 724 ipstat.ips_badfrags++; 725 IPQ_UNLOCK(); 726 goto bad; 727 } 728 } 729 ip->ip_off <<= 3; 730 731 /* 732 * If datagram marked as having more fragments 733 * or if this is not the first fragment, 734 * attempt reassembly; if it succeeds, proceed. 735 */ 736 if (mff || ip->ip_off) { 737 ipstat.ips_fragments++; 738 ipqe = pool_get(&ipqent_pool, PR_NOWAIT); 739 if (ipqe == NULL) { 740 ipstat.ips_rcvmemdrop++; 741 IPQ_UNLOCK(); 742 goto bad; 743 } 744 ipqe->ipqe_mff = mff; 745 ipqe->ipqe_m = m; 746 ipqe->ipqe_ip = ip; 747 m = ip_reass(ipqe, fp); 748 if (m == 0) { 749 IPQ_UNLOCK(); 750 return; 751 } 752 ipstat.ips_reassembled++; 753 ip = mtod(m, struct ip *); 754 hlen = ip->ip_hl << 2; 755 ip->ip_len += hlen; 756 } else 757 if (fp) 758 ip_freef(fp); 759 IPQ_UNLOCK(); 760 } 761 762#ifdef IPSEC 763 /* 764 * enforce IPsec policy checking if we are seeing last header. 765 * note that we do not visit this with protocols with pcb layer 766 * code - like udp/tcp/raw ip. 767 */ 768 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 769 ipsec4_in_reject(m, NULL)) { 770 ipsecstat.in_polvio++; 771 goto bad; 772 } 773#endif 774 775 /* 776 * Switch out to protocol's input routine. 777 */ 778#if IFA_STATS 779 if (ia && ip) 780 ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len; 781#endif 782 ipstat.ips_delivered++; 783 { 784 int off = hlen, nh = ip->ip_p; 785 786 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh); 787 return; 788 } 789bad: 790 m_freem(m); 791 return; 792 793badcsum: 794 ipstat.ips_badsum++; 795 m_freem(m); 796} 797 798/* 799 * Take incoming datagram fragment and try to 800 * reassemble it into whole datagram. If a chain for 801 * reassembly of this datagram already exists, then it 802 * is given as fp; otherwise have to make a chain. 803 */ 804struct mbuf * 805ip_reass(ipqe, fp) 806 struct ipqent *ipqe; 807 struct ipq *fp; 808{ 809 struct mbuf *m = ipqe->ipqe_m; 810 struct ipqent *nq, *p, *q; 811 struct ip *ip; 812 struct mbuf *t; 813 int hlen = ipqe->ipqe_ip->ip_hl << 2; 814 int i, next; 815 816 IPQ_LOCK_CHECK(); 817 818 /* 819 * Presence of header sizes in mbufs 820 * would confuse code below. 821 */ 822 m->m_data += hlen; 823 m->m_len -= hlen; 824 825 /* 826 * If first fragment to arrive, create a reassembly queue. 827 */ 828 if (fp == 0) { 829 /* 830 * Enforce upper bound on number of fragmented packets 831 * for which we attempt reassembly; 832 * If maxfrag is 0, never accept fragments. 833 * If maxfrag is -1, accept all fragments without limitation. 834 */ 835 if (ip_maxfragpackets < 0) 836 ; 837 else if (ip_nfragpackets >= ip_maxfragpackets) 838 goto dropfrag; 839 ip_nfragpackets++; 840 MALLOC(fp, struct ipq *, sizeof (struct ipq), 841 M_FTABLE, M_NOWAIT); 842 if (fp == NULL) 843 goto dropfrag; 844 LIST_INSERT_HEAD(&ipq, fp, ipq_q); 845 fp->ipq_ttl = IPFRAGTTL; 846 fp->ipq_p = ipqe->ipqe_ip->ip_p; 847 fp->ipq_id = ipqe->ipqe_ip->ip_id; 848 LIST_INIT(&fp->ipq_fragq); 849 fp->ipq_src = ipqe->ipqe_ip->ip_src; 850 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 851 p = NULL; 852 goto insert; 853 } 854 855 /* 856 * Find a segment which begins after this one does. 857 */ 858 for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL; 859 p = q, q = LIST_NEXT(q, ipqe_q)) 860 if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off) 861 break; 862 863 /* 864 * If there is a preceding segment, it may provide some of 865 * our data already. If so, drop the data from the incoming 866 * segment. If it provides all of our data, drop us. 867 */ 868 if (p != NULL) { 869 i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len - 870 ipqe->ipqe_ip->ip_off; 871 if (i > 0) { 872 if (i >= ipqe->ipqe_ip->ip_len) 873 goto dropfrag; 874 m_adj(ipqe->ipqe_m, i); 875 ipqe->ipqe_ip->ip_off += i; 876 ipqe->ipqe_ip->ip_len -= i; 877 } 878 } 879 880 /* 881 * While we overlap succeeding segments trim them or, 882 * if they are completely covered, dequeue them. 883 */ 884 for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len > 885 q->ipqe_ip->ip_off; q = nq) { 886 i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) - 887 q->ipqe_ip->ip_off; 888 if (i < q->ipqe_ip->ip_len) { 889 q->ipqe_ip->ip_len -= i; 890 q->ipqe_ip->ip_off += i; 891 m_adj(q->ipqe_m, i); 892 break; 893 } 894 nq = LIST_NEXT(q, ipqe_q); 895 m_freem(q->ipqe_m); 896 LIST_REMOVE(q, ipqe_q); 897 pool_put(&ipqent_pool, q); 898 } 899 900insert: 901 /* 902 * Stick new segment in its place; 903 * check for complete reassembly. 904 */ 905 if (p == NULL) { 906 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 907 } else { 908 LIST_INSERT_AFTER(p, ipqe, ipqe_q); 909 } 910 next = 0; 911 for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL; 912 p = q, q = LIST_NEXT(q, ipqe_q)) { 913 if (q->ipqe_ip->ip_off != next) 914 return (0); 915 next += q->ipqe_ip->ip_len; 916 } 917 if (p->ipqe_mff) 918 return (0); 919 920 /* 921 * Reassembly is complete. Check for a bogus message size and 922 * concatenate fragments. 923 */ 924 q = LIST_FIRST(&fp->ipq_fragq); 925 ip = q->ipqe_ip; 926 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 927 ipstat.ips_toolong++; 928 ip_freef(fp); 929 return (0); 930 } 931 m = q->ipqe_m; 932 t = m->m_next; 933 m->m_next = 0; 934 m_cat(m, t); 935 nq = LIST_NEXT(q, ipqe_q); 936 pool_put(&ipqent_pool, q); 937 for (q = nq; q != NULL; q = nq) { 938 t = q->ipqe_m; 939 nq = LIST_NEXT(q, ipqe_q); 940 pool_put(&ipqent_pool, q); 941 m_cat(m, t); 942 } 943 944 /* 945 * Create header for new ip packet by 946 * modifying header of first packet; 947 * dequeue and discard fragment reassembly header. 948 * Make header visible. 949 */ 950 ip->ip_len = next; 951 ip->ip_src = fp->ipq_src; 952 ip->ip_dst = fp->ipq_dst; 953 LIST_REMOVE(fp, ipq_q); 954 FREE(fp, M_FTABLE); 955 ip_nfragpackets--; 956 m->m_len += (ip->ip_hl << 2); 957 m->m_data -= (ip->ip_hl << 2); 958 /* some debugging cruft by sklower, below, will go away soon */ 959 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 960 int plen = 0; 961 for (t = m; t; t = t->m_next) 962 plen += t->m_len; 963 m->m_pkthdr.len = plen; 964 } 965 return (m); 966 967dropfrag: 968 ipstat.ips_fragdropped++; 969 m_freem(m); 970 pool_put(&ipqent_pool, ipqe); 971 return (0); 972} 973 974/* 975 * Free a fragment reassembly header and all 976 * associated datagrams. 977 */ 978void 979ip_freef(fp) 980 struct ipq *fp; 981{ 982 struct ipqent *q, *p; 983 984 IPQ_LOCK_CHECK(); 985 986 for (q = LIST_FIRST(&fp->ipq_fragq); q != NULL; q = p) { 987 p = LIST_NEXT(q, ipqe_q); 988 m_freem(q->ipqe_m); 989 LIST_REMOVE(q, ipqe_q); 990 pool_put(&ipqent_pool, q); 991 } 992 LIST_REMOVE(fp, ipq_q); 993 FREE(fp, M_FTABLE); 994 ip_nfragpackets--; 995} 996 997/* 998 * IP timer processing; 999 * if a timer expires on a reassembly 1000 * queue, discard it. 1001 */ 1002void 1003ip_slowtimo() 1004{ 1005 struct ipq *fp, *nfp; 1006 int s = splsoftnet(); 1007 1008 IPQ_LOCK(); 1009 for (fp = LIST_FIRST(&ipq); fp != NULL; fp = nfp) { 1010 nfp = LIST_NEXT(fp, ipq_q); 1011 if (--fp->ipq_ttl == 0) { 1012 ipstat.ips_fragtimeout++; 1013 ip_freef(fp); 1014 } 1015 } 1016 /* 1017 * If we are over the maximum number of fragments 1018 * (due to the limit being lowered), drain off 1019 * enough to get down to the new limit. 1020 */ 1021 if (ip_maxfragpackets < 0) 1022 ; 1023 else { 1024 while (ip_nfragpackets > ip_maxfragpackets && LIST_FIRST(&ipq)) 1025 ip_freef(LIST_FIRST(&ipq)); 1026 } 1027 IPQ_UNLOCK(); 1028#ifdef GATEWAY 1029 ipflow_slowtimo(); 1030#endif 1031 splx(s); 1032} 1033 1034/* 1035 * Drain off all datagram fragments. 1036 */ 1037void 1038ip_drain() 1039{ 1040 1041 /* 1042 * We may be called from a device's interrupt context. If 1043 * the ipq is already busy, just bail out now. 1044 */ 1045 if (ipq_lock_try() == 0) 1046 return; 1047 1048 while (LIST_FIRST(&ipq) != NULL) { 1049 ipstat.ips_fragdropped++; 1050 ip_freef(LIST_FIRST(&ipq)); 1051 } 1052 1053 IPQ_UNLOCK(); 1054} 1055 1056/* 1057 * Do option processing on a datagram, 1058 * possibly discarding it if bad options are encountered, 1059 * or forwarding it if source-routed. 1060 * Returns 1 if packet has been forwarded/freed, 1061 * 0 if the packet should be processed further. 1062 */ 1063int 1064ip_dooptions(m) 1065 struct mbuf *m; 1066{ 1067 struct ip *ip = mtod(m, struct ip *); 1068 u_char *cp, *cp0; 1069 struct ip_timestamp *ipt; 1070 struct in_ifaddr *ia; 1071 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1072 struct in_addr dst; 1073 n_time ntime; 1074 1075 dst = ip->ip_dst; 1076 cp = (u_char *)(ip + 1); 1077 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1078 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1079 opt = cp[IPOPT_OPTVAL]; 1080 if (opt == IPOPT_EOL) 1081 break; 1082 if (opt == IPOPT_NOP) 1083 optlen = 1; 1084 else { 1085 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1086 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1087 goto bad; 1088 } 1089 optlen = cp[IPOPT_OLEN]; 1090 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1091 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1092 goto bad; 1093 } 1094 } 1095 switch (opt) { 1096 1097 default: 1098 break; 1099 1100 /* 1101 * Source routing with record. 1102 * Find interface with current destination address. 1103 * If none on this machine then drop if strictly routed, 1104 * or do nothing if loosely routed. 1105 * Record interface address and bring up next address 1106 * component. If strictly routed make sure next 1107 * address is on directly accessible net. 1108 */ 1109 case IPOPT_LSRR: 1110 case IPOPT_SSRR: 1111 if (ip_allowsrcrt == 0) { 1112 type = ICMP_UNREACH; 1113 code = ICMP_UNREACH_NET_PROHIB; 1114 goto bad; 1115 } 1116 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1117 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1118 goto bad; 1119 } 1120 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1121 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1122 goto bad; 1123 } 1124 ipaddr.sin_addr = ip->ip_dst; 1125 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 1126 if (ia == 0) { 1127 if (opt == IPOPT_SSRR) { 1128 type = ICMP_UNREACH; 1129 code = ICMP_UNREACH_SRCFAIL; 1130 goto bad; 1131 } 1132 /* 1133 * Loose routing, and not at next destination 1134 * yet; nothing to do except forward. 1135 */ 1136 break; 1137 } 1138 off--; /* 0 origin */ 1139 if ((off + sizeof(struct in_addr)) > optlen) { 1140 /* 1141 * End of source route. Should be for us. 1142 */ 1143 save_rte(cp, ip->ip_src); 1144 break; 1145 } 1146 /* 1147 * locate outgoing interface 1148 */ 1149 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 1150 sizeof(ipaddr.sin_addr)); 1151 if (opt == IPOPT_SSRR) 1152 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 1153 else 1154 ia = ip_rtaddr(ipaddr.sin_addr); 1155 if (ia == 0) { 1156 type = ICMP_UNREACH; 1157 code = ICMP_UNREACH_SRCFAIL; 1158 goto bad; 1159 } 1160 ip->ip_dst = ipaddr.sin_addr; 1161 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1162 (caddr_t)(cp + off), sizeof(struct in_addr)); 1163 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1164 /* 1165 * Let ip_intr's mcast routing check handle mcast pkts 1166 */ 1167 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 1168 break; 1169 1170 case IPOPT_RR: 1171 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1172 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1173 goto bad; 1174 } 1175 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1176 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1177 goto bad; 1178 } 1179 /* 1180 * If no space remains, ignore. 1181 */ 1182 off--; /* 0 origin */ 1183 if ((off + sizeof(struct in_addr)) > optlen) 1184 break; 1185 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 1186 sizeof(ipaddr.sin_addr)); 1187 /* 1188 * locate outgoing interface; if we're the destination, 1189 * use the incoming interface (should be same). 1190 */ 1191 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))) 1192 == NULL && 1193 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 1194 type = ICMP_UNREACH; 1195 code = ICMP_UNREACH_HOST; 1196 goto bad; 1197 } 1198 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1199 (caddr_t)(cp + off), sizeof(struct in_addr)); 1200 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1201 break; 1202 1203 case IPOPT_TS: 1204 code = cp - (u_char *)ip; 1205 ipt = (struct ip_timestamp *)cp; 1206 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { 1207 code = (u_char *)&ipt->ipt_len - (u_char *)ip; 1208 goto bad; 1209 } 1210 if (ipt->ipt_ptr < 5) { 1211 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; 1212 goto bad; 1213 } 1214 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 1215 if (++ipt->ipt_oflw == 0) { 1216 code = (u_char *)&ipt->ipt_ptr - 1217 (u_char *)ip; 1218 goto bad; 1219 } 1220 break; 1221 } 1222 cp0 = (cp + ipt->ipt_ptr - 1); 1223 switch (ipt->ipt_flg) { 1224 1225 case IPOPT_TS_TSONLY: 1226 break; 1227 1228 case IPOPT_TS_TSANDADDR: 1229 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1230 sizeof(struct in_addr) > ipt->ipt_len) { 1231 code = (u_char *)&ipt->ipt_ptr - 1232 (u_char *)ip; 1233 goto bad; 1234 } 1235 ipaddr.sin_addr = dst; 1236 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr), 1237 m->m_pkthdr.rcvif)); 1238 if (ia == 0) 1239 continue; 1240 bcopy(&ia->ia_addr.sin_addr, 1241 cp0, sizeof(struct in_addr)); 1242 ipt->ipt_ptr += sizeof(struct in_addr); 1243 break; 1244 1245 case IPOPT_TS_PRESPEC: 1246 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1247 sizeof(struct in_addr) > ipt->ipt_len) { 1248 code = (u_char *)&ipt->ipt_ptr - 1249 (u_char *)ip; 1250 goto bad; 1251 } 1252 bcopy(cp0, &ipaddr.sin_addr, 1253 sizeof(struct in_addr)); 1254 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))) 1255 == NULL) 1256 continue; 1257 ipt->ipt_ptr += sizeof(struct in_addr); 1258 break; 1259 1260 default: 1261 /* XXX can't take &ipt->ipt_flg */ 1262 code = (u_char *)&ipt->ipt_ptr - 1263 (u_char *)ip + 1; 1264 goto bad; 1265 } 1266 ntime = iptime(); 1267 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */ 1268 bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1, 1269 sizeof(n_time)); 1270 ipt->ipt_ptr += sizeof(n_time); 1271 } 1272 } 1273 if (forward) { 1274 if (ip_forwsrcrt == 0) { 1275 type = ICMP_UNREACH; 1276 code = ICMP_UNREACH_SRCFAIL; 1277 goto bad; 1278 } 1279 ip_forward(m, 1); 1280 return (1); 1281 } 1282 return (0); 1283bad: 1284 icmp_error(m, type, code, 0, 0); 1285 ipstat.ips_badoptions++; 1286 return (1); 1287} 1288 1289/* 1290 * Given address of next destination (final or next hop), 1291 * return internet address info of interface to be used to get there. 1292 */ 1293struct in_ifaddr * 1294ip_rtaddr(dst) 1295 struct in_addr dst; 1296{ 1297 struct sockaddr_in *sin; 1298 1299 sin = satosin(&ipforward_rt.ro_dst); 1300 1301 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) { 1302 if (ipforward_rt.ro_rt) { 1303 RTFREE(ipforward_rt.ro_rt); 1304 ipforward_rt.ro_rt = 0; 1305 } 1306 sin->sin_family = AF_INET; 1307 sin->sin_len = sizeof(*sin); 1308 sin->sin_addr = dst; 1309 1310 rtalloc(&ipforward_rt); 1311 } 1312 if (ipforward_rt.ro_rt == 0) 1313 return ((struct in_ifaddr *)0); 1314 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1315} 1316 1317/* 1318 * Save incoming source route for use in replies, 1319 * to be picked up later by ip_srcroute if the receiver is interested. 1320 */ 1321void 1322save_rte(option, dst) 1323 u_char *option; 1324 struct in_addr dst; 1325{ 1326 unsigned olen; 1327 1328 olen = option[IPOPT_OLEN]; 1329#ifdef DIAGNOSTIC 1330 if (ipprintfs) 1331 printf("save_rte: olen %d\n", olen); 1332#endif /* 0 */ 1333 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1334 return; 1335 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 1336 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1337 ip_srcrt.dst = dst; 1338} 1339 1340/* 1341 * Retrieve incoming source route for use in replies, 1342 * in the same form used by setsockopt. 1343 * The first hop is placed before the options, will be removed later. 1344 */ 1345struct mbuf * 1346ip_srcroute() 1347{ 1348 struct in_addr *p, *q; 1349 struct mbuf *m; 1350 1351 if (ip_nhops == 0) 1352 return ((struct mbuf *)0); 1353 m = m_get(M_DONTWAIT, MT_SOOPTS); 1354 if (m == 0) 1355 return ((struct mbuf *)0); 1356 1357#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1358 1359 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1360 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1361 OPTSIZ; 1362#ifdef DIAGNOSTIC 1363 if (ipprintfs) 1364 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1365#endif 1366 1367 /* 1368 * First save first hop for return route 1369 */ 1370 p = &ip_srcrt.route[ip_nhops - 1]; 1371 *(mtod(m, struct in_addr *)) = *p--; 1372#ifdef DIAGNOSTIC 1373 if (ipprintfs) 1374 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1375#endif 1376 1377 /* 1378 * Copy option fields and padding (nop) to mbuf. 1379 */ 1380 ip_srcrt.nop = IPOPT_NOP; 1381 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1382 bcopy((caddr_t)&ip_srcrt.nop, 1383 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 1384 q = (struct in_addr *)(mtod(m, caddr_t) + 1385 sizeof(struct in_addr) + OPTSIZ); 1386#undef OPTSIZ 1387 /* 1388 * Record return path as an IP source route, 1389 * reversing the path (pointers are now aligned). 1390 */ 1391 while (p >= ip_srcrt.route) { 1392#ifdef DIAGNOSTIC 1393 if (ipprintfs) 1394 printf(" %x", ntohl(q->s_addr)); 1395#endif 1396 *q++ = *p--; 1397 } 1398 /* 1399 * Last hop goes to final destination. 1400 */ 1401 *q = ip_srcrt.dst; 1402#ifdef DIAGNOSTIC 1403 if (ipprintfs) 1404 printf(" %x\n", ntohl(q->s_addr)); 1405#endif 1406 return (m); 1407} 1408 1409/* 1410 * Strip out IP options, at higher 1411 * level protocol in the kernel. 1412 * Second argument is buffer to which options 1413 * will be moved, and return value is their length. 1414 * XXX should be deleted; last arg currently ignored. 1415 */ 1416void 1417ip_stripoptions(m, mopt) 1418 struct mbuf *m; 1419 struct mbuf *mopt; 1420{ 1421 int i; 1422 struct ip *ip = mtod(m, struct ip *); 1423 caddr_t opts; 1424 int olen; 1425 1426 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1427 opts = (caddr_t)(ip + 1); 1428 i = m->m_len - (sizeof (struct ip) + olen); 1429 bcopy(opts + olen, opts, (unsigned)i); 1430 m->m_len -= olen; 1431 if (m->m_flags & M_PKTHDR) 1432 m->m_pkthdr.len -= olen; 1433 ip->ip_len -= olen; 1434 ip->ip_hl = sizeof (struct ip) >> 2; 1435} 1436 1437const int inetctlerrmap[PRC_NCMDS] = { 1438 0, 0, 0, 0, 1439 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1440 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1441 EMSGSIZE, EHOSTUNREACH, 0, 0, 1442 0, 0, 0, 0, 1443 ENOPROTOOPT 1444}; 1445 1446/* 1447 * Forward a packet. If some error occurs return the sender 1448 * an icmp packet. Note we can't always generate a meaningful 1449 * icmp message because icmp doesn't have a large enough repertoire 1450 * of codes and types. 1451 * 1452 * If not forwarding, just drop the packet. This could be confusing 1453 * if ipforwarding was zero but some routing protocol was advancing 1454 * us as a gateway to somewhere. However, we must let the routing 1455 * protocol deal with that. 1456 * 1457 * The srcrt parameter indicates whether the packet is being forwarded 1458 * via a source route. 1459 */ 1460void 1461ip_forward(m, srcrt) 1462 struct mbuf *m; 1463 int srcrt; 1464{ 1465 struct ip *ip = mtod(m, struct ip *); 1466 struct sockaddr_in *sin; 1467 struct rtentry *rt; 1468 int error, type = 0, code = 0; 1469 struct mbuf *mcopy; 1470 n_long dest; 1471 struct ifnet *destifp; 1472#ifdef IPSEC 1473 struct ifnet dummyifp; 1474#endif 1475 1476 /* 1477 * Clear any in-bound checksum flags for this packet. 1478 */ 1479 m->m_pkthdr.csum_flags = 0; 1480 1481 dest = 0; 1482#ifdef DIAGNOSTIC 1483 if (ipprintfs) 1484 printf("forward: src %2.2x dst %2.2x ttl %x\n", 1485 ntohl(ip->ip_src.s_addr), 1486 ntohl(ip->ip_dst.s_addr), ip->ip_ttl); 1487#endif 1488 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1489 ipstat.ips_cantforward++; 1490 m_freem(m); 1491 return; 1492 } 1493 if (ip->ip_ttl <= IPTTLDEC) { 1494 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1495 return; 1496 } 1497 ip->ip_ttl -= IPTTLDEC; 1498 1499 sin = satosin(&ipforward_rt.ro_dst); 1500 if ((rt = ipforward_rt.ro_rt) == 0 || 1501 !in_hosteq(ip->ip_dst, sin->sin_addr)) { 1502 if (ipforward_rt.ro_rt) { 1503 RTFREE(ipforward_rt.ro_rt); 1504 ipforward_rt.ro_rt = 0; 1505 } 1506 sin->sin_family = AF_INET; 1507 sin->sin_len = sizeof(struct sockaddr_in); 1508 sin->sin_addr = ip->ip_dst; 1509 1510 rtalloc(&ipforward_rt); 1511 if (ipforward_rt.ro_rt == 0) { 1512 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1513 return; 1514 } 1515 rt = ipforward_rt.ro_rt; 1516 } 1517 1518 /* 1519 * Save at most 68 bytes of the packet in case 1520 * we need to generate an ICMP message to the src. 1521 * Pullup to avoid sharing mbuf cluster between m and mcopy. 1522 */ 1523 mcopy = m_copym(m, 0, imin((int)ip->ip_len, 68), M_DONTWAIT); 1524 if (mcopy) 1525 mcopy = m_pullup(mcopy, ip->ip_hl << 2); 1526 1527 /* 1528 * If forwarding packet using same interface that it came in on, 1529 * perhaps should send a redirect to sender to shortcut a hop. 1530 * Only send redirect if source is sending directly to us, 1531 * and if packet was not source routed (or has any options). 1532 * Also, don't send redirect if forwarding using a default route 1533 * or a route modified by a redirect. 1534 */ 1535 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1536 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1537 !in_nullhost(satosin(rt_key(rt))->sin_addr) && 1538 ipsendredirects && !srcrt) { 1539 if (rt->rt_ifa && 1540 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1541 ifatoia(rt->rt_ifa)->ia_subnet) { 1542 if (rt->rt_flags & RTF_GATEWAY) 1543 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1544 else 1545 dest = ip->ip_dst.s_addr; 1546 /* 1547 * Router requirements says to only send host 1548 * redirects. 1549 */ 1550 type = ICMP_REDIRECT; 1551 code = ICMP_REDIRECT_HOST; 1552#ifdef DIAGNOSTIC 1553 if (ipprintfs) 1554 printf("redirect (%d) to %x\n", code, 1555 (u_int32_t)dest); 1556#endif 1557 } 1558 } 1559 1560#ifdef IPSEC 1561 /* Don't lookup socket in forwarding case */ 1562 (void)ipsec_setsocket(m, NULL); 1563#endif 1564 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1565 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0); 1566 if (error) 1567 ipstat.ips_cantforward++; 1568 else { 1569 ipstat.ips_forward++; 1570 if (type) 1571 ipstat.ips_redirectsent++; 1572 else { 1573 if (mcopy) { 1574#ifdef GATEWAY 1575 if (mcopy->m_flags & M_CANFASTFWD) 1576 ipflow_create(&ipforward_rt, mcopy); 1577#endif 1578 m_freem(mcopy); 1579 } 1580 return; 1581 } 1582 } 1583 if (mcopy == NULL) 1584 return; 1585 destifp = NULL; 1586 1587 switch (error) { 1588 1589 case 0: /* forwarded, but need redirect */ 1590 /* type, code set above */ 1591 break; 1592 1593 case ENETUNREACH: /* shouldn't happen, checked above */ 1594 case EHOSTUNREACH: 1595 case ENETDOWN: 1596 case EHOSTDOWN: 1597 default: 1598 type = ICMP_UNREACH; 1599 code = ICMP_UNREACH_HOST; 1600 break; 1601 1602 case EMSGSIZE: 1603 type = ICMP_UNREACH; 1604 code = ICMP_UNREACH_NEEDFRAG; 1605#ifndef IPSEC 1606 if (ipforward_rt.ro_rt) 1607 destifp = ipforward_rt.ro_rt->rt_ifp; 1608#else 1609 /* 1610 * If the packet is routed over IPsec tunnel, tell the 1611 * originator the tunnel MTU. 1612 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1613 * XXX quickhack!!! 1614 */ 1615 if (ipforward_rt.ro_rt) { 1616 struct secpolicy *sp; 1617 int ipsecerror; 1618 size_t ipsechdr; 1619 struct route *ro; 1620 1621 sp = ipsec4_getpolicybyaddr(mcopy, 1622 IPSEC_DIR_OUTBOUND, 1623 IP_FORWARDING, 1624 &ipsecerror); 1625 1626 if (sp == NULL) 1627 destifp = ipforward_rt.ro_rt->rt_ifp; 1628 else { 1629 /* count IPsec header size */ 1630 ipsechdr = ipsec4_hdrsiz(mcopy, 1631 IPSEC_DIR_OUTBOUND, 1632 NULL); 1633 1634 /* 1635 * find the correct route for outer IPv4 1636 * header, compute tunnel MTU. 1637 * 1638 * XXX BUG ALERT 1639 * The "dummyifp" code relies upon the fact 1640 * that icmp_error() touches only ifp->if_mtu. 1641 */ 1642 /*XXX*/ 1643 destifp = NULL; 1644 if (sp->req != NULL 1645 && sp->req->sav != NULL 1646 && sp->req->sav->sah != NULL) { 1647 ro = &sp->req->sav->sah->sa_route; 1648 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1649 dummyifp.if_mtu = 1650 ro->ro_rt->rt_ifp->if_mtu; 1651 dummyifp.if_mtu -= ipsechdr; 1652 destifp = &dummyifp; 1653 } 1654 } 1655 1656 key_freesp(sp); 1657 } 1658 } 1659#endif /*IPSEC*/ 1660 ipstat.ips_cantfrag++; 1661 break; 1662 1663 case ENOBUFS: 1664 type = ICMP_SOURCEQUENCH; 1665 code = 0; 1666 break; 1667 } 1668 icmp_error(mcopy, type, code, dest, destifp); 1669} 1670 1671void 1672ip_savecontrol(inp, mp, ip, m) 1673 struct inpcb *inp; 1674 struct mbuf **mp; 1675 struct ip *ip; 1676 struct mbuf *m; 1677{ 1678 1679 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1680 struct timeval tv; 1681 1682 microtime(&tv); 1683 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1684 SCM_TIMESTAMP, SOL_SOCKET); 1685 if (*mp) 1686 mp = &(*mp)->m_next; 1687 } 1688 if (inp->inp_flags & INP_RECVDSTADDR) { 1689 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1690 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1691 if (*mp) 1692 mp = &(*mp)->m_next; 1693 } 1694#ifdef notyet 1695 /* 1696 * XXX 1697 * Moving these out of udp_input() made them even more broken 1698 * than they already were. 1699 * - fenner@parc.xerox.com 1700 */ 1701 /* options were tossed already */ 1702 if (inp->inp_flags & INP_RECVOPTS) { 1703 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1704 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1705 if (*mp) 1706 mp = &(*mp)->m_next; 1707 } 1708 /* ip_srcroute doesn't do what we want here, need to fix */ 1709 if (inp->inp_flags & INP_RECVRETOPTS) { 1710 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1711 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1712 if (*mp) 1713 mp = &(*mp)->m_next; 1714 } 1715#endif 1716 if (inp->inp_flags & INP_RECVIF) { 1717 struct sockaddr_dl sdl; 1718 1719 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1720 sdl.sdl_family = AF_LINK; 1721 sdl.sdl_index = m->m_pkthdr.rcvif ? 1722 m->m_pkthdr.rcvif->if_index : 0; 1723 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1724 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1725 IP_RECVIF, IPPROTO_IP); 1726 if (*mp) 1727 mp = &(*mp)->m_next; 1728 } 1729} 1730 1731int 1732ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 1733 int *name; 1734 u_int namelen; 1735 void *oldp; 1736 size_t *oldlenp; 1737 void *newp; 1738 size_t newlen; 1739{ 1740 extern int subnetsarelocal, hostzeroisbroadcast; 1741 1742 int error, old; 1743 1744 /* All sysctl names at this level are terminal. */ 1745 if (namelen != 1) 1746 return (ENOTDIR); 1747 1748 switch (name[0]) { 1749 case IPCTL_FORWARDING: 1750 return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding)); 1751 case IPCTL_SENDREDIRECTS: 1752 return (sysctl_int(oldp, oldlenp, newp, newlen, 1753 &ipsendredirects)); 1754 case IPCTL_DEFTTL: 1755 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl)); 1756#ifdef notyet 1757 case IPCTL_DEFMTU: 1758 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 1759#endif 1760 case IPCTL_FORWSRCRT: 1761 /* Don't allow this to change in a secure environment. */ 1762 if (securelevel > 0) 1763 return (sysctl_rdint(oldp, oldlenp, newp, 1764 ip_forwsrcrt)); 1765 else 1766 return (sysctl_int(oldp, oldlenp, newp, newlen, 1767 &ip_forwsrcrt)); 1768 case IPCTL_DIRECTEDBCAST: 1769 return (sysctl_int(oldp, oldlenp, newp, newlen, 1770 &ip_directedbcast)); 1771 case IPCTL_ALLOWSRCRT: 1772 return (sysctl_int(oldp, oldlenp, newp, newlen, 1773 &ip_allowsrcrt)); 1774 case IPCTL_SUBNETSARELOCAL: 1775 return (sysctl_int(oldp, oldlenp, newp, newlen, 1776 &subnetsarelocal)); 1777 case IPCTL_MTUDISC: 1778 error = sysctl_int(oldp, oldlenp, newp, newlen, 1779 &ip_mtudisc); 1780 if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) { 1781 ip_mtudisc_timeout_q = 1782 rt_timer_queue_create(ip_mtudisc_timeout); 1783 } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) { 1784 rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE); 1785 ip_mtudisc_timeout_q = NULL; 1786 } 1787 return error; 1788 case IPCTL_ANONPORTMIN: 1789 old = anonportmin; 1790 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin); 1791 if (anonportmin >= anonportmax || anonportmin < 0 1792 || anonportmin > 65535 1793#ifndef IPNOPRIVPORTS 1794 || anonportmin < IPPORT_RESERVED 1795#endif 1796 ) { 1797 anonportmin = old; 1798 return (EINVAL); 1799 } 1800 return (error); 1801 case IPCTL_ANONPORTMAX: 1802 old = anonportmax; 1803 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax); 1804 if (anonportmin >= anonportmax || anonportmax < 0 1805 || anonportmax > 65535 1806#ifndef IPNOPRIVPORTS 1807 || anonportmax < IPPORT_RESERVED 1808#endif 1809 ) { 1810 anonportmax = old; 1811 return (EINVAL); 1812 } 1813 return (error); 1814 case IPCTL_MTUDISCTIMEOUT: 1815 error = sysctl_int(oldp, oldlenp, newp, newlen, 1816 &ip_mtudisc_timeout); 1817 if (ip_mtudisc_timeout_q != NULL) 1818 rt_timer_queue_change(ip_mtudisc_timeout_q, 1819 ip_mtudisc_timeout); 1820 return (error); 1821#ifdef GATEWAY 1822 case IPCTL_MAXFLOWS: 1823 { 1824 int s; 1825 1826 error = sysctl_int(oldp, oldlenp, newp, newlen, 1827 &ip_maxflows); 1828 s = splsoftnet(); 1829 ipflow_reap(0); 1830 splx(s); 1831 return (error); 1832 } 1833#endif 1834 case IPCTL_HOSTZEROBROADCAST: 1835 return (sysctl_int(oldp, oldlenp, newp, newlen, 1836 &hostzeroisbroadcast)); 1837#if NGIF > 0 1838 case IPCTL_GIF_TTL: 1839 return(sysctl_int(oldp, oldlenp, newp, newlen, 1840 &ip_gif_ttl)); 1841#endif 1842 1843#ifndef IPNOPRIVPORTS 1844 case IPCTL_LOWPORTMIN: 1845 old = lowportmin; 1846 error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmin); 1847 if (lowportmin >= lowportmax 1848 || lowportmin > IPPORT_RESERVEDMAX 1849 || lowportmin < IPPORT_RESERVEDMIN 1850 ) { 1851 lowportmin = old; 1852 return (EINVAL); 1853 } 1854 return (error); 1855 case IPCTL_LOWPORTMAX: 1856 old = lowportmax; 1857 error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmax); 1858 if (lowportmin >= lowportmax 1859 || lowportmax > IPPORT_RESERVEDMAX 1860 || lowportmax < IPPORT_RESERVEDMIN 1861 ) { 1862 lowportmax = old; 1863 return (EINVAL); 1864 } 1865 return (error); 1866#endif 1867 1868 case IPCTL_MAXFRAGPACKETS: 1869 return (sysctl_int(oldp, oldlenp, newp, newlen, 1870 &ip_maxfragpackets)); 1871 1872 default: 1873 return (EOPNOTSUPP); 1874 } 1875 /* NOTREACHED */ 1876} 1877