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