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