Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Packet matching code for ARP packets. | |
3 | * | |
4 | * Based heavily, if not almost entirely, upon ip_tables.c framework. | |
5 | * | |
6 | * Some ARP specific bits are: | |
7 | * | |
8 | * Copyright (C) 2002 David S. Miller (davem@redhat.com) | |
9 | * | |
10 | */ | |
11 | ||
1da177e4 LT |
12 | #include <linux/kernel.h> |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/netdevice.h> | |
4fc268d2 | 15 | #include <linux/capability.h> |
1da177e4 LT |
16 | #include <linux/if_arp.h> |
17 | #include <linux/kmod.h> | |
18 | #include <linux/vmalloc.h> | |
19 | #include <linux/proc_fs.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/init.h> | |
22 | ||
23 | #include <asm/uaccess.h> | |
57b47a53 | 24 | #include <linux/mutex.h> |
1da177e4 | 25 | |
2e4e6a17 | 26 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
27 | #include <linux/netfilter_arp/arp_tables.h> |
28 | ||
29 | MODULE_LICENSE("GPL"); | |
30 | MODULE_AUTHOR("David S. Miller <davem@redhat.com>"); | |
31 | MODULE_DESCRIPTION("arptables core"); | |
32 | ||
33 | /*#define DEBUG_ARP_TABLES*/ | |
34 | /*#define DEBUG_ARP_TABLES_USER*/ | |
35 | ||
36 | #ifdef DEBUG_ARP_TABLES | |
37 | #define dprintf(format, args...) printk(format , ## args) | |
38 | #else | |
39 | #define dprintf(format, args...) | |
40 | #endif | |
41 | ||
42 | #ifdef DEBUG_ARP_TABLES_USER | |
43 | #define duprintf(format, args...) printk(format , ## args) | |
44 | #else | |
45 | #define duprintf(format, args...) | |
46 | #endif | |
47 | ||
48 | #ifdef CONFIG_NETFILTER_DEBUG | |
49 | #define ARP_NF_ASSERT(x) \ | |
50 | do { \ | |
51 | if (!(x)) \ | |
52 | printk("ARP_NF_ASSERT: %s:%s:%u\n", \ | |
53 | __FUNCTION__, __FILE__, __LINE__); \ | |
54 | } while(0) | |
55 | #else | |
56 | #define ARP_NF_ASSERT(x) | |
57 | #endif | |
1da177e4 | 58 | |
1da177e4 LT |
59 | static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap, |
60 | char *hdr_addr, int len) | |
61 | { | |
62 | int i, ret; | |
63 | ||
64 | if (len > ARPT_DEV_ADDR_LEN_MAX) | |
65 | len = ARPT_DEV_ADDR_LEN_MAX; | |
66 | ||
67 | ret = 0; | |
68 | for (i = 0; i < len; i++) | |
69 | ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i]; | |
70 | ||
71 | return (ret != 0); | |
72 | } | |
73 | ||
74 | /* Returns whether packet matches rule or not. */ | |
75 | static inline int arp_packet_match(const struct arphdr *arphdr, | |
76 | struct net_device *dev, | |
77 | const char *indev, | |
78 | const char *outdev, | |
79 | const struct arpt_arp *arpinfo) | |
80 | { | |
81 | char *arpptr = (char *)(arphdr + 1); | |
82 | char *src_devaddr, *tgt_devaddr; | |
59b8bfd8 | 83 | __be32 src_ipaddr, tgt_ipaddr; |
1da177e4 LT |
84 | int i, ret; |
85 | ||
86 | #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg)) | |
87 | ||
88 | if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop, | |
89 | ARPT_INV_ARPOP)) { | |
90 | dprintf("ARP operation field mismatch.\n"); | |
91 | dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n", | |
92 | arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd, | |
97 | ARPT_INV_ARPHRD)) { | |
98 | dprintf("ARP hardware address format mismatch.\n"); | |
99 | dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n", | |
100 | arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask); | |
101 | return 0; | |
102 | } | |
103 | ||
104 | if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro, | |
105 | ARPT_INV_ARPPRO)) { | |
106 | dprintf("ARP protocol address format mismatch.\n"); | |
107 | dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n", | |
108 | arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln, | |
113 | ARPT_INV_ARPHLN)) { | |
114 | dprintf("ARP hardware address length mismatch.\n"); | |
115 | dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n", | |
116 | arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | src_devaddr = arpptr; | |
121 | arpptr += dev->addr_len; | |
122 | memcpy(&src_ipaddr, arpptr, sizeof(u32)); | |
123 | arpptr += sizeof(u32); | |
124 | tgt_devaddr = arpptr; | |
125 | arpptr += dev->addr_len; | |
126 | memcpy(&tgt_ipaddr, arpptr, sizeof(u32)); | |
127 | ||
128 | if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len), | |
129 | ARPT_INV_SRCDEVADDR) || | |
130 | FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len), | |
131 | ARPT_INV_TGTDEVADDR)) { | |
132 | dprintf("Source or target device address mismatch.\n"); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr, | |
138 | ARPT_INV_SRCIP) || | |
139 | FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr), | |
140 | ARPT_INV_TGTIP)) { | |
141 | dprintf("Source or target IP address mismatch.\n"); | |
142 | ||
143 | dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n", | |
144 | NIPQUAD(src_ipaddr), | |
145 | NIPQUAD(arpinfo->smsk.s_addr), | |
146 | NIPQUAD(arpinfo->src.s_addr), | |
147 | arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : ""); | |
148 | dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n", | |
149 | NIPQUAD(tgt_ipaddr), | |
150 | NIPQUAD(arpinfo->tmsk.s_addr), | |
151 | NIPQUAD(arpinfo->tgt.s_addr), | |
152 | arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : ""); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | /* Look for ifname matches. */ | |
157 | for (i = 0, ret = 0; i < IFNAMSIZ; i++) { | |
158 | ret |= (indev[i] ^ arpinfo->iniface[i]) | |
159 | & arpinfo->iniface_mask[i]; | |
160 | } | |
161 | ||
162 | if (FWINV(ret != 0, ARPT_INV_VIA_IN)) { | |
163 | dprintf("VIA in mismatch (%s vs %s).%s\n", | |
164 | indev, arpinfo->iniface, | |
165 | arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":""); | |
166 | return 0; | |
167 | } | |
168 | ||
49688c84 DM |
169 | for (i = 0, ret = 0; i < IFNAMSIZ; i++) { |
170 | ret |= (outdev[i] ^ arpinfo->outiface[i]) | |
171 | & arpinfo->outiface_mask[i]; | |
1da177e4 LT |
172 | } |
173 | ||
174 | if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) { | |
175 | dprintf("VIA out mismatch (%s vs %s).%s\n", | |
176 | outdev, arpinfo->outiface, | |
177 | arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":""); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | return 1; | |
182 | } | |
183 | ||
184 | static inline int arp_checkentry(const struct arpt_arp *arp) | |
185 | { | |
186 | if (arp->flags & ~ARPT_F_MASK) { | |
187 | duprintf("Unknown flag bits set: %08X\n", | |
188 | arp->flags & ~ARPT_F_MASK); | |
189 | return 0; | |
190 | } | |
191 | if (arp->invflags & ~ARPT_INV_MASK) { | |
192 | duprintf("Unknown invflag bits set: %08X\n", | |
193 | arp->invflags & ~ARPT_INV_MASK); | |
194 | return 0; | |
195 | } | |
196 | ||
197 | return 1; | |
198 | } | |
199 | ||
3db05fea | 200 | static unsigned int arpt_error(struct sk_buff *skb, |
1da177e4 LT |
201 | const struct net_device *in, |
202 | const struct net_device *out, | |
2e4e6a17 | 203 | unsigned int hooknum, |
c4986734 | 204 | const struct xt_target *target, |
fe1cb108 | 205 | const void *targinfo) |
1da177e4 LT |
206 | { |
207 | if (net_ratelimit()) | |
208 | printk("arp_tables: error: '%s'\n", (char *)targinfo); | |
209 | ||
210 | return NF_DROP; | |
211 | } | |
212 | ||
213 | static inline struct arpt_entry *get_entry(void *base, unsigned int offset) | |
214 | { | |
215 | return (struct arpt_entry *)(base + offset); | |
216 | } | |
217 | ||
3db05fea | 218 | unsigned int arpt_do_table(struct sk_buff *skb, |
1da177e4 LT |
219 | unsigned int hook, |
220 | const struct net_device *in, | |
221 | const struct net_device *out, | |
fe1cb108 | 222 | struct arpt_table *table) |
1da177e4 LT |
223 | { |
224 | static const char nulldevname[IFNAMSIZ]; | |
225 | unsigned int verdict = NF_DROP; | |
226 | struct arphdr *arp; | |
cff533ac | 227 | bool hotdrop = false; |
1da177e4 LT |
228 | struct arpt_entry *e, *back; |
229 | const char *indev, *outdev; | |
230 | void *table_base; | |
e0b7cde9 | 231 | struct xt_table_info *private; |
1da177e4 LT |
232 | |
233 | /* ARP header, plus 2 device addresses, plus 2 IP addresses. */ | |
3db05fea HX |
234 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + |
235 | (2 * skb->dev->addr_len) + | |
236 | (2 * sizeof(u32))))) | |
1da177e4 LT |
237 | return NF_DROP; |
238 | ||
239 | indev = in ? in->name : nulldevname; | |
240 | outdev = out ? out->name : nulldevname; | |
241 | ||
242 | read_lock_bh(&table->lock); | |
e0b7cde9 | 243 | private = table->private; |
2e4e6a17 HW |
244 | table_base = (void *)private->entries[smp_processor_id()]; |
245 | e = get_entry(table_base, private->hook_entry[hook]); | |
246 | back = get_entry(table_base, private->underflow[hook]); | |
1da177e4 | 247 | |
3db05fea | 248 | arp = arp_hdr(skb); |
1da177e4 | 249 | do { |
3db05fea | 250 | if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) { |
1da177e4 LT |
251 | struct arpt_entry_target *t; |
252 | int hdr_len; | |
253 | ||
254 | hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) + | |
3db05fea | 255 | (2 * skb->dev->addr_len); |
1da177e4 LT |
256 | ADD_COUNTER(e->counters, hdr_len, 1); |
257 | ||
258 | t = arpt_get_target(e); | |
259 | ||
260 | /* Standard target? */ | |
261 | if (!t->u.kernel.target->target) { | |
262 | int v; | |
263 | ||
264 | v = ((struct arpt_standard_target *)t)->verdict; | |
265 | if (v < 0) { | |
266 | /* Pop from stack? */ | |
267 | if (v != ARPT_RETURN) { | |
268 | verdict = (unsigned)(-v) - 1; | |
269 | break; | |
270 | } | |
271 | e = back; | |
272 | back = get_entry(table_base, | |
273 | back->comefrom); | |
274 | continue; | |
275 | } | |
276 | if (table_base + v | |
277 | != (void *)e + e->next_offset) { | |
278 | /* Save old back ptr in next entry */ | |
279 | struct arpt_entry *next | |
280 | = (void *)e + e->next_offset; | |
281 | next->comefrom = | |
282 | (void *)back - table_base; | |
283 | ||
284 | /* set back pointer to next entry */ | |
285 | back = next; | |
286 | } | |
287 | ||
288 | e = get_entry(table_base, v); | |
289 | } else { | |
290 | /* Targets which reenter must return | |
291 | * abs. verdicts | |
292 | */ | |
3db05fea | 293 | verdict = t->u.kernel.target->target(skb, |
1da177e4 | 294 | in, out, |
2e4e6a17 | 295 | hook, |
1c524830 | 296 | t->u.kernel.target, |
fe1cb108 | 297 | t->data); |
1da177e4 LT |
298 | |
299 | /* Target might have changed stuff. */ | |
3db05fea | 300 | arp = arp_hdr(skb); |
1da177e4 LT |
301 | |
302 | if (verdict == ARPT_CONTINUE) | |
303 | e = (void *)e + e->next_offset; | |
304 | else | |
305 | /* Verdict */ | |
306 | break; | |
307 | } | |
308 | } else { | |
309 | e = (void *)e + e->next_offset; | |
310 | } | |
311 | } while (!hotdrop); | |
312 | read_unlock_bh(&table->lock); | |
313 | ||
314 | if (hotdrop) | |
315 | return NF_DROP; | |
316 | else | |
317 | return verdict; | |
318 | } | |
319 | ||
1da177e4 LT |
320 | /* All zeroes == unconditional rule. */ |
321 | static inline int unconditional(const struct arpt_arp *arp) | |
322 | { | |
323 | unsigned int i; | |
324 | ||
325 | for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++) | |
326 | if (((__u32 *)arp)[i]) | |
327 | return 0; | |
328 | ||
329 | return 1; | |
330 | } | |
331 | ||
332 | /* Figures out from what hook each rule can be called: returns 0 if | |
333 | * there are loops. Puts hook bitmask in comefrom. | |
334 | */ | |
2e4e6a17 | 335 | static int mark_source_chains(struct xt_table_info *newinfo, |
31836064 | 336 | unsigned int valid_hooks, void *entry0) |
1da177e4 LT |
337 | { |
338 | unsigned int hook; | |
339 | ||
340 | /* No recursion; use packet counter to save back ptrs (reset | |
341 | * to 0 as we leave), and comefrom to save source hook bitmask. | |
342 | */ | |
343 | for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) { | |
344 | unsigned int pos = newinfo->hook_entry[hook]; | |
345 | struct arpt_entry *e | |
31836064 | 346 | = (struct arpt_entry *)(entry0 + pos); |
1da177e4 LT |
347 | |
348 | if (!(valid_hooks & (1 << hook))) | |
349 | continue; | |
350 | ||
351 | /* Set initial back pointer. */ | |
352 | e->counters.pcnt = pos; | |
353 | ||
354 | for (;;) { | |
355 | struct arpt_standard_target *t | |
356 | = (void *)arpt_get_target(e); | |
e1b4b9f3 | 357 | int visited = e->comefrom & (1 << hook); |
1da177e4 LT |
358 | |
359 | if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) { | |
360 | printk("arptables: loop hook %u pos %u %08X.\n", | |
361 | hook, pos, e->comefrom); | |
362 | return 0; | |
363 | } | |
364 | e->comefrom | |
365 | |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS)); | |
366 | ||
367 | /* Unconditional return/END. */ | |
e1b4b9f3 | 368 | if ((e->target_offset == sizeof(struct arpt_entry) |
1da177e4 LT |
369 | && (strcmp(t->target.u.user.name, |
370 | ARPT_STANDARD_TARGET) == 0) | |
371 | && t->verdict < 0 | |
e1b4b9f3 | 372 | && unconditional(&e->arp)) || visited) { |
1da177e4 LT |
373 | unsigned int oldpos, size; |
374 | ||
74c9c0c1 DM |
375 | if (t->verdict < -NF_MAX_VERDICT - 1) { |
376 | duprintf("mark_source_chains: bad " | |
377 | "negative verdict (%i)\n", | |
378 | t->verdict); | |
379 | return 0; | |
380 | } | |
381 | ||
1da177e4 LT |
382 | /* Return: backtrack through the last |
383 | * big jump. | |
384 | */ | |
385 | do { | |
386 | e->comefrom ^= (1<<NF_ARP_NUMHOOKS); | |
387 | oldpos = pos; | |
388 | pos = e->counters.pcnt; | |
389 | e->counters.pcnt = 0; | |
390 | ||
391 | /* We're at the start. */ | |
392 | if (pos == oldpos) | |
393 | goto next; | |
394 | ||
395 | e = (struct arpt_entry *) | |
31836064 | 396 | (entry0 + pos); |
1da177e4 LT |
397 | } while (oldpos == pos + e->next_offset); |
398 | ||
399 | /* Move along one */ | |
400 | size = e->next_offset; | |
401 | e = (struct arpt_entry *) | |
31836064 | 402 | (entry0 + pos + size); |
1da177e4 LT |
403 | e->counters.pcnt = pos; |
404 | pos += size; | |
405 | } else { | |
406 | int newpos = t->verdict; | |
407 | ||
408 | if (strcmp(t->target.u.user.name, | |
409 | ARPT_STANDARD_TARGET) == 0 | |
410 | && newpos >= 0) { | |
74c9c0c1 DM |
411 | if (newpos > newinfo->size - |
412 | sizeof(struct arpt_entry)) { | |
413 | duprintf("mark_source_chains: " | |
414 | "bad verdict (%i)\n", | |
415 | newpos); | |
416 | return 0; | |
417 | } | |
418 | ||
1da177e4 LT |
419 | /* This a jump; chase it. */ |
420 | duprintf("Jump rule %u -> %u\n", | |
421 | pos, newpos); | |
422 | } else { | |
423 | /* ... this is a fallthru */ | |
424 | newpos = pos + e->next_offset; | |
425 | } | |
426 | e = (struct arpt_entry *) | |
31836064 | 427 | (entry0 + newpos); |
1da177e4 LT |
428 | e->counters.pcnt = pos; |
429 | pos = newpos; | |
430 | } | |
431 | } | |
432 | next: | |
433 | duprintf("Finished chain %u\n", hook); | |
434 | } | |
435 | return 1; | |
436 | } | |
437 | ||
fb5b6095 | 438 | static inline int check_entry(struct arpt_entry *e, const char *name) |
1da177e4 LT |
439 | { |
440 | struct arpt_entry_target *t; | |
1da177e4 LT |
441 | |
442 | if (!arp_checkentry(&e->arp)) { | |
443 | duprintf("arp_tables: arp check failed %p %s.\n", e, name); | |
444 | return -EINVAL; | |
445 | } | |
446 | ||
590bdf7f DM |
447 | if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset) |
448 | return -EINVAL; | |
449 | ||
1da177e4 | 450 | t = arpt_get_target(e); |
590bdf7f DM |
451 | if (e->target_offset + t->u.target_size > e->next_offset) |
452 | return -EINVAL; | |
453 | ||
fb5b6095 PM |
454 | return 0; |
455 | } | |
456 | ||
457 | static inline int check_target(struct arpt_entry *e, const char *name) | |
458 | { | |
459 | struct arpt_entry_target *t; | |
460 | struct arpt_target *target; | |
461 | int ret; | |
462 | ||
463 | t = arpt_get_target(e); | |
464 | target = t->u.kernel.target; | |
465 | ||
466 | ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t), | |
467 | name, e->comefrom, 0, 0); | |
468 | if (!ret && t->u.kernel.target->checkentry | |
469 | && !t->u.kernel.target->checkentry(name, e, target, t->data, | |
470 | e->comefrom)) { | |
471 | duprintf("arp_tables: check failed for `%s'.\n", | |
472 | t->u.kernel.target->name); | |
473 | ret = -EINVAL; | |
474 | } | |
475 | return ret; | |
476 | } | |
477 | ||
478 | static inline int | |
479 | find_check_entry(struct arpt_entry *e, const char *name, unsigned int size, | |
480 | unsigned int *i) | |
481 | { | |
482 | struct arpt_entry_target *t; | |
483 | struct arpt_target *target; | |
484 | int ret; | |
485 | ||
486 | ret = check_entry(e, name); | |
487 | if (ret) | |
488 | return ret; | |
489 | ||
490 | t = arpt_get_target(e); | |
2e4e6a17 HW |
491 | target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name, |
492 | t->u.user.revision), | |
6b7d31fc HW |
493 | "arpt_%s", t->u.user.name); |
494 | if (IS_ERR(target) || !target) { | |
fb5b6095 | 495 | duprintf("find_check_entry: `%s' not found\n", t->u.user.name); |
6b7d31fc | 496 | ret = target ? PTR_ERR(target) : -ENOENT; |
1da177e4 LT |
497 | goto out; |
498 | } | |
1da177e4 | 499 | t->u.kernel.target = target; |
1da177e4 | 500 | |
fb5b6095 | 501 | ret = check_target(e, name); |
3cdc7c95 PM |
502 | if (ret) |
503 | goto err; | |
504 | ||
1da177e4 LT |
505 | (*i)++; |
506 | return 0; | |
3cdc7c95 PM |
507 | err: |
508 | module_put(t->u.kernel.target->me); | |
1da177e4 LT |
509 | out: |
510 | return ret; | |
511 | } | |
512 | ||
513 | static inline int check_entry_size_and_hooks(struct arpt_entry *e, | |
2e4e6a17 | 514 | struct xt_table_info *newinfo, |
1da177e4 LT |
515 | unsigned char *base, |
516 | unsigned char *limit, | |
517 | const unsigned int *hook_entries, | |
518 | const unsigned int *underflows, | |
519 | unsigned int *i) | |
520 | { | |
521 | unsigned int h; | |
522 | ||
523 | if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 | |
524 | || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) { | |
525 | duprintf("Bad offset %p\n", e); | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
529 | if (e->next_offset | |
530 | < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) { | |
531 | duprintf("checking: element %p size %u\n", | |
532 | e, e->next_offset); | |
533 | return -EINVAL; | |
534 | } | |
535 | ||
536 | /* Check hooks & underflows */ | |
537 | for (h = 0; h < NF_ARP_NUMHOOKS; h++) { | |
538 | if ((unsigned char *)e - base == hook_entries[h]) | |
539 | newinfo->hook_entry[h] = hook_entries[h]; | |
540 | if ((unsigned char *)e - base == underflows[h]) | |
541 | newinfo->underflow[h] = underflows[h]; | |
542 | } | |
543 | ||
544 | /* FIXME: underflows must be unconditional, standard verdicts | |
e905a9ed | 545 | < 0 (not ARPT_RETURN). --RR */ |
1da177e4 LT |
546 | |
547 | /* Clear counters and comefrom */ | |
2e4e6a17 | 548 | e->counters = ((struct xt_counters) { 0, 0 }); |
1da177e4 LT |
549 | e->comefrom = 0; |
550 | ||
551 | (*i)++; | |
552 | return 0; | |
553 | } | |
554 | ||
555 | static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i) | |
556 | { | |
557 | struct arpt_entry_target *t; | |
558 | ||
559 | if (i && (*i)-- == 0) | |
560 | return 1; | |
561 | ||
562 | t = arpt_get_target(e); | |
563 | if (t->u.kernel.target->destroy) | |
efa74165 | 564 | t->u.kernel.target->destroy(t->u.kernel.target, t->data); |
1da177e4 LT |
565 | module_put(t->u.kernel.target->me); |
566 | return 0; | |
567 | } | |
568 | ||
569 | /* Checks and translates the user-supplied table segment (held in | |
570 | * newinfo). | |
571 | */ | |
572 | static int translate_table(const char *name, | |
573 | unsigned int valid_hooks, | |
2e4e6a17 | 574 | struct xt_table_info *newinfo, |
31836064 | 575 | void *entry0, |
1da177e4 LT |
576 | unsigned int size, |
577 | unsigned int number, | |
578 | const unsigned int *hook_entries, | |
579 | const unsigned int *underflows) | |
580 | { | |
581 | unsigned int i; | |
582 | int ret; | |
583 | ||
584 | newinfo->size = size; | |
585 | newinfo->number = number; | |
586 | ||
587 | /* Init all hooks to impossible value. */ | |
588 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
589 | newinfo->hook_entry[i] = 0xFFFFFFFF; | |
590 | newinfo->underflow[i] = 0xFFFFFFFF; | |
591 | } | |
592 | ||
593 | duprintf("translate_table: size %u\n", newinfo->size); | |
594 | i = 0; | |
595 | ||
596 | /* Walk through entries, checking offsets. */ | |
31836064 | 597 | ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size, |
1da177e4 LT |
598 | check_entry_size_and_hooks, |
599 | newinfo, | |
31836064 ED |
600 | entry0, |
601 | entry0 + size, | |
1da177e4 LT |
602 | hook_entries, underflows, &i); |
603 | duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret); | |
604 | if (ret != 0) | |
605 | return ret; | |
606 | ||
607 | if (i != number) { | |
608 | duprintf("translate_table: %u not %u entries\n", | |
609 | i, number); | |
610 | return -EINVAL; | |
611 | } | |
612 | ||
613 | /* Check hooks all assigned */ | |
614 | for (i = 0; i < NF_ARP_NUMHOOKS; i++) { | |
615 | /* Only hooks which are valid */ | |
616 | if (!(valid_hooks & (1 << i))) | |
617 | continue; | |
618 | if (newinfo->hook_entry[i] == 0xFFFFFFFF) { | |
619 | duprintf("Invalid hook entry %u %u\n", | |
620 | i, hook_entries[i]); | |
621 | return -EINVAL; | |
622 | } | |
623 | if (newinfo->underflow[i] == 0xFFFFFFFF) { | |
624 | duprintf("Invalid underflow %u %u\n", | |
625 | i, underflows[i]); | |
626 | return -EINVAL; | |
627 | } | |
628 | } | |
629 | ||
74c9c0c1 DM |
630 | if (!mark_source_chains(newinfo, valid_hooks, entry0)) { |
631 | duprintf("Looping hook\n"); | |
632 | return -ELOOP; | |
633 | } | |
634 | ||
1da177e4 LT |
635 | /* Finally, each sanity check must pass */ |
636 | i = 0; | |
31836064 | 637 | ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size, |
fb5b6095 | 638 | find_check_entry, name, size, &i); |
1da177e4 | 639 | |
74c9c0c1 DM |
640 | if (ret != 0) { |
641 | ARPT_ENTRY_ITERATE(entry0, newinfo->size, | |
642 | cleanup_entry, &i); | |
643 | return ret; | |
1da177e4 LT |
644 | } |
645 | ||
646 | /* And one copy for every other CPU */ | |
6f912042 | 647 | for_each_possible_cpu(i) { |
31836064 ED |
648 | if (newinfo->entries[i] && newinfo->entries[i] != entry0) |
649 | memcpy(newinfo->entries[i], entry0, newinfo->size); | |
1da177e4 LT |
650 | } |
651 | ||
652 | return ret; | |
653 | } | |
654 | ||
1da177e4 LT |
655 | /* Gets counters. */ |
656 | static inline int add_entry_to_counter(const struct arpt_entry *e, | |
2e4e6a17 | 657 | struct xt_counters total[], |
1da177e4 LT |
658 | unsigned int *i) |
659 | { | |
660 | ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt); | |
661 | ||
662 | (*i)++; | |
663 | return 0; | |
664 | } | |
665 | ||
31836064 | 666 | static inline int set_entry_to_counter(const struct arpt_entry *e, |
2e4e6a17 | 667 | struct xt_counters total[], |
31836064 ED |
668 | unsigned int *i) |
669 | { | |
670 | SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt); | |
671 | ||
672 | (*i)++; | |
673 | return 0; | |
674 | } | |
675 | ||
2e4e6a17 HW |
676 | static void get_counters(const struct xt_table_info *t, |
677 | struct xt_counters counters[]) | |
1da177e4 LT |
678 | { |
679 | unsigned int cpu; | |
680 | unsigned int i; | |
31836064 ED |
681 | unsigned int curcpu; |
682 | ||
683 | /* Instead of clearing (by a previous call to memset()) | |
684 | * the counters and using adds, we set the counters | |
685 | * with data used by 'current' CPU | |
686 | * We dont care about preemption here. | |
687 | */ | |
688 | curcpu = raw_smp_processor_id(); | |
689 | ||
690 | i = 0; | |
691 | ARPT_ENTRY_ITERATE(t->entries[curcpu], | |
692 | t->size, | |
693 | set_entry_to_counter, | |
694 | counters, | |
695 | &i); | |
1da177e4 | 696 | |
6f912042 | 697 | for_each_possible_cpu(cpu) { |
31836064 ED |
698 | if (cpu == curcpu) |
699 | continue; | |
1da177e4 | 700 | i = 0; |
31836064 | 701 | ARPT_ENTRY_ITERATE(t->entries[cpu], |
1da177e4 LT |
702 | t->size, |
703 | add_entry_to_counter, | |
704 | counters, | |
705 | &i); | |
706 | } | |
707 | } | |
708 | ||
709 | static int copy_entries_to_user(unsigned int total_size, | |
710 | struct arpt_table *table, | |
711 | void __user *userptr) | |
712 | { | |
713 | unsigned int off, num, countersize; | |
714 | struct arpt_entry *e; | |
2e4e6a17 HW |
715 | struct xt_counters *counters; |
716 | struct xt_table_info *private = table->private; | |
1da177e4 | 717 | int ret = 0; |
31836064 | 718 | void *loc_cpu_entry; |
1da177e4 LT |
719 | |
720 | /* We need atomic snapshot of counters: rest doesn't change | |
721 | * (other than comefrom, which userspace doesn't care | |
722 | * about). | |
723 | */ | |
2e4e6a17 HW |
724 | countersize = sizeof(struct xt_counters) * private->number; |
725 | counters = vmalloc_node(countersize, numa_node_id()); | |
1da177e4 LT |
726 | |
727 | if (counters == NULL) | |
728 | return -ENOMEM; | |
729 | ||
730 | /* First, sum counters... */ | |
1da177e4 | 731 | write_lock_bh(&table->lock); |
2e4e6a17 | 732 | get_counters(private, counters); |
1da177e4 LT |
733 | write_unlock_bh(&table->lock); |
734 | ||
2e4e6a17 | 735 | loc_cpu_entry = private->entries[raw_smp_processor_id()]; |
31836064 ED |
736 | /* ... then copy entire thing ... */ |
737 | if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) { | |
1da177e4 LT |
738 | ret = -EFAULT; |
739 | goto free_counters; | |
740 | } | |
741 | ||
742 | /* FIXME: use iterator macros --RR */ | |
743 | /* ... then go back and fix counters and names */ | |
744 | for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ | |
745 | struct arpt_entry_target *t; | |
746 | ||
31836064 | 747 | e = (struct arpt_entry *)(loc_cpu_entry + off); |
1da177e4 LT |
748 | if (copy_to_user(userptr + off |
749 | + offsetof(struct arpt_entry, counters), | |
750 | &counters[num], | |
751 | sizeof(counters[num])) != 0) { | |
752 | ret = -EFAULT; | |
753 | goto free_counters; | |
754 | } | |
755 | ||
756 | t = arpt_get_target(e); | |
757 | if (copy_to_user(userptr + off + e->target_offset | |
758 | + offsetof(struct arpt_entry_target, | |
759 | u.user.name), | |
760 | t->u.kernel.target->name, | |
761 | strlen(t->u.kernel.target->name)+1) != 0) { | |
762 | ret = -EFAULT; | |
763 | goto free_counters; | |
764 | } | |
765 | } | |
766 | ||
767 | free_counters: | |
768 | vfree(counters); | |
769 | return ret; | |
770 | } | |
771 | ||
772 | static int get_entries(const struct arpt_get_entries *entries, | |
773 | struct arpt_get_entries __user *uptr) | |
774 | { | |
775 | int ret; | |
776 | struct arpt_table *t; | |
777 | ||
2e4e6a17 | 778 | t = xt_find_table_lock(NF_ARP, entries->name); |
31fe4d33 | 779 | if (t && !IS_ERR(t)) { |
2e4e6a17 | 780 | struct xt_table_info *private = t->private; |
1da177e4 | 781 | duprintf("t->private->number = %u\n", |
2e4e6a17 HW |
782 | private->number); |
783 | if (entries->size == private->size) | |
784 | ret = copy_entries_to_user(private->size, | |
1da177e4 LT |
785 | t, uptr->entrytable); |
786 | else { | |
787 | duprintf("get_entries: I've got %u not %u!\n", | |
2e4e6a17 | 788 | private->size, entries->size); |
1da177e4 LT |
789 | ret = -EINVAL; |
790 | } | |
6b7d31fc | 791 | module_put(t->me); |
2e4e6a17 | 792 | xt_table_unlock(t); |
1da177e4 | 793 | } else |
6b7d31fc | 794 | ret = t ? PTR_ERR(t) : -ENOENT; |
1da177e4 LT |
795 | |
796 | return ret; | |
797 | } | |
798 | ||
799 | static int do_replace(void __user *user, unsigned int len) | |
800 | { | |
801 | int ret; | |
802 | struct arpt_replace tmp; | |
803 | struct arpt_table *t; | |
2e4e6a17 HW |
804 | struct xt_table_info *newinfo, *oldinfo; |
805 | struct xt_counters *counters; | |
31836064 | 806 | void *loc_cpu_entry, *loc_cpu_old_entry; |
1da177e4 LT |
807 | |
808 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
809 | return -EFAULT; | |
810 | ||
ee4bb818 | 811 | /* overflow check */ |
ee4bb818 KK |
812 | if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) |
813 | return -ENOMEM; | |
814 | ||
2e4e6a17 | 815 | newinfo = xt_alloc_table_info(tmp.size); |
1da177e4 LT |
816 | if (!newinfo) |
817 | return -ENOMEM; | |
818 | ||
31836064 ED |
819 | /* choose the copy that is on our node/cpu */ |
820 | loc_cpu_entry = newinfo->entries[raw_smp_processor_id()]; | |
821 | if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), | |
1da177e4 LT |
822 | tmp.size) != 0) { |
823 | ret = -EFAULT; | |
824 | goto free_newinfo; | |
825 | } | |
826 | ||
19763120 PM |
827 | counters = vmalloc_node(tmp.num_counters * sizeof(struct xt_counters), |
828 | numa_node_id()); | |
1da177e4 LT |
829 | if (!counters) { |
830 | ret = -ENOMEM; | |
831 | goto free_newinfo; | |
832 | } | |
1da177e4 LT |
833 | |
834 | ret = translate_table(tmp.name, tmp.valid_hooks, | |
31836064 | 835 | newinfo, loc_cpu_entry, tmp.size, tmp.num_entries, |
1da177e4 LT |
836 | tmp.hook_entry, tmp.underflow); |
837 | if (ret != 0) | |
838 | goto free_newinfo_counters; | |
839 | ||
840 | duprintf("arp_tables: Translated table\n"); | |
841 | ||
2e4e6a17 | 842 | t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name), |
6b7d31fc HW |
843 | "arptable_%s", tmp.name); |
844 | if (!t || IS_ERR(t)) { | |
845 | ret = t ? PTR_ERR(t) : -ENOENT; | |
1da177e4 | 846 | goto free_newinfo_counters_untrans; |
6b7d31fc | 847 | } |
1da177e4 LT |
848 | |
849 | /* You lied! */ | |
850 | if (tmp.valid_hooks != t->valid_hooks) { | |
851 | duprintf("Valid hook crap: %08X vs %08X\n", | |
852 | tmp.valid_hooks, t->valid_hooks); | |
853 | ret = -EINVAL; | |
6b7d31fc | 854 | goto put_module; |
1da177e4 LT |
855 | } |
856 | ||
2e4e6a17 | 857 | oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret); |
1da177e4 LT |
858 | if (!oldinfo) |
859 | goto put_module; | |
860 | ||
861 | /* Update module usage count based on number of rules */ | |
862 | duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n", | |
863 | oldinfo->number, oldinfo->initial_entries, newinfo->number); | |
e905a9ed YH |
864 | if ((oldinfo->number > oldinfo->initial_entries) || |
865 | (newinfo->number <= oldinfo->initial_entries)) | |
1da177e4 LT |
866 | module_put(t->me); |
867 | if ((oldinfo->number > oldinfo->initial_entries) && | |
868 | (newinfo->number <= oldinfo->initial_entries)) | |
869 | module_put(t->me); | |
870 | ||
871 | /* Get the old counters. */ | |
872 | get_counters(oldinfo, counters); | |
873 | /* Decrease module usage counts and free resource */ | |
31836064 ED |
874 | loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()]; |
875 | ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL); | |
876 | ||
2e4e6a17 | 877 | xt_free_table_info(oldinfo); |
1da177e4 | 878 | if (copy_to_user(tmp.counters, counters, |
2e4e6a17 | 879 | sizeof(struct xt_counters) * tmp.num_counters) != 0) |
1da177e4 LT |
880 | ret = -EFAULT; |
881 | vfree(counters); | |
2e4e6a17 | 882 | xt_table_unlock(t); |
1da177e4 LT |
883 | return ret; |
884 | ||
885 | put_module: | |
886 | module_put(t->me); | |
2e4e6a17 | 887 | xt_table_unlock(t); |
1da177e4 | 888 | free_newinfo_counters_untrans: |
31836064 | 889 | ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL); |
1da177e4 LT |
890 | free_newinfo_counters: |
891 | vfree(counters); | |
892 | free_newinfo: | |
2e4e6a17 | 893 | xt_free_table_info(newinfo); |
1da177e4 LT |
894 | return ret; |
895 | } | |
896 | ||
897 | /* We're lazy, and add to the first CPU; overflow works its fey magic | |
898 | * and everything is OK. | |
899 | */ | |
900 | static inline int add_counter_to_entry(struct arpt_entry *e, | |
2e4e6a17 | 901 | const struct xt_counters addme[], |
1da177e4 LT |
902 | unsigned int *i) |
903 | { | |
904 | ||
905 | ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt); | |
906 | ||
907 | (*i)++; | |
908 | return 0; | |
909 | } | |
910 | ||
911 | static int do_add_counters(void __user *user, unsigned int len) | |
912 | { | |
913 | unsigned int i; | |
2e4e6a17 | 914 | struct xt_counters_info tmp, *paddc; |
1da177e4 | 915 | struct arpt_table *t; |
2e4e6a17 | 916 | struct xt_table_info *private; |
6b7d31fc | 917 | int ret = 0; |
31836064 | 918 | void *loc_cpu_entry; |
1da177e4 LT |
919 | |
920 | if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) | |
921 | return -EFAULT; | |
922 | ||
2e4e6a17 | 923 | if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters)) |
1da177e4 LT |
924 | return -EINVAL; |
925 | ||
19763120 | 926 | paddc = vmalloc_node(len, numa_node_id()); |
1da177e4 LT |
927 | if (!paddc) |
928 | return -ENOMEM; | |
929 | ||
930 | if (copy_from_user(paddc, user, len) != 0) { | |
931 | ret = -EFAULT; | |
932 | goto free; | |
933 | } | |
934 | ||
2e4e6a17 | 935 | t = xt_find_table_lock(NF_ARP, tmp.name); |
6b7d31fc HW |
936 | if (!t || IS_ERR(t)) { |
937 | ret = t ? PTR_ERR(t) : -ENOENT; | |
1da177e4 | 938 | goto free; |
6b7d31fc | 939 | } |
1da177e4 LT |
940 | |
941 | write_lock_bh(&t->lock); | |
2e4e6a17 | 942 | private = t->private; |
2c8ac66b | 943 | if (private->number != tmp.num_counters) { |
1da177e4 LT |
944 | ret = -EINVAL; |
945 | goto unlock_up_free; | |
946 | } | |
947 | ||
948 | i = 0; | |
31836064 | 949 | /* Choose the copy that is on our node */ |
2e4e6a17 | 950 | loc_cpu_entry = private->entries[smp_processor_id()]; |
31836064 | 951 | ARPT_ENTRY_ITERATE(loc_cpu_entry, |
2e4e6a17 | 952 | private->size, |
1da177e4 LT |
953 | add_counter_to_entry, |
954 | paddc->counters, | |
955 | &i); | |
956 | unlock_up_free: | |
957 | write_unlock_bh(&t->lock); | |
2e4e6a17 | 958 | xt_table_unlock(t); |
6b7d31fc | 959 | module_put(t->me); |
1da177e4 LT |
960 | free: |
961 | vfree(paddc); | |
962 | ||
963 | return ret; | |
964 | } | |
965 | ||
966 | static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) | |
967 | { | |
968 | int ret; | |
969 | ||
970 | if (!capable(CAP_NET_ADMIN)) | |
971 | return -EPERM; | |
972 | ||
973 | switch (cmd) { | |
974 | case ARPT_SO_SET_REPLACE: | |
975 | ret = do_replace(user, len); | |
976 | break; | |
977 | ||
978 | case ARPT_SO_SET_ADD_COUNTERS: | |
979 | ret = do_add_counters(user, len); | |
980 | break; | |
981 | ||
982 | default: | |
983 | duprintf("do_arpt_set_ctl: unknown request %i\n", cmd); | |
984 | ret = -EINVAL; | |
985 | } | |
986 | ||
987 | return ret; | |
988 | } | |
989 | ||
990 | static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) | |
991 | { | |
992 | int ret; | |
993 | ||
994 | if (!capable(CAP_NET_ADMIN)) | |
995 | return -EPERM; | |
996 | ||
997 | switch (cmd) { | |
998 | case ARPT_SO_GET_INFO: { | |
999 | char name[ARPT_TABLE_MAXNAMELEN]; | |
1000 | struct arpt_table *t; | |
1001 | ||
1002 | if (*len != sizeof(struct arpt_getinfo)) { | |
1003 | duprintf("length %u != %Zu\n", *len, | |
1004 | sizeof(struct arpt_getinfo)); | |
1005 | ret = -EINVAL; | |
1006 | break; | |
1007 | } | |
1008 | ||
1009 | if (copy_from_user(name, user, sizeof(name)) != 0) { | |
1010 | ret = -EFAULT; | |
1011 | break; | |
1012 | } | |
1013 | name[ARPT_TABLE_MAXNAMELEN-1] = '\0'; | |
6b7d31fc | 1014 | |
2e4e6a17 | 1015 | t = try_then_request_module(xt_find_table_lock(NF_ARP, name), |
6b7d31fc HW |
1016 | "arptable_%s", name); |
1017 | if (t && !IS_ERR(t)) { | |
1da177e4 | 1018 | struct arpt_getinfo info; |
2e4e6a17 | 1019 | struct xt_table_info *private = t->private; |
1da177e4 LT |
1020 | |
1021 | info.valid_hooks = t->valid_hooks; | |
2e4e6a17 | 1022 | memcpy(info.hook_entry, private->hook_entry, |
1da177e4 | 1023 | sizeof(info.hook_entry)); |
2e4e6a17 | 1024 | memcpy(info.underflow, private->underflow, |
1da177e4 | 1025 | sizeof(info.underflow)); |
2e4e6a17 HW |
1026 | info.num_entries = private->number; |
1027 | info.size = private->size; | |
1da177e4 LT |
1028 | strcpy(info.name, name); |
1029 | ||
1030 | if (copy_to_user(user, &info, *len) != 0) | |
1031 | ret = -EFAULT; | |
1032 | else | |
1033 | ret = 0; | |
2e4e6a17 | 1034 | xt_table_unlock(t); |
6b7d31fc HW |
1035 | module_put(t->me); |
1036 | } else | |
1037 | ret = t ? PTR_ERR(t) : -ENOENT; | |
1da177e4 LT |
1038 | } |
1039 | break; | |
1040 | ||
1041 | case ARPT_SO_GET_ENTRIES: { | |
1042 | struct arpt_get_entries get; | |
1043 | ||
1044 | if (*len < sizeof(get)) { | |
1045 | duprintf("get_entries: %u < %Zu\n", *len, sizeof(get)); | |
1046 | ret = -EINVAL; | |
1047 | } else if (copy_from_user(&get, user, sizeof(get)) != 0) { | |
1048 | ret = -EFAULT; | |
1049 | } else if (*len != sizeof(struct arpt_get_entries) + get.size) { | |
1050 | duprintf("get_entries: %u != %Zu\n", *len, | |
1051 | sizeof(struct arpt_get_entries) + get.size); | |
1052 | ret = -EINVAL; | |
1053 | } else | |
1054 | ret = get_entries(&get, user); | |
1055 | break; | |
1056 | } | |
1057 | ||
6b7d31fc | 1058 | case ARPT_SO_GET_REVISION_TARGET: { |
2e4e6a17 | 1059 | struct xt_get_revision rev; |
6b7d31fc HW |
1060 | |
1061 | if (*len != sizeof(rev)) { | |
1062 | ret = -EINVAL; | |
1063 | break; | |
1064 | } | |
1065 | if (copy_from_user(&rev, user, sizeof(rev)) != 0) { | |
1066 | ret = -EFAULT; | |
1067 | break; | |
1068 | } | |
1069 | ||
2e4e6a17 HW |
1070 | try_then_request_module(xt_find_revision(NF_ARP, rev.name, |
1071 | rev.revision, 1, &ret), | |
6b7d31fc HW |
1072 | "arpt_%s", rev.name); |
1073 | break; | |
1074 | } | |
1075 | ||
1da177e4 LT |
1076 | default: |
1077 | duprintf("do_arpt_get_ctl: unknown request %i\n", cmd); | |
1078 | ret = -EINVAL; | |
1079 | } | |
1080 | ||
1081 | return ret; | |
1082 | } | |
1083 | ||
1da177e4 LT |
1084 | int arpt_register_table(struct arpt_table *table, |
1085 | const struct arpt_replace *repl) | |
1086 | { | |
1087 | int ret; | |
2e4e6a17 | 1088 | struct xt_table_info *newinfo; |
259d4e41 | 1089 | struct xt_table_info bootstrap |
1da177e4 | 1090 | = { 0, 0, 0, { 0 }, { 0 }, { } }; |
31836064 | 1091 | void *loc_cpu_entry; |
1da177e4 | 1092 | |
2e4e6a17 | 1093 | newinfo = xt_alloc_table_info(repl->size); |
1da177e4 LT |
1094 | if (!newinfo) { |
1095 | ret = -ENOMEM; | |
1096 | return ret; | |
1097 | } | |
31836064 ED |
1098 | |
1099 | /* choose the copy on our node/cpu */ | |
1100 | loc_cpu_entry = newinfo->entries[raw_smp_processor_id()]; | |
1101 | memcpy(loc_cpu_entry, repl->entries, repl->size); | |
1da177e4 LT |
1102 | |
1103 | ret = translate_table(table->name, table->valid_hooks, | |
31836064 | 1104 | newinfo, loc_cpu_entry, repl->size, |
1da177e4 LT |
1105 | repl->num_entries, |
1106 | repl->hook_entry, | |
1107 | repl->underflow); | |
2e4e6a17 | 1108 | |
1da177e4 LT |
1109 | duprintf("arpt_register_table: translate table gives %d\n", ret); |
1110 | if (ret != 0) { | |
2e4e6a17 | 1111 | xt_free_table_info(newinfo); |
1da177e4 LT |
1112 | return ret; |
1113 | } | |
1114 | ||
da298d3a PM |
1115 | ret = xt_register_table(table, &bootstrap, newinfo); |
1116 | if (ret != 0) { | |
2e4e6a17 | 1117 | xt_free_table_info(newinfo); |
1da177e4 LT |
1118 | return ret; |
1119 | } | |
1120 | ||
2e4e6a17 | 1121 | return 0; |
1da177e4 LT |
1122 | } |
1123 | ||
1124 | void arpt_unregister_table(struct arpt_table *table) | |
1125 | { | |
2e4e6a17 | 1126 | struct xt_table_info *private; |
31836064 ED |
1127 | void *loc_cpu_entry; |
1128 | ||
2e4e6a17 | 1129 | private = xt_unregister_table(table); |
1da177e4 LT |
1130 | |
1131 | /* Decrease module usage counts and free resources */ | |
2e4e6a17 HW |
1132 | loc_cpu_entry = private->entries[raw_smp_processor_id()]; |
1133 | ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size, | |
1da177e4 | 1134 | cleanup_entry, NULL); |
2e4e6a17 | 1135 | xt_free_table_info(private); |
1da177e4 LT |
1136 | } |
1137 | ||
1138 | /* The built-in targets: standard (NULL) and error. */ | |
9f15c530 | 1139 | static struct arpt_target arpt_standard_target __read_mostly = { |
1da177e4 | 1140 | .name = ARPT_STANDARD_TARGET, |
aa83c1ab | 1141 | .targetsize = sizeof(int), |
a45049c5 | 1142 | .family = NF_ARP, |
1da177e4 LT |
1143 | }; |
1144 | ||
9f15c530 | 1145 | static struct arpt_target arpt_error_target __read_mostly = { |
1da177e4 LT |
1146 | .name = ARPT_ERROR_TARGET, |
1147 | .target = arpt_error, | |
aa83c1ab | 1148 | .targetsize = ARPT_FUNCTION_MAXNAMELEN, |
a45049c5 | 1149 | .family = NF_ARP, |
1da177e4 LT |
1150 | }; |
1151 | ||
1152 | static struct nf_sockopt_ops arpt_sockopts = { | |
1153 | .pf = PF_INET, | |
1154 | .set_optmin = ARPT_BASE_CTL, | |
1155 | .set_optmax = ARPT_SO_SET_MAX+1, | |
1156 | .set = do_arpt_set_ctl, | |
1157 | .get_optmin = ARPT_BASE_CTL, | |
1158 | .get_optmax = ARPT_SO_GET_MAX+1, | |
1159 | .get = do_arpt_get_ctl, | |
16fcec35 | 1160 | .owner = THIS_MODULE, |
1da177e4 LT |
1161 | }; |
1162 | ||
65b4b4e8 | 1163 | static int __init arp_tables_init(void) |
1da177e4 LT |
1164 | { |
1165 | int ret; | |
1166 | ||
0eff66e6 PM |
1167 | ret = xt_proto_init(NF_ARP); |
1168 | if (ret < 0) | |
1169 | goto err1; | |
2e4e6a17 | 1170 | |
1da177e4 | 1171 | /* Noone else will be downing sem now, so we won't sleep */ |
0eff66e6 PM |
1172 | ret = xt_register_target(&arpt_standard_target); |
1173 | if (ret < 0) | |
1174 | goto err2; | |
1175 | ret = xt_register_target(&arpt_error_target); | |
1176 | if (ret < 0) | |
1177 | goto err3; | |
1da177e4 LT |
1178 | |
1179 | /* Register setsockopt */ | |
1180 | ret = nf_register_sockopt(&arpt_sockopts); | |
0eff66e6 PM |
1181 | if (ret < 0) |
1182 | goto err4; | |
1da177e4 | 1183 | |
a887c1c1 | 1184 | printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n"); |
1da177e4 | 1185 | return 0; |
0eff66e6 PM |
1186 | |
1187 | err4: | |
1188 | xt_unregister_target(&arpt_error_target); | |
1189 | err3: | |
1190 | xt_unregister_target(&arpt_standard_target); | |
1191 | err2: | |
1192 | xt_proto_fini(NF_ARP); | |
1193 | err1: | |
1194 | return ret; | |
1da177e4 LT |
1195 | } |
1196 | ||
65b4b4e8 | 1197 | static void __exit arp_tables_fini(void) |
1da177e4 LT |
1198 | { |
1199 | nf_unregister_sockopt(&arpt_sockopts); | |
f603b6ec PM |
1200 | xt_unregister_target(&arpt_error_target); |
1201 | xt_unregister_target(&arpt_standard_target); | |
2e4e6a17 | 1202 | xt_proto_fini(NF_ARP); |
1da177e4 LT |
1203 | } |
1204 | ||
1205 | EXPORT_SYMBOL(arpt_register_table); | |
1206 | EXPORT_SYMBOL(arpt_unregister_table); | |
1207 | EXPORT_SYMBOL(arpt_do_table); | |
1da177e4 | 1208 | |
65b4b4e8 AM |
1209 | module_init(arp_tables_init); |
1210 | module_exit(arp_tables_fini); |