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