2 * Packet matching code.
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
6 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/capability.h>
18 #include <linux/skbuff.h>
19 #include <linux/kmod.h>
20 #include <linux/vmalloc.h>
21 #include <linux/netdevice.h>
22 #include <linux/module.h>
23 #include <linux/poison.h>
24 #include <linux/icmpv6.h>
26 #include <net/compat.h>
27 #include <linux/uaccess.h>
28 #include <linux/mutex.h>
29 #include <linux/proc_fs.h>
30 #include <linux/err.h>
31 #include <linux/cpumask.h>
33 #include <linux/netfilter_ipv6/ip6_tables.h>
34 #include <linux/netfilter/x_tables.h>
35 #include <net/netfilter/nf_log.h>
36 #include "../../netfilter/xt_repldata.h"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
40 MODULE_DESCRIPTION("IPv6 packet filter");
42 #ifdef CONFIG_NETFILTER_DEBUG
43 #define IP_NF_ASSERT(x) WARN_ON(!(x))
45 #define IP_NF_ASSERT(x)
48 void *ip6t_alloc_initial_table(const struct xt_table
*info
)
50 return xt_alloc_initial_table(ip6t
, IP6T
);
52 EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table
);
54 /* Returns whether matches rule or not. */
55 /* Performance critical - called for every packet */
57 ip6_packet_match(const struct sk_buff
*skb
,
60 const struct ip6t_ip6
*ip6info
,
61 unsigned int *protoff
,
62 int *fragoff
, bool *hotdrop
)
65 const struct ipv6hdr
*ipv6
= ipv6_hdr(skb
);
67 if (NF_INVF(ip6info
, IP6T_INV_SRCIP
,
68 ipv6_masked_addr_cmp(&ipv6
->saddr
, &ip6info
->smsk
,
70 NF_INVF(ip6info
, IP6T_INV_DSTIP
,
71 ipv6_masked_addr_cmp(&ipv6
->daddr
, &ip6info
->dmsk
,
75 ret
= ifname_compare_aligned(indev
, ip6info
->iniface
, ip6info
->iniface_mask
);
77 if (NF_INVF(ip6info
, IP6T_INV_VIA_IN
, ret
!= 0))
80 ret
= ifname_compare_aligned(outdev
, ip6info
->outiface
, ip6info
->outiface_mask
);
82 if (NF_INVF(ip6info
, IP6T_INV_VIA_OUT
, ret
!= 0))
85 /* ... might want to do something with class and flowlabel here ... */
87 /* look for the desired protocol header */
88 if (ip6info
->flags
& IP6T_F_PROTO
) {
90 unsigned short _frag_off
;
92 protohdr
= ipv6_find_hdr(skb
, protoff
, -1, &_frag_off
, NULL
);
100 if (ip6info
->proto
== protohdr
) {
101 if (ip6info
->invflags
& IP6T_INV_PROTO
)
107 /* We need match for the '-p all', too! */
108 if ((ip6info
->proto
!= 0) &&
109 !(ip6info
->invflags
& IP6T_INV_PROTO
))
115 /* should be ip6 safe */
117 ip6_checkentry(const struct ip6t_ip6
*ipv6
)
119 if (ipv6
->flags
& ~IP6T_F_MASK
)
121 if (ipv6
->invflags
& ~IP6T_INV_MASK
)
128 ip6t_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
130 net_info_ratelimited("error: `%s'\n", (const char *)par
->targinfo
);
135 static inline struct ip6t_entry
*
136 get_entry(const void *base
, unsigned int offset
)
138 return (struct ip6t_entry
*)(base
+ offset
);
141 /* All zeroes == unconditional rule. */
142 /* Mildly perf critical (only if packet tracing is on) */
143 static inline bool unconditional(const struct ip6t_entry
*e
)
145 static const struct ip6t_ip6 uncond
;
147 return e
->target_offset
== sizeof(struct ip6t_entry
) &&
148 memcmp(&e
->ipv6
, &uncond
, sizeof(uncond
)) == 0;
151 static inline const struct xt_entry_target
*
152 ip6t_get_target_c(const struct ip6t_entry
*e
)
154 return ip6t_get_target((struct ip6t_entry
*)e
);
157 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
158 /* This cries for unification! */
159 static const char *const hooknames
[] = {
160 [NF_INET_PRE_ROUTING
] = "PREROUTING",
161 [NF_INET_LOCAL_IN
] = "INPUT",
162 [NF_INET_FORWARD
] = "FORWARD",
163 [NF_INET_LOCAL_OUT
] = "OUTPUT",
164 [NF_INET_POST_ROUTING
] = "POSTROUTING",
167 enum nf_ip_trace_comments
{
168 NF_IP6_TRACE_COMMENT_RULE
,
169 NF_IP6_TRACE_COMMENT_RETURN
,
170 NF_IP6_TRACE_COMMENT_POLICY
,
173 static const char *const comments
[] = {
174 [NF_IP6_TRACE_COMMENT_RULE
] = "rule",
175 [NF_IP6_TRACE_COMMENT_RETURN
] = "return",
176 [NF_IP6_TRACE_COMMENT_POLICY
] = "policy",
179 static struct nf_loginfo trace_loginfo
= {
180 .type
= NF_LOG_TYPE_LOG
,
183 .level
= LOGLEVEL_WARNING
,
184 .logflags
= NF_LOG_DEFAULT_MASK
,
189 /* Mildly perf critical (only if packet tracing is on) */
191 get_chainname_rulenum(const struct ip6t_entry
*s
, const struct ip6t_entry
*e
,
192 const char *hookname
, const char **chainname
,
193 const char **comment
, unsigned int *rulenum
)
195 const struct xt_standard_target
*t
= (void *)ip6t_get_target_c(s
);
197 if (strcmp(t
->target
.u
.kernel
.target
->name
, XT_ERROR_TARGET
) == 0) {
198 /* Head of user chain: ERROR target with chainname */
199 *chainname
= t
->target
.data
;
204 if (unconditional(s
) &&
205 strcmp(t
->target
.u
.kernel
.target
->name
,
206 XT_STANDARD_TARGET
) == 0 &&
208 /* Tail of chains: STANDARD target (return/policy) */
209 *comment
= *chainname
== hookname
210 ? comments
[NF_IP6_TRACE_COMMENT_POLICY
]
211 : comments
[NF_IP6_TRACE_COMMENT_RETURN
];
220 static void trace_packet(struct net
*net
,
221 const struct sk_buff
*skb
,
223 const struct net_device
*in
,
224 const struct net_device
*out
,
225 const char *tablename
,
226 const struct xt_table_info
*private,
227 const struct ip6t_entry
*e
)
229 const struct ip6t_entry
*root
;
230 const char *hookname
, *chainname
, *comment
;
231 const struct ip6t_entry
*iter
;
232 unsigned int rulenum
= 0;
234 root
= get_entry(private->entries
, private->hook_entry
[hook
]);
236 hookname
= chainname
= hooknames
[hook
];
237 comment
= comments
[NF_IP6_TRACE_COMMENT_RULE
];
239 xt_entry_foreach(iter
, root
, private->size
- private->hook_entry
[hook
])
240 if (get_chainname_rulenum(iter
, e
, hookname
,
241 &chainname
, &comment
, &rulenum
) != 0)
244 nf_log_trace(net
, AF_INET6
, hook
, skb
, in
, out
, &trace_loginfo
,
245 "TRACE: %s:%s:%s:%u ",
246 tablename
, chainname
, comment
, rulenum
);
250 static inline struct ip6t_entry
*
251 ip6t_next_entry(const struct ip6t_entry
*entry
)
253 return (void *)entry
+ entry
->next_offset
;
256 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
258 ip6t_do_table(struct sk_buff
*skb
,
259 const struct nf_hook_state
*state
,
260 struct xt_table
*table
)
262 unsigned int hook
= state
->hook
;
263 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
264 /* Initializing verdict to NF_DROP keeps gcc happy. */
265 unsigned int verdict
= NF_DROP
;
266 const char *indev
, *outdev
;
267 const void *table_base
;
268 struct ip6t_entry
*e
, **jumpstack
;
269 unsigned int stackidx
, cpu
;
270 const struct xt_table_info
*private;
271 struct xt_action_param acpar
;
276 indev
= state
->in
? state
->in
->name
: nulldevname
;
277 outdev
= state
->out
? state
->out
->name
: nulldevname
;
278 /* We handle fragments by dealing with the first fragment as
279 * if it was a normal packet. All other fragments are treated
280 * normally, except that they will NEVER match rules that ask
281 * things we don't know, ie. tcp syn flag or ports). If the
282 * rule is also a fragment-specific rule, non-fragments won't
284 acpar
.hotdrop
= false;
287 IP_NF_ASSERT(table
->valid_hooks
& (1 << hook
));
290 addend
= xt_write_recseq_begin();
291 private = table
->private;
293 * Ensure we load private-> members after we've fetched the base
296 smp_read_barrier_depends();
297 cpu
= smp_processor_id();
298 table_base
= private->entries
;
299 jumpstack
= (struct ip6t_entry
**)private->jumpstack
[cpu
];
301 /* Switch to alternate jumpstack if we're being invoked via TEE.
302 * TEE issues XT_CONTINUE verdict on original skb so we must not
303 * clobber the jumpstack.
305 * For recursion via REJECT or SYNPROXY the stack will be clobbered
306 * but it is no problem since absolute verdict is issued by these.
308 if (static_key_false(&xt_tee_enabled
))
309 jumpstack
+= private->stacksize
* __this_cpu_read(nf_skb_duplicated
);
311 e
= get_entry(table_base
, private->hook_entry
[hook
]);
314 const struct xt_entry_target
*t
;
315 const struct xt_entry_match
*ematch
;
316 struct xt_counters
*counter
;
320 if (!ip6_packet_match(skb
, indev
, outdev
, &e
->ipv6
,
321 &acpar
.thoff
, &acpar
.fragoff
, &acpar
.hotdrop
)) {
323 e
= ip6t_next_entry(e
);
327 xt_ematch_foreach(ematch
, e
) {
328 acpar
.match
= ematch
->u
.kernel
.match
;
329 acpar
.matchinfo
= ematch
->data
;
330 if (!acpar
.match
->match(skb
, &acpar
))
334 counter
= xt_get_this_cpu_counter(&e
->counters
);
335 ADD_COUNTER(*counter
, skb
->len
, 1);
337 t
= ip6t_get_target_c(e
);
338 IP_NF_ASSERT(t
->u
.kernel
.target
);
340 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
341 /* The packet is traced: log it */
342 if (unlikely(skb
->nf_trace
))
343 trace_packet(state
->net
, skb
, hook
, state
->in
,
344 state
->out
, table
->name
, private, e
);
346 /* Standard target? */
347 if (!t
->u
.kernel
.target
->target
) {
350 v
= ((struct xt_standard_target
*)t
)->verdict
;
352 /* Pop from stack? */
353 if (v
!= XT_RETURN
) {
354 verdict
= (unsigned int)(-v
) - 1;
358 e
= get_entry(table_base
,
359 private->underflow
[hook
]);
361 e
= ip6t_next_entry(jumpstack
[--stackidx
]);
364 if (table_base
+ v
!= ip6t_next_entry(e
) &&
365 !(e
->ipv6
.flags
& IP6T_F_GOTO
)) {
366 jumpstack
[stackidx
++] = e
;
369 e
= get_entry(table_base
, v
);
373 acpar
.target
= t
->u
.kernel
.target
;
374 acpar
.targinfo
= t
->data
;
376 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
377 if (verdict
== XT_CONTINUE
)
378 e
= ip6t_next_entry(e
);
382 } while (!acpar
.hotdrop
);
384 xt_write_recseq_end(addend
);
392 static bool next_offset_ok(const struct xt_table_info
*t
, unsigned int newpos
)
394 if (newpos
> t
->size
- sizeof(struct ip6t_entry
))
397 if (newpos
% __alignof__(struct ip6t_entry
) != 0)
403 /* Figures out from what hook each rule can be called: returns 0 if
404 there are loops. Puts hook bitmask in comefrom. */
406 mark_source_chains(const struct xt_table_info
*newinfo
,
407 unsigned int valid_hooks
, void *entry0
,
408 unsigned int *offsets
)
412 /* No recursion; use packet counter to save back ptrs (reset
413 to 0 as we leave), and comefrom to save source hook bitmask */
414 for (hook
= 0; hook
< NF_INET_NUMHOOKS
; hook
++) {
415 unsigned int pos
= newinfo
->hook_entry
[hook
];
416 struct ip6t_entry
*e
= entry0
+ pos
;
418 if (!(valid_hooks
& (1 << hook
)))
421 /* Set initial back pointer. */
422 e
->counters
.pcnt
= pos
;
425 const struct xt_standard_target
*t
426 = (void *)ip6t_get_target_c(e
);
427 int visited
= e
->comefrom
& (1 << hook
);
429 if (e
->comefrom
& (1 << NF_INET_NUMHOOKS
))
432 e
->comefrom
|= ((1 << hook
) | (1 << NF_INET_NUMHOOKS
));
434 /* Unconditional return/END. */
435 if ((unconditional(e
) &&
436 (strcmp(t
->target
.u
.user
.name
,
437 XT_STANDARD_TARGET
) == 0) &&
438 t
->verdict
< 0) || visited
) {
439 unsigned int oldpos
, size
;
441 if ((strcmp(t
->target
.u
.user
.name
,
442 XT_STANDARD_TARGET
) == 0) &&
443 t
->verdict
< -NF_MAX_VERDICT
- 1)
446 /* Return: backtrack through the last
449 e
->comefrom
^= (1<<NF_INET_NUMHOOKS
);
451 pos
= e
->counters
.pcnt
;
452 e
->counters
.pcnt
= 0;
454 /* We're at the start. */
459 } while (oldpos
== pos
+ e
->next_offset
);
462 size
= e
->next_offset
;
463 if (!next_offset_ok(newinfo
, pos
+ size
))
465 e
= entry0
+ pos
+ size
;
466 if (pos
+ size
>= newinfo
->size
)
468 e
->counters
.pcnt
= pos
;
471 int newpos
= t
->verdict
;
473 if (strcmp(t
->target
.u
.user
.name
,
474 XT_STANDARD_TARGET
) == 0 &&
476 /* This a jump; chase it. */
477 if (!xt_find_jump_offset(offsets
, newpos
,
482 /* ... this is a fallthru */
483 newpos
= pos
+ e
->next_offset
;
484 if (newpos
>= newinfo
->size
)
488 if (!next_offset_ok(newinfo
, newpos
))
492 e
->counters
.pcnt
= pos
;
501 static void cleanup_match(struct xt_entry_match
*m
, struct net
*net
)
503 struct xt_mtdtor_param par
;
506 par
.match
= m
->u
.kernel
.match
;
507 par
.matchinfo
= m
->data
;
508 par
.family
= NFPROTO_IPV6
;
509 if (par
.match
->destroy
!= NULL
)
510 par
.match
->destroy(&par
);
511 module_put(par
.match
->me
);
514 static int check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
516 const struct ip6t_ip6
*ipv6
= par
->entryinfo
;
518 par
->match
= m
->u
.kernel
.match
;
519 par
->matchinfo
= m
->data
;
521 return xt_check_match(par
, m
->u
.match_size
- sizeof(*m
),
522 ipv6
->proto
, ipv6
->invflags
& IP6T_INV_PROTO
);
526 find_check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
528 struct xt_match
*match
;
531 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
534 return PTR_ERR(match
);
536 m
->u
.kernel
.match
= match
;
538 ret
= check_match(m
, par
);
544 module_put(m
->u
.kernel
.match
->me
);
548 static int check_target(struct ip6t_entry
*e
, struct net
*net
, const char *name
)
550 struct xt_entry_target
*t
= ip6t_get_target(e
);
551 struct xt_tgchk_param par
= {
555 .target
= t
->u
.kernel
.target
,
557 .hook_mask
= e
->comefrom
,
558 .family
= NFPROTO_IPV6
,
561 t
= ip6t_get_target(e
);
562 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
),
564 e
->ipv6
.invflags
& IP6T_INV_PROTO
);
568 find_check_entry(struct ip6t_entry
*e
, struct net
*net
, const char *name
,
570 struct xt_percpu_counter_alloc_state
*alloc_state
)
572 struct xt_entry_target
*t
;
573 struct xt_target
*target
;
576 struct xt_mtchk_param mtpar
;
577 struct xt_entry_match
*ematch
;
579 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
585 mtpar
.entryinfo
= &e
->ipv6
;
586 mtpar
.hook_mask
= e
->comefrom
;
587 mtpar
.family
= NFPROTO_IPV6
;
588 xt_ematch_foreach(ematch
, e
) {
589 ret
= find_check_match(ematch
, &mtpar
);
591 goto cleanup_matches
;
595 t
= ip6t_get_target(e
);
596 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
598 if (IS_ERR(target
)) {
599 ret
= PTR_ERR(target
);
600 goto cleanup_matches
;
602 t
->u
.kernel
.target
= target
;
604 ret
= check_target(e
, net
, name
);
609 module_put(t
->u
.kernel
.target
->me
);
611 xt_ematch_foreach(ematch
, e
) {
614 cleanup_match(ematch
, net
);
617 xt_percpu_counter_free(&e
->counters
);
622 static bool check_underflow(const struct ip6t_entry
*e
)
624 const struct xt_entry_target
*t
;
625 unsigned int verdict
;
627 if (!unconditional(e
))
629 t
= ip6t_get_target_c(e
);
630 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
632 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
633 verdict
= -verdict
- 1;
634 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
638 check_entry_size_and_hooks(struct ip6t_entry
*e
,
639 struct xt_table_info
*newinfo
,
640 const unsigned char *base
,
641 const unsigned char *limit
,
642 const unsigned int *hook_entries
,
643 const unsigned int *underflows
,
644 unsigned int valid_hooks
)
649 if ((unsigned long)e
% __alignof__(struct ip6t_entry
) != 0 ||
650 (unsigned char *)e
+ sizeof(struct ip6t_entry
) >= limit
||
651 (unsigned char *)e
+ e
->next_offset
> limit
)
655 < sizeof(struct ip6t_entry
) + sizeof(struct xt_entry_target
))
658 if (!ip6_checkentry(&e
->ipv6
))
661 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
666 /* Check hooks & underflows */
667 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
668 if (!(valid_hooks
& (1 << h
)))
670 if ((unsigned char *)e
- base
== hook_entries
[h
])
671 newinfo
->hook_entry
[h
] = hook_entries
[h
];
672 if ((unsigned char *)e
- base
== underflows
[h
]) {
673 if (!check_underflow(e
))
676 newinfo
->underflow
[h
] = underflows
[h
];
680 /* Clear counters and comefrom */
681 e
->counters
= ((struct xt_counters
) { 0, 0 });
686 static void cleanup_entry(struct ip6t_entry
*e
, struct net
*net
)
688 struct xt_tgdtor_param par
;
689 struct xt_entry_target
*t
;
690 struct xt_entry_match
*ematch
;
692 /* Cleanup all matches */
693 xt_ematch_foreach(ematch
, e
)
694 cleanup_match(ematch
, net
);
695 t
= ip6t_get_target(e
);
698 par
.target
= t
->u
.kernel
.target
;
699 par
.targinfo
= t
->data
;
700 par
.family
= NFPROTO_IPV6
;
701 if (par
.target
->destroy
!= NULL
)
702 par
.target
->destroy(&par
);
703 module_put(par
.target
->me
);
704 xt_percpu_counter_free(&e
->counters
);
707 /* Checks and translates the user-supplied table segment (held in
710 translate_table(struct net
*net
, struct xt_table_info
*newinfo
, void *entry0
,
711 const struct ip6t_replace
*repl
)
713 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
714 struct ip6t_entry
*iter
;
715 unsigned int *offsets
;
719 newinfo
->size
= repl
->size
;
720 newinfo
->number
= repl
->num_entries
;
722 /* Init all hooks to impossible value. */
723 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
724 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
725 newinfo
->underflow
[i
] = 0xFFFFFFFF;
728 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
732 /* Walk through entries, checking offsets. */
733 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
734 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
741 if (i
< repl
->num_entries
)
742 offsets
[i
] = (void *)iter
- entry0
;
744 if (strcmp(ip6t_get_target(iter
)->u
.user
.name
,
745 XT_ERROR_TARGET
) == 0)
746 ++newinfo
->stacksize
;
750 if (i
!= repl
->num_entries
)
753 /* Check hooks all assigned */
754 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
755 /* Only hooks which are valid */
756 if (!(repl
->valid_hooks
& (1 << i
)))
758 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
760 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
764 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
770 /* Finally, each sanity check must pass */
772 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
773 ret
= find_check_entry(iter
, net
, repl
->name
, repl
->size
,
781 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
784 cleanup_entry(iter
, net
);
796 get_counters(const struct xt_table_info
*t
,
797 struct xt_counters counters
[])
799 struct ip6t_entry
*iter
;
803 for_each_possible_cpu(cpu
) {
804 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
807 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
808 struct xt_counters
*tmp
;
812 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
814 start
= read_seqcount_begin(s
);
817 } while (read_seqcount_retry(s
, start
));
819 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
825 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
827 unsigned int countersize
;
828 struct xt_counters
*counters
;
829 const struct xt_table_info
*private = table
->private;
831 /* We need atomic snapshot of counters: rest doesn't change
832 (other than comefrom, which userspace doesn't care
834 countersize
= sizeof(struct xt_counters
) * private->number
;
835 counters
= vzalloc(countersize
);
837 if (counters
== NULL
)
838 return ERR_PTR(-ENOMEM
);
840 get_counters(private, counters
);
846 copy_entries_to_user(unsigned int total_size
,
847 const struct xt_table
*table
,
848 void __user
*userptr
)
850 unsigned int off
, num
;
851 const struct ip6t_entry
*e
;
852 struct xt_counters
*counters
;
853 const struct xt_table_info
*private = table
->private;
855 const void *loc_cpu_entry
;
857 counters
= alloc_counters(table
);
858 if (IS_ERR(counters
))
859 return PTR_ERR(counters
);
861 loc_cpu_entry
= private->entries
;
863 /* FIXME: use iterator macros --RR */
864 /* ... then go back and fix counters and names */
865 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
867 const struct xt_entry_match
*m
;
868 const struct xt_entry_target
*t
;
870 e
= loc_cpu_entry
+ off
;
871 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
875 if (copy_to_user(userptr
+ off
876 + offsetof(struct ip6t_entry
, counters
),
878 sizeof(counters
[num
])) != 0) {
883 for (i
= sizeof(struct ip6t_entry
);
884 i
< e
->target_offset
;
885 i
+= m
->u
.match_size
) {
888 if (xt_match_to_user(m
, userptr
+ off
+ i
)) {
894 t
= ip6t_get_target_c(e
);
895 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
907 static void compat_standard_from_user(void *dst
, const void *src
)
909 int v
= *(compat_int_t
*)src
;
912 v
+= xt_compat_calc_jump(AF_INET6
, v
);
913 memcpy(dst
, &v
, sizeof(v
));
916 static int compat_standard_to_user(void __user
*dst
, const void *src
)
918 compat_int_t cv
= *(int *)src
;
921 cv
-= xt_compat_calc_jump(AF_INET6
, cv
);
922 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
925 static int compat_calc_entry(const struct ip6t_entry
*e
,
926 const struct xt_table_info
*info
,
927 const void *base
, struct xt_table_info
*newinfo
)
929 const struct xt_entry_match
*ematch
;
930 const struct xt_entry_target
*t
;
931 unsigned int entry_offset
;
934 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
935 entry_offset
= (void *)e
- base
;
936 xt_ematch_foreach(ematch
, e
)
937 off
+= xt_compat_match_offset(ematch
->u
.kernel
.match
);
938 t
= ip6t_get_target_c(e
);
939 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
940 newinfo
->size
-= off
;
941 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
945 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
946 if (info
->hook_entry
[i
] &&
947 (e
< (struct ip6t_entry
*)(base
+ info
->hook_entry
[i
])))
948 newinfo
->hook_entry
[i
] -= off
;
949 if (info
->underflow
[i
] &&
950 (e
< (struct ip6t_entry
*)(base
+ info
->underflow
[i
])))
951 newinfo
->underflow
[i
] -= off
;
956 static int compat_table_info(const struct xt_table_info
*info
,
957 struct xt_table_info
*newinfo
)
959 struct ip6t_entry
*iter
;
960 const void *loc_cpu_entry
;
963 if (!newinfo
|| !info
)
966 /* we dont care about newinfo->entries */
967 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
968 newinfo
->initial_entries
= 0;
969 loc_cpu_entry
= info
->entries
;
970 xt_compat_init_offsets(AF_INET6
, info
->number
);
971 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
972 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
980 static int get_info(struct net
*net
, void __user
*user
,
981 const int *len
, int compat
)
983 char name
[XT_TABLE_MAXNAMELEN
];
987 if (*len
!= sizeof(struct ip6t_getinfo
))
990 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
993 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
996 xt_compat_lock(AF_INET6
);
998 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
999 "ip6table_%s", name
);
1001 struct ip6t_getinfo info
;
1002 const struct xt_table_info
*private = t
->private;
1003 #ifdef CONFIG_COMPAT
1004 struct xt_table_info tmp
;
1007 ret
= compat_table_info(private, &tmp
);
1008 xt_compat_flush_offsets(AF_INET6
);
1012 memset(&info
, 0, sizeof(info
));
1013 info
.valid_hooks
= t
->valid_hooks
;
1014 memcpy(info
.hook_entry
, private->hook_entry
,
1015 sizeof(info
.hook_entry
));
1016 memcpy(info
.underflow
, private->underflow
,
1017 sizeof(info
.underflow
));
1018 info
.num_entries
= private->number
;
1019 info
.size
= private->size
;
1020 strcpy(info
.name
, name
);
1022 if (copy_to_user(user
, &info
, *len
) != 0)
1031 #ifdef CONFIG_COMPAT
1033 xt_compat_unlock(AF_INET6
);
1039 get_entries(struct net
*net
, struct ip6t_get_entries __user
*uptr
,
1043 struct ip6t_get_entries get
;
1046 if (*len
< sizeof(get
))
1048 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1050 if (*len
!= sizeof(struct ip6t_get_entries
) + get
.size
)
1053 get
.name
[sizeof(get
.name
) - 1] = '\0';
1055 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1057 struct xt_table_info
*private = t
->private;
1058 if (get
.size
== private->size
)
1059 ret
= copy_entries_to_user(private->size
,
1060 t
, uptr
->entrytable
);
1073 __do_replace(struct net
*net
, const char *name
, unsigned int valid_hooks
,
1074 struct xt_table_info
*newinfo
, unsigned int num_counters
,
1075 void __user
*counters_ptr
)
1079 struct xt_table_info
*oldinfo
;
1080 struct xt_counters
*counters
;
1081 struct ip6t_entry
*iter
;
1084 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
1090 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
1091 "ip6table_%s", name
);
1094 goto free_newinfo_counters_untrans
;
1098 if (valid_hooks
!= t
->valid_hooks
) {
1103 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1107 /* Update module usage count based on number of rules */
1108 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1109 (newinfo
->number
<= oldinfo
->initial_entries
))
1111 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1112 (newinfo
->number
<= oldinfo
->initial_entries
))
1115 /* Get the old counters, and synchronize with replace */
1116 get_counters(oldinfo
, counters
);
1118 /* Decrease module usage counts and free resource */
1119 xt_entry_foreach(iter
, oldinfo
->entries
, oldinfo
->size
)
1120 cleanup_entry(iter
, net
);
1122 xt_free_table_info(oldinfo
);
1123 if (copy_to_user(counters_ptr
, counters
,
1124 sizeof(struct xt_counters
) * num_counters
) != 0) {
1125 /* Silent error, can't fail, new table is already in place */
1126 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1135 free_newinfo_counters_untrans
:
1142 do_replace(struct net
*net
, const void __user
*user
, unsigned int len
)
1145 struct ip6t_replace tmp
;
1146 struct xt_table_info
*newinfo
;
1147 void *loc_cpu_entry
;
1148 struct ip6t_entry
*iter
;
1150 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1153 /* overflow check */
1154 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1156 if (tmp
.num_counters
== 0)
1159 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1161 newinfo
= xt_alloc_table_info(tmp
.size
);
1165 loc_cpu_entry
= newinfo
->entries
;
1166 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1172 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, &tmp
);
1176 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1177 tmp
.num_counters
, tmp
.counters
);
1179 goto free_newinfo_untrans
;
1182 free_newinfo_untrans
:
1183 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1184 cleanup_entry(iter
, net
);
1186 xt_free_table_info(newinfo
);
1191 do_add_counters(struct net
*net
, const void __user
*user
, unsigned int len
,
1195 struct xt_counters_info tmp
;
1196 struct xt_counters
*paddc
;
1198 const struct xt_table_info
*private;
1200 struct ip6t_entry
*iter
;
1201 unsigned int addend
;
1203 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1205 return PTR_ERR(paddc
);
1206 t
= xt_find_table_lock(net
, AF_INET6
, tmp
.name
);
1213 private = t
->private;
1214 if (private->number
!= tmp
.num_counters
) {
1216 goto unlock_up_free
;
1220 addend
= xt_write_recseq_begin();
1221 xt_entry_foreach(iter
, private->entries
, private->size
) {
1222 struct xt_counters
*tmp
;
1224 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1225 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1228 xt_write_recseq_end(addend
);
1239 #ifdef CONFIG_COMPAT
1240 struct compat_ip6t_replace
{
1241 char name
[XT_TABLE_MAXNAMELEN
];
1245 u32 hook_entry
[NF_INET_NUMHOOKS
];
1246 u32 underflow
[NF_INET_NUMHOOKS
];
1248 compat_uptr_t counters
; /* struct xt_counters * */
1249 struct compat_ip6t_entry entries
[0];
1253 compat_copy_entry_to_user(struct ip6t_entry
*e
, void __user
**dstptr
,
1254 unsigned int *size
, struct xt_counters
*counters
,
1257 struct xt_entry_target
*t
;
1258 struct compat_ip6t_entry __user
*ce
;
1259 u_int16_t target_offset
, next_offset
;
1260 compat_uint_t origsize
;
1261 const struct xt_entry_match
*ematch
;
1266 if (copy_to_user(ce
, e
, sizeof(struct ip6t_entry
)) != 0 ||
1267 copy_to_user(&ce
->counters
, &counters
[i
],
1268 sizeof(counters
[i
])) != 0)
1271 *dstptr
+= sizeof(struct compat_ip6t_entry
);
1272 *size
-= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1274 xt_ematch_foreach(ematch
, e
) {
1275 ret
= xt_compat_match_to_user(ematch
, dstptr
, size
);
1279 target_offset
= e
->target_offset
- (origsize
- *size
);
1280 t
= ip6t_get_target(e
);
1281 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1284 next_offset
= e
->next_offset
- (origsize
- *size
);
1285 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1286 put_user(next_offset
, &ce
->next_offset
) != 0)
1292 compat_find_calc_match(struct xt_entry_match
*m
,
1293 const struct ip6t_ip6
*ipv6
,
1296 struct xt_match
*match
;
1298 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
1299 m
->u
.user
.revision
);
1301 return PTR_ERR(match
);
1303 m
->u
.kernel
.match
= match
;
1304 *size
+= xt_compat_match_offset(match
);
1308 static void compat_release_entry(struct compat_ip6t_entry
*e
)
1310 struct xt_entry_target
*t
;
1311 struct xt_entry_match
*ematch
;
1313 /* Cleanup all matches */
1314 xt_ematch_foreach(ematch
, e
)
1315 module_put(ematch
->u
.kernel
.match
->me
);
1316 t
= compat_ip6t_get_target(e
);
1317 module_put(t
->u
.kernel
.target
->me
);
1321 check_compat_entry_size_and_hooks(struct compat_ip6t_entry
*e
,
1322 struct xt_table_info
*newinfo
,
1324 const unsigned char *base
,
1325 const unsigned char *limit
)
1327 struct xt_entry_match
*ematch
;
1328 struct xt_entry_target
*t
;
1329 struct xt_target
*target
;
1330 unsigned int entry_offset
;
1334 if ((unsigned long)e
% __alignof__(struct compat_ip6t_entry
) != 0 ||
1335 (unsigned char *)e
+ sizeof(struct compat_ip6t_entry
) >= limit
||
1336 (unsigned char *)e
+ e
->next_offset
> limit
)
1339 if (e
->next_offset
< sizeof(struct compat_ip6t_entry
) +
1340 sizeof(struct compat_xt_entry_target
))
1343 if (!ip6_checkentry(&e
->ipv6
))
1346 ret
= xt_compat_check_entry_offsets(e
, e
->elems
,
1347 e
->target_offset
, e
->next_offset
);
1351 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1352 entry_offset
= (void *)e
- (void *)base
;
1354 xt_ematch_foreach(ematch
, e
) {
1355 ret
= compat_find_calc_match(ematch
, &e
->ipv6
, &off
);
1357 goto release_matches
;
1361 t
= compat_ip6t_get_target(e
);
1362 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
1363 t
->u
.user
.revision
);
1364 if (IS_ERR(target
)) {
1365 ret
= PTR_ERR(target
);
1366 goto release_matches
;
1368 t
->u
.kernel
.target
= target
;
1370 off
+= xt_compat_target_offset(target
);
1372 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
1379 module_put(t
->u
.kernel
.target
->me
);
1381 xt_ematch_foreach(ematch
, e
) {
1384 module_put(ematch
->u
.kernel
.match
->me
);
1390 compat_copy_entry_from_user(struct compat_ip6t_entry
*e
, void **dstptr
,
1392 struct xt_table_info
*newinfo
, unsigned char *base
)
1394 struct xt_entry_target
*t
;
1395 struct ip6t_entry
*de
;
1396 unsigned int origsize
;
1398 struct xt_entry_match
*ematch
;
1402 memcpy(de
, e
, sizeof(struct ip6t_entry
));
1403 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1405 *dstptr
+= sizeof(struct ip6t_entry
);
1406 *size
+= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1408 xt_ematch_foreach(ematch
, e
)
1409 xt_compat_match_from_user(ematch
, dstptr
, size
);
1411 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1412 t
= compat_ip6t_get_target(e
);
1413 xt_compat_target_from_user(t
, dstptr
, size
);
1415 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1416 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
1417 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1418 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1419 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1420 newinfo
->underflow
[h
] -= origsize
- *size
;
1425 translate_compat_table(struct net
*net
,
1426 struct xt_table_info
**pinfo
,
1428 const struct compat_ip6t_replace
*compatr
)
1431 struct xt_table_info
*newinfo
, *info
;
1432 void *pos
, *entry0
, *entry1
;
1433 struct compat_ip6t_entry
*iter0
;
1434 struct ip6t_replace repl
;
1440 size
= compatr
->size
;
1441 info
->number
= compatr
->num_entries
;
1444 xt_compat_lock(AF_INET6
);
1445 xt_compat_init_offsets(AF_INET6
, compatr
->num_entries
);
1446 /* Walk through entries, checking offsets. */
1447 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1448 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1450 entry0
+ compatr
->size
);
1457 if (j
!= compatr
->num_entries
)
1461 newinfo
= xt_alloc_table_info(size
);
1465 newinfo
->number
= compatr
->num_entries
;
1466 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1467 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1468 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1470 entry1
= newinfo
->entries
;
1472 size
= compatr
->size
;
1473 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1474 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1477 /* all module references in entry0 are now gone. */
1478 xt_compat_flush_offsets(AF_INET6
);
1479 xt_compat_unlock(AF_INET6
);
1481 memcpy(&repl
, compatr
, sizeof(*compatr
));
1483 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1484 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1485 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1488 repl
.num_counters
= 0;
1489 repl
.counters
= NULL
;
1490 repl
.size
= newinfo
->size
;
1491 ret
= translate_table(net
, newinfo
, entry1
, &repl
);
1497 xt_free_table_info(info
);
1501 xt_free_table_info(newinfo
);
1504 xt_compat_flush_offsets(AF_INET6
);
1505 xt_compat_unlock(AF_INET6
);
1506 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1509 compat_release_entry(iter0
);
1515 compat_do_replace(struct net
*net
, void __user
*user
, unsigned int len
)
1518 struct compat_ip6t_replace tmp
;
1519 struct xt_table_info
*newinfo
;
1520 void *loc_cpu_entry
;
1521 struct ip6t_entry
*iter
;
1523 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1526 /* overflow check */
1527 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1529 if (tmp
.num_counters
== 0)
1532 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1534 newinfo
= xt_alloc_table_info(tmp
.size
);
1538 loc_cpu_entry
= newinfo
->entries
;
1539 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1545 ret
= translate_compat_table(net
, &newinfo
, &loc_cpu_entry
, &tmp
);
1549 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1550 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1552 goto free_newinfo_untrans
;
1555 free_newinfo_untrans
:
1556 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1557 cleanup_entry(iter
, net
);
1559 xt_free_table_info(newinfo
);
1564 compat_do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1569 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1573 case IP6T_SO_SET_REPLACE
:
1574 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1577 case IP6T_SO_SET_ADD_COUNTERS
:
1578 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1588 struct compat_ip6t_get_entries
{
1589 char name
[XT_TABLE_MAXNAMELEN
];
1591 struct compat_ip6t_entry entrytable
[0];
1595 compat_copy_entries_to_user(unsigned int total_size
, struct xt_table
*table
,
1596 void __user
*userptr
)
1598 struct xt_counters
*counters
;
1599 const struct xt_table_info
*private = table
->private;
1604 struct ip6t_entry
*iter
;
1606 counters
= alloc_counters(table
);
1607 if (IS_ERR(counters
))
1608 return PTR_ERR(counters
);
1612 xt_entry_foreach(iter
, private->entries
, total_size
) {
1613 ret
= compat_copy_entry_to_user(iter
, &pos
,
1614 &size
, counters
, i
++);
1624 compat_get_entries(struct net
*net
, struct compat_ip6t_get_entries __user
*uptr
,
1628 struct compat_ip6t_get_entries get
;
1631 if (*len
< sizeof(get
))
1634 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1637 if (*len
!= sizeof(struct compat_ip6t_get_entries
) + get
.size
)
1640 get
.name
[sizeof(get
.name
) - 1] = '\0';
1642 xt_compat_lock(AF_INET6
);
1643 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1645 const struct xt_table_info
*private = t
->private;
1646 struct xt_table_info info
;
1647 ret
= compat_table_info(private, &info
);
1648 if (!ret
&& get
.size
== info
.size
)
1649 ret
= compat_copy_entries_to_user(private->size
,
1650 t
, uptr
->entrytable
);
1654 xt_compat_flush_offsets(AF_INET6
);
1660 xt_compat_unlock(AF_INET6
);
1664 static int do_ip6t_get_ctl(struct sock
*, int, void __user
*, int *);
1667 compat_do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1671 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1675 case IP6T_SO_GET_INFO
:
1676 ret
= get_info(sock_net(sk
), user
, len
, 1);
1678 case IP6T_SO_GET_ENTRIES
:
1679 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1682 ret
= do_ip6t_get_ctl(sk
, cmd
, user
, len
);
1689 do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1693 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1697 case IP6T_SO_SET_REPLACE
:
1698 ret
= do_replace(sock_net(sk
), user
, len
);
1701 case IP6T_SO_SET_ADD_COUNTERS
:
1702 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1713 do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1717 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1721 case IP6T_SO_GET_INFO
:
1722 ret
= get_info(sock_net(sk
), user
, len
, 0);
1725 case IP6T_SO_GET_ENTRIES
:
1726 ret
= get_entries(sock_net(sk
), user
, len
);
1729 case IP6T_SO_GET_REVISION_MATCH
:
1730 case IP6T_SO_GET_REVISION_TARGET
: {
1731 struct xt_get_revision rev
;
1734 if (*len
!= sizeof(rev
)) {
1738 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1742 rev
.name
[sizeof(rev
.name
)-1] = 0;
1744 if (cmd
== IP6T_SO_GET_REVISION_TARGET
)
1749 try_then_request_module(xt_find_revision(AF_INET6
, rev
.name
,
1752 "ip6t_%s", rev
.name
);
1763 static void __ip6t_unregister_table(struct net
*net
, struct xt_table
*table
)
1765 struct xt_table_info
*private;
1766 void *loc_cpu_entry
;
1767 struct module
*table_owner
= table
->me
;
1768 struct ip6t_entry
*iter
;
1770 private = xt_unregister_table(table
);
1772 /* Decrease module usage counts and free resources */
1773 loc_cpu_entry
= private->entries
;
1774 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1775 cleanup_entry(iter
, net
);
1776 if (private->number
> private->initial_entries
)
1777 module_put(table_owner
);
1778 xt_free_table_info(private);
1781 int ip6t_register_table(struct net
*net
, const struct xt_table
*table
,
1782 const struct ip6t_replace
*repl
,
1783 const struct nf_hook_ops
*ops
,
1784 struct xt_table
**res
)
1787 struct xt_table_info
*newinfo
;
1788 struct xt_table_info bootstrap
= {0};
1789 void *loc_cpu_entry
;
1790 struct xt_table
*new_table
;
1792 newinfo
= xt_alloc_table_info(repl
->size
);
1796 loc_cpu_entry
= newinfo
->entries
;
1797 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1799 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, repl
);
1803 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1804 if (IS_ERR(new_table
)) {
1805 ret
= PTR_ERR(new_table
);
1809 /* set res now, will see skbs right after nf_register_net_hooks */
1810 WRITE_ONCE(*res
, new_table
);
1812 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1814 __ip6t_unregister_table(net
, new_table
);
1821 xt_free_table_info(newinfo
);
1825 void ip6t_unregister_table(struct net
*net
, struct xt_table
*table
,
1826 const struct nf_hook_ops
*ops
)
1828 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1829 __ip6t_unregister_table(net
, table
);
1832 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1834 icmp6_type_code_match(u_int8_t test_type
, u_int8_t min_code
, u_int8_t max_code
,
1835 u_int8_t type
, u_int8_t code
,
1838 return (type
== test_type
&& code
>= min_code
&& code
<= max_code
)
1843 icmp6_match(const struct sk_buff
*skb
, struct xt_action_param
*par
)
1845 const struct icmp6hdr
*ic
;
1846 struct icmp6hdr _icmph
;
1847 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1849 /* Must not be a fragment. */
1850 if (par
->fragoff
!= 0)
1853 ic
= skb_header_pointer(skb
, par
->thoff
, sizeof(_icmph
), &_icmph
);
1855 /* We've been asked to examine this packet, and we
1856 * can't. Hence, no choice but to drop.
1858 par
->hotdrop
= true;
1862 return icmp6_type_code_match(icmpinfo
->type
,
1865 ic
->icmp6_type
, ic
->icmp6_code
,
1866 !!(icmpinfo
->invflags
&IP6T_ICMP_INV
));
1869 /* Called when user tries to insert an entry of this type. */
1870 static int icmp6_checkentry(const struct xt_mtchk_param
*par
)
1872 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1874 /* Must specify no unknown invflags */
1875 return (icmpinfo
->invflags
& ~IP6T_ICMP_INV
) ? -EINVAL
: 0;
1878 /* The built-in targets: standard (NULL) and error. */
1879 static struct xt_target ip6t_builtin_tg
[] __read_mostly
= {
1881 .name
= XT_STANDARD_TARGET
,
1882 .targetsize
= sizeof(int),
1883 .family
= NFPROTO_IPV6
,
1884 #ifdef CONFIG_COMPAT
1885 .compatsize
= sizeof(compat_int_t
),
1886 .compat_from_user
= compat_standard_from_user
,
1887 .compat_to_user
= compat_standard_to_user
,
1891 .name
= XT_ERROR_TARGET
,
1892 .target
= ip6t_error
,
1893 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1894 .family
= NFPROTO_IPV6
,
1898 static struct nf_sockopt_ops ip6t_sockopts
= {
1900 .set_optmin
= IP6T_BASE_CTL
,
1901 .set_optmax
= IP6T_SO_SET_MAX
+1,
1902 .set
= do_ip6t_set_ctl
,
1903 #ifdef CONFIG_COMPAT
1904 .compat_set
= compat_do_ip6t_set_ctl
,
1906 .get_optmin
= IP6T_BASE_CTL
,
1907 .get_optmax
= IP6T_SO_GET_MAX
+1,
1908 .get
= do_ip6t_get_ctl
,
1909 #ifdef CONFIG_COMPAT
1910 .compat_get
= compat_do_ip6t_get_ctl
,
1912 .owner
= THIS_MODULE
,
1915 static struct xt_match ip6t_builtin_mt
[] __read_mostly
= {
1918 .match
= icmp6_match
,
1919 .matchsize
= sizeof(struct ip6t_icmp
),
1920 .checkentry
= icmp6_checkentry
,
1921 .proto
= IPPROTO_ICMPV6
,
1922 .family
= NFPROTO_IPV6
,
1926 static int __net_init
ip6_tables_net_init(struct net
*net
)
1928 return xt_proto_init(net
, NFPROTO_IPV6
);
1931 static void __net_exit
ip6_tables_net_exit(struct net
*net
)
1933 xt_proto_fini(net
, NFPROTO_IPV6
);
1936 static struct pernet_operations ip6_tables_net_ops
= {
1937 .init
= ip6_tables_net_init
,
1938 .exit
= ip6_tables_net_exit
,
1941 static int __init
ip6_tables_init(void)
1945 ret
= register_pernet_subsys(&ip6_tables_net_ops
);
1949 /* No one else will be downing sem now, so we won't sleep */
1950 ret
= xt_register_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1953 ret
= xt_register_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1957 /* Register setsockopt */
1958 ret
= nf_register_sockopt(&ip6t_sockopts
);
1962 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1966 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1968 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1970 unregister_pernet_subsys(&ip6_tables_net_ops
);
1975 static void __exit
ip6_tables_fini(void)
1977 nf_unregister_sockopt(&ip6t_sockopts
);
1979 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1980 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1981 unregister_pernet_subsys(&ip6_tables_net_ops
);
1984 EXPORT_SYMBOL(ip6t_register_table
);
1985 EXPORT_SYMBOL(ip6t_unregister_table
);
1986 EXPORT_SYMBOL(ip6t_do_table
);
1988 module_init(ip6_tables_init
);
1989 module_exit(ip6_tables_fini
);