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