blob: 428eaa502ec2d57b8e8a89eb268dd83a6c3e2903 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP fragmentation functionality.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
9 *
10 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
11 * Alan Cox <Alan.Cox@linux.org>
12 *
13 * Fixes:
14 * Alan Cox : Split from ip.c , see ip_input.c for history.
15 * David S. Miller : Begin massive cleanup...
16 * Andi Kleen : Add sysctls.
17 * xxxx : Overlapfrag bug.
18 * Ultima : ip_expire() kernel panic.
19 * Bill Hawes : Frag accounting and evictor fixes.
20 * John McDonald : 0 length frag bug.
21 * Alexey Kuznetsov: SMP races, threading, cleanup.
22 * Patrick McHardy : LRU queue of frag heads for evictor.
23 */
24
Herbert Xu89cee8b2005-12-13 23:14:27 -080025#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/jiffies.h>
30#include <linux/skbuff.h>
31#include <linux/list.h>
32#include <linux/ip.h>
33#include <linux/icmp.h>
34#include <linux/netdevice.h>
35#include <linux/jhash.h>
36#include <linux/random.h>
37#include <net/sock.h>
38#include <net/ip.h>
39#include <net/icmp.h>
40#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080041#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070042#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/tcp.h>
44#include <linux/udp.h>
45#include <linux/inet.h>
46#include <linux/netfilter_ipv4.h>
47
48/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
49 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
50 * as well. Or notify me, at least. --ANK
51 */
52
53/* Fragment cache limits. We will commit 256K at one time. Should we
54 * cross that limit we will prune down to 192K. This should cope with
55 * even the most extreme cases without allowing an attacker to measurably
56 * harm machine performance.
57 */
Brian Haleyab32ea52006-09-22 14:15:41 -070058int sysctl_ipfrag_high_thresh __read_mostly = 256*1024;
59int sysctl_ipfrag_low_thresh __read_mostly = 192*1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Brian Haleyab32ea52006-09-22 14:15:41 -070061int sysctl_ipfrag_max_dist __read_mostly = 64;
Herbert Xu89cee8b2005-12-13 23:14:27 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Important NOTE! Fragment queue must be destroyed before MSL expires.
64 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
65 */
Brian Haleyab32ea52006-09-22 14:15:41 -070066int sysctl_ipfrag_time __read_mostly = IP_FRAG_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68struct ipfrag_skb_cb
69{
70 struct inet_skb_parm h;
71 int offset;
72};
73
74#define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
75
76/* Describe an entry in the "incomplete datagrams" queue. */
77struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070078 struct inet_frag_queue q;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u32 user;
Al Viro18277772006-09-26 22:19:02 -070081 __be32 saddr;
82 __be32 daddr;
83 __be16 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 u8 protocol;
Herbert Xu89cee8b2005-12-13 23:14:27 -080085 int iif;
86 unsigned int rid;
87 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90/* Hash table. */
91
92#define IPQ_HASHSZ 64
93
94/* Per-bucket lock is easy to add now. */
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -080095static struct hlist_head ipq_hash[IPQ_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static DEFINE_RWLOCK(ipfrag_lock);
97static u32 ipfrag_hash_rnd;
98static LIST_HEAD(ipq_lru_list);
99int ip_frag_nqueues = 0;
100
Herbert Xu1706d582007-10-14 00:38:15 -0700101static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
102 struct net_device *dev);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static __inline__ void __ipq_unlink(struct ipq *qp)
105{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700106 hlist_del(&qp->q.list);
107 list_del(&qp->q.lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ip_frag_nqueues--;
109}
110
111static __inline__ void ipq_unlink(struct ipq *ipq)
112{
113 write_lock(&ipfrag_lock);
114 __ipq_unlink(ipq);
115 write_unlock(&ipfrag_lock);
116}
117
Al Viro18277772006-09-26 22:19:02 -0700118static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Al Viro18277772006-09-26 22:19:02 -0700120 return jhash_3words((__force u32)id << 16 | prot,
121 (__force u32)saddr, (__force u32)daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
123}
124
125static struct timer_list ipfrag_secret_timer;
Brian Haleyab32ea52006-09-22 14:15:41 -0700126int sysctl_ipfrag_secret_interval __read_mostly = 10 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128static void ipfrag_secret_rebuild(unsigned long dummy)
129{
130 unsigned long now = jiffies;
131 int i;
132
133 write_lock(&ipfrag_lock);
134 get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
135 for (i = 0; i < IPQ_HASHSZ; i++) {
136 struct ipq *q;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800137 struct hlist_node *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700139 hlist_for_each_entry_safe(q, p, n, &ipq_hash[i], q.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned int hval = ipqhashfn(q->id, q->saddr,
141 q->daddr, q->protocol);
142
143 if (hval != i) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700144 hlist_del(&q->q.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* Relink to new hash chain. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700147 hlist_add_head(&q->q.list, &ipq_hash[hval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
151 write_unlock(&ipfrag_lock);
152
153 mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
154}
155
156atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
157
158/* Memory Tracking Functions. */
159static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
160{
161 if (work)
162 *work -= skb->truesize;
163 atomic_sub(skb->truesize, &ip_frag_mem);
164 kfree_skb(skb);
165}
166
167static __inline__ void frag_free_queue(struct ipq *qp, int *work)
168{
169 if (work)
170 *work -= sizeof(struct ipq);
171 atomic_sub(sizeof(struct ipq), &ip_frag_mem);
172 kfree(qp);
173}
174
175static __inline__ struct ipq *frag_alloc_queue(void)
176{
177 struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
178
Stephen Hemminger132adf52007-03-08 20:44:43 -0800179 if (!qp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return NULL;
181 atomic_add(sizeof(struct ipq), &ip_frag_mem);
182 return qp;
183}
184
185
186/* Destruction primitives. */
187
188/* Complete destruction of ipq. */
189static void ip_frag_destroy(struct ipq *qp, int *work)
190{
191 struct sk_buff *fp;
192
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700193 BUG_TRAP(qp->q.last_in&COMPLETE);
194 BUG_TRAP(del_timer(&qp->q.timer) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Herbert Xu89cee8b2005-12-13 23:14:27 -0800196 if (qp->peer)
197 inet_putpeer(qp->peer);
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Release all fragment data. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700200 fp = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 while (fp) {
202 struct sk_buff *xp = fp->next;
203
204 frag_kfree_skb(fp, work);
205 fp = xp;
206 }
207
208 /* Finally, release the queue descriptor itself. */
209 frag_free_queue(qp, work);
210}
211
212static __inline__ void ipq_put(struct ipq *ipq, int *work)
213{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700214 if (atomic_dec_and_test(&ipq->q.refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ip_frag_destroy(ipq, work);
216}
217
218/* Kill ipq entry. It is not destroyed immediately,
219 * because caller (and someone more) holds reference count.
220 */
221static void ipq_kill(struct ipq *ipq)
222{
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700223 if (del_timer(&ipq->q.timer))
224 atomic_dec(&ipq->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700226 if (!(ipq->q.last_in & COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ipq_unlink(ipq);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700228 atomic_dec(&ipq->q.refcnt);
229 ipq->q.last_in |= COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231}
232
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900233/* Memory limiting on fragments. Evictor trashes the oldest
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * fragment queue until we are back under the threshold.
235 */
236static void ip_evictor(void)
237{
238 struct ipq *qp;
239 struct list_head *tmp;
240 int work;
241
242 work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
243 if (work <= 0)
244 return;
245
246 while (work > 0) {
247 read_lock(&ipfrag_lock);
248 if (list_empty(&ipq_lru_list)) {
249 read_unlock(&ipfrag_lock);
250 return;
251 }
252 tmp = ipq_lru_list.next;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700253 qp = list_entry(tmp, struct ipq, q.lru_list);
254 atomic_inc(&qp->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 read_unlock(&ipfrag_lock);
256
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700257 spin_lock(&qp->q.lock);
258 if (!(qp->q.last_in&COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ipq_kill(qp);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700260 spin_unlock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 ipq_put(qp, &work);
263 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
264 }
265}
266
267/*
268 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
269 */
270static void ip_expire(unsigned long arg)
271{
272 struct ipq *qp = (struct ipq *) arg;
273
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700274 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700276 if (qp->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto out;
278
279 ipq_kill(qp);
280
281 IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
282 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
283
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700284 if ((qp->q.last_in&FIRST_IN) && qp->q.fragments != NULL) {
285 struct sk_buff *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Send an ICMP "Fragment Reassembly Timeout" message. */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700287 if ((head->dev = dev_get_by_index(&init_net, qp->iif)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
289 dev_put(head->dev);
290 }
291 }
292out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700293 spin_unlock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ipq_put(qp, NULL);
295}
296
297/* Creation primitives. */
298
David S. Miller55c00222006-04-09 22:43:55 -0700299static struct ipq *ip_frag_intern(struct ipq *qp_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct ipq *qp;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800302#ifdef CONFIG_SMP
303 struct hlist_node *n;
304#endif
David S. Miller55c00222006-04-09 22:43:55 -0700305 unsigned int hash;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 write_lock(&ipfrag_lock);
David S. Miller55c00222006-04-09 22:43:55 -0700308 hash = ipqhashfn(qp_in->id, qp_in->saddr, qp_in->daddr,
309 qp_in->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#ifdef CONFIG_SMP
311 /* With SMP race we have to recheck hash table, because
312 * such entry could be created on other cpu, while we
313 * promoted read lock to write lock.
314 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700315 hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800316 if (qp->id == qp_in->id &&
317 qp->saddr == qp_in->saddr &&
318 qp->daddr == qp_in->daddr &&
319 qp->protocol == qp_in->protocol &&
320 qp->user == qp_in->user) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700321 atomic_inc(&qp->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 write_unlock(&ipfrag_lock);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700323 qp_in->q.last_in |= COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ipq_put(qp_in, NULL);
325 return qp;
326 }
327 }
328#endif
329 qp = qp_in;
330
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700331 if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time))
332 atomic_inc(&qp->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700334 atomic_inc(&qp->q.refcnt);
335 hlist_add_head(&qp->q.list, &ipq_hash[hash]);
336 INIT_LIST_HEAD(&qp->q.lru_list);
337 list_add_tail(&qp->q.lru_list, &ipq_lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ip_frag_nqueues++;
339 write_unlock(&ipfrag_lock);
340 return qp;
341}
342
343/* Add an entry to the 'ipq' queue for a newly received IP datagram. */
David S. Miller55c00222006-04-09 22:43:55 -0700344static struct ipq *ip_frag_create(struct iphdr *iph, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct ipq *qp;
347
348 if ((qp = frag_alloc_queue()) == NULL)
349 goto out_nomem;
350
351 qp->protocol = iph->protocol;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 qp->q.last_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 qp->id = iph->id;
354 qp->saddr = iph->saddr;
355 qp->daddr = iph->daddr;
356 qp->user = user;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700357 qp->q.len = 0;
358 qp->q.meat = 0;
359 qp->q.fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 qp->iif = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800361 qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Initialize a timer for this entry. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700364 init_timer(&qp->q.timer);
365 qp->q.timer.data = (unsigned long) qp; /* pointer to queue */
366 qp->q.timer.function = ip_expire; /* expire function */
367 spin_lock_init(&qp->q.lock);
368 atomic_set(&qp->q.refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David S. Miller55c00222006-04-09 22:43:55 -0700370 return ip_frag_intern(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372out_nomem:
Patrick McHardy64ce2072005-08-09 20:50:53 -0700373 LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
375}
376
377/* Find the correct entry in the "incomplete datagrams" queue for
378 * this IP datagram, and create new one, if nothing is found.
379 */
380static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
381{
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800382 __be16 id = iph->id;
Al Viro18277772006-09-26 22:19:02 -0700383 __be32 saddr = iph->saddr;
384 __be32 daddr = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 __u8 protocol = iph->protocol;
David S. Miller55c00222006-04-09 22:43:55 -0700386 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct ipq *qp;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800388 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 read_lock(&ipfrag_lock);
David S. Miller55c00222006-04-09 22:43:55 -0700391 hash = ipqhashfn(id, saddr, daddr, protocol);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700392 hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800393 if (qp->id == id &&
394 qp->saddr == saddr &&
395 qp->daddr == daddr &&
396 qp->protocol == protocol &&
397 qp->user == user) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700398 atomic_inc(&qp->q.refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 read_unlock(&ipfrag_lock);
400 return qp;
401 }
402 }
403 read_unlock(&ipfrag_lock);
404
David S. Miller55c00222006-04-09 22:43:55 -0700405 return ip_frag_create(iph, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Herbert Xu89cee8b2005-12-13 23:14:27 -0800408/* Is the fragment too far ahead to be part of ipq? */
409static inline int ip_frag_too_far(struct ipq *qp)
410{
411 struct inet_peer *peer = qp->peer;
412 unsigned int max = sysctl_ipfrag_max_dist;
413 unsigned int start, end;
414
415 int rc;
416
417 if (!peer || !max)
418 return 0;
419
420 start = qp->rid;
421 end = atomic_inc_return(&peer->rid);
422 qp->rid = end;
423
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700424 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800425
426 if (rc) {
427 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
428 }
429
430 return rc;
431}
432
433static int ip_frag_reinit(struct ipq *qp)
434{
435 struct sk_buff *fp;
436
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700437 if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time)) {
438 atomic_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800439 return -ETIMEDOUT;
440 }
441
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700442 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800443 do {
444 struct sk_buff *xp = fp->next;
445 frag_kfree_skb(fp, NULL);
446 fp = xp;
447 } while (fp);
448
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700449 qp->q.last_in = 0;
450 qp->q.len = 0;
451 qp->q.meat = 0;
452 qp->q.fragments = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800453 qp->iif = 0;
454
455 return 0;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700459static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700462 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int flags, offset;
464 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700465 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700467 if (qp->q.last_in & COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto err;
469
Herbert Xu89cee8b2005-12-13 23:14:27 -0800470 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700471 unlikely(ip_frag_too_far(qp)) &&
472 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800473 ipq_kill(qp);
474 goto err;
475 }
476
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700477 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 flags = offset & ~IP_OFFSET;
479 offset &= IP_OFFSET;
480 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300481 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Determine the position of this fragment. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900484 end = offset + skb->len - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700485 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Is this the final fragment? */
488 if ((flags & IP_MF) == 0) {
489 /* If we already have some bits beyond end
490 * or have different end, the segment is corrrupted.
491 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700492 if (end < qp->q.len ||
493 ((qp->q.last_in & LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700495 qp->q.last_in |= LAST_IN;
496 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
498 if (end&7) {
499 end &= ~7;
500 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
501 skb->ip_summed = CHECKSUM_NONE;
502 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700503 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Some bits beyond end -> corruption. */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700505 if (qp->q.last_in & LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700507 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510 if (end == offset)
511 goto err;
512
Herbert Xu1706d582007-10-14 00:38:15 -0700513 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (pskb_pull(skb, ihl) == NULL)
515 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700516
517 err = pskb_trim_rcsum(skb, end - offset);
518 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto err;
520
521 /* Find out which fragments are in front and at the back of us
522 * in the chain of fragments so far. We must know where to put
523 * this fragment, right?
524 */
525 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700526 for (next = qp->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (FRAG_CB(next)->offset >= offset)
528 break; /* bingo! */
529 prev = next;
530 }
531
532 /* We found where to put this one. Check for overlap with
533 * preceding fragment, and, if needed, align things so that
534 * any overlaps are eliminated.
535 */
536 if (prev) {
537 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
538
539 if (i > 0) {
540 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700541 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (end <= offset)
543 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700544 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (!pskb_pull(skb, i))
546 goto err;
547 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
548 skb->ip_summed = CHECKSUM_NONE;
549 }
550 }
551
Herbert Xu1706d582007-10-14 00:38:15 -0700552 err = -ENOMEM;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 while (next && FRAG_CB(next)->offset < end) {
555 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
556
557 if (i < next->len) {
558 /* Eat head of the next overlapped fragment
559 * and leave the loop. The next ones cannot overlap.
560 */
561 if (!pskb_pull(next, i))
562 goto err;
563 FRAG_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700564 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (next->ip_summed != CHECKSUM_UNNECESSARY)
566 next->ip_summed = CHECKSUM_NONE;
567 break;
568 } else {
569 struct sk_buff *free_it = next;
570
Peter Zijlstra47c6bf772006-12-12 19:48:59 +0100571 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * new one drop it.
573 */
574 next = next->next;
575
576 if (prev)
577 prev->next = next;
578 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700579 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700581 qp->q.meat -= free_it->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 frag_kfree_skb(free_it, NULL);
583 }
584 }
585
586 FRAG_CB(skb)->offset = offset;
587
588 /* Insert this fragment in the chain of fragments. */
589 skb->next = next;
590 if (prev)
591 prev->next = skb;
592 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700593 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Herbert Xu1706d582007-10-14 00:38:15 -0700595 dev = skb->dev;
596 if (dev) {
597 qp->iif = dev->ifindex;
598 skb->dev = NULL;
599 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700600 qp->q.stamp = skb->tstamp;
601 qp->q.meat += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 atomic_add(skb->truesize, &ip_frag_mem);
603 if (offset == 0)
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700604 qp->q.last_in |= FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700606 if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
Herbert Xu1706d582007-10-14 00:38:15 -0700607 return ip_frag_reasm(qp, prev, dev);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 write_lock(&ipfrag_lock);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700610 list_move_tail(&qp->q.lru_list, &ipq_lru_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 write_unlock(&ipfrag_lock);
Herbert Xu1706d582007-10-14 00:38:15 -0700612 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614err:
615 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619
620/* Build a new IP datagram from all its fragments. */
621
Herbert Xu1706d582007-10-14 00:38:15 -0700622static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
623 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700626 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int len;
628 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700629 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 ipq_kill(qp);
632
Herbert Xu1706d582007-10-14 00:38:15 -0700633 /* Make the one we just received the head. */
634 if (prev) {
635 head = prev->next;
636 fp = skb_clone(head, GFP_ATOMIC);
637
638 if (!fp)
639 goto out_nomem;
640
641 fp->next = head->next;
642 prev->next = fp;
643
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700644 skb_morph(head, qp->q.fragments);
645 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700646
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700647 kfree_skb(qp->q.fragments);
648 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 BUG_TRAP(head != NULL);
652 BUG_TRAP(FRAG_CB(head)->offset == 0);
653
654 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300655 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700656 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Herbert Xu1706d582007-10-14 00:38:15 -0700658 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800659 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out_oversize;
661
662 /* Head of list must not be cloned. */
Herbert Xu1706d582007-10-14 00:38:15 -0700663 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
665 goto out_nomem;
666
667 /* If the first fragment is fragmented itself, we split
668 * it to two chunks: the first with data and paged part
669 * and the second, holding only fragments. */
670 if (skb_shinfo(head)->frag_list) {
671 struct sk_buff *clone;
672 int i, plen = 0;
673
674 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
675 goto out_nomem;
676 clone->next = head->next;
677 head->next = clone;
678 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
679 skb_shinfo(head)->frag_list = NULL;
680 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
681 plen += skb_shinfo(head)->frags[i].size;
682 clone->len = clone->data_len = head->data_len - plen;
683 head->data_len -= clone->len;
684 head->len -= clone->len;
685 clone->csum = 0;
686 clone->ip_summed = head->ip_summed;
687 atomic_add(clone->truesize, &ip_frag_mem);
688 }
689
690 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700691 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 atomic_sub(head->truesize, &ip_frag_mem);
693
694 for (fp=head->next; fp; fp = fp->next) {
695 head->data_len += fp->len;
696 head->len += fp->len;
697 if (head->ip_summed != fp->ip_summed)
698 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700699 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 head->csum = csum_add(head->csum, fp->csum);
701 head->truesize += fp->truesize;
702 atomic_sub(fp->truesize, &ip_frag_mem);
703 }
704
705 head->next = NULL;
706 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700707 head->tstamp = qp->q.stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700709 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 iph->frag_off = 0;
711 iph->tot_len = htons(len);
712 IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700713 qp->q.fragments = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716out_nomem:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900717 LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
Patrick McHardy64ce2072005-08-09 20:50:53 -0700718 "queue %p\n", qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out_fail;
720out_oversize:
721 if (net_ratelimit())
722 printk(KERN_INFO
723 "Oversized IP packet from %d.%d.%d.%d.\n",
724 NIPQUAD(qp->saddr));
725out_fail:
726 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730/* Process an incoming IP datagram fragment. */
Herbert Xu776c7292007-10-14 00:38:32 -0700731int ip_defrag(struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
736
737 /* Start by cleaning up the memory. */
738 if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
739 ip_evictor();
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* Lookup (or create) queue header */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700742 if ((qp = ip_find(ip_hdr(skb), user)) != NULL) {
Herbert Xu1706d582007-10-14 00:38:15 -0700743 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700745 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Herbert Xu1706d582007-10-14 00:38:15 -0700747 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700749 spin_unlock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ipq_put(qp, NULL);
Herbert Xu776c7292007-10-14 00:38:32 -0700751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
755 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700756 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700759void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
762 (jiffies ^ (jiffies >> 6)));
763
764 init_timer(&ipfrag_secret_timer);
765 ipfrag_secret_timer.function = ipfrag_secret_rebuild;
766 ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
767 add_timer(&ipfrag_secret_timer);
768}
769
770EXPORT_SYMBOL(ip_defrag);