ip_fragment.c 20 KB

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