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 46 /* save source/dest address, mark, hoplimit, flowlabel, priority, */ 47 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr)); 48 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr)); 49 mark = skb->mark; 50 hop_limit = ipv6_hdr(skb)->hop_limit; 51 52 /* flowlabel and prio (includes version, which shouldn't change either */ 53 flowlabel = *((u_int32_t *)ipv6_hdr(skb)); 54 55 ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle); 56 57 if (ret != NF_DROP && ret != NF_STOLEN && 58 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) || 59 !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) || 60 skb->mark != mark || 61 ipv6_hdr(skb)->hop_limit != hop_limit || 62 flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) { 63 err = ip6_route_me_harder(state->net, skb); 64 if (err < 0) 65 ret = NF_DROP_ERR(err); 66 } 67 68 return ret; 69 } 70 71 /* The work comes in here from netfilter.c. */ 72 static unsigned int 73 ip6table_mangle_hook(void *priv, struct sk_buff *skb, 74 const struct nf_hook_state *state) 75 { 76 if (state->hook == NF_INET_LOCAL_OUT) 77 return ip6t_mangle_out(skb, state); 78 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle); 79 } 80 81 static struct nf_hook_ops *mangle_ops __read_mostly; 82 static int __net_init ip6table_mangle_table_init(struct net *net) 83 { 84 struct ip6t_replace *repl; 85 int ret; 86 87 if (net->ipv6.ip6table_mangle) 88 return 0; 89 90 repl = ip6t_alloc_initial_table(&packet_mangler); 91 if (repl == NULL) 92 return -ENOMEM; 93 ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops, 94 &net->ipv6.ip6table_mangle); 95 kfree(repl); 96 return ret; 97 } 98 99 static void __net_exit ip6table_mangle_net_exit(struct net *net) 100 { 101 if (!net->ipv6.ip6table_mangle) 102 return; 103 104 ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops); 105 net->ipv6.ip6table_mangle = NULL; 106 } 107 108 static struct pernet_operations ip6table_mangle_net_ops = { 109 .exit = ip6table_mangle_net_exit, 110 }; 111 112 static int __init ip6table_mangle_init(void) 113 { 114 int ret; 115 116 mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook); 117 if (IS_ERR(mangle_ops)) 118 return PTR_ERR(mangle_ops); 119 120 ret = register_pernet_subsys(&ip6table_mangle_net_ops); 121 if (ret < 0) { 122 kfree(mangle_ops); 123 return ret; 124 } 125 126 ret = ip6table_mangle_table_init(&init_net); 127 if (ret) { 128 unregister_pernet_subsys(&ip6table_mangle_net_ops); 129 kfree(mangle_ops); 130 } 131 return ret; 132 } 133 134 static void __exit ip6table_mangle_fini(void) 135 { 136 unregister_pernet_subsys(&ip6table_mangle_net_ops); 137 kfree(mangle_ops); 138 } 139 140 module_init(ip6table_mangle_init); 141 module_exit(ip6table_mangle_fini); 142