xref: /csrg-svn/sys/netinet/ip_input.c (revision 6411)
1*6411Ssam /*	ip_input.c	1.38	82/03/31	*/
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();
48*6411Ssam int	ipforwarding = 1;
49*6411Ssam 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 	 */
1084640Swnj 	i = 0;
1095161Swnj 	m0 = m;
1106088Sroot 	for (; m != NULL; m = m->m_next) {
1116088Sroot 		if (m->m_free) panic("ipinput already free");
1124495Swnj 		i += m->m_len;
1136088Sroot 	}
1144640Swnj 	m = m0;
1154640Swnj 	if (i != ip->ip_len) {
1165161Swnj 		if (i < ip->ip_len) {
1175161Swnj 			ipstat.ips_tooshort++;
1184951Swnj 			goto bad;
1195161Swnj 		}
1204640Swnj 		m_adj(m, ip->ip_len - i);
1214495Swnj 	}
1224495Swnj 
1234640Swnj 	/*
1244640Swnj 	 * Process options and, if not destined for us,
1254640Swnj 	 * ship it on.
1264640Swnj 	 */
1274543Swnj 	if (hlen > sizeof (struct ip))
1284907Swnj 		ip_dooptions(ip);
1296210Swnj 
1306338Ssam 	/*
1316350Ssam 	 * Fast check on the first internet
1326350Ssam 	 * interface in the list.
1336338Ssam 	 */
1346338Ssam 	if (ifinet) {
1356338Ssam 		struct sockaddr_in *sin;
1366338Ssam 
1376338Ssam 		sin = (struct sockaddr_in *)&ifinet->if_addr;
1386338Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1396338Ssam 			goto ours;
1406350Ssam 		if ((ifinet->if_flags & IFF_BROADCAST) &&
1416350Ssam 		    sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1426350Ssam 			goto ours;
1436338Ssam 	}
1446338Ssam 	ipaddr.sin_addr = ip->ip_dst;
1456338Ssam 	if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) {
146*6411Ssam 		if (ipprintfs)
147*6411Ssam 			printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
148*6411Ssam 				ip->ip_dst, ip->ip_ttl);
1496338Ssam 		if (ipforwarding == 0)
1506338Ssam 			goto bad;
1516338Ssam 		if (ip->ip_ttl < IPTTLDEC) {
1524907Swnj 			icmp_error(ip, ICMP_TIMXCEED, 0);
1535084Swnj 			goto next;
1544495Swnj 		}
1556338Ssam 		ip->ip_ttl -= IPTTLDEC;
1565217Swnj 		mopt = m_get(M_DONTWAIT);
1575217Swnj 		if (mopt == 0)
1585217Swnj 			goto bad;
1595217Swnj 		ip_stripoptions(ip, mopt);
1606338Ssam 
1616350Ssam 		/* last 0 here means no directed broadcast */
1626338Ssam 		(void) ip_output(m0, mopt, 0, 0);
1635084Swnj 		goto next;
1644543Swnj 	}
1654495Swnj 
1666338Ssam ours:
1674640Swnj 	/*
1684640Swnj 	 * Look for queue of fragments
1694640Swnj 	 * of this datagram.
1704640Swnj 	 */
1714640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1724640Swnj 		if (ip->ip_id == fp->ipq_id &&
1734640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1744640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1754640Swnj 		    ip->ip_p == fp->ipq_p)
1764640Swnj 			goto found;
1774640Swnj 	fp = 0;
1784640Swnj found:
1794495Swnj 
1804640Swnj 	/*
1814640Swnj 	 * Adjust ip_len to not reflect header,
1824640Swnj 	 * set ip_mff if more fragments are expected,
1834640Swnj 	 * convert offset of this to bytes.
1844640Swnj 	 */
1854640Swnj 	ip->ip_len -= hlen;
1864898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1874640Swnj 	if (ip->ip_off & IP_MF)
1884898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1894640Swnj 	ip->ip_off <<= 3;
1904495Swnj 
1914640Swnj 	/*
1924640Swnj 	 * If datagram marked as having more fragments
1934640Swnj 	 * or if this is not the first fragment,
1944640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1954640Swnj 	 */
1964898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1974898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1984640Swnj 		if (ip == 0)
1995084Swnj 			goto next;
2004640Swnj 		hlen = ip->ip_hl << 2;
2014640Swnj 		m = dtom(ip);
2024640Swnj 	} else
2034640Swnj 		if (fp)
2044640Swnj 			(void) ip_freef(fp);
2054951Swnj 
2064951Swnj 	/*
2074951Swnj 	 * Switch out to protocol's input routine.
2084951Swnj 	 */
2094898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
2105084Swnj 	goto next;
2114951Swnj bad:
2124951Swnj 	m_freem(m);
2135084Swnj 	goto next;
2144640Swnj }
2154495Swnj 
2164640Swnj /*
2174640Swnj  * Take incoming datagram fragment and try to
2184951Swnj  * reassemble it into whole datagram.  If a chain for
2194640Swnj  * reassembly of this datagram already exists, then it
2204640Swnj  * is given as fp; otherwise have to make a chain.
2214640Swnj  */
2224640Swnj struct ip *
2234640Swnj ip_reass(ip, fp)
2244898Swnj 	register struct ipasfrag *ip;
2254640Swnj 	register struct ipq *fp;
2264640Swnj {
2274640Swnj 	register struct mbuf *m = dtom(ip);
2284898Swnj 	register struct ipasfrag *q;
2294640Swnj 	struct mbuf *t;
2304640Swnj 	int hlen = ip->ip_hl << 2;
2314640Swnj 	int i, next;
2324951Swnj COUNT(IP_REASS);
2334543Swnj 
2344640Swnj 	/*
2354640Swnj 	 * Presence of header sizes in mbufs
2364640Swnj 	 * would confuse code below.
2374640Swnj 	 */
2384640Swnj 	m->m_off += hlen;
2394640Swnj 	m->m_len -= hlen;
2404495Swnj 
2414640Swnj 	/*
2424640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2434640Swnj 	 */
2444640Swnj 	if (fp == 0) {
2455851Sroot 		if ((t = m_get(M_WAIT)) == NULL)
2464640Swnj 			goto dropfrag;
2474640Swnj 		t->m_off = MMINOFF;
2484640Swnj 		fp = mtod(t, struct ipq *);
2494640Swnj 		insque(fp, &ipq);
2504640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2514640Swnj 		fp->ipq_p = ip->ip_p;
2524640Swnj 		fp->ipq_id = ip->ip_id;
2534898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2544898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2554898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2565161Swnj 		q = (struct ipasfrag *)fp;
2575161Swnj 		goto insert;
2584640Swnj 	}
2594495Swnj 
2604640Swnj 	/*
2614640Swnj 	 * Find a segment which begins after this one does.
2624640Swnj 	 */
2634898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2644640Swnj 		if (q->ip_off > ip->ip_off)
2654640Swnj 			break;
2664495Swnj 
2674640Swnj 	/*
2684640Swnj 	 * If there is a preceding segment, it may provide some of
2694640Swnj 	 * our data already.  If so, drop the data from the incoming
2704640Swnj 	 * segment.  If it provides all of our data, drop us.
2714640Swnj 	 */
2724898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2734898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2744640Swnj 		if (i > 0) {
2754640Swnj 			if (i >= ip->ip_len)
2764640Swnj 				goto dropfrag;
2774640Swnj 			m_adj(dtom(ip), i);
2784640Swnj 			ip->ip_off += i;
2794640Swnj 			ip->ip_len -= i;
2804640Swnj 		}
2814640Swnj 	}
2824543Swnj 
2834640Swnj 	/*
2844640Swnj 	 * While we overlap succeeding segments trim them or,
2854640Swnj 	 * if they are completely covered, dequeue them.
2864640Swnj 	 */
2874898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2884640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2894640Swnj 		if (i < q->ip_len) {
2904640Swnj 			q->ip_len -= i;
2916256Sroot 			q->ip_off += i;
2924640Swnj 			m_adj(dtom(q), i);
2934640Swnj 			break;
2944495Swnj 		}
2954898Swnj 		q = q->ipf_next;
2964898Swnj 		m_freem(dtom(q->ipf_prev));
2974898Swnj 		ip_deq(q->ipf_prev);
2984543Swnj 	}
2994495Swnj 
3005161Swnj insert:
3014640Swnj 	/*
3024640Swnj 	 * Stick new segment in its place;
3034640Swnj 	 * check for complete reassembly.
3044640Swnj 	 */
3054898Swnj 	ip_enq(ip, q->ipf_prev);
3064640Swnj 	next = 0;
3074898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
3084640Swnj 		if (q->ip_off != next)
3094640Swnj 			return (0);
3104640Swnj 		next += q->ip_len;
3114640Swnj 	}
3124898Swnj 	if (q->ipf_prev->ipf_mff)
3134640Swnj 		return (0);
3144495Swnj 
3154640Swnj 	/*
3164640Swnj 	 * Reassembly is complete; concatenate fragments.
3174640Swnj 	 */
3184640Swnj 	q = fp->ipq_next;
3194640Swnj 	m = dtom(q);
3204640Swnj 	t = m->m_next;
3214640Swnj 	m->m_next = 0;
3224640Swnj 	m_cat(m, t);
3236298Swnj 	q = q->ipf_next;
3246298Swnj 	while (q != (struct ipasfrag *)fp) {
3256298Swnj 		t = dtom(q);
3266298Swnj 		q = q->ipf_next;
3276298Swnj 		m_cat(m, t);
3286298Swnj 	}
3294495Swnj 
3304640Swnj 	/*
3314640Swnj 	 * Create header for new ip packet by
3324640Swnj 	 * modifying header of first packet;
3334640Swnj 	 * dequeue and discard fragment reassembly header.
3344640Swnj 	 * Make header visible.
3354640Swnj 	 */
3364640Swnj 	ip = fp->ipq_next;
3374640Swnj 	ip->ip_len = next;
3384898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
3394898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
3404640Swnj 	remque(fp);
3414907Swnj 	(void) m_free(dtom(fp));
3424640Swnj 	m = dtom(ip);
3434898Swnj 	m->m_len += sizeof (struct ipasfrag);
3444898Swnj 	m->m_off -= sizeof (struct ipasfrag);
3454898Swnj 	return ((struct ip *)ip);
3464495Swnj 
3474640Swnj dropfrag:
3484640Swnj 	m_freem(m);
3494640Swnj 	return (0);
3504495Swnj }
3514495Swnj 
3524640Swnj /*
3534640Swnj  * Free a fragment reassembly header and all
3544640Swnj  * associated datagrams.
3554640Swnj  */
3564640Swnj struct ipq *
3574640Swnj ip_freef(fp)
3584640Swnj 	struct ipq *fp;
3594495Swnj {
3604898Swnj 	register struct ipasfrag *q;
3614640Swnj 	struct mbuf *m;
3624951Swnj COUNT(IP_FREEF);
3634495Swnj 
3644898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3654640Swnj 		m_freem(dtom(q));
3664640Swnj 	m = dtom(fp);
3674640Swnj 	fp = fp->next;
3684640Swnj 	remque(fp->prev);
3694907Swnj 	(void) m_free(m);
3704640Swnj 	return (fp);
3714495Swnj }
3724495Swnj 
3734640Swnj /*
3744640Swnj  * Put an ip fragment on a reassembly chain.
3754640Swnj  * Like insque, but pointers in middle of structure.
3764640Swnj  */
3774640Swnj ip_enq(p, prev)
3784898Swnj 	register struct ipasfrag *p, *prev;
3794495Swnj {
3804951Swnj 
3814640Swnj COUNT(IP_ENQ);
3824898Swnj 	p->ipf_prev = prev;
3834898Swnj 	p->ipf_next = prev->ipf_next;
3844898Swnj 	prev->ipf_next->ipf_prev = p;
3854898Swnj 	prev->ipf_next = p;
3864495Swnj }
3874495Swnj 
3884640Swnj /*
3894640Swnj  * To ip_enq as remque is to insque.
3904640Swnj  */
3914640Swnj ip_deq(p)
3924898Swnj 	register struct ipasfrag *p;
3934640Swnj {
3944951Swnj 
3954640Swnj COUNT(IP_DEQ);
3964898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3974898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3984495Swnj }
3994495Swnj 
4004640Swnj /*
4014640Swnj  * IP timer processing;
4024640Swnj  * if a timer expires on a reassembly
4034640Swnj  * queue, discard it.
4044640Swnj  */
4054801Swnj ip_slowtimo()
4064495Swnj {
4074495Swnj 	register struct ipq *fp;
4084640Swnj 	int s = splnet();
4094951Swnj 
4104801Swnj COUNT(IP_SLOWTIMO);
4115243Sroot 	fp = ipq.next;
4125243Sroot 	if (fp == 0) {
4135243Sroot 		splx(s);
4145243Sroot 		return;
4155243Sroot 	}
4165243Sroot 	while (fp != &ipq)
4174640Swnj 		if (--fp->ipq_ttl == 0)
4184640Swnj 			fp = ip_freef(fp);
4194640Swnj 		else
4204640Swnj 			fp = fp->next;
4214640Swnj 	splx(s);
4224495Swnj }
4234495Swnj 
4244951Swnj /*
4254951Swnj  * Drain off all datagram fragments.
4264951Swnj  */
4274801Swnj ip_drain()
4284801Swnj {
4294801Swnj 
4304951Swnj COUNT(IP_DRAIN);
4314951Swnj 	while (ipq.next != &ipq)
4324951Swnj 		(void) ip_freef(ipq.next);
4334801Swnj }
4344923Swnj 
4354640Swnj /*
4364640Swnj  * Do option processing on a datagram,
4374640Swnj  * possibly discarding it if bad options
4384640Swnj  * are encountered.
4394640Swnj  */
4404640Swnj ip_dooptions(ip)
4414640Swnj 	struct ip *ip;
4424495Swnj {
4434640Swnj 	register u_char *cp;
4444907Swnj 	int opt, optlen, cnt;
4454923Swnj 	struct in_addr *sin;
4464801Swnj 	register struct ip_timestamp *ipt;
4474951Swnj 	register struct ifnet *ifp;
4484951Swnj 	struct in_addr t;
4494495Swnj 
4504951Swnj COUNT(IP_DOOPTIONS);
4514640Swnj 	cp = (u_char *)(ip + 1);
4524640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
4534640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4544640Swnj 		opt = cp[0];
4554640Swnj 		if (opt == IPOPT_EOL)
4564640Swnj 			break;
4574640Swnj 		if (opt == IPOPT_NOP)
4584640Swnj 			optlen = 1;
4594640Swnj 		else
4604640Swnj 			optlen = cp[1];
4614640Swnj 		switch (opt) {
4624495Swnj 
4634640Swnj 		default:
4644640Swnj 			break;
4654495Swnj 
4664951Swnj 		/*
4674951Swnj 		 * Source routing with record.
4684951Swnj 		 * Find interface with current destination address.
4694951Swnj 		 * If none on this machine then drop if strictly routed,
4704951Swnj 		 * or do nothing if loosely routed.
4714951Swnj 		 * Record interface address and bring up next address
4724951Swnj 		 * component.  If strictly routed make sure next
4734951Swnj 		 * address on directly accessible net.
4744951Swnj 		 */
4754640Swnj 		case IPOPT_LSRR:
4764801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4774640Swnj 				break;
4784923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4796338Ssam 			ipaddr.sin_addr = *sin;
4806338Ssam 			ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
4814951Swnj 			if (ifp == 0) {
4824951Swnj 				if (opt == IPOPT_SSRR)
4834951Swnj 					goto bad;
4844951Swnj 				break;
4854640Swnj 			}
4864951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4874951Swnj 			cp[2] += 4;
4884951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4894951Swnj 				break;
4904951Swnj 			ip->ip_dst = sin[1];
4916338Ssam 			if (opt == IPOPT_SSRR &&
4926338Ssam 			    if_ifonnetof(ip->ip_dst.s_net) == 0)
4934951Swnj 				goto bad;
4944640Swnj 			break;
4954495Swnj 
4964640Swnj 		case IPOPT_TS:
4974801Swnj 			ipt = (struct ip_timestamp *)cp;
4984801Swnj 			if (ipt->ipt_len < 5)
4994640Swnj 				goto bad;
5004801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
5014801Swnj 				if (++ipt->ipt_oflw == 0)
5024640Swnj 					goto bad;
5034495Swnj 				break;
5044640Swnj 			}
5054923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
5064801Swnj 			switch (ipt->ipt_flg) {
5074495Swnj 
5084640Swnj 			case IPOPT_TS_TSONLY:
5094640Swnj 				break;
5104640Swnj 
5114640Swnj 			case IPOPT_TS_TSANDADDR:
5124801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5134640Swnj 					goto bad;
5146338Ssam 				if (ifinet == 0)
5156338Ssam 					goto bad;	/* ??? */
5166338Ssam 				*sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr;
5174640Swnj 				break;
5184640Swnj 
5194640Swnj 			case IPOPT_TS_PRESPEC:
5206338Ssam 				ipaddr.sin_addr = *sin;
5216338Ssam 				if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0)
5224951Swnj 					continue;
5234801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5244640Swnj 					goto bad;
5254801Swnj 				ipt->ipt_ptr += 4;
5264640Swnj 				break;
5274640Swnj 
5284495Swnj 			default:
5294640Swnj 				goto bad;
5304495Swnj 			}
5314923Swnj 			*(n_time *)sin = iptime();
5324801Swnj 			ipt->ipt_ptr += 4;
5334640Swnj 		}
5344495Swnj 	}
5354907Swnj 	return;
5364640Swnj bad:
5374640Swnj 	/* SHOULD FORCE ICMP MESSAGE */
5384907Swnj 	return;
5394495Swnj }
5404495Swnj 
5414640Swnj /*
5424951Swnj  * Strip out IP options, at higher
5434951Swnj  * level protocol in the kernel.
5444951Swnj  * Second argument is buffer to which options
5454951Swnj  * will be moved, and return value is their length.
5464640Swnj  */
5475217Swnj ip_stripoptions(ip, mopt)
5484640Swnj 	struct ip *ip;
5495217Swnj 	struct mbuf *mopt;
5504495Swnj {
5514640Swnj 	register int i;
5524640Swnj 	register struct mbuf *m;
5534640Swnj 	int olen;
5544951Swnj COUNT(IP_STRIPOPTIONS);
5554640Swnj 
5564640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
5574951Swnj 	m = dtom(ip);
5584951Swnj 	ip++;
5595217Swnj 	if (mopt) {
5605217Swnj 		mopt->m_len = olen;
5615217Swnj 		mopt->m_off = MMINOFF;
5625217Swnj 		bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen);
5635217Swnj 	}
5644640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5654907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5665243Sroot 	m->m_len -= olen;
5674495Swnj }
568