1 // SPDX-License-Identifier: GPL-2.0-only
3 * Packet matching code.
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
7 * Copyright (c) 2006-2010 Patrick McHardy <kaber@trash.net>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/capability.h>
15 #include <linux/skbuff.h>
16 #include <linux/kmod.h>
17 #include <linux/vmalloc.h>
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
20 #include <linux/poison.h>
22 #include <net/compat.h>
23 #include <linux/uaccess.h>
24 #include <linux/mutex.h>
25 #include <linux/proc_fs.h>
26 #include <linux/err.h>
27 #include <linux/cpumask.h>
29 #include <linux/netfilter_ipv6/ip6_tables.h>
30 #include <linux/netfilter/x_tables.h>
31 #include <net/netfilter/nf_log.h>
32 #include "../../netfilter/xt_repldata.h"
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
36 MODULE_DESCRIPTION("IPv6 packet filter");
38 void *ip6t_alloc_initial_table(const struct xt_table
*info
)
40 return xt_alloc_initial_table(ip6t
, IP6T
);
42 EXPORT_SYMBOL_GPL(ip6t_alloc_initial_table
);
44 /* Returns whether matches rule or not. */
45 /* Performance critical - called for every packet */
47 ip6_packet_match(const struct sk_buff
*skb
,
50 const struct ip6t_ip6
*ip6info
,
51 unsigned int *protoff
,
52 u16
*fragoff
, bool *hotdrop
)
55 const struct ipv6hdr
*ipv6
= ipv6_hdr(skb
);
57 if (NF_INVF(ip6info
, IP6T_INV_SRCIP
,
58 ipv6_masked_addr_cmp(&ipv6
->saddr
, &ip6info
->smsk
,
60 NF_INVF(ip6info
, IP6T_INV_DSTIP
,
61 ipv6_masked_addr_cmp(&ipv6
->daddr
, &ip6info
->dmsk
,
65 ret
= ifname_compare_aligned(indev
, ip6info
->iniface
, ip6info
->iniface_mask
);
67 if (NF_INVF(ip6info
, IP6T_INV_VIA_IN
, ret
!= 0))
70 ret
= ifname_compare_aligned(outdev
, ip6info
->outiface
, ip6info
->outiface_mask
);
72 if (NF_INVF(ip6info
, IP6T_INV_VIA_OUT
, ret
!= 0))
75 /* ... might want to do something with class and flowlabel here ... */
77 /* look for the desired protocol header */
78 if (ip6info
->flags
& IP6T_F_PROTO
) {
80 unsigned short _frag_off
;
82 protohdr
= ipv6_find_hdr(skb
, protoff
, -1, &_frag_off
, NULL
);
90 if (ip6info
->proto
== protohdr
) {
91 if (ip6info
->invflags
& IP6T_INV_PROTO
)
97 /* We need match for the '-p all', too! */
98 if ((ip6info
->proto
!= 0) &&
99 !(ip6info
->invflags
& IP6T_INV_PROTO
))
105 /* should be ip6 safe */
107 ip6_checkentry(const struct ip6t_ip6
*ipv6
)
109 if (ipv6
->flags
& ~IP6T_F_MASK
)
111 if (ipv6
->invflags
& ~IP6T_INV_MASK
)
118 ip6t_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
120 net_info_ratelimited("error: `%s'\n", (const char *)par
->targinfo
);
125 static inline struct ip6t_entry
*
126 get_entry(const void *base
, unsigned int offset
)
128 return (struct ip6t_entry
*)(base
+ offset
);
131 /* All zeroes == unconditional rule. */
132 /* Mildly perf critical (only if packet tracing is on) */
133 static inline bool unconditional(const struct ip6t_entry
*e
)
135 static const struct ip6t_ip6 uncond
;
137 return e
->target_offset
== sizeof(struct ip6t_entry
) &&
138 memcmp(&e
->ipv6
, &uncond
, sizeof(uncond
)) == 0;
141 static inline const struct xt_entry_target
*
142 ip6t_get_target_c(const struct ip6t_entry
*e
)
144 return ip6t_get_target((struct ip6t_entry
*)e
);
147 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
148 /* This cries for unification! */
149 static const char *const hooknames
[] = {
150 [NF_INET_PRE_ROUTING
] = "PREROUTING",
151 [NF_INET_LOCAL_IN
] = "INPUT",
152 [NF_INET_FORWARD
] = "FORWARD",
153 [NF_INET_LOCAL_OUT
] = "OUTPUT",
154 [NF_INET_POST_ROUTING
] = "POSTROUTING",
157 enum nf_ip_trace_comments
{
158 NF_IP6_TRACE_COMMENT_RULE
,
159 NF_IP6_TRACE_COMMENT_RETURN
,
160 NF_IP6_TRACE_COMMENT_POLICY
,
163 static const char *const comments
[] = {
164 [NF_IP6_TRACE_COMMENT_RULE
] = "rule",
165 [NF_IP6_TRACE_COMMENT_RETURN
] = "return",
166 [NF_IP6_TRACE_COMMENT_POLICY
] = "policy",
169 static const struct nf_loginfo trace_loginfo
= {
170 .type
= NF_LOG_TYPE_LOG
,
173 .level
= LOGLEVEL_WARNING
,
174 .logflags
= NF_LOG_DEFAULT_MASK
,
179 /* Mildly perf critical (only if packet tracing is on) */
181 get_chainname_rulenum(const struct ip6t_entry
*s
, const struct ip6t_entry
*e
,
182 const char *hookname
, const char **chainname
,
183 const char **comment
, unsigned int *rulenum
)
185 const struct xt_standard_target
*t
= (void *)ip6t_get_target_c(s
);
187 if (strcmp(t
->target
.u
.kernel
.target
->name
, XT_ERROR_TARGET
) == 0) {
188 /* Head of user chain: ERROR target with chainname */
189 *chainname
= t
->target
.data
;
194 if (unconditional(s
) &&
195 strcmp(t
->target
.u
.kernel
.target
->name
,
196 XT_STANDARD_TARGET
) == 0 &&
198 /* Tail of chains: STANDARD target (return/policy) */
199 *comment
= *chainname
== hookname
200 ? comments
[NF_IP6_TRACE_COMMENT_POLICY
]
201 : comments
[NF_IP6_TRACE_COMMENT_RETURN
];
210 static void trace_packet(struct net
*net
,
211 const struct sk_buff
*skb
,
213 const struct net_device
*in
,
214 const struct net_device
*out
,
215 const char *tablename
,
216 const struct xt_table_info
*private,
217 const struct ip6t_entry
*e
)
219 const struct ip6t_entry
*root
;
220 const char *hookname
, *chainname
, *comment
;
221 const struct ip6t_entry
*iter
;
222 unsigned int rulenum
= 0;
224 root
= get_entry(private->entries
, private->hook_entry
[hook
]);
226 hookname
= chainname
= hooknames
[hook
];
227 comment
= comments
[NF_IP6_TRACE_COMMENT_RULE
];
229 xt_entry_foreach(iter
, root
, private->size
- private->hook_entry
[hook
])
230 if (get_chainname_rulenum(iter
, e
, hookname
,
231 &chainname
, &comment
, &rulenum
) != 0)
234 nf_log_trace(net
, AF_INET6
, hook
, skb
, in
, out
, &trace_loginfo
,
235 "TRACE: %s:%s:%s:%u ",
236 tablename
, chainname
, comment
, rulenum
);
240 static inline struct ip6t_entry
*
241 ip6t_next_entry(const struct ip6t_entry
*entry
)
243 return (void *)entry
+ entry
->next_offset
;
246 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
248 ip6t_do_table(void *priv
, struct sk_buff
*skb
,
249 const struct nf_hook_state
*state
)
251 const struct xt_table
*table
= priv
;
252 unsigned int hook
= state
->hook
;
253 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
254 /* Initializing verdict to NF_DROP keeps gcc happy. */
255 unsigned int verdict
= NF_DROP
;
256 const char *indev
, *outdev
;
257 const void *table_base
;
258 struct ip6t_entry
*e
, **jumpstack
;
259 unsigned int stackidx
, cpu
;
260 const struct xt_table_info
*private;
261 struct xt_action_param acpar
;
266 indev
= state
->in
? state
->in
->name
: nulldevname
;
267 outdev
= state
->out
? state
->out
->name
: nulldevname
;
268 /* We handle fragments by dealing with the first fragment as
269 * if it was a normal packet. All other fragments are treated
270 * normally, except that they will NEVER match rules that ask
271 * things we don't know, ie. tcp syn flag or ports). If the
272 * rule is also a fragment-specific rule, non-fragments won't
275 acpar
.hotdrop
= false;
278 WARN_ON(!(table
->valid_hooks
& (1 << hook
)));
281 addend
= xt_write_recseq_begin();
282 private = READ_ONCE(table
->private); /* Address dependency. */
283 cpu
= smp_processor_id();
284 table_base
= private->entries
;
285 jumpstack
= (struct ip6t_entry
**)private->jumpstack
[cpu
];
287 /* Switch to alternate jumpstack if we're being invoked via TEE.
288 * TEE issues XT_CONTINUE verdict on original skb so we must not
289 * clobber the jumpstack.
291 * For recursion via REJECT or SYNPROXY the stack will be clobbered
292 * but it is no problem since absolute verdict is issued by these.
294 if (static_key_false(&xt_tee_enabled
))
295 jumpstack
+= private->stacksize
* __this_cpu_read(nf_skb_duplicated
);
297 e
= get_entry(table_base
, private->hook_entry
[hook
]);
300 const struct xt_entry_target
*t
;
301 const struct xt_entry_match
*ematch
;
302 struct xt_counters
*counter
;
306 if (!ip6_packet_match(skb
, indev
, outdev
, &e
->ipv6
,
307 &acpar
.thoff
, &acpar
.fragoff
, &acpar
.hotdrop
)) {
309 e
= ip6t_next_entry(e
);
313 xt_ematch_foreach(ematch
, e
) {
314 acpar
.match
= ematch
->u
.kernel
.match
;
315 acpar
.matchinfo
= ematch
->data
;
316 if (!acpar
.match
->match(skb
, &acpar
))
320 counter
= xt_get_this_cpu_counter(&e
->counters
);
321 ADD_COUNTER(*counter
, skb
->len
, 1);
323 t
= ip6t_get_target_c(e
);
324 WARN_ON(!t
->u
.kernel
.target
);
326 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
327 /* The packet is traced: log it */
328 if (unlikely(skb
->nf_trace
))
329 trace_packet(state
->net
, skb
, hook
, state
->in
,
330 state
->out
, table
->name
, private, e
);
332 /* Standard target? */
333 if (!t
->u
.kernel
.target
->target
) {
336 v
= ((struct xt_standard_target
*)t
)->verdict
;
338 /* Pop from stack? */
339 if (v
!= XT_RETURN
) {
340 verdict
= (unsigned int)(-v
) - 1;
344 e
= get_entry(table_base
,
345 private->underflow
[hook
]);
347 e
= ip6t_next_entry(jumpstack
[--stackidx
]);
350 if (table_base
+ v
!= ip6t_next_entry(e
) &&
351 !(e
->ipv6
.flags
& IP6T_F_GOTO
)) {
352 if (unlikely(stackidx
>= private->stacksize
)) {
356 jumpstack
[stackidx
++] = e
;
359 e
= get_entry(table_base
, v
);
363 acpar
.target
= t
->u
.kernel
.target
;
364 acpar
.targinfo
= t
->data
;
366 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
367 if (verdict
== XT_CONTINUE
)
368 e
= ip6t_next_entry(e
);
372 } while (!acpar
.hotdrop
);
374 xt_write_recseq_end(addend
);
382 /* Figures out from what hook each rule can be called: returns 0 if
383 there are loops. Puts hook bitmask in comefrom. */
385 mark_source_chains(const struct xt_table_info
*newinfo
,
386 unsigned int valid_hooks
, void *entry0
,
387 unsigned int *offsets
)
391 /* No recursion; use packet counter to save back ptrs (reset
392 to 0 as we leave), and comefrom to save source hook bitmask */
393 for (hook
= 0; hook
< NF_INET_NUMHOOKS
; hook
++) {
394 unsigned int pos
= newinfo
->hook_entry
[hook
];
395 struct ip6t_entry
*e
= entry0
+ pos
;
397 if (!(valid_hooks
& (1 << hook
)))
400 /* Set initial back pointer. */
401 e
->counters
.pcnt
= pos
;
404 const struct xt_standard_target
*t
405 = (void *)ip6t_get_target_c(e
);
406 int visited
= e
->comefrom
& (1 << hook
);
408 if (e
->comefrom
& (1 << NF_INET_NUMHOOKS
))
411 e
->comefrom
|= ((1 << hook
) | (1 << NF_INET_NUMHOOKS
));
413 /* Unconditional return/END. */
414 if ((unconditional(e
) &&
415 (strcmp(t
->target
.u
.user
.name
,
416 XT_STANDARD_TARGET
) == 0) &&
417 t
->verdict
< 0) || visited
) {
418 unsigned int oldpos
, size
;
420 /* Return: backtrack through the last
423 e
->comefrom
^= (1<<NF_INET_NUMHOOKS
);
425 pos
= e
->counters
.pcnt
;
426 e
->counters
.pcnt
= 0;
428 /* We're at the start. */
433 } while (oldpos
== pos
+ e
->next_offset
);
436 size
= e
->next_offset
;
437 e
= entry0
+ pos
+ size
;
438 if (pos
+ size
>= newinfo
->size
)
440 e
->counters
.pcnt
= pos
;
443 int newpos
= t
->verdict
;
445 if (strcmp(t
->target
.u
.user
.name
,
446 XT_STANDARD_TARGET
) == 0 &&
448 /* This a jump; chase it. */
449 if (!xt_find_jump_offset(offsets
, newpos
,
453 /* ... this is a fallthru */
454 newpos
= pos
+ e
->next_offset
;
455 if (newpos
>= newinfo
->size
)
459 e
->counters
.pcnt
= pos
;
468 static void cleanup_match(struct xt_entry_match
*m
, struct net
*net
)
470 struct xt_mtdtor_param par
;
473 par
.match
= m
->u
.kernel
.match
;
474 par
.matchinfo
= m
->data
;
475 par
.family
= NFPROTO_IPV6
;
476 if (par
.match
->destroy
!= NULL
)
477 par
.match
->destroy(&par
);
478 module_put(par
.match
->me
);
481 static int check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
483 const struct ip6t_ip6
*ipv6
= par
->entryinfo
;
485 par
->match
= m
->u
.kernel
.match
;
486 par
->matchinfo
= m
->data
;
488 return xt_check_match(par
, m
->u
.match_size
- sizeof(*m
),
489 ipv6
->proto
, ipv6
->invflags
& IP6T_INV_PROTO
);
493 find_check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
495 struct xt_match
*match
;
498 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
501 return PTR_ERR(match
);
503 m
->u
.kernel
.match
= match
;
505 ret
= check_match(m
, par
);
511 module_put(m
->u
.kernel
.match
->me
);
515 static int check_target(struct ip6t_entry
*e
, struct net
*net
, const char *name
)
517 struct xt_entry_target
*t
= ip6t_get_target(e
);
518 struct xt_tgchk_param par
= {
522 .target
= t
->u
.kernel
.target
,
524 .hook_mask
= e
->comefrom
,
525 .family
= NFPROTO_IPV6
,
528 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
),
530 e
->ipv6
.invflags
& IP6T_INV_PROTO
);
534 find_check_entry(struct ip6t_entry
*e
, struct net
*net
, const char *name
,
536 struct xt_percpu_counter_alloc_state
*alloc_state
)
538 struct xt_entry_target
*t
;
539 struct xt_target
*target
;
542 struct xt_mtchk_param mtpar
;
543 struct xt_entry_match
*ematch
;
545 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
549 memset(&mtpar
, 0, sizeof(mtpar
));
552 mtpar
.entryinfo
= &e
->ipv6
;
553 mtpar
.hook_mask
= e
->comefrom
;
554 mtpar
.family
= NFPROTO_IPV6
;
555 xt_ematch_foreach(ematch
, e
) {
556 ret
= find_check_match(ematch
, &mtpar
);
558 goto cleanup_matches
;
562 t
= ip6t_get_target(e
);
563 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
565 if (IS_ERR(target
)) {
566 ret
= PTR_ERR(target
);
567 goto cleanup_matches
;
569 t
->u
.kernel
.target
= target
;
571 ret
= check_target(e
, net
, name
);
576 module_put(t
->u
.kernel
.target
->me
);
578 xt_ematch_foreach(ematch
, e
) {
581 cleanup_match(ematch
, net
);
584 xt_percpu_counter_free(&e
->counters
);
589 static bool check_underflow(const struct ip6t_entry
*e
)
591 const struct xt_entry_target
*t
;
592 unsigned int verdict
;
594 if (!unconditional(e
))
596 t
= ip6t_get_target_c(e
);
597 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
599 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
600 verdict
= -verdict
- 1;
601 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
605 check_entry_size_and_hooks(struct ip6t_entry
*e
,
606 struct xt_table_info
*newinfo
,
607 const unsigned char *base
,
608 const unsigned char *limit
,
609 const unsigned int *hook_entries
,
610 const unsigned int *underflows
,
611 unsigned int valid_hooks
)
616 if ((unsigned long)e
% __alignof__(struct ip6t_entry
) != 0 ||
617 (unsigned char *)e
+ sizeof(struct ip6t_entry
) >= limit
||
618 (unsigned char *)e
+ e
->next_offset
> limit
)
622 < sizeof(struct ip6t_entry
) + sizeof(struct xt_entry_target
))
625 if (!ip6_checkentry(&e
->ipv6
))
628 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
633 /* Check hooks & underflows */
634 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
635 if (!(valid_hooks
& (1 << h
)))
637 if ((unsigned char *)e
- base
== hook_entries
[h
])
638 newinfo
->hook_entry
[h
] = hook_entries
[h
];
639 if ((unsigned char *)e
- base
== underflows
[h
]) {
640 if (!check_underflow(e
))
643 newinfo
->underflow
[h
] = underflows
[h
];
647 /* Clear counters and comefrom */
648 e
->counters
= ((struct xt_counters
) { 0, 0 });
653 static void cleanup_entry(struct ip6t_entry
*e
, struct net
*net
)
655 struct xt_tgdtor_param par
;
656 struct xt_entry_target
*t
;
657 struct xt_entry_match
*ematch
;
659 /* Cleanup all matches */
660 xt_ematch_foreach(ematch
, e
)
661 cleanup_match(ematch
, net
);
662 t
= ip6t_get_target(e
);
665 par
.target
= t
->u
.kernel
.target
;
666 par
.targinfo
= t
->data
;
667 par
.family
= NFPROTO_IPV6
;
668 if (par
.target
->destroy
!= NULL
)
669 par
.target
->destroy(&par
);
670 module_put(par
.target
->me
);
671 xt_percpu_counter_free(&e
->counters
);
674 /* Checks and translates the user-supplied table segment (held in
677 translate_table(struct net
*net
, struct xt_table_info
*newinfo
, void *entry0
,
678 const struct ip6t_replace
*repl
)
680 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
681 struct ip6t_entry
*iter
;
682 unsigned int *offsets
;
686 newinfo
->size
= repl
->size
;
687 newinfo
->number
= repl
->num_entries
;
689 /* Init all hooks to impossible value. */
690 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
691 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
692 newinfo
->underflow
[i
] = 0xFFFFFFFF;
695 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
699 /* Walk through entries, checking offsets. */
700 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
701 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
708 if (i
< repl
->num_entries
)
709 offsets
[i
] = (void *)iter
- entry0
;
711 if (strcmp(ip6t_get_target(iter
)->u
.user
.name
,
712 XT_ERROR_TARGET
) == 0)
713 ++newinfo
->stacksize
;
717 if (i
!= repl
->num_entries
)
720 ret
= xt_check_table_hooks(newinfo
, repl
->valid_hooks
);
724 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
730 /* Finally, each sanity check must pass */
732 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
733 ret
= find_check_entry(iter
, net
, repl
->name
, repl
->size
,
741 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
744 cleanup_entry(iter
, net
);
756 get_counters(const struct xt_table_info
*t
,
757 struct xt_counters counters
[])
759 struct ip6t_entry
*iter
;
763 for_each_possible_cpu(cpu
) {
764 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
767 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
768 struct xt_counters
*tmp
;
772 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
774 start
= read_seqcount_begin(s
);
777 } while (read_seqcount_retry(s
, start
));
779 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
786 static void get_old_counters(const struct xt_table_info
*t
,
787 struct xt_counters counters
[])
789 struct ip6t_entry
*iter
;
792 for_each_possible_cpu(cpu
) {
794 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
795 const struct xt_counters
*tmp
;
797 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
798 ADD_COUNTER(counters
[i
], tmp
->bcnt
, tmp
->pcnt
);
805 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
807 unsigned int countersize
;
808 struct xt_counters
*counters
;
809 const struct xt_table_info
*private = table
->private;
811 /* We need atomic snapshot of counters: rest doesn't change
812 (other than comefrom, which userspace doesn't care
814 countersize
= sizeof(struct xt_counters
) * private->number
;
815 counters
= vzalloc(countersize
);
817 if (counters
== NULL
)
818 return ERR_PTR(-ENOMEM
);
820 get_counters(private, counters
);
826 copy_entries_to_user(unsigned int total_size
,
827 const struct xt_table
*table
,
828 void __user
*userptr
)
830 unsigned int off
, num
;
831 const struct ip6t_entry
*e
;
832 struct xt_counters
*counters
;
833 const struct xt_table_info
*private = table
->private;
835 const void *loc_cpu_entry
;
837 counters
= alloc_counters(table
);
838 if (IS_ERR(counters
))
839 return PTR_ERR(counters
);
841 loc_cpu_entry
= private->entries
;
843 /* FIXME: use iterator macros --RR */
844 /* ... then go back and fix counters and names */
845 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
847 const struct xt_entry_match
*m
;
848 const struct xt_entry_target
*t
;
850 e
= loc_cpu_entry
+ off
;
851 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
855 if (copy_to_user(userptr
+ off
856 + offsetof(struct ip6t_entry
, counters
),
858 sizeof(counters
[num
])) != 0) {
863 for (i
= sizeof(struct ip6t_entry
);
864 i
< e
->target_offset
;
865 i
+= m
->u
.match_size
) {
868 if (xt_match_to_user(m
, userptr
+ off
+ i
)) {
874 t
= ip6t_get_target_c(e
);
875 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
886 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
887 static void compat_standard_from_user(void *dst
, const void *src
)
889 int v
= *(compat_int_t
*)src
;
892 v
+= xt_compat_calc_jump(AF_INET6
, v
);
893 memcpy(dst
, &v
, sizeof(v
));
896 static int compat_standard_to_user(void __user
*dst
, const void *src
)
898 compat_int_t cv
= *(int *)src
;
901 cv
-= xt_compat_calc_jump(AF_INET6
, cv
);
902 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
905 static int compat_calc_entry(const struct ip6t_entry
*e
,
906 const struct xt_table_info
*info
,
907 const void *base
, struct xt_table_info
*newinfo
)
909 const struct xt_entry_match
*ematch
;
910 const struct xt_entry_target
*t
;
911 unsigned int entry_offset
;
914 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
915 entry_offset
= (void *)e
- base
;
916 xt_ematch_foreach(ematch
, e
)
917 off
+= xt_compat_match_offset(ematch
->u
.kernel
.match
);
918 t
= ip6t_get_target_c(e
);
919 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
920 newinfo
->size
-= off
;
921 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
925 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
926 if (info
->hook_entry
[i
] &&
927 (e
< (struct ip6t_entry
*)(base
+ info
->hook_entry
[i
])))
928 newinfo
->hook_entry
[i
] -= off
;
929 if (info
->underflow
[i
] &&
930 (e
< (struct ip6t_entry
*)(base
+ info
->underflow
[i
])))
931 newinfo
->underflow
[i
] -= off
;
936 static int compat_table_info(const struct xt_table_info
*info
,
937 struct xt_table_info
*newinfo
)
939 struct ip6t_entry
*iter
;
940 const void *loc_cpu_entry
;
943 if (!newinfo
|| !info
)
946 /* we dont care about newinfo->entries */
947 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
948 newinfo
->initial_entries
= 0;
949 loc_cpu_entry
= info
->entries
;
950 ret
= xt_compat_init_offsets(AF_INET6
, info
->number
);
953 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
954 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
962 static int get_info(struct net
*net
, void __user
*user
, const int *len
)
964 char name
[XT_TABLE_MAXNAMELEN
];
968 if (*len
!= sizeof(struct ip6t_getinfo
))
971 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
974 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
975 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
976 if (in_compat_syscall())
977 xt_compat_lock(AF_INET6
);
979 t
= xt_request_find_table_lock(net
, AF_INET6
, name
);
981 struct ip6t_getinfo info
;
982 const struct xt_table_info
*private = t
->private;
983 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
984 struct xt_table_info tmp
;
986 if (in_compat_syscall()) {
987 ret
= compat_table_info(private, &tmp
);
988 xt_compat_flush_offsets(AF_INET6
);
992 memset(&info
, 0, sizeof(info
));
993 info
.valid_hooks
= t
->valid_hooks
;
994 memcpy(info
.hook_entry
, private->hook_entry
,
995 sizeof(info
.hook_entry
));
996 memcpy(info
.underflow
, private->underflow
,
997 sizeof(info
.underflow
));
998 info
.num_entries
= private->number
;
999 info
.size
= private->size
;
1000 strcpy(info
.name
, name
);
1002 if (copy_to_user(user
, &info
, *len
) != 0)
1011 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1012 if (in_compat_syscall())
1013 xt_compat_unlock(AF_INET6
);
1019 get_entries(struct net
*net
, struct ip6t_get_entries __user
*uptr
,
1023 struct ip6t_get_entries get
;
1026 if (*len
< sizeof(get
))
1028 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1030 if (*len
!= sizeof(struct ip6t_get_entries
) + get
.size
)
1033 get
.name
[sizeof(get
.name
) - 1] = '\0';
1035 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1037 struct xt_table_info
*private = t
->private;
1038 if (get
.size
== private->size
)
1039 ret
= copy_entries_to_user(private->size
,
1040 t
, uptr
->entrytable
);
1053 __do_replace(struct net
*net
, const char *name
, unsigned int valid_hooks
,
1054 struct xt_table_info
*newinfo
, unsigned int num_counters
,
1055 void __user
*counters_ptr
)
1059 struct xt_table_info
*oldinfo
;
1060 struct xt_counters
*counters
;
1061 struct ip6t_entry
*iter
;
1063 counters
= xt_counters_alloc(num_counters
);
1069 t
= xt_request_find_table_lock(net
, AF_INET6
, name
);
1072 goto free_newinfo_counters_untrans
;
1076 if (valid_hooks
!= t
->valid_hooks
) {
1081 oldinfo
= xt_replace_table(t
, num_counters
, newinfo
, &ret
);
1085 /* Update module usage count based on number of rules */
1086 if ((oldinfo
->number
> oldinfo
->initial_entries
) ||
1087 (newinfo
->number
<= oldinfo
->initial_entries
))
1089 if ((oldinfo
->number
> oldinfo
->initial_entries
) &&
1090 (newinfo
->number
<= oldinfo
->initial_entries
))
1095 get_old_counters(oldinfo
, counters
);
1097 /* Decrease module usage counts and free resource */
1098 xt_entry_foreach(iter
, oldinfo
->entries
, oldinfo
->size
)
1099 cleanup_entry(iter
, net
);
1101 xt_free_table_info(oldinfo
);
1102 if (copy_to_user(counters_ptr
, counters
,
1103 sizeof(struct xt_counters
) * num_counters
) != 0) {
1104 /* Silent error, can't fail, new table is already in place */
1105 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1113 free_newinfo_counters_untrans
:
1120 do_replace(struct net
*net
, sockptr_t arg
, unsigned int len
)
1123 struct ip6t_replace tmp
;
1124 struct xt_table_info
*newinfo
;
1125 void *loc_cpu_entry
;
1126 struct ip6t_entry
*iter
;
1128 if (len
< sizeof(tmp
))
1130 if (copy_from_sockptr(&tmp
, arg
, sizeof(tmp
)) != 0)
1133 /* overflow check */
1134 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1136 if (tmp
.num_counters
== 0)
1138 if ((u64
)len
< (u64
)tmp
.size
+ sizeof(tmp
))
1141 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1143 newinfo
= xt_alloc_table_info(tmp
.size
);
1147 loc_cpu_entry
= newinfo
->entries
;
1148 if (copy_from_sockptr_offset(loc_cpu_entry
, arg
, sizeof(tmp
),
1154 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, &tmp
);
1158 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1159 tmp
.num_counters
, tmp
.counters
);
1161 goto free_newinfo_untrans
;
1164 free_newinfo_untrans
:
1165 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1166 cleanup_entry(iter
, net
);
1168 xt_free_table_info(newinfo
);
1173 do_add_counters(struct net
*net
, sockptr_t arg
, unsigned int len
)
1176 struct xt_counters_info tmp
;
1177 struct xt_counters
*paddc
;
1179 const struct xt_table_info
*private;
1181 struct ip6t_entry
*iter
;
1182 unsigned int addend
;
1184 paddc
= xt_copy_counters(arg
, len
, &tmp
);
1186 return PTR_ERR(paddc
);
1187 t
= xt_find_table_lock(net
, AF_INET6
, tmp
.name
);
1194 private = t
->private;
1195 if (private->number
!= tmp
.num_counters
) {
1197 goto unlock_up_free
;
1201 addend
= xt_write_recseq_begin();
1202 xt_entry_foreach(iter
, private->entries
, private->size
) {
1203 struct xt_counters
*tmp
;
1205 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1206 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1209 xt_write_recseq_end(addend
);
1220 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1221 struct compat_ip6t_replace
{
1222 char name
[XT_TABLE_MAXNAMELEN
];
1226 u32 hook_entry
[NF_INET_NUMHOOKS
];
1227 u32 underflow
[NF_INET_NUMHOOKS
];
1229 compat_uptr_t counters
; /* struct xt_counters * */
1230 struct compat_ip6t_entry entries
[];
1234 compat_copy_entry_to_user(struct ip6t_entry
*e
, void __user
**dstptr
,
1235 unsigned int *size
, struct xt_counters
*counters
,
1238 struct xt_entry_target
*t
;
1239 struct compat_ip6t_entry __user
*ce
;
1240 u_int16_t target_offset
, next_offset
;
1241 compat_uint_t origsize
;
1242 const struct xt_entry_match
*ematch
;
1247 if (copy_to_user(ce
, e
, sizeof(struct ip6t_entry
)) != 0 ||
1248 copy_to_user(&ce
->counters
, &counters
[i
],
1249 sizeof(counters
[i
])) != 0)
1252 *dstptr
+= sizeof(struct compat_ip6t_entry
);
1253 *size
-= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1255 xt_ematch_foreach(ematch
, e
) {
1256 ret
= xt_compat_match_to_user(ematch
, dstptr
, size
);
1260 target_offset
= e
->target_offset
- (origsize
- *size
);
1261 t
= ip6t_get_target(e
);
1262 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1265 next_offset
= e
->next_offset
- (origsize
- *size
);
1266 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1267 put_user(next_offset
, &ce
->next_offset
) != 0)
1273 compat_find_calc_match(struct xt_entry_match
*m
,
1274 const struct ip6t_ip6
*ipv6
,
1277 struct xt_match
*match
;
1279 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
1280 m
->u
.user
.revision
);
1282 return PTR_ERR(match
);
1284 m
->u
.kernel
.match
= match
;
1285 *size
+= xt_compat_match_offset(match
);
1289 static void compat_release_entry(struct compat_ip6t_entry
*e
)
1291 struct xt_entry_target
*t
;
1292 struct xt_entry_match
*ematch
;
1294 /* Cleanup all matches */
1295 xt_ematch_foreach(ematch
, e
)
1296 module_put(ematch
->u
.kernel
.match
->me
);
1297 t
= compat_ip6t_get_target(e
);
1298 module_put(t
->u
.kernel
.target
->me
);
1302 check_compat_entry_size_and_hooks(struct compat_ip6t_entry
*e
,
1303 struct xt_table_info
*newinfo
,
1305 const unsigned char *base
,
1306 const unsigned char *limit
)
1308 struct xt_entry_match
*ematch
;
1309 struct xt_entry_target
*t
;
1310 struct xt_target
*target
;
1311 unsigned int entry_offset
;
1315 if ((unsigned long)e
% __alignof__(struct compat_ip6t_entry
) != 0 ||
1316 (unsigned char *)e
+ sizeof(struct compat_ip6t_entry
) >= limit
||
1317 (unsigned char *)e
+ e
->next_offset
> limit
)
1320 if (e
->next_offset
< sizeof(struct compat_ip6t_entry
) +
1321 sizeof(struct compat_xt_entry_target
))
1324 if (!ip6_checkentry(&e
->ipv6
))
1327 ret
= xt_compat_check_entry_offsets(e
, e
->elems
,
1328 e
->target_offset
, e
->next_offset
);
1332 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1333 entry_offset
= (void *)e
- (void *)base
;
1335 xt_ematch_foreach(ematch
, e
) {
1336 ret
= compat_find_calc_match(ematch
, &e
->ipv6
, &off
);
1338 goto release_matches
;
1342 t
= compat_ip6t_get_target(e
);
1343 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
1344 t
->u
.user
.revision
);
1345 if (IS_ERR(target
)) {
1346 ret
= PTR_ERR(target
);
1347 goto release_matches
;
1349 t
->u
.kernel
.target
= target
;
1351 off
+= xt_compat_target_offset(target
);
1353 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
1360 module_put(t
->u
.kernel
.target
->me
);
1362 xt_ematch_foreach(ematch
, e
) {
1365 module_put(ematch
->u
.kernel
.match
->me
);
1371 compat_copy_entry_from_user(struct compat_ip6t_entry
*e
, void **dstptr
,
1373 struct xt_table_info
*newinfo
, unsigned char *base
)
1375 struct xt_entry_target
*t
;
1376 struct ip6t_entry
*de
;
1377 unsigned int origsize
;
1379 struct xt_entry_match
*ematch
;
1383 memcpy(de
, e
, sizeof(struct ip6t_entry
));
1384 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1386 *dstptr
+= sizeof(struct ip6t_entry
);
1387 *size
+= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1389 xt_ematch_foreach(ematch
, e
)
1390 xt_compat_match_from_user(ematch
, dstptr
, size
);
1392 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1393 t
= compat_ip6t_get_target(e
);
1394 xt_compat_target_from_user(t
, dstptr
, size
);
1396 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1397 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
1398 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1399 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1400 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1401 newinfo
->underflow
[h
] -= origsize
- *size
;
1406 translate_compat_table(struct net
*net
,
1407 struct xt_table_info
**pinfo
,
1409 const struct compat_ip6t_replace
*compatr
)
1412 struct xt_table_info
*newinfo
, *info
;
1413 void *pos
, *entry0
, *entry1
;
1414 struct compat_ip6t_entry
*iter0
;
1415 struct ip6t_replace repl
;
1421 size
= compatr
->size
;
1422 info
->number
= compatr
->num_entries
;
1425 xt_compat_lock(AF_INET6
);
1426 ret
= xt_compat_init_offsets(AF_INET6
, compatr
->num_entries
);
1429 /* Walk through entries, checking offsets. */
1430 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1431 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1433 entry0
+ compatr
->size
);
1440 if (j
!= compatr
->num_entries
)
1444 newinfo
= xt_alloc_table_info(size
);
1448 memset(newinfo
->entries
, 0, size
);
1450 newinfo
->number
= compatr
->num_entries
;
1451 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1452 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1453 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1455 entry1
= newinfo
->entries
;
1457 size
= compatr
->size
;
1458 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1459 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1462 /* all module references in entry0 are now gone. */
1463 xt_compat_flush_offsets(AF_INET6
);
1464 xt_compat_unlock(AF_INET6
);
1466 memcpy(&repl
, compatr
, sizeof(*compatr
));
1468 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1469 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1470 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1473 repl
.num_counters
= 0;
1474 repl
.counters
= NULL
;
1475 repl
.size
= newinfo
->size
;
1476 ret
= translate_table(net
, newinfo
, entry1
, &repl
);
1482 xt_free_table_info(info
);
1486 xt_free_table_info(newinfo
);
1489 xt_compat_flush_offsets(AF_INET6
);
1490 xt_compat_unlock(AF_INET6
);
1491 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1494 compat_release_entry(iter0
);
1500 compat_do_replace(struct net
*net
, sockptr_t arg
, unsigned int len
)
1503 struct compat_ip6t_replace tmp
;
1504 struct xt_table_info
*newinfo
;
1505 void *loc_cpu_entry
;
1506 struct ip6t_entry
*iter
;
1508 if (len
< sizeof(tmp
))
1510 if (copy_from_sockptr(&tmp
, arg
, sizeof(tmp
)) != 0)
1513 /* overflow check */
1514 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1516 if (tmp
.num_counters
== 0)
1518 if ((u64
)len
< (u64
)tmp
.size
+ sizeof(tmp
))
1521 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1523 newinfo
= xt_alloc_table_info(tmp
.size
);
1527 loc_cpu_entry
= newinfo
->entries
;
1528 if (copy_from_sockptr_offset(loc_cpu_entry
, arg
, sizeof(tmp
),
1534 ret
= translate_compat_table(net
, &newinfo
, &loc_cpu_entry
, &tmp
);
1538 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1539 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1541 goto free_newinfo_untrans
;
1544 free_newinfo_untrans
:
1545 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1546 cleanup_entry(iter
, net
);
1548 xt_free_table_info(newinfo
);
1552 struct compat_ip6t_get_entries
{
1553 char name
[XT_TABLE_MAXNAMELEN
];
1555 struct compat_ip6t_entry entrytable
[];
1559 compat_copy_entries_to_user(unsigned int total_size
, struct xt_table
*table
,
1560 void __user
*userptr
)
1562 struct xt_counters
*counters
;
1563 const struct xt_table_info
*private = table
->private;
1568 struct ip6t_entry
*iter
;
1570 counters
= alloc_counters(table
);
1571 if (IS_ERR(counters
))
1572 return PTR_ERR(counters
);
1576 xt_entry_foreach(iter
, private->entries
, total_size
) {
1577 ret
= compat_copy_entry_to_user(iter
, &pos
,
1578 &size
, counters
, i
++);
1588 compat_get_entries(struct net
*net
, struct compat_ip6t_get_entries __user
*uptr
,
1592 struct compat_ip6t_get_entries get
;
1595 if (*len
< sizeof(get
))
1598 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1601 if (*len
!= sizeof(struct compat_ip6t_get_entries
) + get
.size
)
1604 get
.name
[sizeof(get
.name
) - 1] = '\0';
1606 xt_compat_lock(AF_INET6
);
1607 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1609 const struct xt_table_info
*private = t
->private;
1610 struct xt_table_info info
;
1611 ret
= compat_table_info(private, &info
);
1612 if (!ret
&& get
.size
== info
.size
)
1613 ret
= compat_copy_entries_to_user(private->size
,
1614 t
, uptr
->entrytable
);
1618 xt_compat_flush_offsets(AF_INET6
);
1624 xt_compat_unlock(AF_INET6
);
1630 do_ip6t_set_ctl(struct sock
*sk
, int cmd
, sockptr_t arg
, unsigned int len
)
1634 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1638 case IP6T_SO_SET_REPLACE
:
1639 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1640 if (in_compat_syscall())
1641 ret
= compat_do_replace(sock_net(sk
), arg
, len
);
1644 ret
= do_replace(sock_net(sk
), arg
, len
);
1647 case IP6T_SO_SET_ADD_COUNTERS
:
1648 ret
= do_add_counters(sock_net(sk
), arg
, len
);
1659 do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1663 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1667 case IP6T_SO_GET_INFO
:
1668 ret
= get_info(sock_net(sk
), user
, len
);
1671 case IP6T_SO_GET_ENTRIES
:
1672 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1673 if (in_compat_syscall())
1674 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1677 ret
= get_entries(sock_net(sk
), user
, len
);
1680 case IP6T_SO_GET_REVISION_MATCH
:
1681 case IP6T_SO_GET_REVISION_TARGET
: {
1682 struct xt_get_revision rev
;
1685 if (*len
!= sizeof(rev
)) {
1689 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1693 rev
.name
[sizeof(rev
.name
)-1] = 0;
1695 if (cmd
== IP6T_SO_GET_REVISION_TARGET
)
1700 try_then_request_module(xt_find_revision(AF_INET6
, rev
.name
,
1703 "ip6t_%s", rev
.name
);
1714 static void __ip6t_unregister_table(struct net
*net
, struct xt_table
*table
)
1716 struct xt_table_info
*private;
1717 void *loc_cpu_entry
;
1718 struct module
*table_owner
= table
->me
;
1719 struct ip6t_entry
*iter
;
1721 private = xt_unregister_table(table
);
1723 /* Decrease module usage counts and free resources */
1724 loc_cpu_entry
= private->entries
;
1725 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1726 cleanup_entry(iter
, net
);
1727 if (private->number
> private->initial_entries
)
1728 module_put(table_owner
);
1729 xt_free_table_info(private);
1732 int ip6t_register_table(struct net
*net
, const struct xt_table
*table
,
1733 const struct ip6t_replace
*repl
,
1734 const struct nf_hook_ops
*template_ops
)
1736 struct nf_hook_ops
*ops
;
1737 unsigned int num_ops
;
1739 struct xt_table_info
*newinfo
;
1740 struct xt_table_info bootstrap
= {0};
1741 void *loc_cpu_entry
;
1742 struct xt_table
*new_table
;
1744 newinfo
= xt_alloc_table_info(repl
->size
);
1748 loc_cpu_entry
= newinfo
->entries
;
1749 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1751 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, repl
);
1753 xt_free_table_info(newinfo
);
1757 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1758 if (IS_ERR(new_table
)) {
1759 struct ip6t_entry
*iter
;
1761 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1762 cleanup_entry(iter
, net
);
1763 xt_free_table_info(newinfo
);
1764 return PTR_ERR(new_table
);
1770 num_ops
= hweight32(table
->valid_hooks
);
1776 ops
= kmemdup_array(template_ops
, num_ops
, sizeof(*ops
), GFP_KERNEL
);
1782 for (i
= 0; i
< num_ops
; i
++)
1783 ops
[i
].priv
= new_table
;
1785 new_table
->ops
= ops
;
1787 ret
= nf_register_net_hooks(net
, ops
, num_ops
);
1794 __ip6t_unregister_table(net
, new_table
);
1798 void ip6t_unregister_table_pre_exit(struct net
*net
, const char *name
)
1800 struct xt_table
*table
= xt_find_table(net
, NFPROTO_IPV6
, name
);
1803 nf_unregister_net_hooks(net
, table
->ops
, hweight32(table
->valid_hooks
));
1806 void ip6t_unregister_table_exit(struct net
*net
, const char *name
)
1808 struct xt_table
*table
= xt_find_table(net
, NFPROTO_IPV6
, name
);
1811 __ip6t_unregister_table(net
, table
);
1814 /* The built-in targets: standard (NULL) and error. */
1815 static struct xt_target ip6t_builtin_tg
[] __read_mostly
= {
1817 .name
= XT_STANDARD_TARGET
,
1818 .targetsize
= sizeof(int),
1819 .family
= NFPROTO_IPV6
,
1820 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1821 .compatsize
= sizeof(compat_int_t
),
1822 .compat_from_user
= compat_standard_from_user
,
1823 .compat_to_user
= compat_standard_to_user
,
1827 .name
= XT_ERROR_TARGET
,
1828 .target
= ip6t_error
,
1829 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1830 .family
= NFPROTO_IPV6
,
1834 static struct nf_sockopt_ops ip6t_sockopts
= {
1836 .set_optmin
= IP6T_BASE_CTL
,
1837 .set_optmax
= IP6T_SO_SET_MAX
+1,
1838 .set
= do_ip6t_set_ctl
,
1839 .get_optmin
= IP6T_BASE_CTL
,
1840 .get_optmax
= IP6T_SO_GET_MAX
+1,
1841 .get
= do_ip6t_get_ctl
,
1842 .owner
= THIS_MODULE
,
1845 static int __net_init
ip6_tables_net_init(struct net
*net
)
1847 return xt_proto_init(net
, NFPROTO_IPV6
);
1850 static void __net_exit
ip6_tables_net_exit(struct net
*net
)
1852 xt_proto_fini(net
, NFPROTO_IPV6
);
1855 static struct pernet_operations ip6_tables_net_ops
= {
1856 .init
= ip6_tables_net_init
,
1857 .exit
= ip6_tables_net_exit
,
1860 static int __init
ip6_tables_init(void)
1864 ret
= register_pernet_subsys(&ip6_tables_net_ops
);
1868 /* No one else will be downing sem now, so we won't sleep */
1869 ret
= xt_register_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1873 /* Register setsockopt */
1874 ret
= nf_register_sockopt(&ip6t_sockopts
);
1881 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1883 unregister_pernet_subsys(&ip6_tables_net_ops
);
1888 static void __exit
ip6_tables_fini(void)
1890 nf_unregister_sockopt(&ip6t_sockopts
);
1892 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1893 unregister_pernet_subsys(&ip6_tables_net_ops
);
1896 EXPORT_SYMBOL(ip6t_register_table
);
1897 EXPORT_SYMBOL(ip6t_unregister_table_pre_exit
);
1898 EXPORT_SYMBOL(ip6t_unregister_table_exit
);
1899 EXPORT_SYMBOL(ip6t_do_table
);
1901 module_init(ip6_tables_init
);
1902 module_exit(ip6_tables_fini
);