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