ip_fragment.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 sum_frag_mem_limit(&net->ipv4.frags);
  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 bool 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. static void ip4_frag_init(struct inet_frag_queue *q, void *a)
  142. {
  143. struct ipq *qp = container_of(q, struct ipq, q);
  144. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  145. frags);
  146. struct net *net = container_of(ipv4, struct net, ipv4);
  147. struct ip4_create_arg *arg = a;
  148. qp->protocol = arg->iph->protocol;
  149. qp->id = arg->iph->id;
  150. qp->ecn = ip4_frag_ecn(arg->iph->tos);
  151. qp->saddr = arg->iph->saddr;
  152. qp->daddr = arg->iph->daddr;
  153. qp->user = arg->user;
  154. qp->peer = sysctl_ipfrag_max_dist ?
  155. inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
  156. }
  157. static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
  158. {
  159. struct ipq *qp;
  160. qp = container_of(q, struct ipq, q);
  161. if (qp->peer)
  162. inet_putpeer(qp->peer);
  163. }
  164. /* Destruction primitives. */
  165. static __inline__ void ipq_put(struct ipq *ipq)
  166. {
  167. inet_frag_put(&ipq->q, &ip4_frags);
  168. }
  169. /* Kill ipq entry. It is not destroyed immediately,
  170. * because caller (and someone more) holds reference count.
  171. */
  172. static void ipq_kill(struct ipq *ipq)
  173. {
  174. inet_frag_kill(&ipq->q, &ip4_frags);
  175. }
  176. /* Memory limiting on fragments. Evictor trashes the oldest
  177. * fragment queue until we are back under the threshold.
  178. */
  179. static void ip_evictor(struct net *net)
  180. {
  181. int evicted;
  182. evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags, false);
  183. if (evicted)
  184. IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
  185. }
  186. /*
  187. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  188. */
  189. static void ip_expire(unsigned long arg)
  190. {
  191. struct ipq *qp;
  192. struct net *net;
  193. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  194. net = container_of(qp->q.net, struct net, ipv4.frags);
  195. spin_lock(&qp->q.lock);
  196. if (qp->q.last_in & INET_FRAG_COMPLETE)
  197. goto out;
  198. ipq_kill(qp);
  199. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
  200. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  201. if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
  202. struct sk_buff *head = qp->q.fragments;
  203. const struct iphdr *iph;
  204. int err;
  205. rcu_read_lock();
  206. head->dev = dev_get_by_index_rcu(net, qp->iif);
  207. if (!head->dev)
  208. goto out_rcu_unlock;
  209. /* skb dst is stale, drop it, and perform route lookup again */
  210. skb_dst_drop(head);
  211. iph = ip_hdr(head);
  212. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  213. iph->tos, head->dev);
  214. if (err)
  215. goto out_rcu_unlock;
  216. /*
  217. * Only an end host needs to send an ICMP
  218. * "Fragment Reassembly Timeout" message, per RFC792.
  219. */
  220. if (qp->user == IP_DEFRAG_AF_PACKET ||
  221. (qp->user == IP_DEFRAG_CONNTRACK_IN &&
  222. skb_rtable(head)->rt_type != RTN_LOCAL))
  223. goto out_rcu_unlock;
  224. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  225. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  226. out_rcu_unlock:
  227. rcu_read_unlock();
  228. }
  229. out:
  230. spin_unlock(&qp->q.lock);
  231. ipq_put(qp);
  232. }
  233. /* Find the correct entry in the "incomplete datagrams" queue for
  234. * this IP datagram, and create new one, if nothing is found.
  235. */
  236. static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
  237. {
  238. struct inet_frag_queue *q;
  239. struct ip4_create_arg arg;
  240. unsigned int hash;
  241. arg.iph = iph;
  242. arg.user = user;
  243. read_lock(&ip4_frags.lock);
  244. hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
  245. q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
  246. if (q == NULL)
  247. goto out_nomem;
  248. return container_of(q, struct ipq, q);
  249. out_nomem:
  250. LIMIT_NETDEBUG(KERN_ERR pr_fmt("ip_frag_create: no memory left !\n"));
  251. return NULL;
  252. }
  253. /* Is the fragment too far ahead to be part of ipq? */
  254. static inline int ip_frag_too_far(struct ipq *qp)
  255. {
  256. struct inet_peer *peer = qp->peer;
  257. unsigned int max = sysctl_ipfrag_max_dist;
  258. unsigned int start, end;
  259. int rc;
  260. if (!peer || !max)
  261. return 0;
  262. start = qp->rid;
  263. end = atomic_inc_return(&peer->rid);
  264. qp->rid = end;
  265. rc = qp->q.fragments && (end - start) > max;
  266. if (rc) {
  267. struct net *net;
  268. net = container_of(qp->q.net, struct net, ipv4.frags);
  269. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  270. }
  271. return rc;
  272. }
  273. static int ip_frag_reinit(struct ipq *qp)
  274. {
  275. struct sk_buff *fp;
  276. unsigned int sum_truesize = 0;
  277. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  278. atomic_inc(&qp->q.refcnt);
  279. return -ETIMEDOUT;
  280. }
  281. fp = qp->q.fragments;
  282. do {
  283. struct sk_buff *xp = fp->next;
  284. sum_truesize += fp->truesize;
  285. kfree_skb(fp);
  286. fp = xp;
  287. } while (fp);
  288. sub_frag_mem_limit(&qp->q, sum_truesize);
  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. sub_frag_mem_limit(&qp->q, free_it->truesize);
  415. kfree_skb(free_it);
  416. }
  417. }
  418. FRAG_CB(skb)->offset = offset;
  419. /* Insert this fragment in the chain of fragments. */
  420. skb->next = next;
  421. if (!next)
  422. qp->q.fragments_tail = skb;
  423. if (prev)
  424. prev->next = skb;
  425. else
  426. qp->q.fragments = skb;
  427. dev = skb->dev;
  428. if (dev) {
  429. qp->iif = dev->ifindex;
  430. skb->dev = NULL;
  431. }
  432. qp->q.stamp = skb->tstamp;
  433. qp->q.meat += skb->len;
  434. qp->ecn |= ecn;
  435. add_frag_mem_limit(&qp->q, skb->truesize);
  436. if (offset == 0)
  437. qp->q.last_in |= INET_FRAG_FIRST_IN;
  438. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  439. skb->len + ihl > qp->q.max_size)
  440. qp->q.max_size = skb->len + ihl;
  441. if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  442. qp->q.meat == qp->q.len)
  443. return ip_frag_reasm(qp, prev, dev);
  444. write_lock(&ip4_frags.lock);
  445. list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
  446. write_unlock(&ip4_frags.lock);
  447. return -EINPROGRESS;
  448. err:
  449. kfree_skb(skb);
  450. return err;
  451. }
  452. /* Build a new IP datagram from all its fragments. */
  453. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  454. struct net_device *dev)
  455. {
  456. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  457. struct iphdr *iph;
  458. struct sk_buff *fp, *head = qp->q.fragments;
  459. int len;
  460. int ihlen;
  461. int err;
  462. int sum_truesize;
  463. u8 ecn;
  464. ipq_kill(qp);
  465. ecn = ip4_frag_ecn_table[qp->ecn];
  466. if (unlikely(ecn == 0xff)) {
  467. err = -EINVAL;
  468. goto out_fail;
  469. }
  470. /* Make the one we just received the head. */
  471. if (prev) {
  472. head = prev->next;
  473. fp = skb_clone(head, GFP_ATOMIC);
  474. if (!fp)
  475. goto out_nomem;
  476. fp->next = head->next;
  477. if (!fp->next)
  478. qp->q.fragments_tail = fp;
  479. prev->next = fp;
  480. skb_morph(head, qp->q.fragments);
  481. head->next = qp->q.fragments->next;
  482. consume_skb(qp->q.fragments);
  483. qp->q.fragments = head;
  484. }
  485. WARN_ON(head == NULL);
  486. WARN_ON(FRAG_CB(head)->offset != 0);
  487. /* Allocate a new buffer for the datagram. */
  488. ihlen = ip_hdrlen(head);
  489. len = ihlen + qp->q.len;
  490. err = -E2BIG;
  491. if (len > 65535)
  492. goto out_oversize;
  493. /* Head of list must not be cloned. */
  494. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  495. goto out_nomem;
  496. /* If the first fragment is fragmented itself, we split
  497. * it to two chunks: the first with data and paged part
  498. * and the second, holding only fragments. */
  499. if (skb_has_frag_list(head)) {
  500. struct sk_buff *clone;
  501. int i, plen = 0;
  502. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  503. goto out_nomem;
  504. clone->next = head->next;
  505. head->next = clone;
  506. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  507. skb_frag_list_init(head);
  508. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  509. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  510. clone->len = clone->data_len = head->data_len - plen;
  511. head->data_len -= clone->len;
  512. head->len -= clone->len;
  513. clone->csum = 0;
  514. clone->ip_summed = head->ip_summed;
  515. add_frag_mem_limit(&qp->q, clone->truesize);
  516. }
  517. skb_push(head, head->data - skb_network_header(head));
  518. sum_truesize = head->truesize;
  519. for (fp = head->next; fp;) {
  520. bool headstolen;
  521. int delta;
  522. struct sk_buff *next = fp->next;
  523. sum_truesize += fp->truesize;
  524. if (head->ip_summed != fp->ip_summed)
  525. head->ip_summed = CHECKSUM_NONE;
  526. else if (head->ip_summed == CHECKSUM_COMPLETE)
  527. head->csum = csum_add(head->csum, fp->csum);
  528. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  529. kfree_skb_partial(fp, headstolen);
  530. } else {
  531. if (!skb_shinfo(head)->frag_list)
  532. skb_shinfo(head)->frag_list = fp;
  533. head->data_len += fp->len;
  534. head->len += fp->len;
  535. head->truesize += fp->truesize;
  536. }
  537. fp = next;
  538. }
  539. sub_frag_mem_limit(&qp->q, sum_truesize);
  540. head->next = NULL;
  541. head->dev = dev;
  542. head->tstamp = qp->q.stamp;
  543. IPCB(head)->frag_max_size = qp->q.max_size;
  544. iph = ip_hdr(head);
  545. /* max_size != 0 implies at least one fragment had IP_DF set */
  546. iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0;
  547. iph->tot_len = htons(len);
  548. iph->tos |= ecn;
  549. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
  550. qp->q.fragments = NULL;
  551. qp->q.fragments_tail = NULL;
  552. return 0;
  553. out_nomem:
  554. LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"),
  555. qp);
  556. err = -ENOMEM;
  557. goto out_fail;
  558. out_oversize:
  559. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
  560. out_fail:
  561. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  562. return err;
  563. }
  564. /* Process an incoming IP datagram fragment. */
  565. int ip_defrag(struct sk_buff *skb, u32 user)
  566. {
  567. struct ipq *qp;
  568. struct net *net;
  569. net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
  570. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
  571. /* Start by cleaning up the memory. */
  572. ip_evictor(net);
  573. /* Lookup (or create) queue header */
  574. if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
  575. int ret;
  576. spin_lock(&qp->q.lock);
  577. ret = ip_frag_queue(qp, skb);
  578. spin_unlock(&qp->q.lock);
  579. ipq_put(qp);
  580. return ret;
  581. }
  582. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  583. kfree_skb(skb);
  584. return -ENOMEM;
  585. }
  586. EXPORT_SYMBOL(ip_defrag);
  587. struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user)
  588. {
  589. struct iphdr iph;
  590. u32 len;
  591. if (skb->protocol != htons(ETH_P_IP))
  592. return skb;
  593. if (!skb_copy_bits(skb, 0, &iph, sizeof(iph)))
  594. return skb;
  595. if (iph.ihl < 5 || iph.version != 4)
  596. return skb;
  597. len = ntohs(iph.tot_len);
  598. if (skb->len < len || len < (iph.ihl * 4))
  599. return skb;
  600. if (ip_is_fragment(&iph)) {
  601. skb = skb_share_check(skb, GFP_ATOMIC);
  602. if (skb) {
  603. if (!pskb_may_pull(skb, iph.ihl*4))
  604. return skb;
  605. if (pskb_trim_rcsum(skb, len))
  606. return skb;
  607. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  608. if (ip_defrag(skb, user))
  609. return NULL;
  610. skb->rxhash = 0;
  611. }
  612. }
  613. return skb;
  614. }
  615. EXPORT_SYMBOL(ip_check_defrag);
  616. #ifdef CONFIG_SYSCTL
  617. static int zero;
  618. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  619. {
  620. .procname = "ipfrag_high_thresh",
  621. .data = &init_net.ipv4.frags.high_thresh,
  622. .maxlen = sizeof(int),
  623. .mode = 0644,
  624. .proc_handler = proc_dointvec
  625. },
  626. {
  627. .procname = "ipfrag_low_thresh",
  628. .data = &init_net.ipv4.frags.low_thresh,
  629. .maxlen = sizeof(int),
  630. .mode = 0644,
  631. .proc_handler = proc_dointvec
  632. },
  633. {
  634. .procname = "ipfrag_time",
  635. .data = &init_net.ipv4.frags.timeout,
  636. .maxlen = sizeof(int),
  637. .mode = 0644,
  638. .proc_handler = proc_dointvec_jiffies,
  639. },
  640. { }
  641. };
  642. static struct ctl_table ip4_frags_ctl_table[] = {
  643. {
  644. .procname = "ipfrag_secret_interval",
  645. .data = &ip4_frags.secret_interval,
  646. .maxlen = sizeof(int),
  647. .mode = 0644,
  648. .proc_handler = proc_dointvec_jiffies,
  649. },
  650. {
  651. .procname = "ipfrag_max_dist",
  652. .data = &sysctl_ipfrag_max_dist,
  653. .maxlen = sizeof(int),
  654. .mode = 0644,
  655. .proc_handler = proc_dointvec_minmax,
  656. .extra1 = &zero
  657. },
  658. { }
  659. };
  660. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  661. {
  662. struct ctl_table *table;
  663. struct ctl_table_header *hdr;
  664. table = ip4_frags_ns_ctl_table;
  665. if (!net_eq(net, &init_net)) {
  666. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  667. if (table == NULL)
  668. goto err_alloc;
  669. table[0].data = &net->ipv4.frags.high_thresh;
  670. table[1].data = &net->ipv4.frags.low_thresh;
  671. table[2].data = &net->ipv4.frags.timeout;
  672. /* Don't export sysctls to unprivileged users */
  673. if (net->user_ns != &init_user_ns)
  674. table[0].procname = NULL;
  675. }
  676. hdr = register_net_sysctl(net, "net/ipv4", table);
  677. if (hdr == NULL)
  678. goto err_reg;
  679. net->ipv4.frags_hdr = hdr;
  680. return 0;
  681. err_reg:
  682. if (!net_eq(net, &init_net))
  683. kfree(table);
  684. err_alloc:
  685. return -ENOMEM;
  686. }
  687. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  688. {
  689. struct ctl_table *table;
  690. table = net->ipv4.frags_hdr->ctl_table_arg;
  691. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  692. kfree(table);
  693. }
  694. static void ip4_frags_ctl_register(void)
  695. {
  696. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  697. }
  698. #else
  699. static inline int ip4_frags_ns_ctl_register(struct net *net)
  700. {
  701. return 0;
  702. }
  703. static inline void ip4_frags_ns_ctl_unregister(struct net *net)
  704. {
  705. }
  706. static inline void ip4_frags_ctl_register(void)
  707. {
  708. }
  709. #endif
  710. static int __net_init ipv4_frags_init_net(struct net *net)
  711. {
  712. /* Fragment cache limits.
  713. *
  714. * The fragment memory accounting code, (tries to) account for
  715. * the real memory usage, by measuring both the size of frag
  716. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  717. * and the SKB's truesize.
  718. *
  719. * A 64K fragment consumes 129736 bytes (44*2944)+200
  720. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  721. *
  722. * We will commit 4MB at one time. Should we cross that limit
  723. * we will prune down to 3MB, making room for approx 8 big 64K
  724. * fragments 8x128k.
  725. */
  726. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  727. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  728. /*
  729. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  730. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  731. * by TTL.
  732. */
  733. net->ipv4.frags.timeout = IP_FRAG_TIME;
  734. inet_frags_init_net(&net->ipv4.frags);
  735. return ip4_frags_ns_ctl_register(net);
  736. }
  737. static void __net_exit ipv4_frags_exit_net(struct net *net)
  738. {
  739. ip4_frags_ns_ctl_unregister(net);
  740. inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
  741. }
  742. static struct pernet_operations ip4_frags_ops = {
  743. .init = ipv4_frags_init_net,
  744. .exit = ipv4_frags_exit_net,
  745. };
  746. void __init ipfrag_init(void)
  747. {
  748. ip4_frags_ctl_register();
  749. register_pernet_subsys(&ip4_frags_ops);
  750. ip4_frags.hashfn = ip4_hashfn;
  751. ip4_frags.constructor = ip4_frag_init;
  752. ip4_frags.destructor = ip4_frag_free;
  753. ip4_frags.skb_free = NULL;
  754. ip4_frags.qsize = sizeof(struct ipq);
  755. ip4_frags.match = ip4_frag_match;
  756. ip4_frags.frag_expire = ip_expire;
  757. ip4_frags.secret_interval = 10 * 60 * HZ;
  758. inet_frags_init(&ip4_frags);
  759. }