treewide: Add SPDX license identifier for more missed files
[linux-block.git] / net / ipv4 / netfilter / arp_tables.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * Packet matching code for ARP packets.
4 *
5 * Based heavily, if not almost entirely, upon ip_tables.c framework.
6 *
7 * Some ARP specific bits are:
8 *
9 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
f229f6ce 10 * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
1da177e4
LT
11 *
12 */
90e7d4ab 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4
LT
14#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
4fc268d2 17#include <linux/capability.h>
1da177e4
LT
18#include <linux/if_arp.h>
19#include <linux/kmod.h>
20#include <linux/vmalloc.h>
21#include <linux/proc_fs.h>
22#include <linux/module.h>
23#include <linux/init.h>
57b47a53 24#include <linux/mutex.h>
d6a2ba07
PM
25#include <linux/err.h>
26#include <net/compat.h>
79df341a 27#include <net/sock.h>
7c0f6ba6 28#include <linux/uaccess.h>
1da177e4 29
2e4e6a17 30#include <linux/netfilter/x_tables.h>
1da177e4 31#include <linux/netfilter_arp/arp_tables.h>
e3eaa991 32#include "../../netfilter/xt_repldata.h"
1da177e4
LT
33
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
36MODULE_DESCRIPTION("arptables core");
37
e3eaa991
JE
38void *arpt_alloc_initial_table(const struct xt_table *info)
39{
40 return xt_alloc_initial_table(arpt, ARPT);
41}
42EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
43
1da177e4 44static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
5452e425 45 const char *hdr_addr, int len)
1da177e4
LT
46{
47 int i, ret;
48
49 if (len > ARPT_DEV_ADDR_LEN_MAX)
50 len = ARPT_DEV_ADDR_LEN_MAX;
51
52 ret = 0;
53 for (i = 0; i < len; i++)
54 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
55
a02cec21 56 return ret != 0;
1da177e4
LT
57}
58
ddc214c4 59/*
25985edc 60 * Unfortunately, _b and _mask are not aligned to an int (or long int)
ddc214c4 61 * Some arches dont care, unrolling the loop is a win on them.
35c7f6de 62 * For other arches, we only have a 16bit alignement.
ddc214c4
ED
63 */
64static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
65{
66#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
b8dfe498 67 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
ddc214c4
ED
68#else
69 unsigned long ret = 0;
35c7f6de
ED
70 const u16 *a = (const u16 *)_a;
71 const u16 *b = (const u16 *)_b;
72 const u16 *mask = (const u16 *)_mask;
ddc214c4
ED
73 int i;
74
35c7f6de
ED
75 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
76 ret |= (a[i] ^ b[i]) & mask[i];
ddc214c4
ED
77#endif
78 return ret;
79}
80
1da177e4
LT
81/* Returns whether packet matches rule or not. */
82static inline int arp_packet_match(const struct arphdr *arphdr,
83 struct net_device *dev,
84 const char *indev,
85 const char *outdev,
86 const struct arpt_arp *arpinfo)
87{
5452e425
JE
88 const char *arpptr = (char *)(arphdr + 1);
89 const char *src_devaddr, *tgt_devaddr;
59b8bfd8 90 __be32 src_ipaddr, tgt_ipaddr;
ddc214c4 91 long ret;
1da177e4 92
c37a2dfa
JP
93 if (NF_INVF(arpinfo, ARPT_INV_ARPOP,
94 (arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop))
1da177e4 95 return 0;
1da177e4 96
c37a2dfa
JP
97 if (NF_INVF(arpinfo, ARPT_INV_ARPHRD,
98 (arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd))
1da177e4 99 return 0;
1da177e4 100
c37a2dfa
JP
101 if (NF_INVF(arpinfo, ARPT_INV_ARPPRO,
102 (arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro))
1da177e4 103 return 0;
1da177e4 104
c37a2dfa
JP
105 if (NF_INVF(arpinfo, ARPT_INV_ARPHLN,
106 (arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln))
1da177e4 107 return 0;
1da177e4
LT
108
109 src_devaddr = arpptr;
110 arpptr += dev->addr_len;
111 memcpy(&src_ipaddr, arpptr, sizeof(u32));
112 arpptr += sizeof(u32);
113 tgt_devaddr = arpptr;
114 arpptr += dev->addr_len;
115 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
116
c37a2dfa
JP
117 if (NF_INVF(arpinfo, ARPT_INV_SRCDEVADDR,
118 arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr,
119 dev->addr_len)) ||
120 NF_INVF(arpinfo, ARPT_INV_TGTDEVADDR,
121 arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr,
122 dev->addr_len)))
1da177e4 123 return 0;
1da177e4 124
c37a2dfa
JP
125 if (NF_INVF(arpinfo, ARPT_INV_SRCIP,
126 (src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr) ||
127 NF_INVF(arpinfo, ARPT_INV_TGTIP,
128 (tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr))
1da177e4 129 return 0;
1da177e4
LT
130
131 /* Look for ifname matches. */
ddc214c4 132 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
1da177e4 133
c37a2dfa 134 if (NF_INVF(arpinfo, ARPT_INV_VIA_IN, ret != 0))
1da177e4 135 return 0;
1da177e4 136
ddc214c4 137 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
1da177e4 138
c37a2dfa 139 if (NF_INVF(arpinfo, ARPT_INV_VIA_OUT, ret != 0))
1da177e4 140 return 0;
1da177e4
LT
141
142 return 1;
143}
144
145static inline int arp_checkentry(const struct arpt_arp *arp)
146{
d7cdf816 147 if (arp->flags & ~ARPT_F_MASK)
1da177e4 148 return 0;
d7cdf816 149 if (arp->invflags & ~ARPT_INV_MASK)
1da177e4 150 return 0;
1da177e4
LT
151
152 return 1;
153}
154
7eb35586 155static unsigned int
4b560b44 156arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 157{
e87cc472
JP
158 net_err_ratelimited("arp_tables: error: '%s'\n",
159 (const char *)par->targinfo);
1da177e4
LT
160
161 return NF_DROP;
162}
163
87a2e70d 164static inline const struct xt_entry_target *
d5d1baa1
JE
165arpt_get_target_c(const struct arpt_entry *e)
166{
167 return arpt_get_target((struct arpt_entry *)e);
168}
169
170static inline struct arpt_entry *
171get_entry(const void *base, unsigned int offset)
1da177e4
LT
172{
173 return (struct arpt_entry *)(base + offset);
174}
175
6c7941de 176static inline
98e86403
JE
177struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
178{
179 return (void *)entry + entry->next_offset;
180}
181
3db05fea 182unsigned int arpt_do_table(struct sk_buff *skb,
b85c3dc9 183 const struct nf_hook_state *state,
4abff077 184 struct xt_table *table)
1da177e4 185{
6cb8ff3f 186 unsigned int hook = state->hook;
ddc214c4 187 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
1da177e4 188 unsigned int verdict = NF_DROP;
5452e425 189 const struct arphdr *arp;
3bd22997 190 struct arpt_entry *e, **jumpstack;
1da177e4 191 const char *indev, *outdev;
711bdde6 192 const void *table_base;
3bd22997 193 unsigned int cpu, stackidx = 0;
5452e425 194 const struct xt_table_info *private;
de74c169 195 struct xt_action_param acpar;
7f5c6d4f 196 unsigned int addend;
1da177e4 197
988b7050 198 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
1da177e4
LT
199 return NF_DROP;
200
b85c3dc9
DM
201 indev = state->in ? state->in->name : nulldevname;
202 outdev = state->out ? state->out->name : nulldevname;
1da177e4 203
7f5c6d4f
ED
204 local_bh_disable();
205 addend = xt_write_recseq_begin();
4be2b04e 206 private = READ_ONCE(table->private); /* Address dependency. */
3bd22997 207 cpu = smp_processor_id();
482cfc31 208 table_base = private->entries;
3bd22997 209 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
78454473 210
7814b6ec
FW
211 /* No TEE support for arptables, so no need to switch to alternate
212 * stack. All targets that reenter must return absolute verdicts.
213 */
2e4e6a17 214 e = get_entry(table_base, private->hook_entry[hook]);
1da177e4 215
613dbd95 216 acpar.state = state;
b4ba2611 217 acpar.hotdrop = false;
7eb35586 218
3db05fea 219 arp = arp_hdr(skb);
1da177e4 220 do {
87a2e70d 221 const struct xt_entry_target *t;
71ae0dff 222 struct xt_counters *counter;
a1ff4ac8
JE
223
224 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
225 e = arpt_next_entry(e);
226 continue;
227 }
228
71ae0dff
FW
229 counter = xt_get_this_cpu_counter(&e->counters);
230 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
1da177e4 231
d5d1baa1 232 t = arpt_get_target_c(e);
a1ff4ac8
JE
233
234 /* Standard target? */
235 if (!t->u.kernel.target->target) {
236 int v;
237
87a2e70d 238 v = ((struct xt_standard_target *)t)->verdict;
a1ff4ac8
JE
239 if (v < 0) {
240 /* Pop from stack? */
243bf6e2 241 if (v != XT_RETURN) {
95c96174 242 verdict = (unsigned int)(-v) - 1;
1da177e4 243 break;
a1ff4ac8 244 }
3bd22997
FW
245 if (stackidx == 0) {
246 e = get_entry(table_base,
247 private->underflow[hook]);
248 } else {
249 e = jumpstack[--stackidx];
250 e = arpt_next_entry(e);
251 }
a1ff4ac8 252 continue;
1da177e4 253 }
a1ff4ac8
JE
254 if (table_base + v
255 != arpt_next_entry(e)) {
57ebd808
FW
256 if (unlikely(stackidx >= private->stacksize)) {
257 verdict = NF_DROP;
258 break;
259 }
3bd22997 260 jumpstack[stackidx++] = e;
a1ff4ac8
JE
261 }
262
263 e = get_entry(table_base, v);
7a6b1c46 264 continue;
1da177e4 265 }
7a6b1c46 266
de74c169
JE
267 acpar.target = t->u.kernel.target;
268 acpar.targinfo = t->data;
269 verdict = t->u.kernel.target->target(skb, &acpar);
7a6b1c46 270
9beceb54
TY
271 if (verdict == XT_CONTINUE) {
272 /* Target might have changed stuff. */
273 arp = arp_hdr(skb);
7a6b1c46 274 e = arpt_next_entry(e);
9beceb54 275 } else {
7a6b1c46
JE
276 /* Verdict */
277 break;
9beceb54 278 }
b4ba2611 279 } while (!acpar.hotdrop);
7f5c6d4f
ED
280 xt_write_recseq_end(addend);
281 local_bh_enable();
1da177e4 282
b4ba2611 283 if (acpar.hotdrop)
1da177e4
LT
284 return NF_DROP;
285 else
286 return verdict;
287}
288
1da177e4 289/* All zeroes == unconditional rule. */
54d83fc7 290static inline bool unconditional(const struct arpt_entry *e)
1da177e4 291{
47901dc2 292 static const struct arpt_arp uncond;
1da177e4 293
54d83fc7
FW
294 return e->target_offset == sizeof(struct arpt_entry) &&
295 memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
1da177e4
LT
296}
297
298/* Figures out from what hook each rule can be called: returns 0 if
299 * there are loops. Puts hook bitmask in comefrom.
300 */
98dbbfc3 301static int mark_source_chains(const struct xt_table_info *newinfo,
f4dc7771
FW
302 unsigned int valid_hooks, void *entry0,
303 unsigned int *offsets)
1da177e4
LT
304{
305 unsigned int hook;
306
307 /* No recursion; use packet counter to save back ptrs (reset
308 * to 0 as we leave), and comefrom to save source hook bitmask.
309 */
310 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
311 unsigned int pos = newinfo->hook_entry[hook];
68ad546a 312 struct arpt_entry *e = entry0 + pos;
1da177e4
LT
313
314 if (!(valid_hooks & (1 << hook)))
315 continue;
316
317 /* Set initial back pointer. */
318 e->counters.pcnt = pos;
319
320 for (;;) {
87a2e70d 321 const struct xt_standard_target *t
d5d1baa1 322 = (void *)arpt_get_target_c(e);
e1b4b9f3 323 int visited = e->comefrom & (1 << hook);
1da177e4 324
d7cdf816 325 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
1da177e4 326 return 0;
d7cdf816 327
1da177e4
LT
328 e->comefrom
329 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
330
331 /* Unconditional return/END. */
54d83fc7 332 if ((unconditional(e) &&
3666ed1c 333 (strcmp(t->target.u.user.name,
243bf6e2 334 XT_STANDARD_TARGET) == 0) &&
54d83fc7 335 t->verdict < 0) || visited) {
1da177e4
LT
336 unsigned int oldpos, size;
337
338 /* Return: backtrack through the last
339 * big jump.
340 */
341 do {
342 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
343 oldpos = pos;
344 pos = e->counters.pcnt;
345 e->counters.pcnt = 0;
346
347 /* We're at the start. */
348 if (pos == oldpos)
349 goto next;
350
68ad546a 351 e = entry0 + pos;
1da177e4
LT
352 } while (oldpos == pos + e->next_offset);
353
354 /* Move along one */
355 size = e->next_offset;
68ad546a 356 e = entry0 + pos + size;
f24e230d
FW
357 if (pos + size >= newinfo->size)
358 return 0;
1da177e4
LT
359 e->counters.pcnt = pos;
360 pos += size;
361 } else {
362 int newpos = t->verdict;
363
364 if (strcmp(t->target.u.user.name,
243bf6e2 365 XT_STANDARD_TARGET) == 0 &&
3666ed1c 366 newpos >= 0) {
1da177e4 367 /* This a jump; chase it. */
f4dc7771
FW
368 if (!xt_find_jump_offset(offsets, newpos,
369 newinfo->number))
370 return 0;
1da177e4
LT
371 } else {
372 /* ... this is a fallthru */
373 newpos = pos + e->next_offset;
f24e230d
FW
374 if (newpos >= newinfo->size)
375 return 0;
1da177e4 376 }
68ad546a 377 e = entry0 + newpos;
1da177e4
LT
378 e->counters.pcnt = pos;
379 pos = newpos;
380 }
381 }
d7cdf816 382next: ;
1da177e4
LT
383 }
384 return 1;
385}
386
fb5b6095
PM
387static inline int check_target(struct arpt_entry *e, const char *name)
388{
87a2e70d 389 struct xt_entry_target *t = arpt_get_target(e);
af5d6dc2
JE
390 struct xt_tgchk_param par = {
391 .table = name,
392 .entryinfo = e,
393 .target = t->u.kernel.target,
394 .targinfo = t->data,
395 .hook_mask = e->comefrom,
916a917d 396 .family = NFPROTO_ARP,
af5d6dc2
JE
397 };
398
d7cdf816 399 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
fb5b6095
PM
400}
401
402static inline int
ae0ac0ed
FW
403find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
404 struct xt_percpu_counter_alloc_state *alloc_state)
fb5b6095 405{
87a2e70d 406 struct xt_entry_target *t;
95eea855 407 struct xt_target *target;
fb5b6095
PM
408 int ret;
409
ae0ac0ed 410 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
71ae0dff
FW
411 return -ENOMEM;
412
fb5b6095 413 t = arpt_get_target(e);
d2a7b6ba
JE
414 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
415 t->u.user.revision);
416 if (IS_ERR(target)) {
d2a7b6ba 417 ret = PTR_ERR(target);
1da177e4
LT
418 goto out;
419 }
1da177e4 420 t->u.kernel.target = target;
1da177e4 421
fb5b6095 422 ret = check_target(e, name);
3cdc7c95
PM
423 if (ret)
424 goto err;
1da177e4 425 return 0;
3cdc7c95
PM
426err:
427 module_put(t->u.kernel.target->me);
1da177e4 428out:
4d31eef5 429 xt_percpu_counter_free(&e->counters);
71ae0dff 430
1da177e4
LT
431 return ret;
432}
433
d5d1baa1 434static bool check_underflow(const struct arpt_entry *e)
e2fe35c1 435{
87a2e70d 436 const struct xt_entry_target *t;
e2fe35c1
JE
437 unsigned int verdict;
438
54d83fc7 439 if (!unconditional(e))
e2fe35c1 440 return false;
d5d1baa1 441 t = arpt_get_target_c(e);
e2fe35c1
JE
442 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
443 return false;
87a2e70d 444 verdict = ((struct xt_standard_target *)t)->verdict;
e2fe35c1
JE
445 verdict = -verdict - 1;
446 return verdict == NF_DROP || verdict == NF_ACCEPT;
447}
448
1da177e4 449static inline int check_entry_size_and_hooks(struct arpt_entry *e,
2e4e6a17 450 struct xt_table_info *newinfo,
d5d1baa1
JE
451 const unsigned char *base,
452 const unsigned char *limit,
1da177e4
LT
453 const unsigned int *hook_entries,
454 const unsigned int *underflows,
0559518b 455 unsigned int valid_hooks)
1da177e4
LT
456{
457 unsigned int h;
bdf533de 458 int err;
1da177e4 459
3666ed1c 460 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
6e94e0cf 461 (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
d7cdf816 462 (unsigned char *)e + e->next_offset > limit)
1da177e4 463 return -EINVAL;
1da177e4
LT
464
465 if (e->next_offset
d7cdf816 466 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
1da177e4 467 return -EINVAL;
1da177e4 468
aa412ba2
FW
469 if (!arp_checkentry(&e->arp))
470 return -EINVAL;
471
ce683e5f
FW
472 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
473 e->next_offset);
bdf533de
FW
474 if (err)
475 return err;
476
1da177e4
LT
477 /* Check hooks & underflows */
478 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
a7d51738
JE
479 if (!(valid_hooks & (1 << h)))
480 continue;
1da177e4
LT
481 if ((unsigned char *)e - base == hook_entries[h])
482 newinfo->hook_entry[h] = hook_entries[h];
90e7d4ab 483 if ((unsigned char *)e - base == underflows[h]) {
d7cdf816 484 if (!check_underflow(e))
90e7d4ab 485 return -EINVAL;
d7cdf816 486
1da177e4 487 newinfo->underflow[h] = underflows[h];
90e7d4ab 488 }
1da177e4
LT
489 }
490
1da177e4 491 /* Clear counters and comefrom */
2e4e6a17 492 e->counters = ((struct xt_counters) { 0, 0 });
1da177e4 493 e->comefrom = 0;
1da177e4
LT
494 return 0;
495}
496
0559518b 497static inline void cleanup_entry(struct arpt_entry *e)
1da177e4 498{
a2df1648 499 struct xt_tgdtor_param par;
87a2e70d 500 struct xt_entry_target *t;
1da177e4 501
1da177e4 502 t = arpt_get_target(e);
a2df1648
JE
503 par.target = t->u.kernel.target;
504 par.targinfo = t->data;
916a917d 505 par.family = NFPROTO_ARP;
a2df1648
JE
506 if (par.target->destroy != NULL)
507 par.target->destroy(&par);
508 module_put(par.target->me);
4d31eef5 509 xt_percpu_counter_free(&e->counters);
1da177e4
LT
510}
511
512/* Checks and translates the user-supplied table segment (held in
513 * newinfo).
514 */
0f234214 515static int translate_table(struct xt_table_info *newinfo, void *entry0,
6c28255b 516 const struct arpt_replace *repl)
1da177e4 517{
ae0ac0ed 518 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
72b2b1dd 519 struct arpt_entry *iter;
f4dc7771 520 unsigned int *offsets;
1da177e4 521 unsigned int i;
72b2b1dd 522 int ret = 0;
1da177e4 523
0f234214
JE
524 newinfo->size = repl->size;
525 newinfo->number = repl->num_entries;
1da177e4
LT
526
527 /* Init all hooks to impossible value. */
528 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
529 newinfo->hook_entry[i] = 0xFFFFFFFF;
530 newinfo->underflow[i] = 0xFFFFFFFF;
531 }
532
f4dc7771
FW
533 offsets = xt_alloc_entry_offsets(newinfo->number);
534 if (!offsets)
535 return -ENOMEM;
1da177e4
LT
536 i = 0;
537
538 /* Walk through entries, checking offsets. */
72b2b1dd
JE
539 xt_entry_foreach(iter, entry0, newinfo->size) {
540 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
6b4ff2d7
JE
541 entry0 + repl->size,
542 repl->hook_entry,
543 repl->underflow,
544 repl->valid_hooks);
72b2b1dd 545 if (ret != 0)
f4dc7771
FW
546 goto out_free;
547 if (i < repl->num_entries)
548 offsets[i] = (void *)iter - entry0;
0559518b 549 ++i;
98dbbfc3
FW
550 if (strcmp(arpt_get_target(iter)->u.user.name,
551 XT_ERROR_TARGET) == 0)
552 ++newinfo->stacksize;
72b2b1dd 553 }
1da177e4 554
f4dc7771 555 ret = -EINVAL;
d7cdf816 556 if (i != repl->num_entries)
f4dc7771 557 goto out_free;
1da177e4 558
1b293e30
FW
559 ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
560 if (ret)
561 goto out_free;
1da177e4 562
f4dc7771
FW
563 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
564 ret = -ELOOP;
565 goto out_free;
566 }
567 kvfree(offsets);
74c9c0c1 568
1da177e4
LT
569 /* Finally, each sanity check must pass */
570 i = 0;
72b2b1dd 571 xt_entry_foreach(iter, entry0, newinfo->size) {
ae0ac0ed
FW
572 ret = find_check_entry(iter, repl->name, repl->size,
573 &alloc_state);
72b2b1dd
JE
574 if (ret != 0)
575 break;
0559518b 576 ++i;
72b2b1dd 577 }
1da177e4 578
74c9c0c1 579 if (ret != 0) {
0559518b
JE
580 xt_entry_foreach(iter, entry0, newinfo->size) {
581 if (i-- == 0)
72b2b1dd 582 break;
0559518b
JE
583 cleanup_entry(iter);
584 }
74c9c0c1 585 return ret;
1da177e4
LT
586 }
587
f4dc7771
FW
588 return ret;
589 out_free:
590 kvfree(offsets);
1da177e4
LT
591 return ret;
592}
593
2e4e6a17
HW
594static void get_counters(const struct xt_table_info *t,
595 struct xt_counters counters[])
1da177e4 596{
72b2b1dd 597 struct arpt_entry *iter;
1da177e4
LT
598 unsigned int cpu;
599 unsigned int i;
600
6f912042 601 for_each_possible_cpu(cpu) {
7f5c6d4f 602 seqcount_t *s = &per_cpu(xt_recseq, cpu);
83723d60 603
1da177e4 604 i = 0;
482cfc31 605 xt_entry_foreach(iter, t->entries, t->size) {
71ae0dff 606 struct xt_counters *tmp;
83723d60
ED
607 u64 bcnt, pcnt;
608 unsigned int start;
609
71ae0dff 610 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
83723d60 611 do {
7f5c6d4f 612 start = read_seqcount_begin(s);
71ae0dff
FW
613 bcnt = tmp->bcnt;
614 pcnt = tmp->pcnt;
7f5c6d4f 615 } while (read_seqcount_retry(s, start));
83723d60
ED
616
617 ADD_COUNTER(counters[i], bcnt, pcnt);
0559518b 618 ++i;
a5d7a714 619 cond_resched();
0559518b 620 }
1da177e4 621 }
78454473
SH
622}
623
d13e7b2e
FW
624static void get_old_counters(const struct xt_table_info *t,
625 struct xt_counters counters[])
626{
627 struct arpt_entry *iter;
628 unsigned int cpu, i;
629
630 for_each_possible_cpu(cpu) {
631 i = 0;
632 xt_entry_foreach(iter, t->entries, t->size) {
633 struct xt_counters *tmp;
634
635 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
636 ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
637 ++i;
638 }
639 cond_resched();
640 }
641}
642
d5d1baa1 643static struct xt_counters *alloc_counters(const struct xt_table *table)
1da177e4 644{
27e2c26b 645 unsigned int countersize;
2e4e6a17 646 struct xt_counters *counters;
d5d1baa1 647 const struct xt_table_info *private = table->private;
1da177e4
LT
648
649 /* We need atomic snapshot of counters: rest doesn't change
650 * (other than comefrom, which userspace doesn't care
651 * about).
652 */
2e4e6a17 653 countersize = sizeof(struct xt_counters) * private->number;
83723d60 654 counters = vzalloc(countersize);
1da177e4
LT
655
656 if (counters == NULL)
942e4a2b 657 return ERR_PTR(-ENOMEM);
78454473 658
942e4a2b 659 get_counters(private, counters);
1da177e4 660
27e2c26b
PM
661 return counters;
662}
663
664static int copy_entries_to_user(unsigned int total_size,
d5d1baa1 665 const struct xt_table *table,
27e2c26b
PM
666 void __user *userptr)
667{
668 unsigned int off, num;
d5d1baa1 669 const struct arpt_entry *e;
27e2c26b 670 struct xt_counters *counters;
4abff077 671 struct xt_table_info *private = table->private;
27e2c26b
PM
672 int ret = 0;
673 void *loc_cpu_entry;
674
675 counters = alloc_counters(table);
676 if (IS_ERR(counters))
677 return PTR_ERR(counters);
678
482cfc31 679 loc_cpu_entry = private->entries;
1da177e4
LT
680
681 /* FIXME: use iterator macros --RR */
682 /* ... then go back and fix counters and names */
683 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
87a2e70d 684 const struct xt_entry_target *t;
1da177e4 685
68ad546a 686 e = loc_cpu_entry + off;
244b531b
WB
687 if (copy_to_user(userptr + off, e, sizeof(*e))) {
688 ret = -EFAULT;
689 goto free_counters;
690 }
1da177e4
LT
691 if (copy_to_user(userptr + off
692 + offsetof(struct arpt_entry, counters),
693 &counters[num],
694 sizeof(counters[num])) != 0) {
695 ret = -EFAULT;
696 goto free_counters;
697 }
698
d5d1baa1 699 t = arpt_get_target_c(e);
244b531b 700 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
1da177e4
LT
701 ret = -EFAULT;
702 goto free_counters;
703 }
704 }
705
706 free_counters:
707 vfree(counters);
708 return ret;
709}
710
d6a2ba07 711#ifdef CONFIG_COMPAT
739674fb 712static void compat_standard_from_user(void *dst, const void *src)
d6a2ba07
PM
713{
714 int v = *(compat_int_t *)src;
715
716 if (v > 0)
ee999d8b 717 v += xt_compat_calc_jump(NFPROTO_ARP, v);
d6a2ba07
PM
718 memcpy(dst, &v, sizeof(v));
719}
720
739674fb 721static int compat_standard_to_user(void __user *dst, const void *src)
d6a2ba07
PM
722{
723 compat_int_t cv = *(int *)src;
724
725 if (cv > 0)
ee999d8b 726 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
d6a2ba07
PM
727 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
728}
729
d5d1baa1 730static int compat_calc_entry(const struct arpt_entry *e,
d6a2ba07 731 const struct xt_table_info *info,
d5d1baa1 732 const void *base, struct xt_table_info *newinfo)
d6a2ba07 733{
87a2e70d 734 const struct xt_entry_target *t;
d6a2ba07
PM
735 unsigned int entry_offset;
736 int off, i, ret;
737
738 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
739 entry_offset = (void *)e - base;
740
d5d1baa1 741 t = arpt_get_target_c(e);
d6a2ba07
PM
742 off += xt_compat_target_offset(t->u.kernel.target);
743 newinfo->size -= off;
ee999d8b 744 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
d6a2ba07
PM
745 if (ret)
746 return ret;
747
748 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
749 if (info->hook_entry[i] &&
750 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
751 newinfo->hook_entry[i] -= off;
752 if (info->underflow[i] &&
753 (e < (struct arpt_entry *)(base + info->underflow[i])))
754 newinfo->underflow[i] -= off;
755 }
756 return 0;
757}
758
759static int compat_table_info(const struct xt_table_info *info,
760 struct xt_table_info *newinfo)
761{
72b2b1dd 762 struct arpt_entry *iter;
711bdde6 763 const void *loc_cpu_entry;
0559518b 764 int ret;
d6a2ba07
PM
765
766 if (!newinfo || !info)
767 return -EINVAL;
768
482cfc31 769 /* we dont care about newinfo->entries */
d6a2ba07
PM
770 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
771 newinfo->initial_entries = 0;
482cfc31 772 loc_cpu_entry = info->entries;
9782a11e
FW
773 ret = xt_compat_init_offsets(NFPROTO_ARP, info->number);
774 if (ret)
775 return ret;
72b2b1dd
JE
776 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
777 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
778 if (ret != 0)
0559518b 779 return ret;
72b2b1dd 780 }
0559518b 781 return 0;
d6a2ba07
PM
782}
783#endif
784
d5d1baa1 785static int get_info(struct net *net, void __user *user,
6c28255b 786 const int *len, int compat)
41acd975 787{
12b00c2c 788 char name[XT_TABLE_MAXNAMELEN];
4abff077 789 struct xt_table *t;
41acd975
PM
790 int ret;
791
d7cdf816 792 if (*len != sizeof(struct arpt_getinfo))
41acd975 793 return -EINVAL;
41acd975
PM
794
795 if (copy_from_user(name, user, sizeof(name)) != 0)
796 return -EFAULT;
797
12b00c2c 798 name[XT_TABLE_MAXNAMELEN-1] = '\0';
d6a2ba07
PM
799#ifdef CONFIG_COMPAT
800 if (compat)
ee999d8b 801 xt_compat_lock(NFPROTO_ARP);
d6a2ba07 802#endif
03d13b68
FW
803 t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
804 if (!IS_ERR(t)) {
41acd975 805 struct arpt_getinfo info;
5452e425 806 const struct xt_table_info *private = t->private;
d6a2ba07 807#ifdef CONFIG_COMPAT
14c7dbe0
AD
808 struct xt_table_info tmp;
809
d6a2ba07 810 if (compat) {
d6a2ba07 811 ret = compat_table_info(private, &tmp);
ee999d8b 812 xt_compat_flush_offsets(NFPROTO_ARP);
d6a2ba07
PM
813 private = &tmp;
814 }
815#endif
1a8b7a67 816 memset(&info, 0, sizeof(info));
41acd975
PM
817 info.valid_hooks = t->valid_hooks;
818 memcpy(info.hook_entry, private->hook_entry,
819 sizeof(info.hook_entry));
820 memcpy(info.underflow, private->underflow,
821 sizeof(info.underflow));
822 info.num_entries = private->number;
823 info.size = private->size;
824 strcpy(info.name, name);
825
826 if (copy_to_user(user, &info, *len) != 0)
827 ret = -EFAULT;
828 else
829 ret = 0;
830 xt_table_unlock(t);
831 module_put(t->me);
832 } else
03d13b68 833 ret = PTR_ERR(t);
d6a2ba07
PM
834#ifdef CONFIG_COMPAT
835 if (compat)
ee999d8b 836 xt_compat_unlock(NFPROTO_ARP);
d6a2ba07 837#endif
41acd975
PM
838 return ret;
839}
840
79df341a 841static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
d5d1baa1 842 const int *len)
1da177e4
LT
843{
844 int ret;
11f6dff8 845 struct arpt_get_entries get;
4abff077 846 struct xt_table *t;
1da177e4 847
d7cdf816 848 if (*len < sizeof(get))
11f6dff8 849 return -EINVAL;
11f6dff8
PM
850 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
851 return -EFAULT;
d7cdf816 852 if (*len != sizeof(struct arpt_get_entries) + get.size)
11f6dff8 853 return -EINVAL;
d7cdf816 854
b301f253 855 get.name[sizeof(get.name) - 1] = '\0';
11f6dff8 856
ee999d8b 857 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
03d13b68 858 if (!IS_ERR(t)) {
5452e425
JE
859 const struct xt_table_info *private = t->private;
860
11f6dff8 861 if (get.size == private->size)
2e4e6a17 862 ret = copy_entries_to_user(private->size,
1da177e4 863 t, uptr->entrytable);
d7cdf816 864 else
544473c1 865 ret = -EAGAIN;
d7cdf816 866
6b7d31fc 867 module_put(t->me);
2e4e6a17 868 xt_table_unlock(t);
1da177e4 869 } else
03d13b68 870 ret = PTR_ERR(t);
1da177e4
LT
871
872 return ret;
873}
874
79df341a
AD
875static int __do_replace(struct net *net, const char *name,
876 unsigned int valid_hooks,
d6a2ba07
PM
877 struct xt_table_info *newinfo,
878 unsigned int num_counters,
879 void __user *counters_ptr)
1da177e4
LT
880{
881 int ret;
4abff077 882 struct xt_table *t;
d6a2ba07 883 struct xt_table_info *oldinfo;
2e4e6a17 884 struct xt_counters *counters;
d6a2ba07 885 void *loc_cpu_old_entry;
72b2b1dd 886 struct arpt_entry *iter;
1da177e4 887
d6a2ba07 888 ret = 0;
c84ca954 889 counters = xt_counters_alloc(num_counters);
1da177e4
LT
890 if (!counters) {
891 ret = -ENOMEM;
d6a2ba07 892 goto out;
1da177e4 893 }
1da177e4 894
03d13b68
FW
895 t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
896 if (IS_ERR(t)) {
897 ret = PTR_ERR(t);
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
f31e5f1a
XL
919 xt_table_unlock(t);
920
d13e7b2e 921 get_old_counters(oldinfo, counters);
942e4a2b 922
1da177e4 923 /* Decrease module usage counts and free resource */
482cfc31 924 loc_cpu_old_entry = oldinfo->entries;
72b2b1dd 925 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
0559518b 926 cleanup_entry(iter);
31836064 927
2e4e6a17 928 xt_free_table_info(oldinfo);
d6a2ba07 929 if (copy_to_user(counters_ptr, counters,
c58dd2dd
TG
930 sizeof(struct xt_counters) * num_counters) != 0) {
931 /* Silent error, can't fail, new table is already in place */
932 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
933 }
1da177e4 934 vfree(counters);
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);
03d13b68
FW
1012 if (IS_ERR(t)) {
1013 ret = PTR_ERR(t);
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;
9782a11e 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);
9782a11e
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);
03d13b68 1402 if (!IS_ERR(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
03d13b68 1417 ret = PTR_ERR(t);
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
1da177e4 1652 return 0;
0eff66e6
PM
1653
1654err4:
4538506b 1655 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
0eff66e6 1656err2:
3cb609d5 1657 unregister_pernet_subsys(&arp_tables_net_ops);
0eff66e6
PM
1658err1:
1659 return ret;
1da177e4
LT
1660}
1661
65b4b4e8 1662static void __exit arp_tables_fini(void)
1da177e4
LT
1663{
1664 nf_unregister_sockopt(&arpt_sockopts);
4538506b 1665 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
3cb609d5 1666 unregister_pernet_subsys(&arp_tables_net_ops);
1da177e4
LT
1667}
1668
1669EXPORT_SYMBOL(arpt_register_table);
1670EXPORT_SYMBOL(arpt_unregister_table);
1671EXPORT_SYMBOL(arpt_do_table);
1da177e4 1672
65b4b4e8
AM
1673module_init(arp_tables_init);
1674module_exit(arp_tables_fini);