ip_fragment.c 20 KB

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