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