ip_fragment.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. struct ipfrag_skb_cb
  55. {
  56. struct inet_skb_parm h;
  57. int offset;
  58. };
  59. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  60. /* Describe an entry in the "incomplete datagrams" queue. */
  61. struct ipq {
  62. struct inet_frag_queue q;
  63. u32 user;
  64. __be32 saddr;
  65. __be32 daddr;
  66. __be16 id;
  67. u8 protocol;
  68. u8 ecn; /* RFC3168 support */
  69. int iif;
  70. unsigned int rid;
  71. struct inet_peer *peer;
  72. };
  73. static inline u8 ip4_frag_ecn(u8 tos)
  74. {
  75. return 1 << (tos & INET_ECN_MASK);
  76. }
  77. static struct inet_frags ip4_frags;
  78. int ip_frag_nqueues(struct net *net)
  79. {
  80. return net->ipv4.frags.nqueues;
  81. }
  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. };
  92. static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
  93. {
  94. return jhash_3words((__force u32)id << 16 | prot,
  95. (__force u32)saddr, (__force u32)daddr,
  96. ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
  97. }
  98. static unsigned int ip4_hashfn(struct inet_frag_queue *q)
  99. {
  100. struct ipq *ipq;
  101. ipq = container_of(q, struct ipq, q);
  102. return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
  103. }
  104. static bool ip4_frag_match(struct inet_frag_queue *q, void *a)
  105. {
  106. struct ipq *qp;
  107. struct ip4_create_arg *arg = a;
  108. qp = container_of(q, struct ipq, q);
  109. return qp->id == arg->iph->id &&
  110. qp->saddr == arg->iph->saddr &&
  111. qp->daddr == arg->iph->daddr &&
  112. qp->protocol == arg->iph->protocol &&
  113. qp->user == arg->user;
  114. }
  115. static void ip4_frag_init(struct inet_frag_queue *q, void *a)
  116. {
  117. struct ipq *qp = container_of(q, struct ipq, q);
  118. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  119. frags);
  120. struct net *net = container_of(ipv4, struct net, ipv4);
  121. struct ip4_create_arg *arg = a;
  122. qp->protocol = arg->iph->protocol;
  123. qp->id = arg->iph->id;
  124. qp->ecn = ip4_frag_ecn(arg->iph->tos);
  125. qp->saddr = arg->iph->saddr;
  126. qp->daddr = arg->iph->daddr;
  127. qp->user = arg->user;
  128. qp->peer = sysctl_ipfrag_max_dist ?
  129. inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
  130. }
  131. static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
  132. {
  133. struct ipq *qp;
  134. qp = container_of(q, struct ipq, q);
  135. if (qp->peer)
  136. inet_putpeer(qp->peer);
  137. }
  138. /* Destruction primitives. */
  139. static __inline__ void ipq_put(struct ipq *ipq)
  140. {
  141. inet_frag_put(&ipq->q, &ip4_frags);
  142. }
  143. /* Kill ipq entry. It is not destroyed immediately,
  144. * because caller (and someone more) holds reference count.
  145. */
  146. static void ipq_kill(struct ipq *ipq)
  147. {
  148. inet_frag_kill(&ipq->q, &ip4_frags);
  149. }
  150. /* Memory limiting on fragments. Evictor trashes the oldest
  151. * fragment queue until we are back under the threshold.
  152. */
  153. static void ip_evictor(struct net *net)
  154. {
  155. int evicted;
  156. evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags, false);
  157. if (evicted)
  158. IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
  159. }
  160. /*
  161. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  162. */
  163. static void ip_expire(unsigned long arg)
  164. {
  165. struct ipq *qp;
  166. struct net *net;
  167. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  168. net = container_of(qp->q.net, struct net, ipv4.frags);
  169. spin_lock(&qp->q.lock);
  170. if (qp->q.last_in & INET_FRAG_COMPLETE)
  171. goto out;
  172. ipq_kill(qp);
  173. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
  174. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  175. if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
  176. struct sk_buff *head = qp->q.fragments;
  177. const struct iphdr *iph;
  178. int err;
  179. rcu_read_lock();
  180. head->dev = dev_get_by_index_rcu(net, qp->iif);
  181. if (!head->dev)
  182. goto out_rcu_unlock;
  183. /* skb dst is stale, drop it, and perform route lookup again */
  184. skb_dst_drop(head);
  185. iph = ip_hdr(head);
  186. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  187. iph->tos, head->dev);
  188. if (err)
  189. goto out_rcu_unlock;
  190. /*
  191. * Only an end host needs to send an ICMP
  192. * "Fragment Reassembly Timeout" message, per RFC792.
  193. */
  194. if (qp->user == IP_DEFRAG_AF_PACKET ||
  195. (qp->user == IP_DEFRAG_CONNTRACK_IN &&
  196. skb_rtable(head)->rt_type != RTN_LOCAL))
  197. goto out_rcu_unlock;
  198. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  199. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  200. out_rcu_unlock:
  201. rcu_read_unlock();
  202. }
  203. out:
  204. spin_unlock(&qp->q.lock);
  205. ipq_put(qp);
  206. }
  207. /* Find the correct entry in the "incomplete datagrams" queue for
  208. * this IP datagram, and create new one, if nothing is found.
  209. */
  210. static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
  211. {
  212. struct inet_frag_queue *q;
  213. struct ip4_create_arg arg;
  214. unsigned int hash;
  215. arg.iph = iph;
  216. arg.user = user;
  217. read_lock(&ip4_frags.lock);
  218. hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
  219. q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
  220. if (IS_ERR_OR_NULL(q)) {
  221. inet_frag_maybe_warn_overflow(q, pr_fmt());
  222. return NULL;
  223. }
  224. return container_of(q, struct ipq, q);
  225. }
  226. /* Is the fragment too far ahead to be part of ipq? */
  227. static inline int ip_frag_too_far(struct ipq *qp)
  228. {
  229. struct inet_peer *peer = qp->peer;
  230. unsigned int max = sysctl_ipfrag_max_dist;
  231. unsigned int start, end;
  232. int rc;
  233. if (!peer || !max)
  234. return 0;
  235. start = qp->rid;
  236. end = atomic_inc_return(&peer->rid);
  237. qp->rid = end;
  238. rc = qp->q.fragments && (end - start) > max;
  239. if (rc) {
  240. struct net *net;
  241. net = container_of(qp->q.net, struct net, ipv4.frags);
  242. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  243. }
  244. return rc;
  245. }
  246. static int ip_frag_reinit(struct ipq *qp)
  247. {
  248. struct sk_buff *fp;
  249. unsigned int sum_truesize = 0;
  250. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  251. atomic_inc(&qp->q.refcnt);
  252. return -ETIMEDOUT;
  253. }
  254. fp = qp->q.fragments;
  255. do {
  256. struct sk_buff *xp = fp->next;
  257. sum_truesize += fp->truesize;
  258. kfree_skb(fp);
  259. fp = xp;
  260. } while (fp);
  261. sub_frag_mem_limit(&qp->q, sum_truesize);
  262. qp->q.last_in = 0;
  263. qp->q.len = 0;
  264. qp->q.meat = 0;
  265. qp->q.fragments = NULL;
  266. qp->q.fragments_tail = NULL;
  267. qp->iif = 0;
  268. qp->ecn = 0;
  269. return 0;
  270. }
  271. /* Add new segment to existing queue. */
  272. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  273. {
  274. struct sk_buff *prev, *next;
  275. struct net_device *dev;
  276. int flags, offset;
  277. int ihl, end;
  278. int err = -ENOENT;
  279. u8 ecn;
  280. if (qp->q.last_in & INET_FRAG_COMPLETE)
  281. goto err;
  282. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  283. unlikely(ip_frag_too_far(qp)) &&
  284. unlikely(err = ip_frag_reinit(qp))) {
  285. ipq_kill(qp);
  286. goto err;
  287. }
  288. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  289. offset = ntohs(ip_hdr(skb)->frag_off);
  290. flags = offset & ~IP_OFFSET;
  291. offset &= IP_OFFSET;
  292. offset <<= 3; /* offset is in 8-byte chunks */
  293. ihl = ip_hdrlen(skb);
  294. /* Determine the position of this fragment. */
  295. end = offset + skb->len - ihl;
  296. err = -EINVAL;
  297. /* Is this the final fragment? */
  298. if ((flags & IP_MF) == 0) {
  299. /* If we already have some bits beyond end
  300. * or have different end, the segment is corrupted.
  301. */
  302. if (end < qp->q.len ||
  303. ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
  304. goto err;
  305. qp->q.last_in |= INET_FRAG_LAST_IN;
  306. qp->q.len = end;
  307. } else {
  308. if (end&7) {
  309. end &= ~7;
  310. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  311. skb->ip_summed = CHECKSUM_NONE;
  312. }
  313. if (end > qp->q.len) {
  314. /* Some bits beyond end -> corruption. */
  315. if (qp->q.last_in & INET_FRAG_LAST_IN)
  316. goto err;
  317. qp->q.len = end;
  318. }
  319. }
  320. if (end == offset)
  321. goto err;
  322. err = -ENOMEM;
  323. if (pskb_pull(skb, ihl) == NULL)
  324. goto err;
  325. err = pskb_trim_rcsum(skb, end - offset);
  326. if (err)
  327. goto err;
  328. /* Find out which fragments are in front and at the back of us
  329. * in the chain of fragments so far. We must know where to put
  330. * this fragment, right?
  331. */
  332. prev = qp->q.fragments_tail;
  333. if (!prev || FRAG_CB(prev)->offset < offset) {
  334. next = NULL;
  335. goto found;
  336. }
  337. prev = NULL;
  338. for (next = qp->q.fragments; next != NULL; next = next->next) {
  339. if (FRAG_CB(next)->offset >= offset)
  340. break; /* bingo! */
  341. prev = next;
  342. }
  343. found:
  344. /* We found where to put this one. Check for overlap with
  345. * preceding fragment, and, if needed, align things so that
  346. * any overlaps are eliminated.
  347. */
  348. if (prev) {
  349. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  350. if (i > 0) {
  351. offset += i;
  352. err = -EINVAL;
  353. if (end <= offset)
  354. goto err;
  355. err = -ENOMEM;
  356. if (!pskb_pull(skb, i))
  357. goto err;
  358. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  359. skb->ip_summed = CHECKSUM_NONE;
  360. }
  361. }
  362. err = -ENOMEM;
  363. while (next && FRAG_CB(next)->offset < end) {
  364. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  365. if (i < next->len) {
  366. /* Eat head of the next overlapped fragment
  367. * and leave the loop. The next ones cannot overlap.
  368. */
  369. if (!pskb_pull(next, i))
  370. goto err;
  371. FRAG_CB(next)->offset += i;
  372. qp->q.meat -= i;
  373. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  374. next->ip_summed = CHECKSUM_NONE;
  375. break;
  376. } else {
  377. struct sk_buff *free_it = next;
  378. /* Old fragment is completely overridden with
  379. * new one drop it.
  380. */
  381. next = next->next;
  382. if (prev)
  383. prev->next = next;
  384. else
  385. qp->q.fragments = next;
  386. qp->q.meat -= free_it->len;
  387. sub_frag_mem_limit(&qp->q, free_it->truesize);
  388. kfree_skb(free_it);
  389. }
  390. }
  391. FRAG_CB(skb)->offset = offset;
  392. /* Insert this fragment in the chain of fragments. */
  393. skb->next = next;
  394. if (!next)
  395. qp->q.fragments_tail = skb;
  396. if (prev)
  397. prev->next = skb;
  398. else
  399. qp->q.fragments = skb;
  400. dev = skb->dev;
  401. if (dev) {
  402. qp->iif = dev->ifindex;
  403. skb->dev = NULL;
  404. }
  405. qp->q.stamp = skb->tstamp;
  406. qp->q.meat += skb->len;
  407. qp->ecn |= ecn;
  408. add_frag_mem_limit(&qp->q, skb->truesize);
  409. if (offset == 0)
  410. qp->q.last_in |= INET_FRAG_FIRST_IN;
  411. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  412. skb->len + ihl > qp->q.max_size)
  413. qp->q.max_size = skb->len + ihl;
  414. if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  415. qp->q.meat == qp->q.len)
  416. return ip_frag_reasm(qp, prev, dev);
  417. inet_frag_lru_move(&qp->q);
  418. return -EINPROGRESS;
  419. err:
  420. kfree_skb(skb);
  421. return err;
  422. }
  423. /* Build a new IP datagram from all its fragments. */
  424. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  425. struct net_device *dev)
  426. {
  427. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  428. struct iphdr *iph;
  429. struct sk_buff *fp, *head = qp->q.fragments;
  430. int len;
  431. int ihlen;
  432. int err;
  433. int sum_truesize;
  434. u8 ecn;
  435. ipq_kill(qp);
  436. ecn = ip_frag_ecn_table[qp->ecn];
  437. if (unlikely(ecn == 0xff)) {
  438. err = -EINVAL;
  439. goto out_fail;
  440. }
  441. /* Make the one we just received the head. */
  442. if (prev) {
  443. head = prev->next;
  444. fp = skb_clone(head, GFP_ATOMIC);
  445. if (!fp)
  446. goto out_nomem;
  447. fp->next = head->next;
  448. if (!fp->next)
  449. qp->q.fragments_tail = fp;
  450. prev->next = fp;
  451. skb_morph(head, qp->q.fragments);
  452. head->next = qp->q.fragments->next;
  453. consume_skb(qp->q.fragments);
  454. qp->q.fragments = head;
  455. }
  456. WARN_ON(head == NULL);
  457. WARN_ON(FRAG_CB(head)->offset != 0);
  458. /* Allocate a new buffer for the datagram. */
  459. ihlen = ip_hdrlen(head);
  460. len = ihlen + qp->q.len;
  461. err = -E2BIG;
  462. if (len > 65535)
  463. goto out_oversize;
  464. /* Head of list must not be cloned. */
  465. if (skb_unclone(head, GFP_ATOMIC))
  466. goto out_nomem;
  467. /* If the first fragment is fragmented itself, we split
  468. * it to two chunks: the first with data and paged part
  469. * and the second, holding only fragments. */
  470. if (skb_has_frag_list(head)) {
  471. struct sk_buff *clone;
  472. int i, plen = 0;
  473. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  474. goto out_nomem;
  475. clone->next = head->next;
  476. head->next = clone;
  477. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  478. skb_frag_list_init(head);
  479. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  480. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  481. clone->len = clone->data_len = head->data_len - plen;
  482. head->data_len -= clone->len;
  483. head->len -= clone->len;
  484. clone->csum = 0;
  485. clone->ip_summed = head->ip_summed;
  486. add_frag_mem_limit(&qp->q, clone->truesize);
  487. }
  488. skb_push(head, head->data - skb_network_header(head));
  489. sum_truesize = head->truesize;
  490. for (fp = head->next; fp;) {
  491. bool headstolen;
  492. int delta;
  493. struct sk_buff *next = fp->next;
  494. sum_truesize += fp->truesize;
  495. if (head->ip_summed != fp->ip_summed)
  496. head->ip_summed = CHECKSUM_NONE;
  497. else if (head->ip_summed == CHECKSUM_COMPLETE)
  498. head->csum = csum_add(head->csum, fp->csum);
  499. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  500. kfree_skb_partial(fp, headstolen);
  501. } else {
  502. if (!skb_shinfo(head)->frag_list)
  503. skb_shinfo(head)->frag_list = fp;
  504. head->data_len += fp->len;
  505. head->len += fp->len;
  506. head->truesize += fp->truesize;
  507. }
  508. fp = next;
  509. }
  510. sub_frag_mem_limit(&qp->q, sum_truesize);
  511. head->next = NULL;
  512. head->dev = dev;
  513. head->tstamp = qp->q.stamp;
  514. IPCB(head)->frag_max_size = qp->q.max_size;
  515. iph = ip_hdr(head);
  516. /* max_size != 0 implies at least one fragment had IP_DF set */
  517. iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0;
  518. iph->tot_len = htons(len);
  519. iph->tos |= ecn;
  520. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
  521. qp->q.fragments = NULL;
  522. qp->q.fragments_tail = NULL;
  523. return 0;
  524. out_nomem:
  525. LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"),
  526. qp);
  527. err = -ENOMEM;
  528. goto out_fail;
  529. out_oversize:
  530. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
  531. out_fail:
  532. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  533. return err;
  534. }
  535. /* Process an incoming IP datagram fragment. */
  536. int ip_defrag(struct sk_buff *skb, u32 user)
  537. {
  538. struct ipq *qp;
  539. struct net *net;
  540. net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
  541. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
  542. /* Start by cleaning up the memory. */
  543. ip_evictor(net);
  544. /* Lookup (or create) queue header */
  545. if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
  546. int ret;
  547. spin_lock(&qp->q.lock);
  548. ret = ip_frag_queue(qp, skb);
  549. spin_unlock(&qp->q.lock);
  550. ipq_put(qp);
  551. return ret;
  552. }
  553. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  554. kfree_skb(skb);
  555. return -ENOMEM;
  556. }
  557. EXPORT_SYMBOL(ip_defrag);
  558. struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user)
  559. {
  560. struct iphdr iph;
  561. u32 len;
  562. if (skb->protocol != htons(ETH_P_IP))
  563. return skb;
  564. if (!skb_copy_bits(skb, 0, &iph, sizeof(iph)))
  565. return skb;
  566. if (iph.ihl < 5 || iph.version != 4)
  567. return skb;
  568. len = ntohs(iph.tot_len);
  569. if (skb->len < len || len < (iph.ihl * 4))
  570. return skb;
  571. if (ip_is_fragment(&iph)) {
  572. skb = skb_share_check(skb, GFP_ATOMIC);
  573. if (skb) {
  574. if (!pskb_may_pull(skb, iph.ihl*4))
  575. return skb;
  576. if (pskb_trim_rcsum(skb, len))
  577. return skb;
  578. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  579. if (ip_defrag(skb, user))
  580. return NULL;
  581. skb->rxhash = 0;
  582. }
  583. }
  584. return skb;
  585. }
  586. EXPORT_SYMBOL(ip_check_defrag);
  587. #ifdef CONFIG_SYSCTL
  588. static int zero;
  589. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  590. {
  591. .procname = "ipfrag_high_thresh",
  592. .data = &init_net.ipv4.frags.high_thresh,
  593. .maxlen = sizeof(int),
  594. .mode = 0644,
  595. .proc_handler = proc_dointvec
  596. },
  597. {
  598. .procname = "ipfrag_low_thresh",
  599. .data = &init_net.ipv4.frags.low_thresh,
  600. .maxlen = sizeof(int),
  601. .mode = 0644,
  602. .proc_handler = proc_dointvec
  603. },
  604. {
  605. .procname = "ipfrag_time",
  606. .data = &init_net.ipv4.frags.timeout,
  607. .maxlen = sizeof(int),
  608. .mode = 0644,
  609. .proc_handler = proc_dointvec_jiffies,
  610. },
  611. { }
  612. };
  613. static struct ctl_table ip4_frags_ctl_table[] = {
  614. {
  615. .procname = "ipfrag_secret_interval",
  616. .data = &ip4_frags.secret_interval,
  617. .maxlen = sizeof(int),
  618. .mode = 0644,
  619. .proc_handler = proc_dointvec_jiffies,
  620. },
  621. {
  622. .procname = "ipfrag_max_dist",
  623. .data = &sysctl_ipfrag_max_dist,
  624. .maxlen = sizeof(int),
  625. .mode = 0644,
  626. .proc_handler = proc_dointvec_minmax,
  627. .extra1 = &zero
  628. },
  629. { }
  630. };
  631. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  632. {
  633. struct ctl_table *table;
  634. struct ctl_table_header *hdr;
  635. table = ip4_frags_ns_ctl_table;
  636. if (!net_eq(net, &init_net)) {
  637. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  638. if (table == NULL)
  639. goto err_alloc;
  640. table[0].data = &net->ipv4.frags.high_thresh;
  641. table[1].data = &net->ipv4.frags.low_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 ip4_frags_ctl_register(void)
  666. {
  667. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  668. }
  669. #else
  670. static inline int ip4_frags_ns_ctl_register(struct net *net)
  671. {
  672. return 0;
  673. }
  674. static inline void ip4_frags_ns_ctl_unregister(struct net *net)
  675. {
  676. }
  677. static inline void 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.secret_interval = 10 * 60 * HZ;
  729. inet_frags_init(&ip4_frags);
  730. }