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

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

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

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