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