[NET] IPV4: Fix whitespace errors.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / arp_tables.c
CommitLineData
1da177e4
LT
1/*
2 * Packet matching code for ARP packets.
3 *
4 * Based heavily, if not almost entirely, upon ip_tables.c framework.
5 *
6 * Some ARP specific bits are:
7 *
8 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
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>
22
23#include <asm/uaccess.h>
57b47a53 24#include <linux/mutex.h>
1da177e4 25
2e4e6a17 26#include <linux/netfilter/x_tables.h>
1da177e4
LT
27#include <linux/netfilter_arp/arp_tables.h>
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
31MODULE_DESCRIPTION("arptables core");
32
33/*#define DEBUG_ARP_TABLES*/
34/*#define DEBUG_ARP_TABLES_USER*/
35
36#ifdef DEBUG_ARP_TABLES
37#define dprintf(format, args...) printk(format , ## args)
38#else
39#define dprintf(format, args...)
40#endif
41
42#ifdef DEBUG_ARP_TABLES_USER
43#define duprintf(format, args...) printk(format , ## args)
44#else
45#define duprintf(format, args...)
46#endif
47
48#ifdef CONFIG_NETFILTER_DEBUG
49#define ARP_NF_ASSERT(x) \
50do { \
51 if (!(x)) \
52 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
53 __FUNCTION__, __FILE__, __LINE__); \
54} while(0)
55#else
56#define ARP_NF_ASSERT(x)
57#endif
1da177e4 58
1da177e4
LT
59static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
60 char *hdr_addr, int len)
61{
62 int i, ret;
63
64 if (len > ARPT_DEV_ADDR_LEN_MAX)
65 len = ARPT_DEV_ADDR_LEN_MAX;
66
67 ret = 0;
68 for (i = 0; i < len; i++)
69 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
70
71 return (ret != 0);
72}
73
74/* Returns whether packet matches rule or not. */
75static inline int arp_packet_match(const struct arphdr *arphdr,
76 struct net_device *dev,
77 const char *indev,
78 const char *outdev,
79 const struct arpt_arp *arpinfo)
80{
81 char *arpptr = (char *)(arphdr + 1);
82 char *src_devaddr, *tgt_devaddr;
59b8bfd8 83 __be32 src_ipaddr, tgt_ipaddr;
1da177e4
LT
84 int i, ret;
85
86#define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
87
88 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
89 ARPT_INV_ARPOP)) {
90 dprintf("ARP operation field mismatch.\n");
91 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
92 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
93 return 0;
94 }
95
96 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
97 ARPT_INV_ARPHRD)) {
98 dprintf("ARP hardware address format mismatch.\n");
99 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
100 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
101 return 0;
102 }
103
104 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
105 ARPT_INV_ARPPRO)) {
106 dprintf("ARP protocol address format mismatch.\n");
107 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
108 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
109 return 0;
110 }
111
112 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
113 ARPT_INV_ARPHLN)) {
114 dprintf("ARP hardware address length mismatch.\n");
115 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
116 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
117 return 0;
118 }
119
120 src_devaddr = arpptr;
121 arpptr += dev->addr_len;
122 memcpy(&src_ipaddr, arpptr, sizeof(u32));
123 arpptr += sizeof(u32);
124 tgt_devaddr = arpptr;
125 arpptr += dev->addr_len;
126 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
127
128 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
129 ARPT_INV_SRCDEVADDR) ||
130 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
131 ARPT_INV_TGTDEVADDR)) {
132 dprintf("Source or target device address mismatch.\n");
133
134 return 0;
135 }
136
137 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
138 ARPT_INV_SRCIP) ||
139 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
140 ARPT_INV_TGTIP)) {
141 dprintf("Source or target IP address mismatch.\n");
142
143 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
144 NIPQUAD(src_ipaddr),
145 NIPQUAD(arpinfo->smsk.s_addr),
146 NIPQUAD(arpinfo->src.s_addr),
147 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
148 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
149 NIPQUAD(tgt_ipaddr),
150 NIPQUAD(arpinfo->tmsk.s_addr),
151 NIPQUAD(arpinfo->tgt.s_addr),
152 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
153 return 0;
154 }
155
156 /* Look for ifname matches. */
157 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
158 ret |= (indev[i] ^ arpinfo->iniface[i])
159 & arpinfo->iniface_mask[i];
160 }
161
162 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
163 dprintf("VIA in mismatch (%s vs %s).%s\n",
164 indev, arpinfo->iniface,
165 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
166 return 0;
167 }
168
169 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
170 unsigned long odev;
171 memcpy(&odev, outdev + i*sizeof(unsigned long),
172 sizeof(unsigned long));
173 ret |= (odev
174 ^ ((const unsigned long *)arpinfo->outiface)[i])
175 & ((const unsigned long *)arpinfo->outiface_mask)[i];
176 }
177
178 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
179 dprintf("VIA out mismatch (%s vs %s).%s\n",
180 outdev, arpinfo->outiface,
181 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
182 return 0;
183 }
184
185 return 1;
186}
187
188static inline int arp_checkentry(const struct arpt_arp *arp)
189{
190 if (arp->flags & ~ARPT_F_MASK) {
191 duprintf("Unknown flag bits set: %08X\n",
192 arp->flags & ~ARPT_F_MASK);
193 return 0;
194 }
195 if (arp->invflags & ~ARPT_INV_MASK) {
196 duprintf("Unknown invflag bits set: %08X\n",
197 arp->invflags & ~ARPT_INV_MASK);
198 return 0;
199 }
200
201 return 1;
202}
203
204static unsigned int arpt_error(struct sk_buff **pskb,
1da177e4
LT
205 const struct net_device *in,
206 const struct net_device *out,
2e4e6a17 207 unsigned int hooknum,
c4986734 208 const struct xt_target *target,
fe1cb108 209 const void *targinfo)
1da177e4
LT
210{
211 if (net_ratelimit())
212 printk("arp_tables: error: '%s'\n", (char *)targinfo);
213
214 return NF_DROP;
215}
216
217static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
218{
219 return (struct arpt_entry *)(base + offset);
220}
221
222unsigned int arpt_do_table(struct sk_buff **pskb,
223 unsigned int hook,
224 const struct net_device *in,
225 const struct net_device *out,
fe1cb108 226 struct arpt_table *table)
1da177e4
LT
227{
228 static const char nulldevname[IFNAMSIZ];
229 unsigned int verdict = NF_DROP;
230 struct arphdr *arp;
231 int hotdrop = 0;
232 struct arpt_entry *e, *back;
233 const char *indev, *outdev;
234 void *table_base;
e0b7cde9 235 struct xt_table_info *private;
1da177e4
LT
236
237 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
238 if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
239 (2 * (*pskb)->dev->addr_len) +
240 (2 * sizeof(u32)))))
241 return NF_DROP;
242
243 indev = in ? in->name : nulldevname;
244 outdev = out ? out->name : nulldevname;
245
246 read_lock_bh(&table->lock);
e0b7cde9 247 private = table->private;
2e4e6a17
HW
248 table_base = (void *)private->entries[smp_processor_id()];
249 e = get_entry(table_base, private->hook_entry[hook]);
250 back = get_entry(table_base, private->underflow[hook]);
1da177e4
LT
251
252 arp = (*pskb)->nh.arph;
253 do {
254 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
255 struct arpt_entry_target *t;
256 int hdr_len;
257
258 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
259 (2 * (*pskb)->dev->addr_len);
260 ADD_COUNTER(e->counters, hdr_len, 1);
261
262 t = arpt_get_target(e);
263
264 /* Standard target? */
265 if (!t->u.kernel.target->target) {
266 int v;
267
268 v = ((struct arpt_standard_target *)t)->verdict;
269 if (v < 0) {
270 /* Pop from stack? */
271 if (v != ARPT_RETURN) {
272 verdict = (unsigned)(-v) - 1;
273 break;
274 }
275 e = back;
276 back = get_entry(table_base,
277 back->comefrom);
278 continue;
279 }
280 if (table_base + v
281 != (void *)e + e->next_offset) {
282 /* Save old back ptr in next entry */
283 struct arpt_entry *next
284 = (void *)e + e->next_offset;
285 next->comefrom =
286 (void *)back - table_base;
287
288 /* set back pointer to next entry */
289 back = next;
290 }
291
292 e = get_entry(table_base, v);
293 } else {
294 /* Targets which reenter must return
295 * abs. verdicts
296 */
297 verdict = t->u.kernel.target->target(pskb,
1da177e4 298 in, out,
2e4e6a17 299 hook,
1c524830 300 t->u.kernel.target,
fe1cb108 301 t->data);
1da177e4
LT
302
303 /* Target might have changed stuff. */
304 arp = (*pskb)->nh.arph;
305
306 if (verdict == ARPT_CONTINUE)
307 e = (void *)e + e->next_offset;
308 else
309 /* Verdict */
310 break;
311 }
312 } else {
313 e = (void *)e + e->next_offset;
314 }
315 } while (!hotdrop);
316 read_unlock_bh(&table->lock);
317
318 if (hotdrop)
319 return NF_DROP;
320 else
321 return verdict;
322}
323
1da177e4
LT
324/* All zeroes == unconditional rule. */
325static inline int unconditional(const struct arpt_arp *arp)
326{
327 unsigned int i;
328
329 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
330 if (((__u32 *)arp)[i])
331 return 0;
332
333 return 1;
334}
335
336/* Figures out from what hook each rule can be called: returns 0 if
337 * there are loops. Puts hook bitmask in comefrom.
338 */
2e4e6a17 339static int mark_source_chains(struct xt_table_info *newinfo,
31836064 340 unsigned int valid_hooks, void *entry0)
1da177e4
LT
341{
342 unsigned int hook;
343
344 /* No recursion; use packet counter to save back ptrs (reset
345 * to 0 as we leave), and comefrom to save source hook bitmask.
346 */
347 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
348 unsigned int pos = newinfo->hook_entry[hook];
349 struct arpt_entry *e
31836064 350 = (struct arpt_entry *)(entry0 + pos);
1da177e4
LT
351
352 if (!(valid_hooks & (1 << hook)))
353 continue;
354
355 /* Set initial back pointer. */
356 e->counters.pcnt = pos;
357
358 for (;;) {
359 struct arpt_standard_target *t
360 = (void *)arpt_get_target(e);
e1b4b9f3 361 int visited = e->comefrom & (1 << hook);
1da177e4
LT
362
363 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
364 printk("arptables: loop hook %u pos %u %08X.\n",
365 hook, pos, e->comefrom);
366 return 0;
367 }
368 e->comefrom
369 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
370
371 /* Unconditional return/END. */
e1b4b9f3 372 if ((e->target_offset == sizeof(struct arpt_entry)
1da177e4
LT
373 && (strcmp(t->target.u.user.name,
374 ARPT_STANDARD_TARGET) == 0)
375 && t->verdict < 0
e1b4b9f3 376 && unconditional(&e->arp)) || visited) {
1da177e4
LT
377 unsigned int oldpos, size;
378
74c9c0c1
DM
379 if (t->verdict < -NF_MAX_VERDICT - 1) {
380 duprintf("mark_source_chains: bad "
381 "negative verdict (%i)\n",
382 t->verdict);
383 return 0;
384 }
385
1da177e4
LT
386 /* Return: backtrack through the last
387 * big jump.
388 */
389 do {
390 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
391 oldpos = pos;
392 pos = e->counters.pcnt;
393 e->counters.pcnt = 0;
394
395 /* We're at the start. */
396 if (pos == oldpos)
397 goto next;
398
399 e = (struct arpt_entry *)
31836064 400 (entry0 + pos);
1da177e4
LT
401 } while (oldpos == pos + e->next_offset);
402
403 /* Move along one */
404 size = e->next_offset;
405 e = (struct arpt_entry *)
31836064 406 (entry0 + pos + size);
1da177e4
LT
407 e->counters.pcnt = pos;
408 pos += size;
409 } else {
410 int newpos = t->verdict;
411
412 if (strcmp(t->target.u.user.name,
413 ARPT_STANDARD_TARGET) == 0
414 && newpos >= 0) {
74c9c0c1
DM
415 if (newpos > newinfo->size -
416 sizeof(struct arpt_entry)) {
417 duprintf("mark_source_chains: "
418 "bad verdict (%i)\n",
419 newpos);
420 return 0;
421 }
422
1da177e4
LT
423 /* This a jump; chase it. */
424 duprintf("Jump rule %u -> %u\n",
425 pos, newpos);
426 } else {
427 /* ... this is a fallthru */
428 newpos = pos + e->next_offset;
429 }
430 e = (struct arpt_entry *)
31836064 431 (entry0 + newpos);
1da177e4
LT
432 e->counters.pcnt = pos;
433 pos = newpos;
434 }
435 }
436 next:
437 duprintf("Finished chain %u\n", hook);
438 }
439 return 1;
440}
441
442static inline int standard_check(const struct arpt_entry_target *t,
443 unsigned int max_offset)
444{
1da177e4
LT
445 /* Check standard info. */
446 if (t->u.target_size
447 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
448 duprintf("arpt_standard_check: target size %u != %Zu\n",
449 t->u.target_size,
450 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
451 return 0;
452 }
453
1da177e4
LT
454 return 1;
455}
456
457static struct arpt_target arpt_standard_target;
458
459static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
460 unsigned int *i)
461{
462 struct arpt_entry_target *t;
463 struct arpt_target *target;
464 int ret;
465
466 if (!arp_checkentry(&e->arp)) {
467 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
468 return -EINVAL;
469 }
470
590bdf7f
DM
471 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
472 return -EINVAL;
473
1da177e4 474 t = arpt_get_target(e);
590bdf7f
DM
475 if (e->target_offset + t->u.target_size > e->next_offset)
476 return -EINVAL;
477
2e4e6a17
HW
478 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
479 t->u.user.revision),
6b7d31fc
HW
480 "arpt_%s", t->u.user.name);
481 if (IS_ERR(target) || !target) {
1da177e4 482 duprintf("check_entry: `%s' not found\n", t->u.user.name);
6b7d31fc 483 ret = target ? PTR_ERR(target) : -ENOENT;
1da177e4
LT
484 goto out;
485 }
1da177e4 486 t->u.kernel.target = target;
1da177e4 487
3cdc7c95
PM
488 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
489 name, e->comefrom, 0, 0);
490 if (ret)
491 goto err;
492
1da177e4
LT
493 if (t->u.kernel.target == &arpt_standard_target) {
494 if (!standard_check(t, size)) {
495 ret = -EINVAL;
90d47db4 496 goto err;
1da177e4
LT
497 }
498 } else if (t->u.kernel.target->checkentry
1c524830 499 && !t->u.kernel.target->checkentry(name, e, target, t->data,
1da177e4 500 e->comefrom)) {
1da177e4
LT
501 duprintf("arp_tables: check failed for `%s'.\n",
502 t->u.kernel.target->name);
503 ret = -EINVAL;
3cdc7c95 504 goto err;
1da177e4
LT
505 }
506
507 (*i)++;
508 return 0;
3cdc7c95
PM
509err:
510 module_put(t->u.kernel.target->me);
1da177e4
LT
511out:
512 return ret;
513}
514
515static inline int check_entry_size_and_hooks(struct arpt_entry *e,
2e4e6a17 516 struct xt_table_info *newinfo,
1da177e4
LT
517 unsigned char *base,
518 unsigned char *limit,
519 const unsigned int *hook_entries,
520 const unsigned int *underflows,
521 unsigned int *i)
522{
523 unsigned int h;
524
525 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
526 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
527 duprintf("Bad offset %p\n", e);
528 return -EINVAL;
529 }
530
531 if (e->next_offset
532 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
533 duprintf("checking: element %p size %u\n",
534 e, e->next_offset);
535 return -EINVAL;
536 }
537
538 /* Check hooks & underflows */
539 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
540 if ((unsigned char *)e - base == hook_entries[h])
541 newinfo->hook_entry[h] = hook_entries[h];
542 if ((unsigned char *)e - base == underflows[h])
543 newinfo->underflow[h] = underflows[h];
544 }
545
546 /* FIXME: underflows must be unconditional, standard verdicts
e905a9ed 547 < 0 (not ARPT_RETURN). --RR */
1da177e4
LT
548
549 /* Clear counters and comefrom */
2e4e6a17 550 e->counters = ((struct xt_counters) { 0, 0 });
1da177e4
LT
551 e->comefrom = 0;
552
553 (*i)++;
554 return 0;
555}
556
557static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
558{
559 struct arpt_entry_target *t;
560
561 if (i && (*i)-- == 0)
562 return 1;
563
564 t = arpt_get_target(e);
565 if (t->u.kernel.target->destroy)
efa74165 566 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
1da177e4
LT
567 module_put(t->u.kernel.target->me);
568 return 0;
569}
570
571/* Checks and translates the user-supplied table segment (held in
572 * newinfo).
573 */
574static int translate_table(const char *name,
575 unsigned int valid_hooks,
2e4e6a17 576 struct xt_table_info *newinfo,
31836064 577 void *entry0,
1da177e4
LT
578 unsigned int size,
579 unsigned int number,
580 const unsigned int *hook_entries,
581 const unsigned int *underflows)
582{
583 unsigned int i;
584 int ret;
585
586 newinfo->size = size;
587 newinfo->number = number;
588
589 /* Init all hooks to impossible value. */
590 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
591 newinfo->hook_entry[i] = 0xFFFFFFFF;
592 newinfo->underflow[i] = 0xFFFFFFFF;
593 }
594
595 duprintf("translate_table: size %u\n", newinfo->size);
596 i = 0;
597
598 /* Walk through entries, checking offsets. */
31836064 599 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
1da177e4
LT
600 check_entry_size_and_hooks,
601 newinfo,
31836064
ED
602 entry0,
603 entry0 + size,
1da177e4
LT
604 hook_entries, underflows, &i);
605 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
606 if (ret != 0)
607 return ret;
608
609 if (i != number) {
610 duprintf("translate_table: %u not %u entries\n",
611 i, number);
612 return -EINVAL;
613 }
614
615 /* Check hooks all assigned */
616 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
617 /* Only hooks which are valid */
618 if (!(valid_hooks & (1 << i)))
619 continue;
620 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
621 duprintf("Invalid hook entry %u %u\n",
622 i, hook_entries[i]);
623 return -EINVAL;
624 }
625 if (newinfo->underflow[i] == 0xFFFFFFFF) {
626 duprintf("Invalid underflow %u %u\n",
627 i, underflows[i]);
628 return -EINVAL;
629 }
630 }
631
74c9c0c1
DM
632 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
633 duprintf("Looping hook\n");
634 return -ELOOP;
635 }
636
1da177e4
LT
637 /* Finally, each sanity check must pass */
638 i = 0;
31836064 639 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
1da177e4
LT
640 check_entry, name, size, &i);
641
74c9c0c1
DM
642 if (ret != 0) {
643 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
644 cleanup_entry, &i);
645 return ret;
1da177e4
LT
646 }
647
648 /* And one copy for every other CPU */
6f912042 649 for_each_possible_cpu(i) {
31836064
ED
650 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
651 memcpy(newinfo->entries[i], entry0, newinfo->size);
1da177e4
LT
652 }
653
654 return ret;
655}
656
1da177e4
LT
657/* Gets counters. */
658static inline int add_entry_to_counter(const struct arpt_entry *e,
2e4e6a17 659 struct xt_counters total[],
1da177e4
LT
660 unsigned int *i)
661{
662 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
663
664 (*i)++;
665 return 0;
666}
667
31836064 668static inline int set_entry_to_counter(const struct arpt_entry *e,
2e4e6a17 669 struct xt_counters total[],
31836064
ED
670 unsigned int *i)
671{
672 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
673
674 (*i)++;
675 return 0;
676}
677
2e4e6a17
HW
678static void get_counters(const struct xt_table_info *t,
679 struct xt_counters counters[])
1da177e4
LT
680{
681 unsigned int cpu;
682 unsigned int i;
31836064
ED
683 unsigned int curcpu;
684
685 /* Instead of clearing (by a previous call to memset())
686 * the counters and using adds, we set the counters
687 * with data used by 'current' CPU
688 * We dont care about preemption here.
689 */
690 curcpu = raw_smp_processor_id();
691
692 i = 0;
693 ARPT_ENTRY_ITERATE(t->entries[curcpu],
694 t->size,
695 set_entry_to_counter,
696 counters,
697 &i);
1da177e4 698
6f912042 699 for_each_possible_cpu(cpu) {
31836064
ED
700 if (cpu == curcpu)
701 continue;
1da177e4 702 i = 0;
31836064 703 ARPT_ENTRY_ITERATE(t->entries[cpu],
1da177e4
LT
704 t->size,
705 add_entry_to_counter,
706 counters,
707 &i);
708 }
709}
710
711static int copy_entries_to_user(unsigned int total_size,
712 struct arpt_table *table,
713 void __user *userptr)
714{
715 unsigned int off, num, countersize;
716 struct arpt_entry *e;
2e4e6a17
HW
717 struct xt_counters *counters;
718 struct xt_table_info *private = table->private;
1da177e4 719 int ret = 0;
31836064 720 void *loc_cpu_entry;
1da177e4
LT
721
722 /* We need atomic snapshot of counters: rest doesn't change
723 * (other than comefrom, which userspace doesn't care
724 * about).
725 */
2e4e6a17
HW
726 countersize = sizeof(struct xt_counters) * private->number;
727 counters = vmalloc_node(countersize, numa_node_id());
1da177e4
LT
728
729 if (counters == NULL)
730 return -ENOMEM;
731
732 /* First, sum counters... */
1da177e4 733 write_lock_bh(&table->lock);
2e4e6a17 734 get_counters(private, counters);
1da177e4
LT
735 write_unlock_bh(&table->lock);
736
2e4e6a17 737 loc_cpu_entry = private->entries[raw_smp_processor_id()];
31836064
ED
738 /* ... then copy entire thing ... */
739 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
1da177e4
LT
740 ret = -EFAULT;
741 goto free_counters;
742 }
743
744 /* FIXME: use iterator macros --RR */
745 /* ... then go back and fix counters and names */
746 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
747 struct arpt_entry_target *t;
748
31836064 749 e = (struct arpt_entry *)(loc_cpu_entry + off);
1da177e4
LT
750 if (copy_to_user(userptr + off
751 + offsetof(struct arpt_entry, counters),
752 &counters[num],
753 sizeof(counters[num])) != 0) {
754 ret = -EFAULT;
755 goto free_counters;
756 }
757
758 t = arpt_get_target(e);
759 if (copy_to_user(userptr + off + e->target_offset
760 + offsetof(struct arpt_entry_target,
761 u.user.name),
762 t->u.kernel.target->name,
763 strlen(t->u.kernel.target->name)+1) != 0) {
764 ret = -EFAULT;
765 goto free_counters;
766 }
767 }
768
769 free_counters:
770 vfree(counters);
771 return ret;
772}
773
774static int get_entries(const struct arpt_get_entries *entries,
775 struct arpt_get_entries __user *uptr)
776{
777 int ret;
778 struct arpt_table *t;
779
2e4e6a17 780 t = xt_find_table_lock(NF_ARP, entries->name);
31fe4d33 781 if (t && !IS_ERR(t)) {
2e4e6a17 782 struct xt_table_info *private = t->private;
1da177e4 783 duprintf("t->private->number = %u\n",
2e4e6a17
HW
784 private->number);
785 if (entries->size == private->size)
786 ret = copy_entries_to_user(private->size,
1da177e4
LT
787 t, uptr->entrytable);
788 else {
789 duprintf("get_entries: I've got %u not %u!\n",
2e4e6a17 790 private->size, entries->size);
1da177e4
LT
791 ret = -EINVAL;
792 }
6b7d31fc 793 module_put(t->me);
2e4e6a17 794 xt_table_unlock(t);
1da177e4 795 } else
6b7d31fc 796 ret = t ? PTR_ERR(t) : -ENOENT;
1da177e4
LT
797
798 return ret;
799}
800
801static int do_replace(void __user *user, unsigned int len)
802{
803 int ret;
804 struct arpt_replace tmp;
805 struct arpt_table *t;
2e4e6a17
HW
806 struct xt_table_info *newinfo, *oldinfo;
807 struct xt_counters *counters;
31836064 808 void *loc_cpu_entry, *loc_cpu_old_entry;
1da177e4
LT
809
810 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
811 return -EFAULT;
812
813 /* Hack: Causes ipchains to give correct error msg --RR */
814 if (len != sizeof(tmp) + tmp.size)
815 return -ENOPROTOOPT;
816
ee4bb818
KK
817 /* overflow check */
818 if (tmp.size >= (INT_MAX - sizeof(struct xt_table_info)) / NR_CPUS -
819 SMP_CACHE_BYTES)
820 return -ENOMEM;
821 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
822 return -ENOMEM;
823
2e4e6a17 824 newinfo = xt_alloc_table_info(tmp.size);
1da177e4
LT
825 if (!newinfo)
826 return -ENOMEM;
827
31836064
ED
828 /* choose the copy that is on our node/cpu */
829 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
830 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1da177e4
LT
831 tmp.size) != 0) {
832 ret = -EFAULT;
833 goto free_newinfo;
834 }
835
2e4e6a17 836 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
1da177e4
LT
837 if (!counters) {
838 ret = -ENOMEM;
839 goto free_newinfo;
840 }
1da177e4
LT
841
842 ret = translate_table(tmp.name, tmp.valid_hooks,
31836064 843 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1da177e4
LT
844 tmp.hook_entry, tmp.underflow);
845 if (ret != 0)
846 goto free_newinfo_counters;
847
848 duprintf("arp_tables: Translated table\n");
849
2e4e6a17 850 t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
6b7d31fc
HW
851 "arptable_%s", tmp.name);
852 if (!t || IS_ERR(t)) {
853 ret = t ? PTR_ERR(t) : -ENOENT;
1da177e4 854 goto free_newinfo_counters_untrans;
6b7d31fc 855 }
1da177e4
LT
856
857 /* You lied! */
858 if (tmp.valid_hooks != t->valid_hooks) {
859 duprintf("Valid hook crap: %08X vs %08X\n",
860 tmp.valid_hooks, t->valid_hooks);
861 ret = -EINVAL;
6b7d31fc 862 goto put_module;
1da177e4
LT
863 }
864
2e4e6a17 865 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
1da177e4
LT
866 if (!oldinfo)
867 goto put_module;
868
869 /* Update module usage count based on number of rules */
870 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
871 oldinfo->number, oldinfo->initial_entries, newinfo->number);
e905a9ed
YH
872 if ((oldinfo->number > oldinfo->initial_entries) ||
873 (newinfo->number <= oldinfo->initial_entries))
1da177e4
LT
874 module_put(t->me);
875 if ((oldinfo->number > oldinfo->initial_entries) &&
876 (newinfo->number <= oldinfo->initial_entries))
877 module_put(t->me);
878
879 /* Get the old counters. */
880 get_counters(oldinfo, counters);
881 /* Decrease module usage counts and free resource */
31836064
ED
882 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
883 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
884
2e4e6a17 885 xt_free_table_info(oldinfo);
1da177e4 886 if (copy_to_user(tmp.counters, counters,
2e4e6a17 887 sizeof(struct xt_counters) * tmp.num_counters) != 0)
1da177e4
LT
888 ret = -EFAULT;
889 vfree(counters);
2e4e6a17 890 xt_table_unlock(t);
1da177e4
LT
891 return ret;
892
893 put_module:
894 module_put(t->me);
2e4e6a17 895 xt_table_unlock(t);
1da177e4 896 free_newinfo_counters_untrans:
31836064 897 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1da177e4
LT
898 free_newinfo_counters:
899 vfree(counters);
900 free_newinfo:
2e4e6a17 901 xt_free_table_info(newinfo);
1da177e4
LT
902 return ret;
903}
904
905/* We're lazy, and add to the first CPU; overflow works its fey magic
906 * and everything is OK.
907 */
908static inline int add_counter_to_entry(struct arpt_entry *e,
2e4e6a17 909 const struct xt_counters addme[],
1da177e4
LT
910 unsigned int *i)
911{
912
913 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
914
915 (*i)++;
916 return 0;
917}
918
919static int do_add_counters(void __user *user, unsigned int len)
920{
921 unsigned int i;
2e4e6a17 922 struct xt_counters_info tmp, *paddc;
1da177e4 923 struct arpt_table *t;
2e4e6a17 924 struct xt_table_info *private;
6b7d31fc 925 int ret = 0;
31836064 926 void *loc_cpu_entry;
1da177e4
LT
927
928 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
929 return -EFAULT;
930
2e4e6a17 931 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
1da177e4
LT
932 return -EINVAL;
933
934 paddc = vmalloc(len);
935 if (!paddc)
936 return -ENOMEM;
937
938 if (copy_from_user(paddc, user, len) != 0) {
939 ret = -EFAULT;
940 goto free;
941 }
942
2e4e6a17 943 t = xt_find_table_lock(NF_ARP, tmp.name);
6b7d31fc
HW
944 if (!t || IS_ERR(t)) {
945 ret = t ? PTR_ERR(t) : -ENOENT;
1da177e4 946 goto free;
6b7d31fc 947 }
1da177e4
LT
948
949 write_lock_bh(&t->lock);
2e4e6a17 950 private = t->private;
2c8ac66b 951 if (private->number != tmp.num_counters) {
1da177e4
LT
952 ret = -EINVAL;
953 goto unlock_up_free;
954 }
955
956 i = 0;
31836064 957 /* Choose the copy that is on our node */
2e4e6a17 958 loc_cpu_entry = private->entries[smp_processor_id()];
31836064 959 ARPT_ENTRY_ITERATE(loc_cpu_entry,
2e4e6a17 960 private->size,
1da177e4
LT
961 add_counter_to_entry,
962 paddc->counters,
963 &i);
964 unlock_up_free:
965 write_unlock_bh(&t->lock);
2e4e6a17 966 xt_table_unlock(t);
6b7d31fc 967 module_put(t->me);
1da177e4
LT
968 free:
969 vfree(paddc);
970
971 return ret;
972}
973
974static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
975{
976 int ret;
977
978 if (!capable(CAP_NET_ADMIN))
979 return -EPERM;
980
981 switch (cmd) {
982 case ARPT_SO_SET_REPLACE:
983 ret = do_replace(user, len);
984 break;
985
986 case ARPT_SO_SET_ADD_COUNTERS:
987 ret = do_add_counters(user, len);
988 break;
989
990 default:
991 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
992 ret = -EINVAL;
993 }
994
995 return ret;
996}
997
998static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
999{
1000 int ret;
1001
1002 if (!capable(CAP_NET_ADMIN))
1003 return -EPERM;
1004
1005 switch (cmd) {
1006 case ARPT_SO_GET_INFO: {
1007 char name[ARPT_TABLE_MAXNAMELEN];
1008 struct arpt_table *t;
1009
1010 if (*len != sizeof(struct arpt_getinfo)) {
1011 duprintf("length %u != %Zu\n", *len,
1012 sizeof(struct arpt_getinfo));
1013 ret = -EINVAL;
1014 break;
1015 }
1016
1017 if (copy_from_user(name, user, sizeof(name)) != 0) {
1018 ret = -EFAULT;
1019 break;
1020 }
1021 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
6b7d31fc 1022
2e4e6a17 1023 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
6b7d31fc
HW
1024 "arptable_%s", name);
1025 if (t && !IS_ERR(t)) {
1da177e4 1026 struct arpt_getinfo info;
2e4e6a17 1027 struct xt_table_info *private = t->private;
1da177e4
LT
1028
1029 info.valid_hooks = t->valid_hooks;
2e4e6a17 1030 memcpy(info.hook_entry, private->hook_entry,
1da177e4 1031 sizeof(info.hook_entry));
2e4e6a17 1032 memcpy(info.underflow, private->underflow,
1da177e4 1033 sizeof(info.underflow));
2e4e6a17
HW
1034 info.num_entries = private->number;
1035 info.size = private->size;
1da177e4
LT
1036 strcpy(info.name, name);
1037
1038 if (copy_to_user(user, &info, *len) != 0)
1039 ret = -EFAULT;
1040 else
1041 ret = 0;
2e4e6a17 1042 xt_table_unlock(t);
6b7d31fc
HW
1043 module_put(t->me);
1044 } else
1045 ret = t ? PTR_ERR(t) : -ENOENT;
1da177e4
LT
1046 }
1047 break;
1048
1049 case ARPT_SO_GET_ENTRIES: {
1050 struct arpt_get_entries get;
1051
1052 if (*len < sizeof(get)) {
1053 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1054 ret = -EINVAL;
1055 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1056 ret = -EFAULT;
1057 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1058 duprintf("get_entries: %u != %Zu\n", *len,
1059 sizeof(struct arpt_get_entries) + get.size);
1060 ret = -EINVAL;
1061 } else
1062 ret = get_entries(&get, user);
1063 break;
1064 }
1065
6b7d31fc 1066 case ARPT_SO_GET_REVISION_TARGET: {
2e4e6a17 1067 struct xt_get_revision rev;
6b7d31fc
HW
1068
1069 if (*len != sizeof(rev)) {
1070 ret = -EINVAL;
1071 break;
1072 }
1073 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1074 ret = -EFAULT;
1075 break;
1076 }
1077
2e4e6a17
HW
1078 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1079 rev.revision, 1, &ret),
6b7d31fc
HW
1080 "arpt_%s", rev.name);
1081 break;
1082 }
1083
1da177e4
LT
1084 default:
1085 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1086 ret = -EINVAL;
1087 }
1088
1089 return ret;
1090}
1091
1da177e4
LT
1092int arpt_register_table(struct arpt_table *table,
1093 const struct arpt_replace *repl)
1094{
1095 int ret;
2e4e6a17
HW
1096 struct xt_table_info *newinfo;
1097 static struct xt_table_info bootstrap
1da177e4 1098 = { 0, 0, 0, { 0 }, { 0 }, { } };
31836064 1099 void *loc_cpu_entry;
1da177e4 1100
2e4e6a17 1101 newinfo = xt_alloc_table_info(repl->size);
1da177e4
LT
1102 if (!newinfo) {
1103 ret = -ENOMEM;
1104 return ret;
1105 }
31836064
ED
1106
1107 /* choose the copy on our node/cpu */
1108 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1109 memcpy(loc_cpu_entry, repl->entries, repl->size);
1da177e4
LT
1110
1111 ret = translate_table(table->name, table->valid_hooks,
31836064 1112 newinfo, loc_cpu_entry, repl->size,
1da177e4
LT
1113 repl->num_entries,
1114 repl->hook_entry,
1115 repl->underflow);
2e4e6a17 1116
1da177e4
LT
1117 duprintf("arpt_register_table: translate table gives %d\n", ret);
1118 if (ret != 0) {
2e4e6a17 1119 xt_free_table_info(newinfo);
1da177e4
LT
1120 return ret;
1121 }
1122
da298d3a
PM
1123 ret = xt_register_table(table, &bootstrap, newinfo);
1124 if (ret != 0) {
2e4e6a17 1125 xt_free_table_info(newinfo);
1da177e4
LT
1126 return ret;
1127 }
1128
2e4e6a17 1129 return 0;
1da177e4
LT
1130}
1131
1132void arpt_unregister_table(struct arpt_table *table)
1133{
2e4e6a17 1134 struct xt_table_info *private;
31836064
ED
1135 void *loc_cpu_entry;
1136
2e4e6a17 1137 private = xt_unregister_table(table);
1da177e4
LT
1138
1139 /* Decrease module usage counts and free resources */
2e4e6a17
HW
1140 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1141 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1da177e4 1142 cleanup_entry, NULL);
2e4e6a17 1143 xt_free_table_info(private);
1da177e4
LT
1144}
1145
1146/* The built-in targets: standard (NULL) and error. */
1147static struct arpt_target arpt_standard_target = {
1148 .name = ARPT_STANDARD_TARGET,
aa83c1ab 1149 .targetsize = sizeof(int),
a45049c5 1150 .family = NF_ARP,
1da177e4
LT
1151};
1152
1153static struct arpt_target arpt_error_target = {
1154 .name = ARPT_ERROR_TARGET,
1155 .target = arpt_error,
aa83c1ab 1156 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
a45049c5 1157 .family = NF_ARP,
1da177e4
LT
1158};
1159
1160static struct nf_sockopt_ops arpt_sockopts = {
1161 .pf = PF_INET,
1162 .set_optmin = ARPT_BASE_CTL,
1163 .set_optmax = ARPT_SO_SET_MAX+1,
1164 .set = do_arpt_set_ctl,
1165 .get_optmin = ARPT_BASE_CTL,
1166 .get_optmax = ARPT_SO_GET_MAX+1,
1167 .get = do_arpt_get_ctl,
1168};
1169
65b4b4e8 1170static int __init arp_tables_init(void)
1da177e4
LT
1171{
1172 int ret;
1173
0eff66e6
PM
1174 ret = xt_proto_init(NF_ARP);
1175 if (ret < 0)
1176 goto err1;
2e4e6a17 1177
1da177e4 1178 /* Noone else will be downing sem now, so we won't sleep */
0eff66e6
PM
1179 ret = xt_register_target(&arpt_standard_target);
1180 if (ret < 0)
1181 goto err2;
1182 ret = xt_register_target(&arpt_error_target);
1183 if (ret < 0)
1184 goto err3;
1da177e4
LT
1185
1186 /* Register setsockopt */
1187 ret = nf_register_sockopt(&arpt_sockopts);
0eff66e6
PM
1188 if (ret < 0)
1189 goto err4;
1da177e4 1190
1da177e4
LT
1191 printk("arp_tables: (C) 2002 David S. Miller\n");
1192 return 0;
0eff66e6
PM
1193
1194err4:
1195 xt_unregister_target(&arpt_error_target);
1196err3:
1197 xt_unregister_target(&arpt_standard_target);
1198err2:
1199 xt_proto_fini(NF_ARP);
1200err1:
1201 return ret;
1da177e4
LT
1202}
1203
65b4b4e8 1204static void __exit arp_tables_fini(void)
1da177e4
LT
1205{
1206 nf_unregister_sockopt(&arpt_sockopts);
f603b6ec
PM
1207 xt_unregister_target(&arpt_error_target);
1208 xt_unregister_target(&arpt_standard_target);
2e4e6a17 1209 xt_proto_fini(NF_ARP);
1da177e4
LT
1210}
1211
1212EXPORT_SYMBOL(arpt_register_table);
1213EXPORT_SYMBOL(arpt_unregister_table);
1214EXPORT_SYMBOL(arpt_do_table);
1da177e4 1215
65b4b4e8
AM
1216module_init(arp_tables_init);
1217module_exit(arp_tables_fini);