xref: /csrg-svn/sys/netinet/ip_input.c (revision 6475)
1*6475Sroot /*	ip_input.c	1.39	82/04/04	*/
24571Swnj 
34495Swnj #include "../h/param.h"
44543Swnj #include "../h/systm.h"
54640Swnj #include "../h/clock.h"
64640Swnj #include "../h/mbuf.h"
74898Swnj #include "../h/protosw.h"
84923Swnj #include "../h/socket.h"
95084Swnj #include "../net/in.h"
105084Swnj #include "../net/in_systm.h"
114951Swnj #include "../net/if.h"
125084Swnj #include "../net/ip.h"			/* belongs before in.h */
134898Swnj #include "../net/ip_var.h"
144801Swnj #include "../net/ip_icmp.h"
154801Swnj #include "../net/tcp.h"
164495Swnj 
174898Swnj u_char	ip_protox[IPPROTO_MAX];
186210Swnj int	ipqmaxlen = IFQ_MAXLEN;
196338Ssam struct	ifnet *ifinet;			/* first inet interface */
204898Swnj 
214801Swnj /*
225172Swnj  * IP initialization: fill in IP protocol switch table.
235161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
244801Swnj  */
254801Swnj ip_init()
264801Swnj {
274898Swnj 	register struct protosw *pr;
284898Swnj 	register int i;
294495Swnj 
304951Swnj COUNT(IP_INIT);
314898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
324898Swnj 	if (pr == 0)
334898Swnj 		panic("ip_init");
344898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
354898Swnj 		ip_protox[i] = pr - protosw;
364898Swnj 	for (pr = protosw; pr <= protoswLAST; pr++)
374898Swnj 		if (pr->pr_family == PF_INET &&
384898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
394898Swnj 			ip_protox[pr->pr_protocol] = pr - protosw;
404801Swnj 	ipq.next = ipq.prev = &ipq;
414801Swnj 	ip_id = time & 0xffff;
426210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
436338Ssam 	ifinet = if_ifwithaf(AF_INET);
444801Swnj }
454801Swnj 
464898Swnj u_char	ipcksum = 1;
474640Swnj struct	ip *ip_reass();
486411Ssam int	ipforwarding = 1;
496411Ssam int	ipprintfs = 0;
506338Ssam struct	sockaddr_in ipaddr = { AF_INET };
514640Swnj 
524640Swnj /*
534640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
544640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
554640Swnj  * Process options.  Pass to next level.
564640Swnj  */
575084Swnj ipintr()
584495Swnj {
594923Swnj 	register struct ip *ip;
605084Swnj 	register struct mbuf *m;
615217Swnj 	struct mbuf *m0, *mopt;
624640Swnj 	register int i;
634495Swnj 	register struct ipq *fp;
645084Swnj 	int hlen, s;
654495Swnj 
665084Swnj COUNT(IPINTR);
675084Swnj next:
684640Swnj 	/*
695084Swnj 	 * Get next datagram off input queue and get IP header
705084Swnj 	 * in first mbuf.
714640Swnj 	 */
725084Swnj 	s = splimp();
735084Swnj 	IF_DEQUEUE(&ipintrq, m);
745084Swnj 	splx(s);
755218Swnj 	if (m == 0)
765084Swnj 		return;
775306Sroot 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) &&
785306Sroot 	    (m = m_pullup(m, sizeof (struct ip))) == 0)
795306Sroot 		return;
804640Swnj 	ip = mtod(m, struct ip *);
815161Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
825306Sroot 		if ((m = m_pullup(m, hlen)) == 0)
835306Sroot 			return;
845161Swnj 		ip = mtod(m, struct ip *);
855161Swnj 	}
864951Swnj 	if (ipcksum)
875217Swnj 		if (ip->ip_sum = in_cksum(m, hlen)) {
885161Swnj 			printf("ip_sum %x\n", ip->ip_sum);	/* XXX */
894951Swnj 			ipstat.ips_badsum++;
904951Swnj 			goto bad;
914495Swnj 		}
924951Swnj 
935217Swnj #if vax
944951Swnj 	/*
954951Swnj 	 * Convert fields to host representation.
964951Swnj 	 */
974907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
984640Swnj 	ip->ip_id = ntohs(ip->ip_id);
994951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
1005217Swnj #endif
1014495Swnj 
1024543Swnj 	/*
1034640Swnj 	 * Check that the amount of data in the buffers
1044640Swnj 	 * is as at least much as the IP header would have us expect.
1054640Swnj 	 * Trim mbufs if longer than we expect.
1064640Swnj 	 * Drop packet if shorter than we expect.
1074543Swnj 	 */
108*6475Sroot 	i = -ip->ip_len;
1095161Swnj 	m0 = m;
110*6475Sroot 	for (;;) {
1114495Swnj 		i += m->m_len;
112*6475Sroot 		if (m->m_next == 0)
113*6475Sroot 			break;
114*6475Sroot 		m = m->m_next;
1156088Sroot 	}
116*6475Sroot 	if (i != 0) {
117*6475Sroot 		if (i < 0) {
1185161Swnj 			ipstat.ips_tooshort++;
1194951Swnj 			goto bad;
1205161Swnj 		}
121*6475Sroot 		if (i <= m->m_len)
122*6475Sroot 			m->m_len -= i;
123*6475Sroot 		else
124*6475Sroot 			m_adj(m0, -i);
1254495Swnj 	}
126*6475Sroot 	m = m0;
1274495Swnj 
1284640Swnj 	/*
1294640Swnj 	 * Process options and, if not destined for us,
1304640Swnj 	 * ship it on.
1314640Swnj 	 */
1324543Swnj 	if (hlen > sizeof (struct ip))
1334907Swnj 		ip_dooptions(ip);
1346210Swnj 
1356338Ssam 	/*
1366350Ssam 	 * Fast check on the first internet
1376350Ssam 	 * interface in the list.
1386338Ssam 	 */
1396338Ssam 	if (ifinet) {
1406338Ssam 		struct sockaddr_in *sin;
1416338Ssam 
1426338Ssam 		sin = (struct sockaddr_in *)&ifinet->if_addr;
1436338Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1446338Ssam 			goto ours;
1456350Ssam 		if ((ifinet->if_flags & IFF_BROADCAST) &&
1466350Ssam 		    sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1476350Ssam 			goto ours;
1486338Ssam 	}
1496338Ssam 	ipaddr.sin_addr = ip->ip_dst;
1506338Ssam 	if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) {
1516411Ssam 		if (ipprintfs)
1526411Ssam 			printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
1536411Ssam 				ip->ip_dst, ip->ip_ttl);
1546338Ssam 		if (ipforwarding == 0)
1556338Ssam 			goto bad;
1566338Ssam 		if (ip->ip_ttl < IPTTLDEC) {
1574907Swnj 			icmp_error(ip, ICMP_TIMXCEED, 0);
1585084Swnj 			goto next;
1594495Swnj 		}
1606338Ssam 		ip->ip_ttl -= IPTTLDEC;
1615217Swnj 		mopt = m_get(M_DONTWAIT);
1625217Swnj 		if (mopt == 0)
1635217Swnj 			goto bad;
1645217Swnj 		ip_stripoptions(ip, mopt);
1656338Ssam 
1666350Ssam 		/* last 0 here means no directed broadcast */
1676338Ssam 		(void) ip_output(m0, mopt, 0, 0);
1685084Swnj 		goto next;
1694543Swnj 	}
1704495Swnj 
1716338Ssam ours:
1724640Swnj 	/*
1734640Swnj 	 * Look for queue of fragments
1744640Swnj 	 * of this datagram.
1754640Swnj 	 */
1764640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1774640Swnj 		if (ip->ip_id == fp->ipq_id &&
1784640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1794640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1804640Swnj 		    ip->ip_p == fp->ipq_p)
1814640Swnj 			goto found;
1824640Swnj 	fp = 0;
1834640Swnj found:
1844495Swnj 
1854640Swnj 	/*
1864640Swnj 	 * Adjust ip_len to not reflect header,
1874640Swnj 	 * set ip_mff if more fragments are expected,
1884640Swnj 	 * convert offset of this to bytes.
1894640Swnj 	 */
1904640Swnj 	ip->ip_len -= hlen;
1914898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1924640Swnj 	if (ip->ip_off & IP_MF)
1934898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1944640Swnj 	ip->ip_off <<= 3;
1954495Swnj 
1964640Swnj 	/*
1974640Swnj 	 * If datagram marked as having more fragments
1984640Swnj 	 * or if this is not the first fragment,
1994640Swnj 	 * attempt reassembly; if it succeeds, proceed.
2004640Swnj 	 */
2014898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
2024898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
2034640Swnj 		if (ip == 0)
2045084Swnj 			goto next;
2054640Swnj 		hlen = ip->ip_hl << 2;
2064640Swnj 		m = dtom(ip);
2074640Swnj 	} else
2084640Swnj 		if (fp)
2094640Swnj 			(void) ip_freef(fp);
2104951Swnj 
2114951Swnj 	/*
2124951Swnj 	 * Switch out to protocol's input routine.
2134951Swnj 	 */
2144898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
2155084Swnj 	goto next;
2164951Swnj bad:
2174951Swnj 	m_freem(m);
2185084Swnj 	goto next;
2194640Swnj }
2204495Swnj 
2214640Swnj /*
2224640Swnj  * Take incoming datagram fragment and try to
2234951Swnj  * reassemble it into whole datagram.  If a chain for
2244640Swnj  * reassembly of this datagram already exists, then it
2254640Swnj  * is given as fp; otherwise have to make a chain.
2264640Swnj  */
2274640Swnj struct ip *
2284640Swnj ip_reass(ip, fp)
2294898Swnj 	register struct ipasfrag *ip;
2304640Swnj 	register struct ipq *fp;
2314640Swnj {
2324640Swnj 	register struct mbuf *m = dtom(ip);
2334898Swnj 	register struct ipasfrag *q;
2344640Swnj 	struct mbuf *t;
2354640Swnj 	int hlen = ip->ip_hl << 2;
2364640Swnj 	int i, next;
2374951Swnj COUNT(IP_REASS);
2384543Swnj 
2394640Swnj 	/*
2404640Swnj 	 * Presence of header sizes in mbufs
2414640Swnj 	 * would confuse code below.
2424640Swnj 	 */
2434640Swnj 	m->m_off += hlen;
2444640Swnj 	m->m_len -= hlen;
2454495Swnj 
2464640Swnj 	/*
2474640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2484640Swnj 	 */
2494640Swnj 	if (fp == 0) {
2505851Sroot 		if ((t = m_get(M_WAIT)) == NULL)
2514640Swnj 			goto dropfrag;
2524640Swnj 		t->m_off = MMINOFF;
2534640Swnj 		fp = mtod(t, struct ipq *);
2544640Swnj 		insque(fp, &ipq);
2554640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2564640Swnj 		fp->ipq_p = ip->ip_p;
2574640Swnj 		fp->ipq_id = ip->ip_id;
2584898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2594898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2604898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2615161Swnj 		q = (struct ipasfrag *)fp;
2625161Swnj 		goto insert;
2634640Swnj 	}
2644495Swnj 
2654640Swnj 	/*
2664640Swnj 	 * Find a segment which begins after this one does.
2674640Swnj 	 */
2684898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2694640Swnj 		if (q->ip_off > ip->ip_off)
2704640Swnj 			break;
2714495Swnj 
2724640Swnj 	/*
2734640Swnj 	 * If there is a preceding segment, it may provide some of
2744640Swnj 	 * our data already.  If so, drop the data from the incoming
2754640Swnj 	 * segment.  If it provides all of our data, drop us.
2764640Swnj 	 */
2774898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2784898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2794640Swnj 		if (i > 0) {
2804640Swnj 			if (i >= ip->ip_len)
2814640Swnj 				goto dropfrag;
2824640Swnj 			m_adj(dtom(ip), i);
2834640Swnj 			ip->ip_off += i;
2844640Swnj 			ip->ip_len -= i;
2854640Swnj 		}
2864640Swnj 	}
2874543Swnj 
2884640Swnj 	/*
2894640Swnj 	 * While we overlap succeeding segments trim them or,
2904640Swnj 	 * if they are completely covered, dequeue them.
2914640Swnj 	 */
2924898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2934640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2944640Swnj 		if (i < q->ip_len) {
2954640Swnj 			q->ip_len -= i;
2966256Sroot 			q->ip_off += i;
2974640Swnj 			m_adj(dtom(q), i);
2984640Swnj 			break;
2994495Swnj 		}
3004898Swnj 		q = q->ipf_next;
3014898Swnj 		m_freem(dtom(q->ipf_prev));
3024898Swnj 		ip_deq(q->ipf_prev);
3034543Swnj 	}
3044495Swnj 
3055161Swnj insert:
3064640Swnj 	/*
3074640Swnj 	 * Stick new segment in its place;
3084640Swnj 	 * check for complete reassembly.
3094640Swnj 	 */
3104898Swnj 	ip_enq(ip, q->ipf_prev);
3114640Swnj 	next = 0;
3124898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
3134640Swnj 		if (q->ip_off != next)
3144640Swnj 			return (0);
3154640Swnj 		next += q->ip_len;
3164640Swnj 	}
3174898Swnj 	if (q->ipf_prev->ipf_mff)
3184640Swnj 		return (0);
3194495Swnj 
3204640Swnj 	/*
3214640Swnj 	 * Reassembly is complete; concatenate fragments.
3224640Swnj 	 */
3234640Swnj 	q = fp->ipq_next;
3244640Swnj 	m = dtom(q);
3254640Swnj 	t = m->m_next;
3264640Swnj 	m->m_next = 0;
3274640Swnj 	m_cat(m, t);
3286298Swnj 	q = q->ipf_next;
3296298Swnj 	while (q != (struct ipasfrag *)fp) {
3306298Swnj 		t = dtom(q);
3316298Swnj 		q = q->ipf_next;
3326298Swnj 		m_cat(m, t);
3336298Swnj 	}
3344495Swnj 
3354640Swnj 	/*
3364640Swnj 	 * Create header for new ip packet by
3374640Swnj 	 * modifying header of first packet;
3384640Swnj 	 * dequeue and discard fragment reassembly header.
3394640Swnj 	 * Make header visible.
3404640Swnj 	 */
3414640Swnj 	ip = fp->ipq_next;
3424640Swnj 	ip->ip_len = next;
3434898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
3444898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
3454640Swnj 	remque(fp);
3464907Swnj 	(void) m_free(dtom(fp));
3474640Swnj 	m = dtom(ip);
3484898Swnj 	m->m_len += sizeof (struct ipasfrag);
3494898Swnj 	m->m_off -= sizeof (struct ipasfrag);
3504898Swnj 	return ((struct ip *)ip);
3514495Swnj 
3524640Swnj dropfrag:
3534640Swnj 	m_freem(m);
3544640Swnj 	return (0);
3554495Swnj }
3564495Swnj 
3574640Swnj /*
3584640Swnj  * Free a fragment reassembly header and all
3594640Swnj  * associated datagrams.
3604640Swnj  */
3614640Swnj struct ipq *
3624640Swnj ip_freef(fp)
3634640Swnj 	struct ipq *fp;
3644495Swnj {
3654898Swnj 	register struct ipasfrag *q;
3664640Swnj 	struct mbuf *m;
3674951Swnj COUNT(IP_FREEF);
3684495Swnj 
3694898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3704640Swnj 		m_freem(dtom(q));
3714640Swnj 	m = dtom(fp);
3724640Swnj 	fp = fp->next;
3734640Swnj 	remque(fp->prev);
3744907Swnj 	(void) m_free(m);
3754640Swnj 	return (fp);
3764495Swnj }
3774495Swnj 
3784640Swnj /*
3794640Swnj  * Put an ip fragment on a reassembly chain.
3804640Swnj  * Like insque, but pointers in middle of structure.
3814640Swnj  */
3824640Swnj ip_enq(p, prev)
3834898Swnj 	register struct ipasfrag *p, *prev;
3844495Swnj {
3854951Swnj 
3864640Swnj COUNT(IP_ENQ);
3874898Swnj 	p->ipf_prev = prev;
3884898Swnj 	p->ipf_next = prev->ipf_next;
3894898Swnj 	prev->ipf_next->ipf_prev = p;
3904898Swnj 	prev->ipf_next = p;
3914495Swnj }
3924495Swnj 
3934640Swnj /*
3944640Swnj  * To ip_enq as remque is to insque.
3954640Swnj  */
3964640Swnj ip_deq(p)
3974898Swnj 	register struct ipasfrag *p;
3984640Swnj {
3994951Swnj 
4004640Swnj COUNT(IP_DEQ);
4014898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
4024898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
4034495Swnj }
4044495Swnj 
4054640Swnj /*
4064640Swnj  * IP timer processing;
4074640Swnj  * if a timer expires on a reassembly
4084640Swnj  * queue, discard it.
4094640Swnj  */
4104801Swnj ip_slowtimo()
4114495Swnj {
4124495Swnj 	register struct ipq *fp;
4134640Swnj 	int s = splnet();
4144951Swnj 
4154801Swnj COUNT(IP_SLOWTIMO);
4165243Sroot 	fp = ipq.next;
4175243Sroot 	if (fp == 0) {
4185243Sroot 		splx(s);
4195243Sroot 		return;
4205243Sroot 	}
4215243Sroot 	while (fp != &ipq)
4224640Swnj 		if (--fp->ipq_ttl == 0)
4234640Swnj 			fp = ip_freef(fp);
4244640Swnj 		else
4254640Swnj 			fp = fp->next;
4264640Swnj 	splx(s);
4274495Swnj }
4284495Swnj 
4294951Swnj /*
4304951Swnj  * Drain off all datagram fragments.
4314951Swnj  */
4324801Swnj ip_drain()
4334801Swnj {
4344801Swnj 
4354951Swnj COUNT(IP_DRAIN);
4364951Swnj 	while (ipq.next != &ipq)
4374951Swnj 		(void) ip_freef(ipq.next);
4384801Swnj }
4394923Swnj 
4404640Swnj /*
4414640Swnj  * Do option processing on a datagram,
4424640Swnj  * possibly discarding it if bad options
4434640Swnj  * are encountered.
4444640Swnj  */
4454640Swnj ip_dooptions(ip)
4464640Swnj 	struct ip *ip;
4474495Swnj {
4484640Swnj 	register u_char *cp;
4494907Swnj 	int opt, optlen, cnt;
4504923Swnj 	struct in_addr *sin;
4514801Swnj 	register struct ip_timestamp *ipt;
4524951Swnj 	register struct ifnet *ifp;
4534951Swnj 	struct in_addr t;
4544495Swnj 
4554951Swnj COUNT(IP_DOOPTIONS);
4564640Swnj 	cp = (u_char *)(ip + 1);
4574640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
4584640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4594640Swnj 		opt = cp[0];
4604640Swnj 		if (opt == IPOPT_EOL)
4614640Swnj 			break;
4624640Swnj 		if (opt == IPOPT_NOP)
4634640Swnj 			optlen = 1;
4644640Swnj 		else
4654640Swnj 			optlen = cp[1];
4664640Swnj 		switch (opt) {
4674495Swnj 
4684640Swnj 		default:
4694640Swnj 			break;
4704495Swnj 
4714951Swnj 		/*
4724951Swnj 		 * Source routing with record.
4734951Swnj 		 * Find interface with current destination address.
4744951Swnj 		 * If none on this machine then drop if strictly routed,
4754951Swnj 		 * or do nothing if loosely routed.
4764951Swnj 		 * Record interface address and bring up next address
4774951Swnj 		 * component.  If strictly routed make sure next
4784951Swnj 		 * address on directly accessible net.
4794951Swnj 		 */
4804640Swnj 		case IPOPT_LSRR:
4814801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4824640Swnj 				break;
4834923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4846338Ssam 			ipaddr.sin_addr = *sin;
4856338Ssam 			ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
4864951Swnj 			if (ifp == 0) {
4874951Swnj 				if (opt == IPOPT_SSRR)
4884951Swnj 					goto bad;
4894951Swnj 				break;
4904640Swnj 			}
4914951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4924951Swnj 			cp[2] += 4;
4934951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4944951Swnj 				break;
4954951Swnj 			ip->ip_dst = sin[1];
4966338Ssam 			if (opt == IPOPT_SSRR &&
4976338Ssam 			    if_ifonnetof(ip->ip_dst.s_net) == 0)
4984951Swnj 				goto bad;
4994640Swnj 			break;
5004495Swnj 
5014640Swnj 		case IPOPT_TS:
5024801Swnj 			ipt = (struct ip_timestamp *)cp;
5034801Swnj 			if (ipt->ipt_len < 5)
5044640Swnj 				goto bad;
5054801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
5064801Swnj 				if (++ipt->ipt_oflw == 0)
5074640Swnj 					goto bad;
5084495Swnj 				break;
5094640Swnj 			}
5104923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
5114801Swnj 			switch (ipt->ipt_flg) {
5124495Swnj 
5134640Swnj 			case IPOPT_TS_TSONLY:
5144640Swnj 				break;
5154640Swnj 
5164640Swnj 			case IPOPT_TS_TSANDADDR:
5174801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5184640Swnj 					goto bad;
5196338Ssam 				if (ifinet == 0)
5206338Ssam 					goto bad;	/* ??? */
5216338Ssam 				*sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr;
5224640Swnj 				break;
5234640Swnj 
5244640Swnj 			case IPOPT_TS_PRESPEC:
5256338Ssam 				ipaddr.sin_addr = *sin;
5266338Ssam 				if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0)
5274951Swnj 					continue;
5284801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5294640Swnj 					goto bad;
5304801Swnj 				ipt->ipt_ptr += 4;
5314640Swnj 				break;
5324640Swnj 
5334495Swnj 			default:
5344640Swnj 				goto bad;
5354495Swnj 			}
5364923Swnj 			*(n_time *)sin = iptime();
5374801Swnj 			ipt->ipt_ptr += 4;
5384640Swnj 		}
5394495Swnj 	}
5404907Swnj 	return;
5414640Swnj bad:
5424640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
5434907Swnj 	return;
5444495Swnj }
5454495Swnj 
5464640Swnj /*
5474951Swnj  * Strip out IP options, at higher
5484951Swnj  * level protocol in the kernel.
5494951Swnj  * Second argument is buffer to which options
5504951Swnj  * will be moved, and return value is their length.
5514640Swnj  */
5525217Swnj ip_stripoptions(ip, mopt)
5534640Swnj 	struct ip *ip;
5545217Swnj 	struct mbuf *mopt;
5554495Swnj {
5564640Swnj 	register int i;
5574640Swnj 	register struct mbuf *m;
5584640Swnj 	int olen;
5594951Swnj COUNT(IP_STRIPOPTIONS);
5604640Swnj 
5614640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
5624951Swnj 	m = dtom(ip);
5634951Swnj 	ip++;
5645217Swnj 	if (mopt) {
5655217Swnj 		mopt->m_len = olen;
5665217Swnj 		mopt->m_off = MMINOFF;
5675217Swnj 		bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen);
5685217Swnj 	}
5694640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5704907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5715243Sroot 	m->m_len -= olen;
5724495Swnj }
573