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