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