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