blob: e02b8626519424d5cac93a5dda41f31145427ade [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
Jan Engelhardte3eaa992009-06-17 22:14:54 +020037void *arpt_alloc_initial_table(const struct xt_table *info)
38{
39 return xt_alloc_initial_table(arpt, ARPT);
40}
41EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
Jan Engelhardt5452e422008-04-14 11:15:35 +020044 const char *hdr_addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 int i, ret;
47
48 if (len > ARPT_DEV_ADDR_LEN_MAX)
49 len = ARPT_DEV_ADDR_LEN_MAX;
50
51 ret = 0;
52 for (i = 0; i < len; i++)
53 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
54
Eric Dumazeta02cec22010-09-22 20:43:57 +000055 return ret != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Eric Dumazetddc214c2009-02-18 17:47:50 +010058/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030059 * Unfortunately, _b and _mask are not aligned to an int (or long int)
Eric Dumazetddc214c2009-02-18 17:47:50 +010060 * Some arches dont care, unrolling the loop is a win on them.
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070061 * For other arches, we only have a 16bit alignement.
Eric Dumazetddc214c2009-02-18 17:47:50 +010062 */
63static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
64{
65#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Eric Dumazetb8dfe492009-03-25 17:31:52 +010066 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
Eric Dumazetddc214c2009-02-18 17:47:50 +010067#else
68 unsigned long ret = 0;
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070069 const u16 *a = (const u16 *)_a;
70 const u16 *b = (const u16 *)_b;
71 const u16 *mask = (const u16 *)_mask;
Eric Dumazetddc214c2009-02-18 17:47:50 +010072 int i;
73
Eric Dumazet35c7f6d2009-03-24 14:15:22 -070074 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
75 ret |= (a[i] ^ b[i]) & mask[i];
Eric Dumazetddc214c2009-02-18 17:47:50 +010076#endif
77 return ret;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Returns whether packet matches rule or not. */
81static inline int arp_packet_match(const struct arphdr *arphdr,
82 struct net_device *dev,
83 const char *indev,
84 const char *outdev,
85 const struct arpt_arp *arpinfo)
86{
Jan Engelhardt5452e422008-04-14 11:15:35 +020087 const char *arpptr = (char *)(arphdr + 1);
88 const char *src_devaddr, *tgt_devaddr;
Al Viro59b8bfd2006-09-28 14:21:07 -070089 __be32 src_ipaddr, tgt_ipaddr;
Eric Dumazetddc214c2009-02-18 17:47:50 +010090 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Joe Perchesc37a2df2016-06-24 13:25:22 -070092 if (NF_INVF(arpinfo, ARPT_INV_ARPOP,
93 (arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Joe Perchesc37a2df2016-06-24 13:25:22 -070096 if (NF_INVF(arpinfo, ARPT_INV_ARPHRD,
97 (arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Joe Perchesc37a2df2016-06-24 13:25:22 -0700100 if (NF_INVF(arpinfo, ARPT_INV_ARPPRO,
101 (arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Joe Perchesc37a2df2016-06-24 13:25:22 -0700104 if (NF_INVF(arpinfo, ARPT_INV_ARPHLN,
105 (arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 src_devaddr = arpptr;
109 arpptr += dev->addr_len;
110 memcpy(&src_ipaddr, arpptr, sizeof(u32));
111 arpptr += sizeof(u32);
112 tgt_devaddr = arpptr;
113 arpptr += dev->addr_len;
114 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
115
Joe Perchesc37a2df2016-06-24 13:25:22 -0700116 if (NF_INVF(arpinfo, ARPT_INV_SRCDEVADDR,
117 arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr,
118 dev->addr_len)) ||
119 NF_INVF(arpinfo, ARPT_INV_TGTDEVADDR,
120 arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr,
121 dev->addr_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Joe Perchesc37a2df2016-06-24 13:25:22 -0700124 if (NF_INVF(arpinfo, ARPT_INV_SRCIP,
125 (src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr) ||
126 NF_INVF(arpinfo, ARPT_INV_TGTIP,
127 (tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* Look for ifname matches. */
Eric Dumazetddc214c2009-02-18 17:47:50 +0100131 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Joe Perchesc37a2df2016-06-24 13:25:22 -0700133 if (NF_INVF(arpinfo, ARPT_INV_VIA_IN, ret != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Eric Dumazetddc214c2009-02-18 17:47:50 +0100136 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Joe Perchesc37a2df2016-06-24 13:25:22 -0700138 if (NF_INVF(arpinfo, ARPT_INV_VIA_OUT, ret != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return 1;
142}
143
144static inline int arp_checkentry(const struct arpt_arp *arp)
145{
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200146 if (arp->flags & ~ARPT_F_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200148 if (arp->invflags & ~ARPT_INV_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return 1;
152}
153
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200154static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200155arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Joe Perchese87cc472012-05-13 21:56:26 +0000157 net_err_ratelimited("arp_tables: error: '%s'\n",
158 (const char *)par->targinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return NF_DROP;
161}
162
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200163static inline const struct xt_entry_target *
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200164arpt_get_target_c(const struct arpt_entry *e)
165{
166 return arpt_get_target((struct arpt_entry *)e);
167}
168
169static inline struct arpt_entry *
170get_entry(const void *base, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 return (struct arpt_entry *)(base + offset);
173}
174
Florian Westphal6c7941d2015-07-14 17:51:10 +0200175static inline
Jan Engelhardt98e86402009-04-15 21:06:05 +0200176struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
177{
178 return (void *)entry + entry->next_offset;
179}
180
Herbert Xu3db05fe2007-10-15 00:53:15 -0700181unsigned int arpt_do_table(struct sk_buff *skb,
David S. Millerb85c3dc2015-04-03 21:18:46 -0400182 const struct nf_hook_state *state,
Jan Engelhardt4abff072008-04-14 11:15:43 +0200183 struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -0500185 unsigned int hook = state->hook;
Eric Dumazetddc214c2009-02-18 17:47:50 +0100186 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned int verdict = NF_DROP;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200188 const struct arphdr *arp;
Florian Westphal3bd22992015-06-30 22:21:00 +0200189 struct arpt_entry *e, **jumpstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 const char *indev, *outdev;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700191 const void *table_base;
Florian Westphal3bd22992015-06-30 22:21:00 +0200192 unsigned int cpu, stackidx = 0;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200193 const struct xt_table_info *private;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200194 struct xt_action_param acpar;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200195 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800197 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return NF_DROP;
199
David S. Millerb85c3dc2015-04-03 21:18:46 -0400200 indev = state->in ? state->in->name : nulldevname;
201 outdev = state->out ? state->out->name : nulldevname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200203 local_bh_disable();
204 addend = xt_write_recseq_begin();
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700205 private = table->private;
Florian Westphal3bd22992015-06-30 22:21:00 +0200206 cpu = smp_processor_id();
Will Deaconb416c142013-10-21 13:14:53 +0100207 /*
208 * Ensure we load private-> members after we've fetched the base
209 * pointer.
210 */
211 smp_read_barrier_depends();
Florian Westphal482cfc32015-06-11 01:34:55 +0200212 table_base = private->entries;
Florian Westphal3bd22992015-06-30 22:21:00 +0200213 jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
Stephen Hemminger78454472009-02-20 10:35:32 +0100214
Florian Westphal7814b6e2015-07-14 17:51:08 +0200215 /* No TEE support for arptables, so no need to switch to alternate
216 * stack. All targets that reenter must return absolute verdicts.
217 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800218 e = get_entry(table_base, private->hook_entry[hook]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Eric W. Biederman156c1962015-09-18 14:32:58 -0500220 acpar.net = state->net;
David S. Millerb85c3dc2015-04-03 21:18:46 -0400221 acpar.in = state->in;
222 acpar.out = state->out;
Jan Engelhardtde74c162009-07-05 18:26:37 +0200223 acpar.hooknum = hook;
224 acpar.family = NFPROTO_ARP;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200225 acpar.hotdrop = false;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200226
Herbert Xu3db05fe2007-10-15 00:53:15 -0700227 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 do {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200229 const struct xt_entry_target *t;
Florian Westphal71ae0df2015-06-11 01:34:54 +0200230 struct xt_counters *counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200232 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
Jan Engelhardt98e86402009-04-15 21:06:05 +0200233 e = arpt_next_entry(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200234 continue;
235 }
236
Florian Westphal71ae0df2015-06-11 01:34:54 +0200237 counter = xt_get_this_cpu_counter(&e->counters);
238 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200239
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200240 t = arpt_get_target_c(e);
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200241
242 /* Standard target? */
243 if (!t->u.kernel.target->target) {
244 int v;
245
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200246 v = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200247 if (v < 0) {
248 /* Pop from stack? */
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200249 if (v != XT_RETURN) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000250 verdict = (unsigned int)(-v) - 1;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200251 break;
252 }
Florian Westphal3bd22992015-06-30 22:21:00 +0200253 if (stackidx == 0) {
254 e = get_entry(table_base,
255 private->underflow[hook]);
256 } else {
257 e = jumpstack[--stackidx];
258 e = arpt_next_entry(e);
259 }
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200260 continue;
261 }
262 if (table_base + v
263 != arpt_next_entry(e)) {
Florian Westphalf506da52018-02-07 13:46:25 +0100264 if (unlikely(stackidx >= private->stacksize)) {
265 verdict = NF_DROP;
266 break;
267 }
Florian Westphal3bd22992015-06-30 22:21:00 +0200268 jumpstack[stackidx++] = e;
Jan Engelhardta1ff4ac2009-04-15 21:28:39 +0200269 }
270
271 e = get_entry(table_base, v);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200272 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200274
Jan Engelhardtde74c162009-07-05 18:26:37 +0200275 acpar.target = t->u.kernel.target;
276 acpar.targinfo = t->data;
277 verdict = t->u.kernel.target->target(skb, &acpar);
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200278
279 /* Target might have changed stuff. */
280 arp = arp_hdr(skb);
281
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200282 if (verdict == XT_CONTINUE)
Jan Engelhardt7a6b1c42009-04-15 21:35:33 +0200283 e = arpt_next_entry(e);
284 else
285 /* Verdict */
286 break;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200287 } while (!acpar.hotdrop);
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200288 xt_write_recseq_end(addend);
289 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200291 if (acpar.hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return NF_DROP;
293 else
294 return verdict;
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/* All zeroes == unconditional rule. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100298static inline bool unconditional(const struct arpt_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jan Engelhardt47901dc2009-07-09 23:00:19 +0200300 static const struct arpt_arp uncond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Florian Westphal54d83fc2016-03-22 18:02:52 +0100302 return e->target_offset == sizeof(struct arpt_entry) &&
303 memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/* Figures out from what hook each rule can be called: returns 0 if
307 * there are loops. Puts hook bitmask in comefrom.
308 */
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200309static int mark_source_chains(const struct xt_table_info *newinfo,
Florian Westphalf4dc7772016-07-14 17:51:26 +0200310 unsigned int valid_hooks, void *entry0,
311 unsigned int *offsets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned int hook;
314
315 /* No recursion; use packet counter to save back ptrs (reset
316 * to 0 as we leave), and comefrom to save source hook bitmask.
317 */
318 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
319 unsigned int pos = newinfo->hook_entry[hook];
320 struct arpt_entry *e
Eric Dumazet31836062005-12-13 23:13:48 -0800321 = (struct arpt_entry *)(entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!(valid_hooks & (1 << hook)))
324 continue;
325
326 /* Set initial back pointer. */
327 e->counters.pcnt = pos;
328
329 for (;;) {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200330 const struct xt_standard_target *t
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200331 = (void *)arpt_get_target_c(e);
Al Viroe1b4b9f2006-12-12 00:29:52 -0800332 int visited = e->comefrom & (1 << hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200334 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 e->comefrom
338 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
339
340 /* Unconditional return/END. */
Florian Westphal54d83fc2016-03-22 18:02:52 +0100341 if ((unconditional(e) &&
Joe Perches3666ed12009-11-23 23:17:06 +0100342 (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200343 XT_STANDARD_TARGET) == 0) &&
Florian Westphal54d83fc2016-03-22 18:02:52 +0100344 t->verdict < 0) || visited) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned int oldpos, size;
346
Patrick McHardy1f9352a2009-03-25 19:26:35 +0100347 if ((strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200348 XT_STANDARD_TARGET) == 0) &&
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200349 t->verdict < -NF_MAX_VERDICT - 1)
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800350 return 0;
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Return: backtrack through the last
353 * big jump.
354 */
355 do {
356 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
357 oldpos = pos;
358 pos = e->counters.pcnt;
359 e->counters.pcnt = 0;
360
361 /* We're at the start. */
362 if (pos == oldpos)
363 goto next;
364
365 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800366 (entry0 + pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } while (oldpos == pos + e->next_offset);
368
369 /* Move along one */
370 size = e->next_offset;
371 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800372 (entry0 + pos + size);
Florian Westphalf24e2302016-04-01 14:17:21 +0200373 if (pos + size >= newinfo->size)
374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 e->counters.pcnt = pos;
376 pos += size;
377 } else {
378 int newpos = t->verdict;
379
380 if (strcmp(t->target.u.user.name,
Jan Engelhardt243bf6e2010-10-13 16:28:00 +0200381 XT_STANDARD_TARGET) == 0 &&
Joe Perches3666ed12009-11-23 23:17:06 +0100382 newpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* This a jump; chase it. */
Florian Westphalf4dc7772016-07-14 17:51:26 +0200384 if (!xt_find_jump_offset(offsets, newpos,
385 newinfo->number))
386 return 0;
Florian Westphal36472342016-04-01 14:17:22 +0200387 e = (struct arpt_entry *)
388 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else {
390 /* ... this is a fallthru */
391 newpos = pos + e->next_offset;
Florian Westphalf24e2302016-04-01 14:17:21 +0200392 if (newpos >= newinfo->size)
393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 e = (struct arpt_entry *)
Eric Dumazet31836062005-12-13 23:13:48 -0800396 (entry0 + newpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 e->counters.pcnt = pos;
398 pos = newpos;
399 }
400 }
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200401next: ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return 1;
404}
405
Florian Westphal76c146c2019-12-27 01:33:10 +0100406static int check_target(struct arpt_entry *e, struct net *net, const char *name)
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800407{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200408 struct xt_entry_target *t = arpt_get_target(e);
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200409 struct xt_tgchk_param par = {
Florian Westphal76c146c2019-12-27 01:33:10 +0100410 .net = net,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200411 .table = name,
412 .entryinfo = e,
413 .target = t->u.kernel.target,
414 .targinfo = t->data,
415 .hook_mask = e->comefrom,
Jan Engelhardt916a9172008-10-08 11:35:20 +0200416 .family = NFPROTO_ARP,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200417 };
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800418
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200419 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800420}
421
Florian Westphal76c146c2019-12-27 01:33:10 +0100422static int
423find_check_entry(struct arpt_entry *e, struct net *net, const char *name,
424 unsigned int size,
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100425 struct xt_percpu_counter_alloc_state *alloc_state)
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800426{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200427 struct xt_entry_target *t;
Jan Engelhardt95eea852008-04-14 11:15:43 +0200428 struct xt_target *target;
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800429 int ret;
430
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100431 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
Florian Westphal71ae0df2015-06-11 01:34:54 +0200432 return -ENOMEM;
433
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800434 t = arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200435 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
436 t->u.user.revision);
437 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200438 ret = PTR_ERR(target);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800439 goto out;
440 }
441 t->u.kernel.target = target;
442
Florian Westphal76c146c2019-12-27 01:33:10 +0100443 ret = check_target(e, net, name);
Patrick McHardyfb5b6092007-12-17 21:56:33 -0800444 if (ret)
445 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
Patrick McHardy3cdc7c92006-03-20 18:00:36 -0800447err:
448 module_put(t->u.kernel.target->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449out:
Florian Westphal61346e22016-11-22 14:44:17 +0100450 xt_percpu_counter_free(&e->counters);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ret;
453}
454
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200455static bool check_underflow(const struct arpt_entry *e)
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200456{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200457 const struct xt_entry_target *t;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200458 unsigned int verdict;
459
Florian Westphal54d83fc2016-03-22 18:02:52 +0100460 if (!unconditional(e))
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200461 return false;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200462 t = arpt_get_target_c(e);
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200463 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
464 return false;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200465 verdict = ((struct xt_standard_target *)t)->verdict;
Jan Engelhardte2fe35c2009-07-18 15:22:30 +0200466 verdict = -verdict - 1;
467 return verdict == NF_DROP || verdict == NF_ACCEPT;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static inline int check_entry_size_and_hooks(struct arpt_entry *e,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800471 struct xt_table_info *newinfo,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200472 const unsigned char *base,
473 const unsigned char *limit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 const unsigned int *hook_entries,
475 const unsigned int *underflows,
Jan Engelhardt05595182010-02-24 18:33:43 +0100476 unsigned int valid_hooks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned int h;
Florian Westphalbdf533d2016-03-22 18:02:49 +0100479 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Joe Perches3666ed12009-11-23 23:17:06 +0100481 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +0100482 (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200483 (unsigned char *)e + e->next_offset > limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (e->next_offset
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200487 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Florian Westphalaa412ba2016-04-01 14:17:24 +0200490 if (!arp_checkentry(&e->arp))
491 return -EINVAL;
492
Florian Westphalce683e52016-04-01 14:17:28 +0200493 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
494 e->next_offset);
Florian Westphalbdf533d2016-03-22 18:02:49 +0100495 if (err)
496 return err;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Check hooks & underflows */
499 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
Jan Engelhardta7d51732009-07-18 14:52:58 +0200500 if (!(valid_hooks & (1 << h)))
501 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if ((unsigned char *)e - base == hook_entries[h])
503 newinfo->hook_entry[h] = hook_entries[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200504 if ((unsigned char *)e - base == underflows[h]) {
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200505 if (!check_underflow(e))
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200506 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 newinfo->underflow[h] = underflows[h];
Jan Engelhardt90e7d4a2009-07-09 22:54:53 +0200509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Clear counters and comefrom */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800513 e->counters = ((struct xt_counters) { 0, 0 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 e->comefrom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516}
517
Florian Westphal2564ab92020-01-11 23:19:53 +0100518static void cleanup_entry(struct arpt_entry *e, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200520 struct xt_tgdtor_param par;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200521 struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 t = arpt_get_target(e);
Florian Westphal2564ab92020-01-11 23:19:53 +0100524 par.net = net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200525 par.target = t->u.kernel.target;
526 par.targinfo = t->data;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200527 par.family = NFPROTO_ARP;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200528 if (par.target->destroy != NULL)
529 par.target->destroy(&par);
530 module_put(par.target->me);
Florian Westphal61346e22016-11-22 14:44:17 +0100531 xt_percpu_counter_free(&e->counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/* Checks and translates the user-supplied table segment (held in
535 * newinfo).
536 */
Florian Westphal76c146c2019-12-27 01:33:10 +0100537static int translate_table(struct net *net,
538 struct xt_table_info *newinfo,
539 void *entry0,
Ian Morris6c282552015-10-14 23:17:06 +0100540 const struct arpt_replace *repl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100542 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100543 struct arpt_entry *iter;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200544 unsigned int *offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned int i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100546 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jan Engelhardt0f234212010-02-24 18:36:04 +0100548 newinfo->size = repl->size;
549 newinfo->number = repl->num_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Init all hooks to impossible value. */
552 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
553 newinfo->hook_entry[i] = 0xFFFFFFFF;
554 newinfo->underflow[i] = 0xFFFFFFFF;
555 }
556
Florian Westphalf4dc7772016-07-14 17:51:26 +0200557 offsets = xt_alloc_entry_offsets(newinfo->number);
558 if (!offsets)
559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 i = 0;
561
562 /* Walk through entries, checking offsets. */
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100563 xt_entry_foreach(iter, entry0, newinfo->size) {
564 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +0100565 entry0 + repl->size,
566 repl->hook_entry,
567 repl->underflow,
568 repl->valid_hooks);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100569 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200570 goto out_free;
571 if (i < repl->num_entries)
572 offsets[i] = (void *)iter - entry0;
Jan Engelhardt05595182010-02-24 18:33:43 +0100573 ++i;
Florian Westphal98dbbfc2015-08-26 23:20:51 +0200574 if (strcmp(arpt_get_target(iter)->u.user.name,
575 XT_ERROR_TARGET) == 0)
576 ++newinfo->stacksize;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (ret != 0)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200579 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Florian Westphalf4dc7772016-07-14 17:51:26 +0200581 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200582 if (i != repl->num_entries)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200583 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Check hooks all assigned */
586 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
587 /* Only hooks which are valid */
Jan Engelhardt0f234212010-02-24 18:36:04 +0100588 if (!(repl->valid_hooks & (1 << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 continue;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200590 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200591 goto out_free;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200592 if (newinfo->underflow[i] == 0xFFFFFFFF)
Florian Westphalf4dc7772016-07-14 17:51:26 +0200593 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Florian Westphalf4dc7772016-07-14 17:51:26 +0200596 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
597 ret = -ELOOP;
598 goto out_free;
599 }
600 kvfree(offsets);
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Finally, each sanity check must pass */
603 i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100604 xt_entry_foreach(iter, entry0, newinfo->size) {
Florian Westphal76c146c2019-12-27 01:33:10 +0100605 ret = find_check_entry(iter, net, repl->name, repl->size,
Florian Westphaldb6a0cb2016-11-22 14:44:19 +0100606 &alloc_state);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100607 if (ret != 0)
608 break;
Jan Engelhardt05595182010-02-24 18:33:43 +0100609 ++i;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800612 if (ret != 0) {
Jan Engelhardt05595182010-02-24 18:33:43 +0100613 xt_entry_foreach(iter, entry0, newinfo->size) {
614 if (i-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100615 break;
Florian Westphal2564ab92020-01-11 23:19:53 +0100616 cleanup_entry(iter, net);
Jan Engelhardt05595182010-02-24 18:33:43 +0100617 }
Dmitry Mishin74c9c0c2006-12-05 13:43:50 -0800618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return ret;
Florian Westphalf4dc7772016-07-14 17:51:26 +0200622 out_free:
623 kvfree(offsets);
624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Harald Welte2e4e6a12006-01-12 13:30:04 -0800627static void get_counters(const struct xt_table_info *t,
628 struct xt_counters counters[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100630 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned int cpu;
632 unsigned int i;
633
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700634 for_each_possible_cpu(cpu) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200635 seqcount_t *s = &per_cpu(xt_recseq, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200638 xt_entry_foreach(iter, t->entries, t->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +0200639 struct xt_counters *tmp;
Eric Dumazet83723d62011-01-10 20:11:38 +0100640 u64 bcnt, pcnt;
641 unsigned int start;
642
Florian Westphal71ae0df2015-06-11 01:34:54 +0200643 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
Eric Dumazet83723d62011-01-10 20:11:38 +0100644 do {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200645 start = read_seqcount_begin(s);
Florian Westphal71ae0df2015-06-11 01:34:54 +0200646 bcnt = tmp->bcnt;
647 pcnt = tmp->pcnt;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200648 } while (read_seqcount_retry(s, start));
Eric Dumazet83723d62011-01-10 20:11:38 +0100649
650 ADD_COUNTER(counters[i], bcnt, pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +0100651 ++i;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Stephen Hemminger78454472009-02-20 10:35:32 +0100654}
655
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200656static struct xt_counters *alloc_counters(const struct xt_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Patrick McHardy27e2c262007-12-17 21:56:48 -0800658 unsigned int countersize;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800659 struct xt_counters *counters;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200660 const struct xt_table_info *private = table->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* We need atomic snapshot of counters: rest doesn't change
663 * (other than comefrom, which userspace doesn't care
664 * about).
665 */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800666 countersize = sizeof(struct xt_counters) * private->number;
Eric Dumazet83723d62011-01-10 20:11:38 +0100667 counters = vzalloc(countersize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (counters == NULL)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700670 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700672 get_counters(private, counters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Patrick McHardy27e2c262007-12-17 21:56:48 -0800674 return counters;
675}
676
677static int copy_entries_to_user(unsigned int total_size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200678 const struct xt_table *table,
Patrick McHardy27e2c262007-12-17 21:56:48 -0800679 void __user *userptr)
680{
681 unsigned int off, num;
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200682 const struct arpt_entry *e;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800683 struct xt_counters *counters;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200684 struct xt_table_info *private = table->private;
Patrick McHardy27e2c262007-12-17 21:56:48 -0800685 int ret = 0;
686 void *loc_cpu_entry;
687
688 counters = alloc_counters(table);
689 if (IS_ERR(counters))
690 return PTR_ERR(counters);
691
Florian Westphal482cfc32015-06-11 01:34:55 +0200692 loc_cpu_entry = private->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800693 /* ... then copy entire thing ... */
694 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ret = -EFAULT;
696 goto free_counters;
697 }
698
699 /* FIXME: use iterator macros --RR */
700 /* ... then go back and fix counters and names */
701 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200702 const struct xt_entry_target *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Eric Dumazet31836062005-12-13 23:13:48 -0800704 e = (struct arpt_entry *)(loc_cpu_entry + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (copy_to_user(userptr + off
706 + offsetof(struct arpt_entry, counters),
707 &counters[num],
708 sizeof(counters[num])) != 0) {
709 ret = -EFAULT;
710 goto free_counters;
711 }
712
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200713 t = arpt_get_target_c(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (copy_to_user(userptr + off + e->target_offset
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200715 + offsetof(struct xt_entry_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 u.user.name),
717 t->u.kernel.target->name,
718 strlen(t->u.kernel.target->name)+1) != 0) {
719 ret = -EFAULT;
720 goto free_counters;
721 }
722 }
723
724 free_counters:
725 vfree(counters);
726 return ret;
727}
728
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800729#ifdef CONFIG_COMPAT
Jan Engelhardt739674f2009-06-26 08:23:19 +0200730static void compat_standard_from_user(void *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800731{
732 int v = *(compat_int_t *)src;
733
734 if (v > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200735 v += xt_compat_calc_jump(NFPROTO_ARP, v);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800736 memcpy(dst, &v, sizeof(v));
737}
738
Jan Engelhardt739674f2009-06-26 08:23:19 +0200739static int compat_standard_to_user(void __user *dst, const void *src)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800740{
741 compat_int_t cv = *(int *)src;
742
743 if (cv > 0)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200744 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800745 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
746}
747
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200748static int compat_calc_entry(const struct arpt_entry *e,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800749 const struct xt_table_info *info,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200750 const void *base, struct xt_table_info *newinfo)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800751{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200752 const struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800753 unsigned int entry_offset;
754 int off, i, ret;
755
756 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
757 entry_offset = (void *)e - base;
758
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200759 t = arpt_get_target_c(e);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800760 off += xt_compat_target_offset(t->u.kernel.target);
761 newinfo->size -= off;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200762 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800763 if (ret)
764 return ret;
765
766 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
767 if (info->hook_entry[i] &&
768 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
769 newinfo->hook_entry[i] -= off;
770 if (info->underflow[i] &&
771 (e < (struct arpt_entry *)(base + info->underflow[i])))
772 newinfo->underflow[i] -= off;
773 }
774 return 0;
775}
776
777static int compat_table_info(const struct xt_table_info *info,
778 struct xt_table_info *newinfo)
779{
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100780 struct arpt_entry *iter;
Eric Dumazet711bdde2015-06-15 09:57:30 -0700781 const void *loc_cpu_entry;
Jan Engelhardt05595182010-02-24 18:33:43 +0100782 int ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800783
784 if (!newinfo || !info)
785 return -EINVAL;
786
Florian Westphal482cfc32015-06-11 01:34:55 +0200787 /* we dont care about newinfo->entries */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800788 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
789 newinfo->initial_entries = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +0200790 loc_cpu_entry = info->entries;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100791 xt_compat_init_offsets(NFPROTO_ARP, info->number);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100792 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
793 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
794 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +0100795 return ret;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100796 }
Jan Engelhardt05595182010-02-24 18:33:43 +0100797 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800798}
799#endif
800
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200801static int get_info(struct net *net, void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +0100802 const int *len, int compat)
Patrick McHardy41acd972007-12-17 22:26:24 -0800803{
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200804 char name[XT_TABLE_MAXNAMELEN];
Jan Engelhardt4abff072008-04-14 11:15:43 +0200805 struct xt_table *t;
Patrick McHardy41acd972007-12-17 22:26:24 -0800806 int ret;
807
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200808 if (*len != sizeof(struct arpt_getinfo))
Patrick McHardy41acd972007-12-17 22:26:24 -0800809 return -EINVAL;
Patrick McHardy41acd972007-12-17 22:26:24 -0800810
811 if (copy_from_user(name, user, sizeof(name)) != 0)
812 return -EFAULT;
813
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200814 name[XT_TABLE_MAXNAMELEN-1] = '\0';
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800815#ifdef CONFIG_COMPAT
816 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200817 xt_compat_lock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800818#endif
Jan Engelhardtee999d82008-10-08 11:35:01 +0200819 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardy41acd972007-12-17 22:26:24 -0800820 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000821 if (!IS_ERR_OR_NULL(t)) {
Patrick McHardy41acd972007-12-17 22:26:24 -0800822 struct arpt_getinfo info;
Jan Engelhardt5452e422008-04-14 11:15:35 +0200823 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800824#ifdef CONFIG_COMPAT
Alexey Dobriyan14c7dbe2010-02-08 11:17:43 -0800825 struct xt_table_info tmp;
826
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800827 if (compat) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800828 ret = compat_table_info(private, &tmp);
Jan Engelhardtee999d82008-10-08 11:35:01 +0200829 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800830 private = &tmp;
831 }
832#endif
Vasiliy Kulikov1a8b7a62010-11-03 08:44:12 +0100833 memset(&info, 0, sizeof(info));
Patrick McHardy41acd972007-12-17 22:26:24 -0800834 info.valid_hooks = t->valid_hooks;
835 memcpy(info.hook_entry, private->hook_entry,
836 sizeof(info.hook_entry));
837 memcpy(info.underflow, private->underflow,
838 sizeof(info.underflow));
839 info.num_entries = private->number;
840 info.size = private->size;
841 strcpy(info.name, name);
842
843 if (copy_to_user(user, &info, *len) != 0)
844 ret = -EFAULT;
845 else
846 ret = 0;
847 xt_table_unlock(t);
848 module_put(t->me);
849 } else
850 ret = t ? PTR_ERR(t) : -ENOENT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800851#ifdef CONFIG_COMPAT
852 if (compat)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200853 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800854#endif
Patrick McHardy41acd972007-12-17 22:26:24 -0800855 return ret;
856}
857
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800858static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200859 const int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 int ret;
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800862 struct arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200863 struct xt_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200865 if (*len < sizeof(get))
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800866 return -EINVAL;
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800867 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
868 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200869 if (*len != sizeof(struct arpt_get_entries) + get.size)
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800870 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200871
Pablo Neira Ayusob301f252016-03-24 21:29:53 +0100872 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800873
Jan Engelhardtee999d82008-10-08 11:35:01 +0200874 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000875 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +0200876 const struct xt_table_info *private = t->private;
877
Patrick McHardy11f6dff2007-12-17 22:26:38 -0800878 if (get.size == private->size)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800879 ret = copy_entries_to_user(private->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200881 else
Patrick McHardy544473c2008-04-14 11:15:45 +0200882 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +0200883
Harald Welte6b7d31f2005-10-26 09:34:24 +0200884 module_put(t->me);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800885 xt_table_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else
Harald Welte6b7d31f2005-10-26 09:34:24 +0200887 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return ret;
890}
891
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800892static int __do_replace(struct net *net, const char *name,
893 unsigned int valid_hooks,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800894 struct xt_table_info *newinfo,
895 unsigned int num_counters,
896 void __user *counters_ptr)
897{
898 int ret;
Jan Engelhardt4abff072008-04-14 11:15:43 +0200899 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800900 struct xt_table_info *oldinfo;
901 struct xt_counters *counters;
902 void *loc_cpu_old_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100903 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800904
905 ret = 0;
Eric Dumazet83723d62011-01-10 20:11:38 +0100906 counters = vzalloc(num_counters * sizeof(struct xt_counters));
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800907 if (!counters) {
908 ret = -ENOMEM;
909 goto out;
910 }
911
Jan Engelhardtee999d82008-10-08 11:35:01 +0200912 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800913 "arptable_%s", name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +0000914 if (IS_ERR_OR_NULL(t)) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800915 ret = t ? PTR_ERR(t) : -ENOENT;
916 goto free_newinfo_counters_untrans;
917 }
918
919 /* You lied! */
920 if (valid_hooks != t->valid_hooks) {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800921 ret = -EINVAL;
922 goto put_module;
923 }
924
925 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
926 if (!oldinfo)
927 goto put_module;
928
929 /* Update module usage count based on number of rules */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800930 if ((oldinfo->number > oldinfo->initial_entries) ||
931 (newinfo->number <= oldinfo->initial_entries))
932 module_put(t->me);
933 if ((oldinfo->number > oldinfo->initial_entries) &&
934 (newinfo->number <= oldinfo->initial_entries))
935 module_put(t->me);
936
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700937 /* Get the old counters, and synchronize with replace */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800938 get_counters(oldinfo, counters);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700939
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800940 /* Decrease module usage counts and free resource */
Florian Westphal482cfc32015-06-11 01:34:55 +0200941 loc_cpu_old_entry = oldinfo->entries;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100942 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
Florian Westphal2564ab92020-01-11 23:19:53 +0100943 cleanup_entry(iter, net);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800944
945 xt_free_table_info(oldinfo);
946 if (copy_to_user(counters_ptr, counters,
Thomas Grafc58dd2d2014-04-04 17:57:45 +0200947 sizeof(struct xt_counters) * num_counters) != 0) {
948 /* Silent error, can't fail, new table is already in place */
949 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
950 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800951 vfree(counters);
952 xt_table_unlock(t);
953 return ret;
954
955 put_module:
956 module_put(t->me);
957 xt_table_unlock(t);
958 free_newinfo_counters_untrans:
959 vfree(counters);
960 out:
961 return ret;
962}
963
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +0200964static int do_replace(struct net *net, const void __user *user,
Ian Morris6c282552015-10-14 23:17:06 +0100965 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 int ret;
968 struct arpt_replace tmp;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800969 struct xt_table_info *newinfo;
970 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100971 struct arpt_entry *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
974 return -EFAULT;
975
Kirill Korotaevee4bb812006-02-04 02:16:56 -0800976 /* overflow check */
Kirill Korotaevee4bb812006-02-04 02:16:56 -0800977 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
978 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -0400979 if (tmp.num_counters == 0)
980 return -EINVAL;
981
Vasiliy Kulikov42eab942011-03-15 13:35:21 +0100982 tmp.name[sizeof(tmp.name)-1] = 0;
Kirill Korotaevee4bb812006-02-04 02:16:56 -0800983
Harald Welte2e4e6a12006-01-12 13:30:04 -0800984 newinfo = xt_alloc_table_info(tmp.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (!newinfo)
986 return -ENOMEM;
987
Florian Westphal482cfc32015-06-11 01:34:55 +0200988 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -0800989 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 tmp.size) != 0) {
991 ret = -EFAULT;
992 goto free_newinfo;
993 }
994
Florian Westphal76c146c2019-12-27 01:33:10 +0100995 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (ret != 0)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800997 goto free_newinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Alexey Dobriyan79df3412008-01-31 04:04:32 -0800999 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001000 tmp.num_counters, tmp.counters);
1001 if (ret)
1002 goto free_newinfo_untrans;
1003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001005 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001006 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Florian Westphal2564ab92020-01-11 23:19:53 +01001007 cleanup_entry(iter, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 free_newinfo:
Harald Welte2e4e6a12006-01-12 13:30:04 -08001009 xt_free_table_info(newinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return ret;
1011}
1012
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001013static int do_add_counters(struct net *net, const void __user *user,
1014 unsigned int len, int compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Florian Westphal482cfc32015-06-11 01:34:55 +02001016 unsigned int i;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001017 struct xt_counters_info tmp;
1018 struct xt_counters *paddc;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001019 struct xt_table *t;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001020 const struct xt_table_info *private;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001021 int ret = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001022 struct arpt_entry *iter;
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001023 unsigned int addend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Florian Westphald7591f02016-04-01 15:37:59 +02001025 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1026 if (IS_ERR(paddc))
1027 return PTR_ERR(paddc);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001028
Florian Westphald7591f02016-04-01 15:37:59 +02001029 t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001030 if (IS_ERR_OR_NULL(t)) {
Harald Welte6b7d31f2005-10-26 09:34:24 +02001031 ret = t ? PTR_ERR(t) : -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto free;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001035 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001036 private = t->private;
Florian Westphald7591f02016-04-01 15:37:59 +02001037 if (private->number != tmp.num_counters) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ret = -EINVAL;
1039 goto unlock_up_free;
1040 }
1041
1042 i = 0;
Florian Westphal482cfc32015-06-11 01:34:55 +02001043
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001044 addend = xt_write_recseq_begin();
Florian Westphal482cfc32015-06-11 01:34:55 +02001045 xt_entry_foreach(iter, private->entries, private->size) {
Florian Westphal71ae0df2015-06-11 01:34:54 +02001046 struct xt_counters *tmp;
1047
1048 tmp = xt_get_this_cpu_counter(&iter->counters);
1049 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
Jan Engelhardt05595182010-02-24 18:33:43 +01001050 ++i;
1051 }
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001052 xt_write_recseq_end(addend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unlock_up_free:
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001054 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001055 xt_table_unlock(t);
Harald Welte6b7d31f2005-10-26 09:34:24 +02001056 module_put(t->me);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 free:
1058 vfree(paddc);
1059
1060 return ret;
1061}
1062
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001063#ifdef CONFIG_COMPAT
Florian Westphal8dddd322016-04-01 14:17:32 +02001064struct compat_arpt_replace {
1065 char name[XT_TABLE_MAXNAMELEN];
1066 u32 valid_hooks;
1067 u32 num_entries;
1068 u32 size;
1069 u32 hook_entry[NF_ARP_NUMHOOKS];
1070 u32 underflow[NF_ARP_NUMHOOKS];
1071 u32 num_counters;
1072 compat_uptr_t counters;
1073 struct compat_arpt_entry entries[0];
1074};
1075
Jan Engelhardt05595182010-02-24 18:33:43 +01001076static inline void compat_release_entry(struct compat_arpt_entry *e)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001077{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001078 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001079
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001080 t = compat_arpt_get_target(e);
1081 module_put(t->u.kernel.target->me);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001082}
1083
Florian Westphal09d96862016-04-01 14:17:34 +02001084static int
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001085check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1086 struct xt_table_info *newinfo,
1087 unsigned int *size,
Jan Engelhardtd5d1baa2009-06-26 07:51:59 +02001088 const unsigned char *base,
Florian Westphal09d96862016-04-01 14:17:34 +02001089 const unsigned char *limit)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001090{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001091 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001092 struct xt_target *target;
1093 unsigned int entry_offset;
Florian Westphal09d96862016-04-01 14:17:34 +02001094 int ret, off;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001095
Joe Perches3666ed12009-11-23 23:17:06 +01001096 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
Florian Westphal6e94e0c2016-03-22 18:02:50 +01001097 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001098 (unsigned char *)e + e->next_offset > limit)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001099 return -EINVAL;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001100
1101 if (e->next_offset < sizeof(struct compat_arpt_entry) +
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001102 sizeof(struct compat_xt_entry_target))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001103 return -EINVAL;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001104
Florian Westphalaa412ba2016-04-01 14:17:24 +02001105 if (!arp_checkentry(&e->arp))
1106 return -EINVAL;
1107
Florian Westphalce683e52016-04-01 14:17:28 +02001108 ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
Florian Westphalfc1221b2016-04-01 14:17:26 +02001109 e->next_offset);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001110 if (ret)
1111 return ret;
1112
1113 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1114 entry_offset = (void *)e - (void *)base;
1115
1116 t = compat_arpt_get_target(e);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001117 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1118 t->u.user.revision);
1119 if (IS_ERR(target)) {
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +02001120 ret = PTR_ERR(target);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001121 goto out;
1122 }
1123 t->u.kernel.target = target;
1124
1125 off += xt_compat_target_offset(target);
1126 *size += off;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001127 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001128 if (ret)
1129 goto release_target;
1130
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001131 return 0;
1132
1133release_target:
1134 module_put(t->u.kernel.target->me);
1135out:
1136 return ret;
1137}
1138
Florian Westphal01883462016-04-01 14:17:33 +02001139static void
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001140compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
Florian Westphal8dddd322016-04-01 14:17:32 +02001141 unsigned int *size,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001142 struct xt_table_info *newinfo, unsigned char *base)
1143{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001144 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001145 struct xt_target *target;
1146 struct arpt_entry *de;
1147 unsigned int origsize;
Florian Westphal01883462016-04-01 14:17:33 +02001148 int h;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001149
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001150 origsize = *size;
1151 de = (struct arpt_entry *)*dstptr;
1152 memcpy(de, e, sizeof(struct arpt_entry));
1153 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1154
1155 *dstptr += sizeof(struct arpt_entry);
1156 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1157
1158 de->target_offset = e->target_offset - (origsize - *size);
1159 t = compat_arpt_get_target(e);
1160 target = t->u.kernel.target;
1161 xt_compat_target_from_user(t, dstptr, size);
1162
1163 de->next_offset = e->next_offset - (origsize - *size);
1164 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1165 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1166 newinfo->hook_entry[h] -= origsize - *size;
1167 if ((unsigned char *)de - base < newinfo->underflow[h])
1168 newinfo->underflow[h] -= origsize - *size;
1169 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001170}
1171
Florian Westphal76c146c2019-12-27 01:33:10 +01001172static int translate_compat_table(struct net *net,
1173 struct xt_table_info **pinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001174 void **pentry0,
Florian Westphal8dddd322016-04-01 14:17:32 +02001175 const struct compat_arpt_replace *compatr)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001176{
1177 unsigned int i, j;
1178 struct xt_table_info *newinfo, *info;
1179 void *pos, *entry0, *entry1;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001180 struct compat_arpt_entry *iter0;
Florian Westphal09d96862016-04-01 14:17:34 +02001181 struct arpt_replace repl;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001182 unsigned int size;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001183 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001184
1185 info = *pinfo;
1186 entry0 = *pentry0;
Florian Westphal8dddd322016-04-01 14:17:32 +02001187 size = compatr->size;
1188 info->number = compatr->num_entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001189
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001190 j = 0;
Jan Engelhardtee999d82008-10-08 11:35:01 +02001191 xt_compat_lock(NFPROTO_ARP);
Florian Westphal8dddd322016-04-01 14:17:32 +02001192 xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001193 /* Walk through entries, checking offsets. */
Florian Westphal8dddd322016-04-01 14:17:32 +02001194 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001195 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001196 entry0,
Florian Westphal09d96862016-04-01 14:17:34 +02001197 entry0 + compatr->size);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001198 if (ret != 0)
Jan Engelhardt05595182010-02-24 18:33:43 +01001199 goto out_unlock;
1200 ++j;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001201 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001202
1203 ret = -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001204 if (j != compatr->num_entries)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001205 goto out_unlock;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001206
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001207 ret = -ENOMEM;
1208 newinfo = xt_alloc_table_info(size);
1209 if (!newinfo)
1210 goto out_unlock;
1211
Florian Westphal8dddd322016-04-01 14:17:32 +02001212 newinfo->number = compatr->num_entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001213 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
Hongxu Jia17a49cd2016-11-29 21:56:26 -05001214 newinfo->hook_entry[i] = compatr->hook_entry[i];
1215 newinfo->underflow[i] = compatr->underflow[i];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001216 }
Florian Westphal482cfc32015-06-11 01:34:55 +02001217 entry1 = newinfo->entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001218 pos = entry1;
Florian Westphal8dddd322016-04-01 14:17:32 +02001219 size = compatr->size;
Florian Westphal01883462016-04-01 14:17:33 +02001220 xt_entry_foreach(iter0, entry0, compatr->size)
1221 compat_copy_entry_from_user(iter0, &pos, &size,
1222 newinfo, entry1);
Florian Westphal09d96862016-04-01 14:17:34 +02001223
1224 /* all module references in entry0 are now gone */
1225
Jan Engelhardtee999d82008-10-08 11:35:01 +02001226 xt_compat_flush_offsets(NFPROTO_ARP);
1227 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001228
Florian Westphal09d96862016-04-01 14:17:34 +02001229 memcpy(&repl, compatr, sizeof(*compatr));
1230
1231 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1232 repl.hook_entry[i] = newinfo->hook_entry[i];
1233 repl.underflow[i] = newinfo->underflow[i];
1234 }
1235
1236 repl.num_counters = 0;
1237 repl.counters = NULL;
1238 repl.size = newinfo->size;
Florian Westphal76c146c2019-12-27 01:33:10 +01001239 ret = translate_table(net, newinfo, entry1, &repl);
Florian Westphal09d96862016-04-01 14:17:34 +02001240 if (ret)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001241 goto free_newinfo;
1242
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001243 *pinfo = newinfo;
1244 *pentry0 = entry1;
1245 xt_free_table_info(info);
1246 return 0;
1247
1248free_newinfo:
1249 xt_free_table_info(newinfo);
Florian Westphal09d96862016-04-01 14:17:34 +02001250 return ret;
1251out_unlock:
1252 xt_compat_flush_offsets(NFPROTO_ARP);
1253 xt_compat_unlock(NFPROTO_ARP);
Florian Westphal8dddd322016-04-01 14:17:32 +02001254 xt_entry_foreach(iter0, entry0, compatr->size) {
Jan Engelhardt05595182010-02-24 18:33:43 +01001255 if (j-- == 0)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001256 break;
Jan Engelhardt05595182010-02-24 18:33:43 +01001257 compat_release_entry(iter0);
1258 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001259 return ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001260}
1261
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001262static int compat_do_replace(struct net *net, void __user *user,
1263 unsigned int len)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001264{
1265 int ret;
1266 struct compat_arpt_replace tmp;
1267 struct xt_table_info *newinfo;
1268 void *loc_cpu_entry;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001269 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001270
1271 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1272 return -EFAULT;
1273
1274 /* overflow check */
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001275 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1276 return -ENOMEM;
Dave Jones1086bbe2015-05-19 20:55:17 -04001277 if (tmp.num_counters == 0)
1278 return -EINVAL;
1279
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001280 tmp.name[sizeof(tmp.name)-1] = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001281
1282 newinfo = xt_alloc_table_info(tmp.size);
1283 if (!newinfo)
1284 return -ENOMEM;
1285
Florian Westphal482cfc32015-06-11 01:34:55 +02001286 loc_cpu_entry = newinfo->entries;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001287 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1288 ret = -EFAULT;
1289 goto free_newinfo;
1290 }
1291
Florian Westphal76c146c2019-12-27 01:33:10 +01001292 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001293 if (ret != 0)
1294 goto free_newinfo;
1295
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001296 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001297 tmp.num_counters, compat_ptr(tmp.counters));
1298 if (ret)
1299 goto free_newinfo_untrans;
1300 return 0;
1301
1302 free_newinfo_untrans:
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001303 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
Florian Westphal2564ab92020-01-11 23:19:53 +01001304 cleanup_entry(iter, net);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001305 free_newinfo:
1306 xt_free_table_info(newinfo);
1307 return ret;
1308}
1309
1310static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1311 unsigned int len)
1312{
1313 int ret;
1314
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001315 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001316 return -EPERM;
1317
1318 switch (cmd) {
1319 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001320 ret = compat_do_replace(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001321 break;
1322
1323 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001324 ret = do_add_counters(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001325 break;
1326
1327 default:
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001328 ret = -EINVAL;
1329 }
1330
1331 return ret;
1332}
1333
1334static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1335 compat_uint_t *size,
1336 struct xt_counters *counters,
Jan Engelhardt05595182010-02-24 18:33:43 +01001337 unsigned int i)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001338{
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +02001339 struct xt_entry_target *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001340 struct compat_arpt_entry __user *ce;
1341 u_int16_t target_offset, next_offset;
1342 compat_uint_t origsize;
1343 int ret;
1344
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001345 origsize = *size;
1346 ce = (struct compat_arpt_entry __user *)*dstptr;
Jan Engelhardt05595182010-02-24 18:33:43 +01001347 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1348 copy_to_user(&ce->counters, &counters[i],
1349 sizeof(counters[i])) != 0)
1350 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001351
1352 *dstptr += sizeof(struct compat_arpt_entry);
1353 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1354
1355 target_offset = e->target_offset - (origsize - *size);
1356
1357 t = arpt_get_target(e);
1358 ret = xt_compat_target_to_user(t, dstptr, size);
1359 if (ret)
Jan Engelhardt05595182010-02-24 18:33:43 +01001360 return ret;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001361 next_offset = e->next_offset - (origsize - *size);
Jan Engelhardt05595182010-02-24 18:33:43 +01001362 if (put_user(target_offset, &ce->target_offset) != 0 ||
1363 put_user(next_offset, &ce->next_offset) != 0)
1364 return -EFAULT;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001365 return 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001366}
1367
1368static int compat_copy_entries_to_user(unsigned int total_size,
Jan Engelhardt4abff072008-04-14 11:15:43 +02001369 struct xt_table *table,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001370 void __user *userptr)
1371{
1372 struct xt_counters *counters;
Jan Engelhardt5452e422008-04-14 11:15:35 +02001373 const struct xt_table_info *private = table->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001374 void __user *pos;
1375 unsigned int size;
1376 int ret = 0;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001377 unsigned int i = 0;
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001378 struct arpt_entry *iter;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001379
1380 counters = alloc_counters(table);
1381 if (IS_ERR(counters))
1382 return PTR_ERR(counters);
1383
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001384 pos = userptr;
1385 size = total_size;
Florian Westphal482cfc32015-06-11 01:34:55 +02001386 xt_entry_foreach(iter, private->entries, total_size) {
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001387 ret = compat_copy_entry_to_user(iter, &pos,
Jan Engelhardt6b4ff2d2010-02-26 17:53:31 +01001388 &size, counters, i++);
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +01001389 if (ret != 0)
1390 break;
1391 }
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001392 vfree(counters);
1393 return ret;
1394}
1395
1396struct compat_arpt_get_entries {
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001397 char name[XT_TABLE_MAXNAMELEN];
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001398 compat_uint_t size;
1399 struct compat_arpt_entry entrytable[0];
1400};
1401
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001402static int compat_get_entries(struct net *net,
1403 struct compat_arpt_get_entries __user *uptr,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001404 int *len)
1405{
1406 int ret;
1407 struct compat_arpt_get_entries get;
Jan Engelhardt4abff072008-04-14 11:15:43 +02001408 struct xt_table *t;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001409
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001410 if (*len < sizeof(get))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001411 return -EINVAL;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001412 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1413 return -EFAULT;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001414 if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001415 return -EINVAL;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001416
Pablo Neira Ayusob301f252016-03-24 21:29:53 +01001417 get.name[sizeof(get.name) - 1] = '\0';
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001418
Jan Engelhardtee999d82008-10-08 11:35:01 +02001419 xt_compat_lock(NFPROTO_ARP);
1420 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
YOSHIFUJI Hideaki / 吉藤英明0cc8d8d2013-01-22 06:33:09 +00001421 if (!IS_ERR_OR_NULL(t)) {
Jan Engelhardt5452e422008-04-14 11:15:35 +02001422 const struct xt_table_info *private = t->private;
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001423 struct xt_table_info info;
1424
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001425 ret = compat_table_info(private, &info);
1426 if (!ret && get.size == info.size) {
1427 ret = compat_copy_entries_to_user(private->size,
1428 t, uptr->entrytable);
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001429 } else if (!ret)
Patrick McHardy544473c2008-04-14 11:15:45 +02001430 ret = -EAGAIN;
Pablo Neira Ayusod7cdf812016-05-03 13:54:23 +02001431
Jan Engelhardtee999d82008-10-08 11:35:01 +02001432 xt_compat_flush_offsets(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001433 module_put(t->me);
1434 xt_table_unlock(t);
1435 } else
1436 ret = t ? PTR_ERR(t) : -ENOENT;
1437
Jan Engelhardtee999d82008-10-08 11:35:01 +02001438 xt_compat_unlock(NFPROTO_ARP);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001439 return ret;
1440}
1441
1442static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1443
1444static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1445 int *len)
1446{
1447 int ret;
1448
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001449 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001450 return -EPERM;
1451
1452 switch (cmd) {
1453 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001454 ret = get_info(sock_net(sk), user, len, 1);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001455 break;
1456 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001457 ret = compat_get_entries(sock_net(sk), user, len);
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001458 break;
1459 default:
1460 ret = do_arpt_get_ctl(sk, cmd, user, len);
1461 }
1462 return ret;
1463}
1464#endif
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1467{
1468 int ret;
1469
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001470 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return -EPERM;
1472
1473 switch (cmd) {
1474 case ARPT_SO_SET_REPLACE:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001475 ret = do_replace(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477
1478 case ARPT_SO_SET_ADD_COUNTERS:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001479 ret = do_add_counters(sock_net(sk), user, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481
1482 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 ret = -EINVAL;
1484 }
1485
1486 return ret;
1487}
1488
1489static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1490{
1491 int ret;
1492
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001493 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EPERM;
1495
1496 switch (cmd) {
Patrick McHardy41acd972007-12-17 22:26:24 -08001497 case ARPT_SO_GET_INFO:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001498 ret = get_info(sock_net(sk), user, len, 0);
Patrick McHardy41acd972007-12-17 22:26:24 -08001499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Patrick McHardy11f6dff2007-12-17 22:26:38 -08001501 case ARPT_SO_GET_ENTRIES:
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001502 ret = get_entries(sock_net(sk), user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Harald Welte6b7d31f2005-10-26 09:34:24 +02001505 case ARPT_SO_GET_REVISION_TARGET: {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001506 struct xt_get_revision rev;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001507
1508 if (*len != sizeof(rev)) {
1509 ret = -EINVAL;
1510 break;
1511 }
1512 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1513 ret = -EFAULT;
1514 break;
1515 }
Vasiliy Kulikov42eab942011-03-15 13:35:21 +01001516 rev.name[sizeof(rev.name)-1] = 0;
Harald Welte6b7d31f2005-10-26 09:34:24 +02001517
Jan Engelhardtee999d82008-10-08 11:35:01 +02001518 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001519 rev.revision, 1, &ret),
Harald Welte6b7d31f2005-10-26 09:34:24 +02001520 "arpt_%s", rev.name);
1521 break;
1522 }
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ret = -EINVAL;
1526 }
1527
1528 return ret;
1529}
1530
Florian Westphal2564ab92020-01-11 23:19:53 +01001531static void __arpt_unregister_table(struct net *net, struct xt_table *table)
Florian Westphalb9e69e12016-02-25 10:08:36 +01001532{
1533 struct xt_table_info *private;
1534 void *loc_cpu_entry;
1535 struct module *table_owner = table->me;
1536 struct arpt_entry *iter;
1537
1538 private = xt_unregister_table(table);
1539
1540 /* Decrease module usage counts and free resources */
1541 loc_cpu_entry = private->entries;
1542 xt_entry_foreach(iter, loc_cpu_entry, private->size)
Florian Westphal2564ab92020-01-11 23:19:53 +01001543 cleanup_entry(iter, net);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001544 if (private->number > private->initial_entries)
1545 module_put(table_owner);
1546 xt_free_table_info(private);
1547}
1548
Florian Westphala67dd262016-02-25 10:08:35 +01001549int arpt_register_table(struct net *net,
1550 const struct xt_table *table,
1551 const struct arpt_replace *repl,
1552 const struct nf_hook_ops *ops,
1553 struct xt_table **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001556 struct xt_table_info *newinfo;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001557 struct xt_table_info bootstrap = {0};
Eric Dumazet31836062005-12-13 23:13:48 -08001558 void *loc_cpu_entry;
Alexey Dobriyana98da112008-01-31 04:01:49 -08001559 struct xt_table *new_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Harald Welte2e4e6a12006-01-12 13:30:04 -08001561 newinfo = xt_alloc_table_info(repl->size);
Florian Westphala67dd262016-02-25 10:08:35 +01001562 if (!newinfo)
1563 return -ENOMEM;
Eric Dumazet31836062005-12-13 23:13:48 -08001564
Florian Westphal482cfc32015-06-11 01:34:55 +02001565 loc_cpu_entry = newinfo->entries;
Eric Dumazet31836062005-12-13 23:13:48 -08001566 memcpy(loc_cpu_entry, repl->entries, repl->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Florian Westphal76c146c2019-12-27 01:33:10 +01001568 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001569 if (ret != 0)
1570 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Alexey Dobriyan79df3412008-01-31 04:04:32 -08001572 new_table = xt_register_table(net, table, &bootstrap, newinfo);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001573 if (IS_ERR(new_table)) {
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001574 ret = PTR_ERR(new_table);
1575 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
Florian Westphala67dd262016-02-25 10:08:35 +01001577
Florian Westphalb9e69e12016-02-25 10:08:36 +01001578 /* set res now, will see skbs right after nf_register_net_hooks */
Florian Westphala67dd262016-02-25 10:08:35 +01001579 WRITE_ONCE(*res, new_table);
1580
Florian Westphalb9e69e12016-02-25 10:08:36 +01001581 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1582 if (ret != 0) {
Florian Westphal2564ab92020-01-11 23:19:53 +01001583 __arpt_unregister_table(net, new_table);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001584 *res = NULL;
1585 }
1586
Florian Westphala67dd262016-02-25 10:08:35 +01001587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001589out_free:
1590 xt_free_table_info(newinfo);
Florian Westphala67dd262016-02-25 10:08:35 +01001591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Florian Westphala67dd262016-02-25 10:08:35 +01001594void arpt_unregister_table(struct net *net, struct xt_table *table,
1595 const struct nf_hook_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001597 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
Florian Westphal2564ab92020-01-11 23:19:53 +01001598 __arpt_unregister_table(net, table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/* The built-in targets: standard (NULL) and error. */
Jan Engelhardt45385062009-07-04 12:50:00 +02001602static struct xt_target arpt_builtin_tg[] __read_mostly = {
1603 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001604 .name = XT_STANDARD_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001605 .targetsize = sizeof(int),
1606 .family = NFPROTO_ARP,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001607#ifdef CONFIG_COMPAT
Jan Engelhardt45385062009-07-04 12:50:00 +02001608 .compatsize = sizeof(compat_int_t),
1609 .compat_from_user = compat_standard_from_user,
1610 .compat_to_user = compat_standard_to_user,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001611#endif
Jan Engelhardt45385062009-07-04 12:50:00 +02001612 },
1613 {
Jan Engelhardt243bf6e2010-10-13 16:28:00 +02001614 .name = XT_ERROR_TARGET,
Jan Engelhardt45385062009-07-04 12:50:00 +02001615 .target = arpt_error,
Jan Engelhardt12b00c22010-10-13 15:56:56 +02001616 .targetsize = XT_FUNCTION_MAXNAMELEN,
Jan Engelhardt45385062009-07-04 12:50:00 +02001617 .family = NFPROTO_ARP,
1618 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619};
1620
1621static struct nf_sockopt_ops arpt_sockopts = {
1622 .pf = PF_INET,
1623 .set_optmin = ARPT_BASE_CTL,
1624 .set_optmax = ARPT_SO_SET_MAX+1,
1625 .set = do_arpt_set_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001626#ifdef CONFIG_COMPAT
1627 .compat_set = compat_do_arpt_set_ctl,
1628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 .get_optmin = ARPT_BASE_CTL,
1630 .get_optmax = ARPT_SO_GET_MAX+1,
1631 .get = do_arpt_get_ctl,
Patrick McHardyd6a2ba02007-12-17 22:26:54 -08001632#ifdef CONFIG_COMPAT
1633 .compat_get = compat_do_arpt_get_ctl,
1634#endif
Neil Horman16fcec32007-09-11 11:28:26 +02001635 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636};
1637
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001638static int __net_init arp_tables_net_init(struct net *net)
1639{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001640 return xt_proto_init(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001641}
1642
1643static void __net_exit arp_tables_net_exit(struct net *net)
1644{
Jan Engelhardtee999d82008-10-08 11:35:01 +02001645 xt_proto_fini(net, NFPROTO_ARP);
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001646}
1647
1648static struct pernet_operations arp_tables_net_ops = {
1649 .init = arp_tables_net_init,
1650 .exit = arp_tables_net_exit,
1651};
1652
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001653static int __init arp_tables_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 int ret;
1656
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001657 ret = register_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001658 if (ret < 0)
1659 goto err1;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001660
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001661 /* No one else will be downing sem now, so we won't sleep */
Jan Engelhardt45385062009-07-04 12:50:00 +02001662 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001663 if (ret < 0)
1664 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* Register setsockopt */
1667 ret = nf_register_sockopt(&arpt_sockopts);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001668 if (ret < 0)
1669 goto err4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Bastian Stender09605cc2015-11-13 11:40:34 +01001671 pr_info("arp_tables: (C) 2002 David S. Miller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 0;
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001673
1674err4:
Jan Engelhardt45385062009-07-04 12:50:00 +02001675 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001676err2:
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001677 unregister_pernet_subsys(&arp_tables_net_ops);
Patrick McHardy0eff66e2006-08-13 18:57:28 -07001678err1:
1679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001682static void __exit arp_tables_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 nf_unregister_sockopt(&arpt_sockopts);
Jan Engelhardt45385062009-07-04 12:50:00 +02001685 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001686 unregister_pernet_subsys(&arp_tables_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689EXPORT_SYMBOL(arpt_register_table);
1690EXPORT_SYMBOL(arpt_unregister_table);
1691EXPORT_SYMBOL(arpt_do_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001693module_init(arp_tables_init);
1694module_exit(arp_tables_fini);