ip_fragment.c 21 KB

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