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