blob: 85a4f21aac1ad117f740d40da7123a663fdfefa8 [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,
251 const struct net_device *in,
252 const struct net_device *out,
Jan Engelhardt4abff072008-04-14 11:15:43 +0200253 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Eric Dumazetddc214c2009-02-18 17:47:50 +0100255 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned int verdict = NF_DROP;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200257 const struct arphdr *arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct arpt_entry *e, *back;
259 const char *indev, *outdev;
260 void *table_base;
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
268 indev = in ? in->name : nulldevname;
269 outdev = out ? out->name : nulldevname;
270
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;
274 table_base = private->entries[smp_processor_id()];
Stephen Hemminger78454472009-02-20 10:35:32 +0100275
Harald Welte2e4e6a12006-01-12 13:30:04 -0800276 e = get_entry(table_base, private->hook_entry[hook]);
277 back = get_entry(table_base, private->underflow[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jan Engelhardtde74c162009-07-05 18:26:37 +0200279 acpar.in = in;
280 acpar.out = out;
281 acpar.hooknum = hook;
282 acpar.family = NFPROTO_ARP;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200283 acpar.hotdrop = false;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200284
Herbert Xu3db05fe2007-10-15 00:53:15 -0700285 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200287 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200289 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
Jan Engelhardt98e86402009-04-15 21:06:05 +0200290 e = arpt_next_entry(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200291 continue;
292 }
293
Changli Gaof6670092010-07-23 13:40:53 +0200294 ADD_COUNTER(e->counters, arp_hdr_len(skb->dev), 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200295
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200296 t = arpt_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200297
298 /* Standard target? */
299 if (!t->u.kernel.target->target) {
300 int v;
301
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200302 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200303 if (v < 0) {
304 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200305 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000306 verdict = (unsigned int)(-v) - 1;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200307 break;
308 }
309 e = back;
310 back = get_entry(table_base, back->comefrom);
311 continue;
312 }
313 if (table_base + v
314 != arpt_next_entry(e)) {
315 /* Save old back ptr in next entry */
316 struct arpt_entry *next = arpt_next_entry(e);
317 next->comefrom = (void *)back - table_base;
318
319 /* set back pointer to next entry */
320 back = next;
321 }
322
323 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200326
327 /* Targets which reenter must return
328 * abs. verdicts
329 */
Jan Engelhardtde74c162009-07-05 18:26:37 +0200330 acpar.target = t->u.kernel.target;
331 acpar.targinfo = t->data;
332 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200333
334 /* Target might have changed stuff. */
335 arp = arp_hdr(skb);
336
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200337 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200338 e = arpt_next_entry(e);
339 else
340 /* Verdict */
341 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200342 } while (!acpar.hotdrop);
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200343 xt_write_recseq_end(addend);
344 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200346 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return NF_DROP;
348 else
349 return verdict;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* All zeroes == unconditional rule. */
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200353static inline bool unconditional(const struct arpt_arp *arp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200355 static const struct arpt_arp uncond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200357 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360/* Figures out from what hook each rule can be called: returns 0 if
361 * there are loops. Puts hook bitmask in comefrom.
362 */
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200363static int mark_source_chains(const struct xt_table_info *newinfo,
Eric Dumazet31836062005-12-13 23:13:48 -0800364 unsigned int valid_hooks, void *entry0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 unsigned int hook;
367
368 /* No recursion; use packet counter to save back ptrs (reset
369 * to 0 as we leave), and comefrom to save source hook bitmask.
370 */
371 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
372 unsigned int pos = newinfo->hook_entry[hook];
373 struct arpt_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800374 = (struct arpt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!(valid_hooks & (1 << hook)))
377 continue;
378
379 /* Set initial back pointer. */
380 e->counters.pcnt = pos;
381
382 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200383 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200384 = (void *)arpt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800385 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200388 pr_notice("arptables: loop hook %u pos %u %08X.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 hook, pos, e->comefrom);
390 return 0;
391 }
392 e->comefrom
393 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
394
395 /* Unconditional return/END. */
Joe Perches3666ed12009-11-23 23:17:06 +0100396 if ((e->target_offset == sizeof(struct arpt_entry) &&
397 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200398 XT_STANDARD_TARGET) == 0) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100399 t->verdict < 0 && unconditional(&e->arp)) ||
400 visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned int oldpos, size;
402
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100403 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200404 XT_STANDARD_TARGET) == 0) &&
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100405 t->verdict < -NF_MAX_VERDICT - 1) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800406 duprintf("mark_source_chains: bad "
407 "negative verdict (%i)\n",
408 t->verdict);
409 return 0;
410 }
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Return: backtrack through the last
413 * big jump.
414 */
415 do {
416 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
417 oldpos = pos;
418 pos = e->counters.pcnt;
419 e->counters.pcnt = 0;
420
421 /* We're at the start. */
422 if (pos == oldpos)
423 goto next;
424
425 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800426 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } while (oldpos == pos + e->next_offset);
428
429 /* Move along one */
430 size = e->next_offset;
431 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800432 (entry0 + pos + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 e->counters.pcnt = pos;
434 pos += size;
435 } else {
436 int newpos = t->verdict;
437
438 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200439 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100440 newpos >= 0) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800441 if (newpos > newinfo->size -
442 sizeof(struct arpt_entry)) {
443 duprintf("mark_source_chains: "
444 "bad verdict (%i)\n",
445 newpos);
446 return 0;
447 }
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* This a jump; chase it. */
450 duprintf("Jump rule %u -> %u\n",
451 pos, newpos);
452 } else {
453 /* ... this is a fallthru */
454 newpos = pos + e->next_offset;
455 }
456 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800457 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 e->counters.pcnt = pos;
459 pos = newpos;
460 }
461 }
462 next:
463 duprintf("Finished chain %u\n", hook);
464 }
465 return 1;
466}
467
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200468static inline int check_entry(const struct arpt_entry *e, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200470 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!arp_checkentry(&e->arp)) {
473 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
474 return -EINVAL;
475 }
476
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200477 if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800478 return -EINVAL;
479
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200480 t = arpt_get_target_c(e);
Dmitry Mishin590bdf72006-10-30 15:12:55 -0800481 if (e->target_offset + t->u.target_size > e->next_offset)
482 return -EINVAL;
483
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800484 return 0;
485}
486
487static inline int check_target(struct arpt_entry *e, const char *name)
488{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200489 struct xt_entry_target *t = arpt_get_target(e);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800490 int ret;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200491 struct xt_tgchk_param par = {
492 .table = name,
493 .entryinfo = e,
494 .target = t->u.kernel.target,
495 .targinfo = t->data,
496 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200497 .family = NFPROTO_ARP,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200498 };
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800499
Jan Engelhardt916a9172008-10-08 11:35:20 +0200500 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200501 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 duprintf("arp_tables: check failed for `%s'.\n",
503 t->u.kernel.target->name);
Jan Engelhardt367c6792008-10-08 11:35:17 +0200504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Jan Engelhardt367c6792008-10-08 11:35:17 +0200506 return 0;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800507}
508
509static inline int
Jan Engelhardt05595182010-02-24 18:33:43 +0100510find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800511{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200512 struct xt_entry_target *t;
Jan Engelhardt95eea852008-04-14 11:15:43 +0200513 struct xt_target *target;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800514 int ret;
515
516 ret = check_entry(e, name);
517 if (ret)
518 return ret;
519
520 t = arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200521 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
522 t->u.user.revision);
523 if (IS_ERR(target)) {
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800524 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200525 ret = PTR_ERR(target);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800526 goto out;
527 }
528 t->u.kernel.target = target;
529
530 ret = check_target(e, name);
531 if (ret)
532 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800534err:
535 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536out:
537 return ret;
538}
539
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200540static bool check_underflow(const struct arpt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200541{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200542 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200543 unsigned int verdict;
544
545 if (!unconditional(&e->arp))
546 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200547 t = arpt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200548 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
549 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200550 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200551 verdict = -verdict - 1;
552 return verdict == NF_DROP || verdict == NF_ACCEPT;
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static inline int check_entry_size_and_hooks(struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800556 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200557 const unsigned char *base,
558 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 const unsigned int *hook_entries,
560 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100561 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 unsigned int h;
564
Joe Perches3666ed12009-11-23 23:17:06 +0100565 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
566 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 duprintf("Bad offset %p\n", e);
568 return -EINVAL;
569 }
570
571 if (e->next_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200572 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 duprintf("checking: element %p size %u\n",
574 e, e->next_offset);
575 return -EINVAL;
576 }
577
578 /* Check hooks & underflows */
579 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200580 if (!(valid_hooks & (1 << h)))
581 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if ((unsigned char *)e - base == hook_entries[h])
583 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200584 if ((unsigned char *)e - base == underflows[h]) {
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200585 if (!check_underflow(e)) {
586 pr_err("Underflows must be unconditional and "
587 "use the STANDARD target with "
588 "ACCEPT/DROP\n");
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200589 return -EINVAL;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800596 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}
600
Jan Engelhardt05595182010-02-24 18:33:43 +0100601static inline void cleanup_entry(struct arpt_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200603 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200604 struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 t = arpt_get_target(e);
Jan Engelhardta2df1642008-10-08 11:35:19 +0200607 par.target = t->u.kernel.target;
608 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200609 par.family = NFPROTO_ARP;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200610 if (par.target->destroy != NULL)
611 par.target->destroy(&par);
612 module_put(par.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/* Checks and translates the user-supplied table segment (held in
616 * newinfo).
617 */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100618static int translate_table(struct xt_table_info *newinfo, void *entry0,
619 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100621 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100623 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jan Engelhardt0f234212010-02-24 18:36:04 +0100625 newinfo->size = repl->size;
626 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Init all hooks to impossible value. */
629 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
630 newinfo->hook_entry[i] = 0xFFFFFFFF;
631 newinfo->underflow[i] = 0xFFFFFFFF;
632 }
633
634 duprintf("translate_table: size %u\n", newinfo->size);
635 i = 0;
636
637 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100638 xt_entry_foreach(iter, entry0, newinfo->size) {
639 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100640 entry0 + repl->size,
641 repl->hook_entry,
642 repl->underflow,
643 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100644 if (ret != 0)
645 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100646 ++i;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200647 if (strcmp(arpt_get_target(iter)->u.user.name,
648 XT_ERROR_TARGET) == 0)
649 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
652 if (ret != 0)
653 return ret;
654
Jan Engelhardt0f234212010-02-24 18:36:04 +0100655 if (i != repl->num_entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 duprintf("translate_table: %u not %u entries\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100657 i, repl->num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
659 }
660
661 /* Check hooks all assigned */
662 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
663 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100664 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 continue;
666 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
667 duprintf("Invalid hook entry %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100668 i, repl->hook_entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EINVAL;
670 }
671 if (newinfo->underflow[i] == 0xFFFFFFFF) {
672 duprintf("Invalid underflow %u %u\n",
Jan Engelhardt0f234212010-02-24 18:36:04 +0100673 i, repl->underflow[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
675 }
676 }
677
Jan Engelhardt0f234212010-02-24 18:36:04 +0100678 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800679 duprintf("Looping hook\n");
680 return -ELOOP;
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Finally, each sanity check must pass */
684 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100685 xt_entry_foreach(iter, entry0, newinfo->size) {
Jan Engelhardt0f234212010-02-24 18:36:04 +0100686 ret = find_check_entry(iter, repl->name, repl->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100687 if (ret != 0)
688 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100689 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800692 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100693 xt_entry_foreach(iter, entry0, newinfo->size) {
694 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100695 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100696 cleanup_entry(iter);
697 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 /* And one copy for every other CPU */
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700702 for_each_possible_cpu(i) {
Eric Dumazet31836062005-12-13 23:13:48 -0800703 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
704 memcpy(newinfo->entries[i], entry0, newinfo->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 return ret;
708}
709
Harald Welte2e4e6a12006-01-12 13:30:04 -0800710static void get_counters(const struct xt_table_info *t,
711 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100713 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 unsigned int cpu;
715 unsigned int i;
716
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700717 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200718 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 i = 0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100721 xt_entry_foreach(iter, t->entries[cpu], t->size) {
Eric Dumazet83723d62011-01-10 20:11:38 +0100722 u64 bcnt, pcnt;
723 unsigned int start;
724
725 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200726 start = read_seqcount_begin(s);
Eric Dumazet83723d62011-01-10 20:11:38 +0100727 bcnt = iter->counters.bcnt;
728 pcnt = iter->counters.pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200729 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100730
731 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100732 ++i;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100735}
736
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200737static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Patrick McHardy27e2c262007-12-17 21:56:48 -0800739 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800740 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200741 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* We need atomic snapshot of counters: rest doesn't change
744 * (other than comefrom, which userspace doesn't care
745 * about).
746 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800747 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100748 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700751 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700753 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Patrick McHardy27e2c262007-12-17 21:56:48 -0800755 return counters;
756}
757
758static int copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200759 const struct xt_table *table,
Patrick McHardy27e2c262007-12-17 21:56:48 -0800760 void __user *userptr)
761{
762 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200763 const struct arpt_entry *e;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800764 struct xt_counters *counters;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200765 struct xt_table_info *private = table->private;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800766 int ret = 0;
767 void *loc_cpu_entry;
768
769 counters = alloc_counters(table);
770 if (IS_ERR(counters))
771 return PTR_ERR(counters);
772
Harald Welte2e4e6a12006-01-12 13:30:04 -0800773 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Eric Dumazet31836062005-12-13 23:13:48 -0800774 /* ... then copy entire thing ... */
775 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ret = -EFAULT;
777 goto free_counters;
778 }
779
780 /* FIXME: use iterator macros --RR */
781 /* ... then go back and fix counters and names */
782 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200783 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Eric Dumazet31836062005-12-13 23:13:48 -0800785 e = (struct arpt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (copy_to_user(userptr + off
787 + offsetof(struct arpt_entry, counters),
788 &counters[num],
789 sizeof(counters[num])) != 0) {
790 ret = -EFAULT;
791 goto free_counters;
792 }
793
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200794 t = arpt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200796 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 u.user.name),
798 t->u.kernel.target->name,
799 strlen(t->u.kernel.target->name)+1) != 0) {
800 ret = -EFAULT;
801 goto free_counters;
802 }
803 }
804
805 free_counters:
806 vfree(counters);
807 return ret;
808}
809
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800810#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200811static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800812{
813 int v = *(compat_int_t *)src;
814
815 if (v > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200816 v += xt_compat_calc_jump(NFPROTO_ARP, v);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800817 memcpy(dst, &v, sizeof(v));
818}
819
Jan Engelhardt739674f2009-06-26 08:23:19 +0200820static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800821{
822 compat_int_t cv = *(int *)src;
823
824 if (cv > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200825 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800826 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
827}
828
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200829static int compat_calc_entry(const struct arpt_entry *e,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800830 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200831 const void *base, struct xt_table_info *newinfo)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800832{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200833 const struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800834 unsigned int entry_offset;
835 int off, i, ret;
836
837 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
838 entry_offset = (void *)e - base;
839
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200840 t = arpt_get_target_c(e);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800841 off += xt_compat_target_offset(t->u.kernel.target);
842 newinfo->size -= off;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200843 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800844 if (ret)
845 return ret;
846
847 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
848 if (info->hook_entry[i] &&
849 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
850 newinfo->hook_entry[i] -= off;
851 if (info->underflow[i] &&
852 (e < (struct arpt_entry *)(base + info->underflow[i])))
853 newinfo->underflow[i] -= off;
854 }
855 return 0;
856}
857
858static int compat_table_info(const struct xt_table_info *info,
859 struct xt_table_info *newinfo)
860{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100861 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800862 void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100863 int ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800864
865 if (!newinfo || !info)
866 return -EINVAL;
867
868 /* we dont care about newinfo->entries[] */
869 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
870 newinfo->initial_entries = 0;
871 loc_cpu_entry = info->entries[raw_smp_processor_id()];
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100872 xt_compat_init_offsets(NFPROTO_ARP, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100873 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
874 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
875 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100876 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100877 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100878 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800879}
880#endif
881
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200882static int get_info(struct net *net, void __user *user,
883 const int *len, int compat)
Patrick McHardy41acd972007-12-17 22:26:24 -0800884{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200885 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardt4abff072008-04-14 11:15:43 +0200886 struct xt_table *t;
Patrick McHardy41acd972007-12-17 22:26:24 -0800887 int ret;
888
889 if (*len != sizeof(struct arpt_getinfo)) {
890 duprintf("length %u != %Zu\n", *len,
891 sizeof(struct arpt_getinfo));
892 return -EINVAL;
893 }
894
895 if (copy_from_user(name, user, sizeof(name)) != 0)
896 return -EFAULT;
897
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200898 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800899#ifdef CONFIG_COMPAT
900 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200901 xt_compat_lock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800902#endif
Jan Engelhardtee999d82008-10-08 11:35:01 +0200903 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardy41acd972007-12-17 22:26:24 -0800904 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000905 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy41acd972007-12-17 22:26:24 -0800906 struct arpt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200907 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800908#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800909 struct xt_table_info tmp;
910
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800911 if (compat) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800912 ret = compat_table_info(private, &tmp);
Jan Engelhardtee999d82008-10-08 11:35:01 +0200913 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800914 private = &tmp;
915 }
916#endif
Vasiliy Kulikov1a8b7a62010-11-03 08:44:12 +0100917 memset(&info, 0, sizeof(info));
Patrick McHardy41acd972007-12-17 22:26:24 -0800918 info.valid_hooks = t->valid_hooks;
919 memcpy(info.hook_entry, private->hook_entry,
920 sizeof(info.hook_entry));
921 memcpy(info.underflow, private->underflow,
922 sizeof(info.underflow));
923 info.num_entries = private->number;
924 info.size = private->size;
925 strcpy(info.name, name);
926
927 if (copy_to_user(user, &info, *len) != 0)
928 ret = -EFAULT;
929 else
930 ret = 0;
931 xt_table_unlock(t);
932 module_put(t->me);
933 } else
934 ret = t ? PTR_ERR(t) : -ENOENT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800935#ifdef CONFIG_COMPAT
936 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200937 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800938#endif
Patrick McHardy41acd972007-12-17 22:26:24 -0800939 return ret;
940}
941
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800942static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200943 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int ret;
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800946 struct arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200947 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800949 if (*len < sizeof(get)) {
950 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
951 return -EINVAL;
952 }
953 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
954 return -EFAULT;
955 if (*len != sizeof(struct arpt_get_entries) + get.size) {
956 duprintf("get_entries: %u != %Zu\n", *len,
957 sizeof(struct arpt_get_entries) + get.size);
958 return -EINVAL;
959 }
960
Jan Engelhardtee999d82008-10-08 11:35:01 +0200961 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000962 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +0200963 const struct xt_table_info *private = t->private;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 duprintf("t->private->number = %u\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800966 private->number);
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800967 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800968 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 t, uptr->entrytable);
970 else {
971 duprintf("get_entries: I've got %u not %u!\n",
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800972 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +0200973 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Harald Welte6b7d31f2005-10-26 09:34:24 +0200975 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800976 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200978 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 return ret;
981}
982
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800983static int __do_replace(struct net *net, const char *name,
984 unsigned int valid_hooks,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800985 struct xt_table_info *newinfo,
986 unsigned int num_counters,
987 void __user *counters_ptr)
988{
989 int ret;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200990 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800991 struct xt_table_info *oldinfo;
992 struct xt_counters *counters;
993 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100994 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800995
996 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +0100997 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800998 if (!counters) {
999 ret = -ENOMEM;
1000 goto out;
1001 }
1002
Jan Engelhardtee999d82008-10-08 11:35:01 +02001003 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001004 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001005 if (IS_ERR_OR_NULL(t)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001006 ret = t ? PTR_ERR(t) : -ENOENT;
1007 goto free_newinfo_counters_untrans;
1008 }
1009
1010 /* You lied! */
1011 if (valid_hooks != t->valid_hooks) {
1012 duprintf("Valid hook crap: %08X vs %08X\n",
1013 valid_hooks, t->valid_hooks);
1014 ret = -EINVAL;
1015 goto put_module;
1016 }
1017
1018 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1019 if (!oldinfo)
1020 goto put_module;
1021
1022 /* Update module usage count based on number of rules */
1023 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1024 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1025 if ((oldinfo->number > oldinfo->initial_entries) ||
1026 (newinfo->number <= oldinfo->initial_entries))
1027 module_put(t->me);
1028 if ((oldinfo->number > oldinfo->initial_entries) &&
1029 (newinfo->number <= oldinfo->initial_entries))
1030 module_put(t->me);
1031
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001032 /* Get the old counters, and synchronize with replace */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001033 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001034
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001035 /* Decrease module usage counts and free resource */
1036 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001037 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001038 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001039
1040 xt_free_table_info(oldinfo);
1041 if (copy_to_user(counters_ptr, counters,
1042 sizeof(struct xt_counters) * num_counters) != 0)
1043 ret = -EFAULT;
1044 vfree(counters);
1045 xt_table_unlock(t);
1046 return ret;
1047
1048 put_module:
1049 module_put(t->me);
1050 xt_table_unlock(t);
1051 free_newinfo_counters_untrans:
1052 vfree(counters);
1053 out:
1054 return ret;
1055}
1056
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001057static int do_replace(struct net *net, const void __user *user,
1058 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 int ret;
1061 struct arpt_replace tmp;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001062 struct xt_table_info *newinfo;
1063 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001064 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1067 return -EFAULT;
1068
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001069 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001070 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1071 return -ENOMEM;
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001072 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -08001073
Harald Welte2e4e6a12006-01-12 13:30:04 -08001074 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!newinfo)
1076 return -ENOMEM;
1077
Eric Dumazet31836062005-12-13 23:13:48 -08001078 /* choose the copy that is on our node/cpu */
1079 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1080 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 tmp.size) != 0) {
1082 ret = -EFAULT;
1083 goto free_newinfo;
1084 }
1085
Jan Engelhardt0f234212010-02-24 18:36:04 +01001086 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (ret != 0)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001088 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 duprintf("arp_tables: Translated table\n");
1091
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001092 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001093 tmp.num_counters, tmp.counters);
1094 if (ret)
1095 goto free_newinfo_untrans;
1096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001098 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001099 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001100 cleanup_entry(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001102 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return ret;
1104}
1105
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001106static int do_add_counters(struct net *net, const void __user *user,
1107 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001109 unsigned int i, curcpu;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001110 struct xt_counters_info tmp;
1111 struct xt_counters *paddc;
1112 unsigned int num_counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001113 const char *name;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001114 int size;
1115 void *ptmp;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001116 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001117 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001118 int ret = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001119 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001120 struct arpt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001121 unsigned int addend;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001122#ifdef CONFIG_COMPAT
1123 struct compat_xt_counters_info compat_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001125 if (compat) {
1126 ptmp = &compat_tmp;
1127 size = sizeof(struct compat_xt_counters_info);
1128 } else
1129#endif
1130 {
1131 ptmp = &tmp;
1132 size = sizeof(struct xt_counters_info);
1133 }
1134
1135 if (copy_from_user(ptmp, user, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -EFAULT;
1137
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001138#ifdef CONFIG_COMPAT
1139 if (compat) {
1140 num_counters = compat_tmp.num_counters;
1141 name = compat_tmp.name;
1142 } else
1143#endif
1144 {
1145 num_counters = tmp.num_counters;
1146 name = tmp.name;
1147 }
1148
1149 if (len != size + num_counters * sizeof(struct xt_counters))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EINVAL;
1151
Eric Dumazete12f8e22010-06-04 13:31:29 +02001152 paddc = vmalloc(len - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!paddc)
1154 return -ENOMEM;
1155
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001156 if (copy_from_user(paddc, user + size, len - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ret = -EFAULT;
1158 goto free;
1159 }
1160
Jan Engelhardtee999d82008-10-08 11:35:01 +02001161 t = xt_find_table_lock(net, NFPROTO_ARP, name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001162 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001163 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001167 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001168 private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001169 if (private->number != num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 ret = -EINVAL;
1171 goto unlock_up_free;
1172 }
1173
1174 i = 0;
Eric Dumazet31836062005-12-13 23:13:48 -08001175 /* Choose the copy that is on our node */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001176 curcpu = smp_processor_id();
1177 loc_cpu_entry = private->entries[curcpu];
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001178 addend = xt_write_recseq_begin();
Jan Engelhardt05595182010-02-24 18:33:43 +01001179 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1180 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1181 ++i;
1182 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001183 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001185 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001186 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001187 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 free:
1189 vfree(paddc);
1190
1191 return ret;
1192}
1193
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001194#ifdef CONFIG_COMPAT
Jan Engelhardt05595182010-02-24 18:33:43 +01001195static inline void compat_release_entry(struct compat_arpt_entry *e)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001196{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001197 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001198
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001199 t = compat_arpt_get_target(e);
1200 module_put(t->u.kernel.target->me);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001201}
1202
1203static inline int
1204check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1205 struct xt_table_info *newinfo,
1206 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001207 const unsigned char *base,
1208 const unsigned char *limit,
1209 const unsigned int *hook_entries,
1210 const unsigned int *underflows,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001211 const char *name)
1212{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001213 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001214 struct xt_target *target;
1215 unsigned int entry_offset;
1216 int ret, off, h;
1217
1218 duprintf("check_compat_entry_size_and_hooks %p\n", e);
Joe Perches3666ed12009-11-23 23:17:06 +01001219 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1220 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001221 duprintf("Bad offset %p, limit = %p\n", e, limit);
1222 return -EINVAL;
1223 }
1224
1225 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1226 sizeof(struct compat_xt_entry_target)) {
1227 duprintf("checking: element %p size %u\n",
1228 e, e->next_offset);
1229 return -EINVAL;
1230 }
1231
1232 /* For purposes of check_entry casting the compat entry is fine */
1233 ret = check_entry((struct arpt_entry *)e, name);
1234 if (ret)
1235 return ret;
1236
1237 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1238 entry_offset = (void *)e - (void *)base;
1239
1240 t = compat_arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001241 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1242 t->u.user.revision);
1243 if (IS_ERR(target)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001244 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1245 t->u.user.name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001246 ret = PTR_ERR(target);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001247 goto out;
1248 }
1249 t->u.kernel.target = target;
1250
1251 off += xt_compat_target_offset(target);
1252 *size += off;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001253 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001254 if (ret)
1255 goto release_target;
1256
1257 /* Check hooks & underflows */
1258 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1259 if ((unsigned char *)e - base == hook_entries[h])
1260 newinfo->hook_entry[h] = hook_entries[h];
1261 if ((unsigned char *)e - base == underflows[h])
1262 newinfo->underflow[h] = underflows[h];
1263 }
1264
1265 /* Clear counters and comefrom */
1266 memset(&e->counters, 0, sizeof(e->counters));
1267 e->comefrom = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001268 return 0;
1269
1270release_target:
1271 module_put(t->u.kernel.target->me);
1272out:
1273 return ret;
1274}
1275
1276static int
1277compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1278 unsigned int *size, const char *name,
1279 struct xt_table_info *newinfo, unsigned char *base)
1280{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001281 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001282 struct xt_target *target;
1283 struct arpt_entry *de;
1284 unsigned int origsize;
1285 int ret, h;
1286
1287 ret = 0;
1288 origsize = *size;
1289 de = (struct arpt_entry *)*dstptr;
1290 memcpy(de, e, sizeof(struct arpt_entry));
1291 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1292
1293 *dstptr += sizeof(struct arpt_entry);
1294 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1295
1296 de->target_offset = e->target_offset - (origsize - *size);
1297 t = compat_arpt_get_target(e);
1298 target = t->u.kernel.target;
1299 xt_compat_target_from_user(t, dstptr, size);
1300
1301 de->next_offset = e->next_offset - (origsize - *size);
1302 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1303 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1304 newinfo->hook_entry[h] -= origsize - *size;
1305 if ((unsigned char *)de - base < newinfo->underflow[h])
1306 newinfo->underflow[h] -= origsize - *size;
1307 }
1308 return ret;
1309}
1310
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001311static int translate_compat_table(const char *name,
1312 unsigned int valid_hooks,
1313 struct xt_table_info **pinfo,
1314 void **pentry0,
1315 unsigned int total_size,
1316 unsigned int number,
1317 unsigned int *hook_entries,
1318 unsigned int *underflows)
1319{
1320 unsigned int i, j;
1321 struct xt_table_info *newinfo, *info;
1322 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001323 struct compat_arpt_entry *iter0;
1324 struct arpt_entry *iter1;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001325 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001326 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001327
1328 info = *pinfo;
1329 entry0 = *pentry0;
1330 size = total_size;
1331 info->number = number;
1332
1333 /* Init all hooks to impossible value. */
1334 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1335 info->hook_entry[i] = 0xFFFFFFFF;
1336 info->underflow[i] = 0xFFFFFFFF;
1337 }
1338
1339 duprintf("translate_compat_table: size %u\n", info->size);
1340 j = 0;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001341 xt_compat_lock(NFPROTO_ARP);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001342 xt_compat_init_offsets(NFPROTO_ARP, number);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001343 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001344 xt_entry_foreach(iter0, entry0, total_size) {
1345 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001346 entry0,
1347 entry0 + total_size,
1348 hook_entries,
1349 underflows,
1350 name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001351 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001352 goto out_unlock;
1353 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001354 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001355
1356 ret = -EINVAL;
1357 if (j != number) {
1358 duprintf("translate_compat_table: %u not %u entries\n",
1359 j, number);
1360 goto out_unlock;
1361 }
1362
1363 /* Check hooks all assigned */
1364 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1365 /* Only hooks which are valid */
1366 if (!(valid_hooks & (1 << i)))
1367 continue;
1368 if (info->hook_entry[i] == 0xFFFFFFFF) {
1369 duprintf("Invalid hook entry %u %u\n",
1370 i, hook_entries[i]);
1371 goto out_unlock;
1372 }
1373 if (info->underflow[i] == 0xFFFFFFFF) {
1374 duprintf("Invalid underflow %u %u\n",
1375 i, underflows[i]);
1376 goto out_unlock;
1377 }
1378 }
1379
1380 ret = -ENOMEM;
1381 newinfo = xt_alloc_table_info(size);
1382 if (!newinfo)
1383 goto out_unlock;
1384
1385 newinfo->number = number;
1386 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1387 newinfo->hook_entry[i] = info->hook_entry[i];
1388 newinfo->underflow[i] = info->underflow[i];
1389 }
1390 entry1 = newinfo->entries[raw_smp_processor_id()];
1391 pos = entry1;
1392 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001393 xt_entry_foreach(iter0, entry0, total_size) {
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001394 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1395 name, newinfo, entry1);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001396 if (ret != 0)
1397 break;
1398 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001399 xt_compat_flush_offsets(NFPROTO_ARP);
1400 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001401 if (ret)
1402 goto free_newinfo;
1403
1404 ret = -ELOOP;
1405 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1406 goto free_newinfo;
1407
1408 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001409 xt_entry_foreach(iter1, entry1, newinfo->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001410 ret = check_target(iter1, name);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001411 if (ret != 0)
1412 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001413 ++i;
Florian Westphalcca77b72010-08-23 14:41:22 -07001414 if (strcmp(arpt_get_target(iter1)->u.user.name,
1415 XT_ERROR_TARGET) == 0)
1416 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001417 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001418 if (ret) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001419 /*
1420 * The first i matches need cleanup_entry (calls ->destroy)
1421 * because they had called ->check already. The other j-i
1422 * entries need only release.
1423 */
1424 int skip = i;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001425 j -= i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001426 xt_entry_foreach(iter0, entry0, newinfo->size) {
1427 if (skip-- > 0)
1428 continue;
Jan Engelhardt05595182010-02-24 18:33:43 +01001429 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001430 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001431 compat_release_entry(iter0);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001432 }
Jan Engelhardt05595182010-02-24 18:33:43 +01001433 xt_entry_foreach(iter1, entry1, newinfo->size) {
1434 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001435 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001436 cleanup_entry(iter1);
1437 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001438 xt_free_table_info(newinfo);
1439 return ret;
1440 }
1441
1442 /* And one copy for every other CPU */
1443 for_each_possible_cpu(i)
1444 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1445 memcpy(newinfo->entries[i], entry1, newinfo->size);
1446
1447 *pinfo = newinfo;
1448 *pentry0 = entry1;
1449 xt_free_table_info(info);
1450 return 0;
1451
1452free_newinfo:
1453 xt_free_table_info(newinfo);
1454out:
Jan Engelhardt05595182010-02-24 18:33:43 +01001455 xt_entry_foreach(iter0, entry0, total_size) {
1456 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001457 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001458 compat_release_entry(iter0);
1459 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001460 return ret;
1461out_unlock:
Jan Engelhardtee999d82008-10-08 11:35:01 +02001462 xt_compat_flush_offsets(NFPROTO_ARP);
1463 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001464 goto out;
1465}
1466
1467struct compat_arpt_replace {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001468 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001469 u32 valid_hooks;
1470 u32 num_entries;
1471 u32 size;
1472 u32 hook_entry[NF_ARP_NUMHOOKS];
1473 u32 underflow[NF_ARP_NUMHOOKS];
1474 u32 num_counters;
1475 compat_uptr_t counters;
1476 struct compat_arpt_entry entries[0];
1477};
1478
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001479static int compat_do_replace(struct net *net, void __user *user,
1480 unsigned int len)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001481{
1482 int ret;
1483 struct compat_arpt_replace tmp;
1484 struct xt_table_info *newinfo;
1485 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001486 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001487
1488 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1489 return -EFAULT;
1490
1491 /* overflow check */
1492 if (tmp.size >= INT_MAX / num_possible_cpus())
1493 return -ENOMEM;
1494 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1495 return -ENOMEM;
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001496 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001497
1498 newinfo = xt_alloc_table_info(tmp.size);
1499 if (!newinfo)
1500 return -ENOMEM;
1501
1502 /* choose the copy that is on our node/cpu */
1503 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1504 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1505 ret = -EFAULT;
1506 goto free_newinfo;
1507 }
1508
1509 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1510 &newinfo, &loc_cpu_entry, tmp.size,
1511 tmp.num_entries, tmp.hook_entry,
1512 tmp.underflow);
1513 if (ret != 0)
1514 goto free_newinfo;
1515
1516 duprintf("compat_do_replace: Translated table\n");
1517
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001518 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001519 tmp.num_counters, compat_ptr(tmp.counters));
1520 if (ret)
1521 goto free_newinfo_untrans;
1522 return 0;
1523
1524 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001525 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001526 cleanup_entry(iter);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001527 free_newinfo:
1528 xt_free_table_info(newinfo);
1529 return ret;
1530}
1531
1532static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1533 unsigned int len)
1534{
1535 int ret;
1536
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001537 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001538 return -EPERM;
1539
1540 switch (cmd) {
1541 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001542 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001543 break;
1544
1545 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001546 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001547 break;
1548
1549 default:
1550 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1551 ret = -EINVAL;
1552 }
1553
1554 return ret;
1555}
1556
1557static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1558 compat_uint_t *size,
1559 struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001560 unsigned int i)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001561{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001562 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001563 struct compat_arpt_entry __user *ce;
1564 u_int16_t target_offset, next_offset;
1565 compat_uint_t origsize;
1566 int ret;
1567
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001568 origsize = *size;
1569 ce = (struct compat_arpt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001570 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1571 copy_to_user(&ce->counters, &counters[i],
1572 sizeof(counters[i])) != 0)
1573 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001574
1575 *dstptr += sizeof(struct compat_arpt_entry);
1576 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1577
1578 target_offset = e->target_offset - (origsize - *size);
1579
1580 t = arpt_get_target(e);
1581 ret = xt_compat_target_to_user(t, dstptr, size);
1582 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001583 return ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001584 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001585 if (put_user(target_offset, &ce->target_offset) != 0 ||
1586 put_user(next_offset, &ce->next_offset) != 0)
1587 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001588 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001589}
1590
1591static int compat_copy_entries_to_user(unsigned int total_size,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001592 struct xt_table *table,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001593 void __user *userptr)
1594{
1595 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001596 const struct xt_table_info *private = table->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001597 void __user *pos;
1598 unsigned int size;
1599 int ret = 0;
1600 void *loc_cpu_entry;
1601 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001602 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001603
1604 counters = alloc_counters(table);
1605 if (IS_ERR(counters))
1606 return PTR_ERR(counters);
1607
1608 /* choose the copy on our node/cpu */
1609 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1610 pos = userptr;
1611 size = total_size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001612 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1613 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001614 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001615 if (ret != 0)
1616 break;
1617 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001618 vfree(counters);
1619 return ret;
1620}
1621
1622struct compat_arpt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001623 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001624 compat_uint_t size;
1625 struct compat_arpt_entry entrytable[0];
1626};
1627
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001628static int compat_get_entries(struct net *net,
1629 struct compat_arpt_get_entries __user *uptr,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001630 int *len)
1631{
1632 int ret;
1633 struct compat_arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001634 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001635
1636 if (*len < sizeof(get)) {
1637 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1638 return -EINVAL;
1639 }
1640 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1641 return -EFAULT;
1642 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1643 duprintf("compat_get_entries: %u != %zu\n",
1644 *len, sizeof(get) + get.size);
1645 return -EINVAL;
1646 }
1647
Jan Engelhardtee999d82008-10-08 11:35:01 +02001648 xt_compat_lock(NFPROTO_ARP);
1649 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001650 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001651 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001652 struct xt_table_info info;
1653
1654 duprintf("t->private->number = %u\n", private->number);
1655 ret = compat_table_info(private, &info);
1656 if (!ret && get.size == info.size) {
1657 ret = compat_copy_entries_to_user(private->size,
1658 t, uptr->entrytable);
1659 } else if (!ret) {
1660 duprintf("compat_get_entries: I've got %u not %u!\n",
1661 private->size, get.size);
Patrick McHardy544473c2008-04-14 11:15:45 +02001662 ret = -EAGAIN;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001663 }
Jan Engelhardtee999d82008-10-08 11:35:01 +02001664 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001665 module_put(t->me);
1666 xt_table_unlock(t);
1667 } else
1668 ret = t ? PTR_ERR(t) : -ENOENT;
1669
Jan Engelhardtee999d82008-10-08 11:35:01 +02001670 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001671 return ret;
1672}
1673
1674static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1675
1676static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1677 int *len)
1678{
1679 int ret;
1680
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001681 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001682 return -EPERM;
1683
1684 switch (cmd) {
1685 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001686 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001687 break;
1688 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001689 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001690 break;
1691 default:
1692 ret = do_arpt_get_ctl(sk, cmd, user, len);
1693 }
1694 return ret;
1695}
1696#endif
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1699{
1700 int ret;
1701
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001702 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return -EPERM;
1704
1705 switch (cmd) {
1706 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001707 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709
1710 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001711 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713
1714 default:
1715 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1716 ret = -EINVAL;
1717 }
1718
1719 return ret;
1720}
1721
1722static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1723{
1724 int ret;
1725
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001726 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -EPERM;
1728
1729 switch (cmd) {
Patrick McHardy41acd972007-12-17 22:26:24 -08001730 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001731 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy41acd972007-12-17 22:26:24 -08001732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Patrick McHardy11f6dff2007-12-17 22:26:38 -08001734 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001735 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Harald Welte6b7d31f2005-10-26 09:34:24 +02001738 case ARPT_SO_GET_REVISION_TARGET: {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001739 struct xt_get_revision rev;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001740
1741 if (*len != sizeof(rev)) {
1742 ret = -EINVAL;
1743 break;
1744 }
1745 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1746 ret = -EFAULT;
1747 break;
1748 }
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001749 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001750
Jan Engelhardtee999d82008-10-08 11:35:01 +02001751 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001752 rev.revision, 1, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001753 "arpt_%s", rev.name);
1754 break;
1755 }
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 default:
1758 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1759 ret = -EINVAL;
1760 }
1761
1762 return ret;
1763}
1764
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001765struct xt_table *arpt_register_table(struct net *net,
1766 const struct xt_table *table,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001767 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001770 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001771 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001772 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001773 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Harald Welte2e4e6a12006-01-12 13:30:04 -08001775 newinfo = xt_alloc_table_info(repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!newinfo) {
1777 ret = -ENOMEM;
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Eric Dumazet31836062005-12-13 23:13:48 -08001780
1781 /* choose the copy on our node/cpu */
1782 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1783 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jan Engelhardt0f234212010-02-24 18:36:04 +01001785 ret = translate_table(newinfo, loc_cpu_entry, repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 duprintf("arpt_register_table: translate table gives %d\n", ret);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001787 if (ret != 0)
1788 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001790 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001791 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001792 ret = PTR_ERR(new_table);
1793 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001795 return new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001797out_free:
1798 xt_free_table_info(newinfo);
1799out:
1800 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Jan Engelhardt4abff072008-04-14 11:15:43 +02001803void arpt_unregister_table(struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Harald Welte2e4e6a12006-01-12 13:30:04 -08001805 struct xt_table_info *private;
Eric Dumazet31836062005-12-13 23:13:48 -08001806 void *loc_cpu_entry;
Alexey Dobriyandf200962008-01-31 04:05:34 -08001807 struct module *table_owner = table->me;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001808 struct arpt_entry *iter;
Eric Dumazet31836062005-12-13 23:13:48 -08001809
Harald Welte2e4e6a12006-01-12 13:30:04 -08001810 private = xt_unregister_table(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 /* Decrease module usage counts and free resources */
Harald Welte2e4e6a12006-01-12 13:30:04 -08001813 loc_cpu_entry = private->entries[raw_smp_processor_id()];
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001814 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Jan Engelhardt05595182010-02-24 18:33:43 +01001815 cleanup_entry(iter);
Alexey Dobriyandf200962008-01-31 04:05:34 -08001816 if (private->number > private->initial_entries)
1817 module_put(table_owner);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001818 xt_free_table_info(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
1821/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001822static struct xt_target arpt_builtin_tg[] __read_mostly = {
1823 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001824 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001825 .targetsize = sizeof(int),
1826 .family = NFPROTO_ARP,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001827#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001828 .compatsize = sizeof(compat_int_t),
1829 .compat_from_user = compat_standard_from_user,
1830 .compat_to_user = compat_standard_to_user,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001831#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001832 },
1833 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001834 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001835 .target = arpt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001836 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001837 .family = NFPROTO_ARP,
1838 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839};
1840
1841static struct nf_sockopt_ops arpt_sockopts = {
1842 .pf = PF_INET,
1843 .set_optmin = ARPT_BASE_CTL,
1844 .set_optmax = ARPT_SO_SET_MAX+1,
1845 .set = do_arpt_set_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001846#ifdef CONFIG_COMPAT
1847 .compat_set = compat_do_arpt_set_ctl,
1848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 .get_optmin = ARPT_BASE_CTL,
1850 .get_optmax = ARPT_SO_GET_MAX+1,
1851 .get = do_arpt_get_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001852#ifdef CONFIG_COMPAT
1853 .compat_get = compat_do_arpt_get_ctl,
1854#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001855 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856};
1857
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001858static int __net_init arp_tables_net_init(struct net *net)
1859{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001860 return xt_proto_init(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001861}
1862
1863static void __net_exit arp_tables_net_exit(struct net *net)
1864{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001865 xt_proto_fini(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001866}
1867
1868static struct pernet_operations arp_tables_net_ops = {
1869 .init = arp_tables_net_init,
1870 .exit = arp_tables_net_exit,
1871};
1872
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001873static int __init arp_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int ret;
1876
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001877 ret = register_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001878 if (ret < 0)
1879 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001880
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001881 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001882 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001883 if (ret < 0)
1884 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* Register setsockopt */
1887 ret = nf_register_sockopt(&arpt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001888 if (ret < 0)
1889 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Patrick McHardya887c1c2007-07-14 20:46:15 -07001891 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001893
1894err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001895 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001896err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001897 unregister_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001898err1:
1899 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001902static void __exit arp_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 nf_unregister_sockopt(&arpt_sockopts);
Jan Engelhardt45385062009-07-04 12:50:00 +02001905 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001906 unregister_pernet_subsys(&arp_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909EXPORT_SYMBOL(arpt_register_table);
1910EXPORT_SYMBOL(arpt_unregister_table);
1911EXPORT_SYMBOL(arpt_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001913module_init(arp_tables_init);
1914module_exit(arp_tables_fini);