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