inet_frag.h 4.9 KB

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