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