Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Harald Welte | 9bb7bc9 | 2005-05-30 15:35:26 -0700 | [diff] [blame] | 6 | * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/security.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/sock.h> |
| 30 | #include <net/route.h> |
Patrick McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 31 | #include <net/netfilter/nf_queue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #define IPQ_QMAX_DEFAULT 1024 |
| 34 | #define IPQ_PROC_FS_NAME "ip_queue" |
| 35 | #define NET_IPQ_QMAX 2088 |
| 36 | #define NET_IPQ_QMAX_NAME "ip_queue_maxlen" |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct ipq_queue_entry { |
| 39 | struct list_head list; |
| 40 | struct nf_info *info; |
| 41 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long); |
| 45 | |
Brian Haley | 1192e40 | 2006-09-20 12:03:46 -0700 | [diff] [blame] | 46 | static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE; |
Brian Haley | 94aec08 | 2006-09-18 00:05:22 -0700 | [diff] [blame] | 47 | static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static DEFINE_RWLOCK(queue_lock); |
Brian Haley | 1192e40 | 2006-09-20 12:03:46 -0700 | [diff] [blame] | 49 | static int peer_pid __read_mostly; |
| 50 | static unsigned int copy_range __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static unsigned int queue_total; |
| 52 | static unsigned int queue_dropped = 0; |
| 53 | static unsigned int queue_user_dropped = 0; |
Brian Haley | 1192e40 | 2006-09-20 12:03:46 -0700 | [diff] [blame] | 54 | static struct sock *ipqnl __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static LIST_HEAD(queue_list); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 56 | static DEFINE_MUTEX(ipqnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | static void |
| 59 | ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict) |
| 60 | { |
Harald Welte | 9bb7bc9 | 2005-05-30 15:35:26 -0700 | [diff] [blame] | 61 | /* TCP input path (and probably other bits) assume to be called |
| 62 | * from softirq context, not from syscall, like ipq_issue_verdict is |
| 63 | * called. TCP input path deadlocks with locks taken from timer |
| 64 | * softirq, e.g. We therefore emulate this by local_bh_disable() */ |
| 65 | |
| 66 | local_bh_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | nf_reinject(entry->skb, entry->info, verdict); |
Harald Welte | 9bb7bc9 | 2005-05-30 15:35:26 -0700 | [diff] [blame] | 68 | local_bh_enable(); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | kfree(entry); |
| 71 | } |
| 72 | |
| 73 | static inline void |
| 74 | __ipq_enqueue_entry(struct ipq_queue_entry *entry) |
| 75 | { |
Patrick McHardy | 0ac41e8 | 2007-12-05 01:25:03 -0800 | [diff] [blame] | 76 | list_add_tail(&entry->list, &queue_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | queue_total++; |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static inline int |
| 81 | __ipq_set_mode(unsigned char mode, unsigned int range) |
| 82 | { |
| 83 | int status = 0; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | switch(mode) { |
| 86 | case IPQ_COPY_NONE: |
| 87 | case IPQ_COPY_META: |
| 88 | copy_mode = mode; |
| 89 | copy_range = 0; |
| 90 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | case IPQ_COPY_PACKET: |
| 93 | copy_mode = mode; |
| 94 | copy_range = range; |
| 95 | if (copy_range > 0xFFFF) |
| 96 | copy_range = 0xFFFF; |
| 97 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | default: |
| 100 | status = -EINVAL; |
| 101 | |
| 102 | } |
| 103 | return status; |
| 104 | } |
| 105 | |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 106 | static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data); |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static inline void |
| 109 | __ipq_reset(void) |
| 110 | { |
| 111 | peer_pid = 0; |
| 112 | net_disable_timestamp(); |
| 113 | __ipq_set_mode(IPQ_COPY_NONE, 0); |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 114 | __ipq_flush(NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static struct ipq_queue_entry * |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 118 | ipq_find_dequeue_entry(unsigned long id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 120 | struct ipq_queue_entry *entry = NULL, *i; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | write_lock_bh(&queue_lock); |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 123 | |
| 124 | list_for_each_entry(i, &queue_list, list) { |
| 125 | if ((unsigned long)i == id) { |
| 126 | entry = i; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (entry) { |
| 132 | list_del(&entry->list); |
| 133 | queue_total--; |
| 134 | } |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | write_unlock_bh(&queue_lock); |
| 137 | return entry; |
| 138 | } |
| 139 | |
| 140 | static void |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 141 | __ipq_flush(ipq_cmpfn cmpfn, unsigned long data) |
| 142 | { |
| 143 | struct ipq_queue_entry *entry, *next; |
| 144 | |
| 145 | list_for_each_entry_safe(entry, next, &queue_list, list) { |
| 146 | if (!cmpfn || cmpfn(entry, data)) { |
| 147 | list_del(&entry->list); |
| 148 | queue_total--; |
| 149 | ipq_issue_verdict(entry, NF_DROP); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | ipq_flush(ipq_cmpfn cmpfn, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | write_lock_bh(&queue_lock); |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 158 | __ipq_flush(cmpfn, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | write_unlock_bh(&queue_lock); |
| 160 | } |
| 161 | |
| 162 | static struct sk_buff * |
| 163 | ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp) |
| 164 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 165 | sk_buff_data_t old_tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | size_t size = 0; |
| 167 | size_t data_len = 0; |
| 168 | struct sk_buff *skb; |
| 169 | struct ipq_packet_msg *pmsg; |
| 170 | struct nlmsghdr *nlh; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 171 | struct timeval tv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | read_lock_bh(&queue_lock); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | switch (copy_mode) { |
| 176 | case IPQ_COPY_META: |
| 177 | case IPQ_COPY_NONE: |
| 178 | size = NLMSG_SPACE(sizeof(*pmsg)); |
| 179 | data_len = 0; |
| 180 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | case IPQ_COPY_PACKET: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 183 | if ((entry->skb->ip_summed == CHECKSUM_PARTIAL || |
| 184 | entry->skb->ip_summed == CHECKSUM_COMPLETE) && |
| 185 | (*errp = skb_checksum_help(entry->skb))) { |
Patrick McHardy | 66a79a1 | 2005-08-23 10:10:35 -0700 | [diff] [blame] | 186 | read_unlock_bh(&queue_lock); |
| 187 | return NULL; |
| 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (copy_range == 0 || copy_range > entry->skb->len) |
| 190 | data_len = entry->skb->len; |
| 191 | else |
| 192 | data_len = copy_range; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | size = NLMSG_SPACE(sizeof(*pmsg) + data_len); |
| 195 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | default: |
| 198 | *errp = -EINVAL; |
| 199 | read_unlock_bh(&queue_lock); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | read_unlock_bh(&queue_lock); |
| 204 | |
| 205 | skb = alloc_skb(size, GFP_ATOMIC); |
| 206 | if (!skb) |
| 207 | goto nlmsg_failure; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 208 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 209 | old_tail = skb->tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh)); |
| 211 | pmsg = NLMSG_DATA(nlh); |
| 212 | memset(pmsg, 0, sizeof(*pmsg)); |
| 213 | |
| 214 | pmsg->packet_id = (unsigned long )entry; |
| 215 | pmsg->data_len = data_len; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 216 | tv = ktime_to_timeval(entry->skb->tstamp); |
| 217 | pmsg->timestamp_sec = tv.tv_sec; |
| 218 | pmsg->timestamp_usec = tv.tv_usec; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 219 | pmsg->mark = entry->skb->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | pmsg->hook = entry->info->hook; |
| 221 | pmsg->hw_protocol = entry->skb->protocol; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (entry->info->indev) |
| 224 | strcpy(pmsg->indev_name, entry->info->indev->name); |
| 225 | else |
| 226 | pmsg->indev_name[0] = '\0'; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (entry->info->outdev) |
| 229 | strcpy(pmsg->outdev_name, entry->info->outdev->name); |
| 230 | else |
| 231 | pmsg->outdev_name[0] = '\0'; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (entry->info->indev && entry->skb->dev) { |
| 234 | pmsg->hw_type = entry->skb->dev->type; |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 235 | pmsg->hw_addrlen = dev_parse_header(entry->skb, |
| 236 | pmsg->hw_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (data_len) |
| 240 | if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len)) |
| 241 | BUG(); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | nlh->nlmsg_len = skb->tail - old_tail; |
| 244 | return skb; |
| 245 | |
| 246 | nlmsg_failure: |
| 247 | if (skb) |
| 248 | kfree_skb(skb); |
| 249 | *errp = -EINVAL; |
| 250 | printk(KERN_ERR "ip_queue: error creating packet message\n"); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | static int |
Harald Welte | 0ab43f8 | 2005-08-09 19:43:44 -0700 | [diff] [blame] | 255 | ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, |
Patrick McHardy | f9d8928 | 2007-12-05 01:24:30 -0800 | [diff] [blame] | 256 | unsigned int queuenum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | int status = -EINVAL; |
| 259 | struct sk_buff *nskb; |
| 260 | struct ipq_queue_entry *entry; |
| 261 | |
| 262 | if (copy_mode == IPQ_COPY_NONE) |
| 263 | return -EAGAIN; |
| 264 | |
| 265 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 266 | if (entry == NULL) { |
| 267 | printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n"); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | entry->info = info; |
| 272 | entry->skb = skb; |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | nskb = ipq_build_packet_message(entry, &status); |
| 275 | if (nskb == NULL) |
| 276 | goto err_out_free; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | write_lock_bh(&queue_lock); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (!peer_pid) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 281 | goto err_out_free_nskb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | if (queue_total >= queue_maxlen) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 284 | queue_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | status = -ENOSPC; |
| 286 | if (net_ratelimit()) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 287 | printk (KERN_WARNING "ip_queue: full at %d entries, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | "dropping packets(s). Dropped: %d\n", queue_total, |
| 289 | queue_dropped); |
| 290 | goto err_out_free_nskb; |
| 291 | } |
| 292 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 293 | /* netlink_unicast will either free the nskb or attach it to a socket */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT); |
| 295 | if (status < 0) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 296 | queue_user_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | goto err_out_unlock; |
| 298 | } |
| 299 | |
| 300 | __ipq_enqueue_entry(entry); |
| 301 | |
| 302 | write_unlock_bh(&queue_lock); |
| 303 | return status; |
| 304 | |
| 305 | err_out_free_nskb: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 306 | kfree_skb(nskb); |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | err_out_unlock: |
| 309 | write_unlock_bh(&queue_lock); |
| 310 | |
| 311 | err_out_free: |
| 312 | kfree(entry); |
| 313 | return status; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e) |
| 318 | { |
| 319 | int diff; |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 320 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | struct iphdr *user_iph = (struct iphdr *)v->payload; |
| 322 | |
| 323 | if (v->data_len < sizeof(*user_iph)) |
| 324 | return 0; |
| 325 | diff = v->data_len - e->skb->len; |
Patrick McHardy | d8a585d | 2006-11-14 19:48:09 -0800 | [diff] [blame] | 326 | if (diff < 0) { |
| 327 | if (pskb_trim(e->skb, v->data_len)) |
| 328 | return -ENOMEM; |
| 329 | } else if (diff > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (v->data_len > 0xFFFF) |
| 331 | return -EINVAL; |
| 332 | if (diff > skb_tailroom(e->skb)) { |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 333 | err = pskb_expand_head(e->skb, 0, |
| 334 | diff - skb_tailroom(e->skb), |
| 335 | GFP_ATOMIC); |
| 336 | if (err) { |
| 337 | printk(KERN_WARNING "ip_queue: error " |
| 338 | "in mangle, dropping packet: %d\n", -err); |
| 339 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | skb_put(e->skb, diff); |
| 343 | } |
Herbert Xu | 37d4187 | 2007-10-14 00:39:18 -0700 | [diff] [blame] | 344 | if (!skb_make_writable(e->skb, v->data_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 346 | skb_copy_to_linear_data(e->skb, v->payload, v->data_len); |
Patrick McHardy | 66a79a1 | 2005-08-23 10:10:35 -0700 | [diff] [blame] | 347 | e->skb->ip_summed = CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | static int |
| 353 | ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len) |
| 354 | { |
| 355 | struct ipq_queue_entry *entry; |
| 356 | |
| 357 | if (vmsg->value > NF_MAX_VERDICT) |
| 358 | return -EINVAL; |
| 359 | |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 360 | entry = ipq_find_dequeue_entry(vmsg->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (entry == NULL) |
| 362 | return -ENOENT; |
| 363 | else { |
| 364 | int verdict = vmsg->value; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (vmsg->data_len && vmsg->data_len == len) |
| 367 | if (ipq_mangle_ipv4(vmsg, entry) < 0) |
| 368 | verdict = NF_DROP; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | ipq_issue_verdict(entry, verdict); |
| 371 | return 0; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | static int |
| 376 | ipq_set_mode(unsigned char mode, unsigned int range) |
| 377 | { |
| 378 | int status; |
| 379 | |
| 380 | write_lock_bh(&queue_lock); |
| 381 | status = __ipq_set_mode(mode, range); |
| 382 | write_unlock_bh(&queue_lock); |
| 383 | return status; |
| 384 | } |
| 385 | |
| 386 | static int |
| 387 | ipq_receive_peer(struct ipq_peer_msg *pmsg, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 388 | unsigned char type, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | int status = 0; |
| 391 | |
| 392 | if (len < sizeof(*pmsg)) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | switch (type) { |
| 396 | case IPQM_MODE: |
| 397 | status = ipq_set_mode(pmsg->msg.mode.value, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 398 | pmsg->msg.mode.range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | case IPQM_VERDICT: |
| 402 | if (pmsg->msg.verdict.value > NF_MAX_VERDICT) |
| 403 | status = -EINVAL; |
| 404 | else |
| 405 | status = ipq_set_verdict(&pmsg->msg.verdict, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 406 | len - sizeof(*pmsg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | break; |
| 408 | default: |
| 409 | status = -EINVAL; |
| 410 | } |
| 411 | return status; |
| 412 | } |
| 413 | |
| 414 | static int |
| 415 | dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex) |
| 416 | { |
| 417 | if (entry->info->indev) |
| 418 | if (entry->info->indev->ifindex == ifindex) |
| 419 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (entry->info->outdev) |
| 421 | if (entry->info->outdev->ifindex == ifindex) |
| 422 | return 1; |
Patrick McHardy | ef47c6a | 2006-06-27 03:01:48 -0700 | [diff] [blame] | 423 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 424 | if (entry->skb->nf_bridge) { |
| 425 | if (entry->skb->nf_bridge->physindev && |
| 426 | entry->skb->nf_bridge->physindev->ifindex == ifindex) |
| 427 | return 1; |
| 428 | if (entry->skb->nf_bridge->physoutdev && |
| 429 | entry->skb->nf_bridge->physoutdev->ifindex == ifindex) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 430 | return 1; |
Patrick McHardy | ef47c6a | 2006-06-27 03:01:48 -0700 | [diff] [blame] | 431 | } |
| 432 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static void |
| 437 | ipq_dev_drop(int ifindex) |
| 438 | { |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 439 | ipq_flush(dev_cmp, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) |
| 443 | |
| 444 | static inline void |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 445 | __ipq_rcv_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | int status, type, pid, flags, nlmsglen, skblen; |
| 448 | struct nlmsghdr *nlh; |
| 449 | |
| 450 | skblen = skb->len; |
| 451 | if (skblen < sizeof(*nlh)) |
| 452 | return; |
| 453 | |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 454 | nlh = nlmsg_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | nlmsglen = nlh->nlmsg_len; |
| 456 | if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen) |
| 457 | return; |
| 458 | |
| 459 | pid = nlh->nlmsg_pid; |
| 460 | flags = nlh->nlmsg_flags; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI) |
| 463 | RCV_SKB_FAIL(-EINVAL); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (flags & MSG_TRUNC) |
| 466 | RCV_SKB_FAIL(-ECOMM); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | type = nlh->nlmsg_type; |
| 469 | if (type < NLMSG_NOOP || type >= IPQM_MAX) |
| 470 | RCV_SKB_FAIL(-EINVAL); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (type <= IPQM_BASE) |
| 473 | return; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 474 | |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 475 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | RCV_SKB_FAIL(-EPERM); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | write_lock_bh(&queue_lock); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (peer_pid) { |
| 481 | if (peer_pid != pid) { |
| 482 | write_unlock_bh(&queue_lock); |
| 483 | RCV_SKB_FAIL(-EBUSY); |
| 484 | } |
| 485 | } else { |
| 486 | net_enable_timestamp(); |
| 487 | peer_pid = pid; |
| 488 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | write_unlock_bh(&queue_lock); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | status = ipq_receive_peer(NLMSG_DATA(nlh), type, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 493 | nlmsglen - NLMSG_LENGTH(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (status < 0) |
| 495 | RCV_SKB_FAIL(status); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (flags & NLM_F_ACK) |
| 498 | netlink_ack(skb, nlh, 0); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 499 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static void |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 503 | ipq_rcv_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 505 | mutex_lock(&ipqnl_mutex); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 506 | __ipq_rcv_skb(skb); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 507 | mutex_unlock(&ipqnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static int |
| 511 | ipq_rcv_dev_event(struct notifier_block *this, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 512 | unsigned long event, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | struct net_device *dev = ptr; |
| 515 | |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 516 | if (dev->nd_net != &init_net) |
| 517 | return NOTIFY_DONE; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /* Drop any packets associated with the downed device */ |
| 520 | if (event == NETDEV_DOWN) |
| 521 | ipq_dev_drop(dev->ifindex); |
| 522 | return NOTIFY_DONE; |
| 523 | } |
| 524 | |
| 525 | static struct notifier_block ipq_dev_notifier = { |
| 526 | .notifier_call = ipq_rcv_dev_event, |
| 527 | }; |
| 528 | |
| 529 | static int |
| 530 | ipq_rcv_nl_event(struct notifier_block *this, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 531 | unsigned long event, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | struct netlink_notify *n = ptr; |
| 534 | |
| 535 | if (event == NETLINK_URELEASE && |
| 536 | n->protocol == NETLINK_FIREWALL && n->pid) { |
| 537 | write_lock_bh(&queue_lock); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 538 | if ((n->net == &init_net) && (n->pid == peer_pid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | __ipq_reset(); |
| 540 | write_unlock_bh(&queue_lock); |
| 541 | } |
| 542 | return NOTIFY_DONE; |
| 543 | } |
| 544 | |
| 545 | static struct notifier_block ipq_nl_notifier = { |
| 546 | .notifier_call = ipq_rcv_nl_event, |
| 547 | }; |
| 548 | |
| 549 | static struct ctl_table_header *ipq_sysctl_header; |
| 550 | |
| 551 | static ctl_table ipq_table[] = { |
| 552 | { |
| 553 | .ctl_name = NET_IPQ_QMAX, |
| 554 | .procname = NET_IPQ_QMAX_NAME, |
| 555 | .data = &queue_maxlen, |
| 556 | .maxlen = sizeof(queue_maxlen), |
| 557 | .mode = 0644, |
| 558 | .proc_handler = proc_dointvec |
| 559 | }, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 560 | { .ctl_name = 0 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | static ctl_table ipq_dir_table[] = { |
| 564 | { |
| 565 | .ctl_name = NET_IPV4, |
| 566 | .procname = "ipv4", |
| 567 | .mode = 0555, |
| 568 | .child = ipq_table |
| 569 | }, |
| 570 | { .ctl_name = 0 } |
| 571 | }; |
| 572 | |
| 573 | static ctl_table ipq_root_table[] = { |
| 574 | { |
| 575 | .ctl_name = CTL_NET, |
| 576 | .procname = "net", |
| 577 | .mode = 0555, |
| 578 | .child = ipq_dir_table |
| 579 | }, |
| 580 | { .ctl_name = 0 } |
| 581 | }; |
| 582 | |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 583 | static int ip_queue_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | read_lock_bh(&queue_lock); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 586 | |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 587 | seq_printf(m, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 588 | "Peer PID : %d\n" |
| 589 | "Copy mode : %hu\n" |
| 590 | "Copy range : %u\n" |
| 591 | "Queue length : %u\n" |
| 592 | "Queue max. length : %u\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | "Queue dropped : %u\n" |
| 594 | "Netlink dropped : %u\n", |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 595 | peer_pid, |
| 596 | copy_mode, |
| 597 | copy_range, |
| 598 | queue_total, |
| 599 | queue_maxlen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | queue_dropped, |
| 601 | queue_user_dropped); |
| 602 | |
| 603 | read_unlock_bh(&queue_lock); |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 604 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 606 | |
| 607 | static int ip_queue_open(struct inode *inode, struct file *file) |
| 608 | { |
| 609 | return single_open(file, ip_queue_show, NULL); |
| 610 | } |
| 611 | |
| 612 | static const struct file_operations ip_queue_proc_fops = { |
| 613 | .open = ip_queue_open, |
| 614 | .read = seq_read, |
| 615 | .llseek = seq_lseek, |
| 616 | .release = single_release, |
| 617 | .owner = THIS_MODULE, |
| 618 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 620 | static const struct nf_queue_handler nfqh = { |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 621 | .name = "ip_queue", |
| 622 | .outfn = &ipq_enqueue_packet, |
| 623 | }; |
| 624 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 625 | static int __init ip_queue_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
| 627 | int status = -ENOMEM; |
| 628 | struct proc_dir_entry *proc; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | netlink_register_notifier(&ipq_nl_notifier); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 631 | ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0, |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 632 | ipq_rcv_skb, NULL, THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (ipqnl == NULL) { |
| 634 | printk(KERN_ERR "ip_queue: failed to create netlink socket\n"); |
| 635 | goto cleanup_netlink_notifier; |
| 636 | } |
| 637 | |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 638 | proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net); |
| 639 | if (proc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | proc->owner = THIS_MODULE; |
Alexey Dobriyan | 7351a22 | 2007-11-05 20:33:46 -0800 | [diff] [blame] | 641 | proc->proc_fops = &ip_queue_proc_fops; |
| 642 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | printk(KERN_ERR "ip_queue: failed to create proc entry\n"); |
| 644 | goto cleanup_ipqnl; |
| 645 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | register_netdevice_notifier(&ipq_dev_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 648 | ipq_sysctl_header = register_sysctl_table(ipq_root_table); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 649 | |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 650 | status = nf_register_queue_handler(PF_INET, &nfqh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | if (status < 0) { |
| 652 | printk(KERN_ERR "ip_queue: failed to register queue handler\n"); |
| 653 | goto cleanup_sysctl; |
| 654 | } |
| 655 | return status; |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | cleanup_sysctl: |
| 658 | unregister_sysctl_table(ipq_sysctl_header); |
| 659 | unregister_netdevice_notifier(&ipq_dev_notifier); |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 660 | proc_net_remove(&init_net, IPQ_PROC_FS_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | cleanup_ipqnl: |
| 662 | sock_release(ipqnl->sk_socket); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 663 | mutex_lock(&ipqnl_mutex); |
| 664 | mutex_unlock(&ipqnl_mutex); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | cleanup_netlink_notifier: |
| 667 | netlink_unregister_notifier(&ipq_nl_notifier); |
| 668 | return status; |
| 669 | } |
| 670 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 671 | static void __exit ip_queue_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 673 | nf_unregister_queue_handlers(&nfqh); |
| 674 | synchronize_net(); |
Patrick McHardy | 9521409 | 2007-12-05 01:25:46 -0800 | [diff] [blame^] | 675 | ipq_flush(NULL, 0); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 676 | |
| 677 | unregister_sysctl_table(ipq_sysctl_header); |
| 678 | unregister_netdevice_notifier(&ipq_dev_notifier); |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 679 | proc_net_remove(&init_net, IPQ_PROC_FS_NAME); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 680 | |
| 681 | sock_release(ipqnl->sk_socket); |
| 682 | mutex_lock(&ipqnl_mutex); |
| 683 | mutex_unlock(&ipqnl_mutex); |
| 684 | |
| 685 | netlink_unregister_notifier(&ipq_nl_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | MODULE_DESCRIPTION("IPv4 packet queue handler"); |
| 689 | MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); |
| 690 | MODULE_LICENSE("GPL"); |
| 691 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 692 | module_init(ip_queue_init); |
| 693 | module_exit(ip_queue_fini); |