linux/net/ipv4/netfilter/iptable_mangle.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
   4 *
   5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
   6 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
   7 */
   8#include <linux/module.h>
   9#include <linux/netfilter_ipv4/ip_tables.h>
  10#include <linux/netdevice.h>
  11#include <linux/skbuff.h>
  12#include <linux/slab.h>
  13#include <net/sock.h>
  14#include <net/route.h>
  15#include <linux/ip.h>
  16#include <net/ip.h>
  17
  18MODULE_LICENSE("GPL");
  19MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  20MODULE_DESCRIPTION("iptables mangle table");
  21
  22#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
  23                            (1 << NF_INET_LOCAL_IN) | \
  24                            (1 << NF_INET_FORWARD) | \
  25                            (1 << NF_INET_LOCAL_OUT) | \
  26                            (1 << NF_INET_POST_ROUTING))
  27
  28static int __net_init iptable_mangle_table_init(struct net *net);
  29
  30static const struct xt_table packet_mangler = {
  31        .name           = "mangle",
  32        .valid_hooks    = MANGLE_VALID_HOOKS,
  33        .me             = THIS_MODULE,
  34        .af             = NFPROTO_IPV4,
  35        .priority       = NF_IP_PRI_MANGLE,
  36        .table_init     = iptable_mangle_table_init,
  37};
  38
  39static unsigned int
  40ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state, void *priv)
  41{
  42        unsigned int ret;
  43        const struct iphdr *iph;
  44        u_int8_t tos;
  45        __be32 saddr, daddr;
  46        u_int32_t mark;
  47        int err;
  48
  49        /* Save things which could affect route */
  50        mark = skb->mark;
  51        iph = ip_hdr(skb);
  52        saddr = iph->saddr;
  53        daddr = iph->daddr;
  54        tos = iph->tos;
  55
  56        ret = ipt_do_table(skb, state, priv);
  57        /* Reroute for ANY change. */
  58        if (ret != NF_DROP && ret != NF_STOLEN) {
  59                iph = ip_hdr(skb);
  60
  61                if (iph->saddr != saddr ||
  62                    iph->daddr != daddr ||
  63                    skb->mark != mark ||
  64                    iph->tos != tos) {
  65                        err = ip_route_me_harder(state->net, state->sk, skb, RTN_UNSPEC);
  66                        if (err < 0)
  67                                ret = NF_DROP_ERR(err);
  68                }
  69        }
  70
  71        return ret;
  72}
  73
  74/* The work comes in here from netfilter.c. */
  75static unsigned int
  76iptable_mangle_hook(void *priv,
  77                     struct sk_buff *skb,
  78                     const struct nf_hook_state *state)
  79{
  80        if (state->hook == NF_INET_LOCAL_OUT)
  81                return ipt_mangle_out(skb, state, priv);
  82        return ipt_do_table(skb, state, priv);
  83}
  84
  85static struct nf_hook_ops *mangle_ops __read_mostly;
  86static int __net_init iptable_mangle_table_init(struct net *net)
  87{
  88        struct ipt_replace *repl;
  89        int ret;
  90
  91        repl = ipt_alloc_initial_table(&packet_mangler);
  92        if (repl == NULL)
  93                return -ENOMEM;
  94        ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops);
  95        kfree(repl);
  96        return ret;
  97}
  98
  99static void __net_exit iptable_mangle_net_pre_exit(struct net *net)
 100{
 101        ipt_unregister_table_pre_exit(net, "mangle");
 102}
 103
 104static void __net_exit iptable_mangle_net_exit(struct net *net)
 105{
 106        ipt_unregister_table_exit(net, "mangle");
 107}
 108
 109static struct pernet_operations iptable_mangle_net_ops = {
 110        .pre_exit = iptable_mangle_net_pre_exit,
 111        .exit = iptable_mangle_net_exit,
 112};
 113
 114static int __init iptable_mangle_init(void)
 115{
 116        int ret;
 117
 118        mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
 119        if (IS_ERR(mangle_ops)) {
 120                ret = PTR_ERR(mangle_ops);
 121                return ret;
 122        }
 123
 124        ret = register_pernet_subsys(&iptable_mangle_net_ops);
 125        if (ret < 0) {
 126                kfree(mangle_ops);
 127                return ret;
 128        }
 129
 130        ret = iptable_mangle_table_init(&init_net);
 131        if (ret) {
 132                unregister_pernet_subsys(&iptable_mangle_net_ops);
 133                kfree(mangle_ops);
 134        }
 135
 136        return ret;
 137}
 138
 139static void __exit iptable_mangle_fini(void)
 140{
 141        unregister_pernet_subsys(&iptable_mangle_net_ops);
 142        kfree(mangle_ops);
 143}
 144
 145module_init(iptable_mangle_init);
 146module_exit(iptable_mangle_fini);
 147