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