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