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

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

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ 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 for ARP packets.
  3  *
  4  * Based heavily, if not almost entirely, upon ip_tables.c framework.
  5  *
  6  * Some ARP specific bits are:
  7  *
  8  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  9  * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
 10  *
 11  */
 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13 #include <linux/kernel.h>
 14 #include <linux/skbuff.h>
 15 #include <linux/netdevice.h>
 16 #include <linux/capability.h>
 17 #include <linux/if_arp.h>
 18 #include <linux/kmod.h>
 19 #include <linux/vmalloc.h>
 20 #include <linux/proc_fs.h>
 21 #include <linux/module.h>
 22 #include <linux/init.h>
 23 #include <linux/mutex.h>
 24 #include <linux/err.h>
 25 #include <net/compat.h>
 26 #include <net/sock.h>
 27 #include <asm/uaccess.h>
 28 
 29 #include <linux/netfilter/x_tables.h>
 30 #include <linux/netfilter_arp/arp_tables.h>
 31 #include "../../netfilter/xt_repldata.h"
 32 
 33 MODULE_LICENSE("GPL");
 34 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
 35 MODULE_DESCRIPTION("arptables core");
 36 
 37 /*#define DEBUG_ARP_TABLES*/
 38 /*#define DEBUG_ARP_TABLES_USER*/
 39 
 40 #ifdef DEBUG_ARP_TABLES
 41 #define dprintf(format, args...)  printk(format , ## args)
 42 #else
 43 #define dprintf(format, args...)
 44 #endif
 45 
 46 #ifdef DEBUG_ARP_TABLES_USER
 47 #define duprintf(format, args...) printk(format , ## args)
 48 #else
 49 #define duprintf(format, args...)
 50 #endif
 51 
 52 #ifdef CONFIG_NETFILTER_DEBUG
 53 #define ARP_NF_ASSERT(x)        WARN_ON(!(x))
 54 #else
 55 #define ARP_NF_ASSERT(x)
 56 #endif
 57 
 58 void *arpt_alloc_initial_table(const struct xt_table *info)
 59 {
 60         return xt_alloc_initial_table(arpt, ARPT);
 61 }
 62 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
 63 
 64 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
 65                                       const char *hdr_addr, int len)
 66 {
 67         int i, ret;
 68 
 69         if (len > ARPT_DEV_ADDR_LEN_MAX)
 70                 len = ARPT_DEV_ADDR_LEN_MAX;
 71 
 72         ret = 0;
 73         for (i = 0; i < len; i++)
 74                 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
 75 
 76         return ret != 0;
 77 }
 78 
 79 /*
 80  * Unfortunately, _b and _mask are not aligned to an int (or long int)
 81  * Some arches dont care, unrolling the loop is a win on them.
 82  * For other arches, we only have a 16bit alignement.
 83  */
 84 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
 85 {
 86 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
 87         unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
 88 #else
 89         unsigned long ret = 0;
 90         const u16 *a = (const u16 *)_a;
 91         const u16 *b = (const u16 *)_b;
 92         const u16 *mask = (const u16 *)_mask;
 93         int i;
 94 
 95         for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
 96                 ret |= (a[i] ^ b[i]) & mask[i];
 97 #endif
 98         return ret;
 99 }
