ipv6_frag.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _IPV6_FRAG_H
  3. #define _IPV6_FRAG_H
  4. #include <linux/icmpv6.h>
  5. #include <linux/kernel.h>
  6. #include <net/addrconf.h>
  7. #include <net/ipv6.h>
  8. #include <net/inet_frag.h>
  9. enum ip6_defrag_users {
  10. IP6_DEFRAG_LOCAL_DELIVER,
  11. IP6_DEFRAG_CONNTRACK_IN,
  12. __IP6_DEFRAG_CONNTRACK_IN = IP6_DEFRAG_CONNTRACK_IN + USHRT_MAX,
  13. IP6_DEFRAG_CONNTRACK_OUT,
  14. __IP6_DEFRAG_CONNTRACK_OUT = IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
  15. IP6_DEFRAG_CONNTRACK_BRIDGE_IN,
  16. __IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
  17. };
  18. /*
  19. * Equivalent of ipv4 struct ip
  20. */
  21. struct frag_queue {
  22. struct inet_frag_queue q;
  23. int iif;
  24. __u16 nhoffset;
  25. u8 ecn;
  26. };
  27. #if IS_ENABLED(CONFIG_IPV6)
  28. static inline void ip6frag_init(struct inet_frag_queue *q, const void *a)
  29. {
  30. struct frag_queue *fq = container_of(q, struct frag_queue, q);
  31. const struct frag_v6_compare_key *key = a;
  32. q->key.v6 = *key;
  33. fq->ecn = 0;
  34. }
  35. static inline u32 ip6frag_key_hashfn(const void *data, u32 len, u32 seed)
  36. {
  37. return jhash2(data,
  38. sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
  39. }
  40. static inline u32 ip6frag_obj_hashfn(const void *data, u32 len, u32 seed)
  41. {
  42. const struct inet_frag_queue *fq = data;
  43. return jhash2((const u32 *)&fq->key.v6,
  44. sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
  45. }
  46. static inline int
  47. ip6frag_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
  48. {
  49. const struct frag_v6_compare_key *key = arg->key;
  50. const struct inet_frag_queue *fq = ptr;
  51. return !!memcmp(&fq->key, key, sizeof(*key));
  52. }
  53. static inline void
  54. ip6frag_expire_frag_queue(struct net *net, struct frag_queue *fq)
  55. {
  56. struct net_device *dev = NULL;
  57. struct sk_buff *head;
  58. rcu_read_lock();
  59. /* Paired with the WRITE_ONCE() in fqdir_pre_exit(). */
  60. if (READ_ONCE(fq->q.fqdir->dead))
  61. goto out_rcu_unlock;
  62. spin_lock(&fq->q.lock);
  63. if (fq->q.flags & INET_FRAG_COMPLETE)
  64. goto out;
  65. fq->q.flags |= INET_FRAG_DROP;
  66. inet_frag_kill(&fq->q);
  67. dev = dev_get_by_index_rcu(net, fq->iif);
  68. if (!dev)
  69. goto out;
  70. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  71. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
  72. /* Don't send error if the first segment did not arrive. */
  73. if (!(fq->q.flags & INET_FRAG_FIRST_IN))
  74. goto out;
  75. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  76. * pull the head out of the tree in order to be able to
  77. * deal with head->dev.
  78. */
  79. head = inet_frag_pull_head(&fq->q);
  80. if (!head)
  81. goto out;
  82. head->dev = dev;
  83. spin_unlock(&fq->q.lock);
  84. icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
  85. kfree_skb_reason(head, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
  86. goto out_rcu_unlock;
  87. out:
  88. spin_unlock(&fq->q.lock);
  89. out_rcu_unlock:
  90. rcu_read_unlock();
  91. inet_frag_put(&fq->q);
  92. }
  93. /* Check if the upper layer header is truncated in the first fragment. */
  94. static inline bool
  95. ipv6frag_thdr_truncated(struct sk_buff *skb, int start, u8 *nexthdrp)
  96. {
  97. u8 nexthdr = *nexthdrp;
  98. __be16 frag_off;
  99. int offset;
  100. offset = ipv6_skip_exthdr(skb, start, &nexthdr, &frag_off);
  101. if (offset < 0 || (frag_off & htons(IP6_OFFSET)))
  102. return false;
  103. switch (nexthdr) {
  104. case NEXTHDR_TCP:
  105. offset += sizeof(struct tcphdr);
  106. break;
  107. case NEXTHDR_UDP:
  108. offset += sizeof(struct udphdr);
  109. break;
  110. case NEXTHDR_ICMP:
  111. offset += sizeof(struct icmp6hdr);
  112. break;
  113. default:
  114. offset += 1;
  115. }
  116. if (offset > skb->len)
  117. return true;
  118. return false;
  119. }
  120. #endif
  121. #endif