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 const struct xt_table packet_mangler = {
  29        .name           = "mangle",
  30        .valid_hooks    = MANGLE_VALID_HOOKS,
  31        .me             = THIS_MODULE,
  32        .af             = NFPROTO_IPV4,
  33        .priority       = NF_IP_PRI_MANGLE,
  34};
  35
  36static unsigned int
  37ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state, void *priv)
  38{
  39        unsigned int ret;
  40        const struct iphdr *iph;
  41        u_int8_t tos;
  42        __be32 saddr, daddr;
  43        u_int32_t mark;
  44        int err;
  45
  46        /* Save things which could affect route */
  47        mark = skb->mark;
  48        iph = ip_hdr(skb);
  49        saddr = iph->saddr;
  50        daddr = iph->daddr;
  51        tos = iph->tos;
  52
  53        ret = ipt_do_table(skb, state, priv);
  54        /* Reroute for ANY change. */
  55        if (ret != NF_DROP && ret != NF_STOLEN) {
  56                iph = ip_hdr(skb);
  57
  58                if (iph->saddr != saddr ||
  59                    iph->daddr != daddr ||
  60                    skb->mark != mark ||
  61                    iph->tos != tos) {
  62                        err = ip_route_me_harder(state->net, state->sk, skb, RTN_UNSPEC);
  63                        if (err < 0)
  64                                ret = NF_DROP_ERR(err);
  65                }
  66        }
  67
  68        return ret;
  69}
  70
  71/* The work comes in here from netfilter.c. */
  72static unsigned int
  73iptable_mangle_hook(void *priv,
  74                     struct sk_buff *skb,
  75                     const struct nf_hook_state *state)
  76{
  77        if (state->hook == NF_INET_LOCAL_OUT)
  78                return ipt_mangle_out(skb, state, priv);
  79        return ipt_do_table(skb, state, priv);
  80}
  81
  82static struct nf_hook_ops *mangle_ops __read_mostly;
  83static int iptable_mangle_table_init(struct net *net)
  84{
  85        struct ipt_replace *repl;
  86        int ret;
  87
  88        repl = ipt_alloc_initial_table(&packet_mangler);
  89        if (repl == NULL)
  90                return -ENOMEM;
  91        ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops);
  92        kfree(repl);
  93        return ret;
  94}
  95
  96static void __net_exit iptable_mangle_net_pre_exit(struct net *net)
  97{
  98        ipt_unregister_table_pre_exit(net, "mangle");
  99}
 100
 101static void __net_exit iptable_mangle_net_exit(struct net *net)
 102{
 103        ipt_unregister_table_exit(net, "mangle");
 104}
 105
 106static struct pernet_operations iptable_mangle_net_ops = {
 107        .pre_exit = iptable_mangle_net_pre_exit,
 108        .exit = iptable_mangle_net_exit,
 109};
 110
 111static int __init iptable_mangle_init(void)
 112{
 113        int ret = xt_register_template(&packet_mangler,
 114                                       iptable_mangle_table_init);
 115        if (ret < 0)
 116                return ret;
 117
 118        mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
 119        if (IS_ERR(mangle_ops)) {
 120                xt_unregister_template(&packet_mangler);
 121                ret = PTR_ERR(mangle_ops);
 122                return ret;
 123        }
 124
 125        ret = register_pernet_subsys(&iptable_mangle_net_ops);
 126        if (ret < 0) {
 127                xt_unregister_template(&packet_mangler);
 128                kfree(mangle_ops);
 129                return ret;
 130        }
 131
 132        return ret;
 133}
 134
 135static void __exit iptable_mangle_fini(void)
 136{
 137        unregister_pernet_subsys(&iptable_mangle_net_ops);
 138        xt_unregister_template(&packet_mangler);
 139        kfree(mangle_ops);
 140}
 141
 142module_init(iptable_mangle_init);
 143module_exit(iptable_mangle_fini);
 144