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