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