ip_fragment.c 21 KB

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