xref: /csrg-svn/sys/netinet/ip_input.c (revision 7508)
1*7508Sroot /*	ip_input.c	1.46	82/07/24	*/
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"
166583Ssam #include <errno.h>
174495Swnj 
184898Swnj u_char	ip_protox[IPPROTO_MAX];
196210Swnj int	ipqmaxlen = IFQ_MAXLEN;
206338Ssam struct	ifnet *ifinet;			/* first inet interface */
214898Swnj 
224801Swnj /*
235172Swnj  * IP initialization: fill in IP protocol switch table.
245161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
254801Swnj  */
264801Swnj ip_init()
274801Swnj {
284898Swnj 	register struct protosw *pr;
294898Swnj 	register int i;
304495Swnj 
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();
486338Ssam struct	sockaddr_in ipaddr = { AF_INET };
494640Swnj 
504640Swnj /*
514640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
524640Swnj  * try to reassamble.  If complete and fragment queue exists, discard.
534640Swnj  * Process options.  Pass to next level.
544640Swnj  */
555084Swnj ipintr()
564495Swnj {
574923Swnj 	register struct ip *ip;
585084Swnj 	register struct mbuf *m;
595217Swnj 	struct mbuf *m0, *mopt;
604640Swnj 	register int i;
614495Swnj 	register struct ipq *fp;
625084Swnj 	int hlen, s;
634495Swnj 
645084Swnj next:
654640Swnj 	/*
665084Swnj 	 * Get next datagram off input queue and get IP header
675084Swnj 	 * in first mbuf.
684640Swnj 	 */
695084Swnj 	s = splimp();
705084Swnj 	IF_DEQUEUE(&ipintrq, m);
715084Swnj 	splx(s);
725218Swnj 	if (m == 0)
735084Swnj 		return;
745306Sroot 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) &&
755306Sroot 	    (m = m_pullup(m, sizeof (struct ip))) == 0)
765306Sroot 		return;
774640Swnj 	ip = mtod(m, struct ip *);
785161Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
795306Sroot 		if ((m = m_pullup(m, hlen)) == 0)
805306Sroot 			return;
815161Swnj 		ip = mtod(m, struct ip *);
825161Swnj 	}
834951Swnj 	if (ipcksum)
845217Swnj 		if (ip->ip_sum = in_cksum(m, hlen)) {
855161Swnj 			printf("ip_sum %x\n", ip->ip_sum);	/* XXX */
864951Swnj 			ipstat.ips_badsum++;
874951Swnj 			goto bad;
884495Swnj 		}
894951Swnj 
905217Swnj #if vax
914951Swnj 	/*
924951Swnj 	 * Convert fields to host representation.
934951Swnj 	 */
944907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
954640Swnj 	ip->ip_id = ntohs(ip->ip_id);
964951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
975217Swnj #endif
984495Swnj 
994543Swnj 	/*
1004640Swnj 	 * Check that the amount of data in the buffers
1014640Swnj 	 * is as at least much as the IP header would have us expect.
1024640Swnj 	 * Trim mbufs if longer than we expect.
1034640Swnj 	 * Drop packet if shorter than we expect.
1044543Swnj 	 */
1056475Sroot 	i = -ip->ip_len;
1065161Swnj 	m0 = m;
1076475Sroot 	for (;;) {
1084495Swnj 		i += m->m_len;
1096475Sroot 		if (m->m_next == 0)
1106475Sroot 			break;
1116475Sroot 		m = m->m_next;
1126088Sroot 	}
1136475Sroot 	if (i != 0) {
1146475Sroot 		if (i < 0) {
1155161Swnj 			ipstat.ips_tooshort++;
1164951Swnj 			goto bad;
1175161Swnj 		}
1186475Sroot 		if (i <= m->m_len)
1196475Sroot 			m->m_len -= i;
1206475Sroot 		else
1216475Sroot 			m_adj(m0, -i);
1224495Swnj 	}
1236475Sroot 	m = m0;
1244495Swnj 
1254640Swnj 	/*
1264640Swnj 	 * Process options and, if not destined for us,
1276583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1286583Ssam 	 * error was detected (causing an icmp message
1296583Ssam 	 * to be sent).
1304640Swnj 	 */
1316583Ssam 	if (hlen > sizeof (struct ip) && ip_dooptions(ip))
1326583Ssam 		goto next;
1336210Swnj 
1346338Ssam 	/*
1356350Ssam 	 * Fast check on the first internet
1366350Ssam 	 * interface in the list.
1376338Ssam 	 */
1386338Ssam 	if (ifinet) {
1396338Ssam 		struct sockaddr_in *sin;
1406338Ssam 
1416338Ssam 		sin = (struct sockaddr_in *)&ifinet->if_addr;
1426338Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1436338Ssam 			goto ours;
1446483Ssam 		sin = (struct sockaddr_in *)&ifinet->if_broadaddr;
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) {
1516583Ssam 		ip_forward(ip);
1525084Swnj 		goto next;
1534543Swnj 	}
1544495Swnj 
1556338Ssam ours:
1564640Swnj 	/*
1574640Swnj 	 * Look for queue of fragments
1584640Swnj 	 * of this datagram.
1594640Swnj 	 */
1604640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1614640Swnj 		if (ip->ip_id == fp->ipq_id &&
1624640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1634640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1644640Swnj 		    ip->ip_p == fp->ipq_p)
1654640Swnj 			goto found;
1664640Swnj 	fp = 0;
1674640Swnj found:
1684495Swnj 
1694640Swnj 	/*
1704640Swnj 	 * Adjust ip_len to not reflect header,
1714640Swnj 	 * set ip_mff if more fragments are expected,
1724640Swnj 	 * convert offset of this to bytes.
1734640Swnj 	 */
1744640Swnj 	ip->ip_len -= hlen;
1754898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1764640Swnj 	if (ip->ip_off & IP_MF)
1774898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1784640Swnj 	ip->ip_off <<= 3;
1794495Swnj 
1804640Swnj 	/*
1814640Swnj 	 * If datagram marked as having more fragments
1824640Swnj 	 * or if this is not the first fragment,
1834640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1844640Swnj 	 */
1854898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1864898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1874640Swnj 		if (ip == 0)
1885084Swnj 			goto next;
1894640Swnj 		hlen = ip->ip_hl << 2;
1904640Swnj 		m = dtom(ip);
1914640Swnj 	} else
1924640Swnj 		if (fp)
1934640Swnj 			(void) ip_freef(fp);
1944951Swnj 
1954951Swnj 	/*
1964951Swnj 	 * Switch out to protocol's input routine.
1974951Swnj 	 */
1984898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
1995084Swnj 	goto next;
2004951Swnj bad:
2014951Swnj 	m_freem(m);
2025084Swnj 	goto next;
2034640Swnj }
2044495Swnj 
2054640Swnj /*
2064640Swnj  * Take incoming datagram fragment and try to
2074951Swnj  * reassemble it into whole datagram.  If a chain for
2084640Swnj  * reassembly of this datagram already exists, then it
2094640Swnj  * is given as fp; otherwise have to make a chain.
2104640Swnj  */
2114640Swnj struct ip *
2124640Swnj ip_reass(ip, fp)
2134898Swnj 	register struct ipasfrag *ip;
2144640Swnj 	register struct ipq *fp;
2154640Swnj {
2164640Swnj 	register struct mbuf *m = dtom(ip);
2174898Swnj 	register struct ipasfrag *q;
2184640Swnj 	struct mbuf *t;
2194640Swnj 	int hlen = ip->ip_hl << 2;
2204640Swnj 	int i, next;
2214543Swnj 
2224640Swnj 	/*
2234640Swnj 	 * Presence of header sizes in mbufs
2244640Swnj 	 * would confuse code below.
2254640Swnj 	 */
2264640Swnj 	m->m_off += hlen;
2274640Swnj 	m->m_len -= hlen;
2284495Swnj 
2294640Swnj 	/*
2304640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2314640Swnj 	 */
2324640Swnj 	if (fp == 0) {
2335851Sroot 		if ((t = m_get(M_WAIT)) == NULL)
2344640Swnj 			goto dropfrag;
2354640Swnj 		t->m_off = MMINOFF;
2364640Swnj 		fp = mtod(t, struct ipq *);
2374640Swnj 		insque(fp, &ipq);
2384640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2394640Swnj 		fp->ipq_p = ip->ip_p;
2404640Swnj 		fp->ipq_id = ip->ip_id;
2414898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2424898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2434898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2445161Swnj 		q = (struct ipasfrag *)fp;
2455161Swnj 		goto insert;
2464640Swnj 	}
2474495Swnj 
2484640Swnj 	/*
2494640Swnj 	 * Find a segment which begins after this one does.
2504640Swnj 	 */
2514898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2524640Swnj 		if (q->ip_off > ip->ip_off)
2534640Swnj 			break;
2544495Swnj 
2554640Swnj 	/*
2564640Swnj 	 * If there is a preceding segment, it may provide some of
2574640Swnj 	 * our data already.  If so, drop the data from the incoming
2584640Swnj 	 * segment.  If it provides all of our data, drop us.
2594640Swnj 	 */
2604898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2614898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2624640Swnj 		if (i > 0) {
2634640Swnj 			if (i >= ip->ip_len)
2644640Swnj 				goto dropfrag;
2654640Swnj 			m_adj(dtom(ip), i);
2664640Swnj 			ip->ip_off += i;
2674640Swnj 			ip->ip_len -= i;
2684640Swnj 		}
2694640Swnj 	}
2704543Swnj 
2714640Swnj 	/*
2724640Swnj 	 * While we overlap succeeding segments trim them or,
2734640Swnj 	 * if they are completely covered, dequeue them.
2744640Swnj 	 */
2754898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2764640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2774640Swnj 		if (i < q->ip_len) {
2784640Swnj 			q->ip_len -= i;
2796256Sroot 			q->ip_off += i;
2804640Swnj 			m_adj(dtom(q), i);
2814640Swnj 			break;
2824495Swnj 		}
2834898Swnj 		q = q->ipf_next;
2844898Swnj 		m_freem(dtom(q->ipf_prev));
2854898Swnj 		ip_deq(q->ipf_prev);
2864543Swnj 	}
2874495Swnj 
2885161Swnj insert:
2894640Swnj 	/*
2904640Swnj 	 * Stick new segment in its place;
2914640Swnj 	 * check for complete reassembly.
2924640Swnj 	 */
2934898Swnj 	ip_enq(ip, q->ipf_prev);
2944640Swnj 	next = 0;
2954898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
2964640Swnj 		if (q->ip_off != next)
2974640Swnj 			return (0);
2984640Swnj 		next += q->ip_len;
2994640Swnj 	}
3004898Swnj 	if (q->ipf_prev->ipf_mff)
3014640Swnj 		return (0);
3024495Swnj 
3034640Swnj 	/*
3044640Swnj 	 * Reassembly is complete; concatenate fragments.
3054640Swnj 	 */
3064640Swnj 	q = fp->ipq_next;
3074640Swnj 	m = dtom(q);
3084640Swnj 	t = m->m_next;
3094640Swnj 	m->m_next = 0;
3104640Swnj 	m_cat(m, t);
3116298Swnj 	q = q->ipf_next;
3126298Swnj 	while (q != (struct ipasfrag *)fp) {
3136298Swnj 		t = dtom(q);
3146298Swnj 		q = q->ipf_next;
3156298Swnj 		m_cat(m, t);
3166298Swnj 	}
3174495Swnj 
3184640Swnj 	/*
3194640Swnj 	 * Create header for new ip packet by
3204640Swnj 	 * modifying header of first packet;
3214640Swnj 	 * dequeue and discard fragment reassembly header.
3224640Swnj 	 * Make header visible.
3234640Swnj 	 */
3244640Swnj 	ip = fp->ipq_next;
3254640Swnj 	ip->ip_len = next;
3264898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
3274898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
3284640Swnj 	remque(fp);
3294907Swnj 	(void) m_free(dtom(fp));
3304640Swnj 	m = dtom(ip);
3314898Swnj 	m->m_len += sizeof (struct ipasfrag);
3324898Swnj 	m->m_off -= sizeof (struct ipasfrag);
3334898Swnj 	return ((struct ip *)ip);
3344495Swnj 
3354640Swnj dropfrag:
3364640Swnj 	m_freem(m);
3374640Swnj 	return (0);
3384495Swnj }
3394495Swnj 
3404640Swnj /*
3414640Swnj  * Free a fragment reassembly header and all
3424640Swnj  * associated datagrams.
3434640Swnj  */
3444640Swnj struct ipq *
3454640Swnj ip_freef(fp)
3464640Swnj 	struct ipq *fp;
3474495Swnj {
3484898Swnj 	register struct ipasfrag *q;
3494640Swnj 	struct mbuf *m;
3504495Swnj 
3514898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3524640Swnj 		m_freem(dtom(q));
3534640Swnj 	m = dtom(fp);
3544640Swnj 	fp = fp->next;
3554640Swnj 	remque(fp->prev);
3564907Swnj 	(void) m_free(m);
3574640Swnj 	return (fp);
3584495Swnj }
3594495Swnj 
3604640Swnj /*
3614640Swnj  * Put an ip fragment on a reassembly chain.
3624640Swnj  * Like insque, but pointers in middle of structure.
3634640Swnj  */
3644640Swnj ip_enq(p, prev)
3654898Swnj 	register struct ipasfrag *p, *prev;
3664495Swnj {
3674951Swnj 
3684898Swnj 	p->ipf_prev = prev;
3694898Swnj 	p->ipf_next = prev->ipf_next;
3704898Swnj 	prev->ipf_next->ipf_prev = p;
3714898Swnj 	prev->ipf_next = p;
3724495Swnj }
3734495Swnj 
3744640Swnj /*
3754640Swnj  * To ip_enq as remque is to insque.
3764640Swnj  */
3774640Swnj ip_deq(p)
3784898Swnj 	register struct ipasfrag *p;
3794640Swnj {
3804951Swnj 
3814898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3824898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3834495Swnj }
3844495Swnj 
3854640Swnj /*
3864640Swnj  * IP timer processing;
3874640Swnj  * if a timer expires on a reassembly
3884640Swnj  * queue, discard it.
3894640Swnj  */
3904801Swnj ip_slowtimo()
3914495Swnj {
3924495Swnj 	register struct ipq *fp;
3934640Swnj 	int s = splnet();
3944951Swnj 
3955243Sroot 	fp = ipq.next;
3965243Sroot 	if (fp == 0) {
3975243Sroot 		splx(s);
3985243Sroot 		return;
3995243Sroot 	}
4005243Sroot 	while (fp != &ipq)
4014640Swnj 		if (--fp->ipq_ttl == 0)
4024640Swnj 			fp = ip_freef(fp);
4034640Swnj 		else
4044640Swnj 			fp = fp->next;
4054640Swnj 	splx(s);
4064495Swnj }
4074495Swnj 
4084951Swnj /*
4094951Swnj  * Drain off all datagram fragments.
4104951Swnj  */
4114801Swnj ip_drain()
4124801Swnj {
4134801Swnj 
4144951Swnj 	while (ipq.next != &ipq)
4154951Swnj 		(void) ip_freef(ipq.next);
4164801Swnj }
4174923Swnj 
4184640Swnj /*
4194640Swnj  * Do option processing on a datagram,
4204640Swnj  * possibly discarding it if bad options
4214640Swnj  * are encountered.
4224640Swnj  */
4234640Swnj ip_dooptions(ip)
4244640Swnj 	struct ip *ip;
4254495Swnj {
4264640Swnj 	register u_char *cp;
4276583Ssam 	int opt, optlen, cnt, code, type;
4284923Swnj 	struct in_addr *sin;
4294801Swnj 	register struct ip_timestamp *ipt;
4304951Swnj 	register struct ifnet *ifp;
4314951Swnj 	struct in_addr t;
4324495Swnj 
4334640Swnj 	cp = (u_char *)(ip + 1);
4344640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
4354640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4364640Swnj 		opt = cp[0];
4374640Swnj 		if (opt == IPOPT_EOL)
4384640Swnj 			break;
4394640Swnj 		if (opt == IPOPT_NOP)
4404640Swnj 			optlen = 1;
4414640Swnj 		else
4424640Swnj 			optlen = cp[1];
4434640Swnj 		switch (opt) {
4444495Swnj 
4454640Swnj 		default:
4464640Swnj 			break;
4474495Swnj 
4484951Swnj 		/*
4494951Swnj 		 * Source routing with record.
4504951Swnj 		 * Find interface with current destination address.
4514951Swnj 		 * If none on this machine then drop if strictly routed,
4524951Swnj 		 * or do nothing if loosely routed.
4534951Swnj 		 * Record interface address and bring up next address
4544951Swnj 		 * component.  If strictly routed make sure next
4554951Swnj 		 * address on directly accessible net.
4564951Swnj 		 */
4574640Swnj 		case IPOPT_LSRR:
458*7508Sroot 		case IPOPT_SSRR:
4594801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4604640Swnj 				break;
4614923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4626338Ssam 			ipaddr.sin_addr = *sin;
4636338Ssam 			ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
4646583Ssam 			type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL;
4654951Swnj 			if (ifp == 0) {
4664951Swnj 				if (opt == IPOPT_SSRR)
4674951Swnj 					goto bad;
4684951Swnj 				break;
4694640Swnj 			}
4704951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4714951Swnj 			cp[2] += 4;
4724951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4734951Swnj 				break;
4744951Swnj 			ip->ip_dst = sin[1];
4756338Ssam 			if (opt == IPOPT_SSRR &&
4767166Ssam 			    if_ifonnetof(in_netof(ip->ip_dst)) == 0)
4774951Swnj 				goto bad;
4784640Swnj 			break;
4794495Swnj 
4804640Swnj 		case IPOPT_TS:
4816583Ssam 			code = cp - (u_char *)ip;
4826583Ssam 			type = ICMP_PARAMPROB;
4834801Swnj 			ipt = (struct ip_timestamp *)cp;
4844801Swnj 			if (ipt->ipt_len < 5)
4854640Swnj 				goto bad;
4864801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
4874801Swnj 				if (++ipt->ipt_oflw == 0)
4884640Swnj 					goto bad;
4894495Swnj 				break;
4904640Swnj 			}
4914923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
4924801Swnj 			switch (ipt->ipt_flg) {
4934495Swnj 
4944640Swnj 			case IPOPT_TS_TSONLY:
4954640Swnj 				break;
4964640Swnj 
4974640Swnj 			case IPOPT_TS_TSANDADDR:
4984801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
4994640Swnj 					goto bad;
5006338Ssam 				if (ifinet == 0)
5016338Ssam 					goto bad;	/* ??? */
5026338Ssam 				*sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr;
5034640Swnj 				break;
5044640Swnj 
5054640Swnj 			case IPOPT_TS_PRESPEC:
5066338Ssam 				ipaddr.sin_addr = *sin;
5076583Ssam 				if (!if_ifwithaddr((struct sockaddr *)&ipaddr))
5084951Swnj 					continue;
5094801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5104640Swnj 					goto bad;
5114801Swnj 				ipt->ipt_ptr += 4;
5124640Swnj 				break;
5134640Swnj 
5144495Swnj 			default:
5154640Swnj 				goto bad;
5164495Swnj 			}
5174923Swnj 			*(n_time *)sin = iptime();
5184801Swnj 			ipt->ipt_ptr += 4;
5194640Swnj 		}
5204495Swnj 	}
5216583Ssam 	return (0);
5224640Swnj bad:
5236583Ssam 	icmp_error(ip, type, code);
5246583Ssam 	return (1);
5254495Swnj }
5264495Swnj 
5274640Swnj /*
5284951Swnj  * Strip out IP options, at higher
5294951Swnj  * level protocol in the kernel.
5304951Swnj  * Second argument is buffer to which options
5314951Swnj  * will be moved, and return value is their length.
5324640Swnj  */
5335217Swnj ip_stripoptions(ip, mopt)
5344640Swnj 	struct ip *ip;
5355217Swnj 	struct mbuf *mopt;
5364495Swnj {
5374640Swnj 	register int i;
5384640Swnj 	register struct mbuf *m;
5394640Swnj 	int olen;
5404640Swnj 
5414640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
5424951Swnj 	m = dtom(ip);
5434951Swnj 	ip++;
5445217Swnj 	if (mopt) {
5455217Swnj 		mopt->m_len = olen;
5465217Swnj 		mopt->m_off = MMINOFF;
5475217Swnj 		bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen);
5485217Swnj 	}
5494640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5504907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5515243Sroot 	m->m_len -= olen;
5524495Swnj }
5536583Ssam 
5546590Ssam u_char inetctlerrmap[] = {
5556583Ssam 	ECONNABORTED,	ECONNABORTED,	0,		0,
5566609Ssam 	0,		0,
5576609Ssam 	EHOSTDOWN,	EHOSTUNREACH,	ENETUNREACH,	EHOSTUNREACH,
5586583Ssam 	ECONNREFUSED,	ECONNREFUSED,	EMSGSIZE,	0,
5596583Ssam 	0,		0,		0,		0
5606583Ssam };
5616583Ssam 
5626583Ssam ip_ctlinput(cmd, arg)
5636583Ssam 	int cmd;
5646583Ssam 	caddr_t arg;
5656583Ssam {
5666583Ssam 	struct in_addr *sin;
5676590Ssam 	int tcp_abort(), udp_abort();
5686583Ssam 	extern struct inpcb tcb, udb;
5696583Ssam 
5706583Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
5716583Ssam 		return;
5726590Ssam 	if (inetctlerrmap[cmd] == 0)
5736583Ssam 		return;		/* XXX */
5746583Ssam 	if (cmd == PRC_IFDOWN)
5756583Ssam 		sin = &((struct sockaddr_in *)arg)->sin_addr;
5766583Ssam 	else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH)
5776583Ssam 		sin = (struct in_addr *)arg;
5786583Ssam 	else
5796583Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
5806590Ssam 	in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort);
5816590Ssam 	in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort);
5826583Ssam }
5836583Ssam 
5846583Ssam int	ipprintfs = 0;
5856583Ssam int	ipforwarding = 1;
5866583Ssam /*
5876583Ssam  * Forward a packet.  If some error occurs return the sender
5886583Ssam  * and icmp packet.  Note we can't always generate a meaningful
5896583Ssam  * icmp message because icmp doesn't have a large enough repetoire
5906583Ssam  * of codes and types.
5916583Ssam  */
5926583Ssam ip_forward(ip)
5936583Ssam 	register struct ip *ip;
5946583Ssam {
5956583Ssam 	register int error, type, code;
5966609Ssam 	struct mbuf *mopt, *mcopy;
5976583Ssam 
5986583Ssam 	if (ipprintfs)
5996583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
6006583Ssam 			ip->ip_dst, ip->ip_ttl);
6016583Ssam 	if (ipforwarding == 0) {
6026583Ssam 		/* can't tell difference between net and host */
6036583Ssam 		type = ICMP_UNREACH, code = ICMP_UNREACH_NET;
6046583Ssam 		goto sendicmp;
6056583Ssam 	}
6066583Ssam 	if (ip->ip_ttl < IPTTLDEC) {
6076583Ssam 		type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS;
6086583Ssam 		goto sendicmp;
6096583Ssam 	}
6106583Ssam 	ip->ip_ttl -= IPTTLDEC;
6116583Ssam 	mopt = m_get(M_DONTWAIT);
6126583Ssam 	if (mopt == 0) {
6136583Ssam 		m_freem(dtom(ip));
6146583Ssam 		return;
6156583Ssam 	}
6166609Ssam 
6176609Ssam 	/*
6186609Ssam 	 * Save at most 64 bytes of the packet in case
6196609Ssam 	 * we need to generate an ICMP message to the src.
6206609Ssam 	 */
6216609Ssam 	mcopy = m_copy(dtom(ip), 0, min(ip->ip_len, 64));
6226583Ssam 	ip_stripoptions(ip, mopt);
6236583Ssam 
6246583Ssam 	/* last 0 here means no directed broadcast */
6256609Ssam 	if ((error = ip_output(dtom(ip), mopt, 0, 0)) == 0) {
6266609Ssam 		if (mcopy)
6276609Ssam 			m_freem(mcopy);
6286583Ssam 		return;
6296609Ssam 	}
6306609Ssam 	ip = mtod(mcopy, struct ip *);
6316609Ssam 	type = ICMP_UNREACH, code = 0;		/* need ``undefined'' */
6326609Ssam 	switch (error) {
6336609Ssam 
6346609Ssam 	case ENETUNREACH:
6356609Ssam 	case ENETDOWN:
6366583Ssam 		code = ICMP_UNREACH_NET;
6376609Ssam 		break;
6386609Ssam 
6396609Ssam 	case EMSGSIZE:
6406583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
6416609Ssam 		break;
6426609Ssam 
6436609Ssam 	case EPERM:
6446609Ssam 		code = ICMP_UNREACH_PORT;
6456609Ssam 		break;
6466609Ssam 
6476609Ssam 	case ENOBUFS:
6486609Ssam 		type = ICMP_SOURCEQUENCH;
6496609Ssam 		break;
6506609Ssam 
6516609Ssam 	case EHOSTDOWN:
6526609Ssam 	case EHOSTUNREACH:
6536609Ssam 		code = ICMP_UNREACH_HOST;
6546609Ssam 		break;
6556609Ssam 	}
6566583Ssam sendicmp:
6576583Ssam 	icmp_error(ip, type, code);
6586583Ssam }
659