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 * $Id: ip_input.c,v 1.62 1997/05/11 18:05:37 tegge Exp $ 35 * $ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $ 36 */ 37 38 #define _IP_VHL 39 40 #include "opt_ipfw.h" 41 42 #include <stddef.h> 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/domain.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/errno.h> 52 #include <sys/time.h> 53 #include <sys/kernel.h> 54 #include <sys/syslog.h> 55 #include <sys/sysctl.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/route.h> 60 #include <net/netisr.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/in_var.h> 65 #include <netinet/ip.h> 66 #include <netinet/in_pcb.h> 67 #include <netinet/in_var.h> 68 #include <netinet/ip_var.h> 69 #include <netinet/ip_icmp.h> 70 #include <machine/in_cksum.h> 71 72 #include <sys/socketvar.h> 73 74 #ifdef IPFIREWALL 75 #include <netinet/ip_fw.h> 76 #endif 77 78 int rsvp_on = 0; 79 static int ip_rsvp_on; 80 struct socket *ip_rsvpd; 81 82 static int ipforwarding = 0; 83 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 84 &ipforwarding, 0, ""); 85 86 static int ipsendredirects = 1; /* XXX */ 87 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 88 &ipsendredirects, 0, ""); 89 90 int ip_defttl = IPDEFTTL; 91 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 92 &ip_defttl, 0, ""); 93 94 static int ip_dosourceroute = 0; 95 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 96 &ip_dosourceroute, 0, ""); 97 #ifdef DIAGNOSTIC 98 static int ipprintfs = 0; 99 #endif 100 101 extern struct domain inetdomain; 102 extern struct protosw inetsw[]; 103 u_char ip_protox[IPPROTO_MAX]; 104 static int ipqmaxlen = IFQ_MAXLEN; 105 struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 106 struct ifqueue ipintrq; 107 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD, 108 &ipintrq.ifq_maxlen, 0, ""); 109 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 110 &ipintrq.ifq_drops, 0, ""); 111 112 struct ipstat ipstat; 113 static struct ipq ipq; 114 115 #ifdef IPCTL_DEFMTU 116 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 117 &ip_mtu, 0, ""); 118 #endif 119 120 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 121 #undef COMPAT_IPFW 122 #define COMPAT_IPFW 1 123 #else 124 #undef COMPAT_IPFW 125 #endif 126 127 #ifdef COMPAT_IPFW 128 /* Firewall hooks */ 129 ip_fw_chk_t *ip_fw_chk_ptr; 130 ip_fw_ctl_t *ip_fw_ctl_ptr; 131 132 /* IP Network Address Translation (NAT) hooks */ 133 ip_nat_t *ip_nat_ptr; 134 ip_nat_ctl_t *ip_nat_ctl_ptr; 135 #endif 136 137 #if defined(IPFILTER_LKM) || defined(IPFILTER) 138 int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **)); 139 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL; 140 #endif 141 142 143 /* 144 * We need to save the IP options in case a protocol wants to respond 145 * to an incoming packet over the same route if the packet got here 146 * using IP source routing. This allows connection establishment and 147 * maintenance when the remote end is on a network that is not known 148 * to us. 149 */ 150 static int ip_nhops = 0; 151 static struct ip_srcrt { 152 struct in_addr dst; /* final destination */ 153 char nop; /* one NOP to align */ 154 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 155 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 156 } ip_srcrt; 157 158 #ifdef IPDIVERT 159 /* 160 * Shared variable between ip_input() and ip_reass() to communicate 161 * about which packets, once assembled from fragments, get diverted, 162 * and to which port. 163 */ 164 static u_short frag_divert_port; 165 #endif 166 167 static void save_rte __P((u_char *, struct in_addr)); 168 static void ip_deq __P((struct ipasfrag *)); 169 static int ip_dooptions __P((struct mbuf *)); 170 static void ip_enq __P((struct ipasfrag *, struct ipasfrag *)); 171 static void ip_forward __P((struct mbuf *, int)); 172 static void ip_freef __P((struct ipq *)); 173 static struct ip * 174 ip_reass __P((struct ipasfrag *, struct ipq *)); 175 static struct in_ifaddr * 176 ip_rtaddr __P((struct in_addr)); 177 static void ipintr __P((void)); 178 /* 179 * IP initialization: fill in IP protocol switch table. 180 * All protocols not implemented in kernel go to raw IP protocol handler. 181 */ 182 void 183 ip_init() 184 { 185 register struct protosw *pr; 186 register int i; 187 188 TAILQ_INIT(&in_ifaddrhead); 189 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 190 if (pr == 0) 191 panic("ip_init"); 192 for (i = 0; i < IPPROTO_MAX; i++) 193 ip_protox[i] = pr - inetsw; 194 for (pr = inetdomain.dom_protosw; 195 pr < inetdomain.dom_protoswNPROTOSW; pr++) 196 if (pr->pr_domain->dom_family == PF_INET && 197 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 198 ip_protox[pr->pr_protocol] = pr - inetsw; 199 ipq.next = ipq.prev = &ipq; 200 ip_id = time.tv_sec & 0xffff; 201 ipintrq.ifq_maxlen = ipqmaxlen; 202 #ifdef IPFIREWALL 203 ip_fw_init(); 204 #endif 205 #ifdef IPNAT 206 ip_nat_init(); 207 #endif 208 209 } 210 211 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 212 static struct route ipforward_rt; 213 214 /* 215 * Ip input routine. Checksum and byte swap header. If fragmented 216 * try to reassemble. Process options. Pass to next level. 217 */ 218 void 219 ip_input(struct mbuf *m) 220 { 221 struct ip *ip; 222 struct ipq *fp; 223 struct in_ifaddr *ia; 224 int hlen; 225 u_short sum; 226 227 #ifdef DIAGNOSTIC 228 if ((m->m_flags & M_PKTHDR) == 0) 229 panic("ip_input no HDR"); 230 #endif 231 /* 232 * If no IP addresses have been set yet but the interfaces 233 * are receiving, can't do anything with incoming packets yet. 234 * XXX This is broken! We should be able to receive broadcasts 235 * and multicasts even without any local addresses configured. 236 */ 237 if (TAILQ_EMPTY(&in_ifaddrhead)) 238 goto bad; 239 ipstat.ips_total++; 240 241 if (m->m_pkthdr.len < sizeof(struct ip)) 242 goto tooshort; 243 244 #ifdef DIAGNOSTIC 245 if (m->m_len < sizeof(struct ip)) 246 panic("ipintr mbuf too short"); 247 #endif 248 249 if (m->m_len < sizeof (struct ip) && 250 (m = m_pullup(m, sizeof (struct ip))) == 0) { 251 ipstat.ips_toosmall++; 252 return; 253 } 254 ip = mtod(m, struct ip *); 255 256 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 257 ipstat.ips_badvers++; 258 goto bad; 259 } 260 261 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 262 if (hlen < sizeof(struct ip)) { /* minimum header length */ 263 ipstat.ips_badhlen++; 264 goto bad; 265 } 266 if (hlen > m->m_len) { 267 if ((m = m_pullup(m, hlen)) == 0) { 268 ipstat.ips_badhlen++; 269 return; 270 } 271 ip = mtod(m, struct ip *); 272 } 273 if (hlen == sizeof(struct ip)) { 274 sum = in_cksum_hdr(ip); 275 } else { 276 sum = in_cksum(m, hlen); 277 } 278 if (sum) { 279 ipstat.ips_badsum++; 280 goto bad; 281 } 282 283 /* 284 * Convert fields to host representation. 285 */ 286 NTOHS(ip->ip_len); 287 if (ip->ip_len < hlen) { 288 ipstat.ips_badlen++; 289 goto bad; 290 } 291 NTOHS(ip->ip_id); 292 NTOHS(ip->ip_off); 293 294 /* 295 * Check that the amount of data in the buffers 296 * is as at least much as the IP header would have us expect. 297 * Trim mbufs if longer than we expect. 298 * Drop packet if shorter than we expect. 299 */ 300 if (m->m_pkthdr.len < ip->ip_len) { 301 tooshort: 302 ipstat.ips_tooshort++; 303 goto bad; 304 } 305 if (m->m_pkthdr.len > ip->ip_len) { 306 if (m->m_len == m->m_pkthdr.len) { 307 m->m_len = ip->ip_len; 308 m->m_pkthdr.len = ip->ip_len; 309 } else 310 m_adj(m, ip->ip_len - m->m_pkthdr.len); 311 } 312 /* 313 * IpHack's section. 314 * Right now when no processing on packet has done 315 * and it is still fresh out of network we do our black 316 * deals with it. 317 * - Firewall: deny/allow/divert 318 * - Xlate: translate packet's addr/port (NAT). 319 * - Wrap: fake packet's addr/port <unimpl.> 320 * - Encapsulate: put it in another IP and send out. <unimp.> 321 */ 322 #if defined(IPFILTER) || defined(IPFILTER_LKM) 323 /* 324 * Check if we want to allow this packet to be processed. 325 * Consider it to be bad if not. 326 */ 327 if (fr_check) { 328 struct mbuf *m1 = m; 329 330 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) 331 return; 332 ip = mtod(m = m1, struct ip *); 333 } 334 #endif 335 #ifdef COMPAT_IPFW 336 if (ip_fw_chk_ptr) { 337 #ifdef IPDIVERT 338 u_short port; 339 340 port = (*ip_fw_chk_ptr)(&ip, hlen, NULL, ip_divert_ignore, &m); 341 ip_divert_ignore = 0; 342 if (port) { /* Divert packet */ 343 frag_divert_port = port; 344 goto ours; 345 } 346 #else 347 /* If ipfw says divert, we have to just drop packet */ 348 if ((*ip_fw_chk_ptr)(&ip, hlen, NULL, 0, &m)) { 349 m_freem(m); 350 m = NULL; 351 } 352 #endif 353 if (!m) 354 return; 355 } 356 357 if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN)) 358 return; 359 #endif 360 361 /* 362 * Process options and, if not destined for us, 363 * ship it on. ip_dooptions returns 1 when an 364 * error was detected (causing an icmp message 365 * to be sent and the original packet to be freed). 366 */ 367 ip_nhops = 0; /* for source routed packets */ 368 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 369 return; 370 371 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 372 * matter if it is destined to another node, or whether it is 373 * a multicast one, RSVP wants it! and prevents it from being forwarded 374 * anywhere else. Also checks if the rsvp daemon is running before 375 * grabbing the packet. 376 */ 377 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 378 goto ours; 379 380 /* 381 * Check our list of addresses, to see if the packet is for us. 382 */ 383 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) { 384 #define satosin(sa) ((struct sockaddr_in *)(sa)) 385 386 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 387 goto ours; 388 #ifdef BOOTP_COMPAT 389 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 390 goto ours; 391 #endif 392 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 393 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 394 ip->ip_dst.s_addr) 395 goto ours; 396 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 397 goto ours; 398 } 399 } 400 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 401 struct in_multi *inm; 402 if (ip_mrouter) { 403 /* 404 * If we are acting as a multicast router, all 405 * incoming multicast packets are passed to the 406 * kernel-level multicast forwarding function. 407 * The packet is returned (relatively) intact; if 408 * ip_mforward() returns a non-zero value, the packet 409 * must be discarded, else it may be accepted below. 410 * 411 * (The IP ident field is put in the same byte order 412 * as expected when ip_mforward() is called from 413 * ip_output().) 414 */ 415 ip->ip_id = htons(ip->ip_id); 416 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 417 ipstat.ips_cantforward++; 418 m_freem(m); 419 return; 420 } 421 ip->ip_id = ntohs(ip->ip_id); 422 423 /* 424 * The process-level routing demon needs to receive 425 * all multicast IGMP packets, whether or not this 426 * host belongs to their destination groups. 427 */ 428 if (ip->ip_p == IPPROTO_IGMP) 429 goto ours; 430 ipstat.ips_forward++; 431 } 432 /* 433 * See if we belong to the destination multicast group on the 434 * arrival interface. 435 */ 436 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 437 if (inm == NULL) { 438 ipstat.ips_notmember++; 439 m_freem(m); 440 return; 441 } 442 goto ours; 443 } 444 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 445 goto ours; 446 if (ip->ip_dst.s_addr == INADDR_ANY) 447 goto ours; 448 449 /* 450 * Not for us; forward if possible and desirable. 451 */ 452 if (ipforwarding == 0) { 453 ipstat.ips_cantforward++; 454 m_freem(m); 455 } else 456 ip_forward(m, 0); 457 return; 458 459 ours: 460 461 /* 462 * If offset or IP_MF are set, must reassemble. 463 * Otherwise, nothing need be done. 464 * (We could look in the reassembly queue to see 465 * if the packet was previously fragmented, 466 * but it's not worth the time; just let them time out.) 467 */ 468 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 469 if (m->m_flags & M_EXT) { /* XXX */ 470 if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 471 ipstat.ips_toosmall++; 472 #ifdef IPDIVERT 473 frag_divert_port = 0; 474 #endif 475 return; 476 } 477 ip = mtod(m, struct ip *); 478 } 479 /* 480 * Look for queue of fragments 481 * of this datagram. 482 */ 483 for (fp = ipq.next; fp != &ipq; fp = fp->next) 484 if (ip->ip_id == fp->ipq_id && 485 ip->ip_src.s_addr == fp->ipq_src.s_addr && 486 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 487 ip->ip_p == fp->ipq_p) 488 goto found; 489 fp = 0; 490 found: 491 492 /* 493 * Adjust ip_len to not reflect header, 494 * set ip_mff if more fragments are expected, 495 * convert offset of this to bytes. 496 */ 497 ip->ip_len -= hlen; 498 ((struct ipasfrag *)ip)->ipf_mff &= ~1; 499 if (ip->ip_off & IP_MF) 500 ((struct ipasfrag *)ip)->ipf_mff |= 1; 501 ip->ip_off <<= 3; 502 503 /* 504 * If datagram marked as having more fragments 505 * or if this is not the first fragment, 506 * attempt reassembly; if it succeeds, proceed. 507 */ 508 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 509 ipstat.ips_fragments++; 510 ip = ip_reass((struct ipasfrag *)ip, fp); 511 if (ip == 0) 512 return; 513 ipstat.ips_reassembled++; 514 m = dtom(ip); 515 } else 516 if (fp) 517 ip_freef(fp); 518 } else 519 ip->ip_len -= hlen; 520 521 #ifdef IPDIVERT 522 /* 523 * Divert reassembled packets to the divert protocol if required 524 */ 525 if (frag_divert_port) { 526 ipstat.ips_delivered++; 527 ip_divert_port = frag_divert_port; 528 frag_divert_port = 0; 529 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen); 530 return; 531 } 532 #endif 533 534 /* 535 * Switch out to protocol's input routine. 536 */ 537 ipstat.ips_delivered++; 538 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 539 return; 540 bad: 541 m_freem(m); 542 } 543 544 /* 545 * IP software interrupt routine - to go away sometime soon 546 */ 547 static void 548 ipintr(void) 549 { 550 int s; 551 struct mbuf *m; 552 553 while(1) { 554 s = splimp(); 555 IF_DEQUEUE(&ipintrq, m); 556 splx(s); 557 if (m == 0) 558 return; 559 ip_input(m); 560 } 561 } 562 563 NETISR_SET(NETISR_IP, ipintr); 564 565 /* 566 * Take incoming datagram fragment and try to 567 * reassemble it into whole datagram. If a chain for 568 * reassembly of this datagram already exists, then it 569 * is given as fp; otherwise have to make a chain. 570 */ 571 static struct ip * 572 ip_reass(ip, fp) 573 register struct ipasfrag *ip; 574 register struct ipq *fp; 575 { 576 register struct mbuf *m = dtom(ip); 577 register struct ipasfrag *q; 578 struct mbuf *t; 579 int hlen = ip->ip_hl << 2; 580 int i, next; 581 582 /* 583 * Presence of header sizes in mbufs 584 * would confuse code below. 585 */ 586 m->m_data += hlen; 587 m->m_len -= hlen; 588 589 /* 590 * If first fragment to arrive, create a reassembly queue. 591 */ 592 if (fp == 0) { 593 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 594 goto dropfrag; 595 fp = mtod(t, struct ipq *); 596 insque(fp, &ipq); 597 fp->ipq_ttl = IPFRAGTTL; 598 fp->ipq_p = ip->ip_p; 599 fp->ipq_id = ip->ip_id; 600 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 601 fp->ipq_src = ((struct ip *)ip)->ip_src; 602 fp->ipq_dst = ((struct ip *)ip)->ip_dst; 603 #ifdef IPDIVERT 604 fp->ipq_divert = 0; 605 #endif 606 q = (struct ipasfrag *)fp; 607 goto insert; 608 } 609 610 /* 611 * Find a segment which begins after this one does. 612 */ 613 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 614 if (q->ip_off > ip->ip_off) 615 break; 616 617 /* 618 * If there is a preceding segment, it may provide some of 619 * our data already. If so, drop the data from the incoming 620 * segment. If it provides all of our data, drop us. 621 */ 622 if (q->ipf_prev != (struct ipasfrag *)fp) { 623 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 624 if (i > 0) { 625 if (i >= ip->ip_len) 626 goto dropfrag; 627 m_adj(dtom(ip), i); 628 ip->ip_off += i; 629 ip->ip_len -= i; 630 } 631 } 632 633 /* 634 * While we overlap succeeding segments trim them or, 635 * if they are completely covered, dequeue them. 636 */ 637 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 638 struct mbuf *m0; 639 640 i = (ip->ip_off + ip->ip_len) - q->ip_off; 641 if (i < q->ip_len) { 642 q->ip_len -= i; 643 q->ip_off += i; 644 m_adj(dtom(q), i); 645 break; 646 } 647 m0 = dtom(q); 648 q = q->ipf_next; 649 ip_deq(q->ipf_prev); 650 m_freem(m0); 651 } 652 653 insert: 654 655 #ifdef IPDIVERT 656 /* 657 * Any fragment diverting causes the whole packet to divert 658 */ 659 if (frag_divert_port != 0) 660 fp->ipq_divert = frag_divert_port; 661 frag_divert_port = 0; 662 #endif 663 664 /* 665 * Stick new segment in its place; 666 * check for complete reassembly. 667 */ 668 ip_enq(ip, q->ipf_prev); 669 next = 0; 670 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 671 if (q->ip_off != next) 672 return (0); 673 next += q->ip_len; 674 } 675 if (q->ipf_prev->ipf_mff & 1) 676 return (0); 677 678 /* 679 * Reassembly is complete. Make sure the packet is a sane size. 680 */ 681 if (next + (IP_VHL_HL(((struct ip *)fp->ipq_next)->ip_vhl) << 2) 682 > IP_MAXPACKET) { 683 ipstat.ips_toolong++; 684 ip_freef(fp); 685 return (0); 686 } 687 688 /* 689 * Concatenate fragments. 690 */ 691 q = fp->ipq_next; 692 m = dtom(q); 693 t = m->m_next; 694 m->m_next = 0; 695 m_cat(m, t); 696 q = q->ipf_next; 697 while (q != (struct ipasfrag *)fp) { 698 t = dtom(q); 699 q = q->ipf_next; 700 m_cat(m, t); 701 } 702 703 #ifdef IPDIVERT 704 /* 705 * Record divert port for packet, if any 706 */ 707 frag_divert_port = fp->ipq_divert; 708 #endif 709 710 /* 711 * Create header for new ip packet by 712 * modifying header of first packet; 713 * dequeue and discard fragment reassembly header. 714 * Make header visible. 715 */ 716 ip = fp->ipq_next; 717 ip->ip_len = next; 718 ip->ipf_mff &= ~1; 719 ((struct ip *)ip)->ip_src = fp->ipq_src; 720 ((struct ip *)ip)->ip_dst = fp->ipq_dst; 721 remque(fp); 722 (void) m_free(dtom(fp)); 723 m = dtom(ip); 724 m->m_len += (ip->ip_hl << 2); 725 m->m_data -= (ip->ip_hl << 2); 726 /* some debugging cruft by sklower, below, will go away soon */ 727 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 728 register int plen = 0; 729 for (t = m; m; m = m->m_next) 730 plen += m->m_len; 731 t->m_pkthdr.len = plen; 732 } 733 return ((struct ip *)ip); 734 735 dropfrag: 736 ipstat.ips_fragdropped++; 737 m_freem(m); 738 return (0); 739 } 740 741 /* 742 * Free a fragment reassembly header and all 743 * associated datagrams. 744 */ 745 static void 746 ip_freef(fp) 747 struct ipq *fp; 748 { 749 register struct ipasfrag *q, *p; 750 751 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 752 p = q->ipf_next; 753 ip_deq(q); 754 m_freem(dtom(q)); 755 } 756 remque(fp); 757 (void) m_free(dtom(fp)); 758 } 759 760 /* 761 * Put an ip fragment on a reassembly chain. 762 * Like insque, but pointers in middle of structure. 763 */ 764 static void 765 ip_enq(p, prev) 766 register struct ipasfrag *p, *prev; 767 { 768 769 p->ipf_prev = prev; 770 p->ipf_next = prev->ipf_next; 771 prev->ipf_next->ipf_prev = p; 772 prev->ipf_next = p; 773 } 774 775 /* 776 * To ip_enq as remque is to insque. 777 */ 778 static void 779 ip_deq(p) 780 register struct ipasfrag *p; 781 { 782 783 p->ipf_prev->ipf_next = p->ipf_next; 784 p->ipf_next->ipf_prev = p->ipf_prev; 785 } 786 787 /* 788 * IP timer processing; 789 * if a timer expires on a reassembly 790 * queue, discard it. 791 */ 792 void 793 ip_slowtimo() 794 { 795 register struct ipq *fp; 796 int s = splnet(); 797 798 fp = ipq.next; 799 if (fp == 0) { 800 splx(s); 801 return; 802 } 803 while (fp != &ipq) { 804 --fp->ipq_ttl; 805 fp = fp->next; 806 if (fp->prev->ipq_ttl == 0) { 807 ipstat.ips_fragtimeout++; 808 ip_freef(fp->prev); 809 } 810 } 811 splx(s); 812 } 813 814 /* 815 * Drain off all datagram fragments. 816 */ 817 void 818 ip_drain() 819 { 820 while (ipq.next != &ipq) { 821 ipstat.ips_fragdropped++; 822 ip_freef(ipq.next); 823 } 824 825 in_rtqdrain(); 826 } 827 828 /* 829 * Do option processing on a datagram, 830 * possibly discarding it if bad options are encountered, 831 * or forwarding it if source-routed. 832 * Returns 1 if packet has been forwarded/freed, 833 * 0 if the packet should be processed further. 834 */ 835 static int 836 ip_dooptions(m) 837 struct mbuf *m; 838 { 839 register struct ip *ip = mtod(m, struct ip *); 840 register u_char *cp; 841 register struct ip_timestamp *ipt; 842 register struct in_ifaddr *ia; 843 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 844 struct in_addr *sin, dst; 845 n_time ntime; 846 847 dst = ip->ip_dst; 848 cp = (u_char *)(ip + 1); 849 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 850 for (; cnt > 0; cnt -= optlen, cp += optlen) { 851 opt = cp[IPOPT_OPTVAL]; 852 if (opt == IPOPT_EOL) 853 break; 854 if (opt == IPOPT_NOP) 855 optlen = 1; 856 else { 857 optlen = cp[IPOPT_OLEN]; 858 if (optlen <= 0 || optlen > cnt) { 859 code = &cp[IPOPT_OLEN] - (u_char *)ip; 860 goto bad; 861 } 862 } 863 switch (opt) { 864 865 default: 866 break; 867 868 /* 869 * Source routing with record. 870 * Find interface with current destination address. 871 * If none on this machine then drop if strictly routed, 872 * or do nothing if loosely routed. 873 * Record interface address and bring up next address 874 * component. If strictly routed make sure next 875 * address is on directly accessible net. 876 */ 877 case IPOPT_LSRR: 878 case IPOPT_SSRR: 879 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 880 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 881 goto bad; 882 } 883 ipaddr.sin_addr = ip->ip_dst; 884 ia = (struct in_ifaddr *) 885 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 886 if (ia == 0) { 887 if (opt == IPOPT_SSRR) { 888 type = ICMP_UNREACH; 889 code = ICMP_UNREACH_SRCFAIL; 890 goto bad; 891 } 892 /* 893 * Loose routing, and not at next destination 894 * yet; nothing to do except forward. 895 */ 896 break; 897 } 898 off--; /* 0 origin */ 899 if (off > optlen - sizeof(struct in_addr)) { 900 /* 901 * End of source route. Should be for us. 902 */ 903 save_rte(cp, ip->ip_src); 904 break; 905 } 906 907 if (!ip_dosourceroute) { 908 char buf[4*sizeof "123"]; 909 strcpy(buf, inet_ntoa(ip->ip_dst)); 910 911 log(LOG_WARNING, 912 "attempted source route from %s to %s\n", 913 inet_ntoa(ip->ip_src), buf); 914 type = ICMP_UNREACH; 915 code = ICMP_UNREACH_SRCFAIL; 916 goto bad; 917 } 918 919 /* 920 * locate outgoing interface 921 */ 922 (void)memcpy(&ipaddr.sin_addr, cp + off, 923 sizeof(ipaddr.sin_addr)); 924 925 if (opt == IPOPT_SSRR) { 926 #define INA struct in_ifaddr * 927 #define SA struct sockaddr * 928 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 929 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 930 } else 931 ia = ip_rtaddr(ipaddr.sin_addr); 932 if (ia == 0) { 933 type = ICMP_UNREACH; 934 code = ICMP_UNREACH_SRCFAIL; 935 goto bad; 936 } 937 ip->ip_dst = ipaddr.sin_addr; 938 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 939 sizeof(struct in_addr)); 940 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 941 /* 942 * Let ip_intr's mcast routing check handle mcast pkts 943 */ 944 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 945 break; 946 947 case IPOPT_RR: 948 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 949 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 950 goto bad; 951 } 952 /* 953 * If no space remains, ignore. 954 */ 955 off--; /* 0 origin */ 956 if (off > optlen - sizeof(struct in_addr)) 957 break; 958 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 959 sizeof(ipaddr.sin_addr)); 960 /* 961 * locate outgoing interface; if we're the destination, 962 * use the incoming interface (should be same). 963 */ 964 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 965 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 966 type = ICMP_UNREACH; 967 code = ICMP_UNREACH_HOST; 968 goto bad; 969 } 970 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 971 sizeof(struct in_addr)); 972 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 973 break; 974 975 case IPOPT_TS: 976 code = cp - (u_char *)ip; 977 ipt = (struct ip_timestamp *)cp; 978 if (ipt->ipt_len < 5) 979 goto bad; 980 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 981 if (++ipt->ipt_oflw == 0) 982 goto bad; 983 break; 984 } 985 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 986 switch (ipt->ipt_flg) { 987 988 case IPOPT_TS_TSONLY: 989 break; 990 991 case IPOPT_TS_TSANDADDR: 992 if (ipt->ipt_ptr + sizeof(n_time) + 993 sizeof(struct in_addr) > ipt->ipt_len) 994 goto bad; 995 ipaddr.sin_addr = dst; 996 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 997 m->m_pkthdr.rcvif); 998 if (ia == 0) 999 continue; 1000 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1001 sizeof(struct in_addr)); 1002 ipt->ipt_ptr += sizeof(struct in_addr); 1003 break; 1004 1005 case IPOPT_TS_PRESPEC: 1006 if (ipt->ipt_ptr + sizeof(n_time) + 1007 sizeof(struct in_addr) > ipt->ipt_len) 1008 goto bad; 1009 (void)memcpy(&ipaddr.sin_addr, sin, 1010 sizeof(struct in_addr)); 1011 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1012 continue; 1013 ipt->ipt_ptr += sizeof(struct in_addr); 1014 break; 1015 1016 default: 1017 goto bad; 1018 } 1019 ntime = iptime(); 1020 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1021 sizeof(n_time)); 1022 ipt->ipt_ptr += sizeof(n_time); 1023 } 1024 } 1025 if (forward) { 1026 ip_forward(m, 1); 1027 return (1); 1028 } 1029 return (0); 1030 bad: 1031 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ 1032 icmp_error(m, type, code, 0, 0); 1033 ipstat.ips_badoptions++; 1034 return (1); 1035 } 1036 1037 /* 1038 * Given address of next destination (final or next hop), 1039 * return internet address info of interface to be used to get there. 1040 */ 1041 static struct in_ifaddr * 1042 ip_rtaddr(dst) 1043 struct in_addr dst; 1044 { 1045 register struct sockaddr_in *sin; 1046 1047 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1048 1049 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1050 if (ipforward_rt.ro_rt) { 1051 RTFREE(ipforward_rt.ro_rt); 1052 ipforward_rt.ro_rt = 0; 1053 } 1054 sin->sin_family = AF_INET; 1055 sin->sin_len = sizeof(*sin); 1056 sin->sin_addr = dst; 1057 1058 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1059 } 1060 if (ipforward_rt.ro_rt == 0) 1061 return ((struct in_ifaddr *)0); 1062 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1063 } 1064 1065 /* 1066 * Save incoming source route for use in replies, 1067 * to be picked up later by ip_srcroute if the receiver is interested. 1068 */ 1069 void 1070 save_rte(option, dst) 1071 u_char *option; 1072 struct in_addr dst; 1073 { 1074 unsigned olen; 1075 1076 olen = option[IPOPT_OLEN]; 1077 #ifdef DIAGNOSTIC 1078 if (ipprintfs) 1079 printf("save_rte: olen %d\n", olen); 1080 #endif 1081 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1082 return; 1083 bcopy(option, ip_srcrt.srcopt, olen); 1084 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1085 ip_srcrt.dst = dst; 1086 } 1087 1088 /* 1089 * Retrieve incoming source route for use in replies, 1090 * in the same form used by setsockopt. 1091 * The first hop is placed before the options, will be removed later. 1092 */ 1093 struct mbuf * 1094 ip_srcroute() 1095 { 1096 register struct in_addr *p, *q; 1097 register struct mbuf *m; 1098 1099 if (ip_nhops == 0) 1100 return ((struct mbuf *)0); 1101 m = m_get(M_DONTWAIT, MT_SOOPTS); 1102 if (m == 0) 1103 return ((struct mbuf *)0); 1104 1105 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1106 1107 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1108 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1109 OPTSIZ; 1110 #ifdef DIAGNOSTIC 1111 if (ipprintfs) 1112 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1113 #endif 1114 1115 /* 1116 * First save first hop for return route 1117 */ 1118 p = &ip_srcrt.route[ip_nhops - 1]; 1119 *(mtod(m, struct in_addr *)) = *p--; 1120 #ifdef DIAGNOSTIC 1121 if (ipprintfs) 1122 printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 1123 #endif 1124 1125 /* 1126 * Copy option fields and padding (nop) to mbuf. 1127 */ 1128 ip_srcrt.nop = IPOPT_NOP; 1129 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1130 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1131 &ip_srcrt.nop, OPTSIZ); 1132 q = (struct in_addr *)(mtod(m, caddr_t) + 1133 sizeof(struct in_addr) + OPTSIZ); 1134 #undef OPTSIZ 1135 /* 1136 * Record return path as an IP source route, 1137 * reversing the path (pointers are now aligned). 1138 */ 1139 while (p >= ip_srcrt.route) { 1140 #ifdef DIAGNOSTIC 1141 if (ipprintfs) 1142 printf(" %lx", ntohl(q->s_addr)); 1143 #endif 1144 *q++ = *p--; 1145 } 1146 /* 1147 * Last hop goes to final destination. 1148 */ 1149 *q = ip_srcrt.dst; 1150 #ifdef DIAGNOSTIC 1151 if (ipprintfs) 1152 printf(" %lx\n", ntohl(q->s_addr)); 1153 #endif 1154 return (m); 1155 } 1156 1157 /* 1158 * Strip out IP options, at higher 1159 * level protocol in the kernel. 1160 * Second argument is buffer to which options 1161 * will be moved, and return value is their length. 1162 * XXX should be deleted; last arg currently ignored. 1163 */ 1164 void 1165 ip_stripoptions(m, mopt) 1166 register struct mbuf *m; 1167 struct mbuf *mopt; 1168 { 1169 register int i; 1170 struct ip *ip = mtod(m, struct ip *); 1171 register caddr_t opts; 1172 int olen; 1173 1174 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1175 opts = (caddr_t)(ip + 1); 1176 i = m->m_len - (sizeof (struct ip) + olen); 1177 bcopy(opts + olen, opts, (unsigned)i); 1178 m->m_len -= olen; 1179 if (m->m_flags & M_PKTHDR) 1180 m->m_pkthdr.len -= olen; 1181 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1182 } 1183 1184 u_char inetctlerrmap[PRC_NCMDS] = { 1185 0, 0, 0, 0, 1186 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1187 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1188 EMSGSIZE, EHOSTUNREACH, 0, 0, 1189 0, 0, 0, 0, 1190 ENOPROTOOPT 1191 }; 1192 1193 /* 1194 * Forward a packet. If some error occurs return the sender 1195 * an icmp packet. Note we can't always generate a meaningful 1196 * icmp message because icmp doesn't have a large enough repertoire 1197 * of codes and types. 1198 * 1199 * If not forwarding, just drop the packet. This could be confusing 1200 * if ipforwarding was zero but some routing protocol was advancing 1201 * us as a gateway to somewhere. However, we must let the routing 1202 * protocol deal with that. 1203 * 1204 * The srcrt parameter indicates whether the packet is being forwarded 1205 * via a source route. 1206 */ 1207 static void 1208 ip_forward(m, srcrt) 1209 struct mbuf *m; 1210 int srcrt; 1211 { 1212 register struct ip *ip = mtod(m, struct ip *); 1213 register struct sockaddr_in *sin; 1214 register struct rtentry *rt; 1215 int error, type = 0, code = 0; 1216 struct mbuf *mcopy; 1217 n_long dest; 1218 struct ifnet *destifp; 1219 1220 dest = 0; 1221 #ifdef DIAGNOSTIC 1222 if (ipprintfs) 1223 printf("forward: src %lx dst %lx ttl %x\n", 1224 ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl); 1225 #endif 1226 1227 1228 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1229 ipstat.ips_cantforward++; 1230 m_freem(m); 1231 return; 1232 } 1233 HTONS(ip->ip_id); 1234 if (ip->ip_ttl <= IPTTLDEC) { 1235 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1236 return; 1237 } 1238 ip->ip_ttl -= IPTTLDEC; 1239 1240 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1241 if ((rt = ipforward_rt.ro_rt) == 0 || 1242 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1243 if (ipforward_rt.ro_rt) { 1244 RTFREE(ipforward_rt.ro_rt); 1245 ipforward_rt.ro_rt = 0; 1246 } 1247 sin->sin_family = AF_INET; 1248 sin->sin_len = sizeof(*sin); 1249 sin->sin_addr = ip->ip_dst; 1250 1251 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1252 if (ipforward_rt.ro_rt == 0) { 1253 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1254 return; 1255 } 1256 rt = ipforward_rt.ro_rt; 1257 } 1258 1259 /* 1260 * Save at most 64 bytes of the packet in case 1261 * we need to generate an ICMP message to the src. 1262 */ 1263 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1264 1265 /* 1266 * If forwarding packet using same interface that it came in on, 1267 * perhaps should send a redirect to sender to shortcut a hop. 1268 * Only send redirect if source is sending directly to us, 1269 * and if packet was not source routed (or has any options). 1270 * Also, don't send redirect if forwarding using a default route 1271 * or a route modified by a redirect. 1272 */ 1273 #define satosin(sa) ((struct sockaddr_in *)(sa)) 1274 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1275 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1276 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1277 ipsendredirects && !srcrt) { 1278 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1279 u_long src = ntohl(ip->ip_src.s_addr); 1280 1281 if (RTA(rt) && 1282 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1283 if (rt->rt_flags & RTF_GATEWAY) 1284 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1285 else 1286 dest = ip->ip_dst.s_addr; 1287 /* Router requirements says to only send host redirects */ 1288 type = ICMP_REDIRECT; 1289 code = ICMP_REDIRECT_HOST; 1290 #ifdef DIAGNOSTIC 1291 if (ipprintfs) 1292 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1293 #endif 1294 } 1295 } 1296 1297 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1298 IP_FORWARDING, 0); 1299 if (error) 1300 ipstat.ips_cantforward++; 1301 else { 1302 ipstat.ips_forward++; 1303 if (type) 1304 ipstat.ips_redirectsent++; 1305 else { 1306 if (mcopy) 1307 m_freem(mcopy); 1308 return; 1309 } 1310 } 1311 if (mcopy == NULL) 1312 return; 1313 destifp = NULL; 1314 1315 switch (error) { 1316 1317 case 0: /* forwarded, but need redirect */ 1318 /* type, code set above */ 1319 break; 1320 1321 case ENETUNREACH: /* shouldn't happen, checked above */ 1322 case EHOSTUNREACH: 1323 case ENETDOWN: 1324 case EHOSTDOWN: 1325 default: 1326 type = ICMP_UNREACH; 1327 code = ICMP_UNREACH_HOST; 1328 break; 1329 1330 case EMSGSIZE: 1331 type = ICMP_UNREACH; 1332 code = ICMP_UNREACH_NEEDFRAG; 1333 if (ipforward_rt.ro_rt) 1334 destifp = ipforward_rt.ro_rt->rt_ifp; 1335 ipstat.ips_cantfrag++; 1336 break; 1337 1338 case ENOBUFS: 1339 type = ICMP_SOURCEQUENCH; 1340 code = 0; 1341 break; 1342 } 1343 icmp_error(mcopy, type, code, dest, destifp); 1344 } 1345 1346 void 1347 ip_savecontrol(inp, mp, ip, m) 1348 register struct inpcb *inp; 1349 register struct mbuf **mp; 1350 register struct ip *ip; 1351 register struct mbuf *m; 1352 { 1353 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1354 struct timeval tv; 1355 1356 microtime(&tv); 1357 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1358 SCM_TIMESTAMP, SOL_SOCKET); 1359 if (*mp) 1360 mp = &(*mp)->m_next; 1361 } 1362 if (inp->inp_flags & INP_RECVDSTADDR) { 1363 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1364 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1365 if (*mp) 1366 mp = &(*mp)->m_next; 1367 } 1368 #ifdef notyet 1369 /* XXX 1370 * Moving these out of udp_input() made them even more broken 1371 * than they already were. 1372 */ 1373 /* options were tossed already */ 1374 if (inp->inp_flags & INP_RECVOPTS) { 1375 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1376 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1377 if (*mp) 1378 mp = &(*mp)->m_next; 1379 } 1380 /* ip_srcroute doesn't do what we want here, need to fix */ 1381 if (inp->inp_flags & INP_RECVRETOPTS) { 1382 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1383 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1384 if (*mp) 1385 mp = &(*mp)->m_next; 1386 } 1387 #endif 1388 if (inp->inp_flags & INP_RECVIF) { 1389 struct sockaddr_dl sdl; 1390 1391 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1392 sdl.sdl_family = AF_LINK; 1393 sdl.sdl_index = m->m_pkthdr.rcvif ? 1394 m->m_pkthdr.rcvif->if_index : 0; 1395 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1396 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1397 IP_RECVIF, IPPROTO_IP); 1398 if (*mp) 1399 mp = &(*mp)->m_next; 1400 } 1401 } 1402 1403 int 1404 ip_rsvp_init(struct socket *so) 1405 { 1406 if (so->so_type != SOCK_RAW || 1407 so->so_proto->pr_protocol != IPPROTO_RSVP) 1408 return EOPNOTSUPP; 1409 1410 if (ip_rsvpd != NULL) 1411 return EADDRINUSE; 1412 1413 ip_rsvpd = so; 1414 /* 1415 * This may seem silly, but we need to be sure we don't over-increment 1416 * the RSVP counter, in case something slips up. 1417 */ 1418 if (!ip_rsvp_on) { 1419 ip_rsvp_on = 1; 1420 rsvp_on++; 1421 } 1422 1423 return 0; 1424 } 1425 1426 int 1427 ip_rsvp_done(void) 1428 { 1429 ip_rsvpd = NULL; 1430 /* 1431 * This may seem silly, but we need to be sure we don't over-decrement 1432 * the RSVP counter, in case something slips up. 1433 */ 1434 if (ip_rsvp_on) { 1435 ip_rsvp_on = 0; 1436 rsvp_on--; 1437 } 1438 return 0; 1439 } 1440