reassembly.c 15 KB

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