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