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