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

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

Version: ~ [ linux-6.4-rc3 ] ~ [ linux-6.3.4 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.30 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.113 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.180 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.243 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.283 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.315 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
  4  *
  5  * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
  6  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
  7  */
  8 #include <linux/module.h>
  9 #include <linux/netfilter_ipv6/ip6_tables.h>
 10 #include <linux/slab.h>
 11 #include <net/ipv6.h>
 12 
 13 MODULE_LICENSE("GPL");
 14 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 15 MODULE_DESCRIPTION("ip6tables mangle table");
 16 
 17 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
 18                             (1 << NF_INET_LOCAL_IN) | \
 19                             (1 << NF_INET_FORWARD) | \
 20                             (1 << NF_INET_LOCAL_OUT) | \
 21                             (1 << NF_INET_POST_ROUTING))
 22 
 23 static const struct xt_table packet_mangler = {
 24         .name           = "mangle",
 25         .valid_hooks    = MANGLE_VALID_HOOKS,
 26         .me             = THIS_MODULE,
 27         .af             = NFPROTO_IPV6,
 28         .priority       = NF_IP6_PRI_MANGLE,
 29 };
 30 
 31 static unsigned int
 32 ip6t_mangle_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
 33 {
 34         unsigned int ret;
 35         struct in6_addr saddr, daddr;
 36         u_int8_t hop_limit;
 37         u_int32_t flowlabel, mark;
 38         int err;
 39 
 40         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
 41         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
 42         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
 43         mark = skb->mark;
 44         hop_limit = ipv6_hdr(skb)->hop_limit;
 45 
 46         /* flowlabel and prio (includes version, which shouldn't change either */
 47         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
 48 
 49         ret = ip6t_do_table(priv, skb, state);
 50 
 51         if (ret != NF_DROP && ret != NF_STOLEN &&
 52             (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
 53              !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
 54              skb->mark != mark ||
 55              ipv6_hdr(skb)->hop_limit != hop_limit ||
 56              flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
 57                 err = ip6_route_me_harder(state->net, state->sk, skb);
 58                 if (err < 0)
 59                         ret = NF_DROP_ERR(err);
 60         }
 61 
 62         return ret;
 63 }
 64 
 65 /* The work comes in here from netfilter.c. */
 66 static unsigned int
 67 ip6table_mangle_hook(void *priv, struct sk_buff *skb,
 68                      const struct nf_hook_state *state)
 69 {
 70         if (state->hook == NF_INET_LOCAL_OUT)
 71                 return ip6t_mangle_out(priv, skb, state);
 72         return ip6t_do_table(priv, skb, state);
 73 }
 74 
 75 static struct nf_hook_ops *mangle_ops __read_mostly;
 76 static int ip6table_mangle_table_init(struct net *net)
 77 {
 78         struct ip6t_replace *repl;
 79         int ret;
 80 
 81         repl = ip6t_alloc_initial_table(&packet_mangler);
 82         if (repl == NULL)
 83                 return -ENOMEM;
 84         ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops);
 85         kfree(repl);
 86         return ret;
 87 }
 88 
 89 static void __net_exit ip6table_mangle_net_pre_exit(struct net *net)
 90 {
 91         ip6t_unregister_table_pre_exit(net, "mangle");
 92 }
 93 
 94 static void __net_exit ip6table_mangle_net_exit(struct net *net)
 95 {
 96         ip6t_unregister_table_exit(net, "mangle");
 97 }
 98 
 99 static struct pernet_operations ip6table_mangle_net_ops = {
100         .pre_exit = ip6table_mangle_net_pre_exit,
101         .exit = ip6table_mangle_net_exit,
102 };
103 
104 static int __init ip6table_mangle_init(void)
105 {
106         int ret = xt_register_template(&packet_mangler,
107                                        ip6table_mangle_table_init);
108 
109         if (ret < 0)
110                 return ret;
111 
112         mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
113         if (IS_ERR(mangle_ops)) {
114                 xt_unregister_template(&packet_mangler);
115                 return PTR_ERR(mangle_ops);
116         }
117 
118         ret = register_pernet_subsys(&ip6table_mangle_net_ops);
119         if (ret < 0) {
120                 xt_unregister_template(&packet_mangler);
121                 kfree(mangle_ops);
122                 return ret;
123         }
124 
125         return ret;
126 }
127 
128 static void __exit ip6table_mangle_fini(void)
129 {
130         unregister_pernet_subsys(&ip6table_mangle_net_ops);
131         xt_unregister_template(&packet_mangler);
132         kfree(mangle_ops);
133 }
134 
135 module_init(ip6table_mangle_init);
136 module_exit(ip6table_mangle_fini);
137 

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