reassembly.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * IPv6 fragment reassembly
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on: net/ipv4/ip_fragment.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /*
  16. * Fixes:
  17. * Andi Kleen Make it work with multiple hosts.
  18. * More RFC compliance.
  19. *
  20. * Horst von Brand Add missing #include <linux/string.h>
  21. * Alexey Kuznetsov SMP races, threading, cleanup.
  22. * Patrick McHardy LRU queue of frag heads for evictor.
  23. * Mitsuru KANDA @USAGI Register inet6_protocol{}.
  24. * David Stevens and
  25. * YOSHIFUJI,H. @USAGI Always remove fragment header to
  26. * calculate ICV correctly.
  27. */
  28. #define pr_fmt(fmt) "IPv6: " fmt
  29. #include <linux/errno.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/socket.h>
  33. #include <linux/sockios.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/net.h>
  36. #include <linux/list.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/in6.h>
  39. #include <linux/ipv6.h>
  40. #include <linux/icmpv6.h>
  41. #include <linux/random.h>
  42. #include <linux/jhash.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/slab.h>
  45. #include <linux/export.h>
  46. #include <net/sock.h>
  47. #include <net/snmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/ip6_route.h>
  50. #include <net/protocol.h>
  51. #include <net/transp_v6.h>
  52. #include <net/rawv6.h>
  53. #include <net/ndisc.h>
  54. #include <net/addrconf.h>
  55. #include <net/ipv6_frag.h>
  56. #include <net/inet_ecn.h>
  57. static const char ip6_frag_cache_name[] = "ip6-frags";
  58. static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
  59. {
  60. return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
  61. }
  62. static struct inet_frags ip6_frags;
  63. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
  64. struct sk_buff *prev_tail, struct net_device *dev);
  65. static void ip6_frag_expire(struct timer_list *t)
  66. {
  67. struct inet_frag_queue *frag = from_timer(frag, t, timer);
  68. struct frag_queue *fq;
  69. struct net *net;
  70. fq = container_of(frag, struct frag_queue, q);
  71. net = container_of(fq->q.net, struct net, ipv6.frags);
  72. ip6frag_expire_frag_queue(net, fq);
  73. }
  74. static struct frag_queue *
  75. fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
  76. {
  77. struct frag_v6_compare_key key = {
  78. .id = id,
  79. .saddr = hdr->saddr,
  80. .daddr = hdr->daddr,
  81. .user = IP6_DEFRAG_LOCAL_DELIVER,
  82. .iif = iif,
  83. };
  84. struct inet_frag_queue *q;
  85. if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
  86. IPV6_ADDR_LINKLOCAL)))
  87. key.iif = 0;
  88. q = inet_frag_find(&net->ipv6.frags, &key);
  89. if (!q)
  90. return NULL;
  91. return container_of(q, struct frag_queue, q);
  92. }
  93. static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
  94. struct frag_hdr *fhdr, int nhoff,
  95. u32 *prob_offset)
  96. {
  97. struct net *net = dev_net(skb_dst(skb)->dev);
  98. int offset, end, fragsize;
  99. struct sk_buff *prev_tail;
  100. struct net_device *dev;
  101. int err = -ENOENT;
  102. u8 ecn;
  103. if (fq->q.flags & INET_FRAG_COMPLETE)
  104. goto err;
  105. err = -EINVAL;
  106. offset = ntohs(fhdr->frag_off) & ~0x7;
  107. end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
  108. ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
  109. if ((unsigned int)end > IPV6_MAXPLEN) {
  110. *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
  111. /* note that if prob_offset is set, the skb is freed elsewhere,
  112. * we do not free it here.
  113. */
  114. return -1;
  115. }
  116. ecn = ip6_frag_ecn(ipv6_hdr(skb));
  117. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  118. const unsigned char *nh = skb_network_header(skb);
  119. skb->csum = csum_sub(skb->csum,
  120. csum_partial(nh, (u8 *)(fhdr + 1) - nh,
  121. 0));
  122. }
  123. /* Is this the final fragment? */
  124. if (!(fhdr->frag_off & htons(IP6_MF))) {
  125. /* If we already have some bits beyond end
  126. * or have different end, the segment is corrupted.
  127. */
  128. if (end < fq->q.len ||
  129. ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
  130. goto discard_fq;
  131. fq->q.flags |= INET_FRAG_LAST_IN;
  132. fq->q.len = end;
  133. } else {
  134. /* Check if the fragment is rounded to 8 bytes.
  135. * Required by the RFC.
  136. */
  137. if (end & 0x7) {
  138. /* RFC2460 says always send parameter problem in
  139. * this case. -DaveM
  140. */
  141. *prob_offset = offsetof(struct ipv6hdr, payload_len);
  142. return -1;
  143. }
  144. if (end > fq->q.len) {
  145. /* Some bits beyond end -> corruption. */
  146. if (fq->q.flags & INET_FRAG_LAST_IN)
  147. goto discard_fq;
  148. fq->q.len = end;
  149. }
  150. }
  151. if (end == offset)
  152. goto discard_fq;
  153. err = -ENOMEM;
  154. /* Point into the IP datagram 'data' part. */
  155. if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
  156. goto discard_fq;
  157. err = pskb_trim_rcsum(skb, end - offset);
  158. if (err)
  159. goto discard_fq;
  160. /* Note : skb->rbnode and skb->dev share the same location. */
  161. dev = skb->dev;
  162. /* Makes sure compiler wont do silly aliasing games */
  163. barrier();
  164. prev_tail = fq->q.fragments_tail;
  165. err = inet_frag_queue_insert(&fq->q, skb, offset, end);
  166. if (err)
  167. goto insert_error;
  168. if (dev)
  169. fq->iif = dev->ifindex;
  170. fq->q.stamp = skb->tstamp;
  171. fq->q.meat += skb->len;
  172. fq->ecn |= ecn;
  173. add_frag_mem_limit(fq->q.net, skb->truesize);
  174. fragsize = -skb_network_offset(skb) + skb->len;
  175. if (fragsize > fq->q.max_size)
  176. fq->q.max_size = fragsize;
  177. /* The first fragment.
  178. * nhoffset is obtained from the first fragment, of course.
  179. */
  180. if (offset == 0) {
  181. fq->nhoffset = nhoff;
  182. fq->q.flags |= INET_FRAG_FIRST_IN;
  183. }
  184. if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  185. fq->q.meat == fq->q.len) {
  186. unsigned long orefdst = skb->_skb_refdst;
  187. skb->_skb_refdst = 0UL;
  188. err = ip6_frag_reasm(fq, skb, prev_tail, dev);
  189. skb->_skb_refdst = orefdst;
  190. return err;
  191. }
  192. skb_dst_drop(skb);
  193. return -EINPROGRESS;
  194. insert_error:
  195. if (err == IPFRAG_DUP) {
  196. kfree_skb(skb);
  197. return -EINVAL;
  198. }
  199. err = -EINVAL;
  200. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  201. IPSTATS_MIB_REASM_OVERLAPS);
  202. discard_fq:
  203. inet_frag_kill(&fq->q);
  204. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  205. IPSTATS_MIB_REASMFAILS);
  206. err:
  207. kfree_skb(skb);
  208. return err;
  209. }
  210. /*
  211. * Check if this packet is complete.
  212. *
  213. * It is called with locked fq, and caller must check that
  214. * queue is eligible for reassembly i.e. it is not COMPLETE,
  215. * the last and the first frames arrived and all the bits are here.
  216. */
  217. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
  218. struct sk_buff *prev_tail, struct net_device *dev)
  219. {
  220. struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
  221. unsigned int nhoff;
  222. void *reasm_data;
  223. int payload_len;
  224. u8 ecn;
  225. inet_frag_kill(&fq->q);
  226. ecn = ip_frag_ecn_table[fq->ecn];
  227. if (unlikely(ecn == 0xff))
  228. goto out_fail;
  229. reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
  230. if (!reasm_data)
  231. goto out_oom;
  232. payload_len = ((skb->data - skb_network_header(skb)) -
  233. sizeof(struct ipv6hdr) + fq->q.len -
  234. sizeof(struct frag_hdr));
  235. if (payload_len > IPV6_MAXPLEN)
  236. goto out_oversize;
  237. /* We have to remove fragment header from datagram and to relocate
  238. * header in order to calculate ICV correctly. */
  239. nhoff = fq->nhoffset;
  240. skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
  241. memmove(skb->head + sizeof(struct frag_hdr), skb->head,
  242. (skb->data - skb->head) - sizeof(struct frag_hdr));
  243. if (skb_mac_header_was_set(skb))
  244. skb->mac_header += sizeof(struct frag_hdr);
  245. skb->network_header += sizeof(struct frag_hdr);
  246. skb_reset_transport_header(skb);
  247. inet_frag_reasm_finish(&fq->q, skb, reasm_data);
  248. skb->dev = dev;
  249. ipv6_hdr(skb)->payload_len = htons(payload_len);
  250. ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
  251. IP6CB(skb)->nhoff = nhoff;
  252. IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
  253. IP6CB(skb)->frag_max_size = fq->q.max_size;
  254. /* Yes, and fold redundant checksum back. 8) */
  255. skb_postpush_rcsum(skb, skb_network_header(skb),
  256. skb_network_header_len(skb));
  257. rcu_read_lock();
  258. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
  259. rcu_read_unlock();
  260. fq->q.fragments = NULL;
  261. fq->q.rb_fragments = RB_ROOT;
  262. fq->q.fragments_tail = NULL;
  263. fq->q.last_run_head = NULL;
  264. return 1;
  265. out_oversize:
  266. net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
  267. goto out_fail;
  268. out_oom:
  269. net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
  270. out_fail:
  271. rcu_read_lock();
  272. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  273. rcu_read_unlock();
  274. inet_frag_kill(&fq->q);
  275. return -1;
  276. }
  277. static int ipv6_frag_rcv(struct sk_buff *skb)
  278. {
  279. struct frag_hdr *fhdr;
  280. struct frag_queue *fq;
  281. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  282. struct net *net = dev_net(skb_dst(skb)->dev);
  283. int iif;
  284. if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
  285. goto fail_hdr;
  286. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
  287. /* Jumbo payload inhibits frag. header */
  288. if (hdr->payload_len == 0)
  289. goto fail_hdr;
  290. if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
  291. sizeof(struct frag_hdr))))
  292. goto fail_hdr;
  293. hdr = ipv6_hdr(skb);
  294. fhdr = (struct frag_hdr *)skb_transport_header(skb);
  295. if (!(fhdr->frag_off & htons(0xFFF9))) {
  296. /* It is not a fragmented frame */
  297. skb->transport_header += sizeof(struct frag_hdr);
  298. __IP6_INC_STATS(net,
  299. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
  300. IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
  301. IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
  302. return 1;
  303. }
  304. iif = skb->dev ? skb->dev->ifindex : 0;
  305. fq = fq_find(net, fhdr->identification, hdr, iif);
  306. if (fq) {
  307. u32 prob_offset = 0;
  308. int ret;
  309. spin_lock(&fq->q.lock);
  310. fq->iif = iif;
  311. ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
  312. &prob_offset);
  313. spin_unlock(&fq->q.lock);
  314. inet_frag_put(&fq->q);
  315. if (prob_offset) {
  316. __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
  317. IPSTATS_MIB_INHDRERRORS);
  318. /* icmpv6_param_prob() calls kfree_skb(skb) */
  319. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
  320. }
  321. return ret;
  322. }
  323. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
  324. kfree_skb(skb);
  325. return -1;
  326. fail_hdr:
  327. __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
  328. IPSTATS_MIB_INHDRERRORS);
  329. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
  330. return -1;
  331. }
  332. static const struct inet6_protocol frag_protocol = {
  333. .handler = ipv6_frag_rcv,
  334. .flags = INET6_PROTO_NOPOLICY,
  335. };
  336. #ifdef CONFIG_SYSCTL
  337. static struct ctl_table ip6_frags_ns_ctl_table[] = {
  338. {
  339. .procname = "ip6frag_high_thresh",
  340. .data = &init_net.ipv6.frags.high_thresh,
  341. .maxlen = sizeof(unsigned long),
  342. .mode = 0644,
  343. .proc_handler = proc_doulongvec_minmax,
  344. .extra1 = &init_net.ipv6.frags.low_thresh
  345. },
  346. {
  347. .procname = "ip6frag_low_thresh",
  348. .data = &init_net.ipv6.frags.low_thresh,
  349. .maxlen = sizeof(unsigned long),
  350. .mode = 0644,
  351. .proc_handler = proc_doulongvec_minmax,
  352. .extra2 = &init_net.ipv6.frags.high_thresh
  353. },
  354. {
  355. .procname = "ip6frag_time",
  356. .data = &init_net.ipv6.frags.timeout,
  357. .maxlen = sizeof(int),
  358. .mode = 0644,
  359. .proc_handler = proc_dointvec_jiffies,
  360. },
  361. { }
  362. };
  363. /* secret interval has been deprecated */
  364. static int ip6_frags_secret_interval_unused;
  365. static struct ctl_table ip6_frags_ctl_table[] = {
  366. {
  367. .procname = "ip6frag_secret_interval",
  368. .data = &ip6_frags_secret_interval_unused,
  369. .maxlen = sizeof(int),
  370. .mode = 0644,
  371. .proc_handler = proc_dointvec_jiffies,
  372. },
  373. { }
  374. };
  375. static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
  376. {
  377. struct ctl_table *table;
  378. struct ctl_table_header *hdr;
  379. table = ip6_frags_ns_ctl_table;
  380. if (!net_eq(net, &init_net)) {
  381. table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
  382. if (!table)
  383. goto err_alloc;
  384. table[0].data = &net->ipv6.frags.high_thresh;
  385. table[0].extra1 = &net->ipv6.frags.low_thresh;
  386. table[0].extra2 = &init_net.ipv6.frags.high_thresh;
  387. table[1].data = &net->ipv6.frags.low_thresh;
  388. table[1].extra2 = &net->ipv6.frags.high_thresh;
  389. table[2].data = &net->ipv6.frags.timeout;
  390. }
  391. hdr = register_net_sysctl(net, "net/ipv6", table);
  392. if (!hdr)
  393. goto err_reg;
  394. net->ipv6.sysctl.frags_hdr = hdr;
  395. return 0;
  396. err_reg:
  397. if (!net_eq(net, &init_net))
  398. kfree(table);
  399. err_alloc:
  400. return -ENOMEM;
  401. }
  402. static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
  403. {
  404. struct ctl_table *table;
  405. table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
  406. unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
  407. if (!net_eq(net, &init_net))
  408. kfree(table);
  409. }
  410. static struct ctl_table_header *ip6_ctl_header;
  411. static int ip6_frags_sysctl_register(void)
  412. {
  413. ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
  414. ip6_frags_ctl_table);
  415. return ip6_ctl_header == NULL ? -ENOMEM : 0;
  416. }
  417. static void ip6_frags_sysctl_unregister(void)
  418. {
  419. unregister_net_sysctl_table(ip6_ctl_header);
  420. }
  421. #else
  422. static int ip6_frags_ns_sysctl_register(struct net *net)
  423. {
  424. return 0;
  425. }
  426. static void ip6_frags_ns_sysctl_unregister(struct net *net)
  427. {
  428. }
  429. static int ip6_frags_sysctl_register(void)
  430. {
  431. return 0;
  432. }
  433. static void ip6_frags_sysctl_unregister(void)
  434. {
  435. }
  436. #endif
  437. static int __net_init ipv6_frags_init_net(struct net *net)
  438. {
  439. int res;
  440. net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  441. net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  442. net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
  443. net->ipv6.frags.f = &ip6_frags;
  444. res = inet_frags_init_net(&net->ipv6.frags);
  445. if (res < 0)
  446. return res;
  447. res = ip6_frags_ns_sysctl_register(net);
  448. if (res < 0)
  449. inet_frags_exit_net(&net->ipv6.frags);
  450. return res;
  451. }
  452. static void __net_exit ipv6_frags_exit_net(struct net *net)
  453. {
  454. ip6_frags_ns_sysctl_unregister(net);
  455. inet_frags_exit_net(&net->ipv6.frags);
  456. }
  457. static struct pernet_operations ip6_frags_ops = {
  458. .init = ipv6_frags_init_net,
  459. .exit = ipv6_frags_exit_net,
  460. };
  461. static const struct rhashtable_params ip6_rhash_params = {
  462. .head_offset = offsetof(struct inet_frag_queue, node),
  463. .hashfn = ip6frag_key_hashfn,
  464. .obj_hashfn = ip6frag_obj_hashfn,
  465. .obj_cmpfn = ip6frag_obj_cmpfn,
  466. .automatic_shrinking = true,
  467. };
  468. int __init ipv6_frag_init(void)
  469. {
  470. int ret;
  471. ip6_frags.constructor = ip6frag_init;
  472. ip6_frags.destructor = NULL;
  473. ip6_frags.qsize = sizeof(struct frag_queue);
  474. ip6_frags.frag_expire = ip6_frag_expire;
  475. ip6_frags.frags_cache_name = ip6_frag_cache_name;
  476. ip6_frags.rhash_params = ip6_rhash_params;
  477. ret = inet_frags_init(&ip6_frags);
  478. if (ret)
  479. goto out;
  480. ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  481. if (ret)
  482. goto err_protocol;
  483. ret = ip6_frags_sysctl_register();
  484. if (ret)
  485. goto err_sysctl;
  486. ret = register_pernet_subsys(&ip6_frags_ops);
  487. if (ret)
  488. goto err_pernet;
  489. out:
  490. return ret;
  491. err_pernet:
  492. ip6_frags_sysctl_unregister();
  493. err_sysctl:
  494. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  495. err_protocol:
  496. inet_frags_fini(&ip6_frags);
  497. goto out;
  498. }
  499. void ipv6_frag_exit(void)
  500. {
  501. ip6_frags_sysctl_unregister();
  502. unregister_pernet_subsys(&ip6_frags_ops);
  503. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  504. inet_frags_fini(&ip6_frags);
  505. }