1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 63 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.52 2003/03/07 07:01:28 silby Exp $ 64 */ 65 66 #define _IP_VHL 67 68 #include "opt_bootp.h" 69 #include "opt_ipdn.h" 70 #include "opt_ipdivert.h" 71 #include "opt_ipstealth.h" 72 #include "opt_ipsec.h" 73 #include "opt_rss.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/mbuf.h> 78 #include <sys/malloc.h> 79 #include <sys/mpipe.h> 80 #include <sys/domain.h> 81 #include <sys/protosw.h> 82 #include <sys/socket.h> 83 #include <sys/time.h> 84 #include <sys/globaldata.h> 85 #include <sys/thread.h> 86 #include <sys/kernel.h> 87 #include <sys/syslog.h> 88 #include <sys/sysctl.h> 89 #include <sys/in_cksum.h> 90 #include <sys/lock.h> 91 92 #include <sys/mplock2.h> 93 94 #include <machine/stdarg.h> 95 96 #include <net/if.h> 97 #include <net/if_types.h> 98 #include <net/if_var.h> 99 #include <net/if_dl.h> 100 #include <net/pfil.h> 101 #include <net/route.h> 102 #include <net/netisr2.h> 103 104 #include <netinet/in.h> 105 #include <netinet/in_systm.h> 106 #include <netinet/in_var.h> 107 #include <netinet/ip.h> 108 #include <netinet/in_pcb.h> 109 #include <netinet/ip_var.h> 110 #include <netinet/ip_icmp.h> 111 #include <netinet/ip_divert.h> 112 #include <netinet/ip_flow.h> 113 114 #include <sys/thread2.h> 115 #include <sys/msgport2.h> 116 #include <net/netmsg2.h> 117 118 #include <sys/socketvar.h> 119 120 #include <net/ipfw/ip_fw.h> 121 #include <net/dummynet/ip_dummynet.h> 122 123 #ifdef IPSEC 124 #include <netinet6/ipsec.h> 125 #include <netproto/key/key.h> 126 #endif 127 128 #ifdef FAST_IPSEC 129 #include <netproto/ipsec/ipsec.h> 130 #include <netproto/ipsec/key.h> 131 #endif 132 133 int rsvp_on = 0; 134 static int ip_rsvp_on; 135 struct socket *ip_rsvpd; 136 137 int ipforwarding = 0; 138 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 139 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 140 141 static int ipsendredirects = 1; /* XXX */ 142 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 143 &ipsendredirects, 0, "Enable sending IP redirects"); 144 145 int ip_defttl = IPDEFTTL; 146 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 147 &ip_defttl, 0, "Maximum TTL on IP packets"); 148 149 static int ip_dosourceroute = 0; 150 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 151 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 152 153 static int ip_acceptsourceroute = 0; 154 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 155 CTLFLAG_RW, &ip_acceptsourceroute, 0, 156 "Enable accepting source routed IP packets"); 157 158 static int ip_keepfaith = 0; 159 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 160 &ip_keepfaith, 0, 161 "Enable packet capture for FAITH IPv4->IPv6 translator daemon"); 162 163 static int nipq = 0; /* total # of reass queues */ 164 static int maxnipq; 165 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW, 166 &maxnipq, 0, 167 "Maximum number of IPv4 fragment reassembly queue entries"); 168 169 static int maxfragsperpacket; 170 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 171 &maxfragsperpacket, 0, 172 "Maximum number of IPv4 fragments allowed per packet"); 173 174 static int ip_sendsourcequench = 0; 175 SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 176 &ip_sendsourcequench, 0, 177 "Enable the transmission of source quench packets"); 178 179 int ip_do_randomid = 1; 180 SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, 181 &ip_do_randomid, 0, 182 "Assign random ip_id values"); 183 /* 184 * XXX - Setting ip_checkinterface mostly implements the receive side of 185 * the Strong ES model described in RFC 1122, but since the routing table 186 * and transmit implementation do not implement the Strong ES model, 187 * setting this to 1 results in an odd hybrid. 188 * 189 * XXX - ip_checkinterface currently must be disabled if you use ipnat 190 * to translate the destination address to another local interface. 191 * 192 * XXX - ip_checkinterface must be disabled if you add IP aliases 193 * to the loopback interface instead of the interface where the 194 * packets for those addresses are received. 195 */ 196 static int ip_checkinterface = 0; 197 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 198 &ip_checkinterface, 0, "Verify packet arrives on correct interface"); 199 200 static u_long ip_hash_count = 0; 201 SYSCTL_ULONG(_net_inet_ip, OID_AUTO, hash_count, CTLFLAG_RD, 202 &ip_hash_count, 0, "Number of packets hashed by IP"); 203 204 #ifdef RSS_DEBUG 205 static u_long ip_rehash_count = 0; 206 SYSCTL_ULONG(_net_inet_ip, OID_AUTO, rehash_count, CTLFLAG_RD, 207 &ip_rehash_count, 0, "Number of packets rehashed by IP"); 208 209 static u_long ip_dispatch_fast = 0; 210 SYSCTL_ULONG(_net_inet_ip, OID_AUTO, dispatch_fast_count, CTLFLAG_RD, 211 &ip_dispatch_fast, 0, "Number of packets handled on current CPU"); 212 213 static u_long ip_dispatch_slow = 0; 214 SYSCTL_ULONG(_net_inet_ip, OID_AUTO, dispatch_slow_count, CTLFLAG_RD, 215 &ip_dispatch_slow, 0, "Number of packets messaged to another CPU"); 216 #endif 217 218 static struct lwkt_token ipq_token = LWKT_TOKEN_INITIALIZER(ipq_token); 219 220 #ifdef DIAGNOSTIC 221 static int ipprintfs = 0; 222 #endif 223 224 extern struct domain inetdomain; 225 extern struct protosw inetsw[]; 226 u_char ip_protox[IPPROTO_MAX]; 227 struct in_ifaddrhead in_ifaddrheads[MAXCPU]; /* first inet address */ 228 struct in_ifaddrhashhead *in_ifaddrhashtbls[MAXCPU]; 229 /* inet addr hash table */ 230 u_long in_ifaddrhmask; /* mask for hash table */ 231 232 static struct mbuf *ipforward_mtemp[MAXCPU]; 233 234 struct ip_stats ipstats_percpu[MAXCPU] __cachealign; 235 236 static int 237 sysctl_ipstats(SYSCTL_HANDLER_ARGS) 238 { 239 int cpu, error = 0; 240 241 for (cpu = 0; cpu < ncpus; ++cpu) { 242 if ((error = SYSCTL_OUT(req, &ipstats_percpu[cpu], 243 sizeof(struct ip_stats)))) 244 break; 245 if ((error = SYSCTL_IN(req, &ipstats_percpu[cpu], 246 sizeof(struct ip_stats)))) 247 break; 248 } 249 250 return (error); 251 } 252 SYSCTL_PROC(_net_inet_ip, IPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW), 253 0, 0, sysctl_ipstats, "S,ip_stats", "IP statistics"); 254 255 /* Packet reassembly stuff */ 256 #define IPREASS_NHASH_LOG2 6 257 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 258 #define IPREASS_HMASK (IPREASS_NHASH - 1) 259 #define IPREASS_HASH(x,y) \ 260 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 261 262 static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH]; 263 264 #ifdef IPCTL_DEFMTU 265 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 266 &ip_mtu, 0, "Default MTU"); 267 #endif 268 269 #ifdef IPSTEALTH 270 static int ipstealth = 0; 271 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, &ipstealth, 0, ""); 272 #else 273 static const int ipstealth = 0; 274 #endif 275 276 struct mbuf *(*ip_divert_p)(struct mbuf *, int, int); 277 278 struct pfil_head inet_pfil_hook; 279 280 /* 281 * struct ip_srcrt_opt is used to store packet state while it travels 282 * through the stack. 283 * 284 * XXX Note that the code even makes assumptions on the size and 285 * alignment of fields inside struct ip_srcrt so e.g. adding some 286 * fields will break the code. This needs to be fixed. 287 * 288 * We need to save the IP options in case a protocol wants to respond 289 * to an incoming packet over the same route if the packet got here 290 * using IP source routing. This allows connection establishment and 291 * maintenance when the remote end is on a network that is not known 292 * to us. 293 */ 294 struct ip_srcrt { 295 struct in_addr dst; /* final destination */ 296 char nop; /* one NOP to align */ 297 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 298 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 299 }; 300 301 struct ip_srcrt_opt { 302 int ip_nhops; 303 struct ip_srcrt ip_srcrt; 304 }; 305 306 static MALLOC_DEFINE(M_IPQ, "ipq", "IP Fragment Management"); 307 static struct malloc_pipe ipq_mpipe; 308 309 static void save_rte(struct mbuf *, u_char *, struct in_addr); 310 static int ip_dooptions(struct mbuf *m, int, struct sockaddr_in *); 311 static void ip_freef(struct ipqhead *, struct ipq *); 312 static void ip_input_handler(netmsg_t); 313 314 /* 315 * IP initialization: fill in IP protocol switch table. 316 * All protocols not implemented in kernel go to raw IP protocol handler. 317 */ 318 void 319 ip_init(void) 320 { 321 struct protosw *pr; 322 int i; 323 int cpu; 324 325 /* 326 * Make sure we can handle a reasonable number of fragments but 327 * cap it at 4000 (XXX). 328 */ 329 mpipe_init(&ipq_mpipe, M_IPQ, sizeof(struct ipq), 330 IFQ_MAXLEN, 4000, 0, NULL, NULL, NULL); 331 for (i = 0; i < ncpus; ++i) { 332 TAILQ_INIT(&in_ifaddrheads[i]); 333 in_ifaddrhashtbls[i] = 334 hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask); 335 } 336 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 337 if (pr == NULL) 338 panic("ip_init"); 339 for (i = 0; i < IPPROTO_MAX; i++) 340 ip_protox[i] = pr - inetsw; 341 for (pr = inetdomain.dom_protosw; 342 pr < inetdomain.dom_protoswNPROTOSW; pr++) { 343 if (pr->pr_domain->dom_family == PF_INET && pr->pr_protocol) { 344 if (pr->pr_protocol != IPPROTO_RAW) 345 ip_protox[pr->pr_protocol] = pr - inetsw; 346 } 347 } 348 349 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 350 inet_pfil_hook.ph_af = AF_INET; 351 if ((i = pfil_head_register(&inet_pfil_hook)) != 0) { 352 kprintf("%s: WARNING: unable to register pfil hook, " 353 "error %d\n", __func__, i); 354 } 355 356 for (i = 0; i < IPREASS_NHASH; i++) 357 TAILQ_INIT(&ipq[i]); 358 359 maxnipq = nmbclusters / 32; 360 maxfragsperpacket = 16; 361 362 ip_id = time_second & 0xffff; /* time_second survives reboots */ 363 364 for (cpu = 0; cpu < ncpus; ++cpu) { 365 /* 366 * Initialize IP statistics counters for each CPU. 367 */ 368 bzero(&ipstats_percpu[cpu], sizeof(struct ip_stats)); 369 370 /* 371 * Preallocate mbuf template for forwarding 372 */ 373 MGETHDR(ipforward_mtemp[cpu], MB_WAIT, MT_DATA); 374 } 375 376 netisr_register(NETISR_IP, ip_input_handler, ip_hashfn_in); 377 netisr_register_hashcheck(NETISR_IP, ip_hashcheck); 378 } 379 380 /* Do transport protocol processing. */ 381 static void 382 transport_processing_oncpu(struct mbuf *m, int hlen, struct ip *ip) 383 { 384 const struct protosw *pr = &inetsw[ip_protox[ip->ip_p]]; 385 386 /* 387 * Switch out to protocol's input routine. 388 */ 389 PR_GET_MPLOCK(pr); 390 pr->pr_input(&m, &hlen, ip->ip_p); 391 PR_REL_MPLOCK(pr); 392 } 393 394 static void 395 transport_processing_handler(netmsg_t msg) 396 { 397 struct netmsg_packet *pmsg = &msg->packet; 398 struct ip *ip; 399 int hlen; 400 401 ip = mtod(pmsg->nm_packet, struct ip *); 402 hlen = pmsg->base.lmsg.u.ms_result; 403 404 transport_processing_oncpu(pmsg->nm_packet, hlen, ip); 405 /* msg was embedded in the mbuf, do not reply! */ 406 } 407 408 static void 409 ip_input_handler(netmsg_t msg) 410 { 411 ip_input(msg->packet.nm_packet); 412 /* msg was embedded in the mbuf, do not reply! */ 413 } 414 415 /* 416 * IP input routine. Checksum and byte swap header. If fragmented 417 * try to reassemble. Process options. Pass to next level. 418 */ 419 void 420 ip_input(struct mbuf *m) 421 { 422 struct ip *ip; 423 struct in_ifaddr *ia = NULL; 424 struct in_ifaddr_container *iac; 425 int hlen, checkif; 426 u_short sum; 427 struct in_addr pkt_dst; 428 boolean_t using_srcrt = FALSE; /* forward (by PFIL_HOOKS) */ 429 struct in_addr odst; /* original dst address(NAT) */ 430 struct m_tag *mtag; 431 struct sockaddr_in *next_hop = NULL; 432 lwkt_port_t port; 433 #ifdef FAST_IPSEC 434 struct tdb_ident *tdbi; 435 struct secpolicy *sp; 436 int error; 437 #endif 438 439 M_ASSERTPKTHDR(m); 440 441 /* 442 * This routine is called from numerous places which may not have 443 * characterized the packet. 444 */ 445 ip = mtod(m, struct ip *); 446 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 447 ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) { 448 /* 449 * Force hash recalculation for fragments and multicast 450 * packets; hardware may not do it correctly. 451 * XXX add flag to indicate the hash is from hardware 452 */ 453 m->m_flags &= ~M_HASH; 454 } 455 if ((m->m_flags & M_HASH) == 0) { 456 ip_hashfn(&m, 0, IP_MPORT_IN); 457 if (m == NULL) 458 return; 459 KKASSERT(m->m_flags & M_HASH); 460 461 if (&curthread->td_msgport != 462 netisr_hashport(m->m_pkthdr.hash)) { 463 netisr_queue(NETISR_IP, m); 464 /* Requeued to other netisr msgport; done */ 465 return; 466 } 467 468 /* mbuf could have been changed */ 469 ip = mtod(m, struct ip *); 470 } 471 472 /* 473 * Pull out certain tags 474 */ 475 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 476 /* Next hop */ 477 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 478 KKASSERT(mtag != NULL); 479 next_hop = m_tag_data(mtag); 480 } 481 482 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 483 /* dummynet already filtered us */ 484 ip = mtod(m, struct ip *); 485 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 486 goto iphack; 487 } 488 489 ipstat.ips_total++; 490 491 /* length checks already done in ip_hashfn() */ 492 KASSERT(m->m_len >= sizeof(struct ip), ("IP header not in one mbuf")); 493 494 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 495 ipstat.ips_badvers++; 496 goto bad; 497 } 498 499 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 500 /* length checks already done in ip_hashfn() */ 501 KASSERT(hlen >= sizeof(struct ip), ("IP header len too small")); 502 KASSERT(m->m_len >= hlen, ("complete IP header not in one mbuf")); 503 504 /* 127/8 must not appear on wire - RFC1122 */ 505 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 506 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 507 if (!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) { 508 ipstat.ips_badaddr++; 509 goto bad; 510 } 511 } 512 513 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 514 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 515 } else { 516 if (hlen == sizeof(struct ip)) 517 sum = in_cksum_hdr(ip); 518 else 519 sum = in_cksum(m, hlen); 520 } 521 if (sum != 0) { 522 ipstat.ips_badsum++; 523 goto bad; 524 } 525 526 #ifdef ALTQ 527 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) { 528 /* packet is dropped by traffic conditioner */ 529 return; 530 } 531 #endif 532 /* 533 * Convert fields to host representation. 534 */ 535 ip->ip_len = ntohs(ip->ip_len); 536 ip->ip_off = ntohs(ip->ip_off); 537 538 /* length checks already done in ip_hashfn() */ 539 KASSERT(ip->ip_len >= hlen, ("total length less then header length")); 540 KASSERT(m->m_pkthdr.len >= ip->ip_len, ("mbuf too short")); 541 542 /* 543 * Trim mbufs if longer than the IP header would have us expect. 544 */ 545 if (m->m_pkthdr.len > ip->ip_len) { 546 if (m->m_len == m->m_pkthdr.len) { 547 m->m_len = ip->ip_len; 548 m->m_pkthdr.len = ip->ip_len; 549 } else { 550 m_adj(m, ip->ip_len - m->m_pkthdr.len); 551 } 552 } 553 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF) 554 /* 555 * Bypass packet filtering for packets from a tunnel (gif). 556 */ 557 if (ipsec_gethist(m, NULL)) 558 goto pass; 559 #endif 560 561 /* 562 * IpHack's section. 563 * Right now when no processing on packet has done 564 * and it is still fresh out of network we do our black 565 * deals with it. 566 * - Firewall: deny/allow/divert 567 * - Xlate: translate packet's addr/port (NAT). 568 * - Pipe: pass pkt through dummynet. 569 * - Wrap: fake packet's addr/port <unimpl.> 570 * - Encapsulate: put it in another IP and send out. <unimp.> 571 */ 572 573 iphack: 574 /* 575 * If we've been forwarded from the output side, then 576 * skip the firewall a second time 577 */ 578 if (next_hop != NULL) 579 goto ours; 580 581 /* No pfil hooks */ 582 if (!pfil_has_hooks(&inet_pfil_hook)) { 583 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 584 /* 585 * Strip dummynet tags from stranded packets 586 */ 587 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 588 KKASSERT(mtag != NULL); 589 m_tag_delete(m, mtag); 590 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; 591 } 592 goto pass; 593 } 594 595 /* 596 * Run through list of hooks for input packets. 597 * 598 * NOTE! If the packet is rewritten pf/ipfw/whoever must 599 * clear M_HASH. 600 */ 601 odst = ip->ip_dst; 602 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN)) 603 return; 604 if (m == NULL) /* consumed by filter */ 605 return; 606 ip = mtod(m, struct ip *); 607 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 608 using_srcrt = (odst.s_addr != ip->ip_dst.s_addr); 609 610 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 611 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 612 KKASSERT(mtag != NULL); 613 next_hop = m_tag_data(mtag); 614 } 615 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 616 ip_dn_queue(m); 617 return; 618 } 619 if (m->m_pkthdr.fw_flags & FW_MBUF_REDISPATCH) { 620 m->m_pkthdr.fw_flags &= ~FW_MBUF_REDISPATCH; 621 } 622 pass: 623 /* 624 * Process options and, if not destined for us, 625 * ship it on. ip_dooptions returns 1 when an 626 * error was detected (causing an icmp message 627 * to be sent and the original packet to be freed). 628 */ 629 if (hlen > sizeof(struct ip) && ip_dooptions(m, 0, next_hop)) 630 return; 631 632 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 633 * matter if it is destined to another node, or whether it is 634 * a multicast one, RSVP wants it! and prevents it from being forwarded 635 * anywhere else. Also checks if the rsvp daemon is running before 636 * grabbing the packet. 637 */ 638 if (rsvp_on && ip->ip_p == IPPROTO_RSVP) 639 goto ours; 640 641 /* 642 * Check our list of addresses, to see if the packet is for us. 643 * If we don't have any addresses, assume any unicast packet 644 * we receive might be for us (and let the upper layers deal 645 * with it). 646 */ 647 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid]) && 648 !(m->m_flags & (M_MCAST | M_BCAST))) 649 goto ours; 650 651 /* 652 * Cache the destination address of the packet; this may be 653 * changed by use of 'ipfw fwd'. 654 */ 655 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 656 657 /* 658 * Enable a consistency check between the destination address 659 * and the arrival interface for a unicast packet (the RFC 1122 660 * strong ES model) if IP forwarding is disabled and the packet 661 * is not locally generated and the packet is not subject to 662 * 'ipfw fwd'. 663 * 664 * XXX - Checking also should be disabled if the destination 665 * address is ipnat'ed to a different interface. 666 * 667 * XXX - Checking is incompatible with IP aliases added 668 * to the loopback interface instead of the interface where 669 * the packets are received. 670 */ 671 checkif = ip_checkinterface && 672 !ipforwarding && 673 m->m_pkthdr.rcvif != NULL && 674 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) && 675 next_hop == NULL; 676 677 /* 678 * Check for exact addresses in the hash bucket. 679 */ 680 LIST_FOREACH(iac, INADDR_HASH(pkt_dst.s_addr), ia_hash) { 681 ia = iac->ia; 682 683 /* 684 * If the address matches, verify that the packet 685 * arrived via the correct interface if checking is 686 * enabled. 687 */ 688 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && 689 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 690 goto ours; 691 } 692 ia = NULL; 693 694 /* 695 * Check for broadcast addresses. 696 * 697 * Only accept broadcast packets that arrive via the matching 698 * interface. Reception of forwarded directed broadcasts would 699 * be handled via ip_forward() and ether_output() with the loopback 700 * into the stack for SIMPLEX interfaces handled by ether_output(). 701 */ 702 if (m->m_pkthdr.rcvif != NULL && 703 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 704 struct ifaddr_container *ifac; 705 706 TAILQ_FOREACH(ifac, &m->m_pkthdr.rcvif->if_addrheads[mycpuid], 707 ifa_link) { 708 struct ifaddr *ifa = ifac->ifa; 709 710 if (ifa->ifa_addr == NULL) /* shutdown/startup race */ 711 continue; 712 if (ifa->ifa_addr->sa_family != AF_INET) 713 continue; 714 ia = ifatoia(ifa); 715 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 716 pkt_dst.s_addr) 717 goto ours; 718 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr) 719 goto ours; 720 #ifdef BOOTP_COMPAT 721 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 722 goto ours; 723 #endif 724 } 725 } 726 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 727 struct in_multi *inm; 728 729 if (ip_mrouter != NULL) { 730 /* XXX Multicast routing is not MPSAFE yet */ 731 get_mplock(); 732 733 /* 734 * If we are acting as a multicast router, all 735 * incoming multicast packets are passed to the 736 * kernel-level multicast forwarding function. 737 * The packet is returned (relatively) intact; if 738 * ip_mforward() returns a non-zero value, the packet 739 * must be discarded, else it may be accepted below. 740 */ 741 if (ip_mforward != NULL && 742 ip_mforward(ip, m->m_pkthdr.rcvif, m, NULL) != 0) { 743 rel_mplock(); 744 ipstat.ips_cantforward++; 745 m_freem(m); 746 return; 747 } 748 749 rel_mplock(); 750 751 /* 752 * The process-level routing daemon needs to receive 753 * all multicast IGMP packets, whether or not this 754 * host belongs to their destination groups. 755 */ 756 if (ip->ip_p == IPPROTO_IGMP) 757 goto ours; 758 ipstat.ips_forward++; 759 } 760 /* 761 * See if we belong to the destination multicast group on the 762 * arrival interface. 763 */ 764 inm = IN_LOOKUP_MULTI(&ip->ip_dst, m->m_pkthdr.rcvif); 765 if (inm == NULL) { 766 ipstat.ips_notmember++; 767 m_freem(m); 768 return; 769 } 770 goto ours; 771 } 772 if (ip->ip_dst.s_addr == INADDR_BROADCAST) 773 goto ours; 774 if (ip->ip_dst.s_addr == INADDR_ANY) 775 goto ours; 776 777 /* 778 * FAITH(Firewall Aided Internet Translator) 779 */ 780 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 781 if (ip_keepfaith) { 782 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 783 goto ours; 784 } 785 m_freem(m); 786 return; 787 } 788 789 /* 790 * Not for us; forward if possible and desirable. 791 */ 792 if (!ipforwarding) { 793 ipstat.ips_cantforward++; 794 m_freem(m); 795 } else { 796 #ifdef IPSEC 797 /* 798 * Enforce inbound IPsec SPD. 799 */ 800 if (ipsec4_in_reject(m, NULL)) { 801 ipsecstat.in_polvio++; 802 goto bad; 803 } 804 #endif 805 #ifdef FAST_IPSEC 806 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 807 crit_enter(); 808 if (mtag != NULL) { 809 tdbi = (struct tdb_ident *)m_tag_data(mtag); 810 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 811 } else { 812 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 813 IP_FORWARDING, &error); 814 } 815 if (sp == NULL) { /* NB: can happen if error */ 816 crit_exit(); 817 /*XXX error stat???*/ 818 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 819 goto bad; 820 } 821 822 /* 823 * Check security policy against packet attributes. 824 */ 825 error = ipsec_in_reject(sp, m); 826 KEY_FREESP(&sp); 827 crit_exit(); 828 if (error) { 829 ipstat.ips_cantforward++; 830 goto bad; 831 } 832 #endif 833 ip_forward(m, using_srcrt, next_hop); 834 } 835 return; 836 837 ours: 838 839 /* 840 * IPSTEALTH: Process non-routing options only 841 * if the packet is destined for us. 842 */ 843 if (ipstealth && 844 hlen > sizeof(struct ip) && 845 ip_dooptions(m, 1, next_hop)) 846 return; 847 848 /* Count the packet in the ip address stats */ 849 if (ia != NULL) { 850 IFA_STAT_INC(&ia->ia_ifa, ipackets, 1); 851 IFA_STAT_INC(&ia->ia_ifa, ibytes, m->m_pkthdr.len); 852 } 853 854 /* 855 * If offset or IP_MF are set, must reassemble. 856 * Otherwise, nothing need be done. 857 * (We could look in the reassembly queue to see 858 * if the packet was previously fragmented, 859 * but it's not worth the time; just let them time out.) 860 */ 861 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 862 /* 863 * Attempt reassembly; if it succeeds, proceed. ip_reass() 864 * will return a different mbuf. 865 * 866 * NOTE: ip_reass() returns m with M_HASH cleared to force 867 * us to recharacterize the packet. 868 */ 869 m = ip_reass(m); 870 if (m == NULL) 871 return; 872 ip = mtod(m, struct ip *); 873 874 /* Get the header length of the reassembled packet */ 875 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 876 } else { 877 ip->ip_len -= hlen; 878 } 879 880 #ifdef IPSEC 881 /* 882 * enforce IPsec policy checking if we are seeing last header. 883 * note that we do not visit this with protocols with pcb layer 884 * code - like udp/tcp/raw ip. 885 */ 886 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) && 887 ipsec4_in_reject(m, NULL)) { 888 ipsecstat.in_polvio++; 889 goto bad; 890 } 891 #endif 892 #if FAST_IPSEC 893 /* 894 * enforce IPsec policy checking if we are seeing last header. 895 * note that we do not visit this with protocols with pcb layer 896 * code - like udp/tcp/raw ip. 897 */ 898 if (inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) { 899 /* 900 * Check if the packet has already had IPsec processing 901 * done. If so, then just pass it along. This tag gets 902 * set during AH, ESP, etc. input handling, before the 903 * packet is returned to the ip input queue for delivery. 904 */ 905 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 906 crit_enter(); 907 if (mtag != NULL) { 908 tdbi = (struct tdb_ident *)m_tag_data(mtag); 909 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 910 } else { 911 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 912 IP_FORWARDING, &error); 913 } 914 if (sp != NULL) { 915 /* 916 * Check security policy against packet attributes. 917 */ 918 error = ipsec_in_reject(sp, m); 919 KEY_FREESP(&sp); 920 } else { 921 /* XXX error stat??? */ 922 error = EINVAL; 923 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 924 crit_exit(); 925 goto bad; 926 } 927 crit_exit(); 928 if (error) 929 goto bad; 930 } 931 #endif /* FAST_IPSEC */ 932 933 /* 934 * We must forward the packet to the correct protocol thread if 935 * we are not already in it. 936 * 937 * NOTE: ip_len is now in host form. ip_len is not adjusted 938 * further for protocol processing, instead we pass hlen 939 * to the protosw and let it deal with it. 940 */ 941 ipstat.ips_delivered++; 942 943 if ((m->m_flags & M_HASH) == 0) { 944 #ifdef RSS_DEBUG 945 atomic_add_long(&ip_rehash_count, 1); 946 #endif 947 ip->ip_len = htons(ip->ip_len + hlen); 948 ip->ip_off = htons(ip->ip_off); 949 950 ip_hashfn(&m, 0, IP_MPORT_IN); 951 if (m == NULL) 952 return; 953 954 ip = mtod(m, struct ip *); 955 ip->ip_len = ntohs(ip->ip_len) - hlen; 956 ip->ip_off = ntohs(ip->ip_off); 957 KKASSERT(m->m_flags & M_HASH); 958 } 959 port = netisr_hashport(m->m_pkthdr.hash); 960 961 if (port != &curthread->td_msgport) { 962 struct netmsg_packet *pmsg; 963 964 #ifdef RSS_DEBUG 965 atomic_add_long(&ip_dispatch_slow, 1); 966 #endif 967 968 pmsg = &m->m_hdr.mh_netmsg; 969 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, 970 0, transport_processing_handler); 971 pmsg->nm_packet = m; 972 pmsg->base.lmsg.u.ms_result = hlen; 973 lwkt_sendmsg(port, &pmsg->base.lmsg); 974 } else { 975 #ifdef RSS_DEBUG 976 atomic_add_long(&ip_dispatch_fast, 1); 977 #endif 978 transport_processing_oncpu(m, hlen, ip); 979 } 980 return; 981 982 bad: 983 m_freem(m); 984 } 985 986 /* 987 * Take incoming datagram fragment and try to reassemble it into 988 * whole datagram. If a chain for reassembly of this datagram already 989 * exists, then it is given as fp; otherwise have to make a chain. 990 */ 991 struct mbuf * 992 ip_reass(struct mbuf *m) 993 { 994 struct ip *ip = mtod(m, struct ip *); 995 struct mbuf *p = NULL, *q, *nq; 996 struct mbuf *n; 997 struct ipq *fp = NULL; 998 struct ipqhead *head; 999 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 1000 int i, next; 1001 u_short sum; 1002 1003 /* If maxnipq is 0, never accept fragments. */ 1004 if (maxnipq == 0) { 1005 ipstat.ips_fragments++; 1006 ipstat.ips_fragdropped++; 1007 m_freem(m); 1008 return NULL; 1009 } 1010 1011 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 1012 /* 1013 * Look for queue of fragments of this datagram. 1014 */ 1015 lwkt_gettoken(&ipq_token); 1016 head = &ipq[sum]; 1017 TAILQ_FOREACH(fp, head, ipq_list) { 1018 if (ip->ip_id == fp->ipq_id && 1019 ip->ip_src.s_addr == fp->ipq_src.s_addr && 1020 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1021 ip->ip_p == fp->ipq_p) 1022 goto found; 1023 } 1024 1025 fp = NULL; 1026 1027 /* 1028 * Enforce upper bound on number of fragmented packets 1029 * for which we attempt reassembly; 1030 * If maxnipq is -1, accept all fragments without limitation. 1031 */ 1032 if (nipq > maxnipq && maxnipq > 0) { 1033 /* 1034 * drop something from the tail of the current queue 1035 * before proceeding further 1036 */ 1037 struct ipq *q = TAILQ_LAST(head, ipqhead); 1038 if (q == NULL) { 1039 /* 1040 * The current queue is empty, 1041 * so drop from one of the others. 1042 */ 1043 for (i = 0; i < IPREASS_NHASH; i++) { 1044 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead); 1045 if (r) { 1046 ipstat.ips_fragtimeout += r->ipq_nfrags; 1047 ip_freef(&ipq[i], r); 1048 break; 1049 } 1050 } 1051 } else { 1052 ipstat.ips_fragtimeout += q->ipq_nfrags; 1053 ip_freef(head, q); 1054 } 1055 } 1056 found: 1057 /* 1058 * Adjust ip_len to not reflect header, 1059 * convert offset of this to bytes. 1060 */ 1061 ip->ip_len -= hlen; 1062 if (ip->ip_off & IP_MF) { 1063 /* 1064 * Make sure that fragments have a data length 1065 * that's a non-zero multiple of 8 bytes. 1066 */ 1067 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 1068 ipstat.ips_toosmall++; /* XXX */ 1069 m_freem(m); 1070 goto done; 1071 } 1072 m->m_flags |= M_FRAG; 1073 } else { 1074 m->m_flags &= ~M_FRAG; 1075 } 1076 ip->ip_off <<= 3; 1077 1078 ipstat.ips_fragments++; 1079 m->m_pkthdr.header = ip; 1080 1081 /* 1082 * If the hardware has not done csum over this fragment 1083 * then csum_data is not valid at all. 1084 */ 1085 if ((m->m_pkthdr.csum_flags & (CSUM_FRAG_NOT_CHECKED | CSUM_DATA_VALID)) 1086 == (CSUM_FRAG_NOT_CHECKED | CSUM_DATA_VALID)) { 1087 m->m_pkthdr.csum_data = 0; 1088 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 1089 } 1090 1091 /* 1092 * Presence of header sizes in mbufs 1093 * would confuse code below. 1094 */ 1095 m->m_data += hlen; 1096 m->m_len -= hlen; 1097 1098 /* 1099 * If first fragment to arrive, create a reassembly queue. 1100 */ 1101 if (fp == NULL) { 1102 if ((fp = mpipe_alloc_nowait(&ipq_mpipe)) == NULL) 1103 goto dropfrag; 1104 TAILQ_INSERT_HEAD(head, fp, ipq_list); 1105 nipq++; 1106 fp->ipq_nfrags = 1; 1107 fp->ipq_ttl = IPFRAGTTL; 1108 fp->ipq_p = ip->ip_p; 1109 fp->ipq_id = ip->ip_id; 1110 fp->ipq_src = ip->ip_src; 1111 fp->ipq_dst = ip->ip_dst; 1112 fp->ipq_frags = m; 1113 m->m_nextpkt = NULL; 1114 goto inserted; 1115 } else { 1116 fp->ipq_nfrags++; 1117 } 1118 1119 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 1120 1121 /* 1122 * Find a segment which begins after this one does. 1123 */ 1124 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1125 if (GETIP(q)->ip_off > ip->ip_off) 1126 break; 1127 } 1128 1129 /* 1130 * If there is a preceding segment, it may provide some of 1131 * our data already. If so, drop the data from the incoming 1132 * segment. If it provides all of our data, drop us, otherwise 1133 * stick new segment in the proper place. 1134 * 1135 * If some of the data is dropped from the the preceding 1136 * segment, then it's checksum is invalidated. 1137 */ 1138 if (p) { 1139 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 1140 if (i > 0) { 1141 if (i >= ip->ip_len) 1142 goto dropfrag; 1143 m_adj(m, i); 1144 m->m_pkthdr.csum_flags = 0; 1145 ip->ip_off += i; 1146 ip->ip_len -= i; 1147 } 1148 m->m_nextpkt = p->m_nextpkt; 1149 p->m_nextpkt = m; 1150 } else { 1151 m->m_nextpkt = fp->ipq_frags; 1152 fp->ipq_frags = m; 1153 } 1154 1155 /* 1156 * While we overlap succeeding segments trim them or, 1157 * if they are completely covered, dequeue them. 1158 */ 1159 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 1160 q = nq) { 1161 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 1162 if (i < GETIP(q)->ip_len) { 1163 GETIP(q)->ip_len -= i; 1164 GETIP(q)->ip_off += i; 1165 m_adj(q, i); 1166 q->m_pkthdr.csum_flags = 0; 1167 break; 1168 } 1169 nq = q->m_nextpkt; 1170 m->m_nextpkt = nq; 1171 ipstat.ips_fragdropped++; 1172 fp->ipq_nfrags--; 1173 q->m_nextpkt = NULL; 1174 m_freem(q); 1175 } 1176 1177 inserted: 1178 /* 1179 * Check for complete reassembly and perform frag per packet 1180 * limiting. 1181 * 1182 * Frag limiting is performed here so that the nth frag has 1183 * a chance to complete the packet before we drop the packet. 1184 * As a result, n+1 frags are actually allowed per packet, but 1185 * only n will ever be stored. (n = maxfragsperpacket.) 1186 * 1187 */ 1188 next = 0; 1189 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1190 if (GETIP(q)->ip_off != next) { 1191 if (fp->ipq_nfrags > maxfragsperpacket) { 1192 ipstat.ips_fragdropped += fp->ipq_nfrags; 1193 ip_freef(head, fp); 1194 } 1195 goto done; 1196 } 1197 next += GETIP(q)->ip_len; 1198 } 1199 /* Make sure the last packet didn't have the IP_MF flag */ 1200 if (p->m_flags & M_FRAG) { 1201 if (fp->ipq_nfrags > maxfragsperpacket) { 1202 ipstat.ips_fragdropped += fp->ipq_nfrags; 1203 ip_freef(head, fp); 1204 } 1205 goto done; 1206 } 1207 1208 /* 1209 * Reassembly is complete. Make sure the packet is a sane size. 1210 */ 1211 q = fp->ipq_frags; 1212 ip = GETIP(q); 1213 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 1214 ipstat.ips_toolong++; 1215 ipstat.ips_fragdropped += fp->ipq_nfrags; 1216 ip_freef(head, fp); 1217 goto done; 1218 } 1219 1220 /* 1221 * Concatenate fragments. 1222 */ 1223 m = q; 1224 n = m->m_next; 1225 m->m_next = NULL; 1226 m_cat(m, n); 1227 nq = q->m_nextpkt; 1228 q->m_nextpkt = NULL; 1229 for (q = nq; q != NULL; q = nq) { 1230 nq = q->m_nextpkt; 1231 q->m_nextpkt = NULL; 1232 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1233 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1234 m_cat(m, q); 1235 } 1236 1237 /* 1238 * Clean up the 1's complement checksum. Carry over 16 bits must 1239 * be added back. This assumes no more then 65535 packet fragments 1240 * were reassembled. A second carry can also occur (but not a third). 1241 */ 1242 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) + 1243 (m->m_pkthdr.csum_data >> 16); 1244 if (m->m_pkthdr.csum_data > 0xFFFF) 1245 m->m_pkthdr.csum_data -= 0xFFFF; 1246 1247 /* 1248 * Create header for new ip packet by 1249 * modifying header of first packet; 1250 * dequeue and discard fragment reassembly header. 1251 * Make header visible. 1252 */ 1253 ip->ip_len = next; 1254 ip->ip_src = fp->ipq_src; 1255 ip->ip_dst = fp->ipq_dst; 1256 TAILQ_REMOVE(head, fp, ipq_list); 1257 nipq--; 1258 mpipe_free(&ipq_mpipe, fp); 1259 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 1260 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 1261 /* some debugging cruft by sklower, below, will go away soon */ 1262 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1263 int plen = 0; 1264 1265 for (n = m; n; n = n->m_next) 1266 plen += n->m_len; 1267 m->m_pkthdr.len = plen; 1268 } 1269 1270 /* 1271 * Reassembly complete, return the next protocol. 1272 * 1273 * Be sure to clear M_HASH to force the packet 1274 * to be re-characterized. 1275 * 1276 * Clear M_FRAG, we are no longer a fragment. 1277 */ 1278 m->m_flags &= ~(M_HASH | M_FRAG); 1279 1280 ipstat.ips_reassembled++; 1281 lwkt_reltoken(&ipq_token); 1282 return (m); 1283 1284 dropfrag: 1285 ipstat.ips_fragdropped++; 1286 if (fp != NULL) 1287 fp->ipq_nfrags--; 1288 m_freem(m); 1289 done: 1290 lwkt_reltoken(&ipq_token); 1291 return (NULL); 1292 1293 #undef GETIP 1294 } 1295 1296 /* 1297 * Free a fragment reassembly header and all 1298 * associated datagrams. 1299 * 1300 * Called with ipq_token held. 1301 */ 1302 static void 1303 ip_freef(struct ipqhead *fhp, struct ipq *fp) 1304 { 1305 struct mbuf *q; 1306 1307 /* 1308 * Remove first to protect against blocking 1309 */ 1310 TAILQ_REMOVE(fhp, fp, ipq_list); 1311 1312 /* 1313 * Clean out at our leisure 1314 */ 1315 while (fp->ipq_frags) { 1316 q = fp->ipq_frags; 1317 fp->ipq_frags = q->m_nextpkt; 1318 q->m_nextpkt = NULL; 1319 m_freem(q); 1320 } 1321 mpipe_free(&ipq_mpipe, fp); 1322 nipq--; 1323 } 1324 1325 /* 1326 * IP timer processing; 1327 * if a timer expires on a reassembly 1328 * queue, discard it. 1329 */ 1330 void 1331 ip_slowtimo(void) 1332 { 1333 struct ipq *fp, *fp_temp; 1334 struct ipqhead *head; 1335 int i; 1336 1337 lwkt_gettoken(&ipq_token); 1338 for (i = 0; i < IPREASS_NHASH; i++) { 1339 head = &ipq[i]; 1340 TAILQ_FOREACH_MUTABLE(fp, head, ipq_list, fp_temp) { 1341 if (--fp->ipq_ttl == 0) { 1342 ipstat.ips_fragtimeout += fp->ipq_nfrags; 1343 ip_freef(head, fp); 1344 } 1345 } 1346 } 1347 /* 1348 * If we are over the maximum number of fragments 1349 * (due to the limit being lowered), drain off 1350 * enough to get down to the new limit. 1351 */ 1352 if (maxnipq >= 0 && nipq > maxnipq) { 1353 for (i = 0; i < IPREASS_NHASH; i++) { 1354 head = &ipq[i]; 1355 while (nipq > maxnipq && !TAILQ_EMPTY(head)) { 1356 ipstat.ips_fragdropped += 1357 TAILQ_FIRST(head)->ipq_nfrags; 1358 ip_freef(head, TAILQ_FIRST(head)); 1359 } 1360 } 1361 } 1362 lwkt_reltoken(&ipq_token); 1363 ipflow_slowtimo(); 1364 } 1365 1366 /* 1367 * Drain off all datagram fragments. 1368 */ 1369 void 1370 ip_drain(void) 1371 { 1372 struct ipqhead *head; 1373 int i; 1374 1375 lwkt_gettoken(&ipq_token); 1376 for (i = 0; i < IPREASS_NHASH; i++) { 1377 head = &ipq[i]; 1378 while (!TAILQ_EMPTY(head)) { 1379 ipstat.ips_fragdropped += TAILQ_FIRST(head)->ipq_nfrags; 1380 ip_freef(head, TAILQ_FIRST(head)); 1381 } 1382 } 1383 lwkt_reltoken(&ipq_token); 1384 in_rtqdrain(); 1385 } 1386 1387 /* 1388 * Do option processing on a datagram, 1389 * possibly discarding it if bad options are encountered, 1390 * or forwarding it if source-routed. 1391 * The pass argument is used when operating in the IPSTEALTH 1392 * mode to tell what options to process: 1393 * [LS]SRR (pass 0) or the others (pass 1). 1394 * The reason for as many as two passes is that when doing IPSTEALTH, 1395 * non-routing options should be processed only if the packet is for us. 1396 * Returns 1 if packet has been forwarded/freed, 1397 * 0 if the packet should be processed further. 1398 */ 1399 static int 1400 ip_dooptions(struct mbuf *m, int pass, struct sockaddr_in *next_hop) 1401 { 1402 struct sockaddr_in ipaddr = { sizeof ipaddr, AF_INET }; 1403 struct ip *ip = mtod(m, struct ip *); 1404 u_char *cp; 1405 struct in_ifaddr *ia; 1406 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 1407 boolean_t forward = FALSE; 1408 struct in_addr *sin, dst; 1409 n_time ntime; 1410 1411 dst = ip->ip_dst; 1412 cp = (u_char *)(ip + 1); 1413 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 1414 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1415 opt = cp[IPOPT_OPTVAL]; 1416 if (opt == IPOPT_EOL) 1417 break; 1418 if (opt == IPOPT_NOP) 1419 optlen = 1; 1420 else { 1421 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1422 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1423 goto bad; 1424 } 1425 optlen = cp[IPOPT_OLEN]; 1426 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1427 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1428 goto bad; 1429 } 1430 } 1431 switch (opt) { 1432 1433 default: 1434 break; 1435 1436 /* 1437 * Source routing with record. 1438 * Find interface with current destination address. 1439 * If none on this machine then drop if strictly routed, 1440 * or do nothing if loosely routed. 1441 * Record interface address and bring up next address 1442 * component. If strictly routed make sure next 1443 * address is on directly accessible net. 1444 */ 1445 case IPOPT_LSRR: 1446 case IPOPT_SSRR: 1447 if (ipstealth && pass > 0) 1448 break; 1449 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1450 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1451 goto bad; 1452 } 1453 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1454 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1455 goto bad; 1456 } 1457 ipaddr.sin_addr = ip->ip_dst; 1458 ia = (struct in_ifaddr *) 1459 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1460 if (ia == NULL) { 1461 if (opt == IPOPT_SSRR) { 1462 type = ICMP_UNREACH; 1463 code = ICMP_UNREACH_SRCFAIL; 1464 goto bad; 1465 } 1466 if (!ip_dosourceroute) 1467 goto nosourcerouting; 1468 /* 1469 * Loose routing, and not at next destination 1470 * yet; nothing to do except forward. 1471 */ 1472 break; 1473 } 1474 off--; /* 0 origin */ 1475 if (off > optlen - (int)sizeof(struct in_addr)) { 1476 /* 1477 * End of source route. Should be for us. 1478 */ 1479 if (!ip_acceptsourceroute) 1480 goto nosourcerouting; 1481 save_rte(m, cp, ip->ip_src); 1482 break; 1483 } 1484 if (ipstealth) 1485 goto dropit; 1486 if (!ip_dosourceroute) { 1487 if (ipforwarding) { 1488 char buf[sizeof "aaa.bbb.ccc.ddd"]; 1489 1490 /* 1491 * Acting as a router, so generate ICMP 1492 */ 1493 nosourcerouting: 1494 strcpy(buf, inet_ntoa(ip->ip_dst)); 1495 log(LOG_WARNING, 1496 "attempted source route from %s to %s\n", 1497 inet_ntoa(ip->ip_src), buf); 1498 type = ICMP_UNREACH; 1499 code = ICMP_UNREACH_SRCFAIL; 1500 goto bad; 1501 } else { 1502 /* 1503 * Not acting as a router, 1504 * so silently drop. 1505 */ 1506 dropit: 1507 ipstat.ips_cantforward++; 1508 m_freem(m); 1509 return (1); 1510 } 1511 } 1512 1513 /* 1514 * locate outgoing interface 1515 */ 1516 memcpy(&ipaddr.sin_addr, cp + off, 1517 sizeof ipaddr.sin_addr); 1518 1519 if (opt == IPOPT_SSRR) { 1520 #define INA struct in_ifaddr * 1521 #define SA struct sockaddr * 1522 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) 1523 == NULL) 1524 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1525 } else { 1526 ia = ip_rtaddr(ipaddr.sin_addr, NULL); 1527 } 1528 if (ia == NULL) { 1529 type = ICMP_UNREACH; 1530 code = ICMP_UNREACH_SRCFAIL; 1531 goto bad; 1532 } 1533 ip->ip_dst = ipaddr.sin_addr; 1534 memcpy(cp + off, &IA_SIN(ia)->sin_addr, 1535 sizeof(struct in_addr)); 1536 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1537 /* 1538 * Let ip_intr's mcast routing check handle mcast pkts 1539 */ 1540 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1541 break; 1542 1543 case IPOPT_RR: 1544 if (ipstealth && pass == 0) 1545 break; 1546 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1547 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1548 goto bad; 1549 } 1550 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1551 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1552 goto bad; 1553 } 1554 /* 1555 * If no space remains, ignore. 1556 */ 1557 off--; /* 0 origin */ 1558 if (off > optlen - (int)sizeof(struct in_addr)) 1559 break; 1560 memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1561 sizeof ipaddr.sin_addr); 1562 /* 1563 * locate outgoing interface; if we're the destination, 1564 * use the incoming interface (should be same). 1565 */ 1566 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 1567 (ia = ip_rtaddr(ipaddr.sin_addr, NULL)) == NULL) { 1568 type = ICMP_UNREACH; 1569 code = ICMP_UNREACH_HOST; 1570 goto bad; 1571 } 1572 memcpy(cp + off, &IA_SIN(ia)->sin_addr, 1573 sizeof(struct in_addr)); 1574 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1575 break; 1576 1577 case IPOPT_TS: 1578 if (ipstealth && pass == 0) 1579 break; 1580 code = cp - (u_char *)ip; 1581 if (optlen < 4 || optlen > 40) { 1582 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1583 goto bad; 1584 } 1585 if ((off = cp[IPOPT_OFFSET]) < 5) { 1586 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1587 goto bad; 1588 } 1589 if (off > optlen - (int)sizeof(int32_t)) { 1590 cp[IPOPT_OFFSET + 1] += (1 << 4); 1591 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1592 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1593 goto bad; 1594 } 1595 break; 1596 } 1597 off--; /* 0 origin */ 1598 sin = (struct in_addr *)(cp + off); 1599 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1600 1601 case IPOPT_TS_TSONLY: 1602 break; 1603 1604 case IPOPT_TS_TSANDADDR: 1605 if (off + sizeof(n_time) + 1606 sizeof(struct in_addr) > optlen) { 1607 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1608 goto bad; 1609 } 1610 ipaddr.sin_addr = dst; 1611 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1612 m->m_pkthdr.rcvif); 1613 if (ia == NULL) 1614 continue; 1615 memcpy(sin, &IA_SIN(ia)->sin_addr, 1616 sizeof(struct in_addr)); 1617 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1618 off += sizeof(struct in_addr); 1619 break; 1620 1621 case IPOPT_TS_PRESPEC: 1622 if (off + sizeof(n_time) + 1623 sizeof(struct in_addr) > optlen) { 1624 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1625 goto bad; 1626 } 1627 memcpy(&ipaddr.sin_addr, sin, 1628 sizeof(struct in_addr)); 1629 if (ifa_ifwithaddr((SA)&ipaddr) == NULL) 1630 continue; 1631 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1632 off += sizeof(struct in_addr); 1633 break; 1634 1635 default: 1636 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1637 goto bad; 1638 } 1639 ntime = iptime(); 1640 memcpy(cp + off, &ntime, sizeof(n_time)); 1641 cp[IPOPT_OFFSET] += sizeof(n_time); 1642 } 1643 } 1644 if (forward && ipforwarding) { 1645 ip_forward(m, TRUE, next_hop); 1646 return (1); 1647 } 1648 return (0); 1649 bad: 1650 icmp_error(m, type, code, 0, 0); 1651 ipstat.ips_badoptions++; 1652 return (1); 1653 } 1654 1655 /* 1656 * Given address of next destination (final or next hop), 1657 * return internet address info of interface to be used to get there. 1658 */ 1659 struct in_ifaddr * 1660 ip_rtaddr(struct in_addr dst, struct route *ro0) 1661 { 1662 struct route sro, *ro; 1663 struct sockaddr_in *sin; 1664 struct in_ifaddr *ia; 1665 1666 if (ro0 != NULL) { 1667 ro = ro0; 1668 } else { 1669 bzero(&sro, sizeof(sro)); 1670 ro = &sro; 1671 } 1672 1673 sin = (struct sockaddr_in *)&ro->ro_dst; 1674 1675 if (ro->ro_rt == NULL || dst.s_addr != sin->sin_addr.s_addr) { 1676 if (ro->ro_rt != NULL) { 1677 RTFREE(ro->ro_rt); 1678 ro->ro_rt = NULL; 1679 } 1680 sin->sin_family = AF_INET; 1681 sin->sin_len = sizeof *sin; 1682 sin->sin_addr = dst; 1683 rtalloc_ign(ro, RTF_PRCLONING); 1684 } 1685 1686 if (ro->ro_rt == NULL) 1687 return (NULL); 1688 1689 ia = ifatoia(ro->ro_rt->rt_ifa); 1690 1691 if (ro == &sro) 1692 RTFREE(ro->ro_rt); 1693 return ia; 1694 } 1695 1696 /* 1697 * Save incoming source route for use in replies, 1698 * to be picked up later by ip_srcroute if the receiver is interested. 1699 */ 1700 static void 1701 save_rte(struct mbuf *m, u_char *option, struct in_addr dst) 1702 { 1703 struct m_tag *mtag; 1704 struct ip_srcrt_opt *opt; 1705 unsigned olen; 1706 1707 mtag = m_tag_get(PACKET_TAG_IPSRCRT, sizeof(*opt), MB_DONTWAIT); 1708 if (mtag == NULL) 1709 return; 1710 opt = m_tag_data(mtag); 1711 1712 olen = option[IPOPT_OLEN]; 1713 #ifdef DIAGNOSTIC 1714 if (ipprintfs) 1715 kprintf("save_rte: olen %d\n", olen); 1716 #endif 1717 if (olen > sizeof(opt->ip_srcrt) - (1 + sizeof(dst))) { 1718 m_tag_free(mtag); 1719 return; 1720 } 1721 bcopy(option, opt->ip_srcrt.srcopt, olen); 1722 opt->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1723 opt->ip_srcrt.dst = dst; 1724 m_tag_prepend(m, mtag); 1725 } 1726 1727 /* 1728 * Retrieve incoming source route for use in replies, 1729 * in the same form used by setsockopt. 1730 * The first hop is placed before the options, will be removed later. 1731 */ 1732 struct mbuf * 1733 ip_srcroute(struct mbuf *m0) 1734 { 1735 struct in_addr *p, *q; 1736 struct mbuf *m; 1737 struct m_tag *mtag; 1738 struct ip_srcrt_opt *opt; 1739 1740 if (m0 == NULL) 1741 return NULL; 1742 1743 mtag = m_tag_find(m0, PACKET_TAG_IPSRCRT, NULL); 1744 if (mtag == NULL) 1745 return NULL; 1746 opt = m_tag_data(mtag); 1747 1748 if (opt->ip_nhops == 0) 1749 return (NULL); 1750 m = m_get(MB_DONTWAIT, MT_HEADER); 1751 if (m == NULL) 1752 return (NULL); 1753 1754 #define OPTSIZ (sizeof(opt->ip_srcrt.nop) + sizeof(opt->ip_srcrt.srcopt)) 1755 1756 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1757 m->m_len = opt->ip_nhops * sizeof(struct in_addr) + 1758 sizeof(struct in_addr) + OPTSIZ; 1759 #ifdef DIAGNOSTIC 1760 if (ipprintfs) { 1761 kprintf("ip_srcroute: nhops %d mlen %d", 1762 opt->ip_nhops, m->m_len); 1763 } 1764 #endif 1765 1766 /* 1767 * First save first hop for return route 1768 */ 1769 p = &opt->ip_srcrt.route[opt->ip_nhops - 1]; 1770 *(mtod(m, struct in_addr *)) = *p--; 1771 #ifdef DIAGNOSTIC 1772 if (ipprintfs) 1773 kprintf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1774 #endif 1775 1776 /* 1777 * Copy option fields and padding (nop) to mbuf. 1778 */ 1779 opt->ip_srcrt.nop = IPOPT_NOP; 1780 opt->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1781 memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &opt->ip_srcrt.nop, 1782 OPTSIZ); 1783 q = (struct in_addr *)(mtod(m, caddr_t) + 1784 sizeof(struct in_addr) + OPTSIZ); 1785 #undef OPTSIZ 1786 /* 1787 * Record return path as an IP source route, 1788 * reversing the path (pointers are now aligned). 1789 */ 1790 while (p >= opt->ip_srcrt.route) { 1791 #ifdef DIAGNOSTIC 1792 if (ipprintfs) 1793 kprintf(" %x", ntohl(q->s_addr)); 1794 #endif 1795 *q++ = *p--; 1796 } 1797 /* 1798 * Last hop goes to final destination. 1799 */ 1800 *q = opt->ip_srcrt.dst; 1801 m_tag_delete(m0, mtag); 1802 #ifdef DIAGNOSTIC 1803 if (ipprintfs) 1804 kprintf(" %x\n", ntohl(q->s_addr)); 1805 #endif 1806 return (m); 1807 } 1808 1809 /* 1810 * Strip out IP options. 1811 */ 1812 void 1813 ip_stripoptions(struct mbuf *m) 1814 { 1815 int datalen; 1816 struct ip *ip = mtod(m, struct ip *); 1817 caddr_t opts; 1818 int optlen; 1819 1820 optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 1821 opts = (caddr_t)(ip + 1); 1822 datalen = m->m_len - (sizeof(struct ip) + optlen); 1823 bcopy(opts + optlen, opts, datalen); 1824 m->m_len -= optlen; 1825 if (m->m_flags & M_PKTHDR) 1826 m->m_pkthdr.len -= optlen; 1827 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1828 } 1829 1830 u_char inetctlerrmap[PRC_NCMDS] = { 1831 0, 0, 0, 0, 1832 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1833 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1834 EMSGSIZE, EHOSTUNREACH, 0, 0, 1835 0, 0, 0, 0, 1836 ENOPROTOOPT, ECONNREFUSED 1837 }; 1838 1839 /* 1840 * Forward a packet. If some error occurs return the sender 1841 * an icmp packet. Note we can't always generate a meaningful 1842 * icmp message because icmp doesn't have a large enough repertoire 1843 * of codes and types. 1844 * 1845 * If not forwarding, just drop the packet. This could be confusing 1846 * if ipforwarding was zero but some routing protocol was advancing 1847 * us as a gateway to somewhere. However, we must let the routing 1848 * protocol deal with that. 1849 * 1850 * The using_srcrt parameter indicates whether the packet is being forwarded 1851 * via a source route. 1852 */ 1853 void 1854 ip_forward(struct mbuf *m, boolean_t using_srcrt, struct sockaddr_in *next_hop) 1855 { 1856 struct ip *ip = mtod(m, struct ip *); 1857 struct rtentry *rt; 1858 struct route fwd_ro; 1859 int error, type = 0, code = 0, destmtu = 0; 1860 struct mbuf *mcopy, *mtemp = NULL; 1861 n_long dest; 1862 struct in_addr pkt_dst; 1863 1864 dest = INADDR_ANY; 1865 /* 1866 * Cache the destination address of the packet; this may be 1867 * changed by use of 'ipfw fwd'. 1868 */ 1869 pkt_dst = (next_hop != NULL) ? next_hop->sin_addr : ip->ip_dst; 1870 1871 #ifdef DIAGNOSTIC 1872 if (ipprintfs) 1873 kprintf("forward: src %x dst %x ttl %x\n", 1874 ip->ip_src.s_addr, pkt_dst.s_addr, ip->ip_ttl); 1875 #endif 1876 1877 if (m->m_flags & (M_BCAST | M_MCAST) || !in_canforward(pkt_dst)) { 1878 ipstat.ips_cantforward++; 1879 m_freem(m); 1880 return; 1881 } 1882 if (!ipstealth && ip->ip_ttl <= IPTTLDEC) { 1883 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1884 return; 1885 } 1886 1887 bzero(&fwd_ro, sizeof(fwd_ro)); 1888 ip_rtaddr(pkt_dst, &fwd_ro); 1889 if (fwd_ro.ro_rt == NULL) { 1890 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1891 return; 1892 } 1893 rt = fwd_ro.ro_rt; 1894 1895 if (curthread->td_type == TD_TYPE_NETISR) { 1896 /* 1897 * Save the IP header and at most 8 bytes of the payload, 1898 * in case we need to generate an ICMP message to the src. 1899 */ 1900 mtemp = ipforward_mtemp[mycpuid]; 1901 KASSERT((mtemp->m_flags & M_EXT) == 0 && 1902 mtemp->m_data == mtemp->m_pktdat && 1903 m_tag_first(mtemp) == NULL, 1904 ("ip_forward invalid mtemp1")); 1905 1906 if (!m_dup_pkthdr(mtemp, m, MB_DONTWAIT)) { 1907 /* 1908 * It's probably ok if the pkthdr dup fails (because 1909 * the deep copy of the tag chain failed), but for now 1910 * be conservative and just discard the copy since 1911 * code below may some day want the tags. 1912 */ 1913 mtemp = NULL; 1914 } else { 1915 mtemp->m_type = m->m_type; 1916 mtemp->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8, 1917 (int)ip->ip_len); 1918 mtemp->m_pkthdr.len = mtemp->m_len; 1919 m_copydata(m, 0, mtemp->m_len, mtod(mtemp, caddr_t)); 1920 } 1921 } 1922 1923 if (!ipstealth) 1924 ip->ip_ttl -= IPTTLDEC; 1925 1926 /* 1927 * If forwarding packet using same interface that it came in on, 1928 * perhaps should send a redirect to sender to shortcut a hop. 1929 * Only send redirect if source is sending directly to us, 1930 * and if packet was not source routed (or has any options). 1931 * Also, don't send redirect if forwarding using a default route 1932 * or a route modified by a redirect. 1933 */ 1934 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1935 !(rt->rt_flags & (RTF_DYNAMIC | RTF_MODIFIED)) && 1936 satosin(rt_key(rt))->sin_addr.s_addr != INADDR_ANY && 1937 ipsendredirects && !using_srcrt && next_hop == NULL) { 1938 u_long src = ntohl(ip->ip_src.s_addr); 1939 struct in_ifaddr *rt_ifa = (struct in_ifaddr *)rt->rt_ifa; 1940 1941 if (rt_ifa != NULL && 1942 (src & rt_ifa->ia_subnetmask) == rt_ifa->ia_subnet) { 1943 if (rt->rt_flags & RTF_GATEWAY) 1944 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1945 else 1946 dest = pkt_dst.s_addr; 1947 /* 1948 * Router requirements says to only send 1949 * host redirects. 1950 */ 1951 type = ICMP_REDIRECT; 1952 code = ICMP_REDIRECT_HOST; 1953 #ifdef DIAGNOSTIC 1954 if (ipprintfs) 1955 kprintf("redirect (%d) to %x\n", code, dest); 1956 #endif 1957 } 1958 } 1959 1960 error = ip_output(m, NULL, &fwd_ro, IP_FORWARDING, NULL, NULL); 1961 if (error == 0) { 1962 ipstat.ips_forward++; 1963 if (type == 0) { 1964 if (mtemp) 1965 ipflow_create(&fwd_ro, mtemp); 1966 goto done; 1967 } else { 1968 ipstat.ips_redirectsent++; 1969 } 1970 } else { 1971 ipstat.ips_cantforward++; 1972 } 1973 1974 if (mtemp == NULL) 1975 goto done; 1976 1977 /* 1978 * Errors that do not require generating ICMP message 1979 */ 1980 switch (error) { 1981 case ENOBUFS: 1982 /* 1983 * A router should not generate ICMP_SOURCEQUENCH as 1984 * required in RFC1812 Requirements for IP Version 4 Routers. 1985 * Source quench could be a big problem under DoS attacks, 1986 * or if the underlying interface is rate-limited. 1987 * Those who need source quench packets may re-enable them 1988 * via the net.inet.ip.sendsourcequench sysctl. 1989 */ 1990 if (!ip_sendsourcequench) 1991 goto done; 1992 break; 1993 1994 case EACCES: /* ipfw denied packet */ 1995 goto done; 1996 } 1997 1998 KASSERT((mtemp->m_flags & M_EXT) == 0 && 1999 mtemp->m_data == mtemp->m_pktdat, 2000 ("ip_forward invalid mtemp2")); 2001 mcopy = m_copym(mtemp, 0, mtemp->m_len, MB_DONTWAIT); 2002 if (mcopy == NULL) 2003 goto done; 2004 2005 /* 2006 * Send ICMP message. 2007 */ 2008 switch (error) { 2009 case 0: /* forwarded, but need redirect */ 2010 /* type, code set above */ 2011 break; 2012 2013 case ENETUNREACH: /* shouldn't happen, checked above */ 2014 case EHOSTUNREACH: 2015 case ENETDOWN: 2016 case EHOSTDOWN: 2017 default: 2018 type = ICMP_UNREACH; 2019 code = ICMP_UNREACH_HOST; 2020 break; 2021 2022 case EMSGSIZE: 2023 type = ICMP_UNREACH; 2024 code = ICMP_UNREACH_NEEDFRAG; 2025 #ifdef IPSEC 2026 /* 2027 * If the packet is routed over IPsec tunnel, tell the 2028 * originator the tunnel MTU. 2029 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 2030 * XXX quickhack!!! 2031 */ 2032 if (fwd_ro.ro_rt != NULL) { 2033 struct secpolicy *sp = NULL; 2034 int ipsecerror; 2035 int ipsechdr; 2036 struct route *ro; 2037 2038 sp = ipsec4_getpolicybyaddr(mcopy, 2039 IPSEC_DIR_OUTBOUND, 2040 IP_FORWARDING, 2041 &ipsecerror); 2042 2043 if (sp == NULL) 2044 destmtu = fwd_ro.ro_rt->rt_ifp->if_mtu; 2045 else { 2046 /* count IPsec header size */ 2047 ipsechdr = ipsec4_hdrsiz(mcopy, 2048 IPSEC_DIR_OUTBOUND, 2049 NULL); 2050 2051 /* 2052 * find the correct route for outer IPv4 2053 * header, compute tunnel MTU. 2054 * 2055 */ 2056 if (sp->req != NULL && sp->req->sav != NULL && 2057 sp->req->sav->sah != NULL) { 2058 ro = &sp->req->sav->sah->sa_route; 2059 if (ro->ro_rt != NULL && 2060 ro->ro_rt->rt_ifp != NULL) { 2061 destmtu = 2062 ro->ro_rt->rt_ifp->if_mtu; 2063 destmtu -= ipsechdr; 2064 } 2065 } 2066 2067 key_freesp(sp); 2068 } 2069 } 2070 #elif FAST_IPSEC 2071 /* 2072 * If the packet is routed over IPsec tunnel, tell the 2073 * originator the tunnel MTU. 2074 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 2075 * XXX quickhack!!! 2076 */ 2077 if (fwd_ro.ro_rt != NULL) { 2078 struct secpolicy *sp = NULL; 2079 int ipsecerror; 2080 int ipsechdr; 2081 struct route *ro; 2082 2083 sp = ipsec_getpolicybyaddr(mcopy, 2084 IPSEC_DIR_OUTBOUND, 2085 IP_FORWARDING, 2086 &ipsecerror); 2087 2088 if (sp == NULL) 2089 destmtu = fwd_ro.ro_rt->rt_ifp->if_mtu; 2090 else { 2091 /* count IPsec header size */ 2092 ipsechdr = ipsec4_hdrsiz(mcopy, 2093 IPSEC_DIR_OUTBOUND, 2094 NULL); 2095 2096 /* 2097 * find the correct route for outer IPv4 2098 * header, compute tunnel MTU. 2099 */ 2100 2101 if (sp->req != NULL && 2102 sp->req->sav != NULL && 2103 sp->req->sav->sah != NULL) { 2104 ro = &sp->req->sav->sah->sa_route; 2105 if (ro->ro_rt != NULL && 2106 ro->ro_rt->rt_ifp != NULL) { 2107 destmtu = 2108 ro->ro_rt->rt_ifp->if_mtu; 2109 destmtu -= ipsechdr; 2110 } 2111 } 2112 2113 KEY_FREESP(&sp); 2114 } 2115 } 2116 #else /* !IPSEC && !FAST_IPSEC */ 2117 if (fwd_ro.ro_rt != NULL) 2118 destmtu = fwd_ro.ro_rt->rt_ifp->if_mtu; 2119 #endif /*IPSEC*/ 2120 ipstat.ips_cantfrag++; 2121 break; 2122 2123 case ENOBUFS: 2124 type = ICMP_SOURCEQUENCH; 2125 code = 0; 2126 break; 2127 2128 case EACCES: /* ipfw denied packet */ 2129 panic("ip_forward EACCES should not reach"); 2130 } 2131 icmp_error(mcopy, type, code, dest, destmtu); 2132 done: 2133 if (mtemp != NULL) 2134 m_tag_delete_chain(mtemp); 2135 if (fwd_ro.ro_rt != NULL) 2136 RTFREE(fwd_ro.ro_rt); 2137 } 2138 2139 void 2140 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 2141 struct mbuf *m) 2142 { 2143 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 2144 struct timeval tv; 2145 2146 microtime(&tv); 2147 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 2148 SCM_TIMESTAMP, SOL_SOCKET); 2149 if (*mp) 2150 mp = &(*mp)->m_next; 2151 } 2152 if (inp->inp_flags & INP_RECVDSTADDR) { 2153 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 2154 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 2155 if (*mp) 2156 mp = &(*mp)->m_next; 2157 } 2158 if (inp->inp_flags & INP_RECVTTL) { 2159 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 2160 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 2161 if (*mp) 2162 mp = &(*mp)->m_next; 2163 } 2164 #ifdef notyet 2165 /* XXX 2166 * Moving these out of udp_input() made them even more broken 2167 * than they already were. 2168 */ 2169 /* options were tossed already */ 2170 if (inp->inp_flags & INP_RECVOPTS) { 2171 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2172 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2173 if (*mp) 2174 mp = &(*mp)->m_next; 2175 } 2176 /* ip_srcroute doesn't do what we want here, need to fix */ 2177 if (inp->inp_flags & INP_RECVRETOPTS) { 2178 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 2179 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2180 if (*mp) 2181 mp = &(*mp)->m_next; 2182 } 2183 #endif 2184 if (inp->inp_flags & INP_RECVIF) { 2185 struct ifnet *ifp; 2186 struct sdlbuf { 2187 struct sockaddr_dl sdl; 2188 u_char pad[32]; 2189 } sdlbuf; 2190 struct sockaddr_dl *sdp; 2191 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 2192 2193 if (((ifp = m->m_pkthdr.rcvif)) && 2194 ((ifp->if_index != 0) && (ifp->if_index <= if_index))) { 2195 sdp = IF_LLSOCKADDR(ifp); 2196 /* 2197 * Change our mind and don't try copy. 2198 */ 2199 if ((sdp->sdl_family != AF_LINK) || 2200 (sdp->sdl_len > sizeof(sdlbuf))) { 2201 goto makedummy; 2202 } 2203 bcopy(sdp, sdl2, sdp->sdl_len); 2204 } else { 2205 makedummy: 2206 sdl2->sdl_len = 2207 offsetof(struct sockaddr_dl, sdl_data[0]); 2208 sdl2->sdl_family = AF_LINK; 2209 sdl2->sdl_index = 0; 2210 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 2211 } 2212 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 2213 IP_RECVIF, IPPROTO_IP); 2214 if (*mp) 2215 mp = &(*mp)->m_next; 2216 } 2217 } 2218 2219 /* 2220 * XXX these routines are called from the upper part of the kernel. 2221 * 2222 * They could also be moved to ip_mroute.c, since all the RSVP 2223 * handling is done there already. 2224 */ 2225 int 2226 ip_rsvp_init(struct socket *so) 2227 { 2228 if (so->so_type != SOCK_RAW || 2229 so->so_proto->pr_protocol != IPPROTO_RSVP) 2230 return EOPNOTSUPP; 2231 2232 if (ip_rsvpd != NULL) 2233 return EADDRINUSE; 2234 2235 ip_rsvpd = so; 2236 /* 2237 * This may seem silly, but we need to be sure we don't over-increment 2238 * the RSVP counter, in case something slips up. 2239 */ 2240 if (!ip_rsvp_on) { 2241 ip_rsvp_on = 1; 2242 rsvp_on++; 2243 } 2244 2245 return 0; 2246 } 2247 2248 int 2249 ip_rsvp_done(void) 2250 { 2251 ip_rsvpd = NULL; 2252 /* 2253 * This may seem silly, but we need to be sure we don't over-decrement 2254 * the RSVP counter, in case something slips up. 2255 */ 2256 if (ip_rsvp_on) { 2257 ip_rsvp_on = 0; 2258 rsvp_on--; 2259 } 2260 return 0; 2261 } 2262 2263 int 2264 rsvp_input(struct mbuf **mp, int *offp, int proto) 2265 { 2266 struct mbuf *m = *mp; 2267 2268 *mp = NULL; 2269 2270 if (rsvp_input_p) { /* call the real one if loaded */ 2271 *mp = m; 2272 rsvp_input_p(mp, offp, proto); 2273 return(IPPROTO_DONE); 2274 } 2275 2276 /* Can still get packets with rsvp_on = 0 if there is a local member 2277 * of the group to which the RSVP packet is addressed. But in this 2278 * case we want to throw the packet away. 2279 */ 2280 2281 if (!rsvp_on) { 2282 m_freem(m); 2283 return(IPPROTO_DONE); 2284 } 2285 2286 if (ip_rsvpd != NULL) { 2287 *mp = m; 2288 rip_input(mp, offp, proto); 2289 return(IPPROTO_DONE); 2290 } 2291 /* Drop the packet */ 2292 m_freem(m); 2293 return(IPPROTO_DONE); 2294 } 2295