linux/net/ipv4/netfilter/ipt_NETMAP.c
<<
>>
Prefs
   1/* NETMAP - static NAT mapping of IP network addresses (1:1).
   2 * The mapping can be applied to source (POSTROUTING),
   3 * destination (PREROUTING), or both (with separate rules).
   4 */
   5
   6/* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/ip.h>
  14#include <linux/module.h>
  15#include <linux/netdevice.h>
  16#include <linux/netfilter.h>
  17#include <linux/netfilter_ipv4.h>
  18#include <linux/netfilter/x_tables.h>
  19#include <net/netfilter/nf_nat_rule.h>
  20
  21MODULE_LICENSE("GPL");
  22MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
  23MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of IPv4 subnets");
  24
  25static bool netmap_tg_check(const struct xt_tgchk_param *par)
  26{
  27        const struct nf_nat_multi_range_compat *mr = par->targinfo;
  28
  29        if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
  30                pr_debug("NETMAP:check: bad MAP_IPS.\n");
  31                return false;
  32        }
  33        if (mr->rangesize != 1) {
  34                pr_debug("NETMAP:check: bad rangesize %u.\n", mr->rangesize);
  35                return false;
  36        }
  37        return true;
  38}
  39
  40static unsigned int
  41netmap_tg(struct sk_buff *skb, const struct xt_target_param *par)
  42{
  43        struct nf_conn *ct;
  44        enum ip_conntrack_info ctinfo;
  45        __be32 new_ip, netmask;
  46        const struct nf_nat_multi_range_compat *mr = par->targinfo;
  47        struct nf_nat_range newrange;
  48
  49        NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
  50                     par->hooknum == NF_INET_POST_ROUTING ||
  51                     par->hooknum == NF_INET_LOCAL_OUT);
  52        ct = nf_ct_get(skb, &ctinfo);
  53
  54        netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
  55
  56        if (par->hooknum == NF_INET_PRE_ROUTING ||
  57            par->hooknum == NF_INET_LOCAL_OUT)
  58                new_ip = ip_hdr(skb)->daddr & ~netmask;
  59        else
  60                new_ip = ip_hdr(skb)->saddr & ~netmask;
  61        new_ip |= mr->range[0].min_ip & netmask;
  62
  63        newrange = ((struct nf_nat_range)
  64                { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
  65                  new_ip, new_ip,
  66                  mr->range[0].min, mr->range[0].max });
  67
  68        /* Hand modified range to generic setup. */
  69        return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
  70}
  71
  72static struct xt_target netmap_tg_reg __read_mostly = {
  73        .name           = "NETMAP",
  74        .family         = NFPROTO_IPV4,
  75        .target         = netmap_tg,
  76        .targetsize     = sizeof(struct nf_nat_multi_range_compat),
  77        .table          = "nat",
  78        .hooks          = (1 << NF_INET_PRE_ROUTING) |
  79                          (1 << NF_INET_POST_ROUTING) |
  80                          (1 << NF_INET_LOCAL_OUT),
  81        .checkentry     = netmap_tg_check,
  82        .me             = THIS_MODULE
  83};
  84
  85static int __init netmap_tg_init(void)
  86{
  87        return xt_register_target(&netmap_tg_reg);
  88}
  89
  90static void __exit netmap_tg_exit(void)
  91{
  92        xt_unregister_target(&netmap_tg_reg);
  93}
  94
  95module_init(netmap_tg_init);
  96module_exit(netmap_tg_exit);
  97