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