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