blob: a61200754f4ba29301855ae67055dd11d004780b [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
Jan Engelhardt98e86402009-04-15 21:06:05 +0200243static inline __pure
244struct 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct arpt_entry *e, *back;
258 const char *indev, *outdev;
259 void *table_base;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200260 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200261 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200262 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800264 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return NF_DROP;
266
David S. Millerb85c3dc2015-04-03 21:18:46 -0400267 indev = state->in ? state->in->name : nulldevname;
268 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200270 local_bh_disable();
271 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700272 private = table->private;
Will Deaconb416c142013-10-21 13:14:53 +0100273 /*
274 * Ensure we load private-> members after we've fetched the base
275 * pointer.
276 */
277 smp_read_barrier_depends();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700278 table_base = private->entries[smp_processor_id()];
Stephen Hemminger78454472009-02-20 10:35:32 +0100279
Harald Welte2e4e6a12006-01-12 13:30:04 -0800280 e = get_entry(table_base, private->hook_entry[hook]);
281 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David S. Millerb85c3dc2015-04-03 21:18:46 -0400283 acpar.in = state->in;
284 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200285 acpar.hooknum = hook;
286 acpar.family = NFPROTO_ARP;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200287 acpar.hotdrop = false;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200288
Herbert Xu3db05fe2007-10-15 00:53:15 -0700289 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200291 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200293 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
Jan Engelhardt98e86402009-04-15 21:06:05 +0200294 e = arpt_next_entry(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200295 continue;
296 }
297
Changli Gaof6670092010-07-23 13:40:53 +0200298 ADD_COUNTER(e->counters, arp_hdr_len(skb->dev), 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200299
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200300 t = arpt_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200301
302 /* Standard target? */
303 if (!t->u.kernel.target->target) {
304 int v;
305
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200306 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200307 if (v < 0) {
308 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200309 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000310 verdict = (unsigned int)(-v) - 1;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200311 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 Engelhardt7a6b1c42009-04-15 21:35:33 +0200328 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200330
331 /* Targets which reenter must return
332 * abs. verdicts
333 */
Jan Engelhardtde74c162009-07-05 18:26:37 +0200334 acpar.target = t->u.kernel.target;
335 acpar.targinfo = t->data;
336 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200337
338 /* Target might have changed stuff. */
339 arp = arp_hdr(skb);
340
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200341 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200342 e = arpt_next_entry(e);
343 else
344 /* Verdict */
345 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200346 } while (!acpar.hotdrop);
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200347 xt_write_recseq_end(addend);
348 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200350 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return NF_DROP;
352 else
353 return verdict;
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* All zeroes == unconditional rule. */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200357static inline bool unconditional(const struct arpt_arp *arp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200359 static const struct arpt_arp uncond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200361 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
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 Engelhardtd5d1baa2009-06-26 07:51:59 +0200367static int mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800368 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
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 Dumazet31836062005-12-13 23:13:48 -0800378 = (struct arpt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!(valid_hooks & (1 << hook)))
381 continue;
382
383 /* Set initial back pointer. */
384 e->counters.pcnt = pos;
385
386 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200387 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200388 = (void *)arpt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800389 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200392 pr_notice("arptables: loop hook %u pos %u %08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 hook, pos, e->comefrom);
394 return 0;
395 }
396 e->comefrom
397 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
398
399 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100400 if ((e->target_offset == sizeof(struct arpt_entry) &&
401 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200402 XT_STANDARD_TARGET) == 0) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100403 t->verdict < 0 && unconditional(&e->arp)) ||
404 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned int oldpos, size;
406
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100407 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200408 XT_STANDARD_TARGET) == 0) &&
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100409 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800410 duprintf("mark_source_chains: bad "
411 "negative verdict (%i)\n",
412 t->verdict);
413 return 0;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* 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 Dumazet31836062005-12-13 23:13:48 -0800430 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } while (oldpos == pos + e->next_offset);
432
433 /* Move along one */
434 size = e->next_offset;
435 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800436 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 e->counters.pcnt = pos;
438 pos += size;
439 } else {
440 int newpos = t->verdict;
441
442 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200443 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100444 newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800445 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 Torvalds1da177e2005-04-16 15:20:36 -0700453 /* 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 Dumazet31836062005-12-13 23:13:48 -0800461 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 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 Engelhardtd5d1baa2009-06-26 07:51:59 +0200472static inline int check_entry(const struct arpt_entry *e, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200474 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (!arp_checkentry(&e->arp)) {
477 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
478 return -EINVAL;
479 }
480
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200481 if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800482 return -EINVAL;
483
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200484 t = arpt_get_target_c(e);
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800485 if (e->target_offset + t->u.target_size > e->next_offset)
486 return -EINVAL;
487
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800488 return 0;
489}
490
491static inline int check_target(struct arpt_entry *e, const char *name)
492{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200493 struct xt_entry_target *t = arpt_get_target(e);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800494 int ret;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200495 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 Engelhardt916a9172008-10-08 11:35:20 +0200501 .family = NFPROTO_ARP,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200502 };
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800503
Jan Engelhardt916a9172008-10-08 11:35:20 +0200504 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200505 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 duprintf("arp_tables: check failed for `%s'.\n",
507 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200510 return 0;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800511}
512
513static inline int
Jan Engelhardt05595182010-02-24 18:33:43 +0100514find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800515{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200516 struct xt_entry_target *t;
Jan Engelhardt95eea852008-04-14 11:15:43 +0200517 struct xt_target *target;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800518 int ret;
519
520 ret = check_entry(e, name);
521 if (ret)
522 return ret;
523
524 t = arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200525 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
526 t->u.user.revision);
527 if (IS_ERR(target)) {
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800528 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200529 ret = PTR_ERR(target);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800530 goto out;
531 }
532 t->u.kernel.target = target;
533
534 ret = check_target(e, name);
535 if (ret)
536 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800538err:
539 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540out:
541 return ret;
542}
543
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200544static bool check_underflow(const struct arpt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200545{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200546 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200547 unsigned int verdict;
548
549 if (!unconditional(&e->arp))
550 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200551 t = arpt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200552 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
553 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200554 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200555 verdict = -verdict - 1;
556 return verdict == NF_DROP || verdict == NF_ACCEPT;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static inline int check_entry_size_and_hooks(struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800560 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200561 const unsigned char *base,
562 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 const unsigned int *hook_entries,
564 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100565 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 unsigned int h;
568
Joe Perches3666ed12009-11-23 23:17:06 +0100569 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
570 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 duprintf("Bad offset %p\n", e);
572 return -EINVAL;
573 }
574
575 if (e->next_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200576 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 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 Engelhardta7d51732009-07-18 14:52:58 +0200584 if (!(valid_hooks & (1 << h)))
585 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if ((unsigned char *)e - base == hook_entries[h])
587 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200588 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200589 if (!check_underflow(e)) {
590 pr_err("Underflows must be unconditional and "
591 "use the STANDARD target with "
592 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200593 return -EINVAL;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800600 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603}
604
Jan Engelhardt05595182010-02-24 18:33:43 +0100605static inline void cleanup_entry(struct arpt_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200607 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200608 struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 t = arpt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200611 par.target = t->u.kernel.target;
612 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200613 par.family = NFPROTO_ARP;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200614 if (par.target->destroy != NULL)
615 par.target->destroy(&par);
616 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/* Checks and translates the user-supplied table segment (held in
620 * newinfo).
621 */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100622static int translate_table(struct xt_table_info *newinfo, void *entry0,
623 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100625 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100627 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jan Engelhardt0f234212010-02-24 18:36:04 +0100629 newinfo->size = repl->size;
630 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
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 Engelhardt72b2b1d2010-02-24 18:32:59 +0100642 xt_entry_foreach(iter, entry0, newinfo->size) {
643 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100644 entry0 + repl->size,
645 repl->hook_entry,
646 repl->underflow,
647 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100648 if (ret != 0)
649 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100650 ++i;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200651 if (strcmp(arpt_get_target(iter)->u.user.name,
652 XT_ERROR_TARGET) == 0)
653 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
656 if (ret != 0)
657 return ret;
658
Jan Engelhardt0f234212010-02-24 18:36:04 +0100659 if (i != repl->num_entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 duprintf("translate_table: %u not %u entries\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100661 i, repl->num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 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 Engelhardt0f234212010-02-24 18:36:04 +0100668 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 continue;
670 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
671 duprintf("Invalid hook entry %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100672 i, repl->hook_entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
674 }
675 if (newinfo->underflow[i] == 0xFFFFFFFF) {
676 duprintf("Invalid underflow %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100677 i, repl->underflow[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
679 }
680 }
681
Jan Engelhardt0f234212010-02-24 18:36:04 +0100682 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800683 duprintf("Looping hook\n");
684 return -ELOOP;
685 }
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Finally, each sanity check must pass */
688 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100689 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100690 ret = find_check_entry(iter, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100691 if (ret != 0)
692 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100693 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800696 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100697 xt_entry_foreach(iter, entry0, newinfo->size) {
698 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100699 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100700 cleanup_entry(iter);
701 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700706 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800707 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
708 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 return ret;
712}
713
Harald Welte2e4e6a12006-01-12 13:30:04 -0800714static void get_counters(const struct xt_table_info *t,
715 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100717 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned int cpu;
719 unsigned int i;
720
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700721 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200722 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 i = 0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100725 xt_entry_foreach(iter, t->entries[cpu], t->size) {
Eric Dumazet83723d62011-01-10 20:11:38 +0100726 u64 bcnt, pcnt;
727 unsigned int start;
728
729 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200730 start = read_seqcount_begin(s);
Eric Dumazet83723d62011-01-10 20:11:38 +0100731 bcnt = iter->counters.bcnt;
732 pcnt = iter->counters.pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200733 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100734
735 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100736 ++i;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100739}
740
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200741static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Patrick McHardy27e2c262007-12-17 21:56:48 -0800743 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800744 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200745 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* We need atomic snapshot of counters: rest doesn't change
748 * (other than comefrom, which userspace doesn't care
749 * about).
750 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800751 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100752 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700755 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700757 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Patrick McHardy27e2c262007-12-17 21:56:48 -0800759 return counters;
760}
761
762static int copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200763 const struct xt_table *table,
Patrick McHardy27e2c262007-12-17 21:56:48 -0800764 void __user *userptr)
765{
766 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200767 const struct arpt_entry *e;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800768 struct xt_counters *counters;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200769 struct xt_table_info *private = table->private;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800770 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 Welte2e4e6a12006-01-12 13:30:04 -0800777 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800778 /* ... then copy entire thing ... */
779 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 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 Engelhardt87a2e70d2010-10-13 16:11:22 +0200787 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Eric Dumazet31836062005-12-13 23:13:48 -0800789 e = (struct arpt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 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 Engelhardtd5d1baa2009-06-26 07:51:59 +0200798 t = arpt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200800 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 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 McHardyd6a2ba02007-12-17 22:26:54 -0800814#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200815static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800816{
817 int v = *(compat_int_t *)src;
818
819 if (v > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200820 v += xt_compat_calc_jump(NFPROTO_ARP, v);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800821 memcpy(dst, &v, sizeof(v));
822}
823
Jan Engelhardt739674f2009-06-26 08:23:19 +0200824static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800825{
826 compat_int_t cv = *(int *)src;
827
828 if (cv > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200829 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800830 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
831}
832
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200833static int compat_calc_entry(const struct arpt_entry *e,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800834 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200835 const void *base, struct xt_table_info *newinfo)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800836{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200837 const struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800838 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 Engelhardtd5d1baa2009-06-26 07:51:59 +0200844 t = arpt_get_target_c(e);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800845 off += xt_compat_target_offset(t->u.kernel.target);
846 newinfo->size -= off;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200847 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800848 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
862static int compat_table_info(const struct xt_table_info *info,
863 struct xt_table_info *newinfo)
864{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100865 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800866 void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100867 int ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800868
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 Dumazet255d0dc2010-12-18 18:35:15 +0100876 xt_compat_init_offsets(NFPROTO_ARP, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100877 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 Engelhardt05595182010-02-24 18:33:43 +0100880 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100881 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100882 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800883}
884#endif
885
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200886static int get_info(struct net *net, void __user *user,
887 const int *len, int compat)
Patrick McHardy41acd972007-12-17 22:26:24 -0800888{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200889 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardt4abff072008-04-14 11:15:43 +0200890 struct xt_table *t;
Patrick McHardy41acd972007-12-17 22:26:24 -0800891 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 Engelhardt12b00c22010-10-13 15:56:56 +0200902 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800903#ifdef CONFIG_COMPAT
904 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200905 xt_compat_lock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800906#endif
Jan Engelhardtee999d82008-10-08 11:35:01 +0200907 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardy41acd972007-12-17 22:26:24 -0800908 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000909 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy41acd972007-12-17 22:26:24 -0800910 struct arpt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200911 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800912#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800913 struct xt_table_info tmp;
914
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800915 if (compat) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800916 ret = compat_table_info(private, &tmp);
Jan Engelhardtee999d82008-10-08 11:35:01 +0200917 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800918 private = &tmp;
919 }
920#endif
Vasiliy Kulikov1a8b7a62010-11-03 08:44:12 +0100921 memset(&info, 0, sizeof(info));
Patrick McHardy41acd972007-12-17 22:26:24 -0800922 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 McHardyd6a2ba02007-12-17 22:26:54 -0800939#ifdef CONFIG_COMPAT
940 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200941 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800942#endif
Patrick McHardy41acd972007-12-17 22:26:24 -0800943 return ret;
944}
945
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800946static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200947 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int ret;
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800950 struct arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200951 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800953 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 Engelhardtee999d82008-10-08 11:35:01 +0200965 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000966 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +0200967 const struct xt_table_info *private = t->private;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 duprintf("t->private->number = %u\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800970 private->number);
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800971 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800972 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 t, uptr->entrytable);
974 else {
975 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800976 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +0200977 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Harald Welte6b7d31f2005-10-26 09:34:24 +0200979 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800980 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200982 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 return ret;
985}
986
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800987static int __do_replace(struct net *net, const char *name,
988 unsigned int valid_hooks,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800989 struct xt_table_info *newinfo,
990 unsigned int num_counters,
991 void __user *counters_ptr)
992{
993 int ret;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200994 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800995 struct xt_table_info *oldinfo;
996 struct xt_counters *counters;
997 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100998 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800999
1000 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +01001001 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001002 if (!counters) {
1003 ret = -ENOMEM;
1004 goto out;
1005 }
1006
Jan Engelhardtee999d82008-10-08 11:35:01 +02001007 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001008 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001009 if (IS_ERR_OR_NULL(t)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001010 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 Hemminger942e4a22009-04-28 22:36:33 -07001036 /* Get the old counters, and synchronize with replace */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001037 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001038
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001039 /* Decrease module usage counts and free resource */
1040 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001041 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001042 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001043
1044 xt_free_table_info(oldinfo);
1045 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +02001046 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 McHardyd6a2ba02007-12-17 22:26:54 -08001050 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 Engelhardtd5d1baa2009-06-26 07:51:59 +02001063static int do_replace(struct net *net, const void __user *user,
1064 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 int ret;
1067 struct arpt_replace tmp;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001068 struct xt_table_info *newinfo;
1069 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001070 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1073 return -EFAULT;
1074
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001075 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001076 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1077 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001078 if (tmp.num_counters == 0)
1079 return -EINVAL;
1080
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001081 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001082
Harald Welte2e4e6a12006-01-12 13:30:04 -08001083 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!newinfo)
1085 return -ENOMEM;
1086
Eric Dumazet31836062005-12-13 23:13:48 -08001087 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001090 tmp.size) != 0) {
1091 ret = -EFAULT;
1092 goto free_newinfo;
1093 }
1094
Jan Engelhardt0f234212010-02-24 18:36:04 +01001095 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (ret != 0)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001097 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 duprintf("arp_tables: Translated table\n");
1100
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001101 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001102 tmp.num_counters, tmp.counters);
1103 if (ret)
1104 goto free_newinfo_untrans;
1105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001107 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001108 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001109 cleanup_entry(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001111 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return ret;
1113}
1114
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001115static int do_add_counters(struct net *net, const void __user *user,
1116 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001118 unsigned int i, curcpu;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001119 struct xt_counters_info tmp;
1120 struct xt_counters *paddc;
1121 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001122 const char *name;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001123 int size;
1124 void *ptmp;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001125 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001126 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001127 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001128 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001129 struct arpt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001130 unsigned int addend;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001131#ifdef CONFIG_COMPAT
1132 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001134 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 Torvalds1da177e2005-04-16 15:20:36 -07001145 return -EFAULT;
1146
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001147#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 Torvalds1da177e2005-04-16 15:20:36 -07001159 return -EINVAL;
1160
Eric Dumazete12f8e22010-06-04 13:31:29 +02001161 paddc = vmalloc(len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!paddc)
1163 return -ENOMEM;
1164
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001165 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ret = -EFAULT;
1167 goto free;
1168 }
1169
Jan Engelhardtee999d82008-10-08 11:35:01 +02001170 t = xt_find_table_lock(net, NFPROTO_ARP, name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001171 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001172 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001176 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001177 private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001178 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 ret = -EINVAL;
1180 goto unlock_up_free;
1181 }
1182
1183 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001184 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001185 curcpu = smp_processor_id();
1186 loc_cpu_entry = private->entries[curcpu];
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001187 addend = xt_write_recseq_begin();
Jan Engelhardt05595182010-02-24 18:33:43 +01001188 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1189 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1190 ++i;
1191 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001192 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001194 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001195 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001196 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 free:
1198 vfree(paddc);
1199
1200 return ret;
1201}
1202
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001203#ifdef CONFIG_COMPAT
Jan Engelhardt05595182010-02-24 18:33:43 +01001204static inline void compat_release_entry(struct compat_arpt_entry *e)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001205{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001206 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001207
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001208 t = compat_arpt_get_target(e);
1209 module_put(t->u.kernel.target->me);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001210}
1211
1212static inline int
1213check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1214 struct xt_table_info *newinfo,
1215 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001216 const unsigned char *base,
1217 const unsigned char *limit,
1218 const unsigned int *hook_entries,
1219 const unsigned int *underflows,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001220 const char *name)
1221{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001222 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001223 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 Perches3666ed12009-11-23 23:17:06 +01001228 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1229 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001230 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 Engelhardtd2a7b6b2009-07-10 18:55:11 +02001250 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1251 t->u.user.revision);
1252 if (IS_ERR(target)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001253 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1254 t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001255 ret = PTR_ERR(target);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001256 goto out;
1257 }
1258 t->u.kernel.target = target;
1259
1260 off += xt_compat_target_offset(target);
1261 *size += off;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001262 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001263 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 McHardyd6a2ba02007-12-17 22:26:54 -08001277 return 0;
1278
1279release_target:
1280 module_put(t->u.kernel.target->me);
1281out:
1282 return ret;
1283}
1284
1285static int
1286compat_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 Engelhardt87a2e70d2010-10-13 16:11:22 +02001290 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001291 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 McHardyd6a2ba02007-12-17 22:26:54 -08001320static 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 Engelhardt72b2b1d2010-02-24 18:32:59 +01001332 struct compat_arpt_entry *iter0;
1333 struct arpt_entry *iter1;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001334 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001335 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001336
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 Engelhardtee999d82008-10-08 11:35:01 +02001350 xt_compat_lock(NFPROTO_ARP);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001351 xt_compat_init_offsets(NFPROTO_ARP, number);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001352 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001353 xt_entry_foreach(iter0, entry0, total_size) {
1354 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001355 entry0,
1356 entry0 + total_size,
1357 hook_entries,
1358 underflows,
1359 name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001360 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001361 goto out_unlock;
1362 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001363 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001364
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 Engelhardt72b2b1d2010-02-24 18:32:59 +01001402 xt_entry_foreach(iter0, entry0, total_size) {
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001403 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1404 name, newinfo, entry1);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001405 if (ret != 0)
1406 break;
1407 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001408 xt_compat_flush_offsets(NFPROTO_ARP);
1409 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001410 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 Engelhardt72b2b1d2010-02-24 18:32:59 +01001418 xt_entry_foreach(iter1, entry1, newinfo->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001419 ret = check_target(iter1, name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001420 if (ret != 0)
1421 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001422 ++i;
Florian Westphalcca77b72010-08-23 14:41:22 -07001423 if (strcmp(arpt_get_target(iter1)->u.user.name,
1424 XT_ERROR_TARGET) == 0)
1425 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001426 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001427 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001428 /*
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 McHardyd6a2ba02007-12-17 22:26:54 -08001434 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001435 xt_entry_foreach(iter0, entry0, newinfo->size) {
1436 if (skip-- > 0)
1437 continue;
Jan Engelhardt05595182010-02-24 18:33:43 +01001438 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001439 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001440 compat_release_entry(iter0);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001441 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001442 xt_entry_foreach(iter1, entry1, newinfo->size) {
1443 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001444 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001445 cleanup_entry(iter1);
1446 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001447 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
1461free_newinfo:
1462 xt_free_table_info(newinfo);
1463out:
Jan Engelhardt05595182010-02-24 18:33:43 +01001464 xt_entry_foreach(iter0, entry0, total_size) {
1465 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001466 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001467 compat_release_entry(iter0);
1468 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001469 return ret;
1470out_unlock:
Jan Engelhardtee999d82008-10-08 11:35:01 +02001471 xt_compat_flush_offsets(NFPROTO_ARP);
1472 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001473 goto out;
1474}
1475
1476struct compat_arpt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001477 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001478 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 Dobriyan79df3412008-01-31 04:04:32 -08001488static int compat_do_replace(struct net *net, void __user *user,
1489 unsigned int len)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001490{
1491 int ret;
1492 struct compat_arpt_replace tmp;
1493 struct xt_table_info *newinfo;
1494 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001495 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001496
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 Jones1086bbe2015-05-19 20:55:17 -04001505 if (tmp.num_counters == 0)
1506 return -EINVAL;
1507
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001508 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001509
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 Dobriyan79df3412008-01-31 04:04:32 -08001530 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001531 tmp.num_counters, compat_ptr(tmp.counters));
1532 if (ret)
1533 goto free_newinfo_untrans;
1534 return 0;
1535
1536 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001537 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001538 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001539 free_newinfo:
1540 xt_free_table_info(newinfo);
1541 return ret;
1542}
1543
1544static 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. Biederman52e804c2012-11-16 03:03:05 +00001549 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001550 return -EPERM;
1551
1552 switch (cmd) {
1553 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001554 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001555 break;
1556
1557 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001558 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001559 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
1569static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1570 compat_uint_t *size,
1571 struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001572 unsigned int i)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001573{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001574 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001575 struct compat_arpt_entry __user *ce;
1576 u_int16_t target_offset, next_offset;
1577 compat_uint_t origsize;
1578 int ret;
1579
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001580 origsize = *size;
1581 ce = (struct compat_arpt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001582 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 McHardyd6a2ba02007-12-17 22:26:54 -08001586
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 Engelhardt05595182010-02-24 18:33:43 +01001595 return ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001596 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001597 if (put_user(target_offset, &ce->target_offset) != 0 ||
1598 put_user(next_offset, &ce->next_offset) != 0)
1599 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001600 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001601}
1602
1603static int compat_copy_entries_to_user(unsigned int total_size,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001604 struct xt_table *table,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001605 void __user *userptr)
1606{
1607 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001608 const struct xt_table_info *private = table->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001609 void __user *pos;
1610 unsigned int size;
1611 int ret = 0;
1612 void *loc_cpu_entry;
1613 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001614 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001615
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 Engelhardt72b2b1d2010-02-24 18:32:59 +01001624 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1625 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001626 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001627 if (ret != 0)
1628 break;
1629 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001630 vfree(counters);
1631 return ret;
1632}
1633
1634struct compat_arpt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001635 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001636 compat_uint_t size;
1637 struct compat_arpt_entry entrytable[0];
1638};
1639
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001640static int compat_get_entries(struct net *net,
1641 struct compat_arpt_get_entries __user *uptr,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001642 int *len)
1643{
1644 int ret;
1645 struct compat_arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001646 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001647
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 Engelhardtee999d82008-10-08 11:35:01 +02001660 xt_compat_lock(NFPROTO_ARP);
1661 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001662 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001663 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001664 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 McHardy544473c2008-04-14 11:15:45 +02001674 ret = -EAGAIN;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001675 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001676 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001677 module_put(t->me);
1678 xt_table_unlock(t);
1679 } else
1680 ret = t ? PTR_ERR(t) : -ENOENT;
1681
Jan Engelhardtee999d82008-10-08 11:35:01 +02001682 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001683 return ret;
1684}
1685
1686static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1687
1688static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1689 int *len)
1690{
1691 int ret;
1692
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001693 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001694 return -EPERM;
1695
1696 switch (cmd) {
1697 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001698 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001699 break;
1700 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001701 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001702 break;
1703 default:
1704 ret = do_arpt_get_ctl(sk, cmd, user, len);
1705 }
1706 return ret;
1707}
1708#endif
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1711{
1712 int ret;
1713
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001714 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return -EPERM;
1716
1717 switch (cmd) {
1718 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001719 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721
1722 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001723 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 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
1734static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1735{
1736 int ret;
1737
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001738 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -EPERM;
1740
1741 switch (cmd) {
Patrick McHardy41acd972007-12-17 22:26:24 -08001742 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001743 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy41acd972007-12-17 22:26:24 -08001744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Patrick McHardy11f6dff2007-12-17 22:26:38 -08001746 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001747 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Harald Welte6b7d31f2005-10-26 09:34:24 +02001750 case ARPT_SO_GET_REVISION_TARGET: {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001751 struct xt_get_revision rev;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001752
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 Kulikov42eab942011-03-15 13:35:21 +01001761 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001762
Jan Engelhardtee999d82008-10-08 11:35:01 +02001763 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001764 rev.revision, 1, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001765 "arpt_%s", rev.name);
1766 break;
1767 }
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 default:
1770 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1771 ret = -EINVAL;
1772 }
1773
1774 return ret;
1775}
1776
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001777struct xt_table *arpt_register_table(struct net *net,
1778 const struct xt_table *table,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001779 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001782 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001783 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001784 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001785 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Harald Welte2e4e6a12006-01-12 13:30:04 -08001787 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (!newinfo) {
1789 ret = -ENOMEM;
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Eric Dumazet31836062005-12-13 23:13:48 -08001792
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 Torvalds1da177e2005-04-16 15:20:36 -07001796
Jan Engelhardt0f234212010-02-24 18:36:04 +01001797 ret = translate_table(newinfo, loc_cpu_entry, repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 duprintf("arpt_register_table: translate table gives %d\n", ret);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001799 if (ret != 0)
1800 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001802 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001803 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001804 ret = PTR_ERR(new_table);
1805 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001807 return new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001809out_free:
1810 xt_free_table_info(newinfo);
1811out:
1812 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Jan Engelhardt4abff072008-04-14 11:15:43 +02001815void arpt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001817 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001818 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08001819 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001820 struct arpt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08001821
Harald Welte2e4e6a12006-01-12 13:30:04 -08001822 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001825 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001826 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001827 cleanup_entry(iter);
Alexey Dobriyandf200962008-01-31 04:05:34 -08001828 if (private->number > private->initial_entries)
1829 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001830 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001834static struct xt_target arpt_builtin_tg[] __read_mostly = {
1835 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001836 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001837 .targetsize = sizeof(int),
1838 .family = NFPROTO_ARP,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001839#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001840 .compatsize = sizeof(compat_int_t),
1841 .compat_from_user = compat_standard_from_user,
1842 .compat_to_user = compat_standard_to_user,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001843#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001844 },
1845 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001846 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001847 .target = arpt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001848 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001849 .family = NFPROTO_ARP,
1850 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851};
1852
1853static 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 McHardyd6a2ba02007-12-17 22:26:54 -08001858#ifdef CONFIG_COMPAT
1859 .compat_set = compat_do_arpt_set_ctl,
1860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .get_optmin = ARPT_BASE_CTL,
1862 .get_optmax = ARPT_SO_GET_MAX+1,
1863 .get = do_arpt_get_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001864#ifdef CONFIG_COMPAT
1865 .compat_get = compat_do_arpt_get_ctl,
1866#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001867 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868};
1869
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001870static int __net_init arp_tables_net_init(struct net *net)
1871{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001872 return xt_proto_init(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001873}
1874
1875static void __net_exit arp_tables_net_exit(struct net *net)
1876{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001877 xt_proto_fini(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001878}
1879
1880static struct pernet_operations arp_tables_net_ops = {
1881 .init = arp_tables_net_init,
1882 .exit = arp_tables_net_exit,
1883};
1884
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001885static int __init arp_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 int ret;
1888
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001889 ret = register_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001890 if (ret < 0)
1891 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001892
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001893 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001894 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001895 if (ret < 0)
1896 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* Register setsockopt */
1899 ret = nf_register_sockopt(&arpt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001900 if (ret < 0)
1901 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Patrick McHardya887c1c2007-07-14 20:46:15 -07001903 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001905
1906err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001907 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001908err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001909 unregister_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001910err1:
1911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001914static void __exit arp_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 nf_unregister_sockopt(&arpt_sockopts);
Jan Engelhardt45385062009-07-04 12:50:00 +02001917 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001918 unregister_pernet_subsys(&arp_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921EXPORT_SYMBOL(arpt_register_table);
1922EXPORT_SYMBOL(arpt_unregister_table);
1923EXPORT_SYMBOL(arpt_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001925module_init(arp_tables_init);
1926module_exit(arp_tables_fini);