ipv6_frag.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _IPV6_FRAG_H
  3. #define _IPV6_FRAG_H
  4. #include <linux/kernel.h>
  5. #include <net/addrconf.h>
  6. #include <net/ipv6.h>
  7. #include <net/inet_frag.h>
  8. enum ip6_defrag_users {
  9. IP6_DEFRAG_LOCAL_DELIVER,
  10. IP6_DEFRAG_CONNTRACK_IN,
  11. __IP6_DEFRAG_CONNTRACK_IN = IP6_DEFRAG_CONNTRACK_IN + USHRT_MAX,
  12. IP6_DEFRAG_CONNTRACK_OUT,
  13. __IP6_DEFRAG_CONNTRACK_OUT = IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
  14. IP6_DEFRAG_CONNTRACK_BRIDGE_IN,
  15. __IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
  16. };
  17. /*
  18. * Equivalent of ipv4 struct ip
  19. */
  20. struct frag_queue {
  21. struct inet_frag_queue q;
  22. int iif;
  23. __u16 nhoffset;
  24. u8 ecn;
  25. };
  26. #if IS_ENABLED(CONFIG_IPV6)
  27. static inline void ip6frag_init(struct inet_frag_queue *q, const void *a)
  28. {
  29. struct frag_queue *fq = container_of(q, struct frag_queue, q);
  30. const struct frag_v6_compare_key *key = a;
  31. q->key.v6 = *key;
  32. fq->ecn = 0;
  33. }
  34. static inline u32 ip6frag_key_hashfn(const void *data, u32 len, u32 seed)
  35. {
  36. return jhash2(data,
  37. sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
  38. }
  39. static inline u32 ip6frag_obj_hashfn(const void *data, u32 len, u32 seed)
  40. {
  41. const struct inet_frag_queue *fq = data;
  42. return jhash2((const u32 *)&fq->key.v6,
  43. sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
  44. }
  45. static inline int
  46. ip6frag_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  47. {
  48. const struct frag_v6_compare_key *key = arg->key;
  49. const struct inet_frag_queue *fq = ptr;
  50. return !!memcmp(&fq->key, key, sizeof(*key));
  51. }
  52. static inline void
  53. ip6frag_expire_frag_queue(struct net *net, struct frag_queue *fq)
  54. {
  55. struct net_device *dev = NULL;
  56. struct sk_buff *head;
  57. rcu_read_lock();
  58. spin_lock(&fq->q.lock);
  59. if (fq->q.flags & INET_FRAG_COMPLETE)
  60. goto out;
  61. inet_frag_kill(&fq->q);
  62. dev = dev_get_by_index_rcu(net, fq->iif);
  63. if (!dev)
  64. goto out;
  65. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  66. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
  67. /* Don't send error if the first segment did not arrive. */
  68. if (!(fq->q.flags & INET_FRAG_FIRST_IN))
  69. goto out;
  70. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  71. * pull the head out of the tree in order to be able to
  72. * deal with head->dev.
  73. */
  74. head = inet_frag_pull_head(&fq->q);
  75. if (!head)
  76. goto out;
  77. head->dev = dev;
  78. spin_unlock(&fq->q.lock);
  79. icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
  80. kfree_skb(head);
  81. goto out_rcu_unlock;
  82. out:
  83. spin_unlock(&fq->q.lock);
  84. out_rcu_unlock:
  85. rcu_read_unlock();
  86. inet_frag_put(&fq->q);
  87. }
  88. #endif
  89. #endif