inet_frag.h 4.5 KB

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