ip_fragment.c 21 KB

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