xref: /csrg-svn/sys/netinet/ip_input.c (revision 17551)
1*17551Skarels /*	ip_input.c	6.8	84/12/20	*/
24571Swnj 
317060Sbloom #include "param.h"
417060Sbloom #include "systm.h"
517060Sbloom #include "mbuf.h"
617060Sbloom #include "domain.h"
717060Sbloom #include "protosw.h"
817060Sbloom #include "socket.h"
917060Sbloom #include "errno.h"
1017060Sbloom #include "time.h"
1117060Sbloom #include "kernel.h"
128695Sroot 
138695Sroot #include "../net/if.h"
148695Sroot #include "../net/route.h"
1510892Ssam 
1617060Sbloom #include "in.h"
1717060Sbloom #include "in_pcb.h"
1817060Sbloom #include "in_systm.h"
1917060Sbloom #include "ip.h"
2017060Sbloom #include "ip_var.h"
2117060Sbloom #include "ip_icmp.h"
2217060Sbloom #include "tcp.h"
234495Swnj 
244898Swnj u_char	ip_protox[IPPROTO_MAX];
256210Swnj int	ipqmaxlen = IFQ_MAXLEN;
266338Ssam struct	ifnet *ifinet;			/* first inet interface */
274898Swnj 
284801Swnj /*
295172Swnj  * IP initialization: fill in IP protocol switch table.
305161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
314801Swnj  */
324801Swnj ip_init()
334801Swnj {
344898Swnj 	register struct protosw *pr;
354898Swnj 	register int i;
364495Swnj 
374898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
384898Swnj 	if (pr == 0)
394898Swnj 		panic("ip_init");
404898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
419030Sroot 		ip_protox[i] = pr - inetsw;
429030Sroot 	for (pr = inetdomain.dom_protosw;
43*17551Skarels 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
4416990Skarels 		if (pr->pr_domain->dom_family == PF_INET &&
454898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
469030Sroot 			ip_protox[pr->pr_protocol] = pr - inetsw;
474801Swnj 	ipq.next = ipq.prev = &ipq;
488172Sroot 	ip_id = time.tv_sec & 0xffff;
496210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
506338Ssam 	ifinet = if_ifwithaf(AF_INET);
514801Swnj }
524801Swnj 
534898Swnj u_char	ipcksum = 1;
544640Swnj struct	ip *ip_reass();
556338Ssam struct	sockaddr_in ipaddr = { AF_INET };
564640Swnj 
574640Swnj /*
584640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
594640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
604640Swnj  * Process options.  Pass to next level.
614640Swnj  */
625084Swnj ipintr()
634495Swnj {
644923Swnj 	register struct ip *ip;
655084Swnj 	register struct mbuf *m;
668597Sroot 	struct mbuf *m0;
674640Swnj 	register int i;
684495Swnj 	register struct ipq *fp;
695084Swnj 	int hlen, s;
704495Swnj 
715084Swnj next:
724640Swnj 	/*
735084Swnj 	 * Get next datagram off input queue and get IP header
745084Swnj 	 * in first mbuf.
754640Swnj 	 */
765084Swnj 	s = splimp();
775084Swnj 	IF_DEQUEUE(&ipintrq, m);
785084Swnj 	splx(s);
795218Swnj 	if (m == 0)
805084Swnj 		return;
815306Sroot 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) &&
8211232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
8311232Ssam 		ipstat.ips_toosmall++;
8411232Ssam 		goto next;
8511232Ssam 	}
864640Swnj 	ip = mtod(m, struct ip *);
875161Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
8811232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
8911232Ssam 			ipstat.ips_badhlen++;
9011232Ssam 			goto next;
9111232Ssam 		}
925161Swnj 		ip = mtod(m, struct ip *);
935161Swnj 	}
944951Swnj 	if (ipcksum)
955217Swnj 		if (ip->ip_sum = in_cksum(m, hlen)) {
964951Swnj 			ipstat.ips_badsum++;
974951Swnj 			goto bad;
984495Swnj 		}
994951Swnj 
1004951Swnj 	/*
1014951Swnj 	 * Convert fields to host representation.
1024951Swnj 	 */
1034907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
10411232Ssam 	if (ip->ip_len < hlen) {
10511232Ssam 		ipstat.ips_badlen++;
10611232Ssam 		goto bad;
10711232Ssam 	}
1084640Swnj 	ip->ip_id = ntohs(ip->ip_id);
1094951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
1104495Swnj 
1114543Swnj 	/*
1124640Swnj 	 * Check that the amount of data in the buffers
1134640Swnj 	 * is as at least much as the IP header would have us expect.
1144640Swnj 	 * Trim mbufs if longer than we expect.
1154640Swnj 	 * Drop packet if shorter than we expect.
1164543Swnj 	 */
1176475Sroot 	i = -ip->ip_len;
1185161Swnj 	m0 = m;
1196475Sroot 	for (;;) {
1204495Swnj 		i += m->m_len;
1216475Sroot 		if (m->m_next == 0)
1226475Sroot 			break;
1236475Sroot 		m = m->m_next;
1246088Sroot 	}
1256475Sroot 	if (i != 0) {
1266475Sroot 		if (i < 0) {
1275161Swnj 			ipstat.ips_tooshort++;
12817358Skarels 			m = m0;
1294951Swnj 			goto bad;
1305161Swnj 		}
1316475Sroot 		if (i <= m->m_len)
1326475Sroot 			m->m_len -= i;
1336475Sroot 		else
1346475Sroot 			m_adj(m0, -i);
1354495Swnj 	}
1366475Sroot 	m = m0;
1374495Swnj 
1384640Swnj 	/*
1394640Swnj 	 * Process options and, if not destined for us,
1406583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1416583Ssam 	 * error was detected (causing an icmp message
1426583Ssam 	 * to be sent).
1434640Swnj 	 */
1446583Ssam 	if (hlen > sizeof (struct ip) && ip_dooptions(ip))
1456583Ssam 		goto next;
1466210Swnj 
1476338Ssam 	/*
1486350Ssam 	 * Fast check on the first internet
1496350Ssam 	 * interface in the list.
1506338Ssam 	 */
1516338Ssam 	if (ifinet) {
1526338Ssam 		struct sockaddr_in *sin;
1536338Ssam 
1546338Ssam 		sin = (struct sockaddr_in *)&ifinet->if_addr;
1556338Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1566338Ssam 			goto ours;
1576483Ssam 		sin = (struct sockaddr_in *)&ifinet->if_broadaddr;
1586350Ssam 		if ((ifinet->if_flags & IFF_BROADCAST) &&
1596350Ssam 		    sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1606350Ssam 			goto ours;
1616338Ssam 	}
1626338Ssam 	ipaddr.sin_addr = ip->ip_dst;
1636338Ssam 	if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) {
1646583Ssam 		ip_forward(ip);
1655084Swnj 		goto next;
1664543Swnj 	}
1674495Swnj 
1686338Ssam ours:
1694640Swnj 	/*
1704640Swnj 	 * Look for queue of fragments
1714640Swnj 	 * of this datagram.
1724640Swnj 	 */
1734640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1744640Swnj 		if (ip->ip_id == fp->ipq_id &&
1754640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1764640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1774640Swnj 		    ip->ip_p == fp->ipq_p)
1784640Swnj 			goto found;
1794640Swnj 	fp = 0;
1804640Swnj found:
1814495Swnj 
1824640Swnj 	/*
1834640Swnj 	 * Adjust ip_len to not reflect header,
1844640Swnj 	 * set ip_mff if more fragments are expected,
1854640Swnj 	 * convert offset of this to bytes.
1864640Swnj 	 */
1874640Swnj 	ip->ip_len -= hlen;
1884898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1894640Swnj 	if (ip->ip_off & IP_MF)
1904898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1914640Swnj 	ip->ip_off <<= 3;
1924495Swnj 
1934640Swnj 	/*
1944640Swnj 	 * If datagram marked as having more fragments
1954640Swnj 	 * or if this is not the first fragment,
1964640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1974640Swnj 	 */
1984898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1994898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
2004640Swnj 		if (ip == 0)
2015084Swnj 			goto next;
2024640Swnj 		hlen = ip->ip_hl << 2;
2034640Swnj 		m = dtom(ip);
2044640Swnj 	} else
2054640Swnj 		if (fp)
20610735Ssam 			ip_freef(fp);
2074951Swnj 
2084951Swnj 	/*
2094951Swnj 	 * Switch out to protocol's input routine.
2104951Swnj 	 */
2119030Sroot 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m);
2125084Swnj 	goto next;
2134951Swnj bad:
2144951Swnj 	m_freem(m);
2155084Swnj 	goto next;
2164640Swnj }
2174495Swnj 
2184640Swnj /*
2194640Swnj  * Take incoming datagram fragment and try to
2204951Swnj  * reassemble it into whole datagram.  If a chain for
2214640Swnj  * reassembly of this datagram already exists, then it
2224640Swnj  * is given as fp; otherwise have to make a chain.
2234640Swnj  */
2244640Swnj struct ip *
2254640Swnj ip_reass(ip, fp)
2264898Swnj 	register struct ipasfrag *ip;
2274640Swnj 	register struct ipq *fp;
2284640Swnj {
2294640Swnj 	register struct mbuf *m = dtom(ip);
2304898Swnj 	register struct ipasfrag *q;
2314640Swnj 	struct mbuf *t;
2324640Swnj 	int hlen = ip->ip_hl << 2;
2334640Swnj 	int i, next;
2344543Swnj 
2354640Swnj 	/*
2364640Swnj 	 * Presence of header sizes in mbufs
2374640Swnj 	 * would confuse code below.
2384640Swnj 	 */
2394640Swnj 	m->m_off += hlen;
2404640Swnj 	m->m_len -= hlen;
2414495Swnj 
2424640Swnj 	/*
2434640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2444640Swnj 	 */
2454640Swnj 	if (fp == 0) {
2469641Ssam 		if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL)
2474640Swnj 			goto dropfrag;
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 ip_freef(fp)
3574640Swnj 	struct ipq *fp;
3584495Swnj {
35910735Ssam 	register struct ipasfrag *q, *p;
3604495Swnj 
36110735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
36210735Ssam 		p = q->ipf_next;
36310735Ssam 		ip_deq(q);
3644640Swnj 		m_freem(dtom(q));
36510735Ssam 	}
36610735Ssam 	remque(fp);
36710735Ssam 	(void) m_free(dtom(fp));
3684495Swnj }
3694495Swnj 
3704640Swnj /*
3714640Swnj  * Put an ip fragment on a reassembly chain.
3724640Swnj  * Like insque, but pointers in middle of structure.
3734640Swnj  */
3744640Swnj ip_enq(p, prev)
3754898Swnj 	register struct ipasfrag *p, *prev;
3764495Swnj {
3774951Swnj 
3784898Swnj 	p->ipf_prev = prev;
3794898Swnj 	p->ipf_next = prev->ipf_next;
3804898Swnj 	prev->ipf_next->ipf_prev = p;
3814898Swnj 	prev->ipf_next = p;
3824495Swnj }
3834495Swnj 
3844640Swnj /*
3854640Swnj  * To ip_enq as remque is to insque.
3864640Swnj  */
3874640Swnj ip_deq(p)
3884898Swnj 	register struct ipasfrag *p;
3894640Swnj {
3904951Swnj 
3914898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3924898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3934495Swnj }
3944495Swnj 
3954640Swnj /*
3964640Swnj  * IP timer processing;
3974640Swnj  * if a timer expires on a reassembly
3984640Swnj  * queue, discard it.
3994640Swnj  */
4004801Swnj ip_slowtimo()
4014495Swnj {
4024495Swnj 	register struct ipq *fp;
4034640Swnj 	int s = splnet();
4044951Swnj 
4055243Sroot 	fp = ipq.next;
4065243Sroot 	if (fp == 0) {
4075243Sroot 		splx(s);
4085243Sroot 		return;
4095243Sroot 	}
41010735Ssam 	while (fp != &ipq) {
41110735Ssam 		--fp->ipq_ttl;
41210735Ssam 		fp = fp->next;
41310735Ssam 		if (fp->prev->ipq_ttl == 0)
41410735Ssam 			ip_freef(fp->prev);
41510735Ssam 	}
4164640Swnj 	splx(s);
4174495Swnj }
4184495Swnj 
4194951Swnj /*
4204951Swnj  * Drain off all datagram fragments.
4214951Swnj  */
4224801Swnj ip_drain()
4234801Swnj {
4244801Swnj 
4254951Swnj 	while (ipq.next != &ipq)
42610735Ssam 		ip_freef(ipq.next);
4274801Swnj }
4284923Swnj 
4294640Swnj /*
4304640Swnj  * Do option processing on a datagram,
4314640Swnj  * possibly discarding it if bad options
4324640Swnj  * are encountered.
4334640Swnj  */
4344640Swnj ip_dooptions(ip)
4354640Swnj 	struct ip *ip;
4364495Swnj {
4374640Swnj 	register u_char *cp;
4386583Ssam 	int opt, optlen, cnt, code, type;
4394923Swnj 	struct in_addr *sin;
4404801Swnj 	register struct ip_timestamp *ipt;
4414951Swnj 	register struct ifnet *ifp;
4424951Swnj 	struct in_addr t;
4434495Swnj 
4444640Swnj 	cp = (u_char *)(ip + 1);
4454640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
4464640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4474640Swnj 		opt = cp[0];
4484640Swnj 		if (opt == IPOPT_EOL)
4494640Swnj 			break;
4504640Swnj 		if (opt == IPOPT_NOP)
4514640Swnj 			optlen = 1;
45216392Ssam 		else {
4534640Swnj 			optlen = cp[1];
454*17551Skarels 			if (optlen <= 0 || optlen >= cnt)
455*17551Skarels 				goto bad;
45616392Ssam 		}
4574640Swnj 		switch (opt) {
4584495Swnj 
4594640Swnj 		default:
4604640Swnj 			break;
4614495Swnj 
4624951Swnj 		/*
4634951Swnj 		 * Source routing with record.
4644951Swnj 		 * Find interface with current destination address.
4654951Swnj 		 * If none on this machine then drop if strictly routed,
4664951Swnj 		 * or do nothing if loosely routed.
4674951Swnj 		 * Record interface address and bring up next address
4684951Swnj 		 * component.  If strictly routed make sure next
4694951Swnj 		 * address on directly accessible net.
4704951Swnj 		 */
4714640Swnj 		case IPOPT_LSRR:
4727508Sroot 		case IPOPT_SSRR:
4734801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4744640Swnj 				break;
4754923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4766338Ssam 			ipaddr.sin_addr = *sin;
4776338Ssam 			ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
4786583Ssam 			type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL;
4794951Swnj 			if (ifp == 0) {
4804951Swnj 				if (opt == IPOPT_SSRR)
4814951Swnj 					goto bad;
4824951Swnj 				break;
4834640Swnj 			}
4844951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4854951Swnj 			cp[2] += 4;
4864951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4874951Swnj 				break;
4884951Swnj 			ip->ip_dst = sin[1];
4896338Ssam 			if (opt == IPOPT_SSRR &&
4907166Ssam 			    if_ifonnetof(in_netof(ip->ip_dst)) == 0)
4914951Swnj 				goto bad;
4924640Swnj 			break;
4934495Swnj 
4944640Swnj 		case IPOPT_TS:
4956583Ssam 			code = cp - (u_char *)ip;
4966583Ssam 			type = ICMP_PARAMPROB;
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;
52110142Ssam 				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 	}
5356583Ssam 	return (0);
5364640Swnj bad:
5376583Ssam 	icmp_error(ip, type, code);
5386583Ssam 	return (1);
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;
5544640Swnj 
5554640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
5564951Swnj 	m = dtom(ip);
5574951Swnj 	ip++;
5585217Swnj 	if (mopt) {
5595217Swnj 		mopt->m_len = olen;
5605217Swnj 		mopt->m_off = MMINOFF;
56116544Skarels 		bcopy((caddr_t)ip, mtod(mopt, caddr_t), (unsigned)olen);
5625217Swnj 	}
5634640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5644907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5655243Sroot 	m->m_len -= olen;
5664495Swnj }
5676583Ssam 
56814670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
5696583Ssam 	ECONNABORTED,	ECONNABORTED,	0,		0,
57014670Ssam 	0,		0,		EHOSTDOWN,	EHOSTUNREACH,
57114670Ssam 	ENETUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
57214670Ssam 	EMSGSIZE,	0,		0,		0,
5736583Ssam 	0,		0,		0,		0
5746583Ssam };
5756583Ssam 
5766583Ssam ip_ctlinput(cmd, arg)
5776583Ssam 	int cmd;
5786583Ssam 	caddr_t arg;
5796583Ssam {
5808775Sroot 	struct in_addr *in;
58117358Skarels 	int in_rtchange(), tcp_abort(), udp_abort();
5826583Ssam 	extern struct inpcb tcb, udb;
5836583Ssam 
5846583Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
5856583Ssam 		return;
5866590Ssam 	if (inetctlerrmap[cmd] == 0)
5876583Ssam 		return;		/* XXX */
5886583Ssam 	if (cmd == PRC_IFDOWN)
5898775Sroot 		in = &((struct sockaddr_in *)arg)->sin_addr;
5906583Ssam 	else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH)
5918775Sroot 		in = (struct in_addr *)arg;
5926583Ssam 	else
5938775Sroot 		in = &((struct icmp *)arg)->icmp_ip.ip_dst;
59417358Skarels 	/* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */
59517358Skarels 	if (cmd == PRC_REDIRECT_NET || cmd == PRC_REDIRECT_HOST) {
59617358Skarels 		in_pcbnotify(&tcb, in, 0, in_rtchange);
59717358Skarels 		in_pcbnotify(&udb, in, 0, in_rtchange);
59817358Skarels 	} else {
59917358Skarels 		in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort);
60017358Skarels 		in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort);
60117358Skarels 	}
6026583Ssam }
6036583Ssam 
6046583Ssam int	ipprintfs = 0;
6056583Ssam int	ipforwarding = 1;
6066583Ssam /*
6076583Ssam  * Forward a packet.  If some error occurs return the sender
6086583Ssam  * and icmp packet.  Note we can't always generate a meaningful
6096583Ssam  * icmp message because icmp doesn't have a large enough repetoire
6106583Ssam  * of codes and types.
6116583Ssam  */
6126583Ssam ip_forward(ip)
6136583Ssam 	register struct ip *ip;
6146583Ssam {
6156583Ssam 	register int error, type, code;
6166609Ssam 	struct mbuf *mopt, *mcopy;
6176583Ssam 
6186583Ssam 	if (ipprintfs)
6196583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
6206583Ssam 			ip->ip_dst, ip->ip_ttl);
6216583Ssam 	if (ipforwarding == 0) {
6226583Ssam 		/* can't tell difference between net and host */
6236583Ssam 		type = ICMP_UNREACH, code = ICMP_UNREACH_NET;
6246583Ssam 		goto sendicmp;
6256583Ssam 	}
6266583Ssam 	if (ip->ip_ttl < IPTTLDEC) {
6276583Ssam 		type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS;
6286583Ssam 		goto sendicmp;
6296583Ssam 	}
6306583Ssam 	ip->ip_ttl -= IPTTLDEC;
6319641Ssam 	mopt = m_get(M_DONTWAIT, MT_DATA);
63211232Ssam 	if (mopt == NULL) {
6336583Ssam 		m_freem(dtom(ip));
6346583Ssam 		return;
6356583Ssam 	}
6366609Ssam 
6376609Ssam 	/*
6386609Ssam 	 * Save at most 64 bytes of the packet in case
6396609Ssam 	 * we need to generate an ICMP message to the src.
6406609Ssam 	 */
6417843Sroot 	mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64));
6426583Ssam 	ip_stripoptions(ip, mopt);
6436583Ssam 
64412416Ssam 	error = ip_output(dtom(ip), mopt, (struct route *)0, IP_FORWARDING);
64511540Ssam 	if (error == 0) {
6466609Ssam 		if (mcopy)
6476609Ssam 			m_freem(mcopy);
6486583Ssam 		return;
6496609Ssam 	}
65011540Ssam 	if (mcopy == NULL)
65111540Ssam 		return;
6526609Ssam 	ip = mtod(mcopy, struct ip *);
6536609Ssam 	type = ICMP_UNREACH, code = 0;		/* need ``undefined'' */
6546609Ssam 	switch (error) {
6556609Ssam 
6566609Ssam 	case ENETUNREACH:
6576609Ssam 	case ENETDOWN:
6586583Ssam 		code = ICMP_UNREACH_NET;
6596609Ssam 		break;
6606609Ssam 
6616609Ssam 	case EMSGSIZE:
6626583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
6636609Ssam 		break;
6646609Ssam 
6656609Ssam 	case EPERM:
6666609Ssam 		code = ICMP_UNREACH_PORT;
6676609Ssam 		break;
6686609Ssam 
6696609Ssam 	case ENOBUFS:
6706609Ssam 		type = ICMP_SOURCEQUENCH;
6716609Ssam 		break;
6726609Ssam 
6736609Ssam 	case EHOSTDOWN:
6746609Ssam 	case EHOSTUNREACH:
6756609Ssam 		code = ICMP_UNREACH_HOST;
6766609Ssam 		break;
6776609Ssam 	}
6786583Ssam sendicmp:
6796583Ssam 	icmp_error(ip, type, code);
6806583Ssam }
681