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