ip_fragment.c 21 KB

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