]>
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(); | |
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 | |
ae0ac0ed FW |
414 | find_check_entry(struct arpt_entry *e, const char *name, unsigned int size, |
415 | struct xt_percpu_counter_alloc_state *alloc_state) | |
fb5b6095 | 416 | { |
87a2e70d | 417 | struct xt_entry_target *t; |
95eea855 | 418 | struct xt_target *target; |
fb5b6095 PM |
419 | int ret; |
420 | ||
ae0ac0ed | 421 | if (!xt_percpu_counter_alloc(alloc_state, &e->counters)) |
71ae0dff FW |
422 | return -ENOMEM; |
423 | ||
fb5b6095 | 424 | t = arpt_get_target(e); |
d2a7b6ba JE |
425 | target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
426 | t->u.user.revision); | |
427 | if (IS_ERR(target)) { | |
d2a7b6ba | 428 | ret = PTR_ERR(target); |
1da177e4 LT |
429 | goto out; |
430 | } | |
1da177e4 | 431 | t->u.kernel.target = target; |
1da177e4 | 432 | |
fb5b6095 | 433 | ret = check_target(e, name); |
3cdc7c95 PM |
434 | if (ret) |
435 | goto err; | |
1da177e4 | 436 | return 0; |
3cdc7c95 PM |
437 | err: |
438 | module_put(t->u.kernel.target->me); | |
1da177e4 | 439 | out: |
4d31eef5 | 440 | xt_percpu_counter_free(&e->counters); |
71ae0dff | 441 | |
1da177e4 LT |
442 | return ret; |
443 | } | |
444 | ||
d5d1baa1 | 445 | static bool check_underflow(const struct arpt_entry *e) |
e2fe35c1 | 446 | { |
87a2e70d | 447 | const struct xt_entry_target *t; |
e2fe35c1 JE |
448 | unsigned int verdict; |
449 | ||
54d83fc7 | 450 | if (!unconditional(e)) |
e2fe35c1 | 451 | return false; |
d5d1baa1 | 452 | t = arpt_get_target_c(e); |
e2fe35c1 JE |
453 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0) |
454 | return false; | |
87a2e70d | 455 | verdict = ((struct xt_standard_target *)t)->verdict; |
e2fe35c1 JE |
456 | verdict = -verdict - 1; |
457 | return verdict == NF_DROP || verdict == NF_ACCEPT; | |
458 | } | |
459 | ||
1da177e4 | 460 | static inline int check_entry_size_and_hooks(struct arpt_entry *e, |
2e4e6a17 | 461 | struct xt_table_info *newinfo, |
d5d1baa1 JE |
462 | const unsigned char *base, |
463 | const unsigned char *limit, | |
1da177e4 LT |
464 | const unsigned int *hook_entries, |
465 | const unsigned int *underflows, | |
0559518b | 466 | unsigned int valid_hooks) |
1da177e4 LT |
467 | { |
468 | unsigned int h; | |
bdf533de | 469 | int err; |
1da177e4 | 470 | |
3666ed1c | 471 | if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 || |
6e94e0cf | 472 | (unsigned char *)e + sizeof(struct arpt_entry) >= limit || |
d7cdf816 | 473 | (unsigned char *)e + e->next_offset > limit) |
1da177e4 | 474 | return -EINVAL; |
1da177e4 LT |
475 | |
476 | if (e->next_offset | |
d7cdf816 | 477 | < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) |
1da177e4 | 478 | return -EINVAL; |
1da177e4 | 479 | |
aa412ba2 FW |
480 | if (!arp_checkentry(&e->arp)) |
481 | return -EINVAL; | |
482 | ||
ce683e5f FW |
483 | err = xt_check_entry_offsets(e, e->elems, e->target_offset, |
484 | e->next_offset); | |
bdf533de FW |
485 | if (err) |
486 | return err; | |
487 | ||
1da177e4 LT |
488 | /* Check hooks & underflows */ |
489 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
a7d51738 JE |
490 | if (!(valid_hooks & (1 << h))) |
491 | continue; | |
1da177e4 LT |
492 | if ((unsigned char *)e - base == hook_entries[h]) |
493 | newinfo->hook_entry[h] = hook_entries[h]; | |
90e7d4ab | 494 | if ((unsigned char *)e - base == underflows[h]) { |
d7cdf816 | 495 | if (!check_underflow(e)) |
90e7d4ab | 496 | return -EINVAL; |
d7cdf816 | 497 | |
1da177e4 | 498 | newinfo->underflow[h] = underflows[h]; |
90e7d4ab | 499 | } |
1da177e4 LT |
500 | } |
501 | ||
1da177e4 | 502 | /* Clear counters and comefrom */ |
2e4e6a17 | 503 | e->counters = ((struct xt_counters) { 0, 0 }); |
1da177e4 | 504 | e->comefrom = 0; |
1da177e4 LT |
505 | return 0; |
506 | } | |
507 | ||
0559518b | 508 | static inline void cleanup_entry(struct arpt_entry *e) |
1da177e4 | 509 | { |
a2df1648 | 510 | struct xt_tgdtor_param par; |
87a2e70d | 511 | struct xt_entry_target *t; |
1da177e4 | 512 | |
1da177e4 | 513 | t = arpt_get_target(e); |
a2df1648 JE |
514 | par.target = t->u.kernel.target; |
515 | par.targinfo = t->data; | |
916a917d | 516 | par.family = NFPROTO_ARP; |
a2df1648 JE |
517 | if (par.target->destroy != NULL) |
518 | par.target->destroy(&par); | |
519 | module_put(par.target->me); | |
4d31eef5 | 520 | xt_percpu_counter_free(&e->counters); |
1da177e4 LT |
521 | } |
522 | ||
523 | /* Checks and translates the user-supplied table segment (held in | |
524 | * newinfo). | |
525 | */ | |
0f234214 | 526 | static int translate_table(struct xt_table_info *newinfo, void *entry0, |
6c28255b | 527 | const struct arpt_replace *repl) |
1da177e4 | 528 | { |
ae0ac0ed | 529 | struct xt_percpu_counter_alloc_state alloc_state = { 0 }; |
72b2b1dd | 530 | struct arpt_entry *iter; |
f4dc7771 | 531 | unsigned int *offsets; |
1da177e4 | 532 | unsigned int i; |
72b2b1dd | 533 | int ret = 0; |
1da177e4 | 534 | |
0f234214 JE |
535 | newinfo->size = repl->size; |
536 | newinfo->number = repl->num_entries; | |
1da177e4 LT |
537 | |
538 | /* Init all hooks to impossible value. */ | |
539 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
540 | newinfo->hook_entry[i] = 0xFFFFFFFF; | |
541 | newinfo->underflow[i] = 0xFFFFFFFF; | |
542 | } | |
543 | ||
f4dc7771 FW |
544 | offsets = xt_alloc_entry_offsets(newinfo->number); |
545 | if (!offsets) | |
546 | return -ENOMEM; | |
1da177e4 LT |
547 | i = 0; |
548 | ||
549 | /* Walk through entries, checking offsets. */ | |
72b2b1dd JE |
550 | xt_entry_foreach(iter, entry0, newinfo->size) { |
551 | ret = check_entry_size_and_hooks(iter, newinfo, entry0, | |
6b4ff2d7 JE |
552 | entry0 + repl->size, |
553 | repl->hook_entry, | |
554 | repl->underflow, | |
555 | repl->valid_hooks); | |
72b2b1dd | 556 | if (ret != 0) |
f4dc7771 FW |
557 | goto out_free; |
558 | if (i < repl->num_entries) | |
559 | offsets[i] = (void *)iter - entry0; | |
0559518b | 560 | ++i; |
98dbbfc3 FW |
561 | if (strcmp(arpt_get_target(iter)->u.user.name, |
562 | XT_ERROR_TARGET) == 0) | |
563 | ++newinfo->stacksize; | |
72b2b1dd | 564 | } |
1da177e4 | 565 | if (ret != 0) |
f4dc7771 | 566 | goto out_free; |
1da177e4 | 567 | |
f4dc7771 | 568 | ret = -EINVAL; |
d7cdf816 | 569 | if (i != repl->num_entries) |
f4dc7771 | 570 | goto out_free; |
1da177e4 LT |
571 | |
572 | /* Check hooks all assigned */ | |
573 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
574 | /* Only hooks which are valid */ | |
0f234214 | 575 | if (!(repl->valid_hooks & (1 << i))) |
1da177e4 | 576 | continue; |
d7cdf816 | 577 | if (newinfo->hook_entry[i] == 0xFFFFFFFF) |
f4dc7771 | 578 | goto out_free; |
d7cdf816 | 579 | if (newinfo->underflow[i] == 0xFFFFFFFF) |
f4dc7771 | 580 | goto out_free; |
1da177e4 LT |
581 | } |
582 | ||
f4dc7771 FW |
583 | if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) { |
584 | ret = -ELOOP; | |
585 | goto out_free; | |
586 | } | |
587 | kvfree(offsets); | |
74c9c0c1 | 588 | |
1da177e4 LT |
589 | /* Finally, each sanity check must pass */ |
590 | i = 0; | |
72b2b1dd | 591 | xt_entry_foreach(iter, entry0, newinfo->size) { |
ae0ac0ed FW |
592 | ret = find_check_entry(iter, repl->name, repl->size, |
593 | &alloc_state); | |
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; |
1da177e4 LT |
680 | |
681 | /* FIXME: use iterator macros --RR */ | |
682 | /* ... then go back and fix counters and names */ | |
683 | for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ | |
87a2e70d | 684 | const struct xt_entry_target *t; |
1da177e4 | 685 | |
31836064 | 686 | e = (struct arpt_entry *)(loc_cpu_entry + off); |
244b531b WB |
687 | if (copy_to_user(userptr + off, e, sizeof(*e))) { |
688 | ret = -EFAULT; | |
689 | goto free_counters; | |
690 | } | |
1da177e4 LT |
691 | if (copy_to_user(userptr + off |
692 | + offsetof(struct arpt_entry, counters), | |
693 | &counters[num], | |
694 | sizeof(counters[num])) != 0) { | |
695 | ret = -EFAULT; | |
696 | goto free_counters; | |
697 | } | |
698 | ||
d5d1baa1 | 699 | t = arpt_get_target_c(e); |
244b531b | 700 | if (xt_target_to_user(t, userptr + off + e->target_offset)) { |
1da177e4 LT |
701 | ret = -EFAULT; |
702 | goto free_counters; | |
703 | } | |
704 | } | |
705 | ||
706 | free_counters: | |
707 | vfree(counters); | |
708 | return ret; | |
709 | } | |
710 | ||
d6a2ba07 | 711 | #ifdef CONFIG_COMPAT |
739674fb | 712 | static void compat_standard_from_user(void *dst, const void *src) |
d6a2ba07 PM |
713 | { |
714 | int v = *(compat_int_t *)src; | |
715 | ||
716 | if (v > 0) | |
ee999d8b | 717 | v += xt_compat_calc_jump(NFPROTO_ARP, v); |
d6a2ba07 PM |
718 | memcpy(dst, &v, sizeof(v)); |
719 | } | |
720 | ||
739674fb | 721 | static int compat_standard_to_user(void __user *dst, const void *src) |
d6a2ba07 PM |
722 | { |
723 | compat_int_t cv = *(int *)src; | |
724 | ||
725 | if (cv > 0) | |
ee999d8b | 726 | cv -= xt_compat_calc_jump(NFPROTO_ARP, cv); |
d6a2ba07 PM |
727 | return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; |
728 | } | |
729 | ||
d5d1baa1 | 730 | static int compat_calc_entry(const struct arpt_entry *e, |
d6a2ba07 | 731 | const struct xt_table_info *info, |
d5d1baa1 | 732 | const void *base, struct xt_table_info *newinfo) |
d6a2ba07 | 733 | { |
87a2e70d | 734 | const struct xt_entry_target *t; |
d6a2ba07 PM |
735 | unsigned int entry_offset; |
736 | int off, i, ret; | |
737 | ||
738 | off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
739 | entry_offset = (void *)e - base; | |
740 | ||
d5d1baa1 | 741 | t = arpt_get_target_c(e); |
d6a2ba07 PM |
742 | off += xt_compat_target_offset(t->u.kernel.target); |
743 | newinfo->size -= off; | |
ee999d8b | 744 | ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
d6a2ba07 PM |
745 | if (ret) |
746 | return ret; | |
747 | ||
748 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
749 | if (info->hook_entry[i] && | |
750 | (e < (struct arpt_entry *)(base + info->hook_entry[i]))) | |
751 | newinfo->hook_entry[i] -= off; | |
752 | if (info->underflow[i] && | |
753 | (e < (struct arpt_entry *)(base + info->underflow[i]))) | |
754 | newinfo->underflow[i] -= off; | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
759 | static int compat_table_info(const struct xt_table_info *info, | |
760 | struct xt_table_info *newinfo) | |
761 | { | |
72b2b1dd | 762 | struct arpt_entry *iter; |
711bdde6 | 763 | const void *loc_cpu_entry; |
0559518b | 764 | int ret; |
d6a2ba07 PM |
765 | |
766 | if (!newinfo || !info) | |
767 | return -EINVAL; | |
768 | ||
482cfc31 | 769 | /* we dont care about newinfo->entries */ |
d6a2ba07 PM |
770 | memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); |
771 | newinfo->initial_entries = 0; | |
482cfc31 | 772 | loc_cpu_entry = info->entries; |
255d0dc3 | 773 | xt_compat_init_offsets(NFPROTO_ARP, info->number); |
72b2b1dd JE |
774 | xt_entry_foreach(iter, loc_cpu_entry, info->size) { |
775 | ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo); | |
776 | if (ret != 0) | |
0559518b | 777 | return ret; |
72b2b1dd | 778 | } |
0559518b | 779 | return 0; |
d6a2ba07 PM |
780 | } |
781 | #endif | |
782 | ||
d5d1baa1 | 783 | static int get_info(struct net *net, void __user *user, |
6c28255b | 784 | const int *len, int compat) |
41acd975 | 785 | { |
12b00c2c | 786 | char name[XT_TABLE_MAXNAMELEN]; |
4abff077 | 787 | struct xt_table *t; |
41acd975 PM |
788 | int ret; |
789 | ||
d7cdf816 | 790 | if (*len != sizeof(struct arpt_getinfo)) |
41acd975 | 791 | return -EINVAL; |
41acd975 PM |
792 | |
793 | if (copy_from_user(name, user, sizeof(name)) != 0) | |
794 | return -EFAULT; | |
795 | ||
12b00c2c | 796 | name[XT_TABLE_MAXNAMELEN-1] = '\0'; |
d6a2ba07 PM |
797 | #ifdef CONFIG_COMPAT |
798 | if (compat) | |
ee999d8b | 799 | xt_compat_lock(NFPROTO_ARP); |
d6a2ba07 | 800 | #endif |
ee999d8b | 801 | t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name), |
41acd975 | 802 | "arptable_%s", name); |
eb1a6bdc | 803 | if (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 | |
eb1a6bdc | 832 | ret = -ENOENT; |
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); |
eb1a6bdc | 857 | if (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 |
eb1a6bdc | 869 | ret = -ENOENT; |
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; |
83723d60 | 888 | counters = vzalloc(num_counters * sizeof(struct xt_counters)); |
1da177e4 LT |
889 | if (!counters) { |
890 | ret = -ENOMEM; | |
d6a2ba07 | 891 | goto out; |
1da177e4 | 892 | } |
1da177e4 | 893 | |
ee999d8b | 894 | t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name), |
d6a2ba07 | 895 | "arptable_%s", name); |
eb1a6bdc JL |
896 | if (!t) { |
897 | ret = -ENOENT; | |
1da177e4 | 898 | goto free_newinfo_counters_untrans; |
6b7d31fc | 899 | } |
1da177e4 LT |
900 | |
901 | /* You lied! */ | |
d6a2ba07 | 902 | if (valid_hooks != t->valid_hooks) { |
1da177e4 | 903 | ret = -EINVAL; |
6b7d31fc | 904 | goto put_module; |
1da177e4 LT |
905 | } |
906 | ||
d6a2ba07 | 907 | oldinfo = xt_replace_table(t, num_counters, newinfo, &ret); |
1da177e4 LT |
908 | if (!oldinfo) |
909 | goto put_module; | |
910 | ||
911 | /* Update module usage count based on number of rules */ | |
e905a9ed YH |
912 | if ((oldinfo->number > oldinfo->initial_entries) || |
913 | (newinfo->number <= oldinfo->initial_entries)) | |
1da177e4 LT |
914 | module_put(t->me); |
915 | if ((oldinfo->number > oldinfo->initial_entries) && | |
916 | (newinfo->number <= oldinfo->initial_entries)) | |
917 | module_put(t->me); | |
918 | ||
942e4a2b | 919 | /* Get the old counters, and synchronize with replace */ |
1da177e4 | 920 | get_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); |
2e4e6a17 | 934 | xt_table_unlock(t); |
1da177e4 LT |
935 | return ret; |
936 | ||
937 | put_module: | |
938 | module_put(t->me); | |
2e4e6a17 | 939 | xt_table_unlock(t); |
1da177e4 | 940 | free_newinfo_counters_untrans: |
1da177e4 | 941 | vfree(counters); |
d6a2ba07 PM |
942 | out: |
943 | return ret; | |
944 | } | |
945 | ||
d5d1baa1 | 946 | static int do_replace(struct net *net, const void __user *user, |
6c28255b | 947 | unsigned int len) |
d6a2ba07 PM |
948 | { |
949 | int ret; | |
950 | struct arpt_replace tmp; | |
951 | struct xt_table_info *newinfo; | |
952 | void *loc_cpu_entry; | |
72b2b1dd | 953 | struct arpt_entry *iter; |
d6a2ba07 PM |
954 | |
955 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
956 | return -EFAULT; | |
957 | ||
958 | /* overflow check */ | |
959 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) | |
960 | return -ENOMEM; | |
1086bbe9 DJ |
961 | if (tmp.num_counters == 0) |
962 | return -EINVAL; | |
963 | ||
42eab94f | 964 | tmp.name[sizeof(tmp.name)-1] = 0; |
d6a2ba07 PM |
965 | |
966 | newinfo = xt_alloc_table_info(tmp.size); | |
967 | if (!newinfo) | |
968 | return -ENOMEM; | |
969 | ||
482cfc31 | 970 | loc_cpu_entry = newinfo->entries; |
d6a2ba07 PM |
971 | if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), |
972 | tmp.size) != 0) { | |
973 | ret = -EFAULT; | |
974 | goto free_newinfo; | |
975 | } | |
976 | ||
0f234214 | 977 | ret = translate_table(newinfo, loc_cpu_entry, &tmp); |
d6a2ba07 PM |
978 | if (ret != 0) |
979 | goto free_newinfo; | |
980 | ||
79df341a | 981 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
d6a2ba07 PM |
982 | tmp.num_counters, tmp.counters); |
983 | if (ret) | |
984 | goto free_newinfo_untrans; | |
985 | return 0; | |
986 | ||
987 | free_newinfo_untrans: | |
72b2b1dd | 988 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
0559518b | 989 | cleanup_entry(iter); |
1da177e4 | 990 | free_newinfo: |
2e4e6a17 | 991 | xt_free_table_info(newinfo); |
1da177e4 LT |
992 | return ret; |
993 | } | |
994 | ||
d5d1baa1 JE |
995 | static int do_add_counters(struct net *net, const void __user *user, |
996 | unsigned int len, int compat) | |
1da177e4 | 997 | { |
482cfc31 | 998 | unsigned int i; |
d6a2ba07 PM |
999 | struct xt_counters_info tmp; |
1000 | struct xt_counters *paddc; | |
4abff077 | 1001 | struct xt_table *t; |
5452e425 | 1002 | const struct xt_table_info *private; |
6b7d31fc | 1003 | int ret = 0; |
72b2b1dd | 1004 | struct arpt_entry *iter; |
7f5c6d4f | 1005 | unsigned int addend; |
d6a2ba07 | 1006 | |
d7591f0c FW |
1007 | paddc = xt_copy_counters_from_user(user, len, &tmp, compat); |
1008 | if (IS_ERR(paddc)) | |
1009 | return PTR_ERR(paddc); | |
1da177e4 | 1010 | |
d7591f0c | 1011 | t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name); |
eb1a6bdc JL |
1012 | if (!t) { |
1013 | ret = -ENOENT; | |
1da177e4 | 1014 | goto free; |
6b7d31fc | 1015 | } |
1da177e4 | 1016 | |
942e4a2b | 1017 | local_bh_disable(); |
2e4e6a17 | 1018 | private = t->private; |
d7591f0c | 1019 | if (private->number != tmp.num_counters) { |
1da177e4 LT |
1020 | ret = -EINVAL; |
1021 | goto unlock_up_free; | |
1022 | } | |
1023 | ||
1024 | i = 0; | |
482cfc31 | 1025 | |
7f5c6d4f | 1026 | addend = xt_write_recseq_begin(); |
482cfc31 | 1027 | xt_entry_foreach(iter, private->entries, private->size) { |
71ae0dff FW |
1028 | struct xt_counters *tmp; |
1029 | ||
1030 | tmp = xt_get_this_cpu_counter(&iter->counters); | |
1031 | ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt); | |
0559518b JE |
1032 | ++i; |
1033 | } | |
7f5c6d4f | 1034 | xt_write_recseq_end(addend); |
1da177e4 | 1035 | unlock_up_free: |
942e4a2b | 1036 | local_bh_enable(); |
2e4e6a17 | 1037 | xt_table_unlock(t); |
6b7d31fc | 1038 | module_put(t->me); |
1da177e4 LT |
1039 | free: |
1040 | vfree(paddc); | |
1041 | ||
1042 | return ret; | |
1043 | } | |
1044 | ||
d6a2ba07 | 1045 | #ifdef CONFIG_COMPAT |
8dddd327 FW |
1046 | struct compat_arpt_replace { |
1047 | char name[XT_TABLE_MAXNAMELEN]; | |
1048 | u32 valid_hooks; | |
1049 | u32 num_entries; | |
1050 | u32 size; | |
1051 | u32 hook_entry[NF_ARP_NUMHOOKS]; | |
1052 | u32 underflow[NF_ARP_NUMHOOKS]; | |
1053 | u32 num_counters; | |
1054 | compat_uptr_t counters; | |
1055 | struct compat_arpt_entry entries[0]; | |
1056 | }; | |
1057 | ||
0559518b | 1058 | static inline void compat_release_entry(struct compat_arpt_entry *e) |
d6a2ba07 | 1059 | { |
87a2e70d | 1060 | struct xt_entry_target *t; |
d6a2ba07 | 1061 | |
d6a2ba07 PM |
1062 | t = compat_arpt_get_target(e); |
1063 | module_put(t->u.kernel.target->me); | |
d6a2ba07 PM |
1064 | } |
1065 | ||
09d96860 | 1066 | static int |
d6a2ba07 PM |
1067 | check_compat_entry_size_and_hooks(struct compat_arpt_entry *e, |
1068 | struct xt_table_info *newinfo, | |
1069 | unsigned int *size, | |
d5d1baa1 | 1070 | const unsigned char *base, |
09d96860 | 1071 | const unsigned char *limit) |
d6a2ba07 | 1072 | { |
87a2e70d | 1073 | struct xt_entry_target *t; |
d6a2ba07 PM |
1074 | struct xt_target *target; |
1075 | unsigned int entry_offset; | |
09d96860 | 1076 | int ret, off; |
d6a2ba07 | 1077 | |
3666ed1c | 1078 | if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 || |
6e94e0cf | 1079 | (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit || |
d7cdf816 | 1080 | (unsigned char *)e + e->next_offset > limit) |
d6a2ba07 | 1081 | return -EINVAL; |
d6a2ba07 PM |
1082 | |
1083 | if (e->next_offset < sizeof(struct compat_arpt_entry) + | |
d7cdf816 | 1084 | sizeof(struct compat_xt_entry_target)) |
d6a2ba07 | 1085 | return -EINVAL; |
d6a2ba07 | 1086 | |
aa412ba2 FW |
1087 | if (!arp_checkentry(&e->arp)) |
1088 | return -EINVAL; | |
1089 | ||
ce683e5f | 1090 | ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset, |
fc1221b3 | 1091 | e->next_offset); |
d6a2ba07 PM |
1092 | if (ret) |
1093 | return ret; | |
1094 | ||
1095 | off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1096 | entry_offset = (void *)e - (void *)base; | |
1097 | ||
1098 | t = compat_arpt_get_target(e); | |
d2a7b6ba JE |
1099 | target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
1100 | t->u.user.revision); | |
1101 | if (IS_ERR(target)) { | |
d2a7b6ba | 1102 | ret = PTR_ERR(target); |
d6a2ba07 PM |
1103 | goto out; |
1104 | } | |
1105 | t->u.kernel.target = target; | |
1106 | ||
1107 | off += xt_compat_target_offset(target); | |
1108 | *size += off; | |
ee999d8b | 1109 | ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
d6a2ba07 PM |
1110 | if (ret) |
1111 | goto release_target; | |
1112 | ||
d6a2ba07 PM |
1113 | return 0; |
1114 | ||
1115 | release_target: | |
1116 | module_put(t->u.kernel.target->me); | |
1117 | out: | |
1118 | return ret; | |
1119 | } | |
1120 | ||
0188346f | 1121 | static void |
d6a2ba07 | 1122 | compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr, |
8dddd327 | 1123 | unsigned int *size, |
d6a2ba07 PM |
1124 | struct xt_table_info *newinfo, unsigned char *base) |
1125 | { | |
87a2e70d | 1126 | struct xt_entry_target *t; |
d6a2ba07 PM |
1127 | struct xt_target *target; |
1128 | struct arpt_entry *de; | |
1129 | unsigned int origsize; | |
0188346f | 1130 | int h; |
d6a2ba07 | 1131 | |
d6a2ba07 PM |
1132 | origsize = *size; |
1133 | de = (struct arpt_entry *)*dstptr; | |
1134 | memcpy(de, e, sizeof(struct arpt_entry)); | |
1135 | memcpy(&de->counters, &e->counters, sizeof(e->counters)); | |
1136 | ||
1137 | *dstptr += sizeof(struct arpt_entry); | |
1138 | *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1139 | ||
1140 | de->target_offset = e->target_offset - (origsize - *size); | |
1141 | t = compat_arpt_get_target(e); | |
1142 | target = t->u.kernel.target; | |
1143 | xt_compat_target_from_user(t, dstptr, size); | |
1144 | ||
1145 | de->next_offset = e->next_offset - (origsize - *size); | |
1146 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
1147 | if ((unsigned char *)de - base < newinfo->hook_entry[h]) | |
1148 | newinfo->hook_entry[h] -= origsize - *size; | |
1149 | if ((unsigned char *)de - base < newinfo->underflow[h]) | |
1150 | newinfo->underflow[h] -= origsize - *size; | |
1151 | } | |
d6a2ba07 PM |
1152 | } |
1153 | ||
8dddd327 | 1154 | static int translate_compat_table(struct xt_table_info **pinfo, |
d6a2ba07 | 1155 | void **pentry0, |
8dddd327 | 1156 | const struct compat_arpt_replace *compatr) |
d6a2ba07 PM |
1157 | { |
1158 | unsigned int i, j; | |
1159 | struct xt_table_info *newinfo, *info; | |
1160 | void *pos, *entry0, *entry1; | |
72b2b1dd | 1161 | struct compat_arpt_entry *iter0; |
09d96860 | 1162 | struct arpt_replace repl; |
d6a2ba07 | 1163 | unsigned int size; |
72b2b1dd | 1164 | int ret = 0; |
d6a2ba07 PM |
1165 | |
1166 | info = *pinfo; | |
1167 | entry0 = *pentry0; | |
8dddd327 FW |
1168 | size = compatr->size; |
1169 | info->number = compatr->num_entries; | |
d6a2ba07 | 1170 | |
d6a2ba07 | 1171 | j = 0; |
ee999d8b | 1172 | xt_compat_lock(NFPROTO_ARP); |
8dddd327 | 1173 | xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries); |
d6a2ba07 | 1174 | /* Walk through entries, checking offsets. */ |
8dddd327 | 1175 | xt_entry_foreach(iter0, entry0, compatr->size) { |
72b2b1dd | 1176 | ret = check_compat_entry_size_and_hooks(iter0, info, &size, |
6b4ff2d7 | 1177 | entry0, |
09d96860 | 1178 | entry0 + compatr->size); |
72b2b1dd | 1179 | if (ret != 0) |
0559518b JE |
1180 | goto out_unlock; |
1181 | ++j; | |
72b2b1dd | 1182 | } |
d6a2ba07 PM |
1183 | |
1184 | ret = -EINVAL; | |
d7cdf816 | 1185 | if (j != compatr->num_entries) |
d6a2ba07 | 1186 | goto out_unlock; |
d6a2ba07 | 1187 | |
d6a2ba07 PM |
1188 | ret = -ENOMEM; |
1189 | newinfo = xt_alloc_table_info(size); | |
1190 | if (!newinfo) | |
1191 | goto out_unlock; | |
1192 | ||
8dddd327 | 1193 | newinfo->number = compatr->num_entries; |
d6a2ba07 | 1194 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
17a49cd5 HJ |
1195 | newinfo->hook_entry[i] = compatr->hook_entry[i]; |
1196 | newinfo->underflow[i] = compatr->underflow[i]; | |
d6a2ba07 | 1197 | } |
482cfc31 | 1198 | entry1 = newinfo->entries; |
d6a2ba07 | 1199 | pos = entry1; |
8dddd327 | 1200 | size = compatr->size; |
0188346f FW |
1201 | xt_entry_foreach(iter0, entry0, compatr->size) |
1202 | compat_copy_entry_from_user(iter0, &pos, &size, | |
1203 | newinfo, entry1); | |
09d96860 FW |
1204 | |
1205 | /* all module references in entry0 are now gone */ | |
1206 | ||
ee999d8b JE |
1207 | xt_compat_flush_offsets(NFPROTO_ARP); |
1208 | xt_compat_unlock(NFPROTO_ARP); | |
d6a2ba07 | 1209 | |
09d96860 | 1210 | memcpy(&repl, compatr, sizeof(*compatr)); |
71ae0dff | 1211 | |
09d96860 FW |
1212 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
1213 | repl.hook_entry[i] = newinfo->hook_entry[i]; | |
1214 | repl.underflow[i] = newinfo->underflow[i]; | |
d6a2ba07 PM |
1215 | } |
1216 | ||
09d96860 FW |
1217 | repl.num_counters = 0; |
1218 | repl.counters = NULL; | |
1219 | repl.size = newinfo->size; | |
1220 | ret = translate_table(newinfo, entry1, &repl); | |
1221 | if (ret) | |
1222 | goto free_newinfo; | |
1223 | ||
d6a2ba07 PM |
1224 | *pinfo = newinfo; |
1225 | *pentry0 = entry1; | |
1226 | xt_free_table_info(info); | |
1227 | return 0; | |
1228 | ||
1229 | free_newinfo: | |
1230 | xt_free_table_info(newinfo); | |
09d96860 FW |
1231 | return ret; |
1232 | out_unlock: | |
1233 | xt_compat_flush_offsets(NFPROTO_ARP); | |
1234 | xt_compat_unlock(NFPROTO_ARP); | |
8dddd327 | 1235 | xt_entry_foreach(iter0, entry0, compatr->size) { |
0559518b | 1236 | if (j-- == 0) |
72b2b1dd | 1237 | break; |
0559518b JE |
1238 | compat_release_entry(iter0); |
1239 | } | |
d6a2ba07 | 1240 | return ret; |
d6a2ba07 PM |
1241 | } |
1242 | ||
79df341a AD |
1243 | static int compat_do_replace(struct net *net, void __user *user, |
1244 | unsigned int len) | |
d6a2ba07 PM |
1245 | { |
1246 | int ret; | |
1247 | struct compat_arpt_replace tmp; | |
1248 | struct xt_table_info *newinfo; | |
1249 | void *loc_cpu_entry; | |
72b2b1dd | 1250 | struct arpt_entry *iter; |
d6a2ba07 PM |
1251 | |
1252 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
1253 | return -EFAULT; | |
1254 | ||
1255 | /* overflow check */ | |
d6a2ba07 PM |
1256 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) |
1257 | return -ENOMEM; | |
1086bbe9 DJ |
1258 | if (tmp.num_counters == 0) |
1259 | return -EINVAL; | |
1260 | ||
42eab94f | 1261 | tmp.name[sizeof(tmp.name)-1] = 0; |
d6a2ba07 PM |
1262 | |
1263 | newinfo = xt_alloc_table_info(tmp.size); | |
1264 | if (!newinfo) | |
1265 | return -ENOMEM; | |
1266 | ||
482cfc31 | 1267 | loc_cpu_entry = newinfo->entries; |
d6a2ba07 PM |
1268 | if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) { |
1269 | ret = -EFAULT; | |
1270 | goto free_newinfo; | |
1271 | } | |
1272 | ||
8dddd327 | 1273 | ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp); |
d6a2ba07 PM |
1274 | if (ret != 0) |
1275 | goto free_newinfo; | |
1276 | ||
79df341a | 1277 | ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
d6a2ba07 PM |
1278 | tmp.num_counters, compat_ptr(tmp.counters)); |
1279 | if (ret) | |
1280 | goto free_newinfo_untrans; | |
1281 | return 0; | |
1282 | ||
1283 | free_newinfo_untrans: | |
72b2b1dd | 1284 | xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
0559518b | 1285 | cleanup_entry(iter); |
d6a2ba07 PM |
1286 | free_newinfo: |
1287 | xt_free_table_info(newinfo); | |
1288 | return ret; | |
1289 | } | |
1290 | ||
1291 | static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, | |
1292 | unsigned int len) | |
1293 | { | |
1294 | int ret; | |
1295 | ||
52e804c6 | 1296 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
d6a2ba07 PM |
1297 | return -EPERM; |
1298 | ||
1299 | switch (cmd) { | |
1300 | case ARPT_SO_SET_REPLACE: | |
3b1e0a65 | 1301 | ret = compat_do_replace(sock_net(sk), user, len); |
d6a2ba07 PM |
1302 | break; |
1303 | ||
1304 | case ARPT_SO_SET_ADD_COUNTERS: | |
3b1e0a65 | 1305 | ret = do_add_counters(sock_net(sk), user, len, 1); |
d6a2ba07 PM |
1306 | break; |
1307 | ||
1308 | default: | |
d6a2ba07 PM |
1309 | ret = -EINVAL; |
1310 | } | |
1311 | ||
1312 | return ret; | |
1313 | } | |
1314 | ||
1315 | static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr, | |
1316 | compat_uint_t *size, | |
1317 | struct xt_counters *counters, | |
0559518b | 1318 | unsigned int i) |
d6a2ba07 | 1319 | { |
87a2e70d | 1320 | struct xt_entry_target *t; |
d6a2ba07 PM |
1321 | struct compat_arpt_entry __user *ce; |
1322 | u_int16_t target_offset, next_offset; | |
1323 | compat_uint_t origsize; | |
1324 | int ret; | |
1325 | ||
d6a2ba07 PM |
1326 | origsize = *size; |
1327 | ce = (struct compat_arpt_entry __user *)*dstptr; | |
0559518b JE |
1328 | if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 || |
1329 | copy_to_user(&ce->counters, &counters[i], | |
1330 | sizeof(counters[i])) != 0) | |
1331 | return -EFAULT; | |
d6a2ba07 PM |
1332 | |
1333 | *dstptr += sizeof(struct compat_arpt_entry); | |
1334 | *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); | |
1335 | ||
1336 | target_offset = e->target_offset - (origsize - *size); | |
1337 | ||
1338 | t = arpt_get_target(e); | |
1339 | ret = xt_compat_target_to_user(t, dstptr, size); | |
1340 | if (ret) | |
0559518b | 1341 | return ret; |
d6a2ba07 | 1342 | next_offset = e->next_offset - (origsize - *size); |
0559518b JE |
1343 | if (put_user(target_offset, &ce->target_offset) != 0 || |
1344 | put_user(next_offset, &ce->next_offset) != 0) | |
1345 | return -EFAULT; | |
d6a2ba07 | 1346 | return 0; |
d6a2ba07 PM |
1347 | } |
1348 | ||
1349 | static int compat_copy_entries_to_user(unsigned int total_size, | |
4abff077 | 1350 | struct xt_table *table, |
d6a2ba07 PM |
1351 | void __user *userptr) |
1352 | { | |
1353 | struct xt_counters *counters; | |
5452e425 | 1354 | const struct xt_table_info *private = table->private; |
d6a2ba07 PM |
1355 | void __user *pos; |
1356 | unsigned int size; | |
1357 | int ret = 0; | |
d6a2ba07 | 1358 | unsigned int i = 0; |
72b2b1dd | 1359 | struct arpt_entry *iter; |
d6a2ba07 PM |
1360 | |
1361 | counters = alloc_counters(table); | |
1362 | if (IS_ERR(counters)) | |
1363 | return PTR_ERR(counters); | |
1364 | ||
d6a2ba07 PM |
1365 | pos = userptr; |
1366 | size = total_size; | |
482cfc31 | 1367 | xt_entry_foreach(iter, private->entries, total_size) { |
72b2b1dd | 1368 | ret = compat_copy_entry_to_user(iter, &pos, |
6b4ff2d7 | 1369 | &size, counters, i++); |
72b2b1dd JE |
1370 | if (ret != 0) |
1371 | break; | |
1372 | } | |
d6a2ba07 PM |
1373 | vfree(counters); |
1374 | return ret; | |
1375 | } | |
1376 | ||
1377 | struct compat_arpt_get_entries { | |
12b00c2c | 1378 | char name[XT_TABLE_MAXNAMELEN]; |
d6a2ba07 PM |
1379 | compat_uint_t size; |
1380 | struct compat_arpt_entry entrytable[0]; | |
1381 | }; | |
1382 | ||
79df341a AD |
1383 | static int compat_get_entries(struct net *net, |
1384 | struct compat_arpt_get_entries __user *uptr, | |
d6a2ba07 PM |
1385 | int *len) |
1386 | { | |
1387 | int ret; | |
1388 | struct compat_arpt_get_entries get; | |
4abff077 | 1389 | struct xt_table *t; |
d6a2ba07 | 1390 | |
d7cdf816 | 1391 | if (*len < sizeof(get)) |
d6a2ba07 | 1392 | return -EINVAL; |
d6a2ba07 PM |
1393 | if (copy_from_user(&get, uptr, sizeof(get)) != 0) |
1394 | return -EFAULT; | |
d7cdf816 | 1395 | if (*len != sizeof(struct compat_arpt_get_entries) + get.size) |
d6a2ba07 | 1396 | return -EINVAL; |
d7cdf816 | 1397 | |
b301f253 | 1398 | get.name[sizeof(get.name) - 1] = '\0'; |
d6a2ba07 | 1399 | |
ee999d8b JE |
1400 | xt_compat_lock(NFPROTO_ARP); |
1401 | t = xt_find_table_lock(net, NFPROTO_ARP, get.name); | |
eb1a6bdc | 1402 | if (t) { |
5452e425 | 1403 | const struct xt_table_info *private = t->private; |
d6a2ba07 PM |
1404 | struct xt_table_info info; |
1405 | ||
d6a2ba07 PM |
1406 | ret = compat_table_info(private, &info); |
1407 | if (!ret && get.size == info.size) { | |
1408 | ret = compat_copy_entries_to_user(private->size, | |
1409 | t, uptr->entrytable); | |
d7cdf816 | 1410 | } else if (!ret) |
544473c1 | 1411 | ret = -EAGAIN; |
d7cdf816 | 1412 | |
ee999d8b | 1413 | xt_compat_flush_offsets(NFPROTO_ARP); |
d6a2ba07 PM |
1414 | module_put(t->me); |
1415 | xt_table_unlock(t); | |
1416 | } else | |
eb1a6bdc | 1417 | ret = -ENOENT; |
d6a2ba07 | 1418 | |
ee999d8b | 1419 | xt_compat_unlock(NFPROTO_ARP); |
d6a2ba07 PM |
1420 | return ret; |
1421 | } | |
1422 | ||
1423 | static int do_arpt_get_ctl(struct sock *, int, void __user *, int *); | |
1424 | ||
1425 | static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, | |
1426 | int *len) | |
1427 | { | |
1428 | int ret; | |
1429 | ||
52e804c6 | 1430 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
d6a2ba07 PM |
1431 | return -EPERM; |
1432 | ||
1433 | switch (cmd) { | |
1434 | case ARPT_SO_GET_INFO: | |
3b1e0a65 | 1435 | ret = get_info(sock_net(sk), user, len, 1); |
d6a2ba07 PM |
1436 | break; |
1437 | case ARPT_SO_GET_ENTRIES: | |
3b1e0a65 | 1438 | ret = compat_get_entries(sock_net(sk), user, len); |
d6a2ba07 PM |
1439 | break; |
1440 | default: | |
1441 | ret = do_arpt_get_ctl(sk, cmd, user, len); | |
1442 | } | |
1443 | return ret; | |
1444 | } | |
1445 | #endif | |
1446 | ||
1da177e4 LT |
1447 | static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) |
1448 | { | |
1449 | int ret; | |
1450 | ||
52e804c6 | 1451 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1452 | return -EPERM; |
1453 | ||
1454 | switch (cmd) { | |
1455 | case ARPT_SO_SET_REPLACE: | |
3b1e0a65 | 1456 | ret = do_replace(sock_net(sk), user, len); |
1da177e4 LT |
1457 | break; |
1458 | ||
1459 | case ARPT_SO_SET_ADD_COUNTERS: | |
3b1e0a65 | 1460 | ret = do_add_counters(sock_net(sk), user, len, 0); |
1da177e4 LT |
1461 | break; |
1462 | ||
1463 | default: | |
1da177e4 LT |
1464 | ret = -EINVAL; |
1465 | } | |
1466 | ||
1467 | return ret; | |
1468 | } | |
1469 | ||
1470 | static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) | |
1471 | { | |
1472 | int ret; | |
1473 | ||
52e804c6 | 1474 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1475 | return -EPERM; |
1476 | ||
1477 | switch (cmd) { | |
41acd975 | 1478 | case ARPT_SO_GET_INFO: |
3b1e0a65 | 1479 | ret = get_info(sock_net(sk), user, len, 0); |
41acd975 | 1480 | break; |
1da177e4 | 1481 | |
11f6dff8 | 1482 | case ARPT_SO_GET_ENTRIES: |
3b1e0a65 | 1483 | ret = get_entries(sock_net(sk), user, len); |
1da177e4 | 1484 | break; |
1da177e4 | 1485 | |
6b7d31fc | 1486 | case ARPT_SO_GET_REVISION_TARGET: { |
2e4e6a17 | 1487 | struct xt_get_revision rev; |
6b7d31fc HW |
1488 | |
1489 | if (*len != sizeof(rev)) { | |
1490 | ret = -EINVAL; | |
1491 | break; | |
1492 | } | |
1493 | if (copy_from_user(&rev, user, sizeof(rev)) != 0) { | |
1494 | ret = -EFAULT; | |
1495 | break; | |
1496 | } | |
42eab94f | 1497 | rev.name[sizeof(rev.name)-1] = 0; |
6b7d31fc | 1498 | |
ee999d8b | 1499 | try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name, |
2e4e6a17 | 1500 | rev.revision, 1, &ret), |
6b7d31fc HW |
1501 | "arpt_%s", rev.name); |
1502 | break; | |
1503 | } | |
1504 | ||
1da177e4 | 1505 | default: |
1da177e4 LT |
1506 | ret = -EINVAL; |
1507 | } | |
1508 | ||
1509 | return ret; | |
1510 | } | |
1511 | ||
b9e69e12 FW |
1512 | static void __arpt_unregister_table(struct xt_table *table) |
1513 | { | |
1514 | struct xt_table_info *private; | |
1515 | void *loc_cpu_entry; | |
1516 | struct module *table_owner = table->me; | |
1517 | struct arpt_entry *iter; | |
1518 | ||
1519 | private = xt_unregister_table(table); | |
1520 | ||
1521 | /* Decrease module usage counts and free resources */ | |
1522 | loc_cpu_entry = private->entries; | |
1523 | xt_entry_foreach(iter, loc_cpu_entry, private->size) | |
1524 | cleanup_entry(iter); | |
1525 | if (private->number > private->initial_entries) | |
1526 | module_put(table_owner); | |
1527 | xt_free_table_info(private); | |
1528 | } | |
1529 | ||
a67dd266 FW |
1530 | int arpt_register_table(struct net *net, |
1531 | const struct xt_table *table, | |
1532 | const struct arpt_replace *repl, | |
1533 | const struct nf_hook_ops *ops, | |
1534 | struct xt_table **res) | |
1da177e4 LT |
1535 | { |
1536 | int ret; | |
2e4e6a17 | 1537 | struct xt_table_info *newinfo; |
f3c5c1bf | 1538 | struct xt_table_info bootstrap = {0}; |
31836064 | 1539 | void *loc_cpu_entry; |
a98da11d | 1540 | struct xt_table *new_table; |
1da177e4 | 1541 | |
2e4e6a17 | 1542 | newinfo = xt_alloc_table_info(repl->size); |
a67dd266 FW |
1543 | if (!newinfo) |
1544 | return -ENOMEM; | |
31836064 | 1545 | |
482cfc31 | 1546 | loc_cpu_entry = newinfo->entries; |
31836064 | 1547 | memcpy(loc_cpu_entry, repl->entries, repl->size); |
1da177e4 | 1548 | |
0f234214 | 1549 | ret = translate_table(newinfo, loc_cpu_entry, repl); |
44d34e72 AD |
1550 | if (ret != 0) |
1551 | goto out_free; | |
1da177e4 | 1552 | |
79df341a | 1553 | new_table = xt_register_table(net, table, &bootstrap, newinfo); |
a98da11d | 1554 | if (IS_ERR(new_table)) { |
44d34e72 AD |
1555 | ret = PTR_ERR(new_table); |
1556 | goto out_free; | |
1da177e4 | 1557 | } |
a67dd266 | 1558 | |
b9e69e12 | 1559 | /* set res now, will see skbs right after nf_register_net_hooks */ |
a67dd266 FW |
1560 | WRITE_ONCE(*res, new_table); |
1561 | ||
b9e69e12 FW |
1562 | ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks)); |
1563 | if (ret != 0) { | |
1564 | __arpt_unregister_table(new_table); | |
1565 | *res = NULL; | |
1566 | } | |
1567 | ||
a67dd266 | 1568 | return ret; |
1da177e4 | 1569 | |
44d34e72 AD |
1570 | out_free: |
1571 | xt_free_table_info(newinfo); | |
a67dd266 | 1572 | return ret; |
1da177e4 LT |
1573 | } |
1574 | ||
a67dd266 FW |
1575 | void arpt_unregister_table(struct net *net, struct xt_table *table, |
1576 | const struct nf_hook_ops *ops) | |
1da177e4 | 1577 | { |
b9e69e12 FW |
1578 | nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks)); |
1579 | __arpt_unregister_table(table); | |
1da177e4 LT |
1580 | } |
1581 | ||
1582 | /* The built-in targets: standard (NULL) and error. */ | |
4538506b JE |
1583 | static struct xt_target arpt_builtin_tg[] __read_mostly = { |
1584 | { | |
243bf6e2 | 1585 | .name = XT_STANDARD_TARGET, |
4538506b JE |
1586 | .targetsize = sizeof(int), |
1587 | .family = NFPROTO_ARP, | |
d6a2ba07 | 1588 | #ifdef CONFIG_COMPAT |
4538506b JE |
1589 | .compatsize = sizeof(compat_int_t), |
1590 | .compat_from_user = compat_standard_from_user, | |
1591 | .compat_to_user = compat_standard_to_user, | |
d6a2ba07 | 1592 | #endif |
4538506b JE |
1593 | }, |
1594 | { | |
243bf6e2 | 1595 | .name = XT_ERROR_TARGET, |
4538506b | 1596 | .target = arpt_error, |
12b00c2c | 1597 | .targetsize = XT_FUNCTION_MAXNAMELEN, |
4538506b JE |
1598 | .family = NFPROTO_ARP, |
1599 | }, | |
1da177e4 LT |
1600 | }; |
1601 | ||
1602 | static struct nf_sockopt_ops arpt_sockopts = { | |
1603 | .pf = PF_INET, | |
1604 | .set_optmin = ARPT_BASE_CTL, | |
1605 | .set_optmax = ARPT_SO_SET_MAX+1, | |
1606 | .set = do_arpt_set_ctl, | |
d6a2ba07 PM |
1607 | #ifdef CONFIG_COMPAT |
1608 | .compat_set = compat_do_arpt_set_ctl, | |
1609 | #endif | |
1da177e4 LT |
1610 | .get_optmin = ARPT_BASE_CTL, |
1611 | .get_optmax = ARPT_SO_GET_MAX+1, | |
1612 | .get = do_arpt_get_ctl, | |
d6a2ba07 PM |
1613 | #ifdef CONFIG_COMPAT |
1614 | .compat_get = compat_do_arpt_get_ctl, | |
1615 | #endif | |
16fcec35 | 1616 | .owner = THIS_MODULE, |
1da177e4 LT |
1617 | }; |
1618 | ||
3cb609d5 AD |
1619 | static int __net_init arp_tables_net_init(struct net *net) |
1620 | { | |
ee999d8b | 1621 | return xt_proto_init(net, NFPROTO_ARP); |
3cb609d5 AD |
1622 | } |
1623 | ||
1624 | static void __net_exit arp_tables_net_exit(struct net *net) | |
1625 | { | |
ee999d8b | 1626 | xt_proto_fini(net, NFPROTO_ARP); |
3cb609d5 AD |
1627 | } |
1628 | ||
1629 | static struct pernet_operations arp_tables_net_ops = { | |
1630 | .init = arp_tables_net_init, | |
1631 | .exit = arp_tables_net_exit, | |
1632 | }; | |
1633 | ||
65b4b4e8 | 1634 | static int __init arp_tables_init(void) |
1da177e4 LT |
1635 | { |
1636 | int ret; | |
1637 | ||
3cb609d5 | 1638 | ret = register_pernet_subsys(&arp_tables_net_ops); |
0eff66e6 PM |
1639 | if (ret < 0) |
1640 | goto err1; | |
2e4e6a17 | 1641 | |
25985edc | 1642 | /* No one else will be downing sem now, so we won't sleep */ |
4538506b | 1643 | ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
0eff66e6 PM |
1644 | if (ret < 0) |
1645 | goto err2; | |
1da177e4 LT |
1646 | |
1647 | /* Register setsockopt */ | |
1648 | ret = nf_register_sockopt(&arpt_sockopts); | |
0eff66e6 PM |
1649 | if (ret < 0) |
1650 | goto err4; | |
1da177e4 | 1651 | |
09605cc1 | 1652 | pr_info("arp_tables: (C) 2002 David S. Miller\n"); |
1da177e4 | 1653 | return 0; |
0eff66e6 PM |
1654 | |
1655 | err4: | |
4538506b | 1656 | xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
0eff66e6 | 1657 | err2: |
3cb609d5 | 1658 | unregister_pernet_subsys(&arp_tables_net_ops); |
0eff66e6 PM |
1659 | err1: |
1660 | return ret; | |
1da177e4 LT |
1661 | } |
1662 | ||
65b4b4e8 | 1663 | static void __exit arp_tables_fini(void) |
1da177e4 LT |
1664 | { |
1665 | nf_unregister_sockopt(&arpt_sockopts); | |
4538506b | 1666 | xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
3cb609d5 | 1667 | unregister_pernet_subsys(&arp_tables_net_ops); |
1da177e4 LT |
1668 | } |
1669 | ||
1670 | EXPORT_SYMBOL(arpt_register_table); | |
1671 | EXPORT_SYMBOL(arpt_unregister_table); | |
1672 | EXPORT_SYMBOL(arpt_do_table); | |
1da177e4 | 1673 | |
65b4b4e8 AM |
1674 | module_init(arp_tables_init); |
1675 | module_exit(arp_tables_fini); |