| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Packet matching code for ARP packets. |
| 4 |
* |
| 5 |
* Based heavily, if not almost entirely, upon ip_tables.c framework. |
| 6 |
* |
| 7 |
* Some ARP specific bits are: |
| 8 |
* |
| 9 |
* Copyright (C) 2002 David S. Miller ([email protected]) |
| 10 |
* Copyright (C) 2006-2009 Patrick McHardy <[email protected]> |
| 11 |
* |
| 12 |
*/ |
| 13 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/skbuff.h> |
| 16 |
#include <linux/netdevice.h> |
| 17 |
#include <linux/capability.h> |
| 18 |
#include <linux/if_arp.h> |
| 19 |
#include <linux/kmod.h> |
| 20 |
#include <linux/vmalloc.h> |
| 21 |
#include <linux/proc_fs.h> |
| 22 |
#include <linux/module.h> |
| 23 |
#include <linux/init.h> |
| 24 |
#include <linux/mutex.h> |
| 25 |
#include <linux/err.h> |
| 26 |
#include <net/compat.h> |
| 27 |
#include <net/sock.h> |
| 28 |
#include <linux/uaccess.h> |
| 29 |
|
| 30 |
#include <linux/netfilter/x_tables.h> |
| 31 |
#include <linux/netfilter_arp/arp_tables.h> |
| 32 |
#include "../../netfilter/xt_repldata.h" |
| 33 |
|
| 34 |
MODULE_LICENSE("GPL"); |
| 35 |
MODULE_AUTHOR("David S. Miller <[email protected]>"); |
| 36 |
MODULE_DESCRIPTION("arptables core"); |
| 37 |
|
| 38 |
void *arpt_alloc_initial_table(const struct xt_table *info) |
| 39 |
{ |
| 40 |
return xt_alloc_initial_table(arpt, ARPT); |
| 41 |
} |
| 42 |
EXPORT_SYMBOL_GPL(arpt_alloc_initial_table); |
| 43 |
|
| 44 |
static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap, |
| 45 |
const char *hdr_addr, int len) |
| 46 |
{ |
| 47 |
int i, ret; |
| 48 |
|
| 49 |
if (len > ARPT_DEV_ADDR_LEN_MAX) |
| 50 |
len = ARPT_DEV_ADDR_LEN_MAX; |
| 51 |
|
| 52 |
ret = 0; |
| 53 |
for (i = 0; i < len; i++) |
| 54 |
ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i]; |
| 55 |
|
| 56 |
return ret != 0; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* Unfortunately, _b and _mask are not aligned to an int (or long int) |
| 61 |
* Some arches dont care, unrolling the loop is a win on them. |
| 62 |
* For other arches, we only have a 16bit alignement. |
| 63 |
*/ |
| 64 |
static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask) |
| 65 |
{ |
| 66 |
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 67 |
unsigned long ret = ifname_compare_aligned(_a, _b, _mask); |
| 68 |
#else |
| 69 |
unsigned long ret = 0; |
| 70 |
const u16 *a = (const u16 *)_a; |
| 71 |
const u16 *b = (const u16 *)_b; |
| 72 |
const u16 *mask = (const u16 *)_mask; |
| 73 |
int i; |
| 74 |
|
| 75 |
for (i = 0; i < IFNAMSIZ/sizeof(u16); i++) |
| 76 |
ret |= (a[i] ^ b[i]) & mask[i]; |
| 77 |
#endif |
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
/* Returns whether packet matches rule or not. */ |
| 82 |
static inline int arp_packet_match(const struct arphdr *arphdr, |
| 83 |
struct net_device *dev, |
| 84 |
const char *indev, |
| 85 |
const char *outdev, |
| 86 |
const struct arpt_arp *arpinfo) |
| 87 |
{ |
| 88 |
const char *arpptr = (char *)(arphdr + 1); |
| 89 |
const char *src_devaddr, *tgt_devaddr; |
| 90 |
__be32 src_ipaddr, tgt_ipaddr; |
| 91 |
long ret; |
| 92 |
|
| 93 |
if (NF_INVF(arpinfo, ARPT_INV_ARPOP, |
| 94 |
(arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop)) |
| 95 |
return 0; |
| 96 |
|
| 97 |
if (NF_INVF(arpinfo, ARPT_INV_ARPHRD, |
| 98 |
(arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd)) |
| 99 |
return 0; |
| 100 |
|
| 101 |
if (NF_INVF(arpinfo, ARPT_INV_ARPPRO, |
| 102 |
(arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro)) |
| 103 |
return 0; |
| 104 |
|
| 105 |
if (NF_INVF(arpinfo, ARPT_INV_ARPHLN, |
| 106 |
(arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln)) |
| 107 |
return 0; |
| 108 |
|
| 109 |
src_devaddr = arpptr; |
| 110 |
arpptr += dev->addr_len; |
| 111 |
memcpy(&src_ipaddr, arpptr, sizeof(u32)); |
| 112 |
arpptr += sizeof(u32); |
| 113 |
|
| 114 |
if (IS_ENABLED(CONFIG_FIREWIRE_NET) && dev->type == ARPHRD_IEEE1394) { |
| 115 |
if (unlikely(memchr_inv(arpinfo->tgt_devaddr.mask, 0, |
| 116 |
sizeof(arpinfo->tgt_devaddr.mask)))) |
| 117 |
return 0; |
| 118 |
|
| 119 |
tgt_devaddr = NULL; |
| 120 |
} else { |
| 121 |
tgt_devaddr = arpptr; |
| 122 |
arpptr += dev->addr_len; |
| 123 |
} |
| 124 |
memcpy(&tgt_ipaddr, arpptr, sizeof(u32)); |
| 125 |
|
| 126 |
if (NF_INVF(arpinfo, ARPT_INV_SRCDEVADDR, |
| 127 |
arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, |
| 128 |
dev->addr_len))) |
| 129 |
return 0; |
| 130 |
|
| 131 |
if (tgt_devaddr && |
| 132 |
NF_INVF(arpinfo, ARPT_INV_TGTDEVADDR, |
| 133 |
arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, |
| 134 |
dev->addr_len))) |
| 135 |
return 0; |
| 136 |
|
| 137 |
if (NF_INVF(arpinfo, ARPT_INV_SRCIP, |
| 138 |
(src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr) || |
| 139 |
NF_INVF(arpinfo, ARPT_INV_TGTIP, |
| 140 |
(tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr)) |
| 141 |
return 0; |
| 142 |
|
| 143 |
/* Look for ifname matches. */ |
| 144 |
ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask); |
| 145 |
|
| 146 |
if (NF_INVF(arpinfo, ARPT_INV_VIA_IN, ret != 0)) |
| 147 |
return 0; |
| 148 |
|
| 149 |
ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask); |
| 150 |
|
| 151 |
if (NF_INVF(arpinfo, ARPT_INV_VIA_OUT, ret != 0)) |
| 152 |
return 0; |
| 153 |
|
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
static inline int arp_checkentry(const struct arpt_arp *arp) |
| 158 |
{ |
| 159 |
if (arp->flags & ~ARPT_F_MASK) |
| 160 |
return 0; |
| 161 |
if (arp->invflags & ~ARPT_INV_MASK) |
| 162 |
return 0; |
| 163 |
|
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
static unsigned int |
| 168 |
arpt_error(struct sk_buff *skb, const struct xt_action_param *par) |
| 169 |
{ |
| 170 |
net_err_ratelimited("arp_tables: error: '%s'\n", |
| 171 |
(const char *)par->targinfo); |
| 172 |
|
| 173 |
return NF_DROP; |
| 174 |
} |
| 175 |
|
| 176 |
static inline const struct xt_entry_target * |
| 177 |
arpt_get_target_c(const struct arpt_entry *e) |
| 178 |
{ |
| 179 |
return arpt_get_target((struct arpt_entry *)e); |
| 180 |
} |
| 181 |
|
| 182 |
static inline struct arpt_entry * |
| 183 |
get_entry(const void *base, unsigned int offset) |
| 184 |
{ |
| 185 |
return (struct arpt_entry *)(base + offset); |
| 186 |
} |
| 187 |
|
| 188 |
static inline |
| 189 |
struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry) |
| 190 |
{ |
| 191 |
return (void *)entry + entry->next_offset; |
| 192 |
} |
| 193 |
|
| 194 |
unsigned int arpt_do_table(void *priv, |
| 195 |
struct sk_buff *skb, |
| 196 |
const struct nf_hook_state *state) |
| 197 |
{ |
| 198 |
const struct xt_table *table = priv; |
| 199 |
unsigned int hook = state->hook; |
| 200 |
static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); |
| 201 |
unsigned int verdict = NF_DROP; |
| 202 |
const struct arphdr *arp; |
| 203 |
struct arpt_entry *e, **jumpstack; |
| 204 |
const char *indev, *outdev; |
| 205 |
const void *table_base; |
| 206 |
unsigned int cpu, stackidx = 0; |
| 207 |
const struct xt_table_info *private; |
| 208 |
struct xt_action_param acpar; |
| 209 |
unsigned int addend; |
| 210 |
|
| 211 |
if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) |
| 212 |
return NF_DROP; |
| 213 |
|
| 214 |
indev = state->in ? state->in->name : nulldevname; |
| 215 |
outdev = state->out ? state->out->name : nulldevname; |
| 216 |
|
| 217 |
local_bh_disable(); |
| 218 |
addend = xt_write_recseq_begin(); |
| 219 |
private = READ_ONCE(table->private); /* Address dependency. */ |
| 220 |
cpu = smp_processor_id(); |
| 221 |
table_base = private->entries; |
| 222 |
jumpstack = (struct arpt_entry **)private->jumpstack[cpu]; |
| 223 |
|
| 224 |
/* No TEE support for arptables, so no need to switch to alternate |
| 225 |
* stack. All targets that reenter must return absolute verdicts. |
| 226 |
*/ |
| 227 |
e = get_entry(table_base, private->hook_entry[hook]); |
| 228 |
|
| 229 |
acpar.state = state; |
| 230 |
acpar.hotdrop = false; |
| 231 |
|
| 232 |
arp = arp_hdr(skb); |
| 233 |
do { |
| 234 |
const struct xt_entry_target *t; |
| 235 |
struct xt_counters *counter; |
| 236 |
|
| 237 |
if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) { |
| 238 |
e = arpt_next_entry(e); |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
counter = xt_get_this_cpu_counter(&e->counters); |
| 243 |
ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1); |
| 244 |
|
| 245 |
t = arpt_get_target_c(e); |
| 246 |
|
| 247 |
/* Standard target? */ |
| 248 |
if (!t->u.kernel.target->target) { |
| 249 |
int v; |
| 250 |
|
| 251 |
v = ((struct xt_standard_target *)t)->verdict; |
| 252 |
if (v < 0) { |
| 253 |
/* Pop from stack? */ |
| 254 |
if (v != XT_RETURN) { |
| 255 |
verdict = (unsigned int)(-v) - 1; |
| 256 |
break; |
| 257 |
} |
| 258 |
if (stackidx == 0) { |
| 259 |
e = get_entry(table_base, |
| 260 |
private->underflow[hook]); |
| 261 |
} else { |
| 262 |
e = jumpstack[--stackidx]; |
| 263 |
e = arpt_next_entry(e); |
| 264 |
} |
| 265 |
continue; |
| 266 |
} |
| 267 |
if (table_base + v |
| 268 |
!= arpt_next_entry(e)) { |
| 269 |
if (unlikely(stackidx >= private->stacksize)) { |
| 270 |
verdict = NF_DROP; |
| 271 |
break; |
| 272 |
} |
| 273 |
jumpstack[stackidx++] = e; |
| 274 |
} |
| 275 |
|
| 276 |
e = get_entry(table_base, v); |
| 277 |
continue; |
| 278 |
} |
| 279 |
|
| 280 |
acpar.target = t->u.kernel.target; |
| 281 |
acpar.targinfo = t->data; |
| 282 |
verdict = t->u.kernel.target->target(skb, &acpar); |
| 283 |
|
| 284 |
if (verdict == XT_CONTINUE) { |
| 285 |
/* Target might have changed stuff. */ |
| 286 |
arp = arp_hdr(skb); |
| 287 |
e = arpt_next_entry(e); |
| 288 |
} else { |
| 289 |
/* Verdict */ |
| 290 |
break; |
| 291 |
} |
| 292 |
} while (!acpar.hotdrop); |
| 293 |
xt_write_recseq_end(addend); |
| 294 |
local_bh_enable(); |
| 295 |
|
| 296 |
if (acpar.hotdrop) |
| 297 |
return NF_DROP; |
| 298 |
else |
| 299 |
return verdict; |
| 300 |
} |
| 301 |
|
| 302 |
/* All zeroes == unconditional rule. */ |
| 303 |
static inline bool unconditional(const struct arpt_entry *e) |
| 304 |
{ |
| 305 |
static const struct arpt_arp uncond; |
| 306 |
|
| 307 |
return e->target_offset == sizeof(struct arpt_entry) && |
| 308 |
memcmp(&e->arp, &uncond, sizeof(uncond)) == 0; |
| 309 |
} |
| 310 |
|
| 311 |
/* Figures out from what hook each rule can be called: returns 0 if |
| 312 |
* there are loops. Puts hook bitmask in comefrom. |
| 313 |
*/ |
| 314 |
static int mark_source_chains(const struct xt_table_info *newinfo, |
| 315 |
unsigned int valid_hooks, void *entry0, |
| 316 |
unsigned int *offsets) |
| 317 |
{ |
| 318 |
unsigned int hook; |
| 319 |
|
| 320 |
/* No recursion; use packet counter to save back ptrs (reset |
| 321 |
* to 0 as we leave), and comefrom to save source hook bitmask. |
| 322 |
*/ |
| 323 |
for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) { |
| 324 |
unsigned int pos = newinfo->hook_entry[hook]; |
| 325 |
struct arpt_entry *e = entry0 + pos; |
| 326 |
|
| 327 |
if (!(valid_hooks & (1 << hook))) |
| 328 |
continue; |
| 329 |
|
| 330 |
/* Set initial back pointer. */ |
| 331 |
e->counters.pcnt = pos; |
| 332 |
|
| 333 |
for (;;) { |
| 334 |
const struct xt_standard_target *t |
| 335 |
= (void *)arpt_get_target_c(e); |
| 336 |
int visited = e->comefrom & (1 << hook); |
| 337 |
|
| 338 |
if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) |
| 339 |
return 0; |
| 340 |
|
| 341 |
e->comefrom |
| 342 |
|= ((1 << hook) | (1 << NF_ARP_NUMHOOKS)); |
| 343 |
|
| 344 |
/* Unconditional return/END. */ |
| 345 |
if ((unconditional(e) && |
| 346 |
(strcmp(t->target.u.user.name, |
| 347 |
XT_STANDARD_TARGET) == 0) && |
| 348 |
t->verdict < 0) || visited) { |
| 349 |
unsigned int oldpos, size; |
| 350 |
|
| 351 |
/* Return: backtrack through the last |
| 352 |
* big jump. |
| 353 |
*/ |
| 354 |
do { |
| 355 |
e->comefrom ^= (1<<NF_ARP_NUMHOOKS); |
| 356 |
oldpos = pos; |
| 357 |
pos = e->counters.pcnt; |
| 358 |
e->counters.pcnt = 0; |
| 359 |
|
| 360 |
/* We're at the start. */ |
| 361 |
if (pos == oldpos) |
| 362 |
goto next; |
| 363 |
|
| 364 |
e = entry0 + pos; |
| 365 |
} while (oldpos == pos + e->next_offset); |
| 366 |
|
| 367 |
/* Move along one */ |
| 368 |
size = e->next_offset; |
| 369 |
e = entry0 + pos + size; |
| 370 |
if (pos + size >= newinfo->size) |
| 371 |
return 0; |
| 372 |
e->counters.pcnt = pos; |
| 373 |
pos += size; |
| 374 |
} else { |
| 375 |
int newpos = t->verdict; |
| 376 |
|
| 377 |
if (strcmp(t->target.u.user.name, |
| 378 |
XT_STANDARD_TARGET) == 0 && |
| 379 |
newpos >= 0) { |
| 380 |
/* This a jump; chase it. */ |
| 381 |
if (!xt_find_jump_offset(offsets, newpos, |
| 382 |
newinfo->number)) |
| 383 |
return 0; |
| 384 |
} else { |
| 385 |
/* ... this is a fallthru */ |
| 386 |
newpos = pos + e->next_offset; |
| 387 |
if (newpos >= newinfo->size) |
| 388 |
return 0; |
| 389 |
} |
| 390 |
e = entry0 + newpos; |
| 391 |
e->counters.pcnt = pos; |
| 392 |
pos = newpos; |
| 393 |
} |
| 394 |
} |
| 395 |
next: ; |
| 396 |
} |
| 397 |
return 1; |
| 398 |
} |
| 399 |
|
| 400 |
static int check_target(struct arpt_entry *e, struct net *net, const char *name) |
| 401 |
{ |
| 402 |
struct xt_entry_target *t = arpt_get_target(e); |
| 403 |
struct xt_tgchk_param par = { |
| 404 |
.net = net, |
| 405 |
.table = name, |
| 406 |
.entryinfo = e, |
| 407 |
.target = t->u.kernel.target, |
| 408 |
.targinfo = t->data, |
| 409 |
.hook_mask = e->comefrom, |
| 410 |
.family = NFPROTO_ARP, |
| 411 |
}; |
| 412 |
|
| 413 |
return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false); |
| 414 |
} |
| 415 |
|
| 416 |
static int |
| 417 |
find_check_entry(struct arpt_entry *e, struct net *net, const char *name, |
| 418 |
unsigned int size, |
| 419 |
struct xt_percpu_counter_alloc_state *alloc_state) |
| 420 |
{ |
| 421 |
struct xt_entry_target *t; |
| 422 |
struct xt_target *target; |
| 423 |
int ret; |
| 424 |
|
| 425 |
if (!xt_percpu_counter_alloc(alloc_state, &e->counters)) |
| 426 |
return -ENOMEM; |
| 427 |
|
| 428 |
t = arpt_get_target(e); |
| 429 |
target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
| 430 |
t->u.user.revision); |
| 431 |
if (IS_ERR(target)) { |
| 432 |
ret = PTR_ERR(target); |
| 433 |
goto out; |
| 434 |
} |
| 435 |
t->u.kernel.target = target; |
| 436 |
|
| 437 |
ret = check_target(e, net, name); |
| 438 |
if (ret) |
| 439 |
goto err; |
| 440 |
return 0; |
| 441 |
err: |
| 442 |
module_put(t->u.kernel.target->me); |
| 443 |
out: |
| 444 |
xt_percpu_counter_free(&e->counters); |
| 445 |
|
| 446 |
return ret; |
| 447 |
} |
| 448 |
|
| 449 |
static bool check_underflow(const struct arpt_entry *e) |
| 450 |
{ |
| 451 |
const struct xt_entry_target *t; |
| 452 |
unsigned int verdict; |
| 453 |
|
| 454 |
if (!unconditional(e)) |
| 455 |
return false; |
| 456 |
t = arpt_get_target_c(e); |
| 457 |
if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0) |
| 458 |
return false; |
| 459 |
verdict = ((struct xt_standard_target *)t)->verdict; |
| 460 |
verdict = -verdict - 1; |
| 461 |
return verdict == NF_DROP || verdict == NF_ACCEPT; |
| 462 |
} |
| 463 |
|
| 464 |
static inline int check_entry_size_and_hooks(struct arpt_entry *e, |
| 465 |
struct xt_table_info *newinfo, |
| 466 |
const unsigned char *base, |
| 467 |
const unsigned char *limit, |
| 468 |
const unsigned int *hook_entries, |
| 469 |
const unsigned int *underflows, |
| 470 |
unsigned int valid_hooks) |
| 471 |
{ |
| 472 |
unsigned int h; |
| 473 |
int err; |
| 474 |
|
| 475 |
if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 || |
| 476 |
(unsigned char *)e + sizeof(struct arpt_entry) >= limit || |
| 477 |
(unsigned char *)e + e->next_offset > limit) |
| 478 |
return -EINVAL; |
| 479 |
|
| 480 |
if (e->next_offset |
| 481 |
< sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) |
| 482 |
return -EINVAL; |
| 483 |
|
| 484 |
if (!arp_checkentry(&e->arp)) |
| 485 |
return -EINVAL; |
| 486 |
|
| 487 |
err = xt_check_entry_offsets(e, e->elems, e->target_offset, |
| 488 |
e->next_offset); |
| 489 |
if (err) |
| 490 |
return err; |
| 491 |
|
| 492 |
/* Check hooks & underflows */ |
| 493 |
for (h = 0; h < NF_ARP_NUMHOOKS; h++) { |
| 494 |
if (!(valid_hooks & (1 << h))) |
| 495 |
continue; |
| 496 |
if ((unsigned char *)e - base == hook_entries[h]) |
| 497 |
newinfo->hook_entry[h] = hook_entries[h]; |
| 498 |
if ((unsigned char *)e - base == underflows[h]) { |
| 499 |
if (!check_underflow(e)) |
| 500 |
return -EINVAL; |
| 501 |
|
| 502 |
newinfo->underflow[h] = underflows[h]; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/* Clear counters and comefrom */ |
| 507 |
e->counters = ((struct xt_counters) { 0, 0 }); |
| 508 |
e->comefrom = 0; |
| 509 |
return 0; |
| 510 |
} |
| 511 |
|
| 512 |
static void cleanup_entry(struct arpt_entry *e, struct net *net) |
| 513 |
{ |
| 514 |
struct xt_tgdtor_param par; |
| 515 |
struct xt_entry_target *t; |
| 516 |
|
| 517 |
t = arpt_get_target(e); |
| 518 |
par.net = net; |
| 519 |
par.target = t->u.kernel.target; |
| 520 |
par.targinfo = t->data; |
| 521 |
par.family = NFPROTO_ARP; |
| 522 |
if (par.target->destroy != NULL) |
| 523 |
par.target->destroy(&par); |
| 524 |
module_put(par.target->me); |
| 525 |
xt_percpu_counter_free(&e->counters); |
| 526 |
} |
| 527 |
|
| 528 |
/* Checks and translates the user-supplied table segment (held in |
| 529 |
* newinfo). |
| 530 |
*/ |
| 531 |
static int translate_table(struct net *net, |
| 532 |
struct xt_table_info *newinfo, |
| 533 |
void *entry0, |
| 534 |
const struct arpt_replace *repl) |
| 535 |
{ |
| 536 |
struct xt_percpu_counter_alloc_state alloc_state = { 0 }; |
| 537 |
struct arpt_entry *iter; |
| 538 |
unsigned int *offsets; |
| 539 |
unsigned int i; |
| 540 |
int ret = 0; |
| 541 |
|
| 542 |
newinfo->size = repl->size; |
| 543 |
newinfo->number = repl->num_entries; |
| 544 |
|
| 545 |
/* Init all hooks to impossible value. */ |
| 546 |
for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
| 547 |
newinfo->hook_entry[i] = 0xFFFFFFFF; |
| 548 |
newinfo->underflow[i] = 0xFFFFFFFF; |
| 549 |
} |
| 550 |
|
| 551 |
offsets = xt_alloc_entry_offsets(newinfo->number); |
| 552 |
if (!offsets) |
| 553 |
return -ENOMEM; |
| 554 |
i = 0; |
| 555 |
|
| 556 |
/* Walk through entries, checking offsets. */ |
| 557 |
xt_entry_foreach(iter, entry0, newinfo->size) { |
| 558 |
ret = check_entry_size_and_hooks(iter, newinfo, entry0, |
| 559 |
entry0 + repl->size, |
| 560 |
repl->hook_entry, |
| 561 |
repl->underflow, |
| 562 |
repl->valid_hooks); |
| 563 |
if (ret != 0) |
| 564 |
goto out_free; |
| 565 |
if (i < repl->num_entries) |
| 566 |
offsets[i] = (void *)iter - entry0; |
| 567 |
++i; |
| 568 |
if (strcmp(arpt_get_target(iter)->u.user.name, |
| 569 |
XT_ERROR_TARGET) == 0) |
| 570 |
++newinfo->stacksize; |
| 571 |
} |
| 572 |
|
| 573 |
ret = -EINVAL; |
| 574 |
if (i != repl->num_entries) |
| 575 |
goto out_free; |
| 576 |
|
| 577 |
ret = xt_check_table_hooks(newinfo, repl->valid_hooks); |
| 578 |
if (ret) |
| 579 |
goto out_free; |
| 580 |
|
| 581 |
if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) { |
| 582 |
ret = -ELOOP; |
| 583 |
goto out_free; |
| 584 |
} |
| 585 |
kvfree(offsets); |
| 586 |
|
| 587 |
/* Finally, each sanity check must pass */ |
| 588 |
i = 0; |
| 589 |
xt_entry_foreach(iter, entry0, newinfo->size) { |
| 590 |
ret = find_check_entry(iter, net, repl->name, repl->size, |
| 591 |
&alloc_state); |
| 592 |
if (ret != 0) |
| 593 |
break; |
| 594 |
++i; |
| 595 |
} |
| 596 |
|
| 597 |
if (ret != 0) { |
| 598 |
xt_entry_foreach(iter, entry0, newinfo->size) { |
| 599 |
if (i-- == 0) |
| 600 |
break; |
| 601 |
cleanup_entry(iter, net); |
| 602 |
} |
| 603 |
return ret; |
| 604 |
} |
| 605 |
|
| 606 |
return ret; |
| 607 |
out_free: |
| 608 |
kvfree(offsets); |
| 609 |
return ret; |
| 610 |
} |
| 611 |
|
| 612 |
static void get_counters(const struct xt_table_info *t, |
| 613 |
struct xt_counters counters[]) |
| 614 |
{ |
| 615 |
struct arpt_entry *iter; |
| 616 |
unsigned int cpu; |
| 617 |
unsigned int i; |
| 618 |
|
| 619 |
for_each_possible_cpu(cpu) { |
| 620 |
seqcount_t *s = &per_cpu(xt_recseq, cpu); |
| 621 |
|
| 622 |
i = 0; |
| 623 |
xt_entry_foreach(iter, t->entries, t->size) { |
| 624 |
struct xt_counters *tmp; |
| 625 |
u64 bcnt, pcnt; |
| 626 |
unsigned int start; |
| 627 |
|
| 628 |
tmp = xt_get_per_cpu_counter(&iter->counters, cpu); |
| 629 |
do { |
| 630 |
start = read_seqcount_begin(s); |
| 631 |
bcnt = tmp->bcnt; |
| 632 |
pcnt = tmp->pcnt; |
| 633 |
} while (read_seqcount_retry(s, start)); |
| 634 |
|
| 635 |
ADD_COUNTER(counters[i], bcnt, pcnt); |
| 636 |
++i; |
| 637 |
cond_resched(); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
static void get_old_counters(const struct xt_table_info *t, |
| 643 |
struct xt_counters counters[]) |
| 644 |
{ |
| 645 |
struct arpt_entry *iter; |
| 646 |
unsigned int cpu, i; |
| 647 |
|
| 648 |
for_each_possible_cpu(cpu) { |
| 649 |
i = 0; |
| 650 |
xt_entry_foreach(iter, t->entries, t->size) { |
| 651 |
struct xt_counters *tmp; |
| 652 |
|
| 653 |
tmp = xt_get_per_cpu_counter(&iter->counters, cpu); |
| 654 |
ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt); |
| 655 |
++i; |
| 656 |
} |
| 657 |
cond_resched(); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
static struct xt_counters *alloc_counters(const struct xt_table *table) |
| 662 |
{ |
| 663 |
unsigned int countersize; |
| 664 |
struct xt_counters *counters; |
| 665 |
const struct xt_table_info *private = table->private; |
| 666 |
|
| 667 |
/* We need atomic snapshot of counters: rest doesn't change |
| 668 |
* (other than comefrom, which userspace doesn't care |
| 669 |
* about). |
| 670 |
*/ |
| 671 |
countersize = sizeof(struct xt_counters) * private->number; |
| 672 |
counters = vzalloc(countersize); |
| 673 |
|
| 674 |
if (counters == NULL) |
| 675 |
return ERR_PTR(-ENOMEM); |
| 676 |
|
| 677 |
get_counters(private, counters); |
| 678 |
|
| 679 |
return counters; |
| 680 |
} |
| 681 |
|
| 682 |
static int copy_entries_to_user(unsigned int total_size, |
| 683 |
const struct xt_table *table, |
| 684 |
void __user *userptr) |
| 685 |
{ |
| 686 |
unsigned int off, num; |
| 687 |
const struct arpt_entry *e; |
| 688 |
struct xt_counters *counters; |
| 689 |
struct xt_table_info *private = table->private; |
| 690 |
int ret = 0; |
| 691 |
void *loc_cpu_entry; |
| 692 |
|
| 693 |
counters = alloc_counters(table); |
| 694 |
if (IS_ERR(counters)) |
| 695 |
return PTR_ERR(counters); |
| 696 |
|
| 697 |
loc_cpu_entry = private->entries; |
| 698 |
|
| 699 |
/* FIXME: use iterator macros --RR */ |
| 700 |
/* ... then go back and fix counters and names */ |
| 701 |
for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){ |
| 702 |
const struct xt_entry_target *t; |
| 703 |
|
| 704 |
e = loc_cpu_entry + off; |
| 705 |
if (copy_to_user(userptr + off, e, sizeof(*e))) { |
| 706 |
ret = -EFAULT; |
| 707 |
goto free_counters; |
| 708 |
} |
| 709 |
if (copy_to_user(userptr + off |
| 710 |
+ offsetof(struct arpt_entry, counters), |
| 711 |
&counters[num], |
| 712 |
sizeof(counters[num])) != 0) { |
| 713 |
ret = -EFAULT; |
| 714 |
goto free_counters; |
| 715 |
} |
| 716 |
|
| 717 |
t = arpt_get_target_c(e); |
| 718 |
if (xt_target_to_user(t, userptr + off + e->target_offset)) { |
| 719 |
ret = -EFAULT; |
| 720 |
goto free_counters; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
free_counters: |
| 725 |
vfree(counters); |
| 726 |
return ret; |
| 727 |
} |
| 728 |
|
| 729 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 730 |
static void compat_standard_from_user(void *dst, const void *src) |
| 731 |
{ |
| 732 |
int v = *(compat_int_t *)src; |
| 733 |
|
| 734 |
if (v > 0) |
| 735 |
v += xt_compat_calc_jump(NFPROTO_ARP, v); |
| 736 |
memcpy(dst, &v, sizeof(v)); |
| 737 |
} |
| 738 |
|
| 739 |
static int compat_standard_to_user(void __user *dst, const void *src) |
| 740 |
{ |
| 741 |
compat_int_t cv = *(int *)src; |
| 742 |
|
| 743 |
if (cv > 0) |
| 744 |
cv -= xt_compat_calc_jump(NFPROTO_ARP, cv); |
| 745 |
return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0; |
| 746 |
} |
| 747 |
|
| 748 |
static int compat_calc_entry(const struct arpt_entry *e, |
| 749 |
const struct xt_table_info *info, |
| 750 |
const void *base, struct xt_table_info *newinfo) |
| 751 |
{ |
| 752 |
const struct xt_entry_target *t; |
| 753 |
unsigned int entry_offset; |
| 754 |
int off, i, ret; |
| 755 |
|
| 756 |
off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); |
| 757 |
entry_offset = (void *)e - base; |
| 758 |
|
| 759 |
t = arpt_get_target_c(e); |
| 760 |
off += xt_compat_target_offset(t->u.kernel.target); |
| 761 |
newinfo->size -= off; |
| 762 |
ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
| 763 |
if (ret) |
| 764 |
return ret; |
| 765 |
|
| 766 |
for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
| 767 |
if (info->hook_entry[i] && |
| 768 |
(e < (struct arpt_entry *)(base + info->hook_entry[i]))) |
| 769 |
newinfo->hook_entry[i] -= off; |
| 770 |
if (info->underflow[i] && |
| 771 |
(e < (struct arpt_entry *)(base + info->underflow[i]))) |
| 772 |
newinfo->underflow[i] -= off; |
| 773 |
} |
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
static int compat_table_info(const struct xt_table_info *info, |
| 778 |
struct xt_table_info *newinfo) |
| 779 |
{ |
| 780 |
struct arpt_entry *iter; |
| 781 |
const void *loc_cpu_entry; |
| 782 |
int ret; |
| 783 |
|
| 784 |
if (!newinfo || !info) |
| 785 |
return -EINVAL; |
| 786 |
|
| 787 |
/* we dont care about newinfo->entries */ |
| 788 |
memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); |
| 789 |
newinfo->initial_entries = 0; |
| 790 |
loc_cpu_entry = info->entries; |
| 791 |
ret = xt_compat_init_offsets(NFPROTO_ARP, info->number); |
| 792 |
if (ret) |
| 793 |
return ret; |
| 794 |
xt_entry_foreach(iter, loc_cpu_entry, info->size) { |
| 795 |
ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo); |
| 796 |
if (ret != 0) |
| 797 |
return ret; |
| 798 |
} |
| 799 |
return 0; |
| 800 |
} |
| 801 |
#endif |
| 802 |
|
| 803 |
static int get_info(struct net *net, void __user *user, const int *len) |
| 804 |
{ |
| 805 |
char name[XT_TABLE_MAXNAMELEN]; |
| 806 |
struct xt_table *t; |
| 807 |
int ret; |
| 808 |
|
| 809 |
if (*len != sizeof(struct arpt_getinfo)) |
| 810 |
return -EINVAL; |
| 811 |
|
| 812 |
if (copy_from_user(name, user, sizeof(name)) != 0) |
| 813 |
return -EFAULT; |
| 814 |
|
| 815 |
name[XT_TABLE_MAXNAMELEN-1] = '\0'; |
| 816 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 817 |
if (in_compat_syscall()) |
| 818 |
xt_compat_lock(NFPROTO_ARP); |
| 819 |
#endif |
| 820 |
t = xt_request_find_table_lock(net, NFPROTO_ARP, name); |
| 821 |
if (!IS_ERR(t)) { |
| 822 |
struct arpt_getinfo info; |
| 823 |
const struct xt_table_info *private = t->private; |
| 824 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 825 |
struct xt_table_info tmp; |
| 826 |
|
| 827 |
if (in_compat_syscall()) { |
| 828 |
ret = compat_table_info(private, &tmp); |
| 829 |
xt_compat_flush_offsets(NFPROTO_ARP); |
| 830 |
private = &tmp; |
| 831 |
} |
| 832 |
#endif |
| 833 |
memset(&info, 0, sizeof(info)); |
| 834 |
info.valid_hooks = t->valid_hooks; |
| 835 |
memcpy(info.hook_entry, private->hook_entry, |
| 836 |
sizeof(info.hook_entry)); |
| 837 |
memcpy(info.underflow, private->underflow, |
| 838 |
sizeof(info.underflow)); |
| 839 |
info.num_entries = private->number; |
| 840 |
info.size = private->size; |
| 841 |
strscpy(info.name, name); |
| 842 |
|
| 843 |
if (copy_to_user(user, &info, *len) != 0) |
| 844 |
ret = -EFAULT; |
| 845 |
else |
| 846 |
ret = 0; |
| 847 |
xt_table_unlock(t); |
| 848 |
module_put(t->me); |
| 849 |
} else |
| 850 |
ret = PTR_ERR(t); |
| 851 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 852 |
if (in_compat_syscall()) |
| 853 |
xt_compat_unlock(NFPROTO_ARP); |
| 854 |
#endif |
| 855 |
return ret; |
| 856 |
} |
| 857 |
|
| 858 |
static int get_entries(struct net *net, struct arpt_get_entries __user *uptr, |
| 859 |
const int *len) |
| 860 |
{ |
| 861 |
int ret; |
| 862 |
struct arpt_get_entries get; |
| 863 |
struct xt_table *t; |
| 864 |
|
| 865 |
if (*len < sizeof(get)) |
| 866 |
return -EINVAL; |
| 867 |
if (copy_from_user(&get, uptr, sizeof(get)) != 0) |
| 868 |
return -EFAULT; |
| 869 |
if (*len != sizeof(struct arpt_get_entries) + get.size) |
| 870 |
return -EINVAL; |
| 871 |
|
| 872 |
get.name[sizeof(get.name) - 1] = '\0'; |
| 873 |
|
| 874 |
t = xt_find_table_lock(net, NFPROTO_ARP, get.name); |
| 875 |
if (!IS_ERR(t)) { |
| 876 |
const struct xt_table_info *private = t->private; |
| 877 |
|
| 878 |
if (get.size == private->size) |
| 879 |
ret = copy_entries_to_user(private->size, |
| 880 |
t, uptr->entrytable); |
| 881 |
else |
| 882 |
ret = -EAGAIN; |
| 883 |
|
| 884 |
module_put(t->me); |
| 885 |
xt_table_unlock(t); |
| 886 |
} else |
| 887 |
ret = PTR_ERR(t); |
| 888 |
|
| 889 |
return ret; |
| 890 |
} |
| 891 |
|
| 892 |
static int __do_replace(struct net *net, const char *name, |
| 893 |
unsigned int valid_hooks, |
| 894 |
struct xt_table_info *newinfo, |
| 895 |
unsigned int num_counters, |
| 896 |
void __user *counters_ptr) |
| 897 |
{ |
| 898 |
int ret; |
| 899 |
struct xt_table *t; |
| 900 |
struct xt_table_info *oldinfo; |
| 901 |
struct xt_counters *counters; |
| 902 |
void *loc_cpu_old_entry; |
| 903 |
struct arpt_entry *iter; |
| 904 |
|
| 905 |
ret = 0; |
| 906 |
counters = xt_counters_alloc(num_counters); |
| 907 |
if (!counters) { |
| 908 |
ret = -ENOMEM; |
| 909 |
goto out; |
| 910 |
} |
| 911 |
|
| 912 |
t = xt_request_find_table_lock(net, NFPROTO_ARP, name); |
| 913 |
if (IS_ERR(t)) { |
| 914 |
ret = PTR_ERR(t); |
| 915 |
goto free_newinfo_counters_untrans; |
| 916 |
} |
| 917 |
|
| 918 |
/* You lied! */ |
| 919 |
if (valid_hooks != t->valid_hooks) { |
| 920 |
ret = -EINVAL; |
| 921 |
goto put_module; |
| 922 |
} |
| 923 |
|
| 924 |
oldinfo = xt_replace_table(t, num_counters, newinfo, &ret); |
| 925 |
if (!oldinfo) |
| 926 |
goto put_module; |
| 927 |
|
| 928 |
/* Update module usage count based on number of rules */ |
| 929 |
if ((oldinfo->number > oldinfo->initial_entries) || |
| 930 |
(newinfo->number <= oldinfo->initial_entries)) |
| 931 |
module_put(t->me); |
| 932 |
if ((oldinfo->number > oldinfo->initial_entries) && |
| 933 |
(newinfo->number <= oldinfo->initial_entries)) |
| 934 |
module_put(t->me); |
| 935 |
|
| 936 |
xt_table_unlock(t); |
| 937 |
|
| 938 |
get_old_counters(oldinfo, counters); |
| 939 |
|
| 940 |
/* Decrease module usage counts and free resource */ |
| 941 |
loc_cpu_old_entry = oldinfo->entries; |
| 942 |
xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size) |
| 943 |
cleanup_entry(iter, net); |
| 944 |
|
| 945 |
xt_free_table_info(oldinfo); |
| 946 |
if (copy_to_user(counters_ptr, counters, |
| 947 |
sizeof(struct xt_counters) * num_counters) != 0) { |
| 948 |
/* Silent error, can't fail, new table is already in place */ |
| 949 |
net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n"); |
| 950 |
} |
| 951 |
vfree(counters); |
| 952 |
return ret; |
| 953 |
|
| 954 |
put_module: |
| 955 |
module_put(t->me); |
| 956 |
xt_table_unlock(t); |
| 957 |
free_newinfo_counters_untrans: |
| 958 |
vfree(counters); |
| 959 |
out: |
| 960 |
return ret; |
| 961 |
} |
| 962 |
|
| 963 |
static int do_replace(struct net *net, sockptr_t arg, unsigned int len) |
| 964 |
{ |
| 965 |
int ret; |
| 966 |
struct arpt_replace tmp; |
| 967 |
struct xt_table_info *newinfo; |
| 968 |
void *loc_cpu_entry; |
| 969 |
struct arpt_entry *iter; |
| 970 |
|
| 971 |
if (len < sizeof(tmp)) |
| 972 |
return -EINVAL; |
| 973 |
if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0) |
| 974 |
return -EFAULT; |
| 975 |
|
| 976 |
/* overflow check */ |
| 977 |
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) |
| 978 |
return -ENOMEM; |
| 979 |
if (tmp.num_counters == 0) |
| 980 |
return -EINVAL; |
| 981 |
if ((u64)len < (u64)tmp.size + sizeof(tmp)) |
| 982 |
return -EINVAL; |
| 983 |
|
| 984 |
tmp.name[sizeof(tmp.name)-1] = 0; |
| 985 |
|
| 986 |
newinfo = xt_alloc_table_info(tmp.size); |
| 987 |
if (!newinfo) |
| 988 |
return -ENOMEM; |
| 989 |
|
| 990 |
loc_cpu_entry = newinfo->entries; |
| 991 |
if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp), |
| 992 |
tmp.size) != 0) { |
| 993 |
ret = -EFAULT; |
| 994 |
goto free_newinfo; |
| 995 |
} |
| 996 |
|
| 997 |
ret = translate_table(net, newinfo, loc_cpu_entry, &tmp); |
| 998 |
if (ret != 0) |
| 999 |
goto free_newinfo; |
| 1000 |
|
| 1001 |
ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
| 1002 |
tmp.num_counters, tmp.counters); |
| 1003 |
if (ret) |
| 1004 |
goto free_newinfo_untrans; |
| 1005 |
return 0; |
| 1006 |
|
| 1007 |
free_newinfo_untrans: |
| 1008 |
xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
| 1009 |
cleanup_entry(iter, net); |
| 1010 |
free_newinfo: |
| 1011 |
xt_free_table_info(newinfo); |
| 1012 |
return ret; |
| 1013 |
} |
| 1014 |
|
| 1015 |
static int do_add_counters(struct net *net, sockptr_t arg, unsigned int len) |
| 1016 |
{ |
| 1017 |
unsigned int i; |
| 1018 |
struct xt_counters_info tmp; |
| 1019 |
struct xt_counters *paddc; |
| 1020 |
struct xt_table *t; |
| 1021 |
const struct xt_table_info *private; |
| 1022 |
int ret = 0; |
| 1023 |
struct arpt_entry *iter; |
| 1024 |
unsigned int addend; |
| 1025 |
|
| 1026 |
paddc = xt_copy_counters(arg, len, &tmp); |
| 1027 |
if (IS_ERR(paddc)) |
| 1028 |
return PTR_ERR(paddc); |
| 1029 |
|
| 1030 |
t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name); |
| 1031 |
if (IS_ERR(t)) { |
| 1032 |
ret = PTR_ERR(t); |
| 1033 |
goto free; |
| 1034 |
} |
| 1035 |
|
| 1036 |
local_bh_disable(); |
| 1037 |
private = t->private; |
| 1038 |
if (private->number != tmp.num_counters) { |
| 1039 |
ret = -EINVAL; |
| 1040 |
goto unlock_up_free; |
| 1041 |
} |
| 1042 |
|
| 1043 |
i = 0; |
| 1044 |
|
| 1045 |
addend = xt_write_recseq_begin(); |
| 1046 |
xt_entry_foreach(iter, private->entries, private->size) { |
| 1047 |
struct xt_counters *tmp; |
| 1048 |
|
| 1049 |
tmp = xt_get_this_cpu_counter(&iter->counters); |
| 1050 |
ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt); |
| 1051 |
++i; |
| 1052 |
} |
| 1053 |
xt_write_recseq_end(addend); |
| 1054 |
unlock_up_free: |
| 1055 |
local_bh_enable(); |
| 1056 |
xt_table_unlock(t); |
| 1057 |
module_put(t->me); |
| 1058 |
free: |
| 1059 |
vfree(paddc); |
| 1060 |
|
| 1061 |
return ret; |
| 1062 |
} |
| 1063 |
|
| 1064 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 1065 |
struct compat_arpt_replace { |
| 1066 |
char name[XT_TABLE_MAXNAMELEN]; |
| 1067 |
u32 valid_hooks; |
| 1068 |
u32 num_entries; |
| 1069 |
u32 size; |
| 1070 |
u32 hook_entry[NF_ARP_NUMHOOKS]; |
| 1071 |
u32 underflow[NF_ARP_NUMHOOKS]; |
| 1072 |
u32 num_counters; |
| 1073 |
compat_uptr_t counters; |
| 1074 |
struct compat_arpt_entry entries[]; |
| 1075 |
}; |
| 1076 |
|
| 1077 |
static inline void compat_release_entry(struct compat_arpt_entry *e) |
| 1078 |
{ |
| 1079 |
struct xt_entry_target *t; |
| 1080 |
|
| 1081 |
t = compat_arpt_get_target(e); |
| 1082 |
module_put(t->u.kernel.target->me); |
| 1083 |
} |
| 1084 |
|
| 1085 |
static int |
| 1086 |
check_compat_entry_size_and_hooks(struct compat_arpt_entry *e, |
| 1087 |
struct xt_table_info *newinfo, |
| 1088 |
unsigned int *size, |
| 1089 |
const unsigned char *base, |
| 1090 |
const unsigned char *limit) |
| 1091 |
{ |
| 1092 |
struct xt_entry_target *t; |
| 1093 |
struct xt_target *target; |
| 1094 |
unsigned int entry_offset; |
| 1095 |
int ret, off; |
| 1096 |
|
| 1097 |
if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 || |
| 1098 |
(unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit || |
| 1099 |
(unsigned char *)e + e->next_offset > limit) |
| 1100 |
return -EINVAL; |
| 1101 |
|
| 1102 |
if (e->next_offset < sizeof(struct compat_arpt_entry) + |
| 1103 |
sizeof(struct compat_xt_entry_target)) |
| 1104 |
return -EINVAL; |
| 1105 |
|
| 1106 |
if (!arp_checkentry(&e->arp)) |
| 1107 |
return -EINVAL; |
| 1108 |
|
| 1109 |
ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset, |
| 1110 |
e->next_offset); |
| 1111 |
if (ret) |
| 1112 |
return ret; |
| 1113 |
|
| 1114 |
off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); |
| 1115 |
entry_offset = (void *)e - (void *)base; |
| 1116 |
|
| 1117 |
t = compat_arpt_get_target(e); |
| 1118 |
target = xt_request_find_target(NFPROTO_ARP, t->u.user.name, |
| 1119 |
t->u.user.revision); |
| 1120 |
if (IS_ERR(target)) { |
| 1121 |
ret = PTR_ERR(target); |
| 1122 |
goto out; |
| 1123 |
} |
| 1124 |
t->u.kernel.target = target; |
| 1125 |
|
| 1126 |
off += xt_compat_target_offset(target); |
| 1127 |
*size += off; |
| 1128 |
ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off); |
| 1129 |
if (ret) |
| 1130 |
goto release_target; |
| 1131 |
|
| 1132 |
return 0; |
| 1133 |
|
| 1134 |
release_target: |
| 1135 |
module_put(t->u.kernel.target->me); |
| 1136 |
out: |
| 1137 |
return ret; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static void |
| 1141 |
compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr, |
| 1142 |
unsigned int *size, |
| 1143 |
struct xt_table_info *newinfo, unsigned char *base) |
| 1144 |
{ |
| 1145 |
struct xt_entry_target *t; |
| 1146 |
struct arpt_entry *de; |
| 1147 |
unsigned int origsize; |
| 1148 |
int h; |
| 1149 |
|
| 1150 |
origsize = *size; |
| 1151 |
de = *dstptr; |
| 1152 |
memcpy(de, e, sizeof(struct arpt_entry)); |
| 1153 |
memcpy(&de->counters, &e->counters, sizeof(e->counters)); |
| 1154 |
|
| 1155 |
*dstptr += sizeof(struct arpt_entry); |
| 1156 |
*size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); |
| 1157 |
|
| 1158 |
de->target_offset = e->target_offset - (origsize - *size); |
| 1159 |
t = compat_arpt_get_target(e); |
| 1160 |
xt_compat_target_from_user(t, dstptr, size); |
| 1161 |
|
| 1162 |
de->next_offset = e->next_offset - (origsize - *size); |
| 1163 |
for (h = 0; h < NF_ARP_NUMHOOKS; h++) { |
| 1164 |
if ((unsigned char *)de - base < newinfo->hook_entry[h]) |
| 1165 |
newinfo->hook_entry[h] -= origsize - *size; |
| 1166 |
if ((unsigned char *)de - base < newinfo->underflow[h]) |
| 1167 |
newinfo->underflow[h] -= origsize - *size; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
static int translate_compat_table(struct net *net, |
| 1172 |
struct xt_table_info **pinfo, |
| 1173 |
void **pentry0, |
| 1174 |
const struct compat_arpt_replace *compatr) |
| 1175 |
{ |
| 1176 |
unsigned int i, j; |
| 1177 |
struct xt_table_info *newinfo, *info; |
| 1178 |
void *pos, *entry0, *entry1; |
| 1179 |
struct compat_arpt_entry *iter0; |
| 1180 |
struct arpt_replace repl; |
| 1181 |
unsigned int size; |
| 1182 |
int ret; |
| 1183 |
|
| 1184 |
info = *pinfo; |
| 1185 |
entry0 = *pentry0; |
| 1186 |
size = compatr->size; |
| 1187 |
info->number = compatr->num_entries; |
| 1188 |
|
| 1189 |
j = 0; |
| 1190 |
xt_compat_lock(NFPROTO_ARP); |
| 1191 |
ret = xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries); |
| 1192 |
if (ret) |
| 1193 |
goto out_unlock; |
| 1194 |
/* Walk through entries, checking offsets. */ |
| 1195 |
xt_entry_foreach(iter0, entry0, compatr->size) { |
| 1196 |
ret = check_compat_entry_size_and_hooks(iter0, info, &size, |
| 1197 |
entry0, |
| 1198 |
entry0 + compatr->size); |
| 1199 |
if (ret != 0) |
| 1200 |
goto out_unlock; |
| 1201 |
++j; |
| 1202 |
} |
| 1203 |
|
| 1204 |
ret = -EINVAL; |
| 1205 |
if (j != compatr->num_entries) |
| 1206 |
goto out_unlock; |
| 1207 |
|
| 1208 |
ret = -ENOMEM; |
| 1209 |
newinfo = xt_alloc_table_info(size); |
| 1210 |
if (!newinfo) |
| 1211 |
goto out_unlock; |
| 1212 |
|
| 1213 |
memset(newinfo->entries, 0, size); |
| 1214 |
|
| 1215 |
newinfo->number = compatr->num_entries; |
| 1216 |
for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
| 1217 |
newinfo->hook_entry[i] = compatr->hook_entry[i]; |
| 1218 |
newinfo->underflow[i] = compatr->underflow[i]; |
| 1219 |
} |
| 1220 |
entry1 = newinfo->entries; |
| 1221 |
pos = entry1; |
| 1222 |
size = compatr->size; |
| 1223 |
xt_entry_foreach(iter0, entry0, compatr->size) |
| 1224 |
compat_copy_entry_from_user(iter0, &pos, &size, |
| 1225 |
newinfo, entry1); |
| 1226 |
|
| 1227 |
/* all module references in entry0 are now gone */ |
| 1228 |
|
| 1229 |
xt_compat_flush_offsets(NFPROTO_ARP); |
| 1230 |
xt_compat_unlock(NFPROTO_ARP); |
| 1231 |
|
| 1232 |
memcpy(&repl, compatr, sizeof(*compatr)); |
| 1233 |
|
| 1234 |
for (i = 0; i < NF_ARP_NUMHOOKS; i++) { |
| 1235 |
repl.hook_entry[i] = newinfo->hook_entry[i]; |
| 1236 |
repl.underflow[i] = newinfo->underflow[i]; |
| 1237 |
} |
| 1238 |
|
| 1239 |
repl.num_counters = 0; |
| 1240 |
repl.counters = NULL; |
| 1241 |
repl.size = newinfo->size; |
| 1242 |
ret = translate_table(net, newinfo, entry1, &repl); |
| 1243 |
if (ret) |
| 1244 |
goto free_newinfo; |
| 1245 |
|
| 1246 |
*pinfo = newinfo; |
| 1247 |
*pentry0 = entry1; |
| 1248 |
xt_free_table_info(info); |
| 1249 |
return 0; |
| 1250 |
|
| 1251 |
free_newinfo: |
| 1252 |
xt_free_table_info(newinfo); |
| 1253 |
return ret; |
| 1254 |
out_unlock: |
| 1255 |
xt_compat_flush_offsets(NFPROTO_ARP); |
| 1256 |
xt_compat_unlock(NFPROTO_ARP); |
| 1257 |
xt_entry_foreach(iter0, entry0, compatr->size) { |
| 1258 |
if (j-- == 0) |
| 1259 |
break; |
| 1260 |
compat_release_entry(iter0); |
| 1261 |
} |
| 1262 |
return ret; |
| 1263 |
} |
| 1264 |
|
| 1265 |
static int compat_do_replace(struct net *net, sockptr_t arg, unsigned int len) |
| 1266 |
{ |
| 1267 |
int ret; |
| 1268 |
struct compat_arpt_replace tmp; |
| 1269 |
struct xt_table_info *newinfo; |
| 1270 |
void *loc_cpu_entry; |
| 1271 |
struct arpt_entry *iter; |
| 1272 |
|
| 1273 |
if (len < sizeof(tmp)) |
| 1274 |
return -EINVAL; |
| 1275 |
if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0) |
| 1276 |
return -EFAULT; |
| 1277 |
|
| 1278 |
/* overflow check */ |
| 1279 |
if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters)) |
| 1280 |
return -ENOMEM; |
| 1281 |
if (tmp.num_counters == 0) |
| 1282 |
return -EINVAL; |
| 1283 |
if ((u64)len < (u64)tmp.size + sizeof(tmp)) |
| 1284 |
return -EINVAL; |
| 1285 |
|
| 1286 |
tmp.name[sizeof(tmp.name)-1] = 0; |
| 1287 |
|
| 1288 |
newinfo = xt_alloc_table_info(tmp.size); |
| 1289 |
if (!newinfo) |
| 1290 |
return -ENOMEM; |
| 1291 |
|
| 1292 |
loc_cpu_entry = newinfo->entries; |
| 1293 |
if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp), |
| 1294 |
tmp.size) != 0) { |
| 1295 |
ret = -EFAULT; |
| 1296 |
goto free_newinfo; |
| 1297 |
} |
| 1298 |
|
| 1299 |
ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp); |
| 1300 |
if (ret != 0) |
| 1301 |
goto free_newinfo; |
| 1302 |
|
| 1303 |
ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo, |
| 1304 |
tmp.num_counters, compat_ptr(tmp.counters)); |
| 1305 |
if (ret) |
| 1306 |
goto free_newinfo_untrans; |
| 1307 |
return 0; |
| 1308 |
|
| 1309 |
free_newinfo_untrans: |
| 1310 |
xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
| 1311 |
cleanup_entry(iter, net); |
| 1312 |
free_newinfo: |
| 1313 |
xt_free_table_info(newinfo); |
| 1314 |
return ret; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr, |
| 1318 |
compat_uint_t *size, |
| 1319 |
struct xt_counters *counters, |
| 1320 |
unsigned int i) |
| 1321 |
{ |
| 1322 |
struct xt_entry_target *t; |
| 1323 |
struct compat_arpt_entry __user *ce; |
| 1324 |
u_int16_t target_offset, next_offset; |
| 1325 |
compat_uint_t origsize; |
| 1326 |
int ret; |
| 1327 |
|
| 1328 |
origsize = *size; |
| 1329 |
ce = *dstptr; |
| 1330 |
if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 || |
| 1331 |
copy_to_user(&ce->counters, &counters[i], |
| 1332 |
sizeof(counters[i])) != 0) |
| 1333 |
return -EFAULT; |
| 1334 |
|
| 1335 |
*dstptr += sizeof(struct compat_arpt_entry); |
| 1336 |
*size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry); |
| 1337 |
|
| 1338 |
target_offset = e->target_offset - (origsize - *size); |
| 1339 |
|
| 1340 |
t = arpt_get_target(e); |
| 1341 |
ret = xt_compat_target_to_user(t, dstptr, size); |
| 1342 |
if (ret) |
| 1343 |
return ret; |
| 1344 |
next_offset = e->next_offset - (origsize - *size); |
| 1345 |
if (put_user(target_offset, &ce->target_offset) != 0 || |
| 1346 |
put_user(next_offset, &ce->next_offset) != 0) |
| 1347 |
return -EFAULT; |
| 1348 |
return 0; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int compat_copy_entries_to_user(unsigned int total_size, |
| 1352 |
struct xt_table *table, |
| 1353 |
void __user *userptr) |
| 1354 |
{ |
| 1355 |
struct xt_counters *counters; |
| 1356 |
const struct xt_table_info *private = table->private; |
| 1357 |
void __user *pos; |
| 1358 |
unsigned int size; |
| 1359 |
int ret = 0; |
| 1360 |
unsigned int i = 0; |
| 1361 |
struct arpt_entry *iter; |
| 1362 |
|
| 1363 |
counters = alloc_counters(table); |
| 1364 |
if (IS_ERR(counters)) |
| 1365 |
return PTR_ERR(counters); |
| 1366 |
|
| 1367 |
pos = userptr; |
| 1368 |
size = total_size; |
| 1369 |
xt_entry_foreach(iter, private->entries, total_size) { |
| 1370 |
ret = compat_copy_entry_to_user(iter, &pos, |
| 1371 |
&size, counters, i++); |
| 1372 |
if (ret != 0) |
| 1373 |
break; |
| 1374 |
} |
| 1375 |
vfree(counters); |
| 1376 |
return ret; |
| 1377 |
} |
| 1378 |
|
| 1379 |
struct compat_arpt_get_entries { |
| 1380 |
char name[XT_TABLE_MAXNAMELEN]; |
| 1381 |
compat_uint_t size; |
| 1382 |
struct compat_arpt_entry entrytable[]; |
| 1383 |
}; |
| 1384 |
|
| 1385 |
static int compat_get_entries(struct net *net, |
| 1386 |
struct compat_arpt_get_entries __user *uptr, |
| 1387 |
int *len) |
| 1388 |
{ |
| 1389 |
int ret; |
| 1390 |
struct compat_arpt_get_entries get; |
| 1391 |
struct xt_table *t; |
| 1392 |
|
| 1393 |
if (*len < sizeof(get)) |
| 1394 |
return -EINVAL; |
| 1395 |
if (copy_from_user(&get, uptr, sizeof(get)) != 0) |
| 1396 |
return -EFAULT; |
| 1397 |
if (*len != sizeof(struct compat_arpt_get_entries) + get.size) |
| 1398 |
return -EINVAL; |
| 1399 |
|
| 1400 |
get.name[sizeof(get.name) - 1] = '\0'; |
| 1401 |
|
| 1402 |
xt_compat_lock(NFPROTO_ARP); |
| 1403 |
t = xt_find_table_lock(net, NFPROTO_ARP, get.name); |
| 1404 |
if (!IS_ERR(t)) { |
| 1405 |
const struct xt_table_info *private = t->private; |
| 1406 |
struct xt_table_info info; |
| 1407 |
|
| 1408 |
ret = compat_table_info(private, &info); |
| 1409 |
if (!ret && get.size == info.size) { |
| 1410 |
ret = compat_copy_entries_to_user(private->size, |
| 1411 |
t, uptr->entrytable); |
| 1412 |
} else if (!ret) |
| 1413 |
ret = -EAGAIN; |
| 1414 |
|
| 1415 |
xt_compat_flush_offsets(NFPROTO_ARP); |
| 1416 |
module_put(t->me); |
| 1417 |
xt_table_unlock(t); |
| 1418 |
} else |
| 1419 |
ret = PTR_ERR(t); |
| 1420 |
|
| 1421 |
xt_compat_unlock(NFPROTO_ARP); |
| 1422 |
return ret; |
| 1423 |
} |
| 1424 |
#endif |
| 1425 |
|
| 1426 |
static int do_arpt_set_ctl(struct sock *sk, int cmd, sockptr_t arg, |
| 1427 |
unsigned int len) |
| 1428 |
{ |
| 1429 |
int ret; |
| 1430 |
|
| 1431 |
if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
| 1432 |
return -EPERM; |
| 1433 |
|
| 1434 |
switch (cmd) { |
| 1435 |
case ARPT_SO_SET_REPLACE: |
| 1436 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 1437 |
if (in_compat_syscall()) |
| 1438 |
ret = compat_do_replace(sock_net(sk), arg, len); |
| 1439 |
else |
| 1440 |
#endif |
| 1441 |
ret = do_replace(sock_net(sk), arg, len); |
| 1442 |
break; |
| 1443 |
|
| 1444 |
case ARPT_SO_SET_ADD_COUNTERS: |
| 1445 |
ret = do_add_counters(sock_net(sk), arg, len); |
| 1446 |
break; |
| 1447 |
|
| 1448 |
default: |
| 1449 |
ret = -EINVAL; |
| 1450 |
} |
| 1451 |
|
| 1452 |
return ret; |
| 1453 |
} |
| 1454 |
|
| 1455 |
static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) |
| 1456 |
{ |
| 1457 |
int ret; |
| 1458 |
|
| 1459 |
if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
| 1460 |
return -EPERM; |
| 1461 |
|
| 1462 |
switch (cmd) { |
| 1463 |
case ARPT_SO_GET_INFO: |
| 1464 |
ret = get_info(sock_net(sk), user, len); |
| 1465 |
break; |
| 1466 |
|
| 1467 |
case ARPT_SO_GET_ENTRIES: |
| 1468 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 1469 |
if (in_compat_syscall()) |
| 1470 |
ret = compat_get_entries(sock_net(sk), user, len); |
| 1471 |
else |
| 1472 |
#endif |
| 1473 |
ret = get_entries(sock_net(sk), user, len); |
| 1474 |
break; |
| 1475 |
|
| 1476 |
case ARPT_SO_GET_REVISION_TARGET: { |
| 1477 |
struct xt_get_revision rev; |
| 1478 |
|
| 1479 |
if (*len != sizeof(rev)) { |
| 1480 |
ret = -EINVAL; |
| 1481 |
break; |
| 1482 |
} |
| 1483 |
if (copy_from_user(&rev, user, sizeof(rev)) != 0) { |
| 1484 |
ret = -EFAULT; |
| 1485 |
break; |
| 1486 |
} |
| 1487 |
rev.name[sizeof(rev.name)-1] = 0; |
| 1488 |
|
| 1489 |
try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name, |
| 1490 |
rev.revision, 1, &ret), |
| 1491 |
"arpt_%s", rev.name); |
| 1492 |
break; |
| 1493 |
} |
| 1494 |
|
| 1495 |
default: |
| 1496 |
ret = -EINVAL; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return ret; |
| 1500 |
} |
| 1501 |
|
| 1502 |
static void __arpt_unregister_table(struct net *net, struct xt_table *table) |
| 1503 |
{ |
| 1504 |
struct xt_table_info *private = table->private; |
| 1505 |
struct module *table_owner = table->me; |
| 1506 |
void *loc_cpu_entry; |
| 1507 |
struct arpt_entry *iter; |
| 1508 |
|
| 1509 |
/* Decrease module usage counts and free resources */ |
| 1510 |
loc_cpu_entry = private->entries; |
| 1511 |
xt_entry_foreach(iter, loc_cpu_entry, private->size) |
| 1512 |
cleanup_entry(iter, net); |
| 1513 |
if (private->number > private->initial_entries) |
| 1514 |
module_put(table_owner); |
| 1515 |
xt_free_table_info(private); |
| 1516 |
kfree(table); |
| 1517 |
} |
| 1518 |
|
| 1519 |
int arpt_register_table(struct net *net, |
| 1520 |
const struct xt_table *table, |
| 1521 |
const struct arpt_replace *repl, |
| 1522 |
const struct nf_hook_ops *template_ops) |
| 1523 |
{ |
| 1524 |
struct xt_table_info bootstrap = {0}; |
| 1525 |
struct xt_table_info *newinfo; |
| 1526 |
struct xt_table *new_table; |
| 1527 |
void *loc_cpu_entry; |
| 1528 |
int ret; |
| 1529 |
|
| 1530 |
newinfo = xt_alloc_table_info(repl->size); |
| 1531 |
if (!newinfo) |
| 1532 |
return -ENOMEM; |
| 1533 |
|
| 1534 |
loc_cpu_entry = newinfo->entries; |
| 1535 |
memcpy(loc_cpu_entry, repl->entries, repl->size); |
| 1536 |
|
| 1537 |
ret = translate_table(net, newinfo, loc_cpu_entry, repl); |
| 1538 |
if (ret != 0) { |
| 1539 |
xt_free_table_info(newinfo); |
| 1540 |
return ret; |
| 1541 |
} |
| 1542 |
|
| 1543 |
new_table = xt_register_table(net, table, template_ops, &bootstrap, newinfo); |
| 1544 |
if (IS_ERR(new_table)) { |
| 1545 |
struct arpt_entry *iter; |
| 1546 |
|
| 1547 |
xt_entry_foreach(iter, loc_cpu_entry, newinfo->size) |
| 1548 |
cleanup_entry(iter, net); |
| 1549 |
xt_free_table_info(newinfo); |
| 1550 |
return PTR_ERR(new_table); |
| 1551 |
} |
| 1552 |
|
| 1553 |
return ret; |
| 1554 |
} |
| 1555 |
|
| 1556 |
void arpt_unregister_table(struct net *net, const char *name) |
| 1557 |
{ |
| 1558 |
struct xt_table *table = xt_unregister_table_exit(net, NFPROTO_ARP, name); |
| 1559 |
|
| 1560 |
if (table) |
| 1561 |
__arpt_unregister_table(net, table); |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* The built-in targets: standard (NULL) and error. */ |
| 1565 |
static struct xt_target arpt_builtin_tg[] __read_mostly = { |
| 1566 |
{ |
| 1567 |
.name = XT_STANDARD_TARGET, |
| 1568 |
.targetsize = sizeof(int), |
| 1569 |
.family = NFPROTO_ARP, |
| 1570 |
#ifdef CONFIG_NETFILTER_XTABLES_COMPAT |
| 1571 |
.compatsize = sizeof(compat_int_t), |
| 1572 |
.compat_from_user = compat_standard_from_user, |
| 1573 |
.compat_to_user = compat_standard_to_user, |
| 1574 |
#endif |
| 1575 |
}, |
| 1576 |
{ |
| 1577 |
.name = XT_ERROR_TARGET, |
| 1578 |
.target = arpt_error, |
| 1579 |
.targetsize = XT_FUNCTION_MAXNAMELEN, |
| 1580 |
.family = NFPROTO_ARP, |
| 1581 |
}, |
| 1582 |
}; |
| 1583 |
|
| 1584 |
static struct nf_sockopt_ops arpt_sockopts = { |
| 1585 |
.pf = PF_INET, |
| 1586 |
.set_optmin = ARPT_BASE_CTL, |
| 1587 |
.set_optmax = ARPT_SO_SET_MAX+1, |
| 1588 |
.set = do_arpt_set_ctl, |
| 1589 |
.get_optmin = ARPT_BASE_CTL, |
| 1590 |
.get_optmax = ARPT_SO_GET_MAX+1, |
| 1591 |
.get = do_arpt_get_ctl, |
| 1592 |
.owner = THIS_MODULE, |
| 1593 |
}; |
| 1594 |
|
| 1595 |
static int __net_init arp_tables_net_init(struct net *net) |
| 1596 |
{ |
| 1597 |
return xt_proto_init(net, NFPROTO_ARP); |
| 1598 |
} |
| 1599 |
|
| 1600 |
static void __net_exit arp_tables_net_exit(struct net *net) |
| 1601 |
{ |
| 1602 |
xt_proto_fini(net, NFPROTO_ARP); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static struct pernet_operations arp_tables_net_ops = { |
| 1606 |
.init = arp_tables_net_init, |
| 1607 |
.exit = arp_tables_net_exit, |
| 1608 |
}; |
| 1609 |
|
| 1610 |
static int __init arp_tables_init(void) |
| 1611 |
{ |
| 1612 |
int ret; |
| 1613 |
|
| 1614 |
ret = register_pernet_subsys(&arp_tables_net_ops); |
| 1615 |
if (ret < 0) |
| 1616 |
goto err1; |
| 1617 |
|
| 1618 |
/* No one else will be downing sem now, so we won't sleep */ |
| 1619 |
ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
| 1620 |
if (ret < 0) |
| 1621 |
goto err2; |
| 1622 |
|
| 1623 |
/* Register setsockopt */ |
| 1624 |
ret = nf_register_sockopt(&arpt_sockopts); |
| 1625 |
if (ret < 0) |
| 1626 |
goto err4; |
| 1627 |
|
| 1628 |
return 0; |
| 1629 |
|
| 1630 |
err4: |
| 1631 |
xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
| 1632 |
err2: |
| 1633 |
unregister_pernet_subsys(&arp_tables_net_ops); |
| 1634 |
err1: |
| 1635 |
return ret; |
| 1636 |
} |
| 1637 |
|
| 1638 |
static void __exit arp_tables_fini(void) |
| 1639 |
{ |
| 1640 |
nf_unregister_sockopt(&arpt_sockopts); |
| 1641 |
xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg)); |
| 1642 |
unregister_pernet_subsys(&arp_tables_net_ops); |
| 1643 |
} |
| 1644 |
|
| 1645 |
EXPORT_SYMBOL(arpt_register_table); |
| 1646 |
EXPORT_SYMBOL(arpt_unregister_table); |
| 1647 |
EXPORT_SYMBOL(arpt_do_table); |
| 1648 |
|
| 1649 |
module_init(arp_tables_init); |
| 1650 |
module_exit(arp_tables_fini); |