100 
101 /* Returns whether packet matches rule or not. */
102 static inline int arp_packet_match(const struct arphdr *arphdr,
103                                    struct net_device *dev,
104                                    const char *indev,
105                                    const char *outdev,
106                                    const struct arpt_arp *arpinfo)
107 {
108         const char *arpptr = (char *)(arphdr + 1);
109         const char *src_devaddr, *tgt_devaddr;
110         __be32 src_ipaddr, tgt_ipaddr;
111         long ret;
112 
113 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
114 
115         if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
116                   ARPT_INV_ARPOP)) {
117                 dprintf("ARP operation field mismatch.\n");
118                 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
119                         arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
120                 return 0;
121         }
122 
123         if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
124                   ARPT_INV_ARPHRD)) {
125                 dprintf("ARP hardware address format mismatch.\n");
126                 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
127                         arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
128                 return 0;
129         }
130 
131         if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
132                   ARPT_INV_ARPPRO)) {
133                 dprintf("ARP protocol address format mismatch.\n");
134                 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
135                         arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
136                 return 0;
137         }
138 
139         if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
140                   ARPT_INV_ARPHLN)) {
141                 dprintf("ARP hardware address length mismatch.\n");
142                 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
143                         arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
144                 return 0;
145         }
146 
147         src_devaddr = arpptr;
148         arpptr += dev->addr_len;
149         memcpy(&src_ipaddr, arpptr, sizeof(u32));
150         arpptr += sizeof(u32);
151         tgt_devaddr = arpptr;
152         arpptr += dev->addr_len;
153         memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
154 
155         if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
156                   ARPT_INV_SRCDEVADDR) ||
157             FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
158                   ARPT_INV_TGTDEVADDR)) {
159                 dprintf("Source or target device address mismatch.\n");
160 
161                 return 0;
162         }
163 
164         if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
165                   ARPT_INV_SRCIP) ||
166             FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
167                   ARPT_INV_TGTIP)) {
168                 dprintf("Source or target IP address mismatch.\n");
169 
170                 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
171                         &src_ipaddr,
172                         &arpinfo->smsk.s_addr,
173                         &arpinfo->src.s_addr,
174                         arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
175                 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
176                         &tgt_ipaddr,
177                         &arpinfo->tmsk.s_addr,
178                         &arpinfo->tgt.s_addr,
179                         arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
180                 return 0;
181         }
182 
183         /* Look for ifname matches.  */
184         ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
185 
186         if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
187                 dprintf("VIA in mismatch (%s vs %s).%s\n",
188                         indev, arpinfo->iniface,
189                         arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
190                 return 0;
191         }
192 
193         ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
194 
195         if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
196                 dprintf("VIA out mismatch (%s vs %s).%s\n",
197                         outdev, arpinfo->outiface,
198                         arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
199                 return 0;
200         }
201 
202         return 1;
203 #undef FWINV
204 }
205 
206 static inline int arp_checkentry(const struct arpt_arp *arp)
207 {
208         if (arp->flags & ~ARPT_F_MASK) {
209                 duprintf("Unknown flag bits set: %08X\n",
210                          arp->flags & ~ARPT_F_MASK);
211                 return 0;
212         }
213         if (arp->invflags & ~ARPT_INV_MASK) {
214                 duprintf("Unknown invflag bits set: %08X\n",
215                          arp->invflags & ~ARPT_INV_MASK);
216                 return 0;
217         }
218 
219         return 1;
220 }
221 
222 static unsigned int
223 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
224 {
225         net_err_ratelimited("arp_tables: error: '%s'\n",
226                             (const char *)par->targinfo);
227 
228         return NF_DROP;
229 }
230 
231 static inline const struct xt_entry_target *
232 arpt_get_target_c(const struct arpt_entry *e)
233 {
234         return arpt_get_target((struct arpt_entry *)e);
235 }
236 
237 static inline struct arpt_entry *
238 get_entry(const void *base, unsigned int offset)
239 {
240         return (struct arpt_entry *)(base + offset);
241 }
242 
243 static inline __pure
244 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
245 {
246         return (void *)entry + entry->next_offset;
247 }
248 
249 unsigned int arpt_do_table(struct sk_buff *skb,
250                            unsigned int hook,
251                            const struct net_device *in,
252                            const struct net_device *out,
253                            struct xt_table *table)
254 {
255         static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
256         unsigned int verdict = NF_DROP;
257         const struct arphdr *arp;
258         struct arpt_entry *e, *back;
259         const char *indev, *outdev;
260         void *table_base;
261         const struct xt_table_info *private;
262         struct xt_action_param acpar;
263         unsigned int addend;
264 
265         if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
266                 return NF_DROP;
267 
268         indev = in ? in->name : nulldevname;
269         outdev = out ? out->name : nulldevname;
270 
271         local_bh_disable();
272         addend = xt_write_recseq_begin();
273         private = table->private;
274         /*
275          * Ensure we load private-> members after we've fetched the base
276          * pointer.
277          */
278         smp_read_barrier_depends();
279         table_base = private->entries[smp_processor_id()];
280 
281         e = get_entry(table_base, private->hook_entry[hook]);
282         back = get_entry(table_base, private->underflow[hook]);
283 
284         acpar.in      = in;
285         acpar.out     = out;
286         acpar.hooknum = hook;
287         acpar.family  = NFPROTO_ARP;
288         acpar.hotdrop = false;
289 
290         arp = arp_hdr(skb);
291         do {
292                 const struct xt_entry_target *t;
293 
294                 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
295                         e = arpt_next_entry(e);
296                         continue;
297                 }
298 
299                 ADD_COUNTER(e->counters, arp_hdr_len(skb->dev), 1);
300 
301                 t = arpt_get_target_c(e);
302 
303                 /* Standard target? */
304                 if (!t->u.kernel.target->target) {
305                         int v;
306 
307                         v = ((struct xt_standard_target *)t)->verdict;
308                         if (v < 0) {
309                                 /* Pop from stack? */
310                                 if (v != XT_RETURN) {
311                                         verdict = (unsigned int)(-v) - 1;
312                                         break;
313                                 }
314                                 e = back;
315                                 back = get_entry(table_base, back->comefrom);
316                                 continue;
317                         }
318                         if (table_base + v
319                             != arpt_next_entry(e)) {
320                                 /* Save old back ptr in next entry */
321                                 struct arpt_entry *next = arpt_next_entry(e);
322                                 next->comefrom = (void *)back - table_base;
323 
324                                 /* set back pointer to next entry */
325                                 back = next;
326                         }
327 
328                         e = get_entry(table_base, v);
329                         continue;
330                 }
331 
332                 /* Targets which reenter must return
333                  * abs. verdicts
334                  */
335                 acpar.target   = t->u.kernel.target;
336                 acpar.targinfo = t->data;
337                 verdict = t->u.kernel.target->target(skb, &acpar);
338 
339                 /* Target might have changed stuff. */
340                 arp = arp_hdr(skb);
341 
342                 if (verdict == XT_CONTINUE)
343                         e = arpt_next_entry(e);
344                 else
345                         /* Verdict */
346                         break;
347         } while (!acpar.hotdrop);
348         xt_write_recseq_end(addend);
349         local_bh_enable();
350 
351         if (acpar.hotdrop)
352                 return NF_DROP;
353         else
354                 return verdict;
355 }
356 
357 /* All zeroes == unconditional rule. */
358 static inline bool unconditional(const struct arpt_entry *e)
359 {
360         static const struct arpt_arp uncond;
361 
362         return e->target_offset == sizeof(struct arpt_entry) &&
363                memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
364 }
365 
366 static bool find_jump_target(const struct xt_table_info *t,
367                              const void *entry0,
368                              const struct arpt_entry *target)
369 {
370         struct arpt_entry *iter;
371 
372         xt_entry_foreach(iter, entry0, t->size) {
373                  if (iter == target)
374                         return true;
375         }
376         return false;
377 }
378 
379 /* Figures out from what hook each rule can be called: returns 0 if
380  * there are loops.  Puts hook bitmask in comefrom.
381  */
382 static int mark_source_chains(const struct xt_table_info *newinfo,
383                               unsigned int valid_hooks, void *entry0)
384 {
385         unsigned int hook;
386 
387         /* No recursion; use packet counter to save back ptrs (reset
388          * to 0 as we leave), and comefrom to save source hook bitmask.
389          */
390         for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
391                 unsigned int pos = newinfo->hook_entry[hook];
392                 struct arpt_entry *e
393                         = (struct arpt_entry *)(entry0 + pos);
394 
395                 if (!(valid_hooks & (1 << hook)))
396                         continue;
397 
398                 /* Set initial back pointer. */
399                 e->counters.pcnt = pos;
400 
401                 for (;;) {
402                         const struct xt_standard_target *t
403                                 = (void *)arpt_get_target_c(e);
404                         int visited = e->comefrom & (1 << hook);
405 
406                         if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
407                                 pr_notice("arptables: loop hook %u pos %u %08X.\n",
408                                        hook, pos, e->comefrom);
409                                 return 0;
410                         }
411                         e->comefrom
412                                 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
413 
414                         /* Unconditional return/END. */
415                         if ((unconditional(e) &&
416                              (strcmp(t->target.u.user.name,
417                                      XT_STANDARD_TARGET) == 0) &&
418                              t->verdict < 0) || visited) {
419                                 unsigned int oldpos, size;
420 
421                                 if ((strcmp(t->target.u.user.name,
422                                             XT_STANDARD_TARGET) == 0) &&
423                                     t->verdict < -NF_MAX_VERDICT - 1) {
424                                         duprintf("mark_source_chains: bad "
425                                                 "negative verdict (%i)\n",
426                                                                 t->verdict);
427                                         return 0;
428                                 }
429 
430                                 /* Return: backtrack through the last
431                                  * big jump.
432                                  */
433                                 do {
434                                         e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
435                                         oldpos = pos;
436                                         pos = e->counters.pcnt;
437                                         e->counters.pcnt = 0;
438 
439                                         /* We're at the start. */
440                                         if (pos == oldpos)
441                                                 goto next;
442 
443                                         e = (struct arpt_entry *)
444                                                 (entry0 + pos);
445                                 } while (oldpos == pos + e->next_offset);
446 
447                                 /* Move along one */
448                                 size = e->next_offset;
449                                 e = (struct arpt_entry *)
450                                         (entry0 + pos + size);
451                                 if (pos + size >= newinfo->size)
452                                         return 0;
453                                 e->counters.pcnt = pos;
454                                 pos += size;
455                         } else {
456                                 int newpos = t->verdict;
457 
458                                 if (strcmp(t->target.u.user.name,
459                                            XT_STANDARD_TARGET) == 0 &&
460                                     newpos >= 0) {
461                                         if (newpos > newinfo->size -
462                                                 sizeof(struct arpt_entry)) {
463                                                 duprintf("mark_source_chains: "
464                                                         "bad verdict (%i)\n",
465                                                                 newpos);
466                                                 return 0;
467                                         }
468 
469                                         /* This a jump; chase it. */
470                                         duprintf("Jump rule %u -> %u\n",
471                                                  pos, newpos);
472                                         e = (struct arpt_entry *)
473                                                 (entry0 + newpos);
474                                         if (!find_jump_target(newinfo, entry0, e))
475                                                 return 0;
476                                 } else {
477                                         /* ... this is a fallthru */
478                                         newpos = pos + e->next_offset;
479                                         if (newpos >= newinfo->size)
480                                                 return 0;
481                                 }
482                                 e = (struct arpt_entry *)
483                                         (entry0 + newpos);
484                                 e->counters.pcnt = pos;
485                                 pos = newpos;
486                         }
487                 }
488                 next:
489                 duprintf("Finished chain %u\n", hook);
490         }
491         return 1;
492 }
493 
494 static inline int check_target(struct arpt_entry *e, const char *name)
495 {
496         struct xt_entry_target *t = arpt_get_target(e);
497         int ret;
498         struct xt_tgchk_param par = {
499                 .table     = name,
500                 .entryinfo = e,
501                 .target    = t->u.kernel.target,
502                 .targinfo  = t->data,
503                 .hook_mask = e->comefrom,
504                 .family    = NFPROTO_ARP,
505         };
506 
507         ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
508         if (ret < 0) {
509                 duprintf("arp_tables: check failed for `%s'.\n",
510                          t->u.kernel.target->name);
511                 return ret;
512         }
513         return 0;
514 }
515 
516 static inline int
517 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
518 {
519         struct xt_entry_target *t;
520         struct xt_target *target;
521         int ret;
522 
523         t = arpt_get_target(e);
524         target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
525                                         t->u.user.revision);
526         if (IS_ERR(target)) {
527                 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
528                 ret = PTR_ERR(target);
529                 goto out;
530         }
531         t->u.kernel.target = target;
532 
533         ret = check_target(e, name);
534         if (ret)
535                 goto err;
536         return 0;
537 err:
538         module_put(t->u.kernel.target->me);
539 out:
540         return ret;
541 }
542 
543 static bool check_underflow(const struct arpt_entry *e)
544 {
545         const struct xt_entry_target *t;
546         unsigned int verdict;
547 
548         if (!unconditional(e))
549                 return false;
550         t = arpt_get_target_c(e);
551         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
552                 return false;
553         verdict = ((struct xt_standard_target *)t)->verdict;
554         verdict = -verdict - 1;
555         return verdict == NF_DROP || verdict == NF_ACCEPT;
556 }
557 
558 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
559                                              struct xt_table_info *newinfo,
560                                              const unsigned char *base,
561                                              const unsigned char *limit,
562                                              const unsigned int *hook_entries,
563                                              const unsigned int *underflows,
564                                              unsigned int valid_hooks)
565 {
566         unsigned int h;
567         int err;
568 
569         if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
570             (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
571             (unsigned char *)e + e->next_offset > limit) {
572                 duprintf("Bad offset %p\n", e);
573                 return -EINVAL;
574         }
575 
576         if (e->next_offset
577             < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
578                 duprintf("checking: element %p size %u\n",
579                          e, e->next_offset);
580                 return -EINVAL;
581         }
582 
583         if (!arp_checkentry(&e->arp))
584                 return -EINVAL;
585 
586         err = xt_check_entry_offsets(e, e->elems, e->target_offset,
587                                      e->next_offset);
588         if (err)
589                 return err;
590 
591         /* Check hooks & underflows */
592         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
593                 if (!(valid_hooks & (1 << h)))
594                         continue;
595                 if ((unsigned char *)e - base == hook_entries[h])
596                         newinfo->hook_entry[h] = hook_entries[h];
597                 if ((unsigned char *)e - base == underflows[h]) {
598                         if (!check_underflow(e)) {
599                                 pr_debug("Underflows must be unconditional and "
600                                          "use the STANDARD target with "
601                                          "ACCEPT/DROP\n");
602                                 return -EINVAL;
603                         }
604                         newinfo->underflow[h] = underflows[h];
605                 }
606         }
607 
608         /* Clear counters and comefrom */
609         e->counters = ((struct xt_counters) { 0, 0 });
610         e->comefrom = 0;
611         return 0;
612 }
613 
614 static inline void cleanup_entry(struct arpt_entry *e)
615 {
616         struct xt_tgdtor_param par;
617         struct xt_entry_target *t;
618 
619         t = arpt_get_target(e);
620         par.target   = t->u.kernel.target;
621         par.targinfo = t->data;
622         par.family   = NFPROTO_ARP;
623         if (par.target->destroy != NULL)
624                 par.target->destroy(&par);
625         module_put(par.target->me);
626 }
627 
628 /* Checks and translates the user-supplied table segment (held in
629  * newinfo).
630  */
631 static int translate_table(struct xt_table_info *newinfo, void *entry0,
632                            const struct arpt_replace *repl)
633 {
634         struct arpt_entry *iter;
635         unsigned int i;
636         int ret = 0;
637 
638         newinfo->size = repl->size;
639         newinfo->number = repl->num_entries;
640 
641         /* Init all hooks to impossible value. */
642         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
643                 newinfo->hook_entry[i] = 0xFFFFFFFF;
644                 newinfo->underflow[i] = 0xFFFFFFFF;
645         }
646 
647         duprintf("translate_table: size %u\n", newinfo->size);
648         i = 0;
649 
650         /* Walk through entries, checking offsets. */
651         xt_entry_foreach(iter, entry0, newinfo->size) {
652                 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
653                                                  entry0 + repl->size,
654                                                  repl->hook_entry,
655                                                  repl->underflow,
656                                                  repl->valid_hooks);
657                 if (ret != 0)
658                         break;
659                 ++i;
660                 if (strcmp(arpt_get_target(iter)->u.user.name,
661                     XT_ERROR_TARGET) == 0)
662                         ++newinfo->stacksize;
663         }
664         duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
665         if (ret != 0)
666                 return ret;
667 
668         if (i != repl->num_entries) {
669                 duprintf("translate_table: %u not %u entries\n",
670                          i, repl->num_entries);
671                 return -EINVAL;
672         }
673 
674         /* Check hooks all assigned */
675         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
676                 /* Only hooks which are valid */
677                 if (!(repl->valid_hooks & (1 << i)))
678                         continue;
679                 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
680                         duprintf("Invalid hook entry %u %u\n",
681                                  i, repl->hook_entry[i]);
682                         return -EINVAL;
683                 }
684                 if (newinfo->underflow[i] == 0xFFFFFFFF) {
685                         duprintf("Invalid underflow %u %u\n",
686                                  i, repl->underflow[i]);
687                         return -EINVAL;
688                 }
689         }
690 
691         if (!mark_source_chains(newinfo, repl->valid_hooks, entry0))
692                 return -ELOOP;
693 
694         /* Finally, each sanity check must pass */
695         i = 0;
696         xt_entry_foreach(iter, entry0, newinfo->size) {
697                 ret = find_check_entry(iter, repl->name, repl->size);
698                 if (ret != 0)
699                         break;
700                 ++i;
701         }
702 
703         if (ret != 0) {
704                 xt_entry_foreach(iter, entry0, newinfo->size) {
705                         if (i-- == 0)
706                                 break;
707                         cleanup_entry(iter);
708                 }
709                 return ret;
710         }
711 
712         /* And one copy for every other CPU */
713         for_each_possible_cpu(i) {
714                 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
715                         memcpy(newinfo->entries[i], entry0, newinfo->size);
716         }
717 
718         return ret;
719 }
720 
721 static void get_counters(const struct xt_table_info *t,
722                          struct xt_counters counters[])
723 {
724         struct arpt_entry *iter;
725         unsigned int cpu;
726         unsigned int i;
727 
728         for_each_possible_cpu(cpu) {
729                 seqcount_t *s = &per_cpu(xt_recseq, cpu);
730 
731                 i = 0;
732                 xt_entry_foreach(iter, t->entries[cpu], t->size) {
733                         u64 bcnt, pcnt;
734                         unsigned int start;
735 
736                         do {
737                                 start = read_seqcount_begin(s);
738                                 bcnt = iter->counters.bcnt;
739                                 pcnt = iter->counters.pcnt;
740                         } while (read_seqcount_retry(s, start));
741 
742                         ADD_COUNTER(counters[i], bcnt, pcnt);
743                         ++i;
744                 }
745         }
746 }
747 
748 static struct xt_counters *alloc_counters(const struct xt_table *table)
749 {
750         unsigned int countersize;
751         struct xt_counters *counters;
752         const struct xt_table_info *private = table->private;
753 
754         /* We need atomic snapshot of counters: rest doesn't change
755          * (other than comefrom, which userspace doesn't care
756          * about).
757          */
758         countersize = sizeof(struct xt_counters) * private->number;
759         counters = vzalloc(countersize);
760 
761         if (counters == NULL)
762                 return ERR_PTR(-ENOMEM);
763 
764         get_counters(private, counters);
765 
766         return counters;
767 }
768 
769 static int copy_entries_to_user(unsigned int total_size,
770                                 const struct xt_table *table,
771                                 void __user *userptr)
772 {
773         unsigned int off, num;
774         const struct arpt_entry *e;
775         struct xt_counters *counters;
776         struct xt_table_info *private = table->private;
777         int ret = 0;
778         void *loc_cpu_entry;
779 
780         counters = alloc_counters(table);
781         if (IS_ERR(counters))
782                 return PTR_ERR(counters);
783 
784         loc_cpu_entry = private->entries[raw_smp_processor_id()];
785         /* ... then copy entire thing ... */
786         if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
787                 ret = -EFAULT;
788                 goto free_counters;
789         }
790 
791         /* FIXME: use iterator macros --RR */
792         /* ... then go back and fix counters and names */
793         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
794                 const struct xt_entry_target *t;
795 
796                 e = (struct arpt_entry *)(loc_cpu_entry + off);
797                 if (copy_to_user(userptr + off
798                                  + offsetof(struct arpt_entry, counters),
799                                  &counters[num],
800                                  sizeof(counters[num])) != 0) {
801                         ret = -EFAULT;
802                         goto free_counters;
803                 }
804 
805                 t = arpt_get_target_c(e);
806                 if (copy_to_user(userptr + off + e->target_offset
807                                  + offsetof(struct xt_entry_target,
808                                             u.user.name),
809                                  t->u.kernel.target->name,
810                                  strlen(t->u.kernel.target->name)+1) != 0) {
811                         ret = -EFAULT;
812                         goto free_counters;
813                 }
814         }
815 
816  free_counters:
817         vfree(counters);
818         return ret;
819 }
820 
821 #ifdef CONFIG_COMPAT
822 static void compat_standard_from_user(void *dst, const void *src)
823 {
824         int v = *(compat_int_t *)src;
825 
826         if (v > 0)
827                 v += xt_compat_calc_jump(NFPROTO_ARP, v);
828         memcpy(dst, &v, sizeof(v));
829 }
830 
831 static int compat_standard_to_user(void __user *dst, const void *src)
832 {
833         compat_int_t cv = *(int *)src;
834 
835         if (cv > 0)
836                 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
837         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
838 }
839 
840 static int compat_calc_entry(const struct arpt_entry *e,
841                              const struct xt_table_info *info,
842                              const void *base, struct xt_table_info *newinfo)
843 {
844         const struct xt_entry_target *t;
845         unsigned int entry_offset;
846         int off, i, ret;
847 
848         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
849         entry_offset = (void *)e - base;
850 
851         t = arpt_get_target_c(e);
852         off += xt_compat_target_offset(t->u.kernel.target);
853         newinfo->size -= off;
854         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
855         if (ret)
856                 return ret;
857 
858         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
859                 if (info->hook_entry[i] &&
860                     (e < (struct arpt_entry *)(base + info->hook_entry[i])))
861                         newinfo->hook_entry[i] -= off;
862                 if (info->underflow[i] &&
863                     (e < (struct arpt_entry *)(base + info->underflow[i])))
864                         newinfo->underflow[i] -= off;
865         }
866         return 0;
867 }
868 
869 static int compat_table_info(const struct xt_table_info *info,
870                              struct xt_table_info *newinfo)
871 {
872         struct arpt_entry *iter;
873         void *loc_cpu_entry;
874         int ret;
875 
876         if (!newinfo || !info)
877                 return -EINVAL;
878 
879         /* we dont care about newinfo->entries[] */
880         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
881         newinfo->initial_entries = 0;
882         loc_cpu_entry = info->entries[raw_smp_processor_id()];
883         xt_compat_init_offsets(NFPROTO_ARP, info->number);
884         xt_entry_foreach(iter, loc_cpu_entry, info->size) {
885                 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
886                 if (ret != 0)
887                         return ret;
888         }
889         return 0;
890 }
891 #endif
892 
893 static int get_info(struct net *net, void __user *user,
894                     const int *len, int compat)
895 {
896         char name[XT_TABLE_MAXNAMELEN];
897         struct xt_table *t;
898         int ret;
899 
900         if (*len != sizeof(struct arpt_getinfo)) {
901                 duprintf("length %u != %Zu\n", *len,
902                          sizeof(struct arpt_getinfo));
903                 return -EINVAL;
904         }
905 
906         if (copy_from_user(name, user, sizeof(name)) != 0)
907                 return -EFAULT;
908 
909         name[XT_TABLE_MAXNAMELEN-1] = '\0';
910 #ifdef CONFIG_COMPAT
911         if (compat)
912                 xt_compat_lock(NFPROTO_ARP);
913 #endif
914         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
915                                     "arptable_%s", name);
916         if (!IS_ERR_OR_NULL(t)) {
917                 struct arpt_getinfo info;
918                 const struct xt_table_info *private = t->private;
919 #ifdef CONFIG_COMPAT
920                 struct xt_table_info tmp;
921 
922                 if (compat) {
923                         ret = compat_table_info(private, &tmp);
924                         xt_compat_flush_offsets(NFPROTO_ARP);
925                         private = &tmp;
926                 }
927 #endif
928                 memset(&info, 0, sizeof(info));
929                 info.valid_hooks = t->valid_hooks;
930                 memcpy(info.hook_entry, private->hook_entry,
931                        sizeof(info.hook_entry));
932                 memcpy(info.underflow, private->underflow,
933                        sizeof(info.underflow));
934                 info.num_entries = private->number;
935                 info.size = private->size;
936                 strcpy(info.name, name);
937 
938                 if (copy_to_user(user, &info, *len) != 0)
939                         ret = -EFAULT;
940                 else
941                         ret = 0;
942                 xt_table_unlock(t);
943                 module_put(t->me);
944         } else
945                 ret = t ? PTR_ERR(t) : -ENOENT;
946 #ifdef CONFIG_COMPAT
947         if (compat)
948                 xt_compat_unlock(NFPROTO_ARP);
949 #endif
950         return ret;
951 }
952 
953 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
954                        const int *len)
955 {
956         int ret;
957         struct arpt_get_entries get;
958         struct xt_table *t;
959 
960         if (*len < sizeof(get)) {
961                 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
962                 return -EINVAL;
963         }
964         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
965                 return -EFAULT;
966         if (*len != sizeof(struct arpt_get_entries) + get.size) {
967                 duprintf("get_entries: %u != %Zu\n", *len,
968                          sizeof(struct arpt_get_entries) + get.size);
969                 return -EINVAL;
970         }
971 
972         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
973         if (!IS_ERR_OR_NULL(t)) {
974                 const struct xt_table_info *private = t->private;
975 
976                 duprintf("t->private->number = %u\n",
977                          private->number);
978                 if (get.size == private->size)
979                         ret = copy_entries_to_user(private->size,
980                                                    t, uptr->entrytable);
981                 else {
982                         duprintf("get_entries: I've got %u not %u!\n",
983                                  private->size, get.size);
984                         ret = -EAGAIN;
985                 }
986                 module_put(t->me);
987                 xt_table_unlock(t);
988         } else
989                 ret = t ? PTR_ERR(t) : -ENOENT;
990 
991         return ret;
992 }
993 
994 static int __do_replace(struct net *net, const char *name,
995                         unsigned int valid_hooks,
996                         struct xt_table_info *newinfo,
997                         unsigned int num_counters,
998                         void __user *counters_ptr)
999 {
1000         int ret;
1001         struct xt_table *t;
1002         struct xt_table_info *oldinfo;
1003         struct xt_counters *counters;
1004         void *loc_cpu_old_entry;
1005         struct arpt_entry *iter;
1006 
1007         ret = 0;
1008         counters = vzalloc(num_counters * sizeof(struct xt_counters));
1009         if (!counters) {
1010                 ret = -ENOMEM;
1011                 goto out;
1012         }
1013 
1014         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1015                                     "arptable_%s", name);
1016         if (IS_ERR_OR_NULL(t)) {
1017                 ret = t ? PTR_ERR(t) : -ENOENT;
1018                 goto free_newinfo_counters_untrans;
1019         }
1020 
1021         /* You lied! */
1022         if (valid_hooks != t->valid_hooks) {
1023                 duprintf("Valid hook crap: %08X vs %08X\n",
1024                          valid_hooks, t->valid_hooks);
1025                 ret = -EINVAL;
1026                 goto put_module;
1027         }
1028 
1029         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1030         if (!oldinfo)
1031                 goto put_module;
1032 
1033         /* Update module usage count based on number of rules */
1034         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1035                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1036         if ((oldinfo->number > oldinfo->initial_entries) ||
1037             (newinfo->number <= oldinfo->initial_entries))
1038                 module_put(t->me);
1039         if ((oldinfo->number > oldinfo->initial_entries) &&
1040             (newinfo->number <= oldinfo->initial_entries))
1041                 module_put(t->me);
1042 
1043         /* Get the old counters, and synchronize with replace */
1044         get_counters(oldinfo, counters);
1045 
1046         /* Decrease module usage counts and free resource */
1047         loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1048         xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1049                 cleanup_entry(iter);
1050 
1051         xt_free_table_info(oldinfo);
1052         if (copy_to_user(counters_ptr, counters,
1053                          sizeof(struct xt_counters) * num_counters) != 0) {
1054                 /* Silent error, can't fail, new table is already in place */
1055                 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1056         }
1057         vfree(counters);
1058         xt_table_unlock(t);
1059         return ret;
1060 
1061  put_module:
1062         module_put(t->me);
1063         xt_table_unlock(t);
1064  free_newinfo_counters_untrans:
1065         vfree(counters);
1066  out:
1067         return ret;
1068 }
1069 
1070 static int do_replace(struct net *net, const void __user *user,
1071                       unsigned int len)
1072 {
1073         int ret;
1074         struct arpt_replace tmp;
1075         struct xt_table_info *newinfo;
1076         void *loc_cpu_entry;
1077         struct arpt_entry *iter;
1078 
1079         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1080                 return -EFAULT;
1081 
1082         /* overflow check */
1083         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1084                 return -ENOMEM;
1085         if (tmp.num_counters == 0)
1086                 return -EINVAL;
1087 
1088         tmp.name[sizeof(tmp.name)-1] = 0;
1089 
1090         newinfo = xt_alloc_table_info(tmp.size);
1091         if (!newinfo)
1092                 return -ENOMEM;
1093 
1094         /* choose the copy that is on our node/cpu */
1095         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1096         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1097                            tmp.size) != 0) {
1098                 ret = -EFAULT;
1099                 goto free_newinfo;
1100         }
1101 
1102         ret = translate_table(newinfo, loc_cpu_entry, &tmp);
1103         if (ret != 0)
1104                 goto free_newinfo;
1105 
1106         duprintf("arp_tables: Translated table\n");
1107 
1108         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1109                            tmp.num_counters, tmp.counters);
1110         if (ret)
1111                 goto free_newinfo_untrans;
1112         return 0;
1113 
1114  free_newinfo_untrans:
1115         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1116                 cleanup_entry(iter);
1117  free_newinfo:
1118         xt_free_table_info(newinfo);
1119         return ret;
1120 }
1121 
1122 static int do_add_counters(struct net *net, const void __user *user,
1123                            unsigned int len, int compat)
1124 {
1125         unsigned int i, curcpu;
1126         struct xt_counters_info tmp;
1127         struct xt_counters *paddc;
1128         struct xt_table *t;
1129         const struct xt_table_info *private;
1130         int ret = 0;
1131         void *loc_cpu_entry;
1132         struct arpt_entry *iter;
1133         unsigned int addend;
1134 
1135         paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1136         if (IS_ERR(paddc))
1137                 return PTR_ERR(paddc);
1138 
1139         t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1140         if (IS_ERR_OR_NULL(t)) {
1141                 ret = t ? PTR_ERR(t) : -ENOENT;
1142                 goto free;
1143         }
1144 
1145         local_bh_disable();
1146         private = t->private;
1147         if (private->number != tmp.num_counters) {
1148                 ret = -EINVAL;
1149                 goto unlock_up_free;
1150         }
1151 
1152         i = 0;
1153         /* Choose the copy that is on our node */
1154         curcpu = smp_processor_id();
1155         loc_cpu_entry = private->entries[curcpu];
1156         addend = xt_write_recseq_begin();
1157         xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1158                 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1159                 ++i;
1160         }
1161         xt_write_recseq_end(addend);
1162  unlock_up_free:
1163         local_bh_enable();
1164         xt_table_unlock(t);
1165         module_put(t->me);
1166  free:
1167         vfree(paddc);
1168 
1169         return ret;
1170 }
1171 
1172 #ifdef CONFIG_COMPAT
1173 struct compat_arpt_replace {
1174         char                            name[XT_TABLE_MAXNAMELEN];
1175         u32                             valid_hooks;
1176         u32                             num_entries;
1177         u32                             size;
1178         u32                             hook_entry[NF_ARP_NUMHOOKS];
1179         u32                             underflow[NF_ARP_NUMHOOKS];
1180         u32                             num_counters;
1181         compat_uptr_t                   counters;
1182         struct compat_arpt_entry        entries[0];
1183 };
1184 
1185 static inline void compat_release_entry(struct compat_arpt_entry *e)
1186 {
1187         struct xt_entry_target *t;
1188 
1189         t = compat_arpt_get_target(e);
1190         module_put(t->u.kernel.target->me);
1191 }
1192 
1193 static int
1194 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1195                                   struct xt_table_info *newinfo,
1196                                   unsigned int *size,
1197                                   const unsigned char *base,
1198                                   const unsigned char *limit)
1199 {
1200         struct xt_entry_target *t;
1201         struct xt_target *target;
1202         unsigned int entry_offset;
1203         int ret, off;
1204 
1205         duprintf("check_compat_entry_size_and_hooks %p\n", e);
1206         if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1207             (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1208             (unsigned char *)e + e->next_offset > limit) {
1209                 duprintf("Bad offset %p, limit = %p\n", e, limit);
1210                 return -EINVAL;
1211         }
1212 
1213         if (e->next_offset < sizeof(struct compat_arpt_entry) +
1214                              sizeof(struct compat_xt_entry_target)) {
1215                 duprintf("checking: element %p size %u\n",
1216                          e, e->next_offset);
1217                 return -EINVAL;
1218         }
1219 
1220         if (!arp_checkentry(&e->arp))
1221                 return -EINVAL;
1222 
1223         ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1224                                             e->next_offset);
1225         if (ret)
1226                 return ret;
1227 
1228         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1229         entry_offset = (void *)e - (void *)base;
1230 
1231         t = compat_arpt_get_target(e);
1232         target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1233                                         t->u.user.revision);
1234         if (IS_ERR(target)) {
1235                 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1236                          t->u.user.name);
1237                 ret = PTR_ERR(target);
1238                 goto out;
1239         }
1240         t->u.kernel.target = target;
1241 
1242         off += xt_compat_target_offset(target);
1243         *size += off;
1244         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1245         if (ret)
1246                 goto release_target;
1247 
1248         return 0;
1249 
1250 release_target:
1251         module_put(t->u.kernel.target->me);
1252 out:
1253         return ret;
1254 }
1255 
1256 static void
1257 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1258                             unsigned int *size,
1259                             struct xt_table_info *newinfo, unsigned char *base)
1260 {
1261         struct xt_entry_target *t;
1262         struct xt_target *target;
1263         struct arpt_entry *de;
1264         unsigned int origsize;
1265         int h;
1266 
1267         origsize = *size;
1268         de = (struct arpt_entry *)*dstptr;
1269         memcpy(de, e, sizeof(struct arpt_entry));
1270         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1271 
1272         *dstptr += sizeof(struct arpt_entry);
1273         *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1274 
1275         de->target_offset = e->target_offset - (origsize - *size);
1276         t = compat_arpt_get_target(e);
1277         target = t->u.kernel.target;
1278         xt_compat_target_from_user(t, dstptr, size);
1279 
1280         de->next_offset = e->next_offset - (origsize - *size);
1281         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1282                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1283                         newinfo->hook_entry[h] -= origsize - *size;
1284                 if ((unsigned char *)de - base < newinfo->underflow[h])
1285                         newinfo->underflow[h] -= origsize - *size;
1286         }
1287 }
1288 
1289 static int translate_compat_table(struct xt_table_info **pinfo,
1290                                   void **pentry0,
1291                                   const struct compat_arpt_replace *compatr)
1292 {
1293         unsigned int i, j;
1294         struct xt_table_info *newinfo, *info;
1295         void *pos, *entry0, *entry1;
1296         struct compat_arpt_entry *iter0;
1297         struct arpt_replace repl;
1298         unsigned int size;
1299         int ret = 0;
1300 
1301         info = *pinfo;
1302         entry0 = *pentry0;
1303         size = compatr->size;
1304         info->number = compatr->num_entries;
1305 
1306         duprintf("translate_compat_table: size %u\n", info->size);
1307         j = 0;
1308         xt_compat_lock(NFPROTO_ARP);
1309         xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1310         /* Walk through entries, checking offsets. */
1311         xt_entry_foreach(iter0, entry0, compatr->size) {
1312                 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1313                                                         entry0,
1314                                                         entry0 + compatr->size);
1315                 if (ret != 0)
1316                         goto out_unlock;
1317                 ++j;
1318         }
1319 
1320         ret = -EINVAL;
1321         if (j != compatr->num_entries) {
1322                 duprintf("translate_compat_table: %u not %u entries\n",
1323                          j, compatr->num_entries);
1324                 goto out_unlock;
1325         }
1326 
1327         ret = -ENOMEM;
1328         newinfo = xt_alloc_table_info(size);
1329         if (!newinfo)
1330                 goto out_unlock;
1331 
1332         newinfo->number = compatr->num_entries;
1333         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1334                 newinfo->hook_entry[i] = info->hook_entry[i];
1335                 newinfo->underflow[i] = info->underflow[i];
1336         }
1337         entry1 = newinfo->entries[raw_smp_processor_id()];
1338         pos = entry1;
1339         size = compatr->size;
1340         xt_entry_foreach(iter0, entry0, compatr->size)
1341                 compat_copy_entry_from_user(iter0, &pos, &size,
1342                                             newinfo, entry1);
1343 
1344         /* all module references in entry0 are now gone */
1345 
1346         xt_compat_flush_offsets(NFPROTO_ARP);
1347         xt_compat_unlock(NFPROTO_ARP);
1348 
1349         memcpy(&repl, compatr, sizeof(*compatr));
1350 
1351         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1352                 repl.hook_entry[i] = newinfo->hook_entry[i];
1353                 repl.underflow[i] = newinfo->underflow[i];
1354         }
1355 
1356         repl.num_counters = 0;
1357         repl.counters = NULL;
1358         repl.size = newinfo->size;
1359         ret = translate_table(newinfo, entry1, &repl);
1360         if (ret)
1361                 goto free_newinfo;
1362 
1363         *pinfo = newinfo;
1364         *pentry0 = entry1;
1365         xt_free_table_info(info);
1366         return 0;
1367 
1368 free_newinfo:
1369         xt_free_table_info(newinfo);
1370         return ret;
1371 out_unlock:
1372         xt_compat_flush_offsets(NFPROTO_ARP);
1373         xt_compat_unlock(NFPROTO_ARP);
1374         xt_entry_foreach(iter0, entry0, compatr->size) {
1375                 if (j-- == 0)
1376                         break;
1377                 compat_release_entry(iter0);
1378         }
1379         return ret;
1380 }
1381 
1382 static int compat_do_replace(struct net *net, void __user *user,
1383                              unsigned int len)
1384 {
1385         int ret;
1386         struct compat_arpt_replace tmp;
1387         struct xt_table_info *newinfo;
1388         void *loc_cpu_entry;
1389         struct arpt_entry *iter;
1390 
1391         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1392                 return -EFAULT;
1393 
1394         /* overflow check */
1395         if (tmp.size >= INT_MAX / num_possible_cpus())
1396                 return -ENOMEM;
1397         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1398                 return -ENOMEM;
1399         if (tmp.num_counters == 0)
1400                 return -EINVAL;
1401 
1402         tmp.name[sizeof(tmp.name)-1] = 0;
1403 
1404         newinfo = xt_alloc_table_info(tmp.size);
1405         if (!newinfo)
1406                 return -ENOMEM;
1407 
1408         /* choose the copy that is on our node/cpu */
1409         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1410         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1411                 ret = -EFAULT;
1412                 goto free_newinfo;
1413         }
1414 
1415         ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1416         if (ret != 0)
1417                 goto free_newinfo;
1418 
1419         duprintf("compat_do_replace: Translated table\n");
1420 
1421         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1422                            tmp.num_counters, compat_ptr(tmp.counters));
1423         if (ret)
1424                 goto free_newinfo_untrans;
1425         return 0;
1426 
1427  free_newinfo_untrans:
1428         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1429                 cleanup_entry(iter);
1430  free_newinfo:
1431         xt_free_table_info(newinfo);
1432         return ret;
1433 }
1434 
1435 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1436                                   unsigned int len)
1437 {
1438         int ret;
1439 
1440         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1441                 return -EPERM;
1442 
1443         switch (cmd) {
1444         case ARPT_SO_SET_REPLACE:
1445                 ret = compat_do_replace(sock_net(sk), user, len);
1446                 break;
1447 
1448         case ARPT_SO_SET_ADD_COUNTERS:
1449                 ret = do_add_counters(sock_net(sk), user, len, 1);
1450                 break;
1451 
1452         default:
1453                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1454                 ret = -EINVAL;
1455         }
1456 
1457         return ret;
1458 }
1459 
1460 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1461                                      compat_uint_t *size,
1462                                      struct xt_counters *counters,
1463                                      unsigned int i)
1464 {
1465         struct xt_entry_target *t;
1466         struct compat_arpt_entry __user *ce;
1467         u_int16_t target_offset, next_offset;
1468         compat_uint_t origsize;
1469         int ret;
1470 
1471         origsize = *size;
1472         ce = (struct compat_arpt_entry __user *)*dstptr;
1473         if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1474             copy_to_user(&ce->counters, &counters[i],
1475             sizeof(counters[i])) != 0)
1476                 return -EFAULT;
1477 
1478         *dstptr += sizeof(struct compat_arpt_entry);
1479         *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1480 
1481         target_offset = e->target_offset - (origsize - *size);
1482 
1483         t = arpt_get_target(e);
1484         ret = xt_compat_target_to_user(t, dstptr, size);
1485         if (ret)
1486                 return ret;
1487         next_offset = e->next_offset - (origsize - *size);
1488         if (put_user(target_offset, &ce->target_offset) != 0 ||
1489             put_user(next_offset, &ce->next_offset) != 0)
1490                 return -EFAULT;
1491         return 0;
1492 }
1493 
1494 static int compat_copy_entries_to_user(unsigned int total_size,
1495                                        struct xt_table *table,
1496                                        void __user *userptr)
1497 {
1498         struct xt_counters *counters;
1499         const struct xt_table_info *private = table->private;
1500         void __user *pos;
1501         unsigned int size;
1502         int ret = 0;
1503         void *loc_cpu_entry;
1504         unsigned int i = 0;
1505         struct arpt_entry *iter;
1506 
1507         counters = alloc_counters(table);
1508         if (IS_ERR(counters))
1509                 return PTR_ERR(counters);
1510 
1511         /* choose the copy on our node/cpu */
1512         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1513         pos = userptr;
1514         size = total_size;
1515         xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1516                 ret = compat_copy_entry_to_user(iter, &pos,
1517                                                 &size, counters, i++);
1518                 if (ret != 0)
1519                         break;
1520         }
1521         vfree(counters);
1522         return ret;
1523 }
1524 
1525 struct compat_arpt_get_entries {
1526         char name[XT_TABLE_MAXNAMELEN];
1527         compat_uint_t size;
1528         struct compat_arpt_entry entrytable[0];
1529 };
1530 
1531 static int compat_get_entries(struct net *net,
1532                               struct compat_arpt_get_entries __user *uptr,
1533                               int *len)
1534 {
1535         int ret;
1536         struct compat_arpt_get_entries get;
1537         struct xt_table *t;
1538 
1539         if (*len < sizeof(get)) {
1540                 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1541                 return -EINVAL;
1542         }
1543         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1544                 return -EFAULT;
1545         if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1546                 duprintf("compat_get_entries: %u != %zu\n",
1547                          *len, sizeof(get) + get.size);
1548                 return -EINVAL;
1549         }
1550 
1551         xt_compat_lock(NFPROTO_ARP);
1552         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1553         if (!IS_ERR_OR_NULL(t)) {
1554                 const struct xt_table_info *private = t->private;
1555                 struct xt_table_info info;
1556 
1557                 duprintf("t->private->number = %u\n", private->number);
1558                 ret = compat_table_info(private, &info);
1559                 if (!ret && get.size == info.size) {
1560                         ret = compat_copy_entries_to_user(private->size,
1561                                                           t, uptr->entrytable);
1562                 } else if (!ret) {
1563                         duprintf("compat_get_entries: I've got %u not %u!\n",
1564                                  private->size, get.size);
1565                         ret = -EAGAIN;
1566                 }
1567                 xt_compat_flush_offsets(NFPROTO_ARP);
1568                 module_put(t->me);
1569                 xt_table_unlock(t);
1570         } else
1571                 ret = t ? PTR_ERR(t) : -ENOENT;
1572 
1573         xt_compat_unlock(NFPROTO_ARP);
1574         return ret;
1575 }
1576 
1577 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1578 
1579 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1580                                   int *len)
1581 {
1582         int ret;
1583 
1584         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1585                 return -EPERM;
1586 
1587         switch (cmd) {
1588         case ARPT_SO_GET_INFO:
1589                 ret = get_info(sock_net(sk), user, len, 1);
1590                 break;
1591         case ARPT_SO_GET_ENTRIES:
1592                 ret = compat_get_entries(sock_net(sk), user, len);
1593                 break;
1594         default:
1595                 ret = do_arpt_get_ctl(sk, cmd, user, len);
1596         }
1597         return ret;
1598 }
1599 #endif
1600 
1601 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1602 {
1603         int ret;
1604 
1605         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1606                 return -EPERM;
1607 
1608         switch (cmd) {
1609         case ARPT_SO_SET_REPLACE:
1610                 ret = do_replace(sock_net(sk), user, len);
1611                 break;
1612 
1613         case ARPT_SO_SET_ADD_COUNTERS:
1614                 ret = do_add_counters(sock_net(sk), user, len, 0);
1615                 break;
1616 
1617         default:
1618                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1619                 ret = -EINVAL;
1620         }
1621 
1622         return ret;
1623 }
1624 
1625 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1626 {
1627         int ret;
1628 
1629         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1630                 return -EPERM;
1631 
1632         switch (cmd) {
1633         case ARPT_SO_GET_INFO:
1634                 ret = get_info(sock_net(sk), user, len, 0);
1635                 break;
1636 
1637         case ARPT_SO_GET_ENTRIES:
1638                 ret = get_entries(sock_net(sk), user, len);
1639                 break;
1640 
1641         case ARPT_SO_GET_REVISION_TARGET: {
1642                 struct xt_get_revision rev;
1643 
1644                 if (*len != sizeof(rev)) {
1645                         ret = -EINVAL;
1646                         break;
1647                 }
1648                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1649                         ret = -EFAULT;
1650                         break;
1651                 }
1652                 rev.name[sizeof(rev.name)-1] = 0;
1653 
1654                 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1655                                                          rev.revision, 1, &ret),
1656                                         "arpt_%s", rev.name);
1657                 break;
1658         }
1659 
1660         default:
1661                 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1662                 ret = -EINVAL;
1663         }
1664 
1665         return ret;
1666 }
1667 
1668 struct xt_table *arpt_register_table(struct net *net,
1669                                      const struct xt_table *table,
1670                                      const struct arpt_replace *repl)
1671 {
1672         int ret;
1673         struct xt_table_info *newinfo;
1674         struct xt_table_info bootstrap = {0};
1675         void *loc_cpu_entry;
1676         struct xt_table *new_table;
1677 
1678         newinfo = xt_alloc_table_info(repl->size);
1679         if (!newinfo) {
1680                 ret = -ENOMEM;
1681                 goto out;
1682         }
1683 
1684         /* choose the copy on our node/cpu */
1685         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1686         memcpy(loc_cpu_entry, repl->entries, repl->size);
1687 
1688         ret = translate_table(newinfo, loc_cpu_entry, repl);
1689         duprintf("arpt_register_table: translate table gives %d\n", ret);
1690         if (ret != 0)
1691                 goto out_free;
1692 
1693         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1694         if (IS_ERR(new_table)) {
1695                 ret = PTR_ERR(new_table);
1696                 goto out_free;
1697         }
1698         return new_table;
1699 
1700 out_free:
1701         xt_free_table_info(newinfo);
1702 out:
1703         return ERR_PTR(ret);
1704 }
1705 
1706 void arpt_unregister_table(struct xt_table *table)
1707 {
1708         struct xt_table_info *private;
1709         void *loc_cpu_entry;
1710         struct module *table_owner = table->me;
1711         struct arpt_entry *iter;
1712 
1713         private = xt_unregister_table(table);
1714 
1715         /* Decrease module usage counts and free resources */
1716         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1717         xt_entry_foreach(iter, loc_cpu_entry, private->size)
1718                 cleanup_entry(iter);
1719         if (private->number > private->initial_entries)
1720                 module_put(table_owner);
1721         xt_free_table_info(private);
1722 }
1723 
1724 /* The built-in targets: standard (NULL) and error. */
1725 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1726         {
1727                 .name             = XT_STANDARD_TARGET,
1728                 .targetsize       = sizeof(int),
1729                 .family           = NFPROTO_ARP,
1730 #ifdef CONFIG_COMPAT
1731                 .compatsize       = sizeof(compat_int_t),
1732                 .compat_from_user = compat_standard_from_user,
1733                 .compat_to_user   = compat_standard_to_user,
1734 #endif
1735         },
1736         {
1737                 .name             = XT_ERROR_TARGET,
1738                 .target           = arpt_error,
1739                 .targetsize       = XT_FUNCTION_MAXNAMELEN,
1740                 .family           = NFPROTO_ARP,
1741         },
1742 };
1743 
1744 static struct nf_sockopt_ops arpt_sockopts = {
1745         .pf             = PF_INET,
1746         .set_optmin     = ARPT_BASE_CTL,
1747         .set_optmax     = ARPT_SO_SET_MAX+1,
1748         .set            = do_arpt_set_ctl,
1749 #ifdef CONFIG_COMPAT
1750         .compat_set     = compat_do_arpt_set_ctl,
1751 #endif
1752         .get_optmin     = ARPT_BASE_CTL,
1753         .get_optmax     = ARPT_SO_GET_MAX+1,
1754         .get            = do_arpt_get_ctl,
1755 #ifdef CONFIG_COMPAT
1756         .compat_get     = compat_do_arpt_get_ctl,
1757 #endif
1758         .owner          = THIS_MODULE,
1759 };
1760 
1761 static int __net_init arp_tables_net_init(struct net *net)
1762 {
1763         return xt_proto_init(net, NFPROTO_ARP);
1764 }
1765 
1766 static void __net_exit arp_tables_net_exit(struct net *net)
1767 {
1768         xt_proto_fini(net, NFPROTO_ARP);
1769 }
1770 
1771 static struct pernet_operations arp_tables_net_ops = {
1772         .init = arp_tables_net_init,
1773         .exit = arp_tables_net_exit,
1774 };
1775 
1776 static int __init arp_tables_init(void)
1777 {
1778         int ret;
1779 
1780         ret = register_pernet_subsys(&arp_tables_net_ops);
1781         if (ret < 0)
1782                 goto err1;
1783 
1784         /* No one else will be downing sem now, so we won't sleep */
1785         ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1786         if (ret < 0)
1787                 goto err2;
1788 
1789         /* Register setsockopt */
1790         ret = nf_register_sockopt(&arpt_sockopts);
1791         if (ret < 0)
1792                 goto err4;
1793 
1794         printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1795         return 0;
1796 
1797 err4:
1798         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1799 err2:
1800         unregister_pernet_subsys(&arp_tables_net_ops);
1801 err1:
1802         return ret;
1803 }
1804 
1805 static void __exit arp_tables_fini(void)
1806 {
1807         nf_unregister_sockopt(&arpt_sockopts);
1808         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1809         unregister_pernet_subsys(&arp_tables_net_ops);
1810 }
1811 
1812 EXPORT_SYMBOL(arpt_register_table);
1813 EXPORT_SYMBOL(arpt_unregister_table);
1814 EXPORT_SYMBOL(arpt_do_table);
1815 
1816 module_init(arp_tables_init);
1817 module_exit(arp_tables_fini);
1818 

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