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

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

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

~ [ 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