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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/cache.h>
14 #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/icmp.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/x_tables.h>
30 #include <linux/netfilter_ipv4/ip_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("IPv4 packet filter");
38 void *ipt_alloc_initial_table(const struct xt_table
*info
)
40 return xt_alloc_initial_table(ipt
, IPT
);
42 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table
);
44 /* Returns whether matches rule or not. */
45 /* Performance critical - called for every packet */
47 ip_packet_match(const struct iphdr
*ip
,
50 const struct ipt_ip
*ipinfo
,
55 if (NF_INVF(ipinfo
, IPT_INV_SRCIP
,
56 (ip
->saddr
& ipinfo
->smsk
.s_addr
) != ipinfo
->src
.s_addr
) ||
57 NF_INVF(ipinfo
, IPT_INV_DSTIP
,
58 (ip
->daddr
& ipinfo
->dmsk
.s_addr
) != ipinfo
->dst
.s_addr
))
61 ret
= ifname_compare_aligned(indev
, ipinfo
->iniface
, ipinfo
->iniface_mask
);
63 if (NF_INVF(ipinfo
, IPT_INV_VIA_IN
, ret
!= 0))
66 ret
= ifname_compare_aligned(outdev
, ipinfo
->outiface
, ipinfo
->outiface_mask
);
68 if (NF_INVF(ipinfo
, IPT_INV_VIA_OUT
, ret
!= 0))
71 /* Check specific protocol */
73 NF_INVF(ipinfo
, IPT_INV_PROTO
, ip
->protocol
!= ipinfo
->proto
))
76 /* If we have a fragment rule but the packet is not a fragment
77 * then we return zero */
78 if (NF_INVF(ipinfo
, IPT_INV_FRAG
,
79 (ipinfo
->flags
& IPT_F_FRAG
) && !isfrag
))
86 ip_checkentry(const struct ipt_ip
*ip
)
88 if (ip
->flags
& ~IPT_F_MASK
)
90 if (ip
->invflags
& ~IPT_INV_MASK
)
96 ipt_error(struct sk_buff
*skb
, const struct xt_action_param
*par
)
98 net_info_ratelimited("error: `%s'\n", (const char *)par
->targinfo
);
103 /* Performance critical */
104 static inline struct ipt_entry
*
105 get_entry(const void *base
, unsigned int offset
)
107 return (struct ipt_entry
*)(base
+ offset
);
110 /* All zeroes == unconditional rule. */
111 /* Mildly perf critical (only if packet tracing is on) */
112 static inline bool unconditional(const struct ipt_entry
*e
)
114 static const struct ipt_ip uncond
;
116 return e
->target_offset
== sizeof(struct ipt_entry
) &&
117 memcmp(&e
->ip
, &uncond
, sizeof(uncond
)) == 0;
120 /* for const-correctness */
121 static inline const struct xt_entry_target
*
122 ipt_get_target_c(const struct ipt_entry
*e
)
124 return ipt_get_target((struct ipt_entry
*)e
);
127 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
128 static const char *const hooknames
[] = {
129 [NF_INET_PRE_ROUTING
] = "PREROUTING",
130 [NF_INET_LOCAL_IN
] = "INPUT",
131 [NF_INET_FORWARD
] = "FORWARD",
132 [NF_INET_LOCAL_OUT
] = "OUTPUT",
133 [NF_INET_POST_ROUTING
] = "POSTROUTING",
136 enum nf_ip_trace_comments
{
137 NF_IP_TRACE_COMMENT_RULE
,
138 NF_IP_TRACE_COMMENT_RETURN
,
139 NF_IP_TRACE_COMMENT_POLICY
,
142 static const char *const comments
[] = {
143 [NF_IP_TRACE_COMMENT_RULE
] = "rule",
144 [NF_IP_TRACE_COMMENT_RETURN
] = "return",
145 [NF_IP_TRACE_COMMENT_POLICY
] = "policy",
148 static const struct nf_loginfo trace_loginfo
= {
149 .type
= NF_LOG_TYPE_LOG
,
153 .logflags
= NF_LOG_DEFAULT_MASK
,
158 /* Mildly perf critical (only if packet tracing is on) */
160 get_chainname_rulenum(const struct ipt_entry
*s
, const struct ipt_entry
*e
,
161 const char *hookname
, const char **chainname
,
162 const char **comment
, unsigned int *rulenum
)
164 const struct xt_standard_target
*t
= (void *)ipt_get_target_c(s
);
166 if (strcmp(t
->target
.u
.kernel
.target
->name
, XT_ERROR_TARGET
) == 0) {
167 /* Head of user chain: ERROR target with chainname */
168 *chainname
= t
->target
.data
;
173 if (unconditional(s
) &&
174 strcmp(t
->target
.u
.kernel
.target
->name
,
175 XT_STANDARD_TARGET
) == 0 &&
177 /* Tail of chains: STANDARD target (return/policy) */
178 *comment
= *chainname
== hookname
179 ? comments
[NF_IP_TRACE_COMMENT_POLICY
]
180 : comments
[NF_IP_TRACE_COMMENT_RETURN
];
189 static void trace_packet(struct net
*net
,
190 const struct sk_buff
*skb
,
192 const struct net_device
*in
,
193 const struct net_device
*out
,
194 const char *tablename
,
195 const struct xt_table_info
*private,
196 const struct ipt_entry
*e
)
198 const struct ipt_entry
*root
;
199 const char *hookname
, *chainname
, *comment
;
200 const struct ipt_entry
*iter
;
201 unsigned int rulenum
= 0;
203 root
= get_entry(private->entries
, private->hook_entry
[hook
]);
205 hookname
= chainname
= hooknames
[hook
];
206 comment
= comments
[NF_IP_TRACE_COMMENT_RULE
];
208 xt_entry_foreach(iter
, root
, private->size
- private->hook_entry
[hook
])
209 if (get_chainname_rulenum(iter
, e
, hookname
,
210 &chainname
, &comment
, &rulenum
) != 0)
213 nf_log_trace(net
, AF_INET
, hook
, skb
, in
, out
, &trace_loginfo
,
214 "TRACE: %s:%s:%s:%u ",
215 tablename
, chainname
, comment
, rulenum
);
220 struct ipt_entry
*ipt_next_entry(const struct ipt_entry
*entry
)
222 return (void *)entry
+ entry
->next_offset
;
225 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
227 ipt_do_table(struct sk_buff
*skb
,
228 const struct nf_hook_state
*state
,
229 struct xt_table
*table
)
231 unsigned int hook
= state
->hook
;
232 static const char nulldevname
[IFNAMSIZ
] __attribute__((aligned(sizeof(long))));
233 const struct iphdr
*ip
;
234 /* Initializing verdict to NF_DROP keeps gcc happy. */
235 unsigned int verdict
= NF_DROP
;
236 const char *indev
, *outdev
;
237 const void *table_base
;
238 struct ipt_entry
*e
, **jumpstack
;
239 unsigned int stackidx
, cpu
;
240 const struct xt_table_info
*private;
241 struct xt_action_param acpar
;
247 indev
= state
->in
? state
->in
->name
: nulldevname
;
248 outdev
= state
->out
? state
->out
->name
: nulldevname
;
249 /* We handle fragments by dealing with the first fragment as
250 * if it was a normal packet. All other fragments are treated
251 * normally, except that they will NEVER match rules that ask
252 * things we don't know, ie. tcp syn flag or ports). If the
253 * rule is also a fragment-specific rule, non-fragments won't
255 acpar
.fragoff
= ntohs(ip
->frag_off
) & IP_OFFSET
;
256 acpar
.thoff
= ip_hdrlen(skb
);
257 acpar
.hotdrop
= false;
260 WARN_ON(!(table
->valid_hooks
& (1 << hook
)));
262 addend
= xt_write_recseq_begin();
263 private = table
->private;
264 cpu
= smp_processor_id();
266 * Ensure we load private-> members after we've fetched the base
269 smp_read_barrier_depends();
270 table_base
= private->entries
;
271 jumpstack
= (struct ipt_entry
**)private->jumpstack
[cpu
];
273 /* Switch to alternate jumpstack if we're being invoked via TEE.
274 * TEE issues XT_CONTINUE verdict on original skb so we must not
275 * clobber the jumpstack.
277 * For recursion via REJECT or SYNPROXY the stack will be clobbered
278 * but it is no problem since absolute verdict is issued by these.
280 if (static_key_false(&xt_tee_enabled
))
281 jumpstack
+= private->stacksize
* __this_cpu_read(nf_skb_duplicated
);
283 e
= get_entry(table_base
, private->hook_entry
[hook
]);
286 const struct xt_entry_target
*t
;
287 const struct xt_entry_match
*ematch
;
288 struct xt_counters
*counter
;
291 if (!ip_packet_match(ip
, indev
, outdev
,
292 &e
->ip
, acpar
.fragoff
)) {
294 e
= ipt_next_entry(e
);
298 xt_ematch_foreach(ematch
, e
) {
299 acpar
.match
= ematch
->u
.kernel
.match
;
300 acpar
.matchinfo
= ematch
->data
;
301 if (!acpar
.match
->match(skb
, &acpar
))
305 counter
= xt_get_this_cpu_counter(&e
->counters
);
306 ADD_COUNTER(*counter
, skb
->len
, 1);
308 t
= ipt_get_target(e
);
309 WARN_ON(!t
->u
.kernel
.target
);
311 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
312 /* The packet is traced: log it */
313 if (unlikely(skb
->nf_trace
))
314 trace_packet(state
->net
, skb
, hook
, state
->in
,
315 state
->out
, table
->name
, private, e
);
317 /* Standard target? */
318 if (!t
->u
.kernel
.target
->target
) {
321 v
= ((struct xt_standard_target
*)t
)->verdict
;
323 /* Pop from stack? */
324 if (v
!= XT_RETURN
) {
325 verdict
= (unsigned int)(-v
) - 1;
329 e
= get_entry(table_base
,
330 private->underflow
[hook
]);
332 e
= jumpstack
[--stackidx
];
333 e
= ipt_next_entry(e
);
337 if (table_base
+ v
!= ipt_next_entry(e
) &&
338 !(e
->ip
.flags
& IPT_F_GOTO
))
339 jumpstack
[stackidx
++] = e
;
341 e
= get_entry(table_base
, v
);
345 acpar
.target
= t
->u
.kernel
.target
;
346 acpar
.targinfo
= t
->data
;
348 verdict
= t
->u
.kernel
.target
->target(skb
, &acpar
);
349 if (verdict
== XT_CONTINUE
) {
350 /* Target might have changed stuff. */
352 e
= ipt_next_entry(e
);
357 } while (!acpar
.hotdrop
);
359 xt_write_recseq_end(addend
);
367 /* Figures out from what hook each rule can be called: returns 0 if
368 there are loops. Puts hook bitmask in comefrom. */
370 mark_source_chains(const struct xt_table_info
*newinfo
,
371 unsigned int valid_hooks
, void *entry0
,
372 unsigned int *offsets
)
376 /* No recursion; use packet counter to save back ptrs (reset
377 to 0 as we leave), and comefrom to save source hook bitmask */
378 for (hook
= 0; hook
< NF_INET_NUMHOOKS
; hook
++) {
379 unsigned int pos
= newinfo
->hook_entry
[hook
];
380 struct ipt_entry
*e
= entry0
+ pos
;
382 if (!(valid_hooks
& (1 << hook
)))
385 /* Set initial back pointer. */
386 e
->counters
.pcnt
= pos
;
389 const struct xt_standard_target
*t
390 = (void *)ipt_get_target_c(e
);
391 int visited
= e
->comefrom
& (1 << hook
);
393 if (e
->comefrom
& (1 << NF_INET_NUMHOOKS
))
396 e
->comefrom
|= ((1 << hook
) | (1 << NF_INET_NUMHOOKS
));
398 /* Unconditional return/END. */
399 if ((unconditional(e
) &&
400 (strcmp(t
->target
.u
.user
.name
,
401 XT_STANDARD_TARGET
) == 0) &&
402 t
->verdict
< 0) || visited
) {
403 unsigned int oldpos
, size
;
405 if ((strcmp(t
->target
.u
.user
.name
,
406 XT_STANDARD_TARGET
) == 0) &&
407 t
->verdict
< -NF_MAX_VERDICT
- 1)
410 /* Return: backtrack through the last
413 e
->comefrom
^= (1<<NF_INET_NUMHOOKS
);
415 pos
= e
->counters
.pcnt
;
416 e
->counters
.pcnt
= 0;
418 /* We're at the start. */
423 } while (oldpos
== pos
+ e
->next_offset
);
426 size
= e
->next_offset
;
427 e
= entry0
+ pos
+ size
;
428 if (pos
+ size
>= newinfo
->size
)
430 e
->counters
.pcnt
= pos
;
433 int newpos
= t
->verdict
;
435 if (strcmp(t
->target
.u
.user
.name
,
436 XT_STANDARD_TARGET
) == 0 &&
438 /* This a jump; chase it. */
439 if (!xt_find_jump_offset(offsets
, newpos
,
444 /* ... this is a fallthru */
445 newpos
= pos
+ e
->next_offset
;
446 if (newpos
>= newinfo
->size
)
450 e
->counters
.pcnt
= pos
;
459 static void cleanup_match(struct xt_entry_match
*m
, struct net
*net
)
461 struct xt_mtdtor_param par
;
464 par
.match
= m
->u
.kernel
.match
;
465 par
.matchinfo
= m
->data
;
466 par
.family
= NFPROTO_IPV4
;
467 if (par
.match
->destroy
!= NULL
)
468 par
.match
->destroy(&par
);
469 module_put(par
.match
->me
);
473 check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
475 const struct ipt_ip
*ip
= par
->entryinfo
;
477 par
->match
= m
->u
.kernel
.match
;
478 par
->matchinfo
= m
->data
;
480 return xt_check_match(par
, m
->u
.match_size
- sizeof(*m
),
481 ip
->proto
, ip
->invflags
& IPT_INV_PROTO
);
485 find_check_match(struct xt_entry_match
*m
, struct xt_mtchk_param
*par
)
487 struct xt_match
*match
;
490 match
= xt_request_find_match(NFPROTO_IPV4
, m
->u
.user
.name
,
493 return PTR_ERR(match
);
494 m
->u
.kernel
.match
= match
;
496 ret
= check_match(m
, par
);
502 module_put(m
->u
.kernel
.match
->me
);
506 static int check_target(struct ipt_entry
*e
, struct net
*net
, const char *name
)
508 struct xt_entry_target
*t
= ipt_get_target(e
);
509 struct xt_tgchk_param par
= {
513 .target
= t
->u
.kernel
.target
,
515 .hook_mask
= e
->comefrom
,
516 .family
= NFPROTO_IPV4
,
519 return xt_check_target(&par
, t
->u
.target_size
- sizeof(*t
),
520 e
->ip
.proto
, e
->ip
.invflags
& IPT_INV_PROTO
);
524 find_check_entry(struct ipt_entry
*e
, struct net
*net
, const char *name
,
526 struct xt_percpu_counter_alloc_state
*alloc_state
)
528 struct xt_entry_target
*t
;
529 struct xt_target
*target
;
532 struct xt_mtchk_param mtpar
;
533 struct xt_entry_match
*ematch
;
535 if (!xt_percpu_counter_alloc(alloc_state
, &e
->counters
))
541 mtpar
.entryinfo
= &e
->ip
;
542 mtpar
.hook_mask
= e
->comefrom
;
543 mtpar
.family
= NFPROTO_IPV4
;
544 xt_ematch_foreach(ematch
, e
) {
545 ret
= find_check_match(ematch
, &mtpar
);
547 goto cleanup_matches
;
551 t
= ipt_get_target(e
);
552 target
= xt_request_find_target(NFPROTO_IPV4
, t
->u
.user
.name
,
554 if (IS_ERR(target
)) {
555 ret
= PTR_ERR(target
);
556 goto cleanup_matches
;
558 t
->u
.kernel
.target
= target
;
560 ret
= check_target(e
, net
, name
);
566 module_put(t
->u
.kernel
.target
->me
);
568 xt_ematch_foreach(ematch
, e
) {
571 cleanup_match(ematch
, net
);
574 xt_percpu_counter_free(&e
->counters
);
579 static bool check_underflow(const struct ipt_entry
*e
)
581 const struct xt_entry_target
*t
;
582 unsigned int verdict
;
584 if (!unconditional(e
))
586 t
= ipt_get_target_c(e
);
587 if (strcmp(t
->u
.user
.name
, XT_STANDARD_TARGET
) != 0)
589 verdict
= ((struct xt_standard_target
*)t
)->verdict
;
590 verdict
= -verdict
- 1;
591 return verdict
== NF_DROP
|| verdict
== NF_ACCEPT
;
595 check_entry_size_and_hooks(struct ipt_entry
*e
,
596 struct xt_table_info
*newinfo
,
597 const unsigned char *base
,
598 const unsigned char *limit
,
599 const unsigned int *hook_entries
,
600 const unsigned int *underflows
,
601 unsigned int valid_hooks
)
606 if ((unsigned long)e
% __alignof__(struct ipt_entry
) != 0 ||
607 (unsigned char *)e
+ sizeof(struct ipt_entry
) >= limit
||
608 (unsigned char *)e
+ e
->next_offset
> limit
)
612 < sizeof(struct ipt_entry
) + sizeof(struct xt_entry_target
))
615 if (!ip_checkentry(&e
->ip
))
618 err
= xt_check_entry_offsets(e
, e
->elems
, e
->target_offset
,
623 /* Check hooks & underflows */
624 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
625 if (!(valid_hooks
& (1 << h
)))
627 if ((unsigned char *)e
- base
== hook_entries
[h
])
628 newinfo
->hook_entry
[h
] = hook_entries
[h
];
629 if ((unsigned char *)e
- base
== underflows
[h
]) {
630 if (!check_underflow(e
))
633 newinfo
->underflow
[h
] = underflows
[h
];
637 /* Clear counters and comefrom */
638 e
->counters
= ((struct xt_counters
) { 0, 0 });
644 cleanup_entry(struct ipt_entry
*e
, struct net
*net
)
646 struct xt_tgdtor_param par
;
647 struct xt_entry_target
*t
;
648 struct xt_entry_match
*ematch
;
650 /* Cleanup all matches */
651 xt_ematch_foreach(ematch
, e
)
652 cleanup_match(ematch
, net
);
653 t
= ipt_get_target(e
);
656 par
.target
= t
->u
.kernel
.target
;
657 par
.targinfo
= t
->data
;
658 par
.family
= NFPROTO_IPV4
;
659 if (par
.target
->destroy
!= NULL
)
660 par
.target
->destroy(&par
);
661 module_put(par
.target
->me
);
662 xt_percpu_counter_free(&e
->counters
);
665 /* Checks and translates the user-supplied table segment (held in
668 translate_table(struct net
*net
, struct xt_table_info
*newinfo
, void *entry0
,
669 const struct ipt_replace
*repl
)
671 struct xt_percpu_counter_alloc_state alloc_state
= { 0 };
672 struct ipt_entry
*iter
;
673 unsigned int *offsets
;
677 newinfo
->size
= repl
->size
;
678 newinfo
->number
= repl
->num_entries
;
680 /* Init all hooks to impossible value. */
681 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
682 newinfo
->hook_entry
[i
] = 0xFFFFFFFF;
683 newinfo
->underflow
[i
] = 0xFFFFFFFF;
686 offsets
= xt_alloc_entry_offsets(newinfo
->number
);
690 /* Walk through entries, checking offsets. */
691 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
692 ret
= check_entry_size_and_hooks(iter
, newinfo
, entry0
,
699 if (i
< repl
->num_entries
)
700 offsets
[i
] = (void *)iter
- entry0
;
702 if (strcmp(ipt_get_target(iter
)->u
.user
.name
,
703 XT_ERROR_TARGET
) == 0)
704 ++newinfo
->stacksize
;
708 if (i
!= repl
->num_entries
)
711 /* Check hooks all assigned */
712 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
713 /* Only hooks which are valid */
714 if (!(repl
->valid_hooks
& (1 << i
)))
716 if (newinfo
->hook_entry
[i
] == 0xFFFFFFFF)
718 if (newinfo
->underflow
[i
] == 0xFFFFFFFF)
722 if (!mark_source_chains(newinfo
, repl
->valid_hooks
, entry0
, offsets
)) {
728 /* Finally, each sanity check must pass */
730 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
731 ret
= find_check_entry(iter
, net
, repl
->name
, repl
->size
,
739 xt_entry_foreach(iter
, entry0
, newinfo
->size
) {
742 cleanup_entry(iter
, net
);
754 get_counters(const struct xt_table_info
*t
,
755 struct xt_counters counters
[])
757 struct ipt_entry
*iter
;
761 for_each_possible_cpu(cpu
) {
762 seqcount_t
*s
= &per_cpu(xt_recseq
, cpu
);
765 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
766 struct xt_counters
*tmp
;
770 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
772 start
= read_seqcount_begin(s
);
775 } while (read_seqcount_retry(s
, start
));
777 ADD_COUNTER(counters
[i
], bcnt
, pcnt
);
778 ++i
; /* macro does multi eval of i */
784 static void get_old_counters(const struct xt_table_info
*t
,
785 struct xt_counters counters
[])
787 struct ipt_entry
*iter
;
790 for_each_possible_cpu(cpu
) {
792 xt_entry_foreach(iter
, t
->entries
, t
->size
) {
793 const struct xt_counters
*tmp
;
795 tmp
= xt_get_per_cpu_counter(&iter
->counters
, cpu
);
796 ADD_COUNTER(counters
[i
], tmp
->bcnt
, tmp
->pcnt
);
797 ++i
; /* macro does multi eval of i */
804 static struct xt_counters
*alloc_counters(const struct xt_table
*table
)
806 unsigned int countersize
;
807 struct xt_counters
*counters
;
808 const struct xt_table_info
*private = table
->private;
810 /* We need atomic snapshot of counters: rest doesn't change
811 (other than comefrom, which userspace doesn't care
813 countersize
= sizeof(struct xt_counters
) * private->number
;
814 counters
= vzalloc(countersize
);
816 if (counters
== NULL
)
817 return ERR_PTR(-ENOMEM
);
819 get_counters(private, counters
);
825 copy_entries_to_user(unsigned int total_size
,
826 const struct xt_table
*table
,
827 void __user
*userptr
)
829 unsigned int off
, num
;
830 const struct ipt_entry
*e
;
831 struct xt_counters
*counters
;
832 const struct xt_table_info
*private = table
->private;
834 const void *loc_cpu_entry
;
836 counters
= alloc_counters(table
);
837 if (IS_ERR(counters
))
838 return PTR_ERR(counters
);
840 loc_cpu_entry
= private->entries
;
842 /* FIXME: use iterator macros --RR */
843 /* ... then go back and fix counters and names */
844 for (off
= 0, num
= 0; off
< total_size
; off
+= e
->next_offset
, num
++){
846 const struct xt_entry_match
*m
;
847 const struct xt_entry_target
*t
;
849 e
= loc_cpu_entry
+ off
;
850 if (copy_to_user(userptr
+ off
, e
, sizeof(*e
))) {
854 if (copy_to_user(userptr
+ off
855 + offsetof(struct ipt_entry
, counters
),
857 sizeof(counters
[num
])) != 0) {
862 for (i
= sizeof(struct ipt_entry
);
863 i
< e
->target_offset
;
864 i
+= m
->u
.match_size
) {
867 if (xt_match_to_user(m
, userptr
+ off
+ i
)) {
873 t
= ipt_get_target_c(e
);
874 if (xt_target_to_user(t
, userptr
+ off
+ e
->target_offset
)) {
886 static void compat_standard_from_user(void *dst
, const void *src
)
888 int v
= *(compat_int_t
*)src
;
891 v
+= xt_compat_calc_jump(AF_INET
, v
);
892 memcpy(dst
, &v
, sizeof(v
));
895 static int compat_standard_to_user(void __user
*dst
, const void *src
)
897 compat_int_t cv
= *(int *)src
;
900 cv
-= xt_compat_calc_jump(AF_INET
, cv
);
901 return copy_to_user(dst
, &cv
, sizeof(cv
)) ? -EFAULT
: 0;
904 static int compat_calc_entry(const struct ipt_entry
*e
,
905 const struct xt_table_info
*info
,
906 const void *base
, struct xt_table_info
*newinfo
)
908 const struct xt_entry_match
*ematch
;
909 const struct xt_entry_target
*t
;
910 unsigned int entry_offset
;
913 off
= sizeof(struct ipt_entry
) - sizeof(struct compat_ipt_entry
);
914 entry_offset
= (void *)e
- base
;
915 xt_ematch_foreach(ematch
, e
)
916 off
+= xt_compat_match_offset(ematch
->u
.kernel
.match
);
917 t
= ipt_get_target_c(e
);
918 off
+= xt_compat_target_offset(t
->u
.kernel
.target
);
919 newinfo
->size
-= off
;
920 ret
= xt_compat_add_offset(AF_INET
, entry_offset
, off
);
924 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
925 if (info
->hook_entry
[i
] &&
926 (e
< (struct ipt_entry
*)(base
+ info
->hook_entry
[i
])))
927 newinfo
->hook_entry
[i
] -= off
;
928 if (info
->underflow
[i
] &&
929 (e
< (struct ipt_entry
*)(base
+ info
->underflow
[i
])))
930 newinfo
->underflow
[i
] -= off
;
935 static int compat_table_info(const struct xt_table_info
*info
,
936 struct xt_table_info
*newinfo
)
938 struct ipt_entry
*iter
;
939 const void *loc_cpu_entry
;
942 if (!newinfo
|| !info
)
945 /* we dont care about newinfo->entries */
946 memcpy(newinfo
, info
, offsetof(struct xt_table_info
, entries
));
947 newinfo
->initial_entries
= 0;
948 loc_cpu_entry
= info
->entries
;
949 xt_compat_init_offsets(AF_INET
, info
->number
);
950 xt_entry_foreach(iter
, loc_cpu_entry
, info
->size
) {
951 ret
= compat_calc_entry(iter
, info
, loc_cpu_entry
, newinfo
);
959 static int get_info(struct net
*net
, void __user
*user
,
960 const int *len
, int compat
)
962 char name
[XT_TABLE_MAXNAMELEN
];
966 if (*len
!= sizeof(struct ipt_getinfo
))
969 if (copy_from_user(name
, user
, sizeof(name
)) != 0)
972 name
[XT_TABLE_MAXNAMELEN
-1] = '\0';
975 xt_compat_lock(AF_INET
);
977 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET
, name
),
980 struct ipt_getinfo info
;
981 const struct xt_table_info
*private = t
->private;
983 struct xt_table_info tmp
;
986 ret
= compat_table_info(private, &tmp
);
987 xt_compat_flush_offsets(AF_INET
);
991 memset(&info
, 0, sizeof(info
));
992 info
.valid_hooks
= t
->valid_hooks
;
993 memcpy(info
.hook_entry
, private->hook_entry
,
994 sizeof(info
.hook_entry
));
995 memcpy(info
.underflow
, private->underflow
,
996 sizeof(info
.underflow
));
997 info
.num_entries
= private->number
;
998 info
.size
= private->size
;
999 strcpy(info
.name
, name
);
1001 if (copy_to_user(user
, &info
, *len
) != 0)
1010 #ifdef CONFIG_COMPAT
1012 xt_compat_unlock(AF_INET
);
1018 get_entries(struct net
*net
, struct ipt_get_entries __user
*uptr
,
1022 struct ipt_get_entries get
;
1025 if (*len
< sizeof(get
))
1027 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1029 if (*len
!= sizeof(struct ipt_get_entries
) + get
.size
)
1031 get
.name
[sizeof(get
.name
) - 1] = '\0';
1033 t
= xt_find_table_lock(net
, AF_INET
, get
.name
);
1035 const 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 ipt_entry
*iter
;
1062 counters
= vzalloc(num_counters
* sizeof(struct xt_counters
));
1068 t
= try_then_request_module(xt_find_table_lock(net
, AF_INET
, name
),
1069 "iptable_%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_old_counters(oldinfo
, counters
);
1095 /* Decrease module usage counts and free resource */
1096 xt_entry_foreach(iter
, oldinfo
->entries
, oldinfo
->size
)
1097 cleanup_entry(iter
, net
);
1099 xt_free_table_info(oldinfo
);
1100 if (copy_to_user(counters_ptr
, counters
,
1101 sizeof(struct xt_counters
) * num_counters
) != 0) {
1102 /* Silent error, can't fail, new table is already in place */
1103 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1112 free_newinfo_counters_untrans
:
1119 do_replace(struct net
*net
, const void __user
*user
, unsigned int len
)
1122 struct ipt_replace tmp
;
1123 struct xt_table_info
*newinfo
;
1124 void *loc_cpu_entry
;
1125 struct ipt_entry
*iter
;
1127 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1130 /* overflow check */
1131 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1133 if (tmp
.num_counters
== 0)
1136 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1138 newinfo
= xt_alloc_table_info(tmp
.size
);
1142 loc_cpu_entry
= newinfo
->entries
;
1143 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1149 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, &tmp
);
1153 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1154 tmp
.num_counters
, tmp
.counters
);
1156 goto free_newinfo_untrans
;
1159 free_newinfo_untrans
:
1160 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1161 cleanup_entry(iter
, net
);
1163 xt_free_table_info(newinfo
);
1168 do_add_counters(struct net
*net
, const void __user
*user
,
1169 unsigned int len
, int compat
)
1172 struct xt_counters_info tmp
;
1173 struct xt_counters
*paddc
;
1175 const struct xt_table_info
*private;
1177 struct ipt_entry
*iter
;
1178 unsigned int addend
;
1180 paddc
= xt_copy_counters_from_user(user
, len
, &tmp
, compat
);
1182 return PTR_ERR(paddc
);
1184 t
= xt_find_table_lock(net
, AF_INET
, 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_ipt_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_ipt_entry entries
[0];
1231 compat_copy_entry_to_user(struct ipt_entry
*e
, void __user
**dstptr
,
1232 unsigned int *size
, struct xt_counters
*counters
,
1235 struct xt_entry_target
*t
;
1236 struct compat_ipt_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 ipt_entry
)) != 0 ||
1245 copy_to_user(&ce
->counters
, &counters
[i
],
1246 sizeof(counters
[i
])) != 0)
1249 *dstptr
+= sizeof(struct compat_ipt_entry
);
1250 *size
-= sizeof(struct ipt_entry
) - sizeof(struct compat_ipt_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
= ipt_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 ipt_ip
*ip
,
1274 struct xt_match
*match
;
1276 match
= xt_request_find_match(NFPROTO_IPV4
, 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_ipt_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_ipt_get_target(e
);
1295 module_put(t
->u
.kernel
.target
->me
);
1299 check_compat_entry_size_and_hooks(struct compat_ipt_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_ipt_entry
) != 0 ||
1313 (unsigned char *)e
+ sizeof(struct compat_ipt_entry
) >= limit
||
1314 (unsigned char *)e
+ e
->next_offset
> limit
)
1317 if (e
->next_offset
< sizeof(struct compat_ipt_entry
) +
1318 sizeof(struct compat_xt_entry_target
))
1321 if (!ip_checkentry(&e
->ip
))
1324 ret
= xt_compat_check_entry_offsets(e
, e
->elems
,
1325 e
->target_offset
, e
->next_offset
);
1329 off
= sizeof(struct ipt_entry
) - sizeof(struct compat_ipt_entry
);
1330 entry_offset
= (void *)e
- (void *)base
;
1332 xt_ematch_foreach(ematch
, e
) {
1333 ret
= compat_find_calc_match(ematch
, &e
->ip
, &off
);
1335 goto release_matches
;
1339 t
= compat_ipt_get_target(e
);
1340 target
= xt_request_find_target(NFPROTO_IPV4
, 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_INET
, 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_ipt_entry
*e
, void **dstptr
,
1370 struct xt_table_info
*newinfo
, unsigned char *base
)
1372 struct xt_entry_target
*t
;
1373 struct ipt_entry
*de
;
1374 unsigned int origsize
;
1376 struct xt_entry_match
*ematch
;
1380 memcpy(de
, e
, sizeof(struct ipt_entry
));
1381 memcpy(&de
->counters
, &e
->counters
, sizeof(e
->counters
));
1383 *dstptr
+= sizeof(struct ipt_entry
);
1384 *size
+= sizeof(struct ipt_entry
) - sizeof(struct compat_ipt_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_ipt_get_target(e
);
1391 xt_compat_target_from_user(t
, dstptr
, size
);
1393 de
->next_offset
= e
->next_offset
- (origsize
- *size
);
1395 for (h
= 0; h
< NF_INET_NUMHOOKS
; h
++) {
1396 if ((unsigned char *)de
- base
< newinfo
->hook_entry
[h
])
1397 newinfo
->hook_entry
[h
] -= origsize
- *size
;
1398 if ((unsigned char *)de
- base
< newinfo
->underflow
[h
])
1399 newinfo
->underflow
[h
] -= origsize
- *size
;
1404 translate_compat_table(struct net
*net
,
1405 struct xt_table_info
**pinfo
,
1407 const struct compat_ipt_replace
*compatr
)
1410 struct xt_table_info
*newinfo
, *info
;
1411 void *pos
, *entry0
, *entry1
;
1412 struct compat_ipt_entry
*iter0
;
1413 struct ipt_replace repl
;
1419 size
= compatr
->size
;
1420 info
->number
= compatr
->num_entries
;
1423 xt_compat_lock(AF_INET
);
1424 xt_compat_init_offsets(AF_INET
, compatr
->num_entries
);
1425 /* Walk through entries, checking offsets. */
1426 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1427 ret
= check_compat_entry_size_and_hooks(iter0
, info
, &size
,
1429 entry0
+ compatr
->size
);
1436 if (j
!= compatr
->num_entries
)
1440 newinfo
= xt_alloc_table_info(size
);
1444 newinfo
->number
= compatr
->num_entries
;
1445 for (i
= 0; i
< NF_INET_NUMHOOKS
; i
++) {
1446 newinfo
->hook_entry
[i
] = compatr
->hook_entry
[i
];
1447 newinfo
->underflow
[i
] = compatr
->underflow
[i
];
1449 entry1
= newinfo
->entries
;
1451 size
= compatr
->size
;
1452 xt_entry_foreach(iter0
, entry0
, compatr
->size
)
1453 compat_copy_entry_from_user(iter0
, &pos
, &size
,
1456 /* all module references in entry0 are now gone.
1457 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1458 * generated by 64bit userspace.
1460 * Call standard translate_table() to validate all hook_entrys,
1461 * underflows, check for loops, etc.
1463 xt_compat_flush_offsets(AF_INET
);
1464 xt_compat_unlock(AF_INET
);
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_INET
);
1490 xt_compat_unlock(AF_INET
);
1491 xt_entry_foreach(iter0
, entry0
, compatr
->size
) {
1494 compat_release_entry(iter0
);
1500 compat_do_replace(struct net
*net
, void __user
*user
, unsigned int len
)
1503 struct compat_ipt_replace tmp
;
1504 struct xt_table_info
*newinfo
;
1505 void *loc_cpu_entry
;
1506 struct ipt_entry
*iter
;
1508 if (copy_from_user(&tmp
, user
, sizeof(tmp
)) != 0)
1511 /* overflow check */
1512 if (tmp
.num_counters
>= INT_MAX
/ sizeof(struct xt_counters
))
1514 if (tmp
.num_counters
== 0)
1517 tmp
.name
[sizeof(tmp
.name
)-1] = 0;
1519 newinfo
= xt_alloc_table_info(tmp
.size
);
1523 loc_cpu_entry
= newinfo
->entries
;
1524 if (copy_from_user(loc_cpu_entry
, user
+ sizeof(tmp
),
1530 ret
= translate_compat_table(net
, &newinfo
, &loc_cpu_entry
, &tmp
);
1534 ret
= __do_replace(net
, tmp
.name
, tmp
.valid_hooks
, newinfo
,
1535 tmp
.num_counters
, compat_ptr(tmp
.counters
));
1537 goto free_newinfo_untrans
;
1540 free_newinfo_untrans
:
1541 xt_entry_foreach(iter
, loc_cpu_entry
, newinfo
->size
)
1542 cleanup_entry(iter
, net
);
1544 xt_free_table_info(newinfo
);
1549 compat_do_ipt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
,
1554 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1558 case IPT_SO_SET_REPLACE
:
1559 ret
= compat_do_replace(sock_net(sk
), user
, len
);
1562 case IPT_SO_SET_ADD_COUNTERS
:
1563 ret
= do_add_counters(sock_net(sk
), user
, len
, 1);
1573 struct compat_ipt_get_entries
{
1574 char name
[XT_TABLE_MAXNAMELEN
];
1576 struct compat_ipt_entry entrytable
[0];
1580 compat_copy_entries_to_user(unsigned int total_size
, struct xt_table
*table
,
1581 void __user
*userptr
)
1583 struct xt_counters
*counters
;
1584 const struct xt_table_info
*private = table
->private;
1589 struct ipt_entry
*iter
;
1591 counters
= alloc_counters(table
);
1592 if (IS_ERR(counters
))
1593 return PTR_ERR(counters
);
1597 xt_entry_foreach(iter
, private->entries
, total_size
) {
1598 ret
= compat_copy_entry_to_user(iter
, &pos
,
1599 &size
, counters
, i
++);
1609 compat_get_entries(struct net
*net
, struct compat_ipt_get_entries __user
*uptr
,
1613 struct compat_ipt_get_entries get
;
1616 if (*len
< sizeof(get
))
1619 if (copy_from_user(&get
, uptr
, sizeof(get
)) != 0)
1622 if (*len
!= sizeof(struct compat_ipt_get_entries
) + get
.size
)
1625 get
.name
[sizeof(get
.name
) - 1] = '\0';
1627 xt_compat_lock(AF_INET
);
1628 t
= xt_find_table_lock(net
, AF_INET
, get
.name
);
1630 const struct xt_table_info
*private = t
->private;
1631 struct xt_table_info info
;
1632 ret
= compat_table_info(private, &info
);
1633 if (!ret
&& get
.size
== info
.size
)
1634 ret
= compat_copy_entries_to_user(private->size
,
1635 t
, uptr
->entrytable
);
1639 xt_compat_flush_offsets(AF_INET
);
1645 xt_compat_unlock(AF_INET
);
1649 static int do_ipt_get_ctl(struct sock
*, int, void __user
*, int *);
1652 compat_do_ipt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1656 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1660 case IPT_SO_GET_INFO
:
1661 ret
= get_info(sock_net(sk
), user
, len
, 1);
1663 case IPT_SO_GET_ENTRIES
:
1664 ret
= compat_get_entries(sock_net(sk
), user
, len
);
1667 ret
= do_ipt_get_ctl(sk
, cmd
, user
, len
);
1674 do_ipt_set_ctl(struct sock
*sk
, int cmd
, void __user
*user
, unsigned int len
)
1678 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1682 case IPT_SO_SET_REPLACE
:
1683 ret
= do_replace(sock_net(sk
), user
, len
);
1686 case IPT_SO_SET_ADD_COUNTERS
:
1687 ret
= do_add_counters(sock_net(sk
), user
, len
, 0);
1698 do_ipt_get_ctl(struct sock
*sk
, int cmd
, void __user
*user
, int *len
)
1702 if (!ns_capable(sock_net(sk
)->user_ns
, CAP_NET_ADMIN
))
1706 case IPT_SO_GET_INFO
:
1707 ret
= get_info(sock_net(sk
), user
, len
, 0);
1710 case IPT_SO_GET_ENTRIES
:
1711 ret
= get_entries(sock_net(sk
), user
, len
);
1714 case IPT_SO_GET_REVISION_MATCH
:
1715 case IPT_SO_GET_REVISION_TARGET
: {
1716 struct xt_get_revision rev
;
1719 if (*len
!= sizeof(rev
)) {
1723 if (copy_from_user(&rev
, user
, sizeof(rev
)) != 0) {
1727 rev
.name
[sizeof(rev
.name
)-1] = 0;
1729 if (cmd
== IPT_SO_GET_REVISION_TARGET
)
1734 try_then_request_module(xt_find_revision(AF_INET
, rev
.name
,
1737 "ipt_%s", rev
.name
);
1748 static void __ipt_unregister_table(struct net
*net
, struct xt_table
*table
)
1750 struct xt_table_info
*private;
1751 void *loc_cpu_entry
;
1752 struct module
*table_owner
= table
->me
;
1753 struct ipt_entry
*iter
;
1755 private = xt_unregister_table(table
);
1757 /* Decrease module usage counts and free resources */
1758 loc_cpu_entry
= private->entries
;
1759 xt_entry_foreach(iter
, loc_cpu_entry
, private->size
)
1760 cleanup_entry(iter
, net
);
1761 if (private->number
> private->initial_entries
)
1762 module_put(table_owner
);
1763 xt_free_table_info(private);
1766 int ipt_register_table(struct net
*net
, const struct xt_table
*table
,
1767 const struct ipt_replace
*repl
,
1768 const struct nf_hook_ops
*ops
, struct xt_table
**res
)
1771 struct xt_table_info
*newinfo
;
1772 struct xt_table_info bootstrap
= {0};
1773 void *loc_cpu_entry
;
1774 struct xt_table
*new_table
;
1776 newinfo
= xt_alloc_table_info(repl
->size
);
1780 loc_cpu_entry
= newinfo
->entries
;
1781 memcpy(loc_cpu_entry
, repl
->entries
, repl
->size
);
1783 ret
= translate_table(net
, newinfo
, loc_cpu_entry
, repl
);
1787 new_table
= xt_register_table(net
, table
, &bootstrap
, newinfo
);
1788 if (IS_ERR(new_table
)) {
1789 ret
= PTR_ERR(new_table
);
1793 /* set res now, will see skbs right after nf_register_net_hooks */
1794 WRITE_ONCE(*res
, new_table
);
1796 ret
= nf_register_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1798 __ipt_unregister_table(net
, new_table
);
1805 xt_free_table_info(newinfo
);
1809 void ipt_unregister_table(struct net
*net
, struct xt_table
*table
,
1810 const struct nf_hook_ops
*ops
)
1812 nf_unregister_net_hooks(net
, ops
, hweight32(table
->valid_hooks
));
1813 __ipt_unregister_table(net
, table
);
1816 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1818 icmp_type_code_match(u_int8_t test_type
, u_int8_t min_code
, u_int8_t max_code
,
1819 u_int8_t type
, u_int8_t code
,
1822 return ((test_type
== 0xFF) ||
1823 (type
== test_type
&& code
>= min_code
&& code
<= max_code
))
1828 icmp_match(const struct sk_buff
*skb
, struct xt_action_param
*par
)
1830 const struct icmphdr
*ic
;
1831 struct icmphdr _icmph
;
1832 const struct ipt_icmp
*icmpinfo
= par
->matchinfo
;
1834 /* Must not be a fragment. */
1835 if (par
->fragoff
!= 0)
1838 ic
= skb_header_pointer(skb
, par
->thoff
, sizeof(_icmph
), &_icmph
);
1840 /* We've been asked to examine this packet, and we
1841 * can't. Hence, no choice but to drop.
1843 par
->hotdrop
= true;
1847 return icmp_type_code_match(icmpinfo
->type
,
1851 !!(icmpinfo
->invflags
&IPT_ICMP_INV
));
1854 static int icmp_checkentry(const struct xt_mtchk_param
*par
)
1856 const struct ipt_icmp
*icmpinfo
= par
->matchinfo
;
1858 /* Must specify no unknown invflags */
1859 return (icmpinfo
->invflags
& ~IPT_ICMP_INV
) ? -EINVAL
: 0;
1862 static struct xt_target ipt_builtin_tg
[] __read_mostly
= {
1864 .name
= XT_STANDARD_TARGET
,
1865 .targetsize
= sizeof(int),
1866 .family
= NFPROTO_IPV4
,
1867 #ifdef CONFIG_COMPAT
1868 .compatsize
= sizeof(compat_int_t
),
1869 .compat_from_user
= compat_standard_from_user
,
1870 .compat_to_user
= compat_standard_to_user
,
1874 .name
= XT_ERROR_TARGET
,
1875 .target
= ipt_error
,
1876 .targetsize
= XT_FUNCTION_MAXNAMELEN
,
1877 .family
= NFPROTO_IPV4
,
1881 static struct nf_sockopt_ops ipt_sockopts
= {
1883 .set_optmin
= IPT_BASE_CTL
,
1884 .set_optmax
= IPT_SO_SET_MAX
+1,
1885 .set
= do_ipt_set_ctl
,
1886 #ifdef CONFIG_COMPAT
1887 .compat_set
= compat_do_ipt_set_ctl
,
1889 .get_optmin
= IPT_BASE_CTL
,
1890 .get_optmax
= IPT_SO_GET_MAX
+1,
1891 .get
= do_ipt_get_ctl
,
1892 #ifdef CONFIG_COMPAT
1893 .compat_get
= compat_do_ipt_get_ctl
,
1895 .owner
= THIS_MODULE
,
1898 static struct xt_match ipt_builtin_mt
[] __read_mostly
= {
1901 .match
= icmp_match
,
1902 .matchsize
= sizeof(struct ipt_icmp
),
1903 .checkentry
= icmp_checkentry
,
1904 .proto
= IPPROTO_ICMP
,
1905 .family
= NFPROTO_IPV4
,
1909 static int __net_init
ip_tables_net_init(struct net
*net
)
1911 return xt_proto_init(net
, NFPROTO_IPV4
);
1914 static void __net_exit
ip_tables_net_exit(struct net
*net
)
1916 xt_proto_fini(net
, NFPROTO_IPV4
);
1919 static struct pernet_operations ip_tables_net_ops
= {
1920 .init
= ip_tables_net_init
,
1921 .exit
= ip_tables_net_exit
,
1924 static int __init
ip_tables_init(void)
1928 ret
= register_pernet_subsys(&ip_tables_net_ops
);
1932 /* No one else will be downing sem now, so we won't sleep */
1933 ret
= xt_register_targets(ipt_builtin_tg
, ARRAY_SIZE(ipt_builtin_tg
));
1936 ret
= xt_register_matches(ipt_builtin_mt
, ARRAY_SIZE(ipt_builtin_mt
));
1940 /* Register setsockopt */
1941 ret
= nf_register_sockopt(&ipt_sockopts
);
1945 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1949 xt_unregister_matches(ipt_builtin_mt
, ARRAY_SIZE(ipt_builtin_mt
));
1951 xt_unregister_targets(ipt_builtin_tg
, ARRAY_SIZE(ipt_builtin_tg
));
1953 unregister_pernet_subsys(&ip_tables_net_ops
);
1958 static void __exit
ip_tables_fini(void)
1960 nf_unregister_sockopt(&ipt_sockopts
);
1962 xt_unregister_matches(ipt_builtin_mt
, ARRAY_SIZE(ipt_builtin_mt
));
1963 xt_unregister_targets(ipt_builtin_tg
, ARRAY_SIZE(ipt_builtin_tg
));
1964 unregister_pernet_subsys(&ip_tables_net_ops
);
1967 EXPORT_SYMBOL(ipt_register_table
);
1968 EXPORT_SYMBOL(ipt_unregister_table
);
1969 EXPORT_SYMBOL(ipt_do_table
);
1970 module_init(ip_tables_init
);
1971 module_exit(ip_tables_fini
);