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 15MODULE_LICENSE("GPL"); 16MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); 17MODULE_DESCRIPTION("ip6tables mangle table"); 18 19#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \ 20 (1 << NF_INET_LOCAL_IN) | \ 21 (1 << NF_INET_FORWARD) | \ 22 (1 << NF_INET_LOCAL_OUT) | \ 23 (1 << NF_INET_POST_ROUTING)) 24 25static const struct xt_table packet_mangler = { 26 .name = "mangle", 27 .valid_hooks = MANGLE_VALID_HOOKS, 28 .me = THIS_MODULE, 29 .af = NFPROTO_IPV6, 30 .priority = NF_IP6_PRI_MANGLE, 31}; 32 33static unsigned int 34ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out) 35{ 36 unsigned int ret; 37 struct in6_addr saddr, daddr; 38 u_int8_t hop_limit; 39 u_int32_t flowlabel, mark; 40 41 42 /* save source/dest address, mark, hoplimit, flowlabel, priority, */ 43 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr)); 44 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr)); 45 mark = skb->mark; 46 hop_limit = ipv6_hdr(skb)->hop_limit; 47 48 /* flowlabel and prio (includes version, which shouldn't change either */ 49 flowlabel = *((u_int32_t *)ipv6_hdr(skb)); 50 51 ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, NULL, out, 52 dev_net(out)->ipv6.ip6table_mangle); 53 54 if (ret != NF_DROP && ret != NF_STOLEN && 55 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) || 56 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) || 57 skb->mark != mark || 58 ipv6_hdr(skb)->hop_limit != hop_limit)) 59 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP; 60 61 return ret; 62} 63 64/* The work comes in here from netfilter.c. */ 65static unsigned int 66ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb, 67 const struct net_device *in, const struct net_device *out, 68 int (*okfn)(struct sk_buff *)) 69{ 70 if (hook == NF_INET_LOCAL_OUT) 71 return ip6t_mangle_out(skb, out); 72 if (hook == NF_INET_POST_ROUTING) 73 return ip6t_do_table(skb, hook, in, out, 74 dev_net(out)->ipv6.ip6table_mangle); 75 /* INPUT/FORWARD */ 76 return ip6t_do_table(skb, hook, in, out, 77 dev_net(in)->ipv6.ip6table_mangle); 78} 79 80static struct nf_hook_ops *mangle_ops __read_mostly; 81static int __net_init ip6table_mangle_net_init(struct net *net) 82{ 83 struct ip6t_replace *repl; 84 85 repl = ip6t_alloc_initial_table(&packet_mangler); 86 if (repl == NULL) 87 return -ENOMEM; 88 net->ipv6.ip6table_mangle = 89 ip6t_register_table(net, &packet_mangler, repl); 90 kfree(repl); 91 if (IS_ERR(net->ipv6.ip6table_mangle)) 92 return PTR_ERR(net->ipv6.ip6table_mangle); 93 return 0; 94} 95 96static void __net_exit ip6table_mangle_net_exit(struct net *net) 97{ 98 ip6t_unregister_table(net, net->ipv6.ip6table_mangle); 99} 100 101static struct pernet_operations ip6table_mangle_net_ops = { 102 .init = ip6table_mangle_net_init, 103 .exit = ip6table_mangle_net_exit, 104}; 105 106static int __init ip6table_mangle_init(void) 107{ 108 int ret; 109 110 ret = register_pernet_subsys(&ip6table_mangle_net_ops); 111 if (ret < 0) 112 return ret; 113 114 /* Register hooks */ 115 mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook); 116 if (IS_ERR(mangle_ops)) { 117 ret = PTR_ERR(mangle_ops); 118 goto cleanup_table; 119 } 120 121 return ret; 122 123 cleanup_table: 124 unregister_pernet_subsys(&ip6table_mangle_net_ops); 125 return ret; 126} 127 128static void __exit ip6table_mangle_fini(void) 129{ 130 xt_hook_unlink(&packet_mangler, mangle_ops); 131 unregister_pernet_subsys(&ip6table_mangle_net_ops); 132} 133 134module_init(ip6table_mangle_init); 135module_exit(ip6table_mangle_fini); 136