⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myip_input.c

📁 一个基于linux的TCP/IP协议栈的实现
💻 C
字号:
DEFINE_SNMP_STAT(struct ipstats_mib, myip_statistics) __read_mostly;#define MYIP_INC_STATS_BH(field)		SNMP_INC_STATS_BH(myip_statistics, field) static inline int myip_rcv_finish(struct sk_buff *skb){	struct iphdr *iph = skb->nh.iph;		if( likely(skb->dst == NULL) ){		int err = myip_route_input(skb, iph->daddr, iph->saddr, iph->tos,					 skb->dev);		if( unlikely(err) ){			if( err == -EHOSTUNREACH )				MYIP_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);			goto drop; 		}	}#ifdef CONFIG_NET_CLS_ROUTE	if( unlikely(skb->dst->tclassid) ){		struct ip_rt_acct *st = myip_rt_acct + 256 * smp_processor_id();		u32 idx = skb->dst->tclassid;		st[idx&0xFF].o_packets++;		st[idx&0xFF].o_bytes+=skb->len;		st[(idx>>16)&0xFF].i_packets++;		st[(idx>>16)&0xFF].i_bytes+=skb->len;	}#endif	if (iph->ihl > 5 /*&& ip_rcv_options(skb)*/ )		goto drop;	return dst_input(skb);drop:	kfree_skb(skb);	return NET_RX_DROP;}int myip_rcv(struct sk_buff *skb, struct net_device *dev, 				struct packet_type *pt, struct net_device *dev2 ){	struct iphdr *iph;	u32 len;	if (skb->pkt_type == PACKET_OTHERHOST)		goto drop;	MYIP_INC_STATS_BH(IPSTATS_MIB_INRECEIVES);	if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {		MYIP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);		goto out;	}	if (!pskb_may_pull(skb, sizeof(struct iphdr)))		goto inhdr_error;	iph = skb->nh.iph;	if (iph->ihl < 5 || iph->version != 4)		goto inhdr_error;	if (!pskb_may_pull(skb, iph->ihl*4))		goto inhdr_error;	iph = skb->nh.iph;		if( unlikely(ip_fast_csum((u8 *)iph, iph->ihl)) )		goto inhdr_error;	len = ntohs(iph->tot_len);	if (skb->len < len || len < (iph->ihl*4))		goto inhdr_error;	if( pskb_trim_rcsum(skb, len) ){		MYIP_INC_STATS_BH(IPSTATS_MIB_INDISCARDS);		goto drop;	}	return NF_HOOK(MY_PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,					myip_rcv_finish);inhdr_error:	MYIP_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);drop:	kfree_skb(skb);out:	return NET_RX_DROP;}static inline int myip_local_deliver_finish( struct sk_buff *skb ){	int ihl = skb->nh.iph->ihl*4;	__skb_pull(skb, ihl);	skb->h.raw = skb->data;	rcu_read_lock();	{		int protocol = skb->nh.iph->protocol + (MY_IPPROTO_UDP - IPPROTO_UDP);		int hash;		struct sock *raw_sk;		struct net_protocol *ipprot;resubmit:		hash = protocol & (MAX_INET_PROTOS - 1);		raw_sk = sk_head(&myraw_v4_htable[hash]);		if( raw_sk && !myraw_v4_input(skb, skb->nh.iph, hash) )			raw_sk = NULL;		if ((ipprot = rcu_dereference( myinet_protos[hash]) ) != NULL) {			int ret;			//if (!ipprot->no_policy) {			//	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {			//		kfree_skb(skb);			//		goto out;			//	}			//	nf_reset(skb);			//}			ret = ipprot->handler(skb);			if (ret < 0) {				protocol = -ret;				goto resubmit;			}			MYIP_INC_STATS_BH(IPSTATS_MIB_INDELIVERS);		}else{		}	}out:	rcu_read_unlock();	return 0;}int myip_local_deliver(struct sk_buff *skb){	//if( skb->nh.iph->frag_off & htons( IP_MF|IP_OFFSET) ){	//	skb = ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER);	//	if (!skb)	//		return 0;	//}	return NF_HOOK(MY_PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,					myip_local_deliver_finish);}

⌨️ 快捷键说明

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