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