ip_fragment.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. #define pr_fmt(fmt) "IPv4: " fmt
  24. #include <linux/compiler.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/mm.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/list.h>
  31. #include <linux/ip.h>
  32. #include <linux/icmp.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/jhash.h>
  35. #include <linux/random.h>
  36. #include <linux/slab.h>
  37. #include <net/route.h>
  38. #include <net/dst.h>
  39. #include <net/sock.h>
  40. #include <net/ip.h>
  41. #include <net/icmp.h>
  42. #include <net/checksum.h>
  43. #include <net/inetpeer.h>
  44. #include <net/inet_frag.h>
  45. #include <linux/tcp.h>
  46. #include <linux/udp.h>
  47. #include <linux/inet.h>
  48. #include <linux/netfilter_ipv4.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/l3mdev.h>
  51. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  52. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  53. * as well. Or notify me, at least. --ANK
  54. */
  55. static const char ip_frag_cache_name[] = "ip4-frags";
  56. struct ipfrag_skb_cb
  57. {
  58. struct inet_skb_parm h;
  59. int offset;
  60. };
  61. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  62. /* Describe an entry in the "incomplete datagrams" queue. */
  63. struct ipq {
  64. struct inet_frag_queue q;
  65. u32 user;
  66. __be32 saddr;
  67. __be32 daddr;
  68. __be16 id;
  69. u8 protocol;
  70. u8 ecn; /* RFC3168 support */
  71. u16 max_df_size; /* largest frag with DF set seen */
  72. int iif;
  73. int vif; /* L3 master device index */
  74. unsigned int rid;
  75. struct inet_peer *peer;
  76. };
  77. static u8 ip4_frag_ecn(u8 tos)
  78. {
  79. return 1 << (tos & INET_ECN_MASK);
  80. }
  81. static struct inet_frags ip4_frags;
  82. int ip_frag_mem(struct net *net)
  83. {
  84. return sum_frag_mem_limit(&net->ipv4.frags);
  85. }
  86. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  87. struct net_device *dev);
  88. struct ip4_create_arg {
  89. struct iphdr *iph;
  90. u32 user;
  91. int vif;
  92. };
  93. static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
  94. {
  95. net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
  96. return jhash_3words((__force u32)id << 16 | prot,
  97. (__force u32)saddr, (__force u32)daddr,
  98. ip4_frags.rnd);
  99. }
  100. static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
  101. {
  102. const struct ipq *ipq;
  103. ipq = container_of(q, struct ipq, q);
  104. return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
  105. }
  106. static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
  107. {
  108. const struct ipq *qp;
  109. const struct ip4_create_arg *arg = a;
  110. qp = container_of(q, struct ipq, q);
  111. return qp->id == arg->iph->id &&
  112. qp->saddr == arg->iph->saddr &&
  113. qp->daddr == arg->iph->daddr &&
  114. qp->protocol == arg->iph->protocol &&
  115. qp->user == arg->user &&
  116. qp->vif == arg->vif;
  117. }
  118. static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
  119. {
  120. struct ipq *qp = container_of(q, struct ipq, q);
  121. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  122. frags);
  123. struct net *net = container_of(ipv4, struct net, ipv4);
  124. const struct ip4_create_arg *arg = a;
  125. qp->protocol = arg->iph->protocol;
  126. qp->id = arg->iph->id;
  127. qp->ecn = ip4_frag_ecn(arg->iph->tos);
  128. qp->saddr = arg->iph->saddr;
  129. qp->daddr = arg->iph->daddr;
  130. qp->vif = arg->vif;
  131. qp->user = arg->user;
  132. qp->peer = q->net->max_dist ?
  133. inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
  134. NULL;
  135. }
  136. static void ip4_frag_free(struct inet_frag_queue *q)
  137. {
  138. struct ipq *qp;
  139. qp = container_of(q, struct ipq, q);
  140. if (qp->peer)
  141. inet_putpeer(qp->peer);
  142. }
  143. /* Destruction primitives. */
  144. static void ipq_put(struct ipq *ipq)
  145. {
  146. inet_frag_put(&ipq->q, &ip4_frags);
  147. }
  148. /* Kill ipq entry. It is not destroyed immediately,
  149. * because caller (and someone more) holds reference count.
  150. */
  151. static void ipq_kill(struct ipq *ipq)
  152. {
  153. inet_frag_kill(&ipq->q, &ip4_frags);
  154. }
  155. static bool frag_expire_skip_icmp(u32 user)
  156. {
  157. return user == IP_DEFRAG_AF_PACKET ||
  158. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
  159. __IP_DEFRAG_CONNTRACK_IN_END) ||
  160. ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  161. __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
  162. }
  163. /*
  164. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  165. */
  166. static void ip_expire(unsigned long arg)
  167. {
  168. struct ipq *qp;
  169. struct net *net;
  170. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  171. net = container_of(qp->q.net, struct net, ipv4.frags);
  172. rcu_read_lock();
  173. spin_lock(&qp->q.lock);
  174. if (qp->q.flags & INET_FRAG_COMPLETE)
  175. goto out;
  176. ipq_kill(qp);
  177. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  178. if (!inet_frag_evicting(&qp->q)) {
  179. struct sk_buff *clone, *head = qp->q.fragments;
  180. const struct iphdr *iph;
  181. int err;
  182. __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
  183. if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !qp->q.fragments)
  184. goto out;
  185. head->dev = dev_get_by_index_rcu(net, qp->iif);
  186. if (!head->dev)
  187. goto out;
  188. /* skb has no dst, perform route lookup again */
  189. iph = ip_hdr(head);
  190. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  191. iph->tos, head->dev);
  192. if (err)
  193. goto out;
  194. /* Only an end host needs to send an ICMP
  195. * "Fragment Reassembly Timeout" message, per RFC792.
  196. */
  197. if (frag_expire_skip_icmp(qp->user) &&
  198. (skb_rtable(head)->rt_type != RTN_LOCAL))
  199. goto out;
  200. clone = skb_clone(head, GFP_ATOMIC);
  201. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  202. if (clone) {
  203. spin_unlock(&qp->q.lock);
  204. icmp_send(clone, ICMP_TIME_EXCEEDED,
  205. ICMP_EXC_FRAGTIME, 0);
  206. consume_skb(clone);
  207. goto out_rcu_unlock;
  208. }
  209. }
  210. out:
  211. spin_unlock(&qp->q.lock);
  212. out_rcu_unlock:
  213. rcu_read_unlock();
  214. ipq_put(qp);
  215. }
  216. /* Find the correct entry in the "incomplete datagrams" queue for
  217. * this IP datagram, and create new one, if nothing is found.
  218. */
  219. static struct ipq *ip_find(struct net *net, struct iphdr *iph,
  220. u32 user, int vif)
  221. {
  222. struct inet_frag_queue *q;
  223. struct ip4_create_arg arg;
  224. unsigned int hash;
  225. arg.iph = iph;
  226. arg.user = user;
  227. arg.vif = vif;
  228. hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
  229. q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
  230. if (IS_ERR_OR_NULL(q)) {
  231. inet_frag_maybe_warn_overflow(q, pr_fmt());
  232. return NULL;
  233. }
  234. return container_of(q, struct ipq, q);
  235. }
  236. /* Is the fragment too far ahead to be part of ipq? */
  237. static int ip_frag_too_far(struct ipq *qp)
  238. {
  239. struct inet_peer *peer = qp->peer;
  240. unsigned int max = qp->q.net->max_dist;
  241. unsigned int start, end;
  242. int rc;
  243. if (!peer || !max)
  244. return 0;
  245. start = qp->rid;
  246. end = atomic_inc_return(&peer->rid);
  247. qp->rid = end;
  248. rc = qp->q.fragments && (end - start) > max;
  249. if (rc) {
  250. struct net *net;
  251. net = container_of(qp->q.net, struct net, ipv4.frags);
  252. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  253. }
  254. return rc;
  255. }
  256. static int ip_frag_reinit(struct ipq *qp)
  257. {
  258. struct sk_buff *fp;
  259. unsigned int sum_truesize = 0;
  260. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  261. refcount_inc(&qp->q.refcnt);
  262. return -ETIMEDOUT;
  263. }
  264. fp = qp->q.fragments;
  265. do {
  266. struct sk_buff *xp = fp->next;
  267. sum_truesize += fp->truesize;
  268. kfree_skb(fp);
  269. fp = xp;
  270. } while (fp);
  271. sub_frag_mem_limit(qp->q.net, sum_truesize);
  272. qp->q.flags = 0;
  273. qp->q.len = 0;
  274. qp->q.meat = 0;
  275. qp->q.fragments = NULL;
  276. qp->q.fragments_tail = NULL;
  277. qp->iif = 0;
  278. qp->ecn = 0;
  279. return 0;
  280. }
  281. /* Add new segment to existing queue. */
  282. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  283. {
  284. struct sk_buff *prev, *next;
  285. struct net_device *dev;
  286. unsigned int fragsize;
  287. int flags, offset;
  288. int ihl, end;
  289. int err = -ENOENT;
  290. u8 ecn;
  291. if (qp->q.flags & INET_FRAG_COMPLETE)
  292. goto err;
  293. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  294. unlikely(ip_frag_too_far(qp)) &&
  295. unlikely(err = ip_frag_reinit(qp))) {
  296. ipq_kill(qp);
  297. goto err;
  298. }
  299. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  300. offset = ntohs(ip_hdr(skb)->frag_off);
  301. flags = offset & ~IP_OFFSET;
  302. offset &= IP_OFFSET;
  303. offset <<= 3; /* offset is in 8-byte chunks */
  304. ihl = ip_hdrlen(skb);
  305. /* Determine the position of this fragment. */
  306. end = offset + skb->len - skb_network_offset(skb) - ihl;
  307. err = -EINVAL;
  308. /* Is this the final fragment? */
  309. if ((flags & IP_MF) == 0) {
  310. /* If we already have some bits beyond end
  311. * or have different end, the segment is corrupted.
  312. */
  313. if (end < qp->q.len ||
  314. ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
  315. goto err;
  316. qp->q.flags |= INET_FRAG_LAST_IN;
  317. qp->q.len = end;
  318. } else {
  319. if (end&7) {
  320. end &= ~7;
  321. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  322. skb->ip_summed = CHECKSUM_NONE;
  323. }
  324. if (end > qp->q.len) {
  325. /* Some bits beyond end -> corruption. */
  326. if (qp->q.flags & INET_FRAG_LAST_IN)
  327. goto err;
  328. qp->q.len = end;
  329. }
  330. }
  331. if (end == offset)
  332. goto err;
  333. err = -ENOMEM;
  334. if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
  335. goto err;
  336. err = pskb_trim_rcsum(skb, end - offset);
  337. if (err)
  338. goto err;
  339. /* Find out which fragments are in front and at the back of us
  340. * in the chain of fragments so far. We must know where to put
  341. * this fragment, right?
  342. */
  343. prev = qp->q.fragments_tail;
  344. if (!prev || FRAG_CB(prev)->offset < offset) {
  345. next = NULL;
  346. goto found;
  347. }
  348. prev = NULL;
  349. for (next = qp->q.fragments; next != NULL; next = next->next) {
  350. if (FRAG_CB(next)->offset >= offset)
  351. break; /* bingo! */
  352. prev = next;
  353. }
  354. found:
  355. /* We found where to put this one. Check for overlap with
  356. * preceding fragment, and, if needed, align things so that
  357. * any overlaps are eliminated.
  358. */
  359. if (prev) {
  360. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  361. if (i > 0) {
  362. offset += i;
  363. err = -EINVAL;
  364. if (end <= offset)
  365. goto err;
  366. err = -ENOMEM;
  367. if (!pskb_pull(skb, i))
  368. goto err;
  369. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  370. skb->ip_summed = CHECKSUM_NONE;
  371. }
  372. }
  373. err = -ENOMEM;
  374. while (next && FRAG_CB(next)->offset < end) {
  375. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  376. if (i < next->len) {
  377. /* Eat head of the next overlapped fragment
  378. * and leave the loop. The next ones cannot overlap.
  379. */
  380. if (!pskb_pull(next, i))
  381. goto err;
  382. FRAG_CB(next)->offset += i;
  383. qp->q.meat -= i;
  384. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  385. next->ip_summed = CHECKSUM_NONE;
  386. break;
  387. } else {
  388. struct sk_buff *free_it = next;
  389. /* Old fragment is completely overridden with
  390. * new one drop it.
  391. */
  392. next = next->next;
  393. if (prev)
  394. prev->next = next;
  395. else
  396. qp->q.fragments = next;
  397. qp->q.meat -= free_it->len;
  398. sub_frag_mem_limit(qp->q.net, free_it->truesize);
  399. kfree_skb(free_it);
  400. }
  401. }
  402. FRAG_CB(skb)->offset = offset;
  403. /* Insert this fragment in the chain of fragments. */
  404. skb->next = next;
  405. if (!next)
  406. qp->q.fragments_tail = skb;
  407. if (prev)
  408. prev->next = skb;
  409. else
  410. qp->q.fragments = skb;
  411. dev = skb->dev;
  412. if (dev) {
  413. qp->iif = dev->ifindex;
  414. skb->dev = NULL;
  415. }
  416. qp->q.stamp = skb->tstamp;
  417. qp->q.meat += skb->len;
  418. qp->ecn |= ecn;
  419. add_frag_mem_limit(qp->q.net, skb->truesize);
  420. if (offset == 0)
  421. qp->q.flags |= INET_FRAG_FIRST_IN;
  422. fragsize = skb->len + ihl;
  423. if (fragsize > qp->q.max_size)
  424. qp->q.max_size = fragsize;
  425. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  426. fragsize > qp->max_df_size)
  427. qp->max_df_size = fragsize;
  428. if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  429. qp->q.meat == qp->q.len) {
  430. unsigned long orefdst = skb->_skb_refdst;
  431. skb->_skb_refdst = 0UL;
  432. err = ip_frag_reasm(qp, prev, dev);
  433. skb->_skb_refdst = orefdst;
  434. return err;
  435. }
  436. skb_dst_drop(skb);
  437. return -EINPROGRESS;
  438. err:
  439. kfree_skb(skb);
  440. return err;
  441. }
  442. /* Build a new IP datagram from all its fragments. */
  443. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  444. struct net_device *dev)
  445. {
  446. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  447. struct iphdr *iph;
  448. struct sk_buff *fp, *head = qp->q.fragments;
  449. int len;
  450. int ihlen;
  451. int err;
  452. u8 ecn;
  453. ipq_kill(qp);
  454. ecn = ip_frag_ecn_table[qp->ecn];
  455. if (unlikely(ecn == 0xff)) {
  456. err = -EINVAL;
  457. goto out_fail;
  458. }
  459. /* Make the one we just received the head. */
  460. if (prev) {
  461. head = prev->next;
  462. fp = skb_clone(head, GFP_ATOMIC);
  463. if (!fp)
  464. goto out_nomem;
  465. fp->next = head->next;
  466. if (!fp->next)
  467. qp->q.fragments_tail = fp;
  468. prev->next = fp;
  469. skb_morph(head, qp->q.fragments);
  470. head->next = qp->q.fragments->next;
  471. consume_skb(qp->q.fragments);
  472. qp->q.fragments = head;
  473. }
  474. WARN_ON(!head);
  475. WARN_ON(FRAG_CB(head)->offset != 0);
  476. /* Allocate a new buffer for the datagram. */
  477. ihlen = ip_hdrlen(head);
  478. len = ihlen + qp->q.len;
  479. err = -E2BIG;
  480. if (len > 65535)
  481. goto out_oversize;
  482. /* Head of list must not be cloned. */
  483. if (skb_unclone(head, GFP_ATOMIC))
  484. goto out_nomem;
  485. /* If the first fragment is fragmented itself, we split
  486. * it to two chunks: the first with data and paged part
  487. * and the second, holding only fragments. */
  488. if (skb_has_frag_list(head)) {
  489. struct sk_buff *clone;
  490. int i, plen = 0;
  491. clone = alloc_skb(0, GFP_ATOMIC);
  492. if (!clone)
  493. goto out_nomem;
  494. clone->next = head->next;
  495. head->next = clone;
  496. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  497. skb_frag_list_init(head);
  498. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  499. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  500. clone->len = clone->data_len = head->data_len - plen;
  501. head->data_len -= clone->len;
  502. head->len -= clone->len;
  503. clone->csum = 0;
  504. clone->ip_summed = head->ip_summed;
  505. add_frag_mem_limit(qp->q.net, clone->truesize);
  506. }
  507. skb_shinfo(head)->frag_list = head->next;
  508. skb_push(head, head->data - skb_network_header(head));
  509. for (fp=head->next; fp; fp = fp->next) {
  510. head->data_len += fp->len;
  511. head->len += fp->len;
  512. if (head->ip_summed != fp->ip_summed)
  513. head->ip_summed = CHECKSUM_NONE;
  514. else if (head->ip_summed == CHECKSUM_COMPLETE)
  515. head->csum = csum_add(head->csum, fp->csum);
  516. head->truesize += fp->truesize;
  517. }
  518. sub_frag_mem_limit(qp->q.net, head->truesize);
  519. head->next = NULL;
  520. head->dev = dev;
  521. head->tstamp = qp->q.stamp;
  522. IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
  523. iph = ip_hdr(head);
  524. iph->tot_len = htons(len);
  525. iph->tos |= ecn;
  526. /* When we set IP_DF on a refragmented skb we must also force a
  527. * call to ip_fragment to avoid forwarding a DF-skb of size s while
  528. * original sender only sent fragments of size f (where f < s).
  529. *
  530. * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
  531. * frag seen to avoid sending tiny DF-fragments in case skb was built
  532. * from one very small df-fragment and one large non-df frag.
  533. */
  534. if (qp->max_df_size == qp->q.max_size) {
  535. IPCB(head)->flags |= IPSKB_FRAG_PMTU;
  536. iph->frag_off = htons(IP_DF);
  537. } else {
  538. iph->frag_off = 0;
  539. }
  540. ip_send_check(iph);
  541. __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
  542. qp->q.fragments = NULL;
  543. qp->q.fragments_tail = NULL;
  544. return 0;
  545. out_nomem:
  546. net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
  547. err = -ENOMEM;
  548. goto out_fail;
  549. out_oversize:
  550. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
  551. out_fail:
  552. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  553. return err;
  554. }
  555. /* Process an incoming IP datagram fragment. */
  556. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
  557. {
  558. struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
  559. int vif = l3mdev_master_ifindex_rcu(dev);
  560. struct ipq *qp;
  561. __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
  562. skb_orphan(skb);
  563. /* Lookup (or create) queue header */
  564. qp = ip_find(net, ip_hdr(skb), user, vif);
  565. if (qp) {
  566. int ret;
  567. spin_lock(&qp->q.lock);
  568. ret = ip_frag_queue(qp, skb);
  569. spin_unlock(&qp->q.lock);
  570. ipq_put(qp);
  571. return ret;
  572. }
  573. __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
  574. kfree_skb(skb);
  575. return -ENOMEM;
  576. }
  577. EXPORT_SYMBOL(ip_defrag);
  578. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  579. {
  580. struct iphdr iph;
  581. int netoff;
  582. u32 len;
  583. if (skb->protocol != htons(ETH_P_IP))
  584. return skb;
  585. netoff = skb_network_offset(skb);
  586. if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
  587. return skb;
  588. if (iph.ihl < 5 || iph.version != 4)
  589. return skb;
  590. len = ntohs(iph.tot_len);
  591. if (skb->len < netoff + len || len < (iph.ihl * 4))
  592. return skb;
  593. if (ip_is_fragment(&iph)) {
  594. skb = skb_share_check(skb, GFP_ATOMIC);
  595. if (skb) {
  596. if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
  597. return skb;
  598. if (pskb_trim_rcsum(skb, netoff + len))
  599. return skb;
  600. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  601. if (ip_defrag(net, skb, user))
  602. return NULL;
  603. skb_clear_hash(skb);
  604. }
  605. }
  606. return skb;
  607. }
  608. EXPORT_SYMBOL(ip_check_defrag);
  609. #ifdef CONFIG_SYSCTL
  610. static int zero;
  611. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  612. {
  613. .procname = "ipfrag_high_thresh",
  614. .data = &init_net.ipv4.frags.high_thresh,
  615. .maxlen = sizeof(int),
  616. .mode = 0644,
  617. .proc_handler = proc_dointvec_minmax,
  618. .extra1 = &init_net.ipv4.frags.low_thresh
  619. },
  620. {
  621. .procname = "ipfrag_low_thresh",
  622. .data = &init_net.ipv4.frags.low_thresh,
  623. .maxlen = sizeof(int),
  624. .mode = 0644,
  625. .proc_handler = proc_dointvec_minmax,
  626. .extra1 = &zero,
  627. .extra2 = &init_net.ipv4.frags.high_thresh
  628. },
  629. {
  630. .procname = "ipfrag_time",
  631. .data = &init_net.ipv4.frags.timeout,
  632. .maxlen = sizeof(int),
  633. .mode = 0644,
  634. .proc_handler = proc_dointvec_jiffies,
  635. },
  636. {
  637. .procname = "ipfrag_max_dist",
  638. .data = &init_net.ipv4.frags.max_dist,
  639. .maxlen = sizeof(int),
  640. .mode = 0644,
  641. .proc_handler = proc_dointvec_minmax,
  642. .extra1 = &zero
  643. },
  644. { }
  645. };
  646. /* secret interval has been deprecated */
  647. static int ip4_frags_secret_interval_unused;
  648. static struct ctl_table ip4_frags_ctl_table[] = {
  649. {
  650. .procname = "ipfrag_secret_interval",
  651. .data = &ip4_frags_secret_interval_unused,
  652. .maxlen = sizeof(int),
  653. .mode = 0644,
  654. .proc_handler = proc_dointvec_jiffies,
  655. },
  656. { }
  657. };
  658. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  659. {
  660. struct ctl_table *table;
  661. struct ctl_table_header *hdr;
  662. table = ip4_frags_ns_ctl_table;
  663. if (!net_eq(net, &init_net)) {
  664. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  665. if (!table)
  666. goto err_alloc;
  667. table[0].data = &net->ipv4.frags.high_thresh;
  668. table[0].extra1 = &net->ipv4.frags.low_thresh;
  669. table[0].extra2 = &init_net.ipv4.frags.high_thresh;
  670. table[1].data = &net->ipv4.frags.low_thresh;
  671. table[1].extra2 = &net->ipv4.frags.high_thresh;
  672. table[2].data = &net->ipv4.frags.timeout;
  673. table[3].data = &net->ipv4.frags.max_dist;
  674. }
  675. hdr = register_net_sysctl(net, "net/ipv4", table);
  676. if (!hdr)
  677. goto err_reg;
  678. net->ipv4.frags_hdr = hdr;
  679. return 0;
  680. err_reg:
  681. if (!net_eq(net, &init_net))
  682. kfree(table);
  683. err_alloc:
  684. return -ENOMEM;
  685. }
  686. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  687. {
  688. struct ctl_table *table;
  689. table = net->ipv4.frags_hdr->ctl_table_arg;
  690. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  691. kfree(table);
  692. }
  693. static void __init ip4_frags_ctl_register(void)
  694. {
  695. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  696. }
  697. #else
  698. static int ip4_frags_ns_ctl_register(struct net *net)
  699. {
  700. return 0;
  701. }
  702. static void ip4_frags_ns_ctl_unregister(struct net *net)
  703. {
  704. }
  705. static void __init ip4_frags_ctl_register(void)
  706. {
  707. }
  708. #endif
  709. static int __net_init ipv4_frags_init_net(struct net *net)
  710. {
  711. /* Fragment cache limits.
  712. *
  713. * The fragment memory accounting code, (tries to) account for
  714. * the real memory usage, by measuring both the size of frag
  715. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  716. * and the SKB's truesize.
  717. *
  718. * A 64K fragment consumes 129736 bytes (44*2944)+200
  719. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  720. *
  721. * We will commit 4MB at one time. Should we cross that limit
  722. * we will prune down to 3MB, making room for approx 8 big 64K
  723. * fragments 8x128k.
  724. */
  725. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  726. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  727. /*
  728. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  729. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  730. * by TTL.
  731. */
  732. net->ipv4.frags.timeout = IP_FRAG_TIME;
  733. net->ipv4.frags.max_dist = 64;
  734. inet_frags_init_net(&net->ipv4.frags);
  735. return ip4_frags_ns_ctl_register(net);
  736. }
  737. static void __net_exit ipv4_frags_exit_net(struct net *net)
  738. {
  739. ip4_frags_ns_ctl_unregister(net);
  740. inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
  741. }
  742. static struct pernet_operations ip4_frags_ops = {
  743. .init = ipv4_frags_init_net,
  744. .exit = ipv4_frags_exit_net,
  745. };
  746. void __init ipfrag_init(void)
  747. {
  748. ip4_frags_ctl_register();
  749. register_pernet_subsys(&ip4_frags_ops);
  750. ip4_frags.hashfn = ip4_hashfn;
  751. ip4_frags.constructor = ip4_frag_init;
  752. ip4_frags.destructor = ip4_frag_free;
  753. ip4_frags.qsize = sizeof(struct ipq);
  754. ip4_frags.match = ip4_frag_match;
  755. ip4_frags.frag_expire = ip_expire;
  756. ip4_frags.frags_cache_name = ip_frag_cache_name;
  757. if (inet_frags_init(&ip4_frags))
  758. panic("IP: failed to allocate ip4_frags cache\n");
  759. }