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

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

Version: ~ [ linux-6.3-rc3 ] ~ [ linux-6.2.7 ] ~ [ linux-6.1.20 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.103 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.175 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.237 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.278 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.310 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ 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.9 ] ~ [ 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 int __net_init ip6table_mangle_table_init(struct net *net);
 27 
 28 static const struct xt_table packet_mangler = {
 29         .name           = "mangle",
 30         .valid_hooks    = MANGLE_VALID_HOOKS,
 31         .me             = THIS_MODULE,
 32         .af             = NFPROTO_IPV6,
 33         .priority       = NF_IP6_PRI_MANGLE,
 34         .table_init     = ip6table_mangle_table_init,
 35 };
 36 
 37 static unsigned int
 38 ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
 39 {
 40         unsigned int ret;
 41         struct in6_addr saddr, daddr;
 42         u_int8_t hop_limit;
 43         u_int32_t flowlabel, mark;
 44         int err;
 45 #if 0
 46         /* root is playing with raw sockets. */
 47         if (skb->len < sizeof(struct iphdr) ||
 48             ip_hdrlen(skb) < sizeof(struct iphdr)) {
 49                 net_warn_ratelimited("ip6t_hook: happy cracking\n");
 50                 return NF_ACCEPT;
 51         }
 52 #endif
 53 
 54         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
 55         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
 56         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
 57         mark = skb->mark;
 58         hop_limit = ipv6_hdr(skb)->hop_limit;
 59 
 60         /* flowlabel and prio (includes version, which shouldn't change either */
 61         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
 62 
 63         ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
 64 
 65         if (ret != NF_DROP && ret != NF_STOLEN &&
 66             (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
 67              !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
 68              skb->mark != mark ||
 69              ipv6_hdr(skb)->hop_limit != hop_limit ||
 70              flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
 71                 err = ip6_route_me_harder(state->net, skb);
 72                 if (err < 0)
 73                         ret = NF_DROP_ERR(err);
 74         }
 75 
 76         return ret;
 77 }
 78 
 79 /* The work comes in here from netfilter.c. */
 80 static unsigned int
 81 ip6table_mangle_hook(void *priv, struct sk_buff *skb,
 82                      const struct nf_hook_state *state)
 83 {
 84         if (state->hook == NF_INET_LOCAL_OUT)
 85                 return ip6t_mangle_out(skb, state);
 86         if (state->hook == NF_INET_POST_ROUTING)
 87                 return ip6t_do_table(skb, state,
 88                                      state->net->ipv6.ip6table_mangle);
 89         /* INPUT/FORWARD */
 90         return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
 91 }
 92 
 93 static struct nf_hook_ops *mangle_ops __read_mostly;
 94 static int __net_init ip6table_mangle_table_init(struct net *net)
 95 {
 96         struct ip6t_replace *repl;
 97         int ret;
 98 
 99         if (net->ipv6.ip6table_mangle)
100                 return 0;
101 
102         repl = ip6t_alloc_initial_table(&packet_mangler);
103         if (repl == NULL)
104                 return -ENOMEM;
105         ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops,
106                                   &net->ipv6.ip6table_mangle);
107         kfree(repl);
108         return ret;
109 }
110 
111 static void __net_exit ip6table_mangle_net_exit(struct net *net)
112 {
113         if (!net->ipv6.ip6table_mangle)
114                 return;
115 
116         ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops);
117         net->ipv6.ip6table_mangle = NULL;
118 }
119 
120 static struct pernet_operations ip6table_mangle_net_ops = {
121         .exit = ip6table_mangle_net_exit,
122 };
123 
124 static int __init ip6table_mangle_init(void)
125 {
126         int ret;
127 
128         mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
129         if (IS_ERR(mangle_ops))
130                 return PTR_ERR(mangle_ops);
131 
132         ret = register_pernet_subsys(&ip6table_mangle_net_ops);
133         if (ret < 0) {
134                 kfree(mangle_ops);
135                 return ret;
136         }
137 
138         ret = ip6table_mangle_table_init(&init_net);
139         if (ret) {
140                 unregister_pernet_subsys(&ip6table_mangle_net_ops);
141                 kfree(mangle_ops);
142         }
143         return ret;
144 }
145 
146 static void __exit ip6table_mangle_fini(void)
147 {
148         unregister_pernet_subsys(&ip6table_mangle_net_ops);
149         kfree(mangle_ops);
150 }
151 
152 module_init(ip6table_mangle_init);
153 module_exit(ip6table_mangle_fini);
154 

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