ip_fragment.c 21 KB

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