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