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