Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2004 USAGI/WIDE Project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Author: |
| 9 | * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <linux/ipv6.h> |
| 14 | #include <linux/in6.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/icmp.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 19 | #include <net/ipv6.h> |
Pavel Emelyanov | 04128f2 | 2007-10-15 02:33:45 -0700 | [diff] [blame] | 20 | #include <net/inet_frag.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 21 | |
Patrick McHardy | 8fa9ff6 | 2009-12-15 16:59:59 +0100 | [diff] [blame] | 22 | #include <linux/netfilter_bridge.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 23 | #include <linux/netfilter_ipv6.h> |
| 24 | #include <net/netfilter/nf_conntrack.h> |
| 25 | #include <net/netfilter/nf_conntrack_helper.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 27 | #include <net/netfilter/nf_conntrack_l3proto.h> |
| 28 | #include <net/netfilter/nf_conntrack_core.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_zones.h> |
Christoph Paasch | 9d2493f | 2009-03-16 15:15:35 +0100 | [diff] [blame] | 30 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 31 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 32 | #include <net/netfilter/nf_log.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 33 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 34 | static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff, |
| 35 | struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 36 | { |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 37 | const u_int32_t *ap; |
| 38 | u_int32_t _addrs[8]; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 39 | |
| 40 | ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr), |
| 41 | sizeof(_addrs), _addrs); |
| 42 | if (ap == NULL) |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 43 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 44 | |
| 45 | memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6)); |
| 46 | memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6)); |
| 47 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 48 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 51 | static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 52 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 53 | { |
| 54 | memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6)); |
| 55 | memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6)); |
| 56 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 57 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int ipv6_print_tuple(struct seq_file *s, |
| 61 | const struct nf_conntrack_tuple *tuple) |
| 62 | { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 63 | return seq_printf(s, "src=%pI6 dst=%pI6 ", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 64 | tuple->src.u3.ip6, tuple->dst.u3.ip6); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 67 | /* |
| 68 | * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c |
| 69 | * |
| 70 | * This function parses (probably truncated) exthdr set "hdr" |
| 71 | * of length "len". "nexthdrp" initially points to some place, |
| 72 | * where type of the first header can be found. |
| 73 | * |
| 74 | * It skips all well-known exthdrs, and returns pointer to the start |
| 75 | * of unparsable area i.e. the first header with unknown type. |
| 76 | * if success, *nexthdr is updated by type/protocol of this header. |
| 77 | * |
| 78 | * NOTES: - it may return pointer pointing beyond end of packet, |
| 79 | * if the last recognized header is truncated in the middle. |
| 80 | * - if packet is truncated, so that all parsed headers are skipped, |
| 81 | * it returns -1. |
| 82 | * - if packet is fragmented, return pointer of the fragment header. |
| 83 | * - ESP is unparsable for now and considered like |
| 84 | * normal payload protocol. |
| 85 | * - Note also special handling of AUTH header. Thanks to IPsec wizards. |
| 86 | */ |
| 87 | |
Adrian Bunk | 1a3a206 | 2007-07-30 18:04:57 -0700 | [diff] [blame] | 88 | static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start, |
| 89 | u8 *nexthdrp, int len) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 90 | { |
| 91 | u8 nexthdr = *nexthdrp; |
| 92 | |
| 93 | while (ipv6_ext_hdr(nexthdr)) { |
| 94 | struct ipv6_opt_hdr hdr; |
| 95 | int hdrlen; |
| 96 | |
| 97 | if (len < (int)sizeof(struct ipv6_opt_hdr)) |
| 98 | return -1; |
| 99 | if (nexthdr == NEXTHDR_NONE) |
| 100 | break; |
| 101 | if (nexthdr == NEXTHDR_FRAGMENT) |
| 102 | break; |
| 103 | if (skb_copy_bits(skb, start, &hdr, sizeof(hdr))) |
| 104 | BUG(); |
| 105 | if (nexthdr == NEXTHDR_AUTH) |
| 106 | hdrlen = (hdr.hdrlen+2)<<2; |
| 107 | else |
| 108 | hdrlen = ipv6_optlen(&hdr); |
| 109 | |
| 110 | nexthdr = hdr.nexthdr; |
| 111 | len -= hdrlen; |
| 112 | start += hdrlen; |
| 113 | } |
| 114 | |
| 115 | *nexthdrp = nexthdr; |
| 116 | return start; |
| 117 | } |
| 118 | |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 119 | static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
| 120 | unsigned int *dataoff, u_int8_t *protonum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 121 | { |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 122 | unsigned int extoff = nhoff + sizeof(struct ipv6hdr); |
| 123 | unsigned char pnum; |
| 124 | int protoff; |
| 125 | |
| 126 | if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr), |
| 127 | &pnum, sizeof(pnum)) != 0) { |
| 128 | pr_debug("ip6_conntrack_core: can't get nexthdr\n"); |
| 129 | return -NF_ACCEPT; |
| 130 | } |
| 131 | protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 132 | /* |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 133 | * (protoff == skb->len) mean that the packet doesn't have no data |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 134 | * except of IPv6 & ext headers. but it's tracked anyway. - YK |
| 135 | */ |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 136 | if ((protoff < 0) || (protoff > skb->len)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 137 | pr_debug("ip6_conntrack_core: can't find proto in pkt\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | return -NF_ACCEPT; |
| 139 | } |
| 140 | |
| 141 | *dataoff = protoff; |
| 142 | *protonum = pnum; |
| 143 | return NF_ACCEPT; |
| 144 | } |
| 145 | |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 146 | static unsigned int ipv6_helper(unsigned int hooknum, |
| 147 | struct sk_buff *skb, |
| 148 | const struct net_device *in, |
| 149 | const struct net_device *out, |
| 150 | int (*okfn)(struct sk_buff *)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 151 | { |
| 152 | struct nf_conn *ct; |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 153 | const struct nf_conn_help *help; |
| 154 | const struct nf_conntrack_helper *helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | enum ip_conntrack_info ctinfo; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 156 | unsigned int ret, protoff; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 157 | unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data; |
| 158 | unsigned char pnum = ipv6_hdr(skb)->nexthdr; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 159 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 160 | |
| 161 | /* This is where we call the helper: as the packet goes out. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 162 | ct = nf_ct_get(skb, &ctinfo); |
Eric Dumazet | fb04883 | 2011-05-19 15:44:27 +0200 | [diff] [blame] | 163 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 164 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 165 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 166 | help = nfct_help(ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 167 | if (!help) |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 168 | return NF_ACCEPT; |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 169 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 170 | helper = rcu_dereference(help->helper); |
| 171 | if (!helper) |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 172 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 173 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 174 | protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, |
| 175 | skb->len - extoff); |
| 176 | if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 177 | pr_debug("proto header not found\n"); |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 178 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 181 | ret = helper->help(skb, protoff, ct, ctinfo); |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 182 | if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) { |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 183 | nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL, |
| 184 | "nf_ct_%s: dropping packet", helper->name); |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 185 | } |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static unsigned int ipv6_confirm(unsigned int hooknum, |
| 190 | struct sk_buff *skb, |
| 191 | const struct net_device *in, |
| 192 | const struct net_device *out, |
| 193 | int (*okfn)(struct sk_buff *)) |
| 194 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 195 | /* We've seen it coming out the other side: confirm it */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 196 | return nf_conntrack_confirm(skb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 199 | static unsigned int __ipv6_conntrack_in(struct net *net, |
| 200 | unsigned int hooknum, |
| 201 | struct sk_buff *skb, |
| 202 | int (*okfn)(struct sk_buff *)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 203 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 204 | struct sk_buff *reasm = skb->nfct_reasm; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 205 | |
| 206 | /* This packet is fragmented and has reassembled packet. */ |
| 207 | if (reasm) { |
| 208 | /* Reassembled packet isn't parsed yet ? */ |
| 209 | if (!reasm->nfct) { |
| 210 | unsigned int ret; |
| 211 | |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 212 | ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 213 | if (ret != NF_ACCEPT) |
| 214 | return ret; |
| 215 | } |
| 216 | nf_conntrack_get(reasm->nfct); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 217 | skb->nfct = reasm->nfct; |
| 218 | skb->nfctinfo = reasm->nfctinfo; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 219 | return NF_ACCEPT; |
| 220 | } |
| 221 | |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 222 | return nf_conntrack_in(net, PF_INET6, hooknum, skb); |
| 223 | } |
| 224 | |
| 225 | static unsigned int ipv6_conntrack_in(unsigned int hooknum, |
| 226 | struct sk_buff *skb, |
| 227 | const struct net_device *in, |
| 228 | const struct net_device *out, |
| 229 | int (*okfn)(struct sk_buff *)) |
| 230 | { |
| 231 | return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static unsigned int ipv6_conntrack_local(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 235 | struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 236 | const struct net_device *in, |
| 237 | const struct net_device *out, |
| 238 | int (*okfn)(struct sk_buff *)) |
| 239 | { |
| 240 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 241 | if (skb->len < sizeof(struct ipv6hdr)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 242 | net_notice_ratelimited("ipv6_conntrack_local: packet too short\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 243 | return NF_ACCEPT; |
| 244 | } |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 245 | return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 248 | static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 249 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 250 | .hook = ipv6_conntrack_in, |
| 251 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 252 | .pf = NFPROTO_IPV6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 253 | .hooknum = NF_INET_PRE_ROUTING, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 254 | .priority = NF_IP6_PRI_CONNTRACK, |
| 255 | }, |
| 256 | { |
| 257 | .hook = ipv6_conntrack_local, |
| 258 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 259 | .pf = NFPROTO_IPV6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 260 | .hooknum = NF_INET_LOCAL_OUT, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 261 | .priority = NF_IP6_PRI_CONNTRACK, |
| 262 | }, |
| 263 | { |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 264 | .hook = ipv6_helper, |
| 265 | .owner = THIS_MODULE, |
| 266 | .pf = NFPROTO_IPV6, |
| 267 | .hooknum = NF_INET_POST_ROUTING, |
| 268 | .priority = NF_IP6_PRI_CONNTRACK_HELPER, |
| 269 | }, |
| 270 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 271 | .hook = ipv6_confirm, |
| 272 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 273 | .pf = NFPROTO_IPV6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 274 | .hooknum = NF_INET_POST_ROUTING, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 275 | .priority = NF_IP6_PRI_LAST, |
| 276 | }, |
| 277 | { |
Pablo Neira Ayuso | 12f7a50 | 2012-05-13 21:44:54 +0200 | [diff] [blame^] | 278 | .hook = ipv6_helper, |
| 279 | .owner = THIS_MODULE, |
| 280 | .pf = NFPROTO_IPV6, |
| 281 | .hooknum = NF_INET_LOCAL_IN, |
| 282 | .priority = NF_IP6_PRI_CONNTRACK_HELPER, |
| 283 | }, |
| 284 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 285 | .hook = ipv6_confirm, |
| 286 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 287 | .pf = NFPROTO_IPV6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 288 | .hooknum = NF_INET_LOCAL_IN, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 289 | .priority = NF_IP6_PRI_LAST-1, |
| 290 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 291 | }; |
| 292 | |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 293 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 294 | |
| 295 | #include <linux/netfilter/nfnetlink.h> |
| 296 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 297 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 298 | static int ipv6_tuple_to_nlattr(struct sk_buff *skb, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 299 | const struct nf_conntrack_tuple *tuple) |
| 300 | { |
David S. Miller | e549a6b | 2012-04-01 20:28:52 -0400 | [diff] [blame] | 301 | if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4, |
| 302 | &tuple->src.u3.ip6) || |
| 303 | nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4, |
| 304 | &tuple->dst.u3.ip6)) |
| 305 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 306 | return 0; |
| 307 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 308 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
Patrick McHardy | f73e924cd | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 312 | static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = { |
| 313 | [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 }, |
| 314 | [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 317 | static int ipv6_nlattr_to_tuple(struct nlattr *tb[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 318 | struct nf_conntrack_tuple *t) |
| 319 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 320 | if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 323 | memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]), |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 324 | sizeof(u_int32_t) * 4); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 325 | memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]), |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 326 | sizeof(u_int32_t) * 4); |
| 327 | |
| 328 | return 0; |
| 329 | } |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 330 | |
| 331 | static int ipv6_nlattr_tuple_size(void) |
| 332 | { |
| 333 | return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1); |
| 334 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 335 | #endif |
| 336 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 337 | struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 338 | .l3proto = PF_INET6, |
| 339 | .name = "ipv6", |
| 340 | .pkt_to_tuple = ipv6_pkt_to_tuple, |
| 341 | .invert_tuple = ipv6_invert_tuple, |
| 342 | .print_tuple = ipv6_print_tuple, |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 343 | .get_l4proto = ipv6_get_l4proto, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 344 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 345 | .tuple_to_nlattr = ipv6_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 346 | .nlattr_tuple_size = ipv6_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 347 | .nlattr_to_tuple = ipv6_nlattr_to_tuple, |
Patrick McHardy | f73e924cd | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 348 | .nla_policy = ipv6_nla_policy, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 349 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 350 | .me = THIS_MODULE, |
| 351 | }; |
| 352 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 353 | MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6)); |
| 354 | MODULE_LICENSE("GPL"); |
| 355 | MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>"); |
| 356 | |
Gao feng | a7c439d | 2012-05-28 21:04:17 +0000 | [diff] [blame] | 357 | static int ipv6_net_init(struct net *net) |
| 358 | { |
| 359 | int ret = 0; |
| 360 | |
| 361 | ret = nf_conntrack_l4proto_register(net, |
| 362 | &nf_conntrack_l4proto_tcp6); |
| 363 | if (ret < 0) { |
| 364 | printk(KERN_ERR "nf_conntrack_l4proto_tcp6: protocol register failed\n"); |
| 365 | goto out; |
| 366 | } |
| 367 | ret = nf_conntrack_l4proto_register(net, |
| 368 | &nf_conntrack_l4proto_udp6); |
| 369 | if (ret < 0) { |
| 370 | printk(KERN_ERR "nf_conntrack_l4proto_udp6: protocol register failed\n"); |
| 371 | goto cleanup_tcp6; |
| 372 | } |
| 373 | ret = nf_conntrack_l4proto_register(net, |
| 374 | &nf_conntrack_l4proto_icmpv6); |
| 375 | if (ret < 0) { |
| 376 | printk(KERN_ERR "nf_conntrack_l4proto_icmp6: protocol register failed\n"); |
| 377 | goto cleanup_udp6; |
| 378 | } |
| 379 | ret = nf_conntrack_l3proto_register(net, |
| 380 | &nf_conntrack_l3proto_ipv6); |
| 381 | if (ret < 0) { |
| 382 | printk(KERN_ERR "nf_conntrack_l3proto_ipv6: protocol register failed\n"); |
| 383 | goto cleanup_icmpv6; |
| 384 | } |
| 385 | return 0; |
| 386 | cleanup_icmpv6: |
| 387 | nf_conntrack_l4proto_unregister(net, |
| 388 | &nf_conntrack_l4proto_icmpv6); |
| 389 | cleanup_udp6: |
| 390 | nf_conntrack_l4proto_unregister(net, |
| 391 | &nf_conntrack_l4proto_udp6); |
| 392 | cleanup_tcp6: |
| 393 | nf_conntrack_l4proto_unregister(net, |
| 394 | &nf_conntrack_l4proto_tcp6); |
| 395 | out: |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static void ipv6_net_exit(struct net *net) |
| 400 | { |
| 401 | nf_conntrack_l3proto_unregister(net, |
| 402 | &nf_conntrack_l3proto_ipv6); |
| 403 | nf_conntrack_l4proto_unregister(net, |
| 404 | &nf_conntrack_l4proto_icmpv6); |
| 405 | nf_conntrack_l4proto_unregister(net, |
| 406 | &nf_conntrack_l4proto_udp6); |
| 407 | nf_conntrack_l4proto_unregister(net, |
| 408 | &nf_conntrack_l4proto_tcp6); |
| 409 | } |
| 410 | |
| 411 | static struct pernet_operations ipv6_net_ops = { |
| 412 | .init = ipv6_net_init, |
| 413 | .exit = ipv6_net_exit, |
| 414 | }; |
| 415 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 416 | static int __init nf_conntrack_l3proto_ipv6_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 417 | { |
| 418 | int ret = 0; |
| 419 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 420 | need_conntrack(); |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 421 | nf_defrag_ipv6_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 422 | |
Gao feng | a7c439d | 2012-05-28 21:04:17 +0000 | [diff] [blame] | 423 | ret = register_pernet_subsys(&ipv6_net_ops); |
| 424 | if (ret < 0) |
| 425 | goto cleanup_pernet; |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 426 | ret = nf_register_hooks(ipv6_conntrack_ops, |
| 427 | ARRAY_SIZE(ipv6_conntrack_ops)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 428 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 429 | pr_err("nf_conntrack_ipv6: can't register pre-routing defrag " |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 430 | "hook.\n"); |
| 431 | goto cleanup_ipv6; |
| 432 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 433 | return ret; |
| 434 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 435 | cleanup_ipv6: |
Gao feng | a7c439d | 2012-05-28 21:04:17 +0000 | [diff] [blame] | 436 | unregister_pernet_subsys(&ipv6_net_ops); |
| 437 | cleanup_pernet: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 438 | return ret; |
| 439 | } |
| 440 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 441 | static void __exit nf_conntrack_l3proto_ipv6_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 442 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 443 | synchronize_net(); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 444 | nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops)); |
Gao feng | a7c439d | 2012-05-28 21:04:17 +0000 | [diff] [blame] | 445 | unregister_pernet_subsys(&ipv6_net_ops); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 448 | module_init(nf_conntrack_l3proto_ipv6_init); |
| 449 | module_exit(nf_conntrack_l3proto_ipv6_fini); |