Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 fragment reassembly for connection tracking |
| 3 | * |
| 4 | * Copyright (C)2004 USAGI/WIDE Project |
| 5 | * |
| 6 | * Author: |
| 7 | * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 8 | * |
| 9 | * Based on: net/ipv6/reassembly.c |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | */ |
| 16 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/socket.h> |
| 21 | #include <linux/sockios.h> |
| 22 | #include <linux/jiffies.h> |
| 23 | #include <linux/net.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/in6.h> |
| 27 | #include <linux/ipv6.h> |
| 28 | #include <linux/icmpv6.h> |
| 29 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 31 | |
| 32 | #include <net/sock.h> |
| 33 | #include <net/snmp.h> |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 34 | #include <net/inet_frag.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 35 | |
| 36 | #include <net/ipv6.h> |
| 37 | #include <net/protocol.h> |
| 38 | #include <net/transp_v6.h> |
| 39 | #include <net/rawv6.h> |
| 40 | #include <net/ndisc.h> |
| 41 | #include <net/addrconf.h> |
Patrick McHardy | 99fa5f5 | 2008-01-31 04:10:40 -0800 | [diff] [blame] | 42 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 43 | #include <linux/sysctl.h> |
| 44 | #include <linux/netfilter.h> |
| 45 | #include <linux/netfilter_ipv6.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/module.h> |
| 48 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 49 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 50 | struct nf_ct_frag6_skb_cb |
| 51 | { |
| 52 | struct inet6_skb_parm h; |
| 53 | int offset; |
| 54 | struct sk_buff *orig; |
| 55 | }; |
| 56 | |
| 57 | #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb)) |
| 58 | |
| 59 | struct nf_ct_frag6_queue |
| 60 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 61 | struct inet_frag_queue q; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 62 | |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 63 | __be32 id; /* fragment id */ |
Shan Wei | c92b544 | 2010-01-26 02:40:38 +0000 | [diff] [blame] | 64 | u32 user; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 65 | struct in6_addr saddr; |
| 66 | struct in6_addr daddr; |
| 67 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 68 | unsigned int csum; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 69 | __u16 nhoffset; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 72 | static struct inet_frags nf_frags; |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 73 | static struct netns_frags nf_init_frags; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 74 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 75 | #ifdef CONFIG_SYSCTL |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 76 | struct ctl_table nf_ct_frag6_sysctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 77 | { |
| 78 | .procname = "nf_conntrack_frag6_timeout", |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 79 | .data = &nf_init_frags.timeout, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 80 | .maxlen = sizeof(unsigned int), |
| 81 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 82 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 83 | }, |
| 84 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 85 | .procname = "nf_conntrack_frag6_low_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 86 | .data = &nf_init_frags.low_thresh, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 87 | .maxlen = sizeof(unsigned int), |
| 88 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 89 | .proc_handler = proc_dointvec, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 90 | }, |
| 91 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 92 | .procname = "nf_conntrack_frag6_high_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 93 | .data = &nf_init_frags.high_thresh, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 94 | .maxlen = sizeof(unsigned int), |
| 95 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 96 | .proc_handler = proc_dointvec, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 97 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 98 | { } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 99 | }; |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 100 | |
| 101 | static struct ctl_table_header *nf_ct_frag6_sysctl_header; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 102 | #endif |
| 103 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 104 | static unsigned int nf_hashfn(struct inet_frag_queue *q) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | { |
Jan Engelhardt | 58c0fb0 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 106 | const struct nf_ct_frag6_queue *nq; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 107 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 108 | nq = container_of(q, struct nf_ct_frag6_queue, q); |
Ilpo Järvinen | 93c8b90 | 2008-10-01 02:48:31 -0700 | [diff] [blame] | 109 | return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 112 | static void nf_skb_free(struct sk_buff *skb) |
| 113 | { |
| 114 | if (NFCT_FRAG6_CB(skb)->orig) |
| 115 | kfree_skb(NFCT_FRAG6_CB(skb)->orig); |
| 116 | } |
| 117 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 118 | /* Destruction primitives. */ |
| 119 | |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 120 | static __inline__ void fq_put(struct nf_ct_frag6_queue *fq) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 121 | { |
Pavel Emelyanov | 762cc40 | 2007-10-15 02:41:56 -0700 | [diff] [blame] | 122 | inet_frag_put(&fq->q, &nf_frags); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Kill fq entry. It is not destroyed immediately, |
| 126 | * because caller (and someone more) holds reference count. |
| 127 | */ |
| 128 | static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq) |
| 129 | { |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 130 | inet_frag_kill(&fq->q, &nf_frags); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void nf_ct_frag6_evictor(void) |
| 134 | { |
David S. Miller | e8e16b70 | 2008-03-28 17:30:18 -0700 | [diff] [blame] | 135 | local_bh_disable(); |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 136 | inet_frag_evictor(&nf_init_frags, &nf_frags); |
David S. Miller | e8e16b70 | 2008-03-28 17:30:18 -0700 | [diff] [blame] | 137 | local_bh_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void nf_ct_frag6_expire(unsigned long data) |
| 141 | { |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 142 | struct nf_ct_frag6_queue *fq; |
| 143 | |
| 144 | fq = container_of((struct inet_frag_queue *)data, |
| 145 | struct nf_ct_frag6_queue, q); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 146 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 147 | spin_lock(&fq->q.lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 148 | |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 149 | if (fq->q.last_in & INET_FRAG_COMPLETE) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 150 | goto out; |
| 151 | |
| 152 | fq_kill(fq); |
| 153 | |
| 154 | out: |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 155 | spin_unlock(&fq->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 156 | fq_put(fq); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Creation primitives. */ |
| 160 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 161 | static __inline__ struct nf_ct_frag6_queue * |
Patrick McHardy | 0b5ccb2 | 2009-12-15 16:59:18 +0100 | [diff] [blame] | 162 | fq_find(__be32 id, u32 user, struct in6_addr *src, struct in6_addr *dst) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 163 | { |
Pavel Emelyanov | 2588fe1 | 2007-10-17 19:44:34 -0700 | [diff] [blame] | 164 | struct inet_frag_queue *q; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 165 | struct ip6_create_arg arg; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 166 | unsigned int hash; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 167 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 168 | arg.id = id; |
Patrick McHardy | 0b5ccb2 | 2009-12-15 16:59:18 +0100 | [diff] [blame] | 169 | arg.user = user; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 170 | arg.src = src; |
| 171 | arg.dst = dst; |
Pavel Emelyanov | 9a37580 | 2008-06-27 20:06:08 -0700 | [diff] [blame] | 172 | |
| 173 | read_lock_bh(&nf_frags.lock); |
Ilpo Järvinen | 93c8b90 | 2008-10-01 02:48:31 -0700 | [diff] [blame] | 174 | hash = inet6_hash_frag(id, src, dst, nf_frags.rnd); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 175 | |
Pavel Emelyanov | ac18e75 | 2008-01-22 06:02:14 -0800 | [diff] [blame] | 176 | q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash); |
Jarek Poplawski | b9c6989 | 2008-06-04 09:58:27 -0700 | [diff] [blame] | 177 | local_bh_enable(); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 178 | if (q == NULL) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 179 | goto oom; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 180 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 181 | return container_of(q, struct nf_ct_frag6_queue, q); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 182 | |
| 183 | oom: |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 184 | pr_debug("Can't alloc new queue\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
| 187 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 188 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 189 | static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb, |
Jan Engelhardt | 58c0fb0 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 190 | const struct frag_hdr *fhdr, int nhoff) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 191 | { |
| 192 | struct sk_buff *prev, *next; |
| 193 | int offset, end; |
| 194 | |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 195 | if (fq->q.last_in & INET_FRAG_COMPLETE) { |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 196 | pr_debug("Already completed\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 197 | goto err; |
| 198 | } |
| 199 | |
| 200 | offset = ntohs(fhdr->frag_off) & ~0x7; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 201 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
| 202 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 203 | |
| 204 | if ((unsigned int)end > IPV6_MAXPLEN) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 205 | pr_debug("offset is too large.\n"); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 206 | return -1; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 209 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 210 | const unsigned char *nh = skb_network_header(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 211 | skb->csum = csum_sub(skb->csum, |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 212 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 213 | 0)); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 214 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 215 | |
| 216 | /* Is this the final fragment? */ |
| 217 | if (!(fhdr->frag_off & htons(IP6_MF))) { |
| 218 | /* If we already have some bits beyond end |
| 219 | * or have different end, the segment is corrupted. |
| 220 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 221 | if (end < fq->q.len || |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 222 | ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 223 | pr_debug("already received last fragment\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 224 | goto err; |
| 225 | } |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 226 | fq->q.last_in |= INET_FRAG_LAST_IN; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 227 | fq->q.len = end; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 228 | } else { |
| 229 | /* Check if the fragment is rounded to 8 bytes. |
| 230 | * Required by the RFC. |
| 231 | */ |
| 232 | if (end & 0x7) { |
| 233 | /* RFC2460 says always send parameter problem in |
| 234 | * this case. -DaveM |
| 235 | */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 236 | pr_debug("end of fragment not rounded to 8 bytes.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 237 | return -1; |
| 238 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 239 | if (end > fq->q.len) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 240 | /* Some bits beyond end -> corruption. */ |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 241 | if (fq->q.last_in & INET_FRAG_LAST_IN) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 242 | pr_debug("last packet already reached.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 243 | goto err; |
| 244 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 245 | fq->q.len = end; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | if (end == offset) |
| 250 | goto err; |
| 251 | |
| 252 | /* Point into the IP datagram 'data' part. */ |
| 253 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 254 | pr_debug("queue: message is too short.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 255 | goto err; |
| 256 | } |
Herbert Xu | b38dfee | 2006-06-09 16:13:01 -0700 | [diff] [blame] | 257 | if (pskb_trim_rcsum(skb, end - offset)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 258 | pr_debug("Can't trim\n"); |
Herbert Xu | b38dfee | 2006-06-09 16:13:01 -0700 | [diff] [blame] | 259 | goto err; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* Find out which fragments are in front and at the back of us |
| 263 | * in the chain of fragments so far. We must know where to put |
| 264 | * this fragment, right? |
| 265 | */ |
Changli Gao | ea8fbe8 | 2010-07-05 10:38:23 +0200 | [diff] [blame] | 266 | prev = fq->q.fragments_tail; |
| 267 | if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) { |
| 268 | next = NULL; |
| 269 | goto found; |
| 270 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 271 | prev = NULL; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 272 | for (next = fq->q.fragments; next != NULL; next = next->next) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 273 | if (NFCT_FRAG6_CB(next)->offset >= offset) |
| 274 | break; /* bingo! */ |
| 275 | prev = next; |
| 276 | } |
| 277 | |
Changli Gao | ea8fbe8 | 2010-07-05 10:38:23 +0200 | [diff] [blame] | 278 | found: |
Nicolas Dichtel | 1ee89bd | 2010-09-03 05:13:07 +0000 | [diff] [blame] | 279 | /* RFC5722, Section 4: |
| 280 | * When reassembling an IPv6 datagram, if |
| 281 | * one or more its constituent fragments is determined to be an |
| 282 | * overlapping fragment, the entire datagram (and any constituent |
| 283 | * fragments, including those not yet received) MUST be silently |
| 284 | * discarded. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 285 | */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 286 | |
Nicolas Dichtel | 1ee89bd | 2010-09-03 05:13:07 +0000 | [diff] [blame] | 287 | /* Check for overlap with preceding fragment. */ |
| 288 | if (prev && |
Shan Wei | 22e091e | 2010-11-12 08:51:55 +0100 | [diff] [blame] | 289 | (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset) |
Nicolas Dichtel | 1ee89bd | 2010-09-03 05:13:07 +0000 | [diff] [blame] | 290 | goto discard_fq; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 291 | |
Nicolas Dichtel | 1ee89bd | 2010-09-03 05:13:07 +0000 | [diff] [blame] | 292 | /* Look for overlap with succeeding segment. */ |
| 293 | if (next && NFCT_FRAG6_CB(next)->offset < end) |
| 294 | goto discard_fq; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 295 | |
| 296 | NFCT_FRAG6_CB(skb)->offset = offset; |
| 297 | |
| 298 | /* Insert this fragment in the chain of fragments. */ |
| 299 | skb->next = next; |
Changli Gao | ea8fbe8 | 2010-07-05 10:38:23 +0200 | [diff] [blame] | 300 | if (!next) |
| 301 | fq->q.fragments_tail = skb; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 302 | if (prev) |
| 303 | prev->next = skb; |
| 304 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 305 | fq->q.fragments = skb; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 306 | |
| 307 | skb->dev = NULL; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 308 | fq->q.stamp = skb->tstamp; |
| 309 | fq->q.meat += skb->len; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 310 | atomic_add(skb->truesize, &nf_init_frags.mem); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 311 | |
| 312 | /* The first fragment. |
| 313 | * nhoffset is obtained from the first fragment, of course. |
| 314 | */ |
| 315 | if (offset == 0) { |
| 316 | fq->nhoffset = nhoff; |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 317 | fq->q.last_in |= INET_FRAG_FIRST_IN; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 318 | } |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 319 | write_lock(&nf_frags.lock); |
Pavel Emelyanov | 3140c25 | 2008-01-22 06:11:48 -0800 | [diff] [blame] | 320 | list_move_tail(&fq->q.lru_list, &nf_init_frags.lru_list); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 321 | write_unlock(&nf_frags.lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 322 | return 0; |
| 323 | |
Nicolas Dichtel | 1ee89bd | 2010-09-03 05:13:07 +0000 | [diff] [blame] | 324 | discard_fq: |
| 325 | fq_kill(fq); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 326 | err: |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Check if this packet is complete. |
| 332 | * Returns NULL on failure by any reason, and pointer |
| 333 | * to current nexthdr field in reassembled frame. |
| 334 | * |
| 335 | * It is called with locked fq, and caller must check that |
| 336 | * queue is eligible for reassembly i.e. it is not COMPLETE, |
| 337 | * the last and the first frames arrived and all the bits are here. |
| 338 | */ |
| 339 | static struct sk_buff * |
| 340 | nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev) |
| 341 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 342 | struct sk_buff *fp, *op, *head = fq->q.fragments; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 343 | int payload_len; |
| 344 | |
| 345 | fq_kill(fq); |
| 346 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 347 | WARN_ON(head == NULL); |
| 348 | WARN_ON(NFCT_FRAG6_CB(head)->offset != 0); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 349 | |
| 350 | /* Unfragmented part is taken from the first segment. */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 351 | payload_len = ((head->data - skb_network_header(head)) - |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 352 | sizeof(struct ipv6hdr) + fq->q.len - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 353 | sizeof(struct frag_hdr)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 354 | if (payload_len > IPV6_MAXPLEN) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 355 | pr_debug("payload len is too large.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 356 | goto out_oversize; |
| 357 | } |
| 358 | |
| 359 | /* Head of list must not be cloned. */ |
| 360 | if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 361 | pr_debug("skb is cloned but can't expand head"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 362 | goto out_oom; |
| 363 | } |
| 364 | |
| 365 | /* If the first fragment is fragmented itself, we split |
| 366 | * it to two chunks: the first with data and paged part |
| 367 | * and the second, holding only fragments. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 368 | if (skb_has_frag_list(head)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 369 | struct sk_buff *clone; |
| 370 | int i, plen = 0; |
| 371 | |
| 372 | if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 373 | pr_debug("Can't alloc skb\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 374 | goto out_oom; |
| 375 | } |
| 376 | clone->next = head->next; |
| 377 | head->next = clone; |
| 378 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
David S. Miller | 343a997 | 2009-06-09 00:23:58 -0700 | [diff] [blame] | 379 | skb_frag_list_init(head); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 380 | for (i=0; i<skb_shinfo(head)->nr_frags; i++) |
| 381 | plen += skb_shinfo(head)->frags[i].size; |
| 382 | clone->len = clone->data_len = head->data_len - plen; |
| 383 | head->data_len -= clone->len; |
| 384 | head->len -= clone->len; |
| 385 | clone->csum = 0; |
| 386 | clone->ip_summed = head->ip_summed; |
| 387 | |
| 388 | NFCT_FRAG6_CB(clone)->orig = NULL; |
Pavel Emelyanov | 6ddc082 | 2008-01-22 06:07:25 -0800 | [diff] [blame] | 389 | atomic_add(clone->truesize, &nf_init_frags.mem); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* We have to remove fragment header from datagram and to relocate |
| 393 | * header in order to calculate ICV correctly. */ |
Arnaldo Carvalho de Melo | bff9b61 | 2007-03-16 17:19:57 -0300 | [diff] [blame] | 394 | skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0]; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 395 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 396 | (head->data - head->head) - sizeof(struct frag_hdr)); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 397 | head->mac_header += sizeof(struct frag_hdr); |
| 398 | head->network_header += sizeof(struct frag_hdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 399 | |
| 400 | skb_shinfo(head)->frag_list = head->next; |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 401 | skb_reset_transport_header(head); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 402 | skb_push(head, head->data - skb_network_header(head)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | |
| 404 | for (fp=head->next; fp; fp = fp->next) { |
| 405 | head->data_len += fp->len; |
| 406 | head->len += fp->len; |
| 407 | if (head->ip_summed != fp->ip_summed) |
| 408 | head->ip_summed = CHECKSUM_NONE; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 409 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 410 | head->csum = csum_add(head->csum, fp->csum); |
| 411 | head->truesize += fp->truesize; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 412 | } |
Shan Wei | 841a594 | 2010-06-14 16:28:23 +0200 | [diff] [blame] | 413 | atomic_sub(head->truesize, &nf_init_frags.mem); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 414 | |
| 415 | head->next = NULL; |
| 416 | head->dev = dev; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 417 | head->tstamp = fq->q.stamp; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 418 | ipv6_hdr(head)->payload_len = htons(payload_len); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 419 | |
| 420 | /* Yes, and fold redundant checksum back. 8) */ |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 421 | if (head->ip_summed == CHECKSUM_COMPLETE) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 422 | head->csum = csum_partial(skb_network_header(head), |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 423 | skb_network_header_len(head), |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 424 | head->csum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 425 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 426 | fq->q.fragments = NULL; |
Changli Gao | ea8fbe8 | 2010-07-05 10:38:23 +0200 | [diff] [blame] | 427 | fq->q.fragments_tail = NULL; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 428 | |
| 429 | /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */ |
| 430 | fp = skb_shinfo(head)->frag_list; |
Patrick McHardy | 9e2dcf7 | 2010-02-19 18:18:37 +0100 | [diff] [blame] | 431 | if (fp && NFCT_FRAG6_CB(fp)->orig == NULL) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 432 | /* at above code, head skb is divided into two skbs. */ |
| 433 | fp = fp->next; |
| 434 | |
| 435 | op = NFCT_FRAG6_CB(head)->orig; |
| 436 | for (; fp; fp = fp->next) { |
| 437 | struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig; |
| 438 | |
| 439 | op->next = orig; |
| 440 | op = orig; |
| 441 | NFCT_FRAG6_CB(fp)->orig = NULL; |
| 442 | } |
| 443 | |
| 444 | return head; |
| 445 | |
| 446 | out_oversize: |
| 447 | if (net_ratelimit()) |
| 448 | printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len); |
| 449 | goto out_fail; |
| 450 | out_oom: |
| 451 | if (net_ratelimit()) |
| 452 | printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n"); |
| 453 | out_fail: |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * find the header just before Fragment Header. |
| 459 | * |
| 460 | * if success return 0 and set ... |
| 461 | * (*prevhdrp): the value of "Next Header Field" in the header |
| 462 | * just before Fragment Header. |
| 463 | * (*prevhoff): the offset of "Next Header Field" in the header |
| 464 | * just before Fragment Header. |
| 465 | * (*fhoff) : the offset of Fragment Header. |
| 466 | * |
| 467 | * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c |
| 468 | * |
| 469 | */ |
| 470 | static int |
| 471 | find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff) |
| 472 | { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 473 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
Arnaldo Carvalho de Melo | 6b88dd9 | 2007-03-19 22:29:03 -0300 | [diff] [blame] | 474 | const int netoff = skb_network_offset(skb); |
| 475 | u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr); |
| 476 | int start = netoff + sizeof(struct ipv6hdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 477 | int len = skb->len - start; |
| 478 | u8 prevhdr = NEXTHDR_IPV6; |
| 479 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 480 | while (nexthdr != NEXTHDR_FRAGMENT) { |
| 481 | struct ipv6_opt_hdr hdr; |
| 482 | int hdrlen; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 483 | |
| 484 | if (!ipv6_ext_hdr(nexthdr)) { |
| 485 | return -1; |
| 486 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 487 | if (nexthdr == NEXTHDR_NONE) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 488 | pr_debug("next header is none\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 489 | return -1; |
| 490 | } |
Christoph Paasch | d1238d5 | 2009-03-16 15:52:11 +0100 | [diff] [blame] | 491 | if (len < (int)sizeof(struct ipv6_opt_hdr)) { |
| 492 | pr_debug("too short\n"); |
| 493 | return -1; |
| 494 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 495 | if (skb_copy_bits(skb, start, &hdr, sizeof(hdr))) |
| 496 | BUG(); |
| 497 | if (nexthdr == NEXTHDR_AUTH) |
| 498 | hdrlen = (hdr.hdrlen+2)<<2; |
| 499 | else |
| 500 | hdrlen = ipv6_optlen(&hdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 501 | |
| 502 | prevhdr = nexthdr; |
| 503 | prev_nhoff = start; |
| 504 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 505 | nexthdr = hdr.nexthdr; |
| 506 | len -= hdrlen; |
| 507 | start += hdrlen; |
| 508 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 509 | |
| 510 | if (len < 0) |
| 511 | return -1; |
| 512 | |
| 513 | *prevhdrp = prevhdr; |
| 514 | *prevhoff = prev_nhoff; |
| 515 | *fhoff = start; |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Patrick McHardy | 0b5ccb2 | 2009-12-15 16:59:18 +0100 | [diff] [blame] | 520 | struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 521 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 522 | struct sk_buff *clone; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 523 | struct net_device *dev = skb->dev; |
| 524 | struct frag_hdr *fhdr; |
| 525 | struct nf_ct_frag6_queue *fq; |
| 526 | struct ipv6hdr *hdr; |
| 527 | int fhoff, nhoff; |
| 528 | u8 prevhdr; |
| 529 | struct sk_buff *ret_skb = NULL; |
| 530 | |
| 531 | /* Jumbo payload inhibits frag. header */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 532 | if (ipv6_hdr(skb)->payload_len == 0) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 533 | pr_debug("payload len = 0\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 534 | return skb; |
| 535 | } |
| 536 | |
| 537 | if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0) |
| 538 | return skb; |
| 539 | |
| 540 | clone = skb_clone(skb, GFP_ATOMIC); |
| 541 | if (clone == NULL) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 542 | pr_debug("Can't clone skb\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 543 | return skb; |
| 544 | } |
| 545 | |
| 546 | NFCT_FRAG6_CB(clone)->orig = skb; |
| 547 | |
| 548 | if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 549 | pr_debug("message is too short.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 550 | goto ret_orig; |
| 551 | } |
| 552 | |
Arnaldo Carvalho de Melo | 967b05f | 2007-03-13 13:51:52 -0300 | [diff] [blame] | 553 | skb_set_transport_header(clone, fhoff); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 554 | hdr = ipv6_hdr(clone); |
Arnaldo Carvalho de Melo | bff9b61 | 2007-03-16 17:19:57 -0300 | [diff] [blame] | 555 | fhdr = (struct frag_hdr *)skb_transport_header(clone); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 556 | |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 557 | if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 558 | nf_ct_frag6_evictor(); |
| 559 | |
Patrick McHardy | 0b5ccb2 | 2009-12-15 16:59:18 +0100 | [diff] [blame] | 560 | fq = fq_find(fhdr->identification, user, &hdr->saddr, &hdr->daddr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 561 | if (fq == NULL) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 562 | pr_debug("Can't find and can't create new queue\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 563 | goto ret_orig; |
| 564 | } |
| 565 | |
Jarek Poplawski | b9c6989 | 2008-06-04 09:58:27 -0700 | [diff] [blame] | 566 | spin_lock_bh(&fq->q.lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 567 | |
| 568 | if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) { |
Jarek Poplawski | b9c6989 | 2008-06-04 09:58:27 -0700 | [diff] [blame] | 569 | spin_unlock_bh(&fq->q.lock); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 570 | pr_debug("Can't insert skb to queue\n"); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 571 | fq_put(fq); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 572 | goto ret_orig; |
| 573 | } |
| 574 | |
Joe Perches | bc578a5 | 2008-03-28 16:35:27 -0700 | [diff] [blame] | 575 | if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
| 576 | fq->q.meat == fq->q.len) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 577 | ret_skb = nf_ct_frag6_reasm(fq, dev); |
| 578 | if (ret_skb == NULL) |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 579 | pr_debug("Can't reassemble fragmented packets\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 580 | } |
Jarek Poplawski | b9c6989 | 2008-06-04 09:58:27 -0700 | [diff] [blame] | 581 | spin_unlock_bh(&fq->q.lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 582 | |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 583 | fq_put(fq); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 584 | return ret_skb; |
| 585 | |
| 586 | ret_orig: |
| 587 | kfree_skb(clone); |
| 588 | return skb; |
| 589 | } |
| 590 | |
| 591 | void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb, |
| 592 | struct net_device *in, struct net_device *out, |
| 593 | int (*okfn)(struct sk_buff *)) |
| 594 | { |
| 595 | struct sk_buff *s, *s2; |
| 596 | |
| 597 | for (s = NFCT_FRAG6_CB(skb)->orig; s;) { |
| 598 | nf_conntrack_put_reasm(s->nfct_reasm); |
| 599 | nf_conntrack_get_reasm(skb); |
| 600 | s->nfct_reasm = skb; |
| 601 | |
| 602 | s2 = s->next; |
Patrick McHardy | f9f02cc | 2007-01-09 14:32:41 -0800 | [diff] [blame] | 603 | s->next = NULL; |
| 604 | |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 605 | NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s, in, out, okfn, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 606 | NF_IP6_PRI_CONNTRACK_DEFRAG + 1); |
| 607 | s = s2; |
| 608 | } |
| 609 | nf_conntrack_put_reasm(skb); |
| 610 | } |
| 611 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 612 | int nf_ct_frag6_init(void) |
| 613 | { |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 614 | nf_frags.hashfn = nf_hashfn; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 615 | nf_frags.constructor = ip6_frag_init; |
Pavel Emelyanov | c954770 | 2007-10-17 19:48:26 -0700 | [diff] [blame] | 616 | nf_frags.destructor = NULL; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 617 | nf_frags.skb_free = nf_skb_free; |
| 618 | nf_frags.qsize = sizeof(struct nf_ct_frag6_queue); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 619 | nf_frags.match = ip6_frag_match; |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 620 | nf_frags.frag_expire = nf_ct_frag6_expire; |
Pavel Emelyanov | 3b4bc4a | 2008-01-22 06:11:04 -0800 | [diff] [blame] | 621 | nf_frags.secret_interval = 10 * 60 * HZ; |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 622 | nf_init_frags.timeout = IPV6_FRAG_TIMEOUT; |
Shan Wei | 7c070aa | 2010-01-20 10:42:41 +0100 | [diff] [blame] | 623 | nf_init_frags.high_thresh = IPV6_FRAG_HIGH_THRESH; |
| 624 | nf_init_frags.low_thresh = IPV6_FRAG_LOW_THRESH; |
Pavel Emelyanov | e5a2bb8 | 2008-01-22 06:06:23 -0800 | [diff] [blame] | 625 | inet_frags_init_net(&nf_init_frags); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 626 | inet_frags_init(&nf_frags); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 627 | |
David S. Miller | 7932c2e | 2010-10-26 09:08:53 -0700 | [diff] [blame] | 628 | #ifdef CONFIG_SYSCTL |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 629 | nf_ct_frag6_sysctl_header = register_sysctl_paths(nf_net_netfilter_sysctl_path, |
| 630 | nf_ct_frag6_sysctl_table); |
| 631 | if (!nf_ct_frag6_sysctl_header) { |
| 632 | inet_frags_fini(&nf_frags); |
| 633 | return -ENOMEM; |
| 634 | } |
David S. Miller | 7932c2e | 2010-10-26 09:08:53 -0700 | [diff] [blame] | 635 | #endif |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 636 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | void nf_ct_frag6_cleanup(void) |
| 641 | { |
David S. Miller | 7932c2e | 2010-10-26 09:08:53 -0700 | [diff] [blame] | 642 | #ifdef CONFIG_SYSCTL |
Balazs Scheidler | e97c3e2 | 2010-10-21 16:03:43 +0200 | [diff] [blame] | 643 | unregister_sysctl_table(nf_ct_frag6_sysctl_header); |
| 644 | nf_ct_frag6_sysctl_header = NULL; |
David S. Miller | 7932c2e | 2010-10-26 09:08:53 -0700 | [diff] [blame] | 645 | #endif |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 646 | inet_frags_fini(&nf_frags); |
| 647 | |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 648 | nf_init_frags.low_thresh = 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 649 | nf_ct_frag6_evictor(); |
| 650 | } |