PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/net/ipv6/reassembly.c

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