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