Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
arptable_filter.c
Go to the documentation of this file.
1 /*
2  * Filtering ARP tables module.
3  *
4  * Copyright (C) 2002 David S. Miller ([email protected])
5  *
6  */
7 
8 #include <linux/module.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter_arp/arp_tables.h>
11 #include <linux/slab.h>
12 
13 MODULE_LICENSE("GPL");
14 MODULE_AUTHOR("David S. Miller <[email protected]>");
15 MODULE_DESCRIPTION("arptables filter table");
16 
17 #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
18  (1 << NF_ARP_FORWARD))
19 
20 static const struct xt_table packet_filter = {
21  .name = "filter",
22  .valid_hooks = FILTER_VALID_HOOKS,
23  .me = THIS_MODULE,
24  .af = NFPROTO_ARP,
25  .priority = NF_IP_PRI_FILTER,
26 };
27 
28 /* The work comes in here from netfilter.c */
29 static unsigned int
30 arptable_filter_hook(unsigned int hook, struct sk_buff *skb,
31  const struct net_device *in, const struct net_device *out,
32  int (*okfn)(struct sk_buff *))
33 {
34  const struct net *net = dev_net((in != NULL) ? in : out);
35 
36  return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter);
37 }
38 
39 static struct nf_hook_ops *arpfilter_ops __read_mostly;
40 
41 static int __net_init arptable_filter_net_init(struct net *net)
42 {
43  struct arpt_replace *repl;
44 
45  repl = arpt_alloc_initial_table(&packet_filter);
46  if (repl == NULL)
47  return -ENOMEM;
48  net->ipv4.arptable_filter =
49  arpt_register_table(net, &packet_filter, repl);
50  kfree(repl);
51  if (IS_ERR(net->ipv4.arptable_filter))
52  return PTR_ERR(net->ipv4.arptable_filter);
53  return 0;
54 }
55 
56 static void __net_exit arptable_filter_net_exit(struct net *net)
57 {
58  arpt_unregister_table(net->ipv4.arptable_filter);
59 }
60 
61 static struct pernet_operations arptable_filter_net_ops = {
62  .init = arptable_filter_net_init,
63  .exit = arptable_filter_net_exit,
64 };
65 
66 static int __init arptable_filter_init(void)
67 {
68  int ret;
69 
70  ret = register_pernet_subsys(&arptable_filter_net_ops);
71  if (ret < 0)
72  return ret;
73 
74  arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook);
75  if (IS_ERR(arpfilter_ops)) {
76  ret = PTR_ERR(arpfilter_ops);
77  goto cleanup_table;
78  }
79  return ret;
80 
81 cleanup_table:
82  unregister_pernet_subsys(&arptable_filter_net_ops);
83  return ret;
84 }
85 
86 static void __exit arptable_filter_fini(void)
87 {
88  xt_hook_unlink(&packet_filter, arpfilter_ops);
89  unregister_pernet_subsys(&arptable_filter_net_ops);
90 }
91 
92 module_init(arptable_filter_init);
93 module_exit(arptable_filter_fini);