Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 13 | |
| 14 | MODULE_LICENSE("GPL"); |
| 15 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 16 | MODULE_DESCRIPTION("ip6tables mangle table"); |
| 17 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 18 | #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \ |
| 19 | (1 << NF_INET_LOCAL_IN) | \ |
| 20 | (1 << NF_INET_FORWARD) | \ |
| 21 | (1 << NF_INET_LOCAL_OUT) | \ |
| 22 | (1 << NF_INET_POST_ROUTING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 24 | static const struct xt_table packet_mangler = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | .name = "mangle", |
| 26 | .valid_hooks = MANGLE_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 28 | .af = NFPROTO_IPV6, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 29 | .priority = NF_IP6_PRI_MANGLE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Alexey Dobriyan | 7dd1b8d | 2008-10-08 11:35:02 +0200 | [diff] [blame] | 32 | static unsigned int |
| 33 | ip6t_local_out_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 34 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | const struct net_device *out, |
| 36 | int (*okfn)(struct sk_buff *)) |
| 37 | { |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | unsigned int ret; |
| 40 | struct in6_addr saddr, daddr; |
| 41 | u_int8_t hop_limit; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 42 | u_int32_t flowlabel, mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #if 0 |
| 45 | /* root is playing with raw sockets. */ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 46 | if (skb->len < sizeof(struct iphdr) || |
| 47 | ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (net_ratelimit()) |
| 49 | printk("ip6t_hook: happy cracking.\n"); |
| 50 | return NF_ACCEPT; |
| 51 | } |
| 52 | #endif |
| 53 | |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 54 | /* save source/dest address, mark, hoplimit, flowlabel, priority, */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* flowlabel and prio (includes version, which shouldn't change either */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 61 | flowlabel = *((u_int32_t *)ipv6_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 63 | ret = ip6t_do_table(skb, hook, NULL, out, |
Alexey Dobriyan | 7dd1b8d | 2008-10-08 11:35:02 +0200 | [diff] [blame] | 64 | dev_net(out)->ipv6.ip6table_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 66 | if (ret != NF_DROP && ret != NF_STOLEN && |
| 67 | (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) || |
| 68 | memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) || |
| 69 | skb->mark != mark || |
| 70 | ipv6_hdr(skb)->hop_limit != hop_limit)) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 71 | return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 76 | /* The work comes in here from netfilter.c. */ |
| 77 | static unsigned int |
| 78 | ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb, |
| 79 | const struct net_device *in, const struct net_device *out, |
| 80 | int (*okfn)(struct sk_buff *)) |
| 81 | { |
| 82 | if (hook == NF_INET_LOCAL_OUT) |
| 83 | return ip6t_local_out_hook(hook, skb, out, okfn); |
Alexey Dobriyan | b2907e5 | 2010-02-11 18:41:35 +0100 | [diff] [blame] | 84 | if (hook == NF_INET_POST_ROUTING) |
| 85 | return ip6t_do_table(skb, hook, in, out, |
| 86 | dev_net(out)->ipv6.ip6table_mangle); |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 87 | /* INPUT/FORWARD */ |
| 88 | return ip6t_do_table(skb, hook, in, out, |
| 89 | dev_net(in)->ipv6.ip6table_mangle); |
| 90 | } |
| 91 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 92 | static struct nf_hook_ops *mangle_ops __read_mostly; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 93 | static int __net_init ip6table_mangle_net_init(struct net *net) |
| 94 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 95 | struct ip6t_replace *repl; |
| 96 | |
| 97 | repl = ip6t_alloc_initial_table(&packet_mangler); |
| 98 | if (repl == NULL) |
| 99 | return -ENOMEM; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 100 | net->ipv6.ip6table_mangle = |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 101 | ip6t_register_table(net, &packet_mangler, repl); |
| 102 | kfree(repl); |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 103 | if (IS_ERR(net->ipv6.ip6table_mangle)) |
| 104 | return PTR_ERR(net->ipv6.ip6table_mangle); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static void __net_exit ip6table_mangle_net_exit(struct net *net) |
| 109 | { |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 110 | ip6t_unregister_table(net, net->ipv6.ip6table_mangle); |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static struct pernet_operations ip6table_mangle_net_ops = { |
| 114 | .init = ip6table_mangle_net_init, |
| 115 | .exit = ip6table_mangle_net_exit, |
| 116 | }; |
| 117 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 118 | static int __init ip6table_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | int ret; |
| 121 | |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 122 | ret = register_pernet_subsys(&ip6table_mangle_net_ops); |
| 123 | if (ret < 0) |
| 124 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* Register hooks */ |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 127 | mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook); |
| 128 | if (IS_ERR(mangle_ops)) { |
| 129 | ret = PTR_ERR(mangle_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | goto cleanup_table; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return ret; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | cleanup_table: |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 136 | unregister_pernet_subsys(&ip6table_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return ret; |
| 138 | } |
| 139 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 140 | static void __exit ip6table_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 142 | xt_hook_unlink(&packet_mangler, mangle_ops); |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 143 | unregister_pernet_subsys(&ip6table_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 146 | module_init(ip6table_mangle_init); |
| 147 | module_exit(ip6table_mangle_fini); |