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