ip_input.c revision 1.86
1/* $NetBSD: ip_input.c,v 1.86 1999/05/03 22:12:44 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Public Access Networks Corporation ("Panix"). It was developed under 9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Copyright (c) 1982, 1986, 1988, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 73 */ 74 75#include "opt_gateway.h" 76#include "opt_pfil_hooks.h" 77#include "opt_mrouting.h" 78 79#include <sys/param.h> 80#include <sys/systm.h> 81#include <sys/malloc.h> 82#include <sys/mbuf.h> 83#include <sys/domain.h> 84#include <sys/protosw.h> 85#include <sys/socket.h> 86#include <sys/socketvar.h> 87#include <sys/errno.h> 88#include <sys/time.h> 89#include <sys/kernel.h> 90#include <sys/proc.h> 91#include <sys/pool.h> 92 93#include <vm/vm.h> 94#include <sys/sysctl.h> 95 96#include <net/if.h> 97#include <net/if_dl.h> 98#include <net/route.h> 99#include <net/pfil.h> 100 101#include <netinet/in.h> 102#include <netinet/in_systm.h> 103#include <netinet/ip.h> 104#include <netinet/in_pcb.h> 105#include <netinet/in_var.h> 106#include <netinet/ip_var.h> 107#include <netinet/ip_icmp.h> 108 109#ifndef IPFORWARDING 110#ifdef GATEWAY 111#define IPFORWARDING 1 /* forward IP packets not for us */ 112#else /* GATEWAY */ 113#define IPFORWARDING 0 /* don't forward IP packets not for us */ 114#endif /* GATEWAY */ 115#endif /* IPFORWARDING */ 116#ifndef IPSENDREDIRECTS 117#define IPSENDREDIRECTS 1 118#endif 119#ifndef IPFORWSRCRT 120#define IPFORWSRCRT 1 /* forward source-routed packets */ 121#endif 122#ifndef IPALLOWSRCRT 123#define IPALLOWSRCRT 1 /* allow source-routed packets */ 124#endif 125#ifndef IPMTUDISC 126#define IPMTUDISC 0 127#endif 128#ifndef IPMTUDISCTIMEOUT 129#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 130#endif 131 132/* 133 * Note: DIRECTED_BROADCAST is handled this way so that previous 134 * configuration using this option will Just Work. 135 */ 136#ifndef IPDIRECTEDBCAST 137#ifdef DIRECTED_BROADCAST 138#define IPDIRECTEDBCAST 1 139#else 140#define IPDIRECTEDBCAST 0 141#endif /* DIRECTED_BROADCAST */ 142#endif /* IPDIRECTEDBCAST */ 143int ipforwarding = IPFORWARDING; 144int ipsendredirects = IPSENDREDIRECTS; 145int ip_defttl = IPDEFTTL; 146int ip_forwsrcrt = IPFORWSRCRT; 147int ip_directedbcast = IPDIRECTEDBCAST; 148int ip_allowsrcrt = IPALLOWSRCRT; 149int ip_mtudisc = IPMTUDISC; 150u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 151#ifdef DIAGNOSTIC 152int ipprintfs = 0; 153#endif 154 155struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 156 157extern struct domain inetdomain; 158extern struct protosw inetsw[]; 159u_char ip_protox[IPPROTO_MAX]; 160int ipqmaxlen = IFQ_MAXLEN; 161struct in_ifaddrhead in_ifaddr; 162struct in_ifaddrhashhead *in_ifaddrhashtbl; 163struct ifqueue ipintrq; 164struct ipstat ipstat; 165u_int16_t ip_id; 166int ip_defttl; 167 168struct ipqhead ipq; 169int ipq_locked; 170 171static __inline int ipq_lock_try __P((void)); 172static __inline void ipq_unlock __P((void)); 173 174static __inline int 175ipq_lock_try() 176{ 177 int s; 178 179 s = splimp(); 180 if (ipq_locked) { 181 splx(s); 182 return (0); 183 } 184 ipq_locked = 1; 185 splx(s); 186 return (1); 187} 188 189static __inline void 190ipq_unlock() 191{ 192 int s; 193 194 s = splimp(); 195 ipq_locked = 0; 196 splx(s); 197} 198 199#ifdef DIAGNOSTIC 200#define IPQ_LOCK() \ 201do { \ 202 if (ipq_lock_try() == 0) { \ 203 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \ 204 panic("ipq_lock"); \ 205 } \ 206} while (0) 207#define IPQ_LOCK_CHECK() \ 208do { \ 209 if (ipq_locked == 0) { \ 210 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \ 211 panic("ipq lock check"); \ 212 } \ 213} while (0) 214#else 215#define IPQ_LOCK() (void) ipq_lock_try() 216#define IPQ_LOCK_CHECK() /* nothing */ 217#endif 218 219#define IPQ_UNLOCK() ipq_unlock() 220 221struct pool ipqent_pool; 222 223/* 224 * We need to save the IP options in case a protocol wants to respond 225 * to an incoming packet over the same route if the packet got here 226 * using IP source routing. This allows connection establishment and 227 * maintenance when the remote end is on a network that is not known 228 * to us. 229 */ 230int ip_nhops = 0; 231static struct ip_srcrt { 232 struct in_addr dst; /* final destination */ 233 char nop; /* one NOP to align */ 234 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 235 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 236} ip_srcrt; 237 238static void save_rte __P((u_char *, struct in_addr)); 239 240/* 241 * IP initialization: fill in IP protocol switch table. 242 * All protocols not implemented in kernel go to raw IP protocol handler. 243 */ 244void 245ip_init() 246{ 247 register struct protosw *pr; 248 register int i; 249 250 pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", 251 0, NULL, NULL, M_IPQ); 252 253 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 254 if (pr == 0) 255 panic("ip_init"); 256 for (i = 0; i < IPPROTO_MAX; i++) 257 ip_protox[i] = pr - inetsw; 258 for (pr = inetdomain.dom_protosw; 259 pr < inetdomain.dom_protoswNPROTOSW; pr++) 260 if (pr->pr_domain->dom_family == PF_INET && 261 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 262 ip_protox[pr->pr_protocol] = pr - inetsw; 263 LIST_INIT(&ipq); 264 ip_id = time.tv_sec & 0xffff; 265 ipintrq.ifq_maxlen = ipqmaxlen; 266 TAILQ_INIT(&in_ifaddr); 267 in_ifaddrhashtbl = 268 hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash); 269 if (ip_mtudisc != 0) 270 ip_mtudisc_timeout_q = 271 rt_timer_queue_create(ip_mtudisc_timeout); 272#ifdef GATEWAY 273 ipflow_init(); 274#endif 275} 276 277struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 278struct route ipforward_rt; 279 280/* 281 * Ip input routine. Checksum and byte swap header. If fragmented 282 * try to reassemble. Process options. Pass to next level. 283 */ 284void 285ipintr() 286{ 287 register struct ip *ip = NULL; 288 register struct mbuf *m; 289 register struct ipq *fp; 290 register struct in_ifaddr *ia; 291 register struct ifaddr *ifa; 292 struct ipqent *ipqe; 293 int hlen = 0, mff, len, s; 294#ifdef PFIL_HOOKS 295 struct packet_filter_hook *pfh; 296 struct mbuf *m0; 297 int rv; 298#endif /* PFIL_HOOKS */ 299 300next: 301 /* 302 * Get next datagram off input queue and get IP header 303 * in first mbuf. 304 */ 305 s = splimp(); 306 IF_DEQUEUE(&ipintrq, m); 307 splx(s); 308 if (m == 0) 309 return; 310#ifdef DIAGNOSTIC 311 if ((m->m_flags & M_PKTHDR) == 0) 312 panic("ipintr no HDR"); 313#endif 314 /* 315 * If no IP addresses have been set yet but the interfaces 316 * are receiving, can't do anything with incoming packets yet. 317 */ 318 if (in_ifaddr.tqh_first == 0) 319 goto bad; 320 ipstat.ips_total++; 321 if (m->m_len < sizeof (struct ip) && 322 (m = m_pullup(m, sizeof (struct ip))) == 0) { 323 ipstat.ips_toosmall++; 324 goto next; 325 } 326 ip = mtod(m, struct ip *); 327 if (ip->ip_v != IPVERSION) { 328 ipstat.ips_badvers++; 329 goto bad; 330 } 331 hlen = ip->ip_hl << 2; 332 if (hlen < sizeof(struct ip)) { /* minimum header length */ 333 ipstat.ips_badhlen++; 334 goto bad; 335 } 336 if (hlen > m->m_len) { 337 if ((m = m_pullup(m, hlen)) == 0) { 338 ipstat.ips_badhlen++; 339 goto next; 340 } 341 ip = mtod(m, struct ip *); 342 } 343 /* 344 * we drop packets that have a multicast address as source 345 * as wanted by rfc 1112 346 */ 347 if (IN_MULTICAST(ip->ip_src.s_addr)) { 348 ipstat.ips_odropped++; 349 goto bad; 350 } 351 352 if (in_cksum(m, hlen) != 0) { 353 ipstat.ips_badsum++; 354 goto bad; 355 } 356 357 /* 358 * Convert fields to host representation. 359 */ 360 NTOHS(ip->ip_len); 361 NTOHS(ip->ip_off); 362 len = ip->ip_len; 363 364 /* 365 * Check for additional length bogosity 366 */ 367 if (len < hlen) { 368 ipstat.ips_badlen++; 369 goto bad; 370 } 371 372 /* 373 * Check that the amount of data in the buffers 374 * is as at least much as the IP header would have us expect. 375 * Trim mbufs if longer than we expect. 376 * Drop packet if shorter than we expect. 377 */ 378 if (m->m_pkthdr.len < len) { 379 ipstat.ips_tooshort++; 380 goto bad; 381 } 382 if (m->m_pkthdr.len > len) { 383 if (m->m_len == m->m_pkthdr.len) { 384 m->m_len = len; 385 m->m_pkthdr.len = len; 386 } else 387 m_adj(m, len - m->m_pkthdr.len); 388 } 389 390 /* 391 * Assume that we can create a fast-forward IP flow entry 392 * based on this packet. 393 */ 394 m->m_flags |= M_CANFASTFWD; 395 396#ifdef PFIL_HOOKS 397 /* 398 * Run through list of hooks for input packets. If there are any 399 * filters which require that additional packets in the flow are 400 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 401 * Note that filters must _never_ set this flag, as another filter 402 * in the list may have previously cleared it. 403 */ 404 m0 = m; 405 for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next) 406 if (pfh->pfil_func) { 407 rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0); 408 if (rv) 409 goto next; 410 m = m0; 411 if (m == NULL) 412 goto next; 413 ip = mtod(m, struct ip *); 414 } 415#endif /* PFIL_HOOKS */ 416 417 /* 418 * Process options and, if not destined for us, 419 * ship it on. ip_dooptions returns 1 when an 420 * error was detected (causing an icmp message 421 * to be sent and the original packet to be freed). 422 */ 423 ip_nhops = 0; /* for source routed packets */ 424 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 425 goto next; 426 427 /* 428 * Check our list of addresses, to see if the packet is for us. 429 */ 430 INADDR_TO_IA(ip->ip_dst, ia); 431 if (ia != NULL) 432 goto ours; 433 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 434 for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first; 435 ifa != NULL; ifa = ifa->ifa_list.tqe_next) { 436 if (ifa->ifa_addr->sa_family != AF_INET) continue; 437 ia = ifatoia(ifa); 438 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || 439 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || 440 /* 441 * Look for all-0's host part (old broadcast addr), 442 * either for subnet or net. 443 */ 444 ip->ip_dst.s_addr == ia->ia_subnet || 445 ip->ip_dst.s_addr == ia->ia_net) 446 goto ours; 447 /* 448 * An interface with IP address zero accepts 449 * all packets that arrive on that interface. 450 */ 451 if (in_nullhost(ia->ia_addr.sin_addr)) 452 goto ours; 453 } 454 } 455 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 456 struct in_multi *inm; 457#ifdef MROUTING 458 extern struct socket *ip_mrouter; 459 460 if (m->m_flags & M_EXT) { 461 if ((m = m_pullup(m, hlen)) == 0) { 462 ipstat.ips_toosmall++; 463 goto next; 464 } 465 ip = mtod(m, struct ip *); 466 } 467 468 if (ip_mrouter) { 469 /* 470 * If we are acting as a multicast router, all 471 * incoming multicast packets are passed to the 472 * kernel-level multicast forwarding function. 473 * The packet is returned (relatively) intact; if 474 * ip_mforward() returns a non-zero value, the packet 475 * must be discarded, else it may be accepted below. 476 * 477 * (The IP ident field is put in the same byte order 478 * as expected when ip_mforward() is called from 479 * ip_output().) 480 */ 481 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 482 ipstat.ips_cantforward++; 483 m_freem(m); 484 goto next; 485 } 486 487 /* 488 * The process-level routing demon needs to receive 489 * all multicast IGMP packets, whether or not this 490 * host belongs to their destination groups. 491 */ 492 if (ip->ip_p == IPPROTO_IGMP) 493 goto ours; 494 ipstat.ips_forward++; 495 } 496#endif 497 /* 498 * See if we belong to the destination multicast group on the 499 * arrival interface. 500 */ 501 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 502 if (inm == NULL) { 503 ipstat.ips_cantforward++; 504 m_freem(m); 505 goto next; 506 } 507 goto ours; 508 } 509 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 510 in_nullhost(ip->ip_dst)) 511 goto ours; 512 513 /* 514 * Not for us; forward if possible and desirable. 515 */ 516 if (ipforwarding == 0) { 517 ipstat.ips_cantforward++; 518 m_freem(m); 519 } else 520 ip_forward(m, 0); 521 goto next; 522 523ours: 524 /* 525 * If offset or IP_MF are set, must reassemble. 526 * Otherwise, nothing need be done. 527 * (We could look in the reassembly queue to see 528 * if the packet was previously fragmented, 529 * but it's not worth the time; just let them time out.) 530 */ 531 if (ip->ip_off & ~(IP_DF|IP_RF)) { 532 /* 533 * Look for queue of fragments 534 * of this datagram. 535 */ 536 IPQ_LOCK(); 537 for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next) 538 if (ip->ip_id == fp->ipq_id && 539 in_hosteq(ip->ip_src, fp->ipq_src) && 540 in_hosteq(ip->ip_dst, fp->ipq_dst) && 541 ip->ip_p == fp->ipq_p) 542 goto found; 543 fp = 0; 544found: 545 546 /* 547 * Adjust ip_len to not reflect header, 548 * set ipqe_mff if more fragments are expected, 549 * convert offset of this to bytes. 550 */ 551 ip->ip_len -= hlen; 552 mff = (ip->ip_off & IP_MF) != 0; 553 if (mff) { 554 /* 555 * Make sure that fragments have a data length 556 * that's a non-zero multiple of 8 bytes. 557 */ 558 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 559 ipstat.ips_badfrags++; 560 IPQ_UNLOCK(); 561 goto bad; 562 } 563 } 564 ip->ip_off <<= 3; 565 566 /* 567 * If datagram marked as having more fragments 568 * or if this is not the first fragment, 569 * attempt reassembly; if it succeeds, proceed. 570 */ 571 if (mff || ip->ip_off) { 572 ipstat.ips_fragments++; 573 ipqe = pool_get(&ipqent_pool, PR_NOWAIT); 574 if (ipqe == NULL) { 575 ipstat.ips_rcvmemdrop++; 576 IPQ_UNLOCK(); 577 goto bad; 578 } 579 ipqe->ipqe_mff = mff; 580 ipqe->ipqe_m = m; 581 ipqe->ipqe_ip = ip; 582 m = ip_reass(ipqe, fp); 583 if (m == 0) { 584 IPQ_UNLOCK(); 585 goto next; 586 } 587 ipstat.ips_reassembled++; 588 ip = mtod(m, struct ip *); 589 hlen = ip->ip_hl << 2; 590 ip->ip_len += hlen; 591 } else 592 if (fp) 593 ip_freef(fp); 594 IPQ_UNLOCK(); 595 } 596 597 /* 598 * Switch out to protocol's input routine. 599 */ 600#if IFA_STATS 601 ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len; 602#endif 603 ipstat.ips_delivered++; 604 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 605 goto next; 606bad: 607 m_freem(m); 608 goto next; 609} 610 611/* 612 * Take incoming datagram fragment and try to 613 * reassemble it into whole datagram. If a chain for 614 * reassembly of this datagram already exists, then it 615 * is given as fp; otherwise have to make a chain. 616 */ 617struct mbuf * 618ip_reass(ipqe, fp) 619 register struct ipqent *ipqe; 620 register struct ipq *fp; 621{ 622 register struct mbuf *m = ipqe->ipqe_m; 623 register struct ipqent *nq, *p, *q; 624 struct ip *ip; 625 struct mbuf *t; 626 int hlen = ipqe->ipqe_ip->ip_hl << 2; 627 int i, next; 628 629 IPQ_LOCK_CHECK(); 630 631 /* 632 * Presence of header sizes in mbufs 633 * would confuse code below. 634 */ 635 m->m_data += hlen; 636 m->m_len -= hlen; 637 638 /* 639 * If first fragment to arrive, create a reassembly queue. 640 */ 641 if (fp == 0) { 642 MALLOC(fp, struct ipq *, sizeof (struct ipq), 643 M_FTABLE, M_NOWAIT); 644 if (fp == NULL) 645 goto dropfrag; 646 LIST_INSERT_HEAD(&ipq, fp, ipq_q); 647 fp->ipq_ttl = IPFRAGTTL; 648 fp->ipq_p = ipqe->ipqe_ip->ip_p; 649 fp->ipq_id = ipqe->ipqe_ip->ip_id; 650 LIST_INIT(&fp->ipq_fragq); 651 fp->ipq_src = ipqe->ipqe_ip->ip_src; 652 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 653 p = NULL; 654 goto insert; 655 } 656 657 /* 658 * Find a segment which begins after this one does. 659 */ 660 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL; 661 p = q, q = q->ipqe_q.le_next) 662 if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off) 663 break; 664 665 /* 666 * If there is a preceding segment, it may provide some of 667 * our data already. If so, drop the data from the incoming 668 * segment. If it provides all of our data, drop us. 669 */ 670 if (p != NULL) { 671 i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len - 672 ipqe->ipqe_ip->ip_off; 673 if (i > 0) { 674 if (i >= ipqe->ipqe_ip->ip_len) 675 goto dropfrag; 676 m_adj(ipqe->ipqe_m, i); 677 ipqe->ipqe_ip->ip_off += i; 678 ipqe->ipqe_ip->ip_len -= i; 679 } 680 } 681 682 /* 683 * While we overlap succeeding segments trim them or, 684 * if they are completely covered, dequeue them. 685 */ 686 for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len > 687 q->ipqe_ip->ip_off; q = nq) { 688 i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) - 689 q->ipqe_ip->ip_off; 690 if (i < q->ipqe_ip->ip_len) { 691 q->ipqe_ip->ip_len -= i; 692 q->ipqe_ip->ip_off += i; 693 m_adj(q->ipqe_m, i); 694 break; 695 } 696 nq = q->ipqe_q.le_next; 697 m_freem(q->ipqe_m); 698 LIST_REMOVE(q, ipqe_q); 699 pool_put(&ipqent_pool, q); 700 } 701 702insert: 703 /* 704 * Stick new segment in its place; 705 * check for complete reassembly. 706 */ 707 if (p == NULL) { 708 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 709 } else { 710 LIST_INSERT_AFTER(p, ipqe, ipqe_q); 711 } 712 next = 0; 713 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL; 714 p = q, q = q->ipqe_q.le_next) { 715 if (q->ipqe_ip->ip_off != next) 716 return (0); 717 next += q->ipqe_ip->ip_len; 718 } 719 if (p->ipqe_mff) 720 return (0); 721 722 /* 723 * Reassembly is complete. Check for a bogus message size and 724 * concatenate fragments. 725 */ 726 q = fp->ipq_fragq.lh_first; 727 ip = q->ipqe_ip; 728 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 729 ipstat.ips_toolong++; 730 ip_freef(fp); 731 return (0); 732 } 733 m = q->ipqe_m; 734 t = m->m_next; 735 m->m_next = 0; 736 m_cat(m, t); 737 nq = q->ipqe_q.le_next; 738 pool_put(&ipqent_pool, q); 739 for (q = nq; q != NULL; q = nq) { 740 t = q->ipqe_m; 741 nq = q->ipqe_q.le_next; 742 pool_put(&ipqent_pool, q); 743 m_cat(m, t); 744 } 745 746 /* 747 * Create header for new ip packet by 748 * modifying header of first packet; 749 * dequeue and discard fragment reassembly header. 750 * Make header visible. 751 */ 752 ip->ip_len = next; 753 ip->ip_src = fp->ipq_src; 754 ip->ip_dst = fp->ipq_dst; 755 LIST_REMOVE(fp, ipq_q); 756 FREE(fp, M_FTABLE); 757 m->m_len += (ip->ip_hl << 2); 758 m->m_data -= (ip->ip_hl << 2); 759 /* some debugging cruft by sklower, below, will go away soon */ 760 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 761 register int plen = 0; 762 for (t = m; t; t = t->m_next) 763 plen += t->m_len; 764 m->m_pkthdr.len = plen; 765 } 766 return (m); 767 768dropfrag: 769 ipstat.ips_fragdropped++; 770 m_freem(m); 771 pool_put(&ipqent_pool, ipqe); 772 return (0); 773} 774 775/* 776 * Free a fragment reassembly header and all 777 * associated datagrams. 778 */ 779void 780ip_freef(fp) 781 struct ipq *fp; 782{ 783 register struct ipqent *q, *p; 784 785 IPQ_LOCK_CHECK(); 786 787 for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) { 788 p = q->ipqe_q.le_next; 789 m_freem(q->ipqe_m); 790 LIST_REMOVE(q, ipqe_q); 791 pool_put(&ipqent_pool, q); 792 } 793 LIST_REMOVE(fp, ipq_q); 794 FREE(fp, M_FTABLE); 795} 796 797/* 798 * IP timer processing; 799 * if a timer expires on a reassembly 800 * queue, discard it. 801 */ 802void 803ip_slowtimo() 804{ 805 register struct ipq *fp, *nfp; 806 int s = splsoftnet(); 807 808 IPQ_LOCK(); 809 for (fp = ipq.lh_first; fp != NULL; fp = nfp) { 810 nfp = fp->ipq_q.le_next; 811 if (--fp->ipq_ttl == 0) { 812 ipstat.ips_fragtimeout++; 813 ip_freef(fp); 814 } 815 } 816 IPQ_UNLOCK(); 817#ifdef GATEWAY 818 ipflow_slowtimo(); 819#endif 820 splx(s); 821} 822 823/* 824 * Drain off all datagram fragments. 825 */ 826void 827ip_drain() 828{ 829 830 /* 831 * We may be called from a device's interrupt context. If 832 * the ipq is already busy, just bail out now. 833 */ 834 if (ipq_lock_try() == 0) 835 return; 836 837 while (ipq.lh_first != NULL) { 838 ipstat.ips_fragdropped++; 839 ip_freef(ipq.lh_first); 840 } 841 842 IPQ_UNLOCK(); 843} 844 845/* 846 * Do option processing on a datagram, 847 * possibly discarding it if bad options are encountered, 848 * or forwarding it if source-routed. 849 * Returns 1 if packet has been forwarded/freed, 850 * 0 if the packet should be processed further. 851 */ 852int 853ip_dooptions(m) 854 struct mbuf *m; 855{ 856 register struct ip *ip = mtod(m, struct ip *); 857 register u_char *cp; 858 register struct ip_timestamp *ipt; 859 register struct in_ifaddr *ia; 860 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 861 struct in_addr *sin, dst; 862 n_time ntime; 863 864 dst = ip->ip_dst; 865 cp = (u_char *)(ip + 1); 866 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 867 for (; cnt > 0; cnt -= optlen, cp += optlen) { 868 opt = cp[IPOPT_OPTVAL]; 869 if (opt == IPOPT_EOL) 870 break; 871 if (opt == IPOPT_NOP) 872 optlen = 1; 873 else { 874 optlen = cp[IPOPT_OLEN]; 875 if (optlen <= 0 || optlen > cnt) { 876 code = &cp[IPOPT_OLEN] - (u_char *)ip; 877 goto bad; 878 } 879 } 880 switch (opt) { 881 882 default: 883 break; 884 885 /* 886 * Source routing with record. 887 * Find interface with current destination address. 888 * If none on this machine then drop if strictly routed, 889 * or do nothing if loosely routed. 890 * Record interface address and bring up next address 891 * component. If strictly routed make sure next 892 * address is on directly accessible net. 893 */ 894 case IPOPT_LSRR: 895 case IPOPT_SSRR: 896 if (ip_allowsrcrt == 0) { 897 type = ICMP_UNREACH; 898 code = ICMP_UNREACH_NET_PROHIB; 899 goto bad; 900 } 901 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 902 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 903 goto bad; 904 } 905 ipaddr.sin_addr = ip->ip_dst; 906 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 907 if (ia == 0) { 908 if (opt == IPOPT_SSRR) { 909 type = ICMP_UNREACH; 910 code = ICMP_UNREACH_SRCFAIL; 911 goto bad; 912 } 913 /* 914 * Loose routing, and not at next destination 915 * yet; nothing to do except forward. 916 */ 917 break; 918 } 919 off--; /* 0 origin */ 920 if (off > optlen - sizeof(struct in_addr)) { 921 /* 922 * End of source route. Should be for us. 923 */ 924 save_rte(cp, ip->ip_src); 925 break; 926 } 927 /* 928 * locate outgoing interface 929 */ 930 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 931 sizeof(ipaddr.sin_addr)); 932 if (opt == IPOPT_SSRR) { 933#define INA struct in_ifaddr * 934#define SA struct sockaddr * 935 ia = (INA)ifa_ifwithladdr((SA)&ipaddr); 936 } else 937 ia = ip_rtaddr(ipaddr.sin_addr); 938 if (ia == 0) { 939 type = ICMP_UNREACH; 940 code = ICMP_UNREACH_SRCFAIL; 941 goto bad; 942 } 943 ip->ip_dst = ipaddr.sin_addr; 944 bcopy((caddr_t)&ia->ia_addr.sin_addr, 945 (caddr_t)(cp + off), sizeof(struct in_addr)); 946 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 947 /* 948 * Let ip_intr's mcast routing check handle mcast pkts 949 */ 950 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 951 break; 952 953 case IPOPT_RR: 954 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 955 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 956 goto bad; 957 } 958 /* 959 * If no space remains, ignore. 960 */ 961 off--; /* 0 origin */ 962 if (off > optlen - sizeof(struct in_addr)) 963 break; 964 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 965 sizeof(ipaddr.sin_addr)); 966 /* 967 * locate outgoing interface; if we're the destination, 968 * use the incoming interface (should be same). 969 */ 970 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 971 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 972 type = ICMP_UNREACH; 973 code = ICMP_UNREACH_HOST; 974 goto bad; 975 } 976 bcopy((caddr_t)&ia->ia_addr.sin_addr, 977 (caddr_t)(cp + off), sizeof(struct in_addr)); 978 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 979 break; 980 981 case IPOPT_TS: 982 code = cp - (u_char *)ip; 983 ipt = (struct ip_timestamp *)cp; 984 if (ipt->ipt_len < 5) 985 goto bad; 986 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 987 if (++ipt->ipt_oflw == 0) 988 goto bad; 989 break; 990 } 991 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 992 switch (ipt->ipt_flg) { 993 994 case IPOPT_TS_TSONLY: 995 break; 996 997 case IPOPT_TS_TSANDADDR: 998 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 999 sizeof(struct in_addr) > ipt->ipt_len) 1000 goto bad; 1001 ipaddr.sin_addr = dst; 1002 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1003 m->m_pkthdr.rcvif); 1004 if (ia == 0) 1005 continue; 1006 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1007 (caddr_t)sin, sizeof(struct in_addr)); 1008 ipt->ipt_ptr += sizeof(struct in_addr); 1009 break; 1010 1011 case IPOPT_TS_PRESPEC: 1012 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1013 sizeof(struct in_addr) > ipt->ipt_len) 1014 goto bad; 1015 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 1016 sizeof(struct in_addr)); 1017 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1018 continue; 1019 ipt->ipt_ptr += sizeof(struct in_addr); 1020 break; 1021 1022 default: 1023 goto bad; 1024 } 1025 ntime = iptime(); 1026 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 1027 sizeof(n_time)); 1028 ipt->ipt_ptr += sizeof(n_time); 1029 } 1030 } 1031 if (forward) { 1032 if (ip_forwsrcrt == 0) { 1033 type = ICMP_UNREACH; 1034 code = ICMP_UNREACH_SRCFAIL; 1035 goto bad; 1036 } 1037 ip_forward(m, 1); 1038 return (1); 1039 } 1040 return (0); 1041bad: 1042 icmp_error(m, type, code, 0, 0); 1043 ipstat.ips_badoptions++; 1044 return (1); 1045} 1046 1047/* 1048 * Given address of next destination (final or next hop), 1049 * return internet address info of interface to be used to get there. 1050 */ 1051struct in_ifaddr * 1052ip_rtaddr(dst) 1053 struct in_addr dst; 1054{ 1055 register struct sockaddr_in *sin; 1056 1057 sin = satosin(&ipforward_rt.ro_dst); 1058 1059 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) { 1060 if (ipforward_rt.ro_rt) { 1061 RTFREE(ipforward_rt.ro_rt); 1062 ipforward_rt.ro_rt = 0; 1063 } 1064 sin->sin_family = AF_INET; 1065 sin->sin_len = sizeof(*sin); 1066 sin->sin_addr = dst; 1067 1068 rtalloc(&ipforward_rt); 1069 } 1070 if (ipforward_rt.ro_rt == 0) 1071 return ((struct in_ifaddr *)0); 1072 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1073} 1074 1075/* 1076 * Save incoming source route for use in replies, 1077 * to be picked up later by ip_srcroute if the receiver is interested. 1078 */ 1079void 1080save_rte(option, dst) 1081 u_char *option; 1082 struct in_addr dst; 1083{ 1084 unsigned olen; 1085 1086 olen = option[IPOPT_OLEN]; 1087#ifdef DIAGNOSTIC 1088 if (ipprintfs) 1089 printf("save_rte: olen %d\n", olen); 1090#endif 1091 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1092 return; 1093 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 1094 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1095 ip_srcrt.dst = dst; 1096} 1097 1098/* 1099 * Retrieve incoming source route for use in replies, 1100 * in the same form used by setsockopt. 1101 * The first hop is placed before the options, will be removed later. 1102 */ 1103struct mbuf * 1104ip_srcroute() 1105{ 1106 register struct in_addr *p, *q; 1107 register struct mbuf *m; 1108 1109 if (ip_nhops == 0) 1110 return ((struct mbuf *)0); 1111 m = m_get(M_DONTWAIT, MT_SOOPTS); 1112 if (m == 0) 1113 return ((struct mbuf *)0); 1114 1115#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1116 1117 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1118 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1119 OPTSIZ; 1120#ifdef DIAGNOSTIC 1121 if (ipprintfs) 1122 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1123#endif 1124 1125 /* 1126 * First save first hop for return route 1127 */ 1128 p = &ip_srcrt.route[ip_nhops - 1]; 1129 *(mtod(m, struct in_addr *)) = *p--; 1130#ifdef DIAGNOSTIC 1131 if (ipprintfs) 1132 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1133#endif 1134 1135 /* 1136 * Copy option fields and padding (nop) to mbuf. 1137 */ 1138 ip_srcrt.nop = IPOPT_NOP; 1139 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1140 bcopy((caddr_t)&ip_srcrt.nop, 1141 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 1142 q = (struct in_addr *)(mtod(m, caddr_t) + 1143 sizeof(struct in_addr) + OPTSIZ); 1144#undef OPTSIZ 1145 /* 1146 * Record return path as an IP source route, 1147 * reversing the path (pointers are now aligned). 1148 */ 1149 while (p >= ip_srcrt.route) { 1150#ifdef DIAGNOSTIC 1151 if (ipprintfs) 1152 printf(" %x", ntohl(q->s_addr)); 1153#endif 1154 *q++ = *p--; 1155 } 1156 /* 1157 * Last hop goes to final destination. 1158 */ 1159 *q = ip_srcrt.dst; 1160#ifdef DIAGNOSTIC 1161 if (ipprintfs) 1162 printf(" %x\n", ntohl(q->s_addr)); 1163#endif 1164 return (m); 1165} 1166 1167/* 1168 * Strip out IP options, at higher 1169 * level protocol in the kernel. 1170 * Second argument is buffer to which options 1171 * will be moved, and return value is their length. 1172 * XXX should be deleted; last arg currently ignored. 1173 */ 1174void 1175ip_stripoptions(m, mopt) 1176 register struct mbuf *m; 1177 struct mbuf *mopt; 1178{ 1179 register int i; 1180 struct ip *ip = mtod(m, struct ip *); 1181 register caddr_t opts; 1182 int olen; 1183 1184 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1185 opts = (caddr_t)(ip + 1); 1186 i = m->m_len - (sizeof (struct ip) + olen); 1187 bcopy(opts + olen, opts, (unsigned)i); 1188 m->m_len -= olen; 1189 if (m->m_flags & M_PKTHDR) 1190 m->m_pkthdr.len -= olen; 1191 ip->ip_len -= olen; 1192 ip->ip_hl = sizeof (struct ip) >> 2; 1193} 1194 1195int inetctlerrmap[PRC_NCMDS] = { 1196 0, 0, 0, 0, 1197 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1198 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1199 EMSGSIZE, EHOSTUNREACH, 0, 0, 1200 0, 0, 0, 0, 1201 ENOPROTOOPT 1202}; 1203 1204/* 1205 * Forward a packet. If some error occurs return the sender 1206 * an icmp packet. Note we can't always generate a meaningful 1207 * icmp message because icmp doesn't have a large enough repertoire 1208 * of codes and types. 1209 * 1210 * If not forwarding, just drop the packet. This could be confusing 1211 * if ipforwarding was zero but some routing protocol was advancing 1212 * us as a gateway to somewhere. However, we must let the routing 1213 * protocol deal with that. 1214 * 1215 * The srcrt parameter indicates whether the packet is being forwarded 1216 * via a source route. 1217 */ 1218void 1219ip_forward(m, srcrt) 1220 struct mbuf *m; 1221 int srcrt; 1222{ 1223 register struct ip *ip = mtod(m, struct ip *); 1224 register struct sockaddr_in *sin; 1225 register struct rtentry *rt; 1226 int error, type = 0, code = 0; 1227 struct mbuf *mcopy; 1228 n_long dest; 1229 struct ifnet *destifp; 1230 1231 dest = 0; 1232#ifdef DIAGNOSTIC 1233 if (ipprintfs) 1234 printf("forward: src %2.2x dst %2.2x ttl %x\n", 1235 ntohl(ip->ip_src.s_addr), 1236 ntohl(ip->ip_dst.s_addr), ip->ip_ttl); 1237#endif 1238 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1239 ipstat.ips_cantforward++; 1240 m_freem(m); 1241 return; 1242 } 1243 if (ip->ip_ttl <= IPTTLDEC) { 1244 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1245 return; 1246 } 1247 ip->ip_ttl -= IPTTLDEC; 1248 1249 sin = satosin(&ipforward_rt.ro_dst); 1250 if ((rt = ipforward_rt.ro_rt) == 0 || 1251 !in_hosteq(ip->ip_dst, sin->sin_addr)) { 1252 if (ipforward_rt.ro_rt) { 1253 RTFREE(ipforward_rt.ro_rt); 1254 ipforward_rt.ro_rt = 0; 1255 } 1256 sin->sin_family = AF_INET; 1257 sin->sin_len = sizeof(struct sockaddr_in); 1258 sin->sin_addr = ip->ip_dst; 1259 1260 rtalloc(&ipforward_rt); 1261 if (ipforward_rt.ro_rt == 0) { 1262 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1263 return; 1264 } 1265 rt = ipforward_rt.ro_rt; 1266 } 1267 1268 /* 1269 * Save at most 68 bytes of the packet in case 1270 * we need to generate an ICMP message to the src. 1271 */ 1272 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68)); 1273 1274 /* 1275 * If forwarding packet using same interface that it came in on, 1276 * perhaps should send a redirect to sender to shortcut a hop. 1277 * Only send redirect if source is sending directly to us, 1278 * and if packet was not source routed (or has any options). 1279 * Also, don't send redirect if forwarding using a default route 1280 * or a route modified by a redirect. 1281 */ 1282 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1283 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1284 !in_nullhost(satosin(rt_key(rt))->sin_addr) && 1285 ipsendredirects && !srcrt) { 1286 if (rt->rt_ifa && 1287 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1288 ifatoia(rt->rt_ifa)->ia_subnet) { 1289 if (rt->rt_flags & RTF_GATEWAY) 1290 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1291 else 1292 dest = ip->ip_dst.s_addr; 1293 /* 1294 * Router requirements says to only send host 1295 * redirects. 1296 */ 1297 type = ICMP_REDIRECT; 1298 code = ICMP_REDIRECT_HOST; 1299#ifdef DIAGNOSTIC 1300 if (ipprintfs) 1301 printf("redirect (%d) to %x\n", code, 1302 (u_int32_t)dest); 1303#endif 1304 } 1305 } 1306 1307 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1308 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0); 1309 if (error) 1310 ipstat.ips_cantforward++; 1311 else { 1312 ipstat.ips_forward++; 1313 if (type) 1314 ipstat.ips_redirectsent++; 1315 else { 1316 if (mcopy) { 1317#ifdef GATEWAY 1318 if (mcopy->m_flags & M_CANFASTFWD) 1319 ipflow_create(&ipforward_rt, mcopy); 1320#endif 1321 m_freem(mcopy); 1322 } 1323 return; 1324 } 1325 } 1326 if (mcopy == NULL) 1327 return; 1328 destifp = NULL; 1329 1330 switch (error) { 1331 1332 case 0: /* forwarded, but need redirect */ 1333 /* type, code set above */ 1334 break; 1335 1336 case ENETUNREACH: /* shouldn't happen, checked above */ 1337 case EHOSTUNREACH: 1338 case ENETDOWN: 1339 case EHOSTDOWN: 1340 default: 1341 type = ICMP_UNREACH; 1342 code = ICMP_UNREACH_HOST; 1343 break; 1344 1345 case EMSGSIZE: 1346 type = ICMP_UNREACH; 1347 code = ICMP_UNREACH_NEEDFRAG; 1348 if (ipforward_rt.ro_rt) 1349 destifp = ipforward_rt.ro_rt->rt_ifp; 1350 ipstat.ips_cantfrag++; 1351 break; 1352 1353 case ENOBUFS: 1354 type = ICMP_SOURCEQUENCH; 1355 code = 0; 1356 break; 1357 } 1358 icmp_error(mcopy, type, code, dest, destifp); 1359} 1360 1361void 1362ip_savecontrol(inp, mp, ip, m) 1363 register struct inpcb *inp; 1364 register struct mbuf **mp; 1365 register struct ip *ip; 1366 register struct mbuf *m; 1367{ 1368 1369 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1370 struct timeval tv; 1371 1372 microtime(&tv); 1373 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1374 SCM_TIMESTAMP, SOL_SOCKET); 1375 if (*mp) 1376 mp = &(*mp)->m_next; 1377 } 1378 if (inp->inp_flags & INP_RECVDSTADDR) { 1379 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1380 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1381 if (*mp) 1382 mp = &(*mp)->m_next; 1383 } 1384#ifdef notyet 1385 /* 1386 * XXX 1387 * Moving these out of udp_input() made them even more broken 1388 * than they already were. 1389 * - fenner@parc.xerox.com 1390 */ 1391 /* options were tossed already */ 1392 if (inp->inp_flags & INP_RECVOPTS) { 1393 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1394 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1395 if (*mp) 1396 mp = &(*mp)->m_next; 1397 } 1398 /* ip_srcroute doesn't do what we want here, need to fix */ 1399 if (inp->inp_flags & INP_RECVRETOPTS) { 1400 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1401 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1402 if (*mp) 1403 mp = &(*mp)->m_next; 1404 } 1405#endif 1406 if (inp->inp_flags & INP_RECVIF) { 1407 struct sockaddr_dl sdl; 1408 1409 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1410 sdl.sdl_family = AF_LINK; 1411 sdl.sdl_index = m->m_pkthdr.rcvif ? 1412 m->m_pkthdr.rcvif->if_index : 0; 1413 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1414 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1415 IP_RECVIF, IPPROTO_IP); 1416 if (*mp) 1417 mp = &(*mp)->m_next; 1418 } 1419} 1420 1421int 1422ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 1423 int *name; 1424 u_int namelen; 1425 void *oldp; 1426 size_t *oldlenp; 1427 void *newp; 1428 size_t newlen; 1429{ 1430 extern int subnetsarelocal; 1431 1432 int error, old; 1433 1434 /* All sysctl names at this level are terminal. */ 1435 if (namelen != 1) 1436 return (ENOTDIR); 1437 1438 switch (name[0]) { 1439 case IPCTL_FORWARDING: 1440 return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding)); 1441 case IPCTL_SENDREDIRECTS: 1442 return (sysctl_int(oldp, oldlenp, newp, newlen, 1443 &ipsendredirects)); 1444 case IPCTL_DEFTTL: 1445 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl)); 1446#ifdef notyet 1447 case IPCTL_DEFMTU: 1448 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 1449#endif 1450 case IPCTL_FORWSRCRT: 1451 /* Don't allow this to change in a secure environment. */ 1452 if (securelevel > 0) 1453 return (sysctl_rdint(oldp, oldlenp, newp, 1454 ip_forwsrcrt)); 1455 else 1456 return (sysctl_int(oldp, oldlenp, newp, newlen, 1457 &ip_forwsrcrt)); 1458 case IPCTL_DIRECTEDBCAST: 1459 return (sysctl_int(oldp, oldlenp, newp, newlen, 1460 &ip_directedbcast)); 1461 case IPCTL_ALLOWSRCRT: 1462 return (sysctl_int(oldp, oldlenp, newp, newlen, 1463 &ip_allowsrcrt)); 1464 case IPCTL_SUBNETSARELOCAL: 1465 return (sysctl_int(oldp, oldlenp, newp, newlen, 1466 &subnetsarelocal)); 1467 case IPCTL_MTUDISC: 1468 error = sysctl_int(oldp, oldlenp, newp, newlen, 1469 &ip_mtudisc); 1470 if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) { 1471 ip_mtudisc_timeout_q = 1472 rt_timer_queue_create(ip_mtudisc_timeout); 1473 } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) { 1474 rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE); 1475 ip_mtudisc_timeout_q = NULL; 1476 } 1477 return error; 1478 case IPCTL_ANONPORTMIN: 1479 old = anonportmin; 1480 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin); 1481 if (anonportmin >= anonportmax || anonportmin > 65535 1482#ifndef IPNOPRIVPORTS 1483 || anonportmin < IPPORT_RESERVED 1484#endif 1485 ) { 1486 anonportmin = old; 1487 return (EINVAL); 1488 } 1489 return (error); 1490 case IPCTL_ANONPORTMAX: 1491 old = anonportmax; 1492 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax); 1493 if (anonportmin >= anonportmax || anonportmax > 65535 1494#ifndef IPNOPRIVPORTS 1495 || anonportmax < IPPORT_RESERVED 1496#endif 1497 ) { 1498 anonportmax = old; 1499 return (EINVAL); 1500 } 1501 return (error); 1502 case IPCTL_MTUDISCTIMEOUT: 1503 error = sysctl_int(oldp, oldlenp, newp, newlen, 1504 &ip_mtudisc_timeout); 1505 if (ip_mtudisc_timeout_q != NULL) 1506 rt_timer_queue_change(ip_mtudisc_timeout_q, 1507 ip_mtudisc_timeout); 1508 return (error); 1509#ifdef GATEWAY 1510 case IPCTL_MAXFLOWS: 1511 { 1512 int s; 1513 1514 error = sysctl_int(oldp, oldlenp, newp, newlen, 1515 &ip_maxflows); 1516 s = splsoftnet(); 1517 ipflow_reap(0); 1518 splx(s); 1519 return (error); 1520 } 1521#endif 1522 default: 1523 return (EOPNOTSUPP); 1524 } 1525 /* NOTREACHED */ 1526} 1527