ip_fragment.c 21 KB

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