]>
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 | |
41 | #define dprintf(format, args...) printk(format , ## args) | |
42 | #else | |
43 | #define dprintf(format, args...) | |
44 | #endif | |
45 | ||
46 | #ifdef DEBUG_ARP_TABLES_USER | |
47 | #define duprintf(format, args...) printk(format , ## args) | |
48 | #else | |
49 | #define duprintf(format, args...) | |
50 | #endif | |
51 | ||
52 | #ifdef CONFIG_NETFILTER_DEBUG | |
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, | |
189 | arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":""); | |
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, | |
198 | arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":""); | |
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 | ||
98e86403 JE |
243 | static inline __pure |
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, |
1da177e4 | 250 | unsigned int hook, |
b85c3dc9 | 251 | const struct nf_hook_state *state, |
4abff077 | 252 | struct xt_table *table) |
1da177e4 | 253 | { |
ddc214c4 | 254 | static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); |
1da177e4 | 255 | unsigned int verdict = NF_DROP; |
5452e425 | 256 | const struct arphdr *arp; |
1da177e4 LT |
257 | struct arpt_entry *e, *back; |
258 | const char *indev, *outdev; | |
259 | void *table_base; | |
5452e425 | 260 | const struct xt_table_info *private; |
de74c169 | 261 | struct xt_action_param acpar; |
7f5c6d4f | 262 | unsigned int addend; |
1da177e4 | 263 | |
988b7050 | 264 | if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) |
1da177e4 LT |
265 | return NF_DROP; |
266 | ||
b85c3dc9 DM |
267 | indev = state->in ? state->in->name : nulldevname; |
268 | outdev = state->out ? state->out->name : nulldevname; | |
1da177e4 | 269 | |
7f5c6d4f ED |
270 | local_bh_disable(); |
271 | addend = xt_write_recseq_begin(); | |
942e4a2b | 272 | private = table->private; |
b416c144 WD |
273 | /* |
274 | * Ensure we load private-> members after we've fetched the base | |
275 | * pointer. | |
276 | */ | |
277 | smp_read_barrier_depends(); | |
942e4a2b | 278 | table_base = private->entries[smp_processor_id()]; |
78454473 | 279 | |
2e4e6a17 HW |
280 | e = get_entry(table_base, private->hook_entry[hook]); |
281 | back = get_entry(table_base, private->underflow[hook]); | |
1da177e4 | 282 | |
b85c3dc9 DM |
283 | acpar.in = state->in; |
284 | acpar.out = state->out; | |
de74c169 JE |
285 | acpar.hooknum = hook; |
286 | acpar.family = NFPROTO_ARP; | |
b4ba2611 | 287 | acpar.hotdrop = false; |
7eb35586 | 288 | |
3db05fea | 289 | arp = arp_hdr(skb); |
1da177e4 | 290 | do { |
87a2e70d | 291 | const struct xt_entry_target *t; |
71ae0dff | 292 | struct xt_counters *counter; |
a1ff4ac8 JE |
293 | |
294 | if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) { | |
295 | e = arpt_next_entry(e); | |
296 | continue; | |
297 | } | |
298 | ||
71ae0dff FW |
299 | counter = xt_get_this_cpu_counter(&e->counters); |
300 | ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1); | |
1da177e4 | 301 | |
d5d1baa1 | 302 | t = arpt_get_target_c(e); |
a1ff4ac8 JE |
303 | |
304 | /* Standard target? */ | |
305 | if (!t->u.kernel.target->target) { | |
306 | int v; | |
307 | ||
87a2e70d | 308 | v = ((struct xt_standard_target *)t)->verdict; |
a1ff4ac8 JE |
309 | if (v < 0) { |
310 | /* Pop from stack? */ | |
243bf6e2 | 311 | if (v != XT_RETURN) { |
95c96174 | 312 | verdict = (unsigned int)(-v) - 1; |
1da177e4 | 313 | break; |
a1ff4ac8 JE |
314 | } |
315 | e = back; | |
316 | back = get_entry(table_base, back->comefrom); | |
317 | continue; | |
1da177e4 | 318 | } |
a1ff4ac8 JE |
319 | if (table_base + v |
320 | != arpt_next_entry(e)) { | |
321 | /* Save old back ptr in next entry */ | |
322 | struct arpt_entry *next = arpt_next_entry(e); | |
323 | next->comefrom = (void *)back - table_base; | |
324 | ||
325 | /* set back pointer to next entry */ | |
326 | back = next; | |
327 | } | |
328 | ||
329 | e = get_entry(table_base, v); | |
7a6b1c46 | 330 | continue; |
1da177e4 | 331 | } |
7a6b1c46 JE |
332 | |
333 | /* Targets which reenter must return | |
334 | * abs. verdicts | |
335 | */ | |
de74c169 JE |
336 | acpar.target = t->u.kernel.target; |
337 | acpar.targinfo = t->data; | |
338 | verdict = t->u.kernel.target->target(skb, &acpar); | |
7a6b1c46 JE |
339 | |
340 | /* Target might have changed stuff. */ | |
341 | arp = arp_hdr(skb); | |
342 | ||
243bf6e2 | 343 | if (verdict == XT_CONTINUE) |
7a6b1c46 JE |
344 | e = arpt_next_entry(e); |
345 | else | |
346 | /* Verdict */ | |
347 | break; | |
b4ba2611 | 348 | } while (!acpar.hotdrop); |
7f5c6d4f ED |
349 | xt_write_recseq_end(addend); |
350 | local_bh_enable(); | |
1da177e4 | 351 | |
b4ba2611 | 352 | if (acpar.hotdrop) |
1da177e4 LT |
353 | return NF_DROP; |
354 | else | |
355 | return verdict; | |
356 | } | |
357 | ||
1da177e4 | 358 | /* All zeroes == unconditional rule. */ |
47901dc2 | 359 | static inline bool unconditional(const struct arpt_arp *arp) |
1da177e4 | 360 | { |
47901dc2 | 361 | static const struct arpt_arp uncond; |
1da177e4 | 362 | |
47901dc2 | 363 | return memcmp(arp, &uncond, sizeof(uncond)) == 0; |
1da177e4 LT |
364 | } |
365 | ||
366 | /* Figures out from what hook each rule can be called: returns 0 if | |
367 | * there are loops. Puts hook bitmask in comefrom. | |
368 | */ | |
d5d1baa1 | 369 | static int mark_source_chains(const struct xt_table_info *newinfo, |
31836064 | 370 | unsigned int valid_hooks, void *entry0) |
1da177e4 LT |
371 | { |
372 | unsigned int hook; | |
373 | ||
374 | /* No recursion; use packet counter to save back ptrs (reset | |
375 | * to 0 as we leave), and comefrom to save source hook bitmask. | |
376 | */ | |
377 | for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) { | |
378 | unsigned int pos = newinfo->hook_entry[hook]; | |
379 | struct arpt_entry *e | |
31836064 | 380 | = (struct arpt_entry *)(entry0 + pos); |
1da177e4 LT |
381 | |
382 | if (!(valid_hooks & (1 << hook))) | |
383 | continue; | |
384 | ||
385 | /* Set initial back pointer. */ | |
386 | e->counters.pcnt = pos; | |
387 | ||
388 | for (;;) { | |
87a2e70d | 389 | const struct xt_standard_target *t |
d5d1baa1 | 390 | = (void *)arpt_get_target_c(e); |
e1b4b9f3 | 391 | int visited = e->comefrom & (1 << hook); |
1da177e4 LT |
392 | |
393 | if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) { | |
654d0fbd | 394 | pr_notice("arptables: loop hook %u pos %u %08X.\n", |
1da177e4 LT |
395 | hook, pos, e->comefrom); |
396 | return 0; | |
397 | } | |
398 | e->comefrom | |
399 | |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS)); | |
400 | ||
401 | /* Unconditional return/END. */ | |
3666ed1c JP |
402 | if ((e->target_offset == sizeof(struct arpt_entry) && |
403 | (strcmp(t->target.u.user.name, | |
243bf6e2 | 404 | XT_STANDARD_TARGET) == 0) && |
3666ed1c JP |
405 | t->verdict < 0 && unconditional(&e->arp)) || |
406 | visited) { | |
1da177e4 LT |
407 | unsigned int oldpos, size; |
408 | ||
1f9352ae | 409 | if ((strcmp(t->target.u.user.name, |
243bf6e2 | 410 | XT_STANDARD_TARGET) == 0) && |
1f9352ae | 411 | t->verdict < -NF_MAX_VERDICT - 1) { |
74c9c0c1 DM |
412 | duprintf("mark_source_chains: bad " |
413 | "negative verdict (%i)\n", | |
414 | t->verdict); | |
415 | return 0; | |
416 | } | |
417 | ||
1da177e4 LT |
418 | /* Return: backtrack through the last |
419 | * big jump. | |
420 | */ | |
421 | do { | |
422 | e->comefrom ^= (1<<NF_ARP_NUMHOOKS); | |
423 | oldpos = pos; | |
424 | pos = e->counters.pcnt; | |
425 | e->counters.pcnt = 0; | |
426 | ||
427 | /* We're at the start. */ | |
428 | if (pos == oldpos) | |
429 | goto next; | |
430 | ||
431 | e = (struct arpt_entry *) | |
31836064 | 432 | (entry0 + pos); |
1da177e4 LT |
433 | } while (oldpos == pos + e->next_offset); |
434 | ||
435 | /* Move along one */ | |
436 | size = e->next_offset; | |
437 | e = (struct arpt_entry *) | |
31836064 | 438 | (entry0 + pos + size); |
1da177e4 LT |
439 | e->counters.pcnt = pos; |
440 | pos += size; | |
441 | } else { | |
442 | int newpos = t->verdict; | |
443 | ||
444 | if (strcmp(t->target.u.user.name, | |
243bf6e2 | 445 | XT_STANDARD_TARGET) == 0 && |
3666ed1c | 446 | newpos >= 0) { |
74c9c0c1 DM |
447 | if (newpos > newinfo->size - |
448 | sizeof(struct arpt_entry)) { | |
449 | duprintf("mark_source_chains: " | |
450 | "bad verdict (%i)\n", | |
451 | newpos); | |
452 | return 0; | |
453 | } | |
454 | ||
1da177e4 LT |
455 | /* This a jump; chase it. */ |
456 | duprintf("Jump rule %u -> %u\n", | |
457 | pos, newpos); | |
458 | } else { | |
459 | /* ... this is a fallthru */ | |
460 | newpos = pos + e->next_offset; | |
461 | } | |
462 | e = (struct arpt_entry *) | |
31836064 | 463 | (entry0 + newpos); |
1da177e4 LT |
464 | e->counters.pcnt = pos; |
465 | pos = newpos; | |
466 | } | |
467 | } | |
468 | next: | |
469 | duprintf("Finished chain %u\n", hook); | |
470 | } | |
471 | return 1; | |
472 | } | |
473 | ||
d5d1baa1 | 474 | static inline int check_entry(const struct arpt_entry *e, const char *name) |
1da177e4 | 475 | { |
87a2e70d | 476 | const struct xt_entry_target *t; |
1da177e4 LT |
477 | |
478 | if (!arp_checkentry(&e->arp)) { | |
479 | duprintf("arp_tables: arp check failed %p %s.\n", e, name); | |
480 | return -EINVAL; | |
481 | } | |
482 | ||
87a2e70d | 483 | if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset) |
590bdf7f DM |
484 | return -EINVAL; |
485 | ||
d5d1baa1 | 486 | t = arpt_get_target_c(e); |
590bdf7f DM |
487 | if (e->target_offset + t->u.target_size > e->next_offset) |
488 | return -EINVAL; | |
489 | ||
fb5b6095 PM |
490 | return 0; |
491 | } | |
492 | ||
493 | static inline int check_target(struct arpt_entry *e, const char *name) | |
494 | { | |
87a2e70d | 495 | struct xt_entry_target *t = arpt_get_target(e); |
fb5b6095 | 496 | int ret; |
af5d6dc2 JE |
497 | struct xt_tgchk_param par = { |
498 | .table = name, | |
499 | .entryinfo = e, | |
500 | .target = t->u.kernel.target, | |
501 | .targinfo = t->data, | |
502 | .hook_mask = e->comefrom, | |
916a917d | 503 | .family = NFPROTO_ARP, |
af5d6dc2 JE |
504 | }; |
505 | ||
916a917d | 506 | ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false); |
367c6790 | 507 | if (ret < 0) { |
fb5b6095 PM |
508 | duprintf("arp_tables: check failed for `%s'.\n", |
509 | t->u.kernel.target->name); | |
367c6790 | 510 | return ret; |
fb5b6095 | 511 | } |
367c6790 | 512 | return 0; |
fb5b6095 PM |
513 | } |
514 | ||
515 | static inline int | |
0559518b | 516 | find_check_entry(struct arpt_entry *e, const char *name, unsigned int size) |
fb5b6095 | 517 | { |
87a2e70d | 518 | struct xt_entry_target *t; |
95eea855 | 519 | struct xt_target *target; |
fb5b6095 PM |
520 | int ret; |
521 | ||
522 | ret = check_entry(e, name); | |
523 | if (ret) | |
524 | return ret; | |
525 | ||
71ae0dff FW |
526 | e->counters.pcnt = xt_percpu_counter_alloc(); |
527 | if (IS_ERR_VALUE(e->counters.pcnt)) | |
528 | return -ENOMEM; | |
529 | ||
fb5b6095 | 530 | t = arpt_get_target(e); |
d2a7b6ba JE |
531 | target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
532 | t->u.user.revision); | |
533 | if (IS_ERR(target)) { | |
fb5b6095 | 534 | duprintf("find_check_entry: `%s' not found\n", t->u.user.name); |
d2a7b6ba | 535 | ret = PTR_ERR(target); |
1da177e4 LT |
536 | goto out; |
537 | } | |
1da177e4 | 538 | t->u.kernel.target = target; |
1da177e4 | 539 | |
fb5b6095 | 540 | ret = check_target(e, name); |
3cdc7c95 PM |
541 | if (ret) |
542 | goto err; | |
1da177e4 | 543 | return 0; |
3cdc7c95 PM |
544 | err: |
545 | module_put(t->u.kernel.target->me); | |
1da177e4 | 546 | out: |
71ae0dff FW |
547 | xt_percpu_counter_free(e->counters.pcnt); |
548 | ||
1da177e4 LT |
549 | return ret; |
550 | } | |
551 | ||
d5d1baa1 | 552 | static bool check_underflow(const struct arpt_entry *e) |
e2fe35c1 | 553 | { |
87a2e70d | 554 | const struct xt_entry_target *t; |
e2fe35c1 JE |
555 | unsigned int verdict; |
556 | ||
557 | if (!unconditional(&e->arp)) | |
558 | return false; | |
d5d1baa1 | 559 | t = arpt_get_target_c(e); |
e2fe35c1 JE |
560 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0) |
561 | return false; | |
87a2e70d | 562 | verdict = ((struct xt_standard_target *)t)->verdict; |
e2fe35c1 JE |
563 | verdict = -verdict - 1; |
564 | return verdict == NF_DROP || verdict == NF_ACCEPT; | |
565 | } | |
566 | ||
1da177e4 | 567 | static inline int check_entry_size_and_hooks(struct arpt_entry *e, |
2e4e6a17 | 568 | struct xt_table_info *newinfo, |
d5d1baa1 JE |
569 | const unsigned char *base, |
570 | const unsigned char *limit, | |
1da177e4 LT |
571 | const unsigned int *hook_entries, |
572 | const unsigned int *underflows, | |
0559518b | 573 | unsigned int valid_hooks) |
1da177e4 LT |
574 | { |
575 | unsigned int h; | |
576 | ||
3666ed1c JP |
577 | if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 || |
578 | (unsigned char *)e + sizeof(struct arpt_entry) >= limit) { | |
1da177e4 LT |
579 | duprintf("Bad offset %p\n", e); |
580 | return -EINVAL; | |
581 | } | |
582 | ||
583 | if (e->next_offset | |
87a2e70d | 584 | < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) { |
1da177e4 LT |
585 | duprintf("checking: element %p size %u\n", |
586 | e, e->next_offset); | |
587 | return -EINVAL; | |
588 | } | |
589 | ||
590 | /* Check hooks & underflows */ | |
591 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
a7d51738 JE |
592 | if (!(valid_hooks & (1 << h))) |
593 | continue; | |
1da177e4 LT |
594 | if ((unsigned char *)e - base == hook_entries[h]) |
595 | newinfo->hook_entry[h] = hook_entries[h]; | |
90e7d4ab | 596 | if ((unsigned char *)e - base == underflows[h]) { |
e2fe35c1 JE |
597 | if (!check_underflow(e)) { |
598 | pr_err("Underflows must be unconditional and " | |
599 | "use the STANDARD target with " | |
600 | "ACCEPT/DROP\n"); | |
90e7d4ab JE |
601 | return -EINVAL; |
602 | } | |
1da177e4 | 603 | newinfo->underflow[h] = underflows[h]; |
90e7d4ab | 604 | } |
1da177e4 LT |
605 | } |
606 | ||
1da177e4 | 607 | /* Clear counters and comefrom */ |
2e4e6a17 | 608 | e->counters = ((struct xt_counters) { 0, 0 }); |
1da177e4 | 609 | e->comefrom = 0; |
1da177e4 LT |
610 | return 0; |
611 | } | |
612 | ||
0559518b | 613 | static inline void cleanup_entry(struct arpt_entry *e) |
1da177e4 | 614 | { |
a2df1648 | 615 | struct xt_tgdtor_param par; |
87a2e70d | 616 | struct xt_entry_target *t; |
1da177e4 | 617 | |
1da177e4 | 618 | t = arpt_get_target(e); |
a2df1648 JE |
619 | par.target = t->u.kernel.target; |
620 | par.targinfo = t->data; | |
916a917d | 621 | par.family = NFPROTO_ARP; |
a2df1648 JE |
622 | if (par.target->destroy != NULL) |
623 | par.target->destroy(&par); | |
624 | module_put(par.target->me); | |
71ae0dff | 625 | xt_percpu_counter_free(e->counters.pcnt); |
1da177e4 LT |
626 | } |
627 | ||
628 | /* Checks and translates the user-supplied table segment (held in | |
629 | * newinfo). | |
630 | */ | |
0f234214 JE |
631 | static int translate_table(struct xt_table_info *newinfo, void *entry0, |
632 | const struct arpt_replace *repl) | |
1da177e4 | 633 | { |
72b2b1dd | 634 | struct arpt_entry *iter; |
1da177e4 | 635 | unsigned int i; |
72b2b1dd | 636 | int ret = 0; |
1da177e4 | 637 | |
0f234214 JE |
638 | newinfo->size = repl->size; |
639 | newinfo->number = repl->num_entries; | |
1da177e4 LT |
640 | |
641 | /* Init all hooks to impossible value. */ | |
642 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
643 | newinfo->hook_entry[i] = 0xFFFFFFFF; | |
644 | newinfo->underflow[i] = 0xFFFFFFFF; | |
645 | } | |
646 | ||
647 | duprintf("translate_table: size %u\n", newinfo->size); | |
648 | i = 0; | |
649 | ||
650 | /* Walk through entries, checking offsets. */ | |
72b2b1dd JE |
651 | xt_entry_foreach(iter, entry0, newinfo->size) { |
652 | ret = check_entry_size_and_hooks(iter, newinfo, entry0, | |
6b4ff2d7 JE |
653 | entry0 + repl->size, |
654 | repl->hook_entry, | |
655 | repl->underflow, | |
656 | repl->valid_hooks); | |
72b2b1dd JE |
657 | if (ret != 0) |
658 | break; | |
0559518b | 659 | ++i; |
f3c5c1bf JE |
660 | if (strcmp(arpt_get_target(iter)->u.user.name, |
661 | XT_ERROR_TARGET) == 0) | |
662 | ++newinfo->stacksize; | |
72b2b1dd | 663 | } |
1da177e4 LT |
664 | duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret); |
665 | if (ret != 0) | |
666 | return ret; | |
667 | ||
0f234214 | 668 | if (i != repl->num_entries) { |
1da177e4 | 669 | duprintf("translate_table: %u not %u entries\n", |
0f234214 | 670 | i, repl->num_entries); |
1da177e4 LT |
671 | return -EINVAL; |
672 | } | |
673 | ||
674 | /* Check hooks all assigned */ | |
675 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
676 | /* Only hooks which are valid */ | |
0f234214 | 677 | if (!(repl->valid_hooks & (1 << i))) |
1da177e4 LT |
678 | continue; |
679 | if (newinfo->hook_entry[i] == 0xFFFFFFFF) { | |
680 | duprintf("Invalid hook entry %u %u\n", | |
0f234214 | 681 | i, repl->hook_entry[i]); |
1da177e4 LT |
682 | return -EINVAL; |
683 | } | |
684 | if (newinfo->underflow[i] == 0xFFFFFFFF) { | |
685 | duprintf("Invalid underflow %u %u\n", | |
0f234214 | 686 | i, repl->underflow[i]); |
1da177e4 LT |
687 | return -EINVAL; |
688 | } | |
689 | } | |
690 | ||
0f234214 | 691 | if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) { |
74c9c0c1 DM |
692 | duprintf("Looping hook\n"); |
693 | return -ELOOP; | |
694 | } | |
695 | ||
1da177e4 LT |
696 | /* Finally, each sanity check must pass */ |
697 | i = 0; | |
72b2b1dd | 698 | xt_entry_foreach(iter, entry0, newinfo->size) { |
0f234214 | 699 | ret = find_check_entry(iter, repl->name, repl->size); |
72b2b1dd JE |
700 | if (ret != 0) |
701 | break; | |
0559518b | 702 | ++i; |
72b2b1dd | 703 | } |
1da177e4 | 704 | |
74c9c0c1 | 705 | if (ret != 0) { |
0559518b JE |
706 | xt_entry_foreach(iter, entry0, newinfo->size) { |
707 | if (i-- == 0) | |
72b2b1dd | 708 | break; |
0559518b JE |
709 | cleanup_entry(iter); |
710 | } | |
74c9c0c1 | 711 | return ret; |
1da177e4 LT |
712 | } |
713 | ||
714 | /* And one copy for every other CPU */ | |
6f912042 | 715 | for_each_possible_cpu(i) { |
31836064 ED |
716 | if (newinfo->entries[i] && newinfo->entries[i] != entry0) |
717 | memcpy(newinfo->entries[i], entry0, newinfo->size); | |
1da177e4 LT |
718 | } |
719 | ||
720 | return ret; | |
721 | } | |
722 | ||
2e4e6a17 HW |
723 | static void get_counters(const struct xt_table_info *t, |
724 | struct xt_counters counters[]) | |
1da177e4 | 725 | { |
72b2b1dd | 726 | struct arpt_entry *iter; |
1da177e4 LT |
727 | unsigned int cpu; |
728 | unsigned int i; | |
729 | ||
6f912042 | 730 | for_each_possible_cpu(cpu) { |
7f5c6d4f | 731 | seqcount_t *s = &per_cpu(xt_recseq, cpu); |
83723d60 | 732 | |
1da177e4 | 733 | i = 0; |
0559518b | 734 | xt_entry_foreach(iter, t->entries[cpu], t->size) { |
71ae0dff | 735 | struct xt_counters *tmp; |
83723d60 ED |
736 | u64 bcnt, pcnt; |
737 | unsigned int start; | |
738 | ||
71ae0dff | 739 | tmp = xt_get_per_cpu_counter(&iter->counters, cpu); |
83723d60 | 740 | do { |
7f5c6d4f | 741 | start = read_seqcount_begin(s); |
71ae0dff FW |
742 | bcnt = tmp->bcnt; |
743 | pcnt = tmp->pcnt; | |
7f5c6d4f | 744 | } while (read_seqcount_retry(s, start)); |
83723d60 ED |
745 | |
746 | ADD_COUNTER(counters[i], bcnt, pcnt); | |
0559518b JE |
747 | ++i; |
748 | } | |
1da177e4 | 749 | } |
78454473 SH |
750 | } |
751 | ||
d5d1baa1 | 752 | static struct xt_counters *alloc_counters(const struct xt_table *table) |
1da177e4 | 753 | { |
27e2c26b | 754 | unsigned int countersize; |
2e4e6a17 | 755 | struct xt_counters *counters; |
d5d1baa1 | 756 | const struct xt_table_info *private = table->private; |
1da177e4 LT |
757 | |
758 | /* We need atomic snapshot of counters: rest doesn't change | |
759 | * (other than comefrom, which userspace doesn't care | |
760 | * about). | |
761 | */ | |
2e4e6a17 | 762 | countersize = sizeof(struct xt_counters) * private->number; |
83723d60 | 763 | counters = vzalloc(countersize); |
1da177e4 LT |
764 | |
765 | if (counters == NULL) | |
942e4a2b | 766 | return ERR_PTR(-ENOMEM); |
78454473 | 767 | |
942e4a2b | 768 | get_counters(private, counters); |
1da177e4 | 769 | |
27e2c26b PM |
770 | return counters; |
771 | } | |
772 | ||
773 | static int copy_entries_to_user(unsigned int total_size, | |
d5d1baa1 | 774 | const struct xt_table *table, |
27e2c26b PM |
775 | void __user *userptr) |
776 | { | |
777 | unsigned int off, num; | |
d5d1baa1 | 778 | const struct arpt_entry *e; |
27e2c26b | 779 | struct xt_counters *counters; |
4abff077 | 780 | struct xt_table_info *private = table->private; |
27e2c26b PM |
781 | int ret = 0; |
782 | void *loc_cpu_entry; | |
783 | ||
784 | counters = alloc_counters(table); | |
785 | if (IS_ERR(counters)) | |
786 | return PTR_ERR(counters); | |
787 | ||
2e4e6a17 | 788 | loc_cpu_entry = private->entries[raw_smp_processor_id()]; |
31836064 ED |
789 | /* ... then copy entire thing ... */ |
790 | if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) { | |
1da177e4 LT |
791 | ret = -EFAULT; |
792 | goto free_counters; | |
793 | } | |
794 | ||
795 | /* FIXME: use iterator macros --RR */ | |
796 | /* ... then go back and fix counters and names */ | |
797 | for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ | |
87a2e70d | 798 | const struct xt_entry_target *t; |
1da177e4 | 799 | |
31836064 | 800 | e = (struct arpt_entry *)(loc_cpu_entry + off); |
1da177e4 LT |
801 | if (copy_to_user(userptr + off |
802 | + offsetof(struct arpt_entry, counters), | |
803 | &counters[num], | |
804 | sizeof(counters[num])) != 0) { | |
805 | ret = -EFAULT; | |
806 | goto free_counters; | |
807 | } | |
808 | ||
d5d1baa1 | 809 | t = arpt_get_target_c(e); |
1da177e4 | 810 | if (copy_to_user(userptr + off + e->target_offset |
87a2e70d | 811 | + offsetof(struct xt_entry_target, |
1da177e4 LT |
812 | u.user.name), |
813 | t->u.kernel.target->name, | |
814 | strlen(t->u.kernel.target->name)+1) != 0) { | |
815 | ret = -EFAULT; | |
816 | goto free_counters; | |
817 | } | |
818 | } | |
819 | ||
820 | free_counters: | |
821 | vfree(counters); | |
822 | return ret; | |
823 | } | |
824 | ||
d6a2ba07 | 825 | #ifdef CONFIG_COMPAT |
739674fb | 826 | static void compat_standard_from_user(void *dst, const void *src) |
d6a2ba07 PM |
827 | { |
828 | int v = *(compat_int_t *)src; | |
829 | ||
830 | if (v > 0) | |
ee999d8b | 831 | v += xt_compat_calc_jump(NFPROTO_ARP, v); |
d6a2ba07 PM |
832 | memcpy(dst, &v, sizeof(v)); |
833 | } | |
834 | ||
739674fb | 835 | static int compat_standard_to_user(void __user *dst, const void *src) |
d6a2ba07 PM |
836 | { |
837 | compat_int_t cv = *(int *)src; | |
838 | ||
839 | if (cv > 0) | |
ee999d8b | 840 | cv -= xt_compat_calc_jump(NFPROTO_ARP, cv); |
d6a2ba07 PM |
841 | return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; |
842 | } | |
843 | ||
d5d1baa1 | 844 | static int compat_calc_entry(const struct arpt_entry *e, |
d6a2ba07 | 845 | const struct xt_table_info *info, |
d5d1baa1 | 846 | const void *base, struct xt_table_info *newinfo) |
d6a2ba07 | 847 | { |
87a2e70d | 848 | const struct xt_entry_target *t; |
d6a2ba07 PM |
849 | unsigned int entry_offset; |
850 | int off, i, ret; | |
851 | ||
852 | off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
853 | entry_offset = (void *)e - base; | |
854 | ||
d5d1baa1 | 855 | t = arpt_get_target_c(e); |
d6a2ba07 PM |
856 | off += xt_compat_target_offset(t->u.kernel.target); |
857 | newinfo->size -= off; | |
ee999d8b | 858 | ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
d6a2ba07 PM |
859 | if (ret) |
860 | return ret; | |
861 | ||
862 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
863 | if (info->hook_entry[i] && | |
864 | (e < (struct arpt_entry *)(base + info->hook_entry[i]))) | |
865 | newinfo->hook_entry[i] -= off; | |
866 | if (info->underflow[i] && | |
867 | (e < (struct arpt_entry *)(base + info->underflow[i]))) | |
868 | newinfo->underflow[i] -= off; | |
869 | } | |
870 | return 0; | |
871 | } | |
872 | ||
873 | static int compat_table_info(const struct xt_table_info *info, | |
874 | struct xt_table_info *newinfo) | |
875 | { | |
72b2b1dd | 876 | struct arpt_entry *iter; |
d6a2ba07 | 877 | void *loc_cpu_entry; |
0559518b | 878 | int ret; |
d6a2ba07 PM |
879 | |
880 | if (!newinfo || !info) | |
881 | return -EINVAL; | |
882 | ||
883 | /* we dont care about newinfo->entries[] */ | |
884 | memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); | |
885 | newinfo->initial_entries = 0; | |
886 | loc_cpu_entry = info->entries[raw_smp_processor_id()]; | |
255d0dc3 | 887 | xt_compat_init_offsets(NFPROTO_ARP, info->number); |
72b2b1dd JE |
888 | xt_entry_foreach(iter, loc_cpu_entry, info->size) { |
889 | ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo); | |
890 | if (ret != 0) | |
0559518b | 891 | return ret; |
72b2b1dd | 892 | } |
0559518b | 893 | return 0; |
d6a2ba07 PM |
894 | } |
895 | #endif | |
896 | ||
d5d1baa1 JE |
897 | static int get_info(struct net *net, void __user *user, |
898 | const int *len, int compat) | |
41acd975 | 899 | { |
12b00c2c | 900 | char name[XT_TABLE_MAXNAMELEN]; |
4abff077 | 901 | struct xt_table *t; |
41acd975 PM |
902 | int ret; |
903 | ||
904 | if (*len != sizeof(struct arpt_getinfo)) { | |
905 | duprintf("length %u != %Zu\n", *len, | |
906 | sizeof(struct arpt_getinfo)); | |
907 | return -EINVAL; | |
908 | } | |
909 | ||
910 | if (copy_from_user(name, user, sizeof(name)) != 0) | |
911 | return -EFAULT; | |
912 | ||
12b00c2c | 913 | name[XT_TABLE_MAXNAMELEN-1] = '\0'; |
d6a2ba07 PM |
914 | #ifdef CONFIG_COMPAT |
915 | if (compat) | |
ee999d8b | 916 | xt_compat_lock(NFPROTO_ARP); |
d6a2ba07 | 917 | #endif |
ee999d8b | 918 | t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name), |
41acd975 | 919 | "arptable_%s", name); |
0cc8d8df | 920 | if (!IS_ERR_OR_NULL(t)) { |
41acd975 | 921 | struct arpt_getinfo info; |
5452e425 | 922 | const struct xt_table_info *private = t->private; |
d6a2ba07 | 923 | #ifdef CONFIG_COMPAT |
14c7dbe0 AD |
924 | struct xt_table_info tmp; |
925 | ||
d6a2ba07 | 926 | if (compat) { |
d6a2ba07 | 927 | ret = compat_table_info(private, &tmp); |
ee999d8b | 928 | xt_compat_flush_offsets(NFPROTO_ARP); |
d6a2ba07 PM |
929 | private = &tmp; |
930 | } | |
931 | #endif | |
1a8b7a67 | 932 | memset(&info, 0, sizeof(info)); |
41acd975 PM |
933 | info.valid_hooks = t->valid_hooks; |
934 | memcpy(info.hook_entry, private->hook_entry, | |
935 | sizeof(info.hook_entry)); | |
936 | memcpy(info.underflow, private->underflow, | |
937 | sizeof(info.underflow)); | |
938 | info.num_entries = private->number; | |
939 | info.size = private->size; | |
940 | strcpy(info.name, name); | |
941 | ||
942 | if (copy_to_user(user, &info, *len) != 0) | |
943 | ret = -EFAULT; | |
944 | else | |
945 | ret = 0; | |
946 | xt_table_unlock(t); | |
947 | module_put(t->me); | |
948 | } else | |
949 | ret = t ? PTR_ERR(t) : -ENOENT; | |
d6a2ba07 PM |
950 | #ifdef CONFIG_COMPAT |
951 | if (compat) | |
ee999d8b | 952 | xt_compat_unlock(NFPROTO_ARP); |
d6a2ba07 | 953 | #endif |
41acd975 PM |
954 | return ret; |
955 | } | |
956 | ||
79df341a | 957 | static int get_entries(struct net *net, struct arpt_get_entries __user *uptr, |
d5d1baa1 | 958 | const int *len) |
1da177e4 LT |
959 | { |
960 | int ret; | |
11f6dff8 | 961 | struct arpt_get_entries get; |
4abff077 | 962 | struct xt_table *t; |
1da177e4 | 963 | |
11f6dff8 PM |
964 | if (*len < sizeof(get)) { |
965 | duprintf("get_entries: %u < %Zu\n", *len, sizeof(get)); | |
966 | return -EINVAL; | |
967 | } | |
968 | if (copy_from_user(&get, uptr, sizeof(get)) != 0) | |
969 | return -EFAULT; | |
970 | if (*len != sizeof(struct arpt_get_entries) + get.size) { | |
971 | duprintf("get_entries: %u != %Zu\n", *len, | |
972 | sizeof(struct arpt_get_entries) + get.size); | |
973 | return -EINVAL; | |
974 | } | |
975 | ||
ee999d8b | 976 | t = xt_find_table_lock(net, NFPROTO_ARP, get.name); |
0cc8d8df | 977 | if (!IS_ERR_OR_NULL(t)) { |
5452e425 JE |
978 | const struct xt_table_info *private = t->private; |
979 | ||
1da177e4 | 980 | duprintf("t->private->number = %u\n", |
2e4e6a17 | 981 | private->number); |
11f6dff8 | 982 | if (get.size == private->size) |
2e4e6a17 | 983 | ret = copy_entries_to_user(private->size, |
1da177e4 LT |
984 | t, uptr->entrytable); |
985 | else { | |
986 | duprintf("get_entries: I've got %u not %u!\n", | |
11f6dff8 | 987 | private->size, get.size); |
544473c1 | 988 | ret = -EAGAIN; |
1da177e4 | 989 | } |
6b7d31fc | 990 | module_put(t->me); |
2e4e6a17 | 991 | xt_table_unlock(t); |
1da177e4 | 992 | } else |
6b7d31fc | 993 | ret = t ? PTR_ERR(t) : -ENOENT; |
1da177e4 LT |
994 | |
995 | return ret; | |
996 | } | |
997 | ||
79df341a AD |
998 | static int __do_replace(struct net *net, const char *name, |
999 | unsigned int valid_hooks, | |
d6a2ba07 PM |
1000 | struct xt_table_info *newinfo, |
1001 | unsigned int num_counters, | |
1002 | void __user *counters_ptr) | |
1da177e4 LT |
1003 | { |
1004 | int ret; | |
4abff077 | 1005 | struct xt_table *t; |
d6a2ba07 | 1006 | struct xt_table_info *oldinfo; |
2e4e6a17 | 1007 | struct xt_counters *counters; |
d6a2ba07 | 1008 | void *loc_cpu_old_entry; |
72b2b1dd | 1009 | struct arpt_entry *iter; |
1da177e4 | 1010 | |
d6a2ba07 | 1011 | ret = 0; |
83723d60 | 1012 | counters = vzalloc(num_counters * sizeof(struct xt_counters)); |
1da177e4 LT |
1013 | if (!counters) { |
1014 | ret = -ENOMEM; | |
d6a2ba07 | 1015 | goto out; |
1da177e4 | 1016 | } |
1da177e4 | 1017 | |
ee999d8b | 1018 | t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name), |
d6a2ba07 | 1019 | "arptable_%s", name); |
0cc8d8df | 1020 | if (IS_ERR_OR_NULL(t)) { |
6b7d31fc | 1021 | ret = t ? PTR_ERR(t) : -ENOENT; |
1da177e4 | 1022 | goto free_newinfo_counters_untrans; |
6b7d31fc | 1023 | } |
1da177e4 LT |
1024 | |
1025 | /* You lied! */ | |
d6a2ba07 | 1026 | if (valid_hooks != t->valid_hooks) { |
1da177e4 | 1027 | duprintf("Valid hook crap: %08X vs %08X\n", |
d6a2ba07 | 1028 | valid_hooks, t->valid_hooks); |
1da177e4 | 1029 | ret = -EINVAL; |
6b7d31fc | 1030 | goto put_module; |
1da177e4 LT |
1031 | } |
1032 | ||
d6a2ba07 | 1033 | oldinfo = xt_replace_table(t, num_counters, newinfo, &ret); |
1da177e4 LT |
1034 | if (!oldinfo) |
1035 | goto put_module; | |
1036 | ||
1037 | /* Update module usage count based on number of rules */ | |
1038 | duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n", | |
1039 | oldinfo->number, oldinfo->initial_entries, newinfo->number); | |
e905a9ed YH |
1040 | if ((oldinfo->number > oldinfo->initial_entries) || |
1041 | (newinfo->number <= oldinfo->initial_entries)) | |
1da177e4 LT |
1042 | module_put(t->me); |
1043 | if ((oldinfo->number > oldinfo->initial_entries) && | |
1044 | (newinfo->number <= oldinfo->initial_entries)) | |
1045 | module_put(t->me); | |
1046 | ||
942e4a2b | 1047 | /* Get the old counters, and synchronize with replace */ |
1da177e4 | 1048 | get_counters(oldinfo, counters); |
942e4a2b | 1049 | |
1da177e4 | 1050 | /* Decrease module usage counts and free resource */ |
31836064 | 1051 | loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()]; |
72b2b1dd | 1052 | xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size) |
0559518b | 1053 | cleanup_entry(iter); |
31836064 | 1054 | |
2e4e6a17 | 1055 | xt_free_table_info(oldinfo); |
d6a2ba07 | 1056 | if (copy_to_user(counters_ptr, counters, |
c58dd2dd TG |
1057 | sizeof(struct xt_counters) * num_counters) != 0) { |
1058 | /* Silent error, can't fail, new table is already in place */ | |
1059 | net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n"); | |
1060 | } | |
1da177e4 | 1061 | vfree(counters); |
2e4e6a17 | 1062 | xt_table_unlock(t); |
1da177e4 LT |
1063 | return ret; |
1064 | ||
1065 | put_module: | |
1066 | module_put(t->me); | |
2e4e6a17 | 1067 | xt_table_unlock(t); |
1da177e4 | 1068 | free_newinfo_counters_untrans: |
1da177e4 | 1069 | vfree(counters); |
d6a2ba07 PM |
1070 | out: |
1071 | return ret; | |
1072 | } | |
1073 | ||
d5d1baa1 JE |
1074 | static int do_replace(struct net *net, const void __user *user, |
1075 | unsigned int len) | |
d6a2ba07 PM |
1076 | { |
1077 | int ret; | |
1078 | struct arpt_replace tmp; | |
1079 | struct xt_table_info *newinfo; | |
1080 | void *loc_cpu_entry; | |
72b2b1dd | 1081 | struct arpt_entry *iter; |
d6a2ba07 PM |
1082 | |
1083 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
1084 | return -EFAULT; | |
1085 | ||
1086 | /* overflow check */ | |
1087 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) | |
1088 | return -ENOMEM; | |
1086bbe9 DJ |
1089 | if (tmp.num_counters == 0) |
1090 | return -EINVAL; | |
1091 | ||
42eab94f | 1092 | tmp.name[sizeof(tmp.name)-1] = 0; |
d6a2ba07 PM |
1093 | |
1094 | newinfo = xt_alloc_table_info(tmp.size); | |
1095 | if (!newinfo) | |
1096 | return -ENOMEM; | |
1097 | ||
1098 | /* choose the copy that is on our node/cpu */ | |
1099 | loc_cpu_entry = newinfo->entries[raw_smp_processor_id()]; | |
1100 | if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), | |
1101 | tmp.size) != 0) { | |
1102 | ret = -EFAULT; | |
1103 | goto free_newinfo; | |
1104 | } | |
1105 | ||
0f234214 | 1106 | ret = translate_table(newinfo, loc_cpu_entry, &tmp); |
d6a2ba07 PM |
1107 | if (ret != 0) |
1108 | goto free_newinfo; | |
1109 | ||
1110 | duprintf("arp_tables: Translated table\n"); | |
1111 | ||
79df341a | 1112 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
d6a2ba07 PM |
1113 | tmp.num_counters, tmp.counters); |
1114 | if (ret) | |
1115 | goto free_newinfo_untrans; | |
1116 | return 0; | |
1117 | ||
1118 | free_newinfo_untrans: | |
72b2b1dd | 1119 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
0559518b | 1120 | cleanup_entry(iter); |
1da177e4 | 1121 | free_newinfo: |
2e4e6a17 | 1122 | xt_free_table_info(newinfo); |
1da177e4 LT |
1123 | return ret; |
1124 | } | |
1125 | ||
d5d1baa1 JE |
1126 | static int do_add_counters(struct net *net, const void __user *user, |
1127 | unsigned int len, int compat) | |
1da177e4 | 1128 | { |
942e4a2b | 1129 | unsigned int i, curcpu; |
d6a2ba07 PM |
1130 | struct xt_counters_info tmp; |
1131 | struct xt_counters *paddc; | |
1132 | unsigned int num_counters; | |
5452e425 | 1133 | const char *name; |
d6a2ba07 PM |
1134 | int size; |
1135 | void *ptmp; | |
4abff077 | 1136 | struct xt_table *t; |
5452e425 | 1137 | const struct xt_table_info *private; |
6b7d31fc | 1138 | int ret = 0; |
31836064 | 1139 | void *loc_cpu_entry; |
72b2b1dd | 1140 | struct arpt_entry *iter; |
7f5c6d4f | 1141 | unsigned int addend; |
d6a2ba07 PM |
1142 | #ifdef CONFIG_COMPAT |
1143 | struct compat_xt_counters_info compat_tmp; | |
1da177e4 | 1144 | |
d6a2ba07 PM |
1145 | if (compat) { |
1146 | ptmp = &compat_tmp; | |
1147 | size = sizeof(struct compat_xt_counters_info); | |
1148 | } else | |
1149 | #endif | |
1150 | { | |
1151 | ptmp = &tmp; | |
1152 | size = sizeof(struct xt_counters_info); | |
1153 | } | |
1154 | ||
1155 | if (copy_from_user(ptmp, user, size) != 0) | |
1da177e4 LT |
1156 | return -EFAULT; |
1157 | ||
d6a2ba07 PM |
1158 | #ifdef CONFIG_COMPAT |
1159 | if (compat) { | |
1160 | num_counters = compat_tmp.num_counters; | |
1161 | name = compat_tmp.name; | |
1162 | } else | |
1163 | #endif | |
1164 | { | |
1165 | num_counters = tmp.num_counters; | |
1166 | name = tmp.name; | |
1167 | } | |
1168 | ||
1169 | if (len != size + num_counters * sizeof(struct xt_counters)) | |
1da177e4 LT |
1170 | return -EINVAL; |
1171 | ||
e12f8e29 | 1172 | paddc = vmalloc(len - size); |
1da177e4 LT |
1173 | if (!paddc) |
1174 | return -ENOMEM; | |
1175 | ||
d6a2ba07 | 1176 | if (copy_from_user(paddc, user + size, len - size) != 0) { |
1da177e4 LT |
1177 | ret = -EFAULT; |
1178 | goto free; | |
1179 | } | |
1180 | ||
ee999d8b | 1181 | t = xt_find_table_lock(net, NFPROTO_ARP, name); |
0cc8d8df | 1182 | if (IS_ERR_OR_NULL(t)) { |
6b7d31fc | 1183 | ret = t ? PTR_ERR(t) : -ENOENT; |
1da177e4 | 1184 | goto free; |
6b7d31fc | 1185 | } |
1da177e4 | 1186 | |
942e4a2b | 1187 | local_bh_disable(); |
2e4e6a17 | 1188 | private = t->private; |
d6a2ba07 | 1189 | if (private->number != num_counters) { |
1da177e4 LT |
1190 | ret = -EINVAL; |
1191 | goto unlock_up_free; | |
1192 | } | |
1193 | ||
1194 | i = 0; | |
31836064 | 1195 | /* Choose the copy that is on our node */ |
942e4a2b SH |
1196 | curcpu = smp_processor_id(); |
1197 | loc_cpu_entry = private->entries[curcpu]; | |
7f5c6d4f | 1198 | addend = xt_write_recseq_begin(); |
0559518b | 1199 | xt_entry_foreach(iter, loc_cpu_entry, private->size) { |
71ae0dff FW |
1200 | struct xt_counters *tmp; |
1201 | ||
1202 | tmp = xt_get_this_cpu_counter(&iter->counters); | |
1203 | ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt); | |
0559518b JE |
1204 | ++i; |
1205 | } | |
7f5c6d4f | 1206 | xt_write_recseq_end(addend); |
1da177e4 | 1207 | unlock_up_free: |
942e4a2b | 1208 | local_bh_enable(); |
2e4e6a17 | 1209 | xt_table_unlock(t); |
6b7d31fc | 1210 | module_put(t->me); |
1da177e4 LT |
1211 | free: |
1212 | vfree(paddc); | |
1213 | ||
1214 | return ret; | |
1215 | } | |
1216 | ||
d6a2ba07 | 1217 | #ifdef CONFIG_COMPAT |
0559518b | 1218 | static inline void compat_release_entry(struct compat_arpt_entry *e) |
d6a2ba07 | 1219 | { |
87a2e70d | 1220 | struct xt_entry_target *t; |
d6a2ba07 | 1221 | |
d6a2ba07 PM |
1222 | t = compat_arpt_get_target(e); |
1223 | module_put(t->u.kernel.target->me); | |
d6a2ba07 PM |
1224 | } |
1225 | ||
1226 | static inline int | |
1227 | check_compat_entry_size_and_hooks(struct compat_arpt_entry *e, | |
1228 | struct xt_table_info *newinfo, | |
1229 | unsigned int *size, | |
d5d1baa1 JE |
1230 | const unsigned char *base, |
1231 | const unsigned char *limit, | |
1232 | const unsigned int *hook_entries, | |
1233 | const unsigned int *underflows, | |
d6a2ba07 PM |
1234 | const char *name) |
1235 | { | |
87a2e70d | 1236 | struct xt_entry_target *t; |
d6a2ba07 PM |
1237 | struct xt_target *target; |
1238 | unsigned int entry_offset; | |
1239 | int ret, off, h; | |
1240 | ||
1241 | duprintf("check_compat_entry_size_and_hooks %p\n", e); | |
3666ed1c JP |
1242 | if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 || |
1243 | (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) { | |
d6a2ba07 PM |
1244 | duprintf("Bad offset %p, limit = %p\n", e, limit); |
1245 | return -EINVAL; | |
1246 | } | |
1247 | ||
1248 | if (e->next_offset < sizeof(struct compat_arpt_entry) + | |
1249 | sizeof(struct compat_xt_entry_target)) { | |
1250 | duprintf("checking: element %p size %u\n", | |
1251 | e, e->next_offset); | |
1252 | return -EINVAL; | |
1253 | } | |
1254 | ||
1255 | /* For purposes of check_entry casting the compat entry is fine */ | |
1256 | ret = check_entry((struct arpt_entry *)e, name); | |
1257 | if (ret) | |
1258 | return ret; | |
1259 | ||
1260 | off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1261 | entry_offset = (void *)e - (void *)base; | |
1262 | ||
1263 | t = compat_arpt_get_target(e); | |
d2a7b6ba JE |
1264 | target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
1265 | t->u.user.revision); | |
1266 | if (IS_ERR(target)) { | |
d6a2ba07 PM |
1267 | duprintf("check_compat_entry_size_and_hooks: `%s' not found\n", |
1268 | t->u.user.name); | |
d2a7b6ba | 1269 | ret = PTR_ERR(target); |
d6a2ba07 PM |
1270 | goto out; |
1271 | } | |
1272 | t->u.kernel.target = target; | |
1273 | ||
1274 | off += xt_compat_target_offset(target); | |
1275 | *size += off; | |
ee999d8b | 1276 | ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
d6a2ba07 PM |
1277 | if (ret) |
1278 | goto release_target; | |
1279 | ||
1280 | /* Check hooks & underflows */ | |
1281 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
1282 | if ((unsigned char *)e - base == hook_entries[h]) | |
1283 | newinfo->hook_entry[h] = hook_entries[h]; | |
1284 | if ((unsigned char *)e - base == underflows[h]) | |
1285 | newinfo->underflow[h] = underflows[h]; | |
1286 | } | |
1287 | ||
1288 | /* Clear counters and comefrom */ | |
1289 | memset(&e->counters, 0, sizeof(e->counters)); | |
1290 | e->comefrom = 0; | |
d6a2ba07 PM |
1291 | return 0; |
1292 | ||
1293 | release_target: | |
1294 | module_put(t->u.kernel.target->me); | |
1295 | out: | |
1296 | return ret; | |
1297 | } | |
1298 | ||
1299 | static int | |
1300 | compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr, | |
1301 | unsigned int *size, const char *name, | |
1302 | struct xt_table_info *newinfo, unsigned char *base) | |
1303 | { | |
87a2e70d | 1304 | struct xt_entry_target *t; |
d6a2ba07 PM |
1305 | struct xt_target *target; |
1306 | struct arpt_entry *de; | |
1307 | unsigned int origsize; | |
1308 | int ret, h; | |
1309 | ||
1310 | ret = 0; | |
1311 | origsize = *size; | |
1312 | de = (struct arpt_entry *)*dstptr; | |
1313 | memcpy(de, e, sizeof(struct arpt_entry)); | |
1314 | memcpy(&de->counters, &e->counters, sizeof(e->counters)); | |
1315 | ||
1316 | *dstptr += sizeof(struct arpt_entry); | |
1317 | *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1318 | ||
1319 | de->target_offset = e->target_offset - (origsize - *size); | |
1320 | t = compat_arpt_get_target(e); | |
1321 | target = t->u.kernel.target; | |
1322 | xt_compat_target_from_user(t, dstptr, size); | |
1323 | ||
1324 | de->next_offset = e->next_offset - (origsize - *size); | |
1325 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
1326 | if ((unsigned char *)de - base < newinfo->hook_entry[h]) | |
1327 | newinfo->hook_entry[h] -= origsize - *size; | |
1328 | if ((unsigned char *)de - base < newinfo->underflow[h]) | |
1329 | newinfo->underflow[h] -= origsize - *size; | |
1330 | } | |
1331 | return ret; | |
1332 | } | |
1333 | ||
d6a2ba07 PM |
1334 | static int translate_compat_table(const char *name, |
1335 | unsigned int valid_hooks, | |
1336 | struct xt_table_info **pinfo, | |
1337 | void **pentry0, | |
1338 | unsigned int total_size, | |
1339 | unsigned int number, | |
1340 | unsigned int *hook_entries, | |
1341 | unsigned int *underflows) | |
1342 | { | |
1343 | unsigned int i, j; | |
1344 | struct xt_table_info *newinfo, *info; | |
1345 | void *pos, *entry0, *entry1; | |
72b2b1dd JE |
1346 | struct compat_arpt_entry *iter0; |
1347 | struct arpt_entry *iter1; | |
d6a2ba07 | 1348 | unsigned int size; |
72b2b1dd | 1349 | int ret = 0; |
d6a2ba07 PM |
1350 | |
1351 | info = *pinfo; | |
1352 | entry0 = *pentry0; | |
1353 | size = total_size; | |
1354 | info->number = number; | |
1355 | ||
1356 | /* Init all hooks to impossible value. */ | |
1357 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
1358 | info->hook_entry[i] = 0xFFFFFFFF; | |
1359 | info->underflow[i] = 0xFFFFFFFF; | |
1360 | } | |
1361 | ||
1362 | duprintf("translate_compat_table: size %u\n", info->size); | |
1363 | j = 0; | |
ee999d8b | 1364 | xt_compat_lock(NFPROTO_ARP); |
255d0dc3 | 1365 | xt_compat_init_offsets(NFPROTO_ARP, number); |
d6a2ba07 | 1366 | /* Walk through entries, checking offsets. */ |
72b2b1dd JE |
1367 | xt_entry_foreach(iter0, entry0, total_size) { |
1368 | ret = check_compat_entry_size_and_hooks(iter0, info, &size, | |
6b4ff2d7 JE |
1369 | entry0, |
1370 | entry0 + total_size, | |
1371 | hook_entries, | |
1372 | underflows, | |
1373 | name); | |
72b2b1dd | 1374 | if (ret != 0) |
0559518b JE |
1375 | goto out_unlock; |
1376 | ++j; | |
72b2b1dd | 1377 | } |
d6a2ba07 PM |
1378 | |
1379 | ret = -EINVAL; | |
1380 | if (j != number) { | |
1381 | duprintf("translate_compat_table: %u not %u entries\n", | |
1382 | j, number); | |
1383 | goto out_unlock; | |
1384 | } | |
1385 | ||
1386 | /* Check hooks all assigned */ | |
1387 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
1388 | /* Only hooks which are valid */ | |
1389 | if (!(valid_hooks & (1 << i))) | |
1390 | continue; | |
1391 | if (info->hook_entry[i] == 0xFFFFFFFF) { | |
1392 | duprintf("Invalid hook entry %u %u\n", | |
1393 | i, hook_entries[i]); | |
1394 | goto out_unlock; | |
1395 | } | |
1396 | if (info->underflow[i] == 0xFFFFFFFF) { | |
1397 | duprintf("Invalid underflow %u %u\n", | |
1398 | i, underflows[i]); | |
1399 | goto out_unlock; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | ret = -ENOMEM; | |
1404 | newinfo = xt_alloc_table_info(size); | |
1405 | if (!newinfo) | |
1406 | goto out_unlock; | |
1407 | ||
1408 | newinfo->number = number; | |
1409 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
1410 | newinfo->hook_entry[i] = info->hook_entry[i]; | |
1411 | newinfo->underflow[i] = info->underflow[i]; | |
1412 | } | |
1413 | entry1 = newinfo->entries[raw_smp_processor_id()]; | |
1414 | pos = entry1; | |
1415 | size = total_size; | |
72b2b1dd | 1416 | xt_entry_foreach(iter0, entry0, total_size) { |
6b4ff2d7 JE |
1417 | ret = compat_copy_entry_from_user(iter0, &pos, &size, |
1418 | name, newinfo, entry1); | |
72b2b1dd JE |
1419 | if (ret != 0) |
1420 | break; | |
1421 | } | |
ee999d8b JE |
1422 | xt_compat_flush_offsets(NFPROTO_ARP); |
1423 | xt_compat_unlock(NFPROTO_ARP); | |
d6a2ba07 PM |
1424 | if (ret) |
1425 | goto free_newinfo; | |
1426 | ||
1427 | ret = -ELOOP; | |
1428 | if (!mark_source_chains(newinfo, valid_hooks, entry1)) | |
1429 | goto free_newinfo; | |
1430 | ||
1431 | i = 0; | |
72b2b1dd | 1432 | xt_entry_foreach(iter1, entry1, newinfo->size) { |
71ae0dff FW |
1433 | iter1->counters.pcnt = xt_percpu_counter_alloc(); |
1434 | if (IS_ERR_VALUE(iter1->counters.pcnt)) { | |
1435 | ret = -ENOMEM; | |
1436 | break; | |
1437 | } | |
1438 | ||
0559518b | 1439 | ret = check_target(iter1, name); |
71ae0dff FW |
1440 | if (ret != 0) { |
1441 | xt_percpu_counter_free(iter1->counters.pcnt); | |
72b2b1dd | 1442 | break; |
71ae0dff | 1443 | } |
0559518b | 1444 | ++i; |
cca77b7c FW |
1445 | if (strcmp(arpt_get_target(iter1)->u.user.name, |
1446 | XT_ERROR_TARGET) == 0) | |
1447 | ++newinfo->stacksize; | |
72b2b1dd | 1448 | } |
d6a2ba07 | 1449 | if (ret) { |
72b2b1dd JE |
1450 | /* |
1451 | * The first i matches need cleanup_entry (calls ->destroy) | |
1452 | * because they had called ->check already. The other j-i | |
1453 | * entries need only release. | |
1454 | */ | |
1455 | int skip = i; | |
d6a2ba07 | 1456 | j -= i; |
72b2b1dd JE |
1457 | xt_entry_foreach(iter0, entry0, newinfo->size) { |
1458 | if (skip-- > 0) | |
1459 | continue; | |
0559518b | 1460 | if (j-- == 0) |
72b2b1dd | 1461 | break; |
0559518b | 1462 | compat_release_entry(iter0); |
72b2b1dd | 1463 | } |
0559518b JE |
1464 | xt_entry_foreach(iter1, entry1, newinfo->size) { |
1465 | if (i-- == 0) | |
72b2b1dd | 1466 | break; |
0559518b JE |
1467 | cleanup_entry(iter1); |
1468 | } | |
d6a2ba07 PM |
1469 | xt_free_table_info(newinfo); |
1470 | return ret; | |
1471 | } | |
1472 | ||
1473 | /* And one copy for every other CPU */ | |
1474 | for_each_possible_cpu(i) | |
1475 | if (newinfo->entries[i] && newinfo->entries[i] != entry1) | |
1476 | memcpy(newinfo->entries[i], entry1, newinfo->size); | |
1477 | ||
1478 | *pinfo = newinfo; | |
1479 | *pentry0 = entry1; | |
1480 | xt_free_table_info(info); | |
1481 | return 0; | |
1482 | ||
1483 | free_newinfo: | |
1484 | xt_free_table_info(newinfo); | |
1485 | out: | |
0559518b JE |
1486 | xt_entry_foreach(iter0, entry0, total_size) { |
1487 | if (j-- == 0) | |
72b2b1dd | 1488 | break; |
0559518b JE |
1489 | compat_release_entry(iter0); |
1490 | } | |
d6a2ba07 PM |
1491 | return ret; |
1492 | out_unlock: | |
ee999d8b JE |
1493 | xt_compat_flush_offsets(NFPROTO_ARP); |
1494 | xt_compat_unlock(NFPROTO_ARP); | |
d6a2ba07 PM |
1495 | goto out; |
1496 | } | |
1497 | ||
1498 | struct compat_arpt_replace { | |
12b00c2c | 1499 | char name[XT_TABLE_MAXNAMELEN]; |
d6a2ba07 PM |
1500 | u32 valid_hooks; |
1501 | u32 num_entries; | |
1502 | u32 size; | |
1503 | u32 hook_entry[NF_ARP_NUMHOOKS]; | |
1504 | u32 underflow[NF_ARP_NUMHOOKS]; | |
1505 | u32 num_counters; | |
1506 | compat_uptr_t counters; | |
1507 | struct compat_arpt_entry entries[0]; | |
1508 | }; | |
1509 | ||
79df341a AD |
1510 | static int compat_do_replace(struct net *net, void __user *user, |
1511 | unsigned int len) | |
d6a2ba07 PM |
1512 | { |
1513 | int ret; | |
1514 | struct compat_arpt_replace tmp; | |
1515 | struct xt_table_info *newinfo; | |
1516 | void *loc_cpu_entry; | |
72b2b1dd | 1517 | struct arpt_entry *iter; |
d6a2ba07 PM |
1518 | |
1519 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
1520 | return -EFAULT; | |
1521 | ||
1522 | /* overflow check */ | |
1523 | if (tmp.size >= INT_MAX / num_possible_cpus()) | |
1524 | return -ENOMEM; | |
1525 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) | |
1526 | return -ENOMEM; | |
1086bbe9 DJ |
1527 | if (tmp.num_counters == 0) |
1528 | return -EINVAL; | |
1529 | ||
42eab94f | 1530 | tmp.name[sizeof(tmp.name)-1] = 0; |
d6a2ba07 PM |
1531 | |
1532 | newinfo = xt_alloc_table_info(tmp.size); | |
1533 | if (!newinfo) | |
1534 | return -ENOMEM; | |
1535 | ||
1536 | /* choose the copy that is on our node/cpu */ | |
1537 | loc_cpu_entry = newinfo->entries[raw_smp_processor_id()]; | |
1538 | if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) { | |
1539 | ret = -EFAULT; | |
1540 | goto free_newinfo; | |
1541 | } | |
1542 | ||
1543 | ret = translate_compat_table(tmp.name, tmp.valid_hooks, | |
1544 | &newinfo, &loc_cpu_entry, tmp.size, | |
1545 | tmp.num_entries, tmp.hook_entry, | |
1546 | tmp.underflow); | |
1547 | if (ret != 0) | |
1548 | goto free_newinfo; | |
1549 | ||
1550 | duprintf("compat_do_replace: Translated table\n"); | |
1551 | ||
79df341a | 1552 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
d6a2ba07 PM |
1553 | tmp.num_counters, compat_ptr(tmp.counters)); |
1554 | if (ret) | |
1555 | goto free_newinfo_untrans; | |
1556 | return 0; | |
1557 | ||
1558 | free_newinfo_untrans: | |
72b2b1dd | 1559 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
0559518b | 1560 | cleanup_entry(iter); |
d6a2ba07 PM |
1561 | free_newinfo: |
1562 | xt_free_table_info(newinfo); | |
1563 | return ret; | |
1564 | } | |
1565 | ||
1566 | static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, | |
1567 | unsigned int len) | |
1568 | { | |
1569 | int ret; | |
1570 | ||
52e804c6 | 1571 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
d6a2ba07 PM |
1572 | return -EPERM; |
1573 | ||
1574 | switch (cmd) { | |
1575 | case ARPT_SO_SET_REPLACE: | |
3b1e0a65 | 1576 | ret = compat_do_replace(sock_net(sk), user, len); |
d6a2ba07 PM |
1577 | break; |
1578 | ||
1579 | case ARPT_SO_SET_ADD_COUNTERS: | |
3b1e0a65 | 1580 | ret = do_add_counters(sock_net(sk), user, len, 1); |
d6a2ba07 PM |
1581 | break; |
1582 | ||
1583 | default: | |
1584 | duprintf("do_arpt_set_ctl: unknown request %i\n", cmd); | |
1585 | ret = -EINVAL; | |
1586 | } | |
1587 | ||
1588 | return ret; | |
1589 | } | |
1590 | ||
1591 | static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr, | |
1592 | compat_uint_t *size, | |
1593 | struct xt_counters *counters, | |
0559518b | 1594 | unsigned int i) |
d6a2ba07 | 1595 | { |
87a2e70d | 1596 | struct xt_entry_target *t; |
d6a2ba07 PM |
1597 | struct compat_arpt_entry __user *ce; |
1598 | u_int16_t target_offset, next_offset; | |
1599 | compat_uint_t origsize; | |
1600 | int ret; | |
1601 | ||
d6a2ba07 PM |
1602 | origsize = *size; |
1603 | ce = (struct compat_arpt_entry __user *)*dstptr; | |
0559518b JE |
1604 | if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 || |
1605 | copy_to_user(&ce->counters, &counters[i], | |
1606 | sizeof(counters[i])) != 0) | |
1607 | return -EFAULT; | |
d6a2ba07 PM |
1608 | |
1609 | *dstptr += sizeof(struct compat_arpt_entry); | |
1610 | *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1611 | ||
1612 | target_offset = e->target_offset - (origsize - *size); | |
1613 | ||
1614 | t = arpt_get_target(e); | |
1615 | ret = xt_compat_target_to_user(t, dstptr, size); | |
1616 | if (ret) | |
0559518b | 1617 | return ret; |
d6a2ba07 | 1618 | next_offset = e->next_offset - (origsize - *size); |
0559518b JE |
1619 | if (put_user(target_offset, &ce->target_offset) != 0 || |
1620 | put_user(next_offset, &ce->next_offset) != 0) | |
1621 | return -EFAULT; | |
d6a2ba07 | 1622 | return 0; |
d6a2ba07 PM |
1623 | } |
1624 | ||
1625 | static int compat_copy_entries_to_user(unsigned int total_size, | |
4abff077 | 1626 | struct xt_table *table, |
d6a2ba07 PM |
1627 | void __user *userptr) |
1628 | { | |
1629 | struct xt_counters *counters; | |
5452e425 | 1630 | const struct xt_table_info *private = table->private; |
d6a2ba07 PM |
1631 | void __user *pos; |
1632 | unsigned int size; | |
1633 | int ret = 0; | |
1634 | void *loc_cpu_entry; | |
1635 | unsigned int i = 0; | |
72b2b1dd | 1636 | struct arpt_entry *iter; |
d6a2ba07 PM |
1637 | |
1638 | counters = alloc_counters(table); | |
1639 | if (IS_ERR(counters)) | |
1640 | return PTR_ERR(counters); | |
1641 | ||
1642 | /* choose the copy on our node/cpu */ | |
1643 | loc_cpu_entry = private->entries[raw_smp_processor_id()]; | |
1644 | pos = userptr; | |
1645 | size = total_size; | |
72b2b1dd JE |
1646 | xt_entry_foreach(iter, loc_cpu_entry, total_size) { |
1647 | ret = compat_copy_entry_to_user(iter, &pos, | |
6b4ff2d7 | 1648 | &size, counters, i++); |
72b2b1dd JE |
1649 | if (ret != 0) |
1650 | break; | |
1651 | } | |
d6a2ba07 PM |
1652 | vfree(counters); |
1653 | return ret; | |
1654 | } | |
1655 | ||
1656 | struct compat_arpt_get_entries { | |
12b00c2c | 1657 | char name[XT_TABLE_MAXNAMELEN]; |
d6a2ba07 PM |
1658 | compat_uint_t size; |
1659 | struct compat_arpt_entry entrytable[0]; | |
1660 | }; | |
1661 | ||
79df341a AD |
1662 | static int compat_get_entries(struct net *net, |
1663 | struct compat_arpt_get_entries __user *uptr, | |
d6a2ba07 PM |
1664 | int *len) |
1665 | { | |
1666 | int ret; | |
1667 | struct compat_arpt_get_entries get; | |
4abff077 | 1668 | struct xt_table *t; |
d6a2ba07 PM |
1669 | |
1670 | if (*len < sizeof(get)) { | |
1671 | duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get)); | |
1672 | return -EINVAL; | |
1673 | } | |
1674 | if (copy_from_user(&get, uptr, sizeof(get)) != 0) | |
1675 | return -EFAULT; | |
1676 | if (*len != sizeof(struct compat_arpt_get_entries) + get.size) { | |
1677 | duprintf("compat_get_entries: %u != %zu\n", | |
1678 | *len, sizeof(get) + get.size); | |
1679 | return -EINVAL; | |
1680 | } | |
1681 | ||
ee999d8b JE |
1682 | xt_compat_lock(NFPROTO_ARP); |
1683 | t = xt_find_table_lock(net, NFPROTO_ARP, get.name); | |
0cc8d8df | 1684 | if (!IS_ERR_OR_NULL(t)) { |
5452e425 | 1685 | const struct xt_table_info *private = t->private; |
d6a2ba07 PM |
1686 | struct xt_table_info info; |
1687 | ||
1688 | duprintf("t->private->number = %u\n", private->number); | |
1689 | ret = compat_table_info(private, &info); | |
1690 | if (!ret && get.size == info.size) { | |
1691 | ret = compat_copy_entries_to_user(private->size, | |
1692 | t, uptr->entrytable); | |
1693 | } else if (!ret) { | |
1694 | duprintf("compat_get_entries: I've got %u not %u!\n", | |
1695 | private->size, get.size); | |
544473c1 | 1696 | ret = -EAGAIN; |
d6a2ba07 | 1697 | } |
ee999d8b | 1698 | xt_compat_flush_offsets(NFPROTO_ARP); |
d6a2ba07 PM |
1699 | module_put(t->me); |
1700 | xt_table_unlock(t); | |
1701 | } else | |
1702 | ret = t ? PTR_ERR(t) : -ENOENT; | |
1703 | ||
ee999d8b | 1704 | xt_compat_unlock(NFPROTO_ARP); |
d6a2ba07 PM |
1705 | return ret; |
1706 | } | |
1707 | ||
1708 | static int do_arpt_get_ctl(struct sock *, int, void __user *, int *); | |
1709 | ||
1710 | static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, | |
1711 | int *len) | |
1712 | { | |
1713 | int ret; | |
1714 | ||
52e804c6 | 1715 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
d6a2ba07 PM |
1716 | return -EPERM; |
1717 | ||
1718 | switch (cmd) { | |
1719 | case ARPT_SO_GET_INFO: | |
3b1e0a65 | 1720 | ret = get_info(sock_net(sk), user, len, 1); |
d6a2ba07 PM |
1721 | break; |
1722 | case ARPT_SO_GET_ENTRIES: | |
3b1e0a65 | 1723 | ret = compat_get_entries(sock_net(sk), user, len); |
d6a2ba07 PM |
1724 | break; |
1725 | default: | |
1726 | ret = do_arpt_get_ctl(sk, cmd, user, len); | |
1727 | } | |
1728 | return ret; | |
1729 | } | |
1730 | #endif | |
1731 | ||
1da177e4 LT |
1732 | static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) |
1733 | { | |
1734 | int ret; | |
1735 | ||
52e804c6 | 1736 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1737 | return -EPERM; |
1738 | ||
1739 | switch (cmd) { | |
1740 | case ARPT_SO_SET_REPLACE: | |
3b1e0a65 | 1741 | ret = do_replace(sock_net(sk), user, len); |
1da177e4 LT |
1742 | break; |
1743 | ||
1744 | case ARPT_SO_SET_ADD_COUNTERS: | |
3b1e0a65 | 1745 | ret = do_add_counters(sock_net(sk), user, len, 0); |
1da177e4 LT |
1746 | break; |
1747 | ||
1748 | default: | |
1749 | duprintf("do_arpt_set_ctl: unknown request %i\n", cmd); | |
1750 | ret = -EINVAL; | |
1751 | } | |
1752 | ||
1753 | return ret; | |
1754 | } | |
1755 | ||
1756 | static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) | |
1757 | { | |
1758 | int ret; | |
1759 | ||
52e804c6 | 1760 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1761 | return -EPERM; |
1762 | ||
1763 | switch (cmd) { | |
41acd975 | 1764 | case ARPT_SO_GET_INFO: |
3b1e0a65 | 1765 | ret = get_info(sock_net(sk), user, len, 0); |
41acd975 | 1766 | break; |
1da177e4 | 1767 | |
11f6dff8 | 1768 | case ARPT_SO_GET_ENTRIES: |
3b1e0a65 | 1769 | ret = get_entries(sock_net(sk), user, len); |
1da177e4 | 1770 | break; |
1da177e4 | 1771 | |
6b7d31fc | 1772 | case ARPT_SO_GET_REVISION_TARGET: { |
2e4e6a17 | 1773 | struct xt_get_revision rev; |
6b7d31fc HW |
1774 | |
1775 | if (*len != sizeof(rev)) { | |
1776 | ret = -EINVAL; | |
1777 | break; | |
1778 | } | |
1779 | if (copy_from_user(&rev, user, sizeof(rev)) != 0) { | |
1780 | ret = -EFAULT; | |
1781 | break; | |
1782 | } | |
42eab94f | 1783 | rev.name[sizeof(rev.name)-1] = 0; |
6b7d31fc | 1784 | |
ee999d8b | 1785 | try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name, |
2e4e6a17 | 1786 | rev.revision, 1, &ret), |
6b7d31fc HW |
1787 | "arpt_%s", rev.name); |
1788 | break; | |
1789 | } | |
1790 | ||
1da177e4 LT |
1791 | default: |
1792 | duprintf("do_arpt_get_ctl: unknown request %i\n", cmd); | |
1793 | ret = -EINVAL; | |
1794 | } | |
1795 | ||
1796 | return ret; | |
1797 | } | |
1798 | ||
35aad0ff JE |
1799 | struct xt_table *arpt_register_table(struct net *net, |
1800 | const struct xt_table *table, | |
4abff077 | 1801 | const struct arpt_replace *repl) |
1da177e4 LT |
1802 | { |
1803 | int ret; | |
2e4e6a17 | 1804 | struct xt_table_info *newinfo; |
f3c5c1bf | 1805 | struct xt_table_info bootstrap = {0}; |
31836064 | 1806 | void *loc_cpu_entry; |
a98da11d | 1807 | struct xt_table *new_table; |
1da177e4 | 1808 | |
2e4e6a17 | 1809 | newinfo = xt_alloc_table_info(repl->size); |
1da177e4 LT |
1810 | if (!newinfo) { |
1811 | ret = -ENOMEM; | |
44d34e72 | 1812 | goto out; |
1da177e4 | 1813 | } |
31836064 ED |
1814 | |
1815 | /* choose the copy on our node/cpu */ | |
1816 | loc_cpu_entry = newinfo->entries[raw_smp_processor_id()]; | |
1817 | memcpy(loc_cpu_entry, repl->entries, repl->size); | |
1da177e4 | 1818 | |
0f234214 | 1819 | ret = translate_table(newinfo, loc_cpu_entry, repl); |
1da177e4 | 1820 | duprintf("arpt_register_table: translate table gives %d\n", ret); |
44d34e72 AD |
1821 | if (ret != 0) |
1822 | goto out_free; | |
1da177e4 | 1823 | |
79df341a | 1824 | new_table = xt_register_table(net, table, &bootstrap, newinfo); |
a98da11d | 1825 | if (IS_ERR(new_table)) { |
44d34e72 AD |
1826 | ret = PTR_ERR(new_table); |
1827 | goto out_free; | |
1da177e4 | 1828 | } |
44d34e72 | 1829 | return new_table; |
1da177e4 | 1830 | |
44d34e72 AD |
1831 | out_free: |
1832 | xt_free_table_info(newinfo); | |
1833 | out: | |
1834 | return ERR_PTR(ret); | |
1da177e4 LT |
1835 | } |
1836 | ||
4abff077 | 1837 | void arpt_unregister_table(struct xt_table *table) |
1da177e4 | 1838 | { |
2e4e6a17 | 1839 | struct xt_table_info *private; |
31836064 | 1840 | void *loc_cpu_entry; |
df200969 | 1841 | struct module *table_owner = table->me; |
72b2b1dd | 1842 | struct arpt_entry *iter; |
31836064 | 1843 | |
2e4e6a17 | 1844 | private = xt_unregister_table(table); |
1da177e4 LT |
1845 | |
1846 | /* Decrease module usage counts and free resources */ | |
2e4e6a17 | 1847 | loc_cpu_entry = private->entries[raw_smp_processor_id()]; |
72b2b1dd | 1848 | xt_entry_foreach(iter, loc_cpu_entry, private->size) |
0559518b | 1849 | cleanup_entry(iter); |
df200969 AD |
1850 | if (private->number > private->initial_entries) |
1851 | module_put(table_owner); | |
2e4e6a17 | 1852 | xt_free_table_info(private); |
1da177e4 LT |
1853 | } |
1854 | ||
1855 | /* The built-in targets: standard (NULL) and error. */ | |
4538506b JE |
1856 | static struct xt_target arpt_builtin_tg[] __read_mostly = { |
1857 | { | |
243bf6e2 | 1858 | .name = XT_STANDARD_TARGET, |
4538506b JE |
1859 | .targetsize = sizeof(int), |
1860 | .family = NFPROTO_ARP, | |
d6a2ba07 | 1861 | #ifdef CONFIG_COMPAT |
4538506b JE |
1862 | .compatsize = sizeof(compat_int_t), |
1863 | .compat_from_user = compat_standard_from_user, | |
1864 | .compat_to_user = compat_standard_to_user, | |
d6a2ba07 | 1865 | #endif |
4538506b JE |
1866 | }, |
1867 | { | |
243bf6e2 | 1868 | .name = XT_ERROR_TARGET, |
4538506b | 1869 | .target = arpt_error, |
12b00c2c | 1870 | .targetsize = XT_FUNCTION_MAXNAMELEN, |
4538506b JE |
1871 | .family = NFPROTO_ARP, |
1872 | }, | |
1da177e4 LT |
1873 | }; |
1874 | ||
1875 | static struct nf_sockopt_ops arpt_sockopts = { | |
1876 | .pf = PF_INET, | |
1877 | .set_optmin = ARPT_BASE_CTL, | |
1878 | .set_optmax = ARPT_SO_SET_MAX+1, | |
1879 | .set = do_arpt_set_ctl, | |
d6a2ba07 PM |
1880 | #ifdef CONFIG_COMPAT |
1881 | .compat_set = compat_do_arpt_set_ctl, | |
1882 | #endif | |
1da177e4 LT |
1883 | .get_optmin = ARPT_BASE_CTL, |
1884 | .get_optmax = ARPT_SO_GET_MAX+1, | |
1885 | .get = do_arpt_get_ctl, | |
d6a2ba07 PM |
1886 | #ifdef CONFIG_COMPAT |
1887 | .compat_get = compat_do_arpt_get_ctl, | |
1888 | #endif | |
16fcec35 | 1889 | .owner = THIS_MODULE, |
1da177e4 LT |
1890 | }; |
1891 | ||
3cb609d5 AD |
1892 | static int __net_init arp_tables_net_init(struct net *net) |
1893 | { | |
ee999d8b | 1894 | return xt_proto_init(net, NFPROTO_ARP); |
3cb609d5 AD |
1895 | } |
1896 | ||
1897 | static void __net_exit arp_tables_net_exit(struct net *net) | |
1898 | { | |
ee999d8b | 1899 | xt_proto_fini(net, NFPROTO_ARP); |
3cb609d5 AD |
1900 | } |
1901 | ||
1902 | static struct pernet_operations arp_tables_net_ops = { | |
1903 | .init = arp_tables_net_init, | |
1904 | .exit = arp_tables_net_exit, | |
1905 | }; | |
1906 | ||
65b4b4e8 | 1907 | static int __init arp_tables_init(void) |
1da177e4 LT |
1908 | { |
1909 | int ret; | |
1910 | ||
3cb609d5 | 1911 | ret = register_pernet_subsys(&arp_tables_net_ops); |
0eff66e6 PM |
1912 | if (ret < 0) |
1913 | goto err1; | |
2e4e6a17 | 1914 | |
25985edc | 1915 | /* No one else will be downing sem now, so we won't sleep */ |
4538506b | 1916 | ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
0eff66e6 PM |
1917 | if (ret < 0) |
1918 | goto err2; | |
1da177e4 LT |
1919 | |
1920 | /* Register setsockopt */ | |
1921 | ret = nf_register_sockopt(&arpt_sockopts); | |
0eff66e6 PM |
1922 | if (ret < 0) |
1923 | goto err4; | |
1da177e4 | 1924 | |
a887c1c1 | 1925 | printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n"); |
1da177e4 | 1926 | return 0; |
0eff66e6 PM |
1927 | |
1928 | err4: | |
4538506b | 1929 | xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
0eff66e6 | 1930 | err2: |
3cb609d5 | 1931 | unregister_pernet_subsys(&arp_tables_net_ops); |
0eff66e6 PM |
1932 | err1: |
1933 | return ret; | |
1da177e4 LT |
1934 | } |
1935 | ||
65b4b4e8 | 1936 | static void __exit arp_tables_fini(void) |
1da177e4 LT |
1937 | { |
1938 | nf_unregister_sockopt(&arpt_sockopts); | |
4538506b | 1939 | xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
3cb609d5 | 1940 | unregister_pernet_subsys(&arp_tables_net_ops); |
1da177e4 LT |
1941 | } |
1942 | ||
1943 | EXPORT_SYMBOL(arpt_register_table); | |
1944 | EXPORT_SYMBOL(arpt_unregister_table); | |
1945 | EXPORT_SYMBOL(arpt_do_table); | |
1da177e4 | 1946 | |
65b4b4e8 AM |
1947 | module_init(arp_tables_init); |
1948 | module_exit(arp_tables_fini); |