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