linux/net/sched/act_nat.c
<<
>>
Prefs
   1/*
   2 * Stateless NAT actions
   3 *
   4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License as published by the Free
   8 * Software Foundation; either version 2 of the License, or (at your option)
   9 * any later version.
  10 */
  11
  12#include <linux/errno.h>
  13#include <linux/init.h>
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/netfilter.h>
  17#include <linux/rtnetlink.h>
  18#include <linux/skbuff.h>
  19#include <linux/slab.h>
  20#include <linux/spinlock.h>
  21#include <linux/string.h>
  22#include <linux/tc_act/tc_nat.h>
  23#include <net/act_api.h>
  24#include <net/icmp.h>
  25#include <net/ip.h>
  26#include <net/netlink.h>
  27#include <net/tc_act/tc_nat.h>
  28#include <net/tcp.h>
  29#include <net/udp.h>
  30
  31
  32static int nat_net_id;
  33static struct tc_action_ops act_nat_ops;
  34
  35static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
  36        [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
  37};
  38
  39static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
  40                        struct tc_action **a, int ovr, int bind)
  41{
  42        struct tc_action_net *tn = net_generic(net, nat_net_id);
  43        struct nlattr *tb[TCA_NAT_MAX + 1];
  44        struct tc_nat *parm;
  45        int ret = 0, err;
  46        struct tcf_nat *p;
  47
  48        if (nla == NULL)
  49                return -EINVAL;
  50
  51        err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy);
  52        if (err < 0)
  53                return err;
  54
  55        if (tb[TCA_NAT_PARMS] == NULL)
  56                return -EINVAL;
  57        parm = nla_data(tb[TCA_NAT_PARMS]);
  58
  59        if (!tcf_idr_check(tn, parm->index, a, bind)) {
  60                ret = tcf_idr_create(tn, parm->index, est, a,
  61                                     &act_nat_ops, bind, false);
  62                if (ret)
  63                        return ret;
  64                ret = ACT_P_CREATED;
  65        } else {
  66                if (bind)
  67                        return 0;
  68                if (!ovr) {
  69                        tcf_idr_release(*a, bind);
  70                        return -EEXIST;
  71                }
  72        }
  73        p = to_tcf_nat(*a);
  74
  75        spin_lock_bh(&p->tcf_lock);
  76        p->old_addr = parm->old_addr;
  77        p->new_addr = parm->new_addr;
  78        p->mask = parm->mask;
  79        p->flags = parm->flags;
  80
  81        p->tcf_action = parm->action;
  82        spin_unlock_bh(&p->tcf_lock);
  83
  84        if (ret == ACT_P_CREATED)
  85                tcf_idr_insert(tn, *a);
  86
  87        return ret;
  88}
  89
  90static int tcf_nat(struct sk_buff *skb, const struct tc_action *a,
  91                   struct tcf_result *res)
  92{
  93        struct tcf_nat *p = to_tcf_nat(a);
  94        struct iphdr *iph;
  95        __be32 old_addr;
  96        __be32 new_addr;
  97        __be32 mask;
  98        __be32 addr;
  99        int egress;
 100        int action;
 101        int ihl;
 102        int noff;
 103
 104        spin_lock(&p->tcf_lock);
 105
 106        tcf_lastuse_update(&p->tcf_tm);
 107        old_addr = p->old_addr;
 108        new_addr = p->new_addr;
 109        mask = p->mask;
 110        egress = p->flags & TCA_NAT_FLAG_EGRESS;
 111        action = p->tcf_action;
 112
 113        bstats_update(&p->tcf_bstats, skb);
 114
 115        spin_unlock(&p->tcf_lock);
 116
 117        if (unlikely(action == TC_ACT_SHOT))
 118                goto drop;
 119
 120        noff = skb_network_offset(skb);
 121        if (!pskb_may_pull(skb, sizeof(*iph) + noff))
 122                goto drop;
 123
 124        iph = ip_hdr(skb);
 125
 126        if (egress)
 127                addr = iph->saddr;
 128        else
 129                addr = iph->daddr;
 130
 131        if (!((old_addr ^ addr) & mask)) {
 132                if (skb_try_make_writable(skb, sizeof(*iph) + noff))
 133                        goto drop;
 134
 135                new_addr &= mask;
 136                new_addr |= addr & ~mask;
 137
 138                /* Rewrite IP header */
 139                iph = ip_hdr(skb);
 140                if (egress)
 141                        iph->saddr = new_addr;
 142                else
 143                        iph->daddr = new_addr;
 144
 145                csum_replace4(&iph->check, addr, new_addr);
 146        } else if ((iph->frag_off & htons(IP_OFFSET)) ||
 147                   iph->protocol != IPPROTO_ICMP) {
 148                goto out;
 149        }
 150
 151        ihl = iph->ihl * 4;
 152
 153        /* It would be nice to share code with stateful NAT. */
 154        switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
 155        case IPPROTO_TCP:
 156        {
 157                struct tcphdr *tcph;
 158
 159                if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
 160                    skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
 161                        goto drop;
 162
 163                tcph = (void *)(skb_network_header(skb) + ihl);
 164                inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1);
 165                break;
 166        }
 167        case IPPROTO_UDP:
 168        {
 169                struct udphdr *udph;
 170
 171                if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
 172                    skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
 173                        goto drop;
 174
 175                udph = (void *)(skb_network_header(skb) + ihl);
 176                if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
 177                        inet_proto_csum_replace4(&udph->check, skb, addr,
 178                                                 new_addr, 1);
 179                        if (!udph->check)
 180                                udph->check = CSUM_MANGLED_0;
 181                }
 182                break;
 183        }
 184        case IPPROTO_ICMP:
 185        {
 186                struct icmphdr *icmph;
 187
 188                if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
 189                        goto drop;
 190
 191                icmph = (void *)(skb_network_header(skb) + ihl);
 192
 193                if ((icmph->type != ICMP_DEST_UNREACH) &&
 194                    (icmph->type != ICMP_TIME_EXCEEDED) &&
 195                    (icmph->type != ICMP_PARAMETERPROB))
 196                        break;
 197
 198                if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
 199                                        noff))
 200                        goto drop;
 201
 202                icmph = (void *)(skb_network_header(skb) + ihl);
 203                iph = (void *)(icmph + 1);
 204                if (egress)
 205                        addr = iph->daddr;
 206                else
 207                        addr = iph->saddr;
 208
 209                if ((old_addr ^ addr) & mask)
 210                        break;
 211
 212                if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
 213                                          sizeof(*iph) + noff))
 214                        goto drop;
 215
 216                icmph = (void *)(skb_network_header(skb) + ihl);
 217                iph = (void *)(icmph + 1);
 218
 219                new_addr &= mask;
 220                new_addr |= addr & ~mask;
 221
 222                /* XXX Fix up the inner checksums. */
 223                if (egress)
 224                        iph->daddr = new_addr;
 225                else
 226                        iph->saddr = new_addr;
 227
 228                inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
 229                                         0);
 230                break;
 231        }
 232        default:
 233                break;
 234        }
 235
 236out:
 237        return action;
 238
 239drop:
 240        spin_lock(&p->tcf_lock);
 241        p->tcf_qstats.drops++;
 242        spin_unlock(&p->tcf_lock);
 243        return TC_ACT_SHOT;
 244}
 245
 246static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
 247                        int bind, int ref)
 248{
 249        unsigned char *b = skb_tail_pointer(skb);
 250        struct tcf_nat *p = to_tcf_nat(a);
 251        struct tc_nat opt = {
 252                .old_addr = p->old_addr,
 253                .new_addr = p->new_addr,
 254                .mask     = p->mask,
 255                .flags    = p->flags,
 256
 257                .index    = p->tcf_index,
 258                .action   = p->tcf_action,
 259                .refcnt   = p->tcf_refcnt - ref,
 260                .bindcnt  = p->tcf_bindcnt - bind,
 261        };
 262        struct tcf_t t;
 263
 264        if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
 265                goto nla_put_failure;
 266
 267        tcf_tm_dump(&t, &p->tcf_tm);
 268        if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
 269                goto nla_put_failure;
 270
 271        return skb->len;
 272
 273nla_put_failure:
 274        nlmsg_trim(skb, b);
 275        return -1;
 276}
 277
 278static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
 279                          struct netlink_callback *cb, int type,
 280                          const struct tc_action_ops *ops)
 281{
 282        struct tc_action_net *tn = net_generic(net, nat_net_id);
 283
 284        return tcf_generic_walker(tn, skb, cb, type, ops);
 285}
 286
 287static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
 288{
 289        struct tc_action_net *tn = net_generic(net, nat_net_id);
 290
 291        return tcf_idr_search(tn, a, index);
 292}
 293
 294static struct tc_action_ops act_nat_ops = {
 295        .kind           =       "nat",
 296        .type           =       TCA_ACT_NAT,
 297        .owner          =       THIS_MODULE,
 298        .act            =       tcf_nat,
 299        .dump           =       tcf_nat_dump,
 300        .init           =       tcf_nat_init,
 301        .walk           =       tcf_nat_walker,
 302        .lookup         =       tcf_nat_search,
 303        .size           =       sizeof(struct tcf_nat),
 304};
 305
 306static __net_init int nat_init_net(struct net *net)
 307{
 308        struct tc_action_net *tn = net_generic(net, nat_net_id);
 309
 310        return tc_action_net_init(tn, &act_nat_ops);
 311}
 312
 313static void __net_exit nat_exit_net(struct net *net)
 314{
 315        struct tc_action_net *tn = net_generic(net, nat_net_id);
 316
 317        tc_action_net_exit(tn);
 318}
 319
 320static struct pernet_operations nat_net_ops = {
 321        .init = nat_init_net,
 322        .exit = nat_exit_net,
 323        .id   = &nat_net_id,
 324        .size = sizeof(struct tc_action_net),
 325};
 326
 327MODULE_DESCRIPTION("Stateless NAT actions");
 328MODULE_LICENSE("GPL");
 329
 330static int __init nat_init_module(void)
 331{
 332        return tcf_register_action(&act_nat_ops, &nat_net_ops);
 333}
 334
 335static void __exit nat_cleanup_module(void)
 336{
 337        tcf_unregister_action(&act_nat_ops, &nat_net_ops);
 338}
 339
 340module_init(nat_init_module);
 341module_exit(nat_cleanup_module);
 342