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