ipv6_frag.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. int refs = 1;
  59. rcu_read_lock();
  60. spin_lock(&fq->q.lock);
  61. if (fq->q.flags & INET_FRAG_COMPLETE)
  62. goto out;
  63. fq->q.flags |= INET_FRAG_DROP;
  64. inet_frag_kill(&fq->q, &refs);
  65. /* Paired with the WRITE_ONCE() in fqdir_pre_exit(). */
  66. if (READ_ONCE(fq->q.fqdir->dead)) {
  67. inet_frag_queue_flush(&fq->q, 0);
  68. goto out;
  69. }
  70. dev = dev_get_by_index_rcu(net, fq->iif);
  71. if (!dev)
  72. goto out;
  73. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  74. __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
  75. /* Don't send error if the first segment did not arrive. */
  76. if (!(fq->q.flags & INET_FRAG_FIRST_IN))
  77. goto out;
  78. /* sk_buff::dev and sk_buff::rbnode are unionized. So we
  79. * pull the head out of the tree in order to be able to
  80. * deal with head->dev.
  81. */
  82. head = inet_frag_pull_head(&fq->q);
  83. if (!head)
  84. goto out;
  85. head->dev = dev;
  86. spin_unlock(&fq->q.lock);
  87. icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
  88. kfree_skb_reason(head, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
  89. goto out_rcu_unlock;
  90. out:
  91. spin_unlock(&fq->q.lock);
  92. out_rcu_unlock:
  93. rcu_read_unlock();
  94. inet_frag_putn(&fq->q, refs);
  95. }
  96. /* Check if the upper layer header is truncated in the first fragment. */
  97. static inline bool
  98. ipv6frag_thdr_truncated(struct sk_buff *skb, int start, u8 *nexthdrp)
  99. {
  100. u8 nexthdr = *nexthdrp;
  101. __be16 frag_off;
  102. int offset;
  103. offset = ipv6_skip_exthdr(skb, start, &nexthdr, &frag_off);
  104. if (offset < 0 || (frag_off & htons(IP6_OFFSET)))
  105. return false;
  106. switch (nexthdr) {
  107. case NEXTHDR_TCP:
  108. offset += sizeof(struct tcphdr);
  109. break;
  110. case NEXTHDR_UDP:
  111. offset += sizeof(struct udphdr);
  112. break;
  113. case NEXTHDR_ICMP:
  114. offset += sizeof(struct icmp6hdr);
  115. break;
  116. default:
  117. offset += 1;
  118. }
  119. if (offset > skb->len)
  120. return true;
  121. return false;
  122. }
  123. #endif
  124. #endif