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