ip_fragment.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. #include <linux/compiler.h>
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/mm.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/list.h>
  29. #include <linux/ip.h>
  30. #include <linux/icmp.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/jhash.h>
  33. #include <linux/random.h>
  34. #include <linux/slab.h>
  35. #include <net/route.h>
  36. #include <net/dst.h>
  37. #include <net/sock.h>
  38. #include <net/ip.h>
  39. #include <net/icmp.h>
  40. #include <net/checksum.h>
  41. #include <net/inetpeer.h>
  42. #include <net/inet_frag.h>
  43. #include <linux/tcp.h>
  44. #include <linux/udp.h>
  45. #include <linux/inet.h>
  46. #include <linux/netfilter_ipv4.h>
  47. #include <net/inet_ecn.h>
  48. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  49. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  50. * as well. Or notify me, at least. --ANK
  51. */
  52. static int sysctl_ipfrag_max_dist __read_mostly = 64;
  53. struct ipfrag_skb_cb
  54. {
  55. struct inet_skb_parm h;
  56. int offset;
  57. };
  58. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  59. /* Describe an entry in the "incomplete datagrams" queue. */
  60. struct ipq {
  61. struct inet_frag_queue q;
  62. u32 user;
  63. __be32 saddr;
  64. __be32 daddr;
  65. __be16 id;
  66. u8 protocol;
  67. u8 ecn; /* RFC3168 support */
  68. int iif;
  69. unsigned int rid;
  70. struct inet_peer *peer;
  71. };
  72. #define IPFRAG_ECN_CLEAR 0x01 /* one frag had INET_ECN_NOT_ECT */
  73. #define IPFRAG_ECN_SET_CE 0x04 /* one frag had INET_ECN_CE */
  74. static inline u8 ip4_frag_ecn(u8 tos)
  75. {
  76. tos = (tos & INET_ECN_MASK) + 1;
  77. /*
  78. * After the last operation we have (in binary):
  79. * INET_ECN_NOT_ECT => 001
  80. * INET_ECN_ECT_1 => 010
  81. * INET_ECN_ECT_0 => 011
  82. * INET_ECN_CE => 100
  83. */
  84. return (tos & 2) ? 0 : tos;
  85. }
  86. static struct inet_frags ip4_frags;
  87. int ip_frag_nqueues(struct net *net)
  88. {
  89. return net->ipv4.frags.nqueues;
  90. }
  91. int ip_frag_mem(struct net *net)
  92. {
  93. return atomic_read(&net->ipv4.frags.mem);
  94. }
  95. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  96. struct net_device *dev);
  97. struct ip4_create_arg {
  98. struct iphdr *iph;
  99. u32 user;
  100. };
  101. static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
  102. {
  103. return jhash_3words((__force u32)id << 16 | prot,
  104. (__force u32)saddr, (__force u32)daddr,
  105. ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
  106. }
  107. static unsigned int ip4_hashfn(struct inet_frag_queue *q)
  108. {
  109. struct ipq *ipq;
  110. ipq = container_of(q, struct ipq, q);
  111. return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
  112. }
  113. static int ip4_frag_match(struct inet_frag_queue *q, void *a)
  114. {
  115. struct ipq *qp;
  116. struct ip4_create_arg *arg = a;
  117. qp = container_of(q, struct ipq, q);
  118. return qp->id == arg->iph->id &&
  119. qp->saddr == arg->iph->saddr &&
  120. qp->daddr == arg->iph->daddr &&
  121. qp->protocol == arg->iph->protocol &&
  122. qp->user == arg->user;
  123. }
  124. /* Memory Tracking Functions. */
  125. static void frag_kfree_skb(struct netns_frags *nf, struct sk_buff *skb)
  126. {
  127. atomic_sub(skb->truesize, &nf->mem);
  128. kfree_skb(skb);
  129. }
  130. static void ip4_frag_init(struct inet_frag_queue *q, void *a)
  131. {
  132. struct ipq *qp = container_of(q, struct ipq, q);
  133. struct ip4_create_arg *arg = a;
  134. qp->protocol = arg->iph->protocol;
  135. qp->id = arg->iph->id;
  136. qp->ecn = ip4_frag_ecn(arg->iph->tos);
  137. qp->saddr = arg->iph->saddr;
  138. qp->daddr = arg->iph->daddr;
  139. qp->user = arg->user;
  140. qp->peer = sysctl_ipfrag_max_dist ?
  141. inet_getpeer_v4(arg->iph->saddr, 1) : NULL;
  142. }
  143. static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
  144. {
  145. struct ipq *qp;
  146. qp = container_of(q, struct ipq, q);
  147. if (qp->peer)
  148. inet_putpeer(qp->peer);
  149. }
  150. /* Destruction primitives. */
  151. static __inline__ void ipq_put(struct ipq *ipq)
  152. {
  153. inet_frag_put(&ipq->q, &ip4_frags);
  154. }
  155. /* Kill ipq entry. It is not destroyed immediately,
  156. * because caller (and someone more) holds reference count.
  157. */
  158. static void ipq_kill(struct ipq *ipq)
  159. {
  160. inet_frag_kill(&ipq->q, &ip4_frags);
  161. }
  162. /* Memory limiting on fragments. Evictor trashes the oldest
  163. * fragment queue until we are back under the threshold.
  164. */
  165. static void ip_evictor(struct net *net)
  166. {
  167. int evicted;
  168. evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
  169. if (evicted)
  170. IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
  171. }
  172. /*
  173. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  174. */
  175. static void ip_expire(unsigned long arg)
  176. {
  177. struct ipq *qp;
  178. struct net *net;
  179. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  180. net = container_of(qp->q.net, struct net, ipv4.frags);
  181. spin_lock(&qp->q.lock);
  182. if (qp->q.last_in & INET_FRAG_COMPLETE)
  183. goto out;
  184. ipq_kill(qp);
  185. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
  186. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  187. if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
  188. struct sk_buff *head = qp->q.fragments;
  189. const struct iphdr *iph;
  190. int err;
  191. rcu_read_lock();
  192. head->dev = dev_get_by_index_rcu(net, qp->iif);
  193. if (!head->dev)
  194. goto out_rcu_unlock;
  195. /* skb dst is stale, drop it, and perform route lookup again */
  196. skb_dst_drop(head);
  197. iph = ip_hdr(head);
  198. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  199. iph->tos, head->dev);
  200. if (err)
  201. goto out_rcu_unlock;
  202. /*
  203. * Only an end host needs to send an ICMP
  204. * "Fragment Reassembly Timeout" message, per RFC792.
  205. */
  206. if (qp->user == IP_DEFRAG_CONNTRACK_IN &&
  207. skb_rtable(head)->rt_type != RTN_LOCAL)
  208. goto out_rcu_unlock;
  209. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  210. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  211. out_rcu_unlock:
  212. rcu_read_unlock();
  213. }
  214. out:
  215. spin_unlock(&qp->q.lock);
  216. ipq_put(qp);
  217. }
  218. /* Find the correct entry in the "incomplete datagrams" queue for
  219. * this IP datagram, and create new one, if nothing is found.
  220. */
  221. static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
  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. read_lock(&ip4_frags.lock);
  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 (q == NULL)
  232. goto out_nomem;
  233. return container_of(q, struct ipq, q);
  234. out_nomem:
  235. LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
  236. return NULL;
  237. }
  238. /* Is the fragment too far ahead to be part of ipq? */
  239. static inline int ip_frag_too_far(struct ipq *qp)
  240. {
  241. struct inet_peer *peer = qp->peer;
  242. unsigned int max = sysctl_ipfrag_max_dist;
  243. unsigned int start, end;
  244. int rc;
  245. if (!peer || !max)
  246. return 0;
  247. start = qp->rid;
  248. end = atomic_inc_return(&peer->rid);
  249. qp->rid = end;
  250. rc = qp->q.fragments && (end - start) > max;
  251. if (rc) {
  252. struct net *net;
  253. net = container_of(qp->q.net, struct net, ipv4.frags);
  254. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  255. }
  256. return rc;
  257. }
  258. static int ip_frag_reinit(struct ipq *qp)
  259. {
  260. struct sk_buff *fp;
  261. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  262. atomic_inc(&qp->q.refcnt);
  263. return -ETIMEDOUT;
  264. }
  265. fp = qp->q.fragments;
  266. do {
  267. struct sk_buff *xp = fp->next;
  268. frag_kfree_skb(qp->q.net, fp);
  269. fp = xp;
  270. } while (fp);
  271. qp->q.last_in = 0;
  272. qp->q.len = 0;
  273. qp->q.meat = 0;
  274. qp->q.fragments = NULL;
  275. qp->q.fragments_tail = NULL;
  276. qp->iif = 0;
  277. qp->ecn = 0;
  278. return 0;
  279. }
  280. /* Add new segment to existing queue. */
  281. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  282. {
  283. struct sk_buff *prev, *next;
  284. struct net_device *dev;
  285. int flags, offset;
  286. int ihl, end;
  287. int err = -ENOENT;
  288. u8 ecn;
  289. if (qp->q.last_in & INET_FRAG_COMPLETE)
  290. goto err;
  291. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  292. unlikely(ip_frag_too_far(qp)) &&
  293. unlikely(err = ip_frag_reinit(qp))) {
  294. ipq_kill(qp);
  295. goto err;
  296. }
  297. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  298. offset = ntohs(ip_hdr(skb)->frag_off);
  299. flags = offset & ~IP_OFFSET;
  300. offset &= IP_OFFSET;
  301. offset <<= 3; /* offset is in 8-byte chunks */
  302. ihl = ip_hdrlen(skb);
  303. /* Determine the position of this fragment. */
  304. end = offset + skb->len - ihl;
  305. err = -EINVAL;
  306. /* Is this the final fragment? */
  307. if ((flags & IP_MF) == 0) {
  308. /* If we already have some bits beyond end
  309. * or have different end, the segment is corrrupted.
  310. */
  311. if (end < qp->q.len ||
  312. ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
  313. goto err;
  314. qp->q.last_in |= INET_FRAG_LAST_IN;
  315. qp->q.len = end;
  316. } else {
  317. if (end&7) {
  318. end &= ~7;
  319. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  320. skb->ip_summed = CHECKSUM_NONE;
  321. }
  322. if (end > qp->q.len) {
  323. /* Some bits beyond end -> corruption. */
  324. if (qp->q.last_in & INET_FRAG_LAST_IN)
  325. goto err;
  326. qp->q.len = end;
  327. }
  328. }
  329. if (end == offset)
  330. goto err;
  331. err = -ENOMEM;
  332. if (pskb_pull(skb, ihl) == NULL)
  333. goto err;
  334. err = pskb_trim_rcsum(skb, end - offset);
  335. if (err)
  336. goto err;
  337. /* Find out which fragments are in front and at the back of us
  338. * in the chain of fragments so far. We must know where to put
  339. * this fragment, right?
  340. */
  341. prev = qp->q.fragments_tail;
  342. if (!prev || FRAG_CB(prev)->offset < offset) {
  343. next = NULL;
  344. goto found;
  345. }
  346. prev = NULL;
  347. for (next = qp->q.fragments; next != NULL; next = next->next) {
  348. if (FRAG_CB(next)->offset >= offset)
  349. break; /* bingo! */
  350. prev = next;
  351. }
  352. found:
  353. /* We found where to put this one. Check for overlap with
  354. * preceding fragment, and, if needed, align things so that
  355. * any overlaps are eliminated.
  356. */
  357. if (prev) {
  358. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  359. if (i > 0) {
  360. offset += i;
  361. err = -EINVAL;
  362. if (end <= offset)
  363. goto err;
  364. err = -ENOMEM;
  365. if (!pskb_pull(skb, i))
  366. goto err;
  367. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  368. skb->ip_summed = CHECKSUM_NONE;
  369. }
  370. }
  371. err = -ENOMEM;
  372. while (next && FRAG_CB(next)->offset < end) {
  373. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  374. if (i < next->len) {
  375. /* Eat head of the next overlapped fragment
  376. * and leave the loop. The next ones cannot overlap.
  377. */
  378. if (!pskb_pull(next, i))
  379. goto err;
  380. FRAG_CB(next)->offset += i;
  381. qp->q.meat -= i;
  382. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  383. next->ip_summed = CHECKSUM_NONE;
  384. break;
  385. } else {
  386. struct sk_buff *free_it = next;
  387. /* Old fragment is completely overridden with
  388. * new one drop it.
  389. */
  390. next = next->next;
  391. if (prev)
  392. prev->next = next;
  393. else
  394. qp->q.fragments = next;
  395. qp->q.meat -= free_it->len;
  396. frag_kfree_skb(qp->q.net, free_it);
  397. }
  398. }
  399. FRAG_CB(skb)->offset = offset;
  400. /* Insert this fragment in the chain of fragments. */
  401. skb->next = next;
  402. if (!next)
  403. qp->q.fragments_tail = skb;
  404. if (prev)
  405. prev->next = skb;
  406. else
  407. qp->q.fragments = skb;
  408. dev = skb->dev;
  409. if (dev) {
  410. qp->iif = dev->ifindex;
  411. skb->dev = NULL;
  412. }
  413. qp->q.stamp = skb->tstamp;
  414. qp->q.meat += skb->len;
  415. qp->ecn |= ecn;
  416. atomic_add(skb->truesize, &qp->q.net->mem);
  417. if (offset == 0)
  418. qp->q.last_in |= INET_FRAG_FIRST_IN;
  419. if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  420. qp->q.meat == qp->q.len)
  421. return ip_frag_reasm(qp, prev, dev);
  422. write_lock(&ip4_frags.lock);
  423. list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
  424. write_unlock(&ip4_frags.lock);
  425. return -EINPROGRESS;
  426. err:
  427. kfree_skb(skb);
  428. return err;
  429. }
  430. /* Build a new IP datagram from all its fragments. */
  431. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  432. struct net_device *dev)
  433. {
  434. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  435. struct iphdr *iph;
  436. struct sk_buff *fp, *head = qp->q.fragments;
  437. int len;
  438. int ihlen;
  439. int err;
  440. ipq_kill(qp);
  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. kfree_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_cloned(head) && pskb_expand_head(head, 0, 0, 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_shinfo(head)->frags[i].size;
  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. atomic_add(clone->truesize, &qp->q.net->mem);
  487. }
  488. skb_shinfo(head)->frag_list = head->next;
  489. skb_push(head, head->data - skb_network_header(head));
  490. for (fp=head->next; fp; fp = fp->next) {
  491. head->data_len += fp->len;
  492. head->len += fp->len;
  493. if (head->ip_summed != fp->ip_summed)
  494. head->ip_summed = CHECKSUM_NONE;
  495. else if (head->ip_summed == CHECKSUM_COMPLETE)
  496. head->csum = csum_add(head->csum, fp->csum);
  497. head->truesize += fp->truesize;
  498. }
  499. atomic_sub(head->truesize, &qp->q.net->mem);
  500. head->next = NULL;
  501. head->dev = dev;
  502. head->tstamp = qp->q.stamp;
  503. iph = ip_hdr(head);
  504. iph->frag_off = 0;
  505. iph->tot_len = htons(len);
  506. /* RFC3168 5.3 Fragmentation support
  507. * If one fragment had INET_ECN_NOT_ECT,
  508. * reassembled frame also has INET_ECN_NOT_ECT
  509. * Elif one fragment had INET_ECN_CE
  510. * reassembled frame also has INET_ECN_CE
  511. */
  512. if (qp->ecn & IPFRAG_ECN_CLEAR)
  513. iph->tos &= ~INET_ECN_MASK;
  514. else if (qp->ecn & IPFRAG_ECN_SET_CE)
  515. iph->tos |= INET_ECN_CE;
  516. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
  517. qp->q.fragments = NULL;
  518. qp->q.fragments_tail = NULL;
  519. return 0;
  520. out_nomem:
  521. LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
  522. "queue %p\n", qp);
  523. err = -ENOMEM;
  524. goto out_fail;
  525. out_oversize:
  526. if (net_ratelimit())
  527. printk(KERN_INFO "Oversized IP packet from %pI4.\n",
  528. &qp->saddr);
  529. out_fail:
  530. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  531. return err;
  532. }
  533. /* Process an incoming IP datagram fragment. */
  534. int ip_defrag(struct sk_buff *skb, u32 user)
  535. {
  536. struct ipq *qp;
  537. struct net *net;
  538. net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
  539. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
  540. /* Start by cleaning up the memory. */
  541. if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
  542. ip_evictor(net);
  543. /* Lookup (or create) queue header */
  544. if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
  545. int ret;
  546. spin_lock(&qp->q.lock);
  547. ret = ip_frag_queue(qp, skb);
  548. spin_unlock(&qp->q.lock);
  549. ipq_put(qp);
  550. return ret;
  551. }
  552. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  553. kfree_skb(skb);
  554. return -ENOMEM;
  555. }
  556. EXPORT_SYMBOL(ip_defrag);
  557. #ifdef CONFIG_SYSCTL
  558. static int zero;
  559. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  560. {
  561. .procname = "ipfrag_high_thresh",
  562. .data = &init_net.ipv4.frags.high_thresh,
  563. .maxlen = sizeof(int),
  564. .mode = 0644,
  565. .proc_handler = proc_dointvec
  566. },
  567. {
  568. .procname = "ipfrag_low_thresh",
  569. .data = &init_net.ipv4.frags.low_thresh,
  570. .maxlen = sizeof(int),
  571. .mode = 0644,
  572. .proc_handler = proc_dointvec
  573. },
  574. {
  575. .procname = "ipfrag_time",
  576. .data = &init_net.ipv4.frags.timeout,
  577. .maxlen = sizeof(int),
  578. .mode = 0644,
  579. .proc_handler = proc_dointvec_jiffies,
  580. },
  581. { }
  582. };
  583. static struct ctl_table ip4_frags_ctl_table[] = {
  584. {
  585. .procname = "ipfrag_secret_interval",
  586. .data = &ip4_frags.secret_interval,
  587. .maxlen = sizeof(int),
  588. .mode = 0644,
  589. .proc_handler = proc_dointvec_jiffies,
  590. },
  591. {
  592. .procname = "ipfrag_max_dist",
  593. .data = &sysctl_ipfrag_max_dist,
  594. .maxlen = sizeof(int),
  595. .mode = 0644,
  596. .proc_handler = proc_dointvec_minmax,
  597. .extra1 = &zero
  598. },
  599. { }
  600. };
  601. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  602. {
  603. struct ctl_table *table;
  604. struct ctl_table_header *hdr;
  605. table = ip4_frags_ns_ctl_table;
  606. if (!net_eq(net, &init_net)) {
  607. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  608. if (table == NULL)
  609. goto err_alloc;
  610. table[0].data = &net->ipv4.frags.high_thresh;
  611. table[1].data = &net->ipv4.frags.low_thresh;
  612. table[2].data = &net->ipv4.frags.timeout;
  613. }
  614. hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
  615. if (hdr == NULL)
  616. goto err_reg;
  617. net->ipv4.frags_hdr = hdr;
  618. return 0;
  619. err_reg:
  620. if (!net_eq(net, &init_net))
  621. kfree(table);
  622. err_alloc:
  623. return -ENOMEM;
  624. }
  625. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  626. {
  627. struct ctl_table *table;
  628. table = net->ipv4.frags_hdr->ctl_table_arg;
  629. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  630. kfree(table);
  631. }
  632. static void ip4_frags_ctl_register(void)
  633. {
  634. register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table);
  635. }
  636. #else
  637. static inline int ip4_frags_ns_ctl_register(struct net *net)
  638. {
  639. return 0;
  640. }
  641. static inline void ip4_frags_ns_ctl_unregister(struct net *net)
  642. {
  643. }
  644. static inline void ip4_frags_ctl_register(void)
  645. {
  646. }
  647. #endif
  648. static int __net_init ipv4_frags_init_net(struct net *net)
  649. {
  650. /*
  651. * Fragment cache limits. We will commit 256K at one time. Should we
  652. * cross that limit we will prune down to 192K. This should cope with
  653. * even the most extreme cases without allowing an attacker to
  654. * measurably harm machine performance.
  655. */
  656. net->ipv4.frags.high_thresh = 256 * 1024;
  657. net->ipv4.frags.low_thresh = 192 * 1024;
  658. /*
  659. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  660. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  661. * by TTL.
  662. */
  663. net->ipv4.frags.timeout = IP_FRAG_TIME;
  664. inet_frags_init_net(&net->ipv4.frags);
  665. return ip4_frags_ns_ctl_register(net);
  666. }
  667. static void __net_exit ipv4_frags_exit_net(struct net *net)
  668. {
  669. ip4_frags_ns_ctl_unregister(net);
  670. inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
  671. }
  672. static struct pernet_operations ip4_frags_ops = {
  673. .init = ipv4_frags_init_net,
  674. .exit = ipv4_frags_exit_net,
  675. };
  676. void __init ipfrag_init(void)
  677. {
  678. ip4_frags_ctl_register();
  679. register_pernet_subsys(&ip4_frags_ops);
  680. ip4_frags.hashfn = ip4_hashfn;
  681. ip4_frags.constructor = ip4_frag_init;
  682. ip4_frags.destructor = ip4_frag_free;
  683. ip4_frags.skb_free = NULL;
  684. ip4_frags.qsize = sizeof(struct ipq);
  685. ip4_frags.match = ip4_frag_match;
  686. ip4_frags.frag_expire = ip_expire;
  687. ip4_frags.secret_interval = 10 * 60 * HZ;
  688. inet_frags_init(&ip4_frags);
  689. }