inet_frag.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_FRAG_H__
  3. #define __NET_FRAG_H__
  4. #include <linux/rhashtable-types.h>
  5. #include <linux/completion.h>
  6. #include <linux/in6.h>
  7. #include <linux/rbtree_types.h>
  8. #include <linux/refcount.h>
  9. /* Per netns frag queues directory */
  10. struct fqdir {
  11. /* sysctls */
  12. long high_thresh;
  13. long low_thresh;
  14. int timeout;
  15. int max_dist;
  16. struct inet_frags *f;
  17. struct net *net;
  18. bool dead;
  19. struct rhashtable rhashtable ____cacheline_aligned_in_smp;
  20. /* Keep atomic mem on separate cachelines in structs that include it */
  21. atomic_long_t mem ____cacheline_aligned_in_smp;
  22. struct work_struct destroy_work;
  23. struct llist_node free_list;
  24. };
  25. /**
  26. * fragment queue flags
  27. *
  28. * @INET_FRAG_FIRST_IN: first fragment has arrived
  29. * @INET_FRAG_LAST_IN: final fragment has arrived
  30. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  31. * @INET_FRAG_HASH_DEAD: inet_frag_kill() has not removed fq from rhashtable
  32. */
  33. enum {
  34. INET_FRAG_FIRST_IN = BIT(0),
  35. INET_FRAG_LAST_IN = BIT(1),
  36. INET_FRAG_COMPLETE = BIT(2),
  37. INET_FRAG_HASH_DEAD = BIT(3),
  38. };
  39. struct frag_v4_compare_key {
  40. __be32 saddr;
  41. __be32 daddr;
  42. u32 user;
  43. u32 vif;
  44. __be16 id;
  45. u16 protocol;
  46. };
  47. struct frag_v6_compare_key {
  48. struct in6_addr saddr;
  49. struct in6_addr daddr;
  50. u32 user;
  51. __be32 id;
  52. u32 iif;
  53. };
  54. /**
  55. * struct inet_frag_queue - fragment queue
  56. *
  57. * @node: rhash node
  58. * @key: keys identifying this frag.
  59. * @timer: queue expiration timer
  60. * @lock: spinlock protecting this frag
  61. * @refcnt: reference count of the queue
  62. * @rb_fragments: received fragments rb-tree root
  63. * @fragments_tail: received fragments tail
  64. * @last_run_head: the head of the last "run". see ip_fragment.c
  65. * @stamp: timestamp of the last received fragment
  66. * @len: total length of the original datagram
  67. * @meat: length of received fragments so far
  68. * @mono_delivery_time: stamp has a mono delivery time (EDT)
  69. * @flags: fragment queue flags
  70. * @max_size: maximum received fragment size
  71. * @fqdir: pointer to struct fqdir
  72. * @rcu: rcu head for freeing deferall
  73. */
  74. struct inet_frag_queue {
  75. struct rhash_head node;
  76. union {
  77. struct frag_v4_compare_key v4;
  78. struct frag_v6_compare_key v6;
  79. } key;
  80. struct timer_list timer;
  81. spinlock_t lock;
  82. refcount_t refcnt;
  83. struct rb_root rb_fragments;
  84. struct sk_buff *fragments_tail;
  85. struct sk_buff *last_run_head;
  86. ktime_t stamp;
  87. int len;
  88. int meat;
  89. u8 mono_delivery_time;
  90. __u8 flags;
  91. u16 max_size;
  92. struct fqdir *fqdir;
  93. struct rcu_head rcu;
  94. };
  95. struct inet_frags {
  96. unsigned int qsize;
  97. void (*constructor)(struct inet_frag_queue *q,
  98. const void *arg);
  99. void (*destructor)(struct inet_frag_queue *);
  100. void (*frag_expire)(struct timer_list *t);
  101. struct kmem_cache *frags_cachep;
  102. const char *frags_cache_name;
  103. struct rhashtable_params rhash_params;
  104. refcount_t refcnt;
  105. struct completion completion;
  106. };
  107. int inet_frags_init(struct inet_frags *);
  108. void inet_frags_fini(struct inet_frags *);
  109. int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net);
  110. static inline void fqdir_pre_exit(struct fqdir *fqdir)
  111. {
  112. /* Prevent creation of new frags.
  113. * Pairs with READ_ONCE() in inet_frag_find().
  114. */
  115. WRITE_ONCE(fqdir->high_thresh, 0);
  116. /* Pairs with READ_ONCE() in inet_frag_kill(), ip_expire()
  117. * and ip6frag_expire_frag_queue().
  118. */
  119. WRITE_ONCE(fqdir->dead, true);
  120. }
  121. void fqdir_exit(struct fqdir *fqdir);
  122. void inet_frag_kill(struct inet_frag_queue *q);
  123. void inet_frag_destroy(struct inet_frag_queue *q);
  124. struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key);
  125. /* Free all skbs in the queue; return the sum of their truesizes. */
  126. unsigned int inet_frag_rbtree_purge(struct rb_root *root);
  127. static inline void inet_frag_put(struct inet_frag_queue *q)
  128. {
  129. if (refcount_dec_and_test(&q->refcnt))
  130. inet_frag_destroy(q);
  131. }
  132. /* Memory Tracking Functions. */
  133. static inline long frag_mem_limit(const struct fqdir *fqdir)
  134. {
  135. return atomic_long_read(&fqdir->mem);
  136. }
  137. static inline void sub_frag_mem_limit(struct fqdir *fqdir, long val)
  138. {
  139. atomic_long_sub(val, &fqdir->mem);
  140. }
  141. static inline void add_frag_mem_limit(struct fqdir *fqdir, long val)
  142. {
  143. atomic_long_add(val, &fqdir->mem);
  144. }
  145. /* RFC 3168 support :
  146. * We want to check ECN values of all fragments, do detect invalid combinations.
  147. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  148. */
  149. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  150. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  151. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  152. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  153. extern const u8 ip_frag_ecn_table[16];
  154. /* Return values of inet_frag_queue_insert() */
  155. #define IPFRAG_OK 0
  156. #define IPFRAG_DUP 1
  157. #define IPFRAG_OVERLAP 2
  158. int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
  159. int offset, int end);
  160. void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
  161. struct sk_buff *parent);
  162. void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
  163. void *reasm_data, bool try_coalesce);
  164. struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
  165. #endif