root/net/ipv4/netfilter/iptable_mangle.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ipt_mangle_out
  2. iptable_mangle_hook
  3. iptable_mangle_table_init
  4. iptable_mangle_net_exit
  5. iptable_mangle_init
  6. iptable_mangle_fini

   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 
  18 MODULE_LICENSE("GPL");
  19 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  20 MODULE_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 
  28 static int __net_init iptable_mangle_table_init(struct net *net);
  29 
  30 static 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 
  39 static unsigned int
  40 ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
  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, state->net->ipv4.iptable_mangle);
  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, 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. */
  75 static unsigned int
  76 iptable_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);
  82         return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
  83 }
  84 
  85 static struct nf_hook_ops *mangle_ops __read_mostly;
  86 static int __net_init iptable_mangle_table_init(struct net *net)
  87 {
  88         struct ipt_replace *repl;
  89         int ret;
  90 
  91         if (net->ipv4.iptable_mangle)
  92                 return 0;
  93 
  94         repl = ipt_alloc_initial_table(&packet_mangler);
  95         if (repl == NULL)
  96                 return -ENOMEM;
  97         ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
  98                                  &net->ipv4.iptable_mangle);
  99         kfree(repl);
 100         return ret;
 101 }
 102 
 103 static void __net_exit iptable_mangle_net_exit(struct net *net)
 104 {
 105         if (!net->ipv4.iptable_mangle)
 106                 return;
 107         ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops);
 108         net->ipv4.iptable_mangle = NULL;
 109 }
 110 
 111 static struct pernet_operations iptable_mangle_net_ops = {
 112         .exit = iptable_mangle_net_exit,
 113 };
 114 
 115 static int __init iptable_mangle_init(void)
 116 {
 117         int ret;
 118 
 119         mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
 120         if (IS_ERR(mangle_ops)) {
 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                 kfree(mangle_ops);
 128                 return ret;
 129         }
 130 
 131         ret = iptable_mangle_table_init(&init_net);
 132         if (ret) {
 133                 unregister_pernet_subsys(&iptable_mangle_net_ops);
 134                 kfree(mangle_ops);
 135         }
 136 
 137         return ret;
 138 }
 139 
 140 static void __exit iptable_mangle_fini(void)
 141 {
 142         unregister_pernet_subsys(&iptable_mangle_net_ops);
 143         kfree(mangle_ops);
 144 }
 145 
 146 module_init(iptable_mangle_init);
 147 module_exit(iptable_mangle_fini);

/* [<][>][^][v][top][bottom][index][help] */