blob: a1151b8adf3c6d65409ea8944973432187e49fe0 [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 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
Alan Cox113aa832008-10-13 19:01:08 -07009 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Fixes:
12 * Alan Cox : Split from ip.c , see ip_input.c for history.
13 * David S. Miller : Begin massive cleanup...
14 * Andi Kleen : Add sysctls.
15 * xxxx : Overlapfrag bug.
16 * Ultima : ip_expire() kernel panic.
17 * Bill Hawes : Frag accounting and evictor fixes.
18 * John McDonald : 0 length frag bug.
19 * Alexey Kuznetsov: SMP races, threading, cleanup.
20 * Patrick McHardy : LRU queue of frag heads for evictor.
21 */
22
Herbert Xu89cee8b2005-12-13 23:14:27 -080023#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/jiffies.h>
28#include <linux/skbuff.h>
29#include <linux/list.h>
30#include <linux/ip.h>
31#include <linux/icmp.h>
32#include <linux/netdevice.h>
33#include <linux/jhash.h>
34#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Shan Weie9017b52010-01-23 01:57:42 -080036#include <net/route.h>
37#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <net/sock.h>
39#include <net/ip.h>
40#include <net/icmp.h>
41#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080042#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070043#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/tcp.h>
45#include <linux/udp.h>
46#include <linux/inet.h>
47#include <linux/netfilter_ipv4.h>
Eric Dumazet6623e3b2011-01-05 07:52:55 +000048#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
51 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
52 * as well. Or notify me, at least. --ANK
53 */
54
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080055static int sysctl_ipfrag_max_dist __read_mostly = 64;
Herbert Xu89cee8b2005-12-13 23:14:27 -080056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct ipfrag_skb_cb
58{
59 struct inet_skb_parm h;
60 int offset;
61};
62
Jianjun Kongfd3f8c42008-11-03 02:47:38 -080063#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Describe an entry in the "incomplete datagrams" queue. */
66struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070067 struct inet_frag_queue q;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 u32 user;
Al Viro18277772006-09-26 22:19:02 -070070 __be32 saddr;
71 __be32 daddr;
72 __be16 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 u8 protocol;
Eric Dumazet6623e3b2011-01-05 07:52:55 +000074 u8 ecn; /* RFC3168 support */
Herbert Xu89cee8b2005-12-13 23:14:27 -080075 int iif;
76 unsigned int rid;
77 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Eric Dumazet6623e3b2011-01-05 07:52:55 +000080#define IPFRAG_ECN_CLEAR 0x01 /* one frag had INET_ECN_NOT_ECT */
81#define IPFRAG_ECN_SET_CE 0x04 /* one frag had INET_ECN_CE */
82
83static inline u8 ip4_frag_ecn(u8 tos)
84{
85 tos = (tos & INET_ECN_MASK) + 1;
86 /*
87 * After the last operation we have (in binary):
88 * INET_ECN_NOT_ECT => 001
89 * INET_ECN_ECT_1 => 010
90 * INET_ECN_ECT_0 => 011
91 * INET_ECN_CE => 100
92 */
93 return (tos & 2) ? 0 : tos;
94}
95
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070096static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Pavel Emelyanove5a2bb8422008-01-22 06:06:23 -080098int ip_frag_nqueues(struct net *net)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070099{
Pavel Emelyanove5a2bb8422008-01-22 06:06:23 -0800100 return net->ipv4.frags.nqueues;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800103int ip_frag_mem(struct net *net)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700104{
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800105 return atomic_read(&net->ipv4.frags.mem);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Herbert Xu1706d582007-10-14 00:38:15 -0700108static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
109 struct net_device *dev);
110
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700111struct ip4_create_arg {
112 struct iphdr *iph;
113 u32 user;
114};
115
Al Viro18277772006-09-26 22:19:02 -0700116static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Al Viro18277772006-09-26 22:19:02 -0700118 return jhash_3words((__force u32)id << 16 | prot,
119 (__force u32)saddr, (__force u32)daddr,
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700120 ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700123static unsigned int ip4_hashfn(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700125 struct ipq *ipq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700127 ipq = container_of(q, struct ipq, q);
128 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700131static int ip4_frag_match(struct inet_frag_queue *q, void *a)
132{
133 struct ipq *qp;
134 struct ip4_create_arg *arg = a;
135
136 qp = container_of(q, struct ipq, q);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000137 return qp->id == arg->iph->id &&
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700138 qp->saddr == arg->iph->saddr &&
139 qp->daddr == arg->iph->daddr &&
140 qp->protocol == arg->iph->protocol &&
Eric Dumazeta02cec22010-09-22 20:43:57 +0000141 qp->user == arg->user;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Memory Tracking Functions. */
Eric Dumazeta95d8c82010-06-13 23:22:43 +0000145static void frag_kfree_skb(struct netns_frags *nf, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800147 atomic_sub(skb->truesize, &nf->mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 kfree_skb(skb);
149}
150
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700151static void ip4_frag_init(struct inet_frag_queue *q, void *a)
152{
153 struct ipq *qp = container_of(q, struct ipq, q);
154 struct ip4_create_arg *arg = a;
155
156 qp->protocol = arg->iph->protocol;
157 qp->id = arg->iph->id;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000158 qp->ecn = ip4_frag_ecn(arg->iph->tos);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700159 qp->saddr = arg->iph->saddr;
160 qp->daddr = arg->iph->daddr;
161 qp->user = arg->user;
162 qp->peer = sysctl_ipfrag_max_dist ?
David S. Millerb534ecf2010-11-30 11:54:19 -0800163 inet_getpeer_v4(arg->iph->saddr, 1) : NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700164}
165
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700166static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700168 struct ipq *qp;
169
170 qp = container_of(q, struct ipq, q);
171 if (qp->peer)
172 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* Destruction primitives. */
177
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700178static __inline__ void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700180 inet_frag_put(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/* Kill ipq entry. It is not destroyed immediately,
184 * because caller (and someone more) holds reference count.
185 */
186static void ipq_kill(struct ipq *ipq)
187{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700188 inet_frag_kill(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900191/* Memory limiting on fragments. Evictor trashes the oldest
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * fragment queue until we are back under the threshold.
193 */
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800194static void ip_evictor(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700196 int evicted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800198 evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
Pavel Emelyanov8e7999c2007-10-15 02:40:06 -0700199 if (evicted)
Pavel Emelyanovc5346fe2008-07-16 20:20:33 -0700200 IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
205 */
206static void ip_expire(unsigned long arg)
207{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700208 struct ipq *qp;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700209 struct net *net;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700210
211 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700212 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700214 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Joe Perchesbc578a52008-03-28 16:35:27 -0700216 if (qp->q.last_in & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out;
218
219 ipq_kill(qp);
220
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700221 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
222 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Joe Perchesbc578a52008-03-28 16:35:27 -0700224 if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700225 struct sk_buff *head = qp->q.fragments;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700226
Eric Dumazet69df9d52009-11-05 20:59:47 -0800227 rcu_read_lock();
228 head->dev = dev_get_by_index_rcu(net, qp->iif);
Shan Weie9017b52010-01-23 01:57:42 -0800229 if (!head->dev)
230 goto out_rcu_unlock;
231
232 /*
233 * Only search router table for the head fragment,
234 * when defraging timeout at PRE_ROUTING HOOK.
235 */
236 if (qp->user == IP_DEFRAG_CONNTRACK_IN && !skb_dst(head)) {
237 const struct iphdr *iph = ip_hdr(head);
238 int err = ip_route_input(head, iph->daddr, iph->saddr,
239 iph->tos, head->dev);
240 if (unlikely(err))
241 goto out_rcu_unlock;
242
243 /*
244 * Only an end host needs to send an ICMP
245 * "Fragment Reassembly Timeout" message, per RFC792.
246 */
247 if (skb_rtable(head)->rt_type != RTN_LOCAL)
248 goto out_rcu_unlock;
249
250 }
251
252 /* Send an ICMP "Fragment Reassembly Timeout" message. */
253 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
Shan Weie9017b52010-01-23 01:57:42 -0800254out_rcu_unlock:
Patrick McHardyd1c9ae62010-02-02 11:46:50 -0800255 rcu_read_unlock();
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700258 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700259 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700262/* Find the correct entry in the "incomplete datagrams" queue for
263 * this IP datagram, and create new one, if nothing is found.
264 */
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800265static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700267 struct inet_frag_queue *q;
268 struct ip4_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700269 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700271 arg.iph = iph;
272 arg.user = user;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700273
274 read_lock(&ip4_frags.lock);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700275 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700276
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800277 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700278 if (q == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto out_nomem;
280
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700281 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283out_nomem:
Patrick McHardy64ce2072005-08-09 20:50:53 -0700284 LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return NULL;
286}
287
Herbert Xu89cee8b2005-12-13 23:14:27 -0800288/* Is the fragment too far ahead to be part of ipq? */
289static inline int ip_frag_too_far(struct ipq *qp)
290{
291 struct inet_peer *peer = qp->peer;
292 unsigned int max = sysctl_ipfrag_max_dist;
293 unsigned int start, end;
294
295 int rc;
296
297 if (!peer || !max)
298 return 0;
299
300 start = qp->rid;
301 end = atomic_inc_return(&peer->rid);
302 qp->rid = end;
303
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700304 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800305
306 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700307 struct net *net;
308
309 net = container_of(qp->q.net, struct net, ipv4.frags);
310 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800311 }
312
313 return rc;
314}
315
316static int ip_frag_reinit(struct ipq *qp)
317{
318 struct sk_buff *fp;
319
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800320 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700321 atomic_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800322 return -ETIMEDOUT;
323 }
324
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700325 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800326 do {
327 struct sk_buff *xp = fp->next;
Eric Dumazeta95d8c82010-06-13 23:22:43 +0000328 frag_kfree_skb(qp->q.net, fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800329 fp = xp;
330 } while (fp);
331
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700332 qp->q.last_in = 0;
333 qp->q.len = 0;
334 qp->q.meat = 0;
335 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000336 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800337 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000338 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800339
340 return 0;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700344static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700347 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int flags, offset;
349 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700350 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000351 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Joe Perchesbc578a52008-03-28 16:35:27 -0700353 if (qp->q.last_in & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto err;
355
Herbert Xu89cee8b2005-12-13 23:14:27 -0800356 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700357 unlikely(ip_frag_too_far(qp)) &&
358 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800359 ipq_kill(qp);
360 goto err;
361 }
362
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000363 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700364 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 flags = offset & ~IP_OFFSET;
366 offset &= IP_OFFSET;
367 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300368 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* Determine the position of this fragment. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900371 end = offset + skb->len - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700372 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* Is this the final fragment? */
375 if ((flags & IP_MF) == 0) {
376 /* If we already have some bits beyond end
377 * or have different end, the segment is corrrupted.
378 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700379 if (end < qp->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700380 ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto err;
Joe Perchesbc578a52008-03-28 16:35:27 -0700382 qp->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700383 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
385 if (end&7) {
386 end &= ~7;
387 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
388 skb->ip_summed = CHECKSUM_NONE;
389 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700390 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700392 if (qp->q.last_in & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700394 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397 if (end == offset)
398 goto err;
399
Herbert Xu1706d582007-10-14 00:38:15 -0700400 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (pskb_pull(skb, ihl) == NULL)
402 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700403
404 err = pskb_trim_rcsum(skb, end - offset);
405 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto err;
407
408 /* Find out which fragments are in front and at the back of us
409 * in the chain of fragments so far. We must know where to put
410 * this fragment, right?
411 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000412 prev = qp->q.fragments_tail;
413 if (!prev || FRAG_CB(prev)->offset < offset) {
414 next = NULL;
415 goto found;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700418 for (next = qp->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (FRAG_CB(next)->offset >= offset)
420 break; /* bingo! */
421 prev = next;
422 }
423
Changli Gaod6bebca2010-06-29 04:39:37 +0000424found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* We found where to put this one. Check for overlap with
426 * preceding fragment, and, if needed, align things so that
427 * any overlaps are eliminated.
428 */
429 if (prev) {
430 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
431
432 if (i > 0) {
433 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700434 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (end <= offset)
436 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700437 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!pskb_pull(skb, i))
439 goto err;
440 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
441 skb->ip_summed = CHECKSUM_NONE;
442 }
443 }
444
Herbert Xu1706d582007-10-14 00:38:15 -0700445 err = -ENOMEM;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 while (next && FRAG_CB(next)->offset < end) {
448 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
449
450 if (i < next->len) {
451 /* Eat head of the next overlapped fragment
452 * and leave the loop. The next ones cannot overlap.
453 */
454 if (!pskb_pull(next, i))
455 goto err;
456 FRAG_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700457 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (next->ip_summed != CHECKSUM_UNNECESSARY)
459 next->ip_summed = CHECKSUM_NONE;
460 break;
461 } else {
462 struct sk_buff *free_it = next;
463
Peter Zijlstra47c6bf772006-12-12 19:48:59 +0100464 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * new one drop it.
466 */
467 next = next->next;
468
469 if (prev)
470 prev->next = next;
471 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700472 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700474 qp->q.meat -= free_it->len;
Eric Dumazeta95d8c82010-06-13 23:22:43 +0000475 frag_kfree_skb(qp->q.net, free_it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
478
479 FRAG_CB(skb)->offset = offset;
480
481 /* Insert this fragment in the chain of fragments. */
482 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000483 if (!next)
484 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (prev)
486 prev->next = skb;
487 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700488 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Herbert Xu1706d582007-10-14 00:38:15 -0700490 dev = skb->dev;
491 if (dev) {
492 qp->iif = dev->ifindex;
493 skb->dev = NULL;
494 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700495 qp->q.stamp = skb->tstamp;
496 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000497 qp->ecn |= ecn;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800498 atomic_add(skb->truesize, &qp->q.net->mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (offset == 0)
Joe Perchesbc578a52008-03-28 16:35:27 -0700500 qp->q.last_in |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Joe Perchesbc578a52008-03-28 16:35:27 -0700502 if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
503 qp->q.meat == qp->q.len)
Herbert Xu1706d582007-10-14 00:38:15 -0700504 return ip_frag_reasm(qp, prev, dev);
505
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700506 write_lock(&ip4_frags.lock);
Pavel Emelyanov3140c252008-01-22 06:11:48 -0800507 list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700508 write_unlock(&ip4_frags.lock);
Herbert Xu1706d582007-10-14 00:38:15 -0700509 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511err:
512 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516
517/* Build a new IP datagram from all its fragments. */
518
Herbert Xu1706d582007-10-14 00:38:15 -0700519static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
520 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700522 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700524 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int len;
526 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700527 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 ipq_kill(qp);
530
Herbert Xu1706d582007-10-14 00:38:15 -0700531 /* Make the one we just received the head. */
532 if (prev) {
533 head = prev->next;
534 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700535 if (!fp)
536 goto out_nomem;
537
538 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000539 if (!fp->next)
540 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700541 prev->next = fp;
542
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700543 skb_morph(head, qp->q.fragments);
544 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700545
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700546 kfree_skb(qp->q.fragments);
547 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700548 }
549
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700550 WARN_ON(head == NULL);
551 WARN_ON(FRAG_CB(head)->offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300554 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700555 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Herbert Xu1706d582007-10-14 00:38:15 -0700557 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800558 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out_oversize;
560
561 /* Head of list must not be cloned. */
562 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
563 goto out_nomem;
564
565 /* If the first fragment is fragmented itself, we split
566 * it to two chunks: the first with data and paged part
567 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700568 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct sk_buff *clone;
570 int i, plen = 0;
571
572 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
573 goto out_nomem;
574 clone->next = head->next;
575 head->next = clone;
576 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700577 skb_frag_list_init(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
579 plen += skb_shinfo(head)->frags[i].size;
580 clone->len = clone->data_len = head->data_len - plen;
581 head->data_len -= clone->len;
582 head->len -= clone->len;
583 clone->csum = 0;
584 clone->ip_summed = head->ip_summed;
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800585 atomic_add(clone->truesize, &qp->q.net->mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700589 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 for (fp=head->next; fp; fp = fp->next) {
592 head->data_len += fp->len;
593 head->len += fp->len;
594 if (head->ip_summed != fp->ip_summed)
595 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700596 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 head->csum = csum_add(head->csum, fp->csum);
598 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Eric Dumazetd27f9b32010-06-13 23:02:24 +0000600 atomic_sub(head->truesize, &qp->q.net->mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 head->next = NULL;
603 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700604 head->tstamp = qp->q.stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700606 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 iph->frag_off = 0;
608 iph->tot_len = htons(len);
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000609 /* RFC3168 5.3 Fragmentation support
610 * If one fragment had INET_ECN_NOT_ECT,
611 * reassembled frame also has INET_ECN_NOT_ECT
612 * Elif one fragment had INET_ECN_CE
613 * reassembled frame also has INET_ECN_CE
614 */
615 if (qp->ecn & IPFRAG_ECN_CLEAR)
616 iph->tos &= ~INET_ECN_MASK;
617 else if (qp->ecn & IPFRAG_ECN_SET_CE)
618 iph->tos |= INET_ECN_CE;
619
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700620 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700621 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000622 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625out_nomem:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900626 LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
Patrick McHardy64ce2072005-08-09 20:50:53 -0700627 "queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700628 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto out_fail;
630out_oversize:
631 if (net_ratelimit())
Harvey Harrison673d57e2008-10-31 00:53:57 -0700632 printk(KERN_INFO "Oversized IP packet from %pI4.\n",
633 &qp->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634out_fail:
David Fordbbf31bf2009-11-29 23:02:22 -0800635 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/* Process an incoming IP datagram fragment. */
Herbert Xu776c7292007-10-14 00:38:32 -0700640int ip_defrag(struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct ipq *qp;
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800643 struct net *net;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900644
Eric Dumazetadf30902009-06-02 05:19:30 +0000645 net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700646 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* Start by cleaning up the memory. */
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800649 if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -0800650 ip_evictor(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Lookup (or create) queue header */
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800653 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
Herbert Xu1706d582007-10-14 00:38:15 -0700654 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700656 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Herbert Xu1706d582007-10-14 00:38:15 -0700658 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700660 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700661 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700665 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700667 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000669EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800671#ifdef CONFIG_SYSCTL
672static int zero;
673
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700674static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800675 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800676 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800677 .data = &init_net.ipv4.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800678 .maxlen = sizeof(int),
679 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800680 .proc_handler = proc_dointvec
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800681 },
682 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800683 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800684 .data = &init_net.ipv4.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800685 .maxlen = sizeof(int),
686 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800687 .proc_handler = proc_dointvec
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800688 },
689 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800690 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800691 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800692 .maxlen = sizeof(int),
693 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800694 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800695 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700696 { }
697};
698
699static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800700 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800701 .procname = "ipfrag_secret_interval",
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800702 .data = &ip4_frags.secret_interval,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800703 .maxlen = sizeof(int),
704 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800705 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800706 },
707 {
708 .procname = "ipfrag_max_dist",
709 .data = &sysctl_ipfrag_max_dist,
710 .maxlen = sizeof(int),
711 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800712 .proc_handler = proc_dointvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800713 .extra1 = &zero
714 },
715 { }
716};
717
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000718static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800719{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800720 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800721 struct ctl_table_header *hdr;
722
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700723 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800724 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700725 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800726 if (table == NULL)
727 goto err_alloc;
728
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800729 table[0].data = &net->ipv4.frags.high_thresh;
730 table[1].data = &net->ipv4.frags.low_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800731 table[2].data = &net->ipv4.frags.timeout;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800732 }
733
734 hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
735 if (hdr == NULL)
736 goto err_reg;
737
738 net->ipv4.frags_hdr = hdr;
739 return 0;
740
741err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800742 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800743 kfree(table);
744err_alloc:
745 return -ENOMEM;
746}
747
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000748static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800749{
750 struct ctl_table *table;
751
752 table = net->ipv4.frags_hdr->ctl_table_arg;
753 unregister_net_sysctl_table(net->ipv4.frags_hdr);
754 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800755}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700756
757static void ip4_frags_ctl_register(void)
758{
759 register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table);
760}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800761#else
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700762static inline int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800763{
764 return 0;
765}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800766
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700767static inline void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800768{
769}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700770
771static inline void ip4_frags_ctl_register(void)
772{
773}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800774#endif
775
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000776static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800777{
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800778 /*
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800779 * Fragment cache limits. We will commit 256K at one time. Should we
780 * cross that limit we will prune down to 192K. This should cope with
781 * even the most extreme cases without allowing an attacker to
782 * measurably harm machine performance.
783 */
784 net->ipv4.frags.high_thresh = 256 * 1024;
785 net->ipv4.frags.low_thresh = 192 * 1024;
786 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800787 * Important NOTE! Fragment queue must be destroyed before MSL expires.
788 * RFC791 is wrong proposing to prolongate timer each fragment arrival
789 * by TTL.
790 */
791 net->ipv4.frags.timeout = IP_FRAG_TIME;
792
Pavel Emelyanove5a2bb8422008-01-22 06:06:23 -0800793 inet_frags_init_net(&net->ipv4.frags);
794
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700795 return ip4_frags_ns_ctl_register(net);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800796}
797
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000798static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800799{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700800 ip4_frags_ns_ctl_unregister(net);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800801 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
802}
803
804static struct pernet_operations ip4_frags_ops = {
805 .init = ipv4_frags_init_net,
806 .exit = ipv4_frags_exit_net,
807};
808
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700809void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700811 ip4_frags_ctl_register();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800812 register_pernet_subsys(&ip4_frags_ops);
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700813 ip4_frags.hashfn = ip4_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700814 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700815 ip4_frags.destructor = ip4_frag_free;
816 ip4_frags.skb_free = NULL;
817 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700818 ip4_frags.match = ip4_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700819 ip4_frags.frag_expire = ip_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800820 ip4_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700821 inet_frags_init(&ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}