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

📄 ipt_clusterip.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Cluster IP hashmark target * (C) 2003-2004 by Harald Welte <laforge@netfilter.org> * based on ideas of Fabio Olive Leite <olive@unixforge.org> * * Development of this code funded by SuSE Linux AG, http://www.suse.com/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */#include <linux/module.h>#include <linux/proc_fs.h>#include <linux/jhash.h>#include <linux/bitops.h>#include <linux/skbuff.h>#include <linux/ip.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/icmp.h>#include <linux/if_arp.h>#include <linux/seq_file.h>#include <linux/netfilter_arp.h>#include <linux/netfilter/x_tables.h>#include <linux/netfilter_ipv4/ip_tables.h>#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>#include <net/netfilter/nf_conntrack.h>#include <net/net_namespace.h>#include <net/checksum.h>#define CLUSTERIP_VERSION "0.8"MODULE_LICENSE("GPL");MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");MODULE_DESCRIPTION("iptables target for CLUSTERIP");struct clusterip_config {	struct list_head list;			/* list of all configs */	atomic_t refcount;			/* reference count */	atomic_t entries;			/* number of entries/rules						 * referencing us */	__be32 clusterip;			/* the IP address */	u_int8_t clustermac[ETH_ALEN];		/* the MAC address */	struct net_device *dev;			/* device */	u_int16_t num_total_nodes;		/* total number of nodes */	unsigned long local_nodes;		/* node number array */#ifdef CONFIG_PROC_FS	struct proc_dir_entry *pde;		/* proc dir entry */#endif	enum clusterip_hashmode hash_mode;	/* which hashing mode */	u_int32_t hash_initval;			/* hash initialization */};static LIST_HEAD(clusterip_configs);/* clusterip_lock protects the clusterip_configs list */static DEFINE_RWLOCK(clusterip_lock);#ifdef CONFIG_PROC_FSstatic const struct file_operations clusterip_proc_fops;static struct proc_dir_entry *clusterip_procdir;#endifstatic inline voidclusterip_config_get(struct clusterip_config *c){	atomic_inc(&c->refcount);}static inline voidclusterip_config_put(struct clusterip_config *c){	if (atomic_dec_and_test(&c->refcount))		kfree(c);}/* increase the count of entries(rules) using/referencing this config */static inline voidclusterip_config_entry_get(struct clusterip_config *c){	atomic_inc(&c->entries);}/* decrease the count of entries using/referencing this config.  If last * entry(rule) is removed, remove the config from lists, but don't free it * yet, since proc-files could still be holding references */static inline voidclusterip_config_entry_put(struct clusterip_config *c){	if (atomic_dec_and_test(&c->entries)) {		write_lock_bh(&clusterip_lock);		list_del(&c->list);		write_unlock_bh(&clusterip_lock);		dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);		dev_put(c->dev);		/* In case anyone still accesses the file, the open/close		 * functions are also incrementing the refcount on their own,		 * so it's safe to remove the entry even if it's in use. */#ifdef CONFIG_PROC_FS		remove_proc_entry(c->pde->name, c->pde->parent);#endif	}}static struct clusterip_config *__clusterip_config_find(__be32 clusterip){	struct list_head *pos;	list_for_each(pos, &clusterip_configs) {		struct clusterip_config *c = list_entry(pos,					struct clusterip_config, list);		if (c->clusterip == clusterip)			return c;	}	return NULL;}static inline struct clusterip_config *clusterip_config_find_get(__be32 clusterip, int entry){	struct clusterip_config *c;	read_lock_bh(&clusterip_lock);	c = __clusterip_config_find(clusterip);	if (!c) {		read_unlock_bh(&clusterip_lock);		return NULL;	}	atomic_inc(&c->refcount);	if (entry)		atomic_inc(&c->entries);	read_unlock_bh(&clusterip_lock);	return c;}static voidclusterip_config_init_nodelist(struct clusterip_config *c,			       const struct ipt_clusterip_tgt_info *i){	int n;	for (n = 0; n < i->num_local_nodes; n++)		set_bit(i->local_nodes[n] - 1, &c->local_nodes);}static struct clusterip_config *clusterip_config_init(struct ipt_clusterip_tgt_info *i, __be32 ip,			struct net_device *dev){	struct clusterip_config *c;	c = kzalloc(sizeof(*c), GFP_ATOMIC);	if (!c)		return NULL;	c->dev = dev;	c->clusterip = ip;	memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);	c->num_total_nodes = i->num_total_nodes;	clusterip_config_init_nodelist(c, i);	c->hash_mode = i->hash_mode;	c->hash_initval = i->hash_initval;	atomic_set(&c->refcount, 1);	atomic_set(&c->entries, 1);#ifdef CONFIG_PROC_FS	{		char buffer[16];		/* create proc dir entry */		sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));		c->pde = create_proc_entry(buffer, S_IWUSR|S_IRUSR,					   clusterip_procdir);		if (!c->pde) {			kfree(c);			return NULL;		}	}	c->pde->proc_fops = &clusterip_proc_fops;	c->pde->data = c;#endif	write_lock_bh(&clusterip_lock);	list_add(&c->list, &clusterip_configs);	write_unlock_bh(&clusterip_lock);	return c;}#ifdef CONFIG_PROC_FSstatic intclusterip_add_node(struct clusterip_config *c, u_int16_t nodenum){	if (nodenum == 0 ||	    nodenum > c->num_total_nodes)		return 1;	/* check if we already have this number in our bitfield */	if (test_and_set_bit(nodenum - 1, &c->local_nodes))		return 1;	return 0;}static boolclusterip_del_node(struct clusterip_config *c, u_int16_t nodenum){	if (nodenum == 0 ||	    nodenum > c->num_total_nodes)		return true;	if (test_and_clear_bit(nodenum - 1, &c->local_nodes))		return false;	return true;}#endifstatic inline u_int32_tclusterip_hashfn(const struct sk_buff *skb,		 const struct clusterip_config *config){	const struct iphdr *iph = ip_hdr(skb);	unsigned long hashval;	u_int16_t sport, dport;	const u_int16_t *ports;	switch (iph->protocol) {	case IPPROTO_TCP:	case IPPROTO_UDP:	case IPPROTO_UDPLITE:	case IPPROTO_SCTP:	case IPPROTO_DCCP:	case IPPROTO_ICMP:		ports = (const void *)iph+iph->ihl*4;		sport = ports[0];		dport = ports[1];		break;	default:		if (net_ratelimit())			printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",				iph->protocol);		sport = dport = 0;	}	switch (config->hash_mode) {	case CLUSTERIP_HASHMODE_SIP:		hashval = jhash_1word(ntohl(iph->saddr),				      config->hash_initval);		break;	case CLUSTERIP_HASHMODE_SIP_SPT:		hashval = jhash_2words(ntohl(iph->saddr), sport,				       config->hash_initval);		break;	case CLUSTERIP_HASHMODE_SIP_SPT_DPT:		hashval = jhash_3words(ntohl(iph->saddr), sport, dport,				       config->hash_initval);		break;	default:		/* to make gcc happy */		hashval = 0;		/* This cannot happen, unless the check function wasn't called		 * at rule load time */		printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);		BUG();		break;	}	/* node numbers are 1..n, not 0..n */	return (hashval % config->num_total_nodes) + 1;}static inline intclusterip_responsible(const struct clusterip_config *config, u_int32_t hash){	return test_bit(hash - 1, &config->local_nodes);}/*********************************************************************** * IPTABLES TARGET ***********************************************************************/static unsigned inttarget(struct sk_buff *skb,       const struct net_device *in,       const struct net_device *out,       unsigned int hooknum,       const struct xt_target *target,       const void *targinfo){	const struct ipt_clusterip_tgt_info *cipinfo = targinfo;	struct nf_conn *ct;	enum ip_conntrack_info ctinfo;	u_int32_t hash;	/* don't need to clusterip_config_get() here, since refcount	 * is only decremented by destroy() - and ip_tables guarantees	 * that the ->target() function isn't called after ->destroy() */	ct = nf_ct_get(skb, &ctinfo);	if (ct == NULL) {		printk(KERN_ERR "CLUSTERIP: no conntrack!\n");			/* FIXME: need to drop invalid ones, since replies			 * to outgoing connections of other nodes will be			 * marked as INVALID */		return NF_DROP;	}	/* special case: ICMP error handling. conntrack distinguishes between	 * error messages (RELATED) and information requests (see below) */	if (ip_hdr(skb)->protocol == IPPROTO_ICMP	    && (ctinfo == IP_CT_RELATED		|| ctinfo == IP_CT_RELATED+IP_CT_IS_REPLY))		return XT_CONTINUE;	/* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,	 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here	 * on, which all have an ID field [relevant for hashing]. */	hash = clusterip_hashfn(skb, cipinfo->config);	switch (ctinfo) {		case IP_CT_NEW:			ct->mark = hash;			break;		case IP_CT_RELATED:		case IP_CT_RELATED+IP_CT_IS_REPLY:			/* FIXME: we don't handle expectations at the			 * moment.  they can arrive on a different node than			 * the master connection (e.g. FTP passive mode) */		case IP_CT_ESTABLISHED:		case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:			break;		default:			break;	}#ifdef DEBUG	DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);#endif	pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);	if (!clusterip_responsible(cipinfo->config, hash)) {		pr_debug("not responsible\n");		return NF_DROP;	}	pr_debug("responsible\n");	/* despite being received via linklayer multicast, this is	 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */	skb->pkt_type = PACKET_HOST;	return XT_CONTINUE;}static boolcheckentry(const char *tablename,	   const void *e_void,	   const struct xt_target *target,	   void *targinfo,	   unsigned int hook_mask){	struct ipt_clusterip_tgt_info *cipinfo = targinfo;	const struct ipt_entry *e = e_void;	struct clusterip_config *config;	if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&	    cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&	    cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {		printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",			cipinfo->hash_mode);		return false;	}

⌨️ 快捷键说明

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