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