| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * INET		An implementation of the TCP/IP protocol suite for the LINUX | 
|---|
| 4 | *		operating system.  INET is implemented using the  BSD Socket | 
|---|
| 5 | *		interface as the means of communication with the user level. | 
|---|
| 6 | * | 
|---|
| 7 | *		The IP fragmentation functionality. | 
|---|
| 8 | * | 
|---|
| 9 | * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> | 
|---|
| 10 | *		Alan Cox <alan@lxorguk.ukuu.org.uk> | 
|---|
| 11 | * | 
|---|
| 12 | * Fixes: | 
|---|
| 13 | *		Alan Cox	:	Split from ip.c , see ip_input.c for history. | 
|---|
| 14 | *		David S. Miller :	Begin massive cleanup... | 
|---|
| 15 | *		Andi Kleen	:	Add sysctls. | 
|---|
| 16 | *		xxxx		:	Overlapfrag bug. | 
|---|
| 17 | *		Ultima          :       ip_expire() kernel panic. | 
|---|
| 18 | *		Bill Hawes	:	Frag accounting and evictor fixes. | 
|---|
| 19 | *		John McDonald	:	0 length frag bug. | 
|---|
| 20 | *		Alexey Kuznetsov:	SMP races, threading, cleanup. | 
|---|
| 21 | *		Patrick McHardy :	LRU queue of frag heads for evictor. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #define pr_fmt(fmt) "IPv4: " fmt | 
|---|
| 25 |  | 
|---|
| 26 | #include <linux/compiler.h> | 
|---|
| 27 | #include <linux/module.h> | 
|---|
| 28 | #include <linux/types.h> | 
|---|
| 29 | #include <linux/mm.h> | 
|---|
| 30 | #include <linux/jiffies.h> | 
|---|
| 31 | #include <linux/skbuff.h> | 
|---|
| 32 | #include <linux/list.h> | 
|---|
| 33 | #include <linux/ip.h> | 
|---|
| 34 | #include <linux/icmp.h> | 
|---|
| 35 | #include <linux/netdevice.h> | 
|---|
| 36 | #include <linux/jhash.h> | 
|---|
| 37 | #include <linux/random.h> | 
|---|
| 38 | #include <linux/slab.h> | 
|---|
| 39 | #include <net/route.h> | 
|---|
| 40 | #include <net/dst.h> | 
|---|
| 41 | #include <net/sock.h> | 
|---|
| 42 | #include <net/ip.h> | 
|---|
| 43 | #include <net/icmp.h> | 
|---|
| 44 | #include <net/checksum.h> | 
|---|
| 45 | #include <net/inetpeer.h> | 
|---|
| 46 | #include <net/inet_frag.h> | 
|---|
| 47 | #include <linux/tcp.h> | 
|---|
| 48 | #include <linux/udp.h> | 
|---|
| 49 | #include <linux/inet.h> | 
|---|
| 50 | #include <linux/netfilter_ipv4.h> | 
|---|
| 51 | #include <net/inet_ecn.h> | 
|---|
| 52 | #include <net/l3mdev.h> | 
|---|
| 53 |  | 
|---|
| 54 | /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 | 
|---|
| 55 | * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c | 
|---|
| 56 | * as well. Or notify me, at least. --ANK | 
|---|
| 57 | */ | 
|---|
| 58 | static const char ip_frag_cache_name[] = "ip4-frags"; | 
|---|
| 59 |  | 
|---|
| 60 | /* Describe an entry in the "incomplete datagrams" queue. */ | 
|---|
| 61 | struct ipq { | 
|---|
| 62 | struct inet_frag_queue q; | 
|---|
| 63 |  | 
|---|
| 64 | u8		ecn; /* RFC3168 support */ | 
|---|
| 65 | u16		max_df_size; /* largest frag with DF set seen */ | 
|---|
| 66 | int             iif; | 
|---|
| 67 | unsigned int    rid; | 
|---|
| 68 | struct inet_peer *peer; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | static u8 ip4_frag_ecn(u8 tos) | 
|---|
| 72 | { | 
|---|
| 73 | return 1 << (tos & INET_ECN_MASK); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | static struct inet_frags ip4_frags; | 
|---|
| 77 |  | 
|---|
| 78 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, | 
|---|
| 79 | struct sk_buff *prev_tail, struct net_device *dev, | 
|---|
| 80 | int *refs); | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | static void ip4_frag_init(struct inet_frag_queue *q, const void *a) | 
|---|
| 84 | { | 
|---|
| 85 | struct ipq *qp = container_of(q, struct ipq, q); | 
|---|
| 86 | const struct frag_v4_compare_key *key = a; | 
|---|
| 87 | struct net *net = q->fqdir->net; | 
|---|
| 88 | struct inet_peer *p = NULL; | 
|---|
| 89 |  | 
|---|
| 90 | q->key.v4 = *key; | 
|---|
| 91 | qp->ecn = 0; | 
|---|
| 92 | if (q->fqdir->max_dist) { | 
|---|
| 93 | rcu_read_lock(); | 
|---|
| 94 | p = inet_getpeer_v4(base: net->ipv4.peers, v4daddr: key->saddr, vif: key->vif); | 
|---|
| 95 | if (p && !refcount_inc_not_zero(r: &p->refcnt)) | 
|---|
| 96 | p = NULL; | 
|---|
| 97 | rcu_read_unlock(); | 
|---|
| 98 | } | 
|---|
| 99 | qp->peer = p; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | static void ip4_frag_free(struct inet_frag_queue *q) | 
|---|
| 103 | { | 
|---|
| 104 | struct ipq *qp; | 
|---|
| 105 |  | 
|---|
| 106 | qp = container_of(q, struct ipq, q); | 
|---|
| 107 | if (qp->peer) | 
|---|
| 108 | inet_putpeer(p: qp->peer); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | static bool frag_expire_skip_icmp(u32 user) | 
|---|
| 112 | { | 
|---|
| 113 | return user == IP_DEFRAG_AF_PACKET || | 
|---|
| 114 | ip_defrag_user_in_between(user, lower_bond: IP_DEFRAG_CONNTRACK_IN, | 
|---|
| 115 | upper_bond: __IP_DEFRAG_CONNTRACK_IN_END) || | 
|---|
| 116 | ip_defrag_user_in_between(user, lower_bond: IP_DEFRAG_CONNTRACK_BRIDGE_IN, | 
|---|
| 117 | upper_bond: __IP_DEFRAG_CONNTRACK_BRIDGE_IN); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | /* | 
|---|
| 121 | * Oops, a fragment queue timed out.  Kill it and send an ICMP reply. | 
|---|
| 122 | */ | 
|---|
| 123 | static void ip_expire(struct timer_list *t) | 
|---|
| 124 | { | 
|---|
| 125 | enum skb_drop_reason reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT; | 
|---|
| 126 | struct inet_frag_queue *frag = timer_container_of(frag, t, timer); | 
|---|
| 127 | const struct iphdr *iph; | 
|---|
| 128 | struct sk_buff *head = NULL; | 
|---|
| 129 | struct net *net; | 
|---|
| 130 | struct ipq *qp; | 
|---|
| 131 | int refs = 1; | 
|---|
| 132 |  | 
|---|
| 133 | qp = container_of(frag, struct ipq, q); | 
|---|
| 134 | net = qp->q.fqdir->net; | 
|---|
| 135 |  | 
|---|
| 136 | rcu_read_lock(); | 
|---|
| 137 |  | 
|---|
| 138 | /* Paired with WRITE_ONCE() in fqdir_pre_exit(). */ | 
|---|
| 139 | if (READ_ONCE(qp->q.fqdir->dead)) | 
|---|
| 140 | goto out_rcu_unlock; | 
|---|
| 141 |  | 
|---|
| 142 | spin_lock(lock: &qp->q.lock); | 
|---|
| 143 |  | 
|---|
| 144 | if (qp->q.flags & INET_FRAG_COMPLETE) | 
|---|
| 145 | goto out; | 
|---|
| 146 |  | 
|---|
| 147 | qp->q.flags |= INET_FRAG_DROP; | 
|---|
| 148 | inet_frag_kill(q: &qp->q, refs: &refs); | 
|---|
| 149 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); | 
|---|
| 150 | __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); | 
|---|
| 151 |  | 
|---|
| 152 | if (!(qp->q.flags & INET_FRAG_FIRST_IN)) | 
|---|
| 153 | goto out; | 
|---|
| 154 |  | 
|---|
| 155 | /* sk_buff::dev and sk_buff::rbnode are unionized. So we | 
|---|
| 156 | * pull the head out of the tree in order to be able to | 
|---|
| 157 | * deal with head->dev. | 
|---|
| 158 | */ | 
|---|
| 159 | head = inet_frag_pull_head(q: &qp->q); | 
|---|
| 160 | if (!head) | 
|---|
| 161 | goto out; | 
|---|
| 162 | head->dev = dev_get_by_index_rcu(net, ifindex: qp->iif); | 
|---|
| 163 | if (!head->dev) | 
|---|
| 164 | goto out; | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | /* skb has no dst, perform route lookup again */ | 
|---|
| 168 | iph = ip_hdr(skb: head); | 
|---|
| 169 | reason = ip_route_input_noref(skb: head, daddr: iph->daddr, saddr: iph->saddr, | 
|---|
| 170 | dscp: ip4h_dscp(ip4h: iph), dev: head->dev); | 
|---|
| 171 | if (reason) | 
|---|
| 172 | goto out; | 
|---|
| 173 |  | 
|---|
| 174 | /* Only an end host needs to send an ICMP | 
|---|
| 175 | * "Fragment Reassembly Timeout" message, per RFC792. | 
|---|
| 176 | */ | 
|---|
| 177 | reason = SKB_DROP_REASON_FRAG_REASM_TIMEOUT; | 
|---|
| 178 | if (frag_expire_skip_icmp(user: qp->q.key.v4.user) && | 
|---|
| 179 | (skb_rtable(skb: head)->rt_type != RTN_LOCAL)) | 
|---|
| 180 | goto out; | 
|---|
| 181 |  | 
|---|
| 182 | spin_unlock(lock: &qp->q.lock); | 
|---|
| 183 | icmp_send(skb_in: head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, info: 0); | 
|---|
| 184 | goto out_rcu_unlock; | 
|---|
| 185 |  | 
|---|
| 186 | out: | 
|---|
| 187 | spin_unlock(lock: &qp->q.lock); | 
|---|
| 188 | out_rcu_unlock: | 
|---|
| 189 | rcu_read_unlock(); | 
|---|
| 190 | kfree_skb_reason(skb: head, reason); | 
|---|
| 191 | inet_frag_putn(q: &qp->q, refs); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | /* Find the correct entry in the "incomplete datagrams" queue for | 
|---|
| 195 | * this IP datagram, and create new one, if nothing is found. | 
|---|
| 196 | */ | 
|---|
| 197 | static struct ipq *ip_find(struct net *net, struct iphdr *iph, | 
|---|
| 198 | u32 user, int vif) | 
|---|
| 199 | { | 
|---|
| 200 | struct frag_v4_compare_key key = { | 
|---|
| 201 | .saddr = iph->saddr, | 
|---|
| 202 | .daddr = iph->daddr, | 
|---|
| 203 | .user = user, | 
|---|
| 204 | .vif = vif, | 
|---|
| 205 | .id = iph->id, | 
|---|
| 206 | .protocol = iph->protocol, | 
|---|
| 207 | }; | 
|---|
| 208 | struct inet_frag_queue *q; | 
|---|
| 209 |  | 
|---|
| 210 | q = inet_frag_find(fqdir: net->ipv4.fqdir, key: &key); | 
|---|
| 211 | if (!q) | 
|---|
| 212 | return NULL; | 
|---|
| 213 |  | 
|---|
| 214 | return container_of(q, struct ipq, q); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | /* Is the fragment too far ahead to be part of ipq? */ | 
|---|
| 218 | static int ip_frag_too_far(struct ipq *qp) | 
|---|
| 219 | { | 
|---|
| 220 | struct inet_peer *peer = qp->peer; | 
|---|
| 221 | unsigned int max = qp->q.fqdir->max_dist; | 
|---|
| 222 | unsigned int start, end; | 
|---|
| 223 |  | 
|---|
| 224 | int rc; | 
|---|
| 225 |  | 
|---|
| 226 | if (!peer || !max) | 
|---|
| 227 | return 0; | 
|---|
| 228 |  | 
|---|
| 229 | start = qp->rid; | 
|---|
| 230 | end = atomic_inc_return(v: &peer->rid); | 
|---|
| 231 | qp->rid = end; | 
|---|
| 232 |  | 
|---|
| 233 | rc = qp->q.fragments_tail && (end - start) > max; | 
|---|
| 234 |  | 
|---|
| 235 | if (rc) | 
|---|
| 236 | __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS); | 
|---|
| 237 |  | 
|---|
| 238 | return rc; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | static int ip_frag_reinit(struct ipq *qp) | 
|---|
| 242 | { | 
|---|
| 243 | unsigned int sum_truesize = 0; | 
|---|
| 244 |  | 
|---|
| 245 | if (!mod_timer(timer: &qp->q.timer, expires: jiffies + qp->q.fqdir->timeout)) { | 
|---|
| 246 | refcount_inc(r: &qp->q.refcnt); | 
|---|
| 247 | return -ETIMEDOUT; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | sum_truesize = inet_frag_rbtree_purge(root: &qp->q.rb_fragments, | 
|---|
| 251 | reason: SKB_DROP_REASON_FRAG_TOO_FAR); | 
|---|
| 252 | sub_frag_mem_limit(fqdir: qp->q.fqdir, val: sum_truesize); | 
|---|
| 253 |  | 
|---|
| 254 | qp->q.flags = 0; | 
|---|
| 255 | qp->q.len = 0; | 
|---|
| 256 | qp->q.meat = 0; | 
|---|
| 257 | qp->q.rb_fragments = RB_ROOT; | 
|---|
| 258 | qp->q.fragments_tail = NULL; | 
|---|
| 259 | qp->q.last_run_head = NULL; | 
|---|
| 260 | qp->iif = 0; | 
|---|
| 261 | qp->ecn = 0; | 
|---|
| 262 |  | 
|---|
| 263 | return 0; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | /* Add new segment to existing queue. */ | 
|---|
| 267 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb, int *refs) | 
|---|
| 268 | { | 
|---|
| 269 | struct net *net = qp->q.fqdir->net; | 
|---|
| 270 | int ihl, end, flags, offset; | 
|---|
| 271 | struct sk_buff *prev_tail; | 
|---|
| 272 | struct net_device *dev; | 
|---|
| 273 | unsigned int fragsize; | 
|---|
| 274 | int err = -ENOENT; | 
|---|
| 275 | SKB_DR(reason); | 
|---|
| 276 | u8 ecn; | 
|---|
| 277 |  | 
|---|
| 278 | /* If reassembly is already done, @skb must be a duplicate frag. */ | 
|---|
| 279 | if (qp->q.flags & INET_FRAG_COMPLETE) { | 
|---|
| 280 | SKB_DR_SET(reason, DUP_FRAG); | 
|---|
| 281 | goto err; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && | 
|---|
| 285 | unlikely(ip_frag_too_far(qp)) && | 
|---|
| 286 | unlikely(err = ip_frag_reinit(qp))) { | 
|---|
| 287 | inet_frag_kill(q: &qp->q, refs); | 
|---|
| 288 | goto err; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | ecn = ip4_frag_ecn(tos: ip_hdr(skb)->tos); | 
|---|
| 292 | offset = ntohs(ip_hdr(skb)->frag_off); | 
|---|
| 293 | flags = offset & ~IP_OFFSET; | 
|---|
| 294 | offset &= IP_OFFSET; | 
|---|
| 295 | offset <<= 3;		/* offset is in 8-byte chunks */ | 
|---|
| 296 | ihl = ip_hdrlen(skb); | 
|---|
| 297 |  | 
|---|
| 298 | /* Determine the position of this fragment. */ | 
|---|
| 299 | end = offset + skb->len - skb_network_offset(skb) - ihl; | 
|---|
| 300 | err = -EINVAL; | 
|---|
| 301 |  | 
|---|
| 302 | /* Is this the final fragment? */ | 
|---|
| 303 | if ((flags & IP_MF) == 0) { | 
|---|
| 304 | /* If we already have some bits beyond end | 
|---|
| 305 | * or have different end, the segment is corrupted. | 
|---|
| 306 | */ | 
|---|
| 307 | if (end < qp->q.len || | 
|---|
| 308 | ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) | 
|---|
| 309 | goto discard_qp; | 
|---|
| 310 | qp->q.flags |= INET_FRAG_LAST_IN; | 
|---|
| 311 | qp->q.len = end; | 
|---|
| 312 | } else { | 
|---|
| 313 | if (end&7) { | 
|---|
| 314 | end &= ~7; | 
|---|
| 315 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | 
|---|
| 316 | skb->ip_summed = CHECKSUM_NONE; | 
|---|
| 317 | } | 
|---|
| 318 | if (end > qp->q.len) { | 
|---|
| 319 | /* Some bits beyond end -> corruption. */ | 
|---|
| 320 | if (qp->q.flags & INET_FRAG_LAST_IN) | 
|---|
| 321 | goto discard_qp; | 
|---|
| 322 | qp->q.len = end; | 
|---|
| 323 | } | 
|---|
| 324 | } | 
|---|
| 325 | if (end == offset) | 
|---|
| 326 | goto discard_qp; | 
|---|
| 327 |  | 
|---|
| 328 | err = -ENOMEM; | 
|---|
| 329 | if (!pskb_pull(skb, len: skb_network_offset(skb) + ihl)) | 
|---|
| 330 | goto discard_qp; | 
|---|
| 331 |  | 
|---|
| 332 | err = pskb_trim_rcsum(skb, len: end - offset); | 
|---|
| 333 | if (err) | 
|---|
| 334 | goto discard_qp; | 
|---|
| 335 |  | 
|---|
| 336 | /* Note : skb->rbnode and skb->dev share the same location. */ | 
|---|
| 337 | dev = skb->dev; | 
|---|
| 338 | /* Makes sure compiler wont do silly aliasing games */ | 
|---|
| 339 | barrier(); | 
|---|
| 340 |  | 
|---|
| 341 | prev_tail = qp->q.fragments_tail; | 
|---|
| 342 | err = inet_frag_queue_insert(q: &qp->q, skb, offset, end); | 
|---|
| 343 | if (err) | 
|---|
| 344 | goto insert_error; | 
|---|
| 345 |  | 
|---|
| 346 | if (dev) | 
|---|
| 347 | qp->iif = dev->ifindex; | 
|---|
| 348 |  | 
|---|
| 349 | qp->q.stamp = skb->tstamp; | 
|---|
| 350 | qp->q.tstamp_type = skb->tstamp_type; | 
|---|
| 351 | qp->q.meat += skb->len; | 
|---|
| 352 | qp->ecn |= ecn; | 
|---|
| 353 | add_frag_mem_limit(fqdir: qp->q.fqdir, val: skb->truesize); | 
|---|
| 354 | if (offset == 0) | 
|---|
| 355 | qp->q.flags |= INET_FRAG_FIRST_IN; | 
|---|
| 356 |  | 
|---|
| 357 | fragsize = skb->len + ihl; | 
|---|
| 358 |  | 
|---|
| 359 | if (fragsize > qp->q.max_size) | 
|---|
| 360 | qp->q.max_size = fragsize; | 
|---|
| 361 |  | 
|---|
| 362 | if (ip_hdr(skb)->frag_off & htons(IP_DF) && | 
|---|
| 363 | fragsize > qp->max_df_size) | 
|---|
| 364 | qp->max_df_size = fragsize; | 
|---|
| 365 |  | 
|---|
| 366 | if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && | 
|---|
| 367 | qp->q.meat == qp->q.len) { | 
|---|
| 368 | unsigned long orefdst = skb->_skb_refdst; | 
|---|
| 369 |  | 
|---|
| 370 | skb->_skb_refdst = 0UL; | 
|---|
| 371 | err = ip_frag_reasm(qp, skb, prev_tail, dev, refs); | 
|---|
| 372 | skb->_skb_refdst = orefdst; | 
|---|
| 373 | if (err) | 
|---|
| 374 | inet_frag_kill(q: &qp->q, refs); | 
|---|
| 375 | return err; | 
|---|
| 376 | } | 
|---|
| 377 |  | 
|---|
| 378 | skb_dst_drop(skb); | 
|---|
| 379 | skb_orphan(skb); | 
|---|
| 380 | return -EINPROGRESS; | 
|---|
| 381 |  | 
|---|
| 382 | insert_error: | 
|---|
| 383 | if (err == IPFRAG_DUP) { | 
|---|
| 384 | SKB_DR_SET(reason, DUP_FRAG); | 
|---|
| 385 | err = -EINVAL; | 
|---|
| 386 | goto err; | 
|---|
| 387 | } | 
|---|
| 388 | err = -EINVAL; | 
|---|
| 389 | __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS); | 
|---|
| 390 | discard_qp: | 
|---|
| 391 | inet_frag_kill(q: &qp->q, refs); | 
|---|
| 392 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); | 
|---|
| 393 | err: | 
|---|
| 394 | kfree_skb_reason(skb, reason); | 
|---|
| 395 | return err; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | static bool ip_frag_coalesce_ok(const struct ipq *qp) | 
|---|
| 399 | { | 
|---|
| 400 | return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER; | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | /* Build a new IP datagram from all its fragments. */ | 
|---|
| 404 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb, | 
|---|
| 405 | struct sk_buff *prev_tail, struct net_device *dev, | 
|---|
| 406 | int *refs) | 
|---|
| 407 | { | 
|---|
| 408 | struct net *net = qp->q.fqdir->net; | 
|---|
| 409 | struct iphdr *iph; | 
|---|
| 410 | void *reasm_data; | 
|---|
| 411 | int len, err; | 
|---|
| 412 | u8 ecn; | 
|---|
| 413 |  | 
|---|
| 414 | inet_frag_kill(q: &qp->q, refs); | 
|---|
| 415 |  | 
|---|
| 416 | ecn = ip_frag_ecn_table[qp->ecn]; | 
|---|
| 417 | if (unlikely(ecn == 0xff)) { | 
|---|
| 418 | err = -EINVAL; | 
|---|
| 419 | goto out_fail; | 
|---|
| 420 | } | 
|---|
| 421 |  | 
|---|
| 422 | /* Make the one we just received the head. */ | 
|---|
| 423 | reasm_data = inet_frag_reasm_prepare(q: &qp->q, skb, parent: prev_tail); | 
|---|
| 424 | if (!reasm_data) | 
|---|
| 425 | goto out_nomem; | 
|---|
| 426 |  | 
|---|
| 427 | len = ip_hdrlen(skb) + qp->q.len; | 
|---|
| 428 | err = -E2BIG; | 
|---|
| 429 | if (len > 65535) | 
|---|
| 430 | goto out_oversize; | 
|---|
| 431 |  | 
|---|
| 432 | inet_frag_reasm_finish(q: &qp->q, head: skb, reasm_data, | 
|---|
| 433 | try_coalesce: ip_frag_coalesce_ok(qp)); | 
|---|
| 434 |  | 
|---|
| 435 | skb->dev = dev; | 
|---|
| 436 | IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size); | 
|---|
| 437 |  | 
|---|
| 438 | iph = ip_hdr(skb); | 
|---|
| 439 | iph->tot_len = htons(len); | 
|---|
| 440 | iph->tos |= ecn; | 
|---|
| 441 |  | 
|---|
| 442 | /* When we set IP_DF on a refragmented skb we must also force a | 
|---|
| 443 | * call to ip_fragment to avoid forwarding a DF-skb of size s while | 
|---|
| 444 | * original sender only sent fragments of size f (where f < s). | 
|---|
| 445 | * | 
|---|
| 446 | * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest | 
|---|
| 447 | * frag seen to avoid sending tiny DF-fragments in case skb was built | 
|---|
| 448 | * from one very small df-fragment and one large non-df frag. | 
|---|
| 449 | */ | 
|---|
| 450 | if (qp->max_df_size == qp->q.max_size) { | 
|---|
| 451 | IPCB(skb)->flags |= IPSKB_FRAG_PMTU; | 
|---|
| 452 | iph->frag_off = htons(IP_DF); | 
|---|
| 453 | } else { | 
|---|
| 454 | iph->frag_off = 0; | 
|---|
| 455 | } | 
|---|
| 456 |  | 
|---|
| 457 | ip_send_check(ip: iph); | 
|---|
| 458 |  | 
|---|
| 459 | __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); | 
|---|
| 460 | qp->q.rb_fragments = RB_ROOT; | 
|---|
| 461 | qp->q.fragments_tail = NULL; | 
|---|
| 462 | qp->q.last_run_head = NULL; | 
|---|
| 463 | return 0; | 
|---|
| 464 |  | 
|---|
| 465 | out_nomem: | 
|---|
| 466 | net_dbg_ratelimited( "queue_glue: no memory for gluing queue %p\n", qp); | 
|---|
| 467 | err = -ENOMEM; | 
|---|
| 468 | goto out_fail; | 
|---|
| 469 | out_oversize: | 
|---|
| 470 | net_info_ratelimited( "Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); | 
|---|
| 471 | out_fail: | 
|---|
| 472 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); | 
|---|
| 473 | return err; | 
|---|
| 474 | } | 
|---|
| 475 |  | 
|---|
| 476 | /* Process an incoming IP datagram fragment. */ | 
|---|
| 477 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) | 
|---|
| 478 | { | 
|---|
| 479 | struct net_device *dev; | 
|---|
| 480 | struct ipq *qp; | 
|---|
| 481 | int vif; | 
|---|
| 482 |  | 
|---|
| 483 | __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); | 
|---|
| 484 |  | 
|---|
| 485 | /* Lookup (or create) queue header */ | 
|---|
| 486 | rcu_read_lock(); | 
|---|
| 487 | dev = skb->dev ? : skb_dst_dev_rcu(skb); | 
|---|
| 488 | vif = l3mdev_master_ifindex_rcu(dev); | 
|---|
| 489 | qp = ip_find(net, iph: ip_hdr(skb), user, vif); | 
|---|
| 490 | if (qp) { | 
|---|
| 491 | int ret, refs = 0; | 
|---|
| 492 |  | 
|---|
| 493 | spin_lock(lock: &qp->q.lock); | 
|---|
| 494 |  | 
|---|
| 495 | ret = ip_frag_queue(qp, skb, refs: &refs); | 
|---|
| 496 |  | 
|---|
| 497 | spin_unlock(lock: &qp->q.lock); | 
|---|
| 498 | rcu_read_unlock(); | 
|---|
| 499 | inet_frag_putn(q: &qp->q, refs); | 
|---|
| 500 | return ret; | 
|---|
| 501 | } | 
|---|
| 502 | rcu_read_unlock(); | 
|---|
| 503 |  | 
|---|
| 504 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); | 
|---|
| 505 | kfree_skb(skb); | 
|---|
| 506 | return -ENOMEM; | 
|---|
| 507 | } | 
|---|
| 508 | EXPORT_SYMBOL(ip_defrag); | 
|---|
| 509 |  | 
|---|
| 510 | struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) | 
|---|
| 511 | { | 
|---|
| 512 | struct iphdr iph; | 
|---|
| 513 | int netoff; | 
|---|
| 514 | u32 len; | 
|---|
| 515 |  | 
|---|
| 516 | if (skb->protocol != htons(ETH_P_IP)) | 
|---|
| 517 | return skb; | 
|---|
| 518 |  | 
|---|
| 519 | netoff = skb_network_offset(skb); | 
|---|
| 520 |  | 
|---|
| 521 | if (skb_copy_bits(skb, offset: netoff, to: &iph, len: sizeof(iph)) < 0) | 
|---|
| 522 | return skb; | 
|---|
| 523 |  | 
|---|
| 524 | if (iph.ihl < 5 || iph.version != 4) | 
|---|
| 525 | return skb; | 
|---|
| 526 |  | 
|---|
| 527 | len = ntohs(iph.tot_len); | 
|---|
| 528 | if (skb->len < netoff + len || len < (iph.ihl * 4)) | 
|---|
| 529 | return skb; | 
|---|
| 530 |  | 
|---|
| 531 | if (ip_is_fragment(iph: &iph)) { | 
|---|
| 532 | skb = skb_share_check(skb, GFP_ATOMIC); | 
|---|
| 533 | if (skb) { | 
|---|
| 534 | if (!pskb_may_pull(skb, len: netoff + iph.ihl * 4)) { | 
|---|
| 535 | kfree_skb(skb); | 
|---|
| 536 | return NULL; | 
|---|
| 537 | } | 
|---|
| 538 | if (pskb_trim_rcsum(skb, len: netoff + len)) { | 
|---|
| 539 | kfree_skb(skb); | 
|---|
| 540 | return NULL; | 
|---|
| 541 | } | 
|---|
| 542 | memset(IPCB(skb), c: 0, n: sizeof(struct inet_skb_parm)); | 
|---|
| 543 | if (ip_defrag(net, skb, user)) | 
|---|
| 544 | return NULL; | 
|---|
| 545 | skb_clear_hash(skb); | 
|---|
| 546 | } | 
|---|
| 547 | } | 
|---|
| 548 | return skb; | 
|---|
| 549 | } | 
|---|
| 550 | EXPORT_SYMBOL(ip_check_defrag); | 
|---|
| 551 |  | 
|---|
| 552 | #ifdef CONFIG_SYSCTL | 
|---|
| 553 | static int dist_min; | 
|---|
| 554 |  | 
|---|
| 555 | static struct ctl_table ip4_frags_ns_ctl_table[] = { | 
|---|
| 556 | { | 
|---|
| 557 | .procname	= "ipfrag_high_thresh", | 
|---|
| 558 | .maxlen		= sizeof(unsigned long), | 
|---|
| 559 | .mode		= 0644, | 
|---|
| 560 | .proc_handler	= proc_doulongvec_minmax, | 
|---|
| 561 | }, | 
|---|
| 562 | { | 
|---|
| 563 | .procname	= "ipfrag_low_thresh", | 
|---|
| 564 | .maxlen		= sizeof(unsigned long), | 
|---|
| 565 | .mode		= 0644, | 
|---|
| 566 | .proc_handler	= proc_doulongvec_minmax, | 
|---|
| 567 | }, | 
|---|
| 568 | { | 
|---|
| 569 | .procname	= "ipfrag_time", | 
|---|
| 570 | .maxlen		= sizeof(int), | 
|---|
| 571 | .mode		= 0644, | 
|---|
| 572 | .proc_handler	= proc_dointvec_jiffies, | 
|---|
| 573 | }, | 
|---|
| 574 | { | 
|---|
| 575 | .procname	= "ipfrag_max_dist", | 
|---|
| 576 | .maxlen		= sizeof(int), | 
|---|
| 577 | .mode		= 0644, | 
|---|
| 578 | .proc_handler	= proc_dointvec_minmax, | 
|---|
| 579 | .extra1		= &dist_min, | 
|---|
| 580 | }, | 
|---|
| 581 | }; | 
|---|
| 582 |  | 
|---|
| 583 | /* secret interval has been deprecated */ | 
|---|
| 584 | static int ip4_frags_secret_interval_unused; | 
|---|
| 585 | static struct ctl_table ip4_frags_ctl_table[] = { | 
|---|
| 586 | { | 
|---|
| 587 | .procname	= "ipfrag_secret_interval", | 
|---|
| 588 | .data		= &ip4_frags_secret_interval_unused, | 
|---|
| 589 | .maxlen		= sizeof(int), | 
|---|
| 590 | .mode		= 0644, | 
|---|
| 591 | .proc_handler	= proc_dointvec_jiffies, | 
|---|
| 592 | }, | 
|---|
| 593 | }; | 
|---|
| 594 |  | 
|---|
| 595 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) | 
|---|
| 596 | { | 
|---|
| 597 | struct ctl_table *table; | 
|---|
| 598 | struct ctl_table_header *hdr; | 
|---|
| 599 |  | 
|---|
| 600 | table = ip4_frags_ns_ctl_table; | 
|---|
| 601 | if (!net_eq(net1: net, net2: &init_net)) { | 
|---|
| 602 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); | 
|---|
| 603 | if (!table) | 
|---|
| 604 | goto err_alloc; | 
|---|
| 605 |  | 
|---|
| 606 | } | 
|---|
| 607 | table[0].data	= &net->ipv4.fqdir->high_thresh; | 
|---|
| 608 | table[0].extra1	= &net->ipv4.fqdir->low_thresh; | 
|---|
| 609 | table[1].data	= &net->ipv4.fqdir->low_thresh; | 
|---|
| 610 | table[1].extra2	= &net->ipv4.fqdir->high_thresh; | 
|---|
| 611 | table[2].data	= &net->ipv4.fqdir->timeout; | 
|---|
| 612 | table[3].data	= &net->ipv4.fqdir->max_dist; | 
|---|
| 613 |  | 
|---|
| 614 | hdr = register_net_sysctl_sz(net, path: "net/ipv4", table, | 
|---|
| 615 | ARRAY_SIZE(ip4_frags_ns_ctl_table)); | 
|---|
| 616 | if (!hdr) | 
|---|
| 617 | goto err_reg; | 
|---|
| 618 |  | 
|---|
| 619 | net->ipv4.frags_hdr = hdr; | 
|---|
| 620 | return 0; | 
|---|
| 621 |  | 
|---|
| 622 | err_reg: | 
|---|
| 623 | if (!net_eq(net1: net, net2: &init_net)) | 
|---|
| 624 | kfree(objp: table); | 
|---|
| 625 | err_alloc: | 
|---|
| 626 | return -ENOMEM; | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) | 
|---|
| 630 | { | 
|---|
| 631 | const struct ctl_table *table; | 
|---|
| 632 |  | 
|---|
| 633 | table = net->ipv4.frags_hdr->ctl_table_arg; | 
|---|
| 634 | unregister_net_sysctl_table(header: net->ipv4.frags_hdr); | 
|---|
| 635 | kfree(objp: table); | 
|---|
| 636 | } | 
|---|
| 637 |  | 
|---|
| 638 | static void __init ip4_frags_ctl_register(void) | 
|---|
| 639 | { | 
|---|
| 640 | register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); | 
|---|
| 641 | } | 
|---|
| 642 | #else | 
|---|
| 643 | static int ip4_frags_ns_ctl_register(struct net *net) | 
|---|
| 644 | { | 
|---|
| 645 | return 0; | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | static void ip4_frags_ns_ctl_unregister(struct net *net) | 
|---|
| 649 | { | 
|---|
| 650 | } | 
|---|
| 651 |  | 
|---|
| 652 | static void __init ip4_frags_ctl_register(void) | 
|---|
| 653 | { | 
|---|
| 654 | } | 
|---|
| 655 | #endif | 
|---|
| 656 |  | 
|---|
| 657 | static int __net_init ipv4_frags_init_net(struct net *net) | 
|---|
| 658 | { | 
|---|
| 659 | int res; | 
|---|
| 660 |  | 
|---|
| 661 | res = fqdir_init(fqdirp: &net->ipv4.fqdir, f: &ip4_frags, net); | 
|---|
| 662 | if (res < 0) | 
|---|
| 663 | return res; | 
|---|
| 664 | /* Fragment cache limits. | 
|---|
| 665 | * | 
|---|
| 666 | * The fragment memory accounting code, (tries to) account for | 
|---|
| 667 | * the real memory usage, by measuring both the size of frag | 
|---|
| 668 | * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) | 
|---|
| 669 | * and the SKB's truesize. | 
|---|
| 670 | * | 
|---|
| 671 | * A 64K fragment consumes 129736 bytes (44*2944)+200 | 
|---|
| 672 | * (1500 truesize == 2944, sizeof(struct ipq) == 200) | 
|---|
| 673 | * | 
|---|
| 674 | * We will commit 4MB at one time. Should we cross that limit | 
|---|
| 675 | * we will prune down to 3MB, making room for approx 8 big 64K | 
|---|
| 676 | * fragments 8x128k. | 
|---|
| 677 | */ | 
|---|
| 678 | net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024; | 
|---|
| 679 | net->ipv4.fqdir->low_thresh  = 3 * 1024 * 1024; | 
|---|
| 680 | /* | 
|---|
| 681 | * Important NOTE! Fragment queue must be destroyed before MSL expires. | 
|---|
| 682 | * RFC791 is wrong proposing to prolongate timer each fragment arrival | 
|---|
| 683 | * by TTL. | 
|---|
| 684 | */ | 
|---|
| 685 | net->ipv4.fqdir->timeout = IP_FRAG_TIME; | 
|---|
| 686 |  | 
|---|
| 687 | net->ipv4.fqdir->max_dist = 64; | 
|---|
| 688 |  | 
|---|
| 689 | res = ip4_frags_ns_ctl_register(net); | 
|---|
| 690 | if (res < 0) | 
|---|
| 691 | fqdir_exit(fqdir: net->ipv4.fqdir); | 
|---|
| 692 | return res; | 
|---|
| 693 | } | 
|---|
| 694 |  | 
|---|
| 695 | static void __net_exit ipv4_frags_pre_exit_net(struct net *net) | 
|---|
| 696 | { | 
|---|
| 697 | fqdir_pre_exit(fqdir: net->ipv4.fqdir); | 
|---|
| 698 | } | 
|---|
| 699 |  | 
|---|
| 700 | static void __net_exit ipv4_frags_exit_net(struct net *net) | 
|---|
| 701 | { | 
|---|
| 702 | ip4_frags_ns_ctl_unregister(net); | 
|---|
| 703 | fqdir_exit(fqdir: net->ipv4.fqdir); | 
|---|
| 704 | } | 
|---|
| 705 |  | 
|---|
| 706 | static struct pernet_operations ip4_frags_ops = { | 
|---|
| 707 | .init		= ipv4_frags_init_net, | 
|---|
| 708 | .pre_exit	= ipv4_frags_pre_exit_net, | 
|---|
| 709 | .exit		= ipv4_frags_exit_net, | 
|---|
| 710 | }; | 
|---|
| 711 |  | 
|---|
| 712 |  | 
|---|
| 713 | static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) | 
|---|
| 714 | { | 
|---|
| 715 | return jhash2(k: data, | 
|---|
| 716 | length: sizeof(struct frag_v4_compare_key) / sizeof(u32), initval: seed); | 
|---|
| 717 | } | 
|---|
| 718 |  | 
|---|
| 719 | static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) | 
|---|
| 720 | { | 
|---|
| 721 | const struct inet_frag_queue *fq = data; | 
|---|
| 722 |  | 
|---|
| 723 | return jhash2(k: (const u32 *)&fq->key.v4, | 
|---|
| 724 | length: sizeof(struct frag_v4_compare_key) / sizeof(u32), initval: seed); | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 | static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) | 
|---|
| 728 | { | 
|---|
| 729 | const struct frag_v4_compare_key *key = arg->key; | 
|---|
| 730 | const struct inet_frag_queue *fq = ptr; | 
|---|
| 731 |  | 
|---|
| 732 | return !!memcmp(&fq->key, key, sizeof(*key)); | 
|---|
| 733 | } | 
|---|
| 734 |  | 
|---|
| 735 | static const struct rhashtable_params ip4_rhash_params = { | 
|---|
| 736 | .head_offset		= offsetof(struct inet_frag_queue, node), | 
|---|
| 737 | .key_offset		= offsetof(struct inet_frag_queue, key), | 
|---|
| 738 | .key_len		= sizeof(struct frag_v4_compare_key), | 
|---|
| 739 | .hashfn			= ip4_key_hashfn, | 
|---|
| 740 | .obj_hashfn		= ip4_obj_hashfn, | 
|---|
| 741 | .obj_cmpfn		= ip4_obj_cmpfn, | 
|---|
| 742 | .automatic_shrinking	= true, | 
|---|
| 743 | }; | 
|---|
| 744 |  | 
|---|
| 745 | void __init ipfrag_init(void) | 
|---|
| 746 | { | 
|---|
| 747 | ip4_frags.constructor = ip4_frag_init; | 
|---|
| 748 | ip4_frags.destructor = ip4_frag_free; | 
|---|
| 749 | ip4_frags.qsize = sizeof(struct ipq); | 
|---|
| 750 | ip4_frags.frag_expire = ip_expire; | 
|---|
| 751 | ip4_frags.frags_cache_name = ip_frag_cache_name; | 
|---|
| 752 | ip4_frags.rhash_params = ip4_rhash_params; | 
|---|
| 753 | if (inet_frags_init(&ip4_frags)) | 
|---|
| 754 | panic(fmt: "IP: failed to allocate ip4_frags cache\n"); | 
|---|
| 755 | ip4_frags_ctl_register(); | 
|---|
| 756 | register_pernet_subsys(&ip4_frags_ops); | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|