blob: cb56bcc1eee2aa0e04cee60dde42b0573c3d5da4 [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
Joe Perchesafd465032012-03-12 07:03:32 +000023#define pr_fmt(fmt) "IPv4: " fmt
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Shan Weie9017b52010-01-23 01:57:42 -080038#include <net/route.h>
39#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/sock.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080044#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070045#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/tcp.h>
47#include <linux/udp.h>
48#include <linux/inet.h>
49#include <linux/netfilter_ipv4.h>
Eric Dumazet6623e3b2011-01-05 07:52:55 +000050#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
53 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
54 * as well. Or notify me, at least. --ANK
55 */
56
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -080057static int sysctl_ipfrag_max_dist __read_mostly = 64;
Herbert Xu89cee8b2005-12-13 23:14:27 -080058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ipfrag_skb_cb
60{
61 struct inet_skb_parm h;
62 int offset;
63};
64
Jianjun Kongfd3f8c42008-11-03 02:47:38 -080065#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Describe an entry in the "incomplete datagrams" queue. */
68struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070069 struct inet_frag_queue q;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 user;
Al Viro18277772006-09-26 22:19:02 -070072 __be32 saddr;
73 __be32 daddr;
74 __be16 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u8 protocol;
Eric Dumazet6623e3b2011-01-05 07:52:55 +000076 u8 ecn; /* RFC3168 support */
Herbert Xu89cee8b2005-12-13 23:14:27 -080077 int iif;
78 unsigned int rid;
79 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Eric Dumazet6623e3b2011-01-05 07:52:55 +000082static inline u8 ip4_frag_ecn(u8 tos)
83{
Eric Dumazet5173cc02011-05-16 08:37:37 +000084 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000085}
86
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070087static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -080089int ip_frag_mem(struct net *net)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070090{
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +000091 return sum_frag_mem_limit(&net->ipv4.frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070092}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Herbert Xu1706d582007-10-14 00:38:15 -070094static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
95 struct net_device *dev);
96
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070097struct ip4_create_arg {
98 struct iphdr *iph;
99 u32 user;
100};
101
Al Viro18277772006-09-26 22:19:02 -0700102static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Hannes Frederic Sowae7b519b2013-10-23 11:06:55 +0200104 net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
Al Viro18277772006-09-26 22:19:02 -0700105 return jhash_3words((__force u32)id << 16 | prot,
106 (__force u32)saddr, (__force u32)daddr,
Florian Westphalfb3cfe62014-07-24 16:50:30 +0200107 ip4_frags.rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Florian Westphal36c77782014-07-24 16:50:29 +0200110static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Florian Westphal36c77782014-07-24 16:50:29 +0200112 const struct ipq *ipq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700114 ipq = container_of(q, struct ipq, q);
115 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Florian Westphal36c77782014-07-24 16:50:29 +0200118static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700119{
Florian Westphal36c77782014-07-24 16:50:29 +0200120 const struct ipq *qp;
121 const struct ip4_create_arg *arg = a;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700122
123 qp = container_of(q, struct ipq, q);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000124 return qp->id == arg->iph->id &&
Eric Dumazetcbc264c2012-05-18 05:57:13 +0200125 qp->saddr == arg->iph->saddr &&
126 qp->daddr == arg->iph->daddr &&
127 qp->protocol == arg->iph->protocol &&
128 qp->user == arg->user;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700129}
130
Florian Westphal36c77782014-07-24 16:50:29 +0200131static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700132{
133 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +0000134 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
135 frags);
136 struct net *net = container_of(ipv4, struct net, ipv4);
137
Florian Westphal36c77782014-07-24 16:50:29 +0200138 const struct ip4_create_arg *arg = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700139
140 qp->protocol = arg->iph->protocol;
141 qp->id = arg->iph->id;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000142 qp->ecn = ip4_frag_ecn(arg->iph->tos);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700143 qp->saddr = arg->iph->saddr;
144 qp->daddr = arg->iph->daddr;
145 qp->user = arg->user;
146 qp->peer = sysctl_ipfrag_max_dist ?
David S. Millerc0efc882012-06-09 19:12:36 -0700147 inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700148}
149
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700150static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700152 struct ipq *qp;
153
154 qp = container_of(q, struct ipq, q);
155 if (qp->peer)
156 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/* Destruction primitives. */
161
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700162static __inline__ void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700164 inet_frag_put(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/* Kill ipq entry. It is not destroyed immediately,
168 * because caller (and someone more) holds reference count.
169 */
170static void ipq_kill(struct ipq *ipq)
171{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700172 inet_frag_kill(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
176 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
177 */
178static void ip_expire(unsigned long arg)
179{
Pavel Emelyanove521db92007-10-17 19:45:23 -0700180 struct ipq *qp;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700181 struct net *net;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700182
183 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700184 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700186 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200188 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
190
191 ipq_kill(qp);
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700192 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200194 if (!(qp->q.flags & INET_FRAG_EVICTED)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700195 struct sk_buff *head = qp->q.fragments;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000196 const struct iphdr *iph;
197 int err;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700198
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200199 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
200
201 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !qp->q.fragments)
202 goto out;
203
Eric Dumazet69df9d52009-11-05 20:59:47 -0800204 rcu_read_lock();
205 head->dev = dev_get_by_index_rcu(net, qp->iif);
Shan Weie9017b52010-01-23 01:57:42 -0800206 if (!head->dev)
207 goto out_rcu_unlock;
208
Eric Dumazet97599dc2013-04-16 12:55:41 +0000209 /* skb has no dst, perform route lookup again */
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000210 iph = ip_hdr(head);
David S. Millerc6cffba2012-07-26 11:14:38 +0000211 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
212 iph->tos, head->dev);
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000213 if (err)
214 goto out_rcu_unlock;
215
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200216 /* Only an end host needs to send an ICMP
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000217 * "Fragment Reassembly Timeout" message, per RFC792.
Shan Weie9017b52010-01-23 01:57:42 -0800218 */
David S. Miller595fc712011-07-05 01:05:48 -0700219 if (qp->user == IP_DEFRAG_AF_PACKET ||
Vasily Averin7c3d5ab12014-05-03 03:14:04 +0400220 ((qp->user >= IP_DEFRAG_CONNTRACK_IN) &&
221 (qp->user <= __IP_DEFRAG_CONNTRACK_IN_END) &&
222 (skb_rtable(head)->rt_type != RTN_LOCAL)))
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000223 goto out_rcu_unlock;
Shan Weie9017b52010-01-23 01:57:42 -0800224
Shan Weie9017b52010-01-23 01:57:42 -0800225 /* Send an ICMP "Fragment Reassembly Timeout" message. */
226 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
Shan Weie9017b52010-01-23 01:57:42 -0800227out_rcu_unlock:
Patrick McHardyd1c9ae62010-02-02 11:46:50 -0800228 rcu_read_unlock();
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700231 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700232 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700235/* Find the correct entry in the "incomplete datagrams" queue for
236 * this IP datagram, and create new one, if nothing is found.
237 */
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800238static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700240 struct inet_frag_queue *q;
241 struct ip4_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700242 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700244 arg.iph = iph;
245 arg.user = user;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700246
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700247 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700248
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800249 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000250 if (IS_ERR_OR_NULL(q)) {
251 inet_frag_maybe_warn_overflow(q, pr_fmt());
252 return NULL;
253 }
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700254 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Herbert Xu89cee8b2005-12-13 23:14:27 -0800257/* Is the fragment too far ahead to be part of ipq? */
258static inline int ip_frag_too_far(struct ipq *qp)
259{
260 struct inet_peer *peer = qp->peer;
261 unsigned int max = sysctl_ipfrag_max_dist;
262 unsigned int start, end;
263
264 int rc;
265
266 if (!peer || !max)
267 return 0;
268
269 start = qp->rid;
270 end = atomic_inc_return(&peer->rid);
271 qp->rid = end;
272
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700273 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800274
275 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700276 struct net *net;
277
278 net = container_of(qp->q.net, struct net, ipv4.frags);
279 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800280 }
281
282 return rc;
283}
284
285static int ip_frag_reinit(struct ipq *qp)
286{
287 struct sk_buff *fp;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000288 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800289
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800290 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700291 atomic_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800292 return -ETIMEDOUT;
293 }
294
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700295 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800296 do {
297 struct sk_buff *xp = fp->next;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000298
299 sum_truesize += fp->truesize;
300 kfree_skb(fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800301 fp = xp;
302 } while (fp);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000303 sub_frag_mem_limit(&qp->q, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800304
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200305 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700306 qp->q.len = 0;
307 qp->q.meat = 0;
308 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000309 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800310 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000311 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800312
313 return 0;
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700317static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700320 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int flags, offset;
322 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700323 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000324 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200326 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto err;
328
Herbert Xu89cee8b2005-12-13 23:14:27 -0800329 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700330 unlikely(ip_frag_too_far(qp)) &&
331 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800332 ipq_kill(qp);
333 goto err;
334 }
335
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000336 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700337 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 flags = offset & ~IP_OFFSET;
339 offset &= IP_OFFSET;
340 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300341 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Determine the position of this fragment. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900344 end = offset + skb->len - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700345 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* Is this the final fragment? */
348 if ((flags & IP_MF) == 0) {
349 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800350 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200353 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200355 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700356 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 } else {
358 if (end&7) {
359 end &= ~7;
360 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
361 skb->ip_summed = CHECKSUM_NONE;
362 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700363 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200365 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700367 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
370 if (end == offset)
371 goto err;
372
Herbert Xu1706d582007-10-14 00:38:15 -0700373 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (pskb_pull(skb, ihl) == NULL)
375 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700376
377 err = pskb_trim_rcsum(skb, end - offset);
378 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto err;
380
381 /* Find out which fragments are in front and at the back of us
382 * in the chain of fragments so far. We must know where to put
383 * this fragment, right?
384 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000385 prev = qp->q.fragments_tail;
386 if (!prev || FRAG_CB(prev)->offset < offset) {
387 next = NULL;
388 goto found;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700391 for (next = qp->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (FRAG_CB(next)->offset >= offset)
393 break; /* bingo! */
394 prev = next;
395 }
396
Changli Gaod6bebca2010-06-29 04:39:37 +0000397found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* We found where to put this one. Check for overlap with
399 * preceding fragment, and, if needed, align things so that
400 * any overlaps are eliminated.
401 */
402 if (prev) {
403 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
404
405 if (i > 0) {
406 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700407 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (end <= offset)
409 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700410 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!pskb_pull(skb, i))
412 goto err;
413 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
414 skb->ip_summed = CHECKSUM_NONE;
415 }
416 }
417
Herbert Xu1706d582007-10-14 00:38:15 -0700418 err = -ENOMEM;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 while (next && FRAG_CB(next)->offset < end) {
421 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
422
423 if (i < next->len) {
424 /* Eat head of the next overlapped fragment
425 * and leave the loop. The next ones cannot overlap.
426 */
427 if (!pskb_pull(next, i))
428 goto err;
429 FRAG_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700430 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (next->ip_summed != CHECKSUM_UNNECESSARY)
432 next->ip_summed = CHECKSUM_NONE;
433 break;
434 } else {
435 struct sk_buff *free_it = next;
436
Peter Zijlstra47c6bf772006-12-12 19:48:59 +0100437 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * new one drop it.
439 */
440 next = next->next;
441
442 if (prev)
443 prev->next = next;
444 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700445 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700447 qp->q.meat -= free_it->len;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000448 sub_frag_mem_limit(&qp->q, free_it->truesize);
449 kfree_skb(free_it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 }
452
453 FRAG_CB(skb)->offset = offset;
454
455 /* Insert this fragment in the chain of fragments. */
456 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000457 if (!next)
458 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (prev)
460 prev->next = skb;
461 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700462 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Herbert Xu1706d582007-10-14 00:38:15 -0700464 dev = skb->dev;
465 if (dev) {
466 qp->iif = dev->ifindex;
467 skb->dev = NULL;
468 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700469 qp->q.stamp = skb->tstamp;
470 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000471 qp->ecn |= ecn;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000472 add_frag_mem_limit(&qp->q, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200474 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200476 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
477 skb->len + ihl > qp->q.max_size)
478 qp->q.max_size = skb->len + ihl;
479
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200480 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000481 qp->q.meat == qp->q.len) {
482 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700483
Eric Dumazet97599dc2013-04-16 12:55:41 +0000484 skb->_skb_refdst = 0UL;
485 err = ip_frag_reasm(qp, prev, dev);
486 skb->_skb_refdst = orefdst;
487 return err;
488 }
489
490 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700491 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493err:
494 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498
499/* Build a new IP datagram from all its fragments. */
500
Herbert Xu1706d582007-10-14 00:38:15 -0700501static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
502 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700504 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700506 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int len;
508 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700509 int err;
Eric Dumazet3cc49492012-05-19 03:02:20 +0000510 int sum_truesize;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000511 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 ipq_kill(qp);
514
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000515 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000516 if (unlikely(ecn == 0xff)) {
517 err = -EINVAL;
518 goto out_fail;
519 }
Herbert Xu1706d582007-10-14 00:38:15 -0700520 /* Make the one we just received the head. */
521 if (prev) {
522 head = prev->next;
523 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700524 if (!fp)
525 goto out_nomem;
526
527 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000528 if (!fp->next)
529 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700530 prev->next = fp;
531
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700532 skb_morph(head, qp->q.fragments);
533 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700534
Eric Dumazetcbf8f7b2012-04-19 06:10:26 +0000535 consume_skb(qp->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700536 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700537 }
538
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700539 WARN_ON(head == NULL);
540 WARN_ON(FRAG_CB(head)->offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300543 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700544 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Herbert Xu1706d582007-10-14 00:38:15 -0700546 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800547 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto out_oversize;
549
550 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000551 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out_nomem;
553
554 /* If the first fragment is fragmented itself, we split
555 * it to two chunks: the first with data and paged part
556 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700557 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct sk_buff *clone;
559 int i, plen = 0;
560
561 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
562 goto out_nomem;
563 clone->next = head->next;
564 head->next = clone;
565 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700566 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000567 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
568 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 clone->len = clone->data_len = head->data_len - plen;
570 head->data_len -= clone->len;
571 head->len -= clone->len;
572 clone->csum = 0;
573 clone->ip_summed = head->ip_summed;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000574 add_frag_mem_limit(&qp->q, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700577 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Dumazet3cc49492012-05-19 03:02:20 +0000579 sum_truesize = head->truesize;
580 for (fp = head->next; fp;) {
581 bool headstolen;
582 int delta;
583 struct sk_buff *next = fp->next;
584
585 sum_truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (head->ip_summed != fp->ip_summed)
587 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700588 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 head->csum = csum_add(head->csum, fp->csum);
Eric Dumazet3cc49492012-05-19 03:02:20 +0000590
591 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
592 kfree_skb_partial(fp, headstolen);
593 } else {
594 if (!skb_shinfo(head)->frag_list)
595 skb_shinfo(head)->frag_list = fp;
596 head->data_len += fp->len;
597 head->len += fp->len;
598 head->truesize += fp->truesize;
599 }
600 fp = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000602 sub_frag_mem_limit(&qp->q, sum_truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 head->next = NULL;
605 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700606 head->tstamp = qp->q.stamp;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200607 IPCB(head)->frag_max_size = qp->q.max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700609 iph = ip_hdr(head);
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200610 /* max_size != 0 implies at least one fragment had IP_DF set */
611 iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000613 iph->tos |= ecn;
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700614 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700615 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000616 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619out_nomem:
Joe Perchesafd465032012-03-12 07:03:32 +0000620 LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"),
621 qp);
David Howells45542472007-10-17 21:37:22 -0700622 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto out_fail;
624out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000625 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626out_fail:
David Fordbbf31bf2009-11-29 23:02:22 -0800627 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* Process an incoming IP datagram fragment. */
Herbert Xu776c7292007-10-14 00:38:32 -0700632int ip_defrag(struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct ipq *qp;
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800635 struct net *net;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900636
Eric Dumazetadf30902009-06-02 05:19:30 +0000637 net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700638 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Lookup (or create) queue header */
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800641 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
Herbert Xu1706d582007-10-14 00:38:15 -0700642 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700644 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Herbert Xu1706d582007-10-14 00:38:15 -0700646 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700648 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700649 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700653 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000657EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric Dumazetbc416d92011-10-06 10:28:31 +0000659struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user)
660{
Johannes Berg1bf37512012-12-09 23:41:06 +0000661 struct iphdr iph;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000662 u32 len;
663
664 if (skb->protocol != htons(ETH_P_IP))
665 return skb;
666
Johannes Berg1bf37512012-12-09 23:41:06 +0000667 if (!skb_copy_bits(skb, 0, &iph, sizeof(iph)))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000668 return skb;
669
Johannes Berg1bf37512012-12-09 23:41:06 +0000670 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000671 return skb;
672
Johannes Berg1bf37512012-12-09 23:41:06 +0000673 len = ntohs(iph.tot_len);
674 if (skb->len < len || len < (iph.ihl * 4))
675 return skb;
676
677 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000678 skb = skb_share_check(skb, GFP_ATOMIC);
679 if (skb) {
Johannes Berg1bf37512012-12-09 23:41:06 +0000680 if (!pskb_may_pull(skb, iph.ihl*4))
681 return skb;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000682 if (pskb_trim_rcsum(skb, len))
683 return skb;
684 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
685 if (ip_defrag(skb, user))
686 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800687 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000688 }
689 }
690 return skb;
691}
692EXPORT_SYMBOL(ip_check_defrag);
693
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800694#ifdef CONFIG_SYSCTL
695static int zero;
696
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700697static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800698 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800699 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800700 .data = &init_net.ipv4.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800701 .maxlen = sizeof(int),
702 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200703 .proc_handler = proc_dointvec_minmax,
704 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800705 },
706 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800707 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800708 .data = &init_net.ipv4.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800709 .maxlen = sizeof(int),
710 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200711 .proc_handler = proc_dointvec_minmax,
712 .extra1 = &zero,
713 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800714 },
715 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800716 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800717 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800718 .maxlen = sizeof(int),
719 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800720 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800721 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700722 { }
723};
724
Florian Westphale3a57d12014-07-24 16:50:35 +0200725/* secret interval has been deprecated */
726static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700727static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800728 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800729 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200730 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800731 .maxlen = sizeof(int),
732 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800733 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800734 },
735 {
736 .procname = "ipfrag_max_dist",
737 .data = &sysctl_ipfrag_max_dist,
738 .maxlen = sizeof(int),
739 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800740 .proc_handler = proc_dointvec_minmax,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800741 .extra1 = &zero
742 },
743 { }
744};
745
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000746static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800747{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800748 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800749 struct ctl_table_header *hdr;
750
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700751 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800752 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700753 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800754 if (table == NULL)
755 goto err_alloc;
756
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800757 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200758 table[0].extra1 = &net->ipv4.frags.low_thresh;
759 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800760 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200761 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800762 table[2].data = &net->ipv4.frags.timeout;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000763
764 /* Don't export sysctls to unprivileged users */
765 if (net->user_ns != &init_user_ns)
766 table[0].procname = NULL;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800767 }
768
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000769 hdr = register_net_sysctl(net, "net/ipv4", table);
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800770 if (hdr == NULL)
771 goto err_reg;
772
773 net->ipv4.frags_hdr = hdr;
774 return 0;
775
776err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800777 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800778 kfree(table);
779err_alloc:
780 return -ENOMEM;
781}
782
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000783static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800784{
785 struct ctl_table *table;
786
787 table = net->ipv4.frags_hdr->ctl_table_arg;
788 unregister_net_sysctl_table(net->ipv4.frags_hdr);
789 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800790}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700791
792static void ip4_frags_ctl_register(void)
793{
Eric W. Biederman43444752012-04-19 13:22:55 +0000794 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700795}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800796#else
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700797static inline int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800798{
799 return 0;
800}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800801
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700802static inline void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800803{
804}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700805
806static inline void ip4_frags_ctl_register(void)
807{
808}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800809#endif
810
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000811static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800812{
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000813 /* Fragment cache limits.
814 *
815 * The fragment memory accounting code, (tries to) account for
816 * the real memory usage, by measuring both the size of frag
817 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
818 * and the SKB's truesize.
819 *
820 * A 64K fragment consumes 129736 bytes (44*2944)+200
821 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
822 *
823 * We will commit 4MB at one time. Should we cross that limit
824 * we will prune down to 3MB, making room for approx 8 big 64K
825 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800826 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000827 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
828 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800829 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800830 * Important NOTE! Fragment queue must be destroyed before MSL expires.
831 * RFC791 is wrong proposing to prolongate timer each fragment arrival
832 * by TTL.
833 */
834 net->ipv4.frags.timeout = IP_FRAG_TIME;
835
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800836 inet_frags_init_net(&net->ipv4.frags);
837
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700838 return ip4_frags_ns_ctl_register(net);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800839}
840
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000841static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800842{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700843 ip4_frags_ns_ctl_unregister(net);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800844 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
845}
846
847static struct pernet_operations ip4_frags_ops = {
848 .init = ipv4_frags_init_net,
849 .exit = ipv4_frags_exit_net,
850};
851
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700852void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700854 ip4_frags_ctl_register();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800855 register_pernet_subsys(&ip4_frags_ops);
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700856 ip4_frags.hashfn = ip4_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700857 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700858 ip4_frags.destructor = ip4_frag_free;
859 ip4_frags.skb_free = NULL;
860 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700861 ip4_frags.match = ip4_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700862 ip4_frags.frag_expire = ip_expire;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700863 inet_frags_init(&ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}