blob: d0d19192026d9eb2327cf68a47a4ba1b23dc7fe5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/if_arp.h>
18#include <linux/kmod.h>
19#include <linux/vmalloc.h>
20#include <linux/proc_fs.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
24#include <asm/uaccess.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Harald Welte2e4e6a12006-01-12 13:30:04 -080027#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/netfilter_arp/arp_tables.h>
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
32MODULE_DESCRIPTION("arptables core");
33
34/*#define DEBUG_ARP_TABLES*/
35/*#define DEBUG_ARP_TABLES_USER*/
36
37#ifdef DEBUG_ARP_TABLES
38#define dprintf(format, args...) printk(format , ## args)
39#else
40#define dprintf(format, args...)
41#endif
42
43#ifdef DEBUG_ARP_TABLES_USER
44#define duprintf(format, args...) printk(format , ## args)
45#else
46#define duprintf(format, args...)
47#endif
48
49#ifdef CONFIG_NETFILTER_DEBUG
50#define ARP_NF_ASSERT(x) \
51do { \
52 if (!(x)) \
53 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
54 __FUNCTION__, __FILE__, __LINE__); \
55} while(0)
56#else
57#define ARP_NF_ASSERT(x)
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/netfilter_ipv4/listhelp.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
63 char *hdr_addr, int len)
64{
65 int i, ret;
66
67 if (len > ARPT_DEV_ADDR_LEN_MAX)
68 len = ARPT_DEV_ADDR_LEN_MAX;
69
70 ret = 0;
71 for (i = 0; i < len; i++)
72 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
73
74 return (ret != 0);
75}
76
77/* Returns whether packet matches rule or not. */
78static inline int arp_packet_match(const struct arphdr *arphdr,
79 struct net_device *dev,
80 const char *indev,
81 const char *outdev,
82 const struct arpt_arp *arpinfo)
83{
84 char *arpptr = (char *)(arphdr + 1);
85 char *src_devaddr, *tgt_devaddr;
86 u32 src_ipaddr, tgt_ipaddr;
87 int i, ret;
88
89#define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
90
91 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
92 ARPT_INV_ARPOP)) {
93 dprintf("ARP operation field mismatch.\n");
94 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
95 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
96 return 0;
97 }
98
99 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
100 ARPT_INV_ARPHRD)) {
101 dprintf("ARP hardware address format mismatch.\n");
102 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
103 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
104 return 0;
105 }
106
107 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
108 ARPT_INV_ARPPRO)) {
109 dprintf("ARP protocol address format mismatch.\n");
110 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
111 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
112 return 0;
113 }
114
115 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
116 ARPT_INV_ARPHLN)) {
117 dprintf("ARP hardware address length mismatch.\n");
118 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
119 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
120 return 0;
121 }
122
123 src_devaddr = arpptr;
124 arpptr += dev->addr_len;
125 memcpy(&src_ipaddr, arpptr, sizeof(u32));
126 arpptr += sizeof(u32);
127 tgt_devaddr = arpptr;
128 arpptr += dev->addr_len;
129 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
130
131 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
132 ARPT_INV_SRCDEVADDR) ||
133 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
134 ARPT_INV_TGTDEVADDR)) {
135 dprintf("Source or target device address mismatch.\n");
136
137 return 0;
138 }
139
140 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
141 ARPT_INV_SRCIP) ||
142 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
143 ARPT_INV_TGTIP)) {
144 dprintf("Source or target IP address mismatch.\n");
145
146 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
147 NIPQUAD(src_ipaddr),
148 NIPQUAD(arpinfo->smsk.s_addr),
149 NIPQUAD(arpinfo->src.s_addr),
150 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
151 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
152 NIPQUAD(tgt_ipaddr),
153 NIPQUAD(arpinfo->tmsk.s_addr),
154 NIPQUAD(arpinfo->tgt.s_addr),
155 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
156 return 0;
157 }
158
159 /* Look for ifname matches. */
160 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
161 ret |= (indev[i] ^ arpinfo->iniface[i])
162 & arpinfo->iniface_mask[i];
163 }
164
165 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
166 dprintf("VIA in mismatch (%s vs %s).%s\n",
167 indev, arpinfo->iniface,
168 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
169 return 0;
170 }
171
172 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
173 unsigned long odev;
174 memcpy(&odev, outdev + i*sizeof(unsigned long),
175 sizeof(unsigned long));
176 ret |= (odev
177 ^ ((const unsigned long *)arpinfo->outiface)[i])
178 & ((const unsigned long *)arpinfo->outiface_mask)[i];
179 }
180
181 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
182 dprintf("VIA out mismatch (%s vs %s).%s\n",
183 outdev, arpinfo->outiface,
184 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
185 return 0;
186 }
187
188 return 1;
189}
190
191static inline int arp_checkentry(const struct arpt_arp *arp)
192{
193 if (arp->flags & ~ARPT_F_MASK) {
194 duprintf("Unknown flag bits set: %08X\n",
195 arp->flags & ~ARPT_F_MASK);
196 return 0;
197 }
198 if (arp->invflags & ~ARPT_INV_MASK) {
199 duprintf("Unknown invflag bits set: %08X\n",
200 arp->invflags & ~ARPT_INV_MASK);
201 return 0;
202 }
203
204 return 1;
205}
206
207static unsigned int arpt_error(struct sk_buff **pskb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 const struct net_device *in,
209 const struct net_device *out,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800210 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800211 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 const void *targinfo,
213 void *userinfo)
214{
215 if (net_ratelimit())
216 printk("arp_tables: error: '%s'\n", (char *)targinfo);
217
218 return NF_DROP;
219}
220
221static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
222{
223 return (struct arpt_entry *)(base + offset);
224}
225
226unsigned int arpt_do_table(struct sk_buff **pskb,
227 unsigned int hook,
228 const struct net_device *in,
229 const struct net_device *out,
230 struct arpt_table *table,
231 void *userdata)
232{
233 static const char nulldevname[IFNAMSIZ];
234 unsigned int verdict = NF_DROP;
235 struct arphdr *arp;
236 int hotdrop = 0;
237 struct arpt_entry *e, *back;
238 const char *indev, *outdev;
239 void *table_base;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800240 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
243 if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
244 (2 * (*pskb)->dev->addr_len) +
245 (2 * sizeof(u32)))))
246 return NF_DROP;
247
248 indev = in ? in->name : nulldevname;
249 outdev = out ? out->name : nulldevname;
250
251 read_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800252 table_base = (void *)private->entries[smp_processor_id()];
253 e = get_entry(table_base, private->hook_entry[hook]);
254 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 arp = (*pskb)->nh.arph;
257 do {
258 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
259 struct arpt_entry_target *t;
260 int hdr_len;
261
262 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
263 (2 * (*pskb)->dev->addr_len);
264 ADD_COUNTER(e->counters, hdr_len, 1);
265
266 t = arpt_get_target(e);
267
268 /* Standard target? */
269 if (!t->u.kernel.target->target) {
270 int v;
271
272 v = ((struct arpt_standard_target *)t)->verdict;
273 if (v < 0) {
274 /* Pop from stack? */
275 if (v != ARPT_RETURN) {
276 verdict = (unsigned)(-v) - 1;
277 break;
278 }
279 e = back;
280 back = get_entry(table_base,
281 back->comefrom);
282 continue;
283 }
284 if (table_base + v
285 != (void *)e + e->next_offset) {
286 /* Save old back ptr in next entry */
287 struct arpt_entry *next
288 = (void *)e + e->next_offset;
289 next->comefrom =
290 (void *)back - table_base;
291
292 /* set back pointer to next entry */
293 back = next;
294 }
295
296 e = get_entry(table_base, v);
297 } else {
298 /* Targets which reenter must return
299 * abs. verdicts
300 */
301 verdict = t->u.kernel.target->target(pskb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 in, out,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800303 hook,
Patrick McHardy1c524832006-03-20 18:02:15 -0800304 t->u.kernel.target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 t->data,
306 userdata);
307
308 /* Target might have changed stuff. */
309 arp = (*pskb)->nh.arph;
310
311 if (verdict == ARPT_CONTINUE)
312 e = (void *)e + e->next_offset;
313 else
314 /* Verdict */
315 break;
316 }
317 } else {
318 e = (void *)e + e->next_offset;
319 }
320 } while (!hotdrop);
321 read_unlock_bh(&table->lock);
322
323 if (hotdrop)
324 return NF_DROP;
325 else
326 return verdict;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* All zeroes == unconditional rule. */
330static inline int unconditional(const struct arpt_arp *arp)
331{
332 unsigned int i;
333
334 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
335 if (((__u32 *)arp)[i])
336 return 0;
337
338 return 1;
339}
340
341/* Figures out from what hook each rule can be called: returns 0 if
342 * there are loops. Puts hook bitmask in comefrom.
343 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800344static int mark_source_chains(struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800345 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned int hook;
348
349 /* No recursion; use packet counter to save back ptrs (reset
350 * to 0 as we leave), and comefrom to save source hook bitmask.
351 */
352 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
353 unsigned int pos = newinfo->hook_entry[hook];
354 struct arpt_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800355 = (struct arpt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (!(valid_hooks & (1 << hook)))
358 continue;
359
360 /* Set initial back pointer. */
361 e->counters.pcnt = pos;
362
363 for (;;) {
364 struct arpt_standard_target *t
365 = (void *)arpt_get_target(e);
366
367 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
368 printk("arptables: loop hook %u pos %u %08X.\n",
369 hook, pos, e->comefrom);
370 return 0;
371 }
372 e->comefrom
373 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
374
375 /* Unconditional return/END. */
376 if (e->target_offset == sizeof(struct arpt_entry)
377 && (strcmp(t->target.u.user.name,
378 ARPT_STANDARD_TARGET) == 0)
379 && t->verdict < 0
380 && unconditional(&e->arp)) {
381 unsigned int oldpos, size;
382
383 /* Return: backtrack through the last
384 * big jump.
385 */
386 do {
387 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
388 oldpos = pos;
389 pos = e->counters.pcnt;
390 e->counters.pcnt = 0;
391
392 /* We're at the start. */
393 if (pos == oldpos)
394 goto next;
395
396 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800397 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } while (oldpos == pos + e->next_offset);
399
400 /* Move along one */
401 size = e->next_offset;
402 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800403 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 e->counters.pcnt = pos;
405 pos += size;
406 } else {
407 int newpos = t->verdict;
408
409 if (strcmp(t->target.u.user.name,
410 ARPT_STANDARD_TARGET) == 0
411 && newpos >= 0) {
412 /* This a jump; chase it. */
413 duprintf("Jump rule %u -> %u\n",
414 pos, newpos);
415 } else {
416 /* ... this is a fallthru */
417 newpos = pos + e->next_offset;
418 }
419 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800420 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 e->counters.pcnt = pos;
422 pos = newpos;
423 }
424 }
425 next:
426 duprintf("Finished chain %u\n", hook);
427 }
428 return 1;
429}
430
431static inline int standard_check(const struct arpt_entry_target *t,
432 unsigned int max_offset)
433{
434 struct arpt_standard_target *targ = (void *)t;
435
436 /* Check standard info. */
437 if (t->u.target_size
438 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
439 duprintf("arpt_standard_check: target size %u != %Zu\n",
440 t->u.target_size,
441 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
442 return 0;
443 }
444
445 if (targ->verdict >= 0
446 && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
447 duprintf("arpt_standard_check: bad verdict (%i)\n",
448 targ->verdict);
449 return 0;
450 }
451
452 if (targ->verdict < -NF_MAX_VERDICT - 1) {
453 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
454 targ->verdict);
455 return 0;
456 }
457 return 1;
458}
459
460static struct arpt_target arpt_standard_target;
461
462static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
463 unsigned int *i)
464{
465 struct arpt_entry_target *t;
466 struct arpt_target *target;
467 int ret;
468
469 if (!arp_checkentry(&e->arp)) {
470 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
471 return -EINVAL;
472 }
473
474 t = arpt_get_target(e);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800475 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
476 t->u.user.revision),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200477 "arpt_%s", t->u.user.name);
478 if (IS_ERR(target) || !target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 duprintf("check_entry: `%s' not found\n", t->u.user.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200480 ret = target ? PTR_ERR(target) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto out;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 t->u.kernel.target = target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800485 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
486 name, e->comefrom, 0, 0);
487 if (ret)
488 goto err;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (t->u.kernel.target == &arpt_standard_target) {
491 if (!standard_check(t, size)) {
492 ret = -EINVAL;
493 goto out;
494 }
495 } else if (t->u.kernel.target->checkentry
Patrick McHardy1c524832006-03-20 18:02:15 -0800496 && !t->u.kernel.target->checkentry(name, e, target, t->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 t->u.target_size
498 - sizeof(*t),
499 e->comefrom)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 duprintf("arp_tables: check failed for `%s'.\n",
501 t->u.kernel.target->name);
502 ret = -EINVAL;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800503 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 (*i)++;
507 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800508err:
509 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510out:
511 return ret;
512}
513
514static inline int check_entry_size_and_hooks(struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800515 struct xt_table_info *newinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned char *base,
517 unsigned char *limit,
518 const unsigned int *hook_entries,
519 const unsigned int *underflows,
520 unsigned int *i)
521{
522 unsigned int h;
523
524 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
525 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
526 duprintf("Bad offset %p\n", e);
527 return -EINVAL;
528 }
529
530 if (e->next_offset
531 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
532 duprintf("checking: element %p size %u\n",
533 e, e->next_offset);
534 return -EINVAL;
535 }
536
537 /* Check hooks & underflows */
538 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
539 if ((unsigned char *)e - base == hook_entries[h])
540 newinfo->hook_entry[h] = hook_entries[h];
541 if ((unsigned char *)e - base == underflows[h])
542 newinfo->underflow[h] = underflows[h];
543 }
544
545 /* FIXME: underflows must be unconditional, standard verdicts
546 < 0 (not ARPT_RETURN). --RR */
547
548 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800549 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 e->comefrom = 0;
551
552 (*i)++;
553 return 0;
554}
555
556static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
557{
558 struct arpt_entry_target *t;
559
560 if (i && (*i)-- == 0)
561 return 1;
562
563 t = arpt_get_target(e);
564 if (t->u.kernel.target->destroy)
Patrick McHardy1c524832006-03-20 18:02:15 -0800565 t->u.kernel.target->destroy(t->u.kernel.target, t->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 t->u.target_size - sizeof(*t));
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,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800576 struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800577 void *entry0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 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. */
Eric Dumazet31836062005-12-13 23:13:48 -0800599 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 check_entry_size_and_hooks,
601 newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800602 entry0,
603 entry0 + size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 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
Eric Dumazet31836062005-12-13 23:13:48 -0800632 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 duprintf("Looping hook\n");
634 return -ELOOP;
635 }
636
637 /* Finally, each sanity check must pass */
638 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800639 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 check_entry, name, size, &i);
641
642 if (ret != 0) {
Eric Dumazet31836062005-12-13 23:13:48 -0800643 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 cleanup_entry, &i);
645 return ret;
646 }
647
648 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700649 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800650 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
651 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 return ret;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/* Gets counters. */
658static inline int add_entry_to_counter(const struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800659 struct xt_counters total[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int *i)
661{
662 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
663
664 (*i)++;
665 return 0;
666}
667
Eric Dumazet31836062005-12-13 23:13:48 -0800668static inline int set_entry_to_counter(const struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800669 struct xt_counters total[],
Eric Dumazet31836062005-12-13 23:13:48 -0800670 unsigned int *i)
671{
672 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
673
674 (*i)++;
675 return 0;
676}
677
Harald Welte2e4e6a12006-01-12 13:30:04 -0800678static void get_counters(const struct xt_table_info *t,
679 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 unsigned int cpu;
682 unsigned int i;
Eric Dumazet31836062005-12-13 23:13:48 -0800683 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700699 for_each_possible_cpu(cpu) {
Eric Dumazet31836062005-12-13 23:13:48 -0800700 if (cpu == curcpu)
701 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800703 ARPT_ENTRY_ITERATE(t->entries[cpu],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 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;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800717 struct xt_counters *counters;
718 struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800720 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* We need atomic snapshot of counters: rest doesn't change
723 * (other than comefrom, which userspace doesn't care
724 * about).
725 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800726 countersize = sizeof(struct xt_counters) * private->number;
727 counters = vmalloc_node(countersize, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (counters == NULL)
730 return -ENOMEM;
731
732 /* First, sum counters... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 write_lock_bh(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800734 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 write_unlock_bh(&table->lock);
736
Harald Welte2e4e6a12006-01-12 13:30:04 -0800737 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800738 /* ... then copy entire thing ... */
739 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 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
Eric Dumazet31836062005-12-13 23:13:48 -0800749 e = (struct arpt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 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
Harald Welte2e4e6a12006-01-12 13:30:04 -0800780 t = xt_find_table_lock(NF_ARP, entries->name);
Patrick McHardy31fe4d32006-03-12 20:40:43 -0800781 if (t && !IS_ERR(t)) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800782 struct xt_table_info *private = t->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 duprintf("t->private->number = %u\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800784 private->number);
785 if (entries->size == private->size)
786 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 t, uptr->entrytable);
788 else {
789 duprintf("get_entries: I've got %u not %u!\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800790 private->size, entries->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ret = -EINVAL;
792 }
Harald Welte6b7d31f2005-10-26 09:34:24 +0200793 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800794 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200796 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
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;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800806 struct xt_table_info *newinfo, *oldinfo;
807 struct xt_counters *counters;
Eric Dumazet31836062005-12-13 23:13:48 -0800808 void *loc_cpu_entry, *loc_cpu_old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
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
Kirill Korotaevee4bb812006-02-04 02:16:56 -0800817 /* 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
Harald Welte2e4e6a12006-01-12 13:30:04 -0800824 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!newinfo)
826 return -ENOMEM;
827
Eric Dumazet31836062005-12-13 23:13:48 -0800828 /* 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),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 tmp.size) != 0) {
832 ret = -EFAULT;
833 goto free_newinfo;
834 }
835
Harald Welte2e4e6a12006-01-12 13:30:04 -0800836 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!counters) {
838 ret = -ENOMEM;
839 goto free_newinfo;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 ret = translate_table(tmp.name, tmp.valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -0800843 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 tmp.hook_entry, tmp.underflow);
845 if (ret != 0)
846 goto free_newinfo_counters;
847
848 duprintf("arp_tables: Translated table\n");
849
Harald Welte2e4e6a12006-01-12 13:30:04 -0800850 t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
Harald Welte6b7d31f2005-10-26 09:34:24 +0200851 "arptable_%s", tmp.name);
852 if (!t || IS_ERR(t)) {
853 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto free_newinfo_counters_untrans;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
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;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200862 goto put_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Harald Welte2e4e6a12006-01-12 13:30:04 -0800865 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 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);
872 if ((oldinfo->number > oldinfo->initial_entries) ||
873 (newinfo->number <= oldinfo->initial_entries))
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 */
Eric Dumazet31836062005-12-13 23:13:48 -0800882 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
883 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
884
Harald Welte2e4e6a12006-01-12 13:30:04 -0800885 xt_free_table_info(oldinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (copy_to_user(tmp.counters, counters,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800887 sizeof(struct xt_counters) * tmp.num_counters) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ret = -EFAULT;
889 vfree(counters);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800890 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return ret;
892
893 put_module:
894 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800895 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 free_newinfo_counters_untrans:
Eric Dumazet31836062005-12-13 23:13:48 -0800897 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 free_newinfo_counters:
899 vfree(counters);
900 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -0800901 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 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,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909 const struct xt_counters addme[],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 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;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800922 struct xt_counters_info tmp, *paddc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct arpt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800924 struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200925 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800926 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
929 return -EFAULT;
930
Harald Welte2e4e6a12006-01-12 13:30:04 -0800931 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 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
Harald Welte2e4e6a12006-01-12 13:30:04 -0800943 t = xt_find_table_lock(NF_ARP, tmp.name);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200944 if (!t || IS_ERR(t)) {
945 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +0200947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 write_lock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800950 private = t->private;
Solar Designer2c8ac662006-05-19 02:16:52 -0700951 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ret = -EINVAL;
953 goto unlock_up_free;
954 }
955
956 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -0800957 /* Choose the copy that is on our node */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800958 loc_cpu_entry = private->entries[smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800959 ARPT_ENTRY_ITERATE(loc_cpu_entry,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800960 private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 add_counter_to_entry,
962 paddc->counters,
963 &i);
964 unlock_up_free:
965 write_unlock_bh(&t->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800966 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +0200967 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 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';
Harald Welte6b7d31f2005-10-26 09:34:24 +02001022
Harald Welte2e4e6a12006-01-12 13:30:04 -08001023 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001024 "arptable_%s", name);
1025 if (t && !IS_ERR(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct arpt_getinfo info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001027 struct xt_table_info *private = t->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 info.valid_hooks = t->valid_hooks;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001030 memcpy(info.hook_entry, private->hook_entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sizeof(info.hook_entry));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001032 memcpy(info.underflow, private->underflow,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 sizeof(info.underflow));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001034 info.num_entries = private->number;
1035 info.size = private->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 strcpy(info.name, name);
1037
1038 if (copy_to_user(user, &info, *len) != 0)
1039 ret = -EFAULT;
1040 else
1041 ret = 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001042 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001043 module_put(t->me);
1044 } else
1045 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
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
Harald Welte6b7d31f2005-10-26 09:34:24 +02001066 case ARPT_SO_GET_REVISION_TARGET: {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001067 struct xt_get_revision rev;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001068
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
Harald Welte2e4e6a12006-01-12 13:30:04 -08001078 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1079 rev.revision, 1, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001080 "arpt_%s", rev.name);
1081 break;
1082 }
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 default:
1085 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1086 ret = -EINVAL;
1087 }
1088
1089 return ret;
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092int arpt_register_table(struct arpt_table *table,
1093 const struct arpt_replace *repl)
1094{
1095 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001096 struct xt_table_info *newinfo;
1097 static struct xt_table_info bootstrap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 = { 0, 0, 0, { 0 }, { 0 }, { } };
Eric Dumazet31836062005-12-13 23:13:48 -08001099 void *loc_cpu_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Harald Welte2e4e6a12006-01-12 13:30:04 -08001101 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!newinfo) {
1103 ret = -ENOMEM;
1104 return ret;
1105 }
Eric Dumazet31836062005-12-13 23:13:48 -08001106
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 ret = translate_table(table->name, table->valid_hooks,
Eric Dumazet31836062005-12-13 23:13:48 -08001112 newinfo, loc_cpu_entry, repl->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 repl->num_entries,
1114 repl->hook_entry,
1115 repl->underflow);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 duprintf("arpt_register_table: translate table gives %d\n", ret);
1118 if (ret != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001119 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return ret;
1121 }
1122
Harald Welte2e4e6a12006-01-12 13:30:04 -08001123 if (xt_register_table(table, &bootstrap, newinfo) != 0) {
1124 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return ret;
1126 }
1127
Harald Welte2e4e6a12006-01-12 13:30:04 -08001128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131void arpt_unregister_table(struct arpt_table *table)
1132{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001133 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001134 void *loc_cpu_entry;
1135
Harald Welte2e4e6a12006-01-12 13:30:04 -08001136 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001139 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1140 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 cleanup_entry, NULL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001142 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145/* The built-in targets: standard (NULL) and error. */
1146static struct arpt_target arpt_standard_target = {
1147 .name = ARPT_STANDARD_TARGET,
Patrick McHardyaa83c1a2006-03-20 18:01:28 -08001148 .targetsize = sizeof(int),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001149 .family = NF_ARP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
1151
1152static struct arpt_target arpt_error_target = {
1153 .name = ARPT_ERROR_TARGET,
1154 .target = arpt_error,
Patrick McHardyaa83c1a2006-03-20 18:01:28 -08001155 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001156 .family = NF_ARP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157};
1158
1159static struct nf_sockopt_ops arpt_sockopts = {
1160 .pf = PF_INET,
1161 .set_optmin = ARPT_BASE_CTL,
1162 .set_optmax = ARPT_SO_SET_MAX+1,
1163 .set = do_arpt_set_ctl,
1164 .get_optmin = ARPT_BASE_CTL,
1165 .get_optmax = ARPT_SO_GET_MAX+1,
1166 .get = do_arpt_get_ctl,
1167};
1168
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001169static int __init arp_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 int ret;
1172
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173 xt_proto_init(NF_ARP);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* Noone else will be downing sem now, so we won't sleep */
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -08001176 xt_register_target(&arpt_standard_target);
1177 xt_register_target(&arpt_error_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Register setsockopt */
1180 ret = nf_register_sockopt(&arpt_sockopts);
1181 if (ret < 0) {
1182 duprintf("Unable to register sockopts.\n");
1183 return ret;
1184 }
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 printk("arp_tables: (C) 2002 David S. Miller\n");
1187 return 0;
1188}
1189
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001190static void __exit arp_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 nf_unregister_sockopt(&arpt_sockopts);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001193 xt_proto_fini(NF_ARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196EXPORT_SYMBOL(arpt_register_table);
1197EXPORT_SYMBOL(arpt_unregister_table);
1198EXPORT_SYMBOL(arpt_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001200module_init(arp_tables_init);
1201module_exit(arp_tables_fini);