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

TOMOYO Linux Cross Reference
Linux/net/ipv6/netfilter/ip6table_mangle.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ 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  * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
  3  *
  4  * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
  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_ipv6/ip6_tables.h>
 13 #include <linux/slab.h>
 14 #include <net/ipv6.h>
 15 
 16 MODULE_LICENSE("GPL");
 17 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 18 MODULE_DESCRIPTION("ip6tables mangle table");
 19 
 20 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
 21                             (1 << NF_INET_LOCAL_IN) | \
 22                             (1 << NF_INET_FORWARD) | \
 23                             (1 << NF_INET_LOCAL_OUT) | \
 24                             (1 << NF_INET_POST_ROUTING))
 25 
 26 static const struct xt_table packet_mangler = {
 27         .name           = "mangle",
 28         .valid_hooks    = MANGLE_VALID_HOOKS,
 29         .me             = THIS_MODULE,
 30         .af             = NFPROTO_IPV6,
 31         .priority       = NF_IP6_PRI_MANGLE,
 32 };
 33 
 34 static unsigned int
 35 ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
 36 {
 37         unsigned int ret;
 38         struct in6_addr saddr, daddr;
 39         u_int8_t hop_limit;
 40         u_int32_t flowlabel, mark;
 41         int err;
 42 #if 0
 43         /* root is playing with raw sockets. */
 44         if (skb->len < sizeof(struct iphdr) ||
 45             ip_hdrlen(skb) < sizeof(struct iphdr)) {
 46                 net_warn_ratelimited("ip6t_hook: happy cracking\n");
 47                 return NF_ACCEPT;
 48         }
 49 #endif
 50 
 51         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
 52         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
 53         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
 54         mark = skb->mark;
 55         hop_limit = ipv6_hdr(skb)->hop_limit;
 56 
 57         /* flowlabel and prio (includes version, which shouldn't change either */
 58         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
 59 
 60         ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
 61                             dev_net(out)->ipv6.ip6table_mangle);
 62 
 63         if (ret != NF_DROP && ret != NF_STOLEN &&
 64             (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
 65              !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
 66              skb->mark != mark ||
 67              ipv6_hdr(skb)->hop_limit != hop_limit ||
 68              flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
 69                 err = ip6_route_me_harder(skb);
 70                 if (err < 0)
 71                         ret = NF_DROP_ERR(err);
 72         }
 73 
 74         return ret;
 75 }
 76 
 77 /* The work comes in here from netfilter.c. */
 78 static unsigned int
 79 ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
 80                      const struct net_device *in, const struct net_device *out,
 81                      int (*okfn)(struct sk_buff *))
 82 {
 83         if (hook == NF_INET_LOCAL_OUT)
 84                 return ip6t_mangle_out(skb, out);
 85         if (hook == NF_INET_POST_ROUTING)
 86                 return ip6t_do_table(skb, hook, in, out,
 87                                      dev_net(out)->ipv6.ip6table_mangle);
 88         /* INPUT/FORWARD */
 89         return ip6t_do_table(skb, hook, in, out,
 90                              dev_net(in)->ipv6.ip6table_mangle);
 91 }
 92 
 93 static struct nf_hook_ops *mangle_ops __read_mostly;
 94 static int __net_init ip6table_mangle_net_init(struct net *net)
 95 {
 96         struct ip6t_replace *repl;
 97 
 98         repl = ip6t_alloc_initial_table(&packet_mangler);
 99         if (repl == NULL)
100                 return -ENOMEM;
101         net->ipv6.ip6table_mangle =
102                 ip6t_register_table(net, &packet_mangler, repl);
103         kfree(repl);
104         return PTR_RET(net->ipv6.ip6table_mangle);
105 }
106 
107 static void __net_exit ip6table_mangle_net_exit(struct net *net)
108 {
109         ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
110 }
111 
112 static struct pernet_operations ip6table_mangle_net_ops = {
113         .init = ip6table_mangle_net_init,
114         .exit = ip6table_mangle_net_exit,
115 };
116 
117 static int __init ip6table_mangle_init(void)
118 {
119         int ret;
120 
121         ret = register_pernet_subsys(&ip6table_mangle_net_ops);
122         if (ret < 0)
123                 return ret;
124 
125         /* Register hooks */
126         mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
127         if (IS_ERR(mangle_ops)) {
128                 ret = PTR_ERR(mangle_ops);
129                 goto cleanup_table;
130         }
131 
132         return ret;
133 
134  cleanup_table:
135         unregister_pernet_subsys(&ip6table_mangle_net_ops);
136         return ret;
137 }
138 
139 static void __exit ip6table_mangle_fini(void)
140 {
141         xt_hook_unlink(&packet_mangler, mangle_ops);
142         unregister_pernet_subsys(&ip6table_mangle_net_ops);
143 }
144 
145 module_init(ip6table_mangle_init);
146 module_exit(ip6table_mangle_fini);
147 

~ [ 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