~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/ip_tables.c

Version: ~ [ linux-5.16-rc1 ] ~ [ linux-5.15.2 ] ~ [ linux-5.14.18 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.79 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.159 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.217 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.255 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.290 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.292 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Packet matching code.
  3  *
  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>
  7  *
  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.
 11  */
 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>
 21 #include <net/ip.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>
 28 
 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"
 33 
 34 MODULE_LICENSE("GPL");
 35 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 36 MODULE_DESCRIPTION("IPv4 packet filter");
 37 
 38 /*#define DEBUG_IP_FIREWALL*/
 39 /*#define DEBUG_ALLOW_ALL*/ /* Useful for remote debugging */
 40 /*#define DEBUG_IP_FIREWALL_USER*/
 41 
 42 #ifdef DEBUG_IP_FIREWALL
 43 #define dprintf(format, args...) pr_info(format , ## args)
 44 #else
 45 #define dprintf(format, args...)
 46 #endif
 47 
 48 #ifdef DEBUG_IP_FIREWALL_USER
 49 #define duprintf(format, args...) pr_info(format , ## args)
 50 #else
 51 #define duprintf(format, args...)
 52 #endif
 53 
 54 #ifdef CONFIG_NETFILTER_DEBUG
 55 #define IP_NF_ASSERT(x)         WARN_ON(!(x))
 56 #else
 57 #define IP_NF_ASSERT(x)
 58 #endif
 59 
 60 #if 0
 61 /* All the better to debug you with... */
 62 #define static
 63 #define inline
 64 #endif
 65 
 66 void *ipt_alloc_initial_table(const struct xt_table *info)
 67 {
 68         return xt_alloc_initial_table(ipt, IPT);
 69 }
 70 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
 71 
 72 /* Returns whether matches rule or not. */
 73 /* Performance critical - called for every packet */
 74 static inline bool
 75 ip_packet_match(const struct iphdr *ip,
 76                 const char *indev,
 77                 const char *outdev,
 78                 const struct ipt_ip *ipinfo,
 79                 int isfrag)
 80 {
 81         unsigned long ret;
 82 
 83 #define FWINV(bool, invflg) ((bool) ^ !!(ipinfo->invflags & (invflg)))
 84 
 85         if (FWINV((ip->saddr&ipinfo->smsk.s_addr) != ipinfo->src.s_addr,
 86                   IPT_INV_SRCIP) ||
 87             FWINV((ip->daddr&ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr,
 88                   IPT_INV_DSTIP)) {
 89                 dprintf("Source or dest mismatch.\n");
 90 
 91                 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
 92                         &ip->saddr, &ipinfo->smsk.s_addr, &ipinfo->src.s_addr,
 93                         ipinfo->invflags & IPT_INV_SRCIP ? " (INV)" : "");
 94                 dprintf("DST: %pI4 Mask: %pI4 Target: %pI4.%s\n",
 95                         &ip->daddr, &ipinfo->dmsk.s_addr, &ipinfo->dst.s_addr,
 96                         ipinfo->invflags & IPT_INV_DSTIP ? " (INV)" : "");
 97                 return false;
 98         }
 99 
100         ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
101 
102         if (FWINV(ret != 0, IPT_INV_VIA_IN)) {
103                 dprintf("VIA in mismatch (%s vs %s).%s\n",
104                         indev, ipinfo->iniface,
105                         ipinfo->invflags&IPT_INV_VIA_IN ?" (INV)":"");
106                 return false;
107         }
108 
109         ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
110 
111         if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
112                 dprintf("VIA out mismatch (%s vs %s).%s\n",
113                         outdev, ipinfo->outiface,
114                         ipinfo->invflags&IPT_INV_VIA_OUT ?" (INV)":"");
115                 return false;
116         }
117 
118         /* Check specific protocol */
119         if (ipinfo->proto &&
120             FWINV(ip->protocol != ipinfo->proto, IPT_INV_PROTO)) {
121                 dprintf("Packet protocol %hi does not match %hi.%s\n",
122                         ip->protocol, ipinfo->proto,
123                         ipinfo->invflags&IPT_INV_PROTO ? " (INV)":"");
124                 return false;
125         }
126 
127         /* If we have a fragment rule but the packet is not a fragment
128          * then we return zero */
129         if (FWINV((ipinfo->flags&IPT_F_FRAG) && !isfrag, IPT_INV_FRAG)) {
130                 dprintf("Fragment rule but not fragment.%s\n",
131                         ipinfo->invflags & IPT_INV_FRAG ? " (INV)" : "");
132                 return false;
133         }
134 
135         return true;
136 }
137 
138 static bool
139 ip_checkentry(const struct ipt_ip *ip)
140 {
141         if (ip->flags & ~IPT_F_MASK) {
142                 duprintf("Unknown flag bits set: %08X\n",
143                          ip->flags & ~IPT_F_MASK);
144                 return false;
145         }
146         if (ip->invflags & ~IPT_INV_MASK) {
147                 duprintf("Unknown invflag bits set: %08X\n",
148                          ip->invflags & ~IPT_INV_MASK);
149                 return false;
150         }
151         return true;
152 }
153 
154 static unsigned int
155 ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
156 {
157         net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
158 
159         return NF_DROP;
160 }
161 
162 /* Performance critical */
163 static inline struct ipt_entry *
164 get_entry(const void *base, unsigned int offset)
165 {
166         return (struct ipt_entry *)(base + offset);
167 }
168 
169 /* All zeroes == unconditional rule. */
170 /* Mildly perf critical (only if packet tracing is on) */
171 static inline bool unconditional(const struct ipt_entry *e)
172 {
173         static const struct ipt_ip uncond;
174 
175         return e->target_offset == sizeof(struct ipt_entry) &&
176                memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
177 #undef FWINV
178 }
179 
180 /* for const-correctness */
181 static inline const struct xt_entry_target *
182 ipt_get_target_c(const struct ipt_entry *e)
183 {
184         return ipt_get_target((struct ipt_entry *)e);
185 }
186 
187 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
188 static const char *const hooknames[] = {
189         [NF_INET_PRE_ROUTING]           = "PREROUTING",
190         [NF_INET_LOCAL_IN]              = "INPUT",
191         [NF_INET_FORWARD]               = "FORWARD",
192         [NF_INET_LOCAL_OUT]             = "OUTPUT",
193         [NF_INET_POST_ROUTING]          = "POSTROUTING",
194 };
195 
196 enum nf_ip_trace_comments {
197         NF_IP_TRACE_COMMENT_RULE,
198         NF_IP_TRACE_COMMENT_RETURN,
199         NF_IP_TRACE_COMMENT_POLICY,
200 };
201 
202 static const char *const comments[] = {
203         [NF_IP_TRACE_COMMENT_RULE]      = "rule",
204         [NF_IP_TRACE_COMMENT_RETURN]    = "return",
205         [NF_IP_TRACE_COMMENT_POLICY]    = "policy",
206 };
207 
208 static struct nf_loginfo trace_loginfo = {
209         .type = NF_LOG_TYPE_LOG,
210         .u = {
211                 .log = {
212                         .level = 4,
213                         .logflags = NF_LOG_MASK,
214                 },
215         },
216 };
217 
218 /* Mildly perf critical (only if packet tracing is on) */
219 static inline int
220 get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
221                       const char *hookname, const char **chainname,
222                       const char **comment, unsigned int *rulenum)
223 {
224         const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
225 
226         if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
227                 /* Head of user chain: ERROR target with chainname */
228                 *chainname = t->target.data;
229                 (*rulenum) = 0;
230         } else if (s == e) {
231                 (*rulenum)++;
232 
233                 if (unconditional(s) &&
234                     strcmp(t->target.u.kernel.target->name,
235                            XT_STANDARD_TARGET) == 0 &&
236                    t->verdict < 0) {
237                         /* Tail of chains: STANDARD target (return/policy) */
238                         *comment = *chainname == hookname
239                                 ? comments[NF_IP_TRACE_COMMENT_POLICY]
240                                 : comments[NF_IP_TRACE_COMMENT_RETURN];
241                 }
242                 return 1;
243         } else
244                 (*rulenum)++;
245 
246         return 0;
247 }
248 
249 static void trace_packet(const struct sk_buff *skb,
250                          unsigned int hook,
251                          const struct net_device *in,
252                          const struct net_device *out,
253                          const char *tablename,
254                          const struct xt_table_info *private,
255                          const struct ipt_entry *e)
256 {
257         const void *table_base;
258         const struct ipt_entry *root;
259         const char *hookname, *chainname, *comment;
260         const struct ipt_entry *iter;
261         unsigned int rulenum = 0;
262         struct net *net = dev_net(in ? in : out);
263 
264         table_base = private->entries[smp_processor_id()];
265         root = get_entry(table_base, private->hook_entry[hook]);
266 
267         hookname = chainname = hooknames[hook];
268         comment = comments[NF_IP_TRACE_COMMENT_RULE];
269 
270         xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
271                 if (get_chainname_rulenum(iter, e, hookname,
272                     &chainname, &comment, &rulenum) != 0)
273                         break;
274 
275         nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
276                      "TRACE: %s:%s:%s:%u ",
277                      tablename, chainname, comment, rulenum);
278 }
279 #endif
280 
281 static inline __pure
282 struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
283 {
284         return (void *)entry + entry->next_offset;
285 }
286 
287 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
288 unsigned int
289 ipt_do_table(struct sk_buff *skb,
290              unsigned int hook,
291              const struct nf_hook_state *state,
292              struct xt_table *table)
293 {
294         static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
295         const struct iphdr *ip;
296         /* Initializing verdict to NF_DROP keeps gcc happy. */
297         unsigned int verdict = NF_DROP;
298         const char *indev, *outdev;
299         const void *table_base;
300         struct ipt_entry *e, **jumpstack;
301         unsigned int *stackptr, origptr, cpu;
302         const struct xt_table_info *private;
303         struct xt_action_param acpar;
304         unsigned int addend;
305 
306         /* Initialization */
307         ip = ip_hdr(skb);
308         indev = state->in ? state->in->name : nulldevname;
309         outdev = state->out ? state->out->name : nulldevname;
310         /* We handle fragments by dealing with the first fragment as
311          * if it was a normal packet.  All other fragments are treated
312          * normally, except that they will NEVER match rules that ask
313          * things we don't know, ie. tcp syn flag or ports).  If the
314          * rule is also a fragment-specific rule, non-fragments won't
315          * match it. */
316         acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
317         acpar.thoff   = ip_hdrlen(skb);
318         acpar.hotdrop = false;
319         acpar.in      = state->in;
320         acpar.out     = state->out;
321         acpar.family  = NFPROTO_IPV4;
322         acpar.hooknum = hook;
323 
324         IP_NF_ASSERT(table->valid_hooks & (1 << hook));
325         local_bh_disable();
326         addend = xt_write_recseq_begin();
327         private = table->private;
328         cpu        = smp_processor_id();
329         /*
330          * Ensure we load private-> members after we've fetched the base
331          * pointer.
332          */
333         smp_read_barrier_depends();
334         table_base = private->entries[cpu];
335         jumpstack  = (struct ipt_entry **)private->jumpstack[cpu];
336         stackptr   = per_cpu_ptr(private->stackptr, cpu);
337         origptr    = *stackptr;
338 
339         e = get_entry(table_base, private->hook_entry[hook]);
340 
341         pr_debug("Entering %s(hook %u); sp at %u (UF %p)\n",
342                  table->name, hook, origptr,
343                  get_entry(table_base, private->underflow[hook]));
344 
345         do {
346                 const struct xt_entry_target *t;
347                 const struct xt_entry_match *ematch;
348 
349                 IP_NF_ASSERT(e);
350                 if (!ip_packet_match(ip, indev, outdev,
351                     &e->ip, acpar.fragoff)) {
352  no_match:
353                         e = ipt_next_entry(e);
354                         continue;
355                 }
356 
357                 xt_ematch_foreach(ematch, e) {
358                         acpar.match     = ematch->u.kernel.match;
359                         acpar.matchinfo = ematch->data;
360                         if (!acpar.match->match(skb, &acpar))
361                                 goto no_match;
362                 }
363 
364                 ADD_COUNTER(e->counters, skb->len, 1);
365 
366                 t = ipt_get_target(e);
367                 IP_NF_ASSERT(t->u.kernel.target);
368 
369 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
370                 /* The packet is traced: log it */
371                 if (unlikely(skb->nf_trace))
372                         trace_packet(skb, hook, state->in, state->out,
373                                      table->name, private, e);
374 #endif
375                 /* Standard target? */
376                 if (!t->u.kernel.target->target) {
377                         int v;
378 
379                         v = ((struct xt_standard_target *)t)->verdict;
380                         if (v < 0) {
381                                 /* Pop from stack? */
382                                 if (v != XT_RETURN) {
383                                         verdict = (unsigned int)(-v) - 1;
384                                         break;
385                                 }
386                                 if (*stackptr <= origptr) {
387                                         e = get_entry(table_base,
388                                             private->underflow[hook]);
389                                         pr_debug("Underflow (this is normal) "
390                                                  "to %p\n", e);
391                                 } else {
392                                         e = jumpstack[--*stackptr];
393                                         pr_debug("Pulled %p out from pos %u\n",
394                                                  e, *stackptr);
395                                         e = ipt_next_entry(e);
396                                 }
397                                 continue;
398                         }
399                         if (table_base + v != ipt_next_entry(e) &&
400                             !(e->ip.flags & IPT_F_GOTO)) {
401                                 if (*stackptr >= private->stacksize) {
402                                         verdict = NF_DROP;
403                                         break;
404                                 }
405                                 jumpstack[(*stackptr)++] = e;
406                                 pr_debug("Pushed %p into pos %u\n",
407                                          e, *stackptr - 1);
408                         }
409 
410                         e = get_entry(table_base, v);
411                         continue;
412                 }
413 
414                 acpar.target   = t->u.kernel.target;
415                 acpar.targinfo = t->data;
416 
417                 verdict = t->u.kernel.target->target(skb, &acpar);
418                 /* Target might have changed stuff. */
419                 ip = ip_hdr(skb);
420                 if (verdict == XT_CONTINUE)
421                         e = ipt_next_entry(e);
422                 else
423                         /* Verdict */
424                         break;
425         } while (!acpar.hotdrop);
426         pr_debug("Exiting %s; resetting sp from %u to %u\n",
427                  __func__, *stackptr, origptr);
428         *stackptr = origptr;
429         xt_write_recseq_end(addend);
430         local_bh_enable();
431 
432 #ifdef DEBUG_ALLOW_ALL
433         return NF_ACCEPT;
434 #else
435         if (acpar.hotdrop)
436                 return NF_DROP;
437         else return verdict;
438 #endif
439 }
440 
441 /* Figures out from what hook each rule can be called: returns 0 if
442    there are loops.  Puts hook bitmask in comefrom. */
443 static int
444 mark_source_chains(const struct xt_table_info *newinfo,
445                    unsigned int valid_hooks, void *entry0,
446                    unsigned int *offsets)
447 {
448         unsigned int hook;
449 
450         /* No recursion; use packet counter to save back ptrs (reset
451            to 0 as we leave), and comefrom to save source hook bitmask */
452         for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
453                 unsigned int pos = newinfo->hook_entry[hook];
454                 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
455 
456                 if (!(valid_hooks & (1 << hook)))
457                         continue;
458 
459                 /* Set initial back pointer. */
460                 e->counters.pcnt = pos;
461 
462                 for (;;) {
463                         const struct xt_standard_target *t
464                                 = (void *)ipt_get_target_c(e);
465                         int visited = e->comefrom & (1 << hook);
466 
467                         if (e->comefrom & (1 << NF_INET_NUMHOOKS)) {
468                                 pr_err("iptables: loop hook %u pos %u %08X.\n",
469                                        hook, pos, e->comefrom);
470                                 return 0;
471                         }
472                         e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
473 
474                         /* Unconditional return/END. */
475                         if ((unconditional(e) &&
476                              (strcmp(t->target.u.user.name,
477                                      XT_STANDARD_TARGET) == 0) &&
478                              t->verdict < 0) || visited) {
479                                 unsigned int oldpos, size;
480 
481                                 if ((strcmp(t->target.u.user.name,
482                                             XT_STANDARD_TARGET) == 0) &&
483                                     t->verdict < -NF_MAX_VERDICT - 1) {
484                                         duprintf("mark_source_chains: bad "
485                                                 "negative verdict (%i)\n",
486                                                                 t->verdict);
487                                         return 0;
488                                 }
489 
490                                 /* Return: backtrack through the last
491                                    big jump. */
492                                 do {
493                                         e->comefrom ^= (1<<NF_INET_NUMHOOKS);
494 #ifdef DEBUG_IP_FIREWALL_USER
495                                         if (e->comefrom
496                                             & (1 << NF_INET_NUMHOOKS)) {
497                                                 duprintf("Back unset "
498                                                          "on hook %u "
499                                                          "rule %u\n",
500                                                          hook, pos);
501                                         }
502 #endif
503                                         oldpos = pos;
504                                         pos = e->counters.pcnt;
505                                         e->counters.pcnt = 0;
506 
507                                         /* We're at the start. */
508                                         if (pos == oldpos)
509                                                 goto next;
510 
511                                         e = (struct ipt_entry *)
512                                                 (entry0 + pos);
513                                 } while (oldpos == pos + e->next_offset);
514 
515                                 /* Move along one */
516                                 size = e->next_offset;
517                                 e = (struct ipt_entry *)
518                                         (entry0 + pos + size);
519                                 if (pos + size >= newinfo->size)
520                                         return 0;
521                                 e->counters.pcnt = pos;
522                                 pos += size;
523                         } else {
524                                 int newpos = t->verdict;
525 
526                                 if (strcmp(t->target.u.user.name,
527                                            XT_STANDARD_TARGET) == 0 &&
528                                     newpos >= 0) {
529                                         if (newpos > newinfo->size -
530                                                 sizeof(struct ipt_entry)) {
531                                                 duprintf("mark_source_chains: "
532                                                         "bad verdict (%i)\n",
533                                                                 newpos);
534                                                 return 0;
535                                         }
536                                         /* This a jump; chase it. */
537                                         duprintf("Jump rule %u -> %u\n",
538                                                  pos, newpos);
539                                         if (!xt_find_jump_offset(offsets, newpos,
540                                                                  newinfo->number))
541                                                 return 0;
542                                         e = (struct ipt_entry *)
543                                                 (entry0 + newpos);
544                                 } else {
545                                         /* ... this is a fallthru */
546                                         newpos = pos + e->next_offset;
547                                         if (newpos >= newinfo->size)
548                                                 return 0;
549                                 }
550                                 e = (struct ipt_entry *)
551                                         (entry0 + newpos);
552                                 e->counters.pcnt = pos;
553                                 pos = newpos;
554                         }
555                 }
556                 next:
557                 duprintf("Finished chain %u\n", hook);
558         }
559         return 1;
560 }
561 
562 static void cleanup_match(struct xt_entry_match *m, struct net *net)
563 {
564         struct xt_mtdtor_param par;
565 
566         par.net       = net;
567         par.match     = m->u.kernel.match;
568         par.matchinfo = m->data;
569         par.family    = NFPROTO_IPV4;
570         if (par.match->destroy != NULL)
571                 par.match->destroy(&par);
572         module_put(par.match->me);
573 }
574 
575 static int
576 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
577 {
578         const struct ipt_ip *ip = par->entryinfo;
579         int ret;
580 
581         par->match     = m->u.kernel.match;
582         par->matchinfo = m->data;
583 
584         ret = xt_check_match(par, m->u.match_size - sizeof(*m),
585               ip->proto, ip->invflags & IPT_INV_PROTO);
586         if (ret < 0) {
587                 duprintf("check failed for `%s'.\n", par->match->name);
588                 return ret;
589         }
590         return 0;
591 }
592 
593 static int
594 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
595 {
596         struct xt_match *match;
597         int ret;
598 
599         match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
600                                       m->u.user.revision);
601         if (IS_ERR(match)) {
602                 duprintf("find_check_match: `%s' not found\n", m->u.user.name);
603                 return PTR_ERR(match);
604         }
605         m->u.kernel.match = match;
606 
607         ret = check_match(m, par);
608         if (ret)
609                 goto err;
610 
611         return 0;
612 err:
613         module_put(m->u.kernel.match->me);
614         return ret;
615 }
616 
617 static int check_target(struct ipt_entry *e, struct net *net, const char *name)
618 {
619         struct xt_entry_target *t = ipt_get_target(e);
620         struct xt_tgchk_param par = {
621                 .net       = net,
622                 .table     = name,
623                 .entryinfo = e,
624                 .target    = t->u.kernel.target,
625                 .targinfo  = t->data,
626                 .hook_mask = e->comefrom,
627                 .family    = NFPROTO_IPV4,
628         };
629         int ret;
630 
631         ret = xt_check_target(&par, t->u.target_size - sizeof(*t),
632               e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
633         if (ret < 0) {
634                 duprintf("check failed for `%s'.\n",
635                          t->u.kernel.target->name);
636                 return ret;
637         }
638         return 0;
639 }
640 
641 static int
642 find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
643                  unsigned int size)
644 {
645         struct xt_entry_target *t;
646         struct xt_target *target;
647         int ret;
648         unsigned int j;
649         struct xt_mtchk_param mtpar;
650         struct xt_entry_match *ematch;
651 
652         j = 0;
653         mtpar.net       = net;
654         mtpar.table     = name;
655         mtpar.entryinfo = &e->ip;
656         mtpar.hook_mask = e->comefrom;
657         mtpar.family    = NFPROTO_IPV4;
658         xt_ematch_foreach(ematch, e) {
659                 ret = find_check_match(ematch, &mtpar);
660                 if (ret != 0)
661                         goto cleanup_matches;
662                 ++j;
663         }
664 
665         t = ipt_get_target(e);
666         target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
667                                         t->u.user.revision);
668         if (IS_ERR(target)) {
669                 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
670                 ret = PTR_ERR(target);
671                 goto cleanup_matches;
672         }
673         t->u.kernel.target = target;
674 
675         ret = check_target(e, net, name);
676         if (ret)
677                 goto err;
678         return 0;
679  err:
680         module_put(t->u.kernel.target->me);
681  cleanup_matches:
682         xt_ematch_foreach(ematch, e) {
683                 if (j-- == 0)
684                         break;
685                 cleanup_match(ematch, net);
686         }
687         return ret;
688 }
689 
690 static bool check_underflow(const struct ipt_entry *e)
691 {
692         const struct xt_entry_target *t;
693         unsigned int verdict;
694 
695         if (!unconditional(e))
696                 return false;
697         t = ipt_get_target_c(e);
698         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
699                 return false;
700         verdict = ((struct xt_standard_target *)t)->verdict;
701         verdict = -verdict - 1;
702         return verdict == NF_DROP || verdict == NF_ACCEPT;
703 }
704 
705 static int
706 check_entry_size_and_hooks(struct ipt_entry *e,
707                            struct xt_table_info *newinfo,
708                            const unsigned char *base,
709                            const unsigned char *limit,
710                            const unsigned int *hook_entries,
711                            const unsigned int *underflows,
712                            unsigned int valid_hooks)
713 {
714         unsigned int h;
715         int err;
716 
717         if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
718             (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
719             (unsigned char *)e + e->next_offset > limit) {
720                 duprintf("Bad offset %p\n", e);
721                 return -EINVAL;
722         }
723 
724         if (e->next_offset
725             < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target)) {
726                 duprintf("checking: element %p size %u\n",
727                          e, e->next_offset);
728                 return -EINVAL;
729         }
730 
731         if (!ip_checkentry(&e->ip))
732                 return -EINVAL;
733 
734         err = xt_check_entry_offsets(e, e->elems, e->target_offset,
735                                      e->next_offset);
736         if (err)
737                 return err;
738 
739         /* Check hooks & underflows */
740         for (h = 0; h < NF_INET_NUMHOOKS; h++) {
741                 if (!(valid_hooks & (1 << h)))
742                         continue;
743                 if ((unsigned char *)e - base == hook_entries[h])
744                         newinfo->hook_entry[h] = hook_entries[h];
745                 if ((unsigned char *)e - base == underflows[h]) {
746                         if (!check_underflow(e)) {
747                                 pr_debug("Underflows must be unconditional and "
748                                          "use the STANDARD target with "
749                                          "ACCEPT/DROP\n");
750                                 return -EINVAL;
751                         }
752                         newinfo->underflow[h] = underflows[h];
753                 }
754         }
755 
756         /* Clear counters and comefrom */
757         e->counters = ((struct xt_counters) { 0, 0 });
758         e->comefrom = 0;
759         return 0;
760 }
761 
762 static void
763 cleanup_entry(struct ipt_entry *e, struct net *net)
764 {
765         struct xt_tgdtor_param par;
766         struct xt_entry_target *t;
767         struct xt_entry_match *ematch;
768 
769         /* Cleanup all matches */
770         xt_ematch_foreach(ematch, e)
771                 cleanup_match(ematch, net);
772         t = ipt_get_target(e);
773 
774         par.net      = net;
775         par.target   = t->u.kernel.target;
776         par.targinfo = t->data;
777         par.family   = NFPROTO_IPV4;
778         if (par.target->destroy != NULL)
779                 par.target->destroy(&par);
780         module_put(par.target->me);
781 }
782 
783 /* Checks and translates the user-supplied table segment (held in
784    newinfo) */
785 static int
786 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
787                 const struct ipt_replace *repl)
788 {
789         struct ipt_entry *iter;
790         unsigned int *offsets;
791         unsigned int i;
792         int ret = 0;
793 
794         newinfo->size = repl->size;
795         newinfo->number = repl->num_entries;
796 
797         /* Init all hooks to impossible value. */
798         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
799                 newinfo->hook_entry[i] = 0xFFFFFFFF;
800                 newinfo->underflow[i] = 0xFFFFFFFF;
801         }
802 
803         duprintf("translate_table: size %u\n", newinfo->size);
804         offsets = xt_alloc_entry_offsets(newinfo->number);
805         if (!offsets)
806                 return -ENOMEM;
807         i = 0;
808         /* Walk through entries, checking offsets. */
809         xt_entry_foreach(iter, entry0, newinfo->size) {
810                 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
811                                                  entry0 + repl->size,
812                                                  repl->hook_entry,
813                                                  repl->underflow,
814                                                  repl->valid_hooks);
815                 if (ret != 0)
816                         goto out_free;
817                 if (i < repl->num_entries)
818                         offsets[i] = (void *)iter - entry0;
819                 ++i;
820                 if (strcmp(ipt_get_target(iter)->u.user.name,
821                     XT_ERROR_TARGET) == 0)
822                         ++newinfo->stacksize;
823         }
824 
825         ret = -EINVAL;
826         if (i != repl->num_entries) {
827                 duprintf("translate_table: %u not %u entries\n",
828                          i, repl->num_entries);
829                 goto out_free;
830         }
831 
832         /* Check hooks all assigned */
833         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
834                 /* Only hooks which are valid */
835                 if (!(repl->valid_hooks & (1 << i)))
836                         continue;
837                 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
838                         duprintf("Invalid hook entry %u %u\n",
839                                  i, repl->hook_entry[i]);
840                         goto out_free;
841                 }
842                 if (newinfo->underflow[i] == 0xFFFFFFFF) {
843                         duprintf("Invalid underflow %u %u\n",
844                                  i, repl->underflow[i]);
845                         goto out_free;
846                 }
847         }
848 
849         if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
850                 ret = -ELOOP;
851                 goto out_free;
852         }
853         kvfree(offsets);
854 
855         /* Finally, each sanity check must pass */
856         i = 0;
857         xt_entry_foreach(iter, entry0, newinfo->size) {
858                 ret = find_check_entry(iter, net, repl->name, repl->size);
859                 if (ret != 0)
860                         break;
861                 ++i;
862         }
863 
864         if (ret != 0) {
865                 xt_entry_foreach(iter, entry0, newinfo->size) {
866                         if (i-- == 0)
867                                 break;
868                         cleanup_entry(iter, net);
869                 }
870                 return ret;
871         }
872 
873         /* And one copy for every other CPU */
874         for_each_possible_cpu(i) {
875                 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
876                         memcpy(newinfo->entries[i], entry0, newinfo->size);
877         }
878 
879         return ret;
880  out_free:
881         kvfree(offsets);
882         return ret;
883 }
884 
885 static void
886 get_counters(const struct xt_table_info *t,
887              struct xt_counters counters[])
888 {
889         struct ipt_entry *iter;
890         unsigned int cpu;
891         unsigned int i;
892 
893         for_each_possible_cpu(cpu) {
894                 seqcount_t *s = &per_cpu(xt_recseq, cpu);
895 
896                 i = 0;
897                 xt_entry_foreach(iter, t->entries[cpu], t->size) {
898                         u64 bcnt, pcnt;
899                         unsigned int start;
900 
901                         do {
902                                 start = read_seqcount_begin(s);
903                                 bcnt = iter->counters.bcnt;
904                                 pcnt = iter->counters.pcnt;
905                         } while (read_seqcount_retry(s, start));
906 
907                         ADD_COUNTER(counters[i], bcnt, pcnt);
908                         ++i; /* macro does multi eval of i */
909                 }
910         }
911 }
912 
913 static struct xt_counters *alloc_counters(const struct xt_table *table)
914 {
915         unsigned int countersize;
916         struct xt_counters *counters;
917         const struct xt_table_info *private = table->private;
918 
919         /* We need atomic snapshot of counters: rest doesn't change
920            (other than comefrom, which userspace doesn't care
921            about). */
922         countersize = sizeof(struct xt_counters) * private->number;
923         counters = vzalloc(countersize);
924 
925         if (counters == NULL)
926                 return ERR_PTR(-ENOMEM);
927 
928         get_counters(private, counters);
929 
930         return counters;
931 }
932 
933 static int
934 copy_entries_to_user(unsigned int total_size,
935                      const struct xt_table *table,
936                      void __user *userptr)
937 {
938         unsigned int off, num;
939         const struct ipt_entry *e;
940         struct xt_counters *counters;
941         const struct xt_table_info *private = table->private;
942         int ret = 0;
943         const void *loc_cpu_entry;
944 
945         counters = alloc_counters(table);
946         if (IS_ERR(counters))
947                 return PTR_ERR(counters);
948 
949         /* choose the copy that is on our node/cpu, ...
950          * This choice is lazy (because current thread is
951          * allowed to migrate to another cpu)
952          */
953         loc_cpu_entry = private->entries[raw_smp_processor_id()];
954         if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
955                 ret = -EFAULT;
956                 goto free_counters;
957         }
958 
959         /* FIXME: use iterator macros --RR */
960         /* ... then go back and fix counters and names */
961         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
962                 unsigned int i;
963                 const struct xt_entry_match *m;
964                 const struct xt_entry_target *t;
965 
966                 e = (struct ipt_entry *)(loc_cpu_entry + off);
967                 if (copy_to_user(userptr + off
968                                  + offsetof(struct ipt_entry, counters),
969                                  &counters[num],
970                                  sizeof(counters[num])) != 0) {
971                         ret = -EFAULT;
972                         goto free_counters;
973                 }
974 
975                 for (i = sizeof(struct ipt_entry);
976                      i < e->target_offset;
977                      i += m->u.match_size) {
978                         m = (void *)e + i;
979 
980                         if (copy_to_user(userptr + off + i
981                                          + offsetof(struct xt_entry_match,
982                                                     u.user.name),
983                                          m->u.kernel.match->name,
984                                          strlen(m->u.kernel.match->name)+1)
985                             != 0) {
986                                 ret = -EFAULT;
987                                 goto free_counters;
988                         }
989                 }
990 
991                 t = ipt_get_target_c(e);
992                 if (copy_to_user(userptr + off + e->target_offset
993                                  + offsetof(struct xt_entry_target,
994                                             u.user.name),
995                                  t->u.kernel.target->name,
996                                  strlen(t->u.kernel.target->name)+1) != 0) {
997                         ret = -EFAULT;
998                         goto free_counters;
999                 }
1000         }
1001 
1002  free_counters:
1003         vfree(counters);
1004         return ret;
1005 }
1006 
1007 #ifdef CONFIG_COMPAT
1008 static void compat_standard_from_user(void *dst, const void *src)
1009 {
1010         int v = *(compat_int_t *)src;
1011 
1012         if (v > 0)
1013                 v += xt_compat_calc_jump(AF_INET, v);
1014         memcpy(dst, &v, sizeof(v));
1015 }
1016 
1017 static int compat_standard_to_user(void __user *dst, const void *src)
1018 {
1019         compat_int_t cv = *(int *)src;
1020 
1021         if (cv > 0)
1022                 cv -= xt_compat_calc_jump(AF_INET, cv);
1023         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
1024 }
1025 
1026 static int compat_calc_entry(const struct ipt_entry *e,
1027                              const struct xt_table_info *info,
1028                              const void *base, struct xt_table_info *newinfo)
1029 {
1030         const struct xt_entry_match *ematch;
1031         const struct xt_entry_target *t;
1032         unsigned int entry_offset;
1033         int off, i, ret;
1034 
1035         off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1036         entry_offset = (void *)e - base;
1037         xt_ematch_foreach(ematch, e)
1038                 off += xt_compat_match_offset(ematch->u.kernel.match);
1039         t = ipt_get_target_c(e);
1040         off += xt_compat_target_offset(t->u.kernel.target);
1041         newinfo->size -= off;
1042         ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1043         if (ret)
1044                 return ret;
1045 
1046         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1047                 if (info->hook_entry[i] &&
1048                     (e < (struct ipt_entry *)(base + info->hook_entry[i])))
1049                         newinfo->hook_entry[i] -= off;
1050                 if (info->underflow[i] &&
1051                     (e < (struct ipt_entry *)(base + info->underflow[i])))
1052                         newinfo->underflow[i] -= off;
1053         }
1054         return 0;
1055 }
1056 
1057 static int compat_table_info(const struct xt_table_info *info,
1058                              struct xt_table_info *newinfo)
1059 {
1060         struct ipt_entry *iter;
1061         void *loc_cpu_entry;
1062         int ret;
1063 
1064         if (!newinfo || !info)
1065                 return -EINVAL;
1066 
1067         /* we dont care about newinfo->entries[] */
1068         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
1069         newinfo->initial_entries = 0;
1070         loc_cpu_entry = info->entries[raw_smp_processor_id()];
1071         xt_compat_init_offsets(AF_INET, info->number);
1072         xt_entry_foreach(iter, loc_cpu_entry, info->size) {
1073                 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
1074                 if (ret != 0)
1075                         return ret;
1076         }
1077         return 0;
1078 }
1079 #endif
1080 
1081 static int get_info(struct net *net, void __user *user,
1082                     const int *len, int compat)
1083 {
1084         char name[XT_TABLE_MAXNAMELEN];
1085         struct xt_table *t;
1086         int ret;
1087 
1088         if (*len != sizeof(struct ipt_getinfo)) {
1089                 duprintf("length %u != %zu\n", *len,
1090                          sizeof(struct ipt_getinfo));
1091                 return -EINVAL;
1092         }
1093 
1094         if (copy_from_user(name, user, sizeof(name)) != 0)
1095                 return -EFAULT;
1096 
1097         name[XT_TABLE_MAXNAMELEN-1] = '\0';
1098 #ifdef CONFIG_COMPAT
1099         if (compat)
1100                 xt_compat_lock(AF_INET);
1101 #endif
1102         t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1103                                     "iptable_%s", name);
1104         if (!IS_ERR_OR_NULL(t)) {
1105                 struct ipt_getinfo info;
1106                 const struct xt_table_info *private = t->private;
1107 #ifdef CONFIG_COMPAT
1108                 struct xt_table_info tmp;
1109 
1110                 if (compat) {
1111                         ret = compat_table_info(private, &tmp);
1112                         xt_compat_flush_offsets(AF_INET);
1113                         private = &tmp;
1114                 }
1115 #endif
1116                 memset(&info, 0, sizeof(info));
1117                 info.valid_hooks = t->valid_hooks;
1118                 memcpy(info.hook_entry, private->hook_entry,
1119                        sizeof(info.hook_entry));
1120                 memcpy(info.underflow, private->underflow,
1121                        sizeof(info.underflow));
1122                 info.num_entries = private->number;
1123                 info.size = private->size;
1124                 strcpy(info.name, name);
1125 
1126                 if (copy_to_user(user, &info, *len) != 0)
1127                         ret = -EFAULT;
1128                 else
1129                         ret = 0;
1130 
1131                 xt_table_unlock(t);
1132                 module_put(t->me);
1133         } else
1134                 ret = t ? PTR_ERR(t) : -ENOENT;
1135 #ifdef CONFIG_COMPAT
1136         if (compat)
1137                 xt_compat_unlock(AF_INET);
1138 #endif
1139         return ret;
1140 }
1141 
1142 static int
1143 get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1144             const int *len)
1145 {
1146         int ret;
1147         struct ipt_get_entries get;
1148         struct xt_table *t;
1149 
1150         if (*len < sizeof(get)) {
1151                 duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
1152                 return -EINVAL;
1153         }
1154         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1155                 return -EFAULT;
1156         if (*len != sizeof(struct ipt_get_entries) + get.size) {
1157                 duprintf("get_entries: %u != %zu\n",
1158                          *len, sizeof(get) + get.size);
1159                 return -EINVAL;
1160         }
1161 
1162         t = xt_find_table_lock(net, AF_INET, get.name);
1163         if (!IS_ERR_OR_NULL(t)) {
1164                 const struct xt_table_info *private = t->private;
1165                 duprintf("t->private->number = %u\n", private->number);
1166                 if (get.size == private->size)
1167                         ret = copy_entries_to_user(private->size,
1168                                                    t, uptr->entrytable);
1169                 else {
1170                         duprintf("get_entries: I've got %u not %u!\n",
1171                                  private->size, get.size);
1172                         ret = -EAGAIN;
1173                 }
1174                 module_put(t->me);
1175                 xt_table_unlock(t);
1176         } else
1177                 ret = t ? PTR_ERR(t) : -ENOENT;
1178 
1179         return ret;
1180 }
1181 
1182 static int
1183 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1184              struct xt_table_info *newinfo, unsigned int num_counters,
1185              void __user *counters_ptr)
1186 {
1187         int ret;
1188         struct xt_table *t;
1189         struct xt_table_info *oldinfo;
1190         struct xt_counters *counters;
1191         void *loc_cpu_old_entry;
1192         struct ipt_entry *iter;
1193 
1194         ret = 0;
1195         counters = vzalloc(num_counters * sizeof(struct xt_counters));
1196         if (!counters) {
1197                 ret = -ENOMEM;
1198                 goto out;
1199         }
1200 
1201         t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1202                                     "iptable_%s", name);
1203         if (IS_ERR_OR_NULL(t)) {
1204                 ret = t ? PTR_ERR(t) : -ENOENT;
1205                 goto free_newinfo_counters_untrans;
1206         }
1207 
1208         /* You lied! */
1209         if (valid_hooks != t->valid_hooks) {
1210                 duprintf("Valid hook crap: %08X vs %08X\n",
1211                          valid_hooks, t->valid_hooks);
1212                 ret = -EINVAL;
1213                 goto put_module;
1214         }
1215 
1216         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1217         if (!oldinfo)
1218                 goto put_module;
1219 
1220         /* Update module usage count based on number of rules */
1221         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1222                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1223         if ((oldinfo->number > oldinfo->initial_entries) ||
1224             (newinfo->number <= oldinfo->initial_entries))
1225                 module_put(t->me);
1226         if ((oldinfo->number > oldinfo->initial_entries) &&
1227             (newinfo->number <= oldinfo->initial_entries))
1228                 module_put(t->me);
1229 
1230         /* Get the old counters, and synchronize with replace */
1231         get_counters(oldinfo, counters);
1232 
1233         /* Decrease module usage counts and free resource */
1234         loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1235         xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1236                 cleanup_entry(iter, net);
1237 
1238         xt_free_table_info(oldinfo);
1239         if (copy_to_user(counters_ptr, counters,
1240                          sizeof(struct xt_counters) * num_counters) != 0) {
1241                 /* Silent error, can't fail, new table is already in place */
1242                 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1243         }
1244         vfree(counters);
1245         xt_table_unlock(t);
1246         return ret;
1247 
1248  put_module:
1249         module_put(t->me);
1250         xt_table_unlock(t);
1251  free_newinfo_counters_untrans:
1252         vfree(counters);
1253  out:
1254         return ret;
1255 }
1256 
1257 static int
1258 do_replace(struct net *net, const void __user *user, unsigned int len)
1259 {
1260         int ret;
1261         struct ipt_replace tmp;
1262         struct xt_table_info *newinfo;
1263         void *loc_cpu_entry;
1264         struct ipt_entry *iter;
1265 
1266         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1267                 return -EFAULT;
1268 
1269         /* overflow check */
1270         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1271                 return -ENOMEM;
1272         if (tmp.num_counters == 0)
1273                 return -EINVAL;
1274 
1275         tmp.name[sizeof(tmp.name)-1] = 0;
1276 
1277         newinfo = xt_alloc_table_info(tmp.size);
1278         if (!newinfo)
1279                 return -ENOMEM;
1280 
1281         /* choose the copy that is on our node/cpu */
1282         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1283         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1284                            tmp.size) != 0) {
1285                 ret = -EFAULT;
1286                 goto free_newinfo;
1287         }
1288 
1289         ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1290         if (ret != 0)
1291                 goto free_newinfo;
1292 
1293         duprintf("Translated table\n");
1294 
1295         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1296                            tmp.num_counters, tmp.counters);
1297         if (ret)
1298                 goto free_newinfo_untrans;
1299         return 0;
1300 
1301  free_newinfo_untrans:
1302         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1303                 cleanup_entry(iter, net);
1304  free_newinfo:
1305         xt_free_table_info(newinfo);
1306         return ret;
1307 }
1308 
1309 static int
1310 do_add_counters(struct net *net, const void __user *user,
1311                 unsigned int len, int compat)
1312 {
1313         unsigned int i, curcpu;
1314         struct xt_counters_info tmp;
1315         struct xt_counters *paddc;
1316         struct xt_table *t;
1317         const struct xt_table_info *private;
1318         int ret = 0;
1319         void *loc_cpu_entry;
1320         struct ipt_entry *iter;
1321         unsigned int addend;
1322 
1323         paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1324         if (IS_ERR(paddc))
1325                 return PTR_ERR(paddc);
1326 
1327         t = xt_find_table_lock(net, AF_INET, tmp.name);
1328         if (IS_ERR_OR_NULL(t)) {
1329                 ret = t ? PTR_ERR(t) : -ENOENT;
1330                 goto free;
1331         }
1332 
1333         local_bh_disable();
1334         private = t->private;
1335         if (private->number != tmp.num_counters) {
1336                 ret = -EINVAL;
1337                 goto unlock_up_free;
1338         }
1339 
1340         i = 0;
1341         /* Choose the copy that is on our node */
1342         curcpu = smp_processor_id();
1343         loc_cpu_entry = private->entries[curcpu];
1344         addend = xt_write_recseq_begin();
1345         xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1346                 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1347                 ++i;
1348         }
1349         xt_write_recseq_end(addend);
1350  unlock_up_free:
1351         local_bh_enable();
1352         xt_table_unlock(t);
1353         module_put(t->me);
1354  free:
1355         vfree(paddc);
1356 
1357         return ret;
1358 }
1359 
1360 #ifdef CONFIG_COMPAT
1361 struct compat_ipt_replace {
1362         char                    name[XT_TABLE_MAXNAMELEN];
1363         u32                     valid_hooks;
1364         u32                     num_entries;
1365         u32                     size;
1366         u32                     hook_entry[NF_INET_NUMHOOKS];
1367         u32                     underflow[NF_INET_NUMHOOKS];
1368         u32                     num_counters;
1369         compat_uptr_t           counters;       /* struct xt_counters * */
1370         struct compat_ipt_entry entries[0];
1371 };
1372 
1373 static int
1374 compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1375                           unsigned int *size, struct xt_counters *counters,
1376                           unsigned int i)
1377 {
1378         struct xt_entry_target *t;
1379         struct compat_ipt_entry __user *ce;
1380         u_int16_t target_offset, next_offset;
1381         compat_uint_t origsize;
1382         const struct xt_entry_match *ematch;
1383         int ret = 0;
1384 
1385         origsize = *size;
1386         ce = (struct compat_ipt_entry __user *)*dstptr;
1387         if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1388             copy_to_user(&ce->counters, &counters[i],
1389             sizeof(counters[i])) != 0)
1390                 return -EFAULT;
1391 
1392         *dstptr += sizeof(struct compat_ipt_entry);
1393         *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1394 
1395         xt_ematch_foreach(ematch, e) {
1396                 ret = xt_compat_match_to_user(ematch, dstptr, size);
1397                 if (ret != 0)
1398                         return ret;
1399         }
1400         target_offset = e->target_offset - (origsize - *size);
1401         t = ipt_get_target(e);
1402         ret = xt_compat_target_to_user(t, dstptr, size);
1403         if (ret)
1404                 return ret;
1405         next_offset = e->next_offset - (origsize - *size);
1406         if (put_user(target_offset, &ce->target_offset) != 0 ||
1407             put_user(next_offset, &ce->next_offset) != 0)
1408                 return -EFAULT;
1409         return 0;
1410 }
1411 
1412 static int
1413 compat_find_calc_match(struct xt_entry_match *m,
1414                        const struct ipt_ip *ip,
1415                        unsigned int hookmask,
1416                        int *size)
1417 {
1418         struct xt_match *match;
1419 
1420         match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1421                                       m->u.user.revision);
1422         if (IS_ERR(match)) {
1423                 duprintf("compat_check_calc_match: `%s' not found\n",
1424                          m->u.user.name);
1425                 return PTR_ERR(match);
1426         }
1427         m->u.kernel.match = match;
1428         *size += xt_compat_match_offset(match);
1429         return 0;
1430 }
1431 
1432 static void compat_release_entry(struct compat_ipt_entry *e)
1433 {
1434         struct xt_entry_target *t;
1435         struct xt_entry_match *ematch;
1436 
1437         /* Cleanup all matches */
1438         xt_ematch_foreach(ematch, e)
1439                 module_put(ematch->u.kernel.match->me);
1440         t = compat_ipt_get_target(e);
1441         module_put(t->u.kernel.target->me);
1442 }
1443 
1444 static int
1445 check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1446                                   struct xt_table_info *newinfo,
1447                                   unsigned int *size,
1448                                   const unsigned char *base,
1449                                   const unsigned char *limit)
1450 {
1451         struct xt_entry_match *ematch;
1452         struct xt_entry_target *t;
1453         struct xt_target *target;
1454         unsigned int entry_offset;
1455         unsigned int j;
1456         int ret, off;
1457 
1458         duprintf("check_compat_entry_size_and_hooks %p\n", e);
1459         if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1460             (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
1461             (unsigned char *)e + e->next_offset > limit) {
1462                 duprintf("Bad offset %p, limit = %p\n", e, limit);
1463                 return -EINVAL;
1464         }
1465 
1466         if (e->next_offset < sizeof(struct compat_ipt_entry) +
1467                              sizeof(struct compat_xt_entry_target)) {
1468                 duprintf("checking: element %p size %u\n",
1469                          e, e->next_offset);
1470                 return -EINVAL;
1471         }
1472 
1473         if (!ip_checkentry(&e->ip))
1474                 return -EINVAL;
1475 
1476         ret = xt_compat_check_entry_offsets(e, e->elems,
1477                                             e->target_offset, e->next_offset);
1478         if (ret)
1479                 return ret;
1480 
1481         off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1482         entry_offset = (void *)e - (void *)base;
1483         j = 0;
1484         xt_ematch_foreach(ematch, e) {
1485                 ret = compat_find_calc_match(ematch, &e->ip, e->comefrom,
1486                                              &off);
1487                 if (ret != 0)
1488                         goto release_matches;
1489                 ++j;
1490         }
1491 
1492         t = compat_ipt_get_target(e);
1493         target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1494                                         t->u.user.revision);
1495         if (IS_ERR(target)) {
1496                 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1497                          t->u.user.name);
1498                 ret = PTR_ERR(target);
1499                 goto release_matches;
1500         }
1501         t->u.kernel.target = target;
1502 
1503         off += xt_compat_target_offset(target);
1504         *size += off;
1505         ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1506         if (ret)
1507                 goto out;
1508 
1509         return 0;
1510 
1511 out:
1512         module_put(t->u.kernel.target->me);
1513 release_matches:
1514         xt_ematch_foreach(ematch, e) {
1515                 if (j-- == 0)
1516                         break;
1517                 module_put(ematch->u.kernel.match->me);
1518         }
1519         return ret;
1520 }
1521 
1522 static void
1523 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1524                             unsigned int *size,
1525                             struct xt_table_info *newinfo, unsigned char *base)
1526 {
1527         struct xt_entry_target *t;
1528         struct xt_target *target;
1529         struct ipt_entry *de;
1530         unsigned int origsize;
1531         int h;
1532         struct xt_entry_match *ematch;
1533 
1534         origsize = *size;
1535         de = (struct ipt_entry *)*dstptr;
1536         memcpy(de, e, sizeof(struct ipt_entry));
1537         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1538 
1539         *dstptr += sizeof(struct ipt_entry);
1540         *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1541 
1542         xt_ematch_foreach(ematch, e)
1543                 xt_compat_match_from_user(ematch, dstptr, size);
1544 
1545         de->target_offset = e->target_offset - (origsize - *size);
1546         t = compat_ipt_get_target(e);
1547         target = t->u.kernel.target;
1548         xt_compat_target_from_user(t, dstptr, size);
1549 
1550         de->next_offset = e->next_offset - (origsize - *size);
1551 
1552         for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1553                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1554                         newinfo->hook_entry[h] -= origsize - *size;
1555                 if ((unsigned char *)de - base < newinfo->underflow[h])
1556                         newinfo->underflow[h] -= origsize - *size;
1557         }
1558 }
1559 
1560 static int
1561 translate_compat_table(struct net *net,
1562                        struct xt_table_info **pinfo,
1563                        void **pentry0,
1564                        const struct compat_ipt_replace *compatr)
1565 {
1566         unsigned int i, j;
1567         struct xt_table_info *newinfo, *info;
1568         void *pos, *entry0, *entry1;
1569         struct compat_ipt_entry *iter0;
1570         struct ipt_replace repl;
1571         unsigned int size;
1572         int ret;
1573 
1574         info = *pinfo;
1575         entry0 = *pentry0;
1576         size = compatr->size;
1577         info->number = compatr->num_entries;
1578 
1579         duprintf("translate_compat_table: size %u\n", info->size);
1580         j = 0;
1581         xt_compat_lock(AF_INET);
1582         xt_compat_init_offsets(AF_INET, compatr->num_entries);
1583         /* Walk through entries, checking offsets. */
1584         xt_entry_foreach(iter0, entry0, compatr->size) {
1585                 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1586                                                         entry0,
1587                                                         entry0 + compatr->size);
1588                 if (ret != 0)
1589                         goto out_unlock;
1590                 ++j;
1591         }
1592 
1593         ret = -EINVAL;
1594         if (j != compatr->num_entries) {
1595                 duprintf("translate_compat_table: %u not %u entries\n",
1596                          j, compatr->num_entries);
1597                 goto out_unlock;
1598         }
1599 
1600         ret = -ENOMEM;
1601         newinfo = xt_alloc_table_info(size);
1602         if (!newinfo)
1603                 goto out_unlock;
1604 
1605         newinfo->number = compatr->num_entries;
1606         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1607                 newinfo->hook_entry[i] = compatr->hook_entry[i];
1608                 newinfo->underflow[i] = compatr->underflow[i];
1609         }
1610         entry1 = newinfo->entries[raw_smp_processor_id()];
1611         pos = entry1;
1612         size = compatr->size;
1613         xt_entry_foreach(iter0, entry0, compatr->size)
1614                 compat_copy_entry_from_user(iter0, &pos, &size,
1615                                             newinfo, entry1);
1616 
1617         /* all module references in entry0 are now gone.
1618          * entry1/newinfo contains a 64bit ruleset that looks exactly as
1619          * generated by 64bit userspace.
1620          *
1621          * Call standard translate_table() to validate all hook_entrys,
1622          * underflows, check for loops, etc.
1623          */
1624         xt_compat_flush_offsets(AF_INET);
1625         xt_compat_unlock(AF_INET);
1626 
1627         memcpy(&repl, compatr, sizeof(*compatr));
1628 
1629         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1630                 repl.hook_entry[i] = newinfo->hook_entry[i];
1631                 repl.underflow[i] = newinfo->underflow[i];
1632         }
1633 
1634         repl.num_counters = 0;
1635         repl.counters = NULL;
1636         repl.size = newinfo->size;
1637         ret = translate_table(net, newinfo, entry1, &repl);
1638         if (ret)
1639                 goto free_newinfo;
1640 
1641         *pinfo = newinfo;
1642         *pentry0 = entry1;
1643         xt_free_table_info(info);
1644         return 0;
1645 
1646 free_newinfo:
1647         xt_free_table_info(newinfo);
1648         return ret;
1649 out_unlock:
1650         xt_compat_flush_offsets(AF_INET);
1651         xt_compat_unlock(AF_INET);
1652         xt_entry_foreach(iter0, entry0, compatr->size) {
1653                 if (j-- == 0)
1654                         break;
1655                 compat_release_entry(iter0);
1656         }
1657         return ret;
1658 }
1659 
1660 static int
1661 compat_do_replace(struct net *net, void __user *user, unsigned int len)
1662 {
1663         int ret;
1664         struct compat_ipt_replace tmp;
1665         struct xt_table_info *newinfo;
1666         void *loc_cpu_entry;
1667         struct ipt_entry *iter;
1668 
1669         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1670                 return -EFAULT;
1671 
1672         /* overflow check */
1673         if (tmp.size >= INT_MAX / num_possible_cpus())
1674                 return -ENOMEM;
1675         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1676                 return -ENOMEM;
1677         if (tmp.num_counters == 0)
1678                 return -EINVAL;
1679 
1680         tmp.name[sizeof(tmp.name)-1] = 0;
1681 
1682         newinfo = xt_alloc_table_info(tmp.size);
1683         if (!newinfo)
1684                 return -ENOMEM;
1685 
1686         /* choose the copy that is on our node/cpu */
1687         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1688         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1689                            tmp.size) != 0) {
1690                 ret = -EFAULT;
1691                 goto free_newinfo;
1692         }
1693 
1694         ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1695         if (ret != 0)
1696                 goto free_newinfo;
1697 
1698         duprintf("compat_do_replace: Translated table\n");
1699 
1700         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1701                            tmp.num_counters, compat_ptr(tmp.counters));
1702         if (ret)
1703                 goto free_newinfo_untrans;
1704         return 0;
1705 
1706  free_newinfo_untrans:
1707         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1708                 cleanup_entry(iter, net);
1709  free_newinfo:
1710         xt_free_table_info(newinfo);
1711         return ret;
1712 }
1713 
1714 static int
1715 compat_do_ipt_set_ctl(struct sock *sk,  int cmd, void __user *user,
1716                       unsigned int len)
1717 {
1718         int ret;
1719 
1720         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1721                 return -EPERM;
1722 
1723         switch (cmd) {
1724         case IPT_SO_SET_REPLACE:
1725                 ret = compat_do_replace(sock_net(sk), user, len);
1726                 break;
1727 
1728         case IPT_SO_SET_ADD_COUNTERS:
1729                 ret = do_add_counters(sock_net(sk), user, len, 1);
1730                 break;
1731 
1732         default:
1733                 duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
1734                 ret = -EINVAL;
1735         }
1736 
1737         return ret;
1738 }
1739 
1740 struct compat_ipt_get_entries {
1741         char name[XT_TABLE_MAXNAMELEN];
1742         compat_uint_t size;
1743         struct compat_ipt_entry entrytable[0];
1744 };
1745 
1746 static int
1747 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1748                             void __user *userptr)
1749 {
1750         struct xt_counters *counters;
1751         const struct xt_table_info *private = table->private;
1752         void __user *pos;
1753         unsigned int size;
1754         int ret = 0;
1755         const void *loc_cpu_entry;
1756         unsigned int i = 0;
1757         struct ipt_entry *iter;
1758 
1759         counters = alloc_counters(table);
1760         if (IS_ERR(counters))
1761                 return PTR_ERR(counters);
1762 
1763         /* choose the copy that is on our node/cpu, ...
1764          * This choice is lazy (because current thread is
1765          * allowed to migrate to another cpu)
1766          */
1767         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1768         pos = userptr;
1769         size = total_size;
1770         xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1771                 ret = compat_copy_entry_to_user(iter, &pos,
1772                                                 &size, counters, i++);
1773                 if (ret != 0)
1774                         break;
1775         }
1776 
1777         vfree(counters);
1778         return ret;
1779 }
1780 
1781 static int
1782 compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1783                    int *len)
1784 {
1785         int ret;
1786         struct compat_ipt_get_entries get;
1787         struct xt_table *t;
1788 
1789         if (*len < sizeof(get)) {
1790                 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1791                 return -EINVAL;
1792         }
1793 
1794         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1795                 return -EFAULT;
1796 
1797         if (*len != sizeof(struct compat_ipt_get_entries) + get.size) {
1798                 duprintf("compat_get_entries: %u != %zu\n",
1799                          *len, sizeof(get) + get.size);
1800                 return -EINVAL;
1801         }
1802 
1803         xt_compat_lock(AF_INET);
1804         t = xt_find_table_lock(net, AF_INET, get.name);
1805         if (!IS_ERR_OR_NULL(t)) {
1806                 const struct xt_table_info *private = t->private;
1807                 struct xt_table_info info;
1808                 duprintf("t->private->number = %u\n", private->number);
1809                 ret = compat_table_info(private, &info);
1810                 if (!ret && get.size == info.size) {
1811                         ret = compat_copy_entries_to_user(private->size,
1812                                                           t, uptr->entrytable);
1813                 } else if (!ret) {
1814                         duprintf("compat_get_entries: I've got %u not %u!\n",
1815                                  private->size, get.size);
1816                         ret = -EAGAIN;
1817                 }
1818                 xt_compat_flush_offsets(AF_INET);
1819                 module_put(t->me);
1820                 xt_table_unlock(t);
1821         } else
1822                 ret = t ? PTR_ERR(t) : -ENOENT;
1823 
1824         xt_compat_unlock(AF_INET);
1825         return ret;
1826 }
1827 
1828 static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1829 
1830 static int
1831 compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1832 {
1833         int ret;
1834 
1835         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1836                 return -EPERM;
1837 
1838         switch (cmd) {
1839         case IPT_SO_GET_INFO:
1840                 ret = get_info(sock_net(sk), user, len, 1);
1841                 break;
1842         case IPT_SO_GET_ENTRIES:
1843                 ret = compat_get_entries(sock_net(sk), user, len);
1844                 break;
1845         default:
1846                 ret = do_ipt_get_ctl(sk, cmd, user, len);
1847         }
1848         return ret;
1849 }
1850 #endif
1851 
1852 static int
1853 do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1854 {
1855         int ret;
1856 
1857         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1858                 return -EPERM;
1859 
1860         switch (cmd) {
1861         case IPT_SO_SET_REPLACE:
1862                 ret = do_replace(sock_net(sk), user, len);
1863                 break;
1864 
1865         case IPT_SO_SET_ADD_COUNTERS:
1866                 ret = do_add_counters(sock_net(sk), user, len, 0);
1867                 break;
1868 
1869         default:
1870                 duprintf("do_ipt_set_ctl:  unknown request %i\n", cmd);
1871                 ret = -EINVAL;
1872         }
1873 
1874         return ret;
1875 }
1876 
1877 static int
1878 do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1879 {
1880         int ret;
1881 
1882         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1883                 return -EPERM;
1884 
1885         switch (cmd) {
1886         case IPT_SO_GET_INFO:
1887                 ret = get_info(sock_net(sk), user, len, 0);
1888                 break;
1889 
1890         case IPT_SO_GET_ENTRIES:
1891                 ret = get_entries(sock_net(sk), user, len);
1892                 break;
1893 
1894         case IPT_SO_GET_REVISION_MATCH:
1895         case IPT_SO_GET_REVISION_TARGET: {
1896                 struct xt_get_revision rev;
1897                 int target;
1898 
1899                 if (*len != sizeof(rev)) {
1900                         ret = -EINVAL;
1901                         break;
1902                 }
1903                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1904                         ret = -EFAULT;
1905                         break;
1906                 }
1907                 rev.name[sizeof(rev.name)-1] = 0;
1908 
1909                 if (cmd == IPT_SO_GET_REVISION_TARGET)
1910                         target = 1;
1911                 else
1912                         target = 0;
1913 
1914                 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1915                                                          rev.revision,
1916                                                          target, &ret),
1917                                         "ipt_%s", rev.name);
1918                 break;
1919         }
1920 
1921         default:
1922                 duprintf("do_ipt_get_ctl: unknown request %i\n", cmd);
1923                 ret = -EINVAL;
1924         }
1925 
1926         return ret;
1927 }
1928 
1929 struct xt_table *ipt_register_table(struct net *net,
1930                                     const struct xt_table *table,
1931                                     const struct ipt_replace *repl)
1932 {
1933         int ret;
1934         struct xt_table_info *newinfo;
1935         struct xt_table_info bootstrap = {0};
1936         void *loc_cpu_entry;
1937         struct xt_table *new_table;
1938 
1939         newinfo = xt_alloc_table_info(repl->size);
1940         if (!newinfo) {
1941                 ret = -ENOMEM;
1942                 goto out;
1943         }
1944 
1945         /* choose the copy on our node/cpu, but dont care about preemption */
1946         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1947         memcpy(loc_cpu_entry, repl->entries, repl->size);
1948 
1949         ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1950         if (ret != 0)
1951                 goto out_free;
1952 
1953         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1954         if (IS_ERR(new_table)) {
1955                 ret = PTR_ERR(new_table);
1956                 goto out_free;
1957         }
1958 
1959         return new_table;
1960 
1961 out_free:
1962         xt_free_table_info(newinfo);
1963 out:
1964         return ERR_PTR(ret);
1965 }
1966 
1967 void ipt_unregister_table(struct net *net, struct xt_table *table)
1968 {
1969         struct xt_table_info *private;
1970         void *loc_cpu_entry;
1971         struct module *table_owner = table->me;
1972         struct ipt_entry *iter;
1973 
1974         private = xt_unregister_table(table);
1975 
1976         /* Decrease module usage counts and free resources */
1977         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1978         xt_entry_foreach(iter, loc_cpu_entry, private->size)
1979                 cleanup_entry(iter, net);
1980         if (private->number > private->initial_entries)
1981                 module_put(table_owner);
1982         xt_free_table_info(private);
1983 }
1984 
1985 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1986 static inline bool
1987 icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1988                      u_int8_t type, u_int8_t code,
1989                      bool invert)
1990 {
1991         return ((test_type == 0xFF) ||
1992                 (type == test_type && code >= min_code && code <= max_code))
1993                 ^ invert;
1994 }
1995 
1996 static bool
1997 icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
1998 {
1999         const struct icmphdr *ic;
2000         struct icmphdr _icmph;
2001         const struct ipt_icmp *icmpinfo = par->matchinfo;
2002 
2003         /* Must not be a fragment. */
2004         if (par->fragoff != 0)
2005                 return false;
2006 
2007         ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
2008         if (ic == NULL) {
2009                 /* We've been asked to examine this packet, and we
2010                  * can't.  Hence, no choice but to drop.
2011                  */
2012                 duprintf("Dropping evil ICMP tinygram.\n");
2013                 par->hotdrop = true;
2014                 return false;
2015         }
2016 
2017         return icmp_type_code_match(icmpinfo->type,
2018                                     icmpinfo->code[0],
2019                                     icmpinfo->code[1],
2020                                     ic->type, ic->code,
2021                                     !!(icmpinfo->invflags&IPT_ICMP_INV));
2022 }
2023 
2024 static int icmp_checkentry(const struct xt_mtchk_param *par)
2025 {
2026         const struct ipt_icmp *icmpinfo = par->matchinfo;
2027 
2028         /* Must specify no unknown invflags */
2029         return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
2030 }
2031 
2032 static struct xt_target ipt_builtin_tg[] __read_mostly = {
2033         {
2034                 .name             = XT_STANDARD_TARGET,
2035                 .targetsize       = sizeof(int),
2036                 .family           = NFPROTO_IPV4,
2037 #ifdef CONFIG_COMPAT
2038                 .compatsize       = sizeof(compat_int_t),
2039                 .compat_from_user = compat_standard_from_user,
2040                 .compat_to_user   = compat_standard_to_user,
2041 #endif
2042         },
2043         {
2044                 .name             = XT_ERROR_TARGET,
2045                 .target           = ipt_error,
2046                 .targetsize       = XT_FUNCTION_MAXNAMELEN,
2047                 .family           = NFPROTO_IPV4,
2048         },
2049 };
2050 
2051 static struct nf_sockopt_ops ipt_sockopts = {
2052         .pf             = PF_INET,
2053         .set_optmin     = IPT_BASE_CTL,
2054         .set_optmax     = IPT_SO_SET_MAX+1,
2055         .set            = do_ipt_set_ctl,
2056 #ifdef CONFIG_COMPAT
2057         .compat_set     = compat_do_ipt_set_ctl,
2058 #endif
2059         .get_optmin     = IPT_BASE_CTL,
2060         .get_optmax     = IPT_SO_GET_MAX+1,
2061         .get            = do_ipt_get_ctl,
2062 #ifdef CONFIG_COMPAT
2063         .compat_get     = compat_do_ipt_get_ctl,
2064 #endif
2065         .owner          = THIS_MODULE,
2066 };
2067 
2068 static struct xt_match ipt_builtin_mt[] __read_mostly = {
2069         {
2070                 .name       = "icmp",
2071                 .match      = icmp_match,
2072                 .matchsize  = sizeof(struct ipt_icmp),
2073                 .checkentry = icmp_checkentry,
2074                 .proto      = IPPROTO_ICMP,
2075                 .family     = NFPROTO_IPV4,
2076         },
2077 };
2078 
2079 static int __net_init ip_tables_net_init(struct net *net)
2080 {
2081         return xt_proto_init(net, NFPROTO_IPV4);
2082 }
2083 
2084 static void __net_exit ip_tables_net_exit(struct net *net)
2085 {
2086         xt_proto_fini(net, NFPROTO_IPV4);
2087 }
2088 
2089 static struct pernet_operations ip_tables_net_ops = {
2090         .init = ip_tables_net_init,
2091         .exit = ip_tables_net_exit,
2092 };
2093 
2094 static int __init ip_tables_init(void)
2095 {
2096         int ret;
2097 
2098         ret = register_pernet_subsys(&ip_tables_net_ops);
2099         if (ret < 0)
2100                 goto err1;
2101 
2102         /* No one else will be downing sem now, so we won't sleep */
2103         ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2104         if (ret < 0)
2105                 goto err2;
2106         ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2107         if (ret < 0)
2108                 goto err4;
2109 
2110         /* Register setsockopt */
2111         ret = nf_register_sockopt(&ipt_sockopts);
2112         if (ret < 0)
2113                 goto err5;
2114 
2115         pr_info("(C) 2000-2006 Netfilter Core Team\n");
2116         return 0;
2117 
2118 err5:
2119         xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2120 err4:
2121         xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2122 err2:
2123         unregister_pernet_subsys(&ip_tables_net_ops);
2124 err1:
2125         return ret;
2126 }
2127 
2128 static void __exit ip_tables_fini(void)
2129 {
2130         nf_unregister_sockopt(&ipt_sockopts);
2131 
2132         xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
2133         xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
2134         unregister_pernet_subsys(&ip_tables_net_ops);
2135 }
2136 
2137 EXPORT_SYMBOL(ipt_register_table);
2138 EXPORT_SYMBOL(ipt_unregister_table);
2139 EXPORT_SYMBOL(ipt_do_table);
2140 module_init(ip_tables_init);
2141 module_exit(ip_tables_fini);
2142 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp