diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /net/ipv4/netfilter | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'net/ipv4/netfilter')
21 files changed, 4848 insertions, 0 deletions
diff --git a/net/ipv4/netfilter/ip_queue.c b/net/ipv4/netfilter/ip_queue.c new file mode 100644 index 00000000000..e59aabd0eae --- /dev/null +++ b/net/ipv4/netfilter/ip_queue.c | |||
| @@ -0,0 +1,637 @@ | |||
| 1 | /* | ||
| 2 | * This is a module which is used for queueing IPv4 packets and | ||
| 3 | * communicating with userspace via netlink. | ||
| 4 | * | ||
| 5 | * (C) 2000-2002 James Morris <jmorris@intercode.com.au> | ||
| 6 | * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License version 2 as | ||
| 10 | * published by the Free Software Foundation. | ||
| 11 | */ | ||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/skbuff.h> | ||
| 14 | #include <linux/init.h> | ||
| 15 | #include <linux/ip.h> | ||
| 16 | #include <linux/notifier.h> | ||
| 17 | #include <linux/netdevice.h> | ||
| 18 | #include <linux/netfilter.h> | ||
| 19 | #include <linux/netfilter_ipv4/ip_queue.h> | ||
| 20 | #include <linux/netfilter_ipv4/ip_tables.h> | ||
| 21 | #include <linux/netlink.h> | ||
| 22 | #include <linux/spinlock.h> | ||
| 23 | #include <linux/sysctl.h> | ||
| 24 | #include <linux/proc_fs.h> | ||
| 25 | #include <linux/seq_file.h> | ||
| 26 | #include <linux/security.h> | ||
| 27 | #include <linux/net.h> | ||
| 28 | #include <linux/mutex.h> | ||
| 29 | #include <linux/slab.h> | ||
| 30 | #include <net/net_namespace.h> | ||
| 31 | #include <net/sock.h> | ||
| 32 | #include <net/route.h> | ||
| 33 | #include <net/netfilter/nf_queue.h> | ||
| 34 | #include <net/ip.h> | ||
| 35 | |||
| 36 | #define IPQ_QMAX_DEFAULT 1024 | ||
| 37 | #define IPQ_PROC_FS_NAME "ip_queue" | ||
| 38 | #define NET_IPQ_QMAX 2088 | ||
| 39 | #define NET_IPQ_QMAX_NAME "ip_queue_maxlen" | ||
| 40 | |||
| 41 | typedef int (*ipq_cmpfn)(struct nf_queue_entry *, unsigned long); | ||
| 42 | |||
| 43 | static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE; | ||
| 44 | static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT; | ||
| 45 | static DEFINE_SPINLOCK(queue_lock); | ||
| 46 | static int peer_pid __read_mostly; | ||
| 47 | static unsigned int copy_range __read_mostly; | ||
| 48 | static unsigned int queue_total; | ||
| 49 | static unsigned int queue_dropped = 0; | ||
| 50 | static unsigned int queue_user_dropped = 0; | ||
| 51 | static struct sock *ipqnl __read_mostly; | ||
| 52 | static LIST_HEAD(queue_list); | ||
| 53 | static DEFINE_MUTEX(ipqnl_mutex); | ||
| 54 | |||
| 55 | static inline void | ||
| 56 | __ipq_enqueue_entry(struct nf_queue_entry *entry) | ||
| 57 | { | ||
| 58 | list_add_tail(&entry->list, &queue_list); | ||
| 59 | queue_total++; | ||
| 60 | } | ||
| 61 | |||
| 62 | static inline int | ||
| 63 | __ipq_set_mode(unsigned char mode, unsigned int range) | ||
| 64 | { | ||
| 65 | int status = 0; | ||
| 66 | |||
| 67 | switch(mode) { | ||
| 68 | case IPQ_COPY_NONE: | ||
| 69 | case IPQ_COPY_META: | ||
| 70 | copy_mode = mode; | ||
| 71 | copy_range = 0; | ||
| 72 | break; | ||
| 73 | |||
| 74 | case IPQ_COPY_PACKET: | ||
| 75 | if (range > 0xFFFF) | ||
| 76 | range = 0xFFFF; | ||
| 77 | copy_range = range; | ||
| 78 | copy_mode = mode; | ||
| 79 | break; | ||
| 80 | |||
| 81 | default: | ||
| 82 | status = -EINVAL; | ||
| 83 | |||
| 84 | } | ||
| 85 | return status; | ||
| 86 | } | ||
| 87 | |||
| 88 | static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data); | ||
| 89 | |||
| 90 | static inline void | ||
| 91 | __ipq_reset(void) | ||
| 92 | { | ||
| 93 | peer_pid = 0; | ||
| 94 | net_disable_timestamp(); | ||
| 95 | __ipq_set_mode(IPQ_COPY_NONE, 0); | ||
| 96 | __ipq_flush(NULL, 0); | ||
| 97 | } | ||
| 98 | |||
| 99 | static struct nf_queue_entry * | ||
| 100 | ipq_find_dequeue_entry(unsigned long id) | ||
| 101 | { | ||
| 102 | struct nf_queue_entry *entry = NULL, *i; | ||
| 103 | |||
| 104 | spin_lock_bh(&queue_lock); | ||
| 105 | |||
| 106 | list_for_each_entry(i, &queue_list, list) { | ||
| 107 | if ((unsigned long)i == id) { | ||
| 108 | entry = i; | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | if (entry) { | ||
| 114 | list_del(&entry->list); | ||
| 115 | queue_total--; | ||
| 116 | } | ||
| 117 | |||
| 118 | spin_unlock_bh(&queue_lock); | ||
| 119 | return entry; | ||
| 120 | } | ||
| 121 | |||
| 122 | static void | ||
| 123 | __ipq_flush(ipq_cmpfn cmpfn, unsigned long data) | ||
| 124 | { | ||
| 125 | struct nf_queue_entry *entry, *next; | ||
| 126 | |||
| 127 | list_for_each_entry_safe(entry, next, &queue_list, list) { | ||
| 128 | if (!cmpfn || cmpfn(entry, data)) { | ||
| 129 | list_del(&entry->list); | ||
| 130 | queue_total--; | ||
| 131 | nf_reinject(entry, NF_DROP); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | static void | ||
| 137 | ipq_flush(ipq_cmpfn cmpfn, unsigned long data) | ||
| 138 | { | ||
| 139 | spin_lock_bh(&queue_lock); | ||
| 140 | __ipq_flush(cmpfn, data); | ||
| 141 | spin_unlock_bh(&queue_lock); | ||
| 142 | } | ||
| 143 | |||
| 144 | static struct sk_buff * | ||
| 145 | ipq_build_packet_message(struct nf_queue_entry *entry, int *errp) | ||
| 146 | { | ||
| 147 | sk_buff_data_t old_tail; | ||
| 148 | size_t size = 0; | ||
| 149 | size_t data_len = 0; | ||
| 150 | struct sk_buff *skb; | ||
| 151 | struct ipq_packet_msg *pmsg; | ||
| 152 | struct nlmsghdr *nlh; | ||
| 153 | struct timeval tv; | ||
| 154 | |||
| 155 | switch (ACCESS_ONCE(copy_mode)) { | ||
| 156 | case IPQ_COPY_META: | ||
| 157 | case IPQ_COPY_NONE: | ||
| 158 | size = NLMSG_SPACE(sizeof(*pmsg)); | ||
| 159 | break; | ||
| 160 | |||
| 161 | case IPQ_COPY_PACKET: | ||
| 162 | if (entry->skb->ip_summed == CHECKSUM_PARTIAL && | ||
| 163 | (*errp = skb_checksum_help(entry->skb))) | ||
| 164 | return NULL; | ||
| 165 | |||
| 166 | data_len = ACCESS_ONCE(copy_range); | ||
| 167 | if (data_len == 0 || data_len > entry->skb->len) | ||
| 168 | data_len = entry->skb->len; | ||
| 169 | |||
| 170 | size = NLMSG_SPACE(sizeof(*pmsg) + data_len); | ||
| 171 | break; | ||
| 172 | |||
| 173 | default: | ||
| 174 | *errp = -EINVAL; | ||
| 175 | return NULL; | ||
| 176 | } | ||
| 177 | |||
| 178 | skb = alloc_skb(size, GFP_ATOMIC); | ||
| 179 | if (!skb) | ||
| 180 | goto nlmsg_failure; | ||
| 181 | |||
| 182 | old_tail = skb->tail; | ||
| 183 | nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh)); | ||
| 184 | pmsg = NLMSG_DATA(nlh); | ||
| 185 | memset(pmsg, 0, sizeof(*pmsg)); | ||
| 186 | |||
