xref: /csrg-svn/sys/netinet/ip_input.c (revision 8637)
1*8637Sroot /*	ip_input.c	1.53	82/10/17	*/
24571Swnj 
34495Swnj #include "../h/param.h"
44543Swnj #include "../h/systm.h"
54640Swnj #include "../h/mbuf.h"
64898Swnj #include "../h/protosw.h"
74923Swnj #include "../h/socket.h"
88399Swnj #include "../netinet/in.h"
98597Sroot #include "../netinet/in_pcb.h"
108399Swnj #include "../netinet/in_systm.h"
114951Swnj #include "../net/if.h"
128399Swnj #include "../netinet/ip.h"			/* belongs before in.h */
138399Swnj #include "../netinet/ip_var.h"
148399Swnj #include "../netinet/ip_icmp.h"
158399Swnj #include "../netinet/tcp.h"
168172Sroot #include <time.h>
178172Sroot #include "../h/kernel.h"
186583Ssam #include <errno.h>
194495Swnj 
204898Swnj u_char	ip_protox[IPPROTO_MAX];
216210Swnj int	ipqmaxlen = IFQ_MAXLEN;
226338Ssam struct	ifnet *ifinet;			/* first inet interface */
234898Swnj 
244801Swnj /*
255172Swnj  * IP initialization: fill in IP protocol switch table.
265161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
274801Swnj  */
284801Swnj ip_init()
294801Swnj {
304898Swnj 	register struct protosw *pr;
314898Swnj 	register int i;
324495Swnj 
334898Swnj 	pr = pffindproto(PF_INET, IPPROTO_RAW);
344898Swnj 	if (pr == 0)
354898Swnj 		panic("ip_init");
364898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
374898Swnj 		ip_protox[i] = pr - protosw;
384898Swnj 	for (pr = protosw; pr <= protoswLAST; pr++)
394898Swnj 		if (pr->pr_family == PF_INET &&
404898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
414898Swnj 			ip_protox[pr->pr_protocol] = pr - protosw;
424801Swnj 	ipq.next = ipq.prev = &ipq;
438172Sroot 	ip_id = time.tv_sec & 0xffff;
446210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
456338Ssam 	ifinet = if_ifwithaf(AF_INET);
464801Swnj }
474801Swnj 
484898Swnj u_char	ipcksum = 1;
494640Swnj struct	ip *ip_reass();
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;
618597Sroot 	struct mbuf *m0;
624640Swnj 	register int i;
634495Swnj 	register struct ipq *fp;
645084Swnj 	int hlen, s;
654495Swnj 
665084Swnj next:
674640Swnj 	/*
685084Swnj 	 * Get next datagram off input queue and get IP header
695084Swnj 	 * in first mbuf.
704640Swnj 	 */
715084Swnj 	s = splimp();
725084Swnj 	IF_DEQUEUE(&ipintrq, m);
735084Swnj 	splx(s);
745218Swnj 	if (m == 0)
755084Swnj 		return;
765306Sroot 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) &&
775306Sroot 	    (m = m_pullup(m, sizeof (struct ip))) == 0)
785306Sroot 		return;
794640Swnj 	ip = mtod(m, struct ip *);
805161Swnj 	if ((hlen = ip->ip_hl << 2) > m->m_len) {
815306Sroot 		if ((m = m_pullup(m, hlen)) == 0)
825306Sroot 			return;
835161Swnj 		ip = mtod(m, struct ip *);
845161Swnj 	}
854951Swnj 	if (ipcksum)
865217Swnj 		if (ip->ip_sum = in_cksum(m, hlen)) {
875161Swnj 			printf("ip_sum %x\n", ip->ip_sum);	/* XXX */
884951Swnj 			ipstat.ips_badsum++;
894951Swnj 			goto bad;
904495Swnj 		}
914951Swnj 
928597Sroot #if vax || pdp11 || ns16032
934951Swnj 	/*
944951Swnj 	 * Convert fields to host representation.
954951Swnj 	 */
964907Swnj 	ip->ip_len = ntohs((u_short)ip->ip_len);
974640Swnj 	ip->ip_id = ntohs(ip->ip_id);
984951Swnj 	ip->ip_off = ntohs((u_short)ip->ip_off);
995217Swnj #endif
1004495Swnj 
1014543Swnj 	/*
1024640Swnj 	 * Check that the amount of data in the buffers
1034640Swnj 	 * is as at least much as the IP header would have us expect.
1044640Swnj 	 * Trim mbufs if longer than we expect.
1054640Swnj 	 * Drop packet if shorter than we expect.
1064543Swnj 	 */
1076475Sroot 	i = -ip->ip_len;
1085161Swnj 	m0 = m;
1096475Sroot 	for (;;) {
1104495Swnj 		i += m->m_len;
1116475Sroot 		if (m->m_next == 0)
1126475Sroot 			break;
1136475Sroot 		m = m->m_next;
1146088Sroot 	}
1156475Sroot 	if (i != 0) {
1166475Sroot 		if (i < 0) {
1175161Swnj 			ipstat.ips_tooshort++;
1184951Swnj 			goto bad;
1195161Swnj 		}
1206475Sroot 		if (i <= m->m_len)
1216475Sroot 			m->m_len -= i;
1226475Sroot 		else
1236475Sroot 			m_adj(m0, -i);
1244495Swnj 	}
1256475Sroot 	m = m0;
1264495Swnj 
1274640Swnj 	/*
1284640Swnj 	 * Process options and, if not destined for us,
1296583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1306583Ssam 	 * error was detected (causing an icmp message
1316583Ssam 	 * to be sent).
1324640Swnj 	 */
1336583Ssam 	if (hlen > sizeof (struct ip) && ip_dooptions(ip))
1346583Ssam 		goto next;
1356210Swnj 
1366338Ssam 	/*
1376350Ssam 	 * Fast check on the first internet
1386350Ssam 	 * interface in the list.
1396338Ssam 	 */
1406338Ssam 	if (ifinet) {
1416338Ssam 		struct sockaddr_in *sin;
1426338Ssam 
1436338Ssam 		sin = (struct sockaddr_in *)&ifinet->if_addr;
1446338Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1456338Ssam 			goto ours;
1466483Ssam 		sin = (struct sockaddr_in *)&ifinet->if_broadaddr;
1476350Ssam 		if ((ifinet->if_flags & IFF_BROADCAST) &&
1486350Ssam 		    sin->sin_addr.s_addr == ip->ip_dst.s_addr)
1496350Ssam 			goto ours;
1506338Ssam 	}
1516338Ssam 	ipaddr.sin_addr = ip->ip_dst;
1526338Ssam 	if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) {
1536583Ssam 		ip_forward(ip);
1545084Swnj 		goto next;
1554543Swnj 	}
1564495Swnj 
1576338Ssam ours:
1584640Swnj 	/*
1594640Swnj 	 * Look for queue of fragments
1604640Swnj 	 * of this datagram.
1614640Swnj 	 */
1624640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
1634640Swnj 		if (ip->ip_id == fp->ipq_id &&
1644640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
1654640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
1664640Swnj 		    ip->ip_p == fp->ipq_p)
1674640Swnj 			goto found;
1684640Swnj 	fp = 0;
1694640Swnj found:
1704495Swnj 
1714640Swnj 	/*
1724640Swnj 	 * Adjust ip_len to not reflect header,
1734640Swnj 	 * set ip_mff if more fragments are expected,
1744640Swnj 	 * convert offset of this to bytes.
1754640Swnj 	 */
1764640Swnj 	ip->ip_len -= hlen;
1774898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
1784640Swnj 	if (ip->ip_off & IP_MF)
1794898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
1804640Swnj 	ip->ip_off <<= 3;
1814495Swnj 
1824640Swnj 	/*
1834640Swnj 	 * If datagram marked as having more fragments
1844640Swnj 	 * or if this is not the first fragment,
1854640Swnj 	 * attempt reassembly; if it succeeds, proceed.
1864640Swnj 	 */
1874898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
1884898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
1894640Swnj 		if (ip == 0)
1905084Swnj 			goto next;
1914640Swnj 		hlen = ip->ip_hl << 2;
1924640Swnj 		m = dtom(ip);
1934640Swnj 	} else
1944640Swnj 		if (fp)
1954640Swnj 			(void) ip_freef(fp);
1964951Swnj 
1974951Swnj 	/*
1984951Swnj 	 * Switch out to protocol's input routine.
1994951Swnj 	 */
2004898Swnj 	(*protosw[ip_protox[ip->ip_p]].pr_input)(m);
2015084Swnj 	goto next;
2024951Swnj bad:
2034951Swnj 	m_freem(m);
2045084Swnj 	goto next;
2054640Swnj }
2064495Swnj 
2074640Swnj /*
2084640Swnj  * Take incoming datagram fragment and try to
2094951Swnj  * reassemble it into whole datagram.  If a chain for
2104640Swnj  * reassembly of this datagram already exists, then it
2114640Swnj  * is given as fp; otherwise have to make a chain.
2124640Swnj  */
2134640Swnj struct ip *
2144640Swnj ip_reass(ip, fp)
2154898Swnj 	register struct ipasfrag *ip;
2164640Swnj 	register struct ipq *fp;
2174640Swnj {
2184640Swnj 	register struct mbuf *m = dtom(ip);
2194898Swnj 	register struct ipasfrag *q;
2204640Swnj 	struct mbuf *t;
2214640Swnj 	int hlen = ip->ip_hl << 2;
2224640Swnj 	int i, next;
2234543Swnj 
2244640Swnj 	/*
2254640Swnj 	 * Presence of header sizes in mbufs
2264640Swnj 	 * would confuse code below.
2274640Swnj 	 */
2284640Swnj 	m->m_off += hlen;
2294640Swnj 	m->m_len -= hlen;
2304495Swnj 
2314640Swnj 	/*
2324640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2334640Swnj 	 */
2344640Swnj 	if (fp == 0) {
2355851Sroot 		if ((t = m_get(M_WAIT)) == NULL)
2364640Swnj 			goto dropfrag;
2374640Swnj 		fp = mtod(t, struct ipq *);
2384640Swnj 		insque(fp, &ipq);
2394640Swnj 		fp->ipq_ttl = IPFRAGTTL;
2404640Swnj 		fp->ipq_p = ip->ip_p;
2414640Swnj 		fp->ipq_id = ip->ip_id;
2424898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
2434898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
2444898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
2455161Swnj 		q = (struct ipasfrag *)fp;
2465161Swnj 		goto insert;
2474640Swnj 	}
2484495Swnj 
2494640Swnj 	/*
2504640Swnj 	 * Find a segment which begins after this one does.
2514640Swnj 	 */
2524898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
2534640Swnj 		if (q->ip_off > ip->ip_off)
2544640Swnj 			break;
2554495Swnj 
2564640Swnj 	/*
2574640Swnj 	 * If there is a preceding segment, it may provide some of
2584640Swnj 	 * our data already.  If so, drop the data from the incoming
2594640Swnj 	 * segment.  If it provides all of our data, drop us.
2604640Swnj 	 */
2614898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
2624898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
2634640Swnj 		if (i > 0) {
2644640Swnj 			if (i >= ip->ip_len)
2654640Swnj 				goto dropfrag;
2664640Swnj 			m_adj(dtom(ip), i);
2674640Swnj 			ip->ip_off += i;
2684640Swnj 			ip->ip_len -= i;
2694640Swnj 		}
2704640Swnj 	}
2714543Swnj 
2724640Swnj 	/*
2734640Swnj 	 * While we overlap succeeding segments trim them or,
2744640Swnj 	 * if they are completely covered, dequeue them.
2754640Swnj 	 */
2764898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
2774640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
2784640Swnj 		if (i < q->ip_len) {
2794640Swnj 			q->ip_len -= i;
2806256Sroot 			q->ip_off += i;
2814640Swnj 			m_adj(dtom(q), i);
2824640Swnj 			break;
2834495Swnj 		}
2844898Swnj 		q = q->ipf_next;
2854898Swnj 		m_freem(dtom(q->ipf_prev));
2864898Swnj 		ip_deq(q->ipf_prev);
2874543Swnj 	}
2884495Swnj 
2895161Swnj insert:
2904640Swnj 	/*
2914640Swnj 	 * Stick new segment in its place;
2924640Swnj 	 * check for complete reassembly.
2934640Swnj 	 */
2944898Swnj 	ip_enq(ip, q->ipf_prev);
2954640Swnj 	next = 0;
2964898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
2974640Swnj 		if (q->ip_off != next)
2984640Swnj 			return (0);
2994640Swnj 		next += q->ip_len;
3004640Swnj 	}
3014898Swnj 	if (q->ipf_prev->ipf_mff)
3024640Swnj 		return (0);
3034495Swnj 
3044640Swnj 	/*
3054640Swnj 	 * Reassembly is complete; concatenate fragments.
3064640Swnj 	 */
3074640Swnj 	q = fp->ipq_next;
3084640Swnj 	m = dtom(q);
3094640Swnj 	t = m->m_next;
3104640Swnj 	m->m_next = 0;
3114640Swnj 	m_cat(m, t);
3126298Swnj 	q = q->ipf_next;
3136298Swnj 	while (q != (struct ipasfrag *)fp) {
3146298Swnj 		t = dtom(q);
3156298Swnj 		q = q->ipf_next;
3166298Swnj 		m_cat(m, t);
3176298Swnj 	}
3184495Swnj 
3194640Swnj 	/*
3204640Swnj 	 * Create header for new ip packet by
3214640Swnj 	 * modifying header of first packet;
3224640Swnj 	 * dequeue and discard fragment reassembly header.
3234640Swnj 	 * Make header visible.
3244640Swnj 	 */
3254640Swnj 	ip = fp->ipq_next;
3264640Swnj 	ip->ip_len = next;
3274898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
3284898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
3294640Swnj 	remque(fp);
3304907Swnj 	(void) m_free(dtom(fp));
3314640Swnj 	m = dtom(ip);
3324898Swnj 	m->m_len += sizeof (struct ipasfrag);
3334898Swnj 	m->m_off -= sizeof (struct ipasfrag);
3344898Swnj 	return ((struct ip *)ip);
3354495Swnj 
3364640Swnj dropfrag:
3374640Swnj 	m_freem(m);
3384640Swnj 	return (0);
3394495Swnj }
3404495Swnj 
3414640Swnj /*
3424640Swnj  * Free a fragment reassembly header and all
3434640Swnj  * associated datagrams.
3444640Swnj  */
3454640Swnj struct ipq *
3464640Swnj ip_freef(fp)
3474640Swnj 	struct ipq *fp;
3484495Swnj {
3494898Swnj 	register struct ipasfrag *q;
3504640Swnj 	struct mbuf *m;
3514495Swnj 
3524898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3534640Swnj 		m_freem(dtom(q));
3544640Swnj 	m = dtom(fp);
3554640Swnj 	fp = fp->next;
3564640Swnj 	remque(fp->prev);
3574907Swnj 	(void) m_free(m);
3584640Swnj 	return (fp);
3594495Swnj }
3604495Swnj 
3614640Swnj /*
3624640Swnj  * Put an ip fragment on a reassembly chain.
3634640Swnj  * Like insque, but pointers in middle of structure.
3644640Swnj  */
3654640Swnj ip_enq(p, prev)
3664898Swnj 	register struct ipasfrag *p, *prev;
3674495Swnj {
3684951Swnj 
3694898Swnj 	p->ipf_prev = prev;
3704898Swnj 	p->ipf_next = prev->ipf_next;
3714898Swnj 	prev->ipf_next->ipf_prev = p;
3724898Swnj 	prev->ipf_next = p;
3734495Swnj }
3744495Swnj 
3754640Swnj /*
3764640Swnj  * To ip_enq as remque is to insque.
3774640Swnj  */
3784640Swnj ip_deq(p)
3794898Swnj 	register struct ipasfrag *p;
3804640Swnj {
3814951Swnj 
3824898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
3834898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
3844495Swnj }
3854495Swnj 
3864640Swnj /*
3874640Swnj  * IP timer processing;
3884640Swnj  * if a timer expires on a reassembly
3894640Swnj  * queue, discard it.
3904640Swnj  */
3914801Swnj ip_slowtimo()
3924495Swnj {
3934495Swnj 	register struct ipq *fp;
3944640Swnj 	int s = splnet();
3954951Swnj 
3965243Sroot 	fp = ipq.next;
3975243Sroot 	if (fp == 0) {
3985243Sroot 		splx(s);
3995243Sroot 		return;
4005243Sroot 	}
4015243Sroot 	while (fp != &ipq)
4024640Swnj 		if (--fp->ipq_ttl == 0)
4034640Swnj 			fp = ip_freef(fp);
4044640Swnj 		else
4054640Swnj 			fp = fp->next;
4064640Swnj 	splx(s);
4074495Swnj }
4084495Swnj 
4094951Swnj /*
4104951Swnj  * Drain off all datagram fragments.
4114951Swnj  */
4124801Swnj ip_drain()
4134801Swnj {
4144801Swnj 
4154951Swnj 	while (ipq.next != &ipq)
4164951Swnj 		(void) ip_freef(ipq.next);
4174801Swnj }
4184923Swnj 
4194640Swnj /*
4204640Swnj  * Do option processing on a datagram,
4214640Swnj  * possibly discarding it if bad options
4224640Swnj  * are encountered.
4234640Swnj  */
4244640Swnj ip_dooptions(ip)
4254640Swnj 	struct ip *ip;
4264495Swnj {
4274640Swnj 	register u_char *cp;
4286583Ssam 	int opt, optlen, cnt, code, type;
4294923Swnj 	struct in_addr *sin;
4304801Swnj 	register struct ip_timestamp *ipt;
4314951Swnj 	register struct ifnet *ifp;
4324951Swnj 	struct in_addr t;
4334495Swnj 
4344640Swnj 	cp = (u_char *)(ip + 1);
4354640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
4364640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
4374640Swnj 		opt = cp[0];
4384640Swnj 		if (opt == IPOPT_EOL)
4394640Swnj 			break;
4404640Swnj 		if (opt == IPOPT_NOP)
4414640Swnj 			optlen = 1;
4424640Swnj 		else
4434640Swnj 			optlen = cp[1];
4444640Swnj 		switch (opt) {
4454495Swnj 
4464640Swnj 		default:
4474640Swnj 			break;
4484495Swnj 
4494951Swnj 		/*
4504951Swnj 		 * Source routing with record.
4514951Swnj 		 * Find interface with current destination address.
4524951Swnj 		 * If none on this machine then drop if strictly routed,
4534951Swnj 		 * or do nothing if loosely routed.
4544951Swnj 		 * Record interface address and bring up next address
4554951Swnj 		 * component.  If strictly routed make sure next
4564951Swnj 		 * address on directly accessible net.
4574951Swnj 		 */
4584640Swnj 		case IPOPT_LSRR:
4597508Sroot 		case IPOPT_SSRR:
4604801Swnj 			if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
4614640Swnj 				break;
4624923Swnj 			sin = (struct in_addr *)(cp + cp[2]);
4636338Ssam 			ipaddr.sin_addr = *sin;
4646338Ssam 			ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
4656583Ssam 			type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL;
4664951Swnj 			if (ifp == 0) {
4674951Swnj 				if (opt == IPOPT_SSRR)
4684951Swnj 					goto bad;
4694951Swnj 				break;
4704640Swnj 			}
4714951Swnj 			t = ip->ip_dst; ip->ip_dst = *sin; *sin = t;
4724951Swnj 			cp[2] += 4;
4734951Swnj 			if (cp[2] > optlen - (sizeof (long) - 1))
4744951Swnj 				break;
4754951Swnj 			ip->ip_dst = sin[1];
4766338Ssam 			if (opt == IPOPT_SSRR &&
4777166Ssam 			    if_ifonnetof(in_netof(ip->ip_dst)) == 0)
4784951Swnj 				goto bad;
4794640Swnj 			break;
4804495Swnj 
4814640Swnj 		case IPOPT_TS:
4826583Ssam 			code = cp - (u_char *)ip;
4836583Ssam 			type = ICMP_PARAMPROB;
4844801Swnj 			ipt = (struct ip_timestamp *)cp;
4854801Swnj 			if (ipt->ipt_len < 5)
4864640Swnj 				goto bad;
4874801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
4884801Swnj 				if (++ipt->ipt_oflw == 0)
4894640Swnj 					goto bad;
4904495Swnj 				break;
4914640Swnj 			}
4924923Swnj 			sin = (struct in_addr *)(cp+cp[2]);
4934801Swnj 			switch (ipt->ipt_flg) {
4944495Swnj 
4954640Swnj 			case IPOPT_TS_TSONLY:
4964640Swnj 				break;
4974640Swnj 
4984640Swnj 			case IPOPT_TS_TSANDADDR:
4994801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5004640Swnj 					goto bad;
5016338Ssam 				if (ifinet == 0)
5026338Ssam 					goto bad;	/* ??? */
5036338Ssam 				*sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr;
5044640Swnj 				break;
5054640Swnj 
5064640Swnj 			case IPOPT_TS_PRESPEC:
5076338Ssam 				ipaddr.sin_addr = *sin;
5086583Ssam 				if (!if_ifwithaddr((struct sockaddr *)&ipaddr))
5094951Swnj 					continue;
5104801Swnj 				if (ipt->ipt_ptr + 8 > ipt->ipt_len)
5114640Swnj 					goto bad;
5124801Swnj 				ipt->ipt_ptr += 4;
5134640Swnj 				break;
5144640Swnj 
5154495Swnj 			default:
5164640Swnj 				goto bad;
5174495Swnj 			}
5184923Swnj 			*(n_time *)sin = iptime();
5194801Swnj 			ipt->ipt_ptr += 4;
5204640Swnj 		}
5214495Swnj 	}
5226583Ssam 	return (0);
5234640Swnj bad:
5246583Ssam 	icmp_error(ip, type, code);
5256583Ssam 	return (1);
5264495Swnj }
5274495Swnj 
5284640Swnj /*
5294951Swnj  * Strip out IP options, at higher
5304951Swnj  * level protocol in the kernel.
5314951Swnj  * Second argument is buffer to which options
5324951Swnj  * will be moved, and return value is their length.
5334640Swnj  */
5345217Swnj ip_stripoptions(ip, mopt)
5354640Swnj 	struct ip *ip;
5365217Swnj 	struct mbuf *mopt;
5374495Swnj {
5384640Swnj 	register int i;
5394640Swnj 	register struct mbuf *m;
5404640Swnj 	int olen;
5414640Swnj 
5424640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
5434951Swnj 	m = dtom(ip);
5444951Swnj 	ip++;
5455217Swnj 	if (mopt) {
5465217Swnj 		mopt->m_len = olen;
5475217Swnj 		mopt->m_off = MMINOFF;
5485217Swnj 		bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen);
5495217Swnj 	}
5504640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
5514907Swnj 	bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i);
5525243Sroot 	m->m_len -= olen;
5534495Swnj }
5546583Ssam 
5556590Ssam u_char inetctlerrmap[] = {
5566583Ssam 	ECONNABORTED,	ECONNABORTED,	0,		0,
5576609Ssam 	0,		0,
5586609Ssam 	EHOSTDOWN,	EHOSTUNREACH,	ENETUNREACH,	EHOSTUNREACH,
5596583Ssam 	ECONNREFUSED,	ECONNREFUSED,	EMSGSIZE,	0,
5606583Ssam 	0,		0,		0,		0
5616583Ssam };
5626583Ssam 
5636583Ssam ip_ctlinput(cmd, arg)
5646583Ssam 	int cmd;
5656583Ssam 	caddr_t arg;
5666583Ssam {
5676583Ssam 	struct in_addr *sin;
5686590Ssam 	int tcp_abort(), udp_abort();
5696583Ssam 	extern struct inpcb tcb, udb;
5706583Ssam 
5716583Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
5726583Ssam 		return;
5736590Ssam 	if (inetctlerrmap[cmd] == 0)
5746583Ssam 		return;		/* XXX */
5756583Ssam 	if (cmd == PRC_IFDOWN)
5766583Ssam 		sin = &((struct sockaddr_in *)arg)->sin_addr;
5776583Ssam 	else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH)
5786583Ssam 		sin = (struct in_addr *)arg;
5796583Ssam 	else
5806583Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
5818597Sroot /* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */
582*8637Sroot 	in_pcbnotify(&tcb, (struct sockaddr *)sin,
583*8637Sroot 	    inetctlerrmap[cmd], tcp_abort);
584*8637Sroot 	in_pcbnotify(&udb, (struct sockaddr *)sin,
585*8637Sroot 	    inetctlerrmap[cmd], udp_abort);
5866583Ssam }
5876583Ssam 
5886583Ssam int	ipprintfs = 0;
5896583Ssam int	ipforwarding = 1;
5906583Ssam /*
5916583Ssam  * Forward a packet.  If some error occurs return the sender
5926583Ssam  * and icmp packet.  Note we can't always generate a meaningful
5936583Ssam  * icmp message because icmp doesn't have a large enough repetoire
5946583Ssam  * of codes and types.
5956583Ssam  */
5966583Ssam ip_forward(ip)
5976583Ssam 	register struct ip *ip;
5986583Ssam {
5996583Ssam 	register int error, type, code;
6006609Ssam 	struct mbuf *mopt, *mcopy;
6016583Ssam 
6026583Ssam 	if (ipprintfs)
6036583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
6046583Ssam 			ip->ip_dst, ip->ip_ttl);
6056583Ssam 	if (ipforwarding == 0) {
6066583Ssam 		/* can't tell difference between net and host */
6076583Ssam 		type = ICMP_UNREACH, code = ICMP_UNREACH_NET;
6086583Ssam 		goto sendicmp;
6096583Ssam 	}
6106583Ssam 	if (ip->ip_ttl < IPTTLDEC) {
6116583Ssam 		type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS;
6126583Ssam 		goto sendicmp;
6136583Ssam 	}
6146583Ssam 	ip->ip_ttl -= IPTTLDEC;
6156583Ssam 	mopt = m_get(M_DONTWAIT);
6166583Ssam 	if (mopt == 0) {
6176583Ssam 		m_freem(dtom(ip));
6186583Ssam 		return;
6196583Ssam 	}
6206609Ssam 
6216609Ssam 	/*
6226609Ssam 	 * Save at most 64 bytes of the packet in case
6236609Ssam 	 * we need to generate an ICMP message to the src.
6246609Ssam 	 */
6257843Sroot 	mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64));
6266583Ssam 	ip_stripoptions(ip, mopt);
6276583Ssam 
6286583Ssam 	/* last 0 here means no directed broadcast */
629*8637Sroot 	if ((error = ip_output(dtom(ip), mopt, (struct route *)0, 0)) == 0) {
6306609Ssam 		if (mcopy)
6316609Ssam 			m_freem(mcopy);
6326583Ssam 		return;
6336609Ssam 	}
6346609Ssam 	ip = mtod(mcopy, struct ip *);
6356609Ssam 	type = ICMP_UNREACH, code = 0;		/* need ``undefined'' */
6366609Ssam 	switch (error) {
6376609Ssam 
6386609Ssam 	case ENETUNREACH:
6396609Ssam 	case ENETDOWN:
6406583Ssam 		code = ICMP_UNREACH_NET;
6416609Ssam 		break;
6426609Ssam 
6436609Ssam 	case EMSGSIZE:
6446583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
6456609Ssam 		break;
6466609Ssam 
6476609Ssam 	case EPERM:
6486609Ssam 		code = ICMP_UNREACH_PORT;
6496609Ssam 		break;
6506609Ssam 
6516609Ssam 	case ENOBUFS:
6526609Ssam 		type = ICMP_SOURCEQUENCH;
6536609Ssam 		break;
6546609Ssam 
6556609Ssam 	case EHOSTDOWN:
6566609Ssam 	case EHOSTUNREACH:
6576609Ssam 		code = ICMP_UNREACH_HOST;
6586609Ssam 		break;
6596609Ssam 	}
6606583Ssam sendicmp:
6616583Ssam 	icmp_error(ip, type, code);
6626583Ssam }
663