blob: 8f87fc38ccde3a4a55dc3b2ebb8f0ec5f0ecf4c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Packet matching code for ARP packets.
3 *
4 * Based heavily, if not almost entirely, upon ip_tables.c framework.
5 *
6 * Some ARP specific bits are:
7 *
8 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
Patrick McHardyf229f6c2013-04-06 15:24:29 +02009 * Copyright (C) 2006-2009 Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 */
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +020012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/if_arp.h>
18#include <linux/kmod.h>
19#include <linux/vmalloc.h>
20#include <linux/proc_fs.h>
21#include <linux/module.h>
22#include <linux/init.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080023#include <linux/mutex.h>
Patrick McHardyd6a2ba02007-12-17 22:26:54 -080024#include <linux/err.h>
25#include <net/compat.h>
Alexey Dobriyan79df3412008-01-31 04:04:32 -080026#include <net/sock.h>
Patrick McHardyd6a2ba02007-12-17 22:26:54 -080027#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Harald Welte2e4e6a12006-01-12 13:30:04 -080029#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netfilter_arp/arp_tables.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020031#include "../../netfilter/xt_repldata.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33MODULE_LICENSE("GPL");
34MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
35MODULE_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 Hemmingeraf567602010-05-13 15:00:20 +020053#define ARP_NF_ASSERT(x) WARN_ON(!(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
55#define ARP_NF_ASSERT(x)
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jan Engelhardte3eaa992009-06-17 22:14:54 +020058void *arpt_alloc_initial_table(const struct xt_table *info)
59{
60 return xt_alloc_initial_table(arpt, ARPT);
61}
62EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
Jan Engelhardt5452e422008-04-14 11:15:35 +020065 const char *hdr_addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
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 Dumazeta02cec22010-09-22 20:43:57 +000076 return ret != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Eric Dumazetddc214c2009-02-18 17:47:50 +010079/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030080 * Unfortunately, _b and _mask are not aligned to an int (or long int)
Eric Dumazetddc214c2009-02-18 17:47:50 +010081 * Some arches dont care, unrolling the loop is a win on them.
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070082 * For other arches, we only have a 16bit alignement.
Eric Dumazetddc214c2009-02-18 17:47:50 +010083 */
84static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
85{
86#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Eric Dumazetb8dfe492009-03-25 17:31:52 +010087 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
Eric Dumazetddc214c2009-02-18 17:47:50 +010088#else
89 unsigned long ret = 0;
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070090 const u16 *a = (const u16 *)_a;
91 const u16 *b = (const u16 *)_b;
92 const u16 *mask = (const u16 *)_mask;
Eric Dumazetddc214c2009-02-18 17:47:50 +010093 int i;
94
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070095 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
96 ret |= (a[i] ^ b[i]) & mask[i];
Eric Dumazetddc214c2009-02-18 17:47:50 +010097#endif
98 return ret;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Returns whether packet matches rule or not. */
102static 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 Engelhardt5452e422008-04-14 11:15:35 +0200108 const char *arpptr = (char *)(arphdr + 1);
109 const char *src_devaddr, *tgt_devaddr;
Al Viro59b8bfd2006-09-28 14:21:07 -0700110 __be32 src_ipaddr, tgt_ipaddr;
Eric Dumazetddc214c2009-02-18 17:47:50 +0100111 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jan Engelhardte79ec502007-12-17 22:44:06 -0800113#define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
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 Harrisoncffee382008-10-31 00:53:08 -0700170 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
171 &src_ipaddr,
172 &arpinfo->smsk.s_addr,
173 &arpinfo->src.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
Harvey Harrisoncffee382008-10-31 00:53:08 -0700175 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
176 &tgt_ipaddr,
177 &arpinfo->tmsk.s_addr,
178 &arpinfo->tgt.s_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
180 return 0;
181 }
182
183 /* Look for ifname matches. */
Eric Dumazetddc214c2009-02-18 17:47:50 +0100184 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
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 Dumazetddc214c2009-02-18 17:47:50 +0100193 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
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 Engelhardte79ec502007-12-17 22:44:06 -0800203#undef FWINV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static 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 Engelhardt7eb35582008-10-08 11:35:19 +0200222static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200223arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Joe Perchese87cc472012-05-13 21:56:26 +0000225 net_err_ratelimited("arp_tables: error: '%s'\n",
226 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return NF_DROP;
229}
230
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200231static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200232arpt_get_target_c(const struct arpt_entry *e)
233{
234 return arpt_get_target((struct arpt_entry *)e);
235}
236
237static inline struct arpt_entry *
238get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 return (struct arpt_entry *)(base + offset);
241}
242
Florian Westphal6c7941d2015-07-14 17:51:10 +0200243static inline
Jan Engelhardt98e86402009-04-15 21:06:05 +0200244struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
245{
246 return (void *)entry + entry->next_offset;
247}
248
Herbert Xu3db05fe2007-10-15 00:53:15 -0700249unsigned int arpt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned int hook,
David S. Millerb85c3dc2015-04-03 21:18:46 -0400251 const struct nf_hook_state *state,
Jan Engelhardt4abff072008-04-14 11:15:43 +0200252 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Eric Dumazetddc214c2009-02-18 17:47:50 +0100254 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned int verdict = NF_DROP;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200256 const struct arphdr *arp;
Florian Westphal3bd22992015-06-30 22:21:00 +0200257 struct arpt_entry *e, **jumpstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 const char *indev, *outdev;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700259 const void *table_base;
Florian Westphal3bd22992015-06-30 22:21:00 +0200260 unsigned int cpu, stackidx = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200261 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200262 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200263 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800265 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return NF_DROP;
267
David S. Millerb85c3dc2015-04-03 21:18:46 -0400268 indev = state->in ? state->in->name : nulldevname;
269 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200271 local_bh_disable();
272 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700273 private = table->private;
Florian Westphal3bd22992015-06-30 22:21:00 +0200274 cpu = smp_processor_id();
Will Deaconb416c142013-10-21 13:14:53 +0100275 /*
276 * Ensure we load private-> members after we've fetched the base
277 * pointer.
278 */
279 smp_read_barrier_depends();
Florian Westphal482cfc32015-06-11 01:34:55 +0200280 table_base = private->entries;
Florian Westphal3bd22992015-06-30 22:21:00 +0200281 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
Stephen Hemminger78454472009-02-20 10:35:32 +0100282
Florian Westphal7814b6e2015-07-14 17:51:08 +0200283 /* No TEE support for arptables, so no need to switch to alternate
284 * stack. All targets that reenter must return absolute verdicts.
285 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800286 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David S. Millerb85c3dc2015-04-03 21:18:46 -0400288 acpar.in = state->in;
289 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200290 acpar.hooknum = hook;
291 acpar.family = NFPROTO_ARP;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200292 acpar.hotdrop = false;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200293
Herbert Xu3db05fe2007-10-15 00:53:15 -0700294 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200296 const struct xt_entry_target *t;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200297 struct xt_counters *counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200299 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
Jan Engelhardt98e86402009-04-15 21:06:05 +0200300 e = arpt_next_entry(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200301 continue;
302 }
303
Florian Westphal71ae0df2015-06-11 01:34:54 +0200304 counter = xt_get_this_cpu_counter(&e->counters);
305 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200306
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200307 t = arpt_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200308
309 /* Standard target? */
310 if (!t->u.kernel.target->target) {
311 int v;
312
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200313 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200314 if (v < 0) {
315 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200316 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000317 verdict = (unsigned int)(-v) - 1;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200318 break;
319 }
Florian Westphal3bd22992015-06-30 22:21:00 +0200320 if (stackidx == 0) {
321 e = get_entry(table_base,
322 private->underflow[hook]);
323 } else {
324 e = jumpstack[--stackidx];
325 e = arpt_next_entry(e);
326 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200327 continue;
328 }
329 if (table_base + v
330 != arpt_next_entry(e)) {
Florian Westphal3bd22992015-06-30 22:21:00 +0200331 jumpstack[stackidx++] = e;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200332 }
333
334 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200335 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200337
Jan Engelhardtde74c162009-07-05 18:26:37 +0200338 acpar.target = t->u.kernel.target;
339 acpar.targinfo = t->data;
340 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200341
342 /* Target might have changed stuff. */
343 arp = arp_hdr(skb);
344
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200345 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200346 e = arpt_next_entry(e);
347 else
348 /* Verdict */
349 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200350 } while (!acpar.hotdrop);
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200351 xt_write_recseq_end(addend);
352 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200354 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return NF_DROP;
356 else
357 return verdict;
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* All zeroes == unconditional rule. */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200361static inline bool unconditional(const struct arpt_arp *arp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200363 static const struct arpt_arp uncond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200365 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/* Figures out from what hook each rule can be called: returns 0 if
369 * there are loops. Puts hook bitmask in comefrom.
370 */
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200371static int mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800372 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned int hook;
375
376 /* No recursion; use packet counter to save back ptrs (reset
377 * to 0 as we leave), and comefrom to save source hook bitmask.
378 */
379 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
380 unsigned int pos = newinfo->hook_entry[hook];
381 struct arpt_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800382 = (struct arpt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!(valid_hooks & (1 << hook)))
385 continue;
386
387 /* Set initial back pointer. */
388 e->counters.pcnt = pos;
389
390 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200391 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200392 = (void *)arpt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800393 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200396 pr_notice("arptables: loop hook %u pos %u %08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 hook, pos, e->comefrom);
398 return 0;
399 }
400 e->comefrom
401 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
402
403 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100404 if ((e->target_offset == sizeof(struct arpt_entry) &&
405 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200406 XT_STANDARD_TARGET) == 0) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100407 t->verdict < 0 && unconditional(&e->arp)) ||
408 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned int oldpos, size;
410
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100411 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200412 XT_STANDARD_TARGET) == 0) &&
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100413 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800414 duprintf("mark_source_chains: bad "
415 "negative verdict (%i)\n",
416 t->verdict);
417 return 0;
418 }
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Return: backtrack through the last
421 * big jump.
422 */
423 do {
424 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
425 oldpos = pos;
426 pos = e->counters.pcnt;
427 e->counters.pcnt = 0;
428
429 /* We're at the start. */
430 if (pos == oldpos)
431 goto next;
432
433 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800434 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 } while (oldpos == pos + e->next_offset);
436
437 /* Move along one */
438 size = e->next_offset;
439 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800440 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 e->counters.pcnt = pos;
442 pos += size;
443 } else {
444 int newpos = t->verdict;
445
446 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200447 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100448 newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800449 if (newpos > newinfo->size -
450 sizeof(struct arpt_entry)) {
451 duprintf("mark_source_chains: "
452 "bad verdict (%i)\n",
453 newpos);
454 return 0;
455 }
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* This a jump; chase it. */
458 duprintf("Jump rule %u -> %u\n",
459 pos, newpos);
460 } else {
461 /* ... this is a fallthru */
462 newpos = pos + e->next_offset;
463 }
464 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800465 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 e->counters.pcnt = pos;
467 pos = newpos;
468 }
469 }
470 next:
471 duprintf("Finished chain %u\n", hook);
472 }
473 return 1;
474}
475
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200476static inline int check_entry(const struct arpt_entry *e, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200478 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (!arp_checkentry(&e->arp)) {
481 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
482 return -EINVAL;
483 }
484
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200485 if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800486 return -EINVAL;
487
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200488 t = arpt_get_target_c(e);
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800489 if (e->target_offset + t->u.target_size > e->next_offset)
490 return -EINVAL;
491
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800492 return 0;
493}
494
495static inline int check_target(struct arpt_entry *e, const char *name)
496{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200497 struct xt_entry_target *t = arpt_get_target(e);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800498 int ret;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200499 struct xt_tgchk_param par = {
500 .table = name,
501 .entryinfo = e,
502 .target = t->u.kernel.target,
503 .targinfo = t->data,
504 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200505 .family = NFPROTO_ARP,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200506 };
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800507
Jan Engelhardt916a9172008-10-08 11:35:20 +0200508 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200509 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 duprintf("arp_tables: check failed for `%s'.\n",
511 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200514 return 0;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800515}
516
517static inline int
Jan Engelhardt05595182010-02-24 18:33:43 +0100518find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800519{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200520 struct xt_entry_target *t;
Jan Engelhardt95eea852008-04-14 11:15:43 +0200521 struct xt_target *target;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800522 int ret;
523
524 ret = check_entry(e, name);
525 if (ret)
526 return ret;
527
Florian Westphal71ae0df2015-06-11 01:34:54 +0200528 e->counters.pcnt = xt_percpu_counter_alloc();
529 if (IS_ERR_VALUE(e->counters.pcnt))
530 return -ENOMEM;
531
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800532 t = arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200533 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
534 t->u.user.revision);
535 if (IS_ERR(target)) {
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800536 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200537 ret = PTR_ERR(target);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800538 goto out;
539 }
540 t->u.kernel.target = target;
541
542 ret = check_target(e, name);
543 if (ret)
544 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800546err:
547 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548out:
Florian Westphal71ae0df2015-06-11 01:34:54 +0200549 xt_percpu_counter_free(e->counters.pcnt);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return ret;
552}
553
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200554static bool check_underflow(const struct arpt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200555{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200556 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200557 unsigned int verdict;
558
559 if (!unconditional(&e->arp))
560 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200561 t = arpt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200562 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
563 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200564 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200565 verdict = -verdict - 1;
566 return verdict == NF_DROP || verdict == NF_ACCEPT;
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static inline int check_entry_size_and_hooks(struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800570 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200571 const unsigned char *base,
572 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 const unsigned int *hook_entries,
574 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100575 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 unsigned int h;
578
Joe Perches3666ed12009-11-23 23:17:06 +0100579 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
580 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 duprintf("Bad offset %p\n", e);
582 return -EINVAL;
583 }
584
585 if (e->next_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200586 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 duprintf("checking: element %p size %u\n",
588 e, e->next_offset);
589 return -EINVAL;
590 }
591
592 /* Check hooks & underflows */
593 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200594 if (!(valid_hooks & (1 << h)))
595 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if ((unsigned char *)e - base == hook_entries[h])
597 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200598 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200599 if (!check_underflow(e)) {
600 pr_err("Underflows must be unconditional and "
601 "use the STANDARD target with "
602 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200603 return -EINVAL;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800610 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return 0;
613}
614
Jan Engelhardt05595182010-02-24 18:33:43 +0100615static inline void cleanup_entry(struct arpt_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200617 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200618 struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 t = arpt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200621 par.target = t->u.kernel.target;
622 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200623 par.family = NFPROTO_ARP;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200624 if (par.target->destroy != NULL)
625 par.target->destroy(&par);
626 module_put(par.target->me);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200627 xt_percpu_counter_free(e->counters.pcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630/* Checks and translates the user-supplied table segment (held in
631 * newinfo).
632 */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100633static int translate_table(struct xt_table_info *newinfo, void *entry0,
634 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100636 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100638 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jan Engelhardt0f234212010-02-24 18:36:04 +0100640 newinfo->size = repl->size;
641 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Init all hooks to impossible value. */
644 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
645 newinfo->hook_entry[i] = 0xFFFFFFFF;
646 newinfo->underflow[i] = 0xFFFFFFFF;
647 }
648
649 duprintf("translate_table: size %u\n", newinfo->size);
650 i = 0;
651
652 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100653 xt_entry_foreach(iter, entry0, newinfo->size) {
654 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100655 entry0 + repl->size,
656 repl->hook_entry,
657 repl->underflow,
658 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100659 if (ret != 0)
660 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100661 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200662 if (strcmp(arpt_get_target(iter)->u.user.name,
663 XT_ERROR_TARGET) == 0)
664 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
667 if (ret != 0)
668 return ret;
669
Jan Engelhardt0f234212010-02-24 18:36:04 +0100670 if (i != repl->num_entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 duprintf("translate_table: %u not %u entries\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100672 i, repl->num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
674 }
675
676 /* Check hooks all assigned */
677 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
678 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100679 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
681 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
682 duprintf("Invalid hook entry %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100683 i, repl->hook_entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EINVAL;
685 }
686 if (newinfo->underflow[i] == 0xFFFFFFFF) {
687 duprintf("Invalid underflow %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100688 i, repl->underflow[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
690 }
691 }
692
Jan Engelhardt0f234212010-02-24 18:36:04 +0100693 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800694 duprintf("Looping hook\n");
695 return -ELOOP;
696 }
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* Finally, each sanity check must pass */
699 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100700 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100701 ret = find_check_entry(iter, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100702 if (ret != 0)
703 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100704 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800707 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100708 xt_entry_foreach(iter, entry0, newinfo->size) {
709 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100710 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100711 cleanup_entry(iter);
712 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
717}
718
Harald Welte2e4e6a12006-01-12 13:30:04 -0800719static void get_counters(const struct xt_table_info *t,
720 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100722 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned int cpu;
724 unsigned int i;
725
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700726 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200727 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200730 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200731 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100732 u64 bcnt, pcnt;
733 unsigned int start;
734
Florian Westphal71ae0df2015-06-11 01:34:54 +0200735 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100736 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200737 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200738 bcnt = tmp->bcnt;
739 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200740 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100741
742 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100743 ++i;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100746}
747
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200748static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Patrick McHardy27e2c262007-12-17 21:56:48 -0800750 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800751 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200752 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* We need atomic snapshot of counters: rest doesn't change
755 * (other than comefrom, which userspace doesn't care
756 * about).
757 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800758 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100759 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700762 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700764 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Patrick McHardy27e2c262007-12-17 21:56:48 -0800766 return counters;
767}
768
769static int copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200770 const struct xt_table *table,
Patrick McHardy27e2c262007-12-17 21:56:48 -0800771 void __user *userptr)
772{
773 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200774 const struct arpt_entry *e;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800775 struct xt_counters *counters;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200776 struct xt_table_info *private = table->private;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800777 int ret = 0;
778 void *loc_cpu_entry;
779
780 counters = alloc_counters(table);
781 if (IS_ERR(counters))
782 return PTR_ERR(counters);
783
Florian Westphal482cfc32015-06-11 01:34:55 +0200784 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800785 /* ... then copy entire thing ... */
786 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ret = -EFAULT;
788 goto free_counters;
789 }
790
791 /* FIXME: use iterator macros --RR */
792 /* ... then go back and fix counters and names */
793 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200794 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Eric Dumazet31836062005-12-13 23:13:48 -0800796 e = (struct arpt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (copy_to_user(userptr + off
798 + offsetof(struct arpt_entry, counters),
799 &counters[num],
800 sizeof(counters[num])) != 0) {
801 ret = -EFAULT;
802 goto free_counters;
803 }
804
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200805 t = arpt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200807 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 u.user.name),
809 t->u.kernel.target->name,
810 strlen(t->u.kernel.target->name)+1) != 0) {
811 ret = -EFAULT;
812 goto free_counters;
813 }
814 }
815
816 free_counters:
817 vfree(counters);
818 return ret;
819}
820
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800821#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200822static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800823{
824 int v = *(compat_int_t *)src;
825
826 if (v > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200827 v += xt_compat_calc_jump(NFPROTO_ARP, v);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800828 memcpy(dst, &v, sizeof(v));
829}
830
Jan Engelhardt739674f2009-06-26 08:23:19 +0200831static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800832{
833 compat_int_t cv = *(int *)src;
834
835 if (cv > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200836 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800837 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
838}
839
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200840static int compat_calc_entry(const struct arpt_entry *e,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800841 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200842 const void *base, struct xt_table_info *newinfo)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800843{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200844 const struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800845 unsigned int entry_offset;
846 int off, i, ret;
847
848 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
849 entry_offset = (void *)e - base;
850
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200851 t = arpt_get_target_c(e);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800852 off += xt_compat_target_offset(t->u.kernel.target);
853 newinfo->size -= off;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200854 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800855 if (ret)
856 return ret;
857
858 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
859 if (info->hook_entry[i] &&
860 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
861 newinfo->hook_entry[i] -= off;
862 if (info->underflow[i] &&
863 (e < (struct arpt_entry *)(base + info->underflow[i])))
864 newinfo->underflow[i] -= off;
865 }
866 return 0;
867}
868
869static int compat_table_info(const struct xt_table_info *info,
870 struct xt_table_info *newinfo)
871{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100872 struct arpt_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700873 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100874 int ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800875
876 if (!newinfo || !info)
877 return -EINVAL;
878
Florian Westphal482cfc32015-06-11 01:34:55 +0200879 /* we dont care about newinfo->entries */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800880 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
881 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200882 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100883 xt_compat_init_offsets(NFPROTO_ARP, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100884 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
885 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
886 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100887 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100888 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100889 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800890}
891#endif
892
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200893static int get_info(struct net *net, void __user *user,
894 const int *len, int compat)
Patrick McHardy41acd972007-12-17 22:26:24 -0800895{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200896 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardt4abff072008-04-14 11:15:43 +0200897 struct xt_table *t;
Patrick McHardy41acd972007-12-17 22:26:24 -0800898 int ret;
899
900 if (*len != sizeof(struct arpt_getinfo)) {
901 duprintf("length %u != %Zu\n", *len,
902 sizeof(struct arpt_getinfo));
903 return -EINVAL;
904 }
905
906 if (copy_from_user(name, user, sizeof(name)) != 0)
907 return -EFAULT;
908
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200909 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800910#ifdef CONFIG_COMPAT
911 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200912 xt_compat_lock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800913#endif
Jan Engelhardtee999d82008-10-08 11:35:01 +0200914 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardy41acd972007-12-17 22:26:24 -0800915 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000916 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy41acd972007-12-17 22:26:24 -0800917 struct arpt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200918 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800919#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800920 struct xt_table_info tmp;
921
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800922 if (compat) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800923 ret = compat_table_info(private, &tmp);
Jan Engelhardtee999d82008-10-08 11:35:01 +0200924 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800925 private = &tmp;
926 }
927#endif
Vasiliy Kulikov1a8b7a62010-11-03 08:44:12 +0100928 memset(&info, 0, sizeof(info));
Patrick McHardy41acd972007-12-17 22:26:24 -0800929 info.valid_hooks = t->valid_hooks;
930 memcpy(info.hook_entry, private->hook_entry,
931 sizeof(info.hook_entry));
932 memcpy(info.underflow, private->underflow,
933 sizeof(info.underflow));
934 info.num_entries = private->number;
935 info.size = private->size;
936 strcpy(info.name, name);
937
938 if (copy_to_user(user, &info, *len) != 0)
939 ret = -EFAULT;
940 else
941 ret = 0;
942 xt_table_unlock(t);
943 module_put(t->me);
944 } else
945 ret = t ? PTR_ERR(t) : -ENOENT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800946#ifdef CONFIG_COMPAT
947 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200948 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800949#endif
Patrick McHardy41acd972007-12-17 22:26:24 -0800950 return ret;
951}
952
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800953static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200954 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 int ret;
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800957 struct arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200958 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800960 if (*len < sizeof(get)) {
961 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
962 return -EINVAL;
963 }
964 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
965 return -EFAULT;
966 if (*len != sizeof(struct arpt_get_entries) + get.size) {
967 duprintf("get_entries: %u != %Zu\n", *len,
968 sizeof(struct arpt_get_entries) + get.size);
969 return -EINVAL;
970 }
971
Jan Engelhardtee999d82008-10-08 11:35:01 +0200972 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000973 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +0200974 const struct xt_table_info *private = t->private;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 duprintf("t->private->number = %u\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800977 private->number);
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800978 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800979 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 t, uptr->entrytable);
981 else {
982 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800983 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +0200984 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Harald Welte6b7d31f2005-10-26 09:34:24 +0200986 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800987 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200989 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return ret;
992}
993
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800994static int __do_replace(struct net *net, const char *name,
995 unsigned int valid_hooks,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800996 struct xt_table_info *newinfo,
997 unsigned int num_counters,
998 void __user *counters_ptr)
999{
1000 int ret;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001001 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001002 struct xt_table_info *oldinfo;
1003 struct xt_counters *counters;
1004 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001005 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001006
1007 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001008 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001009 if (!counters) {
1010 ret = -ENOMEM;
1011 goto out;
1012 }
1013
Jan Engelhardtee999d82008-10-08 11:35:01 +02001014 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001015 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001016 if (IS_ERR_OR_NULL(t)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001017 ret = t ? PTR_ERR(t) : -ENOENT;
1018 goto free_newinfo_counters_untrans;
1019 }
1020
1021 /* You lied! */
1022 if (valid_hooks != t->valid_hooks) {
1023 duprintf("Valid hook crap: %08X vs %08X\n",
1024 valid_hooks, t->valid_hooks);
1025 ret = -EINVAL;
1026 goto put_module;
1027 }
1028
1029 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1030 if (!oldinfo)
1031 goto put_module;
1032
1033 /* Update module usage count based on number of rules */
1034 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1035 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1036 if ((oldinfo->number > oldinfo->initial_entries) ||
1037 (newinfo->number <= oldinfo->initial_entries))
1038 module_put(t->me);
1039 if ((oldinfo->number > oldinfo->initial_entries) &&
1040 (newinfo->number <= oldinfo->initial_entries))
1041 module_put(t->me);
1042
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001043 /* Get the old counters, and synchronize with replace */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001044 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001045
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001046 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +02001047 loc_cpu_old_entry = oldinfo->entries;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001048 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001049 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001050
1051 xt_free_table_info(oldinfo);
1052 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001053 sizeof(struct xt_counters) * num_counters) != 0) {
1054 /* Silent error, can't fail, new table is already in place */
1055 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1056 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001057 vfree(counters);
1058 xt_table_unlock(t);
1059 return ret;
1060
1061 put_module:
1062 module_put(t->me);
1063 xt_table_unlock(t);
1064 free_newinfo_counters_untrans:
1065 vfree(counters);
1066 out:
1067 return ret;
1068}
1069
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001070static int do_replace(struct net *net, const void __user *user,
1071 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 int ret;
1074 struct arpt_replace tmp;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001075 struct xt_table_info *newinfo;
1076 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001077 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1080 return -EFAULT;
1081
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001082 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001083 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1084 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001085 if (tmp.num_counters == 0)
1086 return -EINVAL;
1087
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001088 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001089
Harald Welte2e4e6a12006-01-12 13:30:04 -08001090 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!newinfo)
1092 return -ENOMEM;
1093
Florian Westphal482cfc32015-06-11 01:34:55 +02001094 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001095 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 tmp.size) != 0) {
1097 ret = -EFAULT;
1098 goto free_newinfo;
1099 }
1100
Jan Engelhardt0f234212010-02-24 18:36:04 +01001101 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (ret != 0)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001103 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 duprintf("arp_tables: Translated table\n");
1106
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001107 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001108 tmp.num_counters, tmp.counters);
1109 if (ret)
1110 goto free_newinfo_untrans;
1111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001113 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001114 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001115 cleanup_entry(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001117 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return ret;
1119}
1120
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001121static int do_add_counters(struct net *net, const void __user *user,
1122 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Florian Westphal482cfc32015-06-11 01:34:55 +02001124 unsigned int i;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001125 struct xt_counters_info tmp;
1126 struct xt_counters *paddc;
1127 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001128 const char *name;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001129 int size;
1130 void *ptmp;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001131 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001132 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001133 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001134 struct arpt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001135 unsigned int addend;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001136#ifdef CONFIG_COMPAT
1137 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001139 if (compat) {
1140 ptmp = &compat_tmp;
1141 size = sizeof(struct compat_xt_counters_info);
1142 } else
1143#endif
1144 {
1145 ptmp = &tmp;
1146 size = sizeof(struct xt_counters_info);
1147 }
1148
1149 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EFAULT;
1151
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001152#ifdef CONFIG_COMPAT
1153 if (compat) {
1154 num_counters = compat_tmp.num_counters;
1155 name = compat_tmp.name;
1156 } else
1157#endif
1158 {
1159 num_counters = tmp.num_counters;
1160 name = tmp.name;
1161 }
1162
1163 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -EINVAL;
1165
Eric Dumazete12f8e22010-06-04 13:31:29 +02001166 paddc = vmalloc(len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!paddc)
1168 return -ENOMEM;
1169
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001170 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ret = -EFAULT;
1172 goto free;
1173 }
1174
Jan Engelhardtee999d82008-10-08 11:35:01 +02001175 t = xt_find_table_lock(net, NFPROTO_ARP, name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001176 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001177 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001181 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001182 private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001183 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ret = -EINVAL;
1185 goto unlock_up_free;
1186 }
1187
1188 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +02001189
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001190 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001191 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001192 struct xt_counters *tmp;
1193
1194 tmp = xt_get_this_cpu_counter(&iter->counters);
1195 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001196 ++i;
1197 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001198 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001200 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001201 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001202 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 free:
1204 vfree(paddc);
1205
1206 return ret;
1207}
1208
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001209#ifdef CONFIG_COMPAT
Jan Engelhardt05595182010-02-24 18:33:43 +01001210static inline void compat_release_entry(struct compat_arpt_entry *e)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001211{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001212 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001213
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001214 t = compat_arpt_get_target(e);
1215 module_put(t->u.kernel.target->me);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001216}
1217
1218static inline int
1219check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1220 struct xt_table_info *newinfo,
1221 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001222 const unsigned char *base,
1223 const unsigned char *limit,
1224 const unsigned int *hook_entries,
1225 const unsigned int *underflows,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001226 const char *name)
1227{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001228 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001229 struct xt_target *target;
1230 unsigned int entry_offset;
1231 int ret, off, h;
1232
1233 duprintf("check_compat_entry_size_and_hooks %p\n", e);
Joe Perches3666ed12009-11-23 23:17:06 +01001234 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1235 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001236 duprintf("Bad offset %p, limit = %p\n", e, limit);
1237 return -EINVAL;
1238 }
1239
1240 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1241 sizeof(struct compat_xt_entry_target)) {
1242 duprintf("checking: element %p size %u\n",
1243 e, e->next_offset);
1244 return -EINVAL;
1245 }
1246
1247 /* For purposes of check_entry casting the compat entry is fine */
1248 ret = check_entry((struct arpt_entry *)e, name);
1249 if (ret)
1250 return ret;
1251
1252 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1253 entry_offset = (void *)e - (void *)base;
1254
1255 t = compat_arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001256 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1257 t->u.user.revision);
1258 if (IS_ERR(target)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001259 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1260 t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001261 ret = PTR_ERR(target);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001262 goto out;
1263 }
1264 t->u.kernel.target = target;
1265
1266 off += xt_compat_target_offset(target);
1267 *size += off;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001268 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001269 if (ret)
1270 goto release_target;
1271
1272 /* Check hooks & underflows */
1273 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1274 if ((unsigned char *)e - base == hook_entries[h])
1275 newinfo->hook_entry[h] = hook_entries[h];
1276 if ((unsigned char *)e - base == underflows[h])
1277 newinfo->underflow[h] = underflows[h];
1278 }
1279
1280 /* Clear counters and comefrom */
1281 memset(&e->counters, 0, sizeof(e->counters));
1282 e->comefrom = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001283 return 0;
1284
1285release_target:
1286 module_put(t->u.kernel.target->me);
1287out:
1288 return ret;
1289}
1290
1291static int
1292compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1293 unsigned int *size, const char *name,
1294 struct xt_table_info *newinfo, unsigned char *base)
1295{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001296 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001297 struct xt_target *target;
1298 struct arpt_entry *de;
1299 unsigned int origsize;
1300 int ret, h;
1301
1302 ret = 0;
1303 origsize = *size;
1304 de = (struct arpt_entry *)*dstptr;
1305 memcpy(de, e, sizeof(struct arpt_entry));
1306 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1307
1308 *dstptr += sizeof(struct arpt_entry);
1309 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1310
1311 de->target_offset = e->target_offset - (origsize - *size);
1312 t = compat_arpt_get_target(e);
1313 target = t->u.kernel.target;
1314 xt_compat_target_from_user(t, dstptr, size);
1315
1316 de->next_offset = e->next_offset - (origsize - *size);
1317 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1318 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1319 newinfo->hook_entry[h] -= origsize - *size;
1320 if ((unsigned char *)de - base < newinfo->underflow[h])
1321 newinfo->underflow[h] -= origsize - *size;
1322 }
1323 return ret;
1324}
1325
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001326static int translate_compat_table(const char *name,
1327 unsigned int valid_hooks,
1328 struct xt_table_info **pinfo,
1329 void **pentry0,
1330 unsigned int total_size,
1331 unsigned int number,
1332 unsigned int *hook_entries,
1333 unsigned int *underflows)
1334{
1335 unsigned int i, j;
1336 struct xt_table_info *newinfo, *info;
1337 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001338 struct compat_arpt_entry *iter0;
1339 struct arpt_entry *iter1;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001340 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001341 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001342
1343 info = *pinfo;
1344 entry0 = *pentry0;
1345 size = total_size;
1346 info->number = number;
1347
1348 /* Init all hooks to impossible value. */
1349 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1350 info->hook_entry[i] = 0xFFFFFFFF;
1351 info->underflow[i] = 0xFFFFFFFF;
1352 }
1353
1354 duprintf("translate_compat_table: size %u\n", info->size);
1355 j = 0;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001356 xt_compat_lock(NFPROTO_ARP);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001357 xt_compat_init_offsets(NFPROTO_ARP, number);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001358 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001359 xt_entry_foreach(iter0, entry0, total_size) {
1360 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001361 entry0,
1362 entry0 + total_size,
1363 hook_entries,
1364 underflows,
1365 name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001366 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001367 goto out_unlock;
1368 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001369 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001370
1371 ret = -EINVAL;
1372 if (j != number) {
1373 duprintf("translate_compat_table: %u not %u entries\n",
1374 j, number);
1375 goto out_unlock;
1376 }
1377
1378 /* Check hooks all assigned */
1379 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1380 /* Only hooks which are valid */
1381 if (!(valid_hooks & (1 << i)))
1382 continue;
1383 if (info->hook_entry[i] == 0xFFFFFFFF) {
1384 duprintf("Invalid hook entry %u %u\n",
1385 i, hook_entries[i]);
1386 goto out_unlock;
1387 }
1388 if (info->underflow[i] == 0xFFFFFFFF) {
1389 duprintf("Invalid underflow %u %u\n",
1390 i, underflows[i]);
1391 goto out_unlock;
1392 }
1393 }
1394
1395 ret = -ENOMEM;
1396 newinfo = xt_alloc_table_info(size);
1397 if (!newinfo)
1398 goto out_unlock;
1399
1400 newinfo->number = number;
1401 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1402 newinfo->hook_entry[i] = info->hook_entry[i];
1403 newinfo->underflow[i] = info->underflow[i];
1404 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001405 entry1 = newinfo->entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001406 pos = entry1;
1407 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001408 xt_entry_foreach(iter0, entry0, total_size) {
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001409 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1410 name, newinfo, entry1);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001411 if (ret != 0)
1412 break;
1413 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001414 xt_compat_flush_offsets(NFPROTO_ARP);
1415 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001416 if (ret)
1417 goto free_newinfo;
1418
1419 ret = -ELOOP;
1420 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1421 goto free_newinfo;
1422
1423 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001424 xt_entry_foreach(iter1, entry1, newinfo->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001425 iter1->counters.pcnt = xt_percpu_counter_alloc();
1426 if (IS_ERR_VALUE(iter1->counters.pcnt)) {
1427 ret = -ENOMEM;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001428 break;
Florian Westphal71ae0df2015-06-11 01:34:54 +02001429 }
1430
1431 ret = check_target(iter1, name);
1432 if (ret != 0) {
1433 xt_percpu_counter_free(iter1->counters.pcnt);
1434 break;
1435 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001436 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +02001437 if (strcmp(arpt_get_target(iter1)->u.user.name,
1438 XT_ERROR_TARGET) == 0)
1439 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001440 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001441 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001442 /*
1443 * The first i matches need cleanup_entry (calls ->destroy)
1444 * because they had called ->check already. The other j-i
1445 * entries need only release.
1446 */
1447 int skip = i;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001448 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001449 xt_entry_foreach(iter0, entry0, newinfo->size) {
1450 if (skip-- > 0)
1451 continue;
Jan Engelhardt05595182010-02-24 18:33:43 +01001452 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001453 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001454 compat_release_entry(iter0);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001455 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001456 xt_entry_foreach(iter1, entry1, newinfo->size) {
1457 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001458 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001459 cleanup_entry(iter1);
1460 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001461 xt_free_table_info(newinfo);
1462 return ret;
1463 }
1464
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001465 *pinfo = newinfo;
1466 *pentry0 = entry1;
1467 xt_free_table_info(info);
1468 return 0;
1469
1470free_newinfo:
1471 xt_free_table_info(newinfo);
1472out:
Jan Engelhardt05595182010-02-24 18:33:43 +01001473 xt_entry_foreach(iter0, entry0, total_size) {
1474 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001475 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001476 compat_release_entry(iter0);
1477 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001478 return ret;
1479out_unlock:
Jan Engelhardtee999d82008-10-08 11:35:01 +02001480 xt_compat_flush_offsets(NFPROTO_ARP);
1481 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001482 goto out;
1483}
1484
1485struct compat_arpt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001486 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001487 u32 valid_hooks;
1488 u32 num_entries;
1489 u32 size;
1490 u32 hook_entry[NF_ARP_NUMHOOKS];
1491 u32 underflow[NF_ARP_NUMHOOKS];
1492 u32 num_counters;
1493 compat_uptr_t counters;
1494 struct compat_arpt_entry entries[0];
1495};
1496
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001497static int compat_do_replace(struct net *net, void __user *user,
1498 unsigned int len)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001499{
1500 int ret;
1501 struct compat_arpt_replace tmp;
1502 struct xt_table_info *newinfo;
1503 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001504 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001505
1506 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1507 return -EFAULT;
1508
1509 /* overflow check */
1510 if (tmp.size >= INT_MAX / num_possible_cpus())
1511 return -ENOMEM;
1512 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1513 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001514 if (tmp.num_counters == 0)
1515 return -EINVAL;
1516
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001517 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001518
1519 newinfo = xt_alloc_table_info(tmp.size);
1520 if (!newinfo)
1521 return -ENOMEM;
1522
Florian Westphal482cfc32015-06-11 01:34:55 +02001523 loc_cpu_entry = newinfo->entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001524 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1525 ret = -EFAULT;
1526 goto free_newinfo;
1527 }
1528
1529 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1530 &newinfo, &loc_cpu_entry, tmp.size,
1531 tmp.num_entries, tmp.hook_entry,
1532 tmp.underflow);
1533 if (ret != 0)
1534 goto free_newinfo;
1535
1536 duprintf("compat_do_replace: Translated table\n");
1537
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001538 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001539 tmp.num_counters, compat_ptr(tmp.counters));
1540 if (ret)
1541 goto free_newinfo_untrans;
1542 return 0;
1543
1544 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001545 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001546 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001547 free_newinfo:
1548 xt_free_table_info(newinfo);
1549 return ret;
1550}
1551
1552static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1553 unsigned int len)
1554{
1555 int ret;
1556
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001557 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001558 return -EPERM;
1559
1560 switch (cmd) {
1561 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001562 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001563 break;
1564
1565 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001566 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001567 break;
1568
1569 default:
1570 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1571 ret = -EINVAL;
1572 }
1573
1574 return ret;
1575}
1576
1577static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1578 compat_uint_t *size,
1579 struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001580 unsigned int i)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001581{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001582 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001583 struct compat_arpt_entry __user *ce;
1584 u_int16_t target_offset, next_offset;
1585 compat_uint_t origsize;
1586 int ret;
1587
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001588 origsize = *size;
1589 ce = (struct compat_arpt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001590 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1591 copy_to_user(&ce->counters, &counters[i],
1592 sizeof(counters[i])) != 0)
1593 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001594
1595 *dstptr += sizeof(struct compat_arpt_entry);
1596 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1597
1598 target_offset = e->target_offset - (origsize - *size);
1599
1600 t = arpt_get_target(e);
1601 ret = xt_compat_target_to_user(t, dstptr, size);
1602 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001603 return ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001604 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001605 if (put_user(target_offset, &ce->target_offset) != 0 ||
1606 put_user(next_offset, &ce->next_offset) != 0)
1607 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001608 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001609}
1610
1611static int compat_copy_entries_to_user(unsigned int total_size,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001612 struct xt_table *table,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001613 void __user *userptr)
1614{
1615 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001616 const struct xt_table_info *private = table->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001617 void __user *pos;
1618 unsigned int size;
1619 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001620 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001621 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001622
1623 counters = alloc_counters(table);
1624 if (IS_ERR(counters))
1625 return PTR_ERR(counters);
1626
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001627 pos = userptr;
1628 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001629 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001630 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001631 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001632 if (ret != 0)
1633 break;
1634 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001635 vfree(counters);
1636 return ret;
1637}
1638
1639struct compat_arpt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001640 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001641 compat_uint_t size;
1642 struct compat_arpt_entry entrytable[0];
1643};
1644
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001645static int compat_get_entries(struct net *net,
1646 struct compat_arpt_get_entries __user *uptr,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001647 int *len)
1648{
1649 int ret;
1650 struct compat_arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001651 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001652
1653 if (*len < sizeof(get)) {
1654 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1655 return -EINVAL;
1656 }
1657 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1658 return -EFAULT;
1659 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1660 duprintf("compat_get_entries: %u != %zu\n",
1661 *len, sizeof(get) + get.size);
1662 return -EINVAL;
1663 }
1664
Jan Engelhardtee999d82008-10-08 11:35:01 +02001665 xt_compat_lock(NFPROTO_ARP);
1666 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001667 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001668 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001669 struct xt_table_info info;
1670
1671 duprintf("t->private->number = %u\n", private->number);
1672 ret = compat_table_info(private, &info);
1673 if (!ret && get.size == info.size) {
1674 ret = compat_copy_entries_to_user(private->size,
1675 t, uptr->entrytable);
1676 } else if (!ret) {
1677 duprintf("compat_get_entries: I've got %u not %u!\n",
1678 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001679 ret = -EAGAIN;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001680 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001681 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001682 module_put(t->me);
1683 xt_table_unlock(t);
1684 } else
1685 ret = t ? PTR_ERR(t) : -ENOENT;
1686
Jan Engelhardtee999d82008-10-08 11:35:01 +02001687 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001688 return ret;
1689}
1690
1691static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1692
1693static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1694 int *len)
1695{
1696 int ret;
1697
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001698 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001699 return -EPERM;
1700
1701 switch (cmd) {
1702 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001703 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001704 break;
1705 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001706 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001707 break;
1708 default:
1709 ret = do_arpt_get_ctl(sk, cmd, user, len);
1710 }
1711 return ret;
1712}
1713#endif
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1716{
1717 int ret;
1718
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001719 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -EPERM;
1721
1722 switch (cmd) {
1723 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001724 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 break;
1726
1727 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001728 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730
1731 default:
1732 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1733 ret = -EINVAL;
1734 }
1735
1736 return ret;
1737}
1738
1739static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1740{
1741 int ret;
1742
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001743 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return -EPERM;
1745
1746 switch (cmd) {
Patrick McHardy41acd972007-12-17 22:26:24 -08001747 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001748 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy41acd972007-12-17 22:26:24 -08001749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Patrick McHardy11f6dff2007-12-17 22:26:38 -08001751 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001752 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Harald Welte6b7d31f2005-10-26 09:34:24 +02001755 case ARPT_SO_GET_REVISION_TARGET: {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001756 struct xt_get_revision rev;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001757
1758 if (*len != sizeof(rev)) {
1759 ret = -EINVAL;
1760 break;
1761 }
1762 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1763 ret = -EFAULT;
1764 break;
1765 }
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001766 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001767
Jan Engelhardtee999d82008-10-08 11:35:01 +02001768 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001769 rev.revision, 1, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001770 "arpt_%s", rev.name);
1771 break;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 default:
1775 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1776 ret = -EINVAL;
1777 }
1778
1779 return ret;
1780}
1781
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001782struct xt_table *arpt_register_table(struct net *net,
1783 const struct xt_table *table,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001784 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001787 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001788 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001789 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001790 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Harald Welte2e4e6a12006-01-12 13:30:04 -08001792 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (!newinfo) {
1794 ret = -ENOMEM;
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001795 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Eric Dumazet31836062005-12-13 23:13:48 -08001797
Florian Westphal482cfc32015-06-11 01:34:55 +02001798 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001799 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jan Engelhardt0f234212010-02-24 18:36:04 +01001801 ret = translate_table(newinfo, loc_cpu_entry, repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 duprintf("arpt_register_table: translate table gives %d\n", ret);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001803 if (ret != 0)
1804 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001806 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001807 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001808 ret = PTR_ERR(new_table);
1809 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001811 return new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001813out_free:
1814 xt_free_table_info(newinfo);
1815out:
1816 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Jan Engelhardt4abff072008-04-14 11:15:43 +02001819void arpt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001821 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001822 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08001823 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001824 struct arpt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08001825
Harald Welte2e4e6a12006-01-12 13:30:04 -08001826 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /* Decrease module usage counts and free resources */
Florian Westphal482cfc32015-06-11 01:34:55 +02001829 loc_cpu_entry = private->entries;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001830 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001831 cleanup_entry(iter);
Alexey Dobriyandf200962008-01-31 04:05:34 -08001832 if (private->number > private->initial_entries)
1833 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001834 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001838static struct xt_target arpt_builtin_tg[] __read_mostly = {
1839 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001840 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001841 .targetsize = sizeof(int),
1842 .family = NFPROTO_ARP,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001843#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001844 .compatsize = sizeof(compat_int_t),
1845 .compat_from_user = compat_standard_from_user,
1846 .compat_to_user = compat_standard_to_user,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001847#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001848 },
1849 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001850 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001851 .target = arpt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001852 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001853 .family = NFPROTO_ARP,
1854 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855};
1856
1857static struct nf_sockopt_ops arpt_sockopts = {
1858 .pf = PF_INET,
1859 .set_optmin = ARPT_BASE_CTL,
1860 .set_optmax = ARPT_SO_SET_MAX+1,
1861 .set = do_arpt_set_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001862#ifdef CONFIG_COMPAT
1863 .compat_set = compat_do_arpt_set_ctl,
1864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .get_optmin = ARPT_BASE_CTL,
1866 .get_optmax = ARPT_SO_GET_MAX+1,
1867 .get = do_arpt_get_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001868#ifdef CONFIG_COMPAT
1869 .compat_get = compat_do_arpt_get_ctl,
1870#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001871 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872};
1873
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001874static int __net_init arp_tables_net_init(struct net *net)
1875{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001876 return xt_proto_init(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001877}
1878
1879static void __net_exit arp_tables_net_exit(struct net *net)
1880{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001881 xt_proto_fini(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001882}
1883
1884static struct pernet_operations arp_tables_net_ops = {
1885 .init = arp_tables_net_init,
1886 .exit = arp_tables_net_exit,
1887};
1888
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001889static int __init arp_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 int ret;
1892
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001893 ret = register_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001894 if (ret < 0)
1895 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001896
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001897 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001898 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001899 if (ret < 0)
1900 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 /* Register setsockopt */
1903 ret = nf_register_sockopt(&arpt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001904 if (ret < 0)
1905 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Patrick McHardya887c1c2007-07-14 20:46:15 -07001907 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001909
1910err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001911 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001912err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001913 unregister_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001914err1:
1915 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001918static void __exit arp_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 nf_unregister_sockopt(&arpt_sockopts);
Jan Engelhardt45385062009-07-04 12:50:00 +02001921 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001922 unregister_pernet_subsys(&arp_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925EXPORT_SYMBOL(arpt_register_table);
1926EXPORT_SYMBOL(arpt_unregister_table);
1927EXPORT_SYMBOL(arpt_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001929module_init(arp_tables_init);
1930module_exit(arp_tables_fini);