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