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