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 jumpstack
[stackidx
++] = e
;
363 e
= get_entry(table_base
, v
);
367 acpar
.target
= t
->u
.kernel
.target
;
368 acpar
.targinfo
= t
->data
;
370 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
371 if (verdict
== XT_CONTINUE
)
372 e
= ip6t_next_entry(e
);
376 } while (!acpar
.hotdrop
);
378 xt_write_recseq_end(addend
);
386 /* Figures out from what hook each rule can be called: returns 0 if
387 there are loops. Puts hook bitmask in comefrom. */
389 mark_source_chains(const struct xt_table_info
*newinfo
,
390 unsigned int valid_hooks
, void *entry0
,
391 unsigned int *offsets
)
395 /* No recursion; use packet counter to save back ptrs (reset
396 to 0 as we leave), and comefrom to save source hook bitmask */
397 for (hook
= 0; hook
< NF_INET_NUMHOOKS
; hook
++) {
398 unsigned int pos
= newinfo
->hook_entry
[hook
];
399 struct ip6t_entry
*e
= entry0
+ pos
;
401 if (!(valid_hooks
& (1 << hook
)))
404 /* Set initial back pointer. */
405 e
->counters
.pcnt
= pos
;
408 const struct xt_standard_target
*t
409 = (void *)ip6t_get_target_c(e
);
410 int visited
= e
->comefrom
& (1 << hook
);
412 if (e
->comefrom
& (1 << NF_INET_NUMHOOKS
))
415 e
->comefrom
|= ((1 << hook
) | (1 << NF_INET_NUMHOOKS
));
417 /* Unconditional return/END. */
418 if ((unconditional(e
) &&
419 (strcmp(t
->target
.u
.user
.name
,
420 XT_STANDARD_TARGET
) == 0) &&
421 t
->verdict
< 0) || visited
) {
422 unsigned int oldpos
, size
;
424 if ((strcmp(t
->target
.u
.user
.name
,
425 XT_STANDARD_TARGET
) == 0) &&
426 t
->verdict
< -NF_MAX_VERDICT
- 1)
429 /* Return: backtrack through the last
432 e
->comefrom
^= (1<<NF_INET_NUMHOOKS
);
434 pos
= e
->counters
.pcnt
;
435 e
->counters
.pcnt
= 0;
437 /* We're at the start. */
442 } while (oldpos
== pos
+ e
->next_offset
);
445 size
= e
->next_offset
;
446 e
= entry0
+ pos
+ size
;
447 if (pos
+ size
>= newinfo
->size
)
449 e
->counters
.pcnt
= pos
;
452 int newpos
= t
->verdict
;
454 if (strcmp(t
->target
.u
.user
.name
,
455 XT_STANDARD_TARGET
) == 0 &&
457 /* This a jump; chase it. */
458 if (!xt_find_jump_offset(offsets
, newpos
,
463 /* ... this is a fallthru */
464 newpos
= pos
+ e
->next_offset
;
465 if (newpos
>= newinfo
->size
)
469 e
->counters
.pcnt
= pos
;
478 static void cleanup_match(struct xt_entry_match
*m
, struct net
*net
)
480 struct xt_mtdtor_param par
;
483 par
.match
= m
->u
.kernel
.match
;
484 par
.matchinfo
= m
->data
;
485 par
.family
= NFPROTO_IPV6
;
486 if (par
.match
->destroy
!= NULL
)
487 par
.match
->destroy(&par
);
488 module_put(par
.match
->me
);
491 static int check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
493 const struct ip6t_ip6
*ipv6
= par
->entryinfo
;
495 par
->match
= m
->u
.kernel
.match
;
496 par
->matchinfo
= m
->data
;
498 return xt_check_match(par
, m
->u
.match_size
- sizeof(*m
),
499 ipv6
->proto
, ipv6
->invflags
& IP6T_INV_PROTO
);
503 find_check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
505 struct xt_match
*match
;
508 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
511 return PTR_ERR(match
);
513 m
->u
.kernel
.match
= match
;
515 ret
= check_match(m
, par
);
521 module_put(m
->u
.kernel
.match
->me
);
525 static int check_target(struct ip6t_entry
*e
, struct net
*net
, const char *name
)
527 struct xt_entry_target
*t
= ip6t_get_target(e
);
528 struct xt_tgchk_param par
= {
532 .target
= t
->u
.kernel
.target
,
534 .hook_mask
= e
->comefrom
,
535 .family
= NFPROTO_IPV6
,
538 t
= ip6t_get_target(e
);
539 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
),
541 e
->ipv6
.invflags
& IP6T_INV_PROTO
);
545 find_check_entry(struct ip6t_entry
*e
, struct net
*net
, const char *name
,
547 struct xt_percpu_counter_alloc_state
*alloc_state
)
549 struct xt_entry_target
*t
;
550 struct xt_target
*target
;
553 struct xt_mtchk_param mtpar
;
554 struct xt_entry_match
*ematch
;
556 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
562 mtpar
.entryinfo
= &e
->ipv6
;
563 mtpar
.hook_mask
= e
->comefrom
;
564 mtpar
.family
= NFPROTO_IPV6
;
565 xt_ematch_foreach(ematch
, e
) {
566 ret
= find_check_match(ematch
, &mtpar
);
568 goto cleanup_matches
;
572 t
= ip6t_get_target(e
);
573 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
575 if (IS_ERR(target
)) {
576 ret
= PTR_ERR(target
);
577 goto cleanup_matches
;
579 t
->u
.kernel
.target
= target
;
581 ret
= check_target(e
, net
, name
);
586 module_put(t
->u
.kernel
.target
->me
);
588 xt_ematch_foreach(ematch
, e
) {
591 cleanup_match(ematch
, net
);
594 xt_percpu_counter_free(&e
->counters
);
599 static bool check_underflow(const struct ip6t_entry
*e
)
601 const struct xt_entry_target
*t
;
602 unsigned int verdict
;
604 if (!unconditional(e
))
606 t
= ip6t_get_target_c(e
);
607 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
609 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
610 verdict
= -verdict
- 1;
611 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
615 check_entry_size_and_hooks(struct ip6t_entry
*e
,
616 struct xt_table_info
*newinfo
,
617 const unsigned char *base
,
618 const unsigned char *limit
,
619 const unsigned int *hook_entries
,
620 const unsigned int *underflows
,
621 unsigned int valid_hooks
)
626 if ((unsigned long)e
% __alignof__(struct ip6t_entry
) != 0 ||
627 (unsigned char *)e
+ sizeof(struct ip6t_entry
) >= limit
||
628 (unsigned char *)e
+ e
->next_offset
> limit
)
632 < sizeof(struct ip6t_entry
) + sizeof(struct xt_entry_target
))
635 if (!ip6_checkentry(&e
->ipv6
))
638 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
643 /* Check hooks & underflows */
644 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
645 if (!(valid_hooks
& (1 << h
)))
647 if ((unsigned char *)e
- base
== hook_entries
[h
])
648 newinfo
->hook_entry
[h
] = hook_entries
[h
];
649 if ((unsigned char *)e
- base
== underflows
[h
]) {
650 if (!check_underflow(e
))
653 newinfo
->underflow
[h
] = underflows
[h
];
657 /* Clear counters and comefrom */
658 e
->counters
= ((struct xt_counters
) { 0, 0 });
663 static void cleanup_entry(struct ip6t_entry
*e
, struct net
*net
)
665 struct xt_tgdtor_param par
;
666 struct xt_entry_target
*t
;
667 struct xt_entry_match
*ematch
;
669 /* Cleanup all matches */
670 xt_ematch_foreach(ematch
, e
)
671 cleanup_match(ematch
, net
);
672 t
= ip6t_get_target(e
);
675 par
.target
= t
->u
.kernel
.target
;
676 par
.targinfo
= t
->data
;
677 par
.family
= NFPROTO_IPV6
;
678 if (par
.target
->destroy
!= NULL
)
679 par
.target
->destroy(&par
);
680 module_put(par
.target
->me
);
681 xt_percpu_counter_free(&e
->counters
);
684 /* Checks and translates the user-supplied table segment (held in
687 translate_table(struct net
*net
, struct xt_table_info
*newinfo
, void *entry0
,
688 const struct ip6t_replace
*repl
)
690 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
691 struct ip6t_entry
*iter
;
692 unsigned int *offsets
;
696 newinfo
->size
= repl
->size
;
697 newinfo
->number
= repl
->num_entries
;
699 /* Init all hooks to impossible value. */
700 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
701 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
702 newinfo
->underflow
[i
] = 0xFFFFFFFF;
705 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
709 /* Walk through entries, checking offsets. */
710 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
711 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
718 if (i
< repl
->num_entries
)
719 offsets
[i
] = (void *)iter
- entry0
;
721 if (strcmp(ip6t_get_target(iter
)->u
.user
.name
,
722 XT_ERROR_TARGET
) == 0)
723 ++newinfo
->stacksize
;
727 if (i
!= repl
->num_entries
)
730 /* Check hooks all assigned */
731 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
732 /* Only hooks which are valid */
733 if (!(repl
->valid_hooks
& (1 << i
)))
735 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
737 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
741 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
747 /* Finally, each sanity check must pass */
749 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
750 ret
= find_check_entry(iter
, net
, repl
->name
, repl
->size
,
758 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
761 cleanup_entry(iter
, net
);
773 get_counters(const struct xt_table_info
*t
,
774 struct xt_counters counters
[])
776 struct ip6t_entry
*iter
;
780 for_each_possible_cpu(cpu
) {
781 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
784 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
785 struct xt_counters
*tmp
;
789 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
791 start
= read_seqcount_begin(s
);
794 } while (read_seqcount_retry(s
, start
));
796 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
803 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
805 unsigned int countersize
;
806 struct xt_counters
*counters
;
807 const struct xt_table_info
*private = table
->private;
809 /* We need atomic snapshot of counters: rest doesn't change
810 (other than comefrom, which userspace doesn't care
812 countersize
= sizeof(struct xt_counters
) * private->number
;
813 counters
= vzalloc(countersize
);
815 if (counters
== NULL
)
816 return ERR_PTR(-ENOMEM
);
818 get_counters(private, counters
);
824 copy_entries_to_user(unsigned int total_size
,
825 const struct xt_table
*table
,
826 void __user
*userptr
)
828 unsigned int off
, num
;
829 const struct ip6t_entry
*e
;
830 struct xt_counters
*counters
;
831 const struct xt_table_info
*private = table
->private;
833 const void *loc_cpu_entry
;
835 counters
= alloc_counters(table
);
836 if (IS_ERR(counters
))
837 return PTR_ERR(counters
);
839 loc_cpu_entry
= private->entries
;
841 /* FIXME: use iterator macros --RR */
842 /* ... then go back and fix counters and names */
843 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
845 const struct xt_entry_match
*m
;
846 const struct xt_entry_target
*t
;
848 e
= loc_cpu_entry
+ off
;
849 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
853 if (copy_to_user(userptr
+ off
854 + offsetof(struct ip6t_entry
, counters
),
856 sizeof(counters
[num
])) != 0) {
861 for (i
= sizeof(struct ip6t_entry
);
862 i
< e
->target_offset
;
863 i
+= m
->u
.match_size
) {
866 if (xt_match_to_user(m
, userptr
+ off
+ i
)) {
872 t
= ip6t_get_target_c(e
);
873 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
885 static void compat_standard_from_user(void *dst
, const void *src
)
887 int v
= *(compat_int_t
*)src
;
890 v
+= xt_compat_calc_jump(AF_INET6
, v
);
891 memcpy(dst
, &v
, sizeof(v
));
894 static int compat_standard_to_user(void __user
*dst
, const void *src
)
896 compat_int_t cv
= *(int *)src
;
899 cv
-= xt_compat_calc_jump(AF_INET6
, cv
);
900 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
903 static int compat_calc_entry(const struct ip6t_entry
*e
,
904 const struct xt_table_info
*info
,
905 const void *base
, struct xt_table_info
*newinfo
)
907 const struct xt_entry_match
*ematch
;
908 const struct xt_entry_target
*t
;
909 unsigned int entry_offset
;
912 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
913 entry_offset
= (void *)e
- base
;
914 xt_ematch_foreach(ematch
, e
)
915 off
+= xt_compat_match_offset(ematch
->u
.kernel
.match
);
916 t
= ip6t_get_target_c(e
);
917 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
918 newinfo
->size
-= off
;
919 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
923 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
924 if (info
->hook_entry
[i
] &&
925 (e
< (struct ip6t_entry
*)(base
+ info
->hook_entry
[i
])))
926 newinfo
->hook_entry
[i
] -= off
;
927 if (info
->underflow
[i
] &&
928 (e
< (struct ip6t_entry
*)(base
+ info
->underflow
[i
])))
929 newinfo
->underflow
[i
] -= off
;
934 static int compat_table_info(const struct xt_table_info
*info
,
935 struct xt_table_info
*newinfo
)
937 struct ip6t_entry
*iter
;
938 const void *loc_cpu_entry
;
941 if (!newinfo
|| !info
)
944 /* we dont care about newinfo->entries */
945 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
946 newinfo
->initial_entries
= 0;
947 loc_cpu_entry
= info
->entries
;
948 xt_compat_init_offsets(AF_INET6
, info
->number
);
949 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
950 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
958 static int get_info(struct net
*net
, void __user
*user
,
959 const int *len
, int compat
)
961 char name
[XT_TABLE_MAXNAMELEN
];
965 if (*len
!= sizeof(struct ip6t_getinfo
))
968 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
971 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
974 xt_compat_lock(AF_INET6
);
976 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
977 "ip6table_%s", name
);
979 struct ip6t_getinfo info
;
980 const struct xt_table_info
*private = t
->private;
982 struct xt_table_info tmp
;
985 ret
= compat_table_info(private, &tmp
);
986 xt_compat_flush_offsets(AF_INET6
);
990 memset(&info
, 0, sizeof(info
));
991 info
.valid_hooks
= t
->valid_hooks
;
992 memcpy(info
.hook_entry
, private->hook_entry
,
993 sizeof(info
.hook_entry
));
994 memcpy(info
.underflow
, private->underflow
,
995 sizeof(info
.underflow
));
996 info
.num_entries
= private->number
;
997 info
.size
= private->size
;
998 strcpy(info
.name
, name
);
1000 if (copy_to_user(user
, &info
, *len
) != 0)
1009 #ifdef CONFIG_COMPAT
1011 xt_compat_unlock(AF_INET6
);
1017 get_entries(struct net
*net
, struct ip6t_get_entries __user
*uptr
,
1021 struct ip6t_get_entries get
;
1024 if (*len
< sizeof(get
))
1026 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1028 if (*len
!= sizeof(struct ip6t_get_entries
) + get
.size
)
1031 get
.name
[sizeof(get
.name
) - 1] = '\0';
1033 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1035 struct xt_table_info
*private = t
->private;
1036 if (get
.size
== private->size
)
1037 ret
= copy_entries_to_user(private->size
,
1038 t
, uptr
->entrytable
);
1051 __do_replace(struct net
*net
, const char *name
, unsigned int valid_hooks
,
1052 struct xt_table_info
*newinfo
, unsigned int num_counters
,
1053 void __user
*counters_ptr
)
1057 struct xt_table_info
*oldinfo
;
1058 struct xt_counters
*counters
;
1059 struct ip6t_entry
*iter
;
1062 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
1068 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET6
, name
),
1069 "ip6table_%s", 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
))
1093 /* Get the old counters, and synchronize with replace */
1094 get_counters(oldinfo
, counters
);
1096 /* Decrease module usage counts and free resource */
1097 xt_entry_foreach(iter
, oldinfo
->entries
, oldinfo
->size
)
1098 cleanup_entry(iter
, net
);
1100 xt_free_table_info(oldinfo
);
1101 if (copy_to_user(counters_ptr
, counters
,
1102 sizeof(struct xt_counters
) * num_counters
) != 0) {
1103 /* Silent error, can't fail, new table is already in place */
1104 net_warn_ratelimited("ip6tables: counters copy to user failed while replacing table\n");
1113 free_newinfo_counters_untrans
:
1120 do_replace(struct net
*net
, const void __user
*user
, 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 (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1131 /* overflow check */
1132 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1134 if (tmp
.num_counters
== 0)
1137 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1139 newinfo
= xt_alloc_table_info(tmp
.size
);
1143 loc_cpu_entry
= newinfo
->entries
;
1144 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1150 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, &tmp
);
1154 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1155 tmp
.num_counters
, tmp
.counters
);
1157 goto free_newinfo_untrans
;
1160 free_newinfo_untrans
:
1161 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1162 cleanup_entry(iter
, net
);
1164 xt_free_table_info(newinfo
);
1169 do_add_counters(struct net
*net
, const void __user
*user
, unsigned int len
,
1173 struct xt_counters_info tmp
;
1174 struct xt_counters
*paddc
;
1176 const struct xt_table_info
*private;
1178 struct ip6t_entry
*iter
;
1179 unsigned int addend
;
1181 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1183 return PTR_ERR(paddc
);
1184 t
= xt_find_table_lock(net
, AF_INET6
, tmp
.name
);
1191 private = t
->private;
1192 if (private->number
!= tmp
.num_counters
) {
1194 goto unlock_up_free
;
1198 addend
= xt_write_recseq_begin();
1199 xt_entry_foreach(iter
, private->entries
, private->size
) {
1200 struct xt_counters
*tmp
;
1202 tmp
= xt_get_this_cpu_counter(&iter
->counters
);
1203 ADD_COUNTER(*tmp
, paddc
[i
].bcnt
, paddc
[i
].pcnt
);
1206 xt_write_recseq_end(addend
);
1217 #ifdef CONFIG_COMPAT
1218 struct compat_ip6t_replace
{
1219 char name
[XT_TABLE_MAXNAMELEN
];
1223 u32 hook_entry
[NF_INET_NUMHOOKS
];
1224 u32 underflow
[NF_INET_NUMHOOKS
];
1226 compat_uptr_t counters
; /* struct xt_counters * */
1227 struct compat_ip6t_entry entries
[0];
1231 compat_copy_entry_to_user(struct ip6t_entry
*e
, void __user
**dstptr
,
1232 unsigned int *size
, struct xt_counters
*counters
,
1235 struct xt_entry_target
*t
;
1236 struct compat_ip6t_entry __user
*ce
;
1237 u_int16_t target_offset
, next_offset
;
1238 compat_uint_t origsize
;
1239 const struct xt_entry_match
*ematch
;
1244 if (copy_to_user(ce
, e
, sizeof(struct ip6t_entry
)) != 0 ||
1245 copy_to_user(&ce
->counters
, &counters
[i
],
1246 sizeof(counters
[i
])) != 0)
1249 *dstptr
+= sizeof(struct compat_ip6t_entry
);
1250 *size
-= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1252 xt_ematch_foreach(ematch
, e
) {
1253 ret
= xt_compat_match_to_user(ematch
, dstptr
, size
);
1257 target_offset
= e
->target_offset
- (origsize
- *size
);
1258 t
= ip6t_get_target(e
);
1259 ret
= xt_compat_target_to_user(t
, dstptr
, size
);
1262 next_offset
= e
->next_offset
- (origsize
- *size
);
1263 if (put_user(target_offset
, &ce
->target_offset
) != 0 ||
1264 put_user(next_offset
, &ce
->next_offset
) != 0)
1270 compat_find_calc_match(struct xt_entry_match
*m
,
1271 const struct ip6t_ip6
*ipv6
,
1274 struct xt_match
*match
;
1276 match
= xt_request_find_match(NFPROTO_IPV6
, m
->u
.user
.name
,
1277 m
->u
.user
.revision
);
1279 return PTR_ERR(match
);
1281 m
->u
.kernel
.match
= match
;
1282 *size
+= xt_compat_match_offset(match
);
1286 static void compat_release_entry(struct compat_ip6t_entry
*e
)
1288 struct xt_entry_target
*t
;
1289 struct xt_entry_match
*ematch
;
1291 /* Cleanup all matches */
1292 xt_ematch_foreach(ematch
, e
)
1293 module_put(ematch
->u
.kernel
.match
->me
);
1294 t
= compat_ip6t_get_target(e
);
1295 module_put(t
->u
.kernel
.target
->me
);
1299 check_compat_entry_size_and_hooks(struct compat_ip6t_entry
*e
,
1300 struct xt_table_info
*newinfo
,
1302 const unsigned char *base
,
1303 const unsigned char *limit
)
1305 struct xt_entry_match
*ematch
;
1306 struct xt_entry_target
*t
;
1307 struct xt_target
*target
;
1308 unsigned int entry_offset
;
1312 if ((unsigned long)e
% __alignof__(struct compat_ip6t_entry
) != 0 ||
1313 (unsigned char *)e
+ sizeof(struct compat_ip6t_entry
) >= limit
||
1314 (unsigned char *)e
+ e
->next_offset
> limit
)
1317 if (e
->next_offset
< sizeof(struct compat_ip6t_entry
) +
1318 sizeof(struct compat_xt_entry_target
))
1321 if (!ip6_checkentry(&e
->ipv6
))
1324 ret
= xt_compat_check_entry_offsets(e
, e
->elems
,
1325 e
->target_offset
, e
->next_offset
);
1329 off
= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1330 entry_offset
= (void *)e
- (void *)base
;
1332 xt_ematch_foreach(ematch
, e
) {
1333 ret
= compat_find_calc_match(ematch
, &e
->ipv6
, &off
);
1335 goto release_matches
;
1339 t
= compat_ip6t_get_target(e
);
1340 target
= xt_request_find_target(NFPROTO_IPV6
, t
->u
.user
.name
,
1341 t
->u
.user
.revision
);
1342 if (IS_ERR(target
)) {
1343 ret
= PTR_ERR(target
);
1344 goto release_matches
;
1346 t
->u
.kernel
.target
= target
;
1348 off
+= xt_compat_target_offset(target
);
1350 ret
= xt_compat_add_offset(AF_INET6
, entry_offset
, off
);
1357 module_put(t
->u
.kernel
.target
->me
);
1359 xt_ematch_foreach(ematch
, e
) {
1362 module_put(ematch
->u
.kernel
.match
->me
);
1368 compat_copy_entry_from_user(struct compat_ip6t_entry
*e
, void **dstptr
,
1370 struct xt_table_info
*newinfo
, unsigned char *base
)
1372 struct xt_entry_target
*t
;
1373 struct ip6t_entry
*de
;
1374 unsigned int origsize
;
1376 struct xt_entry_match
*ematch
;
1380 memcpy(de
, e
, sizeof(struct ip6t_entry
));
1381 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1383 *dstptr
+= sizeof(struct ip6t_entry
);
1384 *size
+= sizeof(struct ip6t_entry
) - sizeof(struct compat_ip6t_entry
);
1386 xt_ematch_foreach(ematch
, e
)
1387 xt_compat_match_from_user(ematch
, dstptr
, size
);
1389 de
->target_offset
= e
->target_offset
- (origsize
- *size
);
1390 t
= compat_ip6t_get_target(e
);
1391 xt_compat_target_from_user(t
, dstptr
, size
);
1393 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1394 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
1395 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1396 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1397 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1398 newinfo
->underflow
[h
] -= origsize
- *size
;
1403 translate_compat_table(struct net
*net
,
1404 struct xt_table_info
**pinfo
,
1406 const struct compat_ip6t_replace
*compatr
)
1409 struct xt_table_info
*newinfo
, *info
;
1410 void *pos
, *entry0
, *entry1
;
1411 struct compat_ip6t_entry
*iter0
;
1412 struct ip6t_replace repl
;
1418 size
= compatr
->size
;
1419 info
->number
= compatr
->num_entries
;
1422 xt_compat_lock(AF_INET6
);
1423 xt_compat_init_offsets(AF_INET6
, compatr
->num_entries
);
1424 /* Walk through entries, checking offsets. */
1425 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1426 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1428 entry0
+ compatr
->size
);
1435 if (j
!= compatr
->num_entries
)
1439 newinfo
= xt_alloc_table_info(size
);
1443 newinfo
->number
= compatr
->num_entries
;
1444 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1445 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1446 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1448 entry1
= newinfo
->entries
;
1450 size
= compatr
->size
;
1451 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1452 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1455 /* all module references in entry0 are now gone. */
1456 xt_compat_flush_offsets(AF_INET6
);
1457 xt_compat_unlock(AF_INET6
);
1459 memcpy(&repl
, compatr
, sizeof(*compatr
));
1461 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1462 repl
.hook_entry
[i
] = newinfo
->hook_entry
[i
];
1463 repl
.underflow
[i
] = newinfo
->underflow
[i
];
1466 repl
.num_counters
= 0;
1467 repl
.counters
= NULL
;
1468 repl
.size
= newinfo
->size
;
1469 ret
= translate_table(net
, newinfo
, entry1
, &repl
);
1475 xt_free_table_info(info
);
1479 xt_free_table_info(newinfo
);
1482 xt_compat_flush_offsets(AF_INET6
);
1483 xt_compat_unlock(AF_INET6
);
1484 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1487 compat_release_entry(iter0
);
1493 compat_do_replace(struct net
*net
, void __user
*user
, unsigned int len
)
1496 struct compat_ip6t_replace tmp
;
1497 struct xt_table_info
*newinfo
;
1498 void *loc_cpu_entry
;
1499 struct ip6t_entry
*iter
;
1501 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1504 /* overflow check */
1505 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1507 if (tmp
.num_counters
== 0)
1510 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1512 newinfo
= xt_alloc_table_info(tmp
.size
);
1516 loc_cpu_entry
= newinfo
->entries
;
1517 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1523 ret
= translate_compat_table(net
, &newinfo
, &loc_cpu_entry
, &tmp
);
1527 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1528 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1530 goto free_newinfo_untrans
;
1533 free_newinfo_untrans
:
1534 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1535 cleanup_entry(iter
, net
);
1537 xt_free_table_info(newinfo
);
1542 compat_do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1547 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1551 case IP6T_SO_SET_REPLACE
:
1552 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1555 case IP6T_SO_SET_ADD_COUNTERS
:
1556 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1566 struct compat_ip6t_get_entries
{
1567 char name
[XT_TABLE_MAXNAMELEN
];
1569 struct compat_ip6t_entry entrytable
[0];
1573 compat_copy_entries_to_user(unsigned int total_size
, struct xt_table
*table
,
1574 void __user
*userptr
)
1576 struct xt_counters
*counters
;
1577 const struct xt_table_info
*private = table
->private;
1582 struct ip6t_entry
*iter
;
1584 counters
= alloc_counters(table
);
1585 if (IS_ERR(counters
))
1586 return PTR_ERR(counters
);
1590 xt_entry_foreach(iter
, private->entries
, total_size
) {
1591 ret
= compat_copy_entry_to_user(iter
, &pos
,
1592 &size
, counters
, i
++);
1602 compat_get_entries(struct net
*net
, struct compat_ip6t_get_entries __user
*uptr
,
1606 struct compat_ip6t_get_entries get
;
1609 if (*len
< sizeof(get
))
1612 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1615 if (*len
!= sizeof(struct compat_ip6t_get_entries
) + get
.size
)
1618 get
.name
[sizeof(get
.name
) - 1] = '\0';
1620 xt_compat_lock(AF_INET6
);
1621 t
= xt_find_table_lock(net
, AF_INET6
, get
.name
);
1623 const struct xt_table_info
*private = t
->private;
1624 struct xt_table_info info
;
1625 ret
= compat_table_info(private, &info
);
1626 if (!ret
&& get
.size
== info
.size
)
1627 ret
= compat_copy_entries_to_user(private->size
,
1628 t
, uptr
->entrytable
);
1632 xt_compat_flush_offsets(AF_INET6
);
1638 xt_compat_unlock(AF_INET6
);
1642 static int do_ip6t_get_ctl(struct sock
*, int, void __user
*, int *);
1645 compat_do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1649 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1653 case IP6T_SO_GET_INFO
:
1654 ret
= get_info(sock_net(sk
), user
, len
, 1);
1656 case IP6T_SO_GET_ENTRIES
:
1657 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1660 ret
= do_ip6t_get_ctl(sk
, cmd
, user
, len
);
1667 do_ip6t_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1671 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1675 case IP6T_SO_SET_REPLACE
:
1676 ret
= do_replace(sock_net(sk
), user
, len
);
1679 case IP6T_SO_SET_ADD_COUNTERS
:
1680 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1691 do_ip6t_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1695 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1699 case IP6T_SO_GET_INFO
:
1700 ret
= get_info(sock_net(sk
), user
, len
, 0);
1703 case IP6T_SO_GET_ENTRIES
:
1704 ret
= get_entries(sock_net(sk
), user
, len
);
1707 case IP6T_SO_GET_REVISION_MATCH
:
1708 case IP6T_SO_GET_REVISION_TARGET
: {
1709 struct xt_get_revision rev
;
1712 if (*len
!= sizeof(rev
)) {
1716 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1720 rev
.name
[sizeof(rev
.name
)-1] = 0;
1722 if (cmd
== IP6T_SO_GET_REVISION_TARGET
)
1727 try_then_request_module(xt_find_revision(AF_INET6
, rev
.name
,
1730 "ip6t_%s", rev
.name
);
1741 static void __ip6t_unregister_table(struct net
*net
, struct xt_table
*table
)
1743 struct xt_table_info
*private;
1744 void *loc_cpu_entry
;
1745 struct module
*table_owner
= table
->me
;
1746 struct ip6t_entry
*iter
;
1748 private = xt_unregister_table(table
);
1750 /* Decrease module usage counts and free resources */
1751 loc_cpu_entry
= private->entries
;
1752 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1753 cleanup_entry(iter
, net
);
1754 if (private->number
> private->initial_entries
)
1755 module_put(table_owner
);
1756 xt_free_table_info(private);
1759 int ip6t_register_table(struct net
*net
, const struct xt_table
*table
,
1760 const struct ip6t_replace
*repl
,
1761 const struct nf_hook_ops
*ops
,
1762 struct xt_table
**res
)
1765 struct xt_table_info
*newinfo
;
1766 struct xt_table_info bootstrap
= {0};
1767 void *loc_cpu_entry
;
1768 struct xt_table
*new_table
;
1770 newinfo
= xt_alloc_table_info(repl
->size
);
1774 loc_cpu_entry
= newinfo
->entries
;
1775 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1777 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, repl
);
1781 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1782 if (IS_ERR(new_table
)) {
1783 ret
= PTR_ERR(new_table
);
1787 /* set res now, will see skbs right after nf_register_net_hooks */
1788 WRITE_ONCE(*res
, new_table
);
1790 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1792 __ip6t_unregister_table(net
, new_table
);
1799 xt_free_table_info(newinfo
);
1803 void ip6t_unregister_table(struct net
*net
, struct xt_table
*table
,
1804 const struct nf_hook_ops
*ops
)
1806 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1807 __ip6t_unregister_table(net
, table
);
1810 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1812 icmp6_type_code_match(u_int8_t test_type
, u_int8_t min_code
, u_int8_t max_code
,
1813 u_int8_t type
, u_int8_t code
,
1816 return (type
== test_type
&& code
>= min_code
&& code
<= max_code
)
1821 icmp6_match(const struct sk_buff
*skb
, struct xt_action_param
*par
)
1823 const struct icmp6hdr
*ic
;
1824 struct icmp6hdr _icmph
;
1825 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1827 /* Must not be a fragment. */
1828 if (par
->fragoff
!= 0)
1831 ic
= skb_header_pointer(skb
, par
->thoff
, sizeof(_icmph
), &_icmph
);
1833 /* We've been asked to examine this packet, and we
1834 * can't. Hence, no choice but to drop.
1836 par
->hotdrop
= true;
1840 return icmp6_type_code_match(icmpinfo
->type
,
1843 ic
->icmp6_type
, ic
->icmp6_code
,
1844 !!(icmpinfo
->invflags
&IP6T_ICMP_INV
));
1847 /* Called when user tries to insert an entry of this type. */
1848 static int icmp6_checkentry(const struct xt_mtchk_param
*par
)
1850 const struct ip6t_icmp
*icmpinfo
= par
->matchinfo
;
1852 /* Must specify no unknown invflags */
1853 return (icmpinfo
->invflags
& ~IP6T_ICMP_INV
) ? -EINVAL
: 0;
1856 /* The built-in targets: standard (NULL) and error. */
1857 static struct xt_target ip6t_builtin_tg
[] __read_mostly
= {
1859 .name
= XT_STANDARD_TARGET
,
1860 .targetsize
= sizeof(int),
1861 .family
= NFPROTO_IPV6
,
1862 #ifdef CONFIG_COMPAT
1863 .compatsize
= sizeof(compat_int_t
),
1864 .compat_from_user
= compat_standard_from_user
,
1865 .compat_to_user
= compat_standard_to_user
,
1869 .name
= XT_ERROR_TARGET
,
1870 .target
= ip6t_error
,
1871 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1872 .family
= NFPROTO_IPV6
,
1876 static struct nf_sockopt_ops ip6t_sockopts
= {
1878 .set_optmin
= IP6T_BASE_CTL
,
1879 .set_optmax
= IP6T_SO_SET_MAX
+1,
1880 .set
= do_ip6t_set_ctl
,
1881 #ifdef CONFIG_COMPAT
1882 .compat_set
= compat_do_ip6t_set_ctl
,
1884 .get_optmin
= IP6T_BASE_CTL
,
1885 .get_optmax
= IP6T_SO_GET_MAX
+1,
1886 .get
= do_ip6t_get_ctl
,
1887 #ifdef CONFIG_COMPAT
1888 .compat_get
= compat_do_ip6t_get_ctl
,
1890 .owner
= THIS_MODULE
,
1893 static struct xt_match ip6t_builtin_mt
[] __read_mostly
= {
1896 .match
= icmp6_match
,
1897 .matchsize
= sizeof(struct ip6t_icmp
),
1898 .checkentry
= icmp6_checkentry
,
1899 .proto
= IPPROTO_ICMPV6
,
1900 .family
= NFPROTO_IPV6
,
1904 static int __net_init
ip6_tables_net_init(struct net
*net
)
1906 return xt_proto_init(net
, NFPROTO_IPV6
);
1909 static void __net_exit
ip6_tables_net_exit(struct net
*net
)
1911 xt_proto_fini(net
, NFPROTO_IPV6
);
1914 static struct pernet_operations ip6_tables_net_ops
= {
1915 .init
= ip6_tables_net_init
,
1916 .exit
= ip6_tables_net_exit
,
1919 static int __init
ip6_tables_init(void)
1923 ret
= register_pernet_subsys(&ip6_tables_net_ops
);
1927 /* No one else will be downing sem now, so we won't sleep */
1928 ret
= xt_register_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1931 ret
= xt_register_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1935 /* Register setsockopt */
1936 ret
= nf_register_sockopt(&ip6t_sockopts
);
1940 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1944 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1946 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1948 unregister_pernet_subsys(&ip6_tables_net_ops
);
1953 static void __exit
ip6_tables_fini(void)
1955 nf_unregister_sockopt(&ip6t_sockopts
);
1957 xt_unregister_matches(ip6t_builtin_mt
, ARRAY_SIZE(ip6t_builtin_mt
));
1958 xt_unregister_targets(ip6t_builtin_tg
, ARRAY_SIZE(ip6t_builtin_tg
));
1959 unregister_pernet_subsys(&ip6_tables_net_ops
);
1962 EXPORT_SYMBOL(ip6t_register_table
);
1963 EXPORT_SYMBOL(ip6t_unregister_table
);
1964 EXPORT_SYMBOL(ip6t_do_table
);
1966 module_init(ip6_tables_init
);
1967 module_exit(ip6_tables_fini
);