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 void *ip6t_alloc_initial_table(const struct xt_table
*info
)
44 return xt_alloc_initial_table(ip6t
, IP6T
);
46 EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table
);
48 /* Returns whether matches rule or not. */
49 /* Performance critical - called for every packet */
51 ip6_packet_match(const struct sk_buff
*skb
,
54 const struct ip6t_ip6
*ip6info
,
55 unsigned int *protoff
,
56 int *fragoff
, bool *hotdrop
)
59 const struct ipv6hdr
*ipv6
= ipv6_hdr(skb
);
61 if (NF_INVF(ip6info
, IP6T_INV_SRCIP
,
62 ipv6_masked_addr_cmp(&ipv6
->saddr
, &ip6info
->smsk
,
64 NF_INVF(ip6info
, IP6T_INV_DSTIP
,
65 ipv6_masked_addr_cmp(&ipv6
->daddr
, &ip6info
->dmsk
,
69 ret
= ifname_compare_aligned(indev
, ip6info
->iniface
, ip6info
->iniface_mask
);
71 if (NF_INVF(ip6info
, IP6T_INV_VIA_IN
, ret
!= 0))
74 ret
= ifname_compare_aligned(outdev
, ip6info
->outiface
, ip6info
->outiface_mask
);
76 if (NF_INVF(ip6info
, IP6T_INV_VIA_OUT
, ret
!= 0))
79 /* ... might want to do something with class and flowlabel here ... */
81 /* look for the desired protocol header */
82 if (ip6info
->flags
& IP6T_F_PROTO
) {
84 unsigned short _frag_off
;
86 protohdr
= ipv6_find_hdr(skb
, protoff
, -1, &_frag_off
, NULL
);
94 if (ip6info
->proto
== protohdr
) {
95 if (ip6info
->invflags
& IP6T_INV_PROTO
)
101 /* We need match for the '-p all', too! */
102 if ((ip6info
->proto
!= 0) &&
103 !(ip6info
->invflags
& IP6T_INV_PROTO
))
109 /* should be ip6 safe */
111 ip6_checkentry(const struct ip6t_ip6
*ipv6
)
113 if (ipv6
->flags
& ~IP6T_F_MASK
)
115 if (ipv6
->invflags
& ~IP6T_INV_MASK
)
122 ip6t_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
124 net_info_ratelimited("error: `%s'\n", (const char *)par
->targinfo
);
129 static inline struct ip6t_entry
*
130 get_entry(const void *base
, unsigned int offset
)
132 return (struct ip6t_entry
*)(base
+ offset
);
135 /* All zeroes == unconditional rule. */
136 /* Mildly perf critical (only if packet tracing is on) */
137 static inline bool unconditional(const struct ip6t_entry
*e
)
139 static const struct ip6t_ip6 uncond
;
141 return e
->target_offset
== sizeof(struct ip6t_entry
) &&
142 memcmp(&e
->ipv6
, &uncond
, sizeof(uncond
)) == 0;
145 static inline const struct xt_entry_target
*
146 ip6t_get_target_c(const struct ip6t_entry
*e
)
148 return ip6t_get_target((struct ip6t_entry
*)e
);
151 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
152 /* This cries for unification! */
153 static const char *const hooknames
[] = {
154 [NF_INET_PRE_ROUTING
] = "PREROUTING",
155 [NF_INET_LOCAL_IN
] = "INPUT",
156 [NF_INET_FORWARD
] = "FORWARD",
157 [NF_INET_LOCAL_OUT
] = "OUTPUT",
158 [NF_INET_POST_ROUTING
] = "POSTROUTING",
161 enum nf_ip_trace_comments
{
162 NF_IP6_TRACE_COMMENT_RULE
,
163 NF_IP6_TRACE_COMMENT_RETURN
,
164 NF_IP6_TRACE_COMMENT_POLICY
,
167 static const char *const comments
[] = {
168 [NF_IP6_TRACE_COMMENT_RULE
] = "rule",
169 [NF_IP6_TRACE_COMMENT_RETURN
] = "return",
170 [NF_IP6_TRACE_COMMENT_POLICY
] = "policy",
173 static const struct nf_loginfo trace_loginfo
= {
174 .type
= NF_LOG_TYPE_LOG
,
177 .level
= LOGLEVEL_WARNING
,
178 .logflags
= NF_LOG_DEFAULT_MASK
,
183 /* Mildly perf critical (only if packet tracing is on) */
185 get_chainname_rulenum(const struct ip6t_entry
*s
, const struct ip6t_entry
*e
,
186 const char *hookname
, const char **chainname
,
187 const char **comment
, unsigned int *rulenum
)
189 const struct xt_standard_target
*t
= (void *)ip6t_get_target_c(s
);
191 if (strcmp(t
->target
.u
.kernel
.target
->name
, XT_ERROR_TARGET
) == 0) {
192 /* Head of user chain: ERROR target with chainname */
193 *chainname
= t
->target
.data
;
198 if (unconditional(s
) &&
199 strcmp(t
->target
.u
.kernel
.target
->name
,
200 XT_STANDARD_TARGET
) == 0 &&
202 /* Tail of chains: STANDARD target (return/policy) */
203 *comment
= *chainname
== hookname
204 ? comments
[NF_IP6_TRACE_COMMENT_POLICY
]
205 : comments
[NF_IP6_TRACE_COMMENT_RETURN
];
214 static void trace_packet(struct net
*net
,
215 const struct sk_buff
*skb
,
217 const struct net_device
*in
,
218 const struct net_device
*out
,
219 const char *tablename
,
220 const struct xt_table_info
*private,
221 const struct ip6t_entry
*e
)
223 const struct ip6t_entry
*root
;
224 const char *hookname
, *chainname
, *comment
;
225 const struct ip6t_entry
*iter
;
226 unsigned int rulenum
= 0;
228 root
= get_entry(private->entries
, private->hook_entry
[hook
]);
230 hookname
= chainname
= hooknames
[hook
];
231 comment
= comments
[NF_IP6_TRACE_COMMENT_RULE
];
233 xt_entry_foreach(iter
, root
, private->size
- private->hook_entry
[hook
])
234 if (get_chainname_rulenum(iter
, e
, hookname
,
235 &chainname
, &comment
, &rulenum
) != 0)
238 nf_log_trace(net
, AF_INET6
, hook
, skb
, in
, out
, &trace_loginfo
,
239 "TRACE: %s:%s:%s:%u ",
240 tablename
, chainname
, comment
, rulenum
);
244 static inline struct ip6t_entry
*
245 ip6t_next_entry(const struct ip6t_entry
*entry
)
247 return (void *)entry
+ entry
->next_offset
;
250 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
252 ip6t_do_table(struct sk_buff
*skb
,
253 const struct nf_hook_state
*state
,
254 struct xt_table
*table
)
256 unsigned int hook
= state
->hook
;
257 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
258 /* Initializing verdict to NF_DROP keeps gcc happy. */
259 unsigned int verdict
= NF_DROP
;
260 const char *indev
, *outdev
;
261 const void *table_base
;
262 struct ip6t_entry
*e
, **jumpstack
;
263 unsigned int stackidx
, cpu
;
264 const struct xt_table_info
*private;
265 struct xt_action_param acpar
;
270 indev
= state
->in
? state
->in
->name
: nulldevname
;
271 outdev
= state
->out
? state
->out
->name
: nulldevname
;
272 /* We handle fragments by dealing with the first fragment as
273 * if it was a normal packet. All other fragments are treated
274 * normally, except that they will NEVER match rules that ask
275 * things we don't know, ie. tcp syn flag or ports). If the
276 * rule is also a fragment-specific rule, non-fragments won't
278 acpar
.hotdrop
= false;
281 WARN_ON(!(table
->valid_hooks
& (1 << hook
)));
284 addend
= xt_write_recseq_begin();
285 private = table
->private;
287 * Ensure we load private-> members after we've fetched the base
290 smp_read_barrier_depends();
291 cpu
= smp_processor_id();
292 table_base
= private->entries
;
293 jumpstack
= (struct ip6t_entry
**)private->jumpstack
[cpu
];
295 /* Switch to alternate jumpstack if we're being invoked via TEE.
296 * TEE issues XT_CONTINUE verdict on original skb so we must not
297 * clobber the jumpstack.
299 * For recursion via REJECT or SYNPROXY the stack will be clobbered
300 * but it is no problem since absolute verdict is issued by these.
302 if (static_key_false(&xt_tee_enabled
))
303 jumpstack
+= private->stacksize
* __this_cpu_read(nf_skb_duplicated
);
305 e
= get_entry(table_base
, private->hook_entry
[hook
]);
308 const struct xt_entry_target
*t
;
309 const struct xt_entry_match
*ematch
;
310 struct xt_counters
*counter
;
314 if (!ip6_packet_match(skb
, indev
, outdev
, &e
->ipv6
,
315 &acpar
.thoff
, &acpar
.fragoff
, &acpar
.hotdrop
)) {
317 e
= ip6t_next_entry(e
);
321 xt_ematch_foreach(ematch
, e
) {
322 acpar
.match
= ematch
->u
.kernel
.match
;
323 acpar
.matchinfo
= ematch
->data
;
324 if (!acpar
.match
->match(skb
, &acpar
))
328 counter
= xt_get_this_cpu_counter(&e
->counters
);
329 ADD_COUNTER(*counter
, skb
->len
, 1);
331 t
= ip6t_get_target_c(e
);
332 WARN_ON(!t
->u
.kernel
.target
);
334 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
335 /* The packet is traced: log it */
336 if (unlikely(skb
->nf_trace
))
337 trace_packet(state
->net
, skb
, hook
, state
->in
,
338 state
->out
, table
->name
, private, e
);
340 /* Standard target? */
341 if (!t
->u
.kernel
.target
->target
) {
344 v
= ((struct xt_standard_target
*)t
)->verdict
;
346 /* Pop from stack? */
347 if (v
!= XT_RETURN
) {
348 verdict
= (unsigned int)(-v
) - 1;
352 e
= get_entry(table_base
,
353 private->underflow
[hook
]);
355 e
= ip6t_next_entry(jumpstack
[--stackidx
]);
358 if (table_base
+ v
!= ip6t_next_entry(e
) &&
359 !(e
->ipv6
.flags
& IP6T_F_GOTO
)) {
360 if (unlikely(stackidx
>= private->stacksize
)) {
364 jumpstack
[stackidx
++] = e
;
367 e
= get_entry(table_base
, v
);
371 acpar
.target
= t
->u
.kernel
.target
;
372 acpar
.targinfo
= t
->data
;
374 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
375 if (verdict
== XT_CONTINUE
)
376 e
= ip6t_next_entry(e
);
380 } while (!acpar
.hotdrop
);
382 xt_write_recseq_end(addend
);
390 /* Figures out from what hook each rule can be called: returns 0 if
391 there are loops. Puts hook bitmask in comefrom. */
393 mark_source_chains(const struct xt_table_info
*newinfo
,
394 unsigned int valid_hooks
, void *entry0
,
395 unsigned int *offsets
)
399 /* No recursion; use packet counter to save back ptrs (reset
400 to 0 as we leave), and comefrom to save source hook bitmask */
401 for (hook
= 0; hook
< NF_INET_NUMHOOKS
; hook
++) {
402 unsigned int pos
= newinfo
->hook_entry
[hook
];
403 struct ip6t_entry
*e
= entry0
+ pos
;
405 if (!(valid_hooks
& (1 << hook
)))
408 /* Set initial back pointer. */
409 e
->counters
.pcnt
= pos
;
412 const struct xt_standard_target
*t
413 = (void *)ip6t_get_target_c(e
);
414 int visited
= e
->comefrom
& (1 << hook
);
416 if (e
->comefrom
& (1 << NF_INET_NUMHOOKS
))
419 e
->comefrom
|= ((1 << hook
) | (1 << NF_INET_NUMHOOKS
));
421 /* Unconditional return/END. */
422 if ((unconditional(e
) &&
423 (strcmp(t
->target
.u
.user
.name
,
424 XT_STANDARD_TARGET
) == 0) &&
425 t
->verdict
< 0) || visited
) {
426 unsigned int oldpos
, size
;
428 if ((strcmp(t
->target
.u
.user
.name
,
429 XT_STANDARD_TARGET
) == 0) &&
430 t
->verdict
< -NF_MAX_VERDICT
- 1)
433 /* Return: backtrack through the last
436 e
->comefrom
^= (1<<NF_INET_NUMHOOKS
);
438 pos
= e
->counters
.pcnt
;
439 e
->counters
.pcnt
= 0;
441 /* We're at the start. */
446 } while (oldpos
== pos
+ e
->next_offset
);
449 size
= e
->next_offset
;
450 e
= entry0
+ pos
+ size
;
451 if (pos
+ size
>= newinfo
->size
)
453 e
->counters
.pcnt
= pos
;
456 int newpos
= t
->verdict
;
458 if (strcmp(t
->target
.u
.user
.name
,
459 XT_STANDARD_TARGET
) == 0 &&
461 /* This a jump; chase it. */
462 if (!xt_find_jump_offset(offsets
, newpos
,
467 /* ... this is a fallthru */
468 newpos
= pos
+ e
->next_offset
;
469 if (newpos
>= newinfo
->size
)
473 e
->counters
.pcnt
= pos
;
482 static void cleanup_match(struct xt_entry_match
*m
, struct net
*net
)
484 struct xt_mtdtor_param par
;
487 par
.match
= m
->u
.kernel
.match
;
488 par
.matchinfo
= m
->data
;
489 par
.family
= NFPROTO_IPV6
;
490 if (par
.match
->destroy
!= NULL
)
491 par
.match
->destroy(&par
);
492 module_put(par
.match
->me
);
495 static int check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
497 const struct ip6t_ip6
*ipv6
= par
->entryinfo
;
499 par
->match
= m
->u
.kernel
.match
;
500 par
->matchinfo
= m
->data
;
502 return xt_check_match(par
, m
->u
.match_size
- sizeof(*m
),
503 ipv6
->proto
, ipv6
->invflags
& IP6T_INV_PROTO
);
507 find_check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
509 struct xt_match
*match
;
512 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
515 return PTR_ERR(match
);
517 m
->u
.kernel
.match
= match
;
519 ret
= check_match(m
, par
);
525 module_put(m
->u
.kernel
.match
->me
);
529 static int check_target(struct ip6t_entry
*e
, struct net
*net
, const char *name
)
531 struct xt_entry_target
*t
= ip6t_get_target(e
);
532 struct xt_tgchk_param par
= {
536 .target
= t
->u
.kernel
.target
,
538 .hook_mask
= e
->comefrom
,
539 .family
= NFPROTO_IPV6
,
542 t
= ip6t_get_target(e
);
543 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
),
545 e
->ipv6
.invflags
& IP6T_INV_PROTO
);
549 find_check_entry(struct ip6t_entry
*e
, struct net
*net
, const char *name
,
551 struct xt_percpu_counter_alloc_state
*alloc_state
)
553 struct xt_entry_target
*t
;
554 struct xt_target
*target
;
557 struct xt_mtchk_param mtpar
;
558 struct xt_entry_match
*ematch
;
560 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
564 memset(&mtpar
, 0, sizeof(mtpar
));
567 mtpar
.entryinfo
= &e
->ipv6
;
568 mtpar
.hook_mask
= e
->comefrom
;
569 mtpar
.family
= NFPROTO_IPV6
;
570 xt_ematch_foreach(ematch
, e
) {
571 ret
= find_check_match(ematch
, &mtpar
);
573 goto cleanup_matches
;
577 t
= ip6t_get_target(e
);
578 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
580 if (IS_ERR(target
)) {
581 ret
= PTR_ERR(target
);
582 goto cleanup_matches
;
584 t
->u
.kernel
.target
= target
;
586 ret
= check_target(e
, net
, name
);
591 module_put(t
->u
.kernel
.target
->me
);
593 xt_ematch_foreach(ematch
, e
) {
596 cleanup_match(ematch
, net
);
599 xt_percpu_counter_free(&e
->counters
);
604 static bool check_underflow(const struct ip6t_entry
*e
)
606 const struct xt_entry_target
*t
;
607 unsigned int verdict
;
609 if (!unconditional(e
))
611 t
= ip6t_get_target_c(e
);
612 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
614 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
615 verdict
= -verdict
- 1;
616 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
620 check_entry_size_and_hooks(struct ip6t_entry
*e
,
621 struct xt_table_info
*newinfo
,
622 const unsigned char *base
,
623 const unsigned char *limit
,
624 const unsigned int *hook_entries
,
625 const unsigned int *underflows
,
626 unsigned int valid_hooks
)
631 if ((unsigned long)e
% __alignof__(struct ip6t_entry
) != 0 ||
632 (unsigned char *)e
+ sizeof(struct ip6t_entry
) >= limit
||
633 (unsigned char *)e
+ e
->next_offset
> limit
)
637 < sizeof(struct ip6t_entry
) + sizeof(struct xt_entry_target
))
640 if (!ip6_checkentry(&e
->ipv6
))
643 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
648 /* Check hooks & underflows */
649 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
650 if (!(valid_hooks
& (1 << h
)))
652 if ((unsigned char *)e
- base
== hook_entries
[h
])
653 newinfo
->hook_entry
[h
] = hook_entries
[h
];
654 if ((unsigned char *)e
- base
== underflows
[h
]) {
655 if (!check_underflow(e
))
658 newinfo
->underflow
[h
] = underflows
[h
];
662 /* Clear counters and comefrom */
663 e
->counters
= ((struct xt_counters
) { 0, 0 });
668 static void cleanup_entry(struct ip6t_entry
*e
, struct net
*net
)
670 struct xt_tgdtor_param par
;
671 struct xt_entry_target
*t
;
672 struct xt_entry_match
*ematch
;
674 /* Cleanup all matches */
675 xt_ematch_foreach(ematch
, e
)
676 cleanup_match(ematch
, net
);
677 t
= ip6t_get_target(e
);
680 par
.target
= t
->u
.kernel
.target
;
681 par
.targinfo
= t
->data
;
682 par
.family
= NFPROTO_IPV6
;
683 if (par
.target
->destroy
!= NULL
)
684 par
.target
->destroy(&par
);
685 module_put(par
.target
->me
);
686 xt_percpu_counter_free(&e
->counters
);
689 /* Checks and translates the user-supplied table segment (held in
692 translate_table(struct net
*net
, struct xt_table_info
*newinfo
, void *entry0
,
693 const struct ip6t_replace
*repl
)
695 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
696 struct ip6t_entry
*iter
;
697 unsigned int *offsets
;
701 newinfo
->size
= repl
->size
;
702 newinfo
->number
= repl
->num_entries
;
704 /* Init all hooks to impossible value. */
705 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
706 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
707 newinfo
->underflow
[i
] = 0xFFFFFFFF;
710 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
714 /* Walk through entries, checking offsets. */
715 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
716 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
723 if (i
< repl
->num_entries
)
724 offsets
[i
] = (void *)iter
- entry0
;
726 if (strcmp(ip6t_get_target(iter
)->u
.user
.name
,
727 XT_ERROR_TARGET
) == 0)
728 ++newinfo
->stacksize
;
732 if (i
!= repl
->num_entries
)
735 /* Check hooks all assigned */
736 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
737 /* Only hooks which are valid */
738 if (!(repl
->valid_hooks
& (1 << i
)))
740 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
742 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
746 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
752 /* Finally, each sanity check must pass */
754 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
755 ret
= find_check_entry(iter
, net
, repl
->name
, repl
->size
,
763 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
766 cleanup_entry(iter
, net
);
778 get_counters(const struct xt_table_info
*t
,
779 struct xt_counters counters
[])
781 struct ip6t_entry
*iter
;
785 for_each_possible_cpu(cpu
) {
786 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
789 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
790 struct xt_counters
*tmp
;
794 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
796 start
= read_seqcount_begin(s
);
799 } while (read_seqcount_retry(s
, start
));
801 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
808 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
810 unsigned int countersize
;
811 struct xt_counters
*counters
;
812 const struct xt_table_info
*private = table
->private;
814 /* We need atomic snapshot of counters: rest doesn't change
815 (other than comefrom, which userspace doesn't care
817 countersize
= sizeof(struct xt_counters
) * private->number
;
818 counters
= vzalloc(countersize
);
820 if (counters
== NULL
)
821 return ERR_PTR(-ENOMEM
);
823 get_counters(private, counters
);
829 copy_entries_to_user(unsigned int total_size
,
830 const struct xt_table
*table
,
831 void __user
*userptr
)
833 unsigned int off
, num
;
834 const struct ip6t_entry
*e
;
835 struct xt_counters
*counters
;
836 const struct xt_table_info
*private = table
->private;
838 const void *loc_cpu_entry
;
840 counters
= alloc_counters(table
);
841 if (IS_ERR(counters
))
842 return PTR_ERR(counters
);
844 loc_cpu_entry
= private->entries
;
846 /* FIXME: use iterator macros --RR */
847 /* ... then go back and fix counters and names */
848 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
850 const struct xt_entry_match
*m
;
851 const struct xt_entry_target
*t
;
853 e
= loc_cpu_entry
+ off
;
854 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
858 if (copy_to_user(userptr
+ off
859 + offsetof(struct ip6t_entry
, counters
),
861 sizeof(counters
[num
])) != 0) {
866 for (i
= sizeof(struct ip6t_entry
);
867 i
< e
->target_offset
;
868 i
+= m
->u
.match_size
) {
871 if (xt_match_to_user(m
, userptr
+ off
+ i
)) {
877 t
= ip6t_get_target_c(e
);
878 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
890 static void compat_standard_from_user(void *dst
, const void *src
)
892 int v
= *(compat_int_t
*)src
;
895 v
+= xt_compat_calc_jump(AF_INET6
, v
);
896 memcpy(dst
, &v
, sizeof(v
));
899 static int compat_standard_to_user(void __user
*dst
, const void *src
)
901 compat_int_t cv
= *(int *)src
;
904 cv
-= xt_compat_calc_jump(AF_INET6
, cv
);
905 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
908 static int compat_calc_entry(const struct ip6t_entry
*e
,
909 const struct xt_table_info
*info
,
910 const void *base
, struct xt_table_info
*newinfo
)
912 const struct xt_entry_match
*ematch
;
913 const struct xt_entry_target
*t
;
914 unsigned int entry_offset
;
917 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
918 entry_offset
= (void *)e
- base
;
919 xt_ematch_foreach(ematch
, e
)
920 off
+= xt_compat_match_offset(ematch
->u
.kernel
.match
);
921 t
= ip6t_get_target_c(e
);
922 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
923 newinfo
->size
-= off
;
924 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
928 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
929 if (info
->hook_entry
[i
] &&
930 (e
< (struct ip6t_entry
*)(base
+ info
->hook_entry
[i
])))
931 newinfo
->hook_entry
[i
] -= off
;
932 if (info
->underflow
[i
] &&
933 (e
< (struct ip6t_entry
*)(base
+ info
->underflow
[i
])))
934 newinfo
->underflow
[i
] -= off
;
939 static int compat_table_info(const struct xt_table_info
*info
,
940 struct xt_table_info
*newinfo
)
942 struct ip6t_entry
*iter
;
943 const void *loc_cpu_entry
;
946 if (!newinfo
|| !info
)
949 /* we dont care about newinfo->entries */
950 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
951 newinfo
->initial_entries
= 0;
952 loc_cpu_entry
= info
->entries
;
953 ret
= xt_compat_init_offsets(AF_INET6
, info
->number
);
956 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
957 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
965 static int get_info(struct net
*net
, void __user
*user
,
966 const int *len
, int compat
)
968 char name
[XT_TABLE_MAXNAMELEN
];
972 if (*len
!= sizeof(struct ip6t_getinfo
))
975 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
978 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
981 xt_compat_lock(AF_INET6
);
983 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
984 "ip6table_%s", name
);
986 struct ip6t_getinfo info
;
987 const struct xt_table_info
*private = t
->private;
989 struct xt_table_info tmp
;
992 ret
= compat_table_info(private, &tmp
);
993 xt_compat_flush_offsets(AF_INET6
);
997 memset(&info
, 0, sizeof(info
));
998 info
.valid_hooks
= t
->valid_hooks
;
999 memcpy(info
.hook_entry
, private->hook_entry
,
1000 sizeof(info
.hook_entry
));
1001 memcpy(info
.underflow
, private->underflow
,
1002 sizeof(info
.underflow
));
1003 info
.num_entries
= private->number
;
1004 info
.size
= private->size
;
1005 strcpy(info
.name
, name
);
1007 if (copy_to_user(user
, &info
, *len
) != 0)
1016 #ifdef CONFIG_COMPAT
1018 xt_compat_unlock(AF_INET6
);
1024 get_entries(struct net
*net
, struct ip6t_get_entries __user
*uptr
,
1028 struct ip6t_get_entries get
;
1031 if (*len
< sizeof(get
))
1033 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1035 if (*len
!= sizeof(struct ip6t_get_entries
) + get
.size
)
1038 get
.name
[sizeof(get
.name
) - 1] = '\0';
1040 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1042 struct xt_table_info
*private = t
->private;
1043 if (get
.size
== private->size
)
1044 ret
= copy_entries_to_user(private->size
,
1045 t
, uptr
->entrytable
);
1058 __do_replace(struct net
*net
, const char *name
, unsigned int valid_hooks
,
1059 struct xt_table_info
*newinfo
, unsigned int num_counters
,
1060 void __user
*counters_ptr
)
1064 struct xt_table_info
*oldinfo
;
1065 struct xt_counters
*counters
;
1066 struct ip6t_entry
*iter
;
1069 counters
= xt_counters_alloc(num_counters
);
1075 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
1076 "ip6table_%s", name
);
1079 goto free_newinfo_counters_untrans
;
1083 if (valid_hooks
!= t
->valid_hooks
) {
1088 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1092 /* Update module usage count based on number of rules */
1093 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1094 (newinfo
->number
<= oldinfo
->initial_entries
))
1096 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1097 (newinfo
->number
<= oldinfo
->initial_entries
))
1100 /* Get the old counters, and synchronize with replace */
1101 get_counters(oldinfo
, counters
);
1103 /* Decrease module usage counts and free resource */
1104 xt_entry_foreach(iter
, oldinfo
->entries
, oldinfo
->size
)
1105 cleanup_entry(iter
, net
);
1107 xt_free_table_info(oldinfo
);
1108 if (copy_to_user(counters_ptr
, counters
,
1109 sizeof(struct xt_counters
) * num_counters
) != 0) {
1110 /* Silent error, can't fail, new table is already in place */
1111 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1120 free_newinfo_counters_untrans
:
1127 do_replace(struct net
*net
, const void __user
*user
, unsigned int len
)
1130 struct ip6t_replace tmp
;
1131 struct xt_table_info
*newinfo
;
1132 void *loc_cpu_entry
;
1133 struct ip6t_entry
*iter
;
1135 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1138 /* overflow check */
1139 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1141 if (tmp
.num_counters
== 0)
1144 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1146 newinfo
= xt_alloc_table_info(tmp
.size
);
1150 loc_cpu_entry
= newinfo
->entries
;
1151 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1157 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, &tmp
);
1161 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1162 tmp
.num_counters
, tmp
.counters
);
1164 goto free_newinfo_untrans
;
1167 free_newinfo_untrans
:
1168 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1169 cleanup_entry(iter
, net
);
1171 xt_free_table_info(newinfo
);
1176 do_add_counters(struct net
*net
, const void __user
*user
, unsigned int len
,
1180 struct xt_counters_info tmp
;
1181 struct xt_counters
*paddc
;
1183 const struct xt_table_info
*private;
1185 struct ip6t_entry
*iter
;
1186 unsigned int addend
;
1188 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1190 return PTR_ERR(paddc
);
1191 t
= xt_find_table_lock(net
, AF_INET6
, tmp
.name
);
1198 private = t
->private;
1199 if (private->number
!= tmp
.num_counters
) {
1201 goto unlock_up_free
;
1205 addend
= xt_write_recseq_begin();
1206 xt_entry_foreach(iter
, private->entries
, private->size
) {
1207 struct xt_counters
*tmp
;
1209 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1210 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1213 xt_write_recseq_end(addend
);
1224 #ifdef CONFIG_COMPAT
1225 struct compat_ip6t_replace
{
1226 char name
[XT_TABLE_MAXNAMELEN
];
1230 u32 hook_entry
[NF_INET_NUMHOOKS
];
1231 u32 underflow
[NF_INET_NUMHOOKS
];
1233 compat_uptr_t counters
; /* struct xt_counters * */
1234 struct compat_ip6t_entry entries
[0];
1238 compat_copy_entry_to_user(struct ip6t_entry
*e
, void __user
**dstptr
,
1239 unsigned int *size
, struct xt_counters
*counters
,
1242 struct xt_entry_target
*t
;
1243 struct compat_ip6t_entry __user
*ce
;
1244 u_int16_t target_offset
, next_offset
;
1245 compat_uint_t origsize
;
1246 const struct xt_entry_match
*ematch
;
1251 if (copy_to_user(ce
, e
, sizeof(struct ip6t_entry
)) != 0 ||
1252 copy_to_user(&ce
->counters
, &counters
[i
],
1253 sizeof(counters
[i
])) != 0)
1256 *dstptr
+= sizeof(struct compat_ip6t_entry
);
1257 *size
-= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1259 xt_ematch_foreach(ematch
, e
) {
1260 ret
= xt_compat_match_to_user(ematch
, dstptr
, size
);
1264 target_offset
= e
->target_offset
- (origsize
- *size
);
1265 t
= ip6t_get_target(e
);
1266 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1269 next_offset
= e
->next_offset
- (origsize
- *size
);
1270 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1271 put_user(next_offset
, &ce
->next_offset
) != 0)
1277 compat_find_calc_match(struct xt_entry_match
*m
,
1278 const struct ip6t_ip6
*ipv6
,
1281 struct xt_match
*match
;
1283 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
1284 m
->u
.user
.revision
);
1286 return PTR_ERR(match
);
1288 m
->u
.kernel
.match
= match
;
1289 *size
+= xt_compat_match_offset(match
);
1293 static void compat_release_entry(struct compat_ip6t_entry
*e
)
1295 struct xt_entry_target
*t
;
1296 struct xt_entry_match
*ematch
;
1298 /* Cleanup all matches */
1299 xt_ematch_foreach(ematch
, e
)
1300 module_put(ematch
->u
.kernel
.match
->me
);
1301 t
= compat_ip6t_get_target(e
);
1302 module_put(t
->u
.kernel
.target
->me
);
1306 check_compat_entry_size_and_hooks(struct compat_ip6t_entry
*e
,
1307 struct xt_table_info
*newinfo
,
1309 const unsigned char *base
,
1310 const unsigned char *limit
)
1312 struct xt_entry_match
*ematch
;
1313 struct xt_entry_target
*t
;
1314 struct xt_target
*target
;
1315 unsigned int entry_offset
;
1319 if ((unsigned long)e
% __alignof__(struct compat_ip6t_entry
) != 0 ||
1320 (unsigned char *)e
+ sizeof(struct compat_ip6t_entry
) >= limit
||
1321 (unsigned char *)e
+ e
->next_offset
> limit
)
1324 if (e
->next_offset
< sizeof(struct compat_ip6t_entry
) +
1325 sizeof(struct compat_xt_entry_target
))
1328 if (!ip6_checkentry(&e
->ipv6
))
1331 ret
= xt_compat_check_entry_offsets(e
, e
->elems
,
1332 e
->target_offset
, e
->next_offset
);
1336 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1337 entry_offset
= (void *)e
- (void *)base
;
1339 xt_ematch_foreach(ematch
, e
) {
1340 ret
= compat_find_calc_match(ematch
, &e
->ipv6
, &off
);
1342 goto release_matches
;
1346 t
= compat_ip6t_get_target(e
);
1347 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
1348 t
->u
.user
.revision
);
1349 if (IS_ERR(target
)) {
1350 ret
= PTR_ERR(target
);
1351 goto release_matches
;
1353 t
->u
.kernel
.target
= target
;
1355 off
+= xt_compat_target_offset(target
);
1357 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
1364 module_put(t
->u
.kernel
.target
->me
);
1366 xt_ematch_foreach(ematch
, e
) {
1369 module_put(ematch
->u
.kernel
.match
->me
);
1375 compat_copy_entry_from_user(struct compat_ip6t_entry
*e
, void **dstptr
,
1377 struct xt_table_info
*newinfo
, unsigned char *base
)
1379 struct xt_entry_target
*t
;
1380 struct ip6t_entry
*de
;
1381 unsigned int origsize
;
1383 struct xt_entry_match
*ematch
;
1387 memcpy(de
, e
, sizeof(struct ip6t_entry
));
1388 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1390 *dstptr
+= sizeof(struct ip6t_entry
);
1391 *size
+= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1393 xt_ematch_foreach(ematch
, e
)
1394 xt_compat_match_from_user(ematch
, dstptr
, size
);
1396 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1397 t
= compat_ip6t_get_target(e
);
1398 xt_compat_target_from_user(t
, dstptr
, size
);
1400 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1401 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
1402 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1403 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1404 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1405 newinfo
->underflow
[h
] -= origsize
- *size
;
1410 translate_compat_table(struct net
*net
,
1411 struct xt_table_info
**pinfo
,
1413 const struct compat_ip6t_replace
*compatr
)
1416 struct xt_table_info
*newinfo
, *info
;
1417 void *pos
, *entry0
, *entry1
;
1418 struct compat_ip6t_entry
*iter0
;
1419 struct ip6t_replace repl
;
1425 size
= compatr
->size
;
1426 info
->number
= compatr
->num_entries
;
1429 xt_compat_lock(AF_INET6
);
1430 ret
= xt_compat_init_offsets(AF_INET6
, compatr
->num_entries
);
1433 /* Walk through entries, checking offsets. */
1434 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1435 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1437 entry0
+ compatr
->size
);
1444 if (j
!= compatr
->num_entries
)
1448 newinfo
= xt_alloc_table_info(size
);
1452 newinfo
->number
= compatr
->num_entries
;
1453 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1454 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1455 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1457 entry1
= newinfo
->entries
;
1459 size
= compatr
->size
;
1460 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1461 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1464 /* all module references in entry0 are now gone. */
1465 xt_compat_flush_offsets(AF_INET6
);
1466 xt_compat_unlock(AF_INET6
);
1468 memcpy(&repl
, compatr
, sizeof(*compatr
));
1470 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1471 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1472 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1475 repl
.num_counters
= 0;
1476 repl
.counters
= NULL
;
1477 repl
.size
= newinfo
->size
;
1478 ret
= translate_table(net
, newinfo
, entry1
, &repl
);
1484 xt_free_table_info(info
);
1488 xt_free_table_info(newinfo
);
1491 xt_compat_flush_offsets(AF_INET6
);
1492 xt_compat_unlock(AF_INET6
);
1493 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1496 compat_release_entry(iter0
);
1502 compat_do_replace(struct net
*net
, void __user
*user
, unsigned int len
)
1505 struct compat_ip6t_replace tmp
;
1506 struct xt_table_info
*newinfo
;
1507 void *loc_cpu_entry
;
1508 struct ip6t_entry
*iter
;
1510 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1513 /* overflow check */
1514 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1516 if (tmp
.num_counters
== 0)
1519 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1521 newinfo
= xt_alloc_table_info(tmp
.size
);
1525 loc_cpu_entry
= newinfo
->entries
;
1526 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1532 ret
= translate_compat_table(net
, &newinfo
, &loc_cpu_entry
, &tmp
);
1536 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1537 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1539 goto free_newinfo_untrans
;
1542 free_newinfo_untrans
:
1543 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1544 cleanup_entry(iter
, net
);
1546 xt_free_table_info(newinfo
);
1551 compat_do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1556 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1560 case IP6T_SO_SET_REPLACE
:
1561 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1564 case IP6T_SO_SET_ADD_COUNTERS
:
1565 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1575 struct compat_ip6t_get_entries
{
1576 char name
[XT_TABLE_MAXNAMELEN
];
1578 struct compat_ip6t_entry entrytable
[0];
1582 compat_copy_entries_to_user(unsigned int total_size
, struct xt_table
*table
,
1583 void __user
*userptr
)
1585 struct xt_counters
*counters
;
1586 const struct xt_table_info
*private = table
->private;
1591 struct ip6t_entry
*iter
;
1593 counters
= alloc_counters(table
);
1594 if (IS_ERR(counters
))
1595 return PTR_ERR(counters
);
1599 xt_entry_foreach(iter
, private->entries
, total_size
) {
1600 ret
= compat_copy_entry_to_user(iter
, &pos
,
1601 &size
, counters
, i
++);
1611 compat_get_entries(struct net
*net
, struct compat_ip6t_get_entries __user
*uptr
,
1615 struct compat_ip6t_get_entries get
;
1618 if (*len
< sizeof(get
))
1621 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1624 if (*len
!= sizeof(struct compat_ip6t_get_entries
) + get
.size
)
1627 get
.name
[sizeof(get
.name
) - 1] = '\0';
1629 xt_compat_lock(AF_INET6
);
1630 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1632 const struct xt_table_info
*private = t
->private;
1633 struct xt_table_info info
;
1634 ret
= compat_table_info(private, &info
);
1635 if (!ret
&& get
.size
== info
.size
)
1636 ret
= compat_copy_entries_to_user(private->size
,
1637 t
, uptr
->entrytable
);
1641 xt_compat_flush_offsets(AF_INET6
);
1647 xt_compat_unlock(AF_INET6
);
1651 static int do_ip6t_get_ctl(struct sock
*, int, void __user
*, int *);
1654 compat_do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1658 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1662 case IP6T_SO_GET_INFO
:
1663 ret
= get_info(sock_net(sk
), user
, len
, 1);
1665 case IP6T_SO_GET_ENTRIES
:
1666 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1669 ret
= do_ip6t_get_ctl(sk
, cmd
, user
, len
);
1676 do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1680 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1684 case IP6T_SO_SET_REPLACE
:
1685 ret
= do_replace(sock_net(sk
), user
, len
);
1688 case IP6T_SO_SET_ADD_COUNTERS
:
1689 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1700 do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1704 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1708 case IP6T_SO_GET_INFO
:
1709 ret
= get_info(sock_net(sk
), user
, len
, 0);
1712 case IP6T_SO_GET_ENTRIES
:
1713 ret
= get_entries(sock_net(sk
), user
, len
);
1716 case IP6T_SO_GET_REVISION_MATCH
:
1717 case IP6T_SO_GET_REVISION_TARGET
: {
1718 struct xt_get_revision rev
;
1721 if (*len
!= sizeof(rev
)) {
1725 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1729 rev
.name
[sizeof(rev
.name
)-1] = 0;
1731 if (cmd
== IP6T_SO_GET_REVISION_TARGET
)
1736 try_then_request_module(xt_find_revision(AF_INET6
, rev
.name
,
1739 "ip6t_%s", rev
.name
);
1750 static void __ip6t_unregister_table(struct net
*net
, struct xt_table
*table
)
1752 struct xt_table_info
*private;
1753 void *loc_cpu_entry
;
1754 struct module
*table_owner
= table
->me
;
1755 struct ip6t_entry
*iter
;
1757 private = xt_unregister_table(table
);
1759 /* Decrease module usage counts and free resources */
1760 loc_cpu_entry
= private->entries
;
1761 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1762 cleanup_entry(iter
, net
);
1763 if (private->number
> private->initial_entries
)
1764 module_put(table_owner
);
1765 xt_free_table_info(private);
1768 int ip6t_register_table(struct net
*net
, const struct xt_table
*table
,
1769 const struct ip6t_replace
*repl
,
1770 const struct nf_hook_ops
*ops
,
1771 struct xt_table
**res
)
1774 struct xt_table_info
*newinfo
;
1775 struct xt_table_info bootstrap
= {0};
1776 void *loc_cpu_entry
;
1777 struct xt_table
*new_table
;
1779 newinfo
= xt_alloc_table_info(repl
->size
);
1783 loc_cpu_entry
= newinfo
->entries
;
1784 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1786 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, repl
);
1790 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1791 if (IS_ERR(new_table
)) {
1792 ret
= PTR_ERR(new_table
);
1796 /* set res now, will see skbs right after nf_register_net_hooks */
1797 WRITE_ONCE(*res
, new_table
);
1799 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1801 __ip6t_unregister_table(net
, new_table
);
1808 xt_free_table_info(newinfo
);
1812 void ip6t_unregister_table(struct net
*net
, struct xt_table
*table
,
1813 const struct nf_hook_ops
*ops
)
1815 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1816 __ip6t_unregister_table(net
, table
);
1819 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1821 icmp6_type_code_match(u_int8_t test_type
, u_int8_t min_code
, u_int8_t max_code
,
1822 u_int8_t type
, u_int8_t code
,
1825 return (type
== test_type
&& code
>= min_code
&& code
<= max_code
)
1830 icmp6_match(const struct sk_buff
*skb
, struct xt_action_param
*par
)
1832 const struct icmp6hdr
*ic
;
1833 struct icmp6hdr _icmph
;
1834 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1836 /* Must not be a fragment. */
1837 if (par
->fragoff
!= 0)
1840 ic
= skb_header_pointer(skb
, par
->thoff
, sizeof(_icmph
), &_icmph
);
1842 /* We've been asked to examine this packet, and we
1843 * can't. Hence, no choice but to drop.
1845 par
->hotdrop
= true;
1849 return icmp6_type_code_match(icmpinfo
->type
,
1852 ic
->icmp6_type
, ic
->icmp6_code
,
1853 !!(icmpinfo
->invflags
&IP6T_ICMP_INV
));
1856 /* Called when user tries to insert an entry of this type. */
1857 static int icmp6_checkentry(const struct xt_mtchk_param
*par
)
1859 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1861 /* Must specify no unknown invflags */
1862 return (icmpinfo
->invflags
& ~IP6T_ICMP_INV
) ? -EINVAL
: 0;
1865 /* The built-in targets: standard (NULL) and error. */
1866 static struct xt_target ip6t_builtin_tg
[] __read_mostly
= {
1868 .name
= XT_STANDARD_TARGET
,
1869 .targetsize
= sizeof(int),
1870 .family
= NFPROTO_IPV6
,
1871 #ifdef CONFIG_COMPAT
1872 .compatsize
= sizeof(compat_int_t
),
1873 .compat_from_user
= compat_standard_from_user
,
1874 .compat_to_user
= compat_standard_to_user
,
1878 .name
= XT_ERROR_TARGET
,
1879 .target
= ip6t_error
,
1880 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1881 .family
= NFPROTO_IPV6
,
1885 static struct nf_sockopt_ops ip6t_sockopts
= {
1887 .set_optmin
= IP6T_BASE_CTL
,
1888 .set_optmax
= IP6T_SO_SET_MAX
+1,
1889 .set
= do_ip6t_set_ctl
,
1890 #ifdef CONFIG_COMPAT
1891 .compat_set
= compat_do_ip6t_set_ctl
,
1893 .get_optmin
= IP6T_BASE_CTL
,
1894 .get_optmax
= IP6T_SO_GET_MAX
+1,
1895 .get
= do_ip6t_get_ctl
,
1896 #ifdef CONFIG_COMPAT
1897 .compat_get
= compat_do_ip6t_get_ctl
,
1899 .owner
= THIS_MODULE
,
1902 static struct xt_match ip6t_builtin_mt
[] __read_mostly
= {
1905 .match
= icmp6_match
,
1906 .matchsize
= sizeof(struct ip6t_icmp
),
1907 .checkentry
= icmp6_checkentry
,
1908 .proto
= IPPROTO_ICMPV6
,
1909 .family
= NFPROTO_IPV6
,
1914 static int __net_init
ip6_tables_net_init(struct net
*net
)
1916 return xt_proto_init(net
, NFPROTO_IPV6
);
1919 static void __net_exit
ip6_tables_net_exit(struct net
*net
)
1921 xt_proto_fini(net
, NFPROTO_IPV6
);
1924 static struct pernet_operations ip6_tables_net_ops
= {
1925 .init
= ip6_tables_net_init
,
1926 .exit
= ip6_tables_net_exit
,
1929 static int __init
ip6_tables_init(void)
1933 ret
= register_pernet_subsys(&ip6_tables_net_ops
);
1937 /* No one else will be downing sem now, so we won't sleep */
1938 ret
= xt_register_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1941 ret
= xt_register_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1945 /* Register setsockopt */
1946 ret
= nf_register_sockopt(&ip6t_sockopts
);
1950 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1954 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1956 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1958 unregister_pernet_subsys(&ip6_tables_net_ops
);
1963 static void __exit
ip6_tables_fini(void)
1965 nf_unregister_sockopt(&ip6t_sockopts
);
1967 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1968 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1969 unregister_pernet_subsys(&ip6_tables_net_ops
);
1972 EXPORT_SYMBOL(ip6t_register_table
);
1973 EXPORT_SYMBOL(ip6t_unregister_table
);
1974 EXPORT_SYMBOL(ip6t_do_table
);
1976 module_init(ip6_tables_init
);
1977 module_exit(ip6_tables_fini
);