linux/net/ipv6/netfilter/ip6table_raw.c
<<
>>
Prefs
   1/*
   2 * IPv6 raw table, a port of the IPv4 raw table to IPv6
   3 *
   4 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
   5 */
   6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   7#include <linux/module.h>
   8#include <linux/netfilter_ipv6/ip6_tables.h>
   9#include <linux/slab.h>
  10
  11#define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
  12
  13static int __net_init ip6table_raw_table_init(struct net *net);
  14
  15static bool raw_before_defrag __read_mostly;
  16MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag");
  17module_param(raw_before_defrag, bool, 0000);
  18
  19static const struct xt_table packet_raw = {
  20        .name = "raw",
  21        .valid_hooks = RAW_VALID_HOOKS,
  22        .me = THIS_MODULE,
  23        .af = NFPROTO_IPV6,
  24        .priority = NF_IP6_PRI_RAW,
  25        .table_init = ip6table_raw_table_init,
  26};
  27
  28static const struct xt_table packet_raw_before_defrag = {
  29        .name = "raw",
  30        .valid_hooks = RAW_VALID_HOOKS,
  31        .me = THIS_MODULE,
  32        .af = NFPROTO_IPV6,
  33        .priority = NF_IP6_PRI_RAW_BEFORE_DEFRAG,
  34        .table_init = ip6table_raw_table_init,
  35};
  36
  37/* The work comes in here from netfilter.c. */
  38static unsigned int
  39ip6table_raw_hook(void *priv, struct sk_buff *skb,
  40                  const struct nf_hook_state *state)
  41{
  42        return ip6t_do_table(skb, state, state->net->ipv6.ip6table_raw);
  43}
  44
  45static struct nf_hook_ops *rawtable_ops __read_mostly;
  46
  47static int __net_init ip6table_raw_table_init(struct net *net)
  48{
  49        struct ip6t_replace *repl;
  50        const struct xt_table *table = &packet_raw;
  51        int ret;
  52
  53        if (raw_before_defrag)
  54                table = &packet_raw_before_defrag;
  55
  56        if (net->ipv6.ip6table_raw)
  57                return 0;
  58
  59        repl = ip6t_alloc_initial_table(table);
  60        if (repl == NULL)
  61                return -ENOMEM;
  62        ret = ip6t_register_table(net, table, repl, rawtable_ops,
  63                                  &net->ipv6.ip6table_raw);
  64        kfree(repl);
  65        return ret;
  66}
  67
  68static void __net_exit ip6table_raw_net_pre_exit(struct net *net)
  69{
  70        if (net->ipv6.ip6table_raw)
  71                ip6t_unregister_table_pre_exit(net, net->ipv6.ip6table_raw,
  72                                               rawtable_ops);
  73}
  74
  75static void __net_exit ip6table_raw_net_exit(struct net *net)
  76{
  77        if (!net->ipv6.ip6table_raw)
  78                return;
  79        ip6t_unregister_table_exit(net, net->ipv6.ip6table_raw);
  80        net->ipv6.ip6table_raw = NULL;
  81}
  82
  83static struct pernet_operations ip6table_raw_net_ops = {
  84        .pre_exit = ip6table_raw_net_pre_exit,
  85        .exit = ip6table_raw_net_exit,
  86};
  87
  88static int __init ip6table_raw_init(void)
  89{
  90        int ret;
  91        const struct xt_table *table = &packet_raw;
  92
  93        if (raw_before_defrag) {
  94                table = &packet_raw_before_defrag;
  95
  96                pr_info("Enabling raw table before defrag\n");
  97        }
  98
  99        /* Register hooks */
 100        rawtable_ops = xt_hook_ops_alloc(table, ip6table_raw_hook);
 101        if (IS_ERR(rawtable_ops))
 102                return PTR_ERR(rawtable_ops);
 103
 104        ret = register_pernet_subsys(&ip6table_raw_net_ops);
 105        if (ret < 0) {
 106                kfree(rawtable_ops);
 107                return ret;
 108        }
 109
 110        ret = ip6table_raw_table_init(&init_net);
 111        if (ret) {
 112                unregister_pernet_subsys(&ip6table_raw_net_ops);
 113                kfree(rawtable_ops);
 114        }
 115        return ret;
 116}
 117
 118static void __exit ip6table_raw_fini(void)
 119{
 120        unregister_pernet_subsys(&ip6table_raw_net_ops);
 121        kfree(rawtable_ops);
 122}
 123
 124module_init(ip6table_raw_init);
 125module_exit(ip6table_raw_fini);
 126MODULE_LICENSE("GPL");
 127