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