~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/iptable_mangle.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
  3  *
  4  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  5  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
  6  *
  7  * This program is free software; you can redistribute it and/or modify
  8  * it under the terms of the GNU General Public License version 2 as
  9  * published by the Free Software Foundation.
 10  */
 11 #include <linux/module.h>
 12 #include <linux/netfilter_ipv4/ip_tables.h>
 13 #include <linux/netdevice.h>
 14 #include <linux/skbuff.h>
 15 #include <linux/slab.h>
 16 #include <net/sock.h>
 17 #include <net/route.h>
 18 #include <linux/ip.h>
 19 #include <net/ip.h>
 20 
 21 MODULE_LICENSE("GPL");
 22 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 23 MODULE_DESCRIPTION("iptables mangle table");
 24 
 25 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
 26                             (1 << NF_INET_LOCAL_IN) | \
 27                             (1 << NF_INET_FORWARD) | \
 28                             (1 << NF_INET_LOCAL_OUT) | \
 29                             (1 << NF_INET_POST_ROUTING))
 30 
 31 static const struct xt_table packet_mangler = {
 32         .name           = "mangle",
 33         .valid_hooks    = MANGLE_VALID_HOOKS,
 34         .me             = THIS_MODULE,
 35         .af             = NFPROTO_IPV4,
 36         .priority       = NF_IP_PRI_MANGLE,
 37 };
 38 
 39 static unsigned int
 40 ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
 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         /* root is playing with raw sockets. */
 50         if (skb->len < sizeof(struct iphdr) ||
 51             ip_hdrlen(skb) < sizeof(struct iphdr))
 52                 return NF_ACCEPT;
 53 
 54         /* Save things which could affect route */
 55         mark = skb->mark;
 56         iph = ip_hdr(skb);
 57         saddr = iph->saddr;
 58         daddr = iph->daddr;
 59         tos = iph->tos;
 60 
 61         ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
 62                            dev_net(out)->ipv4.iptable_mangle);
 63         /* Reroute for ANY change. */
 64         if (ret != NF_DROP && ret != NF_STOLEN) {
 65                 iph = ip_hdr(skb);
 66 
 67                 if (iph->saddr != saddr ||
 68                     iph->daddr != daddr ||
 69                     skb->mark != mark ||
 70                     iph->tos != tos) {
 71                         err = ip_route_me_harder(skb, RTN_UNSPEC);
 72                         if (err < 0)
 73                                 ret = NF_DROP_ERR(err);
 74                 }
 75         }
 76 
 77         return ret;
 78 }
 79 
 80 /* The work comes in here from netfilter.c. */
 81 static unsigned int
 82 iptable_mangle_hook(const struct nf_hook_ops *ops,
 83                      struct sk_buff *skb,
 84                      const struct net_device *in,
 85                      const struct net_device *out,
 86                      int (*okfn)(struct sk_buff *))
 87 {
 88         if (ops->hooknum == NF_INET_LOCAL_OUT)
 89                 return ipt_mangle_out(skb, out);
 90         if (ops->hooknum == NF_INET_POST_ROUTING)
 91                 return ipt_do_table(skb, ops->hooknum, in, out,
 92                                     dev_net(out)->ipv4.iptable_mangle);
 93         /* PREROUTING/INPUT/FORWARD: */
 94         return ipt_do_table(skb, ops->hooknum, in, out,
 95                             dev_net(in)->ipv4.iptable_mangle);
 96 }
 97 
 98 static struct nf_hook_ops *mangle_ops __read_mostly;
 99 
100 static int __net_init iptable_mangle_net_init(struct net *net)
101 {
102         struct ipt_replace *repl;
103 
104         repl = ipt_alloc_initial_table(&packet_mangler);
105         if (repl == NULL)
106                 return -ENOMEM;
107         net->ipv4.iptable_mangle =
108                 ipt_register_table(net, &packet_mangler, repl);
109         kfree(repl);
110         return PTR_ERR_OR_ZERO(net->ipv4.iptable_mangle);
111 }
112 
113 static void __net_exit iptable_mangle_net_exit(struct net *net)
114 {
115         ipt_unregister_table(net, net->ipv4.iptable_mangle);
116 }
117 
118 static struct pernet_operations iptable_mangle_net_ops = {
119         .init = iptable_mangle_net_init,
120         .exit = iptable_mangle_net_exit,
121 };
122 
123 static int __init iptable_mangle_init(void)
124 {
125         int ret;
126 
127         ret = register_pernet_subsys(&iptable_mangle_net_ops);
128         if (ret < 0)
129                 return ret;
130 
131         /* Register hooks */
132         mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
133         if (IS_ERR(mangle_ops)) {
134                 ret = PTR_ERR(mangle_ops);
135                 unregister_pernet_subsys(&iptable_mangle_net_ops);
136         }
137 
138         return ret;
139 }
140 
141 static void __exit iptable_mangle_fini(void)
142 {
143         xt_hook_unlink(&packet_mangler, mangle_ops);
144         unregister_pernet_subsys(&iptable_mangle_net_ops);
145 }
146 
147 module_init(iptable_mangle_init);
148 module_exit(iptable_mangle_fini);
149 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp