ip_fragment.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
  9. *
  10. * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
  11. * Alan Cox <Alan.Cox@linux.org>
  12. *
  13. * Fixes:
  14. * Alan Cox : Split from ip.c , see ip_input.c for history.
  15. * David S. Miller : Begin massive cleanup...
  16. * Andi Kleen : Add sysctls.
  17. * xxxx : Overlapfrag bug.
  18. * Ultima : ip_expire() kernel panic.
  19. * Bill Hawes : Frag accounting and evictor fixes.
  20. * John McDonald : 0 length frag bug.
  21. * Alexey Kuznetsov: SMP races, threading, cleanup.
  22. * Patrick McHardy : LRU queue of frag heads for evictor.
  23. */
  24. #include <linux/config.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 <net/sock.h>
  37. #include <net/ip.h>
  38. #include <net/icmp.h>
  39. #include <net/checksum.h>
  40. #include <linux/tcp.h>
  41. #include <linux/udp.h>
  42. #include <linux/inet.h>
  43. #include <linux/netfilter_ipv4.h>
  44. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  45. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  46. * as well. Or notify me, at least. --ANK
  47. */
  48. /* Fragment cache limits. We will commit 256K at one time. Should we
  49. * cross that limit we will prune down to 192K. This should cope with
  50. * even the most extreme cases without allowing an attacker to measurably
  51. * harm machine performance.
  52. */
  53. int sysctl_ipfrag_high_thresh = 256*1024;
  54. int sysctl_ipfrag_low_thresh = 192*1024;
  55. /* Important NOTE! Fragment queue must be destroyed before MSL expires.
  56. * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
  57. */
  58. int sysctl_ipfrag_time = IP_FRAG_TIME;
  59. struct ipfrag_skb_cb
  60. {
  61. struct inet_skb_parm h;
  62. int offset;
  63. };
  64. #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
  65. /* Describe an entry in the "incomplete datagrams" queue. */
  66. struct ipq {
  67. struct ipq *next; /* linked list pointers */
  68. struct list_head lru_list; /* lru list member */
  69. u32 user;
  70. u32 saddr;
  71. u32 daddr;
  72. u16 id;
  73. u8 protocol;
  74. u8 last_in;
  75. #define COMPLETE 4
  76. #define FIRST_IN 2
  77. #define LAST_IN 1
  78. struct sk_buff *fragments; /* linked list of received fragments */
  79. int len; /* total length of original datagram */
  80. int meat;
  81. spinlock_t lock;
  82. atomic_t refcnt;
  83. struct timer_list timer; /* when will this queue expire? */
  84. struct ipq **pprev;
  85. int iif;
  86. struct timeval stamp;
  87. };
  88. /* Hash table. */
  89. #define IPQ_HASHSZ 64
  90. /* Per-bucket lock is easy to add now. */
  91. static struct ipq *ipq_hash[IPQ_HASHSZ];
  92. static DEFINE_RWLOCK(ipfrag_lock);
  93. static u32 ipfrag_hash_rnd;
  94. static LIST_HEAD(ipq_lru_list);
  95. int ip_frag_nqueues = 0;
  96. static __inline__ void __ipq_unlink(struct ipq *qp)
  97. {
  98. if(qp->next)
  99. qp->next->pprev = qp->pprev;
  100. *qp->pprev = qp->next;
  101. list_del(&qp->lru_list);
  102. ip_frag_nqueues--;
  103. }
  104. static __inline__ void ipq_unlink(struct ipq *ipq)
  105. {
  106. write_lock(&ipfrag_lock);
  107. __ipq_unlink(ipq);
  108. write_unlock(&ipfrag_lock);
  109. }
  110. static unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
  111. {
  112. return jhash_3words((u32)id << 16 | prot, saddr, daddr,
  113. ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
  114. }
  115. static struct timer_list ipfrag_secret_timer;
  116. int sysctl_ipfrag_secret_interval = 10 * 60 * HZ;
  117. static void ipfrag_secret_rebuild(unsigned long dummy)
  118. {
  119. unsigned long now = jiffies;
  120. int i;
  121. write_lock(&ipfrag_lock);
  122. get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
  123. for (i = 0; i < IPQ_HASHSZ; i++) {
  124. struct ipq *q;
  125. q = ipq_hash[i];
  126. while (q) {
  127. struct ipq *next = q->next;
  128. unsigned int hval = ipqhashfn(q->id, q->saddr,
  129. q->daddr, q->protocol);
  130. if (hval != i) {
  131. /* Unlink. */
  132. if (q->next)
  133. q->next->pprev = q->pprev;
  134. *q->pprev = q->next;
  135. /* Relink to new hash chain. */
  136. if ((q->next = ipq_hash[hval]) != NULL)
  137. q->next->pprev = &q->next;
  138. ipq_hash[hval] = q;
  139. q->pprev = &ipq_hash[hval];
  140. }
  141. q = next;
  142. }
  143. }
  144. write_unlock(&ipfrag_lock);
  145. mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
  146. }
  147. atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
  148. /* Memory Tracking Functions. */
  149. static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
  150. {
  151. if (work)
  152. *work -= skb->truesize;
  153. atomic_sub(skb->truesize, &ip_frag_mem);
  154. kfree_skb(skb);
  155. }
  156. static __inline__ void frag_free_queue(struct ipq *qp, int *work)
  157. {
  158. if (work)
  159. *work -= sizeof(struct ipq);
  160. atomic_sub(sizeof(struct ipq), &ip_frag_mem);
  161. kfree(qp);
  162. }
  163. static __inline__ struct ipq *frag_alloc_queue(void)
  164. {
  165. struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
  166. if(!qp)
  167. return NULL;
  168. atomic_add(sizeof(struct ipq), &ip_frag_mem);
  169. return qp;
  170. }
  171. /* Destruction primitives. */
  172. /* Complete destruction of ipq. */
  173. static void ip_frag_destroy(struct ipq *qp, int *work)
  174. {
  175. struct sk_buff *fp;
  176. BUG_TRAP(qp->last_in&COMPLETE);
  177. BUG_TRAP(del_timer(&qp->timer) == 0);
  178. /* Release all fragment data. */
  179. fp = qp->fragments;
  180. while (fp) {
  181. struct sk_buff *xp = fp->next;
  182. frag_kfree_skb(fp, work);
  183. fp = xp;
  184. }
  185. /* Finally, release the queue descriptor itself. */
  186. frag_free_queue(qp, work);
  187. }
  188. static __inline__ void ipq_put(struct ipq *ipq, int *work)
  189. {
  190. if (atomic_dec_and_test(&ipq->refcnt))
  191. ip_frag_destroy(ipq, work);
  192. }
  193. /* Kill ipq entry. It is not destroyed immediately,
  194. * because caller (and someone more) holds reference count.
  195. */
  196. static void ipq_kill(struct ipq *ipq)
  197. {
  198. if (del_timer(&ipq->timer))
  199. atomic_dec(&ipq->refcnt);
  200. if (!(ipq->last_in & COMPLETE)) {
  201. ipq_unlink(ipq);
  202. atomic_dec(&ipq->refcnt);
  203. ipq->last_in |= COMPLETE;
  204. }
  205. }
  206. /* Memory limiting on fragments. Evictor trashes the oldest
  207. * fragment queue until we are back under the threshold.
  208. */
  209. static void ip_evictor(void)
  210. {
  211. struct ipq *qp;
  212. struct list_head *tmp;
  213. int work;
  214. work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
  215. if (work <= 0)
  216. return;
  217. while (work > 0) {
  218. read_lock(&ipfrag_lock);
  219. if (list_empty(&ipq_lru_list)) {
  220. read_unlock(&ipfrag_lock);
  221. return;
  222. }
  223. tmp = ipq_lru_list.next;
  224. qp = list_entry(tmp, struct ipq, lru_list);
  225. atomic_inc(&qp->refcnt);
  226. read_unlock(&ipfrag_lock);
  227. spin_lock(&qp->lock);
  228. if (!(qp->last_in&COMPLETE))
  229. ipq_kill(qp);
  230. spin_unlock(&qp->lock);
  231. ipq_put(qp, &work);
  232. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  233. }
  234. }
  235. /*
  236. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  237. */
  238. static void ip_expire(unsigned long arg)
  239. {
  240. struct ipq *qp = (struct ipq *) arg;
  241. spin_lock(&qp->lock);
  242. if (qp->last_in & COMPLETE)
  243. goto out;
  244. ipq_kill(qp);
  245. IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
  246. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  247. if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
  248. struct sk_buff *head = qp->fragments;
  249. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  250. if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
  251. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  252. dev_put(head->dev);
  253. }
  254. }
  255. out:
  256. spin_unlock(&qp->lock);
  257. ipq_put(qp, NULL);
  258. }
  259. /* Creation primitives. */
  260. static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
  261. {
  262. struct ipq *qp;
  263. write_lock(&ipfrag_lock);
  264. #ifdef CONFIG_SMP
  265. /* With SMP race we have to recheck hash table, because
  266. * such entry could be created on other cpu, while we
  267. * promoted read lock to write lock.
  268. */
  269. for(qp = ipq_hash[hash]; qp; qp = qp->next) {
  270. if(qp->id == qp_in->id &&
  271. qp->saddr == qp_in->saddr &&
  272. qp->daddr == qp_in->daddr &&
  273. qp->protocol == qp_in->protocol &&
  274. qp->user == qp_in->user) {
  275. atomic_inc(&qp->refcnt);
  276. write_unlock(&ipfrag_lock);
  277. qp_in->last_in |= COMPLETE;
  278. ipq_put(qp_in, NULL);
  279. return qp;
  280. }
  281. }
  282. #endif
  283. qp = qp_in;
  284. if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
  285. atomic_inc(&qp->refcnt);
  286. atomic_inc(&qp->refcnt);
  287. if((qp->next = ipq_hash[hash]) != NULL)
  288. qp->next->pprev = &qp->next;
  289. ipq_hash[hash] = qp;
  290. qp->pprev = &ipq_hash[hash];
  291. INIT_LIST_HEAD(&qp->lru_list);
  292. list_add_tail(&qp->lru_list, &ipq_lru_list);
  293. ip_frag_nqueues++;
  294. write_unlock(&ipfrag_lock);
  295. return qp;
  296. }
  297. /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
  298. static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32 user)
  299. {
  300. struct ipq *qp;
  301. if ((qp = frag_alloc_queue()) == NULL)
  302. goto out_nomem;
  303. qp->protocol = iph->protocol;
  304. qp->last_in = 0;
  305. qp->id = iph->id;
  306. qp->saddr = iph->saddr;
  307. qp->daddr = iph->daddr;
  308. qp->user = user;
  309. qp->len = 0;
  310. qp->meat = 0;
  311. qp->fragments = NULL;
  312. qp->iif = 0;
  313. /* Initialize a timer for this entry. */
  314. init_timer(&qp->timer);
  315. qp->timer.data = (unsigned long) qp; /* pointer to queue */
  316. qp->timer.function = ip_expire; /* expire function */
  317. spin_lock_init(&qp->lock);
  318. atomic_set(&qp->refcnt, 1);
  319. return ip_frag_intern(hash, qp);
  320. out_nomem:
  321. NETDEBUG(if (net_ratelimit()) printk(KERN_ERR "ip_frag_create: no memory left !\n"));
  322. return NULL;
  323. }
  324. /* Find the correct entry in the "incomplete datagrams" queue for
  325. * this IP datagram, and create new one, if nothing is found.
  326. */
  327. static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
  328. {
  329. __u16 id = iph->id;
  330. __u32 saddr = iph->saddr;
  331. __u32 daddr = iph->daddr;
  332. __u8 protocol = iph->protocol;
  333. unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
  334. struct ipq *qp;
  335. read_lock(&ipfrag_lock);
  336. for(qp = ipq_hash[hash]; qp; qp = qp->next) {
  337. if(qp->id == id &&
  338. qp->saddr == saddr &&
  339. qp->daddr == daddr &&
  340. qp->protocol == protocol &&
  341. qp->user == user) {
  342. atomic_inc(&qp->refcnt);
  343. read_unlock(&ipfrag_lock);
  344. return qp;
  345. }
  346. }
  347. read_unlock(&ipfrag_lock);
  348. return ip_frag_create(hash, iph, user);
  349. }
  350. /* Add new segment to existing queue. */
  351. static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  352. {
  353. struct sk_buff *prev, *next;
  354. int flags, offset;
  355. int ihl, end;
  356. if (qp->last_in & COMPLETE)
  357. goto err;
  358. offset = ntohs(skb->nh.iph->frag_off);
  359. flags = offset & ~IP_OFFSET;
  360. offset &= IP_OFFSET;
  361. offset <<= 3; /* offset is in 8-byte chunks */
  362. ihl = skb->nh.iph->ihl * 4;
  363. /* Determine the position of this fragment. */
  364. end = offset + skb->len - ihl;
  365. /* Is this the final fragment? */
  366. if ((flags & IP_MF) == 0) {
  367. /* If we already have some bits beyond end
  368. * or have different end, the segment is corrrupted.
  369. */
  370. if (end < qp->len ||
  371. ((qp->last_in & LAST_IN) && end != qp->len))
  372. goto err;
  373. qp->last_in |= LAST_IN;
  374. qp->len = end;
  375. } else {
  376. if (end&7) {
  377. end &= ~7;
  378. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  379. skb->ip_summed = CHECKSUM_NONE;
  380. }
  381. if (end > qp->len) {
  382. /* Some bits beyond end -> corruption. */
  383. if (qp->last_in & LAST_IN)
  384. goto err;
  385. qp->len = end;
  386. }
  387. }
  388. if (end == offset)
  389. goto err;
  390. if (pskb_pull(skb, ihl) == NULL)
  391. goto err;
  392. if (pskb_trim(skb, end-offset))
  393. goto err;
  394. /* Find out which fragments are in front and at the back of us
  395. * in the chain of fragments so far. We must know where to put
  396. * this fragment, right?
  397. */
  398. prev = NULL;
  399. for(next = qp->fragments; next != NULL; next = next->next) {
  400. if (FRAG_CB(next)->offset >= offset)
  401. break; /* bingo! */
  402. prev = next;
  403. }
  404. /* We found where to put this one. Check for overlap with
  405. * preceding fragment, and, if needed, align things so that
  406. * any overlaps are eliminated.
  407. */
  408. if (prev) {
  409. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  410. if (i > 0) {
  411. offset += i;
  412. if (end <= offset)
  413. goto err;
  414. if (!pskb_pull(skb, i))
  415. goto err;
  416. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  417. skb->ip_summed = CHECKSUM_NONE;
  418. }
  419. }
  420. while (next && FRAG_CB(next)->offset < end) {
  421. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  422. if (i < next->len) {
  423. /* Eat head of the next overlapped fragment
  424. * and leave the loop. The next ones cannot overlap.
  425. */
  426. if (!pskb_pull(next, i))
  427. goto err;
  428. FRAG_CB(next)->offset += i;
  429. qp->meat -= i;
  430. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  431. next->ip_summed = CHECKSUM_NONE;
  432. break;
  433. } else {
  434. struct sk_buff *free_it = next;
  435. /* Old fragmnet is completely overridden with
  436. * new one drop it.
  437. */
  438. next = next->next;
  439. if (prev)
  440. prev->next = next;
  441. else
  442. qp->fragments = next;
  443. qp->meat -= free_it->len;
  444. frag_kfree_skb(free_it, NULL);
  445. }
  446. }
  447. FRAG_CB(skb)->offset = offset;
  448. /* Insert this fragment in the chain of fragments. */
  449. skb->next = next;
  450. if (prev)
  451. prev->next = skb;
  452. else
  453. qp->fragments = skb;
  454. if (skb->dev)
  455. qp->iif = skb->dev->ifindex;
  456. skb->dev = NULL;
  457. qp->stamp = skb->stamp;
  458. qp->meat += skb->len;
  459. atomic_add(skb->truesize, &ip_frag_mem);
  460. if (offset == 0)
  461. qp->last_in |= FIRST_IN;
  462. write_lock(&ipfrag_lock);
  463. list_move_tail(&qp->lru_list, &ipq_lru_list);
  464. write_unlock(&ipfrag_lock);
  465. return;
  466. err:
  467. kfree_skb(skb);
  468. }
  469. /* Build a new IP datagram from all its fragments. */
  470. static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
  471. {
  472. struct iphdr *iph;
  473. struct sk_buff *fp, *head = qp->fragments;
  474. int len;
  475. int ihlen;
  476. ipq_kill(qp);
  477. BUG_TRAP(head != NULL);
  478. BUG_TRAP(FRAG_CB(head)->offset == 0);
  479. /* Allocate a new buffer for the datagram. */
  480. ihlen = head->nh.iph->ihl*4;
  481. len = ihlen + qp->len;
  482. if(len > 65535)
  483. goto out_oversize;
  484. /* Head of list must not be cloned. */
  485. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  486. goto out_nomem;
  487. /* If the first fragment is fragmented itself, we split
  488. * it to two chunks: the first with data and paged part
  489. * and the second, holding only fragments. */
  490. if (skb_shinfo(head)->frag_list) {
  491. struct sk_buff *clone;
  492. int i, plen = 0;
  493. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  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_shinfo(head)->frag_list = NULL;
  499. for (i=0; i<skb_shinfo(head)->nr_frags; i++)
  500. plen += skb_shinfo(head)->frags[i].size;
  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. atomic_add(clone->truesize, &ip_frag_mem);
  507. }
  508. skb_shinfo(head)->frag_list = head->next;
  509. skb_push(head, head->data - head->nh.raw);
  510. atomic_sub(head->truesize, &ip_frag_mem);
  511. for (fp=head->next; fp; fp = fp->next) {
  512. head->data_len += fp->len;
  513. head->len += fp->len;
  514. if (head->ip_summed != fp->ip_summed)
  515. head->ip_summed = CHECKSUM_NONE;
  516. else if (head->ip_summed == CHECKSUM_HW)
  517. head->csum = csum_add(head->csum, fp->csum);
  518. head->truesize += fp->truesize;
  519. atomic_sub(fp->truesize, &ip_frag_mem);
  520. }
  521. head->next = NULL;
  522. head->dev = dev;
  523. head->stamp = qp->stamp;
  524. iph = head->nh.iph;
  525. iph->frag_off = 0;
  526. iph->tot_len = htons(len);
  527. IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
  528. qp->fragments = NULL;
  529. return head;
  530. out_nomem:
  531. NETDEBUG(if (net_ratelimit())
  532. printk(KERN_ERR
  533. "IP: queue_glue: no memory for gluing queue %p\n",
  534. qp));
  535. goto out_fail;
  536. out_oversize:
  537. if (net_ratelimit())
  538. printk(KERN_INFO
  539. "Oversized IP packet from %d.%d.%d.%d.\n",
  540. NIPQUAD(qp->saddr));
  541. out_fail:
  542. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  543. return NULL;
  544. }
  545. /* Process an incoming IP datagram fragment. */
  546. struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user)
  547. {
  548. struct iphdr *iph = skb->nh.iph;
  549. struct ipq *qp;
  550. struct net_device *dev;
  551. IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
  552. /* Start by cleaning up the memory. */
  553. if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
  554. ip_evictor();
  555. dev = skb->dev;
  556. /* Lookup (or create) queue header */
  557. if ((qp = ip_find(iph, user)) != NULL) {
  558. struct sk_buff *ret = NULL;
  559. spin_lock(&qp->lock);
  560. ip_frag_queue(qp, skb);
  561. if (qp->last_in == (FIRST_IN|LAST_IN) &&
  562. qp->meat == qp->len)
  563. ret = ip_frag_reasm(qp, dev);
  564. spin_unlock(&qp->lock);
  565. ipq_put(qp, NULL);
  566. return ret;
  567. }
  568. IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
  569. kfree_skb(skb);
  570. return NULL;
  571. }
  572. void ipfrag_init(void)
  573. {
  574. ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  575. (jiffies ^ (jiffies >> 6)));
  576. init_timer(&ipfrag_secret_timer);
  577. ipfrag_secret_timer.function = ipfrag_secret_rebuild;
  578. ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
  579. add_timer(&ipfrag_secret_timer);
  580. }
  581. EXPORT_SYMBOL(ip_defrag);