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 <asm/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 #ifdef CONFIG_NETFILTER_DEBUG
39 #define IP_NF_ASSERT(x) WARN_ON(!(x))
41 #define IP_NF_ASSERT(x)
44 void *ipt_alloc_initial_table(const struct xt_table *info)
46 return xt_alloc_initial_table(ipt, IPT);
48 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
50 /* Returns whether matches rule or not. */
51 /* Performance critical - called for every packet */
53 ip_packet_match(const struct iphdr *ip,
56 const struct ipt_ip *ipinfo,
61 if (NF_INVF(ipinfo, IPT_INV_SRCIP,
62 (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
63 NF_INVF(ipinfo, IPT_INV_DSTIP,
64 (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
67 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
69 if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
72 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
74 if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
77 /* Check specific protocol */
79 NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
82 /* If we have a fragment rule but the packet is not a fragment
83 * then we return zero */
84 if (NF_INVF(ipinfo, IPT_INV_FRAG,
85 (ipinfo->flags & IPT_F_FRAG) && !isfrag))
92 ip_checkentry(const struct ipt_ip *ip)
94 if (ip->flags & ~IPT_F_MASK)
96 if (ip->invflags & ~IPT_INV_MASK)
102 ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
104 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
109 /* Performance critical */
110 static inline struct ipt_entry *
111 get_entry(const void *base, unsigned int offset)
113 return (struct ipt_entry *)(base + offset);
116 /* All zeroes == unconditional rule. */
117 /* Mildly perf critical (only if packet tracing is on) */
118 static inline bool unconditional(const struct ipt_entry *e)
120 static const struct ipt_ip uncond;
122 return e->target_offset == sizeof(struct ipt_entry) &&
123 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
126 /* for const-correctness */
127 static inline const struct xt_entry_target *
128 ipt_get_target_c(const struct ipt_entry *e)
130 return ipt_get_target((struct ipt_entry *)e);
133 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
134 static const char *const hooknames[] = {
135 [NF_INET_PRE_ROUTING] = "PREROUTING",
136 [NF_INET_LOCAL_IN] = "INPUT",
137 [NF_INET_FORWARD] = "FORWARD",
138 [NF_INET_LOCAL_OUT] = "OUTPUT",
139 [NF_INET_POST_ROUTING] = "POSTROUTING",
142 enum nf_ip_trace_comments {
143 NF_IP_TRACE_COMMENT_RULE,
144 NF_IP_TRACE_COMMENT_RETURN,
145 NF_IP_TRACE_COMMENT_POLICY,
148 static const char *const comments[] = {
149 [NF_IP_TRACE_COMMENT_RULE] = "rule",
150 [NF_IP_TRACE_COMMENT_RETURN] = "return",
151 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
154 static struct nf_loginfo trace_loginfo = {
155 .type = NF_LOG_TYPE_LOG,
159 .logflags = NF_LOG_DEFAULT_MASK,
164 /* Mildly perf critical (only if packet tracing is on) */
166 get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
167 const char *hookname, const char **chainname,
168 const char **comment, unsigned int *rulenum)
170 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
172 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
173 /* Head of user chain: ERROR target with chainname */
174 *chainname = t->target.data;
179 if (unconditional(s) &&
180 strcmp(t->target.u.kernel.target->name,
181 XT_STANDARD_TARGET) == 0 &&
183 /* Tail of chains: STANDARD target (return/policy) */
184 *comment = *chainname == hookname
185 ? comments[NF_IP_TRACE_COMMENT_POLICY]
186 : comments[NF_IP_TRACE_COMMENT_RETURN];
195 static void trace_packet(struct net *net,
196 const struct sk_buff *skb,
198 const struct net_device *in,
199 const struct net_device *out,
200 const char *tablename,
201 const struct xt_table_info *private,
202 const struct ipt_entry *e)
204 const struct ipt_entry *root;
205 const char *hookname, *chainname, *comment;
206 const struct ipt_entry *iter;
207 unsigned int rulenum = 0;
209 root = get_entry(private->entries, private->hook_entry[hook]);
211 hookname = chainname = hooknames[hook];
212 comment = comments[NF_IP_TRACE_COMMENT_RULE];
214 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
215 if (get_chainname_rulenum(iter, e, hookname,
216 &chainname, &comment, &rulenum) != 0)
219 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
220 "TRACE: %s:%s:%s:%u ",
221 tablename, chainname, comment, rulenum);
226 struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
228 return (void *)entry + entry->next_offset;
231 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
233 ipt_do_table(struct sk_buff *skb,
234 const struct nf_hook_state *state,
235 struct xt_table *table)
237 unsigned int hook = state->hook;
238 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
239 const struct iphdr *ip;
240 /* Initializing verdict to NF_DROP keeps gcc happy. */
241 unsigned int verdict = NF_DROP;
242 const char *indev, *outdev;
243 const void *table_base;
244 struct ipt_entry *e, **jumpstack;
245 unsigned int stackidx, cpu;
246 const struct xt_table_info *private;
247 struct xt_action_param acpar;
253 indev = state->in ? state->in->name : nulldevname;
254 outdev = state->out ? state->out->name : nulldevname;
255 /* We handle fragments by dealing with the first fragment as
256 * if it was a normal packet. All other fragments are treated
257 * normally, except that they will NEVER match rules that ask
258 * things we don't know, ie. tcp syn flag or ports). If the
259 * rule is also a fragment-specific rule, non-fragments won't
261 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
262 acpar.thoff = ip_hdrlen(skb);
263 acpar.hotdrop = false;
264 acpar.net = state->net;
265 acpar.in = state->in;
266 acpar.out = state->out;
267 acpar.family = NFPROTO_IPV4;
268 acpar.hooknum = hook;
270 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
272 addend = xt_write_recseq_begin();
273 private = table->private;
274 cpu = smp_processor_id();
276 * Ensure we load private-> members after we've fetched the base
279 smp_read_barrier_depends();
280 table_base = private->entries;
281 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
283 /* Switch to alternate jumpstack if we're being invoked via TEE.
284 * TEE issues XT_CONTINUE verdict on original skb so we must not
285 * clobber the jumpstack.
287 * For recursion via REJECT or SYNPROXY the stack will be clobbered
288 * but it is no problem since absolute verdict is issued by these.
290 if (static_key_false(&xt_tee_enabled))
291 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
293 e = get_entry(table_base, private->hook_entry[hook]);
296 const struct xt_entry_target *t;
297 const struct xt_entry_match *ematch;
298 struct xt_counters *counter;
301 if (!ip_packet_match(ip, indev, outdev,
302 &e->ip, acpar.fragoff)) {
304 e = ipt_next_entry(e);
308 xt_ematch_foreach(ematch, e) {
309 acpar.match = ematch->u.kernel.match;
310 acpar.matchinfo = ematch->data;
311 if (!acpar.match->match(skb, &acpar))
315 counter = xt_get_this_cpu_counter(&e->counters);
316 ADD_COUNTER(*counter, skb->len, 1);
318 t = ipt_get_target(e);
319 IP_NF_ASSERT(t->u.kernel.target);
321 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
322 /* The packet is traced: log it */
323 if (unlikely(skb->nf_trace))
324 trace_packet(state->net, skb, hook, state->in,
325 state->out, table->name, private, e);
327 /* Standard target? */
328 if (!t->u.kernel.target->target) {
331 v = ((struct xt_standard_target *)t)->verdict;
333 /* Pop from stack? */
334 if (v != XT_RETURN) {
335 verdict = (unsigned int)(-v) - 1;
339 e = get_entry(table_base,
340 private->underflow[hook]);
342 e = jumpstack[--stackidx];
343 e = ipt_next_entry(e);
347 if (table_base + v != ipt_next_entry(e) &&
348 !(e->ip.flags & IPT_F_GOTO)) {
349 if (unlikely(stackidx >= private->stacksize)) {
353 jumpstack[stackidx++] = e;
356 e = get_entry(table_base, v);
360 acpar.target = t->u.kernel.target;
361 acpar.targinfo = t->data;
363 verdict = t->u.kernel.target->target(skb, &acpar);
364 /* Target might have changed stuff. */
366 if (verdict == XT_CONTINUE)
367 e = ipt_next_entry(e);
371 } while (!acpar.hotdrop);
373 xt_write_recseq_end(addend);
381 /* Figures out from what hook each rule can be called: returns 0 if
382 there are loops. Puts hook bitmask in comefrom. */
384 mark_source_chains(const struct xt_table_info *newinfo,
385 unsigned int valid_hooks, void *entry0,
386 unsigned int *offsets)
390 /* No recursion; use packet counter to save back ptrs (reset
391 to 0 as we leave), and comefrom to save source hook bitmask */
392 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
393 unsigned int pos = newinfo->hook_entry[hook];
394 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
396 if (!(valid_hooks & (1 << hook)))
399 /* Set initial back pointer. */
400 e->counters.pcnt = pos;
403 const struct xt_standard_target *t
404 = (void *)ipt_get_target_c(e);
405 int visited = e->comefrom & (1 << hook);
407 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
410 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
412 /* Unconditional return/END. */
413 if ((unconditional(e) &&
414 (strcmp(t->target.u.user.name,
415 XT_STANDARD_TARGET) == 0) &&
416 t->verdict < 0) || visited) {
417 unsigned int oldpos, size;
419 if ((strcmp(t->target.u.user.name,
420 XT_STANDARD_TARGET) == 0) &&
421 t->verdict < -NF_MAX_VERDICT - 1)
424 /* Return: backtrack through the last
427 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
429 pos = e->counters.pcnt;
430 e->counters.pcnt = 0;
432 /* We're at the start. */
436 e = (struct ipt_entry *)
438 } while (oldpos == pos + e->next_offset);
441 size = e->next_offset;
442 e = (struct ipt_entry *)
443 (entry0 + pos + size);
444 if (pos + size >= newinfo->size)
446 e->counters.pcnt = pos;
449 int newpos = t->verdict;
451 if (strcmp(t->target.u.user.name,
452 XT_STANDARD_TARGET) == 0 &&
454 /* This a jump; chase it. */
455 if (!xt_find_jump_offset(offsets, newpos,
458 e = (struct ipt_entry *)
461 /* ... this is a fallthru */
462 newpos = pos + e->next_offset;
463 if (newpos >= newinfo->size)
466 e = (struct ipt_entry *)
468 e->counters.pcnt = pos;
477 static void cleanup_match(struct xt_entry_match *m, struct net *net)
479 struct xt_mtdtor_param par;
482 par.match = m->u.kernel.match;
483 par.matchinfo = m->data;
484 par.family = NFPROTO_IPV4;
485 if (par.match->destroy != NULL)
486 par.match->destroy(&par);
487 module_put(par.match->me);
491 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
493 const struct ipt_ip *ip = 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 ip->proto, ip->invflags & IPT_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_IPV4, m->u.user.name,
511 return PTR_ERR(match);
512 m->u.kernel.match = match;
514 ret = check_match(m, par);
520 module_put(m->u.kernel.match->me);
524 static int check_target(struct ipt_entry *e, struct net *net, const char *name)
526 struct xt_entry_target *t = ipt_get_target(e);
527 struct xt_tgchk_param par = {
531 .target = t->u.kernel.target,
533 .hook_mask = e->comefrom,
534 .family = NFPROTO_IPV4,
537 return xt_check_target(&par, t->u.target_size - sizeof(*t),
538 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
542 find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
544 struct xt_percpu_counter_alloc_state *alloc_state)
546 struct xt_entry_target *t;
547 struct xt_target *target;
550 struct xt_mtchk_param mtpar;
551 struct xt_entry_match *ematch;
553 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
557 memset(&mtpar, 0, sizeof(mtpar));
560 mtpar.entryinfo = &e->ip;
561 mtpar.hook_mask = e->comefrom;
562 mtpar.family = NFPROTO_IPV4;
563 xt_ematch_foreach(ematch, e) {
564 ret = find_check_match(ematch, &mtpar);
566 goto cleanup_matches;
570 t = ipt_get_target(e);
571 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
573 if (IS_ERR(target)) {
574 ret = PTR_ERR(target);
575 goto cleanup_matches;
577 t->u.kernel.target = target;
579 ret = check_target(e, net, name);
585 module_put(t->u.kernel.target->me);
587 xt_ematch_foreach(ematch, e) {
590 cleanup_match(ematch, net);
593 xt_percpu_counter_free(&e->counters);
598 static bool check_underflow(const struct ipt_entry *e)
600 const struct xt_entry_target *t;
601 unsigned int verdict;
603 if (!unconditional(e))
605 t = ipt_get_target_c(e);
606 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
608 verdict = ((struct xt_standard_target *)t)->verdict;
609 verdict = -verdict - 1;
610 return verdict == NF_DROP || verdict == NF_ACCEPT;
614 check_entry_size_and_hooks(struct ipt_entry *e,
615 struct xt_table_info *newinfo,
616 const unsigned char *base,
617 const unsigned char *limit,
618 const unsigned int *hook_entries,
619 const unsigned int *underflows,
620 unsigned int valid_hooks)
625 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
626 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
627 (unsigned char *)e + e->next_offset > limit)
631 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
634 if (!ip_checkentry(&e->ip))
637 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
642 /* Check hooks & underflows */
643 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
644 if (!(valid_hooks & (1 << h)))
646 if ((unsigned char *)e - base == hook_entries[h])
647 newinfo->hook_entry[h] = hook_entries[h];
648 if ((unsigned char *)e - base == underflows[h]) {
649 if (!check_underflow(e))
652 newinfo->underflow[h] = underflows[h];
656 /* Clear counters and comefrom */
657 e->counters = ((struct xt_counters) { 0, 0 });
663 cleanup_entry(struct ipt_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 = ipt_get_target(e);
675 par.target = t->u.kernel.target;
676 par.targinfo = t->data;
677 par.family = NFPROTO_IPV4;
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 ipt_replace *repl)
690 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
691 struct ipt_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(ipt_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 ipt_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);
797 ++i; /* macro does multi eval of i */
802 static struct xt_counters *alloc_counters(const struct xt_table *table)
804 unsigned int countersize;
805 struct xt_counters *counters;
806 const struct xt_table_info *private = table->private;
808 /* We need atomic snapshot of counters: rest doesn't change
809 (other than comefrom, which userspace doesn't care
811 countersize = sizeof(struct xt_counters) * private->number;
812 counters = vzalloc(countersize);
814 if (counters == NULL)
815 return ERR_PTR(-ENOMEM);
817 get_counters(private, counters);
823 copy_entries_to_user(unsigned int total_size,
824 const struct xt_table *table,
825 void __user *userptr)
827 unsigned int off, num;
828 const struct ipt_entry *e;
829 struct xt_counters *counters;
830 const struct xt_table_info *private = table->private;
832 const void *loc_cpu_entry;
834 counters = alloc_counters(table);
835 if (IS_ERR(counters))
836 return PTR_ERR(counters);
838 loc_cpu_entry = private->entries;
839 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
844 /* FIXME: use iterator macros --RR */
845 /* ... then go back and fix counters and names */
846 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
848 const struct xt_entry_match *m;
849 const struct xt_entry_target *t;
851 e = (struct ipt_entry *)(loc_cpu_entry + off);
852 if (copy_to_user(userptr + off
853 + offsetof(struct ipt_entry, counters),
855 sizeof(counters[num])) != 0) {
860 for (i = sizeof(struct ipt_entry);
861 i < e->target_offset;
862 i += m->u.match_size) {
865 if (copy_to_user(userptr + off + i
866 + offsetof(struct xt_entry_match,
868 m->u.kernel.match->name,
869 strlen(m->u.kernel.match->name)+1)
876 t = ipt_get_target_c(e);
877 if (copy_to_user(userptr + off + e->target_offset
878 + offsetof(struct xt_entry_target,
880 t->u.kernel.target->name,
881 strlen(t->u.kernel.target->name)+1) != 0) {
893 static void compat_standard_from_user(void *dst, const void *src)
895 int v = *(compat_int_t *)src;
898 v += xt_compat_calc_jump(AF_INET, v);
899 memcpy(dst, &v, sizeof(v));
902 static int compat_standard_to_user(void __user *dst, const void *src)
904 compat_int_t cv = *(int *)src;
907 cv -= xt_compat_calc_jump(AF_INET, cv);
908 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
911 static int compat_calc_entry(const struct ipt_entry *e,
912 const struct xt_table_info *info,
913 const void *base, struct xt_table_info *newinfo)
915 const struct xt_entry_match *ematch;
916 const struct xt_entry_target *t;
917 unsigned int entry_offset;
920 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
921 entry_offset = (void *)e - base;
922 xt_ematch_foreach(ematch, e)
923 off += xt_compat_match_offset(ematch->u.kernel.match);
924 t = ipt_get_target_c(e);
925 off += xt_compat_target_offset(t->u.kernel.target);
926 newinfo->size -= off;
927 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
931 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
932 if (info->hook_entry[i] &&
933 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
934 newinfo->hook_entry[i] -= off;
935 if (info->underflow[i] &&
936 (e < (struct ipt_entry *)(base + info->underflow[i])))
937 newinfo->underflow[i] -= off;
942 static int compat_table_info(const struct xt_table_info *info,
943 struct xt_table_info *newinfo)
945 struct ipt_entry *iter;
946 const void *loc_cpu_entry;
949 if (!newinfo || !info)
952 /* we dont care about newinfo->entries */
953 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
954 newinfo->initial_entries = 0;
955 loc_cpu_entry = info->entries;
956 xt_compat_init_offsets(AF_INET, info->number);
957 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
958 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
966 static int get_info(struct net *net, void __user *user,
967 const int *len, int compat)
969 char name[XT_TABLE_MAXNAMELEN];
973 if (*len != sizeof(struct ipt_getinfo))
976 if (copy_from_user(name, user, sizeof(name)) != 0)
979 name[XT_TABLE_MAXNAMELEN-1] = '\0';
982 xt_compat_lock(AF_INET);
984 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
986 if (!IS_ERR_OR_NULL(t)) {
987 struct ipt_getinfo info;
988 const struct xt_table_info *private = t->private;
990 struct xt_table_info tmp;
993 ret = compat_table_info(private, &tmp);
994 xt_compat_flush_offsets(AF_INET);
998 memset(&info, 0, sizeof(info));
999 info.valid_hooks = t->valid_hooks;
1000 memcpy(info.hook_entry, private->hook_entry,
1001 sizeof(info.hook_entry));
1002 memcpy(info.underflow, private->underflow,
1003 sizeof(info.underflow));
1004 info.num_entries = private->number;
1005 info.size = private->size;
1006 strcpy(info.name, name);
1008 if (copy_to_user(user, &info, *len) != 0)
1016 ret = t ? PTR_ERR(t) : -ENOENT;
1017 #ifdef CONFIG_COMPAT
1019 xt_compat_unlock(AF_INET);
1025 get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1029 struct ipt_get_entries get;
1032 if (*len < sizeof(get))
1034 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1036 if (*len != sizeof(struct ipt_get_entries) + get.size)
1038 get.name[sizeof(get.name) - 1] = '\0';
1040 t = xt_find_table_lock(net, AF_INET, get.name);
1041 if (!IS_ERR_OR_NULL(t)) {
1042 const struct xt_table_info *private = t->private;
1043 if (get.size == private->size)
1044 ret = copy_entries_to_user(private->size,
1045 t, uptr->entrytable);
1052 ret = t ? PTR_ERR(t) : -ENOENT;
1058 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1059 struct xt_table_info *newinfo, unsigned int num_counters,
1060 void __user *counters_ptr)
1064 struct xt_table_info *oldinfo;
1065 struct xt_counters *counters;
1066 struct ipt_entry *iter;
1069 counters = vzalloc(num_counters * sizeof(struct xt_counters));
1075 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1076 "iptable_%s", name);
1077 if (IS_ERR_OR_NULL(t)) {
1078 ret = t ? PTR_ERR(t) : -ENOENT;
1079 goto free_newinfo_counters_untrans;
1083 if (valid_hooks != t->valid_hooks) {
1088 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1092 /* Update module usage count based on number of rules */
1093 if ((oldinfo->number > oldinfo->initial_entries) ||
1094 (newinfo->number <= oldinfo->initial_entries))
1096 if ((oldinfo->number > oldinfo->initial_entries) &&
1097 (newinfo->number <= oldinfo->initial_entries))
1100 /* Get the old counters, and synchronize with replace */
1101 get_counters(oldinfo, counters);
1103 /* Decrease module usage counts and free resource */
1104 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1105 cleanup_entry(iter, net);
1107 xt_free_table_info(oldinfo);
1108 if (copy_to_user(counters_ptr, counters,
1109 sizeof(struct xt_counters) * num_counters) != 0) {
1110 /* Silent error, can't fail, new table is already in place */
1111 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1120 free_newinfo_counters_untrans:
1127 do_replace(struct net *net, const void __user *user, unsigned int len)
1130 struct ipt_replace tmp;
1131 struct xt_table_info *newinfo;
1132 void *loc_cpu_entry;
1133 struct ipt_entry *iter;
1135 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1138 /* overflow check */
1139 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1141 if (tmp.num_counters == 0)
1144 tmp.name[sizeof(tmp.name)-1] = 0;
1146 newinfo = xt_alloc_table_info(tmp.size);
1150 loc_cpu_entry = newinfo->entries;
1151 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1157 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1161 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1162 tmp.num_counters, tmp.counters);
1164 goto free_newinfo_untrans;
1167 free_newinfo_untrans:
1168 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1169 cleanup_entry(iter, net);
1171 xt_free_table_info(newinfo);
1176 do_add_counters(struct net *net, const void __user *user,
1177 unsigned int len, int compat)
1180 struct xt_counters_info tmp;
1181 struct xt_counters *paddc;
1183 const struct xt_table_info *private;
1185 struct ipt_entry *iter;
1186 unsigned int addend;
1188 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1190 return PTR_ERR(paddc);
1192 t = xt_find_table_lock(net, AF_INET, tmp.name);
1193 if (IS_ERR_OR_NULL(t)) {
1194 ret = t ? PTR_ERR(t) : -ENOENT;
1199 private = t->private;
1200 if (private->number != tmp.num_counters) {
1202 goto unlock_up_free;
1206 addend = xt_write_recseq_begin();
1207 xt_entry_foreach(iter, private->entries, private->size) {
1208 struct xt_counters *tmp;
1210 tmp = xt_get_this_cpu_counter(&iter->counters);
1211 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1214 xt_write_recseq_end(addend);
1225 #ifdef CONFIG_COMPAT
1226 struct compat_ipt_replace {
1227 char name[XT_TABLE_MAXNAMELEN];
1231 u32 hook_entry[NF_INET_NUMHOOKS];
1232 u32 underflow[NF_INET_NUMHOOKS];
1234 compat_uptr_t counters; /* struct xt_counters * */
1235 struct compat_ipt_entry entries[0];
1239 compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1240 unsigned int *size, struct xt_counters *counters,
1243 struct xt_entry_target *t;
1244 struct compat_ipt_entry __user *ce;
1245 u_int16_t target_offset, next_offset;
1246 compat_uint_t origsize;
1247 const struct xt_entry_match *ematch;
1251 ce = (struct compat_ipt_entry __user *)*dstptr;
1252 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1253 copy_to_user(&ce->counters, &counters[i],
1254 sizeof(counters[i])) != 0)
1257 *dstptr += sizeof(struct compat_ipt_entry);
1258 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1260 xt_ematch_foreach(ematch, e) {
1261 ret = xt_compat_match_to_user(ematch, dstptr, size);
1265 target_offset = e->target_offset - (origsize - *size);
1266 t = ipt_get_target(e);
1267 ret = xt_compat_target_to_user(t, dstptr, size);
1270 next_offset = e->next_offset - (origsize - *size);
1271 if (put_user(target_offset, &ce->target_offset) != 0 ||
1272 put_user(next_offset, &ce->next_offset) != 0)
1278 compat_find_calc_match(struct xt_entry_match *m,
1279 const struct ipt_ip *ip,
1282 struct xt_match *match;
1284 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1285 m->u.user.revision);
1287 return PTR_ERR(match);
1289 m->u.kernel.match = match;
1290 *size += xt_compat_match_offset(match);
1294 static void compat_release_entry(struct compat_ipt_entry *e)
1296 struct xt_entry_target *t;
1297 struct xt_entry_match *ematch;
1299 /* Cleanup all matches */
1300 xt_ematch_foreach(ematch, e)
1301 module_put(ematch->u.kernel.match->me);
1302 t = compat_ipt_get_target(e);
1303 module_put(t->u.kernel.target->me);
1307 check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1308 struct xt_table_info *newinfo,
1310 const unsigned char *base,
1311 const unsigned char *limit)
1313 struct xt_entry_match *ematch;
1314 struct xt_entry_target *t;
1315 struct xt_target *target;
1316 unsigned int entry_offset;
1320 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1321 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
1322 (unsigned char *)e + e->next_offset > limit)
1325 if (e->next_offset < sizeof(struct compat_ipt_entry) +
1326 sizeof(struct compat_xt_entry_target))
1329 if (!ip_checkentry(&e->ip))
1332 ret = xt_compat_check_entry_offsets(e, e->elems,
1333 e->target_offset, e->next_offset);
1337 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1338 entry_offset = (void *)e - (void *)base;
1340 xt_ematch_foreach(ematch, e) {
1341 ret = compat_find_calc_match(ematch, &e->ip, &off);
1343 goto release_matches;
1347 t = compat_ipt_get_target(e);
1348 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1349 t->u.user.revision);
1350 if (IS_ERR(target)) {
1351 ret = PTR_ERR(target);
1352 goto release_matches;
1354 t->u.kernel.target = target;
1356 off += xt_compat_target_offset(target);
1358 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1365 module_put(t->u.kernel.target->me);
1367 xt_ematch_foreach(ematch, e) {
1370 module_put(ematch->u.kernel.match->me);
1376 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1378 struct xt_table_info *newinfo, unsigned char *base)
1380 struct xt_entry_target *t;
1381 struct xt_target *target;
1382 struct ipt_entry *de;
1383 unsigned int origsize;
1385 struct xt_entry_match *ematch;
1388 de = (struct ipt_entry *)*dstptr;
1389 memcpy(de, e, sizeof(struct ipt_entry));
1390 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1392 *dstptr += sizeof(struct ipt_entry);
1393 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1395 xt_ematch_foreach(ematch, e)
1396 xt_compat_match_from_user(ematch, dstptr, size);
1398 de->target_offset = e->target_offset - (origsize - *size);
1399 t = compat_ipt_get_target(e);
1400 target = t->u.kernel.target;
1401 xt_compat_target_from_user(t, dstptr, size);
1403 de->next_offset = e->next_offset - (origsize - *size);
1405 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1406 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1407 newinfo->hook_entry[h] -= origsize - *size;
1408 if ((unsigned char *)de - base < newinfo->underflow[h])
1409 newinfo->underflow[h] -= origsize - *size;
1414 translate_compat_table(struct net *net,
1415 struct xt_table_info **pinfo,
1417 const struct compat_ipt_replace *compatr)
1420 struct xt_table_info *newinfo, *info;
1421 void *pos, *entry0, *entry1;
1422 struct compat_ipt_entry *iter0;
1423 struct ipt_replace repl;
1429 size = compatr->size;
1430 info->number = compatr->num_entries;
1433 xt_compat_lock(AF_INET);
1434 xt_compat_init_offsets(AF_INET, compatr->num_entries);
1435 /* Walk through entries, checking offsets. */
1436 xt_entry_foreach(iter0, entry0, compatr->size) {
1437 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1439 entry0 + compatr->size);
1446 if (j != compatr->num_entries)
1450 newinfo = xt_alloc_table_info(size);
1454 memset(newinfo->entries, 0, size);
1456 newinfo->number = compatr->num_entries;
1457 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1458 newinfo->hook_entry[i] = compatr->hook_entry[i];
1459 newinfo->underflow[i] = compatr->underflow[i];
1461 entry1 = newinfo->entries;
1463 size = compatr->size;
1464 xt_entry_foreach(iter0, entry0, compatr->size)
1465 compat_copy_entry_from_user(iter0, &pos, &size,
1468 /* all module references in entry0 are now gone.
1469 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1470 * generated by 64bit userspace.
1472 * Call standard translate_table() to validate all hook_entrys,
1473 * underflows, check for loops, etc.
1475 xt_compat_flush_offsets(AF_INET);
1476 xt_compat_unlock(AF_INET);
1478 memcpy(&repl, compatr, sizeof(*compatr));
1480 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1481 repl.hook_entry[i] = newinfo->hook_entry[i];
1482 repl.underflow[i] = newinfo->underflow[i];
1485 repl.num_counters = 0;
1486 repl.counters = NULL;
1487 repl.size = newinfo->size;
1488 ret = translate_table(net, newinfo, entry1, &repl);
1494 xt_free_table_info(info);
1498 xt_free_table_info(newinfo);
1501 xt_compat_flush_offsets(AF_INET);
1502 xt_compat_unlock(AF_INET);
1503 xt_entry_foreach(iter0, entry0, compatr->size) {
1506 compat_release_entry(iter0);
1512 compat_do_replace(struct net *net, void __user *user, unsigned int len)
1515 struct compat_ipt_replace tmp;
1516 struct xt_table_info *newinfo;
1517 void *loc_cpu_entry;
1518 struct ipt_entry *iter;
1520 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1523 /* overflow check */
1524 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1526 if (tmp.num_counters == 0)
1529 tmp.name[sizeof(tmp.name)-1] = 0;
1531 newinfo = xt_alloc_table_info(tmp.size);
1535 loc_cpu_entry = newinfo->entries;
1536 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1542 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1546 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1547 tmp.num_counters, compat_ptr(tmp.counters));
1549 goto free_newinfo_untrans;
1552 free_newinfo_untrans:
1553 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1554 cleanup_entry(iter, net);
1556 xt_free_table_info(newinfo);
1561 compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
1566 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1570 case IPT_SO_SET_REPLACE:
1571 ret = compat_do_replace(sock_net(sk), user, len);
1574 case IPT_SO_SET_ADD_COUNTERS:
1575 ret = do_add_counters(sock_net(sk), user, len, 1);
1585 struct compat_ipt_get_entries {
1586 char name[XT_TABLE_MAXNAMELEN];
1588 struct compat_ipt_entry entrytable[0];
1592 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1593 void __user *userptr)
1595 struct xt_counters *counters;
1596 const struct xt_table_info *private = table->private;
1601 struct ipt_entry *iter;
1603 counters = alloc_counters(table);
1604 if (IS_ERR(counters))
1605 return PTR_ERR(counters);
1609 xt_entry_foreach(iter, private->entries, total_size) {
1610 ret = compat_copy_entry_to_user(iter, &pos,
1611 &size, counters, i++);
1621 compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1625 struct compat_ipt_get_entries get;
1628 if (*len < sizeof(get))
1631 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1634 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
1637 get.name[sizeof(get.name) - 1] = '\0';
1639 xt_compat_lock(AF_INET);
1640 t = xt_find_table_lock(net, AF_INET, get.name);
1641 if (!IS_ERR_OR_NULL(t)) {
1642 const struct xt_table_info *private = t->private;
1643 struct xt_table_info info;
1644 ret = compat_table_info(private, &info);
1645 if (!ret && get.size == info.size)
1646 ret = compat_copy_entries_to_user(private->size,
1647 t, uptr->entrytable);
1651 xt_compat_flush_offsets(AF_INET);
1655 ret = t ? PTR_ERR(t) : -ENOENT;
1657 xt_compat_unlock(AF_INET);
1661 static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1664 compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1668 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1672 case IPT_SO_GET_INFO:
1673 ret = get_info(sock_net(sk), user, len, 1);
1675 case IPT_SO_GET_ENTRIES:
1676 ret = compat_get_entries(sock_net(sk), user, len);
1679 ret = do_ipt_get_ctl(sk, cmd, user, len);
1686 do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1690 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1694 case IPT_SO_SET_REPLACE:
1695 ret = do_replace(sock_net(sk), user, len);
1698 case IPT_SO_SET_ADD_COUNTERS:
1699 ret = do_add_counters(sock_net(sk), user, len, 0);
1710 do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1714 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1718 case IPT_SO_GET_INFO:
1719 ret = get_info(sock_net(sk), user, len, 0);
1722 case IPT_SO_GET_ENTRIES:
1723 ret = get_entries(sock_net(sk), user, len);
1726 case IPT_SO_GET_REVISION_MATCH:
1727 case IPT_SO_GET_REVISION_TARGET: {
1728 struct xt_get_revision rev;
1731 if (*len != sizeof(rev)) {
1735 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1739 rev.name[sizeof(rev.name)-1] = 0;
1741 if (cmd == IPT_SO_GET_REVISION_TARGET)
1746 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1749 "ipt_%s", rev.name);
1760 static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1762 struct xt_table_info *private;
1763 void *loc_cpu_entry;
1764 struct module *table_owner = table->me;
1765 struct ipt_entry *iter;
1767 private = xt_unregister_table(table);
1769 /* Decrease module usage counts and free resources */
1770 loc_cpu_entry = private->entries;
1771 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1772 cleanup_entry(iter, net);
1773 if (private->number > private->initial_entries)
1774 module_put(table_owner);
1775 xt_free_table_info(private);
1778 int ipt_register_table(struct net *net, const struct xt_table *table,
1779 const struct ipt_replace *repl,
1780 const struct nf_hook_ops *ops, struct xt_table **res)
1783 struct xt_table_info *newinfo;
1784 struct xt_table_info bootstrap = {0};
1785 void *loc_cpu_entry;
1786 struct xt_table *new_table;
1788 newinfo = xt_alloc_table_info(repl->size);
1792 loc_cpu_entry = newinfo->entries;
1793 memcpy(loc_cpu_entry, repl->entries, repl->size);
1795 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1799 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1800 if (IS_ERR(new_table)) {
1801 ret = PTR_ERR(new_table);
1805 /* set res now, will see skbs right after nf_register_net_hooks */
1806 WRITE_ONCE(*res, new_table);
1808 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1810 __ipt_unregister_table(net, new_table);
1817 xt_free_table_info(newinfo);
1821 void ipt_unregister_table(struct net *net, struct xt_table *table,
1822 const struct nf_hook_ops *ops)
1824 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1825 __ipt_unregister_table(net, table);
1828 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1830 icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1831 u_int8_t type, u_int8_t code,
1834 return ((test_type == 0xFF) ||
1835 (type == test_type && code >= min_code && code <= max_code))
1840 icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
1842 const struct icmphdr *ic;
1843 struct icmphdr _icmph;
1844 const struct ipt_icmp *icmpinfo = par->matchinfo;
1846 /* Must not be a fragment. */
1847 if (par->fragoff != 0)
1850 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1852 /* We've been asked to examine this packet, and we
1853 * can't. Hence, no choice but to drop.
1855 par->hotdrop = true;
1859 return icmp_type_code_match(icmpinfo->type,
1863 !!(icmpinfo->invflags&IPT_ICMP_INV));
1866 static int icmp_checkentry(const struct xt_mtchk_param *par)
1868 const struct ipt_icmp *icmpinfo = par->matchinfo;
1870 /* Must specify no unknown invflags */
1871 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
1874 static struct xt_target ipt_builtin_tg[] __read_mostly = {
1876 .name = XT_STANDARD_TARGET,
1877 .targetsize = sizeof(int),
1878 .family = NFPROTO_IPV4,
1879 #ifdef CONFIG_COMPAT
1880 .compatsize = sizeof(compat_int_t),
1881 .compat_from_user = compat_standard_from_user,
1882 .compat_to_user = compat_standard_to_user,
1886 .name = XT_ERROR_TARGET,
1887 .target = ipt_error,
1888 .targetsize = XT_FUNCTION_MAXNAMELEN,
1889 .family = NFPROTO_IPV4,
1893 static struct nf_sockopt_ops ipt_sockopts = {
1895 .set_optmin = IPT_BASE_CTL,
1896 .set_optmax = IPT_SO_SET_MAX+1,
1897 .set = do_ipt_set_ctl,
1898 #ifdef CONFIG_COMPAT
1899 .compat_set = compat_do_ipt_set_ctl,
1901 .get_optmin = IPT_BASE_CTL,
1902 .get_optmax = IPT_SO_GET_MAX+1,
1903 .get = do_ipt_get_ctl,
1904 #ifdef CONFIG_COMPAT
1905 .compat_get = compat_do_ipt_get_ctl,
1907 .owner = THIS_MODULE,
1910 static struct xt_match ipt_builtin_mt[] __read_mostly = {
1913 .match = icmp_match,
1914 .matchsize = sizeof(struct ipt_icmp),
1915 .checkentry = icmp_checkentry,
1916 .proto = IPPROTO_ICMP,
1917 .family = NFPROTO_IPV4,
1922 static int __net_init ip_tables_net_init(struct net *net)
1924 return xt_proto_init(net, NFPROTO_IPV4);
1927 static void __net_exit ip_tables_net_exit(struct net *net)
1929 xt_proto_fini(net, NFPROTO_IPV4);
1932 static struct pernet_operations ip_tables_net_ops = {
1933 .init = ip_tables_net_init,
1934 .exit = ip_tables_net_exit,
1937 static int __init ip_tables_init(void)
1941 ret = register_pernet_subsys(&ip_tables_net_ops);
1945 /* No one else will be downing sem now, so we won't sleep */
1946 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1949 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1953 /* Register setsockopt */
1954 ret = nf_register_sockopt(&ipt_sockopts);
1958 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1962 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1964 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1966 unregister_pernet_subsys(&ip_tables_net_ops);
1971 static void __exit ip_tables_fini(void)
1973 nf_unregister_sockopt(&ipt_sockopts);
1975 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1976 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1977 unregister_pernet_subsys(&ip_tables_net_ops);
1980 EXPORT_SYMBOL(ipt_register_table);
1981 EXPORT_SYMBOL(ipt_unregister_table);
1982 EXPORT_SYMBOL(ipt_do_table);
1983 module_init(ip_tables_init);
1984 module_exit(ip_tables_fini);