ipv6_frag.h 3.3 KB

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