ip_input.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,282 行 · 第 1/3 页

C
1,282
字号
			m = dtom(ip);		} else			if (fp)				ip_freef(fp);	} else		ip->ip_len -= hlen;	/*	 * Switch out to protocol's input routine.	 */	ipstat.ips_delivered++;	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);	goto next;bad:	m_freem(m);	goto next;}NETISR_SET(NETISR_IP, ipintr);/* * Take incoming datagram fragment and try to * reassemble it into whole datagram.  If a chain for * reassembly of this datagram already exists, then it * is given as fp; otherwise have to make a chain. */struct ip *ip_reass(ip, fp)	register struct ipasfrag *ip;	register struct ipq *fp;{	register struct mbuf *m = dtom(ip);	register struct ipasfrag *q;	struct mbuf *t;	int hlen = ip->ip_hl << 2;	int i, next;	/*	 * Presence of header sizes in mbufs	 * would confuse code below.	 */	m->m_data += hlen;	m->m_len -= hlen;	/*	 * If first fragment to arrive, create a reassembly queue.	 */	if (fp == 0) {		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)			goto dropfrag;		fp = mtod(t, struct ipq *);		insque(fp, &ipq);		fp->ipq_ttl = IPFRAGTTL;		fp->ipq_p = ip->ip_p;		fp->ipq_id = ip->ip_id;		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;		fp->ipq_src = ((struct ip *)ip)->ip_src;		fp->ipq_dst = ((struct ip *)ip)->ip_dst;		q = (struct ipasfrag *)fp;		goto insert;	}	/*	 * Find a segment which begins after this one does.	 */	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)		if (q->ip_off > ip->ip_off)			break;	/*	 * If there is a preceding segment, it may provide some of	 * our data already.  If so, drop the data from the incoming	 * segment.  If it provides all of our data, drop us.	 */	if (q->ipf_prev != (struct ipasfrag *)fp) {		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;		if (i > 0) {			if (i >= ip->ip_len)				goto dropfrag;			m_adj(dtom(ip), i);			ip->ip_off += i;			ip->ip_len -= i;		}	}	/*	 * While we overlap succeeding segments trim them or,	 * if they are completely covered, dequeue them.	 */	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {		struct mbuf *m0;		i = (ip->ip_off + ip->ip_len) - q->ip_off;		if (i < q->ip_len) {			q->ip_len -= i;			q->ip_off += i;			m_adj(dtom(q), i);			break;		}		m0 = dtom(q);		q = q->ipf_next;		ip_deq(q->ipf_prev);		m_freem(m0);	}insert:	/*	 * Stick new segment in its place;	 * check for complete reassembly.	 */	ip_enq(ip, q->ipf_prev);	next = 0;	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {		if (q->ip_off != next)			return (0);		next += q->ip_len;	}	if (q->ipf_prev->ipf_mff & 1)		return (0);	/*	 * Reassembly is complete; concatenate fragments.	 */	q = fp->ipq_next;	m = dtom(q);	t = m->m_next;	m->m_next = 0;	m_cat(m, t);	q = q->ipf_next;	while (q != (struct ipasfrag *)fp) {		t = dtom(q);		q = q->ipf_next;		m_cat(m, t);	}	/*	 * Create header for new ip packet by	 * modifying header of first packet;	 * dequeue and discard fragment reassembly header.	 * Make header visible.	 */	ip = fp->ipq_next;	ip->ip_len = next;	ip->ipf_mff &= ~1;	((struct ip *)ip)->ip_src = fp->ipq_src;	((struct ip *)ip)->ip_dst = fp->ipq_dst;	remque(fp);	(void) m_free(dtom(fp));	m = dtom(ip);	m->m_len += (ip->ip_hl << 2);	m->m_data -= (ip->ip_hl << 2);	/* some debugging cruft by sklower, below, will go away soon */	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */		register int plen = 0;		for (t = m; m; m = m->m_next)			plen += m->m_len;		t->m_pkthdr.len = plen;	}	return ((struct ip *)ip);dropfrag:	ipstat.ips_fragdropped++;	m_freem(m);	return (0);}/* * Free a fragment reassembly header and all * associated datagrams. */voidip_freef(fp)	struct ipq *fp;{	register struct ipasfrag *q, *p;	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {		p = q->ipf_next;		ip_deq(q);		m_freem(dtom(q));	}	remque(fp);	(void) m_free(dtom(fp));}/* * Put an ip fragment on a reassembly chain. * Like insque, but pointers in middle of structure. */voidip_enq(p, prev)	register struct ipasfrag *p, *prev;{	p->ipf_prev = prev;	p->ipf_next = prev->ipf_next;	prev->ipf_next->ipf_prev = p;	prev->ipf_next = p;}/* * To ip_enq as remque is to insque. */voidip_deq(p)	register struct ipasfrag *p;{	p->ipf_prev->ipf_next = p->ipf_next;	p->ipf_next->ipf_prev = p->ipf_prev;}/* * IP timer processing; * if a timer expires on a reassembly * queue, discard it. */voidip_slowtimo(){	register struct ipq *fp;	int s = splnet();	fp = ipq.next;	if (fp == 0) {		splx(s);		return;	}	while (fp != &ipq) {		--fp->ipq_ttl;		fp = fp->next;		if (fp->prev->ipq_ttl == 0) {			ipstat.ips_fragtimeout++;			ip_freef(fp->prev);		}	}	splx(s);}/* * Drain off all datagram fragments. */voidip_drain(){	while (ipq.next != &ipq) {		ipstat.ips_fragdropped++;		ip_freef(ipq.next);	}}/* * Do option processing on a datagram, * possibly discarding it if bad options are encountered, * or forwarding it if source-routed. * Returns 1 if packet has been forwarded/freed, * 0 if the packet should be processed further. */intip_dooptions(m)	struct mbuf *m;{	register struct ip *ip = mtod(m, struct ip *);	register u_char *cp;	register struct ip_timestamp *ipt;	register struct in_ifaddr *ia;	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;	struct in_addr *sin, dst;	n_time ntime;	dst = ip->ip_dst;	cp = (u_char *)(ip + 1);	cnt = (ip->ip_hl << 2) - sizeof (struct ip);	for (; cnt > 0; cnt -= optlen, cp += optlen) {		opt = cp[IPOPT_OPTVAL];		if (opt == IPOPT_EOL)			break;		if (opt == IPOPT_NOP)			optlen = 1;		else {			optlen = cp[IPOPT_OLEN];			if (optlen <= 0 || optlen > cnt) {				code = &cp[IPOPT_OLEN] - (u_char *)ip;				goto bad;			}		}		switch (opt) {		default:			break;		/*		 * Source routing with record.		 * Find interface with current destination address.		 * If none on this machine then drop if strictly routed,		 * or do nothing if loosely routed.		 * Record interface address and bring up next address		 * component.  If strictly routed make sure next		 * address is on directly accessible net.		 */		case IPOPT_LSRR:		case IPOPT_SSRR:			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {				code = &cp[IPOPT_OFFSET] - (u_char *)ip;				goto bad;			}			ipaddr.sin_addr = ip->ip_dst;			ia = (struct in_ifaddr *)				ifa_ifwithaddr((struct sockaddr *)&ipaddr);			if (ia == 0) {				if (opt == IPOPT_SSRR) {					type = ICMP_UNREACH;					code = ICMP_UNREACH_SRCFAIL;					goto bad;				}				/*				 * Loose routing, and not at next destination				 * yet; nothing to do except forward.				 */				break;			}			off--;			/* 0 origin */			if (off > optlen - sizeof(struct in_addr)) {				/*				 * End of source route.  Should be for us.				 */				save_rte(cp, ip->ip_src);				break;			}			if (!ip_dosourceroute) {				char buf[4*sizeof "123"];				strcpy(buf, inet_ntoa(ip->ip_dst));				log(LOG_WARNING, 				    "attempted source route from %s to %s\n",				    inet_ntoa(ip->ip_src), buf);				type = ICMP_UNREACH;				code = ICMP_UNREACH_SRCFAIL;				goto bad;			}			/*			 * locate outgoing interface			 */			(void)memcpy(&ipaddr.sin_addr, cp + off,			    sizeof(ipaddr.sin_addr));			if (opt == IPOPT_SSRR) {#define	INA	struct in_ifaddr *#define	SA	struct sockaddr *			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)				ia = (INA)ifa_ifwithnet((SA)&ipaddr);			} else				ia = ip_rtaddr(ipaddr.sin_addr);			if (ia == 0) {				type = ICMP_UNREACH;				code = ICMP_UNREACH_SRCFAIL;				goto bad;			}			ip->ip_dst = ipaddr.sin_addr;			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),			    sizeof(struct in_addr));			cp[IPOPT_OFFSET] += sizeof(struct in_addr);			/*			 * Let ip_intr's mcast routing check handle mcast pkts			 */			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));			break;		case IPOPT_RR:			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {				code = &cp[IPOPT_OFFSET] - (u_char *)ip;				goto bad;			}			/*			 * If no space remains, ignore.			 */			off--;			/* 0 origin */			if (off > optlen - sizeof(struct in_addr))				break;			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,			    sizeof(ipaddr.sin_addr));			/*			 * locate outgoing interface; if we're the destination,			 * use the incoming interface (should be same).			 */			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {				type = ICMP_UNREACH;				code = ICMP_UNREACH_HOST;				goto bad;			}			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),			    sizeof(struct in_addr));			cp[IPOPT_OFFSET] += sizeof(struct in_addr);			break;		case IPOPT_TS:			code = cp - (u_char *)ip;			ipt = (struct ip_timestamp *)cp;			if (ipt->ipt_len < 5)				goto bad;			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {				if (++ipt->ipt_oflw == 0)					goto bad;				break;			}			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);			switch (ipt->ipt_flg) {			case IPOPT_TS_TSONLY:				break;			case IPOPT_TS_TSANDADDR:				if (ipt->ipt_ptr + sizeof(n_time) +				    sizeof(struct in_addr) > ipt->ipt_len)					goto bad;				ipaddr.sin_addr = dst;				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,							    m->m_pkthdr.rcvif);				if (ia == 0)					continue;				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,				    sizeof(struct in_addr));				ipt->ipt_ptr += sizeof(struct in_addr);				break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?