📄 ip_queue.c
字号:
/* * This is a module which is used for queueing IPv4 packets and * communicating with userspace via netlink. * * (C) 2000 James Morris, this code is GPL. * * 2000-03-27: Simplified code (thanks to Andi Kleen for clues). * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report). * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian * Zander). * 2000-08-01: Added Nick Williams' MAC support. * */#include <linux/module.h>#include <linux/skbuff.h>#include <linux/init.h>#include <linux/ip.h>#include <linux/notifier.h>#include <linux/netdevice.h>#include <linux/netfilter.h>#include <linux/netlink.h>#include <linux/spinlock.h>#include <linux/rtnetlink.h>#include <linux/sysctl.h>#include <linux/proc_fs.h>#include <net/sock.h>#include <linux/netfilter_ipv4/ip_queue.h>#define IPQ_QMAX_DEFAULT 1024#define IPQ_PROC_FS_NAME "ip_queue"#define NET_IPQ_QMAX 2088#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"typedef struct ipq_queue_element { struct list_head list; /* Links element into queue */ int verdict; /* Current verdict */ struct nf_info *info; /* Extra info from netfilter */ struct sk_buff *skb; /* Packet inside */} ipq_queue_element_t;typedef int (*ipq_send_cb_t)(ipq_queue_element_t *e);typedef struct ipq_peer { pid_t pid; /* PID of userland peer */ unsigned char died; /* We think the peer died */ unsigned char copy_mode; /* Copy packet as well as metadata? */ size_t copy_range; /* Range past metadata to copy */ ipq_send_cb_t send; /* Callback for sending data to peer */} ipq_peer_t;typedef struct ipq_queue { int len; /* Current queue len */ int *maxlen; /* Maximum queue len, via sysctl */ unsigned char flushing; /* If queue is being flushed */ unsigned char terminate; /* If the queue is being terminated */ struct list_head list; /* Head of packet queue */ spinlock_t lock; /* Queue spinlock */ ipq_peer_t peer; /* Userland peer */} ipq_queue_t;/**************************************************************************** * * Packet queue * ****************************************************************************//* Dequeue a packet if matched by cmp, or the next available if cmp is NULL */static ipq_queue_element_t *ipq_dequeue(ipq_queue_t *q, int (*cmp)(ipq_queue_element_t *, unsigned long), unsigned long data){ struct list_head *i; spin_lock_bh(&q->lock); for (i = q->list.prev; i != &q->list; i = i->prev) { ipq_queue_element_t *e = (ipq_queue_element_t *)i; if (!cmp || cmp(e, data)) { list_del(&e->list); q->len--; spin_unlock_bh(&q->lock); return e; } } spin_unlock_bh(&q->lock); return NULL;}/* Flush all packets */static void ipq_flush(ipq_queue_t *q){ ipq_queue_element_t *e; spin_lock_bh(&q->lock); q->flushing = 1; spin_unlock_bh(&q->lock); while ((e = ipq_dequeue(q, NULL, 0))) { e->verdict = NF_DROP; nf_reinject(e->skb, e->info, e->verdict); kfree(e); } spin_lock_bh(&q->lock); q->flushing = 0; spin_unlock_bh(&q->lock);}static ipq_queue_t *ipq_create_queue(nf_queue_outfn_t outfn, ipq_send_cb_t send_cb, int *errp, int *sysctl_qmax){ int status; ipq_queue_t *q; *errp = 0; q = kmalloc(sizeof(ipq_queue_t), GFP_KERNEL); if (q == NULL) { *errp = -ENOMEM; return NULL; } q->peer.pid = 0; q->peer.died = 0; q->peer.copy_mode = IPQ_COPY_NONE; q->peer.copy_range = 0; q->peer.send = send_cb; q->len = 0; q->maxlen = sysctl_qmax; q->flushing = 0; q->terminate = 0; INIT_LIST_HEAD(&q->list); spin_lock_init(&q->lock); status = nf_register_queue_handler(PF_INET, outfn, q); if (status < 0) { *errp = -EBUSY; kfree(q); return NULL; } return q;}static int ipq_enqueue(ipq_queue_t *q, struct sk_buff *skb, struct nf_info *info){ ipq_queue_element_t *e; int status; e = kmalloc(sizeof(*e), GFP_ATOMIC); if (e == NULL) { printk(KERN_ERR "ip_queue: OOM in enqueue\n"); return -ENOMEM; } e->verdict = NF_DROP; e->info = info; e->skb = skb; spin_lock_bh(&q->lock); if (q->len >= *q->maxlen) { spin_unlock_bh(&q->lock); if (net_ratelimit()) printk(KERN_WARNING "ip_queue: full at %d entries, " "dropping packet(s).\n", q->len); goto free_drop; } if (q->flushing || q->peer.copy_mode == IPQ_COPY_NONE || q->peer.pid == 0 || q->peer.died || q->terminate) { spin_unlock_bh(&q->lock); goto free_drop; } status = q->peer.send(e); if (status > 0) { list_add(&e->list, &q->list); q->len++; spin_unlock_bh(&q->lock); return status; } spin_unlock_bh(&q->lock); if (status == -ECONNREFUSED) { printk(KERN_INFO "ip_queue: peer %d died, " "resetting state and flushing queue\n", q->peer.pid); q->peer.died = 1; q->peer.pid = 0; q->peer.copy_mode = IPQ_COPY_NONE; q->peer.copy_range = 0; ipq_flush(q); }free_drop: kfree(e); return -EBUSY;}static void ipq_destroy_queue(ipq_queue_t *q){ nf_unregister_queue_handler(PF_INET); spin_lock_bh(&q->lock); q->terminate = 1; spin_unlock_bh(&q->lock); ipq_flush(q); kfree(q);}static int ipq_mangle_ipv4(ipq_verdict_msg_t *v, ipq_queue_element_t *e){ int diff; struct iphdr *user_iph = (struct iphdr *)v->payload; if (v->data_len < sizeof(*user_iph)) return 0; diff = v->data_len - e->skb->len; if (diff < 0) skb_trim(e->skb, v->data_len); else if (diff > 0) { if (v->data_len > 0xFFFF) return -EINVAL; if (diff > skb_tailroom(e->skb)) { struct sk_buff *newskb; newskb = skb_copy_expand(e->skb, skb_headroom(e->skb), diff, GFP_ATOMIC); if (newskb == NULL) { printk(KERN_WARNING "ip_queue: OOM " "in mangle, dropping packet\n"); return -ENOMEM; } kfree_skb(e->skb); e->skb = newskb; } skb_put(e->skb, diff); } memcpy(e->skb->data, v->payload, v->data_len); e->skb->nfcache |= NFC_ALTERED; return 0;}static inline int id_cmp(ipq_queue_element_t *e, unsigned long id){ return (id == (unsigned long )e);}static int ipq_set_verdict(ipq_queue_t *q, ipq_verdict_msg_t *v, unsigned int len){ ipq_queue_element_t *e; if (v->value > NF_MAX_VERDICT) return -EINVAL; e = ipq_dequeue(q, id_cmp, v->id); if (e == NULL) return -ENOENT; else { e->verdict = v->value; if (v->data_len && v->data_len == len) if (ipq_mangle_ipv4(v, e) < 0) e->verdict = NF_DROP; nf_reinject(e->skb, e->info, e->verdict); kfree(e); return 0; }}static int ipq_receive_peer(ipq_queue_t *q, ipq_peer_msg_t *m, unsigned char type, unsigned int len){ int status = 0; int busy; spin_lock_bh(&q->lock); busy = (q->terminate || q->flushing); spin_unlock_bh(&q->lock); if (busy) return -EBUSY; if (len < sizeof(ipq_peer_msg_t)) return -EINVAL; switch (type) { case IPQM_MODE: switch (m->msg.mode.value) { case IPQ_COPY_META: q->peer.copy_mode = IPQ_COPY_META; q->peer.copy_range = 0; break; case IPQ_COPY_PACKET: q->peer.copy_mode = IPQ_COPY_PACKET; q->peer.copy_range = m->msg.mode.range; if (q->peer.copy_range > 0xFFFF) q->peer.copy_range = 0xFFFF; break; default: status = -EINVAL; } break; case IPQM_VERDICT: if (m->msg.verdict.value > NF_MAX_VERDICT) status = -EINVAL; else status = ipq_set_verdict(q, &m->msg.verdict, len - sizeof(*m)); break; default: status = -EINVAL; } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -