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

📄 ip_queue.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
			}			if (e->skb->sk)				skb_set_owner_w(newskb, e->skb->sk);			kfree_skb(e->skb);			e->skb = newskb;		}		skb_put(e->skb, diff);	}	if (!skb_make_writable(&e->skb, v->data_len))		return -ENOMEM;	memcpy(e->skb->data, v->payload, v->data_len);	e->skb->ip_summed = CHECKSUM_NONE;	return 0;}static inline intid_cmp(struct ipq_queue_entry *e, unsigned long id){	return (id == (unsigned long )e);}static intipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len){	struct ipq_queue_entry *entry;	if (vmsg->value > NF_MAX_VERDICT)		return -EINVAL;	entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);	if (entry == NULL)		return -ENOENT;	else {		int verdict = vmsg->value;				if (vmsg->data_len && vmsg->data_len == len)			if (ipq_mangle_ipv4(vmsg, entry) < 0)				verdict = NF_DROP;				ipq_issue_verdict(entry, verdict);		return 0;	}}static intipq_set_mode(unsigned char mode, unsigned int range){	int status;	write_lock_bh(&queue_lock);	status = __ipq_set_mode(mode, range);	write_unlock_bh(&queue_lock);	return status;}static intipq_receive_peer(struct ipq_peer_msg *pmsg,                 unsigned char type, unsigned int len){	int status = 0;	if (len < sizeof(*pmsg))		return -EINVAL;	switch (type) {	case IPQM_MODE:		status = ipq_set_mode(pmsg->msg.mode.value,		                      pmsg->msg.mode.range);		break;			case IPQM_VERDICT:		if (pmsg->msg.verdict.value > NF_MAX_VERDICT)			status = -EINVAL;		else			status = ipq_set_verdict(&pmsg->msg.verdict,			                         len - sizeof(*pmsg));			break;	default:		status = -EINVAL;	}	return status;}static intdev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex){	if (entry->info->indev)		if (entry->info->indev->ifindex == ifindex)			return 1;				if (entry->info->outdev)		if (entry->info->outdev->ifindex == ifindex)			return 1;	return 0;}static voidipq_dev_drop(int ifindex){	struct ipq_queue_entry *entry;		while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)		ipq_issue_verdict(entry, NF_DROP);}#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)static inline voidipq_rcv_skb(struct sk_buff *skb){	int status, type, pid, flags, nlmsglen, skblen;	struct nlmsghdr *nlh;	skblen = skb->len;	if (skblen < sizeof(*nlh))		return;	nlh = (struct nlmsghdr *)skb->data;	nlmsglen = nlh->nlmsg_len;	if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)		return;	pid = nlh->nlmsg_pid;	flags = nlh->nlmsg_flags;		if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)		RCV_SKB_FAIL(-EINVAL);			if (flags & MSG_TRUNC)		RCV_SKB_FAIL(-ECOMM);			type = nlh->nlmsg_type;	if (type < NLMSG_NOOP || type >= IPQM_MAX)		RCV_SKB_FAIL(-EINVAL);			if (type <= IPQM_BASE)		return;			if (security_netlink_recv(skb))		RCV_SKB_FAIL(-EPERM);		write_lock_bh(&queue_lock);		if (peer_pid) {		if (peer_pid != pid) {			write_unlock_bh(&queue_lock);			RCV_SKB_FAIL(-EBUSY);		}	} else {		net_enable_timestamp();		peer_pid = pid;	}			write_unlock_bh(&queue_lock);		status = ipq_receive_peer(NLMSG_DATA(nlh), type,	                          skblen - NLMSG_LENGTH(0));	if (status < 0)		RCV_SKB_FAIL(status);			if (flags & NLM_F_ACK)		netlink_ack(skb, nlh, 0);        return;}static voidipq_rcv_sk(struct sock *sk, int len){	struct sk_buff *skb;	unsigned int qlen;	down(&ipqnl_sem);				for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {		skb = skb_dequeue(&sk->sk_receive_queue);		ipq_rcv_skb(skb);		kfree_skb(skb);	}			up(&ipqnl_sem);}static intipq_rcv_dev_event(struct notifier_block *this,                  unsigned long event, void *ptr){	struct net_device *dev = ptr;	/* Drop any packets associated with the downed device */	if (event == NETDEV_DOWN)		ipq_dev_drop(dev->ifindex);	return NOTIFY_DONE;}static struct notifier_block ipq_dev_notifier = {	.notifier_call	= ipq_rcv_dev_event,};static intipq_rcv_nl_event(struct notifier_block *this,                 unsigned long event, void *ptr){	struct netlink_notify *n = ptr;	if (event == NETLINK_URELEASE &&	    n->protocol == NETLINK_FIREWALL && n->pid) {		write_lock_bh(&queue_lock);		if (n->pid == peer_pid)			__ipq_reset();		write_unlock_bh(&queue_lock);	}	return NOTIFY_DONE;}static struct notifier_block ipq_nl_notifier = {	.notifier_call	= ipq_rcv_nl_event,};static struct ctl_table_header *ipq_sysctl_header;static ctl_table ipq_table[] = {	{		.ctl_name	= NET_IPQ_QMAX,		.procname	= NET_IPQ_QMAX_NAME,		.data		= &queue_maxlen,		.maxlen		= sizeof(queue_maxlen),		.mode		= 0644,		.proc_handler	= proc_dointvec	}, 	{ .ctl_name = 0 }};static ctl_table ipq_dir_table[] = {	{		.ctl_name	= NET_IPV4,		.procname	= "ipv4",		.mode		= 0555,		.child		= ipq_table	},	{ .ctl_name = 0 }};static ctl_table ipq_root_table[] = {	{		.ctl_name	= CTL_NET,		.procname	= "net",		.mode		= 0555,		.child		= ipq_dir_table	},	{ .ctl_name = 0 }};#ifdef CONFIG_PROC_FSstatic intipq_get_info(char *buffer, char **start, off_t offset, int length){	int len;	read_lock_bh(&queue_lock);		len = sprintf(buffer,	              "Peer PID          : %d\n"	              "Copy mode         : %hu\n"	              "Copy range        : %u\n"	              "Queue length      : %u\n"	              "Queue max. length : %u\n"		      "Queue dropped     : %u\n"		      "Netlink dropped   : %u\n",	              peer_pid,	              copy_mode,	              copy_range,	              queue_total,	              queue_maxlen,		      queue_dropped,		      queue_user_dropped);	read_unlock_bh(&queue_lock);		*start = buffer + offset;	len -= offset;	if (len > length)		len = length;	else if (len < 0)		len = 0;	return len;}#endif /* CONFIG_PROC_FS */static struct nf_queue_handler nfqh = {	.name	= "ip_queue",	.outfn	= &ipq_enqueue_packet,};static intinit_or_cleanup(int init){	int status = -ENOMEM;	struct proc_dir_entry *proc;		if (!init)		goto cleanup;	netlink_register_notifier(&ipq_nl_notifier);	ipqnl = netlink_kernel_create(NETLINK_FIREWALL, 0, ipq_rcv_sk,				      THIS_MODULE);	if (ipqnl == NULL) {		printk(KERN_ERR "ip_queue: failed to create netlink socket\n");		goto cleanup_netlink_notifier;	}	proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);	if (proc)		proc->owner = THIS_MODULE;	else {		printk(KERN_ERR "ip_queue: failed to create proc entry\n");		goto cleanup_ipqnl;	}		register_netdevice_notifier(&ipq_dev_notifier);	ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);		status = nf_register_queue_handler(PF_INET, &nfqh);	if (status < 0) {		printk(KERN_ERR "ip_queue: failed to register queue handler\n");		goto cleanup_sysctl;	}	return status;cleanup:	nf_unregister_queue_handlers(&nfqh);	synchronize_net();	ipq_flush(NF_DROP);	cleanup_sysctl:	unregister_sysctl_table(ipq_sysctl_header);	unregister_netdevice_notifier(&ipq_dev_notifier);	proc_net_remove(IPQ_PROC_FS_NAME);	cleanup_ipqnl:	sock_release(ipqnl->sk_socket);	down(&ipqnl_sem);	up(&ipqnl_sem);	cleanup_netlink_notifier:	netlink_unregister_notifier(&ipq_nl_notifier);	return status;}static int __init init(void){		return init_or_cleanup(1);}static void __exit fini(void){	init_or_cleanup(0);}MODULE_DESCRIPTION("IPv4 packet queue handler");MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");MODULE_LICENSE("GPL");module_init(init);module_exit(fini);

⌨️ 快捷键说明

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